blob: 0576be36cd6894d937458d431dbc2714930c9fa6 [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,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070054};
55
56/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010062 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010071 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070072 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_XBGR2101010,
Matt Roper465c1202014-05-29 08:06:54 -070075};
76
Matt Roper3d7d6512014-06-10 08:28:13 -070077/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
Chris Wilson6b383a72010-09-13 13:54:26 +010082static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080083
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020085 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030086static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020087 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020089static int intel_set_mode(struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080090static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020094static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020096static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070097 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020099static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200102static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200103 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200104static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200105 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800106static void intel_begin_crtc_commit(struct drm_crtc *crtc);
107static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700108static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
109 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200110static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
111 int num_connectors);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +0300112static void intel_crtc_enable_planes(struct drm_crtc *crtc);
113static void intel_crtc_disable_planes(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100114
Dave Airlie0e32b392014-05-02 14:02:48 +1000115static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
116{
117 if (!connector->mst_port)
118 return connector->encoder;
119 else
120 return &connector->mst_port->mst_encoders[pipe]->base;
121}
122
Jesse Barnes79e53942008-11-07 14:24:08 -0800123typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400124 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800125} intel_range_t;
126
127typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400128 int dot_limit;
129 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800130} intel_p2_t;
131
Ma Lingd4906092009-03-18 20:13:27 +0800132typedef struct intel_limit intel_limit_t;
133struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400134 intel_range_t dot, vco, n, m, m1, m2, p, p1;
135 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800136};
Jesse Barnes79e53942008-11-07 14:24:08 -0800137
Daniel Vetterd2acd212012-10-20 20:57:43 +0200138int
139intel_pch_rawclk(struct drm_device *dev)
140{
141 struct drm_i915_private *dev_priv = dev->dev_private;
142
143 WARN_ON(!HAS_PCH_SPLIT(dev));
144
145 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
146}
147
Chris Wilson021357a2010-09-07 20:54:59 +0100148static inline u32 /* units of 100MHz */
149intel_fdi_link_freq(struct drm_device *dev)
150{
Chris Wilson8b99e682010-10-13 09:59:17 +0100151 if (IS_GEN5(dev)) {
152 struct drm_i915_private *dev_priv = dev->dev_private;
153 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
154 } else
155 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100156}
157
Daniel Vetter5d536e22013-07-06 12:52:06 +0200158static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400159 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200160 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200161 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400162 .m = { .min = 96, .max = 140 },
163 .m1 = { .min = 18, .max = 26 },
164 .m2 = { .min = 6, .max = 16 },
165 .p = { .min = 4, .max = 128 },
166 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700167 .p2 = { .dot_limit = 165000,
168 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700169};
170
Daniel Vetter5d536e22013-07-06 12:52:06 +0200171static const intel_limit_t intel_limits_i8xx_dvo = {
172 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200173 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200174 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200175 .m = { .min = 96, .max = 140 },
176 .m1 = { .min = 18, .max = 26 },
177 .m2 = { .min = 6, .max = 16 },
178 .p = { .min = 4, .max = 128 },
179 .p1 = { .min = 2, .max = 33 },
180 .p2 = { .dot_limit = 165000,
181 .p2_slow = 4, .p2_fast = 4 },
182};
183
Keith Packarde4b36692009-06-05 19:22:17 -0700184static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400185 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200186 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200187 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400188 .m = { .min = 96, .max = 140 },
189 .m1 = { .min = 18, .max = 26 },
190 .m2 = { .min = 6, .max = 16 },
191 .p = { .min = 4, .max = 128 },
192 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700193 .p2 = { .dot_limit = 165000,
194 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700195};
Eric Anholt273e27c2011-03-30 13:01:10 -0700196
Keith Packarde4b36692009-06-05 19:22:17 -0700197static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400198 .dot = { .min = 20000, .max = 400000 },
199 .vco = { .min = 1400000, .max = 2800000 },
200 .n = { .min = 1, .max = 6 },
201 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100202 .m1 = { .min = 8, .max = 18 },
203 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400204 .p = { .min = 5, .max = 80 },
205 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700206 .p2 = { .dot_limit = 200000,
207 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700208};
209
210static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400211 .dot = { .min = 20000, .max = 400000 },
212 .vco = { .min = 1400000, .max = 2800000 },
213 .n = { .min = 1, .max = 6 },
214 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100215 .m1 = { .min = 8, .max = 18 },
216 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400217 .p = { .min = 7, .max = 98 },
218 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700219 .p2 = { .dot_limit = 112000,
220 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700221};
222
Eric Anholt273e27c2011-03-30 13:01:10 -0700223
Keith Packarde4b36692009-06-05 19:22:17 -0700224static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700225 .dot = { .min = 25000, .max = 270000 },
226 .vco = { .min = 1750000, .max = 3500000},
227 .n = { .min = 1, .max = 4 },
228 .m = { .min = 104, .max = 138 },
229 .m1 = { .min = 17, .max = 23 },
230 .m2 = { .min = 5, .max = 11 },
231 .p = { .min = 10, .max = 30 },
232 .p1 = { .min = 1, .max = 3},
233 .p2 = { .dot_limit = 270000,
234 .p2_slow = 10,
235 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800236 },
Keith Packarde4b36692009-06-05 19:22:17 -0700237};
238
239static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700240 .dot = { .min = 22000, .max = 400000 },
241 .vco = { .min = 1750000, .max = 3500000},
242 .n = { .min = 1, .max = 4 },
243 .m = { .min = 104, .max = 138 },
244 .m1 = { .min = 16, .max = 23 },
245 .m2 = { .min = 5, .max = 11 },
246 .p = { .min = 5, .max = 80 },
247 .p1 = { .min = 1, .max = 8},
248 .p2 = { .dot_limit = 165000,
249 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700250};
251
252static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700253 .dot = { .min = 20000, .max = 115000 },
254 .vco = { .min = 1750000, .max = 3500000 },
255 .n = { .min = 1, .max = 3 },
256 .m = { .min = 104, .max = 138 },
257 .m1 = { .min = 17, .max = 23 },
258 .m2 = { .min = 5, .max = 11 },
259 .p = { .min = 28, .max = 112 },
260 .p1 = { .min = 2, .max = 8 },
261 .p2 = { .dot_limit = 0,
262 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800263 },
Keith Packarde4b36692009-06-05 19:22:17 -0700264};
265
266static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700267 .dot = { .min = 80000, .max = 224000 },
268 .vco = { .min = 1750000, .max = 3500000 },
269 .n = { .min = 1, .max = 3 },
270 .m = { .min = 104, .max = 138 },
271 .m1 = { .min = 17, .max = 23 },
272 .m2 = { .min = 5, .max = 11 },
273 .p = { .min = 14, .max = 42 },
274 .p1 = { .min = 2, .max = 6 },
275 .p2 = { .dot_limit = 0,
276 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800277 },
Keith Packarde4b36692009-06-05 19:22:17 -0700278};
279
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500280static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400281 .dot = { .min = 20000, .max = 400000},
282 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700283 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400284 .n = { .min = 3, .max = 6 },
285 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700286 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400287 .m1 = { .min = 0, .max = 0 },
288 .m2 = { .min = 0, .max = 254 },
289 .p = { .min = 5, .max = 80 },
290 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700291 .p2 = { .dot_limit = 200000,
292 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700293};
294
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500295static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400296 .dot = { .min = 20000, .max = 400000 },
297 .vco = { .min = 1700000, .max = 3500000 },
298 .n = { .min = 3, .max = 6 },
299 .m = { .min = 2, .max = 256 },
300 .m1 = { .min = 0, .max = 0 },
301 .m2 = { .min = 0, .max = 254 },
302 .p = { .min = 7, .max = 112 },
303 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700304 .p2 = { .dot_limit = 112000,
305 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700306};
307
Eric Anholt273e27c2011-03-30 13:01:10 -0700308/* Ironlake / Sandybridge
309 *
310 * We calculate clock using (register_value + 2) for N/M1/M2, so here
311 * the range value for them is (actual_value - 2).
312 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800313static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700314 .dot = { .min = 25000, .max = 350000 },
315 .vco = { .min = 1760000, .max = 3510000 },
316 .n = { .min = 1, .max = 5 },
317 .m = { .min = 79, .max = 127 },
318 .m1 = { .min = 12, .max = 22 },
319 .m2 = { .min = 5, .max = 9 },
320 .p = { .min = 5, .max = 80 },
321 .p1 = { .min = 1, .max = 8 },
322 .p2 = { .dot_limit = 225000,
323 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700324};
325
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800326static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700327 .dot = { .min = 25000, .max = 350000 },
328 .vco = { .min = 1760000, .max = 3510000 },
329 .n = { .min = 1, .max = 3 },
330 .m = { .min = 79, .max = 118 },
331 .m1 = { .min = 12, .max = 22 },
332 .m2 = { .min = 5, .max = 9 },
333 .p = { .min = 28, .max = 112 },
334 .p1 = { .min = 2, .max = 8 },
335 .p2 = { .dot_limit = 225000,
336 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800337};
338
339static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700340 .dot = { .min = 25000, .max = 350000 },
341 .vco = { .min = 1760000, .max = 3510000 },
342 .n = { .min = 1, .max = 3 },
343 .m = { .min = 79, .max = 127 },
344 .m1 = { .min = 12, .max = 22 },
345 .m2 = { .min = 5, .max = 9 },
346 .p = { .min = 14, .max = 56 },
347 .p1 = { .min = 2, .max = 8 },
348 .p2 = { .dot_limit = 225000,
349 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800350};
351
Eric Anholt273e27c2011-03-30 13:01:10 -0700352/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800353static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700354 .dot = { .min = 25000, .max = 350000 },
355 .vco = { .min = 1760000, .max = 3510000 },
356 .n = { .min = 1, .max = 2 },
357 .m = { .min = 79, .max = 126 },
358 .m1 = { .min = 12, .max = 22 },
359 .m2 = { .min = 5, .max = 9 },
360 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400361 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700362 .p2 = { .dot_limit = 225000,
363 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800364};
365
366static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700367 .dot = { .min = 25000, .max = 350000 },
368 .vco = { .min = 1760000, .max = 3510000 },
369 .n = { .min = 1, .max = 3 },
370 .m = { .min = 79, .max = 126 },
371 .m1 = { .min = 12, .max = 22 },
372 .m2 = { .min = 5, .max = 9 },
373 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400374 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700375 .p2 = { .dot_limit = 225000,
376 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800377};
378
Ville Syrjälädc730512013-09-24 21:26:30 +0300379static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300380 /*
381 * These are the data rate limits (measured in fast clocks)
382 * since those are the strictest limits we have. The fast
383 * clock and actual rate limits are more relaxed, so checking
384 * them would make no difference.
385 */
386 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200387 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700388 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700389 .m1 = { .min = 2, .max = 3 },
390 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300391 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300392 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700393};
394
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300395static const intel_limit_t intel_limits_chv = {
396 /*
397 * These are the data rate limits (measured in fast clocks)
398 * since those are the strictest limits we have. The fast
399 * clock and actual rate limits are more relaxed, so checking
400 * them would make no difference.
401 */
402 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200403 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300404 .n = { .min = 1, .max = 1 },
405 .m1 = { .min = 2, .max = 2 },
406 .m2 = { .min = 24 << 22, .max = 175 << 22 },
407 .p1 = { .min = 2, .max = 4 },
408 .p2 = { .p2_slow = 1, .p2_fast = 14 },
409};
410
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200411static const intel_limit_t intel_limits_bxt = {
412 /* FIXME: find real dot limits */
413 .dot = { .min = 0, .max = INT_MAX },
414 .vco = { .min = 4800000, .max = 6480000 },
415 .n = { .min = 1, .max = 1 },
416 .m1 = { .min = 2, .max = 2 },
417 /* FIXME: find real m2 limits */
418 .m2 = { .min = 2 << 22, .max = 255 << 22 },
419 .p1 = { .min = 2, .max = 4 },
420 .p2 = { .p2_slow = 1, .p2_fast = 20 },
421};
422
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300423static void vlv_clock(int refclk, intel_clock_t *clock)
424{
425 clock->m = clock->m1 * clock->m2;
426 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200427 if (WARN_ON(clock->n == 0 || clock->p == 0))
428 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300429 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
430 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300431}
432
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200433static bool
434needs_modeset(struct drm_crtc_state *state)
435{
436 return state->mode_changed || state->active_changed;
437}
438
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300439/**
440 * Returns whether any output on the specified pipe is of the specified type
441 */
Damien Lespiau40935612014-10-29 11:16:59 +0000442bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300443{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300444 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300445 struct intel_encoder *encoder;
446
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300447 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300448 if (encoder->type == type)
449 return true;
450
451 return false;
452}
453
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200454/**
455 * Returns whether any output on the specified pipe will have the specified
456 * type after a staged modeset is complete, i.e., the same as
457 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
458 * encoder->crtc.
459 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200460static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
461 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200462{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200463 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300464 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200465 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200466 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200467 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200468
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300469 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200470 if (connector_state->crtc != crtc_state->base.crtc)
471 continue;
472
473 num_connectors++;
474
475 encoder = to_intel_encoder(connector_state->best_encoder);
476 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200477 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200478 }
479
480 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200481
482 return false;
483}
484
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200485static const intel_limit_t *
486intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800487{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200488 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800489 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800490
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200491 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100492 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000493 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800494 limit = &intel_limits_ironlake_dual_lvds_100m;
495 else
496 limit = &intel_limits_ironlake_dual_lvds;
497 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000498 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800499 limit = &intel_limits_ironlake_single_lvds_100m;
500 else
501 limit = &intel_limits_ironlake_single_lvds;
502 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200503 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800504 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800505
506 return limit;
507}
508
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200509static const intel_limit_t *
510intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800511{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200512 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 const intel_limit_t *limit;
514
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200515 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100516 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800518 else
Keith Packarde4b36692009-06-05 19:22:17 -0700519 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200520 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
521 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700522 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200523 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700524 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800525 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700526 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800527
528 return limit;
529}
530
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200531static const intel_limit_t *
532intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800533{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200534 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800535 const intel_limit_t *limit;
536
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200537 if (IS_BROXTON(dev))
538 limit = &intel_limits_bxt;
539 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200540 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800541 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200542 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500543 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200544 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500545 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800546 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500547 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300548 } else if (IS_CHERRYVIEW(dev)) {
549 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700550 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300551 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100552 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100554 limit = &intel_limits_i9xx_lvds;
555 else
556 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800557 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200558 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700559 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200560 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700561 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200562 else
563 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800564 }
565 return limit;
566}
567
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500568/* m1 is reserved as 0 in Pineview, n is a ring counter */
569static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800570{
Shaohua Li21778322009-02-23 15:19:16 +0800571 clock->m = clock->m2 + 2;
572 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200573 if (WARN_ON(clock->n == 0 || clock->p == 0))
574 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300575 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
576 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800577}
578
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200579static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
580{
581 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
582}
583
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200584static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800585{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200586 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800587 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200588 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
589 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300590 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800592}
593
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300594static void chv_clock(int refclk, intel_clock_t *clock)
595{
596 clock->m = clock->m1 * clock->m2;
597 clock->p = clock->p1 * clock->p2;
598 if (WARN_ON(clock->n == 0 || clock->p == 0))
599 return;
600 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
601 clock->n << 22);
602 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
603}
604
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800605#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800606/**
607 * Returns whether the given set of divisors are valid for a given refclk with
608 * the given connectors.
609 */
610
Chris Wilson1b894b52010-12-14 20:04:54 +0000611static bool intel_PLL_is_valid(struct drm_device *dev,
612 const intel_limit_t *limit,
613 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800614{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300615 if (clock->n < limit->n.min || limit->n.max < clock->n)
616 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800617 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400618 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800619 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400620 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800621 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400622 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300623
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200624 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300625 if (clock->m1 <= clock->m2)
626 INTELPllInvalid("m1 <= m2\n");
627
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200628 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300629 if (clock->p < limit->p.min || limit->p.max < clock->p)
630 INTELPllInvalid("p out of range\n");
631 if (clock->m < limit->m.min || limit->m.max < clock->m)
632 INTELPllInvalid("m out of range\n");
633 }
634
Jesse Barnes79e53942008-11-07 14:24:08 -0800635 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400636 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800637 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
638 * connector, etc., rather than just a single range.
639 */
640 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400641 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800642
643 return true;
644}
645
Ma Lingd4906092009-03-18 20:13:27 +0800646static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200647i9xx_find_best_dpll(const intel_limit_t *limit,
648 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800649 int target, int refclk, intel_clock_t *match_clock,
650 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800651{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200652 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300653 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 int err = target;
656
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200657 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100659 * For LVDS just rely on its current settings for dual-channel.
660 * We haven't figured out how to reliably set up different
661 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800662 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100663 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800664 clock.p2 = limit->p2.p2_fast;
665 else
666 clock.p2 = limit->p2.p2_slow;
667 } else {
668 if (target < limit->p2.dot_limit)
669 clock.p2 = limit->p2.p2_slow;
670 else
671 clock.p2 = limit->p2.p2_fast;
672 }
673
Akshay Joshi0206e352011-08-16 15:34:10 -0400674 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800675
Zhao Yakui42158662009-11-20 11:24:18 +0800676 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
677 clock.m1++) {
678 for (clock.m2 = limit->m2.min;
679 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200680 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800681 break;
682 for (clock.n = limit->n.min;
683 clock.n <= limit->n.max; clock.n++) {
684 for (clock.p1 = limit->p1.min;
685 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800686 int this_err;
687
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200688 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000689 if (!intel_PLL_is_valid(dev, limit,
690 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800691 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800692 if (match_clock &&
693 clock.p != match_clock->p)
694 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800695
696 this_err = abs(clock.dot - target);
697 if (this_err < err) {
698 *best_clock = clock;
699 err = this_err;
700 }
701 }
702 }
703 }
704 }
705
706 return (err != target);
707}
708
Ma Lingd4906092009-03-18 20:13:27 +0800709static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200710pnv_find_best_dpll(const intel_limit_t *limit,
711 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200712 int target, int refclk, intel_clock_t *match_clock,
713 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200714{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200715 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300716 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200717 intel_clock_t clock;
718 int err = target;
719
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200720 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200721 /*
722 * For LVDS just rely on its current settings for dual-channel.
723 * We haven't figured out how to reliably set up different
724 * single/dual channel state, if we even can.
725 */
726 if (intel_is_dual_link_lvds(dev))
727 clock.p2 = limit->p2.p2_fast;
728 else
729 clock.p2 = limit->p2.p2_slow;
730 } else {
731 if (target < limit->p2.dot_limit)
732 clock.p2 = limit->p2.p2_slow;
733 else
734 clock.p2 = limit->p2.p2_fast;
735 }
736
737 memset(best_clock, 0, sizeof(*best_clock));
738
739 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
740 clock.m1++) {
741 for (clock.m2 = limit->m2.min;
742 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200743 for (clock.n = limit->n.min;
744 clock.n <= limit->n.max; clock.n++) {
745 for (clock.p1 = limit->p1.min;
746 clock.p1 <= limit->p1.max; clock.p1++) {
747 int this_err;
748
749 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800750 if (!intel_PLL_is_valid(dev, limit,
751 &clock))
752 continue;
753 if (match_clock &&
754 clock.p != match_clock->p)
755 continue;
756
757 this_err = abs(clock.dot - target);
758 if (this_err < err) {
759 *best_clock = clock;
760 err = this_err;
761 }
762 }
763 }
764 }
765 }
766
767 return (err != target);
768}
769
Ma Lingd4906092009-03-18 20:13:27 +0800770static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200771g4x_find_best_dpll(const intel_limit_t *limit,
772 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200773 int target, int refclk, intel_clock_t *match_clock,
774 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800775{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200776 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300777 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800778 intel_clock_t clock;
779 int max_n;
780 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400781 /* approximately equals target * 0.00585 */
782 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800783 found = false;
784
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200785 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100786 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800787 clock.p2 = limit->p2.p2_fast;
788 else
789 clock.p2 = limit->p2.p2_slow;
790 } else {
791 if (target < limit->p2.dot_limit)
792 clock.p2 = limit->p2.p2_slow;
793 else
794 clock.p2 = limit->p2.p2_fast;
795 }
796
797 memset(best_clock, 0, sizeof(*best_clock));
798 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200799 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800800 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200801 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800802 for (clock.m1 = limit->m1.max;
803 clock.m1 >= limit->m1.min; clock.m1--) {
804 for (clock.m2 = limit->m2.max;
805 clock.m2 >= limit->m2.min; clock.m2--) {
806 for (clock.p1 = limit->p1.max;
807 clock.p1 >= limit->p1.min; clock.p1--) {
808 int this_err;
809
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200810 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000811 if (!intel_PLL_is_valid(dev, limit,
812 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800813 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000814
815 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800816 if (this_err < err_most) {
817 *best_clock = clock;
818 err_most = this_err;
819 max_n = clock.n;
820 found = true;
821 }
822 }
823 }
824 }
825 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800826 return found;
827}
Ma Lingd4906092009-03-18 20:13:27 +0800828
Imre Deakd5dd62b2015-03-17 11:40:03 +0200829/*
830 * Check if the calculated PLL configuration is more optimal compared to the
831 * best configuration and error found so far. Return the calculated error.
832 */
833static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
834 const intel_clock_t *calculated_clock,
835 const intel_clock_t *best_clock,
836 unsigned int best_error_ppm,
837 unsigned int *error_ppm)
838{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200839 /*
840 * For CHV ignore the error and consider only the P value.
841 * Prefer a bigger P value based on HW requirements.
842 */
843 if (IS_CHERRYVIEW(dev)) {
844 *error_ppm = 0;
845
846 return calculated_clock->p > best_clock->p;
847 }
848
Imre Deak24be4e42015-03-17 11:40:04 +0200849 if (WARN_ON_ONCE(!target_freq))
850 return false;
851
Imre Deakd5dd62b2015-03-17 11:40:03 +0200852 *error_ppm = div_u64(1000000ULL *
853 abs(target_freq - calculated_clock->dot),
854 target_freq);
855 /*
856 * Prefer a better P value over a better (smaller) error if the error
857 * is small. Ensure this preference for future configurations too by
858 * setting the error to 0.
859 */
860 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
861 *error_ppm = 0;
862
863 return true;
864 }
865
866 return *error_ppm + 10 < best_error_ppm;
867}
868
Zhenyu Wang2c072452009-06-05 15:38:42 +0800869static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200870vlv_find_best_dpll(const intel_limit_t *limit,
871 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200872 int target, int refclk, intel_clock_t *match_clock,
873 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700874{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200875 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300876 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300877 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300878 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300879 /* min update 19.2 MHz */
880 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300881 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700882
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300883 target *= 5; /* fast clock */
884
885 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700886
887 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300888 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300889 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300890 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300891 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300892 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700893 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300894 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200895 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300896
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300897 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
898 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300899
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300900 vlv_clock(refclk, &clock);
901
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300902 if (!intel_PLL_is_valid(dev, limit,
903 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300904 continue;
905
Imre Deakd5dd62b2015-03-17 11:40:03 +0200906 if (!vlv_PLL_is_optimal(dev, target,
907 &clock,
908 best_clock,
909 bestppm, &ppm))
910 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300911
Imre Deakd5dd62b2015-03-17 11:40:03 +0200912 *best_clock = clock;
913 bestppm = ppm;
914 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700915 }
916 }
917 }
918 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700919
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300920 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700921}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700922
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300923static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200924chv_find_best_dpll(const intel_limit_t *limit,
925 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300926 int target, int refclk, intel_clock_t *match_clock,
927 intel_clock_t *best_clock)
928{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200929 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300930 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200931 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300932 intel_clock_t clock;
933 uint64_t m2;
934 int found = false;
935
936 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200937 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300938
939 /*
940 * Based on hardware doc, the n always set to 1, and m1 always
941 * set to 2. If requires to support 200Mhz refclk, we need to
942 * revisit this because n may not 1 anymore.
943 */
944 clock.n = 1, clock.m1 = 2;
945 target *= 5; /* fast clock */
946
947 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
948 for (clock.p2 = limit->p2.p2_fast;
949 clock.p2 >= limit->p2.p2_slow;
950 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200951 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300952
953 clock.p = clock.p1 * clock.p2;
954
955 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
956 clock.n) << 22, refclk * clock.m1);
957
958 if (m2 > INT_MAX/clock.m1)
959 continue;
960
961 clock.m2 = m2;
962
963 chv_clock(refclk, &clock);
964
965 if (!intel_PLL_is_valid(dev, limit, &clock))
966 continue;
967
Imre Deak9ca3ba02015-03-17 11:40:05 +0200968 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
969 best_error_ppm, &error_ppm))
970 continue;
971
972 *best_clock = clock;
973 best_error_ppm = error_ppm;
974 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300975 }
976 }
977
978 return found;
979}
980
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200981bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
982 intel_clock_t *best_clock)
983{
984 int refclk = i9xx_get_refclk(crtc_state, 0);
985
986 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
987 target_clock, refclk, NULL, best_clock);
988}
989
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300990bool intel_crtc_active(struct drm_crtc *crtc)
991{
992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
993
994 /* Be paranoid as we can arrive here with only partial
995 * state retrieved from the hardware during setup.
996 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100997 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300998 * as Haswell has gained clock readout/fastboot support.
999 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001000 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001001 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001002 *
1003 * FIXME: The intel_crtc->active here should be switched to
1004 * crtc->state->active once we have proper CRTC states wired up
1005 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001006 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001007 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001008 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001009}
1010
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001011enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1012 enum pipe pipe)
1013{
1014 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1016
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001017 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001018}
1019
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001020static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1021{
1022 struct drm_i915_private *dev_priv = dev->dev_private;
1023 u32 reg = PIPEDSL(pipe);
1024 u32 line1, line2;
1025 u32 line_mask;
1026
1027 if (IS_GEN2(dev))
1028 line_mask = DSL_LINEMASK_GEN2;
1029 else
1030 line_mask = DSL_LINEMASK_GEN3;
1031
1032 line1 = I915_READ(reg) & line_mask;
1033 mdelay(5);
1034 line2 = I915_READ(reg) & line_mask;
1035
1036 return line1 == line2;
1037}
1038
Keith Packardab7ad7f2010-10-03 00:33:06 -07001039/*
1040 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001041 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001042 *
1043 * After disabling a pipe, we can't wait for vblank in the usual way,
1044 * spinning on the vblank interrupt status bit, since we won't actually
1045 * see an interrupt when the pipe is disabled.
1046 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001047 * On Gen4 and above:
1048 * wait for the pipe register state bit to turn off
1049 *
1050 * Otherwise:
1051 * wait for the display line value to settle (it usually
1052 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001053 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001054 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001055static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001056{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001057 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001058 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001059 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001060 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001061
Keith Packardab7ad7f2010-10-03 00:33:06 -07001062 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001063 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001064
Keith Packardab7ad7f2010-10-03 00:33:06 -07001065 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001066 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1067 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001068 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001069 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001070 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001071 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001072 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001073 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001074}
1075
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001076/*
1077 * ibx_digital_port_connected - is the specified port connected?
1078 * @dev_priv: i915 private structure
1079 * @port: the port to test
1080 *
1081 * Returns true if @port is connected, false otherwise.
1082 */
1083bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1084 struct intel_digital_port *port)
1085{
1086 u32 bit;
1087
Damien Lespiauc36346e2012-12-13 16:09:03 +00001088 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001089 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001090 case PORT_B:
1091 bit = SDE_PORTB_HOTPLUG;
1092 break;
1093 case PORT_C:
1094 bit = SDE_PORTC_HOTPLUG;
1095 break;
1096 case PORT_D:
1097 bit = SDE_PORTD_HOTPLUG;
1098 break;
1099 default:
1100 return true;
1101 }
1102 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001103 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001104 case PORT_B:
1105 bit = SDE_PORTB_HOTPLUG_CPT;
1106 break;
1107 case PORT_C:
1108 bit = SDE_PORTC_HOTPLUG_CPT;
1109 break;
1110 case PORT_D:
1111 bit = SDE_PORTD_HOTPLUG_CPT;
1112 break;
1113 default:
1114 return true;
1115 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001116 }
1117
1118 return I915_READ(SDEISR) & bit;
1119}
1120
Jesse Barnesb24e7172011-01-04 15:09:30 -08001121static const char *state_string(bool enabled)
1122{
1123 return enabled ? "on" : "off";
1124}
1125
1126/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001127void assert_pll(struct drm_i915_private *dev_priv,
1128 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001129{
1130 int reg;
1131 u32 val;
1132 bool cur_state;
1133
1134 reg = DPLL(pipe);
1135 val = I915_READ(reg);
1136 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001137 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001138 "PLL state assertion failure (expected %s, current %s)\n",
1139 state_string(state), state_string(cur_state));
1140}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001141
Jani Nikula23538ef2013-08-27 15:12:22 +03001142/* XXX: the dsi pll is shared between MIPI DSI ports */
1143static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1144{
1145 u32 val;
1146 bool cur_state;
1147
Ville Syrjäläa5805162015-05-26 20:42:30 +03001148 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001149 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001150 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001151
1152 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001153 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001154 "DSI PLL state assertion failure (expected %s, current %s)\n",
1155 state_string(state), state_string(cur_state));
1156}
1157#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1158#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1159
Daniel Vetter55607e82013-06-16 21:42:39 +02001160struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001161intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001162{
Daniel Vettere2b78262013-06-07 23:10:03 +02001163 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1164
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001165 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001166 return NULL;
1167
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001168 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001169}
1170
Jesse Barnesb24e7172011-01-04 15:09:30 -08001171/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001172void assert_shared_dpll(struct drm_i915_private *dev_priv,
1173 struct intel_shared_dpll *pll,
1174 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001175{
Jesse Barnes040484a2011-01-03 12:14:26 -08001176 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001177 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001178
Chris Wilson92b27b02012-05-20 18:10:50 +01001179 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001180 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001181 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001182
Daniel Vetter53589012013-06-05 13:34:16 +02001183 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001184 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001185 "%s assertion failure (expected %s, current %s)\n",
1186 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001187}
Jesse Barnes040484a2011-01-03 12:14:26 -08001188
1189static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1190 enum pipe pipe, bool state)
1191{
1192 int reg;
1193 u32 val;
1194 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001195 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1196 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001197
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001198 if (HAS_DDI(dev_priv->dev)) {
1199 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001200 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001201 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001202 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001203 } else {
1204 reg = FDI_TX_CTL(pipe);
1205 val = I915_READ(reg);
1206 cur_state = !!(val & FDI_TX_ENABLE);
1207 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001208 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001209 "FDI TX state assertion failure (expected %s, current %s)\n",
1210 state_string(state), state_string(cur_state));
1211}
1212#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1213#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1214
1215static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1216 enum pipe pipe, bool state)
1217{
1218 int reg;
1219 u32 val;
1220 bool cur_state;
1221
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001222 reg = FDI_RX_CTL(pipe);
1223 val = I915_READ(reg);
1224 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001225 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001226 "FDI RX state assertion failure (expected %s, current %s)\n",
1227 state_string(state), state_string(cur_state));
1228}
1229#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1230#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1231
1232static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1233 enum pipe pipe)
1234{
1235 int reg;
1236 u32 val;
1237
1238 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001239 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001240 return;
1241
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001242 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001243 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001244 return;
1245
Jesse Barnes040484a2011-01-03 12:14:26 -08001246 reg = FDI_TX_CTL(pipe);
1247 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001248 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 -08001249}
1250
Daniel Vetter55607e82013-06-16 21:42:39 +02001251void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1252 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001253{
1254 int reg;
1255 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001256 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001257
1258 reg = FDI_RX_CTL(pipe);
1259 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001260 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001261 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001262 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1263 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001264}
1265
Daniel Vetterb680c372014-09-19 18:27:27 +02001266void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1267 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001268{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001269 struct drm_device *dev = dev_priv->dev;
1270 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001271 u32 val;
1272 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001273 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001274
Jani Nikulabedd4db2014-08-22 15:04:13 +03001275 if (WARN_ON(HAS_DDI(dev)))
1276 return;
1277
1278 if (HAS_PCH_SPLIT(dev)) {
1279 u32 port_sel;
1280
Jesse Barnesea0760c2011-01-04 15:09:32 -08001281 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001282 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1283
1284 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1285 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1286 panel_pipe = PIPE_B;
1287 /* XXX: else fix for eDP */
1288 } else if (IS_VALLEYVIEW(dev)) {
1289 /* presumably write lock depends on pipe, not port select */
1290 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1291 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001292 } else {
1293 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001294 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1295 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001296 }
1297
1298 val = I915_READ(pp_reg);
1299 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001300 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001301 locked = false;
1302
Rob Clarke2c719b2014-12-15 13:56:32 -05001303 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001304 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001305 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001306}
1307
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001308static void assert_cursor(struct drm_i915_private *dev_priv,
1309 enum pipe pipe, bool state)
1310{
1311 struct drm_device *dev = dev_priv->dev;
1312 bool cur_state;
1313
Paulo Zanonid9d82082014-02-27 16:30:56 -03001314 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001315 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001316 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001317 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001318
Rob Clarke2c719b2014-12-15 13:56:32 -05001319 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001320 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1321 pipe_name(pipe), state_string(state), state_string(cur_state));
1322}
1323#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1324#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1325
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001326void assert_pipe(struct drm_i915_private *dev_priv,
1327 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001328{
1329 int reg;
1330 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001331 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001332 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1333 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001334
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001335 /* if we need the pipe quirk it must be always on */
1336 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1337 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001338 state = true;
1339
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001340 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001341 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001342 cur_state = false;
1343 } else {
1344 reg = PIPECONF(cpu_transcoder);
1345 val = I915_READ(reg);
1346 cur_state = !!(val & PIPECONF_ENABLE);
1347 }
1348
Rob Clarke2c719b2014-12-15 13:56:32 -05001349 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001350 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001351 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352}
1353
Chris Wilson931872f2012-01-16 23:01:13 +00001354static void assert_plane(struct drm_i915_private *dev_priv,
1355 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356{
1357 int reg;
1358 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001359 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001360
1361 reg = DSPCNTR(plane);
1362 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001363 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001364 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001365 "plane %c assertion failure (expected %s, current %s)\n",
1366 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001367}
1368
Chris Wilson931872f2012-01-16 23:01:13 +00001369#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1370#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1371
Jesse Barnesb24e7172011-01-04 15:09:30 -08001372static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1373 enum pipe pipe)
1374{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001375 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001376 int reg, i;
1377 u32 val;
1378 int cur_pipe;
1379
Ville Syrjälä653e1022013-06-04 13:49:05 +03001380 /* Primary planes are fixed to pipes on gen4+ */
1381 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001382 reg = DSPCNTR(pipe);
1383 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001384 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001385 "plane %c assertion failure, should be disabled but not\n",
1386 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001387 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001388 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001389
Jesse Barnesb24e7172011-01-04 15:09:30 -08001390 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001391 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001392 reg = DSPCNTR(i);
1393 val = I915_READ(reg);
1394 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1395 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001396 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001397 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1398 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001399 }
1400}
1401
Jesse Barnes19332d72013-03-28 09:55:38 -07001402static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1403 enum pipe pipe)
1404{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001405 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001406 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001407 u32 val;
1408
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001409 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001410 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001411 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001412 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001413 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1414 sprite, pipe_name(pipe));
1415 }
1416 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001417 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001418 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001419 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001420 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001421 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001422 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001423 }
1424 } else if (INTEL_INFO(dev)->gen >= 7) {
1425 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001426 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001427 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001428 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001429 plane_name(pipe), pipe_name(pipe));
1430 } else if (INTEL_INFO(dev)->gen >= 5) {
1431 reg = DVSCNTR(pipe);
1432 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001433 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001434 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1435 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001436 }
1437}
1438
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001439static void assert_vblank_disabled(struct drm_crtc *crtc)
1440{
Rob Clarke2c719b2014-12-15 13:56:32 -05001441 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001442 drm_crtc_vblank_put(crtc);
1443}
1444
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001445static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001446{
1447 u32 val;
1448 bool enabled;
1449
Rob Clarke2c719b2014-12-15 13:56:32 -05001450 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001451
Jesse Barnes92f25842011-01-04 15:09:34 -08001452 val = I915_READ(PCH_DREF_CONTROL);
1453 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1454 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001455 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001456}
1457
Daniel Vetterab9412b2013-05-03 11:49:46 +02001458static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1459 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001460{
1461 int reg;
1462 u32 val;
1463 bool enabled;
1464
Daniel Vetterab9412b2013-05-03 11:49:46 +02001465 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001466 val = I915_READ(reg);
1467 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001468 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001469 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1470 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001471}
1472
Keith Packard4e634382011-08-06 10:39:45 -07001473static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1474 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001475{
1476 if ((val & DP_PORT_EN) == 0)
1477 return false;
1478
1479 if (HAS_PCH_CPT(dev_priv->dev)) {
1480 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1481 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1482 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1483 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001484 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1485 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1486 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001487 } else {
1488 if ((val & DP_PIPE_MASK) != (pipe << 30))
1489 return false;
1490 }
1491 return true;
1492}
1493
Keith Packard1519b992011-08-06 10:35:34 -07001494static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1495 enum pipe pipe, u32 val)
1496{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001497 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001498 return false;
1499
1500 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001501 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001502 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001503 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1504 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1505 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001506 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001507 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001508 return false;
1509 }
1510 return true;
1511}
1512
1513static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1514 enum pipe pipe, u32 val)
1515{
1516 if ((val & LVDS_PORT_EN) == 0)
1517 return false;
1518
1519 if (HAS_PCH_CPT(dev_priv->dev)) {
1520 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1521 return false;
1522 } else {
1523 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1524 return false;
1525 }
1526 return true;
1527}
1528
1529static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1530 enum pipe pipe, u32 val)
1531{
1532 if ((val & ADPA_DAC_ENABLE) == 0)
1533 return false;
1534 if (HAS_PCH_CPT(dev_priv->dev)) {
1535 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1536 return false;
1537 } else {
1538 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1539 return false;
1540 }
1541 return true;
1542}
1543
Jesse Barnes291906f2011-02-02 12:28:03 -08001544static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001545 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001546{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001547 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001548 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001549 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001550 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001551
Rob Clarke2c719b2014-12-15 13:56:32 -05001552 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001553 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001554 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001555}
1556
1557static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1558 enum pipe pipe, int reg)
1559{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001560 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001561 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001562 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001563 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001564
Rob Clarke2c719b2014-12-15 13:56:32 -05001565 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001566 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001567 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001568}
1569
1570static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1571 enum pipe pipe)
1572{
1573 int reg;
1574 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001575
Keith Packardf0575e92011-07-25 22:12:43 -07001576 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1577 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1578 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001579
1580 reg = PCH_ADPA;
1581 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001582 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001583 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001584 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001585
1586 reg = PCH_LVDS;
1587 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001588 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001589 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001590 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001591
Paulo Zanonie2debe92013-02-18 19:00:27 -03001592 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1593 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1594 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001595}
1596
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001597static void intel_init_dpio(struct drm_device *dev)
1598{
1599 struct drm_i915_private *dev_priv = dev->dev_private;
1600
1601 if (!IS_VALLEYVIEW(dev))
1602 return;
1603
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001604 /*
1605 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1606 * CHV x1 PHY (DP/HDMI D)
1607 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1608 */
1609 if (IS_CHERRYVIEW(dev)) {
1610 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1611 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1612 } else {
1613 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1614 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001615}
1616
Ville Syrjäläd288f652014-10-28 13:20:22 +02001617static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001618 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001619{
Daniel Vetter426115c2013-07-11 22:13:42 +02001620 struct drm_device *dev = crtc->base.dev;
1621 struct drm_i915_private *dev_priv = dev->dev_private;
1622 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001623 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001624
Daniel Vetter426115c2013-07-11 22:13:42 +02001625 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001626
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001627 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001628 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1629
1630 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001631 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001632 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001633
Daniel Vetter426115c2013-07-11 22:13:42 +02001634 I915_WRITE(reg, dpll);
1635 POSTING_READ(reg);
1636 udelay(150);
1637
1638 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1639 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1640
Ville Syrjäläd288f652014-10-28 13:20:22 +02001641 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001642 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001643
1644 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001645 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001646 POSTING_READ(reg);
1647 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001648 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001649 POSTING_READ(reg);
1650 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001651 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001652 POSTING_READ(reg);
1653 udelay(150); /* wait for warmup */
1654}
1655
Ville Syrjäläd288f652014-10-28 13:20:22 +02001656static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001657 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001658{
1659 struct drm_device *dev = crtc->base.dev;
1660 struct drm_i915_private *dev_priv = dev->dev_private;
1661 int pipe = crtc->pipe;
1662 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001663 u32 tmp;
1664
1665 assert_pipe_disabled(dev_priv, crtc->pipe);
1666
1667 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1668
Ville Syrjäläa5805162015-05-26 20:42:30 +03001669 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001670
1671 /* Enable back the 10bit clock to display controller */
1672 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1673 tmp |= DPIO_DCLKP_EN;
1674 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1675
Ville Syrjälä54433e92015-05-26 20:42:31 +03001676 mutex_unlock(&dev_priv->sb_lock);
1677
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001678 /*
1679 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1680 */
1681 udelay(1);
1682
1683 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001684 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001685
1686 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001687 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001688 DRM_ERROR("PLL %d failed to lock\n", pipe);
1689
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001690 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001691 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001692 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001693}
1694
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001695static int intel_num_dvo_pipes(struct drm_device *dev)
1696{
1697 struct intel_crtc *crtc;
1698 int count = 0;
1699
1700 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001701 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001702 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001703
1704 return count;
1705}
1706
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001707static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001708{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001709 struct drm_device *dev = crtc->base.dev;
1710 struct drm_i915_private *dev_priv = dev->dev_private;
1711 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001712 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001713
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001714 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001715
1716 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001717 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001718
1719 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001720 if (IS_MOBILE(dev) && !IS_I830(dev))
1721 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001722
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001723 /* Enable DVO 2x clock on both PLLs if necessary */
1724 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1725 /*
1726 * It appears to be important that we don't enable this
1727 * for the current pipe before otherwise configuring the
1728 * PLL. No idea how this should be handled if multiple
1729 * DVO outputs are enabled simultaneosly.
1730 */
1731 dpll |= DPLL_DVO_2X_MODE;
1732 I915_WRITE(DPLL(!crtc->pipe),
1733 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1734 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001735
1736 /* Wait for the clocks to stabilize. */
1737 POSTING_READ(reg);
1738 udelay(150);
1739
1740 if (INTEL_INFO(dev)->gen >= 4) {
1741 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001742 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001743 } else {
1744 /* The pixel multiplier can only be updated once the
1745 * DPLL is enabled and the clocks are stable.
1746 *
1747 * So write it again.
1748 */
1749 I915_WRITE(reg, dpll);
1750 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001751
1752 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001753 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001754 POSTING_READ(reg);
1755 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001756 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001757 POSTING_READ(reg);
1758 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001759 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001760 POSTING_READ(reg);
1761 udelay(150); /* wait for warmup */
1762}
1763
1764/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001765 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001766 * @dev_priv: i915 private structure
1767 * @pipe: pipe PLL to disable
1768 *
1769 * Disable the PLL for @pipe, making sure the pipe is off first.
1770 *
1771 * Note! This is for pre-ILK only.
1772 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001773static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001774{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001775 struct drm_device *dev = crtc->base.dev;
1776 struct drm_i915_private *dev_priv = dev->dev_private;
1777 enum pipe pipe = crtc->pipe;
1778
1779 /* Disable DVO 2x clock on both PLLs if necessary */
1780 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001781 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001782 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001783 I915_WRITE(DPLL(PIPE_B),
1784 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1785 I915_WRITE(DPLL(PIPE_A),
1786 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1787 }
1788
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001789 /* Don't disable pipe or pipe PLLs if needed */
1790 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1791 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001792 return;
1793
1794 /* Make sure the pipe isn't still relying on us */
1795 assert_pipe_disabled(dev_priv, pipe);
1796
Daniel Vetter50b44a42013-06-05 13:34:33 +02001797 I915_WRITE(DPLL(pipe), 0);
1798 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001799}
1800
Jesse Barnesf6071162013-10-01 10:41:38 -07001801static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1802{
1803 u32 val = 0;
1804
1805 /* Make sure the pipe isn't still relying on us */
1806 assert_pipe_disabled(dev_priv, pipe);
1807
Imre Deake5cbfbf2014-01-09 17:08:16 +02001808 /*
1809 * Leave integrated clock source and reference clock enabled for pipe B.
1810 * The latter is needed for VGA hotplug / manual detection.
1811 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001812 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001813 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001814 I915_WRITE(DPLL(pipe), val);
1815 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001816
1817}
1818
1819static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1820{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001821 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001822 u32 val;
1823
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001824 /* Make sure the pipe isn't still relying on us */
1825 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001826
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001827 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001828 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001829 if (pipe != PIPE_A)
1830 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1831 I915_WRITE(DPLL(pipe), val);
1832 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001833
Ville Syrjäläa5805162015-05-26 20:42:30 +03001834 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001835
1836 /* Disable 10bit clock to display controller */
1837 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1838 val &= ~DPIO_DCLKP_EN;
1839 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1840
Ville Syrjälä61407f62014-05-27 16:32:55 +03001841 /* disable left/right clock distribution */
1842 if (pipe != PIPE_B) {
1843 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1844 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1845 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1846 } else {
1847 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1848 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1849 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1850 }
1851
Ville Syrjäläa5805162015-05-26 20:42:30 +03001852 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001853}
1854
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001855void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001856 struct intel_digital_port *dport,
1857 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001858{
1859 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001860 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001861
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001862 switch (dport->port) {
1863 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001864 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001865 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001866 break;
1867 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001868 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001869 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001870 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001871 break;
1872 case PORT_D:
1873 port_mask = DPLL_PORTD_READY_MASK;
1874 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001875 break;
1876 default:
1877 BUG();
1878 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001879
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001880 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1881 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1882 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001883}
1884
Daniel Vetterb14b1052014-04-24 23:55:13 +02001885static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1886{
1887 struct drm_device *dev = crtc->base.dev;
1888 struct drm_i915_private *dev_priv = dev->dev_private;
1889 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1890
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001891 if (WARN_ON(pll == NULL))
1892 return;
1893
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001894 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001895 if (pll->active == 0) {
1896 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1897 WARN_ON(pll->on);
1898 assert_shared_dpll_disabled(dev_priv, pll);
1899
1900 pll->mode_set(dev_priv, pll);
1901 }
1902}
1903
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001904/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001905 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001906 * @dev_priv: i915 private structure
1907 * @pipe: pipe PLL to enable
1908 *
1909 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1910 * drives the transcoder clock.
1911 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001912static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001913{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001914 struct drm_device *dev = crtc->base.dev;
1915 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001916 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001917
Daniel Vetter87a875b2013-06-05 13:34:19 +02001918 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001919 return;
1920
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001921 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001922 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001923
Damien Lespiau74dd6922014-07-29 18:06:17 +01001924 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001925 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001926 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001927
Daniel Vettercdbd2312013-06-05 13:34:03 +02001928 if (pll->active++) {
1929 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001930 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001931 return;
1932 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001933 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001934
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001935 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1936
Daniel Vetter46edb022013-06-05 13:34:12 +02001937 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001938 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001939 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001940}
1941
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001942static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001943{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001944 struct drm_device *dev = crtc->base.dev;
1945 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001946 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001947
Jesse Barnes92f25842011-01-04 15:09:34 -08001948 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001949 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001950 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001951 return;
1952
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001953 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001954 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001955
Daniel Vetter46edb022013-06-05 13:34:12 +02001956 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1957 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001958 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001959
Chris Wilson48da64a2012-05-13 20:16:12 +01001960 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001961 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001962 return;
1963 }
1964
Daniel Vettere9d69442013-06-05 13:34:15 +02001965 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001966 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001967 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001968 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001969
Daniel Vetter46edb022013-06-05 13:34:12 +02001970 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001971 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001972 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001973
1974 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001975}
1976
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001977static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1978 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001979{
Daniel Vetter23670b322012-11-01 09:15:30 +01001980 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001981 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001983 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001984
1985 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001986 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001987
1988 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001989 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001990 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001991
1992 /* FDI must be feeding us bits for PCH ports */
1993 assert_fdi_tx_enabled(dev_priv, pipe);
1994 assert_fdi_rx_enabled(dev_priv, pipe);
1995
Daniel Vetter23670b322012-11-01 09:15:30 +01001996 if (HAS_PCH_CPT(dev)) {
1997 /* Workaround: Set the timing override bit before enabling the
1998 * pch transcoder. */
1999 reg = TRANS_CHICKEN2(pipe);
2000 val = I915_READ(reg);
2001 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2002 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03002003 }
Daniel Vetter23670b322012-11-01 09:15:30 +01002004
Daniel Vetterab9412b2013-05-03 11:49:46 +02002005 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002006 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002007 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002008
2009 if (HAS_PCH_IBX(dev_priv->dev)) {
2010 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002011 * Make the BPC in transcoder be consistent with
2012 * that in pipeconf reg. For HDMI we must use 8bpc
2013 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07002014 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002015 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002016 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
2017 val |= PIPECONF_8BPC;
2018 else
2019 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002020 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002021
2022 val &= ~TRANS_INTERLACE_MASK;
2023 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002024 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002025 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002026 val |= TRANS_LEGACY_INTERLACED_ILK;
2027 else
2028 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002029 else
2030 val |= TRANS_PROGRESSIVE;
2031
Jesse Barnes040484a2011-01-03 12:14:26 -08002032 I915_WRITE(reg, val | TRANS_ENABLE);
2033 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002034 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002035}
2036
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002037static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002038 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002039{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002041
2042 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002043 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002044
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002045 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002046 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002047 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002048
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002049 /* Workaround: set timing override bit. */
2050 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002051 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002052 I915_WRITE(_TRANSA_CHICKEN2, val);
2053
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002054 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002055 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002056
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002057 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2058 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002059 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002060 else
2061 val |= TRANS_PROGRESSIVE;
2062
Daniel Vetterab9412b2013-05-03 11:49:46 +02002063 I915_WRITE(LPT_TRANSCONF, val);
2064 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002065 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002066}
2067
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002068static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2069 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002070{
Daniel Vetter23670b322012-11-01 09:15:30 +01002071 struct drm_device *dev = dev_priv->dev;
2072 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002073
2074 /* FDI relies on the transcoder */
2075 assert_fdi_tx_disabled(dev_priv, pipe);
2076 assert_fdi_rx_disabled(dev_priv, pipe);
2077
Jesse Barnes291906f2011-02-02 12:28:03 -08002078 /* Ports must be off as well */
2079 assert_pch_ports_disabled(dev_priv, pipe);
2080
Daniel Vetterab9412b2013-05-03 11:49:46 +02002081 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002082 val = I915_READ(reg);
2083 val &= ~TRANS_ENABLE;
2084 I915_WRITE(reg, val);
2085 /* wait for PCH transcoder off, transcoder state */
2086 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002087 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002088
2089 if (!HAS_PCH_IBX(dev)) {
2090 /* Workaround: Clear the timing override chicken bit again. */
2091 reg = TRANS_CHICKEN2(pipe);
2092 val = I915_READ(reg);
2093 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2094 I915_WRITE(reg, val);
2095 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002096}
2097
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002098static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002099{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002100 u32 val;
2101
Daniel Vetterab9412b2013-05-03 11:49:46 +02002102 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002103 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002104 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002105 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002106 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002107 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002108
2109 /* Workaround: clear timing override bit. */
2110 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002111 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002112 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002113}
2114
2115/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002116 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002117 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002118 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002119 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002120 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002121 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002122static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002123{
Paulo Zanoni03722642014-01-17 13:51:09 -02002124 struct drm_device *dev = crtc->base.dev;
2125 struct drm_i915_private *dev_priv = dev->dev_private;
2126 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002127 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2128 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002129 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002130 int reg;
2131 u32 val;
2132
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002133 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002134 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002135 assert_sprites_disabled(dev_priv, pipe);
2136
Paulo Zanoni681e5812012-12-06 11:12:38 -02002137 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002138 pch_transcoder = TRANSCODER_A;
2139 else
2140 pch_transcoder = pipe;
2141
Jesse Barnesb24e7172011-01-04 15:09:30 -08002142 /*
2143 * A pipe without a PLL won't actually be able to drive bits from
2144 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2145 * need the check.
2146 */
Imre Deak50360402015-01-16 00:55:16 -08002147 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002148 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002149 assert_dsi_pll_enabled(dev_priv);
2150 else
2151 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002152 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002153 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002154 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002155 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002156 assert_fdi_tx_pll_enabled(dev_priv,
2157 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002158 }
2159 /* FIXME: assert CPU port conditions for SNB+ */
2160 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002161
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002162 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002163 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002164 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002165 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2166 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002167 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002168 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002169
2170 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002171 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002172}
2173
2174/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002175 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002176 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002177 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002178 * Disable the pipe of @crtc, making sure that various hardware
2179 * specific requirements are met, if applicable, e.g. plane
2180 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002181 *
2182 * Will wait until the pipe has shut down before returning.
2183 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002184static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002185{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002186 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002187 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002188 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002189 int reg;
2190 u32 val;
2191
2192 /*
2193 * Make sure planes won't keep trying to pump pixels to us,
2194 * or we might hang the display.
2195 */
2196 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002197 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002198 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002199
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002200 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002201 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002202 if ((val & PIPECONF_ENABLE) == 0)
2203 return;
2204
Ville Syrjälä67adc642014-08-15 01:21:57 +03002205 /*
2206 * Double wide has implications for planes
2207 * so best keep it disabled when not needed.
2208 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002209 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002210 val &= ~PIPECONF_DOUBLE_WIDE;
2211
2212 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002213 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2214 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002215 val &= ~PIPECONF_ENABLE;
2216
2217 I915_WRITE(reg, val);
2218 if ((val & PIPECONF_ENABLE) == 0)
2219 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002220}
2221
2222/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002223 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002224 * @plane: plane to be enabled
2225 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002226 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002227 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002228 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002229static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2230 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002231{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002232 struct drm_device *dev = plane->dev;
2233 struct drm_i915_private *dev_priv = dev->dev_private;
2234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002235
2236 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002237 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002238 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002239
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002240 dev_priv->display.update_primary_plane(crtc, plane->fb,
2241 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002242}
2243
Chris Wilson693db182013-03-05 14:52:39 +00002244static bool need_vtd_wa(struct drm_device *dev)
2245{
2246#ifdef CONFIG_INTEL_IOMMU
2247 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2248 return true;
2249#endif
2250 return false;
2251}
2252
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002253unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002254intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2255 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002256{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002257 unsigned int tile_height;
2258 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002259
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002260 switch (fb_format_modifier) {
2261 case DRM_FORMAT_MOD_NONE:
2262 tile_height = 1;
2263 break;
2264 case I915_FORMAT_MOD_X_TILED:
2265 tile_height = IS_GEN2(dev) ? 16 : 8;
2266 break;
2267 case I915_FORMAT_MOD_Y_TILED:
2268 tile_height = 32;
2269 break;
2270 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002271 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2272 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002273 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002274 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002275 tile_height = 64;
2276 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002277 case 2:
2278 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002279 tile_height = 32;
2280 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002281 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002282 tile_height = 16;
2283 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002284 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002285 WARN_ONCE(1,
2286 "128-bit pixels are not supported for display!");
2287 tile_height = 16;
2288 break;
2289 }
2290 break;
2291 default:
2292 MISSING_CASE(fb_format_modifier);
2293 tile_height = 1;
2294 break;
2295 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002296
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002297 return tile_height;
2298}
2299
2300unsigned int
2301intel_fb_align_height(struct drm_device *dev, unsigned int height,
2302 uint32_t pixel_format, uint64_t fb_format_modifier)
2303{
2304 return ALIGN(height, intel_tile_height(dev, pixel_format,
2305 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002306}
2307
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002308static int
2309intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2310 const struct drm_plane_state *plane_state)
2311{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002312 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002313
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002314 *view = i915_ggtt_view_normal;
2315
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002316 if (!plane_state)
2317 return 0;
2318
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002319 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002320 return 0;
2321
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002322 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002323
2324 info->height = fb->height;
2325 info->pixel_format = fb->pixel_format;
2326 info->pitch = fb->pitches[0];
2327 info->fb_modifier = fb->modifier[0];
2328
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002329 return 0;
2330}
2331
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002332static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2333{
2334 if (INTEL_INFO(dev_priv)->gen >= 9)
2335 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002336 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2337 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002338 return 128 * 1024;
2339 else if (INTEL_INFO(dev_priv)->gen >= 4)
2340 return 4 * 1024;
2341 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002342 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002343}
2344
Chris Wilson127bd2a2010-07-23 23:32:05 +01002345int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002346intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2347 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002348 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002349 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002350{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002351 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002352 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002353 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002354 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002355 u32 alignment;
2356 int ret;
2357
Matt Roperebcdd392014-07-09 16:22:11 -07002358 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2359
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002360 switch (fb->modifier[0]) {
2361 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002362 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002363 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002364 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002365 if (INTEL_INFO(dev)->gen >= 9)
2366 alignment = 256 * 1024;
2367 else {
2368 /* pin() will align the object as required by fence */
2369 alignment = 0;
2370 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002371 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002372 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002373 case I915_FORMAT_MOD_Yf_TILED:
2374 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2375 "Y tiling bo slipped through, driver bug!\n"))
2376 return -EINVAL;
2377 alignment = 1 * 1024 * 1024;
2378 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002379 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002380 MISSING_CASE(fb->modifier[0]);
2381 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002382 }
2383
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002384 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2385 if (ret)
2386 return ret;
2387
Chris Wilson693db182013-03-05 14:52:39 +00002388 /* Note that the w/a also requires 64 PTE of padding following the
2389 * bo. We currently fill all unused PTE with the shadow page and so
2390 * we should always have valid PTE following the scanout preventing
2391 * the VT-d warning.
2392 */
2393 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2394 alignment = 256 * 1024;
2395
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002396 /*
2397 * Global gtt pte registers are special registers which actually forward
2398 * writes to a chunk of system memory. Which means that there is no risk
2399 * that the register values disappear as soon as we call
2400 * intel_runtime_pm_put(), so it is correct to wrap only the
2401 * pin/unpin/fence and not more.
2402 */
2403 intel_runtime_pm_get(dev_priv);
2404
Chris Wilsonce453d82011-02-21 14:43:56 +00002405 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002406 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002407 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002408 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002409 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002410
2411 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2412 * fence, whereas 965+ only requires a fence if using
2413 * framebuffer compression. For simplicity, we always install
2414 * a fence as the cost is not that onerous.
2415 */
Chris Wilson06d98132012-04-17 15:31:24 +01002416 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002417 if (ret)
2418 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002419
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002420 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002421
Chris Wilsonce453d82011-02-21 14:43:56 +00002422 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002423 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002424 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002425
2426err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002427 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002428err_interruptible:
2429 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002430 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002431 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002432}
2433
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002434static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2435 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002436{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002437 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002438 struct i915_ggtt_view view;
2439 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002440
Matt Roperebcdd392014-07-09 16:22:11 -07002441 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2442
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002443 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2444 WARN_ONCE(ret, "Couldn't get view from plane state!");
2445
Chris Wilson1690e1e2011-12-14 13:57:08 +01002446 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002447 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002448}
2449
Daniel Vetterc2c75132012-07-05 12:17:30 +02002450/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2451 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002452unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2453 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002454 unsigned int tiling_mode,
2455 unsigned int cpp,
2456 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002457{
Chris Wilsonbc752862013-02-21 20:04:31 +00002458 if (tiling_mode != I915_TILING_NONE) {
2459 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002460
Chris Wilsonbc752862013-02-21 20:04:31 +00002461 tile_rows = *y / 8;
2462 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002463
Chris Wilsonbc752862013-02-21 20:04:31 +00002464 tiles = *x / (512/cpp);
2465 *x %= 512/cpp;
2466
2467 return tile_rows * pitch * 8 + tiles * 4096;
2468 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002469 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002470 unsigned int offset;
2471
2472 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002473 *y = (offset & alignment) / pitch;
2474 *x = ((offset & alignment) - *y * pitch) / cpp;
2475 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002476 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002477}
2478
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002479static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002480{
2481 switch (format) {
2482 case DISPPLANE_8BPP:
2483 return DRM_FORMAT_C8;
2484 case DISPPLANE_BGRX555:
2485 return DRM_FORMAT_XRGB1555;
2486 case DISPPLANE_BGRX565:
2487 return DRM_FORMAT_RGB565;
2488 default:
2489 case DISPPLANE_BGRX888:
2490 return DRM_FORMAT_XRGB8888;
2491 case DISPPLANE_RGBX888:
2492 return DRM_FORMAT_XBGR8888;
2493 case DISPPLANE_BGRX101010:
2494 return DRM_FORMAT_XRGB2101010;
2495 case DISPPLANE_RGBX101010:
2496 return DRM_FORMAT_XBGR2101010;
2497 }
2498}
2499
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002500static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2501{
2502 switch (format) {
2503 case PLANE_CTL_FORMAT_RGB_565:
2504 return DRM_FORMAT_RGB565;
2505 default:
2506 case PLANE_CTL_FORMAT_XRGB_8888:
2507 if (rgb_order) {
2508 if (alpha)
2509 return DRM_FORMAT_ABGR8888;
2510 else
2511 return DRM_FORMAT_XBGR8888;
2512 } else {
2513 if (alpha)
2514 return DRM_FORMAT_ARGB8888;
2515 else
2516 return DRM_FORMAT_XRGB8888;
2517 }
2518 case PLANE_CTL_FORMAT_XRGB_2101010:
2519 if (rgb_order)
2520 return DRM_FORMAT_XBGR2101010;
2521 else
2522 return DRM_FORMAT_XRGB2101010;
2523 }
2524}
2525
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002526static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002527intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2528 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002529{
2530 struct drm_device *dev = crtc->base.dev;
2531 struct drm_i915_gem_object *obj = NULL;
2532 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002533 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002534 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2535 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2536 PAGE_SIZE);
2537
2538 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002539
Chris Wilsonff2652e2014-03-10 08:07:02 +00002540 if (plane_config->size == 0)
2541 return false;
2542
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002543 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2544 base_aligned,
2545 base_aligned,
2546 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002547 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002548 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002549
Damien Lespiau49af4492015-01-20 12:51:44 +00002550 obj->tiling_mode = plane_config->tiling;
2551 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002552 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002553
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002554 mode_cmd.pixel_format = fb->pixel_format;
2555 mode_cmd.width = fb->width;
2556 mode_cmd.height = fb->height;
2557 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002558 mode_cmd.modifier[0] = fb->modifier[0];
2559 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002560
2561 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002562 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002563 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002564 DRM_DEBUG_KMS("intel fb init failed\n");
2565 goto out_unref_obj;
2566 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002567 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002568
Daniel Vetterf6936e22015-03-26 12:17:05 +01002569 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002570 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002571
2572out_unref_obj:
2573 drm_gem_object_unreference(&obj->base);
2574 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002575 return false;
2576}
2577
Matt Roperafd65eb2015-02-03 13:10:04 -08002578/* Update plane->state->fb to match plane->fb after driver-internal updates */
2579static void
2580update_state_fb(struct drm_plane *plane)
2581{
2582 if (plane->fb == plane->state->fb)
2583 return;
2584
2585 if (plane->state->fb)
2586 drm_framebuffer_unreference(plane->state->fb);
2587 plane->state->fb = plane->fb;
2588 if (plane->state->fb)
2589 drm_framebuffer_reference(plane->state->fb);
2590}
2591
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002592static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002593intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2594 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002595{
2596 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002597 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002598 struct drm_crtc *c;
2599 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002600 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002601 struct drm_plane *primary = intel_crtc->base.primary;
2602 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002603
Damien Lespiau2d140302015-02-05 17:22:18 +00002604 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002605 return;
2606
Daniel Vetterf6936e22015-03-26 12:17:05 +01002607 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002608 fb = &plane_config->fb->base;
2609 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002610 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002611
Damien Lespiau2d140302015-02-05 17:22:18 +00002612 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002613
2614 /*
2615 * Failed to alloc the obj, check to see if we should share
2616 * an fb with another CRTC instead
2617 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002618 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002619 i = to_intel_crtc(c);
2620
2621 if (c == &intel_crtc->base)
2622 continue;
2623
Matt Roper2ff8fde2014-07-08 07:50:07 -07002624 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002625 continue;
2626
Daniel Vetter88595ac2015-03-26 12:42:24 +01002627 fb = c->primary->fb;
2628 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002629 continue;
2630
Daniel Vetter88595ac2015-03-26 12:42:24 +01002631 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002632 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002633 drm_framebuffer_reference(fb);
2634 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002635 }
2636 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002637
2638 return;
2639
2640valid_fb:
2641 obj = intel_fb_obj(fb);
2642 if (obj->tiling_mode != I915_TILING_NONE)
2643 dev_priv->preserve_bios_swizzle = true;
2644
2645 primary->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002646 primary->crtc = primary->state->crtc = &intel_crtc->base;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002647 update_state_fb(primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002648 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Daniel Vetter88595ac2015-03-26 12:42:24 +01002649 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002650}
2651
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002652static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2653 struct drm_framebuffer *fb,
2654 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002655{
2656 struct drm_device *dev = crtc->dev;
2657 struct drm_i915_private *dev_priv = dev->dev_private;
2658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002659 struct drm_plane *primary = crtc->primary;
2660 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002661 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002662 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002663 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002664 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002665 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302666 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002667
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002668 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002669 I915_WRITE(reg, 0);
2670 if (INTEL_INFO(dev)->gen >= 4)
2671 I915_WRITE(DSPSURF(plane), 0);
2672 else
2673 I915_WRITE(DSPADDR(plane), 0);
2674 POSTING_READ(reg);
2675 return;
2676 }
2677
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002678 obj = intel_fb_obj(fb);
2679 if (WARN_ON(obj == NULL))
2680 return;
2681
2682 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2683
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002684 dspcntr = DISPPLANE_GAMMA_ENABLE;
2685
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002686 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002687
2688 if (INTEL_INFO(dev)->gen < 4) {
2689 if (intel_crtc->pipe == PIPE_B)
2690 dspcntr |= DISPPLANE_SEL_PIPE_B;
2691
2692 /* pipesrc and dspsize control the size that is scaled from,
2693 * which should always be the user's requested size.
2694 */
2695 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002696 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2697 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002698 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002699 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2700 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002701 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2702 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002703 I915_WRITE(PRIMPOS(plane), 0);
2704 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002705 }
2706
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707 switch (fb->pixel_format) {
2708 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002709 dspcntr |= DISPPLANE_8BPP;
2710 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002711 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002712 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002713 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002714 case DRM_FORMAT_RGB565:
2715 dspcntr |= DISPPLANE_BGRX565;
2716 break;
2717 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002718 dspcntr |= DISPPLANE_BGRX888;
2719 break;
2720 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002721 dspcntr |= DISPPLANE_RGBX888;
2722 break;
2723 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002724 dspcntr |= DISPPLANE_BGRX101010;
2725 break;
2726 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002727 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002728 break;
2729 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002730 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002731 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002732
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002733 if (INTEL_INFO(dev)->gen >= 4 &&
2734 obj->tiling_mode != I915_TILING_NONE)
2735 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002736
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002737 if (IS_G4X(dev))
2738 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2739
Ville Syrjäläb98971272014-08-27 16:51:22 +03002740 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002741
Daniel Vetterc2c75132012-07-05 12:17:30 +02002742 if (INTEL_INFO(dev)->gen >= 4) {
2743 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002744 intel_gen4_compute_page_offset(dev_priv,
2745 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002746 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002747 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002748 linear_offset -= intel_crtc->dspaddr_offset;
2749 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002750 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002751 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002752
Matt Roper8e7d6882015-01-21 16:35:41 -08002753 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302754 dspcntr |= DISPPLANE_ROTATE_180;
2755
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002756 x += (intel_crtc->config->pipe_src_w - 1);
2757 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302758
2759 /* Finding the last pixel of the last line of the display
2760 data and adding to linear_offset*/
2761 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002762 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2763 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302764 }
2765
2766 I915_WRITE(reg, dspcntr);
2767
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002768 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002769 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002770 I915_WRITE(DSPSURF(plane),
2771 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002772 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002773 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002774 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002775 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002776 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002777}
2778
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002779static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2780 struct drm_framebuffer *fb,
2781 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002782{
2783 struct drm_device *dev = crtc->dev;
2784 struct drm_i915_private *dev_priv = dev->dev_private;
2785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002786 struct drm_plane *primary = crtc->primary;
2787 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002788 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002789 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002790 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002791 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002792 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302793 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002794
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002795 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002796 I915_WRITE(reg, 0);
2797 I915_WRITE(DSPSURF(plane), 0);
2798 POSTING_READ(reg);
2799 return;
2800 }
2801
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002802 obj = intel_fb_obj(fb);
2803 if (WARN_ON(obj == NULL))
2804 return;
2805
2806 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2807
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002808 dspcntr = DISPPLANE_GAMMA_ENABLE;
2809
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002810 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002811
2812 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2813 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2814
Ville Syrjälä57779d02012-10-31 17:50:14 +02002815 switch (fb->pixel_format) {
2816 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002817 dspcntr |= DISPPLANE_8BPP;
2818 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002819 case DRM_FORMAT_RGB565:
2820 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002821 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002822 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002823 dspcntr |= DISPPLANE_BGRX888;
2824 break;
2825 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002826 dspcntr |= DISPPLANE_RGBX888;
2827 break;
2828 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002829 dspcntr |= DISPPLANE_BGRX101010;
2830 break;
2831 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002832 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002833 break;
2834 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002835 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002836 }
2837
2838 if (obj->tiling_mode != I915_TILING_NONE)
2839 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002840
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002841 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002842 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002843
Ville Syrjäläb98971272014-08-27 16:51:22 +03002844 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002845 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002846 intel_gen4_compute_page_offset(dev_priv,
2847 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002848 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002849 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002850 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002851 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302852 dspcntr |= DISPPLANE_ROTATE_180;
2853
2854 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002855 x += (intel_crtc->config->pipe_src_w - 1);
2856 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302857
2858 /* Finding the last pixel of the last line of the display
2859 data and adding to linear_offset*/
2860 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002861 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2862 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302863 }
2864 }
2865
2866 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002867
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002868 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002869 I915_WRITE(DSPSURF(plane),
2870 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002871 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002872 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2873 } else {
2874 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2875 I915_WRITE(DSPLINOFF(plane), linear_offset);
2876 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002877 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002878}
2879
Damien Lespiaub3218032015-02-27 11:15:18 +00002880u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2881 uint32_t pixel_format)
2882{
2883 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2884
2885 /*
2886 * The stride is either expressed as a multiple of 64 bytes
2887 * chunks for linear buffers or in number of tiles for tiled
2888 * buffers.
2889 */
2890 switch (fb_modifier) {
2891 case DRM_FORMAT_MOD_NONE:
2892 return 64;
2893 case I915_FORMAT_MOD_X_TILED:
2894 if (INTEL_INFO(dev)->gen == 2)
2895 return 128;
2896 return 512;
2897 case I915_FORMAT_MOD_Y_TILED:
2898 /* No need to check for old gens and Y tiling since this is
2899 * about the display engine and those will be blocked before
2900 * we get here.
2901 */
2902 return 128;
2903 case I915_FORMAT_MOD_Yf_TILED:
2904 if (bits_per_pixel == 8)
2905 return 64;
2906 else
2907 return 128;
2908 default:
2909 MISSING_CASE(fb_modifier);
2910 return 64;
2911 }
2912}
2913
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002914unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2915 struct drm_i915_gem_object *obj)
2916{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002917 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002918
2919 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002920 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002921
2922 return i915_gem_obj_ggtt_offset_view(obj, view);
2923}
2924
Chandra Kondurua1b22782015-04-07 15:28:45 -07002925/*
2926 * This function detaches (aka. unbinds) unused scalers in hardware
2927 */
2928void skl_detach_scalers(struct intel_crtc *intel_crtc)
2929{
2930 struct drm_device *dev;
2931 struct drm_i915_private *dev_priv;
2932 struct intel_crtc_scaler_state *scaler_state;
2933 int i;
2934
2935 if (!intel_crtc || !intel_crtc->config)
2936 return;
2937
2938 dev = intel_crtc->base.dev;
2939 dev_priv = dev->dev_private;
2940 scaler_state = &intel_crtc->config->scaler_state;
2941
2942 /* loop through and disable scalers that aren't in use */
2943 for (i = 0; i < intel_crtc->num_scalers; i++) {
2944 if (!scaler_state->scalers[i].in_use) {
2945 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2946 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2947 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2948 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2949 intel_crtc->base.base.id, intel_crtc->pipe, i);
2950 }
2951 }
2952}
2953
Chandra Konduru6156a452015-04-27 13:48:39 -07002954u32 skl_plane_ctl_format(uint32_t pixel_format)
2955{
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002957 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002962 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 /*
2966 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2967 * to be already pre-multiplied. We need to add a knob (or a different
2968 * DRM_FORMAT) for user-space to configure that.
2969 */
2970 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002974 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002977 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002979 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002980 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002981 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002982 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002983 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002984 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002985 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002986 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002987 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002988 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002989 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002990 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002991
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002992 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002993}
2994
2995u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2996{
Chandra Konduru6156a452015-04-27 13:48:39 -07002997 switch (fb_modifier) {
2998 case DRM_FORMAT_MOD_NONE:
2999 break;
3000 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003001 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003002 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003003 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003004 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003005 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006 default:
3007 MISSING_CASE(fb_modifier);
3008 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003009
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003010 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003011}
3012
3013u32 skl_plane_ctl_rotation(unsigned int rotation)
3014{
Chandra Konduru6156a452015-04-27 13:48:39 -07003015 switch (rotation) {
3016 case BIT(DRM_ROTATE_0):
3017 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303018 /*
3019 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3020 * while i915 HW rotation is clockwise, thats why this swapping.
3021 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303023 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003024 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003025 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003026 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303027 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003028 default:
3029 MISSING_CASE(rotation);
3030 }
3031
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003032 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003033}
3034
Damien Lespiau70d21f02013-07-03 21:06:04 +01003035static void skylake_update_primary_plane(struct drm_crtc *crtc,
3036 struct drm_framebuffer *fb,
3037 int x, int y)
3038{
3039 struct drm_device *dev = crtc->dev;
3040 struct drm_i915_private *dev_priv = dev->dev_private;
3041 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003042 struct drm_plane *plane = crtc->primary;
3043 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003044 struct drm_i915_gem_object *obj;
3045 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303046 u32 plane_ctl, stride_div, stride;
3047 u32 tile_height, plane_offset, plane_size;
3048 unsigned int rotation;
3049 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003050 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003051 struct intel_crtc_state *crtc_state = intel_crtc->config;
3052 struct intel_plane_state *plane_state;
3053 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3054 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3055 int scaler_id = -1;
3056
Chandra Konduru6156a452015-04-27 13:48:39 -07003057 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003058
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003059 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003060 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3061 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3062 POSTING_READ(PLANE_CTL(pipe, 0));
3063 return;
3064 }
3065
3066 plane_ctl = PLANE_CTL_ENABLE |
3067 PLANE_CTL_PIPE_GAMMA_ENABLE |
3068 PLANE_CTL_PIPE_CSC_ENABLE;
3069
Chandra Konduru6156a452015-04-27 13:48:39 -07003070 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3071 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003072 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303073
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303074 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003075 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003076
Damien Lespiaub3218032015-02-27 11:15:18 +00003077 obj = intel_fb_obj(fb);
3078 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3079 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303080 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3081
Chandra Konduru6156a452015-04-27 13:48:39 -07003082 /*
3083 * FIXME: intel_plane_state->src, dst aren't set when transitional
3084 * update_plane helpers are called from legacy paths.
3085 * Once full atomic crtc is available, below check can be avoided.
3086 */
3087 if (drm_rect_width(&plane_state->src)) {
3088 scaler_id = plane_state->scaler_id;
3089 src_x = plane_state->src.x1 >> 16;
3090 src_y = plane_state->src.y1 >> 16;
3091 src_w = drm_rect_width(&plane_state->src) >> 16;
3092 src_h = drm_rect_height(&plane_state->src) >> 16;
3093 dst_x = plane_state->dst.x1;
3094 dst_y = plane_state->dst.y1;
3095 dst_w = drm_rect_width(&plane_state->dst);
3096 dst_h = drm_rect_height(&plane_state->dst);
3097
3098 WARN_ON(x != src_x || y != src_y);
3099 } else {
3100 src_w = intel_crtc->config->pipe_src_w;
3101 src_h = intel_crtc->config->pipe_src_h;
3102 }
3103
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303104 if (intel_rotation_90_or_270(rotation)) {
3105 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003106 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303107 fb->modifier[0]);
3108 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003109 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303110 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003111 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303112 } else {
3113 stride = fb->pitches[0] / stride_div;
3114 x_offset = x;
3115 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003116 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303117 }
3118 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003119
Damien Lespiau70d21f02013-07-03 21:06:04 +01003120 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303121 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3122 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3123 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003124
3125 if (scaler_id >= 0) {
3126 uint32_t ps_ctrl = 0;
3127
3128 WARN_ON(!dst_w || !dst_h);
3129 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3130 crtc_state->scaler_state.scalers[scaler_id].mode;
3131 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3132 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3133 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3134 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3135 I915_WRITE(PLANE_POS(pipe, 0), 0);
3136 } else {
3137 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3138 }
3139
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003140 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003141
3142 POSTING_READ(PLANE_SURF(pipe, 0));
3143}
3144
Jesse Barnes17638cd2011-06-24 12:19:23 -07003145/* Assume fb object is pinned & idle & fenced and just update base pointers */
3146static int
3147intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3148 int x, int y, enum mode_set_atomic state)
3149{
3150 struct drm_device *dev = crtc->dev;
3151 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003152
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003153 if (dev_priv->display.disable_fbc)
3154 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003155
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003156 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3157
3158 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003159}
3160
Ville Syrjälä75147472014-11-24 18:28:11 +02003161static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003162{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003163 struct drm_crtc *crtc;
3164
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003165 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3167 enum plane plane = intel_crtc->plane;
3168
3169 intel_prepare_page_flip(dev, plane);
3170 intel_finish_page_flip_plane(dev, plane);
3171 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003172}
3173
3174static void intel_update_primary_planes(struct drm_device *dev)
3175{
3176 struct drm_i915_private *dev_priv = dev->dev_private;
3177 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003178
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003179 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3181
Rob Clark51fd3712013-11-19 12:10:12 -05003182 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003183 /*
3184 * FIXME: Once we have proper support for primary planes (and
3185 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003186 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003187 */
Matt Roperf4510a22014-04-01 15:22:40 -07003188 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003189 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003190 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003191 crtc->x,
3192 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003193 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003194 }
3195}
3196
Ville Syrjälä75147472014-11-24 18:28:11 +02003197void intel_prepare_reset(struct drm_device *dev)
3198{
3199 /* no reset support for gen2 */
3200 if (IS_GEN2(dev))
3201 return;
3202
3203 /* reset doesn't touch the display */
3204 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3205 return;
3206
3207 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003208 /*
3209 * Disabling the crtcs gracefully seems nicer. Also the
3210 * g33 docs say we should at least disable all the planes.
3211 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003212 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003213}
3214
3215void intel_finish_reset(struct drm_device *dev)
3216{
3217 struct drm_i915_private *dev_priv = to_i915(dev);
3218
3219 /*
3220 * Flips in the rings will be nuked by the reset,
3221 * so complete all pending flips so that user space
3222 * will get its events and not get stuck.
3223 */
3224 intel_complete_page_flips(dev);
3225
3226 /* no reset support for gen2 */
3227 if (IS_GEN2(dev))
3228 return;
3229
3230 /* reset doesn't touch the display */
3231 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3232 /*
3233 * Flips in the rings have been nuked by the reset,
3234 * so update the base address of all primary
3235 * planes to the the last fb to make sure we're
3236 * showing the correct fb after a reset.
3237 */
3238 intel_update_primary_planes(dev);
3239 return;
3240 }
3241
3242 /*
3243 * The display has been reset as well,
3244 * so need a full re-initialization.
3245 */
3246 intel_runtime_pm_disable_interrupts(dev_priv);
3247 intel_runtime_pm_enable_interrupts(dev_priv);
3248
3249 intel_modeset_init_hw(dev);
3250
3251 spin_lock_irq(&dev_priv->irq_lock);
3252 if (dev_priv->display.hpd_irq_setup)
3253 dev_priv->display.hpd_irq_setup(dev);
3254 spin_unlock_irq(&dev_priv->irq_lock);
3255
3256 intel_modeset_setup_hw_state(dev, true);
3257
3258 intel_hpd_init(dev_priv);
3259
3260 drm_modeset_unlock_all(dev);
3261}
3262
Chris Wilson2e2f3512015-04-27 13:41:14 +01003263static void
Chris Wilson14667a42012-04-03 17:58:35 +01003264intel_finish_fb(struct drm_framebuffer *old_fb)
3265{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003266 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003267 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003268 bool was_interruptible = dev_priv->mm.interruptible;
3269 int ret;
3270
Chris Wilson14667a42012-04-03 17:58:35 +01003271 /* Big Hammer, we also need to ensure that any pending
3272 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3273 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003274 * framebuffer. Note that we rely on userspace rendering
3275 * into the buffer attached to the pipe they are waiting
3276 * on. If not, userspace generates a GPU hang with IPEHR
3277 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003278 *
3279 * This should only fail upon a hung GPU, in which case we
3280 * can safely continue.
3281 */
3282 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003283 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003284 dev_priv->mm.interruptible = was_interruptible;
3285
Chris Wilson2e2f3512015-04-27 13:41:14 +01003286 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003287}
3288
Chris Wilson7d5e3792014-03-04 13:15:08 +00003289static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3290{
3291 struct drm_device *dev = crtc->dev;
3292 struct drm_i915_private *dev_priv = dev->dev_private;
3293 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003294 bool pending;
3295
3296 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3297 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3298 return false;
3299
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003300 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003301 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003302 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003303
3304 return pending;
3305}
3306
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003307static void intel_update_pipe_size(struct intel_crtc *crtc)
3308{
3309 struct drm_device *dev = crtc->base.dev;
3310 struct drm_i915_private *dev_priv = dev->dev_private;
3311 const struct drm_display_mode *adjusted_mode;
3312
3313 if (!i915.fastboot)
3314 return;
3315
3316 /*
3317 * Update pipe size and adjust fitter if needed: the reason for this is
3318 * that in compute_mode_changes we check the native mode (not the pfit
3319 * mode) to see if we can flip rather than do a full mode set. In the
3320 * fastboot case, we'll flip, but if we don't update the pipesrc and
3321 * pfit state, we'll end up with a big fb scanned out into the wrong
3322 * sized surface.
3323 *
3324 * To fix this properly, we need to hoist the checks up into
3325 * compute_mode_changes (or above), check the actual pfit state and
3326 * whether the platform allows pfit disable with pipe active, and only
3327 * then update the pipesrc and pfit state, even on the flip path.
3328 */
3329
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003330 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003331
3332 I915_WRITE(PIPESRC(crtc->pipe),
3333 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3334 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003335 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003336 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3337 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003338 I915_WRITE(PF_CTL(crtc->pipe), 0);
3339 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3340 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3341 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003342 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3343 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003344}
3345
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003346static void intel_fdi_normal_train(struct drm_crtc *crtc)
3347{
3348 struct drm_device *dev = crtc->dev;
3349 struct drm_i915_private *dev_priv = dev->dev_private;
3350 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3351 int pipe = intel_crtc->pipe;
3352 u32 reg, temp;
3353
3354 /* enable normal train */
3355 reg = FDI_TX_CTL(pipe);
3356 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003357 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003358 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3359 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003360 } else {
3361 temp &= ~FDI_LINK_TRAIN_NONE;
3362 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003363 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003364 I915_WRITE(reg, temp);
3365
3366 reg = FDI_RX_CTL(pipe);
3367 temp = I915_READ(reg);
3368 if (HAS_PCH_CPT(dev)) {
3369 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3370 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3371 } else {
3372 temp &= ~FDI_LINK_TRAIN_NONE;
3373 temp |= FDI_LINK_TRAIN_NONE;
3374 }
3375 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3376
3377 /* wait one idle pattern time */
3378 POSTING_READ(reg);
3379 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003380
3381 /* IVB wants error correction enabled */
3382 if (IS_IVYBRIDGE(dev))
3383 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3384 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003385}
3386
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003387/* The FDI link training functions for ILK/Ibexpeak. */
3388static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3389{
3390 struct drm_device *dev = crtc->dev;
3391 struct drm_i915_private *dev_priv = dev->dev_private;
3392 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3393 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003394 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003395
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003396 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003397 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003398
Adam Jacksone1a44742010-06-25 15:32:14 -04003399 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3400 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003401 reg = FDI_RX_IMR(pipe);
3402 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003403 temp &= ~FDI_RX_SYMBOL_LOCK;
3404 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 I915_WRITE(reg, temp);
3406 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003407 udelay(150);
3408
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003409 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003410 reg = FDI_TX_CTL(pipe);
3411 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003412 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003413 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003414 temp &= ~FDI_LINK_TRAIN_NONE;
3415 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003416 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417
Chris Wilson5eddb702010-09-11 13:48:45 +01003418 reg = FDI_RX_CTL(pipe);
3419 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003420 temp &= ~FDI_LINK_TRAIN_NONE;
3421 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003422 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3423
3424 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425 udelay(150);
3426
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003427 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003428 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3429 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3430 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003431
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003433 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3436
3437 if ((temp & FDI_RX_BIT_LOCK)) {
3438 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003439 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440 break;
3441 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003443 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003445
3446 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 reg = FDI_TX_CTL(pipe);
3448 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449 temp &= ~FDI_LINK_TRAIN_NONE;
3450 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003452
Chris Wilson5eddb702010-09-11 13:48:45 +01003453 reg = FDI_RX_CTL(pipe);
3454 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003455 temp &= ~FDI_LINK_TRAIN_NONE;
3456 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 I915_WRITE(reg, temp);
3458
3459 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 udelay(150);
3461
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003463 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003464 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003465 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3466
3467 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003468 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469 DRM_DEBUG_KMS("FDI train 2 done.\n");
3470 break;
3471 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003472 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003473 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003474 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003475
3476 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003477
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003478}
3479
Akshay Joshi0206e352011-08-16 15:34:10 -04003480static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3482 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3483 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3484 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3485};
3486
3487/* The FDI link training functions for SNB/Cougarpoint. */
3488static void gen6_fdi_link_train(struct drm_crtc *crtc)
3489{
3490 struct drm_device *dev = crtc->dev;
3491 struct drm_i915_private *dev_priv = dev->dev_private;
3492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3493 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003494 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495
Adam Jacksone1a44742010-06-25 15:32:14 -04003496 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3497 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003498 reg = FDI_RX_IMR(pipe);
3499 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003500 temp &= ~FDI_RX_SYMBOL_LOCK;
3501 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 I915_WRITE(reg, temp);
3503
3504 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003505 udelay(150);
3506
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003507 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003508 reg = FDI_TX_CTL(pipe);
3509 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003510 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003511 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003512 temp &= ~FDI_LINK_TRAIN_NONE;
3513 temp |= FDI_LINK_TRAIN_PATTERN_1;
3514 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3515 /* SNB-B */
3516 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518
Daniel Vetterd74cf322012-10-26 10:58:13 +02003519 I915_WRITE(FDI_RX_MISC(pipe),
3520 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3521
Chris Wilson5eddb702010-09-11 13:48:45 +01003522 reg = FDI_RX_CTL(pipe);
3523 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524 if (HAS_PCH_CPT(dev)) {
3525 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3526 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3527 } else {
3528 temp &= ~FDI_LINK_TRAIN_NONE;
3529 temp |= FDI_LINK_TRAIN_PATTERN_1;
3530 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003531 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3532
3533 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534 udelay(150);
3535
Akshay Joshi0206e352011-08-16 15:34:10 -04003536 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003537 reg = FDI_TX_CTL(pipe);
3538 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003539 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3540 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003541 I915_WRITE(reg, temp);
3542
3543 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003544 udelay(500);
3545
Sean Paulfa37d392012-03-02 12:53:39 -05003546 for (retry = 0; retry < 5; retry++) {
3547 reg = FDI_RX_IIR(pipe);
3548 temp = I915_READ(reg);
3549 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3550 if (temp & FDI_RX_BIT_LOCK) {
3551 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3552 DRM_DEBUG_KMS("FDI train 1 done.\n");
3553 break;
3554 }
3555 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003556 }
Sean Paulfa37d392012-03-02 12:53:39 -05003557 if (retry < 5)
3558 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003559 }
3560 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003562
3563 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003564 reg = FDI_TX_CTL(pipe);
3565 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003566 temp &= ~FDI_LINK_TRAIN_NONE;
3567 temp |= FDI_LINK_TRAIN_PATTERN_2;
3568 if (IS_GEN6(dev)) {
3569 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3570 /* SNB-B */
3571 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3572 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003573 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574
Chris Wilson5eddb702010-09-11 13:48:45 +01003575 reg = FDI_RX_CTL(pipe);
3576 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003577 if (HAS_PCH_CPT(dev)) {
3578 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3579 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3580 } else {
3581 temp &= ~FDI_LINK_TRAIN_NONE;
3582 temp |= FDI_LINK_TRAIN_PATTERN_2;
3583 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003584 I915_WRITE(reg, temp);
3585
3586 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003587 udelay(150);
3588
Akshay Joshi0206e352011-08-16 15:34:10 -04003589 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003590 reg = FDI_TX_CTL(pipe);
3591 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003592 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3593 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003594 I915_WRITE(reg, temp);
3595
3596 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003597 udelay(500);
3598
Sean Paulfa37d392012-03-02 12:53:39 -05003599 for (retry = 0; retry < 5; retry++) {
3600 reg = FDI_RX_IIR(pipe);
3601 temp = I915_READ(reg);
3602 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3603 if (temp & FDI_RX_SYMBOL_LOCK) {
3604 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3605 DRM_DEBUG_KMS("FDI train 2 done.\n");
3606 break;
3607 }
3608 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003609 }
Sean Paulfa37d392012-03-02 12:53:39 -05003610 if (retry < 5)
3611 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003612 }
3613 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003614 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003615
3616 DRM_DEBUG_KMS("FDI train done.\n");
3617}
3618
Jesse Barnes357555c2011-04-28 15:09:55 -07003619/* Manual link training for Ivy Bridge A0 parts */
3620static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3621{
3622 struct drm_device *dev = crtc->dev;
3623 struct drm_i915_private *dev_priv = dev->dev_private;
3624 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3625 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003626 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003627
3628 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3629 for train result */
3630 reg = FDI_RX_IMR(pipe);
3631 temp = I915_READ(reg);
3632 temp &= ~FDI_RX_SYMBOL_LOCK;
3633 temp &= ~FDI_RX_BIT_LOCK;
3634 I915_WRITE(reg, temp);
3635
3636 POSTING_READ(reg);
3637 udelay(150);
3638
Daniel Vetter01a415f2012-10-27 15:58:40 +02003639 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3640 I915_READ(FDI_RX_IIR(pipe)));
3641
Jesse Barnes139ccd32013-08-19 11:04:55 -07003642 /* Try each vswing and preemphasis setting twice before moving on */
3643 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3644 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003645 reg = FDI_TX_CTL(pipe);
3646 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003647 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3648 temp &= ~FDI_TX_ENABLE;
3649 I915_WRITE(reg, temp);
3650
3651 reg = FDI_RX_CTL(pipe);
3652 temp = I915_READ(reg);
3653 temp &= ~FDI_LINK_TRAIN_AUTO;
3654 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3655 temp &= ~FDI_RX_ENABLE;
3656 I915_WRITE(reg, temp);
3657
3658 /* enable CPU FDI TX and PCH FDI RX */
3659 reg = FDI_TX_CTL(pipe);
3660 temp = I915_READ(reg);
3661 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003662 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003663 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003664 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003665 temp |= snb_b_fdi_train_param[j/2];
3666 temp |= FDI_COMPOSITE_SYNC;
3667 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3668
3669 I915_WRITE(FDI_RX_MISC(pipe),
3670 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3671
3672 reg = FDI_RX_CTL(pipe);
3673 temp = I915_READ(reg);
3674 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3675 temp |= FDI_COMPOSITE_SYNC;
3676 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3677
3678 POSTING_READ(reg);
3679 udelay(1); /* should be 0.5us */
3680
3681 for (i = 0; i < 4; i++) {
3682 reg = FDI_RX_IIR(pipe);
3683 temp = I915_READ(reg);
3684 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3685
3686 if (temp & FDI_RX_BIT_LOCK ||
3687 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3688 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3689 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3690 i);
3691 break;
3692 }
3693 udelay(1); /* should be 0.5us */
3694 }
3695 if (i == 4) {
3696 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3697 continue;
3698 }
3699
3700 /* Train 2 */
3701 reg = FDI_TX_CTL(pipe);
3702 temp = I915_READ(reg);
3703 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3704 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3705 I915_WRITE(reg, temp);
3706
3707 reg = FDI_RX_CTL(pipe);
3708 temp = I915_READ(reg);
3709 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3710 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003711 I915_WRITE(reg, temp);
3712
3713 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003714 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003715
Jesse Barnes139ccd32013-08-19 11:04:55 -07003716 for (i = 0; i < 4; i++) {
3717 reg = FDI_RX_IIR(pipe);
3718 temp = I915_READ(reg);
3719 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003720
Jesse Barnes139ccd32013-08-19 11:04:55 -07003721 if (temp & FDI_RX_SYMBOL_LOCK ||
3722 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3723 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3724 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3725 i);
3726 goto train_done;
3727 }
3728 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003729 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003730 if (i == 4)
3731 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003732 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003733
Jesse Barnes139ccd32013-08-19 11:04:55 -07003734train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003735 DRM_DEBUG_KMS("FDI train done.\n");
3736}
3737
Daniel Vetter88cefb62012-08-12 19:27:14 +02003738static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003739{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003740 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003741 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003742 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003743 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003744
Jesse Barnesc64e3112010-09-10 11:27:03 -07003745
Jesse Barnes0e23b992010-09-10 11:10:00 -07003746 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003747 reg = FDI_RX_CTL(pipe);
3748 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003749 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003750 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003751 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003752 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3753
3754 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003755 udelay(200);
3756
3757 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003758 temp = I915_READ(reg);
3759 I915_WRITE(reg, temp | FDI_PCDCLK);
3760
3761 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003762 udelay(200);
3763
Paulo Zanoni20749732012-11-23 15:30:38 -02003764 /* Enable CPU FDI TX PLL, always on for Ironlake */
3765 reg = FDI_TX_CTL(pipe);
3766 temp = I915_READ(reg);
3767 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3768 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003769
Paulo Zanoni20749732012-11-23 15:30:38 -02003770 POSTING_READ(reg);
3771 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003772 }
3773}
3774
Daniel Vetter88cefb62012-08-12 19:27:14 +02003775static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3776{
3777 struct drm_device *dev = intel_crtc->base.dev;
3778 struct drm_i915_private *dev_priv = dev->dev_private;
3779 int pipe = intel_crtc->pipe;
3780 u32 reg, temp;
3781
3782 /* Switch from PCDclk to Rawclk */
3783 reg = FDI_RX_CTL(pipe);
3784 temp = I915_READ(reg);
3785 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3786
3787 /* Disable CPU FDI TX PLL */
3788 reg = FDI_TX_CTL(pipe);
3789 temp = I915_READ(reg);
3790 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3791
3792 POSTING_READ(reg);
3793 udelay(100);
3794
3795 reg = FDI_RX_CTL(pipe);
3796 temp = I915_READ(reg);
3797 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3798
3799 /* Wait for the clocks to turn off. */
3800 POSTING_READ(reg);
3801 udelay(100);
3802}
3803
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003804static void ironlake_fdi_disable(struct drm_crtc *crtc)
3805{
3806 struct drm_device *dev = crtc->dev;
3807 struct drm_i915_private *dev_priv = dev->dev_private;
3808 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3809 int pipe = intel_crtc->pipe;
3810 u32 reg, temp;
3811
3812 /* disable CPU FDI tx and PCH FDI rx */
3813 reg = FDI_TX_CTL(pipe);
3814 temp = I915_READ(reg);
3815 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3816 POSTING_READ(reg);
3817
3818 reg = FDI_RX_CTL(pipe);
3819 temp = I915_READ(reg);
3820 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003821 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003822 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3823
3824 POSTING_READ(reg);
3825 udelay(100);
3826
3827 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003828 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003829 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003830
3831 /* still set train pattern 1 */
3832 reg = FDI_TX_CTL(pipe);
3833 temp = I915_READ(reg);
3834 temp &= ~FDI_LINK_TRAIN_NONE;
3835 temp |= FDI_LINK_TRAIN_PATTERN_1;
3836 I915_WRITE(reg, temp);
3837
3838 reg = FDI_RX_CTL(pipe);
3839 temp = I915_READ(reg);
3840 if (HAS_PCH_CPT(dev)) {
3841 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3842 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3843 } else {
3844 temp &= ~FDI_LINK_TRAIN_NONE;
3845 temp |= FDI_LINK_TRAIN_PATTERN_1;
3846 }
3847 /* BPC in FDI rx is consistent with that in PIPECONF */
3848 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003849 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003850 I915_WRITE(reg, temp);
3851
3852 POSTING_READ(reg);
3853 udelay(100);
3854}
3855
Chris Wilson5dce5b932014-01-20 10:17:36 +00003856bool intel_has_pending_fb_unpin(struct drm_device *dev)
3857{
3858 struct intel_crtc *crtc;
3859
3860 /* Note that we don't need to be called with mode_config.lock here
3861 * as our list of CRTC objects is static for the lifetime of the
3862 * device and so cannot disappear as we iterate. Similarly, we can
3863 * happily treat the predicates as racy, atomic checks as userspace
3864 * cannot claim and pin a new fb without at least acquring the
3865 * struct_mutex and so serialising with us.
3866 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003867 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003868 if (atomic_read(&crtc->unpin_work_count) == 0)
3869 continue;
3870
3871 if (crtc->unpin_work)
3872 intel_wait_for_vblank(dev, crtc->pipe);
3873
3874 return true;
3875 }
3876
3877 return false;
3878}
3879
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003880static void page_flip_completed(struct intel_crtc *intel_crtc)
3881{
3882 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3883 struct intel_unpin_work *work = intel_crtc->unpin_work;
3884
3885 /* ensure that the unpin work is consistent wrt ->pending. */
3886 smp_rmb();
3887 intel_crtc->unpin_work = NULL;
3888
3889 if (work->event)
3890 drm_send_vblank_event(intel_crtc->base.dev,
3891 intel_crtc->pipe,
3892 work->event);
3893
3894 drm_crtc_vblank_put(&intel_crtc->base);
3895
3896 wake_up_all(&dev_priv->pending_flip_queue);
3897 queue_work(dev_priv->wq, &work->work);
3898
3899 trace_i915_flip_complete(intel_crtc->plane,
3900 work->pending_flip_obj);
3901}
3902
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003903void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003904{
Chris Wilson0f911282012-04-17 10:05:38 +01003905 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003906 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003907
Daniel Vetter2c10d572012-12-20 21:24:07 +01003908 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003909 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3910 !intel_crtc_has_pending_flip(crtc),
3911 60*HZ) == 0)) {
3912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003913
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003914 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003915 if (intel_crtc->unpin_work) {
3916 WARN_ONCE(1, "Removing stuck page flip\n");
3917 page_flip_completed(intel_crtc);
3918 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003919 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003920 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003921
Chris Wilson975d5682014-08-20 13:13:34 +01003922 if (crtc->primary->fb) {
3923 mutex_lock(&dev->struct_mutex);
3924 intel_finish_fb(crtc->primary->fb);
3925 mutex_unlock(&dev->struct_mutex);
3926 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003927}
3928
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003929/* Program iCLKIP clock to the desired frequency */
3930static void lpt_program_iclkip(struct drm_crtc *crtc)
3931{
3932 struct drm_device *dev = crtc->dev;
3933 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003934 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003935 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3936 u32 temp;
3937
Ville Syrjäläa5805162015-05-26 20:42:30 +03003938 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003939
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003940 /* It is necessary to ungate the pixclk gate prior to programming
3941 * the divisors, and gate it back when it is done.
3942 */
3943 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3944
3945 /* Disable SSCCTL */
3946 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003947 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3948 SBI_SSCCTL_DISABLE,
3949 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003950
3951 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003952 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003953 auxdiv = 1;
3954 divsel = 0x41;
3955 phaseinc = 0x20;
3956 } else {
3957 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003958 * but the adjusted_mode->crtc_clock in in KHz. To get the
3959 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003960 * convert the virtual clock precision to KHz here for higher
3961 * precision.
3962 */
3963 u32 iclk_virtual_root_freq = 172800 * 1000;
3964 u32 iclk_pi_range = 64;
3965 u32 desired_divisor, msb_divisor_value, pi_value;
3966
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003967 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003968 msb_divisor_value = desired_divisor / iclk_pi_range;
3969 pi_value = desired_divisor % iclk_pi_range;
3970
3971 auxdiv = 0;
3972 divsel = msb_divisor_value - 2;
3973 phaseinc = pi_value;
3974 }
3975
3976 /* This should not happen with any sane values */
3977 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3978 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3979 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3980 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3981
3982 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 +03003983 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003984 auxdiv,
3985 divsel,
3986 phasedir,
3987 phaseinc);
3988
3989 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003990 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003991 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3992 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3993 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3994 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3995 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3996 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003997 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003998
3999 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004000 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004001 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4002 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004003 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004004
4005 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004006 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004007 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004008 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004009
4010 /* Wait for initialization time */
4011 udelay(24);
4012
4013 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004014
Ville Syrjäläa5805162015-05-26 20:42:30 +03004015 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004016}
4017
Daniel Vetter275f01b22013-05-03 11:49:47 +02004018static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4019 enum pipe pch_transcoder)
4020{
4021 struct drm_device *dev = crtc->base.dev;
4022 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004023 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004024
4025 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4026 I915_READ(HTOTAL(cpu_transcoder)));
4027 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4028 I915_READ(HBLANK(cpu_transcoder)));
4029 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4030 I915_READ(HSYNC(cpu_transcoder)));
4031
4032 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4033 I915_READ(VTOTAL(cpu_transcoder)));
4034 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4035 I915_READ(VBLANK(cpu_transcoder)));
4036 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4037 I915_READ(VSYNC(cpu_transcoder)));
4038 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4039 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4040}
4041
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004042static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004043{
4044 struct drm_i915_private *dev_priv = dev->dev_private;
4045 uint32_t temp;
4046
4047 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004048 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004049 return;
4050
4051 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4052 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4053
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004054 temp &= ~FDI_BC_BIFURCATION_SELECT;
4055 if (enable)
4056 temp |= FDI_BC_BIFURCATION_SELECT;
4057
4058 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004059 I915_WRITE(SOUTH_CHICKEN1, temp);
4060 POSTING_READ(SOUTH_CHICKEN1);
4061}
4062
4063static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4064{
4065 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004066
4067 switch (intel_crtc->pipe) {
4068 case PIPE_A:
4069 break;
4070 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004071 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004072 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004073 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004074 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004075
4076 break;
4077 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004078 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004079
4080 break;
4081 default:
4082 BUG();
4083 }
4084}
4085
Jesse Barnesf67a5592011-01-05 10:31:48 -08004086/*
4087 * Enable PCH resources required for PCH ports:
4088 * - PCH PLLs
4089 * - FDI training & RX/TX
4090 * - update transcoder timings
4091 * - DP transcoding bits
4092 * - transcoder
4093 */
4094static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004095{
4096 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004097 struct drm_i915_private *dev_priv = dev->dev_private;
4098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4099 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004100 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004101
Daniel Vetterab9412b2013-05-03 11:49:46 +02004102 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004103
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004104 if (IS_IVYBRIDGE(dev))
4105 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4106
Daniel Vettercd986ab2012-10-26 10:58:12 +02004107 /* Write the TU size bits before fdi link training, so that error
4108 * detection works. */
4109 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4110 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4111
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004112 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004113 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004114
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004115 /* We need to program the right clock selection before writing the pixel
4116 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004117 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004118 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004119
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004120 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004121 temp |= TRANS_DPLL_ENABLE(pipe);
4122 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004123 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004124 temp |= sel;
4125 else
4126 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004127 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004129
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004130 /* XXX: pch pll's can be enabled any time before we enable the PCH
4131 * transcoder, and we actually should do this to not upset any PCH
4132 * transcoder that already use the clock when we share it.
4133 *
4134 * Note that enable_shared_dpll tries to do the right thing, but
4135 * get_shared_dpll unconditionally resets the pll - we need that to have
4136 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004137 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004138
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004139 /* set transcoder timing, panel must allow it */
4140 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004141 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004142
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004143 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004144
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004145 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004146 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004147 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004148 reg = TRANS_DP_CTL(pipe);
4149 temp = I915_READ(reg);
4150 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004151 TRANS_DP_SYNC_MASK |
4152 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004153 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004154 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004155
4156 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004157 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004158 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004159 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160
4161 switch (intel_trans_dp_port_sel(crtc)) {
4162 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004163 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004164 break;
4165 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004166 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004167 break;
4168 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004169 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004170 break;
4171 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004172 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004173 }
4174
Chris Wilson5eddb702010-09-11 13:48:45 +01004175 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004176 }
4177
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004178 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004179}
4180
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004181static void lpt_pch_enable(struct drm_crtc *crtc)
4182{
4183 struct drm_device *dev = crtc->dev;
4184 struct drm_i915_private *dev_priv = dev->dev_private;
4185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004186 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004187
Daniel Vetterab9412b2013-05-03 11:49:46 +02004188 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004189
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004190 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004191
Paulo Zanoni0540e482012-10-31 18:12:40 -02004192 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004193 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004194
Paulo Zanoni937bb612012-10-31 18:12:47 -02004195 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004196}
4197
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004198struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4199 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004200{
Daniel Vettere2b78262013-06-07 23:10:03 +02004201 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004202 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004203 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004204 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004205
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004206 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4207
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004208 if (HAS_PCH_IBX(dev_priv->dev)) {
4209 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004210 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004211 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004212
Daniel Vetter46edb022013-06-05 13:34:12 +02004213 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4214 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004215
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004216 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004217
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004218 goto found;
4219 }
4220
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304221 if (IS_BROXTON(dev_priv->dev)) {
4222 /* PLL is attached to port in bxt */
4223 struct intel_encoder *encoder;
4224 struct intel_digital_port *intel_dig_port;
4225
4226 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4227 if (WARN_ON(!encoder))
4228 return NULL;
4229
4230 intel_dig_port = enc_to_dig_port(&encoder->base);
4231 /* 1:1 mapping between ports and PLLs */
4232 i = (enum intel_dpll_id)intel_dig_port->port;
4233 pll = &dev_priv->shared_dplls[i];
4234 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4235 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004236 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304237
4238 goto found;
4239 }
4240
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004241 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4242 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004243
4244 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004245 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004246 continue;
4247
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004248 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004249 &shared_dpll[i].hw_state,
4250 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004251 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004252 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004253 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004254 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004255 goto found;
4256 }
4257 }
4258
4259 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004260 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4261 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004262 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004263 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4264 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004265 goto found;
4266 }
4267 }
4268
4269 return NULL;
4270
4271found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004272 if (shared_dpll[i].crtc_mask == 0)
4273 shared_dpll[i].hw_state =
4274 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004275
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004276 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004277 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4278 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004279
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004280 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004281
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004282 return pll;
4283}
4284
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004285static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004286{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004287 struct drm_i915_private *dev_priv = to_i915(state->dev);
4288 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004289 struct intel_shared_dpll *pll;
4290 enum intel_dpll_id i;
4291
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004292 if (!to_intel_atomic_state(state)->dpll_set)
4293 return;
4294
4295 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004296 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4297 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004298 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004299 }
4300}
4301
Daniel Vettera1520312013-05-03 11:49:50 +02004302static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004303{
4304 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004305 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004306 u32 temp;
4307
4308 temp = I915_READ(dslreg);
4309 udelay(500);
4310 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004311 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004312 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004313 }
4314}
4315
Chandra Kondurua1b22782015-04-07 15:28:45 -07004316/**
4317 * skl_update_scaler_users - Stages update to crtc's scaler state
4318 * @intel_crtc: crtc
4319 * @crtc_state: crtc_state
4320 * @plane: plane (NULL indicates crtc is requesting update)
4321 * @plane_state: plane's state
4322 * @force_detach: request unconditional detachment of scaler
4323 *
4324 * This function updates scaler state for requested plane or crtc.
4325 * To request scaler usage update for a plane, caller shall pass plane pointer.
4326 * To request scaler usage update for crtc, caller shall pass plane pointer
4327 * as NULL.
4328 *
4329 * Return
4330 * 0 - scaler_usage updated successfully
4331 * error - requested scaling cannot be supported or other error condition
4332 */
4333int
4334skl_update_scaler_users(
4335 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4336 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4337 int force_detach)
4338{
4339 int need_scaling;
4340 int idx;
4341 int src_w, src_h, dst_w, dst_h;
4342 int *scaler_id;
4343 struct drm_framebuffer *fb;
4344 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004345 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004346
4347 if (!intel_crtc || !crtc_state)
4348 return 0;
4349
4350 scaler_state = &crtc_state->scaler_state;
4351
4352 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4353 fb = intel_plane ? plane_state->base.fb : NULL;
4354
4355 if (intel_plane) {
4356 src_w = drm_rect_width(&plane_state->src) >> 16;
4357 src_h = drm_rect_height(&plane_state->src) >> 16;
4358 dst_w = drm_rect_width(&plane_state->dst);
4359 dst_h = drm_rect_height(&plane_state->dst);
4360 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004361 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004362 } else {
4363 struct drm_display_mode *adjusted_mode =
4364 &crtc_state->base.adjusted_mode;
4365 src_w = crtc_state->pipe_src_w;
4366 src_h = crtc_state->pipe_src_h;
4367 dst_w = adjusted_mode->hdisplay;
4368 dst_h = adjusted_mode->vdisplay;
4369 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004370 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004371 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004372
4373 need_scaling = intel_rotation_90_or_270(rotation) ?
4374 (src_h != dst_w || src_w != dst_h):
4375 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004376
4377 /*
4378 * if plane is being disabled or scaler is no more required or force detach
4379 * - free scaler binded to this plane/crtc
4380 * - in order to do this, update crtc->scaler_usage
4381 *
4382 * Here scaler state in crtc_state is set free so that
4383 * scaler can be assigned to other user. Actual register
4384 * update to free the scaler is done in plane/panel-fit programming.
4385 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4386 */
4387 if (force_detach || !need_scaling || (intel_plane &&
4388 (!fb || !plane_state->visible))) {
4389 if (*scaler_id >= 0) {
4390 scaler_state->scaler_users &= ~(1 << idx);
4391 scaler_state->scalers[*scaler_id].in_use = 0;
4392
4393 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4394 "crtc_state = %p scaler_users = 0x%x\n",
4395 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4396 intel_plane ? intel_plane->base.base.id :
4397 intel_crtc->base.base.id, crtc_state,
4398 scaler_state->scaler_users);
4399 *scaler_id = -1;
4400 }
4401 return 0;
4402 }
4403
4404 /* range checks */
4405 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4406 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4407
4408 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4409 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4410 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4411 "size is out of scaler range\n",
4412 intel_plane ? "PLANE" : "CRTC",
4413 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4414 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4415 return -EINVAL;
4416 }
4417
4418 /* check colorkey */
Chandra Konduru225c2282015-05-18 16:18:44 -07004419 if (WARN_ON(intel_plane &&
4420 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4421 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4422 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004423 return -EINVAL;
4424 }
4425
4426 /* Check src format */
4427 if (intel_plane) {
4428 switch (fb->pixel_format) {
4429 case DRM_FORMAT_RGB565:
4430 case DRM_FORMAT_XBGR8888:
4431 case DRM_FORMAT_XRGB8888:
4432 case DRM_FORMAT_ABGR8888:
4433 case DRM_FORMAT_ARGB8888:
4434 case DRM_FORMAT_XRGB2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004435 case DRM_FORMAT_XBGR2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004436 case DRM_FORMAT_YUYV:
4437 case DRM_FORMAT_YVYU:
4438 case DRM_FORMAT_UYVY:
4439 case DRM_FORMAT_VYUY:
4440 break;
4441 default:
4442 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4443 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4444 return -EINVAL;
4445 }
4446 }
4447
4448 /* mark this plane as a scaler user in crtc_state */
4449 scaler_state->scaler_users |= (1 << idx);
4450 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4451 "crtc_state = %p scaler_users = 0x%x\n",
4452 intel_plane ? "PLANE" : "CRTC",
4453 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4454 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4455 return 0;
4456}
4457
4458static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004459{
4460 struct drm_device *dev = crtc->base.dev;
4461 struct drm_i915_private *dev_priv = dev->dev_private;
4462 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004463 struct intel_crtc_scaler_state *scaler_state =
4464 &crtc->config->scaler_state;
4465
4466 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4467
4468 /* To update pfit, first update scaler state */
4469 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4470 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4471 skl_detach_scalers(crtc);
4472 if (!enable)
4473 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004474
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004475 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004476 int id;
4477
4478 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4479 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4480 return;
4481 }
4482
4483 id = scaler_state->scaler_id;
4484 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4485 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4486 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4487 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4488
4489 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004490 }
4491}
4492
Jesse Barnesb074cec2013-04-25 12:55:02 -07004493static void ironlake_pfit_enable(struct intel_crtc *crtc)
4494{
4495 struct drm_device *dev = crtc->base.dev;
4496 struct drm_i915_private *dev_priv = dev->dev_private;
4497 int pipe = crtc->pipe;
4498
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004499 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004500 /* Force use of hard-coded filter coefficients
4501 * as some pre-programmed values are broken,
4502 * e.g. x201.
4503 */
4504 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4505 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4506 PF_PIPE_SEL_IVB(pipe));
4507 else
4508 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004509 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4510 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004511 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004512}
4513
Matt Roper4a3b8762014-12-23 10:41:51 -08004514static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004515{
4516 struct drm_device *dev = crtc->dev;
4517 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004518 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004519 struct intel_plane *intel_plane;
4520
Matt Roperaf2b6532014-04-01 15:22:32 -07004521 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4522 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004523 if (intel_plane->pipe == pipe)
4524 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004525 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004526}
4527
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004528void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004529{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004530 struct drm_device *dev = crtc->base.dev;
4531 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004532
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004533 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004534 return;
4535
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004536 /* We can only enable IPS after we enable a plane and wait for a vblank */
4537 intel_wait_for_vblank(dev, crtc->pipe);
4538
Paulo Zanonid77e4532013-09-24 13:52:55 -03004539 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004540 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004541 mutex_lock(&dev_priv->rps.hw_lock);
4542 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4543 mutex_unlock(&dev_priv->rps.hw_lock);
4544 /* Quoting Art Runyan: "its not safe to expect any particular
4545 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004546 * mailbox." Moreover, the mailbox may return a bogus state,
4547 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004548 */
4549 } else {
4550 I915_WRITE(IPS_CTL, IPS_ENABLE);
4551 /* The bit only becomes 1 in the next vblank, so this wait here
4552 * is essentially intel_wait_for_vblank. If we don't have this
4553 * and don't wait for vblanks until the end of crtc_enable, then
4554 * the HW state readout code will complain that the expected
4555 * IPS_CTL value is not the one we read. */
4556 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4557 DRM_ERROR("Timed out waiting for IPS enable\n");
4558 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004559}
4560
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004561void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004562{
4563 struct drm_device *dev = crtc->base.dev;
4564 struct drm_i915_private *dev_priv = dev->dev_private;
4565
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004566 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004567 return;
4568
4569 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004570 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004571 mutex_lock(&dev_priv->rps.hw_lock);
4572 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4573 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004574 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4575 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4576 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004577 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004578 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004579 POSTING_READ(IPS_CTL);
4580 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004581
4582 /* We need to wait for a vblank before we can disable the plane. */
4583 intel_wait_for_vblank(dev, crtc->pipe);
4584}
4585
4586/** Loads the palette/gamma unit for the CRTC with the prepared values */
4587static void intel_crtc_load_lut(struct drm_crtc *crtc)
4588{
4589 struct drm_device *dev = crtc->dev;
4590 struct drm_i915_private *dev_priv = dev->dev_private;
4591 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4592 enum pipe pipe = intel_crtc->pipe;
4593 int palreg = PALETTE(pipe);
4594 int i;
4595 bool reenable_ips = false;
4596
4597 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004598 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004599 return;
4600
Imre Deak50360402015-01-16 00:55:16 -08004601 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004602 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004603 assert_dsi_pll_enabled(dev_priv);
4604 else
4605 assert_pll_enabled(dev_priv, pipe);
4606 }
4607
4608 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304609 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004610 palreg = LGC_PALETTE(pipe);
4611
4612 /* Workaround : Do not read or write the pipe palette/gamma data while
4613 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4614 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004615 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004616 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4617 GAMMA_MODE_MODE_SPLIT)) {
4618 hsw_disable_ips(intel_crtc);
4619 reenable_ips = true;
4620 }
4621
4622 for (i = 0; i < 256; i++) {
4623 I915_WRITE(palreg + 4 * i,
4624 (intel_crtc->lut_r[i] << 16) |
4625 (intel_crtc->lut_g[i] << 8) |
4626 intel_crtc->lut_b[i]);
4627 }
4628
4629 if (reenable_ips)
4630 hsw_enable_ips(intel_crtc);
4631}
4632
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004633static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004634{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004635 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004636 struct drm_device *dev = intel_crtc->base.dev;
4637 struct drm_i915_private *dev_priv = dev->dev_private;
4638
4639 mutex_lock(&dev->struct_mutex);
4640 dev_priv->mm.interruptible = false;
4641 (void) intel_overlay_switch_off(intel_crtc->overlay);
4642 dev_priv->mm.interruptible = true;
4643 mutex_unlock(&dev->struct_mutex);
4644 }
4645
4646 /* Let userspace switch the overlay on again. In most cases userspace
4647 * has to recompute where to put it anyway.
4648 */
4649}
4650
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004651/**
4652 * intel_post_enable_primary - Perform operations after enabling primary plane
4653 * @crtc: the CRTC whose primary plane was just enabled
4654 *
4655 * Performs potentially sleeping operations that must be done after the primary
4656 * plane is enabled, such as updating FBC and IPS. Note that this may be
4657 * called due to an explicit primary plane update, or due to an implicit
4658 * re-enable that is caused when a sprite plane is updated to no longer
4659 * completely hide the primary plane.
4660 */
4661static void
4662intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004663{
4664 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004665 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004666 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4667 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004668
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004669 /*
4670 * BDW signals flip done immediately if the plane
4671 * is disabled, even if the plane enable is already
4672 * armed to occur at the next vblank :(
4673 */
4674 if (IS_BROADWELL(dev))
4675 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004676
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004677 /*
4678 * FIXME IPS should be fine as long as one plane is
4679 * enabled, but in practice it seems to have problems
4680 * when going from primary only to sprite only and vice
4681 * versa.
4682 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004683 hsw_enable_ips(intel_crtc);
4684
4685 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004686 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004687 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004688
4689 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004690 * Gen2 reports pipe underruns whenever all planes are disabled.
4691 * So don't enable underrun reporting before at least some planes
4692 * are enabled.
4693 * FIXME: Need to fix the logic to work when we turn off all planes
4694 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004695 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004696 if (IS_GEN2(dev))
4697 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4698
4699 /* Underruns don't raise interrupts, so check manually. */
4700 if (HAS_GMCH_DISPLAY(dev))
4701 i9xx_check_fifo_underruns(dev_priv);
4702}
4703
4704/**
4705 * intel_pre_disable_primary - Perform operations before disabling primary plane
4706 * @crtc: the CRTC whose primary plane is to be disabled
4707 *
4708 * Performs potentially sleeping operations that must be done before the
4709 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4710 * be called due to an explicit primary plane update, or due to an implicit
4711 * disable that is caused when a sprite plane completely hides the primary
4712 * plane.
4713 */
4714static void
4715intel_pre_disable_primary(struct drm_crtc *crtc)
4716{
4717 struct drm_device *dev = crtc->dev;
4718 struct drm_i915_private *dev_priv = dev->dev_private;
4719 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4720 int pipe = intel_crtc->pipe;
4721
4722 /*
4723 * Gen2 reports pipe underruns whenever all planes are disabled.
4724 * So diasble underrun reporting before all the planes get disabled.
4725 * FIXME: Need to fix the logic to work when we turn off all planes
4726 * but leave the pipe running.
4727 */
4728 if (IS_GEN2(dev))
4729 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4730
4731 /*
4732 * Vblank time updates from the shadow to live plane control register
4733 * are blocked if the memory self-refresh mode is active at that
4734 * moment. So to make sure the plane gets truly disabled, disable
4735 * first the self-refresh mode. The self-refresh enable bit in turn
4736 * will be checked/applied by the HW only at the next frame start
4737 * event which is after the vblank start event, so we need to have a
4738 * wait-for-vblank between disabling the plane and the pipe.
4739 */
4740 if (HAS_GMCH_DISPLAY(dev))
4741 intel_set_memory_cxsr(dev_priv, false);
4742
4743 mutex_lock(&dev->struct_mutex);
4744 if (dev_priv->fbc.crtc == intel_crtc)
4745 intel_fbc_disable(dev);
4746 mutex_unlock(&dev->struct_mutex);
4747
4748 /*
4749 * FIXME IPS should be fine as long as one plane is
4750 * enabled, but in practice it seems to have problems
4751 * when going from primary only to sprite only and vice
4752 * versa.
4753 */
4754 hsw_disable_ips(intel_crtc);
4755}
4756
4757static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4758{
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004759 struct drm_device *dev = crtc->dev;
4760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4761 int pipe = intel_crtc->pipe;
4762
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004763 intel_enable_primary_hw_plane(crtc->primary, crtc);
4764 intel_enable_sprite_planes(crtc);
Maarten Lankhorstc0165302015-06-12 11:15:42 +02004765 if (to_intel_plane_state(crtc->cursor->state)->visible)
4766 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004767
4768 intel_post_enable_primary(crtc);
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004769
4770 /*
4771 * FIXME: Once we grow proper nuclear flip support out of this we need
4772 * to compute the mask of flip planes precisely. For the time being
4773 * consider this a flip to a NULL plane.
4774 */
4775 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004776}
4777
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004778static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004779{
4780 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004782 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004783 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004784
4785 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004786
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004787 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004788
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004789 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004790 for_each_intel_plane(dev, intel_plane) {
4791 if (intel_plane->pipe == pipe) {
4792 struct drm_crtc *from = intel_plane->base.crtc;
4793
4794 intel_plane->disable_plane(&intel_plane->base,
4795 from ?: crtc, true);
4796 }
4797 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004798
Daniel Vetterf99d7062014-06-19 16:01:59 +02004799 /*
4800 * FIXME: Once we grow proper nuclear flip support out of this we need
4801 * to compute the mask of flip planes precisely. For the time being
4802 * consider this a flip to a NULL plane.
4803 */
4804 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004805}
4806
Jesse Barnesf67a5592011-01-05 10:31:48 -08004807static void ironlake_crtc_enable(struct drm_crtc *crtc)
4808{
4809 struct drm_device *dev = crtc->dev;
4810 struct drm_i915_private *dev_priv = dev->dev_private;
4811 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004812 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004813 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004814
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004815 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004816 return;
4817
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004818 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004819 intel_prepare_shared_dpll(intel_crtc);
4820
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004821 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304822 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004823
4824 intel_set_pipe_timings(intel_crtc);
4825
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004826 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004827 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004828 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004829 }
4830
4831 ironlake_set_pipeconf(crtc);
4832
Jesse Barnesf67a5592011-01-05 10:31:48 -08004833 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004834
Daniel Vettera72e4c92014-09-30 10:56:47 +02004835 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4836 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004837
Daniel Vetterf6736a12013-06-05 13:34:30 +02004838 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004839 if (encoder->pre_enable)
4840 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004841
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004842 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004843 /* Note: FDI PLL enabling _must_ be done before we enable the
4844 * cpu pipes, hence this is separate from all the other fdi/pch
4845 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004846 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004847 } else {
4848 assert_fdi_tx_disabled(dev_priv, pipe);
4849 assert_fdi_rx_disabled(dev_priv, pipe);
4850 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004851
Jesse Barnesb074cec2013-04-25 12:55:02 -07004852 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004853
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004854 /*
4855 * On ILK+ LUT must be loaded before the pipe is running but with
4856 * clocks enabled
4857 */
4858 intel_crtc_load_lut(crtc);
4859
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004860 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004861 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004862
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004863 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004864 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004865
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004866 assert_vblank_disabled(crtc);
4867 drm_crtc_vblank_on(crtc);
4868
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004869 for_each_encoder_on_crtc(dev, crtc, encoder)
4870 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004871
4872 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004873 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004874}
4875
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004876/* IPS only exists on ULT machines and is tied to pipe A. */
4877static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4878{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004879 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004880}
4881
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004882static void haswell_crtc_enable(struct drm_crtc *crtc)
4883{
4884 struct drm_device *dev = crtc->dev;
4885 struct drm_i915_private *dev_priv = dev->dev_private;
4886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4887 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004888 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4889 struct intel_crtc_state *pipe_config =
4890 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004891
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004892 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004893 return;
4894
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004895 if (intel_crtc_to_shared_dpll(intel_crtc))
4896 intel_enable_shared_dpll(intel_crtc);
4897
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004898 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304899 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004900
4901 intel_set_pipe_timings(intel_crtc);
4902
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004903 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4904 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4905 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004906 }
4907
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004908 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004909 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004910 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004911 }
4912
4913 haswell_set_pipeconf(crtc);
4914
4915 intel_set_pipe_csc(crtc);
4916
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004917 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004918
Daniel Vettera72e4c92014-09-30 10:56:47 +02004919 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004920 for_each_encoder_on_crtc(dev, crtc, encoder)
4921 if (encoder->pre_enable)
4922 encoder->pre_enable(encoder);
4923
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004924 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004925 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4926 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004927 dev_priv->display.fdi_link_train(crtc);
4928 }
4929
Paulo Zanoni1f544382012-10-24 11:32:00 -02004930 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004931
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004932 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004933 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004934 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004935 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004936 else
4937 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004938
4939 /*
4940 * On ILK+ LUT must be loaded before the pipe is running but with
4941 * clocks enabled
4942 */
4943 intel_crtc_load_lut(crtc);
4944
Paulo Zanoni1f544382012-10-24 11:32:00 -02004945 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004946 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004947
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004948 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004949 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004950
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004951 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004952 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004953
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004954 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004955 intel_ddi_set_vc_payload_alloc(crtc, true);
4956
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004957 assert_vblank_disabled(crtc);
4958 drm_crtc_vblank_on(crtc);
4959
Jani Nikula8807e552013-08-30 19:40:32 +03004960 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004961 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004962 intel_opregion_notify_encoder(encoder, true);
4963 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004964
Paulo Zanonie4916942013-09-20 16:21:19 -03004965 /* If we change the relative order between pipe/planes enabling, we need
4966 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004967 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4968 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4969 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4970 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4971 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004972}
4973
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004974static void ironlake_pfit_disable(struct intel_crtc *crtc)
4975{
4976 struct drm_device *dev = crtc->base.dev;
4977 struct drm_i915_private *dev_priv = dev->dev_private;
4978 int pipe = crtc->pipe;
4979
4980 /* To avoid upsetting the power well on haswell only disable the pfit if
4981 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004982 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004983 I915_WRITE(PF_CTL(pipe), 0);
4984 I915_WRITE(PF_WIN_POS(pipe), 0);
4985 I915_WRITE(PF_WIN_SZ(pipe), 0);
4986 }
4987}
4988
Jesse Barnes6be4a602010-09-10 10:26:01 -07004989static void ironlake_crtc_disable(struct drm_crtc *crtc)
4990{
4991 struct drm_device *dev = crtc->dev;
4992 struct drm_i915_private *dev_priv = dev->dev_private;
4993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004994 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004995 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004996 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004997
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004998 if (WARN_ON(!intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004999 return;
5000
Daniel Vetterea9d7582012-07-10 10:42:52 +02005001 for_each_encoder_on_crtc(dev, crtc, encoder)
5002 encoder->disable(encoder);
5003
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005004 drm_crtc_vblank_off(crtc);
5005 assert_vblank_disabled(crtc);
5006
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005007 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005008 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005009
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005010 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005011
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005012 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005013
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005014 if (intel_crtc->config->has_pch_encoder)
5015 ironlake_fdi_disable(crtc);
5016
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005017 for_each_encoder_on_crtc(dev, crtc, encoder)
5018 if (encoder->post_disable)
5019 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005020
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005021 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005022 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005023
Daniel Vetterd925c592013-06-05 13:34:04 +02005024 if (HAS_PCH_CPT(dev)) {
5025 /* disable TRANS_DP_CTL */
5026 reg = TRANS_DP_CTL(pipe);
5027 temp = I915_READ(reg);
5028 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5029 TRANS_DP_PORT_SEL_MASK);
5030 temp |= TRANS_DP_PORT_SEL_NONE;
5031 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005032
Daniel Vetterd925c592013-06-05 13:34:04 +02005033 /* disable DPLL_SEL */
5034 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005035 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005036 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005037 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005038
5039 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005040 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005041
5042 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005043 }
5044
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005045 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005046 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005047
5048 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005049 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005050 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005051}
5052
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005053static void haswell_crtc_disable(struct drm_crtc *crtc)
5054{
5055 struct drm_device *dev = crtc->dev;
5056 struct drm_i915_private *dev_priv = dev->dev_private;
5057 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5058 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005059 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005060
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005061 if (WARN_ON(!intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005062 return;
5063
Jani Nikula8807e552013-08-30 19:40:32 +03005064 for_each_encoder_on_crtc(dev, crtc, encoder) {
5065 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005066 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005067 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005068
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005069 drm_crtc_vblank_off(crtc);
5070 assert_vblank_disabled(crtc);
5071
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005072 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005073 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5074 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005075 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005076
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005077 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005078 intel_ddi_set_vc_payload_alloc(crtc, false);
5079
Paulo Zanoniad80a812012-10-24 16:06:19 -02005080 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005081
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005082 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005083 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005084 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005085 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005086 else
5087 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005088
Paulo Zanoni1f544382012-10-24 11:32:00 -02005089 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005090
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005091 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005092 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005093 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005094 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005095
Imre Deak97b040a2014-06-25 22:01:50 +03005096 for_each_encoder_on_crtc(dev, crtc, encoder)
5097 if (encoder->post_disable)
5098 encoder->post_disable(encoder);
5099
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005100 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005101 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005102
5103 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005104 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005105 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005106
5107 if (intel_crtc_to_shared_dpll(intel_crtc))
5108 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005109}
5110
Jesse Barnes2dd24552013-04-25 12:55:01 -07005111static void i9xx_pfit_enable(struct intel_crtc *crtc)
5112{
5113 struct drm_device *dev = crtc->base.dev;
5114 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005115 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005116
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005117 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005118 return;
5119
Daniel Vetterc0b03412013-05-28 12:05:54 +02005120 /*
5121 * The panel fitter should only be adjusted whilst the pipe is disabled,
5122 * according to register description and PRM.
5123 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005124 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5125 assert_pipe_disabled(dev_priv, crtc->pipe);
5126
Jesse Barnesb074cec2013-04-25 12:55:02 -07005127 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5128 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005129
5130 /* Border color in case we don't scale up to the full screen. Black by
5131 * default, change to something else for debugging. */
5132 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005133}
5134
Dave Airlied05410f2014-06-05 13:22:59 +10005135static enum intel_display_power_domain port_to_power_domain(enum port port)
5136{
5137 switch (port) {
5138 case PORT_A:
5139 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5140 case PORT_B:
5141 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5142 case PORT_C:
5143 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5144 case PORT_D:
5145 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5146 default:
5147 WARN_ON_ONCE(1);
5148 return POWER_DOMAIN_PORT_OTHER;
5149 }
5150}
5151
Imre Deak77d22dc2014-03-05 16:20:52 +02005152#define for_each_power_domain(domain, mask) \
5153 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5154 if ((1 << (domain)) & (mask))
5155
Imre Deak319be8a2014-03-04 19:22:57 +02005156enum intel_display_power_domain
5157intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005158{
Imre Deak319be8a2014-03-04 19:22:57 +02005159 struct drm_device *dev = intel_encoder->base.dev;
5160 struct intel_digital_port *intel_dig_port;
5161
5162 switch (intel_encoder->type) {
5163 case INTEL_OUTPUT_UNKNOWN:
5164 /* Only DDI platforms should ever use this output type */
5165 WARN_ON_ONCE(!HAS_DDI(dev));
5166 case INTEL_OUTPUT_DISPLAYPORT:
5167 case INTEL_OUTPUT_HDMI:
5168 case INTEL_OUTPUT_EDP:
5169 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005170 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005171 case INTEL_OUTPUT_DP_MST:
5172 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5173 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005174 case INTEL_OUTPUT_ANALOG:
5175 return POWER_DOMAIN_PORT_CRT;
5176 case INTEL_OUTPUT_DSI:
5177 return POWER_DOMAIN_PORT_DSI;
5178 default:
5179 return POWER_DOMAIN_PORT_OTHER;
5180 }
5181}
5182
5183static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5184{
5185 struct drm_device *dev = crtc->dev;
5186 struct intel_encoder *intel_encoder;
5187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5188 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005189 unsigned long mask;
5190 enum transcoder transcoder;
5191
5192 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5193
5194 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5195 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005196 if (intel_crtc->config->pch_pfit.enabled ||
5197 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005198 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5199
Imre Deak319be8a2014-03-04 19:22:57 +02005200 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5201 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5202
Imre Deak77d22dc2014-03-05 16:20:52 +02005203 return mask;
5204}
5205
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005206static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005207{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005208 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005209 struct drm_i915_private *dev_priv = dev->dev_private;
5210 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5211 struct intel_crtc *crtc;
5212
5213 /*
5214 * First get all needed power domains, then put all unneeded, to avoid
5215 * any unnecessary toggling of the power wells.
5216 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005217 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005218 enum intel_display_power_domain domain;
5219
Matt Roper83d65732015-02-25 13:12:16 -08005220 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005221 continue;
5222
Imre Deak319be8a2014-03-04 19:22:57 +02005223 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005224
5225 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5226 intel_display_power_get(dev_priv, domain);
5227 }
5228
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005229 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005230 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005231
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005232 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005233 enum intel_display_power_domain domain;
5234
5235 for_each_power_domain(domain, crtc->enabled_power_domains)
5236 intel_display_power_put(dev_priv, domain);
5237
5238 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5239 }
5240
5241 intel_display_set_init_power(dev_priv, false);
5242}
5243
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005244static void intel_update_max_cdclk(struct drm_device *dev)
5245{
5246 struct drm_i915_private *dev_priv = dev->dev_private;
5247
5248 if (IS_SKYLAKE(dev)) {
5249 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5250
5251 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5252 dev_priv->max_cdclk_freq = 675000;
5253 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5254 dev_priv->max_cdclk_freq = 540000;
5255 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5256 dev_priv->max_cdclk_freq = 450000;
5257 else
5258 dev_priv->max_cdclk_freq = 337500;
5259 } else if (IS_BROADWELL(dev)) {
5260 /*
5261 * FIXME with extra cooling we can allow
5262 * 540 MHz for ULX and 675 Mhz for ULT.
5263 * How can we know if extra cooling is
5264 * available? PCI ID, VTB, something else?
5265 */
5266 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5267 dev_priv->max_cdclk_freq = 450000;
5268 else if (IS_BDW_ULX(dev))
5269 dev_priv->max_cdclk_freq = 450000;
5270 else if (IS_BDW_ULT(dev))
5271 dev_priv->max_cdclk_freq = 540000;
5272 else
5273 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005274 } else if (IS_CHERRYVIEW(dev)) {
5275 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005276 } else if (IS_VALLEYVIEW(dev)) {
5277 dev_priv->max_cdclk_freq = 400000;
5278 } else {
5279 /* otherwise assume cdclk is fixed */
5280 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5281 }
5282
5283 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5284 dev_priv->max_cdclk_freq);
5285}
5286
5287static void intel_update_cdclk(struct drm_device *dev)
5288{
5289 struct drm_i915_private *dev_priv = dev->dev_private;
5290
5291 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5292 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5293 dev_priv->cdclk_freq);
5294
5295 /*
5296 * Program the gmbus_freq based on the cdclk frequency.
5297 * BSpec erroneously claims we should aim for 4MHz, but
5298 * in fact 1MHz is the correct frequency.
5299 */
5300 if (IS_VALLEYVIEW(dev)) {
5301 /*
5302 * Program the gmbus_freq based on the cdclk frequency.
5303 * BSpec erroneously claims we should aim for 4MHz, but
5304 * in fact 1MHz is the correct frequency.
5305 */
5306 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5307 }
5308
5309 if (dev_priv->max_cdclk_freq == 0)
5310 intel_update_max_cdclk(dev);
5311}
5312
Damien Lespiau70d0c572015-06-04 18:21:29 +01005313static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305314{
5315 struct drm_i915_private *dev_priv = dev->dev_private;
5316 uint32_t divider;
5317 uint32_t ratio;
5318 uint32_t current_freq;
5319 int ret;
5320
5321 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5322 switch (frequency) {
5323 case 144000:
5324 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5325 ratio = BXT_DE_PLL_RATIO(60);
5326 break;
5327 case 288000:
5328 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5329 ratio = BXT_DE_PLL_RATIO(60);
5330 break;
5331 case 384000:
5332 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5333 ratio = BXT_DE_PLL_RATIO(60);
5334 break;
5335 case 576000:
5336 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5337 ratio = BXT_DE_PLL_RATIO(60);
5338 break;
5339 case 624000:
5340 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5341 ratio = BXT_DE_PLL_RATIO(65);
5342 break;
5343 case 19200:
5344 /*
5345 * Bypass frequency with DE PLL disabled. Init ratio, divider
5346 * to suppress GCC warning.
5347 */
5348 ratio = 0;
5349 divider = 0;
5350 break;
5351 default:
5352 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5353
5354 return;
5355 }
5356
5357 mutex_lock(&dev_priv->rps.hw_lock);
5358 /* Inform power controller of upcoming frequency change */
5359 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5360 0x80000000);
5361 mutex_unlock(&dev_priv->rps.hw_lock);
5362
5363 if (ret) {
5364 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5365 ret, frequency);
5366 return;
5367 }
5368
5369 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5370 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5371 current_freq = current_freq * 500 + 1000;
5372
5373 /*
5374 * DE PLL has to be disabled when
5375 * - setting to 19.2MHz (bypass, PLL isn't used)
5376 * - before setting to 624MHz (PLL needs toggling)
5377 * - before setting to any frequency from 624MHz (PLL needs toggling)
5378 */
5379 if (frequency == 19200 || frequency == 624000 ||
5380 current_freq == 624000) {
5381 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5382 /* Timeout 200us */
5383 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5384 1))
5385 DRM_ERROR("timout waiting for DE PLL unlock\n");
5386 }
5387
5388 if (frequency != 19200) {
5389 uint32_t val;
5390
5391 val = I915_READ(BXT_DE_PLL_CTL);
5392 val &= ~BXT_DE_PLL_RATIO_MASK;
5393 val |= ratio;
5394 I915_WRITE(BXT_DE_PLL_CTL, val);
5395
5396 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5397 /* Timeout 200us */
5398 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5399 DRM_ERROR("timeout waiting for DE PLL lock\n");
5400
5401 val = I915_READ(CDCLK_CTL);
5402 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5403 val |= divider;
5404 /*
5405 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5406 * enable otherwise.
5407 */
5408 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5409 if (frequency >= 500000)
5410 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5411
5412 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5413 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5414 val |= (frequency - 1000) / 500;
5415 I915_WRITE(CDCLK_CTL, val);
5416 }
5417
5418 mutex_lock(&dev_priv->rps.hw_lock);
5419 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5420 DIV_ROUND_UP(frequency, 25000));
5421 mutex_unlock(&dev_priv->rps.hw_lock);
5422
5423 if (ret) {
5424 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5425 ret, frequency);
5426 return;
5427 }
5428
Damien Lespiaua47871b2015-06-04 18:21:34 +01005429 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305430}
5431
5432void broxton_init_cdclk(struct drm_device *dev)
5433{
5434 struct drm_i915_private *dev_priv = dev->dev_private;
5435 uint32_t val;
5436
5437 /*
5438 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5439 * or else the reset will hang because there is no PCH to respond.
5440 * Move the handshake programming to initialization sequence.
5441 * Previously was left up to BIOS.
5442 */
5443 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5444 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5445 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5446
5447 /* Enable PG1 for cdclk */
5448 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5449
5450 /* check if cd clock is enabled */
5451 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5452 DRM_DEBUG_KMS("Display already initialized\n");
5453 return;
5454 }
5455
5456 /*
5457 * FIXME:
5458 * - The initial CDCLK needs to be read from VBT.
5459 * Need to make this change after VBT has changes for BXT.
5460 * - check if setting the max (or any) cdclk freq is really necessary
5461 * here, it belongs to modeset time
5462 */
5463 broxton_set_cdclk(dev, 624000);
5464
5465 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005466 POSTING_READ(DBUF_CTL);
5467
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305468 udelay(10);
5469
5470 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5471 DRM_ERROR("DBuf power enable timeout!\n");
5472}
5473
5474void broxton_uninit_cdclk(struct drm_device *dev)
5475{
5476 struct drm_i915_private *dev_priv = dev->dev_private;
5477
5478 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005479 POSTING_READ(DBUF_CTL);
5480
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305481 udelay(10);
5482
5483 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5484 DRM_ERROR("DBuf power disable timeout!\n");
5485
5486 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5487 broxton_set_cdclk(dev, 19200);
5488
5489 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5490}
5491
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005492static const struct skl_cdclk_entry {
5493 unsigned int freq;
5494 unsigned int vco;
5495} skl_cdclk_frequencies[] = {
5496 { .freq = 308570, .vco = 8640 },
5497 { .freq = 337500, .vco = 8100 },
5498 { .freq = 432000, .vco = 8640 },
5499 { .freq = 450000, .vco = 8100 },
5500 { .freq = 540000, .vco = 8100 },
5501 { .freq = 617140, .vco = 8640 },
5502 { .freq = 675000, .vco = 8100 },
5503};
5504
5505static unsigned int skl_cdclk_decimal(unsigned int freq)
5506{
5507 return (freq - 1000) / 500;
5508}
5509
5510static unsigned int skl_cdclk_get_vco(unsigned int freq)
5511{
5512 unsigned int i;
5513
5514 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5515 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5516
5517 if (e->freq == freq)
5518 return e->vco;
5519 }
5520
5521 return 8100;
5522}
5523
5524static void
5525skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5526{
5527 unsigned int min_freq;
5528 u32 val;
5529
5530 /* select the minimum CDCLK before enabling DPLL 0 */
5531 val = I915_READ(CDCLK_CTL);
5532 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5533 val |= CDCLK_FREQ_337_308;
5534
5535 if (required_vco == 8640)
5536 min_freq = 308570;
5537 else
5538 min_freq = 337500;
5539
5540 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5541
5542 I915_WRITE(CDCLK_CTL, val);
5543 POSTING_READ(CDCLK_CTL);
5544
5545 /*
5546 * We always enable DPLL0 with the lowest link rate possible, but still
5547 * taking into account the VCO required to operate the eDP panel at the
5548 * desired frequency. The usual DP link rates operate with a VCO of
5549 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5550 * The modeset code is responsible for the selection of the exact link
5551 * rate later on, with the constraint of choosing a frequency that
5552 * works with required_vco.
5553 */
5554 val = I915_READ(DPLL_CTRL1);
5555
5556 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5557 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5558 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5559 if (required_vco == 8640)
5560 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5561 SKL_DPLL0);
5562 else
5563 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5564 SKL_DPLL0);
5565
5566 I915_WRITE(DPLL_CTRL1, val);
5567 POSTING_READ(DPLL_CTRL1);
5568
5569 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5570
5571 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5572 DRM_ERROR("DPLL0 not locked\n");
5573}
5574
5575static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5576{
5577 int ret;
5578 u32 val;
5579
5580 /* inform PCU we want to change CDCLK */
5581 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5582 mutex_lock(&dev_priv->rps.hw_lock);
5583 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5584 mutex_unlock(&dev_priv->rps.hw_lock);
5585
5586 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5587}
5588
5589static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5590{
5591 unsigned int i;
5592
5593 for (i = 0; i < 15; i++) {
5594 if (skl_cdclk_pcu_ready(dev_priv))
5595 return true;
5596 udelay(10);
5597 }
5598
5599 return false;
5600}
5601
5602static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5603{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005604 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005605 u32 freq_select, pcu_ack;
5606
5607 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5608
5609 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5610 DRM_ERROR("failed to inform PCU about cdclk change\n");
5611 return;
5612 }
5613
5614 /* set CDCLK_CTL */
5615 switch(freq) {
5616 case 450000:
5617 case 432000:
5618 freq_select = CDCLK_FREQ_450_432;
5619 pcu_ack = 1;
5620 break;
5621 case 540000:
5622 freq_select = CDCLK_FREQ_540;
5623 pcu_ack = 2;
5624 break;
5625 case 308570:
5626 case 337500:
5627 default:
5628 freq_select = CDCLK_FREQ_337_308;
5629 pcu_ack = 0;
5630 break;
5631 case 617140:
5632 case 675000:
5633 freq_select = CDCLK_FREQ_675_617;
5634 pcu_ack = 3;
5635 break;
5636 }
5637
5638 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5639 POSTING_READ(CDCLK_CTL);
5640
5641 /* inform PCU of the change */
5642 mutex_lock(&dev_priv->rps.hw_lock);
5643 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5644 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005645
5646 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005647}
5648
5649void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5650{
5651 /* disable DBUF power */
5652 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5653 POSTING_READ(DBUF_CTL);
5654
5655 udelay(10);
5656
5657 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5658 DRM_ERROR("DBuf power disable timeout\n");
5659
5660 /* disable DPLL0 */
5661 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5662 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5663 DRM_ERROR("Couldn't disable DPLL0\n");
5664
5665 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5666}
5667
5668void skl_init_cdclk(struct drm_i915_private *dev_priv)
5669{
5670 u32 val;
5671 unsigned int required_vco;
5672
5673 /* enable PCH reset handshake */
5674 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5675 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5676
5677 /* enable PG1 and Misc I/O */
5678 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5679
5680 /* DPLL0 already enabed !? */
5681 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5682 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5683 return;
5684 }
5685
5686 /* enable DPLL0 */
5687 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5688 skl_dpll0_enable(dev_priv, required_vco);
5689
5690 /* set CDCLK to the frequency the BIOS chose */
5691 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5692
5693 /* enable DBUF power */
5694 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5695 POSTING_READ(DBUF_CTL);
5696
5697 udelay(10);
5698
5699 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5700 DRM_ERROR("DBuf power enable timeout\n");
5701}
5702
Ville Syrjälädfcab172014-06-13 13:37:47 +03005703/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005704static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005705{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005706 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005707
Jesse Barnes586f49d2013-11-04 16:06:59 -08005708 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005709 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005710 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5711 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005712 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005713
Ville Syrjälädfcab172014-06-13 13:37:47 +03005714 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005715}
5716
5717/* Adjust CDclk dividers to allow high res or save power if possible */
5718static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5719{
5720 struct drm_i915_private *dev_priv = dev->dev_private;
5721 u32 val, cmd;
5722
Vandana Kannan164dfd22014-11-24 13:37:41 +05305723 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5724 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005725
Ville Syrjälädfcab172014-06-13 13:37:47 +03005726 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005727 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005728 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005729 cmd = 1;
5730 else
5731 cmd = 0;
5732
5733 mutex_lock(&dev_priv->rps.hw_lock);
5734 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5735 val &= ~DSPFREQGUAR_MASK;
5736 val |= (cmd << DSPFREQGUAR_SHIFT);
5737 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5738 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5739 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5740 50)) {
5741 DRM_ERROR("timed out waiting for CDclk change\n");
5742 }
5743 mutex_unlock(&dev_priv->rps.hw_lock);
5744
Ville Syrjälä54433e92015-05-26 20:42:31 +03005745 mutex_lock(&dev_priv->sb_lock);
5746
Ville Syrjälädfcab172014-06-13 13:37:47 +03005747 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005748 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005749
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005750 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005751
Jesse Barnes30a970c2013-11-04 13:48:12 -08005752 /* adjust cdclk divider */
5753 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005754 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005755 val |= divider;
5756 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005757
5758 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5759 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5760 50))
5761 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005762 }
5763
Jesse Barnes30a970c2013-11-04 13:48:12 -08005764 /* adjust self-refresh exit latency value */
5765 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5766 val &= ~0x7f;
5767
5768 /*
5769 * For high bandwidth configs, we set a higher latency in the bunit
5770 * so that the core display fetch happens in time to avoid underruns.
5771 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005772 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005773 val |= 4500 / 250; /* 4.5 usec */
5774 else
5775 val |= 3000 / 250; /* 3.0 usec */
5776 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005777
Ville Syrjäläa5805162015-05-26 20:42:30 +03005778 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005779
Ville Syrjäläb6283052015-06-03 15:45:07 +03005780 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005781}
5782
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005783static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5784{
5785 struct drm_i915_private *dev_priv = dev->dev_private;
5786 u32 val, cmd;
5787
Vandana Kannan164dfd22014-11-24 13:37:41 +05305788 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5789 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005790
5791 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005792 case 333333:
5793 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005794 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005795 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005796 break;
5797 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005798 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005799 return;
5800 }
5801
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005802 /*
5803 * Specs are full of misinformation, but testing on actual
5804 * hardware has shown that we just need to write the desired
5805 * CCK divider into the Punit register.
5806 */
5807 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5808
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005809 mutex_lock(&dev_priv->rps.hw_lock);
5810 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5811 val &= ~DSPFREQGUAR_MASK_CHV;
5812 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5813 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5814 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5815 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5816 50)) {
5817 DRM_ERROR("timed out waiting for CDclk change\n");
5818 }
5819 mutex_unlock(&dev_priv->rps.hw_lock);
5820
Ville Syrjäläb6283052015-06-03 15:45:07 +03005821 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005822}
5823
Jesse Barnes30a970c2013-11-04 13:48:12 -08005824static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5825 int max_pixclk)
5826{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005827 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005828 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005829
Jesse Barnes30a970c2013-11-04 13:48:12 -08005830 /*
5831 * Really only a few cases to deal with, as only 4 CDclks are supported:
5832 * 200MHz
5833 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005834 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005835 * 400MHz (VLV only)
5836 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5837 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005838 *
5839 * We seem to get an unstable or solid color picture at 200MHz.
5840 * Not sure what's wrong. For now use 200MHz only when all pipes
5841 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005842 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005843 if (!IS_CHERRYVIEW(dev_priv) &&
5844 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005845 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005846 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005847 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005848 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005849 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005850 else
5851 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005852}
5853
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305854static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5855 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305857 /*
5858 * FIXME:
5859 * - remove the guardband, it's not needed on BXT
5860 * - set 19.2MHz bypass frequency if there are no active pipes
5861 */
5862 if (max_pixclk > 576000*9/10)
5863 return 624000;
5864 else if (max_pixclk > 384000*9/10)
5865 return 576000;
5866 else if (max_pixclk > 288000*9/10)
5867 return 384000;
5868 else if (max_pixclk > 144000*9/10)
5869 return 288000;
5870 else
5871 return 144000;
5872}
5873
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005874/* Compute the max pixel clock for new configuration. Uses atomic state if
5875 * that's non-NULL, look at current state otherwise. */
5876static int intel_mode_max_pixclk(struct drm_device *dev,
5877 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005878{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005879 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005880 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005881 int max_pixclk = 0;
5882
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005883 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005884 if (state)
5885 crtc_state =
5886 intel_atomic_get_crtc_state(state, intel_crtc);
5887 else
5888 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005889 if (IS_ERR(crtc_state))
5890 return PTR_ERR(crtc_state);
5891
5892 if (!crtc_state->base.enable)
5893 continue;
5894
5895 max_pixclk = max(max_pixclk,
5896 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005897 }
5898
5899 return max_pixclk;
5900}
5901
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005902static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005903{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005904 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005905 struct drm_crtc *crtc;
5906 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005907 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005908 int cdclk, ret = 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005909
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005910 if (max_pixclk < 0)
5911 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005912
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305913 if (IS_VALLEYVIEW(dev_priv))
5914 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5915 else
5916 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5917
5918 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005919 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005920
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005921 /* add all active pipes to the state */
5922 for_each_crtc(state->dev, crtc) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005923 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5924 if (IS_ERR(crtc_state))
5925 return PTR_ERR(crtc_state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005926
5927 if (!crtc_state->active || needs_modeset(crtc_state))
5928 continue;
5929
5930 crtc_state->mode_changed = true;
5931
5932 ret = drm_atomic_add_affected_connectors(state, crtc);
5933 if (ret)
5934 break;
5935
5936 ret = drm_atomic_add_affected_planes(state, crtc);
5937 if (ret)
5938 break;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005939 }
5940
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005941 return ret;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005942}
5943
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005944static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5945{
5946 unsigned int credits, default_credits;
5947
5948 if (IS_CHERRYVIEW(dev_priv))
5949 default_credits = PFI_CREDIT(12);
5950 else
5951 default_credits = PFI_CREDIT(8);
5952
Vandana Kannan164dfd22014-11-24 13:37:41 +05305953 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005954 /* CHV suggested value is 31 or 63 */
5955 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005956 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005957 else
5958 credits = PFI_CREDIT(15);
5959 } else {
5960 credits = default_credits;
5961 }
5962
5963 /*
5964 * WA - write default credits before re-programming
5965 * FIXME: should we also set the resend bit here?
5966 */
5967 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5968 default_credits);
5969
5970 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5971 credits | PFI_CREDIT_RESEND);
5972
5973 /*
5974 * FIXME is this guaranteed to clear
5975 * immediately or should we poll for it?
5976 */
5977 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5978}
5979
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005980static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005981{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005982 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005983 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005984 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005985 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005986
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005987 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5988 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005989 if (WARN_ON(max_pixclk < 0))
5990 return;
5991
5992 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005993
Vandana Kannan164dfd22014-11-24 13:37:41 +05305994 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005995 /*
5996 * FIXME: We can end up here with all power domains off, yet
5997 * with a CDCLK frequency other than the minimum. To account
5998 * for this take the PIPE-A power domain, which covers the HW
5999 * blocks needed for the following programming. This can be
6000 * removed once it's guaranteed that we get here either with
6001 * the minimum CDCLK set, or the required power domains
6002 * enabled.
6003 */
6004 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6005
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006006 if (IS_CHERRYVIEW(dev))
6007 cherryview_set_cdclk(dev, req_cdclk);
6008 else
6009 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02006010
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006011 vlv_program_pfi_credits(dev_priv);
6012
Imre Deak738c05c2014-11-19 16:25:37 +02006013 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006014 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08006015}
6016
Jesse Barnes89b667f2013-04-18 14:51:36 -07006017static void valleyview_crtc_enable(struct drm_crtc *crtc)
6018{
6019 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006020 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006021 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6022 struct intel_encoder *encoder;
6023 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006024 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006025
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006026 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006027 return;
6028
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006029 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306030
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006031 if (!is_dsi) {
6032 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006033 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006034 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006035 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006036 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006037
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006038 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306039 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006040
6041 intel_set_pipe_timings(intel_crtc);
6042
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006043 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6044 struct drm_i915_private *dev_priv = dev->dev_private;
6045
6046 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6047 I915_WRITE(CHV_CANVAS(pipe), 0);
6048 }
6049
Daniel Vetter5b18e572014-04-24 23:55:06 +02006050 i9xx_set_pipeconf(intel_crtc);
6051
Jesse Barnes89b667f2013-04-18 14:51:36 -07006052 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006053
Daniel Vettera72e4c92014-09-30 10:56:47 +02006054 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006055
Jesse Barnes89b667f2013-04-18 14:51:36 -07006056 for_each_encoder_on_crtc(dev, crtc, encoder)
6057 if (encoder->pre_pll_enable)
6058 encoder->pre_pll_enable(encoder);
6059
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006060 if (!is_dsi) {
6061 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006062 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006063 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006064 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006065 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006066
6067 for_each_encoder_on_crtc(dev, crtc, encoder)
6068 if (encoder->pre_enable)
6069 encoder->pre_enable(encoder);
6070
Jesse Barnes2dd24552013-04-25 12:55:01 -07006071 i9xx_pfit_enable(intel_crtc);
6072
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006073 intel_crtc_load_lut(crtc);
6074
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006075 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006076 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006077
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006078 assert_vblank_disabled(crtc);
6079 drm_crtc_vblank_on(crtc);
6080
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006081 for_each_encoder_on_crtc(dev, crtc, encoder)
6082 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006083}
6084
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006085static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6086{
6087 struct drm_device *dev = crtc->base.dev;
6088 struct drm_i915_private *dev_priv = dev->dev_private;
6089
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006090 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6091 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006092}
6093
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006094static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006095{
6096 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006097 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006099 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006100 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006101
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006102 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006103 return;
6104
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006105 i9xx_set_pll_dividers(intel_crtc);
6106
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006107 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306108 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006109
6110 intel_set_pipe_timings(intel_crtc);
6111
Daniel Vetter5b18e572014-04-24 23:55:06 +02006112 i9xx_set_pipeconf(intel_crtc);
6113
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006114 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006115
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006116 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006117 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006118
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006119 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006120 if (encoder->pre_enable)
6121 encoder->pre_enable(encoder);
6122
Daniel Vetterf6736a12013-06-05 13:34:30 +02006123 i9xx_enable_pll(intel_crtc);
6124
Jesse Barnes2dd24552013-04-25 12:55:01 -07006125 i9xx_pfit_enable(intel_crtc);
6126
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006127 intel_crtc_load_lut(crtc);
6128
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006129 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006130 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006131
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006132 assert_vblank_disabled(crtc);
6133 drm_crtc_vblank_on(crtc);
6134
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006135 for_each_encoder_on_crtc(dev, crtc, encoder)
6136 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006137}
6138
Daniel Vetter87476d62013-04-11 16:29:06 +02006139static void i9xx_pfit_disable(struct intel_crtc *crtc)
6140{
6141 struct drm_device *dev = crtc->base.dev;
6142 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006143
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006144 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006145 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006146
6147 assert_pipe_disabled(dev_priv, crtc->pipe);
6148
Daniel Vetter328d8e82013-05-08 10:36:31 +02006149 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6150 I915_READ(PFIT_CONTROL));
6151 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006152}
6153
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006154static void i9xx_crtc_disable(struct drm_crtc *crtc)
6155{
6156 struct drm_device *dev = crtc->dev;
6157 struct drm_i915_private *dev_priv = dev->dev_private;
6158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006159 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006160 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006161
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006162 if (WARN_ON(!intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006163 return;
6164
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006165 /*
6166 * On gen2 planes are double buffered but the pipe isn't, so we must
6167 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006168 * We also need to wait on all gmch platforms because of the
6169 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006170 */
Imre Deak564ed192014-06-13 14:54:21 +03006171 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006172
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006173 for_each_encoder_on_crtc(dev, crtc, encoder)
6174 encoder->disable(encoder);
6175
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006176 drm_crtc_vblank_off(crtc);
6177 assert_vblank_disabled(crtc);
6178
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006179 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006180
Daniel Vetter87476d62013-04-11 16:29:06 +02006181 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006182
Jesse Barnes89b667f2013-04-18 14:51:36 -07006183 for_each_encoder_on_crtc(dev, crtc, encoder)
6184 if (encoder->post_disable)
6185 encoder->post_disable(encoder);
6186
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006187 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006188 if (IS_CHERRYVIEW(dev))
6189 chv_disable_pll(dev_priv, pipe);
6190 else if (IS_VALLEYVIEW(dev))
6191 vlv_disable_pll(dev_priv, pipe);
6192 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006193 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006194 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006195
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006196 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006197 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006198
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006199 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006200 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006201
Daniel Vetterefa96242014-04-24 23:55:02 +02006202 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006203 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006204 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006205}
6206
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006207static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006208{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006210 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006211 enum intel_display_power_domain domain;
6212 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006213
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006214 if (!intel_crtc->active)
6215 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006216
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006217 intel_crtc_disable_planes(crtc);
6218 dev_priv->display.crtc_disable(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006219
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006220 domains = intel_crtc->enabled_power_domains;
6221 for_each_power_domain(domain, domains)
6222 intel_display_power_put(dev_priv, domain);
6223 intel_crtc->enabled_power_domains = 0;
6224}
6225
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006226/*
6227 * turn all crtc's off, but do not adjust state
6228 * This has to be paired with a call to intel_modeset_setup_hw_state.
6229 */
Maarten Lankhorst9716c692015-06-10 10:24:19 +02006230void intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006231{
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006232 struct drm_crtc *crtc;
6233
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006234 for_each_crtc(dev, crtc)
6235 intel_crtc_disable_noatomic(crtc);
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006236}
6237
Chris Wilsoncdd59982010-09-08 16:30:16 +01006238/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006239int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Daniel Vetter976f8a22012-07-08 22:34:21 +02006240{
6241 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006242 struct drm_mode_config *config = &dev->mode_config;
6243 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006245 struct intel_crtc_state *pipe_config;
6246 struct drm_atomic_state *state;
6247 int ret;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006248
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006249 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006250 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006251
6252 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006253 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006254
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006255 /* this function should be called with drm_modeset_lock_all for now */
6256 if (WARN_ON(!ctx))
6257 return -EIO;
6258 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006259
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006260 state = drm_atomic_state_alloc(dev);
6261 if (WARN_ON(!state))
6262 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006263
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006264 state->acquire_ctx = ctx;
6265 state->allow_modeset = true;
6266
6267 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6268 if (IS_ERR(pipe_config)) {
6269 ret = PTR_ERR(pipe_config);
6270 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006271 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006272 pipe_config->base.active = enable;
6273
6274 ret = intel_set_mode(state);
6275 if (!ret)
6276 return ret;
6277
6278err:
6279 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6280 drm_atomic_state_free(state);
6281 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306282}
6283
6284/**
6285 * Sets the power management mode of the pipe and plane.
6286 */
6287void intel_crtc_update_dpms(struct drm_crtc *crtc)
6288{
6289 struct drm_device *dev = crtc->dev;
6290 struct intel_encoder *intel_encoder;
6291 bool enable = false;
6292
6293 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6294 enable |= intel_encoder->connectors_active;
6295
6296 intel_crtc_control(crtc, enable);
Chris Wilsoncdd59982010-09-08 16:30:16 +01006297}
6298
Chris Wilsonea5b2132010-08-04 13:50:23 +01006299void intel_encoder_destroy(struct drm_encoder *encoder)
6300{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006301 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006302
Chris Wilsonea5b2132010-08-04 13:50:23 +01006303 drm_encoder_cleanup(encoder);
6304 kfree(intel_encoder);
6305}
6306
Damien Lespiau92373292013-08-08 22:28:57 +01006307/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006308 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6309 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006310static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006311{
6312 if (mode == DRM_MODE_DPMS_ON) {
6313 encoder->connectors_active = true;
6314
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006315 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006316 } else {
6317 encoder->connectors_active = false;
6318
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006319 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006320 }
6321}
6322
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006323/* Cross check the actual hw state with our own modeset state tracking (and it's
6324 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006325static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006326{
6327 if (connector->get_hw_state(connector)) {
6328 struct intel_encoder *encoder = connector->encoder;
6329 struct drm_crtc *crtc;
6330 bool encoder_enabled;
6331 enum pipe pipe;
6332
6333 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6334 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006335 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006336
Dave Airlie0e32b392014-05-02 14:02:48 +10006337 /* there is no real hw state for MST connectors */
6338 if (connector->mst_port)
6339 return;
6340
Rob Clarke2c719b2014-12-15 13:56:32 -05006341 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006342 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006343 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006344 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006345
Dave Airlie36cd7442014-05-02 13:44:18 +10006346 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006347 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006348 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006349
Dave Airlie36cd7442014-05-02 13:44:18 +10006350 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006351 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6352 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006353 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006354
Dave Airlie36cd7442014-05-02 13:44:18 +10006355 crtc = encoder->base.crtc;
6356
Matt Roper83d65732015-02-25 13:12:16 -08006357 I915_STATE_WARN(!crtc->state->enable,
6358 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006359 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6360 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006361 "encoder active on the wrong pipe\n");
6362 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006363 }
6364}
6365
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006366int intel_connector_init(struct intel_connector *connector)
6367{
6368 struct drm_connector_state *connector_state;
6369
6370 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6371 if (!connector_state)
6372 return -ENOMEM;
6373
6374 connector->base.state = connector_state;
6375 return 0;
6376}
6377
6378struct intel_connector *intel_connector_alloc(void)
6379{
6380 struct intel_connector *connector;
6381
6382 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6383 if (!connector)
6384 return NULL;
6385
6386 if (intel_connector_init(connector) < 0) {
6387 kfree(connector);
6388 return NULL;
6389 }
6390
6391 return connector;
6392}
6393
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006394/* Even simpler default implementation, if there's really no special case to
6395 * consider. */
6396void intel_connector_dpms(struct drm_connector *connector, int mode)
6397{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006398 /* All the simple cases only support two dpms states. */
6399 if (mode != DRM_MODE_DPMS_ON)
6400 mode = DRM_MODE_DPMS_OFF;
6401
6402 if (mode == connector->dpms)
6403 return;
6404
6405 connector->dpms = mode;
6406
6407 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006408 if (connector->encoder)
6409 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006410
Daniel Vetterb9805142012-08-31 17:37:33 +02006411 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006412}
6413
Daniel Vetterf0947c32012-07-02 13:10:34 +02006414/* Simple connector->get_hw_state implementation for encoders that support only
6415 * one connector and no cloning and hence the encoder state determines the state
6416 * of the connector. */
6417bool intel_connector_get_hw_state(struct intel_connector *connector)
6418{
Daniel Vetter24929352012-07-02 20:28:59 +02006419 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006420 struct intel_encoder *encoder = connector->encoder;
6421
6422 return encoder->get_hw_state(encoder, &pipe);
6423}
6424
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006425static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006426{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006427 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6428 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006429
6430 return 0;
6431}
6432
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006433static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006434 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006435{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006436 struct drm_atomic_state *state = pipe_config->base.state;
6437 struct intel_crtc *other_crtc;
6438 struct intel_crtc_state *other_crtc_state;
6439
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006440 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6441 pipe_name(pipe), pipe_config->fdi_lanes);
6442 if (pipe_config->fdi_lanes > 4) {
6443 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6444 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006445 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006446 }
6447
Paulo Zanonibafb6552013-11-02 21:07:44 -07006448 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006449 if (pipe_config->fdi_lanes > 2) {
6450 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6451 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006452 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006453 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006454 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006455 }
6456 }
6457
6458 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006459 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006460
6461 /* Ivybridge 3 pipe is really complicated */
6462 switch (pipe) {
6463 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006464 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006465 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006466 if (pipe_config->fdi_lanes <= 2)
6467 return 0;
6468
6469 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6470 other_crtc_state =
6471 intel_atomic_get_crtc_state(state, other_crtc);
6472 if (IS_ERR(other_crtc_state))
6473 return PTR_ERR(other_crtc_state);
6474
6475 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006476 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6477 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006478 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006479 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006480 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006481 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006482 if (pipe_config->fdi_lanes > 2) {
6483 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6484 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006485 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006486 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006487
6488 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6489 other_crtc_state =
6490 intel_atomic_get_crtc_state(state, other_crtc);
6491 if (IS_ERR(other_crtc_state))
6492 return PTR_ERR(other_crtc_state);
6493
6494 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006495 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006496 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006497 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006498 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006499 default:
6500 BUG();
6501 }
6502}
6503
Daniel Vettere29c22c2013-02-21 00:00:16 +01006504#define RETRY 1
6505static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006506 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006507{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006508 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006509 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006510 int lane, link_bw, fdi_dotclock, ret;
6511 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006512
Daniel Vettere29c22c2013-02-21 00:00:16 +01006513retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006514 /* FDI is a binary signal running at ~2.7GHz, encoding
6515 * each output octet as 10 bits. The actual frequency
6516 * is stored as a divider into a 100MHz clock, and the
6517 * mode pixel clock is stored in units of 1KHz.
6518 * Hence the bw of each lane in terms of the mode signal
6519 * is:
6520 */
6521 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6522
Damien Lespiau241bfc32013-09-25 16:45:37 +01006523 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006524
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006525 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006526 pipe_config->pipe_bpp);
6527
6528 pipe_config->fdi_lanes = lane;
6529
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006530 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006531 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006532
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006533 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6534 intel_crtc->pipe, pipe_config);
6535 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006536 pipe_config->pipe_bpp -= 2*3;
6537 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6538 pipe_config->pipe_bpp);
6539 needs_recompute = true;
6540 pipe_config->bw_constrained = true;
6541
6542 goto retry;
6543 }
6544
6545 if (needs_recompute)
6546 return RETRY;
6547
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006548 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006549}
6550
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006551static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6552 struct intel_crtc_state *pipe_config)
6553{
6554 if (pipe_config->pipe_bpp > 24)
6555 return false;
6556
6557 /* HSW can handle pixel rate up to cdclk? */
6558 if (IS_HASWELL(dev_priv->dev))
6559 return true;
6560
6561 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006562 * We compare against max which means we must take
6563 * the increased cdclk requirement into account when
6564 * calculating the new cdclk.
6565 *
6566 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006567 */
6568 return ilk_pipe_pixel_rate(pipe_config) <=
6569 dev_priv->max_cdclk_freq * 95 / 100;
6570}
6571
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006572static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006573 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006574{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006575 struct drm_device *dev = crtc->base.dev;
6576 struct drm_i915_private *dev_priv = dev->dev_private;
6577
Jani Nikulad330a952014-01-21 11:24:25 +02006578 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006579 hsw_crtc_supports_ips(crtc) &&
6580 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006581}
6582
Daniel Vettera43f6e02013-06-07 23:10:32 +02006583static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006584 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006585{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006586 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006587 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006588 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006589
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006590 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006591 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006592 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006593
6594 /*
6595 * Enable pixel doubling when the dot clock
6596 * is > 90% of the (display) core speed.
6597 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006598 * GDG double wide on either pipe,
6599 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006600 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006601 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006602 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006603 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006604 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006605 }
6606
Damien Lespiau241bfc32013-09-25 16:45:37 +01006607 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006608 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006609 }
Chris Wilson89749352010-09-12 18:25:19 +01006610
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006611 /*
6612 * Pipe horizontal size must be even in:
6613 * - DVO ganged mode
6614 * - LVDS dual channel mode
6615 * - Double wide pipe
6616 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006617 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006618 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6619 pipe_config->pipe_src_w &= ~1;
6620
Damien Lespiau8693a822013-05-03 18:48:11 +01006621 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6622 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006623 */
6624 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6625 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006626 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006627
Damien Lespiauf5adf942013-06-24 18:29:34 +01006628 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006629 hsw_compute_ips_config(crtc, pipe_config);
6630
Daniel Vetter877d48d2013-04-19 11:24:43 +02006631 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006632 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006633
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006634 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006635}
6636
Ville Syrjälä1652d192015-03-31 14:12:01 +03006637static int skylake_get_display_clock_speed(struct drm_device *dev)
6638{
6639 struct drm_i915_private *dev_priv = to_i915(dev);
6640 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6641 uint32_t cdctl = I915_READ(CDCLK_CTL);
6642 uint32_t linkrate;
6643
Damien Lespiau414355a2015-06-04 18:21:31 +01006644 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006645 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006646
6647 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6648 return 540000;
6649
6650 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006651 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006652
Damien Lespiau71cd8422015-04-30 16:39:17 +01006653 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6654 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006655 /* vco 8640 */
6656 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6657 case CDCLK_FREQ_450_432:
6658 return 432000;
6659 case CDCLK_FREQ_337_308:
6660 return 308570;
6661 case CDCLK_FREQ_675_617:
6662 return 617140;
6663 default:
6664 WARN(1, "Unknown cd freq selection\n");
6665 }
6666 } else {
6667 /* vco 8100 */
6668 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6669 case CDCLK_FREQ_450_432:
6670 return 450000;
6671 case CDCLK_FREQ_337_308:
6672 return 337500;
6673 case CDCLK_FREQ_675_617:
6674 return 675000;
6675 default:
6676 WARN(1, "Unknown cd freq selection\n");
6677 }
6678 }
6679
6680 /* error case, do as if DPLL0 isn't enabled */
6681 return 24000;
6682}
6683
6684static int broadwell_get_display_clock_speed(struct drm_device *dev)
6685{
6686 struct drm_i915_private *dev_priv = dev->dev_private;
6687 uint32_t lcpll = I915_READ(LCPLL_CTL);
6688 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6689
6690 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6691 return 800000;
6692 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6693 return 450000;
6694 else if (freq == LCPLL_CLK_FREQ_450)
6695 return 450000;
6696 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6697 return 540000;
6698 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6699 return 337500;
6700 else
6701 return 675000;
6702}
6703
6704static int haswell_get_display_clock_speed(struct drm_device *dev)
6705{
6706 struct drm_i915_private *dev_priv = dev->dev_private;
6707 uint32_t lcpll = I915_READ(LCPLL_CTL);
6708 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6709
6710 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6711 return 800000;
6712 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6713 return 450000;
6714 else if (freq == LCPLL_CLK_FREQ_450)
6715 return 450000;
6716 else if (IS_HSW_ULT(dev))
6717 return 337500;
6718 else
6719 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006720}
6721
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006722static int valleyview_get_display_clock_speed(struct drm_device *dev)
6723{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006724 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006725 u32 val;
6726 int divider;
6727
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006728 if (dev_priv->hpll_freq == 0)
6729 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6730
Ville Syrjäläa5805162015-05-26 20:42:30 +03006731 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006732 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006733 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006734
6735 divider = val & DISPLAY_FREQUENCY_VALUES;
6736
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006737 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6738 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6739 "cdclk change in progress\n");
6740
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006741 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006742}
6743
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006744static int ilk_get_display_clock_speed(struct drm_device *dev)
6745{
6746 return 450000;
6747}
6748
Jesse Barnese70236a2009-09-21 10:42:27 -07006749static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006750{
Jesse Barnese70236a2009-09-21 10:42:27 -07006751 return 400000;
6752}
Jesse Barnes79e53942008-11-07 14:24:08 -08006753
Jesse Barnese70236a2009-09-21 10:42:27 -07006754static int i915_get_display_clock_speed(struct drm_device *dev)
6755{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006756 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006757}
Jesse Barnes79e53942008-11-07 14:24:08 -08006758
Jesse Barnese70236a2009-09-21 10:42:27 -07006759static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6760{
6761 return 200000;
6762}
Jesse Barnes79e53942008-11-07 14:24:08 -08006763
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006764static int pnv_get_display_clock_speed(struct drm_device *dev)
6765{
6766 u16 gcfgc = 0;
6767
6768 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6769
6770 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6771 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006772 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006773 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006774 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006775 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006776 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006777 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6778 return 200000;
6779 default:
6780 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6781 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006782 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006783 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006784 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006785 }
6786}
6787
Jesse Barnese70236a2009-09-21 10:42:27 -07006788static int i915gm_get_display_clock_speed(struct drm_device *dev)
6789{
6790 u16 gcfgc = 0;
6791
6792 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6793
6794 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006795 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006796 else {
6797 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6798 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006799 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006800 default:
6801 case GC_DISPLAY_CLOCK_190_200_MHZ:
6802 return 190000;
6803 }
6804 }
6805}
Jesse Barnes79e53942008-11-07 14:24:08 -08006806
Jesse Barnese70236a2009-09-21 10:42:27 -07006807static int i865_get_display_clock_speed(struct drm_device *dev)
6808{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006809 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006810}
6811
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006812static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006813{
6814 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006815
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006816 /*
6817 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6818 * encoding is different :(
6819 * FIXME is this the right way to detect 852GM/852GMV?
6820 */
6821 if (dev->pdev->revision == 0x1)
6822 return 133333;
6823
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006824 pci_bus_read_config_word(dev->pdev->bus,
6825 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6826
Jesse Barnese70236a2009-09-21 10:42:27 -07006827 /* Assume that the hardware is in the high speed state. This
6828 * should be the default.
6829 */
6830 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6831 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006832 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006833 case GC_CLOCK_100_200:
6834 return 200000;
6835 case GC_CLOCK_166_250:
6836 return 250000;
6837 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006838 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006839 case GC_CLOCK_133_266:
6840 case GC_CLOCK_133_266_2:
6841 case GC_CLOCK_166_266:
6842 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006843 }
6844
6845 /* Shouldn't happen */
6846 return 0;
6847}
6848
6849static int i830_get_display_clock_speed(struct drm_device *dev)
6850{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006851 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006852}
6853
Ville Syrjälä34edce22015-05-22 11:22:33 +03006854static unsigned int intel_hpll_vco(struct drm_device *dev)
6855{
6856 struct drm_i915_private *dev_priv = dev->dev_private;
6857 static const unsigned int blb_vco[8] = {
6858 [0] = 3200000,
6859 [1] = 4000000,
6860 [2] = 5333333,
6861 [3] = 4800000,
6862 [4] = 6400000,
6863 };
6864 static const unsigned int pnv_vco[8] = {
6865 [0] = 3200000,
6866 [1] = 4000000,
6867 [2] = 5333333,
6868 [3] = 4800000,
6869 [4] = 2666667,
6870 };
6871 static const unsigned int cl_vco[8] = {
6872 [0] = 3200000,
6873 [1] = 4000000,
6874 [2] = 5333333,
6875 [3] = 6400000,
6876 [4] = 3333333,
6877 [5] = 3566667,
6878 [6] = 4266667,
6879 };
6880 static const unsigned int elk_vco[8] = {
6881 [0] = 3200000,
6882 [1] = 4000000,
6883 [2] = 5333333,
6884 [3] = 4800000,
6885 };
6886 static const unsigned int ctg_vco[8] = {
6887 [0] = 3200000,
6888 [1] = 4000000,
6889 [2] = 5333333,
6890 [3] = 6400000,
6891 [4] = 2666667,
6892 [5] = 4266667,
6893 };
6894 const unsigned int *vco_table;
6895 unsigned int vco;
6896 uint8_t tmp = 0;
6897
6898 /* FIXME other chipsets? */
6899 if (IS_GM45(dev))
6900 vco_table = ctg_vco;
6901 else if (IS_G4X(dev))
6902 vco_table = elk_vco;
6903 else if (IS_CRESTLINE(dev))
6904 vco_table = cl_vco;
6905 else if (IS_PINEVIEW(dev))
6906 vco_table = pnv_vco;
6907 else if (IS_G33(dev))
6908 vco_table = blb_vco;
6909 else
6910 return 0;
6911
6912 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6913
6914 vco = vco_table[tmp & 0x7];
6915 if (vco == 0)
6916 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6917 else
6918 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6919
6920 return vco;
6921}
6922
6923static int gm45_get_display_clock_speed(struct drm_device *dev)
6924{
6925 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6926 uint16_t tmp = 0;
6927
6928 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6929
6930 cdclk_sel = (tmp >> 12) & 0x1;
6931
6932 switch (vco) {
6933 case 2666667:
6934 case 4000000:
6935 case 5333333:
6936 return cdclk_sel ? 333333 : 222222;
6937 case 3200000:
6938 return cdclk_sel ? 320000 : 228571;
6939 default:
6940 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6941 return 222222;
6942 }
6943}
6944
6945static int i965gm_get_display_clock_speed(struct drm_device *dev)
6946{
6947 static const uint8_t div_3200[] = { 16, 10, 8 };
6948 static const uint8_t div_4000[] = { 20, 12, 10 };
6949 static const uint8_t div_5333[] = { 24, 16, 14 };
6950 const uint8_t *div_table;
6951 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6952 uint16_t tmp = 0;
6953
6954 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6955
6956 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6957
6958 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6959 goto fail;
6960
6961 switch (vco) {
6962 case 3200000:
6963 div_table = div_3200;
6964 break;
6965 case 4000000:
6966 div_table = div_4000;
6967 break;
6968 case 5333333:
6969 div_table = div_5333;
6970 break;
6971 default:
6972 goto fail;
6973 }
6974
6975 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6976
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006977fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006978 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6979 return 200000;
6980}
6981
6982static int g33_get_display_clock_speed(struct drm_device *dev)
6983{
6984 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6985 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6986 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6987 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6988 const uint8_t *div_table;
6989 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6990 uint16_t tmp = 0;
6991
6992 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6993
6994 cdclk_sel = (tmp >> 4) & 0x7;
6995
6996 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6997 goto fail;
6998
6999 switch (vco) {
7000 case 3200000:
7001 div_table = div_3200;
7002 break;
7003 case 4000000:
7004 div_table = div_4000;
7005 break;
7006 case 4800000:
7007 div_table = div_4800;
7008 break;
7009 case 5333333:
7010 div_table = div_5333;
7011 break;
7012 default:
7013 goto fail;
7014 }
7015
7016 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7017
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007018fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007019 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7020 return 190476;
7021}
7022
Zhenyu Wang2c072452009-06-05 15:38:42 +08007023static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007024intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007025{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007026 while (*num > DATA_LINK_M_N_MASK ||
7027 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007028 *num >>= 1;
7029 *den >>= 1;
7030 }
7031}
7032
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007033static void compute_m_n(unsigned int m, unsigned int n,
7034 uint32_t *ret_m, uint32_t *ret_n)
7035{
7036 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7037 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7038 intel_reduce_m_n_ratio(ret_m, ret_n);
7039}
7040
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007041void
7042intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7043 int pixel_clock, int link_clock,
7044 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007045{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007046 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007047
7048 compute_m_n(bits_per_pixel * pixel_clock,
7049 link_clock * nlanes * 8,
7050 &m_n->gmch_m, &m_n->gmch_n);
7051
7052 compute_m_n(pixel_clock, link_clock,
7053 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007054}
7055
Chris Wilsona7615032011-01-12 17:04:08 +00007056static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7057{
Jani Nikulad330a952014-01-21 11:24:25 +02007058 if (i915.panel_use_ssc >= 0)
7059 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007060 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007061 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007062}
7063
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007064static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7065 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007066{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007067 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007068 struct drm_i915_private *dev_priv = dev->dev_private;
7069 int refclk;
7070
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007071 WARN_ON(!crtc_state->base.state);
7072
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007073 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007074 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007075 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007076 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007077 refclk = dev_priv->vbt.lvds_ssc_freq;
7078 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007079 } else if (!IS_GEN2(dev)) {
7080 refclk = 96000;
7081 } else {
7082 refclk = 48000;
7083 }
7084
7085 return refclk;
7086}
7087
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007088static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007089{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007090 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007091}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007092
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007093static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7094{
7095 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007096}
7097
Daniel Vetterf47709a2013-03-28 10:42:02 +01007098static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007099 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007100 intel_clock_t *reduced_clock)
7101{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007102 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007103 u32 fp, fp2 = 0;
7104
7105 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007106 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007107 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007108 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007109 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007110 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007111 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007112 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007113 }
7114
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007115 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007116
Daniel Vetterf47709a2013-03-28 10:42:02 +01007117 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007118 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007119 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007120 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007121 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007122 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007123 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007124 }
7125}
7126
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007127static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7128 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007129{
7130 u32 reg_val;
7131
7132 /*
7133 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7134 * and set it to a reasonable value instead.
7135 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007136 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007137 reg_val &= 0xffffff00;
7138 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007139 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007140
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007141 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007142 reg_val &= 0x8cffffff;
7143 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007144 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007145
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007146 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007147 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007148 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007149
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007150 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007151 reg_val &= 0x00ffffff;
7152 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007153 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007154}
7155
Daniel Vetterb5518422013-05-03 11:49:48 +02007156static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7157 struct intel_link_m_n *m_n)
7158{
7159 struct drm_device *dev = crtc->base.dev;
7160 struct drm_i915_private *dev_priv = dev->dev_private;
7161 int pipe = crtc->pipe;
7162
Daniel Vettere3b95f12013-05-03 11:49:49 +02007163 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7164 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7165 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7166 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007167}
7168
7169static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007170 struct intel_link_m_n *m_n,
7171 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007172{
7173 struct drm_device *dev = crtc->base.dev;
7174 struct drm_i915_private *dev_priv = dev->dev_private;
7175 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007176 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007177
7178 if (INTEL_INFO(dev)->gen >= 5) {
7179 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7180 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7181 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7182 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007183 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7184 * for gen < 8) and if DRRS is supported (to make sure the
7185 * registers are not unnecessarily accessed).
7186 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307187 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007188 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007189 I915_WRITE(PIPE_DATA_M2(transcoder),
7190 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7191 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7192 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7193 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7194 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007195 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007196 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7197 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7198 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7199 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007200 }
7201}
7202
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307203void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007204{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307205 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7206
7207 if (m_n == M1_N1) {
7208 dp_m_n = &crtc->config->dp_m_n;
7209 dp_m2_n2 = &crtc->config->dp_m2_n2;
7210 } else if (m_n == M2_N2) {
7211
7212 /*
7213 * M2_N2 registers are not supported. Hence m2_n2 divider value
7214 * needs to be programmed into M1_N1.
7215 */
7216 dp_m_n = &crtc->config->dp_m2_n2;
7217 } else {
7218 DRM_ERROR("Unsupported divider value\n");
7219 return;
7220 }
7221
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007222 if (crtc->config->has_pch_encoder)
7223 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007224 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307225 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007226}
7227
Ville Syrjäläd288f652014-10-28 13:20:22 +02007228static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007229 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007230{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007231 u32 dpll, dpll_md;
7232
7233 /*
7234 * Enable DPIO clock input. We should never disable the reference
7235 * clock for pipe B, since VGA hotplug / manual detection depends
7236 * on it.
7237 */
7238 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7239 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7240 /* We should never disable this, set it here for state tracking */
7241 if (crtc->pipe == PIPE_B)
7242 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7243 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007244 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007245
Ville Syrjäläd288f652014-10-28 13:20:22 +02007246 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007247 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007248 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007249}
7250
Ville Syrjäläd288f652014-10-28 13:20:22 +02007251static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007252 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007253{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007254 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007255 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007256 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007257 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007258 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007259 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007260
Ville Syrjäläa5805162015-05-26 20:42:30 +03007261 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007262
Ville Syrjäläd288f652014-10-28 13:20:22 +02007263 bestn = pipe_config->dpll.n;
7264 bestm1 = pipe_config->dpll.m1;
7265 bestm2 = pipe_config->dpll.m2;
7266 bestp1 = pipe_config->dpll.p1;
7267 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007268
Jesse Barnes89b667f2013-04-18 14:51:36 -07007269 /* See eDP HDMI DPIO driver vbios notes doc */
7270
7271 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007272 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007273 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007274
7275 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007276 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007277
7278 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007279 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007280 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007281 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007282
7283 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007284 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285
7286 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007287 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7288 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7289 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007290 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007291
7292 /*
7293 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7294 * but we don't support that).
7295 * Note: don't use the DAC post divider as it seems unstable.
7296 */
7297 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007298 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007299
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007300 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007301 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007302
Jesse Barnes89b667f2013-04-18 14:51:36 -07007303 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007304 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007305 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7306 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007307 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007308 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007309 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007310 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007311 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007312
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007313 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007314 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007315 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007316 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007317 0x0df40000);
7318 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007319 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007320 0x0df70000);
7321 } else { /* HDMI or VGA */
7322 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007323 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007324 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007325 0x0df70000);
7326 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007327 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007328 0x0df40000);
7329 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007330
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007331 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007332 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007333 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7334 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007335 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007336 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007337
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007338 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007339 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007340}
7341
Ville Syrjäläd288f652014-10-28 13:20:22 +02007342static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007343 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007344{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007345 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007346 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7347 DPLL_VCO_ENABLE;
7348 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007349 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007350
Ville Syrjäläd288f652014-10-28 13:20:22 +02007351 pipe_config->dpll_hw_state.dpll_md =
7352 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007353}
7354
Ville Syrjäläd288f652014-10-28 13:20:22 +02007355static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007356 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007357{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007358 struct drm_device *dev = crtc->base.dev;
7359 struct drm_i915_private *dev_priv = dev->dev_private;
7360 int pipe = crtc->pipe;
7361 int dpll_reg = DPLL(crtc->pipe);
7362 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307363 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007364 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307365 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307366 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007367
Ville Syrjäläd288f652014-10-28 13:20:22 +02007368 bestn = pipe_config->dpll.n;
7369 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7370 bestm1 = pipe_config->dpll.m1;
7371 bestm2 = pipe_config->dpll.m2 >> 22;
7372 bestp1 = pipe_config->dpll.p1;
7373 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307374 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307375 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307376 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007377
7378 /*
7379 * Enable Refclk and SSC
7380 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007381 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007382 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007383
Ville Syrjäläa5805162015-05-26 20:42:30 +03007384 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007385
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007386 /* p1 and p2 divider */
7387 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7388 5 << DPIO_CHV_S1_DIV_SHIFT |
7389 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7390 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7391 1 << DPIO_CHV_K_DIV_SHIFT);
7392
7393 /* Feedback post-divider - m2 */
7394 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7395
7396 /* Feedback refclk divider - n and m1 */
7397 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7398 DPIO_CHV_M1_DIV_BY_2 |
7399 1 << DPIO_CHV_N_DIV_SHIFT);
7400
7401 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307402 if (bestm2_frac)
7403 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007404
7405 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307406 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7407 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7408 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7409 if (bestm2_frac)
7410 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7411 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007412
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307413 /* Program digital lock detect threshold */
7414 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7415 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7416 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7417 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7418 if (!bestm2_frac)
7419 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7420 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7421
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007422 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307423 if (vco == 5400000) {
7424 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7425 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7426 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7427 tribuf_calcntr = 0x9;
7428 } else if (vco <= 6200000) {
7429 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7430 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7431 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7432 tribuf_calcntr = 0x9;
7433 } else if (vco <= 6480000) {
7434 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7435 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7436 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7437 tribuf_calcntr = 0x8;
7438 } else {
7439 /* Not supported. Apply the same limits as in the max case */
7440 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7441 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7442 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7443 tribuf_calcntr = 0;
7444 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007445 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7446
Ville Syrjälä968040b2015-03-11 22:52:08 +02007447 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307448 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7449 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7450 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7451
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007452 /* AFC Recal */
7453 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7454 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7455 DPIO_AFC_RECAL);
7456
Ville Syrjäläa5805162015-05-26 20:42:30 +03007457 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007458}
7459
Ville Syrjäläd288f652014-10-28 13:20:22 +02007460/**
7461 * vlv_force_pll_on - forcibly enable just the PLL
7462 * @dev_priv: i915 private structure
7463 * @pipe: pipe PLL to enable
7464 * @dpll: PLL configuration
7465 *
7466 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7467 * in cases where we need the PLL enabled even when @pipe is not going to
7468 * be enabled.
7469 */
7470void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7471 const struct dpll *dpll)
7472{
7473 struct intel_crtc *crtc =
7474 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007475 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007476 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007477 .pixel_multiplier = 1,
7478 .dpll = *dpll,
7479 };
7480
7481 if (IS_CHERRYVIEW(dev)) {
7482 chv_update_pll(crtc, &pipe_config);
7483 chv_prepare_pll(crtc, &pipe_config);
7484 chv_enable_pll(crtc, &pipe_config);
7485 } else {
7486 vlv_update_pll(crtc, &pipe_config);
7487 vlv_prepare_pll(crtc, &pipe_config);
7488 vlv_enable_pll(crtc, &pipe_config);
7489 }
7490}
7491
7492/**
7493 * vlv_force_pll_off - forcibly disable just the PLL
7494 * @dev_priv: i915 private structure
7495 * @pipe: pipe PLL to disable
7496 *
7497 * Disable the PLL for @pipe. To be used in cases where we need
7498 * the PLL enabled even when @pipe is not going to be enabled.
7499 */
7500void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7501{
7502 if (IS_CHERRYVIEW(dev))
7503 chv_disable_pll(to_i915(dev), pipe);
7504 else
7505 vlv_disable_pll(to_i915(dev), pipe);
7506}
7507
Daniel Vetterf47709a2013-03-28 10:42:02 +01007508static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007509 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007510 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007511 int num_connectors)
7512{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007513 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007514 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007515 u32 dpll;
7516 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007517 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007518
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007519 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307520
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007521 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7522 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007523
7524 dpll = DPLL_VGA_MODE_DIS;
7525
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007526 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007527 dpll |= DPLLB_MODE_LVDS;
7528 else
7529 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007530
Daniel Vetteref1b4602013-06-01 17:17:04 +02007531 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007532 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007533 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007534 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007535
7536 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007537 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007538
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007539 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007540 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007541
7542 /* compute bitmask from p1 value */
7543 if (IS_PINEVIEW(dev))
7544 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7545 else {
7546 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7547 if (IS_G4X(dev) && reduced_clock)
7548 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7549 }
7550 switch (clock->p2) {
7551 case 5:
7552 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7553 break;
7554 case 7:
7555 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7556 break;
7557 case 10:
7558 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7559 break;
7560 case 14:
7561 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7562 break;
7563 }
7564 if (INTEL_INFO(dev)->gen >= 4)
7565 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7566
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007567 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007568 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007569 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007570 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7571 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7572 else
7573 dpll |= PLL_REF_INPUT_DREFCLK;
7574
7575 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007576 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007577
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007578 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007579 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007580 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007581 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007582 }
7583}
7584
Daniel Vetterf47709a2013-03-28 10:42:02 +01007585static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007586 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007587 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007588 int num_connectors)
7589{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007590 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007591 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007592 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007593 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007594
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007595 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307596
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007597 dpll = DPLL_VGA_MODE_DIS;
7598
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007599 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007600 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7601 } else {
7602 if (clock->p1 == 2)
7603 dpll |= PLL_P1_DIVIDE_BY_TWO;
7604 else
7605 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7606 if (clock->p2 == 4)
7607 dpll |= PLL_P2_DIVIDE_BY_4;
7608 }
7609
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007610 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007611 dpll |= DPLL_DVO_2X_MODE;
7612
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007613 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007614 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7615 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7616 else
7617 dpll |= PLL_REF_INPUT_DREFCLK;
7618
7619 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007620 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007621}
7622
Daniel Vetter8a654f32013-06-01 17:16:22 +02007623static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007624{
7625 struct drm_device *dev = intel_crtc->base.dev;
7626 struct drm_i915_private *dev_priv = dev->dev_private;
7627 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007628 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007629 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007630 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007631 uint32_t crtc_vtotal, crtc_vblank_end;
7632 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007633
7634 /* We need to be careful not to changed the adjusted mode, for otherwise
7635 * the hw state checker will get angry at the mismatch. */
7636 crtc_vtotal = adjusted_mode->crtc_vtotal;
7637 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007638
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007639 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007640 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007641 crtc_vtotal -= 1;
7642 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007643
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007644 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007645 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7646 else
7647 vsyncshift = adjusted_mode->crtc_hsync_start -
7648 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007649 if (vsyncshift < 0)
7650 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007651 }
7652
7653 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007654 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007655
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007656 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007657 (adjusted_mode->crtc_hdisplay - 1) |
7658 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007659 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007660 (adjusted_mode->crtc_hblank_start - 1) |
7661 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007662 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007663 (adjusted_mode->crtc_hsync_start - 1) |
7664 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7665
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007666 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007667 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007668 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007669 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007670 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007671 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007672 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007673 (adjusted_mode->crtc_vsync_start - 1) |
7674 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7675
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007676 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7677 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7678 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7679 * bits. */
7680 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7681 (pipe == PIPE_B || pipe == PIPE_C))
7682 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7683
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007684 /* pipesrc controls the size that is scaled from, which should
7685 * always be the user's requested size.
7686 */
7687 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007688 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7689 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007690}
7691
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007692static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007693 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007694{
7695 struct drm_device *dev = crtc->base.dev;
7696 struct drm_i915_private *dev_priv = dev->dev_private;
7697 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7698 uint32_t tmp;
7699
7700 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007701 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7702 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007703 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007704 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7705 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007706 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007707 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7708 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007709
7710 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007711 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7712 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007713 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007714 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7715 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007716 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007717 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7718 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007719
7720 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007721 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7722 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7723 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007724 }
7725
7726 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007727 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7728 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7729
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007730 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7731 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007732}
7733
Daniel Vetterf6a83282014-02-11 15:28:57 -08007734void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007735 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007736{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007737 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7738 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7739 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7740 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007741
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007742 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7743 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7744 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7745 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007746
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007747 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007748
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007749 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7750 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007751}
7752
Daniel Vetter84b046f2013-02-19 18:48:54 +01007753static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7754{
7755 struct drm_device *dev = intel_crtc->base.dev;
7756 struct drm_i915_private *dev_priv = dev->dev_private;
7757 uint32_t pipeconf;
7758
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007759 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007760
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007761 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7762 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7763 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007764
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007765 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007766 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007767
Daniel Vetterff9ce462013-04-24 14:57:17 +02007768 /* only g4x and later have fancy bpc/dither controls */
7769 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007770 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007771 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007772 pipeconf |= PIPECONF_DITHER_EN |
7773 PIPECONF_DITHER_TYPE_SP;
7774
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007775 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007776 case 18:
7777 pipeconf |= PIPECONF_6BPC;
7778 break;
7779 case 24:
7780 pipeconf |= PIPECONF_8BPC;
7781 break;
7782 case 30:
7783 pipeconf |= PIPECONF_10BPC;
7784 break;
7785 default:
7786 /* Case prevented by intel_choose_pipe_bpp_dither. */
7787 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007788 }
7789 }
7790
7791 if (HAS_PIPE_CXSR(dev)) {
7792 if (intel_crtc->lowfreq_avail) {
7793 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7794 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7795 } else {
7796 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007797 }
7798 }
7799
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007800 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007801 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007802 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007803 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7804 else
7805 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7806 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007807 pipeconf |= PIPECONF_PROGRESSIVE;
7808
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007809 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007810 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007811
Daniel Vetter84b046f2013-02-19 18:48:54 +01007812 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7813 POSTING_READ(PIPECONF(intel_crtc->pipe));
7814}
7815
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007816static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7817 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007818{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007819 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007820 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007821 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007822 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007823 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007824 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007825 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007826 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007827 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007828 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007829 struct drm_connector_state *connector_state;
7830 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007831
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007832 memset(&crtc_state->dpll_hw_state, 0,
7833 sizeof(crtc_state->dpll_hw_state));
7834
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007835 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007836 if (connector_state->crtc != &crtc->base)
7837 continue;
7838
7839 encoder = to_intel_encoder(connector_state->best_encoder);
7840
Chris Wilson5eddb702010-09-11 13:48:45 +01007841 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007842 case INTEL_OUTPUT_LVDS:
7843 is_lvds = true;
7844 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007845 case INTEL_OUTPUT_DSI:
7846 is_dsi = true;
7847 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007848 default:
7849 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007850 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007851
Eric Anholtc751ce42010-03-25 11:48:48 -07007852 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007853 }
7854
Jani Nikulaf2335332013-09-13 11:03:09 +03007855 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007856 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007857
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007858 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007859 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007860
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007861 /*
7862 * Returns a set of divisors for the desired target clock with
7863 * the given refclk, or FALSE. The returned values represent
7864 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7865 * 2) / p1 / p2.
7866 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007867 limit = intel_limit(crtc_state, refclk);
7868 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007869 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007870 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007871 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007872 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7873 return -EINVAL;
7874 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007875
Jani Nikulaf2335332013-09-13 11:03:09 +03007876 if (is_lvds && dev_priv->lvds_downclock_avail) {
7877 /*
7878 * Ensure we match the reduced clock's P to the target
7879 * clock. If the clocks don't match, we can't switch
7880 * the display clock by using the FP0/FP1. In such case
7881 * we will disable the LVDS downclock feature.
7882 */
7883 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007884 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007885 dev_priv->lvds_downclock,
7886 refclk, &clock,
7887 &reduced_clock);
7888 }
7889 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007890 crtc_state->dpll.n = clock.n;
7891 crtc_state->dpll.m1 = clock.m1;
7892 crtc_state->dpll.m2 = clock.m2;
7893 crtc_state->dpll.p1 = clock.p1;
7894 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007895 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007896
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007897 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007898 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307899 has_reduced_clock ? &reduced_clock : NULL,
7900 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007901 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007902 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007903 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007904 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007905 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007906 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007907 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007908 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007909 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007910
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007911 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007912}
7913
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007914static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007915 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007916{
7917 struct drm_device *dev = crtc->base.dev;
7918 struct drm_i915_private *dev_priv = dev->dev_private;
7919 uint32_t tmp;
7920
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007921 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7922 return;
7923
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007924 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007925 if (!(tmp & PFIT_ENABLE))
7926 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007927
Daniel Vetter06922822013-07-11 13:35:40 +02007928 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007929 if (INTEL_INFO(dev)->gen < 4) {
7930 if (crtc->pipe != PIPE_B)
7931 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007932 } else {
7933 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7934 return;
7935 }
7936
Daniel Vetter06922822013-07-11 13:35:40 +02007937 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007938 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7939 if (INTEL_INFO(dev)->gen < 5)
7940 pipe_config->gmch_pfit.lvds_border_bits =
7941 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7942}
7943
Jesse Barnesacbec812013-09-20 11:29:32 -07007944static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007945 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007946{
7947 struct drm_device *dev = crtc->base.dev;
7948 struct drm_i915_private *dev_priv = dev->dev_private;
7949 int pipe = pipe_config->cpu_transcoder;
7950 intel_clock_t clock;
7951 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007952 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007953
Shobhit Kumarf573de52014-07-30 20:32:37 +05307954 /* In case of MIPI DPLL will not even be used */
7955 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7956 return;
7957
Ville Syrjäläa5805162015-05-26 20:42:30 +03007958 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007959 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007960 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007961
7962 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7963 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7964 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7965 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7966 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7967
Ville Syrjäläf6466282013-10-14 14:50:31 +03007968 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007969
Ville Syrjäläf6466282013-10-14 14:50:31 +03007970 /* clock.dot is the fast clock */
7971 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007972}
7973
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007974static void
7975i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7976 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007977{
7978 struct drm_device *dev = crtc->base.dev;
7979 struct drm_i915_private *dev_priv = dev->dev_private;
7980 u32 val, base, offset;
7981 int pipe = crtc->pipe, plane = crtc->plane;
7982 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007983 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007984 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007985 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007986
Damien Lespiau42a7b082015-02-05 19:35:13 +00007987 val = I915_READ(DSPCNTR(plane));
7988 if (!(val & DISPLAY_PLANE_ENABLE))
7989 return;
7990
Damien Lespiaud9806c92015-01-21 14:07:19 +00007991 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007992 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007993 DRM_DEBUG_KMS("failed to alloc fb\n");
7994 return;
7995 }
7996
Damien Lespiau1b842c82015-01-21 13:50:54 +00007997 fb = &intel_fb->base;
7998
Daniel Vetter18c52472015-02-10 17:16:09 +00007999 if (INTEL_INFO(dev)->gen >= 4) {
8000 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008001 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008002 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8003 }
8004 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008005
8006 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008007 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008008 fb->pixel_format = fourcc;
8009 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008010
8011 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008012 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008013 offset = I915_READ(DSPTILEOFF(plane));
8014 else
8015 offset = I915_READ(DSPLINOFF(plane));
8016 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8017 } else {
8018 base = I915_READ(DSPADDR(plane));
8019 }
8020 plane_config->base = base;
8021
8022 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008023 fb->width = ((val >> 16) & 0xfff) + 1;
8024 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008025
8026 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008027 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008028
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008029 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008030 fb->pixel_format,
8031 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008032
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008033 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008034
Damien Lespiau2844a922015-01-20 12:51:48 +00008035 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8036 pipe_name(pipe), plane, fb->width, fb->height,
8037 fb->bits_per_pixel, base, fb->pitches[0],
8038 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008039
Damien Lespiau2d140302015-02-05 17:22:18 +00008040 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008041}
8042
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008043static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008044 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008045{
8046 struct drm_device *dev = crtc->base.dev;
8047 struct drm_i915_private *dev_priv = dev->dev_private;
8048 int pipe = pipe_config->cpu_transcoder;
8049 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8050 intel_clock_t clock;
8051 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8052 int refclk = 100000;
8053
Ville Syrjäläa5805162015-05-26 20:42:30 +03008054 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008055 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8056 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8057 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8058 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008059 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008060
8061 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8062 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8063 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8064 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8065 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8066
8067 chv_clock(refclk, &clock);
8068
8069 /* clock.dot is the fast clock */
8070 pipe_config->port_clock = clock.dot / 5;
8071}
8072
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008073static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008074 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008075{
8076 struct drm_device *dev = crtc->base.dev;
8077 struct drm_i915_private *dev_priv = dev->dev_private;
8078 uint32_t tmp;
8079
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008080 if (!intel_display_power_is_enabled(dev_priv,
8081 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008082 return false;
8083
Daniel Vettere143a212013-07-04 12:01:15 +02008084 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008085 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008086
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008087 tmp = I915_READ(PIPECONF(crtc->pipe));
8088 if (!(tmp & PIPECONF_ENABLE))
8089 return false;
8090
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008091 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8092 switch (tmp & PIPECONF_BPC_MASK) {
8093 case PIPECONF_6BPC:
8094 pipe_config->pipe_bpp = 18;
8095 break;
8096 case PIPECONF_8BPC:
8097 pipe_config->pipe_bpp = 24;
8098 break;
8099 case PIPECONF_10BPC:
8100 pipe_config->pipe_bpp = 30;
8101 break;
8102 default:
8103 break;
8104 }
8105 }
8106
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008107 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8108 pipe_config->limited_color_range = true;
8109
Ville Syrjälä282740f2013-09-04 18:30:03 +03008110 if (INTEL_INFO(dev)->gen < 4)
8111 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8112
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008113 intel_get_pipe_timings(crtc, pipe_config);
8114
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008115 i9xx_get_pfit_config(crtc, pipe_config);
8116
Daniel Vetter6c49f242013-06-06 12:45:25 +02008117 if (INTEL_INFO(dev)->gen >= 4) {
8118 tmp = I915_READ(DPLL_MD(crtc->pipe));
8119 pipe_config->pixel_multiplier =
8120 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8121 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008122 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008123 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8124 tmp = I915_READ(DPLL(crtc->pipe));
8125 pipe_config->pixel_multiplier =
8126 ((tmp & SDVO_MULTIPLIER_MASK)
8127 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8128 } else {
8129 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8130 * port and will be fixed up in the encoder->get_config
8131 * function. */
8132 pipe_config->pixel_multiplier = 1;
8133 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008134 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8135 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008136 /*
8137 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8138 * on 830. Filter it out here so that we don't
8139 * report errors due to that.
8140 */
8141 if (IS_I830(dev))
8142 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8143
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008144 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8145 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008146 } else {
8147 /* Mask out read-only status bits. */
8148 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8149 DPLL_PORTC_READY_MASK |
8150 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008151 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008152
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008153 if (IS_CHERRYVIEW(dev))
8154 chv_crtc_clock_get(crtc, pipe_config);
8155 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008156 vlv_crtc_clock_get(crtc, pipe_config);
8157 else
8158 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008159
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008160 return true;
8161}
8162
Paulo Zanonidde86e22012-12-01 12:04:25 -02008163static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008164{
8165 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008166 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008167 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008168 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008169 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008170 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008171 bool has_ck505 = false;
8172 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008173
8174 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008175 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008176 switch (encoder->type) {
8177 case INTEL_OUTPUT_LVDS:
8178 has_panel = true;
8179 has_lvds = true;
8180 break;
8181 case INTEL_OUTPUT_EDP:
8182 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008183 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008184 has_cpu_edp = true;
8185 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008186 default:
8187 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008188 }
8189 }
8190
Keith Packard99eb6a02011-09-26 14:29:12 -07008191 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008192 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008193 can_ssc = has_ck505;
8194 } else {
8195 has_ck505 = false;
8196 can_ssc = true;
8197 }
8198
Imre Deak2de69052013-05-08 13:14:04 +03008199 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8200 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008201
8202 /* Ironlake: try to setup display ref clock before DPLL
8203 * enabling. This is only under driver's control after
8204 * PCH B stepping, previous chipset stepping should be
8205 * ignoring this setting.
8206 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008207 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008208
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008209 /* As we must carefully and slowly disable/enable each source in turn,
8210 * compute the final state we want first and check if we need to
8211 * make any changes at all.
8212 */
8213 final = val;
8214 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008215 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008216 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008217 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008218 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8219
8220 final &= ~DREF_SSC_SOURCE_MASK;
8221 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8222 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008223
Keith Packard199e5d72011-09-22 12:01:57 -07008224 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008225 final |= DREF_SSC_SOURCE_ENABLE;
8226
8227 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8228 final |= DREF_SSC1_ENABLE;
8229
8230 if (has_cpu_edp) {
8231 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8232 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8233 else
8234 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8235 } else
8236 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8237 } else {
8238 final |= DREF_SSC_SOURCE_DISABLE;
8239 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8240 }
8241
8242 if (final == val)
8243 return;
8244
8245 /* Always enable nonspread source */
8246 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8247
8248 if (has_ck505)
8249 val |= DREF_NONSPREAD_CK505_ENABLE;
8250 else
8251 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8252
8253 if (has_panel) {
8254 val &= ~DREF_SSC_SOURCE_MASK;
8255 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008256
Keith Packard199e5d72011-09-22 12:01:57 -07008257 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008258 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008259 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008260 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008261 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008262 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008263
8264 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008265 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008266 POSTING_READ(PCH_DREF_CONTROL);
8267 udelay(200);
8268
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008269 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008270
8271 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008272 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008273 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008274 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008275 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008276 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008277 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008278 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008279 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008280
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008281 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008282 POSTING_READ(PCH_DREF_CONTROL);
8283 udelay(200);
8284 } else {
8285 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8286
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008287 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008288
8289 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008290 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008291
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008292 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008293 POSTING_READ(PCH_DREF_CONTROL);
8294 udelay(200);
8295
8296 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008297 val &= ~DREF_SSC_SOURCE_MASK;
8298 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008299
8300 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008301 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008302
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008303 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008304 POSTING_READ(PCH_DREF_CONTROL);
8305 udelay(200);
8306 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008307
8308 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008309}
8310
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008311static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008312{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008313 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008314
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008315 tmp = I915_READ(SOUTH_CHICKEN2);
8316 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8317 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008318
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008319 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8320 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8321 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008322
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008323 tmp = I915_READ(SOUTH_CHICKEN2);
8324 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8325 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008326
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008327 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8328 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8329 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008330}
8331
8332/* WaMPhyProgramming:hsw */
8333static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8334{
8335 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008336
8337 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8338 tmp &= ~(0xFF << 24);
8339 tmp |= (0x12 << 24);
8340 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8341
Paulo Zanonidde86e22012-12-01 12:04:25 -02008342 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8343 tmp |= (1 << 11);
8344 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8345
8346 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8347 tmp |= (1 << 11);
8348 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8349
Paulo Zanonidde86e22012-12-01 12:04:25 -02008350 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8351 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8352 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8353
8354 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8355 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8356 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8357
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008358 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8359 tmp &= ~(7 << 13);
8360 tmp |= (5 << 13);
8361 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008362
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008363 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8364 tmp &= ~(7 << 13);
8365 tmp |= (5 << 13);
8366 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008367
8368 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8369 tmp &= ~0xFF;
8370 tmp |= 0x1C;
8371 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8372
8373 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8374 tmp &= ~0xFF;
8375 tmp |= 0x1C;
8376 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8377
8378 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8379 tmp &= ~(0xFF << 16);
8380 tmp |= (0x1C << 16);
8381 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8382
8383 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8384 tmp &= ~(0xFF << 16);
8385 tmp |= (0x1C << 16);
8386 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8387
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008388 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8389 tmp |= (1 << 27);
8390 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008391
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008392 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8393 tmp |= (1 << 27);
8394 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008395
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008396 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8397 tmp &= ~(0xF << 28);
8398 tmp |= (4 << 28);
8399 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008400
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008401 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8402 tmp &= ~(0xF << 28);
8403 tmp |= (4 << 28);
8404 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008405}
8406
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008407/* Implements 3 different sequences from BSpec chapter "Display iCLK
8408 * Programming" based on the parameters passed:
8409 * - Sequence to enable CLKOUT_DP
8410 * - Sequence to enable CLKOUT_DP without spread
8411 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8412 */
8413static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8414 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008415{
8416 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008417 uint32_t reg, tmp;
8418
8419 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8420 with_spread = true;
8421 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8422 with_fdi, "LP PCH doesn't have FDI\n"))
8423 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008424
Ville Syrjäläa5805162015-05-26 20:42:30 +03008425 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008426
8427 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8428 tmp &= ~SBI_SSCCTL_DISABLE;
8429 tmp |= SBI_SSCCTL_PATHALT;
8430 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8431
8432 udelay(24);
8433
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008434 if (with_spread) {
8435 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8436 tmp &= ~SBI_SSCCTL_PATHALT;
8437 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008438
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008439 if (with_fdi) {
8440 lpt_reset_fdi_mphy(dev_priv);
8441 lpt_program_fdi_mphy(dev_priv);
8442 }
8443 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008444
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008445 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8446 SBI_GEN0 : SBI_DBUFF0;
8447 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8448 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8449 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008450
Ville Syrjäläa5805162015-05-26 20:42:30 +03008451 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008452}
8453
Paulo Zanoni47701c32013-07-23 11:19:25 -03008454/* Sequence to disable CLKOUT_DP */
8455static void lpt_disable_clkout_dp(struct drm_device *dev)
8456{
8457 struct drm_i915_private *dev_priv = dev->dev_private;
8458 uint32_t reg, tmp;
8459
Ville Syrjäläa5805162015-05-26 20:42:30 +03008460 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008461
8462 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8463 SBI_GEN0 : SBI_DBUFF0;
8464 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8465 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8466 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8467
8468 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8469 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8470 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8471 tmp |= SBI_SSCCTL_PATHALT;
8472 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8473 udelay(32);
8474 }
8475 tmp |= SBI_SSCCTL_DISABLE;
8476 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8477 }
8478
Ville Syrjäläa5805162015-05-26 20:42:30 +03008479 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008480}
8481
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008482static void lpt_init_pch_refclk(struct drm_device *dev)
8483{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008484 struct intel_encoder *encoder;
8485 bool has_vga = false;
8486
Damien Lespiaub2784e12014-08-05 11:29:37 +01008487 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008488 switch (encoder->type) {
8489 case INTEL_OUTPUT_ANALOG:
8490 has_vga = true;
8491 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008492 default:
8493 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008494 }
8495 }
8496
Paulo Zanoni47701c32013-07-23 11:19:25 -03008497 if (has_vga)
8498 lpt_enable_clkout_dp(dev, true, true);
8499 else
8500 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008501}
8502
Paulo Zanonidde86e22012-12-01 12:04:25 -02008503/*
8504 * Initialize reference clocks when the driver loads
8505 */
8506void intel_init_pch_refclk(struct drm_device *dev)
8507{
8508 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8509 ironlake_init_pch_refclk(dev);
8510 else if (HAS_PCH_LPT(dev))
8511 lpt_init_pch_refclk(dev);
8512}
8513
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008514static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008515{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008516 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008517 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008518 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008519 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008520 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008521 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008522 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008523 bool is_lvds = false;
8524
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008525 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008526 if (connector_state->crtc != crtc_state->base.crtc)
8527 continue;
8528
8529 encoder = to_intel_encoder(connector_state->best_encoder);
8530
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008531 switch (encoder->type) {
8532 case INTEL_OUTPUT_LVDS:
8533 is_lvds = true;
8534 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008535 default:
8536 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008537 }
8538 num_connectors++;
8539 }
8540
8541 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008542 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008543 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008544 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008545 }
8546
8547 return 120000;
8548}
8549
Daniel Vetter6ff93602013-04-19 11:24:36 +02008550static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008551{
8552 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8554 int pipe = intel_crtc->pipe;
8555 uint32_t val;
8556
Daniel Vetter78114072013-06-13 00:54:57 +02008557 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008558
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008559 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008560 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008561 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008562 break;
8563 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008564 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008565 break;
8566 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008567 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008568 break;
8569 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008570 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008571 break;
8572 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008573 /* Case prevented by intel_choose_pipe_bpp_dither. */
8574 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008575 }
8576
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008577 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008578 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8579
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008580 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008581 val |= PIPECONF_INTERLACED_ILK;
8582 else
8583 val |= PIPECONF_PROGRESSIVE;
8584
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008585 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008586 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008587
Paulo Zanonic8203562012-09-12 10:06:29 -03008588 I915_WRITE(PIPECONF(pipe), val);
8589 POSTING_READ(PIPECONF(pipe));
8590}
8591
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008592/*
8593 * Set up the pipe CSC unit.
8594 *
8595 * Currently only full range RGB to limited range RGB conversion
8596 * is supported, but eventually this should handle various
8597 * RGB<->YCbCr scenarios as well.
8598 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008599static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008600{
8601 struct drm_device *dev = crtc->dev;
8602 struct drm_i915_private *dev_priv = dev->dev_private;
8603 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8604 int pipe = intel_crtc->pipe;
8605 uint16_t coeff = 0x7800; /* 1.0 */
8606
8607 /*
8608 * TODO: Check what kind of values actually come out of the pipe
8609 * with these coeff/postoff values and adjust to get the best
8610 * accuracy. Perhaps we even need to take the bpc value into
8611 * consideration.
8612 */
8613
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008614 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008615 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8616
8617 /*
8618 * GY/GU and RY/RU should be the other way around according
8619 * to BSpec, but reality doesn't agree. Just set them up in
8620 * a way that results in the correct picture.
8621 */
8622 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8623 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8624
8625 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8626 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8627
8628 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8629 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8630
8631 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8632 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8633 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8634
8635 if (INTEL_INFO(dev)->gen > 6) {
8636 uint16_t postoff = 0;
8637
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008638 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008639 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008640
8641 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8642 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8643 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8644
8645 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8646 } else {
8647 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8648
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008649 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008650 mode |= CSC_BLACK_SCREEN_OFFSET;
8651
8652 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8653 }
8654}
8655
Daniel Vetter6ff93602013-04-19 11:24:36 +02008656static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008657{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008658 struct drm_device *dev = crtc->dev;
8659 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008661 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008662 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008663 uint32_t val;
8664
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008665 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008666
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008667 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008668 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8669
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008670 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008671 val |= PIPECONF_INTERLACED_ILK;
8672 else
8673 val |= PIPECONF_PROGRESSIVE;
8674
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008675 I915_WRITE(PIPECONF(cpu_transcoder), val);
8676 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008677
8678 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8679 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008680
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308681 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008682 val = 0;
8683
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008684 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008685 case 18:
8686 val |= PIPEMISC_DITHER_6_BPC;
8687 break;
8688 case 24:
8689 val |= PIPEMISC_DITHER_8_BPC;
8690 break;
8691 case 30:
8692 val |= PIPEMISC_DITHER_10_BPC;
8693 break;
8694 case 36:
8695 val |= PIPEMISC_DITHER_12_BPC;
8696 break;
8697 default:
8698 /* Case prevented by pipe_config_set_bpp. */
8699 BUG();
8700 }
8701
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008702 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008703 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8704
8705 I915_WRITE(PIPEMISC(pipe), val);
8706 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008707}
8708
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008709static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008710 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008711 intel_clock_t *clock,
8712 bool *has_reduced_clock,
8713 intel_clock_t *reduced_clock)
8714{
8715 struct drm_device *dev = crtc->dev;
8716 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008717 int refclk;
8718 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008719 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008720
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008721 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008722
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008723 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008724
8725 /*
8726 * Returns a set of divisors for the desired target clock with the given
8727 * refclk, or FALSE. The returned values represent the clock equation:
8728 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8729 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008730 limit = intel_limit(crtc_state, refclk);
8731 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008732 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008733 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008734 if (!ret)
8735 return false;
8736
8737 if (is_lvds && dev_priv->lvds_downclock_avail) {
8738 /*
8739 * Ensure we match the reduced clock's P to the target clock.
8740 * If the clocks don't match, we can't switch the display clock
8741 * by using the FP0/FP1. In such case we will disable the LVDS
8742 * downclock feature.
8743 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008744 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008745 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008746 dev_priv->lvds_downclock,
8747 refclk, clock,
8748 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008749 }
8750
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008751 return true;
8752}
8753
Paulo Zanonid4b19312012-11-29 11:29:32 -02008754int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8755{
8756 /*
8757 * Account for spread spectrum to avoid
8758 * oversubscribing the link. Max center spread
8759 * is 2.5%; use 5% for safety's sake.
8760 */
8761 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008762 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008763}
8764
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008765static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008766{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008767 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008768}
8769
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008770static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008771 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008772 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008773 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008774{
8775 struct drm_crtc *crtc = &intel_crtc->base;
8776 struct drm_device *dev = crtc->dev;
8777 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008778 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008779 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008780 struct drm_connector_state *connector_state;
8781 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008782 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008783 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008784 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008785
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008786 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008787 if (connector_state->crtc != crtc_state->base.crtc)
8788 continue;
8789
8790 encoder = to_intel_encoder(connector_state->best_encoder);
8791
8792 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008793 case INTEL_OUTPUT_LVDS:
8794 is_lvds = true;
8795 break;
8796 case INTEL_OUTPUT_SDVO:
8797 case INTEL_OUTPUT_HDMI:
8798 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008799 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008800 default:
8801 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008802 }
8803
8804 num_connectors++;
8805 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008806
Chris Wilsonc1858122010-12-03 21:35:48 +00008807 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008808 factor = 21;
8809 if (is_lvds) {
8810 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008811 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008812 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008813 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008814 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008815 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008816
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008817 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008818 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008819
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008820 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8821 *fp2 |= FP_CB_TUNE;
8822
Chris Wilson5eddb702010-09-11 13:48:45 +01008823 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008824
Eric Anholta07d6782011-03-30 13:01:08 -07008825 if (is_lvds)
8826 dpll |= DPLLB_MODE_LVDS;
8827 else
8828 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008829
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008830 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008831 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008832
8833 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008834 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008835 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008836 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008837
Eric Anholta07d6782011-03-30 13:01:08 -07008838 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008839 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008840 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008841 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008842
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008843 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008844 case 5:
8845 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8846 break;
8847 case 7:
8848 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8849 break;
8850 case 10:
8851 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8852 break;
8853 case 14:
8854 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8855 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008856 }
8857
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008858 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008859 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008860 else
8861 dpll |= PLL_REF_INPUT_DREFCLK;
8862
Daniel Vetter959e16d2013-06-05 13:34:21 +02008863 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008864}
8865
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008866static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8867 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008868{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008869 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008870 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008871 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008872 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008873 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008874 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008875
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008876 memset(&crtc_state->dpll_hw_state, 0,
8877 sizeof(crtc_state->dpll_hw_state));
8878
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008879 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008880
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008881 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8882 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8883
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008884 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008885 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008886 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008887 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8888 return -EINVAL;
8889 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008890 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008891 if (!crtc_state->clock_set) {
8892 crtc_state->dpll.n = clock.n;
8893 crtc_state->dpll.m1 = clock.m1;
8894 crtc_state->dpll.m2 = clock.m2;
8895 crtc_state->dpll.p1 = clock.p1;
8896 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008897 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008898
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008899 /* 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 +02008900 if (crtc_state->has_pch_encoder) {
8901 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008902 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008903 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008904
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008905 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008906 &fp, &reduced_clock,
8907 has_reduced_clock ? &fp2 : NULL);
8908
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008909 crtc_state->dpll_hw_state.dpll = dpll;
8910 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008911 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008912 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008913 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008914 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008915
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008916 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008917 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008918 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008919 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008920 return -EINVAL;
8921 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008922 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008923
Rodrigo Viviab585de2015-03-24 12:40:09 -07008924 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008925 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008926 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008927 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008928
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008929 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008930}
8931
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008932static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8933 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008934{
8935 struct drm_device *dev = crtc->base.dev;
8936 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008937 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008938
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008939 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8940 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8941 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8942 & ~TU_SIZE_MASK;
8943 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8944 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8945 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8946}
8947
8948static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8949 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008950 struct intel_link_m_n *m_n,
8951 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008952{
8953 struct drm_device *dev = crtc->base.dev;
8954 struct drm_i915_private *dev_priv = dev->dev_private;
8955 enum pipe pipe = crtc->pipe;
8956
8957 if (INTEL_INFO(dev)->gen >= 5) {
8958 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8959 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8960 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8961 & ~TU_SIZE_MASK;
8962 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8963 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8964 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008965 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8966 * gen < 8) and if DRRS is supported (to make sure the
8967 * registers are not unnecessarily read).
8968 */
8969 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008970 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008971 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8972 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8973 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8974 & ~TU_SIZE_MASK;
8975 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8976 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8977 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8978 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008979 } else {
8980 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8981 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8982 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8983 & ~TU_SIZE_MASK;
8984 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8985 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8986 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8987 }
8988}
8989
8990void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008991 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008992{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008993 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008994 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8995 else
8996 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008997 &pipe_config->dp_m_n,
8998 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008999}
9000
Daniel Vetter72419202013-04-04 13:28:53 +02009001static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009002 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009003{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009004 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009005 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009006}
9007
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009008static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009009 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009010{
9011 struct drm_device *dev = crtc->base.dev;
9012 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009013 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9014 uint32_t ps_ctrl = 0;
9015 int id = -1;
9016 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009017
Chandra Kondurua1b22782015-04-07 15:28:45 -07009018 /* find scaler attached to this pipe */
9019 for (i = 0; i < crtc->num_scalers; i++) {
9020 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9021 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9022 id = i;
9023 pipe_config->pch_pfit.enabled = true;
9024 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9025 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9026 break;
9027 }
9028 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009029
Chandra Kondurua1b22782015-04-07 15:28:45 -07009030 scaler_state->scaler_id = id;
9031 if (id >= 0) {
9032 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9033 } else {
9034 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009035 }
9036}
9037
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009038static void
9039skylake_get_initial_plane_config(struct intel_crtc *crtc,
9040 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009041{
9042 struct drm_device *dev = crtc->base.dev;
9043 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009044 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009045 int pipe = crtc->pipe;
9046 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009047 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009048 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009049 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009050
Damien Lespiaud9806c92015-01-21 14:07:19 +00009051 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009052 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009053 DRM_DEBUG_KMS("failed to alloc fb\n");
9054 return;
9055 }
9056
Damien Lespiau1b842c82015-01-21 13:50:54 +00009057 fb = &intel_fb->base;
9058
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009059 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009060 if (!(val & PLANE_CTL_ENABLE))
9061 goto error;
9062
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009063 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9064 fourcc = skl_format_to_fourcc(pixel_format,
9065 val & PLANE_CTL_ORDER_RGBX,
9066 val & PLANE_CTL_ALPHA_MASK);
9067 fb->pixel_format = fourcc;
9068 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9069
Damien Lespiau40f46282015-02-27 11:15:21 +00009070 tiling = val & PLANE_CTL_TILED_MASK;
9071 switch (tiling) {
9072 case PLANE_CTL_TILED_LINEAR:
9073 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9074 break;
9075 case PLANE_CTL_TILED_X:
9076 plane_config->tiling = I915_TILING_X;
9077 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9078 break;
9079 case PLANE_CTL_TILED_Y:
9080 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9081 break;
9082 case PLANE_CTL_TILED_YF:
9083 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9084 break;
9085 default:
9086 MISSING_CASE(tiling);
9087 goto error;
9088 }
9089
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009090 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9091 plane_config->base = base;
9092
9093 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9094
9095 val = I915_READ(PLANE_SIZE(pipe, 0));
9096 fb->height = ((val >> 16) & 0xfff) + 1;
9097 fb->width = ((val >> 0) & 0x1fff) + 1;
9098
9099 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009100 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9101 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009102 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9103
9104 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009105 fb->pixel_format,
9106 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009107
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009108 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009109
9110 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9111 pipe_name(pipe), fb->width, fb->height,
9112 fb->bits_per_pixel, base, fb->pitches[0],
9113 plane_config->size);
9114
Damien Lespiau2d140302015-02-05 17:22:18 +00009115 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009116 return;
9117
9118error:
9119 kfree(fb);
9120}
9121
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009122static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009123 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009124{
9125 struct drm_device *dev = crtc->base.dev;
9126 struct drm_i915_private *dev_priv = dev->dev_private;
9127 uint32_t tmp;
9128
9129 tmp = I915_READ(PF_CTL(crtc->pipe));
9130
9131 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009132 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009133 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9134 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009135
9136 /* We currently do not free assignements of panel fitters on
9137 * ivb/hsw (since we don't use the higher upscaling modes which
9138 * differentiates them) so just WARN about this case for now. */
9139 if (IS_GEN7(dev)) {
9140 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9141 PF_PIPE_SEL_IVB(crtc->pipe));
9142 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009143 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009144}
9145
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009146static void
9147ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9148 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009149{
9150 struct drm_device *dev = crtc->base.dev;
9151 struct drm_i915_private *dev_priv = dev->dev_private;
9152 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009153 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009154 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009155 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009156 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009157 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009158
Damien Lespiau42a7b082015-02-05 19:35:13 +00009159 val = I915_READ(DSPCNTR(pipe));
9160 if (!(val & DISPLAY_PLANE_ENABLE))
9161 return;
9162
Damien Lespiaud9806c92015-01-21 14:07:19 +00009163 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009164 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009165 DRM_DEBUG_KMS("failed to alloc fb\n");
9166 return;
9167 }
9168
Damien Lespiau1b842c82015-01-21 13:50:54 +00009169 fb = &intel_fb->base;
9170
Daniel Vetter18c52472015-02-10 17:16:09 +00009171 if (INTEL_INFO(dev)->gen >= 4) {
9172 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009173 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009174 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9175 }
9176 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009177
9178 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009179 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009180 fb->pixel_format = fourcc;
9181 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009182
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009183 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009184 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009185 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009186 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009187 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009188 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009189 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009190 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191 }
9192 plane_config->base = base;
9193
9194 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009195 fb->width = ((val >> 16) & 0xfff) + 1;
9196 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009197
9198 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009199 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009200
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009201 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009202 fb->pixel_format,
9203 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009204
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009205 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009206
Damien Lespiau2844a922015-01-20 12:51:48 +00009207 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9208 pipe_name(pipe), fb->width, fb->height,
9209 fb->bits_per_pixel, base, fb->pitches[0],
9210 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009211
Damien Lespiau2d140302015-02-05 17:22:18 +00009212 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009213}
9214
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009215static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009216 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009217{
9218 struct drm_device *dev = crtc->base.dev;
9219 struct drm_i915_private *dev_priv = dev->dev_private;
9220 uint32_t tmp;
9221
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009222 if (!intel_display_power_is_enabled(dev_priv,
9223 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009224 return false;
9225
Daniel Vettere143a212013-07-04 12:01:15 +02009226 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009227 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009228
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009229 tmp = I915_READ(PIPECONF(crtc->pipe));
9230 if (!(tmp & PIPECONF_ENABLE))
9231 return false;
9232
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009233 switch (tmp & PIPECONF_BPC_MASK) {
9234 case PIPECONF_6BPC:
9235 pipe_config->pipe_bpp = 18;
9236 break;
9237 case PIPECONF_8BPC:
9238 pipe_config->pipe_bpp = 24;
9239 break;
9240 case PIPECONF_10BPC:
9241 pipe_config->pipe_bpp = 30;
9242 break;
9243 case PIPECONF_12BPC:
9244 pipe_config->pipe_bpp = 36;
9245 break;
9246 default:
9247 break;
9248 }
9249
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009250 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9251 pipe_config->limited_color_range = true;
9252
Daniel Vetterab9412b2013-05-03 11:49:46 +02009253 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009254 struct intel_shared_dpll *pll;
9255
Daniel Vetter88adfff2013-03-28 10:42:01 +01009256 pipe_config->has_pch_encoder = true;
9257
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009258 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9259 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9260 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009261
9262 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009263
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009264 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009265 pipe_config->shared_dpll =
9266 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009267 } else {
9268 tmp = I915_READ(PCH_DPLL_SEL);
9269 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9270 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9271 else
9272 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9273 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009274
9275 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9276
9277 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9278 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009279
9280 tmp = pipe_config->dpll_hw_state.dpll;
9281 pipe_config->pixel_multiplier =
9282 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9283 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009284
9285 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009286 } else {
9287 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009288 }
9289
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009290 intel_get_pipe_timings(crtc, pipe_config);
9291
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009292 ironlake_get_pfit_config(crtc, pipe_config);
9293
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009294 return true;
9295}
9296
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009297static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9298{
9299 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009300 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009301
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009302 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009303 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009304 pipe_name(crtc->pipe));
9305
Rob Clarke2c719b2014-12-15 13:56:32 -05009306 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9307 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9308 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9309 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9310 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9311 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009312 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009313 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009314 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009315 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009316 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009317 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009318 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009319 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009320 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009321
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009322 /*
9323 * In theory we can still leave IRQs enabled, as long as only the HPD
9324 * interrupts remain enabled. We used to check for that, but since it's
9325 * gen-specific and since we only disable LCPLL after we fully disable
9326 * the interrupts, the check below should be enough.
9327 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009328 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009329}
9330
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009331static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9332{
9333 struct drm_device *dev = dev_priv->dev;
9334
9335 if (IS_HASWELL(dev))
9336 return I915_READ(D_COMP_HSW);
9337 else
9338 return I915_READ(D_COMP_BDW);
9339}
9340
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009341static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9342{
9343 struct drm_device *dev = dev_priv->dev;
9344
9345 if (IS_HASWELL(dev)) {
9346 mutex_lock(&dev_priv->rps.hw_lock);
9347 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9348 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009349 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009350 mutex_unlock(&dev_priv->rps.hw_lock);
9351 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009352 I915_WRITE(D_COMP_BDW, val);
9353 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009354 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009355}
9356
9357/*
9358 * This function implements pieces of two sequences from BSpec:
9359 * - Sequence for display software to disable LCPLL
9360 * - Sequence for display software to allow package C8+
9361 * The steps implemented here are just the steps that actually touch the LCPLL
9362 * register. Callers should take care of disabling all the display engine
9363 * functions, doing the mode unset, fixing interrupts, etc.
9364 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009365static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9366 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009367{
9368 uint32_t val;
9369
9370 assert_can_disable_lcpll(dev_priv);
9371
9372 val = I915_READ(LCPLL_CTL);
9373
9374 if (switch_to_fclk) {
9375 val |= LCPLL_CD_SOURCE_FCLK;
9376 I915_WRITE(LCPLL_CTL, val);
9377
9378 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9379 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9380 DRM_ERROR("Switching to FCLK failed\n");
9381
9382 val = I915_READ(LCPLL_CTL);
9383 }
9384
9385 val |= LCPLL_PLL_DISABLE;
9386 I915_WRITE(LCPLL_CTL, val);
9387 POSTING_READ(LCPLL_CTL);
9388
9389 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9390 DRM_ERROR("LCPLL still locked\n");
9391
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009392 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009393 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009394 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009395 ndelay(100);
9396
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009397 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9398 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009399 DRM_ERROR("D_COMP RCOMP still in progress\n");
9400
9401 if (allow_power_down) {
9402 val = I915_READ(LCPLL_CTL);
9403 val |= LCPLL_POWER_DOWN_ALLOW;
9404 I915_WRITE(LCPLL_CTL, val);
9405 POSTING_READ(LCPLL_CTL);
9406 }
9407}
9408
9409/*
9410 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9411 * source.
9412 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009413static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009414{
9415 uint32_t val;
9416
9417 val = I915_READ(LCPLL_CTL);
9418
9419 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9420 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9421 return;
9422
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009423 /*
9424 * Make sure we're not on PC8 state before disabling PC8, otherwise
9425 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009426 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009427 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009428
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009429 if (val & LCPLL_POWER_DOWN_ALLOW) {
9430 val &= ~LCPLL_POWER_DOWN_ALLOW;
9431 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009432 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009433 }
9434
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009435 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009436 val |= D_COMP_COMP_FORCE;
9437 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009438 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009439
9440 val = I915_READ(LCPLL_CTL);
9441 val &= ~LCPLL_PLL_DISABLE;
9442 I915_WRITE(LCPLL_CTL, val);
9443
9444 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9445 DRM_ERROR("LCPLL not locked yet\n");
9446
9447 if (val & LCPLL_CD_SOURCE_FCLK) {
9448 val = I915_READ(LCPLL_CTL);
9449 val &= ~LCPLL_CD_SOURCE_FCLK;
9450 I915_WRITE(LCPLL_CTL, val);
9451
9452 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9453 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9454 DRM_ERROR("Switching back to LCPLL failed\n");
9455 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009456
Mika Kuoppala59bad942015-01-16 11:34:40 +02009457 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009458 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009459}
9460
Paulo Zanoni765dab672014-03-07 20:08:18 -03009461/*
9462 * Package states C8 and deeper are really deep PC states that can only be
9463 * reached when all the devices on the system allow it, so even if the graphics
9464 * device allows PC8+, it doesn't mean the system will actually get to these
9465 * states. Our driver only allows PC8+ when going into runtime PM.
9466 *
9467 * The requirements for PC8+ are that all the outputs are disabled, the power
9468 * well is disabled and most interrupts are disabled, and these are also
9469 * requirements for runtime PM. When these conditions are met, we manually do
9470 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9471 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9472 * hang the machine.
9473 *
9474 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9475 * the state of some registers, so when we come back from PC8+ we need to
9476 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9477 * need to take care of the registers kept by RC6. Notice that this happens even
9478 * if we don't put the device in PCI D3 state (which is what currently happens
9479 * because of the runtime PM support).
9480 *
9481 * For more, read "Display Sequences for Package C8" on the hardware
9482 * documentation.
9483 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009484void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009485{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009486 struct drm_device *dev = dev_priv->dev;
9487 uint32_t val;
9488
Paulo Zanonic67a4702013-08-19 13:18:09 -03009489 DRM_DEBUG_KMS("Enabling package C8+\n");
9490
Paulo Zanonic67a4702013-08-19 13:18:09 -03009491 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9492 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9493 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9494 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9495 }
9496
9497 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009498 hsw_disable_lcpll(dev_priv, true, true);
9499}
9500
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009501void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009502{
9503 struct drm_device *dev = dev_priv->dev;
9504 uint32_t val;
9505
Paulo Zanonic67a4702013-08-19 13:18:09 -03009506 DRM_DEBUG_KMS("Disabling package C8+\n");
9507
9508 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009509 lpt_init_pch_refclk(dev);
9510
9511 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9512 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9513 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9514 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9515 }
9516
9517 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009518}
9519
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009520static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309521{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009522 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309523 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009524 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309525 int req_cdclk;
9526
9527 /* see the comment in valleyview_modeset_global_resources */
9528 if (WARN_ON(max_pixclk < 0))
9529 return;
9530
9531 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9532
9533 if (req_cdclk != dev_priv->cdclk_freq)
9534 broxton_set_cdclk(dev, req_cdclk);
9535}
9536
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009537/* compute the max rate for new configuration */
9538static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9539{
9540 struct drm_device *dev = dev_priv->dev;
9541 struct intel_crtc *intel_crtc;
9542 struct drm_crtc *crtc;
9543 int max_pixel_rate = 0;
9544 int pixel_rate;
9545
9546 for_each_crtc(dev, crtc) {
9547 if (!crtc->state->enable)
9548 continue;
9549
9550 intel_crtc = to_intel_crtc(crtc);
9551 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9552
9553 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9554 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9555 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9556
9557 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9558 }
9559
9560 return max_pixel_rate;
9561}
9562
9563static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9564{
9565 struct drm_i915_private *dev_priv = dev->dev_private;
9566 uint32_t val, data;
9567 int ret;
9568
9569 if (WARN((I915_READ(LCPLL_CTL) &
9570 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9571 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9572 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9573 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9574 "trying to change cdclk frequency with cdclk not enabled\n"))
9575 return;
9576
9577 mutex_lock(&dev_priv->rps.hw_lock);
9578 ret = sandybridge_pcode_write(dev_priv,
9579 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9580 mutex_unlock(&dev_priv->rps.hw_lock);
9581 if (ret) {
9582 DRM_ERROR("failed to inform pcode about cdclk change\n");
9583 return;
9584 }
9585
9586 val = I915_READ(LCPLL_CTL);
9587 val |= LCPLL_CD_SOURCE_FCLK;
9588 I915_WRITE(LCPLL_CTL, val);
9589
9590 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9591 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9592 DRM_ERROR("Switching to FCLK failed\n");
9593
9594 val = I915_READ(LCPLL_CTL);
9595 val &= ~LCPLL_CLK_FREQ_MASK;
9596
9597 switch (cdclk) {
9598 case 450000:
9599 val |= LCPLL_CLK_FREQ_450;
9600 data = 0;
9601 break;
9602 case 540000:
9603 val |= LCPLL_CLK_FREQ_54O_BDW;
9604 data = 1;
9605 break;
9606 case 337500:
9607 val |= LCPLL_CLK_FREQ_337_5_BDW;
9608 data = 2;
9609 break;
9610 case 675000:
9611 val |= LCPLL_CLK_FREQ_675_BDW;
9612 data = 3;
9613 break;
9614 default:
9615 WARN(1, "invalid cdclk frequency\n");
9616 return;
9617 }
9618
9619 I915_WRITE(LCPLL_CTL, val);
9620
9621 val = I915_READ(LCPLL_CTL);
9622 val &= ~LCPLL_CD_SOURCE_FCLK;
9623 I915_WRITE(LCPLL_CTL, val);
9624
9625 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9626 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9627 DRM_ERROR("Switching back to LCPLL failed\n");
9628
9629 mutex_lock(&dev_priv->rps.hw_lock);
9630 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9631 mutex_unlock(&dev_priv->rps.hw_lock);
9632
9633 intel_update_cdclk(dev);
9634
9635 WARN(cdclk != dev_priv->cdclk_freq,
9636 "cdclk requested %d kHz but got %d kHz\n",
9637 cdclk, dev_priv->cdclk_freq);
9638}
9639
9640static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9641 int max_pixel_rate)
9642{
9643 int cdclk;
9644
9645 /*
9646 * FIXME should also account for plane ratio
9647 * once 64bpp pixel formats are supported.
9648 */
9649 if (max_pixel_rate > 540000)
9650 cdclk = 675000;
9651 else if (max_pixel_rate > 450000)
9652 cdclk = 540000;
9653 else if (max_pixel_rate > 337500)
9654 cdclk = 450000;
9655 else
9656 cdclk = 337500;
9657
9658 /*
9659 * FIXME move the cdclk caclulation to
9660 * compute_config() so we can fail gracegully.
9661 */
9662 if (cdclk > dev_priv->max_cdclk_freq) {
9663 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9664 cdclk, dev_priv->max_cdclk_freq);
9665 cdclk = dev_priv->max_cdclk_freq;
9666 }
9667
9668 return cdclk;
9669}
9670
9671static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9672{
9673 struct drm_i915_private *dev_priv = to_i915(state->dev);
9674 struct drm_crtc *crtc;
9675 struct drm_crtc_state *crtc_state;
9676 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9677 int cdclk, i;
9678
9679 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9680
9681 if (cdclk == dev_priv->cdclk_freq)
9682 return 0;
9683
9684 /* add all active pipes to the state */
9685 for_each_crtc(state->dev, crtc) {
9686 if (!crtc->state->enable)
9687 continue;
9688
9689 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9690 if (IS_ERR(crtc_state))
9691 return PTR_ERR(crtc_state);
9692 }
9693
9694 /* disable/enable all currently active pipes while we change cdclk */
9695 for_each_crtc_in_state(state, crtc, crtc_state, i)
9696 if (crtc_state->enable)
9697 crtc_state->mode_changed = true;
9698
9699 return 0;
9700}
9701
9702static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9703{
9704 struct drm_device *dev = state->dev;
9705 struct drm_i915_private *dev_priv = dev->dev_private;
9706 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9707 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9708
9709 if (req_cdclk != dev_priv->cdclk_freq)
9710 broadwell_set_cdclk(dev, req_cdclk);
9711}
9712
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009713static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9714 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009715{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009716 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009717 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009718
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009719 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009720
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009721 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009722}
9723
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309724static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9725 enum port port,
9726 struct intel_crtc_state *pipe_config)
9727{
9728 switch (port) {
9729 case PORT_A:
9730 pipe_config->ddi_pll_sel = SKL_DPLL0;
9731 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9732 break;
9733 case PORT_B:
9734 pipe_config->ddi_pll_sel = SKL_DPLL1;
9735 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9736 break;
9737 case PORT_C:
9738 pipe_config->ddi_pll_sel = SKL_DPLL2;
9739 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9740 break;
9741 default:
9742 DRM_ERROR("Incorrect port type\n");
9743 }
9744}
9745
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009746static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9747 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009748 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009749{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009750 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009751
9752 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9753 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9754
9755 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009756 case SKL_DPLL0:
9757 /*
9758 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9759 * of the shared DPLL framework and thus needs to be read out
9760 * separately
9761 */
9762 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9763 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9764 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009765 case SKL_DPLL1:
9766 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9767 break;
9768 case SKL_DPLL2:
9769 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9770 break;
9771 case SKL_DPLL3:
9772 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9773 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009774 }
9775}
9776
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009777static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9778 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009779 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009780{
9781 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9782
9783 switch (pipe_config->ddi_pll_sel) {
9784 case PORT_CLK_SEL_WRPLL1:
9785 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9786 break;
9787 case PORT_CLK_SEL_WRPLL2:
9788 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9789 break;
9790 }
9791}
9792
Daniel Vetter26804af2014-06-25 22:01:55 +03009793static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009794 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009795{
9796 struct drm_device *dev = crtc->base.dev;
9797 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009798 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009799 enum port port;
9800 uint32_t tmp;
9801
9802 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9803
9804 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9805
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009806 if (IS_SKYLAKE(dev))
9807 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309808 else if (IS_BROXTON(dev))
9809 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009810 else
9811 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009812
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009813 if (pipe_config->shared_dpll >= 0) {
9814 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9815
9816 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9817 &pipe_config->dpll_hw_state));
9818 }
9819
Daniel Vetter26804af2014-06-25 22:01:55 +03009820 /*
9821 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9822 * DDI E. So just check whether this pipe is wired to DDI E and whether
9823 * the PCH transcoder is on.
9824 */
Damien Lespiauca370452013-12-03 13:56:24 +00009825 if (INTEL_INFO(dev)->gen < 9 &&
9826 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009827 pipe_config->has_pch_encoder = true;
9828
9829 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9830 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9831 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9832
9833 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9834 }
9835}
9836
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009837static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009838 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009839{
9840 struct drm_device *dev = crtc->base.dev;
9841 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009842 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009843 uint32_t tmp;
9844
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009845 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009846 POWER_DOMAIN_PIPE(crtc->pipe)))
9847 return false;
9848
Daniel Vettere143a212013-07-04 12:01:15 +02009849 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009850 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9851
Daniel Vettereccb1402013-05-22 00:50:22 +02009852 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9853 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9854 enum pipe trans_edp_pipe;
9855 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9856 default:
9857 WARN(1, "unknown pipe linked to edp transcoder\n");
9858 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9859 case TRANS_DDI_EDP_INPUT_A_ON:
9860 trans_edp_pipe = PIPE_A;
9861 break;
9862 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9863 trans_edp_pipe = PIPE_B;
9864 break;
9865 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9866 trans_edp_pipe = PIPE_C;
9867 break;
9868 }
9869
9870 if (trans_edp_pipe == crtc->pipe)
9871 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9872 }
9873
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009874 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009875 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009876 return false;
9877
Daniel Vettereccb1402013-05-22 00:50:22 +02009878 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009879 if (!(tmp & PIPECONF_ENABLE))
9880 return false;
9881
Daniel Vetter26804af2014-06-25 22:01:55 +03009882 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009883
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009884 intel_get_pipe_timings(crtc, pipe_config);
9885
Chandra Kondurua1b22782015-04-07 15:28:45 -07009886 if (INTEL_INFO(dev)->gen >= 9) {
9887 skl_init_scalers(dev, crtc, pipe_config);
9888 }
9889
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009890 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009891
9892 if (INTEL_INFO(dev)->gen >= 9) {
9893 pipe_config->scaler_state.scaler_id = -1;
9894 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9895 }
9896
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009897 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009898 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009899 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009900 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009901 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009902 else
9903 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009904 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009905
Jesse Barnese59150d2014-01-07 13:30:45 -08009906 if (IS_HASWELL(dev))
9907 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9908 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009909
Clint Taylorebb69c92014-09-30 10:30:22 -07009910 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9911 pipe_config->pixel_multiplier =
9912 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9913 } else {
9914 pipe_config->pixel_multiplier = 1;
9915 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009916
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009917 return true;
9918}
9919
Chris Wilson560b85b2010-08-07 11:01:38 +01009920static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9921{
9922 struct drm_device *dev = crtc->dev;
9923 struct drm_i915_private *dev_priv = dev->dev_private;
9924 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009925 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009926
Ville Syrjälädc41c152014-08-13 11:57:05 +03009927 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009928 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9929 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009930 unsigned int stride = roundup_pow_of_two(width) * 4;
9931
9932 switch (stride) {
9933 default:
9934 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9935 width, stride);
9936 stride = 256;
9937 /* fallthrough */
9938 case 256:
9939 case 512:
9940 case 1024:
9941 case 2048:
9942 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009943 }
9944
Ville Syrjälädc41c152014-08-13 11:57:05 +03009945 cntl |= CURSOR_ENABLE |
9946 CURSOR_GAMMA_ENABLE |
9947 CURSOR_FORMAT_ARGB |
9948 CURSOR_STRIDE(stride);
9949
9950 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009951 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009952
Ville Syrjälädc41c152014-08-13 11:57:05 +03009953 if (intel_crtc->cursor_cntl != 0 &&
9954 (intel_crtc->cursor_base != base ||
9955 intel_crtc->cursor_size != size ||
9956 intel_crtc->cursor_cntl != cntl)) {
9957 /* On these chipsets we can only modify the base/size/stride
9958 * whilst the cursor is disabled.
9959 */
9960 I915_WRITE(_CURACNTR, 0);
9961 POSTING_READ(_CURACNTR);
9962 intel_crtc->cursor_cntl = 0;
9963 }
9964
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009965 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009966 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009967 intel_crtc->cursor_base = base;
9968 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009969
9970 if (intel_crtc->cursor_size != size) {
9971 I915_WRITE(CURSIZE, size);
9972 intel_crtc->cursor_size = size;
9973 }
9974
Chris Wilson4b0e3332014-05-30 16:35:26 +03009975 if (intel_crtc->cursor_cntl != cntl) {
9976 I915_WRITE(_CURACNTR, cntl);
9977 POSTING_READ(_CURACNTR);
9978 intel_crtc->cursor_cntl = cntl;
9979 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009980}
9981
9982static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9983{
9984 struct drm_device *dev = crtc->dev;
9985 struct drm_i915_private *dev_priv = dev->dev_private;
9986 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9987 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009988 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009989
Chris Wilson4b0e3332014-05-30 16:35:26 +03009990 cntl = 0;
9991 if (base) {
9992 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009993 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309994 case 64:
9995 cntl |= CURSOR_MODE_64_ARGB_AX;
9996 break;
9997 case 128:
9998 cntl |= CURSOR_MODE_128_ARGB_AX;
9999 break;
10000 case 256:
10001 cntl |= CURSOR_MODE_256_ARGB_AX;
10002 break;
10003 default:
Matt Roper3dd512f2015-02-27 10:12:00 -080010004 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010005 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010006 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010007 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010008
10009 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10010 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +010010011 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010012
Matt Roper8e7d6882015-01-21 16:35:41 -080010013 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010014 cntl |= CURSOR_ROTATE_180;
10015
Chris Wilson4b0e3332014-05-30 16:35:26 +030010016 if (intel_crtc->cursor_cntl != cntl) {
10017 I915_WRITE(CURCNTR(pipe), cntl);
10018 POSTING_READ(CURCNTR(pipe));
10019 intel_crtc->cursor_cntl = cntl;
10020 }
10021
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010022 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010023 I915_WRITE(CURBASE(pipe), base);
10024 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010025
10026 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010027}
10028
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010029/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010030static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10031 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010032{
10033 struct drm_device *dev = crtc->dev;
10034 struct drm_i915_private *dev_priv = dev->dev_private;
10035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10036 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -070010037 int x = crtc->cursor_x;
10038 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010039 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010040
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010041 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010042 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010043
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010044 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010045 base = 0;
10046
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010047 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010048 base = 0;
10049
10050 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010051 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010052 base = 0;
10053
10054 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10055 x = -x;
10056 }
10057 pos |= x << CURSOR_X_SHIFT;
10058
10059 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010060 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010061 base = 0;
10062
10063 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10064 y = -y;
10065 }
10066 pos |= y << CURSOR_Y_SHIFT;
10067
Chris Wilson4b0e3332014-05-30 16:35:26 +030010068 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010069 return;
10070
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010071 I915_WRITE(CURPOS(pipe), pos);
10072
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010073 /* ILK+ do this automagically */
10074 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010075 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010076 base += (intel_crtc->base.cursor->state->crtc_h *
10077 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010078 }
10079
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010080 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010081 i845_update_cursor(crtc, base);
10082 else
10083 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010084}
10085
Ville Syrjälädc41c152014-08-13 11:57:05 +030010086static bool cursor_size_ok(struct drm_device *dev,
10087 uint32_t width, uint32_t height)
10088{
10089 if (width == 0 || height == 0)
10090 return false;
10091
10092 /*
10093 * 845g/865g are special in that they are only limited by
10094 * the width of their cursors, the height is arbitrary up to
10095 * the precision of the register. Everything else requires
10096 * square cursors, limited to a few power-of-two sizes.
10097 */
10098 if (IS_845G(dev) || IS_I865G(dev)) {
10099 if ((width & 63) != 0)
10100 return false;
10101
10102 if (width > (IS_845G(dev) ? 64 : 512))
10103 return false;
10104
10105 if (height > 1023)
10106 return false;
10107 } else {
10108 switch (width | height) {
10109 case 256:
10110 case 128:
10111 if (IS_GEN2(dev))
10112 return false;
10113 case 64:
10114 break;
10115 default:
10116 return false;
10117 }
10118 }
10119
10120 return true;
10121}
10122
Jesse Barnes79e53942008-11-07 14:24:08 -080010123static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010124 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010125{
James Simmons72034252010-08-03 01:33:19 +010010126 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010128
James Simmons72034252010-08-03 01:33:19 +010010129 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010130 intel_crtc->lut_r[i] = red[i] >> 8;
10131 intel_crtc->lut_g[i] = green[i] >> 8;
10132 intel_crtc->lut_b[i] = blue[i] >> 8;
10133 }
10134
10135 intel_crtc_load_lut(crtc);
10136}
10137
Jesse Barnes79e53942008-11-07 14:24:08 -080010138/* VESA 640x480x72Hz mode to set on the pipe */
10139static struct drm_display_mode load_detect_mode = {
10140 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10141 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10142};
10143
Daniel Vettera8bb6812014-02-10 18:00:39 +010010144struct drm_framebuffer *
10145__intel_framebuffer_create(struct drm_device *dev,
10146 struct drm_mode_fb_cmd2 *mode_cmd,
10147 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010148{
10149 struct intel_framebuffer *intel_fb;
10150 int ret;
10151
10152 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10153 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010154 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010155 return ERR_PTR(-ENOMEM);
10156 }
10157
10158 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010159 if (ret)
10160 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010161
10162 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010163err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010164 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010165 kfree(intel_fb);
10166
10167 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010168}
10169
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010170static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010171intel_framebuffer_create(struct drm_device *dev,
10172 struct drm_mode_fb_cmd2 *mode_cmd,
10173 struct drm_i915_gem_object *obj)
10174{
10175 struct drm_framebuffer *fb;
10176 int ret;
10177
10178 ret = i915_mutex_lock_interruptible(dev);
10179 if (ret)
10180 return ERR_PTR(ret);
10181 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10182 mutex_unlock(&dev->struct_mutex);
10183
10184 return fb;
10185}
10186
Chris Wilsond2dff872011-04-19 08:36:26 +010010187static u32
10188intel_framebuffer_pitch_for_width(int width, int bpp)
10189{
10190 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10191 return ALIGN(pitch, 64);
10192}
10193
10194static u32
10195intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10196{
10197 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010198 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010199}
10200
10201static struct drm_framebuffer *
10202intel_framebuffer_create_for_mode(struct drm_device *dev,
10203 struct drm_display_mode *mode,
10204 int depth, int bpp)
10205{
10206 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010207 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010208
10209 obj = i915_gem_alloc_object(dev,
10210 intel_framebuffer_size_for_mode(mode, bpp));
10211 if (obj == NULL)
10212 return ERR_PTR(-ENOMEM);
10213
10214 mode_cmd.width = mode->hdisplay;
10215 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010216 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10217 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010218 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010219
10220 return intel_framebuffer_create(dev, &mode_cmd, obj);
10221}
10222
10223static struct drm_framebuffer *
10224mode_fits_in_fbdev(struct drm_device *dev,
10225 struct drm_display_mode *mode)
10226{
Daniel Vetter4520f532013-10-09 09:18:51 +020010227#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010228 struct drm_i915_private *dev_priv = dev->dev_private;
10229 struct drm_i915_gem_object *obj;
10230 struct drm_framebuffer *fb;
10231
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010232 if (!dev_priv->fbdev)
10233 return NULL;
10234
10235 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010236 return NULL;
10237
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010238 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010239 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010240
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010241 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010242 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10243 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010244 return NULL;
10245
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010246 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010247 return NULL;
10248
10249 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010250#else
10251 return NULL;
10252#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010253}
10254
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010255static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10256 struct drm_crtc *crtc,
10257 struct drm_display_mode *mode,
10258 struct drm_framebuffer *fb,
10259 int x, int y)
10260{
10261 struct drm_plane_state *plane_state;
10262 int hdisplay, vdisplay;
10263 int ret;
10264
10265 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10266 if (IS_ERR(plane_state))
10267 return PTR_ERR(plane_state);
10268
10269 if (mode)
10270 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10271 else
10272 hdisplay = vdisplay = 0;
10273
10274 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10275 if (ret)
10276 return ret;
10277 drm_atomic_set_fb_for_plane(plane_state, fb);
10278 plane_state->crtc_x = 0;
10279 plane_state->crtc_y = 0;
10280 plane_state->crtc_w = hdisplay;
10281 plane_state->crtc_h = vdisplay;
10282 plane_state->src_x = x << 16;
10283 plane_state->src_y = y << 16;
10284 plane_state->src_w = hdisplay << 16;
10285 plane_state->src_h = vdisplay << 16;
10286
10287 return 0;
10288}
10289
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010290bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010291 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010292 struct intel_load_detect_pipe *old,
10293 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010294{
10295 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010296 struct intel_encoder *intel_encoder =
10297 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010298 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010299 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010300 struct drm_crtc *crtc = NULL;
10301 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010302 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010303 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010304 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010305 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010306 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010307 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010308
Chris Wilsond2dff872011-04-19 08:36:26 +010010309 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010310 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010311 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010312
Rob Clark51fd3712013-11-19 12:10:12 -050010313retry:
10314 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10315 if (ret)
10316 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010317
Jesse Barnes79e53942008-11-07 14:24:08 -080010318 /*
10319 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010320 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010321 * - if the connector already has an assigned crtc, use it (but make
10322 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010323 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010324 * - try to find the first unused crtc that can drive this connector,
10325 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010326 */
10327
10328 /* See if we already have a CRTC for this connector */
10329 if (encoder->crtc) {
10330 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010331
Rob Clark51fd3712013-11-19 12:10:12 -050010332 ret = drm_modeset_lock(&crtc->mutex, ctx);
10333 if (ret)
10334 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010335 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10336 if (ret)
10337 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010338
Daniel Vetter24218aa2012-08-12 19:27:11 +020010339 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010340 old->load_detect_temp = false;
10341
10342 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010343 if (connector->dpms != DRM_MODE_DPMS_ON)
10344 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010345
Chris Wilson71731882011-04-19 23:10:58 +010010346 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010347 }
10348
10349 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010350 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010351 i++;
10352 if (!(encoder->possible_crtcs & (1 << i)))
10353 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010354 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010355 continue;
10356 /* This can occur when applying the pipe A quirk on resume. */
10357 if (to_intel_crtc(possible_crtc)->new_enabled)
10358 continue;
10359
10360 crtc = possible_crtc;
10361 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010362 }
10363
10364 /*
10365 * If we didn't find an unused CRTC, don't use any.
10366 */
10367 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010368 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010369 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010370 }
10371
Rob Clark51fd3712013-11-19 12:10:12 -050010372 ret = drm_modeset_lock(&crtc->mutex, ctx);
10373 if (ret)
10374 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010375 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10376 if (ret)
10377 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010378 intel_encoder->new_crtc = to_intel_crtc(crtc);
10379 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010380
10381 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010382 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010383 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010384 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010385 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010386
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010387 state = drm_atomic_state_alloc(dev);
10388 if (!state)
10389 return false;
10390
10391 state->acquire_ctx = ctx;
10392
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010393 connector_state = drm_atomic_get_connector_state(state, connector);
10394 if (IS_ERR(connector_state)) {
10395 ret = PTR_ERR(connector_state);
10396 goto fail;
10397 }
10398
10399 connector_state->crtc = crtc;
10400 connector_state->best_encoder = &intel_encoder->base;
10401
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010402 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10403 if (IS_ERR(crtc_state)) {
10404 ret = PTR_ERR(crtc_state);
10405 goto fail;
10406 }
10407
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010408 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010409
Chris Wilson64927112011-04-20 07:25:26 +010010410 if (!mode)
10411 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010412
Chris Wilsond2dff872011-04-19 08:36:26 +010010413 /* We need a framebuffer large enough to accommodate all accesses
10414 * that the plane may generate whilst we perform load detection.
10415 * We can not rely on the fbcon either being present (we get called
10416 * during its initialisation to detect all boot displays, or it may
10417 * not even exist) or that it is large enough to satisfy the
10418 * requested mode.
10419 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010420 fb = mode_fits_in_fbdev(dev, mode);
10421 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010422 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010423 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10424 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010425 } else
10426 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010427 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010428 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010429 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010430 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010431
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010432 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10433 if (ret)
10434 goto fail;
10435
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010436 drm_mode_copy(&crtc_state->base.mode, mode);
10437
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010438 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010439 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010440 if (old->release_fb)
10441 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010442 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010443 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010444 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010445
Jesse Barnes79e53942008-11-07 14:24:08 -080010446 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010447 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010448 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010449
10450 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010451 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010452fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010453 drm_atomic_state_free(state);
10454 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010455
Rob Clark51fd3712013-11-19 12:10:12 -050010456 if (ret == -EDEADLK) {
10457 drm_modeset_backoff(ctx);
10458 goto retry;
10459 }
10460
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010461 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010462}
10463
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010464void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010465 struct intel_load_detect_pipe *old,
10466 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010467{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010468 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010469 struct intel_encoder *intel_encoder =
10470 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010471 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010472 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010474 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010475 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010476 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010477 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010478
Chris Wilsond2dff872011-04-19 08:36:26 +010010479 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010480 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010481 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010482
Chris Wilson8261b192011-04-19 23:18:09 +010010483 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010484 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010485 if (!state)
10486 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010487
10488 state->acquire_ctx = ctx;
10489
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010490 connector_state = drm_atomic_get_connector_state(state, connector);
10491 if (IS_ERR(connector_state))
10492 goto fail;
10493
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010494 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10495 if (IS_ERR(crtc_state))
10496 goto fail;
10497
Daniel Vetterfc303102012-07-09 10:40:58 +020010498 to_intel_connector(connector)->new_encoder = NULL;
10499 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010500 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010501
10502 connector_state->best_encoder = NULL;
10503 connector_state->crtc = NULL;
10504
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010505 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010506
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010507 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10508 0, 0);
10509 if (ret)
10510 goto fail;
10511
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010512 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010513 if (ret)
10514 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010515
Daniel Vetter36206362012-12-10 20:42:17 +010010516 if (old->release_fb) {
10517 drm_framebuffer_unregister_private(old->release_fb);
10518 drm_framebuffer_unreference(old->release_fb);
10519 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010520
Chris Wilson0622a532011-04-21 09:32:11 +010010521 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010522 }
10523
Eric Anholtc751ce42010-03-25 11:48:48 -070010524 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010525 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10526 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010527
10528 return;
10529fail:
10530 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10531 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010532}
10533
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010534static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010535 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010536{
10537 struct drm_i915_private *dev_priv = dev->dev_private;
10538 u32 dpll = pipe_config->dpll_hw_state.dpll;
10539
10540 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010541 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010542 else if (HAS_PCH_SPLIT(dev))
10543 return 120000;
10544 else if (!IS_GEN2(dev))
10545 return 96000;
10546 else
10547 return 48000;
10548}
10549
Jesse Barnes79e53942008-11-07 14:24:08 -080010550/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010551static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010552 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010553{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010554 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010555 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010556 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010557 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010558 u32 fp;
10559 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010560 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010561
10562 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010563 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010564 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010565 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010566
10567 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010568 if (IS_PINEVIEW(dev)) {
10569 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10570 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010571 } else {
10572 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10573 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10574 }
10575
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010576 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010577 if (IS_PINEVIEW(dev))
10578 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10579 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010580 else
10581 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010582 DPLL_FPA01_P1_POST_DIV_SHIFT);
10583
10584 switch (dpll & DPLL_MODE_MASK) {
10585 case DPLLB_MODE_DAC_SERIAL:
10586 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10587 5 : 10;
10588 break;
10589 case DPLLB_MODE_LVDS:
10590 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10591 7 : 14;
10592 break;
10593 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010594 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010595 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010596 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010597 }
10598
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010599 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010600 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010601 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010602 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010603 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010604 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010605 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010606
10607 if (is_lvds) {
10608 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10609 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010610
10611 if (lvds & LVDS_CLKB_POWER_UP)
10612 clock.p2 = 7;
10613 else
10614 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010615 } else {
10616 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10617 clock.p1 = 2;
10618 else {
10619 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10620 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10621 }
10622 if (dpll & PLL_P2_DIVIDE_BY_4)
10623 clock.p2 = 4;
10624 else
10625 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010626 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010627
10628 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010629 }
10630
Ville Syrjälä18442d02013-09-13 16:00:08 +030010631 /*
10632 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010633 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010634 * encoder's get_config() function.
10635 */
10636 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010637}
10638
Ville Syrjälä6878da02013-09-13 15:59:11 +030010639int intel_dotclock_calculate(int link_freq,
10640 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010641{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010642 /*
10643 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010644 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010645 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010646 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010647 *
10648 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010649 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010650 */
10651
Ville Syrjälä6878da02013-09-13 15:59:11 +030010652 if (!m_n->link_n)
10653 return 0;
10654
10655 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10656}
10657
Ville Syrjälä18442d02013-09-13 16:00:08 +030010658static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010659 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010660{
10661 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010662
10663 /* read out port_clock from the DPLL */
10664 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010665
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010666 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010667 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010668 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010669 * agree once we know their relationship in the encoder's
10670 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010671 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010672 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010673 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10674 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010675}
10676
10677/** Returns the currently programmed mode of the given pipe. */
10678struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10679 struct drm_crtc *crtc)
10680{
Jesse Barnes548f2452011-02-17 10:40:53 -080010681 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010682 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010683 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010684 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010685 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010686 int htot = I915_READ(HTOTAL(cpu_transcoder));
10687 int hsync = I915_READ(HSYNC(cpu_transcoder));
10688 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10689 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010690 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010691
10692 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10693 if (!mode)
10694 return NULL;
10695
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010696 /*
10697 * Construct a pipe_config sufficient for getting the clock info
10698 * back out of crtc_clock_get.
10699 *
10700 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10701 * to use a real value here instead.
10702 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010703 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010704 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010705 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10706 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10707 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010708 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10709
Ville Syrjälä773ae032013-09-23 17:48:20 +030010710 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010711 mode->hdisplay = (htot & 0xffff) + 1;
10712 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10713 mode->hsync_start = (hsync & 0xffff) + 1;
10714 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10715 mode->vdisplay = (vtot & 0xffff) + 1;
10716 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10717 mode->vsync_start = (vsync & 0xffff) + 1;
10718 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10719
10720 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010721
10722 return mode;
10723}
10724
Jesse Barnes652c3932009-08-17 13:31:43 -070010725static void intel_decrease_pllclock(struct drm_crtc *crtc)
10726{
10727 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010728 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010730
Sonika Jindalbaff2962014-07-22 11:16:35 +053010731 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010732 return;
10733
10734 if (!dev_priv->lvds_downclock_avail)
10735 return;
10736
10737 /*
10738 * Since this is called by a timer, we should never get here in
10739 * the manual case.
10740 */
10741 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010742 int pipe = intel_crtc->pipe;
10743 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010744 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010745
Zhao Yakui44d98a62009-10-09 11:39:40 +080010746 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010747
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010748 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010749
Chris Wilson074b5e12012-05-02 12:07:06 +010010750 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010751 dpll |= DISPLAY_RATE_SELECT_FPA1;
10752 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010753 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010754 dpll = I915_READ(dpll_reg);
10755 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010756 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010757 }
10758
10759}
10760
Chris Wilsonf047e392012-07-21 12:31:41 +010010761void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010762{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010763 struct drm_i915_private *dev_priv = dev->dev_private;
10764
Chris Wilsonf62a0072014-02-21 17:55:39 +000010765 if (dev_priv->mm.busy)
10766 return;
10767
Paulo Zanoni43694d62014-03-07 20:08:08 -030010768 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010769 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010770 if (INTEL_INFO(dev)->gen >= 6)
10771 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010772 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010773}
10774
10775void intel_mark_idle(struct drm_device *dev)
10776{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010777 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010778 struct drm_crtc *crtc;
10779
Chris Wilsonf62a0072014-02-21 17:55:39 +000010780 if (!dev_priv->mm.busy)
10781 return;
10782
10783 dev_priv->mm.busy = false;
10784
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010785 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010786 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010787 continue;
10788
10789 intel_decrease_pllclock(crtc);
10790 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010791
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010792 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010793 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010794
Paulo Zanoni43694d62014-03-07 20:08:08 -030010795 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010796}
10797
Jesse Barnes79e53942008-11-07 14:24:08 -080010798static void intel_crtc_destroy(struct drm_crtc *crtc)
10799{
10800 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010801 struct drm_device *dev = crtc->dev;
10802 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010803
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010804 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010805 work = intel_crtc->unpin_work;
10806 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010807 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010808
10809 if (work) {
10810 cancel_work_sync(&work->work);
10811 kfree(work);
10812 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010813
10814 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010815
Jesse Barnes79e53942008-11-07 14:24:08 -080010816 kfree(intel_crtc);
10817}
10818
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010819static void intel_unpin_work_fn(struct work_struct *__work)
10820{
10821 struct intel_unpin_work *work =
10822 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010823 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010824 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010825
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010826 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010827 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010828 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010829
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010830 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010831
10832 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010833 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010834 mutex_unlock(&dev->struct_mutex);
10835
Daniel Vetterf99d7062014-06-19 16:01:59 +020010836 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010837 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010838
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010839 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10840 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10841
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010842 kfree(work);
10843}
10844
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010845static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010846 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010847{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10849 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010850 unsigned long flags;
10851
10852 /* Ignore early vblank irqs */
10853 if (intel_crtc == NULL)
10854 return;
10855
Daniel Vetterf3260382014-09-15 14:55:23 +020010856 /*
10857 * This is called both by irq handlers and the reset code (to complete
10858 * lost pageflips) so needs the full irqsave spinlocks.
10859 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010860 spin_lock_irqsave(&dev->event_lock, flags);
10861 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010862
10863 /* Ensure we don't miss a work->pending update ... */
10864 smp_rmb();
10865
10866 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010867 spin_unlock_irqrestore(&dev->event_lock, flags);
10868 return;
10869 }
10870
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010871 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010872
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010873 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010874}
10875
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010876void intel_finish_page_flip(struct drm_device *dev, int pipe)
10877{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010878 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010879 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10880
Mario Kleiner49b14a52010-12-09 07:00:07 +010010881 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010882}
10883
10884void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10885{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010886 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010887 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10888
Mario Kleiner49b14a52010-12-09 07:00:07 +010010889 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010890}
10891
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010892/* Is 'a' after or equal to 'b'? */
10893static bool g4x_flip_count_after_eq(u32 a, u32 b)
10894{
10895 return !((a - b) & 0x80000000);
10896}
10897
10898static bool page_flip_finished(struct intel_crtc *crtc)
10899{
10900 struct drm_device *dev = crtc->base.dev;
10901 struct drm_i915_private *dev_priv = dev->dev_private;
10902
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010903 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10904 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10905 return true;
10906
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010907 /*
10908 * The relevant registers doen't exist on pre-ctg.
10909 * As the flip done interrupt doesn't trigger for mmio
10910 * flips on gmch platforms, a flip count check isn't
10911 * really needed there. But since ctg has the registers,
10912 * include it in the check anyway.
10913 */
10914 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10915 return true;
10916
10917 /*
10918 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10919 * used the same base address. In that case the mmio flip might
10920 * have completed, but the CS hasn't even executed the flip yet.
10921 *
10922 * A flip count check isn't enough as the CS might have updated
10923 * the base address just after start of vblank, but before we
10924 * managed to process the interrupt. This means we'd complete the
10925 * CS flip too soon.
10926 *
10927 * Combining both checks should get us a good enough result. It may
10928 * still happen that the CS flip has been executed, but has not
10929 * yet actually completed. But in case the base address is the same
10930 * anyway, we don't really care.
10931 */
10932 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10933 crtc->unpin_work->gtt_offset &&
10934 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10935 crtc->unpin_work->flip_count);
10936}
10937
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010938void intel_prepare_page_flip(struct drm_device *dev, int plane)
10939{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010940 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010941 struct intel_crtc *intel_crtc =
10942 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10943 unsigned long flags;
10944
Daniel Vetterf3260382014-09-15 14:55:23 +020010945
10946 /*
10947 * This is called both by irq handlers and the reset code (to complete
10948 * lost pageflips) so needs the full irqsave spinlocks.
10949 *
10950 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010951 * generate a page-flip completion irq, i.e. every modeset
10952 * is also accompanied by a spurious intel_prepare_page_flip().
10953 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010954 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010955 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010956 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010957 spin_unlock_irqrestore(&dev->event_lock, flags);
10958}
10959
Robin Schroereba905b2014-05-18 02:24:50 +020010960static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010961{
10962 /* Ensure that the work item is consistent when activating it ... */
10963 smp_wmb();
10964 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10965 /* and that it is marked active as soon as the irq could fire. */
10966 smp_wmb();
10967}
10968
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010969static int intel_gen2_queue_flip(struct drm_device *dev,
10970 struct drm_crtc *crtc,
10971 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010972 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010973 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010974 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010975{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010977 u32 flip_mask;
10978 int ret;
10979
Daniel Vetter6d90c952012-04-26 23:28:05 +020010980 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010981 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010982 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010983
10984 /* Can't queue multiple flips, so wait for the previous
10985 * one to finish before executing the next.
10986 */
10987 if (intel_crtc->plane)
10988 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10989 else
10990 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010991 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10992 intel_ring_emit(ring, MI_NOOP);
10993 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10994 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10995 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010996 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010997 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010998
10999 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011000 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011001 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011002}
11003
11004static int intel_gen3_queue_flip(struct drm_device *dev,
11005 struct drm_crtc *crtc,
11006 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011007 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011008 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011009 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011010{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011012 u32 flip_mask;
11013 int ret;
11014
Daniel Vetter6d90c952012-04-26 23:28:05 +020011015 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011016 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011017 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011018
11019 if (intel_crtc->plane)
11020 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11021 else
11022 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011023 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11024 intel_ring_emit(ring, MI_NOOP);
11025 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11026 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11027 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011028 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011029 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011030
Chris Wilsone7d841c2012-12-03 11:36:30 +000011031 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011032 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011033 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011034}
11035
11036static int intel_gen4_queue_flip(struct drm_device *dev,
11037 struct drm_crtc *crtc,
11038 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011039 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011040 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011041 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011042{
11043 struct drm_i915_private *dev_priv = dev->dev_private;
11044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11045 uint32_t pf, pipesrc;
11046 int ret;
11047
Daniel Vetter6d90c952012-04-26 23:28:05 +020011048 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011049 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011050 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011051
11052 /* i965+ uses the linear or tiled offsets from the
11053 * Display Registers (which do not change across a page-flip)
11054 * so we need only reprogram the base address.
11055 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011056 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11057 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11058 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011059 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011060 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011061
11062 /* XXX Enabling the panel-fitter across page-flip is so far
11063 * untested on non-native modes, so ignore it for now.
11064 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11065 */
11066 pf = 0;
11067 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011068 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011069
11070 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011071 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011072 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011073}
11074
11075static int intel_gen6_queue_flip(struct drm_device *dev,
11076 struct drm_crtc *crtc,
11077 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011078 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011079 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011080 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011081{
11082 struct drm_i915_private *dev_priv = dev->dev_private;
11083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11084 uint32_t pf, pipesrc;
11085 int ret;
11086
Daniel Vetter6d90c952012-04-26 23:28:05 +020011087 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011088 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011089 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011090
Daniel Vetter6d90c952012-04-26 23:28:05 +020011091 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11092 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11093 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011094 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011095
Chris Wilson99d9acd2012-04-17 20:37:00 +010011096 /* Contrary to the suggestions in the documentation,
11097 * "Enable Panel Fitter" does not seem to be required when page
11098 * flipping with a non-native mode, and worse causes a normal
11099 * modeset to fail.
11100 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11101 */
11102 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011103 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011104 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011105
11106 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011107 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011108 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011109}
11110
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011111static int intel_gen7_queue_flip(struct drm_device *dev,
11112 struct drm_crtc *crtc,
11113 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011114 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011115 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011116 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011117{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011118 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011119 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011120 int len, ret;
11121
Robin Schroereba905b2014-05-18 02:24:50 +020011122 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011123 case PLANE_A:
11124 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11125 break;
11126 case PLANE_B:
11127 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11128 break;
11129 case PLANE_C:
11130 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11131 break;
11132 default:
11133 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011134 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011135 }
11136
Chris Wilsonffe74d72013-08-26 20:58:12 +010011137 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011138 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011139 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011140 /*
11141 * On Gen 8, SRM is now taking an extra dword to accommodate
11142 * 48bits addresses, and we need a NOOP for the batch size to
11143 * stay even.
11144 */
11145 if (IS_GEN8(dev))
11146 len += 2;
11147 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011148
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011149 /*
11150 * BSpec MI_DISPLAY_FLIP for IVB:
11151 * "The full packet must be contained within the same cache line."
11152 *
11153 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11154 * cacheline, if we ever start emitting more commands before
11155 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11156 * then do the cacheline alignment, and finally emit the
11157 * MI_DISPLAY_FLIP.
11158 */
11159 ret = intel_ring_cacheline_align(ring);
11160 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011161 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011162
Chris Wilsonffe74d72013-08-26 20:58:12 +010011163 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011164 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011165 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011166
Chris Wilsonffe74d72013-08-26 20:58:12 +010011167 /* Unmask the flip-done completion message. Note that the bspec says that
11168 * we should do this for both the BCS and RCS, and that we must not unmask
11169 * more than one flip event at any time (or ensure that one flip message
11170 * can be sent by waiting for flip-done prior to queueing new flips).
11171 * Experimentation says that BCS works despite DERRMR masking all
11172 * flip-done completion events and that unmasking all planes at once
11173 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11174 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11175 */
11176 if (ring->id == RCS) {
11177 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11178 intel_ring_emit(ring, DERRMR);
11179 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11180 DERRMR_PIPEB_PRI_FLIP_DONE |
11181 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011182 if (IS_GEN8(dev))
11183 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11184 MI_SRM_LRM_GLOBAL_GTT);
11185 else
11186 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11187 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011188 intel_ring_emit(ring, DERRMR);
11189 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011190 if (IS_GEN8(dev)) {
11191 intel_ring_emit(ring, 0);
11192 intel_ring_emit(ring, MI_NOOP);
11193 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011194 }
11195
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011196 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011197 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011198 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011199 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011200
11201 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011202 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011203 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011204}
11205
Sourab Gupta84c33a62014-06-02 16:47:17 +053011206static bool use_mmio_flip(struct intel_engine_cs *ring,
11207 struct drm_i915_gem_object *obj)
11208{
11209 /*
11210 * This is not being used for older platforms, because
11211 * non-availability of flip done interrupt forces us to use
11212 * CS flips. Older platforms derive flip done using some clever
11213 * tricks involving the flip_pending status bits and vblank irqs.
11214 * So using MMIO flips there would disrupt this mechanism.
11215 */
11216
Chris Wilson8e09bf82014-07-08 10:40:30 +010011217 if (ring == NULL)
11218 return true;
11219
Sourab Gupta84c33a62014-06-02 16:47:17 +053011220 if (INTEL_INFO(ring->dev)->gen < 5)
11221 return false;
11222
11223 if (i915.use_mmio_flip < 0)
11224 return false;
11225 else if (i915.use_mmio_flip > 0)
11226 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011227 else if (i915.enable_execlists)
11228 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011229 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011230 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011231}
11232
Damien Lespiauff944562014-11-20 14:58:16 +000011233static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11234{
11235 struct drm_device *dev = intel_crtc->base.dev;
11236 struct drm_i915_private *dev_priv = dev->dev_private;
11237 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011238 const enum pipe pipe = intel_crtc->pipe;
11239 u32 ctl, stride;
11240
11241 ctl = I915_READ(PLANE_CTL(pipe, 0));
11242 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011243 switch (fb->modifier[0]) {
11244 case DRM_FORMAT_MOD_NONE:
11245 break;
11246 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011247 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011248 break;
11249 case I915_FORMAT_MOD_Y_TILED:
11250 ctl |= PLANE_CTL_TILED_Y;
11251 break;
11252 case I915_FORMAT_MOD_Yf_TILED:
11253 ctl |= PLANE_CTL_TILED_YF;
11254 break;
11255 default:
11256 MISSING_CASE(fb->modifier[0]);
11257 }
Damien Lespiauff944562014-11-20 14:58:16 +000011258
11259 /*
11260 * The stride is either expressed as a multiple of 64 bytes chunks for
11261 * linear buffers or in number of tiles for tiled buffers.
11262 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011263 stride = fb->pitches[0] /
11264 intel_fb_stride_alignment(dev, fb->modifier[0],
11265 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011266
11267 /*
11268 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11269 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11270 */
11271 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11272 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11273
11274 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11275 POSTING_READ(PLANE_SURF(pipe, 0));
11276}
11277
11278static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011279{
11280 struct drm_device *dev = intel_crtc->base.dev;
11281 struct drm_i915_private *dev_priv = dev->dev_private;
11282 struct intel_framebuffer *intel_fb =
11283 to_intel_framebuffer(intel_crtc->base.primary->fb);
11284 struct drm_i915_gem_object *obj = intel_fb->obj;
11285 u32 dspcntr;
11286 u32 reg;
11287
Sourab Gupta84c33a62014-06-02 16:47:17 +053011288 reg = DSPCNTR(intel_crtc->plane);
11289 dspcntr = I915_READ(reg);
11290
Damien Lespiauc5d97472014-10-25 00:11:11 +010011291 if (obj->tiling_mode != I915_TILING_NONE)
11292 dspcntr |= DISPPLANE_TILED;
11293 else
11294 dspcntr &= ~DISPPLANE_TILED;
11295
Sourab Gupta84c33a62014-06-02 16:47:17 +053011296 I915_WRITE(reg, dspcntr);
11297
11298 I915_WRITE(DSPSURF(intel_crtc->plane),
11299 intel_crtc->unpin_work->gtt_offset);
11300 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011301
Damien Lespiauff944562014-11-20 14:58:16 +000011302}
11303
11304/*
11305 * XXX: This is the temporary way to update the plane registers until we get
11306 * around to using the usual plane update functions for MMIO flips
11307 */
11308static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11309{
11310 struct drm_device *dev = intel_crtc->base.dev;
11311 bool atomic_update;
11312 u32 start_vbl_count;
11313
11314 intel_mark_page_flip_active(intel_crtc);
11315
11316 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11317
11318 if (INTEL_INFO(dev)->gen >= 9)
11319 skl_do_mmio_flip(intel_crtc);
11320 else
11321 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11322 ilk_do_mmio_flip(intel_crtc);
11323
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011324 if (atomic_update)
11325 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011326}
11327
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011328static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011329{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011330 struct intel_mmio_flip *mmio_flip =
11331 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011332
Daniel Vettereed29a52015-05-21 14:21:25 +020011333 if (mmio_flip->req)
11334 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011335 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011336 false, NULL,
11337 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011338
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011339 intel_do_mmio_flip(mmio_flip->crtc);
11340
Daniel Vettereed29a52015-05-21 14:21:25 +020011341 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011342 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011343}
11344
11345static int intel_queue_mmio_flip(struct drm_device *dev,
11346 struct drm_crtc *crtc,
11347 struct drm_framebuffer *fb,
11348 struct drm_i915_gem_object *obj,
11349 struct intel_engine_cs *ring,
11350 uint32_t flags)
11351{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011352 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011353
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011354 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11355 if (mmio_flip == NULL)
11356 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011357
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011358 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011359 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011360 mmio_flip->crtc = to_intel_crtc(crtc);
11361
11362 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11363 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011364
Sourab Gupta84c33a62014-06-02 16:47:17 +053011365 return 0;
11366}
11367
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011368static int intel_default_queue_flip(struct drm_device *dev,
11369 struct drm_crtc *crtc,
11370 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011371 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011372 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011373 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011374{
11375 return -ENODEV;
11376}
11377
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011378static bool __intel_pageflip_stall_check(struct drm_device *dev,
11379 struct drm_crtc *crtc)
11380{
11381 struct drm_i915_private *dev_priv = dev->dev_private;
11382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11383 struct intel_unpin_work *work = intel_crtc->unpin_work;
11384 u32 addr;
11385
11386 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11387 return true;
11388
11389 if (!work->enable_stall_check)
11390 return false;
11391
11392 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011393 if (work->flip_queued_req &&
11394 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011395 return false;
11396
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011397 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011398 }
11399
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011400 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011401 return false;
11402
11403 /* Potential stall - if we see that the flip has happened,
11404 * assume a missed interrupt. */
11405 if (INTEL_INFO(dev)->gen >= 4)
11406 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11407 else
11408 addr = I915_READ(DSPADDR(intel_crtc->plane));
11409
11410 /* There is a potential issue here with a false positive after a flip
11411 * to the same address. We could address this by checking for a
11412 * non-incrementing frame counter.
11413 */
11414 return addr == work->gtt_offset;
11415}
11416
11417void intel_check_page_flip(struct drm_device *dev, int pipe)
11418{
11419 struct drm_i915_private *dev_priv = dev->dev_private;
11420 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11421 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011422 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011423
Dave Gordon6c51d462015-03-06 15:34:26 +000011424 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011425
11426 if (crtc == NULL)
11427 return;
11428
Daniel Vetterf3260382014-09-15 14:55:23 +020011429 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011430 work = intel_crtc->unpin_work;
11431 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011432 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011433 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011434 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011435 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011436 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011437 if (work != NULL &&
11438 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11439 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011440 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011441}
11442
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011443static int intel_crtc_page_flip(struct drm_crtc *crtc,
11444 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011445 struct drm_pending_vblank_event *event,
11446 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011447{
11448 struct drm_device *dev = crtc->dev;
11449 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011450 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011451 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011452 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011453 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011454 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011455 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011456 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011457 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011458 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011459
Matt Roper2ff8fde2014-07-08 07:50:07 -070011460 /*
11461 * drm_mode_page_flip_ioctl() should already catch this, but double
11462 * check to be safe. In the future we may enable pageflipping from
11463 * a disabled primary plane.
11464 */
11465 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11466 return -EBUSY;
11467
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011468 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011469 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011470 return -EINVAL;
11471
11472 /*
11473 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11474 * Note that pitch changes could also affect these register.
11475 */
11476 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011477 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11478 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011479 return -EINVAL;
11480
Chris Wilsonf900db42014-02-20 09:26:13 +000011481 if (i915_terminally_wedged(&dev_priv->gpu_error))
11482 goto out_hang;
11483
Daniel Vetterb14c5672013-09-19 12:18:32 +020011484 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011485 if (work == NULL)
11486 return -ENOMEM;
11487
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011488 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011489 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011490 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011491 INIT_WORK(&work->work, intel_unpin_work_fn);
11492
Daniel Vetter87b6b102014-05-15 15:33:46 +020011493 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011494 if (ret)
11495 goto free_work;
11496
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011497 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011498 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011499 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011500 /* Before declaring the flip queue wedged, check if
11501 * the hardware completed the operation behind our backs.
11502 */
11503 if (__intel_pageflip_stall_check(dev, crtc)) {
11504 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11505 page_flip_completed(intel_crtc);
11506 } else {
11507 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011508 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011509
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011510 drm_crtc_vblank_put(crtc);
11511 kfree(work);
11512 return -EBUSY;
11513 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011514 }
11515 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011516 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011517
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011518 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11519 flush_workqueue(dev_priv->wq);
11520
Jesse Barnes75dfca82010-02-10 15:09:44 -080011521 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011522 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011523 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011524
Matt Roperf4510a22014-04-01 15:22:40 -070011525 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011526 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011527
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011528 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011529
Chris Wilson89ed88b2015-02-16 14:31:49 +000011530 ret = i915_mutex_lock_interruptible(dev);
11531 if (ret)
11532 goto cleanup;
11533
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011534 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011535 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011536
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011537 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011538 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011539
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011540 if (IS_VALLEYVIEW(dev)) {
11541 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011542 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011543 /* vlv: DISPLAY_FLIP fails to change tiling */
11544 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011545 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011546 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011547 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011548 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011549 if (ring == NULL || ring->id != RCS)
11550 ring = &dev_priv->ring[BCS];
11551 } else {
11552 ring = &dev_priv->ring[RCS];
11553 }
11554
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011555 mmio_flip = use_mmio_flip(ring, obj);
11556
11557 /* When using CS flips, we want to emit semaphores between rings.
11558 * However, when using mmio flips we will create a task to do the
11559 * synchronisation, so all we want here is to pin the framebuffer
11560 * into the display plane and skip any waits.
11561 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011562 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011563 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011564 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011565 if (ret)
11566 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011567
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011568 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11569 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011570
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011571 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011572 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11573 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011574 if (ret)
11575 goto cleanup_unpin;
11576
John Harrisonf06cc1b2014-11-24 18:49:37 +000011577 i915_gem_request_assign(&work->flip_queued_req,
11578 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011579 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011580 if (obj->last_write_req) {
11581 ret = i915_gem_check_olr(obj->last_write_req);
11582 if (ret)
11583 goto cleanup_unpin;
11584 }
11585
Sourab Gupta84c33a62014-06-02 16:47:17 +053011586 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011587 page_flip_flags);
11588 if (ret)
11589 goto cleanup_unpin;
11590
John Harrisonf06cc1b2014-11-24 18:49:37 +000011591 i915_gem_request_assign(&work->flip_queued_req,
11592 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011593 }
11594
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011595 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011596 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011597
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011598 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011599 INTEL_FRONTBUFFER_PRIMARY(pipe));
11600
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011601 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011602 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011603 mutex_unlock(&dev->struct_mutex);
11604
Jesse Barnese5510fa2010-07-01 16:48:37 -070011605 trace_i915_flip_request(intel_crtc->plane, obj);
11606
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011607 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011608
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011609cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011610 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011611cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011612 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011613 mutex_unlock(&dev->struct_mutex);
11614cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011615 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011616 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011617
Chris Wilson89ed88b2015-02-16 14:31:49 +000011618 drm_gem_object_unreference_unlocked(&obj->base);
11619 drm_framebuffer_unreference(work->old_fb);
11620
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011621 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011622 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011623 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011624
Daniel Vetter87b6b102014-05-15 15:33:46 +020011625 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011626free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011627 kfree(work);
11628
Chris Wilsonf900db42014-02-20 09:26:13 +000011629 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011630 struct drm_atomic_state *state;
11631 struct drm_plane_state *plane_state;
11632
Chris Wilsonf900db42014-02-20 09:26:13 +000011633out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011634 state = drm_atomic_state_alloc(dev);
11635 if (!state)
11636 return -ENOMEM;
11637 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11638
11639retry:
11640 plane_state = drm_atomic_get_plane_state(state, primary);
11641 ret = PTR_ERR_OR_ZERO(plane_state);
11642 if (!ret) {
11643 drm_atomic_set_fb_for_plane(plane_state, fb);
11644
11645 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11646 if (!ret)
11647 ret = drm_atomic_commit(state);
11648 }
11649
11650 if (ret == -EDEADLK) {
11651 drm_modeset_backoff(state->acquire_ctx);
11652 drm_atomic_state_clear(state);
11653 goto retry;
11654 }
11655
11656 if (ret)
11657 drm_atomic_state_free(state);
11658
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011659 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011660 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011661 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011662 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011663 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011664 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011665 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011666}
11667
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011668static bool encoders_cloneable(const struct intel_encoder *a,
11669 const struct intel_encoder *b)
11670{
11671 /* masks could be asymmetric, so check both ways */
11672 return a == b || (a->cloneable & (1 << b->type) &&
11673 b->cloneable & (1 << a->type));
11674}
11675
11676static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11677 struct intel_crtc *crtc,
11678 struct intel_encoder *encoder)
11679{
11680 struct intel_encoder *source_encoder;
11681 struct drm_connector *connector;
11682 struct drm_connector_state *connector_state;
11683 int i;
11684
11685 for_each_connector_in_state(state, connector, connector_state, i) {
11686 if (connector_state->crtc != &crtc->base)
11687 continue;
11688
11689 source_encoder =
11690 to_intel_encoder(connector_state->best_encoder);
11691 if (!encoders_cloneable(encoder, source_encoder))
11692 return false;
11693 }
11694
11695 return true;
11696}
11697
11698static bool check_encoder_cloning(struct drm_atomic_state *state,
11699 struct intel_crtc *crtc)
11700{
11701 struct intel_encoder *encoder;
11702 struct drm_connector *connector;
11703 struct drm_connector_state *connector_state;
11704 int i;
11705
11706 for_each_connector_in_state(state, connector, connector_state, i) {
11707 if (connector_state->crtc != &crtc->base)
11708 continue;
11709
11710 encoder = to_intel_encoder(connector_state->best_encoder);
11711 if (!check_single_encoder_cloning(state, crtc, encoder))
11712 return false;
11713 }
11714
11715 return true;
11716}
11717
11718static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11719 struct drm_crtc_state *crtc_state)
11720{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011721 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011722 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011724 struct intel_crtc_state *pipe_config =
11725 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011726 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011727 int ret, idx = crtc->base.id;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011728 bool mode_changed = needs_modeset(crtc_state);
11729
11730 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11731 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11732 return -EINVAL;
11733 }
11734
11735 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11736 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11737 idx, crtc->state->active, intel_crtc->active);
11738
Maarten Lankhorstad421372015-06-15 12:33:42 +020011739 if (mode_changed && crtc_state->enable &&
11740 dev_priv->display.crtc_compute_clock &&
11741 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11742 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11743 pipe_config);
11744 if (ret)
11745 return ret;
11746 }
11747
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011748 return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011749}
11750
Jani Nikula65b38e02015-04-13 11:26:56 +030011751static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011752 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11753 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011754 .atomic_begin = intel_begin_crtc_commit,
11755 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011756 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011757};
11758
Daniel Vetter9a935852012-07-05 22:34:27 +020011759/**
11760 * intel_modeset_update_staged_output_state
11761 *
11762 * Updates the staged output configuration state, e.g. after we've read out the
11763 * current hw state.
11764 */
11765static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11766{
Ville Syrjälä76688512014-01-10 11:28:06 +020011767 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011768 struct intel_encoder *encoder;
11769 struct intel_connector *connector;
11770
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011771 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011772 connector->new_encoder =
11773 to_intel_encoder(connector->base.encoder);
11774 }
11775
Damien Lespiaub2784e12014-08-05 11:29:37 +010011776 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011777 encoder->new_crtc =
11778 to_intel_crtc(encoder->base.crtc);
11779 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011780
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011781 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011782 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011783 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011784}
11785
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011786/* Transitional helper to copy current connector/encoder state to
11787 * connector->state. This is needed so that code that is partially
11788 * converted to atomic does the right thing.
11789 */
11790static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11791{
11792 struct intel_connector *connector;
11793
11794 for_each_intel_connector(dev, connector) {
11795 if (connector->base.encoder) {
11796 connector->base.state->best_encoder =
11797 connector->base.encoder;
11798 connector->base.state->crtc =
11799 connector->base.encoder->crtc;
11800 } else {
11801 connector->base.state->best_encoder = NULL;
11802 connector->base.state->crtc = NULL;
11803 }
11804 }
11805}
11806
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011807static void
Robin Schroereba905b2014-05-18 02:24:50 +020011808connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011809 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011810{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011811 int bpp = pipe_config->pipe_bpp;
11812
11813 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11814 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011815 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011816
11817 /* Don't use an invalid EDID bpc value */
11818 if (connector->base.display_info.bpc &&
11819 connector->base.display_info.bpc * 3 < bpp) {
11820 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11821 bpp, connector->base.display_info.bpc*3);
11822 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11823 }
11824
11825 /* Clamp bpp to 8 on screens without EDID 1.4 */
11826 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11827 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11828 bpp);
11829 pipe_config->pipe_bpp = 24;
11830 }
11831}
11832
11833static int
11834compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011835 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011836{
11837 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011838 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011839 struct drm_connector *connector;
11840 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011841 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011842
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011843 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011844 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011845 else if (INTEL_INFO(dev)->gen >= 5)
11846 bpp = 12*3;
11847 else
11848 bpp = 8*3;
11849
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011850
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011851 pipe_config->pipe_bpp = bpp;
11852
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011853 state = pipe_config->base.state;
11854
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011855 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011856 for_each_connector_in_state(state, connector, connector_state, i) {
11857 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011858 continue;
11859
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011860 connected_sink_compute_bpp(to_intel_connector(connector),
11861 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011862 }
11863
11864 return bpp;
11865}
11866
Daniel Vetter644db712013-09-19 14:53:58 +020011867static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11868{
11869 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11870 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011871 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011872 mode->crtc_hdisplay, mode->crtc_hsync_start,
11873 mode->crtc_hsync_end, mode->crtc_htotal,
11874 mode->crtc_vdisplay, mode->crtc_vsync_start,
11875 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11876}
11877
Daniel Vetterc0b03412013-05-28 12:05:54 +020011878static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011879 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011880 const char *context)
11881{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011882 struct drm_device *dev = crtc->base.dev;
11883 struct drm_plane *plane;
11884 struct intel_plane *intel_plane;
11885 struct intel_plane_state *state;
11886 struct drm_framebuffer *fb;
11887
11888 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11889 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011890
11891 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11892 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11893 pipe_config->pipe_bpp, pipe_config->dither);
11894 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11895 pipe_config->has_pch_encoder,
11896 pipe_config->fdi_lanes,
11897 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11898 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11899 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011900 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11901 pipe_config->has_dp_encoder,
11902 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11903 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11904 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011905
11906 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11907 pipe_config->has_dp_encoder,
11908 pipe_config->dp_m2_n2.gmch_m,
11909 pipe_config->dp_m2_n2.gmch_n,
11910 pipe_config->dp_m2_n2.link_m,
11911 pipe_config->dp_m2_n2.link_n,
11912 pipe_config->dp_m2_n2.tu);
11913
Daniel Vetter55072d12014-11-20 16:10:28 +010011914 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11915 pipe_config->has_audio,
11916 pipe_config->has_infoframe);
11917
Daniel Vetterc0b03412013-05-28 12:05:54 +020011918 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011919 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011920 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011921 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11922 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011923 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011924 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11925 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011926 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11927 crtc->num_scalers,
11928 pipe_config->scaler_state.scaler_users,
11929 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011930 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11931 pipe_config->gmch_pfit.control,
11932 pipe_config->gmch_pfit.pgm_ratios,
11933 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011934 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011935 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011936 pipe_config->pch_pfit.size,
11937 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011938 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011939 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011940
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011941 if (IS_BROXTON(dev)) {
11942 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11943 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11944 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11945 pipe_config->ddi_pll_sel,
11946 pipe_config->dpll_hw_state.ebb0,
11947 pipe_config->dpll_hw_state.pll0,
11948 pipe_config->dpll_hw_state.pll1,
11949 pipe_config->dpll_hw_state.pll2,
11950 pipe_config->dpll_hw_state.pll3,
11951 pipe_config->dpll_hw_state.pll6,
11952 pipe_config->dpll_hw_state.pll8,
11953 pipe_config->dpll_hw_state.pcsdw12);
11954 } else if (IS_SKYLAKE(dev)) {
11955 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11956 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11957 pipe_config->ddi_pll_sel,
11958 pipe_config->dpll_hw_state.ctrl1,
11959 pipe_config->dpll_hw_state.cfgcr1,
11960 pipe_config->dpll_hw_state.cfgcr2);
11961 } else if (HAS_DDI(dev)) {
11962 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11963 pipe_config->ddi_pll_sel,
11964 pipe_config->dpll_hw_state.wrpll);
11965 } else {
11966 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11967 "fp0: 0x%x, fp1: 0x%x\n",
11968 pipe_config->dpll_hw_state.dpll,
11969 pipe_config->dpll_hw_state.dpll_md,
11970 pipe_config->dpll_hw_state.fp0,
11971 pipe_config->dpll_hw_state.fp1);
11972 }
11973
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011974 DRM_DEBUG_KMS("planes on this crtc\n");
11975 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11976 intel_plane = to_intel_plane(plane);
11977 if (intel_plane->pipe != crtc->pipe)
11978 continue;
11979
11980 state = to_intel_plane_state(plane->state);
11981 fb = state->base.fb;
11982 if (!fb) {
11983 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11984 "disabled, scaler_id = %d\n",
11985 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11986 plane->base.id, intel_plane->pipe,
11987 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11988 drm_plane_index(plane), state->scaler_id);
11989 continue;
11990 }
11991
11992 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11993 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11994 plane->base.id, intel_plane->pipe,
11995 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11996 drm_plane_index(plane));
11997 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11998 fb->base.id, fb->width, fb->height, fb->pixel_format);
11999 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12000 state->scaler_id,
12001 state->src.x1 >> 16, state->src.y1 >> 16,
12002 drm_rect_width(&state->src) >> 16,
12003 drm_rect_height(&state->src) >> 16,
12004 state->dst.x1, state->dst.y1,
12005 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12006 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012007}
12008
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012009static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012010{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012011 struct drm_device *dev = state->dev;
12012 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012013 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012014 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012015 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012016 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012017
12018 /*
12019 * Walk the connector list instead of the encoder
12020 * list to detect the problem on ddi platforms
12021 * where there's just one encoder per digital port.
12022 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012023 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012024 if (!connector_state->best_encoder)
12025 continue;
12026
12027 encoder = to_intel_encoder(connector_state->best_encoder);
12028
12029 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012030
12031 switch (encoder->type) {
12032 unsigned int port_mask;
12033 case INTEL_OUTPUT_UNKNOWN:
12034 if (WARN_ON(!HAS_DDI(dev)))
12035 break;
12036 case INTEL_OUTPUT_DISPLAYPORT:
12037 case INTEL_OUTPUT_HDMI:
12038 case INTEL_OUTPUT_EDP:
12039 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12040
12041 /* the same port mustn't appear more than once */
12042 if (used_ports & port_mask)
12043 return false;
12044
12045 used_ports |= port_mask;
12046 default:
12047 break;
12048 }
12049 }
12050
12051 return true;
12052}
12053
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012054static void
12055clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12056{
12057 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012058 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012059 struct intel_dpll_hw_state dpll_hw_state;
12060 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012061 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012062
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012063 /* FIXME: before the switch to atomic started, a new pipe_config was
12064 * kzalloc'd. Code that depends on any field being zero should be
12065 * fixed, so that the crtc_state can be safely duplicated. For now,
12066 * only fields that are know to not cause problems are preserved. */
12067
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012068 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012069 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012070 shared_dpll = crtc_state->shared_dpll;
12071 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012072 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012073
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012074 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012075
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012076 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012077 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012078 crtc_state->shared_dpll = shared_dpll;
12079 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012080 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012081}
12082
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012083static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012084intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012085 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012086{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012087 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012088 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012089 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012090 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012091 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012092 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012093 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012094
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012095 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012096
Daniel Vettere143a212013-07-04 12:01:15 +020012097 pipe_config->cpu_transcoder =
12098 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012099
Imre Deak2960bc92013-07-30 13:36:32 +030012100 /*
12101 * Sanitize sync polarity flags based on requested ones. If neither
12102 * positive or negative polarity is requested, treat this as meaning
12103 * negative polarity.
12104 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012105 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012106 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012107 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012108
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012109 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012110 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012111 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012112
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012113 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12114 * plane pixel format and any sink constraints into account. Returns the
12115 * source plane bpp so that dithering can be selected on mismatches
12116 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012117 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12118 pipe_config);
12119 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012120 goto fail;
12121
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012122 /*
12123 * Determine the real pipe dimensions. Note that stereo modes can
12124 * increase the actual pipe size due to the frame doubling and
12125 * insertion of additional space for blanks between the frame. This
12126 * is stored in the crtc timings. We use the requested mode to do this
12127 * computation to clearly distinguish it from the adjusted mode, which
12128 * can be changed by the connectors in the below retry loop.
12129 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012130 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012131 &pipe_config->pipe_src_w,
12132 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012133
Daniel Vettere29c22c2013-02-21 00:00:16 +010012134encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012135 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012136 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012137 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012138
Daniel Vetter135c81b2013-07-21 21:37:09 +020012139 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012140 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12141 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012142
Daniel Vetter7758a112012-07-08 19:40:39 +020012143 /* Pass our mode to the connectors and the CRTC to give them a chance to
12144 * adjust it according to limitations or connector properties, and also
12145 * a chance to reject the mode entirely.
12146 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012147 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012148 if (connector_state->crtc != crtc)
12149 continue;
12150
12151 encoder = to_intel_encoder(connector_state->best_encoder);
12152
Daniel Vetterefea6e82013-07-21 21:36:59 +020012153 if (!(encoder->compute_config(encoder, pipe_config))) {
12154 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012155 goto fail;
12156 }
12157 }
12158
Daniel Vetterff9a6752013-06-01 17:16:21 +020012159 /* Set default port clock if not overwritten by the encoder. Needs to be
12160 * done afterwards in case the encoder adjusts the mode. */
12161 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012162 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012163 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012164
Daniel Vettera43f6e02013-06-07 23:10:32 +020012165 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012166 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012167 DRM_DEBUG_KMS("CRTC fixup failed\n");
12168 goto fail;
12169 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012170
12171 if (ret == RETRY) {
12172 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12173 ret = -EINVAL;
12174 goto fail;
12175 }
12176
12177 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12178 retry = false;
12179 goto encoder_retry;
12180 }
12181
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012182 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012183 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012184 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012185
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012186 /* Check if we need to force a modeset */
12187 if (pipe_config->has_audio !=
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012188 to_intel_crtc_state(crtc->state)->has_audio) {
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012189 pipe_config->base.mode_changed = true;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012190 ret = drm_atomic_add_affected_planes(state, crtc);
12191 }
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012192
12193 /*
12194 * Note we have an issue here with infoframes: current code
12195 * only updates them on the full mode set path per hw
12196 * requirements. So here we should be checking for any
12197 * required changes and forcing a mode set.
12198 */
Daniel Vetter7758a112012-07-08 19:40:39 +020012199fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012200 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012201}
12202
Daniel Vetterea9d7582012-07-10 10:42:52 +020012203static bool intel_crtc_in_use(struct drm_crtc *crtc)
12204{
12205 struct drm_encoder *encoder;
12206 struct drm_device *dev = crtc->dev;
12207
12208 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12209 if (encoder->crtc == crtc)
12210 return true;
12211
12212 return false;
12213}
12214
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012215static void
12216intel_modeset_update_state(struct drm_atomic_state *state)
12217{
12218 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012219 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012220 struct drm_crtc *crtc;
12221 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012222 struct drm_connector *connector;
12223
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012224 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012225
Damien Lespiaub2784e12014-08-05 11:29:37 +010012226 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012227 if (!intel_encoder->base.crtc)
12228 continue;
12229
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012230 crtc = intel_encoder->base.crtc;
12231 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12232 if (!crtc_state || !needs_modeset(crtc->state))
12233 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012234
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012235 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012236 }
12237
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012238 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorstf7217902015-06-10 10:24:20 +020012239 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012240
Ville Syrjälä76688512014-01-10 11:28:06 +020012241 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012242 for_each_crtc(dev, crtc) {
12243 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012244
12245 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012246
12247 /* Update hwmode for vblank functions */
12248 if (crtc->state->active)
12249 crtc->hwmode = crtc->state->adjusted_mode;
12250 else
12251 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012252 }
12253
12254 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12255 if (!connector->encoder || !connector->encoder->crtc)
12256 continue;
12257
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012258 crtc = connector->encoder->crtc;
12259 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12260 if (!crtc_state || !needs_modeset(crtc->state))
12261 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012262
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012263 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012264 struct drm_property *dpms_property =
12265 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012266
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012267 connector->dpms = DRM_MODE_DPMS_ON;
12268 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012269
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012270 intel_encoder = to_intel_encoder(connector->encoder);
12271 intel_encoder->connectors_active = true;
12272 } else
12273 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012274 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012275}
12276
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012277static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012278{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012279 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012280
12281 if (clock1 == clock2)
12282 return true;
12283
12284 if (!clock1 || !clock2)
12285 return false;
12286
12287 diff = abs(clock1 - clock2);
12288
12289 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12290 return true;
12291
12292 return false;
12293}
12294
Daniel Vetter25c5b262012-07-08 22:08:04 +020012295#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12296 list_for_each_entry((intel_crtc), \
12297 &(dev)->mode_config.crtc_list, \
12298 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012299 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012300
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012301static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012302intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012303 struct intel_crtc_state *current_config,
12304 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012305{
Daniel Vetter66e985c2013-06-05 13:34:20 +020012306#define PIPE_CONF_CHECK_X(name) \
12307 if (current_config->name != pipe_config->name) { \
12308 DRM_ERROR("mismatch in " #name " " \
12309 "(expected 0x%08x, found 0x%08x)\n", \
12310 current_config->name, \
12311 pipe_config->name); \
12312 return false; \
12313 }
12314
Daniel Vetter08a24032013-04-19 11:25:34 +020012315#define PIPE_CONF_CHECK_I(name) \
12316 if (current_config->name != pipe_config->name) { \
12317 DRM_ERROR("mismatch in " #name " " \
12318 "(expected %i, found %i)\n", \
12319 current_config->name, \
12320 pipe_config->name); \
12321 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012322 }
12323
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012324/* This is required for BDW+ where there is only one set of registers for
12325 * switching between high and low RR.
12326 * This macro can be used whenever a comparison has to be made between one
12327 * hw state and multiple sw state variables.
12328 */
12329#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12330 if ((current_config->name != pipe_config->name) && \
12331 (current_config->alt_name != pipe_config->name)) { \
12332 DRM_ERROR("mismatch in " #name " " \
12333 "(expected %i or %i, found %i)\n", \
12334 current_config->name, \
12335 current_config->alt_name, \
12336 pipe_config->name); \
12337 return false; \
12338 }
12339
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012340#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12341 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070012342 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012343 "(expected %i, found %i)\n", \
12344 current_config->name & (mask), \
12345 pipe_config->name & (mask)); \
12346 return false; \
12347 }
12348
Ville Syrjälä5e550652013-09-06 23:29:07 +030012349#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12350 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12351 DRM_ERROR("mismatch in " #name " " \
12352 "(expected %i, found %i)\n", \
12353 current_config->name, \
12354 pipe_config->name); \
12355 return false; \
12356 }
12357
Daniel Vetterbb760062013-06-06 14:55:52 +020012358#define PIPE_CONF_QUIRK(quirk) \
12359 ((current_config->quirks | pipe_config->quirks) & (quirk))
12360
Daniel Vettereccb1402013-05-22 00:50:22 +020012361 PIPE_CONF_CHECK_I(cpu_transcoder);
12362
Daniel Vetter08a24032013-04-19 11:25:34 +020012363 PIPE_CONF_CHECK_I(has_pch_encoder);
12364 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012365 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12366 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12367 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12368 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12369 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012370
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012371 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012372
12373 if (INTEL_INFO(dev)->gen < 8) {
12374 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12375 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12376 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12377 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12378 PIPE_CONF_CHECK_I(dp_m_n.tu);
12379
12380 if (current_config->has_drrs) {
12381 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12382 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12383 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12384 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12385 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12386 }
12387 } else {
12388 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12389 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12390 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12391 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12392 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12393 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012394
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012395 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12396 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12397 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12398 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12399 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12400 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012401
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012402 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12403 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12404 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12405 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12406 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12407 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012408
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012409 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012410 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012411 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12412 IS_VALLEYVIEW(dev))
12413 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012414 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012415
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012416 PIPE_CONF_CHECK_I(has_audio);
12417
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012418 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012419 DRM_MODE_FLAG_INTERLACE);
12420
Daniel Vetterbb760062013-06-06 14:55:52 +020012421 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012422 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012423 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012424 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012425 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012426 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012427 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012428 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012429 DRM_MODE_FLAG_NVSYNC);
12430 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012431
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012432 PIPE_CONF_CHECK_I(pipe_src_w);
12433 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012434
Daniel Vetter99535992014-04-13 12:00:33 +020012435 /*
12436 * FIXME: BIOS likes to set up a cloned config with lvds+external
12437 * screen. Since we don't yet re-compute the pipe config when moving
12438 * just the lvds port away to another pipe the sw tracking won't match.
12439 *
12440 * Proper atomic modesets with recomputed global state will fix this.
12441 * Until then just don't check gmch state for inherited modes.
12442 */
12443 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12444 PIPE_CONF_CHECK_I(gmch_pfit.control);
12445 /* pfit ratios are autocomputed by the hw on gen4+ */
12446 if (INTEL_INFO(dev)->gen < 4)
12447 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12448 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12449 }
12450
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012451 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12452 if (current_config->pch_pfit.enabled) {
12453 PIPE_CONF_CHECK_I(pch_pfit.pos);
12454 PIPE_CONF_CHECK_I(pch_pfit.size);
12455 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012456
Chandra Kondurua1b22782015-04-07 15:28:45 -070012457 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12458
Jesse Barnese59150d2014-01-07 13:30:45 -080012459 /* BDW+ don't expose a synchronous way to read the state */
12460 if (IS_HASWELL(dev))
12461 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012462
Ville Syrjälä282740f2013-09-04 18:30:03 +030012463 PIPE_CONF_CHECK_I(double_wide);
12464
Daniel Vetter26804af2014-06-25 22:01:55 +030012465 PIPE_CONF_CHECK_X(ddi_pll_sel);
12466
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012467 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012468 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012469 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012470 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12471 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012472 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012473 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12474 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12475 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012476
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012477 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12478 PIPE_CONF_CHECK_I(pipe_bpp);
12479
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012480 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012481 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012482
Daniel Vetter66e985c2013-06-05 13:34:20 +020012483#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012484#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012485#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012486#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012487#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012488#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012489
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012490 return true;
12491}
12492
Damien Lespiau08db6652014-11-04 17:06:52 +000012493static void check_wm_state(struct drm_device *dev)
12494{
12495 struct drm_i915_private *dev_priv = dev->dev_private;
12496 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12497 struct intel_crtc *intel_crtc;
12498 int plane;
12499
12500 if (INTEL_INFO(dev)->gen < 9)
12501 return;
12502
12503 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12504 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12505
12506 for_each_intel_crtc(dev, intel_crtc) {
12507 struct skl_ddb_entry *hw_entry, *sw_entry;
12508 const enum pipe pipe = intel_crtc->pipe;
12509
12510 if (!intel_crtc->active)
12511 continue;
12512
12513 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012514 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012515 hw_entry = &hw_ddb.plane[pipe][plane];
12516 sw_entry = &sw_ddb->plane[pipe][plane];
12517
12518 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12519 continue;
12520
12521 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12522 "(expected (%u,%u), found (%u,%u))\n",
12523 pipe_name(pipe), plane + 1,
12524 sw_entry->start, sw_entry->end,
12525 hw_entry->start, hw_entry->end);
12526 }
12527
12528 /* cursor */
12529 hw_entry = &hw_ddb.cursor[pipe];
12530 sw_entry = &sw_ddb->cursor[pipe];
12531
12532 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12533 continue;
12534
12535 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12536 "(expected (%u,%u), found (%u,%u))\n",
12537 pipe_name(pipe),
12538 sw_entry->start, sw_entry->end,
12539 hw_entry->start, hw_entry->end);
12540 }
12541}
12542
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012543static void
12544check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012545{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012546 struct intel_connector *connector;
12547
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012548 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012549 /* This also checks the encoder/connector hw state with the
12550 * ->get_hw_state callbacks. */
12551 intel_connector_check_state(connector);
12552
Rob Clarke2c719b2014-12-15 13:56:32 -050012553 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012554 "connector's staged encoder doesn't match current encoder\n");
12555 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012556}
12557
12558static void
12559check_encoder_state(struct drm_device *dev)
12560{
12561 struct intel_encoder *encoder;
12562 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012563
Damien Lespiaub2784e12014-08-05 11:29:37 +010012564 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012565 bool enabled = false;
12566 bool active = false;
12567 enum pipe pipe, tracked_pipe;
12568
12569 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12570 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012571 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012572
Rob Clarke2c719b2014-12-15 13:56:32 -050012573 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012574 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012575 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012576 "encoder's active_connectors set, but no crtc\n");
12577
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012578 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012579 if (connector->base.encoder != &encoder->base)
12580 continue;
12581 enabled = true;
12582 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12583 active = true;
12584 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012585 /*
12586 * for MST connectors if we unplug the connector is gone
12587 * away but the encoder is still connected to a crtc
12588 * until a modeset happens in response to the hotplug.
12589 */
12590 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12591 continue;
12592
Rob Clarke2c719b2014-12-15 13:56:32 -050012593 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012594 "encoder's enabled state mismatch "
12595 "(expected %i, found %i)\n",
12596 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012597 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012598 "active encoder with no crtc\n");
12599
Rob Clarke2c719b2014-12-15 13:56:32 -050012600 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012601 "encoder's computed active state doesn't match tracked active state "
12602 "(expected %i, found %i)\n", active, encoder->connectors_active);
12603
12604 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012605 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012606 "encoder's hw state doesn't match sw tracking "
12607 "(expected %i, found %i)\n",
12608 encoder->connectors_active, active);
12609
12610 if (!encoder->base.crtc)
12611 continue;
12612
12613 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012614 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012615 "active encoder's pipe doesn't match"
12616 "(expected %i, found %i)\n",
12617 tracked_pipe, pipe);
12618
12619 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012620}
12621
12622static void
12623check_crtc_state(struct drm_device *dev)
12624{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012625 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012626 struct intel_crtc *crtc;
12627 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012628 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012629
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012630 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012631 bool enabled = false;
12632 bool active = false;
12633
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012634 memset(&pipe_config, 0, sizeof(pipe_config));
12635
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012636 DRM_DEBUG_KMS("[CRTC:%d]\n",
12637 crtc->base.base.id);
12638
Matt Roper83d65732015-02-25 13:12:16 -080012639 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012640 "active crtc, but not enabled in sw tracking\n");
12641
Damien Lespiaub2784e12014-08-05 11:29:37 +010012642 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012643 if (encoder->base.crtc != &crtc->base)
12644 continue;
12645 enabled = true;
12646 if (encoder->connectors_active)
12647 active = true;
12648 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012649
Rob Clarke2c719b2014-12-15 13:56:32 -050012650 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012651 "crtc's computed active state doesn't match tracked active state "
12652 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012653 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012654 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012655 "(expected %i, found %i)\n", enabled,
12656 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012657
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012658 active = dev_priv->display.get_pipe_config(crtc,
12659 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012660
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012661 /* hw state is inconsistent with the pipe quirk */
12662 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12663 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012664 active = crtc->active;
12665
Damien Lespiaub2784e12014-08-05 11:29:37 +010012666 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012667 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012668 if (encoder->base.crtc != &crtc->base)
12669 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012670 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012671 encoder->get_config(encoder, &pipe_config);
12672 }
12673
Rob Clarke2c719b2014-12-15 13:56:32 -050012674 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012675 "crtc active state doesn't match with hw state "
12676 "(expected %i, found %i)\n", crtc->active, active);
12677
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012678 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12679 "transitional active state does not match atomic hw state "
12680 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12681
Daniel Vetterc0b03412013-05-28 12:05:54 +020012682 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012683 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012684 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012685 intel_dump_pipe_config(crtc, &pipe_config,
12686 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012687 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012688 "[sw state]");
12689 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012690 }
12691}
12692
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012693static void
12694check_shared_dpll_state(struct drm_device *dev)
12695{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012696 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012697 struct intel_crtc *crtc;
12698 struct intel_dpll_hw_state dpll_hw_state;
12699 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012700
12701 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12702 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12703 int enabled_crtcs = 0, active_crtcs = 0;
12704 bool active;
12705
12706 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12707
12708 DRM_DEBUG_KMS("%s\n", pll->name);
12709
12710 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12711
Rob Clarke2c719b2014-12-15 13:56:32 -050012712 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012713 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012714 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012715 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012716 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012717 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012718 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012719 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012720 "pll on state mismatch (expected %i, found %i)\n",
12721 pll->on, active);
12722
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012723 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012724 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012725 enabled_crtcs++;
12726 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12727 active_crtcs++;
12728 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012729 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012730 "pll active crtcs mismatch (expected %i, found %i)\n",
12731 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012732 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012733 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012734 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012735
Rob Clarke2c719b2014-12-15 13:56:32 -050012736 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012737 sizeof(dpll_hw_state)),
12738 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012739 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012740}
12741
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012742void
12743intel_modeset_check_state(struct drm_device *dev)
12744{
Damien Lespiau08db6652014-11-04 17:06:52 +000012745 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012746 check_connector_state(dev);
12747 check_encoder_state(dev);
12748 check_crtc_state(dev);
12749 check_shared_dpll_state(dev);
12750}
12751
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012752void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012753 int dotclock)
12754{
12755 /*
12756 * FDI already provided one idea for the dotclock.
12757 * Yell if the encoder disagrees.
12758 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012759 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012760 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012761 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012762}
12763
Ville Syrjälä80715b22014-05-15 20:23:23 +030012764static void update_scanline_offset(struct intel_crtc *crtc)
12765{
12766 struct drm_device *dev = crtc->base.dev;
12767
12768 /*
12769 * The scanline counter increments at the leading edge of hsync.
12770 *
12771 * On most platforms it starts counting from vtotal-1 on the
12772 * first active line. That means the scanline counter value is
12773 * always one less than what we would expect. Ie. just after
12774 * start of vblank, which also occurs at start of hsync (on the
12775 * last active line), the scanline counter will read vblank_start-1.
12776 *
12777 * On gen2 the scanline counter starts counting from 1 instead
12778 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12779 * to keep the value positive), instead of adding one.
12780 *
12781 * On HSW+ the behaviour of the scanline counter depends on the output
12782 * type. For DP ports it behaves like most other platforms, but on HDMI
12783 * there's an extra 1 line difference. So we need to add two instead of
12784 * one to the value.
12785 */
12786 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012787 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012788 int vtotal;
12789
12790 vtotal = mode->crtc_vtotal;
12791 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12792 vtotal /= 2;
12793
12794 crtc->scanline_offset = vtotal - 1;
12795 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012796 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012797 crtc->scanline_offset = 2;
12798 } else
12799 crtc->scanline_offset = 1;
12800}
12801
Maarten Lankhorstad421372015-06-15 12:33:42 +020012802static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012803{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012804 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012805 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012806 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012807 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012808 struct intel_crtc_state *intel_crtc_state;
12809 struct drm_crtc *crtc;
12810 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012811 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012812
12813 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012814 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012815
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012816 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012817 int dpll;
12818
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012819 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012820 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012821 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012822
Maarten Lankhorstad421372015-06-15 12:33:42 +020012823 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012824 continue;
12825
Maarten Lankhorstad421372015-06-15 12:33:42 +020012826 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012827
Maarten Lankhorstad421372015-06-15 12:33:42 +020012828 if (!shared_dpll)
12829 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12830
12831 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012832 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012833}
12834
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012835/*
12836 * This implements the workaround described in the "notes" section of the mode
12837 * set sequence documentation. When going from no pipes or single pipe to
12838 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12839 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12840 */
12841static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12842{
12843 struct drm_crtc_state *crtc_state;
12844 struct intel_crtc *intel_crtc;
12845 struct drm_crtc *crtc;
12846 struct intel_crtc_state *first_crtc_state = NULL;
12847 struct intel_crtc_state *other_crtc_state = NULL;
12848 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12849 int i;
12850
12851 /* look at all crtc's that are going to be enabled in during modeset */
12852 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12853 intel_crtc = to_intel_crtc(crtc);
12854
12855 if (!crtc_state->active || !needs_modeset(crtc_state))
12856 continue;
12857
12858 if (first_crtc_state) {
12859 other_crtc_state = to_intel_crtc_state(crtc_state);
12860 break;
12861 } else {
12862 first_crtc_state = to_intel_crtc_state(crtc_state);
12863 first_pipe = intel_crtc->pipe;
12864 }
12865 }
12866
12867 /* No workaround needed? */
12868 if (!first_crtc_state)
12869 return 0;
12870
12871 /* w/a possibly needed, check how many crtc's are already enabled. */
12872 for_each_intel_crtc(state->dev, intel_crtc) {
12873 struct intel_crtc_state *pipe_config;
12874
12875 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12876 if (IS_ERR(pipe_config))
12877 return PTR_ERR(pipe_config);
12878
12879 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12880
12881 if (!pipe_config->base.active ||
12882 needs_modeset(&pipe_config->base))
12883 continue;
12884
12885 /* 2 or more enabled crtcs means no need for w/a */
12886 if (enabled_pipe != INVALID_PIPE)
12887 return 0;
12888
12889 enabled_pipe = intel_crtc->pipe;
12890 }
12891
12892 if (enabled_pipe != INVALID_PIPE)
12893 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12894 else if (other_crtc_state)
12895 other_crtc_state->hsw_workaround_pipe = first_pipe;
12896
12897 return 0;
12898}
12899
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012900/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012901static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012902{
12903 struct drm_device *dev = state->dev;
12904 int ret;
12905
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012906 if (!check_digital_port_conflicts(state)) {
12907 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12908 return -EINVAL;
12909 }
12910
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012911 /*
12912 * See if the config requires any additional preparation, e.g.
12913 * to adjust global state with pipes off. We need to do this
12914 * here so we can get the modeset_pipe updated config for the new
12915 * mode set on this crtc. For other crtcs we need to use the
12916 * adjusted_mode bits in the crtc directly.
12917 */
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012918 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
12919 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
12920 ret = valleyview_modeset_global_pipes(state);
12921 else
12922 ret = broadwell_modeset_global_pipes(state);
12923
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012924 if (ret)
12925 return ret;
12926 }
12927
Maarten Lankhorstad421372015-06-15 12:33:42 +020012928 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012929
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012930 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012931 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012932
Maarten Lankhorstad421372015-06-15 12:33:42 +020012933 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012934}
12935
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012936static int
12937intel_modeset_compute_config(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012938{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012939 struct drm_crtc *crtc;
12940 struct drm_crtc_state *crtc_state;
12941 int ret, i;
12942
12943 ret = drm_atomic_helper_check_modeset(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012944 if (ret)
12945 return ret;
12946
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012947 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12948 if (!crtc_state->enable &&
12949 WARN_ON(crtc_state->active))
12950 crtc_state->active = false;
12951
12952 if (!crtc_state->enable)
12953 continue;
12954
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012955 if (!needs_modeset(crtc_state)) {
12956 ret = drm_atomic_add_affected_connectors(state, crtc);
12957 if (ret)
12958 return ret;
12959 }
12960
12961 ret = intel_modeset_pipe_config(crtc,
12962 to_intel_crtc_state(crtc_state));
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012963 if (ret)
12964 return ret;
12965
12966 intel_dump_pipe_config(to_intel_crtc(crtc),
12967 to_intel_crtc_state(crtc_state),
12968 "[modeset]");
12969 }
12970
12971 ret = intel_modeset_checks(state);
12972 if (ret)
12973 return ret;
12974
12975 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012976}
12977
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020012978static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012979{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020012980 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012981 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012982 struct drm_crtc *crtc;
12983 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012984 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012985 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012986
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012987 ret = drm_atomic_helper_prepare_planes(dev, state);
12988 if (ret)
12989 return ret;
12990
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020012991 drm_atomic_helper_swap_state(dev, state);
12992
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012993 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020012994 if (!needs_modeset(crtc->state) || !crtc_state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012995 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012996
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012997 intel_crtc_disable_planes(crtc);
12998 dev_priv->display.crtc_disable(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012999 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013000
Daniel Vetterea9d7582012-07-10 10:42:52 +020013001 /* Only after disabling all output pipelines that will be changed can we
13002 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013003 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013004
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013005 /* The state has been swaped above, so state actually contains the
13006 * old state now. */
13007
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030013008 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013009
Daniel Vettera6778b32012-07-02 09:56:42 +020013010 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013011 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013012 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
13013
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020013014 if (!needs_modeset(crtc->state) || !crtc->state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013015 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013016
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013017 update_scanline_offset(to_intel_crtc(crtc));
13018
13019 dev_priv->display.crtc_enable(crtc);
13020 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013021 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013022
Daniel Vettera6778b32012-07-02 09:56:42 +020013023 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013024
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013025 drm_atomic_helper_cleanup_planes(dev, state);
13026
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013027 drm_atomic_state_free(state);
13028
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013029 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013030}
13031
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013032static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013033{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013034 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013035 int ret;
13036
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013037 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013038 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013039 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013040
13041 return ret;
13042}
13043
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013044static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013045{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013046 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013047
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013048 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013049 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013050 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013051
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013052 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013053}
13054
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013055void intel_crtc_restore_mode(struct drm_crtc *crtc)
13056{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013057 struct drm_device *dev = crtc->dev;
13058 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013059 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013060 struct intel_encoder *encoder;
13061 struct intel_connector *connector;
13062 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013063 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013064 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013065
13066 state = drm_atomic_state_alloc(dev);
13067 if (!state) {
13068 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13069 crtc->base.id);
13070 return;
13071 }
13072
13073 state->acquire_ctx = dev->mode_config.acquire_ctx;
13074
13075 /* The force restore path in the HW readout code relies on the staged
13076 * config still keeping the user requested config while the actual
13077 * state has been overwritten by the configuration read from HW. We
13078 * need to copy the staged config to the atomic state, otherwise the
13079 * mode set will just reapply the state the HW is already in. */
13080 for_each_intel_encoder(dev, encoder) {
13081 if (&encoder->new_crtc->base != crtc)
13082 continue;
13083
13084 for_each_intel_connector(dev, connector) {
13085 if (connector->new_encoder != encoder)
13086 continue;
13087
13088 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13089 if (IS_ERR(connector_state)) {
13090 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13091 connector->base.base.id,
13092 connector->base.name,
13093 PTR_ERR(connector_state));
13094 continue;
13095 }
13096
13097 connector_state->crtc = crtc;
13098 connector_state->best_encoder = &encoder->base;
13099 }
13100 }
13101
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013102 for_each_intel_crtc(dev, intel_crtc) {
13103 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13104 continue;
13105
13106 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13107 if (IS_ERR(crtc_state)) {
13108 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13109 intel_crtc->base.base.id,
13110 PTR_ERR(crtc_state));
13111 continue;
13112 }
13113
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013114 crtc_state->base.active = crtc_state->base.enable =
13115 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013116
13117 if (&intel_crtc->base == crtc)
13118 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013119 }
13120
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013121 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13122 crtc->primary->fb, crtc->x, crtc->y);
13123
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013124 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013125 if (ret)
13126 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013127}
13128
Daniel Vetter25c5b262012-07-08 22:08:04 +020013129#undef for_each_intel_crtc_masked
13130
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013131static bool intel_connector_in_mode_set(struct intel_connector *connector,
13132 struct drm_mode_set *set)
13133{
13134 int ro;
13135
13136 for (ro = 0; ro < set->num_connectors; ro++)
13137 if (set->connectors[ro] == &connector->base)
13138 return true;
13139
13140 return false;
13141}
13142
Daniel Vetter2e431052012-07-04 22:42:15 +020013143static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013144intel_modeset_stage_output_state(struct drm_device *dev,
13145 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013146 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013147{
Daniel Vetter9a935852012-07-05 22:34:27 +020013148 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013149 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013150 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013151 struct drm_crtc *crtc;
13152 struct drm_crtc_state *crtc_state;
13153 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013154
Damien Lespiau9abdda72013-02-13 13:29:23 +000013155 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013156 * of connectors. For paranoia, double-check this. */
13157 WARN_ON(!set->fb && (set->num_connectors != 0));
13158 WARN_ON(set->fb && (set->num_connectors == 0));
13159
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013160 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013161 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13162
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013163 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13164 continue;
13165
13166 connector_state =
13167 drm_atomic_get_connector_state(state, &connector->base);
13168 if (IS_ERR(connector_state))
13169 return PTR_ERR(connector_state);
13170
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013171 if (in_mode_set) {
13172 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013173 connector_state->best_encoder =
13174 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013175 }
13176
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013177 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013178 continue;
13179
Daniel Vetter9a935852012-07-05 22:34:27 +020013180 /* If we disable the crtc, disable all its connectors. Also, if
13181 * the connector is on the changing crtc but not on the new
13182 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013183 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013184 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013185
13186 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13187 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013188 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013189 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013190 }
13191 /* connector->new_encoder is now updated for all connectors. */
13192
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013193 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13194 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013195
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013196 if (!connector_state->best_encoder) {
13197 ret = drm_atomic_set_crtc_for_connector(connector_state,
13198 NULL);
13199 if (ret)
13200 return ret;
13201
Daniel Vetter50f56112012-07-02 09:35:43 +020013202 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013203 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013204
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013205 if (intel_connector_in_mode_set(connector, set)) {
13206 struct drm_crtc *crtc = connector->base.state->crtc;
13207
13208 /* If this connector was in a previous crtc, add it
13209 * to the state. We might need to disable it. */
13210 if (crtc) {
13211 crtc_state =
13212 drm_atomic_get_crtc_state(state, crtc);
13213 if (IS_ERR(crtc_state))
13214 return PTR_ERR(crtc_state);
13215 }
13216
13217 ret = drm_atomic_set_crtc_for_connector(connector_state,
13218 set->crtc);
13219 if (ret)
13220 return ret;
13221 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013222
13223 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013224 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13225 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013226 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013227 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013228
Daniel Vetter9a935852012-07-05 22:34:27 +020013229 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13230 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013231 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013232 connector_state->crtc->base.id);
13233
13234 if (connector_state->best_encoder != &connector->encoder->base)
13235 connector->encoder =
13236 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013237 }
13238
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013239 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013240 bool has_connectors;
13241
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013242 ret = drm_atomic_add_affected_connectors(state, crtc);
13243 if (ret)
13244 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013245
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013246 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13247 if (has_connectors != crtc_state->enable)
13248 crtc_state->enable =
13249 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013250 }
13251
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013252 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13253 set->fb, set->x, set->y);
13254 if (ret)
13255 return ret;
13256
13257 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13258 if (IS_ERR(crtc_state))
13259 return PTR_ERR(crtc_state);
13260
Matt Roperce522992015-06-05 15:08:24 -070013261 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13262 if (ret)
13263 return ret;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013264
13265 if (set->num_connectors)
13266 crtc_state->active = true;
13267
Daniel Vetter2e431052012-07-04 22:42:15 +020013268 return 0;
13269}
13270
13271static int intel_crtc_set_config(struct drm_mode_set *set)
13272{
13273 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013274 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013275 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013276
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013277 BUG_ON(!set);
13278 BUG_ON(!set->crtc);
13279 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013280
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013281 /* Enforce sane interface api - has been abused by the fb helper. */
13282 BUG_ON(!set->mode && set->fb);
13283 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013284
Daniel Vetter2e431052012-07-04 22:42:15 +020013285 if (set->fb) {
13286 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13287 set->crtc->base.id, set->fb->base.id,
13288 (int)set->num_connectors, set->x, set->y);
13289 } else {
13290 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013291 }
13292
13293 dev = set->crtc->dev;
13294
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013295 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013296 if (!state)
13297 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013298
13299 state->acquire_ctx = dev->mode_config.acquire_ctx;
13300
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013301 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013302 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013303 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013304
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013305 ret = intel_modeset_compute_config(state);
13306 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013307 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013308
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013309 intel_update_pipe_size(to_intel_crtc(set->crtc));
13310
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013311 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013312 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013313 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13314 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013315 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013316
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013317out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013318 if (ret)
13319 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013320 return ret;
13321}
13322
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013323static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013324 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013325 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013326 .destroy = intel_crtc_destroy,
13327 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013328 .atomic_duplicate_state = intel_crtc_duplicate_state,
13329 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013330};
13331
Daniel Vetter53589012013-06-05 13:34:16 +020013332static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13333 struct intel_shared_dpll *pll,
13334 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013335{
Daniel Vetter53589012013-06-05 13:34:16 +020013336 uint32_t val;
13337
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013338 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013339 return false;
13340
Daniel Vetter53589012013-06-05 13:34:16 +020013341 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013342 hw_state->dpll = val;
13343 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13344 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013345
13346 return val & DPLL_VCO_ENABLE;
13347}
13348
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013349static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13350 struct intel_shared_dpll *pll)
13351{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013352 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13353 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013354}
13355
Daniel Vettere7b903d2013-06-05 13:34:14 +020013356static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13357 struct intel_shared_dpll *pll)
13358{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013359 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013360 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013361
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013362 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013363
13364 /* Wait for the clocks to stabilize. */
13365 POSTING_READ(PCH_DPLL(pll->id));
13366 udelay(150);
13367
13368 /* The pixel multiplier can only be updated once the
13369 * DPLL is enabled and the clocks are stable.
13370 *
13371 * So write it again.
13372 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013373 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013374 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013375 udelay(200);
13376}
13377
13378static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13379 struct intel_shared_dpll *pll)
13380{
13381 struct drm_device *dev = dev_priv->dev;
13382 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013383
13384 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013385 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013386 if (intel_crtc_to_shared_dpll(crtc) == pll)
13387 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13388 }
13389
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013390 I915_WRITE(PCH_DPLL(pll->id), 0);
13391 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013392 udelay(200);
13393}
13394
Daniel Vetter46edb022013-06-05 13:34:12 +020013395static char *ibx_pch_dpll_names[] = {
13396 "PCH DPLL A",
13397 "PCH DPLL B",
13398};
13399
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013400static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013401{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013402 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013403 int i;
13404
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013405 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013406
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013407 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013408 dev_priv->shared_dplls[i].id = i;
13409 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013410 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013411 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13412 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013413 dev_priv->shared_dplls[i].get_hw_state =
13414 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013415 }
13416}
13417
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013418static void intel_shared_dpll_init(struct drm_device *dev)
13419{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013420 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013421
Ville Syrjäläb6283052015-06-03 15:45:07 +030013422 intel_update_cdclk(dev);
13423
Daniel Vetter9cd86932014-06-25 22:01:57 +030013424 if (HAS_DDI(dev))
13425 intel_ddi_pll_init(dev);
13426 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013427 ibx_pch_dpll_init(dev);
13428 else
13429 dev_priv->num_shared_dpll = 0;
13430
13431 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013432}
13433
Matt Roper6beb8c232014-12-01 15:40:14 -080013434/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013435 * intel_wm_need_update - Check whether watermarks need updating
13436 * @plane: drm plane
13437 * @state: new plane state
13438 *
13439 * Check current plane state versus the new one to determine whether
13440 * watermarks need to be recalculated.
13441 *
13442 * Returns true or false.
13443 */
13444bool intel_wm_need_update(struct drm_plane *plane,
13445 struct drm_plane_state *state)
13446{
13447 /* Update watermarks on tiling changes. */
13448 if (!plane->state->fb || !state->fb ||
13449 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13450 plane->state->rotation != state->rotation)
13451 return true;
13452
13453 return false;
13454}
13455
13456/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013457 * intel_prepare_plane_fb - Prepare fb for usage on plane
13458 * @plane: drm plane to prepare for
13459 * @fb: framebuffer to prepare for presentation
13460 *
13461 * Prepares a framebuffer for usage on a display plane. Generally this
13462 * involves pinning the underlying object and updating the frontbuffer tracking
13463 * bits. Some older platforms need special physical address handling for
13464 * cursor planes.
13465 *
13466 * Returns 0 on success, negative error code on failure.
13467 */
13468int
13469intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013470 struct drm_framebuffer *fb,
13471 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013472{
13473 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013474 struct intel_plane *intel_plane = to_intel_plane(plane);
13475 enum pipe pipe = intel_plane->pipe;
13476 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13477 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13478 unsigned frontbuffer_bits = 0;
13479 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013480
Matt Roperea2c67b2014-12-23 10:41:52 -080013481 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013482 return 0;
13483
Matt Roper6beb8c232014-12-01 15:40:14 -080013484 switch (plane->type) {
13485 case DRM_PLANE_TYPE_PRIMARY:
13486 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13487 break;
13488 case DRM_PLANE_TYPE_CURSOR:
13489 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13490 break;
13491 case DRM_PLANE_TYPE_OVERLAY:
13492 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13493 break;
13494 }
Matt Roper465c1202014-05-29 08:06:54 -070013495
Matt Roper4c345742014-07-09 16:22:10 -070013496 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013497
Matt Roper6beb8c232014-12-01 15:40:14 -080013498 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13499 INTEL_INFO(dev)->cursor_needs_physical) {
13500 int align = IS_I830(dev) ? 16 * 1024 : 256;
13501 ret = i915_gem_object_attach_phys(obj, align);
13502 if (ret)
13503 DRM_DEBUG_KMS("failed to attach phys object\n");
13504 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013505 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013506 }
13507
13508 if (ret == 0)
13509 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13510
13511 mutex_unlock(&dev->struct_mutex);
13512
13513 return ret;
13514}
13515
Matt Roper38f3ce32014-12-02 07:45:25 -080013516/**
13517 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13518 * @plane: drm plane to clean up for
13519 * @fb: old framebuffer that was on plane
13520 *
13521 * Cleans up a framebuffer that has just been removed from a plane.
13522 */
13523void
13524intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013525 struct drm_framebuffer *fb,
13526 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013527{
13528 struct drm_device *dev = plane->dev;
13529 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13530
13531 if (WARN_ON(!obj))
13532 return;
13533
13534 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13535 !INTEL_INFO(dev)->cursor_needs_physical) {
13536 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013537 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013538 mutex_unlock(&dev->struct_mutex);
13539 }
Matt Roper465c1202014-05-29 08:06:54 -070013540}
13541
Chandra Konduru6156a452015-04-27 13:48:39 -070013542int
13543skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13544{
13545 int max_scale;
13546 struct drm_device *dev;
13547 struct drm_i915_private *dev_priv;
13548 int crtc_clock, cdclk;
13549
13550 if (!intel_crtc || !crtc_state)
13551 return DRM_PLANE_HELPER_NO_SCALING;
13552
13553 dev = intel_crtc->base.dev;
13554 dev_priv = dev->dev_private;
13555 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13556 cdclk = dev_priv->display.get_display_clock_speed(dev);
13557
13558 if (!crtc_clock || !cdclk)
13559 return DRM_PLANE_HELPER_NO_SCALING;
13560
13561 /*
13562 * skl max scale is lower of:
13563 * close to 3 but not 3, -1 is for that purpose
13564 * or
13565 * cdclk/crtc_clock
13566 */
13567 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13568
13569 return max_scale;
13570}
13571
Matt Roper465c1202014-05-29 08:06:54 -070013572static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013573intel_check_primary_plane(struct drm_plane *plane,
13574 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013575{
Matt Roper32b7eee2014-12-24 07:59:06 -080013576 struct drm_device *dev = plane->dev;
13577 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013578 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013579 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013580 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013581 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013582 struct drm_rect *dest = &state->dst;
13583 struct drm_rect *src = &state->src;
13584 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013585 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013586 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13587 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013588 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013589
Matt Roperea2c67b2014-12-23 10:41:52 -080013590 crtc = crtc ? crtc : plane->crtc;
13591 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013592 crtc_state = state->base.state ?
13593 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013594
Chandra Konduru6156a452015-04-27 13:48:39 -070013595 if (INTEL_INFO(dev)->gen >= 9) {
Chandra Konduru225c2282015-05-18 16:18:44 -070013596 /* use scaler when colorkey is not required */
13597 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13598 min_scale = 1;
13599 max_scale = skl_max_scale(intel_crtc, crtc_state);
13600 }
Sonika Jindald8106362015-04-10 14:37:28 +053013601 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013602 }
Sonika Jindald8106362015-04-10 14:37:28 +053013603
Matt Roperc59cb172014-12-01 15:40:16 -080013604 ret = drm_plane_helper_check_update(plane, crtc, fb,
13605 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013606 min_scale,
13607 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013608 can_position, true,
13609 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013610 if (ret)
13611 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013612
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013613 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013614 struct intel_plane_state *old_state =
13615 to_intel_plane_state(plane->state);
13616
Matt Roper32b7eee2014-12-24 07:59:06 -080013617 intel_crtc->atomic.wait_for_flips = true;
13618
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013619 /*
13620 * FBC does not work on some platforms for rotated
13621 * planes, so disable it when rotation is not 0 and
13622 * update it when rotation is set back to 0.
13623 *
13624 * FIXME: This is redundant with the fbc update done in
13625 * the primary plane enable function except that that
13626 * one is done too late. We eventually need to unify
13627 * this.
13628 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013629 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013630 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013631 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013632 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013633 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013634 }
13635
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013636 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013637 /*
13638 * BDW signals flip done immediately if the plane
13639 * is disabled, even if the plane enable is already
13640 * armed to occur at the next vblank :(
13641 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013642 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013643 intel_crtc->atomic.wait_vblank = true;
Maarten Lankhorstfb9d6cf2015-06-01 12:49:56 +020013644
13645 if (crtc_state && !needs_modeset(&crtc_state->base))
13646 intel_crtc->atomic.post_enable_primary = true;
Matt Roper32b7eee2014-12-24 07:59:06 -080013647 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013648
Maarten Lankhorstfb9d6cf2015-06-01 12:49:56 +020013649 if (!state->visible && old_state->visible &&
13650 crtc_state && !needs_modeset(&crtc_state->base))
13651 intel_crtc->atomic.pre_disable_primary = true;
13652
Matt Roper32b7eee2014-12-24 07:59:06 -080013653 intel_crtc->atomic.fb_bits |=
13654 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13655
13656 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013657
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013658 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013659 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013660 }
13661
Chandra Konduru6156a452015-04-27 13:48:39 -070013662 if (INTEL_INFO(dev)->gen >= 9) {
13663 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13664 to_intel_plane(plane), state, 0);
13665 if (ret)
13666 return ret;
13667 }
13668
Matt Roperc59cb172014-12-01 15:40:16 -080013669 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013670}
13671
Sonika Jindal48404c12014-08-22 14:06:04 +053013672static void
13673intel_commit_primary_plane(struct drm_plane *plane,
13674 struct intel_plane_state *state)
13675{
Matt Roper2b875c22014-12-01 15:40:13 -080013676 struct drm_crtc *crtc = state->base.crtc;
13677 struct drm_framebuffer *fb = state->base.fb;
13678 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013679 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013680 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013681 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013682
Matt Roperea2c67b2014-12-23 10:41:52 -080013683 crtc = crtc ? crtc : plane->crtc;
13684 intel_crtc = to_intel_crtc(crtc);
13685
Matt Ropercf4c7c12014-12-04 10:27:42 -080013686 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013687 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013688 crtc->y = src->y1 >> 16;
13689
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013690 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013691 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013692 /* FIXME: kill this fastboot hack */
13693 intel_update_pipe_size(intel_crtc);
13694
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013695 dev_priv->display.update_primary_plane(crtc, plane->fb,
13696 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013697 }
13698}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013699
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013700static void
13701intel_disable_primary_plane(struct drm_plane *plane,
13702 struct drm_crtc *crtc,
13703 bool force)
13704{
13705 struct drm_device *dev = plane->dev;
13706 struct drm_i915_private *dev_priv = dev->dev_private;
13707
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013708 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13709}
13710
Matt Roper32b7eee2014-12-24 07:59:06 -080013711static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13712{
13713 struct drm_device *dev = crtc->dev;
13714 struct drm_i915_private *dev_priv = dev->dev_private;
13715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5c2db182015-06-01 12:50:11 +020013716 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
Matt Roperea2c67b2014-12-23 10:41:52 -080013717 struct intel_plane *intel_plane;
13718 struct drm_plane *p;
13719 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013720
Matt Roperea2c67b2014-12-23 10:41:52 -080013721 /* Track fb's for any planes being disabled */
13722 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13723 intel_plane = to_intel_plane(p);
13724
13725 if (intel_crtc->atomic.disabled_planes &
13726 (1 << drm_plane_index(p))) {
13727 switch (p->type) {
13728 case DRM_PLANE_TYPE_PRIMARY:
13729 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13730 break;
13731 case DRM_PLANE_TYPE_CURSOR:
13732 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13733 break;
13734 case DRM_PLANE_TYPE_OVERLAY:
13735 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13736 break;
13737 }
13738
13739 mutex_lock(&dev->struct_mutex);
13740 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13741 mutex_unlock(&dev->struct_mutex);
13742 }
13743 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013744
Matt Roper32b7eee2014-12-24 07:59:06 -080013745 if (intel_crtc->atomic.wait_for_flips)
13746 intel_crtc_wait_for_pending_flips(crtc);
13747
13748 if (intel_crtc->atomic.disable_fbc)
13749 intel_fbc_disable(dev);
13750
13751 if (intel_crtc->atomic.pre_disable_primary)
13752 intel_pre_disable_primary(crtc);
13753
13754 if (intel_crtc->atomic.update_wm)
13755 intel_update_watermarks(crtc);
13756
13757 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013758
13759 /* Perform vblank evasion around commit operation */
Maarten Lankhorst5c2db182015-06-01 12:50:11 +020013760 if (crtc_state->active && !needs_modeset(crtc_state))
Matt Roperc34c9ee2014-12-23 10:41:50 -080013761 intel_crtc->atomic.evade =
13762 intel_pipe_update_start(intel_crtc,
13763 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013764}
13765
13766static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13767{
13768 struct drm_device *dev = crtc->dev;
13769 struct drm_i915_private *dev_priv = dev->dev_private;
13770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13771 struct drm_plane *p;
13772
Matt Roperc34c9ee2014-12-23 10:41:50 -080013773 if (intel_crtc->atomic.evade)
13774 intel_pipe_update_end(intel_crtc,
13775 intel_crtc->atomic.start_vbl_count);
13776
Matt Roper32b7eee2014-12-24 07:59:06 -080013777 intel_runtime_pm_put(dev_priv);
13778
Maarten Lankhorst8a8f7f42015-06-01 12:49:55 +020013779 if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
Matt Roper32b7eee2014-12-24 07:59:06 -080013780 intel_wait_for_vblank(dev, intel_crtc->pipe);
13781
13782 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13783
13784 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013785 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013786 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013787 mutex_unlock(&dev->struct_mutex);
13788 }
Matt Roper465c1202014-05-29 08:06:54 -070013789
Matt Roper32b7eee2014-12-24 07:59:06 -080013790 if (intel_crtc->atomic.post_enable_primary)
13791 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013792
Matt Roper32b7eee2014-12-24 07:59:06 -080013793 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13794 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13795 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13796 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013797
Matt Roper32b7eee2014-12-24 07:59:06 -080013798 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013799}
13800
Matt Ropercf4c7c12014-12-04 10:27:42 -080013801/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013802 * intel_plane_destroy - destroy a plane
13803 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013804 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013805 * Common destruction function for all types of planes (primary, cursor,
13806 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013807 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013808void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013809{
13810 struct intel_plane *intel_plane = to_intel_plane(plane);
13811 drm_plane_cleanup(plane);
13812 kfree(intel_plane);
13813}
13814
Matt Roper65a3fea2015-01-21 16:35:42 -080013815const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013816 .update_plane = drm_atomic_helper_update_plane,
13817 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013818 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013819 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013820 .atomic_get_property = intel_plane_atomic_get_property,
13821 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013822 .atomic_duplicate_state = intel_plane_duplicate_state,
13823 .atomic_destroy_state = intel_plane_destroy_state,
13824
Matt Roper465c1202014-05-29 08:06:54 -070013825};
13826
13827static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13828 int pipe)
13829{
13830 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013831 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013832 const uint32_t *intel_primary_formats;
13833 int num_formats;
13834
13835 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13836 if (primary == NULL)
13837 return NULL;
13838
Matt Roper8e7d6882015-01-21 16:35:41 -080013839 state = intel_create_plane_state(&primary->base);
13840 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013841 kfree(primary);
13842 return NULL;
13843 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013844 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013845
Matt Roper465c1202014-05-29 08:06:54 -070013846 primary->can_scale = false;
13847 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013848 if (INTEL_INFO(dev)->gen >= 9) {
13849 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013850 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013851 }
Matt Roper465c1202014-05-29 08:06:54 -070013852 primary->pipe = pipe;
13853 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013854 primary->check_plane = intel_check_primary_plane;
13855 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013856 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013857 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013858 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13859 primary->plane = !pipe;
13860
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013861 if (INTEL_INFO(dev)->gen >= 9) {
13862 intel_primary_formats = skl_primary_formats;
13863 num_formats = ARRAY_SIZE(skl_primary_formats);
13864 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013865 intel_primary_formats = i965_primary_formats;
13866 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013867 } else {
13868 intel_primary_formats = i8xx_primary_formats;
13869 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013870 }
13871
13872 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013873 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013874 intel_primary_formats, num_formats,
13875 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013876
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013877 if (INTEL_INFO(dev)->gen >= 4)
13878 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013879
Matt Roperea2c67b2014-12-23 10:41:52 -080013880 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13881
Matt Roper465c1202014-05-29 08:06:54 -070013882 return &primary->base;
13883}
13884
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013885void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13886{
13887 if (!dev->mode_config.rotation_property) {
13888 unsigned long flags = BIT(DRM_ROTATE_0) |
13889 BIT(DRM_ROTATE_180);
13890
13891 if (INTEL_INFO(dev)->gen >= 9)
13892 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13893
13894 dev->mode_config.rotation_property =
13895 drm_mode_create_rotation_property(dev, flags);
13896 }
13897 if (dev->mode_config.rotation_property)
13898 drm_object_attach_property(&plane->base.base,
13899 dev->mode_config.rotation_property,
13900 plane->base.state->rotation);
13901}
13902
Matt Roper3d7d6512014-06-10 08:28:13 -070013903static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013904intel_check_cursor_plane(struct drm_plane *plane,
13905 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013906{
Matt Roper2b875c22014-12-01 15:40:13 -080013907 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013908 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013909 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013910 struct drm_rect *dest = &state->dst;
13911 struct drm_rect *src = &state->src;
13912 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013913 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013914 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013915 unsigned stride;
13916 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013917
Matt Roperea2c67b2014-12-23 10:41:52 -080013918 crtc = crtc ? crtc : plane->crtc;
13919 intel_crtc = to_intel_crtc(crtc);
13920
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013921 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013922 src, dest, clip,
13923 DRM_PLANE_HELPER_NO_SCALING,
13924 DRM_PLANE_HELPER_NO_SCALING,
13925 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013926 if (ret)
13927 return ret;
13928
13929
13930 /* if we want to turn off the cursor ignore width and height */
13931 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013932 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013933
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013934 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013935 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13936 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13937 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013938 return -EINVAL;
13939 }
13940
Matt Roperea2c67b2014-12-23 10:41:52 -080013941 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13942 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013943 DRM_DEBUG_KMS("buffer is too small\n");
13944 return -ENOMEM;
13945 }
13946
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013947 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013948 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13949 ret = -EINVAL;
13950 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013951
Matt Roper32b7eee2014-12-24 07:59:06 -080013952finish:
13953 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013954 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013955 intel_crtc->atomic.update_wm = true;
13956
13957 intel_crtc->atomic.fb_bits |=
13958 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13959 }
13960
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013961 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013962}
13963
Matt Roperf4a2cf22014-12-01 15:40:12 -080013964static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013965intel_disable_cursor_plane(struct drm_plane *plane,
13966 struct drm_crtc *crtc,
13967 bool force)
13968{
13969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13970
13971 if (!force) {
13972 plane->fb = NULL;
13973 intel_crtc->cursor_bo = NULL;
13974 intel_crtc->cursor_addr = 0;
13975 }
13976
13977 intel_crtc_update_cursor(crtc, false);
13978}
13979
13980static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013981intel_commit_cursor_plane(struct drm_plane *plane,
13982 struct intel_plane_state *state)
13983{
Matt Roper2b875c22014-12-01 15:40:13 -080013984 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013985 struct drm_device *dev = plane->dev;
13986 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013987 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013988 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013989
Matt Roperea2c67b2014-12-23 10:41:52 -080013990 crtc = crtc ? crtc : plane->crtc;
13991 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013992
Matt Roperea2c67b2014-12-23 10:41:52 -080013993 plane->fb = state->base.fb;
13994 crtc->cursor_x = state->base.crtc_x;
13995 crtc->cursor_y = state->base.crtc_y;
13996
Gustavo Padovana912f122014-12-01 15:40:10 -080013997 if (intel_crtc->cursor_bo == obj)
13998 goto update;
13999
Matt Roperf4a2cf22014-12-01 15:40:12 -080014000 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014001 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014002 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014003 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014004 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014005 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014006
Gustavo Padovana912f122014-12-01 15:40:10 -080014007 intel_crtc->cursor_addr = addr;
14008 intel_crtc->cursor_bo = obj;
14009update:
Gustavo Padovana912f122014-12-01 15:40:10 -080014010
Matt Roper32b7eee2014-12-24 07:59:06 -080014011 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014012 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014013}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014014
Matt Roper3d7d6512014-06-10 08:28:13 -070014015static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14016 int pipe)
14017{
14018 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014019 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014020
14021 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14022 if (cursor == NULL)
14023 return NULL;
14024
Matt Roper8e7d6882015-01-21 16:35:41 -080014025 state = intel_create_plane_state(&cursor->base);
14026 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014027 kfree(cursor);
14028 return NULL;
14029 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014030 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014031
Matt Roper3d7d6512014-06-10 08:28:13 -070014032 cursor->can_scale = false;
14033 cursor->max_downscale = 1;
14034 cursor->pipe = pipe;
14035 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080014036 cursor->check_plane = intel_check_cursor_plane;
14037 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014038 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014039
14040 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014041 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014042 intel_cursor_formats,
14043 ARRAY_SIZE(intel_cursor_formats),
14044 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014045
14046 if (INTEL_INFO(dev)->gen >= 4) {
14047 if (!dev->mode_config.rotation_property)
14048 dev->mode_config.rotation_property =
14049 drm_mode_create_rotation_property(dev,
14050 BIT(DRM_ROTATE_0) |
14051 BIT(DRM_ROTATE_180));
14052 if (dev->mode_config.rotation_property)
14053 drm_object_attach_property(&cursor->base.base,
14054 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014055 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014056 }
14057
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014058 if (INTEL_INFO(dev)->gen >=9)
14059 state->scaler_id = -1;
14060
Matt Roperea2c67b2014-12-23 10:41:52 -080014061 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14062
Matt Roper3d7d6512014-06-10 08:28:13 -070014063 return &cursor->base;
14064}
14065
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014066static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14067 struct intel_crtc_state *crtc_state)
14068{
14069 int i;
14070 struct intel_scaler *intel_scaler;
14071 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14072
14073 for (i = 0; i < intel_crtc->num_scalers; i++) {
14074 intel_scaler = &scaler_state->scalers[i];
14075 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014076 intel_scaler->mode = PS_SCALER_MODE_DYN;
14077 }
14078
14079 scaler_state->scaler_id = -1;
14080}
14081
Hannes Ederb358d0a2008-12-18 21:18:47 +010014082static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014083{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014084 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014085 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014086 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014087 struct drm_plane *primary = NULL;
14088 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014089 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014090
Daniel Vetter955382f2013-09-19 14:05:45 +020014091 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014092 if (intel_crtc == NULL)
14093 return;
14094
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014095 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14096 if (!crtc_state)
14097 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014098 intel_crtc->config = crtc_state;
14099 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014100 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014101
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014102 /* initialize shared scalers */
14103 if (INTEL_INFO(dev)->gen >= 9) {
14104 if (pipe == PIPE_C)
14105 intel_crtc->num_scalers = 1;
14106 else
14107 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14108
14109 skl_init_scalers(dev, intel_crtc, crtc_state);
14110 }
14111
Matt Roper465c1202014-05-29 08:06:54 -070014112 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014113 if (!primary)
14114 goto fail;
14115
14116 cursor = intel_cursor_plane_create(dev, pipe);
14117 if (!cursor)
14118 goto fail;
14119
Matt Roper465c1202014-05-29 08:06:54 -070014120 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014121 cursor, &intel_crtc_funcs);
14122 if (ret)
14123 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014124
14125 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014126 for (i = 0; i < 256; i++) {
14127 intel_crtc->lut_r[i] = i;
14128 intel_crtc->lut_g[i] = i;
14129 intel_crtc->lut_b[i] = i;
14130 }
14131
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014132 /*
14133 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014134 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014135 */
Jesse Barnes80824002009-09-10 15:28:06 -070014136 intel_crtc->pipe = pipe;
14137 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014138 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014139 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014140 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014141 }
14142
Chris Wilson4b0e3332014-05-30 16:35:26 +030014143 intel_crtc->cursor_base = ~0;
14144 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014145 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014146
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014147 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14148 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14149 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14150 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14151
Jesse Barnes79e53942008-11-07 14:24:08 -080014152 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014153
14154 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014155 return;
14156
14157fail:
14158 if (primary)
14159 drm_plane_cleanup(primary);
14160 if (cursor)
14161 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014162 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014163 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014164}
14165
Jesse Barnes752aa882013-10-31 18:55:49 +020014166enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14167{
14168 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014169 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014170
Rob Clark51fd3712013-11-19 12:10:12 -050014171 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014172
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014173 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014174 return INVALID_PIPE;
14175
14176 return to_intel_crtc(encoder->crtc)->pipe;
14177}
14178
Carl Worth08d7b3d2009-04-29 14:43:54 -070014179int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014180 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014181{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014182 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014183 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014184 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014185
Rob Clark7707e652014-07-17 23:30:04 -040014186 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014187
Rob Clark7707e652014-07-17 23:30:04 -040014188 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014189 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014190 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014191 }
14192
Rob Clark7707e652014-07-17 23:30:04 -040014193 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014194 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014195
Daniel Vetterc05422d2009-08-11 16:05:30 +020014196 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014197}
14198
Daniel Vetter66a92782012-07-12 20:08:18 +020014199static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014200{
Daniel Vetter66a92782012-07-12 20:08:18 +020014201 struct drm_device *dev = encoder->base.dev;
14202 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014203 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014204 int entry = 0;
14205
Damien Lespiaub2784e12014-08-05 11:29:37 +010014206 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014207 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014208 index_mask |= (1 << entry);
14209
Jesse Barnes79e53942008-11-07 14:24:08 -080014210 entry++;
14211 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014212
Jesse Barnes79e53942008-11-07 14:24:08 -080014213 return index_mask;
14214}
14215
Chris Wilson4d302442010-12-14 19:21:29 +000014216static bool has_edp_a(struct drm_device *dev)
14217{
14218 struct drm_i915_private *dev_priv = dev->dev_private;
14219
14220 if (!IS_MOBILE(dev))
14221 return false;
14222
14223 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14224 return false;
14225
Damien Lespiaue3589902014-02-07 19:12:50 +000014226 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014227 return false;
14228
14229 return true;
14230}
14231
Jesse Barnes84b4e042014-06-25 08:24:29 -070014232static bool intel_crt_present(struct drm_device *dev)
14233{
14234 struct drm_i915_private *dev_priv = dev->dev_private;
14235
Damien Lespiau884497e2013-12-03 13:56:23 +000014236 if (INTEL_INFO(dev)->gen >= 9)
14237 return false;
14238
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014239 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014240 return false;
14241
14242 if (IS_CHERRYVIEW(dev))
14243 return false;
14244
14245 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14246 return false;
14247
14248 return true;
14249}
14250
Jesse Barnes79e53942008-11-07 14:24:08 -080014251static void intel_setup_outputs(struct drm_device *dev)
14252{
Eric Anholt725e30a2009-01-22 13:01:02 -080014253 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014254 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014255 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014256
Daniel Vetterc9093352013-06-06 22:22:47 +020014257 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014258
Jesse Barnes84b4e042014-06-25 08:24:29 -070014259 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014260 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014261
Vandana Kannanc776eb22014-08-19 12:05:01 +053014262 if (IS_BROXTON(dev)) {
14263 /*
14264 * FIXME: Broxton doesn't support port detection via the
14265 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14266 * detect the ports.
14267 */
14268 intel_ddi_init(dev, PORT_A);
14269 intel_ddi_init(dev, PORT_B);
14270 intel_ddi_init(dev, PORT_C);
14271 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014272 int found;
14273
Jesse Barnesde31fac2015-03-06 15:53:32 -080014274 /*
14275 * Haswell uses DDI functions to detect digital outputs.
14276 * On SKL pre-D0 the strap isn't connected, so we assume
14277 * it's there.
14278 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014279 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014280 /* WaIgnoreDDIAStrap: skl */
14281 if (found ||
14282 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014283 intel_ddi_init(dev, PORT_A);
14284
14285 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14286 * register */
14287 found = I915_READ(SFUSE_STRAP);
14288
14289 if (found & SFUSE_STRAP_DDIB_DETECTED)
14290 intel_ddi_init(dev, PORT_B);
14291 if (found & SFUSE_STRAP_DDIC_DETECTED)
14292 intel_ddi_init(dev, PORT_C);
14293 if (found & SFUSE_STRAP_DDID_DETECTED)
14294 intel_ddi_init(dev, PORT_D);
14295 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014296 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014297 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014298
14299 if (has_edp_a(dev))
14300 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014301
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014302 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014303 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014304 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014305 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014306 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014307 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014308 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014309 }
14310
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014311 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014312 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014313
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014314 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014315 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014316
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014317 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014318 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014319
Daniel Vetter270b3042012-10-27 15:52:05 +020014320 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014321 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014322 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014323 /*
14324 * The DP_DETECTED bit is the latched state of the DDC
14325 * SDA pin at boot. However since eDP doesn't require DDC
14326 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14327 * eDP ports may have been muxed to an alternate function.
14328 * Thus we can't rely on the DP_DETECTED bit alone to detect
14329 * eDP ports. Consult the VBT as well as DP_DETECTED to
14330 * detect eDP ports.
14331 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014332 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14333 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014334 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14335 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014336 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14337 intel_dp_is_edp(dev, PORT_B))
14338 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014339
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014340 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14341 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014342 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14343 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014344 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14345 intel_dp_is_edp(dev, PORT_C))
14346 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014347
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014348 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014349 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014350 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14351 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014352 /* eDP not supported on port D, so don't check VBT */
14353 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14354 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014355 }
14356
Jani Nikula3cfca972013-08-27 15:12:26 +030014357 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014358 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014359 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014360
Paulo Zanonie2debe92013-02-18 19:00:27 -030014361 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014362 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014363 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014364 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14365 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014366 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014367 }
Ma Ling27185ae2009-08-24 13:50:23 +080014368
Imre Deake7281ea2013-05-08 13:14:08 +030014369 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014370 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014371 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014372
14373 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014374
Paulo Zanonie2debe92013-02-18 19:00:27 -030014375 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014376 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014377 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014378 }
Ma Ling27185ae2009-08-24 13:50:23 +080014379
Paulo Zanonie2debe92013-02-18 19:00:27 -030014380 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014381
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014382 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14383 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014384 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014385 }
Imre Deake7281ea2013-05-08 13:14:08 +030014386 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014387 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014388 }
Ma Ling27185ae2009-08-24 13:50:23 +080014389
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014390 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014391 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014392 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014393 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014394 intel_dvo_init(dev);
14395
Zhenyu Wang103a1962009-11-27 11:44:36 +080014396 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014397 intel_tv_init(dev);
14398
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014399 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014400
Damien Lespiaub2784e12014-08-05 11:29:37 +010014401 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014402 encoder->base.possible_crtcs = encoder->crtc_mask;
14403 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014404 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014405 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014406
Paulo Zanonidde86e22012-12-01 12:04:25 -020014407 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014408
14409 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014410}
14411
14412static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14413{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014414 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014415 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014416
Daniel Vetteref2d6332014-02-10 18:00:38 +010014417 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014418 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014419 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014420 drm_gem_object_unreference(&intel_fb->obj->base);
14421 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014422 kfree(intel_fb);
14423}
14424
14425static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014426 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014427 unsigned int *handle)
14428{
14429 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014430 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014431
Chris Wilson05394f32010-11-08 19:18:58 +000014432 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014433}
14434
14435static const struct drm_framebuffer_funcs intel_fb_funcs = {
14436 .destroy = intel_user_framebuffer_destroy,
14437 .create_handle = intel_user_framebuffer_create_handle,
14438};
14439
Damien Lespiaub3218032015-02-27 11:15:18 +000014440static
14441u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14442 uint32_t pixel_format)
14443{
14444 u32 gen = INTEL_INFO(dev)->gen;
14445
14446 if (gen >= 9) {
14447 /* "The stride in bytes must not exceed the of the size of 8K
14448 * pixels and 32K bytes."
14449 */
14450 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14451 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14452 return 32*1024;
14453 } else if (gen >= 4) {
14454 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14455 return 16*1024;
14456 else
14457 return 32*1024;
14458 } else if (gen >= 3) {
14459 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14460 return 8*1024;
14461 else
14462 return 16*1024;
14463 } else {
14464 /* XXX DSPC is limited to 4k tiled */
14465 return 8*1024;
14466 }
14467}
14468
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014469static int intel_framebuffer_init(struct drm_device *dev,
14470 struct intel_framebuffer *intel_fb,
14471 struct drm_mode_fb_cmd2 *mode_cmd,
14472 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014473{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014474 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014475 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014476 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014477
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014478 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14479
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014480 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14481 /* Enforce that fb modifier and tiling mode match, but only for
14482 * X-tiled. This is needed for FBC. */
14483 if (!!(obj->tiling_mode == I915_TILING_X) !=
14484 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14485 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14486 return -EINVAL;
14487 }
14488 } else {
14489 if (obj->tiling_mode == I915_TILING_X)
14490 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14491 else if (obj->tiling_mode == I915_TILING_Y) {
14492 DRM_DEBUG("No Y tiling for legacy addfb\n");
14493 return -EINVAL;
14494 }
14495 }
14496
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014497 /* Passed in modifier sanity checking. */
14498 switch (mode_cmd->modifier[0]) {
14499 case I915_FORMAT_MOD_Y_TILED:
14500 case I915_FORMAT_MOD_Yf_TILED:
14501 if (INTEL_INFO(dev)->gen < 9) {
14502 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14503 mode_cmd->modifier[0]);
14504 return -EINVAL;
14505 }
14506 case DRM_FORMAT_MOD_NONE:
14507 case I915_FORMAT_MOD_X_TILED:
14508 break;
14509 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014510 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14511 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014512 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014513 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014514
Damien Lespiaub3218032015-02-27 11:15:18 +000014515 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14516 mode_cmd->pixel_format);
14517 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14518 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14519 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014520 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014521 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014522
Damien Lespiaub3218032015-02-27 11:15:18 +000014523 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14524 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014525 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014526 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14527 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014528 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014529 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014530 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014531 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014532
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014533 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014534 mode_cmd->pitches[0] != obj->stride) {
14535 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14536 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014537 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014538 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014539
Ville Syrjälä57779d02012-10-31 17:50:14 +020014540 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014541 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014542 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014543 case DRM_FORMAT_RGB565:
14544 case DRM_FORMAT_XRGB8888:
14545 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014546 break;
14547 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014548 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014549 DRM_DEBUG("unsupported pixel format: %s\n",
14550 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014551 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014552 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014553 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014554 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014555 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14556 DRM_DEBUG("unsupported pixel format: %s\n",
14557 drm_get_format_name(mode_cmd->pixel_format));
14558 return -EINVAL;
14559 }
14560 break;
14561 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014562 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014563 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014564 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014565 DRM_DEBUG("unsupported pixel format: %s\n",
14566 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014567 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014568 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014569 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014570 case DRM_FORMAT_ABGR2101010:
14571 if (!IS_VALLEYVIEW(dev)) {
14572 DRM_DEBUG("unsupported pixel format: %s\n",
14573 drm_get_format_name(mode_cmd->pixel_format));
14574 return -EINVAL;
14575 }
14576 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014577 case DRM_FORMAT_YUYV:
14578 case DRM_FORMAT_UYVY:
14579 case DRM_FORMAT_YVYU:
14580 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014581 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014582 DRM_DEBUG("unsupported pixel format: %s\n",
14583 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014584 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014585 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014586 break;
14587 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014588 DRM_DEBUG("unsupported pixel format: %s\n",
14589 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014590 return -EINVAL;
14591 }
14592
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014593 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14594 if (mode_cmd->offsets[0] != 0)
14595 return -EINVAL;
14596
Damien Lespiauec2c9812015-01-20 12:51:45 +000014597 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014598 mode_cmd->pixel_format,
14599 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014600 /* FIXME drm helper for size checks (especially planar formats)? */
14601 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14602 return -EINVAL;
14603
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014604 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14605 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014606 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014607
Jesse Barnes79e53942008-11-07 14:24:08 -080014608 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14609 if (ret) {
14610 DRM_ERROR("framebuffer init failed %d\n", ret);
14611 return ret;
14612 }
14613
Jesse Barnes79e53942008-11-07 14:24:08 -080014614 return 0;
14615}
14616
Jesse Barnes79e53942008-11-07 14:24:08 -080014617static struct drm_framebuffer *
14618intel_user_framebuffer_create(struct drm_device *dev,
14619 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014620 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014621{
Chris Wilson05394f32010-11-08 19:18:58 +000014622 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014623
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014624 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14625 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014626 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014627 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014628
Chris Wilsond2dff872011-04-19 08:36:26 +010014629 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014630}
14631
Daniel Vetter4520f532013-10-09 09:18:51 +020014632#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014633static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014634{
14635}
14636#endif
14637
Jesse Barnes79e53942008-11-07 14:24:08 -080014638static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014639 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014640 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014641 .atomic_check = intel_atomic_check,
14642 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014643 .atomic_state_alloc = intel_atomic_state_alloc,
14644 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014645};
14646
Jesse Barnese70236a2009-09-21 10:42:27 -070014647/* Set up chip specific display functions */
14648static void intel_init_display(struct drm_device *dev)
14649{
14650 struct drm_i915_private *dev_priv = dev->dev_private;
14651
Daniel Vetteree9300b2013-06-03 22:40:22 +020014652 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14653 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014654 else if (IS_CHERRYVIEW(dev))
14655 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014656 else if (IS_VALLEYVIEW(dev))
14657 dev_priv->display.find_dpll = vlv_find_best_dpll;
14658 else if (IS_PINEVIEW(dev))
14659 dev_priv->display.find_dpll = pnv_find_best_dpll;
14660 else
14661 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14662
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014663 if (INTEL_INFO(dev)->gen >= 9) {
14664 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014665 dev_priv->display.get_initial_plane_config =
14666 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014667 dev_priv->display.crtc_compute_clock =
14668 haswell_crtc_compute_clock;
14669 dev_priv->display.crtc_enable = haswell_crtc_enable;
14670 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014671 dev_priv->display.update_primary_plane =
14672 skylake_update_primary_plane;
14673 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014674 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014675 dev_priv->display.get_initial_plane_config =
14676 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014677 dev_priv->display.crtc_compute_clock =
14678 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014679 dev_priv->display.crtc_enable = haswell_crtc_enable;
14680 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014681 dev_priv->display.update_primary_plane =
14682 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014683 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014684 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014685 dev_priv->display.get_initial_plane_config =
14686 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014687 dev_priv->display.crtc_compute_clock =
14688 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014689 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14690 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014691 dev_priv->display.update_primary_plane =
14692 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014693 } else if (IS_VALLEYVIEW(dev)) {
14694 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014695 dev_priv->display.get_initial_plane_config =
14696 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014697 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014698 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14699 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014700 dev_priv->display.update_primary_plane =
14701 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014702 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014703 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014704 dev_priv->display.get_initial_plane_config =
14705 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014706 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014707 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14708 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014709 dev_priv->display.update_primary_plane =
14710 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014711 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014712
Jesse Barnese70236a2009-09-21 10:42:27 -070014713 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014714 if (IS_SKYLAKE(dev))
14715 dev_priv->display.get_display_clock_speed =
14716 skylake_get_display_clock_speed;
14717 else if (IS_BROADWELL(dev))
14718 dev_priv->display.get_display_clock_speed =
14719 broadwell_get_display_clock_speed;
14720 else if (IS_HASWELL(dev))
14721 dev_priv->display.get_display_clock_speed =
14722 haswell_get_display_clock_speed;
14723 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014724 dev_priv->display.get_display_clock_speed =
14725 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014726 else if (IS_GEN5(dev))
14727 dev_priv->display.get_display_clock_speed =
14728 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014729 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014730 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014731 dev_priv->display.get_display_clock_speed =
14732 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014733 else if (IS_GM45(dev))
14734 dev_priv->display.get_display_clock_speed =
14735 gm45_get_display_clock_speed;
14736 else if (IS_CRESTLINE(dev))
14737 dev_priv->display.get_display_clock_speed =
14738 i965gm_get_display_clock_speed;
14739 else if (IS_PINEVIEW(dev))
14740 dev_priv->display.get_display_clock_speed =
14741 pnv_get_display_clock_speed;
14742 else if (IS_G33(dev) || IS_G4X(dev))
14743 dev_priv->display.get_display_clock_speed =
14744 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014745 else if (IS_I915G(dev))
14746 dev_priv->display.get_display_clock_speed =
14747 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014748 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014749 dev_priv->display.get_display_clock_speed =
14750 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014751 else if (IS_PINEVIEW(dev))
14752 dev_priv->display.get_display_clock_speed =
14753 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014754 else if (IS_I915GM(dev))
14755 dev_priv->display.get_display_clock_speed =
14756 i915gm_get_display_clock_speed;
14757 else if (IS_I865G(dev))
14758 dev_priv->display.get_display_clock_speed =
14759 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014760 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014761 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014762 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014763 else { /* 830 */
14764 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014765 dev_priv->display.get_display_clock_speed =
14766 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014767 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014768
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014769 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014770 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014771 } else if (IS_GEN6(dev)) {
14772 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014773 } else if (IS_IVYBRIDGE(dev)) {
14774 /* FIXME: detect B0+ stepping and use auto training */
14775 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014776 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014777 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030014778 if (IS_BROADWELL(dev))
14779 dev_priv->display.modeset_global_resources =
14780 broadwell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014781 } else if (IS_VALLEYVIEW(dev)) {
14782 dev_priv->display.modeset_global_resources =
14783 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014784 } else if (IS_BROXTON(dev)) {
14785 dev_priv->display.modeset_global_resources =
14786 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014787 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014788
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014789 switch (INTEL_INFO(dev)->gen) {
14790 case 2:
14791 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14792 break;
14793
14794 case 3:
14795 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14796 break;
14797
14798 case 4:
14799 case 5:
14800 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14801 break;
14802
14803 case 6:
14804 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14805 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014806 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014807 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014808 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14809 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014810 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014811 /* Drop through - unsupported since execlist only. */
14812 default:
14813 /* Default just returns -ENODEV to indicate unsupported */
14814 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014815 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014816
14817 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014818
14819 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014820}
14821
Jesse Barnesb690e962010-07-19 13:53:12 -070014822/*
14823 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14824 * resume, or other times. This quirk makes sure that's the case for
14825 * affected systems.
14826 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014827static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014828{
14829 struct drm_i915_private *dev_priv = dev->dev_private;
14830
14831 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014832 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014833}
14834
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014835static void quirk_pipeb_force(struct drm_device *dev)
14836{
14837 struct drm_i915_private *dev_priv = dev->dev_private;
14838
14839 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14840 DRM_INFO("applying pipe b force quirk\n");
14841}
14842
Keith Packard435793d2011-07-12 14:56:22 -070014843/*
14844 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14845 */
14846static void quirk_ssc_force_disable(struct drm_device *dev)
14847{
14848 struct drm_i915_private *dev_priv = dev->dev_private;
14849 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014850 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014851}
14852
Carsten Emde4dca20e2012-03-15 15:56:26 +010014853/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014854 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14855 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014856 */
14857static void quirk_invert_brightness(struct drm_device *dev)
14858{
14859 struct drm_i915_private *dev_priv = dev->dev_private;
14860 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014861 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014862}
14863
Scot Doyle9c72cc62014-07-03 23:27:50 +000014864/* Some VBT's incorrectly indicate no backlight is present */
14865static void quirk_backlight_present(struct drm_device *dev)
14866{
14867 struct drm_i915_private *dev_priv = dev->dev_private;
14868 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14869 DRM_INFO("applying backlight present quirk\n");
14870}
14871
Jesse Barnesb690e962010-07-19 13:53:12 -070014872struct intel_quirk {
14873 int device;
14874 int subsystem_vendor;
14875 int subsystem_device;
14876 void (*hook)(struct drm_device *dev);
14877};
14878
Egbert Eich5f85f172012-10-14 15:46:38 +020014879/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14880struct intel_dmi_quirk {
14881 void (*hook)(struct drm_device *dev);
14882 const struct dmi_system_id (*dmi_id_list)[];
14883};
14884
14885static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14886{
14887 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14888 return 1;
14889}
14890
14891static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14892 {
14893 .dmi_id_list = &(const struct dmi_system_id[]) {
14894 {
14895 .callback = intel_dmi_reverse_brightness,
14896 .ident = "NCR Corporation",
14897 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14898 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14899 },
14900 },
14901 { } /* terminating entry */
14902 },
14903 .hook = quirk_invert_brightness,
14904 },
14905};
14906
Ben Widawskyc43b5632012-04-16 14:07:40 -070014907static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014908 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14909 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14910
Jesse Barnesb690e962010-07-19 13:53:12 -070014911 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14912 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14913
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014914 /* 830 needs to leave pipe A & dpll A up */
14915 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14916
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014917 /* 830 needs to leave pipe B & dpll B up */
14918 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14919
Keith Packard435793d2011-07-12 14:56:22 -070014920 /* Lenovo U160 cannot use SSC on LVDS */
14921 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014922
14923 /* Sony Vaio Y cannot use SSC on LVDS */
14924 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014925
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014926 /* Acer Aspire 5734Z must invert backlight brightness */
14927 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14928
14929 /* Acer/eMachines G725 */
14930 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14931
14932 /* Acer/eMachines e725 */
14933 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14934
14935 /* Acer/Packard Bell NCL20 */
14936 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14937
14938 /* Acer Aspire 4736Z */
14939 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014940
14941 /* Acer Aspire 5336 */
14942 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014943
14944 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14945 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014946
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014947 /* Acer C720 Chromebook (Core i3 4005U) */
14948 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14949
jens steinb2a96012014-10-28 20:25:53 +010014950 /* Apple Macbook 2,1 (Core 2 T7400) */
14951 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14952
Scot Doyled4967d82014-07-03 23:27:52 +000014953 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14954 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014955
14956 /* HP Chromebook 14 (Celeron 2955U) */
14957 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014958
14959 /* Dell Chromebook 11 */
14960 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014961};
14962
14963static void intel_init_quirks(struct drm_device *dev)
14964{
14965 struct pci_dev *d = dev->pdev;
14966 int i;
14967
14968 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14969 struct intel_quirk *q = &intel_quirks[i];
14970
14971 if (d->device == q->device &&
14972 (d->subsystem_vendor == q->subsystem_vendor ||
14973 q->subsystem_vendor == PCI_ANY_ID) &&
14974 (d->subsystem_device == q->subsystem_device ||
14975 q->subsystem_device == PCI_ANY_ID))
14976 q->hook(dev);
14977 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014978 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14979 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14980 intel_dmi_quirks[i].hook(dev);
14981 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014982}
14983
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014984/* Disable the VGA plane that we never use */
14985static void i915_disable_vga(struct drm_device *dev)
14986{
14987 struct drm_i915_private *dev_priv = dev->dev_private;
14988 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014989 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014990
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014991 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014992 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014993 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014994 sr1 = inb(VGA_SR_DATA);
14995 outb(sr1 | 1<<5, VGA_SR_DATA);
14996 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14997 udelay(300);
14998
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014999 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015000 POSTING_READ(vga_reg);
15001}
15002
Daniel Vetterf8175862012-04-10 15:50:11 +020015003void intel_modeset_init_hw(struct drm_device *dev)
15004{
Ville Syrjäläb6283052015-06-03 15:45:07 +030015005 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030015006 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015007 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015008 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015009}
15010
Jesse Barnes79e53942008-11-07 14:24:08 -080015011void intel_modeset_init(struct drm_device *dev)
15012{
Jesse Barnes652c3932009-08-17 13:31:43 -070015013 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015014 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015015 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015016 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015017
15018 drm_mode_config_init(dev);
15019
15020 dev->mode_config.min_width = 0;
15021 dev->mode_config.min_height = 0;
15022
Dave Airlie019d96c2011-09-29 16:20:42 +010015023 dev->mode_config.preferred_depth = 24;
15024 dev->mode_config.prefer_shadow = 1;
15025
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015026 dev->mode_config.allow_fb_modifiers = true;
15027
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015028 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015029
Jesse Barnesb690e962010-07-19 13:53:12 -070015030 intel_init_quirks(dev);
15031
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015032 intel_init_pm(dev);
15033
Ben Widawskye3c74752013-04-05 13:12:39 -070015034 if (INTEL_INFO(dev)->num_pipes == 0)
15035 return;
15036
Jesse Barnese70236a2009-09-21 10:42:27 -070015037 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015038 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015039
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015040 if (IS_GEN2(dev)) {
15041 dev->mode_config.max_width = 2048;
15042 dev->mode_config.max_height = 2048;
15043 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015044 dev->mode_config.max_width = 4096;
15045 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015046 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015047 dev->mode_config.max_width = 8192;
15048 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015049 }
Damien Lespiau068be562014-03-28 14:17:49 +000015050
Ville Syrjälädc41c152014-08-13 11:57:05 +030015051 if (IS_845G(dev) || IS_I865G(dev)) {
15052 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15053 dev->mode_config.cursor_height = 1023;
15054 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015055 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15056 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15057 } else {
15058 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15059 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15060 }
15061
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015062 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015063
Zhao Yakui28c97732009-10-09 11:39:41 +080015064 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015065 INTEL_INFO(dev)->num_pipes,
15066 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015067
Damien Lespiau055e3932014-08-18 13:49:10 +010015068 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015069 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015070 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015071 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015072 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015073 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015074 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015075 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015076 }
15077
Jesse Barnesf42bb702013-12-16 16:34:23 -080015078 intel_init_dpio(dev);
15079
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015080 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015081
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015082 /* Just disable it once at startup */
15083 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015084 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015085
15086 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015087 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015088
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015089 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015090 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015091 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015092
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015093 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080015094 if (!crtc->active)
15095 continue;
15096
Jesse Barnes46f297f2014-03-07 08:57:48 -080015097 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015098 * Note that reserving the BIOS fb up front prevents us
15099 * from stuffing other stolen allocations like the ring
15100 * on top. This prevents some ugliness at boot time, and
15101 * can even allow for smooth boot transitions if the BIOS
15102 * fb is large enough for the active pipe configuration.
15103 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015104 if (dev_priv->display.get_initial_plane_config) {
15105 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080015106 &crtc->plane_config);
15107 /*
15108 * If the fb is shared between multiple heads, we'll
15109 * just get the first one.
15110 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010015111 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015112 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080015113 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015114}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015115
Daniel Vetter7fad7982012-07-04 17:51:47 +020015116static void intel_enable_pipe_a(struct drm_device *dev)
15117{
15118 struct intel_connector *connector;
15119 struct drm_connector *crt = NULL;
15120 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015121 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015122
15123 /* We can't just switch on the pipe A, we need to set things up with a
15124 * proper mode and output configuration. As a gross hack, enable pipe A
15125 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015126 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015127 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15128 crt = &connector->base;
15129 break;
15130 }
15131 }
15132
15133 if (!crt)
15134 return;
15135
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015136 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015137 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015138}
15139
Daniel Vetterfa555832012-10-10 23:14:00 +020015140static bool
15141intel_check_plane_mapping(struct intel_crtc *crtc)
15142{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015143 struct drm_device *dev = crtc->base.dev;
15144 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015145 u32 reg, val;
15146
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015147 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015148 return true;
15149
15150 reg = DSPCNTR(!crtc->plane);
15151 val = I915_READ(reg);
15152
15153 if ((val & DISPLAY_PLANE_ENABLE) &&
15154 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15155 return false;
15156
15157 return true;
15158}
15159
Daniel Vetter24929352012-07-02 20:28:59 +020015160static void intel_sanitize_crtc(struct intel_crtc *crtc)
15161{
15162 struct drm_device *dev = crtc->base.dev;
15163 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015164 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020015165 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015166 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020015167
Daniel Vetter24929352012-07-02 20:28:59 +020015168 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015169 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015170 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15171
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015172 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015173 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015174 if (crtc->active) {
15175 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015176 drm_crtc_vblank_on(&crtc->base);
15177 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015178
Daniel Vetter24929352012-07-02 20:28:59 +020015179 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015180 * disable the crtc (and hence change the state) if it is wrong. Note
15181 * that gen4+ has a fixed plane -> pipe mapping. */
15182 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015183 bool plane;
15184
Daniel Vetter24929352012-07-02 20:28:59 +020015185 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15186 crtc->base.base.id);
15187
15188 /* Pipe has the wrong plane attached and the plane is active.
15189 * Temporarily change the plane mapping and disable everything
15190 * ... */
15191 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015192 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015193 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015194 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015195 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015196 }
Daniel Vetter24929352012-07-02 20:28:59 +020015197
Daniel Vetter7fad7982012-07-04 17:51:47 +020015198 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15199 crtc->pipe == PIPE_A && !crtc->active) {
15200 /* BIOS forgot to enable pipe A, this mostly happens after
15201 * resume. Force-enable the pipe to fix this, the update_dpms
15202 * call below we restore the pipe to the right state, but leave
15203 * the required bits on. */
15204 intel_enable_pipe_a(dev);
15205 }
15206
Daniel Vetter24929352012-07-02 20:28:59 +020015207 /* Adjust the state of the output pipe according to whether we
15208 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015209 enable = false;
15210 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15211 enable |= encoder->connectors_active;
Daniel Vetter24929352012-07-02 20:28:59 +020015212
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015213 if (!enable)
15214 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015215
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015216 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015217
15218 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015219 * functions or because of calls to intel_crtc_disable_noatomic,
15220 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015221 * pipe A quirk. */
15222 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15223 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015224 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015225 crtc->active ? "enabled" : "disabled");
15226
Matt Roper83d65732015-02-25 13:12:16 -080015227 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015228 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015229 crtc->base.enabled = crtc->active;
15230
15231 /* Because we only establish the connector -> encoder ->
15232 * crtc links if something is active, this means the
15233 * crtc is now deactivated. Break the links. connector
15234 * -> encoder links are only establish when things are
15235 * actually up, hence no need to break them. */
15236 WARN_ON(crtc->active);
15237
15238 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15239 WARN_ON(encoder->connectors_active);
15240 encoder->base.crtc = NULL;
15241 }
15242 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015243
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015244 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015245 /*
15246 * We start out with underrun reporting disabled to avoid races.
15247 * For correct bookkeeping mark this on active crtcs.
15248 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015249 * Also on gmch platforms we dont have any hardware bits to
15250 * disable the underrun reporting. Which means we need to start
15251 * out with underrun reporting disabled also on inactive pipes,
15252 * since otherwise we'll complain about the garbage we read when
15253 * e.g. coming up after runtime pm.
15254 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015255 * No protection against concurrent access is required - at
15256 * worst a fifo underrun happens which also sets this to false.
15257 */
15258 crtc->cpu_fifo_underrun_disabled = true;
15259 crtc->pch_fifo_underrun_disabled = true;
15260 }
Daniel Vetter24929352012-07-02 20:28:59 +020015261}
15262
15263static void intel_sanitize_encoder(struct intel_encoder *encoder)
15264{
15265 struct intel_connector *connector;
15266 struct drm_device *dev = encoder->base.dev;
15267
15268 /* We need to check both for a crtc link (meaning that the
15269 * encoder is active and trying to read from a pipe) and the
15270 * pipe itself being active. */
15271 bool has_active_crtc = encoder->base.crtc &&
15272 to_intel_crtc(encoder->base.crtc)->active;
15273
15274 if (encoder->connectors_active && !has_active_crtc) {
15275 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15276 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015277 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015278
15279 /* Connector is active, but has no active pipe. This is
15280 * fallout from our resume register restoring. Disable
15281 * the encoder manually again. */
15282 if (encoder->base.crtc) {
15283 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15284 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015285 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015286 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015287 if (encoder->post_disable)
15288 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015289 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015290 encoder->base.crtc = NULL;
15291 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015292
15293 /* Inconsistent output/port/pipe state happens presumably due to
15294 * a bug in one of the get_hw_state functions. Or someplace else
15295 * in our code, like the register restore mess on resume. Clamp
15296 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015297 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015298 if (connector->encoder != encoder)
15299 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015300 connector->base.dpms = DRM_MODE_DPMS_OFF;
15301 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015302 }
15303 }
15304 /* Enabled encoders without active connectors will be fixed in
15305 * the crtc fixup. */
15306}
15307
Imre Deak04098752014-02-18 00:02:16 +020015308void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015309{
15310 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015311 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015312
Imre Deak04098752014-02-18 00:02:16 +020015313 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15314 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15315 i915_disable_vga(dev);
15316 }
15317}
15318
15319void i915_redisable_vga(struct drm_device *dev)
15320{
15321 struct drm_i915_private *dev_priv = dev->dev_private;
15322
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015323 /* This function can be called both from intel_modeset_setup_hw_state or
15324 * at a very early point in our resume sequence, where the power well
15325 * structures are not yet restored. Since this function is at a very
15326 * paranoid "someone might have enabled VGA while we were not looking"
15327 * level, just check if the power well is enabled instead of trying to
15328 * follow the "don't touch the power well if we don't need it" policy
15329 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015330 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015331 return;
15332
Imre Deak04098752014-02-18 00:02:16 +020015333 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015334}
15335
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015336static bool primary_get_hw_state(struct intel_crtc *crtc)
15337{
15338 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15339
15340 if (!crtc->active)
15341 return false;
15342
15343 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15344}
15345
Daniel Vetter30e984d2013-06-05 13:34:17 +020015346static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015347{
15348 struct drm_i915_private *dev_priv = dev->dev_private;
15349 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015350 struct intel_crtc *crtc;
15351 struct intel_encoder *encoder;
15352 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015353 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015354
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015355 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015356 struct drm_plane *primary = crtc->base.primary;
15357 struct intel_plane_state *plane_state;
15358
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015359 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015360 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015361
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015362 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015363
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015364 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015365 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015366
Matt Roper83d65732015-02-25 13:12:16 -080015367 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015368 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015369 crtc->base.enabled = crtc->active;
Maarten Lankhorstb8b7fad2015-06-12 11:15:41 +020015370 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015371
15372 plane_state = to_intel_plane_state(primary->state);
15373 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015374
15375 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15376 crtc->base.base.id,
15377 crtc->active ? "enabled" : "disabled");
15378 }
15379
Daniel Vetter53589012013-06-05 13:34:16 +020015380 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15381 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15382
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015383 pll->on = pll->get_hw_state(dev_priv, pll,
15384 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015385 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015386 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015387 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015388 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015389 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015390 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015391 }
Daniel Vetter53589012013-06-05 13:34:16 +020015392 }
Daniel Vetter53589012013-06-05 13:34:16 +020015393
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015394 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015395 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015396
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015397 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015398 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015399 }
15400
Damien Lespiaub2784e12014-08-05 11:29:37 +010015401 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015402 pipe = 0;
15403
15404 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015405 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15406 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015407 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015408 } else {
15409 encoder->base.crtc = NULL;
15410 }
15411
15412 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015413 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015414 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015415 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015416 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015417 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015418 }
15419
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015420 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015421 if (connector->get_hw_state(connector)) {
15422 connector->base.dpms = DRM_MODE_DPMS_ON;
15423 connector->encoder->connectors_active = true;
15424 connector->base.encoder = &connector->encoder->base;
15425 } else {
15426 connector->base.dpms = DRM_MODE_DPMS_OFF;
15427 connector->base.encoder = NULL;
15428 }
15429 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15430 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015431 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015432 connector->base.encoder ? "enabled" : "disabled");
15433 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015434}
15435
15436/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15437 * and i915 state tracking structures. */
15438void intel_modeset_setup_hw_state(struct drm_device *dev,
15439 bool force_restore)
15440{
15441 struct drm_i915_private *dev_priv = dev->dev_private;
15442 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015443 struct intel_crtc *crtc;
15444 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015445 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015446
15447 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015448
Jesse Barnesbabea612013-06-26 18:57:38 +030015449 /*
15450 * Now that we have the config, copy it to each CRTC struct
15451 * Note that this could go away if we move to using crtc_config
15452 * checking everywhere.
15453 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015454 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015455 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015456 intel_mode_from_pipe_config(&crtc->base.mode,
15457 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015458 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15459 crtc->base.base.id);
15460 drm_mode_debug_printmodeline(&crtc->base.mode);
15461 }
15462 }
15463
Daniel Vetter24929352012-07-02 20:28:59 +020015464 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015465 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015466 intel_sanitize_encoder(encoder);
15467 }
15468
Damien Lespiau055e3932014-08-18 13:49:10 +010015469 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015470 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15471 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015472 intel_dump_pipe_config(crtc, crtc->config,
15473 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015474 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015475
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015476 intel_modeset_update_connector_atomic_state(dev);
15477
Daniel Vetter35c95372013-07-17 06:55:04 +020015478 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15479 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15480
15481 if (!pll->on || pll->active)
15482 continue;
15483
15484 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15485
15486 pll->disable(dev_priv, pll);
15487 pll->on = false;
15488 }
15489
Pradeep Bhat30789992014-11-04 17:06:45 +000015490 if (IS_GEN9(dev))
15491 skl_wm_get_hw_state(dev);
15492 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015493 ilk_wm_get_hw_state(dev);
15494
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015495 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015496 i915_redisable_vga(dev);
15497
Daniel Vetterf30da182013-04-11 20:22:50 +020015498 /*
15499 * We need to use raw interfaces for restoring state to avoid
15500 * checking (bogus) intermediate states.
15501 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015502 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015503 struct drm_crtc *crtc =
15504 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015505
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015506 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015507 }
15508 } else {
15509 intel_modeset_update_staged_output_state(dev);
15510 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015511
15512 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015513}
15514
15515void intel_modeset_gem_init(struct drm_device *dev)
15516{
Jesse Barnes92122782014-10-09 12:57:42 -070015517 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015518 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015519 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015520 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015521
Imre Deakae484342014-03-31 15:10:44 +030015522 mutex_lock(&dev->struct_mutex);
15523 intel_init_gt_powersave(dev);
15524 mutex_unlock(&dev->struct_mutex);
15525
Jesse Barnes92122782014-10-09 12:57:42 -070015526 /*
15527 * There may be no VBT; and if the BIOS enabled SSC we can
15528 * just keep using it to avoid unnecessary flicker. Whereas if the
15529 * BIOS isn't using it, don't assume it will work even if the VBT
15530 * indicates as much.
15531 */
15532 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15533 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15534 DREF_SSC1_ENABLE);
15535
Chris Wilson1833b132012-05-09 11:56:28 +010015536 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015537
15538 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015539
15540 /*
15541 * Make sure any fbs we allocated at startup are properly
15542 * pinned & fenced. When we do the allocation it's too early
15543 * for this.
15544 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015545 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015546 obj = intel_fb_obj(c->primary->fb);
15547 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015548 continue;
15549
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015550 mutex_lock(&dev->struct_mutex);
15551 ret = intel_pin_and_fence_fb_obj(c->primary,
15552 c->primary->fb,
15553 c->primary->state,
15554 NULL);
15555 mutex_unlock(&dev->struct_mutex);
15556 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015557 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15558 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015559 drm_framebuffer_unreference(c->primary->fb);
15560 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015561 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015562 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015563 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015564 }
15565 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015566
15567 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015568}
15569
Imre Deak4932e2c2014-02-11 17:12:48 +020015570void intel_connector_unregister(struct intel_connector *intel_connector)
15571{
15572 struct drm_connector *connector = &intel_connector->base;
15573
15574 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015575 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015576}
15577
Jesse Barnes79e53942008-11-07 14:24:08 -080015578void intel_modeset_cleanup(struct drm_device *dev)
15579{
Jesse Barnes652c3932009-08-17 13:31:43 -070015580 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015581 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015582
Imre Deak2eb52522014-11-19 15:30:05 +020015583 intel_disable_gt_powersave(dev);
15584
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015585 intel_backlight_unregister(dev);
15586
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015587 /*
15588 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015589 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015590 * experience fancy races otherwise.
15591 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015592 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015593
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015594 /*
15595 * Due to the hpd irq storm handling the hotplug work can re-arm the
15596 * poll handlers. Hence disable polling after hpd handling is shut down.
15597 */
Keith Packardf87ea762010-10-03 19:36:26 -070015598 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015599
Jesse Barnes652c3932009-08-17 13:31:43 -070015600 mutex_lock(&dev->struct_mutex);
15601
Jesse Barnes723bfd72010-10-07 16:01:13 -070015602 intel_unregister_dsm_handler();
15603
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015604 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015605
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015606 mutex_unlock(&dev->struct_mutex);
15607
Chris Wilson1630fe72011-07-08 12:22:42 +010015608 /* flush any delayed tasks or pending work */
15609 flush_scheduled_work();
15610
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015611 /* destroy the backlight and sysfs files before encoders/connectors */
15612 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015613 struct intel_connector *intel_connector;
15614
15615 intel_connector = to_intel_connector(connector);
15616 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015617 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015618
Jesse Barnes79e53942008-11-07 14:24:08 -080015619 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015620
15621 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015622
15623 mutex_lock(&dev->struct_mutex);
15624 intel_cleanup_gt_powersave(dev);
15625 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015626}
15627
Dave Airlie28d52042009-09-21 14:33:58 +100015628/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015629 * Return which encoder is currently attached for connector.
15630 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015631struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015632{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015633 return &intel_attached_encoder(connector)->base;
15634}
Jesse Barnes79e53942008-11-07 14:24:08 -080015635
Chris Wilsondf0e9242010-09-09 16:20:55 +010015636void intel_connector_attach_encoder(struct intel_connector *connector,
15637 struct intel_encoder *encoder)
15638{
15639 connector->encoder = encoder;
15640 drm_mode_connector_attach_encoder(&connector->base,
15641 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015642}
Dave Airlie28d52042009-09-21 14:33:58 +100015643
15644/*
15645 * set vga decode state - true == enable VGA decode
15646 */
15647int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15648{
15649 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015650 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015651 u16 gmch_ctrl;
15652
Chris Wilson75fa0412014-02-07 18:37:02 -020015653 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15654 DRM_ERROR("failed to read control word\n");
15655 return -EIO;
15656 }
15657
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015658 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15659 return 0;
15660
Dave Airlie28d52042009-09-21 14:33:58 +100015661 if (state)
15662 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15663 else
15664 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015665
15666 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15667 DRM_ERROR("failed to write control word\n");
15668 return -EIO;
15669 }
15670
Dave Airlie28d52042009-09-21 14:33:58 +100015671 return 0;
15672}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015673
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015674struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015675
15676 u32 power_well_driver;
15677
Chris Wilson63b66e52013-08-08 15:12:06 +020015678 int num_transcoders;
15679
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015680 struct intel_cursor_error_state {
15681 u32 control;
15682 u32 position;
15683 u32 base;
15684 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015685 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015686
15687 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015688 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015689 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015690 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015691 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015692
15693 struct intel_plane_error_state {
15694 u32 control;
15695 u32 stride;
15696 u32 size;
15697 u32 pos;
15698 u32 addr;
15699 u32 surface;
15700 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015701 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015702
15703 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015704 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015705 enum transcoder cpu_transcoder;
15706
15707 u32 conf;
15708
15709 u32 htotal;
15710 u32 hblank;
15711 u32 hsync;
15712 u32 vtotal;
15713 u32 vblank;
15714 u32 vsync;
15715 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015716};
15717
15718struct intel_display_error_state *
15719intel_display_capture_error_state(struct drm_device *dev)
15720{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015721 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015722 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015723 int transcoders[] = {
15724 TRANSCODER_A,
15725 TRANSCODER_B,
15726 TRANSCODER_C,
15727 TRANSCODER_EDP,
15728 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015729 int i;
15730
Chris Wilson63b66e52013-08-08 15:12:06 +020015731 if (INTEL_INFO(dev)->num_pipes == 0)
15732 return NULL;
15733
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015734 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015735 if (error == NULL)
15736 return NULL;
15737
Imre Deak190be112013-11-25 17:15:31 +020015738 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015739 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15740
Damien Lespiau055e3932014-08-18 13:49:10 +010015741 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015742 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015743 __intel_display_power_is_enabled(dev_priv,
15744 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015745 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015746 continue;
15747
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015748 error->cursor[i].control = I915_READ(CURCNTR(i));
15749 error->cursor[i].position = I915_READ(CURPOS(i));
15750 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015751
15752 error->plane[i].control = I915_READ(DSPCNTR(i));
15753 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015754 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015755 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015756 error->plane[i].pos = I915_READ(DSPPOS(i));
15757 }
Paulo Zanonica291362013-03-06 20:03:14 -030015758 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15759 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015760 if (INTEL_INFO(dev)->gen >= 4) {
15761 error->plane[i].surface = I915_READ(DSPSURF(i));
15762 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15763 }
15764
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015765 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015766
Sonika Jindal3abfce72014-07-21 15:23:43 +053015767 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015768 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015769 }
15770
15771 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15772 if (HAS_DDI(dev_priv->dev))
15773 error->num_transcoders++; /* Account for eDP. */
15774
15775 for (i = 0; i < error->num_transcoders; i++) {
15776 enum transcoder cpu_transcoder = transcoders[i];
15777
Imre Deakddf9c532013-11-27 22:02:02 +020015778 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015779 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015780 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015781 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015782 continue;
15783
Chris Wilson63b66e52013-08-08 15:12:06 +020015784 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15785
15786 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15787 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15788 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15789 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15790 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15791 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15792 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015793 }
15794
15795 return error;
15796}
15797
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015798#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15799
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015800void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015801intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015802 struct drm_device *dev,
15803 struct intel_display_error_state *error)
15804{
Damien Lespiau055e3932014-08-18 13:49:10 +010015805 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015806 int i;
15807
Chris Wilson63b66e52013-08-08 15:12:06 +020015808 if (!error)
15809 return;
15810
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015811 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015812 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015813 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015814 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015815 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015816 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015817 err_printf(m, " Power: %s\n",
15818 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015819 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015820 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015821
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015822 err_printf(m, "Plane [%d]:\n", i);
15823 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15824 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015825 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015826 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15827 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015828 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015829 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015830 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015831 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015832 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15833 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015834 }
15835
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015836 err_printf(m, "Cursor [%d]:\n", i);
15837 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15838 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15839 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015840 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015841
15842 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015843 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015844 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015845 err_printf(m, " Power: %s\n",
15846 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015847 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15848 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15849 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15850 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15851 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15852 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15853 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15854 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015855}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015856
15857void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15858{
15859 struct intel_crtc *crtc;
15860
15861 for_each_intel_crtc(dev, crtc) {
15862 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015863
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015864 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015865
15866 work = crtc->unpin_work;
15867
15868 if (work && work->event &&
15869 work->event->base.file_priv == file) {
15870 kfree(work->event);
15871 work->event = NULL;
15872 }
15873
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015874 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015875 }
15876}