blob: 449e07d29b3e74ca82bf04ea4571176e2cc66f0a [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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002928static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002929{
2930 struct drm_device *dev;
2931 struct drm_i915_private *dev_priv;
2932 struct intel_crtc_scaler_state *scaler_state;
2933 int i;
2934
Chandra Kondurua1b22782015-04-07 15:28:45 -07002935 dev = intel_crtc->base.dev;
2936 dev_priv = dev->dev_private;
2937 scaler_state = &intel_crtc->config->scaler_state;
2938
2939 /* loop through and disable scalers that aren't in use */
2940 for (i = 0; i < intel_crtc->num_scalers; i++) {
2941 if (!scaler_state->scalers[i].in_use) {
2942 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2943 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2944 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2945 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2946 intel_crtc->base.base.id, intel_crtc->pipe, i);
2947 }
2948 }
2949}
2950
Chandra Konduru6156a452015-04-27 13:48:39 -07002951u32 skl_plane_ctl_format(uint32_t pixel_format)
2952{
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002954 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002961 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 /*
2963 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2964 * to be already pre-multiplied. We need to add a knob (or a different
2965 * DRM_FORMAT) for user-space to configure that.
2966 */
2967 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002968 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002974 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002976 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002982 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002984 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002985 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002986 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002988
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002989 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002990}
2991
2992u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2993{
Chandra Konduru6156a452015-04-27 13:48:39 -07002994 switch (fb_modifier) {
2995 case DRM_FORMAT_MOD_NONE:
2996 break;
2997 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002998 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002999 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003000 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003002 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003003 default:
3004 MISSING_CASE(fb_modifier);
3005 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003006
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003007 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003008}
3009
3010u32 skl_plane_ctl_rotation(unsigned int rotation)
3011{
Chandra Konduru6156a452015-04-27 13:48:39 -07003012 switch (rotation) {
3013 case BIT(DRM_ROTATE_0):
3014 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303015 /*
3016 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3017 * while i915 HW rotation is clockwise, thats why this swapping.
3018 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003019 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303020 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003021 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003022 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003023 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303024 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003025 default:
3026 MISSING_CASE(rotation);
3027 }
3028
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003029 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003030}
3031
Damien Lespiau70d21f02013-07-03 21:06:04 +01003032static void skylake_update_primary_plane(struct drm_crtc *crtc,
3033 struct drm_framebuffer *fb,
3034 int x, int y)
3035{
3036 struct drm_device *dev = crtc->dev;
3037 struct drm_i915_private *dev_priv = dev->dev_private;
3038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003039 struct drm_plane *plane = crtc->primary;
3040 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003041 struct drm_i915_gem_object *obj;
3042 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303043 u32 plane_ctl, stride_div, stride;
3044 u32 tile_height, plane_offset, plane_size;
3045 unsigned int rotation;
3046 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003047 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003048 struct intel_crtc_state *crtc_state = intel_crtc->config;
3049 struct intel_plane_state *plane_state;
3050 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3051 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3052 int scaler_id = -1;
3053
Chandra Konduru6156a452015-04-27 13:48:39 -07003054 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003056 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003057 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3058 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3059 POSTING_READ(PLANE_CTL(pipe, 0));
3060 return;
3061 }
3062
3063 plane_ctl = PLANE_CTL_ENABLE |
3064 PLANE_CTL_PIPE_GAMMA_ENABLE |
3065 PLANE_CTL_PIPE_CSC_ENABLE;
3066
Chandra Konduru6156a452015-04-27 13:48:39 -07003067 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3068 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003069 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303070
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303071 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003072 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003073
Damien Lespiaub3218032015-02-27 11:15:18 +00003074 obj = intel_fb_obj(fb);
3075 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3076 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303077 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3078
Chandra Konduru6156a452015-04-27 13:48:39 -07003079 /*
3080 * FIXME: intel_plane_state->src, dst aren't set when transitional
3081 * update_plane helpers are called from legacy paths.
3082 * Once full atomic crtc is available, below check can be avoided.
3083 */
3084 if (drm_rect_width(&plane_state->src)) {
3085 scaler_id = plane_state->scaler_id;
3086 src_x = plane_state->src.x1 >> 16;
3087 src_y = plane_state->src.y1 >> 16;
3088 src_w = drm_rect_width(&plane_state->src) >> 16;
3089 src_h = drm_rect_height(&plane_state->src) >> 16;
3090 dst_x = plane_state->dst.x1;
3091 dst_y = plane_state->dst.y1;
3092 dst_w = drm_rect_width(&plane_state->dst);
3093 dst_h = drm_rect_height(&plane_state->dst);
3094
3095 WARN_ON(x != src_x || y != src_y);
3096 } else {
3097 src_w = intel_crtc->config->pipe_src_w;
3098 src_h = intel_crtc->config->pipe_src_h;
3099 }
3100
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303101 if (intel_rotation_90_or_270(rotation)) {
3102 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003103 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303104 fb->modifier[0]);
3105 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003106 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303107 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003108 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303109 } else {
3110 stride = fb->pitches[0] / stride_div;
3111 x_offset = x;
3112 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003113 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303114 }
3115 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003116
Damien Lespiau70d21f02013-07-03 21:06:04 +01003117 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303118 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3119 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3120 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003121
3122 if (scaler_id >= 0) {
3123 uint32_t ps_ctrl = 0;
3124
3125 WARN_ON(!dst_w || !dst_h);
3126 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3127 crtc_state->scaler_state.scalers[scaler_id].mode;
3128 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3129 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3130 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3131 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3132 I915_WRITE(PLANE_POS(pipe, 0), 0);
3133 } else {
3134 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3135 }
3136
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003137 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003138
3139 POSTING_READ(PLANE_SURF(pipe, 0));
3140}
3141
Jesse Barnes17638cd2011-06-24 12:19:23 -07003142/* Assume fb object is pinned & idle & fenced and just update base pointers */
3143static int
3144intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3145 int x, int y, enum mode_set_atomic state)
3146{
3147 struct drm_device *dev = crtc->dev;
3148 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003149
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003150 if (dev_priv->display.disable_fbc)
3151 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003152
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003153 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3154
3155 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003156}
3157
Ville Syrjälä75147472014-11-24 18:28:11 +02003158static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003159{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003160 struct drm_crtc *crtc;
3161
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003162 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3164 enum plane plane = intel_crtc->plane;
3165
3166 intel_prepare_page_flip(dev, plane);
3167 intel_finish_page_flip_plane(dev, plane);
3168 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003169}
3170
3171static void intel_update_primary_planes(struct drm_device *dev)
3172{
3173 struct drm_i915_private *dev_priv = dev->dev_private;
3174 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003175
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003176 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3178
Rob Clark51fd3712013-11-19 12:10:12 -05003179 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003180 /*
3181 * FIXME: Once we have proper support for primary planes (and
3182 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003183 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003184 */
Matt Roperf4510a22014-04-01 15:22:40 -07003185 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003186 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003187 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003188 crtc->x,
3189 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003190 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003191 }
3192}
3193
Ville Syrjälä75147472014-11-24 18:28:11 +02003194void intel_prepare_reset(struct drm_device *dev)
3195{
3196 /* no reset support for gen2 */
3197 if (IS_GEN2(dev))
3198 return;
3199
3200 /* reset doesn't touch the display */
3201 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3202 return;
3203
3204 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003205 /*
3206 * Disabling the crtcs gracefully seems nicer. Also the
3207 * g33 docs say we should at least disable all the planes.
3208 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003209 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003210}
3211
3212void intel_finish_reset(struct drm_device *dev)
3213{
3214 struct drm_i915_private *dev_priv = to_i915(dev);
3215
3216 /*
3217 * Flips in the rings will be nuked by the reset,
3218 * so complete all pending flips so that user space
3219 * will get its events and not get stuck.
3220 */
3221 intel_complete_page_flips(dev);
3222
3223 /* no reset support for gen2 */
3224 if (IS_GEN2(dev))
3225 return;
3226
3227 /* reset doesn't touch the display */
3228 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3229 /*
3230 * Flips in the rings have been nuked by the reset,
3231 * so update the base address of all primary
3232 * planes to the the last fb to make sure we're
3233 * showing the correct fb after a reset.
3234 */
3235 intel_update_primary_planes(dev);
3236 return;
3237 }
3238
3239 /*
3240 * The display has been reset as well,
3241 * so need a full re-initialization.
3242 */
3243 intel_runtime_pm_disable_interrupts(dev_priv);
3244 intel_runtime_pm_enable_interrupts(dev_priv);
3245
3246 intel_modeset_init_hw(dev);
3247
3248 spin_lock_irq(&dev_priv->irq_lock);
3249 if (dev_priv->display.hpd_irq_setup)
3250 dev_priv->display.hpd_irq_setup(dev);
3251 spin_unlock_irq(&dev_priv->irq_lock);
3252
3253 intel_modeset_setup_hw_state(dev, true);
3254
3255 intel_hpd_init(dev_priv);
3256
3257 drm_modeset_unlock_all(dev);
3258}
3259
Chris Wilson2e2f3512015-04-27 13:41:14 +01003260static void
Chris Wilson14667a42012-04-03 17:58:35 +01003261intel_finish_fb(struct drm_framebuffer *old_fb)
3262{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003263 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003264 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003265 bool was_interruptible = dev_priv->mm.interruptible;
3266 int ret;
3267
Chris Wilson14667a42012-04-03 17:58:35 +01003268 /* Big Hammer, we also need to ensure that any pending
3269 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3270 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003271 * framebuffer. Note that we rely on userspace rendering
3272 * into the buffer attached to the pipe they are waiting
3273 * on. If not, userspace generates a GPU hang with IPEHR
3274 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003275 *
3276 * This should only fail upon a hung GPU, in which case we
3277 * can safely continue.
3278 */
3279 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003280 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003281 dev_priv->mm.interruptible = was_interruptible;
3282
Chris Wilson2e2f3512015-04-27 13:41:14 +01003283 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003284}
3285
Chris Wilson7d5e3792014-03-04 13:15:08 +00003286static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3287{
3288 struct drm_device *dev = crtc->dev;
3289 struct drm_i915_private *dev_priv = dev->dev_private;
3290 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003291 bool pending;
3292
3293 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3294 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3295 return false;
3296
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003297 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003298 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003299 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003300
3301 return pending;
3302}
3303
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003304static void intel_update_pipe_size(struct intel_crtc *crtc)
3305{
3306 struct drm_device *dev = crtc->base.dev;
3307 struct drm_i915_private *dev_priv = dev->dev_private;
3308 const struct drm_display_mode *adjusted_mode;
3309
3310 if (!i915.fastboot)
3311 return;
3312
3313 /*
3314 * Update pipe size and adjust fitter if needed: the reason for this is
3315 * that in compute_mode_changes we check the native mode (not the pfit
3316 * mode) to see if we can flip rather than do a full mode set. In the
3317 * fastboot case, we'll flip, but if we don't update the pipesrc and
3318 * pfit state, we'll end up with a big fb scanned out into the wrong
3319 * sized surface.
3320 *
3321 * To fix this properly, we need to hoist the checks up into
3322 * compute_mode_changes (or above), check the actual pfit state and
3323 * whether the platform allows pfit disable with pipe active, and only
3324 * then update the pipesrc and pfit state, even on the flip path.
3325 */
3326
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003327 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003328
3329 I915_WRITE(PIPESRC(crtc->pipe),
3330 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3331 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003332 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003333 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3334 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003335 I915_WRITE(PF_CTL(crtc->pipe), 0);
3336 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3337 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3338 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003339 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3340 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003341}
3342
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003343static void intel_fdi_normal_train(struct drm_crtc *crtc)
3344{
3345 struct drm_device *dev = crtc->dev;
3346 struct drm_i915_private *dev_priv = dev->dev_private;
3347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3348 int pipe = intel_crtc->pipe;
3349 u32 reg, temp;
3350
3351 /* enable normal train */
3352 reg = FDI_TX_CTL(pipe);
3353 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003354 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003355 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3356 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003357 } else {
3358 temp &= ~FDI_LINK_TRAIN_NONE;
3359 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003360 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003361 I915_WRITE(reg, temp);
3362
3363 reg = FDI_RX_CTL(pipe);
3364 temp = I915_READ(reg);
3365 if (HAS_PCH_CPT(dev)) {
3366 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3367 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3368 } else {
3369 temp &= ~FDI_LINK_TRAIN_NONE;
3370 temp |= FDI_LINK_TRAIN_NONE;
3371 }
3372 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3373
3374 /* wait one idle pattern time */
3375 POSTING_READ(reg);
3376 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003377
3378 /* IVB wants error correction enabled */
3379 if (IS_IVYBRIDGE(dev))
3380 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3381 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003382}
3383
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003384/* The FDI link training functions for ILK/Ibexpeak. */
3385static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3386{
3387 struct drm_device *dev = crtc->dev;
3388 struct drm_i915_private *dev_priv = dev->dev_private;
3389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3390 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003393 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003394 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003395
Adam Jacksone1a44742010-06-25 15:32:14 -04003396 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3397 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003398 reg = FDI_RX_IMR(pipe);
3399 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003400 temp &= ~FDI_RX_SYMBOL_LOCK;
3401 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 I915_WRITE(reg, temp);
3403 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003404 udelay(150);
3405
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003407 reg = FDI_TX_CTL(pipe);
3408 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003409 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003410 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003411 temp &= ~FDI_LINK_TRAIN_NONE;
3412 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003414
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 reg = FDI_RX_CTL(pipe);
3416 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417 temp &= ~FDI_LINK_TRAIN_NONE;
3418 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3420
3421 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422 udelay(150);
3423
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003424 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003425 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3426 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3427 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003428
Chris Wilson5eddb702010-09-11 13:48:45 +01003429 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003430 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3433
3434 if ((temp & FDI_RX_BIT_LOCK)) {
3435 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 break;
3438 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003440 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003441 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442
3443 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 reg = FDI_TX_CTL(pipe);
3445 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003446 temp &= ~FDI_LINK_TRAIN_NONE;
3447 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003448 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 reg = FDI_RX_CTL(pipe);
3451 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003452 temp &= ~FDI_LINK_TRAIN_NONE;
3453 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003454 I915_WRITE(reg, temp);
3455
3456 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003457 udelay(150);
3458
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003460 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003461 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003462 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3463
3464 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003466 DRM_DEBUG_KMS("FDI train 2 done.\n");
3467 break;
3468 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003470 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003472
3473 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003474
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003475}
3476
Akshay Joshi0206e352011-08-16 15:34:10 -04003477static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003478 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3479 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3480 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3481 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3482};
3483
3484/* The FDI link training functions for SNB/Cougarpoint. */
3485static void gen6_fdi_link_train(struct drm_crtc *crtc)
3486{
3487 struct drm_device *dev = crtc->dev;
3488 struct drm_i915_private *dev_priv = dev->dev_private;
3489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3490 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003491 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003492
Adam Jacksone1a44742010-06-25 15:32:14 -04003493 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3494 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003495 reg = FDI_RX_IMR(pipe);
3496 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003497 temp &= ~FDI_RX_SYMBOL_LOCK;
3498 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003499 I915_WRITE(reg, temp);
3500
3501 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003502 udelay(150);
3503
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003504 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003505 reg = FDI_TX_CTL(pipe);
3506 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003507 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003508 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003509 temp &= ~FDI_LINK_TRAIN_NONE;
3510 temp |= FDI_LINK_TRAIN_PATTERN_1;
3511 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3512 /* SNB-B */
3513 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003514 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515
Daniel Vetterd74cf322012-10-26 10:58:13 +02003516 I915_WRITE(FDI_RX_MISC(pipe),
3517 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3518
Chris Wilson5eddb702010-09-11 13:48:45 +01003519 reg = FDI_RX_CTL(pipe);
3520 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521 if (HAS_PCH_CPT(dev)) {
3522 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3523 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3524 } else {
3525 temp &= ~FDI_LINK_TRAIN_NONE;
3526 temp |= FDI_LINK_TRAIN_PATTERN_1;
3527 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003528 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3529
3530 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531 udelay(150);
3532
Akshay Joshi0206e352011-08-16 15:34:10 -04003533 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003534 reg = FDI_TX_CTL(pipe);
3535 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003536 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3537 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003538 I915_WRITE(reg, temp);
3539
3540 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003541 udelay(500);
3542
Sean Paulfa37d392012-03-02 12:53:39 -05003543 for (retry = 0; retry < 5; retry++) {
3544 reg = FDI_RX_IIR(pipe);
3545 temp = I915_READ(reg);
3546 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3547 if (temp & FDI_RX_BIT_LOCK) {
3548 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3549 DRM_DEBUG_KMS("FDI train 1 done.\n");
3550 break;
3551 }
3552 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003553 }
Sean Paulfa37d392012-03-02 12:53:39 -05003554 if (retry < 5)
3555 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003556 }
3557 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003558 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003559
3560 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 reg = FDI_TX_CTL(pipe);
3562 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003563 temp &= ~FDI_LINK_TRAIN_NONE;
3564 temp |= FDI_LINK_TRAIN_PATTERN_2;
3565 if (IS_GEN6(dev)) {
3566 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3567 /* SNB-B */
3568 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3569 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003570 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003571
Chris Wilson5eddb702010-09-11 13:48:45 +01003572 reg = FDI_RX_CTL(pipe);
3573 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 if (HAS_PCH_CPT(dev)) {
3575 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3576 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3577 } else {
3578 temp &= ~FDI_LINK_TRAIN_NONE;
3579 temp |= FDI_LINK_TRAIN_PATTERN_2;
3580 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 I915_WRITE(reg, temp);
3582
3583 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584 udelay(150);
3585
Akshay Joshi0206e352011-08-16 15:34:10 -04003586 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003587 reg = FDI_TX_CTL(pipe);
3588 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003589 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3590 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003591 I915_WRITE(reg, temp);
3592
3593 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003594 udelay(500);
3595
Sean Paulfa37d392012-03-02 12:53:39 -05003596 for (retry = 0; retry < 5; retry++) {
3597 reg = FDI_RX_IIR(pipe);
3598 temp = I915_READ(reg);
3599 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3600 if (temp & FDI_RX_SYMBOL_LOCK) {
3601 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3602 DRM_DEBUG_KMS("FDI train 2 done.\n");
3603 break;
3604 }
3605 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003606 }
Sean Paulfa37d392012-03-02 12:53:39 -05003607 if (retry < 5)
3608 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003609 }
3610 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003611 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003612
3613 DRM_DEBUG_KMS("FDI train done.\n");
3614}
3615
Jesse Barnes357555c2011-04-28 15:09:55 -07003616/* Manual link training for Ivy Bridge A0 parts */
3617static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3618{
3619 struct drm_device *dev = crtc->dev;
3620 struct drm_i915_private *dev_priv = dev->dev_private;
3621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3622 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003623 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003624
3625 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3626 for train result */
3627 reg = FDI_RX_IMR(pipe);
3628 temp = I915_READ(reg);
3629 temp &= ~FDI_RX_SYMBOL_LOCK;
3630 temp &= ~FDI_RX_BIT_LOCK;
3631 I915_WRITE(reg, temp);
3632
3633 POSTING_READ(reg);
3634 udelay(150);
3635
Daniel Vetter01a415f2012-10-27 15:58:40 +02003636 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3637 I915_READ(FDI_RX_IIR(pipe)));
3638
Jesse Barnes139ccd32013-08-19 11:04:55 -07003639 /* Try each vswing and preemphasis setting twice before moving on */
3640 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3641 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003642 reg = FDI_TX_CTL(pipe);
3643 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003644 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3645 temp &= ~FDI_TX_ENABLE;
3646 I915_WRITE(reg, temp);
3647
3648 reg = FDI_RX_CTL(pipe);
3649 temp = I915_READ(reg);
3650 temp &= ~FDI_LINK_TRAIN_AUTO;
3651 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3652 temp &= ~FDI_RX_ENABLE;
3653 I915_WRITE(reg, temp);
3654
3655 /* enable CPU FDI TX and PCH FDI RX */
3656 reg = FDI_TX_CTL(pipe);
3657 temp = I915_READ(reg);
3658 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003659 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003660 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003661 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003662 temp |= snb_b_fdi_train_param[j/2];
3663 temp |= FDI_COMPOSITE_SYNC;
3664 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3665
3666 I915_WRITE(FDI_RX_MISC(pipe),
3667 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3668
3669 reg = FDI_RX_CTL(pipe);
3670 temp = I915_READ(reg);
3671 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3672 temp |= FDI_COMPOSITE_SYNC;
3673 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3674
3675 POSTING_READ(reg);
3676 udelay(1); /* should be 0.5us */
3677
3678 for (i = 0; i < 4; i++) {
3679 reg = FDI_RX_IIR(pipe);
3680 temp = I915_READ(reg);
3681 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3682
3683 if (temp & FDI_RX_BIT_LOCK ||
3684 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3685 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3686 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3687 i);
3688 break;
3689 }
3690 udelay(1); /* should be 0.5us */
3691 }
3692 if (i == 4) {
3693 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3694 continue;
3695 }
3696
3697 /* Train 2 */
3698 reg = FDI_TX_CTL(pipe);
3699 temp = I915_READ(reg);
3700 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3701 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3702 I915_WRITE(reg, temp);
3703
3704 reg = FDI_RX_CTL(pipe);
3705 temp = I915_READ(reg);
3706 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3707 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003708 I915_WRITE(reg, temp);
3709
3710 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003711 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003712
Jesse Barnes139ccd32013-08-19 11:04:55 -07003713 for (i = 0; i < 4; i++) {
3714 reg = FDI_RX_IIR(pipe);
3715 temp = I915_READ(reg);
3716 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003717
Jesse Barnes139ccd32013-08-19 11:04:55 -07003718 if (temp & FDI_RX_SYMBOL_LOCK ||
3719 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3720 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3721 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3722 i);
3723 goto train_done;
3724 }
3725 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003726 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003727 if (i == 4)
3728 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003729 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003730
Jesse Barnes139ccd32013-08-19 11:04:55 -07003731train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003732 DRM_DEBUG_KMS("FDI train done.\n");
3733}
3734
Daniel Vetter88cefb62012-08-12 19:27:14 +02003735static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003736{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003737 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003738 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003739 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003740 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003741
Jesse Barnesc64e3112010-09-10 11:27:03 -07003742
Jesse Barnes0e23b992010-09-10 11:10:00 -07003743 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003744 reg = FDI_RX_CTL(pipe);
3745 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003746 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003747 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003748 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003749 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3750
3751 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003752 udelay(200);
3753
3754 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003755 temp = I915_READ(reg);
3756 I915_WRITE(reg, temp | FDI_PCDCLK);
3757
3758 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003759 udelay(200);
3760
Paulo Zanoni20749732012-11-23 15:30:38 -02003761 /* Enable CPU FDI TX PLL, always on for Ironlake */
3762 reg = FDI_TX_CTL(pipe);
3763 temp = I915_READ(reg);
3764 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3765 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003766
Paulo Zanoni20749732012-11-23 15:30:38 -02003767 POSTING_READ(reg);
3768 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003769 }
3770}
3771
Daniel Vetter88cefb62012-08-12 19:27:14 +02003772static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3773{
3774 struct drm_device *dev = intel_crtc->base.dev;
3775 struct drm_i915_private *dev_priv = dev->dev_private;
3776 int pipe = intel_crtc->pipe;
3777 u32 reg, temp;
3778
3779 /* Switch from PCDclk to Rawclk */
3780 reg = FDI_RX_CTL(pipe);
3781 temp = I915_READ(reg);
3782 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3783
3784 /* Disable CPU FDI TX PLL */
3785 reg = FDI_TX_CTL(pipe);
3786 temp = I915_READ(reg);
3787 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3788
3789 POSTING_READ(reg);
3790 udelay(100);
3791
3792 reg = FDI_RX_CTL(pipe);
3793 temp = I915_READ(reg);
3794 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3795
3796 /* Wait for the clocks to turn off. */
3797 POSTING_READ(reg);
3798 udelay(100);
3799}
3800
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003801static void ironlake_fdi_disable(struct drm_crtc *crtc)
3802{
3803 struct drm_device *dev = crtc->dev;
3804 struct drm_i915_private *dev_priv = dev->dev_private;
3805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3806 int pipe = intel_crtc->pipe;
3807 u32 reg, temp;
3808
3809 /* disable CPU FDI tx and PCH FDI rx */
3810 reg = FDI_TX_CTL(pipe);
3811 temp = I915_READ(reg);
3812 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3813 POSTING_READ(reg);
3814
3815 reg = FDI_RX_CTL(pipe);
3816 temp = I915_READ(reg);
3817 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003818 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003819 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3820
3821 POSTING_READ(reg);
3822 udelay(100);
3823
3824 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003825 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003826 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003827
3828 /* still set train pattern 1 */
3829 reg = FDI_TX_CTL(pipe);
3830 temp = I915_READ(reg);
3831 temp &= ~FDI_LINK_TRAIN_NONE;
3832 temp |= FDI_LINK_TRAIN_PATTERN_1;
3833 I915_WRITE(reg, temp);
3834
3835 reg = FDI_RX_CTL(pipe);
3836 temp = I915_READ(reg);
3837 if (HAS_PCH_CPT(dev)) {
3838 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3839 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3840 } else {
3841 temp &= ~FDI_LINK_TRAIN_NONE;
3842 temp |= FDI_LINK_TRAIN_PATTERN_1;
3843 }
3844 /* BPC in FDI rx is consistent with that in PIPECONF */
3845 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003846 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003847 I915_WRITE(reg, temp);
3848
3849 POSTING_READ(reg);
3850 udelay(100);
3851}
3852
Chris Wilson5dce5b932014-01-20 10:17:36 +00003853bool intel_has_pending_fb_unpin(struct drm_device *dev)
3854{
3855 struct intel_crtc *crtc;
3856
3857 /* Note that we don't need to be called with mode_config.lock here
3858 * as our list of CRTC objects is static for the lifetime of the
3859 * device and so cannot disappear as we iterate. Similarly, we can
3860 * happily treat the predicates as racy, atomic checks as userspace
3861 * cannot claim and pin a new fb without at least acquring the
3862 * struct_mutex and so serialising with us.
3863 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003864 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003865 if (atomic_read(&crtc->unpin_work_count) == 0)
3866 continue;
3867
3868 if (crtc->unpin_work)
3869 intel_wait_for_vblank(dev, crtc->pipe);
3870
3871 return true;
3872 }
3873
3874 return false;
3875}
3876
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003877static void page_flip_completed(struct intel_crtc *intel_crtc)
3878{
3879 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3880 struct intel_unpin_work *work = intel_crtc->unpin_work;
3881
3882 /* ensure that the unpin work is consistent wrt ->pending. */
3883 smp_rmb();
3884 intel_crtc->unpin_work = NULL;
3885
3886 if (work->event)
3887 drm_send_vblank_event(intel_crtc->base.dev,
3888 intel_crtc->pipe,
3889 work->event);
3890
3891 drm_crtc_vblank_put(&intel_crtc->base);
3892
3893 wake_up_all(&dev_priv->pending_flip_queue);
3894 queue_work(dev_priv->wq, &work->work);
3895
3896 trace_i915_flip_complete(intel_crtc->plane,
3897 work->pending_flip_obj);
3898}
3899
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003900void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003901{
Chris Wilson0f911282012-04-17 10:05:38 +01003902 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003903 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003904
Daniel Vetter2c10d572012-12-20 21:24:07 +01003905 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003906 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3907 !intel_crtc_has_pending_flip(crtc),
3908 60*HZ) == 0)) {
3909 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003910
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003911 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003912 if (intel_crtc->unpin_work) {
3913 WARN_ONCE(1, "Removing stuck page flip\n");
3914 page_flip_completed(intel_crtc);
3915 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003916 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003917 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003918
Chris Wilson975d5682014-08-20 13:13:34 +01003919 if (crtc->primary->fb) {
3920 mutex_lock(&dev->struct_mutex);
3921 intel_finish_fb(crtc->primary->fb);
3922 mutex_unlock(&dev->struct_mutex);
3923 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003924}
3925
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003926/* Program iCLKIP clock to the desired frequency */
3927static void lpt_program_iclkip(struct drm_crtc *crtc)
3928{
3929 struct drm_device *dev = crtc->dev;
3930 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003931 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3933 u32 temp;
3934
Ville Syrjäläa5805162015-05-26 20:42:30 +03003935 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003936
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003937 /* It is necessary to ungate the pixclk gate prior to programming
3938 * the divisors, and gate it back when it is done.
3939 */
3940 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3941
3942 /* Disable SSCCTL */
3943 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003944 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3945 SBI_SSCCTL_DISABLE,
3946 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947
3948 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003949 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003950 auxdiv = 1;
3951 divsel = 0x41;
3952 phaseinc = 0x20;
3953 } else {
3954 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003955 * but the adjusted_mode->crtc_clock in in KHz. To get the
3956 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003957 * convert the virtual clock precision to KHz here for higher
3958 * precision.
3959 */
3960 u32 iclk_virtual_root_freq = 172800 * 1000;
3961 u32 iclk_pi_range = 64;
3962 u32 desired_divisor, msb_divisor_value, pi_value;
3963
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003964 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003965 msb_divisor_value = desired_divisor / iclk_pi_range;
3966 pi_value = desired_divisor % iclk_pi_range;
3967
3968 auxdiv = 0;
3969 divsel = msb_divisor_value - 2;
3970 phaseinc = pi_value;
3971 }
3972
3973 /* This should not happen with any sane values */
3974 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3975 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3976 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3977 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3978
3979 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 +03003980 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003981 auxdiv,
3982 divsel,
3983 phasedir,
3984 phaseinc);
3985
3986 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003987 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003988 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3989 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3990 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3991 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3992 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3993 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003994 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003995
3996 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003997 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003998 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3999 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004000 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004001
4002 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004003 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004004 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004005 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004006
4007 /* Wait for initialization time */
4008 udelay(24);
4009
4010 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004011
Ville Syrjäläa5805162015-05-26 20:42:30 +03004012 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004013}
4014
Daniel Vetter275f01b22013-05-03 11:49:47 +02004015static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4016 enum pipe pch_transcoder)
4017{
4018 struct drm_device *dev = crtc->base.dev;
4019 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004020 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004021
4022 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4023 I915_READ(HTOTAL(cpu_transcoder)));
4024 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4025 I915_READ(HBLANK(cpu_transcoder)));
4026 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4027 I915_READ(HSYNC(cpu_transcoder)));
4028
4029 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4030 I915_READ(VTOTAL(cpu_transcoder)));
4031 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4032 I915_READ(VBLANK(cpu_transcoder)));
4033 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4034 I915_READ(VSYNC(cpu_transcoder)));
4035 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4036 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4037}
4038
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004039static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004040{
4041 struct drm_i915_private *dev_priv = dev->dev_private;
4042 uint32_t temp;
4043
4044 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004045 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004046 return;
4047
4048 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4049 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4050
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004051 temp &= ~FDI_BC_BIFURCATION_SELECT;
4052 if (enable)
4053 temp |= FDI_BC_BIFURCATION_SELECT;
4054
4055 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004056 I915_WRITE(SOUTH_CHICKEN1, temp);
4057 POSTING_READ(SOUTH_CHICKEN1);
4058}
4059
4060static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4061{
4062 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004063
4064 switch (intel_crtc->pipe) {
4065 case PIPE_A:
4066 break;
4067 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004068 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004069 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004070 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004071 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004072
4073 break;
4074 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004075 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004076
4077 break;
4078 default:
4079 BUG();
4080 }
4081}
4082
Jesse Barnesf67a5592011-01-05 10:31:48 -08004083/*
4084 * Enable PCH resources required for PCH ports:
4085 * - PCH PLLs
4086 * - FDI training & RX/TX
4087 * - update transcoder timings
4088 * - DP transcoding bits
4089 * - transcoder
4090 */
4091static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004092{
4093 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004094 struct drm_i915_private *dev_priv = dev->dev_private;
4095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4096 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004097 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004098
Daniel Vetterab9412b2013-05-03 11:49:46 +02004099 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004100
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004101 if (IS_IVYBRIDGE(dev))
4102 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4103
Daniel Vettercd986ab2012-10-26 10:58:12 +02004104 /* Write the TU size bits before fdi link training, so that error
4105 * detection works. */
4106 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4107 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4108
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004109 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004110 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004111
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004112 /* We need to program the right clock selection before writing the pixel
4113 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004114 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004115 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004116
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004117 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004118 temp |= TRANS_DPLL_ENABLE(pipe);
4119 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004120 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004121 temp |= sel;
4122 else
4123 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004124 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004125 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004127 /* XXX: pch pll's can be enabled any time before we enable the PCH
4128 * transcoder, and we actually should do this to not upset any PCH
4129 * transcoder that already use the clock when we share it.
4130 *
4131 * Note that enable_shared_dpll tries to do the right thing, but
4132 * get_shared_dpll unconditionally resets the pll - we need that to have
4133 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004134 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004135
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004136 /* set transcoder timing, panel must allow it */
4137 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004138 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004139
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004140 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004141
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004142 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004143 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004144 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004145 reg = TRANS_DP_CTL(pipe);
4146 temp = I915_READ(reg);
4147 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004148 TRANS_DP_SYNC_MASK |
4149 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004150 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004151 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004152
4153 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004154 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004155 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004156 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157
4158 switch (intel_trans_dp_port_sel(crtc)) {
4159 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004160 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004161 break;
4162 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004163 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004164 break;
4165 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004166 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004167 break;
4168 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004169 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004170 }
4171
Chris Wilson5eddb702010-09-11 13:48:45 +01004172 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004173 }
4174
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004175 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004176}
4177
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004178static void lpt_pch_enable(struct drm_crtc *crtc)
4179{
4180 struct drm_device *dev = crtc->dev;
4181 struct drm_i915_private *dev_priv = dev->dev_private;
4182 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004183 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004184
Daniel Vetterab9412b2013-05-03 11:49:46 +02004185 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004186
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004187 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004188
Paulo Zanoni0540e482012-10-31 18:12:40 -02004189 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004190 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004191
Paulo Zanoni937bb612012-10-31 18:12:47 -02004192 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004193}
4194
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004195struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4196 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004197{
Daniel Vettere2b78262013-06-07 23:10:03 +02004198 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004199 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004200 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004201 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004202
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004203 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4204
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004205 if (HAS_PCH_IBX(dev_priv->dev)) {
4206 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004207 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004208 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004209
Daniel Vetter46edb022013-06-05 13:34:12 +02004210 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4211 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004212
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004213 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004214
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004215 goto found;
4216 }
4217
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304218 if (IS_BROXTON(dev_priv->dev)) {
4219 /* PLL is attached to port in bxt */
4220 struct intel_encoder *encoder;
4221 struct intel_digital_port *intel_dig_port;
4222
4223 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4224 if (WARN_ON(!encoder))
4225 return NULL;
4226
4227 intel_dig_port = enc_to_dig_port(&encoder->base);
4228 /* 1:1 mapping between ports and PLLs */
4229 i = (enum intel_dpll_id)intel_dig_port->port;
4230 pll = &dev_priv->shared_dplls[i];
4231 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4232 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004233 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304234
4235 goto found;
4236 }
4237
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004238 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4239 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004240
4241 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004242 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004243 continue;
4244
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004245 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004246 &shared_dpll[i].hw_state,
4247 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004248 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004249 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004250 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004251 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004252 goto found;
4253 }
4254 }
4255
4256 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004257 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4258 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004259 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004260 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4261 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004262 goto found;
4263 }
4264 }
4265
4266 return NULL;
4267
4268found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004269 if (shared_dpll[i].crtc_mask == 0)
4270 shared_dpll[i].hw_state =
4271 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004272
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004273 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004274 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4275 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004276
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004277 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004278
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004279 return pll;
4280}
4281
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004282static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004283{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004284 struct drm_i915_private *dev_priv = to_i915(state->dev);
4285 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004286 struct intel_shared_dpll *pll;
4287 enum intel_dpll_id i;
4288
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004289 if (!to_intel_atomic_state(state)->dpll_set)
4290 return;
4291
4292 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004293 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4294 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004295 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004296 }
4297}
4298
Daniel Vettera1520312013-05-03 11:49:50 +02004299static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004300{
4301 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004302 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004303 u32 temp;
4304
4305 temp = I915_READ(dslreg);
4306 udelay(500);
4307 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004308 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004309 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004310 }
4311}
4312
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004313static int
4314skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4315 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4316 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004317{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004318 struct intel_crtc_scaler_state *scaler_state =
4319 &crtc_state->scaler_state;
4320 struct intel_crtc *intel_crtc =
4321 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004322 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004323
4324 need_scaling = intel_rotation_90_or_270(rotation) ?
4325 (src_h != dst_w || src_w != dst_h):
4326 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004327
4328 /*
4329 * if plane is being disabled or scaler is no more required or force detach
4330 * - free scaler binded to this plane/crtc
4331 * - in order to do this, update crtc->scaler_usage
4332 *
4333 * Here scaler state in crtc_state is set free so that
4334 * scaler can be assigned to other user. Actual register
4335 * update to free the scaler is done in plane/panel-fit programming.
4336 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4337 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004338 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004339 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004340 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004341 scaler_state->scalers[*scaler_id].in_use = 0;
4342
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004343 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4344 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4345 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004346 scaler_state->scaler_users);
4347 *scaler_id = -1;
4348 }
4349 return 0;
4350 }
4351
4352 /* range checks */
4353 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4354 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4355
4356 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4357 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004358 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004359 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004360 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004361 return -EINVAL;
4362 }
4363
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004364 /* mark this plane as a scaler user in crtc_state */
4365 scaler_state->scaler_users |= (1 << scaler_user);
4366 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4367 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4368 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4369 scaler_state->scaler_users);
4370
4371 return 0;
4372}
4373
4374/**
4375 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4376 *
4377 * @state: crtc's scaler state
4378 * @force_detach: whether to forcibly disable scaler
4379 *
4380 * Return
4381 * 0 - scaler_usage updated successfully
4382 * error - requested scaling cannot be supported or other error condition
4383 */
4384int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
4385{
4386 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4387 struct drm_display_mode *adjusted_mode =
4388 &state->base.adjusted_mode;
4389
4390 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4391 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4392
4393 return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
4394 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4395 state->pipe_src_w, state->pipe_src_h,
4396 adjusted_mode->hdisplay, adjusted_mode->hdisplay);
4397}
4398
4399/**
4400 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4401 *
4402 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004403 * @plane_state: atomic plane state to update
4404 *
4405 * Return
4406 * 0 - scaler_usage updated successfully
4407 * error - requested scaling cannot be supported or other error condition
4408 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004409static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4410 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004411{
4412
4413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004414 struct intel_plane *intel_plane =
4415 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004416 struct drm_framebuffer *fb = plane_state->base.fb;
4417 int ret;
4418
4419 bool force_detach = !fb || !plane_state->visible;
4420
4421 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4422 intel_plane->base.base.id, intel_crtc->pipe,
4423 drm_plane_index(&intel_plane->base));
4424
4425 ret = skl_update_scaler(crtc_state, force_detach,
4426 drm_plane_index(&intel_plane->base),
4427 &plane_state->scaler_id,
4428 plane_state->base.rotation,
4429 drm_rect_width(&plane_state->src) >> 16,
4430 drm_rect_height(&plane_state->src) >> 16,
4431 drm_rect_width(&plane_state->dst),
4432 drm_rect_height(&plane_state->dst));
4433
4434 if (ret || plane_state->scaler_id < 0)
4435 return ret;
4436
Chandra Kondurua1b22782015-04-07 15:28:45 -07004437 /* check colorkey */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004438 if (WARN_ON(intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4439 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
4440 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004441 return -EINVAL;
4442 }
4443
4444 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004445 switch (fb->pixel_format) {
4446 case DRM_FORMAT_RGB565:
4447 case DRM_FORMAT_XBGR8888:
4448 case DRM_FORMAT_XRGB8888:
4449 case DRM_FORMAT_ABGR8888:
4450 case DRM_FORMAT_ARGB8888:
4451 case DRM_FORMAT_XRGB2101010:
4452 case DRM_FORMAT_XBGR2101010:
4453 case DRM_FORMAT_YUYV:
4454 case DRM_FORMAT_YVYU:
4455 case DRM_FORMAT_UYVY:
4456 case DRM_FORMAT_VYUY:
4457 break;
4458 default:
4459 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4460 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4461 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004462 }
4463
Chandra Kondurua1b22782015-04-07 15:28:45 -07004464 return 0;
4465}
4466
4467static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004468{
4469 struct drm_device *dev = crtc->base.dev;
4470 struct drm_i915_private *dev_priv = dev->dev_private;
4471 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004472 struct intel_crtc_scaler_state *scaler_state =
4473 &crtc->config->scaler_state;
4474
4475 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4476
4477 /* To update pfit, first update scaler state */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004478 skl_update_scaler_crtc(crtc->config, !enable);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004479 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4480 skl_detach_scalers(crtc);
4481 if (!enable)
4482 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004483
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004484 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004485 int id;
4486
4487 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4488 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4489 return;
4490 }
4491
4492 id = scaler_state->scaler_id;
4493 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4494 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4495 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4496 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4497
4498 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004499 }
4500}
4501
Jesse Barnesb074cec2013-04-25 12:55:02 -07004502static void ironlake_pfit_enable(struct intel_crtc *crtc)
4503{
4504 struct drm_device *dev = crtc->base.dev;
4505 struct drm_i915_private *dev_priv = dev->dev_private;
4506 int pipe = crtc->pipe;
4507
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004508 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004509 /* Force use of hard-coded filter coefficients
4510 * as some pre-programmed values are broken,
4511 * e.g. x201.
4512 */
4513 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4514 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4515 PF_PIPE_SEL_IVB(pipe));
4516 else
4517 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004518 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4519 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004520 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004521}
4522
Matt Roper4a3b8762014-12-23 10:41:51 -08004523static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004524{
4525 struct drm_device *dev = crtc->dev;
4526 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004527 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004528 struct intel_plane *intel_plane;
4529
Matt Roperaf2b6532014-04-01 15:22:32 -07004530 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4531 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004532 if (intel_plane->pipe == pipe)
4533 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004534 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004535}
4536
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004537void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004538{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004539 struct drm_device *dev = crtc->base.dev;
4540 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004541
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004542 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004543 return;
4544
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004545 /* We can only enable IPS after we enable a plane and wait for a vblank */
4546 intel_wait_for_vblank(dev, crtc->pipe);
4547
Paulo Zanonid77e4532013-09-24 13:52:55 -03004548 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004549 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004550 mutex_lock(&dev_priv->rps.hw_lock);
4551 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4552 mutex_unlock(&dev_priv->rps.hw_lock);
4553 /* Quoting Art Runyan: "its not safe to expect any particular
4554 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004555 * mailbox." Moreover, the mailbox may return a bogus state,
4556 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004557 */
4558 } else {
4559 I915_WRITE(IPS_CTL, IPS_ENABLE);
4560 /* The bit only becomes 1 in the next vblank, so this wait here
4561 * is essentially intel_wait_for_vblank. If we don't have this
4562 * and don't wait for vblanks until the end of crtc_enable, then
4563 * the HW state readout code will complain that the expected
4564 * IPS_CTL value is not the one we read. */
4565 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4566 DRM_ERROR("Timed out waiting for IPS enable\n");
4567 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004568}
4569
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004570void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004571{
4572 struct drm_device *dev = crtc->base.dev;
4573 struct drm_i915_private *dev_priv = dev->dev_private;
4574
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004575 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004576 return;
4577
4578 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004579 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004580 mutex_lock(&dev_priv->rps.hw_lock);
4581 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4582 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004583 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4584 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4585 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004586 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004587 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004588 POSTING_READ(IPS_CTL);
4589 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004590
4591 /* We need to wait for a vblank before we can disable the plane. */
4592 intel_wait_for_vblank(dev, crtc->pipe);
4593}
4594
4595/** Loads the palette/gamma unit for the CRTC with the prepared values */
4596static void intel_crtc_load_lut(struct drm_crtc *crtc)
4597{
4598 struct drm_device *dev = crtc->dev;
4599 struct drm_i915_private *dev_priv = dev->dev_private;
4600 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4601 enum pipe pipe = intel_crtc->pipe;
4602 int palreg = PALETTE(pipe);
4603 int i;
4604 bool reenable_ips = false;
4605
4606 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004607 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004608 return;
4609
Imre Deak50360402015-01-16 00:55:16 -08004610 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004611 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004612 assert_dsi_pll_enabled(dev_priv);
4613 else
4614 assert_pll_enabled(dev_priv, pipe);
4615 }
4616
4617 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304618 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004619 palreg = LGC_PALETTE(pipe);
4620
4621 /* Workaround : Do not read or write the pipe palette/gamma data while
4622 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4623 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004624 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004625 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4626 GAMMA_MODE_MODE_SPLIT)) {
4627 hsw_disable_ips(intel_crtc);
4628 reenable_ips = true;
4629 }
4630
4631 for (i = 0; i < 256; i++) {
4632 I915_WRITE(palreg + 4 * i,
4633 (intel_crtc->lut_r[i] << 16) |
4634 (intel_crtc->lut_g[i] << 8) |
4635 intel_crtc->lut_b[i]);
4636 }
4637
4638 if (reenable_ips)
4639 hsw_enable_ips(intel_crtc);
4640}
4641
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004642static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004643{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004644 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004645 struct drm_device *dev = intel_crtc->base.dev;
4646 struct drm_i915_private *dev_priv = dev->dev_private;
4647
4648 mutex_lock(&dev->struct_mutex);
4649 dev_priv->mm.interruptible = false;
4650 (void) intel_overlay_switch_off(intel_crtc->overlay);
4651 dev_priv->mm.interruptible = true;
4652 mutex_unlock(&dev->struct_mutex);
4653 }
4654
4655 /* Let userspace switch the overlay on again. In most cases userspace
4656 * has to recompute where to put it anyway.
4657 */
4658}
4659
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004660/**
4661 * intel_post_enable_primary - Perform operations after enabling primary plane
4662 * @crtc: the CRTC whose primary plane was just enabled
4663 *
4664 * Performs potentially sleeping operations that must be done after the primary
4665 * plane is enabled, such as updating FBC and IPS. Note that this may be
4666 * called due to an explicit primary plane update, or due to an implicit
4667 * re-enable that is caused when a sprite plane is updated to no longer
4668 * completely hide the primary plane.
4669 */
4670static void
4671intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004672{
4673 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004674 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004675 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4676 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004677
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004678 /*
4679 * BDW signals flip done immediately if the plane
4680 * is disabled, even if the plane enable is already
4681 * armed to occur at the next vblank :(
4682 */
4683 if (IS_BROADWELL(dev))
4684 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004685
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004686 /*
4687 * FIXME IPS should be fine as long as one plane is
4688 * enabled, but in practice it seems to have problems
4689 * when going from primary only to sprite only and vice
4690 * versa.
4691 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004692 hsw_enable_ips(intel_crtc);
4693
4694 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004695 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004696 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004697
4698 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004699 * Gen2 reports pipe underruns whenever all planes are disabled.
4700 * So don't enable underrun reporting before at least some planes
4701 * are enabled.
4702 * FIXME: Need to fix the logic to work when we turn off all planes
4703 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004704 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004705 if (IS_GEN2(dev))
4706 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4707
4708 /* Underruns don't raise interrupts, so check manually. */
4709 if (HAS_GMCH_DISPLAY(dev))
4710 i9xx_check_fifo_underruns(dev_priv);
4711}
4712
4713/**
4714 * intel_pre_disable_primary - Perform operations before disabling primary plane
4715 * @crtc: the CRTC whose primary plane is to be disabled
4716 *
4717 * Performs potentially sleeping operations that must be done before the
4718 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4719 * be called due to an explicit primary plane update, or due to an implicit
4720 * disable that is caused when a sprite plane completely hides the primary
4721 * plane.
4722 */
4723static void
4724intel_pre_disable_primary(struct drm_crtc *crtc)
4725{
4726 struct drm_device *dev = crtc->dev;
4727 struct drm_i915_private *dev_priv = dev->dev_private;
4728 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4729 int pipe = intel_crtc->pipe;
4730
4731 /*
4732 * Gen2 reports pipe underruns whenever all planes are disabled.
4733 * So diasble underrun reporting before all the planes get disabled.
4734 * FIXME: Need to fix the logic to work when we turn off all planes
4735 * but leave the pipe running.
4736 */
4737 if (IS_GEN2(dev))
4738 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4739
4740 /*
4741 * Vblank time updates from the shadow to live plane control register
4742 * are blocked if the memory self-refresh mode is active at that
4743 * moment. So to make sure the plane gets truly disabled, disable
4744 * first the self-refresh mode. The self-refresh enable bit in turn
4745 * will be checked/applied by the HW only at the next frame start
4746 * event which is after the vblank start event, so we need to have a
4747 * wait-for-vblank between disabling the plane and the pipe.
4748 */
4749 if (HAS_GMCH_DISPLAY(dev))
4750 intel_set_memory_cxsr(dev_priv, false);
4751
4752 mutex_lock(&dev->struct_mutex);
4753 if (dev_priv->fbc.crtc == intel_crtc)
4754 intel_fbc_disable(dev);
4755 mutex_unlock(&dev->struct_mutex);
4756
4757 /*
4758 * FIXME IPS should be fine as long as one plane is
4759 * enabled, but in practice it seems to have problems
4760 * when going from primary only to sprite only and vice
4761 * versa.
4762 */
4763 hsw_disable_ips(intel_crtc);
4764}
4765
4766static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4767{
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004768 struct drm_device *dev = crtc->dev;
4769 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4770 int pipe = intel_crtc->pipe;
4771
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004772 intel_enable_primary_hw_plane(crtc->primary, crtc);
4773 intel_enable_sprite_planes(crtc);
Maarten Lankhorstc0165302015-06-12 11:15:42 +02004774 if (to_intel_plane_state(crtc->cursor->state)->visible)
4775 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004776
4777 intel_post_enable_primary(crtc);
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004778
4779 /*
4780 * FIXME: Once we grow proper nuclear flip support out of this we need
4781 * to compute the mask of flip planes precisely. For the time being
4782 * consider this a flip to a NULL plane.
4783 */
4784 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004785}
4786
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004787static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004788{
4789 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004791 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004792 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004793
4794 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004795
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004796 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004797
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004798 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004799 for_each_intel_plane(dev, intel_plane) {
4800 if (intel_plane->pipe == pipe) {
4801 struct drm_crtc *from = intel_plane->base.crtc;
4802
4803 intel_plane->disable_plane(&intel_plane->base,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +02004804 from ?: crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004805 }
4806 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004807
Daniel Vetterf99d7062014-06-19 16:01:59 +02004808 /*
4809 * FIXME: Once we grow proper nuclear flip support out of this we need
4810 * to compute the mask of flip planes precisely. For the time being
4811 * consider this a flip to a NULL plane.
4812 */
4813 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004814}
4815
Jesse Barnesf67a5592011-01-05 10:31:48 -08004816static void ironlake_crtc_enable(struct drm_crtc *crtc)
4817{
4818 struct drm_device *dev = crtc->dev;
4819 struct drm_i915_private *dev_priv = dev->dev_private;
4820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004821 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004822 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004823
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004824 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004825 return;
4826
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004827 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004828 intel_prepare_shared_dpll(intel_crtc);
4829
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004830 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304831 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004832
4833 intel_set_pipe_timings(intel_crtc);
4834
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004835 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004836 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004837 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004838 }
4839
4840 ironlake_set_pipeconf(crtc);
4841
Jesse Barnesf67a5592011-01-05 10:31:48 -08004842 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004843
Daniel Vettera72e4c92014-09-30 10:56:47 +02004844 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4845 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004846
Daniel Vetterf6736a12013-06-05 13:34:30 +02004847 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004848 if (encoder->pre_enable)
4849 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004850
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004851 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004852 /* Note: FDI PLL enabling _must_ be done before we enable the
4853 * cpu pipes, hence this is separate from all the other fdi/pch
4854 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004855 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004856 } else {
4857 assert_fdi_tx_disabled(dev_priv, pipe);
4858 assert_fdi_rx_disabled(dev_priv, pipe);
4859 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004860
Jesse Barnesb074cec2013-04-25 12:55:02 -07004861 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004862
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004863 /*
4864 * On ILK+ LUT must be loaded before the pipe is running but with
4865 * clocks enabled
4866 */
4867 intel_crtc_load_lut(crtc);
4868
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004869 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004870 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004871
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004872 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004873 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004874
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004875 assert_vblank_disabled(crtc);
4876 drm_crtc_vblank_on(crtc);
4877
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004878 for_each_encoder_on_crtc(dev, crtc, encoder)
4879 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004880
4881 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004882 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004883}
4884
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004885/* IPS only exists on ULT machines and is tied to pipe A. */
4886static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4887{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004888 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004889}
4890
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004891static void haswell_crtc_enable(struct drm_crtc *crtc)
4892{
4893 struct drm_device *dev = crtc->dev;
4894 struct drm_i915_private *dev_priv = dev->dev_private;
4895 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4896 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004897 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4898 struct intel_crtc_state *pipe_config =
4899 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004900
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004901 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004902 return;
4903
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004904 if (intel_crtc_to_shared_dpll(intel_crtc))
4905 intel_enable_shared_dpll(intel_crtc);
4906
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004907 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304908 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004909
4910 intel_set_pipe_timings(intel_crtc);
4911
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004912 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4913 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4914 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004915 }
4916
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004917 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004918 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004919 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004920 }
4921
4922 haswell_set_pipeconf(crtc);
4923
4924 intel_set_pipe_csc(crtc);
4925
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004926 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004927
Daniel Vettera72e4c92014-09-30 10:56:47 +02004928 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004929 for_each_encoder_on_crtc(dev, crtc, encoder)
4930 if (encoder->pre_enable)
4931 encoder->pre_enable(encoder);
4932
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004933 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004934 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4935 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004936 dev_priv->display.fdi_link_train(crtc);
4937 }
4938
Paulo Zanoni1f544382012-10-24 11:32:00 -02004939 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004940
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004941 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004942 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004943 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004944 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004945 else
4946 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004947
4948 /*
4949 * On ILK+ LUT must be loaded before the pipe is running but with
4950 * clocks enabled
4951 */
4952 intel_crtc_load_lut(crtc);
4953
Paulo Zanoni1f544382012-10-24 11:32:00 -02004954 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004955 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004956
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004957 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004958 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004959
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004960 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004961 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004962
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004963 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004964 intel_ddi_set_vc_payload_alloc(crtc, true);
4965
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004966 assert_vblank_disabled(crtc);
4967 drm_crtc_vblank_on(crtc);
4968
Jani Nikula8807e552013-08-30 19:40:32 +03004969 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004970 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004971 intel_opregion_notify_encoder(encoder, true);
4972 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004973
Paulo Zanonie4916942013-09-20 16:21:19 -03004974 /* If we change the relative order between pipe/planes enabling, we need
4975 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004976 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4977 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4978 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4979 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4980 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004981}
4982
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004983static void ironlake_pfit_disable(struct intel_crtc *crtc)
4984{
4985 struct drm_device *dev = crtc->base.dev;
4986 struct drm_i915_private *dev_priv = dev->dev_private;
4987 int pipe = crtc->pipe;
4988
4989 /* To avoid upsetting the power well on haswell only disable the pfit if
4990 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004991 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004992 I915_WRITE(PF_CTL(pipe), 0);
4993 I915_WRITE(PF_WIN_POS(pipe), 0);
4994 I915_WRITE(PF_WIN_SZ(pipe), 0);
4995 }
4996}
4997
Jesse Barnes6be4a602010-09-10 10:26:01 -07004998static void ironlake_crtc_disable(struct drm_crtc *crtc)
4999{
5000 struct drm_device *dev = crtc->dev;
5001 struct drm_i915_private *dev_priv = dev->dev_private;
5002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005003 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005004 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005005 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005006
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005007 if (WARN_ON(!intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005008 return;
5009
Daniel Vetterea9d7582012-07-10 10:42:52 +02005010 for_each_encoder_on_crtc(dev, crtc, encoder)
5011 encoder->disable(encoder);
5012
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005013 drm_crtc_vblank_off(crtc);
5014 assert_vblank_disabled(crtc);
5015
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005016 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005017 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005018
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005019 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005020
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005021 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005022
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005023 if (intel_crtc->config->has_pch_encoder)
5024 ironlake_fdi_disable(crtc);
5025
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005026 for_each_encoder_on_crtc(dev, crtc, encoder)
5027 if (encoder->post_disable)
5028 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005029
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005030 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005031 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005032
Daniel Vetterd925c592013-06-05 13:34:04 +02005033 if (HAS_PCH_CPT(dev)) {
5034 /* disable TRANS_DP_CTL */
5035 reg = TRANS_DP_CTL(pipe);
5036 temp = I915_READ(reg);
5037 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5038 TRANS_DP_PORT_SEL_MASK);
5039 temp |= TRANS_DP_PORT_SEL_NONE;
5040 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005041
Daniel Vetterd925c592013-06-05 13:34:04 +02005042 /* disable DPLL_SEL */
5043 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005044 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005045 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005046 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005047
5048 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005049 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005050
5051 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005052 }
5053
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005054 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005055 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005056
5057 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005058 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005059 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005060}
5061
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005062static void haswell_crtc_disable(struct drm_crtc *crtc)
5063{
5064 struct drm_device *dev = crtc->dev;
5065 struct drm_i915_private *dev_priv = dev->dev_private;
5066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5067 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005068 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005069
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005070 if (WARN_ON(!intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005071 return;
5072
Jani Nikula8807e552013-08-30 19:40:32 +03005073 for_each_encoder_on_crtc(dev, crtc, encoder) {
5074 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005075 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005076 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005077
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005078 drm_crtc_vblank_off(crtc);
5079 assert_vblank_disabled(crtc);
5080
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005081 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005082 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5083 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005084 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005085
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005086 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005087 intel_ddi_set_vc_payload_alloc(crtc, false);
5088
Paulo Zanoniad80a812012-10-24 16:06:19 -02005089 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005090
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005091 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005092 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005093 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005094 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005095 else
5096 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005097
Paulo Zanoni1f544382012-10-24 11:32:00 -02005098 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005099
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005100 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005101 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005102 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005103 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005104
Imre Deak97b040a2014-06-25 22:01:50 +03005105 for_each_encoder_on_crtc(dev, crtc, encoder)
5106 if (encoder->post_disable)
5107 encoder->post_disable(encoder);
5108
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005109 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005110 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005111
5112 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005113 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005114 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005115
5116 if (intel_crtc_to_shared_dpll(intel_crtc))
5117 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005118}
5119
Jesse Barnes2dd24552013-04-25 12:55:01 -07005120static void i9xx_pfit_enable(struct intel_crtc *crtc)
5121{
5122 struct drm_device *dev = crtc->base.dev;
5123 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005124 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005125
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005126 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005127 return;
5128
Daniel Vetterc0b03412013-05-28 12:05:54 +02005129 /*
5130 * The panel fitter should only be adjusted whilst the pipe is disabled,
5131 * according to register description and PRM.
5132 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005133 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5134 assert_pipe_disabled(dev_priv, crtc->pipe);
5135
Jesse Barnesb074cec2013-04-25 12:55:02 -07005136 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5137 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005138
5139 /* Border color in case we don't scale up to the full screen. Black by
5140 * default, change to something else for debugging. */
5141 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005142}
5143
Dave Airlied05410f2014-06-05 13:22:59 +10005144static enum intel_display_power_domain port_to_power_domain(enum port port)
5145{
5146 switch (port) {
5147 case PORT_A:
5148 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5149 case PORT_B:
5150 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5151 case PORT_C:
5152 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5153 case PORT_D:
5154 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5155 default:
5156 WARN_ON_ONCE(1);
5157 return POWER_DOMAIN_PORT_OTHER;
5158 }
5159}
5160
Imre Deak77d22dc2014-03-05 16:20:52 +02005161#define for_each_power_domain(domain, mask) \
5162 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5163 if ((1 << (domain)) & (mask))
5164
Imre Deak319be8a2014-03-04 19:22:57 +02005165enum intel_display_power_domain
5166intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005167{
Imre Deak319be8a2014-03-04 19:22:57 +02005168 struct drm_device *dev = intel_encoder->base.dev;
5169 struct intel_digital_port *intel_dig_port;
5170
5171 switch (intel_encoder->type) {
5172 case INTEL_OUTPUT_UNKNOWN:
5173 /* Only DDI platforms should ever use this output type */
5174 WARN_ON_ONCE(!HAS_DDI(dev));
5175 case INTEL_OUTPUT_DISPLAYPORT:
5176 case INTEL_OUTPUT_HDMI:
5177 case INTEL_OUTPUT_EDP:
5178 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005179 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005180 case INTEL_OUTPUT_DP_MST:
5181 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5182 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005183 case INTEL_OUTPUT_ANALOG:
5184 return POWER_DOMAIN_PORT_CRT;
5185 case INTEL_OUTPUT_DSI:
5186 return POWER_DOMAIN_PORT_DSI;
5187 default:
5188 return POWER_DOMAIN_PORT_OTHER;
5189 }
5190}
5191
5192static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5193{
5194 struct drm_device *dev = crtc->dev;
5195 struct intel_encoder *intel_encoder;
5196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5197 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005198 unsigned long mask;
5199 enum transcoder transcoder;
5200
5201 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5202
5203 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5204 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005205 if (intel_crtc->config->pch_pfit.enabled ||
5206 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005207 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5208
Imre Deak319be8a2014-03-04 19:22:57 +02005209 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5210 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5211
Imre Deak77d22dc2014-03-05 16:20:52 +02005212 return mask;
5213}
5214
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005215static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005216{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005217 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005218 struct drm_i915_private *dev_priv = dev->dev_private;
5219 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5220 struct intel_crtc *crtc;
5221
5222 /*
5223 * First get all needed power domains, then put all unneeded, to avoid
5224 * any unnecessary toggling of the power wells.
5225 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005226 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005227 enum intel_display_power_domain domain;
5228
Matt Roper83d65732015-02-25 13:12:16 -08005229 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005230 continue;
5231
Imre Deak319be8a2014-03-04 19:22:57 +02005232 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005233
5234 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5235 intel_display_power_get(dev_priv, domain);
5236 }
5237
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005238 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005239 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005240
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005241 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005242 enum intel_display_power_domain domain;
5243
5244 for_each_power_domain(domain, crtc->enabled_power_domains)
5245 intel_display_power_put(dev_priv, domain);
5246
5247 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5248 }
5249
5250 intel_display_set_init_power(dev_priv, false);
5251}
5252
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005253static void intel_update_max_cdclk(struct drm_device *dev)
5254{
5255 struct drm_i915_private *dev_priv = dev->dev_private;
5256
5257 if (IS_SKYLAKE(dev)) {
5258 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5259
5260 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5261 dev_priv->max_cdclk_freq = 675000;
5262 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5263 dev_priv->max_cdclk_freq = 540000;
5264 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5265 dev_priv->max_cdclk_freq = 450000;
5266 else
5267 dev_priv->max_cdclk_freq = 337500;
5268 } else if (IS_BROADWELL(dev)) {
5269 /*
5270 * FIXME with extra cooling we can allow
5271 * 540 MHz for ULX and 675 Mhz for ULT.
5272 * How can we know if extra cooling is
5273 * available? PCI ID, VTB, something else?
5274 */
5275 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5276 dev_priv->max_cdclk_freq = 450000;
5277 else if (IS_BDW_ULX(dev))
5278 dev_priv->max_cdclk_freq = 450000;
5279 else if (IS_BDW_ULT(dev))
5280 dev_priv->max_cdclk_freq = 540000;
5281 else
5282 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005283 } else if (IS_CHERRYVIEW(dev)) {
5284 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005285 } else if (IS_VALLEYVIEW(dev)) {
5286 dev_priv->max_cdclk_freq = 400000;
5287 } else {
5288 /* otherwise assume cdclk is fixed */
5289 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5290 }
5291
5292 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5293 dev_priv->max_cdclk_freq);
5294}
5295
5296static void intel_update_cdclk(struct drm_device *dev)
5297{
5298 struct drm_i915_private *dev_priv = dev->dev_private;
5299
5300 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5301 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5302 dev_priv->cdclk_freq);
5303
5304 /*
5305 * Program the gmbus_freq based on the cdclk frequency.
5306 * BSpec erroneously claims we should aim for 4MHz, but
5307 * in fact 1MHz is the correct frequency.
5308 */
5309 if (IS_VALLEYVIEW(dev)) {
5310 /*
5311 * Program the gmbus_freq based on the cdclk frequency.
5312 * BSpec erroneously claims we should aim for 4MHz, but
5313 * in fact 1MHz is the correct frequency.
5314 */
5315 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5316 }
5317
5318 if (dev_priv->max_cdclk_freq == 0)
5319 intel_update_max_cdclk(dev);
5320}
5321
Damien Lespiau70d0c572015-06-04 18:21:29 +01005322static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305323{
5324 struct drm_i915_private *dev_priv = dev->dev_private;
5325 uint32_t divider;
5326 uint32_t ratio;
5327 uint32_t current_freq;
5328 int ret;
5329
5330 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5331 switch (frequency) {
5332 case 144000:
5333 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5334 ratio = BXT_DE_PLL_RATIO(60);
5335 break;
5336 case 288000:
5337 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5338 ratio = BXT_DE_PLL_RATIO(60);
5339 break;
5340 case 384000:
5341 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5342 ratio = BXT_DE_PLL_RATIO(60);
5343 break;
5344 case 576000:
5345 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5346 ratio = BXT_DE_PLL_RATIO(60);
5347 break;
5348 case 624000:
5349 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5350 ratio = BXT_DE_PLL_RATIO(65);
5351 break;
5352 case 19200:
5353 /*
5354 * Bypass frequency with DE PLL disabled. Init ratio, divider
5355 * to suppress GCC warning.
5356 */
5357 ratio = 0;
5358 divider = 0;
5359 break;
5360 default:
5361 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5362
5363 return;
5364 }
5365
5366 mutex_lock(&dev_priv->rps.hw_lock);
5367 /* Inform power controller of upcoming frequency change */
5368 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5369 0x80000000);
5370 mutex_unlock(&dev_priv->rps.hw_lock);
5371
5372 if (ret) {
5373 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5374 ret, frequency);
5375 return;
5376 }
5377
5378 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5379 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5380 current_freq = current_freq * 500 + 1000;
5381
5382 /*
5383 * DE PLL has to be disabled when
5384 * - setting to 19.2MHz (bypass, PLL isn't used)
5385 * - before setting to 624MHz (PLL needs toggling)
5386 * - before setting to any frequency from 624MHz (PLL needs toggling)
5387 */
5388 if (frequency == 19200 || frequency == 624000 ||
5389 current_freq == 624000) {
5390 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5391 /* Timeout 200us */
5392 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5393 1))
5394 DRM_ERROR("timout waiting for DE PLL unlock\n");
5395 }
5396
5397 if (frequency != 19200) {
5398 uint32_t val;
5399
5400 val = I915_READ(BXT_DE_PLL_CTL);
5401 val &= ~BXT_DE_PLL_RATIO_MASK;
5402 val |= ratio;
5403 I915_WRITE(BXT_DE_PLL_CTL, val);
5404
5405 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5406 /* Timeout 200us */
5407 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5408 DRM_ERROR("timeout waiting for DE PLL lock\n");
5409
5410 val = I915_READ(CDCLK_CTL);
5411 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5412 val |= divider;
5413 /*
5414 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5415 * enable otherwise.
5416 */
5417 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5418 if (frequency >= 500000)
5419 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5420
5421 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5422 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5423 val |= (frequency - 1000) / 500;
5424 I915_WRITE(CDCLK_CTL, val);
5425 }
5426
5427 mutex_lock(&dev_priv->rps.hw_lock);
5428 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5429 DIV_ROUND_UP(frequency, 25000));
5430 mutex_unlock(&dev_priv->rps.hw_lock);
5431
5432 if (ret) {
5433 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5434 ret, frequency);
5435 return;
5436 }
5437
Damien Lespiaua47871b2015-06-04 18:21:34 +01005438 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305439}
5440
5441void broxton_init_cdclk(struct drm_device *dev)
5442{
5443 struct drm_i915_private *dev_priv = dev->dev_private;
5444 uint32_t val;
5445
5446 /*
5447 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5448 * or else the reset will hang because there is no PCH to respond.
5449 * Move the handshake programming to initialization sequence.
5450 * Previously was left up to BIOS.
5451 */
5452 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5453 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5454 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5455
5456 /* Enable PG1 for cdclk */
5457 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5458
5459 /* check if cd clock is enabled */
5460 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5461 DRM_DEBUG_KMS("Display already initialized\n");
5462 return;
5463 }
5464
5465 /*
5466 * FIXME:
5467 * - The initial CDCLK needs to be read from VBT.
5468 * Need to make this change after VBT has changes for BXT.
5469 * - check if setting the max (or any) cdclk freq is really necessary
5470 * here, it belongs to modeset time
5471 */
5472 broxton_set_cdclk(dev, 624000);
5473
5474 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005475 POSTING_READ(DBUF_CTL);
5476
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305477 udelay(10);
5478
5479 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5480 DRM_ERROR("DBuf power enable timeout!\n");
5481}
5482
5483void broxton_uninit_cdclk(struct drm_device *dev)
5484{
5485 struct drm_i915_private *dev_priv = dev->dev_private;
5486
5487 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005488 POSTING_READ(DBUF_CTL);
5489
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305490 udelay(10);
5491
5492 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5493 DRM_ERROR("DBuf power disable timeout!\n");
5494
5495 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5496 broxton_set_cdclk(dev, 19200);
5497
5498 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5499}
5500
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005501static const struct skl_cdclk_entry {
5502 unsigned int freq;
5503 unsigned int vco;
5504} skl_cdclk_frequencies[] = {
5505 { .freq = 308570, .vco = 8640 },
5506 { .freq = 337500, .vco = 8100 },
5507 { .freq = 432000, .vco = 8640 },
5508 { .freq = 450000, .vco = 8100 },
5509 { .freq = 540000, .vco = 8100 },
5510 { .freq = 617140, .vco = 8640 },
5511 { .freq = 675000, .vco = 8100 },
5512};
5513
5514static unsigned int skl_cdclk_decimal(unsigned int freq)
5515{
5516 return (freq - 1000) / 500;
5517}
5518
5519static unsigned int skl_cdclk_get_vco(unsigned int freq)
5520{
5521 unsigned int i;
5522
5523 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5524 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5525
5526 if (e->freq == freq)
5527 return e->vco;
5528 }
5529
5530 return 8100;
5531}
5532
5533static void
5534skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5535{
5536 unsigned int min_freq;
5537 u32 val;
5538
5539 /* select the minimum CDCLK before enabling DPLL 0 */
5540 val = I915_READ(CDCLK_CTL);
5541 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5542 val |= CDCLK_FREQ_337_308;
5543
5544 if (required_vco == 8640)
5545 min_freq = 308570;
5546 else
5547 min_freq = 337500;
5548
5549 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5550
5551 I915_WRITE(CDCLK_CTL, val);
5552 POSTING_READ(CDCLK_CTL);
5553
5554 /*
5555 * We always enable DPLL0 with the lowest link rate possible, but still
5556 * taking into account the VCO required to operate the eDP panel at the
5557 * desired frequency. The usual DP link rates operate with a VCO of
5558 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5559 * The modeset code is responsible for the selection of the exact link
5560 * rate later on, with the constraint of choosing a frequency that
5561 * works with required_vco.
5562 */
5563 val = I915_READ(DPLL_CTRL1);
5564
5565 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5566 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5567 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5568 if (required_vco == 8640)
5569 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5570 SKL_DPLL0);
5571 else
5572 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5573 SKL_DPLL0);
5574
5575 I915_WRITE(DPLL_CTRL1, val);
5576 POSTING_READ(DPLL_CTRL1);
5577
5578 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5579
5580 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5581 DRM_ERROR("DPLL0 not locked\n");
5582}
5583
5584static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5585{
5586 int ret;
5587 u32 val;
5588
5589 /* inform PCU we want to change CDCLK */
5590 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5591 mutex_lock(&dev_priv->rps.hw_lock);
5592 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5593 mutex_unlock(&dev_priv->rps.hw_lock);
5594
5595 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5596}
5597
5598static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5599{
5600 unsigned int i;
5601
5602 for (i = 0; i < 15; i++) {
5603 if (skl_cdclk_pcu_ready(dev_priv))
5604 return true;
5605 udelay(10);
5606 }
5607
5608 return false;
5609}
5610
5611static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5612{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005613 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005614 u32 freq_select, pcu_ack;
5615
5616 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5617
5618 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5619 DRM_ERROR("failed to inform PCU about cdclk change\n");
5620 return;
5621 }
5622
5623 /* set CDCLK_CTL */
5624 switch(freq) {
5625 case 450000:
5626 case 432000:
5627 freq_select = CDCLK_FREQ_450_432;
5628 pcu_ack = 1;
5629 break;
5630 case 540000:
5631 freq_select = CDCLK_FREQ_540;
5632 pcu_ack = 2;
5633 break;
5634 case 308570:
5635 case 337500:
5636 default:
5637 freq_select = CDCLK_FREQ_337_308;
5638 pcu_ack = 0;
5639 break;
5640 case 617140:
5641 case 675000:
5642 freq_select = CDCLK_FREQ_675_617;
5643 pcu_ack = 3;
5644 break;
5645 }
5646
5647 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5648 POSTING_READ(CDCLK_CTL);
5649
5650 /* inform PCU of the change */
5651 mutex_lock(&dev_priv->rps.hw_lock);
5652 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5653 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005654
5655 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005656}
5657
5658void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5659{
5660 /* disable DBUF power */
5661 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5662 POSTING_READ(DBUF_CTL);
5663
5664 udelay(10);
5665
5666 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5667 DRM_ERROR("DBuf power disable timeout\n");
5668
5669 /* disable DPLL0 */
5670 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5671 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5672 DRM_ERROR("Couldn't disable DPLL0\n");
5673
5674 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5675}
5676
5677void skl_init_cdclk(struct drm_i915_private *dev_priv)
5678{
5679 u32 val;
5680 unsigned int required_vco;
5681
5682 /* enable PCH reset handshake */
5683 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5684 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5685
5686 /* enable PG1 and Misc I/O */
5687 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5688
5689 /* DPLL0 already enabed !? */
5690 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5691 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5692 return;
5693 }
5694
5695 /* enable DPLL0 */
5696 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5697 skl_dpll0_enable(dev_priv, required_vco);
5698
5699 /* set CDCLK to the frequency the BIOS chose */
5700 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5701
5702 /* enable DBUF power */
5703 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5704 POSTING_READ(DBUF_CTL);
5705
5706 udelay(10);
5707
5708 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5709 DRM_ERROR("DBuf power enable timeout\n");
5710}
5711
Ville Syrjälädfcab172014-06-13 13:37:47 +03005712/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005713static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005714{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005715 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005716
Jesse Barnes586f49d2013-11-04 16:06:59 -08005717 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005718 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005719 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5720 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005721 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005722
Ville Syrjälädfcab172014-06-13 13:37:47 +03005723 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005724}
5725
5726/* Adjust CDclk dividers to allow high res or save power if possible */
5727static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5728{
5729 struct drm_i915_private *dev_priv = dev->dev_private;
5730 u32 val, cmd;
5731
Vandana Kannan164dfd22014-11-24 13:37:41 +05305732 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5733 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005734
Ville Syrjälädfcab172014-06-13 13:37:47 +03005735 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005736 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005737 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005738 cmd = 1;
5739 else
5740 cmd = 0;
5741
5742 mutex_lock(&dev_priv->rps.hw_lock);
5743 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5744 val &= ~DSPFREQGUAR_MASK;
5745 val |= (cmd << DSPFREQGUAR_SHIFT);
5746 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5747 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5748 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5749 50)) {
5750 DRM_ERROR("timed out waiting for CDclk change\n");
5751 }
5752 mutex_unlock(&dev_priv->rps.hw_lock);
5753
Ville Syrjälä54433e92015-05-26 20:42:31 +03005754 mutex_lock(&dev_priv->sb_lock);
5755
Ville Syrjälädfcab172014-06-13 13:37:47 +03005756 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005757 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005758
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005759 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005760
Jesse Barnes30a970c2013-11-04 13:48:12 -08005761 /* adjust cdclk divider */
5762 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005763 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005764 val |= divider;
5765 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005766
5767 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5768 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5769 50))
5770 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005771 }
5772
Jesse Barnes30a970c2013-11-04 13:48:12 -08005773 /* adjust self-refresh exit latency value */
5774 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5775 val &= ~0x7f;
5776
5777 /*
5778 * For high bandwidth configs, we set a higher latency in the bunit
5779 * so that the core display fetch happens in time to avoid underruns.
5780 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005781 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005782 val |= 4500 / 250; /* 4.5 usec */
5783 else
5784 val |= 3000 / 250; /* 3.0 usec */
5785 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005786
Ville Syrjäläa5805162015-05-26 20:42:30 +03005787 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005788
Ville Syrjäläb6283052015-06-03 15:45:07 +03005789 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005790}
5791
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005792static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5793{
5794 struct drm_i915_private *dev_priv = dev->dev_private;
5795 u32 val, cmd;
5796
Vandana Kannan164dfd22014-11-24 13:37:41 +05305797 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5798 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005799
5800 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005801 case 333333:
5802 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005803 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005804 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005805 break;
5806 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005807 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005808 return;
5809 }
5810
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005811 /*
5812 * Specs are full of misinformation, but testing on actual
5813 * hardware has shown that we just need to write the desired
5814 * CCK divider into the Punit register.
5815 */
5816 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5817
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005818 mutex_lock(&dev_priv->rps.hw_lock);
5819 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5820 val &= ~DSPFREQGUAR_MASK_CHV;
5821 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5822 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5823 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5824 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5825 50)) {
5826 DRM_ERROR("timed out waiting for CDclk change\n");
5827 }
5828 mutex_unlock(&dev_priv->rps.hw_lock);
5829
Ville Syrjäläb6283052015-06-03 15:45:07 +03005830 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005831}
5832
Jesse Barnes30a970c2013-11-04 13:48:12 -08005833static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5834 int max_pixclk)
5835{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005836 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005837 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005838
Jesse Barnes30a970c2013-11-04 13:48:12 -08005839 /*
5840 * Really only a few cases to deal with, as only 4 CDclks are supported:
5841 * 200MHz
5842 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005843 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005844 * 400MHz (VLV only)
5845 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5846 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005847 *
5848 * We seem to get an unstable or solid color picture at 200MHz.
5849 * Not sure what's wrong. For now use 200MHz only when all pipes
5850 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005851 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005852 if (!IS_CHERRYVIEW(dev_priv) &&
5853 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005854 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005855 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005856 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005857 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005858 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005859 else
5860 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005861}
5862
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305863static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5864 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005865{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305866 /*
5867 * FIXME:
5868 * - remove the guardband, it's not needed on BXT
5869 * - set 19.2MHz bypass frequency if there are no active pipes
5870 */
5871 if (max_pixclk > 576000*9/10)
5872 return 624000;
5873 else if (max_pixclk > 384000*9/10)
5874 return 576000;
5875 else if (max_pixclk > 288000*9/10)
5876 return 384000;
5877 else if (max_pixclk > 144000*9/10)
5878 return 288000;
5879 else
5880 return 144000;
5881}
5882
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005883/* Compute the max pixel clock for new configuration. Uses atomic state if
5884 * that's non-NULL, look at current state otherwise. */
5885static int intel_mode_max_pixclk(struct drm_device *dev,
5886 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005887{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005888 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005889 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005890 int max_pixclk = 0;
5891
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005892 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005893 if (state)
5894 crtc_state =
5895 intel_atomic_get_crtc_state(state, intel_crtc);
5896 else
5897 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005898 if (IS_ERR(crtc_state))
5899 return PTR_ERR(crtc_state);
5900
5901 if (!crtc_state->base.enable)
5902 continue;
5903
5904 max_pixclk = max(max_pixclk,
5905 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005906 }
5907
5908 return max_pixclk;
5909}
5910
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005911static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005912{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005913 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005914 struct drm_crtc *crtc;
5915 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005916 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005917 int cdclk, ret = 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005918
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005919 if (max_pixclk < 0)
5920 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005921
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305922 if (IS_VALLEYVIEW(dev_priv))
5923 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5924 else
5925 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5926
5927 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005928 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005929
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005930 /* add all active pipes to the state */
5931 for_each_crtc(state->dev, crtc) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005932 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5933 if (IS_ERR(crtc_state))
5934 return PTR_ERR(crtc_state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005935
5936 if (!crtc_state->active || needs_modeset(crtc_state))
5937 continue;
5938
5939 crtc_state->mode_changed = true;
5940
5941 ret = drm_atomic_add_affected_connectors(state, crtc);
5942 if (ret)
5943 break;
5944
5945 ret = drm_atomic_add_affected_planes(state, crtc);
5946 if (ret)
5947 break;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005948 }
5949
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005950 return ret;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005951}
5952
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005953static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5954{
5955 unsigned int credits, default_credits;
5956
5957 if (IS_CHERRYVIEW(dev_priv))
5958 default_credits = PFI_CREDIT(12);
5959 else
5960 default_credits = PFI_CREDIT(8);
5961
Vandana Kannan164dfd22014-11-24 13:37:41 +05305962 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005963 /* CHV suggested value is 31 or 63 */
5964 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005965 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005966 else
5967 credits = PFI_CREDIT(15);
5968 } else {
5969 credits = default_credits;
5970 }
5971
5972 /*
5973 * WA - write default credits before re-programming
5974 * FIXME: should we also set the resend bit here?
5975 */
5976 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5977 default_credits);
5978
5979 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5980 credits | PFI_CREDIT_RESEND);
5981
5982 /*
5983 * FIXME is this guaranteed to clear
5984 * immediately or should we poll for it?
5985 */
5986 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5987}
5988
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005989static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005990{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005991 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005992 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005993 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005994 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005995
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005996 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5997 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005998 if (WARN_ON(max_pixclk < 0))
5999 return;
6000
6001 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006002
Vandana Kannan164dfd22014-11-24 13:37:41 +05306003 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02006004 /*
6005 * FIXME: We can end up here with all power domains off, yet
6006 * with a CDCLK frequency other than the minimum. To account
6007 * for this take the PIPE-A power domain, which covers the HW
6008 * blocks needed for the following programming. This can be
6009 * removed once it's guaranteed that we get here either with
6010 * the minimum CDCLK set, or the required power domains
6011 * enabled.
6012 */
6013 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6014
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006015 if (IS_CHERRYVIEW(dev))
6016 cherryview_set_cdclk(dev, req_cdclk);
6017 else
6018 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02006019
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006020 vlv_program_pfi_credits(dev_priv);
6021
Imre Deak738c05c2014-11-19 16:25:37 +02006022 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006023 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08006024}
6025
Jesse Barnes89b667f2013-04-18 14:51:36 -07006026static void valleyview_crtc_enable(struct drm_crtc *crtc)
6027{
6028 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006029 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6031 struct intel_encoder *encoder;
6032 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006033 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006034
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006035 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006036 return;
6037
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006038 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306039
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006040 if (!is_dsi) {
6041 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006042 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006043 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006044 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006045 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006046
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006047 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306048 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006049
6050 intel_set_pipe_timings(intel_crtc);
6051
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006052 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6053 struct drm_i915_private *dev_priv = dev->dev_private;
6054
6055 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6056 I915_WRITE(CHV_CANVAS(pipe), 0);
6057 }
6058
Daniel Vetter5b18e572014-04-24 23:55:06 +02006059 i9xx_set_pipeconf(intel_crtc);
6060
Jesse Barnes89b667f2013-04-18 14:51:36 -07006061 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006062
Daniel Vettera72e4c92014-09-30 10:56:47 +02006063 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006064
Jesse Barnes89b667f2013-04-18 14:51:36 -07006065 for_each_encoder_on_crtc(dev, crtc, encoder)
6066 if (encoder->pre_pll_enable)
6067 encoder->pre_pll_enable(encoder);
6068
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006069 if (!is_dsi) {
6070 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006071 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006072 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006073 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006074 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006075
6076 for_each_encoder_on_crtc(dev, crtc, encoder)
6077 if (encoder->pre_enable)
6078 encoder->pre_enable(encoder);
6079
Jesse Barnes2dd24552013-04-25 12:55:01 -07006080 i9xx_pfit_enable(intel_crtc);
6081
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006082 intel_crtc_load_lut(crtc);
6083
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006084 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006085 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006086
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006087 assert_vblank_disabled(crtc);
6088 drm_crtc_vblank_on(crtc);
6089
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006090 for_each_encoder_on_crtc(dev, crtc, encoder)
6091 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006092}
6093
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006094static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6095{
6096 struct drm_device *dev = crtc->base.dev;
6097 struct drm_i915_private *dev_priv = dev->dev_private;
6098
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006099 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6100 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006101}
6102
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006103static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006104{
6105 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006106 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006107 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006108 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006109 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006110
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006111 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006112 return;
6113
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006114 i9xx_set_pll_dividers(intel_crtc);
6115
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006116 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306117 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006118
6119 intel_set_pipe_timings(intel_crtc);
6120
Daniel Vetter5b18e572014-04-24 23:55:06 +02006121 i9xx_set_pipeconf(intel_crtc);
6122
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006123 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006124
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006125 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006126 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006127
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006128 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006129 if (encoder->pre_enable)
6130 encoder->pre_enable(encoder);
6131
Daniel Vetterf6736a12013-06-05 13:34:30 +02006132 i9xx_enable_pll(intel_crtc);
6133
Jesse Barnes2dd24552013-04-25 12:55:01 -07006134 i9xx_pfit_enable(intel_crtc);
6135
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006136 intel_crtc_load_lut(crtc);
6137
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006138 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006139 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006140
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006141 assert_vblank_disabled(crtc);
6142 drm_crtc_vblank_on(crtc);
6143
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006144 for_each_encoder_on_crtc(dev, crtc, encoder)
6145 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006146}
6147
Daniel Vetter87476d62013-04-11 16:29:06 +02006148static void i9xx_pfit_disable(struct intel_crtc *crtc)
6149{
6150 struct drm_device *dev = crtc->base.dev;
6151 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006152
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006153 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006154 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006155
6156 assert_pipe_disabled(dev_priv, crtc->pipe);
6157
Daniel Vetter328d8e82013-05-08 10:36:31 +02006158 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6159 I915_READ(PFIT_CONTROL));
6160 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006161}
6162
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006163static void i9xx_crtc_disable(struct drm_crtc *crtc)
6164{
6165 struct drm_device *dev = crtc->dev;
6166 struct drm_i915_private *dev_priv = dev->dev_private;
6167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006168 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006169 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006170
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006171 if (WARN_ON(!intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006172 return;
6173
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006174 /*
6175 * On gen2 planes are double buffered but the pipe isn't, so we must
6176 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006177 * We also need to wait on all gmch platforms because of the
6178 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006179 */
Imre Deak564ed192014-06-13 14:54:21 +03006180 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006181
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006182 for_each_encoder_on_crtc(dev, crtc, encoder)
6183 encoder->disable(encoder);
6184
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006185 drm_crtc_vblank_off(crtc);
6186 assert_vblank_disabled(crtc);
6187
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006188 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006189
Daniel Vetter87476d62013-04-11 16:29:06 +02006190 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006191
Jesse Barnes89b667f2013-04-18 14:51:36 -07006192 for_each_encoder_on_crtc(dev, crtc, encoder)
6193 if (encoder->post_disable)
6194 encoder->post_disable(encoder);
6195
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006196 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006197 if (IS_CHERRYVIEW(dev))
6198 chv_disable_pll(dev_priv, pipe);
6199 else if (IS_VALLEYVIEW(dev))
6200 vlv_disable_pll(dev_priv, pipe);
6201 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006202 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006203 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006204
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006205 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006206 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006207
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006208 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006209 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006210
Daniel Vetterefa96242014-04-24 23:55:02 +02006211 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006212 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006213 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006214}
6215
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006216static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006217{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006219 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006220 enum intel_display_power_domain domain;
6221 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006222
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006223 if (!intel_crtc->active)
6224 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006225
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006226 intel_crtc_disable_planes(crtc);
6227 dev_priv->display.crtc_disable(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006228
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006229 domains = intel_crtc->enabled_power_domains;
6230 for_each_power_domain(domain, domains)
6231 intel_display_power_put(dev_priv, domain);
6232 intel_crtc->enabled_power_domains = 0;
6233}
6234
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006235/*
6236 * turn all crtc's off, but do not adjust state
6237 * This has to be paired with a call to intel_modeset_setup_hw_state.
6238 */
Maarten Lankhorst9716c692015-06-10 10:24:19 +02006239void intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006240{
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006241 struct drm_crtc *crtc;
6242
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006243 for_each_crtc(dev, crtc)
6244 intel_crtc_disable_noatomic(crtc);
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006245}
6246
Chris Wilsoncdd59982010-09-08 16:30:16 +01006247/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006248int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Daniel Vetter976f8a22012-07-08 22:34:21 +02006249{
6250 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006251 struct drm_mode_config *config = &dev->mode_config;
6252 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006254 struct intel_crtc_state *pipe_config;
6255 struct drm_atomic_state *state;
6256 int ret;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006257
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006258 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006259 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006260
6261 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006262 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006263
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006264 /* this function should be called with drm_modeset_lock_all for now */
6265 if (WARN_ON(!ctx))
6266 return -EIO;
6267 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006268
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006269 state = drm_atomic_state_alloc(dev);
6270 if (WARN_ON(!state))
6271 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006272
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006273 state->acquire_ctx = ctx;
6274 state->allow_modeset = true;
6275
6276 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6277 if (IS_ERR(pipe_config)) {
6278 ret = PTR_ERR(pipe_config);
6279 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006280 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006281 pipe_config->base.active = enable;
6282
6283 ret = intel_set_mode(state);
6284 if (!ret)
6285 return ret;
6286
6287err:
6288 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6289 drm_atomic_state_free(state);
6290 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306291}
6292
6293/**
6294 * Sets the power management mode of the pipe and plane.
6295 */
6296void intel_crtc_update_dpms(struct drm_crtc *crtc)
6297{
6298 struct drm_device *dev = crtc->dev;
6299 struct intel_encoder *intel_encoder;
6300 bool enable = false;
6301
6302 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6303 enable |= intel_encoder->connectors_active;
6304
6305 intel_crtc_control(crtc, enable);
Chris Wilsoncdd59982010-09-08 16:30:16 +01006306}
6307
Chris Wilsonea5b2132010-08-04 13:50:23 +01006308void intel_encoder_destroy(struct drm_encoder *encoder)
6309{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006310 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006311
Chris Wilsonea5b2132010-08-04 13:50:23 +01006312 drm_encoder_cleanup(encoder);
6313 kfree(intel_encoder);
6314}
6315
Damien Lespiau92373292013-08-08 22:28:57 +01006316/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006317 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6318 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006319static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006320{
6321 if (mode == DRM_MODE_DPMS_ON) {
6322 encoder->connectors_active = true;
6323
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006324 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006325 } else {
6326 encoder->connectors_active = false;
6327
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006328 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006329 }
6330}
6331
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006332/* Cross check the actual hw state with our own modeset state tracking (and it's
6333 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006334static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006335{
6336 if (connector->get_hw_state(connector)) {
6337 struct intel_encoder *encoder = connector->encoder;
6338 struct drm_crtc *crtc;
6339 bool encoder_enabled;
6340 enum pipe pipe;
6341
6342 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6343 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006344 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006345
Dave Airlie0e32b392014-05-02 14:02:48 +10006346 /* there is no real hw state for MST connectors */
6347 if (connector->mst_port)
6348 return;
6349
Rob Clarke2c719b2014-12-15 13:56:32 -05006350 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006351 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006352 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006353 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006354
Dave Airlie36cd7442014-05-02 13:44:18 +10006355 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006356 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006357 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006358
Dave Airlie36cd7442014-05-02 13:44:18 +10006359 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006360 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6361 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006362 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006363
Dave Airlie36cd7442014-05-02 13:44:18 +10006364 crtc = encoder->base.crtc;
6365
Matt Roper83d65732015-02-25 13:12:16 -08006366 I915_STATE_WARN(!crtc->state->enable,
6367 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006368 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6369 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006370 "encoder active on the wrong pipe\n");
6371 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006372 }
6373}
6374
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006375int intel_connector_init(struct intel_connector *connector)
6376{
6377 struct drm_connector_state *connector_state;
6378
6379 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6380 if (!connector_state)
6381 return -ENOMEM;
6382
6383 connector->base.state = connector_state;
6384 return 0;
6385}
6386
6387struct intel_connector *intel_connector_alloc(void)
6388{
6389 struct intel_connector *connector;
6390
6391 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6392 if (!connector)
6393 return NULL;
6394
6395 if (intel_connector_init(connector) < 0) {
6396 kfree(connector);
6397 return NULL;
6398 }
6399
6400 return connector;
6401}
6402
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006403/* Even simpler default implementation, if there's really no special case to
6404 * consider. */
6405void intel_connector_dpms(struct drm_connector *connector, int mode)
6406{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006407 /* All the simple cases only support two dpms states. */
6408 if (mode != DRM_MODE_DPMS_ON)
6409 mode = DRM_MODE_DPMS_OFF;
6410
6411 if (mode == connector->dpms)
6412 return;
6413
6414 connector->dpms = mode;
6415
6416 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006417 if (connector->encoder)
6418 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006419
Daniel Vetterb9805142012-08-31 17:37:33 +02006420 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006421}
6422
Daniel Vetterf0947c32012-07-02 13:10:34 +02006423/* Simple connector->get_hw_state implementation for encoders that support only
6424 * one connector and no cloning and hence the encoder state determines the state
6425 * of the connector. */
6426bool intel_connector_get_hw_state(struct intel_connector *connector)
6427{
Daniel Vetter24929352012-07-02 20:28:59 +02006428 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006429 struct intel_encoder *encoder = connector->encoder;
6430
6431 return encoder->get_hw_state(encoder, &pipe);
6432}
6433
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006434static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006435{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006436 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6437 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006438
6439 return 0;
6440}
6441
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006442static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006443 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006444{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006445 struct drm_atomic_state *state = pipe_config->base.state;
6446 struct intel_crtc *other_crtc;
6447 struct intel_crtc_state *other_crtc_state;
6448
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006449 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6450 pipe_name(pipe), pipe_config->fdi_lanes);
6451 if (pipe_config->fdi_lanes > 4) {
6452 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6453 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006454 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006455 }
6456
Paulo Zanonibafb6552013-11-02 21:07:44 -07006457 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006458 if (pipe_config->fdi_lanes > 2) {
6459 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6460 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006461 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006462 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006463 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006464 }
6465 }
6466
6467 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006468 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006469
6470 /* Ivybridge 3 pipe is really complicated */
6471 switch (pipe) {
6472 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006473 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006474 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006475 if (pipe_config->fdi_lanes <= 2)
6476 return 0;
6477
6478 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6479 other_crtc_state =
6480 intel_atomic_get_crtc_state(state, other_crtc);
6481 if (IS_ERR(other_crtc_state))
6482 return PTR_ERR(other_crtc_state);
6483
6484 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006485 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6486 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006487 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006488 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006489 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006490 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006491 if (pipe_config->fdi_lanes > 2) {
6492 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6493 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006494 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006495 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006496
6497 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6498 other_crtc_state =
6499 intel_atomic_get_crtc_state(state, other_crtc);
6500 if (IS_ERR(other_crtc_state))
6501 return PTR_ERR(other_crtc_state);
6502
6503 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006504 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006505 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006506 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006507 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006508 default:
6509 BUG();
6510 }
6511}
6512
Daniel Vettere29c22c2013-02-21 00:00:16 +01006513#define RETRY 1
6514static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006515 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006516{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006517 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006518 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006519 int lane, link_bw, fdi_dotclock, ret;
6520 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006521
Daniel Vettere29c22c2013-02-21 00:00:16 +01006522retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006523 /* FDI is a binary signal running at ~2.7GHz, encoding
6524 * each output octet as 10 bits. The actual frequency
6525 * is stored as a divider into a 100MHz clock, and the
6526 * mode pixel clock is stored in units of 1KHz.
6527 * Hence the bw of each lane in terms of the mode signal
6528 * is:
6529 */
6530 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6531
Damien Lespiau241bfc32013-09-25 16:45:37 +01006532 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006533
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006534 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006535 pipe_config->pipe_bpp);
6536
6537 pipe_config->fdi_lanes = lane;
6538
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006539 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006540 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006541
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006542 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6543 intel_crtc->pipe, pipe_config);
6544 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006545 pipe_config->pipe_bpp -= 2*3;
6546 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6547 pipe_config->pipe_bpp);
6548 needs_recompute = true;
6549 pipe_config->bw_constrained = true;
6550
6551 goto retry;
6552 }
6553
6554 if (needs_recompute)
6555 return RETRY;
6556
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006557 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006558}
6559
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006560static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6561 struct intel_crtc_state *pipe_config)
6562{
6563 if (pipe_config->pipe_bpp > 24)
6564 return false;
6565
6566 /* HSW can handle pixel rate up to cdclk? */
6567 if (IS_HASWELL(dev_priv->dev))
6568 return true;
6569
6570 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006571 * We compare against max which means we must take
6572 * the increased cdclk requirement into account when
6573 * calculating the new cdclk.
6574 *
6575 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006576 */
6577 return ilk_pipe_pixel_rate(pipe_config) <=
6578 dev_priv->max_cdclk_freq * 95 / 100;
6579}
6580
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006581static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006582 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006583{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006584 struct drm_device *dev = crtc->base.dev;
6585 struct drm_i915_private *dev_priv = dev->dev_private;
6586
Jani Nikulad330a952014-01-21 11:24:25 +02006587 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006588 hsw_crtc_supports_ips(crtc) &&
6589 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006590}
6591
Daniel Vettera43f6e02013-06-07 23:10:32 +02006592static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006593 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006594{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006595 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006596 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006597 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006598
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006599 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006600 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006601 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006602
6603 /*
6604 * Enable pixel doubling when the dot clock
6605 * is > 90% of the (display) core speed.
6606 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006607 * GDG double wide on either pipe,
6608 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006609 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006610 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006611 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006612 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006613 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006614 }
6615
Damien Lespiau241bfc32013-09-25 16:45:37 +01006616 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006617 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006618 }
Chris Wilson89749352010-09-12 18:25:19 +01006619
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006620 /*
6621 * Pipe horizontal size must be even in:
6622 * - DVO ganged mode
6623 * - LVDS dual channel mode
6624 * - Double wide pipe
6625 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006626 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006627 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6628 pipe_config->pipe_src_w &= ~1;
6629
Damien Lespiau8693a822013-05-03 18:48:11 +01006630 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6631 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006632 */
6633 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6634 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006635 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006636
Damien Lespiauf5adf942013-06-24 18:29:34 +01006637 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006638 hsw_compute_ips_config(crtc, pipe_config);
6639
Daniel Vetter877d48d2013-04-19 11:24:43 +02006640 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006641 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006642
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006643 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006644}
6645
Ville Syrjälä1652d192015-03-31 14:12:01 +03006646static int skylake_get_display_clock_speed(struct drm_device *dev)
6647{
6648 struct drm_i915_private *dev_priv = to_i915(dev);
6649 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6650 uint32_t cdctl = I915_READ(CDCLK_CTL);
6651 uint32_t linkrate;
6652
Damien Lespiau414355a2015-06-04 18:21:31 +01006653 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006654 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006655
6656 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6657 return 540000;
6658
6659 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006660 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006661
Damien Lespiau71cd8422015-04-30 16:39:17 +01006662 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6663 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006664 /* vco 8640 */
6665 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6666 case CDCLK_FREQ_450_432:
6667 return 432000;
6668 case CDCLK_FREQ_337_308:
6669 return 308570;
6670 case CDCLK_FREQ_675_617:
6671 return 617140;
6672 default:
6673 WARN(1, "Unknown cd freq selection\n");
6674 }
6675 } else {
6676 /* vco 8100 */
6677 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6678 case CDCLK_FREQ_450_432:
6679 return 450000;
6680 case CDCLK_FREQ_337_308:
6681 return 337500;
6682 case CDCLK_FREQ_675_617:
6683 return 675000;
6684 default:
6685 WARN(1, "Unknown cd freq selection\n");
6686 }
6687 }
6688
6689 /* error case, do as if DPLL0 isn't enabled */
6690 return 24000;
6691}
6692
6693static int broadwell_get_display_clock_speed(struct drm_device *dev)
6694{
6695 struct drm_i915_private *dev_priv = dev->dev_private;
6696 uint32_t lcpll = I915_READ(LCPLL_CTL);
6697 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6698
6699 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6700 return 800000;
6701 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6702 return 450000;
6703 else if (freq == LCPLL_CLK_FREQ_450)
6704 return 450000;
6705 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6706 return 540000;
6707 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6708 return 337500;
6709 else
6710 return 675000;
6711}
6712
6713static int haswell_get_display_clock_speed(struct drm_device *dev)
6714{
6715 struct drm_i915_private *dev_priv = dev->dev_private;
6716 uint32_t lcpll = I915_READ(LCPLL_CTL);
6717 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6718
6719 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6720 return 800000;
6721 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6722 return 450000;
6723 else if (freq == LCPLL_CLK_FREQ_450)
6724 return 450000;
6725 else if (IS_HSW_ULT(dev))
6726 return 337500;
6727 else
6728 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006729}
6730
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006731static int valleyview_get_display_clock_speed(struct drm_device *dev)
6732{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006733 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006734 u32 val;
6735 int divider;
6736
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006737 if (dev_priv->hpll_freq == 0)
6738 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6739
Ville Syrjäläa5805162015-05-26 20:42:30 +03006740 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006741 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006742 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006743
6744 divider = val & DISPLAY_FREQUENCY_VALUES;
6745
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006746 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6747 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6748 "cdclk change in progress\n");
6749
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006750 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006751}
6752
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006753static int ilk_get_display_clock_speed(struct drm_device *dev)
6754{
6755 return 450000;
6756}
6757
Jesse Barnese70236a2009-09-21 10:42:27 -07006758static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006759{
Jesse Barnese70236a2009-09-21 10:42:27 -07006760 return 400000;
6761}
Jesse Barnes79e53942008-11-07 14:24:08 -08006762
Jesse Barnese70236a2009-09-21 10:42:27 -07006763static int i915_get_display_clock_speed(struct drm_device *dev)
6764{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006765 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006766}
Jesse Barnes79e53942008-11-07 14:24:08 -08006767
Jesse Barnese70236a2009-09-21 10:42:27 -07006768static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6769{
6770 return 200000;
6771}
Jesse Barnes79e53942008-11-07 14:24:08 -08006772
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006773static int pnv_get_display_clock_speed(struct drm_device *dev)
6774{
6775 u16 gcfgc = 0;
6776
6777 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6778
6779 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6780 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006781 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006782 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006783 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006784 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006785 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006786 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6787 return 200000;
6788 default:
6789 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6790 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006791 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006792 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006793 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006794 }
6795}
6796
Jesse Barnese70236a2009-09-21 10:42:27 -07006797static int i915gm_get_display_clock_speed(struct drm_device *dev)
6798{
6799 u16 gcfgc = 0;
6800
6801 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6802
6803 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006804 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006805 else {
6806 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6807 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006808 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006809 default:
6810 case GC_DISPLAY_CLOCK_190_200_MHZ:
6811 return 190000;
6812 }
6813 }
6814}
Jesse Barnes79e53942008-11-07 14:24:08 -08006815
Jesse Barnese70236a2009-09-21 10:42:27 -07006816static int i865_get_display_clock_speed(struct drm_device *dev)
6817{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006818 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006819}
6820
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006821static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006822{
6823 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006824
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006825 /*
6826 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6827 * encoding is different :(
6828 * FIXME is this the right way to detect 852GM/852GMV?
6829 */
6830 if (dev->pdev->revision == 0x1)
6831 return 133333;
6832
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006833 pci_bus_read_config_word(dev->pdev->bus,
6834 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6835
Jesse Barnese70236a2009-09-21 10:42:27 -07006836 /* Assume that the hardware is in the high speed state. This
6837 * should be the default.
6838 */
6839 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6840 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006841 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006842 case GC_CLOCK_100_200:
6843 return 200000;
6844 case GC_CLOCK_166_250:
6845 return 250000;
6846 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006847 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006848 case GC_CLOCK_133_266:
6849 case GC_CLOCK_133_266_2:
6850 case GC_CLOCK_166_266:
6851 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006852 }
6853
6854 /* Shouldn't happen */
6855 return 0;
6856}
6857
6858static int i830_get_display_clock_speed(struct drm_device *dev)
6859{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006860 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006861}
6862
Ville Syrjälä34edce22015-05-22 11:22:33 +03006863static unsigned int intel_hpll_vco(struct drm_device *dev)
6864{
6865 struct drm_i915_private *dev_priv = dev->dev_private;
6866 static const unsigned int blb_vco[8] = {
6867 [0] = 3200000,
6868 [1] = 4000000,
6869 [2] = 5333333,
6870 [3] = 4800000,
6871 [4] = 6400000,
6872 };
6873 static const unsigned int pnv_vco[8] = {
6874 [0] = 3200000,
6875 [1] = 4000000,
6876 [2] = 5333333,
6877 [3] = 4800000,
6878 [4] = 2666667,
6879 };
6880 static const unsigned int cl_vco[8] = {
6881 [0] = 3200000,
6882 [1] = 4000000,
6883 [2] = 5333333,
6884 [3] = 6400000,
6885 [4] = 3333333,
6886 [5] = 3566667,
6887 [6] = 4266667,
6888 };
6889 static const unsigned int elk_vco[8] = {
6890 [0] = 3200000,
6891 [1] = 4000000,
6892 [2] = 5333333,
6893 [3] = 4800000,
6894 };
6895 static const unsigned int ctg_vco[8] = {
6896 [0] = 3200000,
6897 [1] = 4000000,
6898 [2] = 5333333,
6899 [3] = 6400000,
6900 [4] = 2666667,
6901 [5] = 4266667,
6902 };
6903 const unsigned int *vco_table;
6904 unsigned int vco;
6905 uint8_t tmp = 0;
6906
6907 /* FIXME other chipsets? */
6908 if (IS_GM45(dev))
6909 vco_table = ctg_vco;
6910 else if (IS_G4X(dev))
6911 vco_table = elk_vco;
6912 else if (IS_CRESTLINE(dev))
6913 vco_table = cl_vco;
6914 else if (IS_PINEVIEW(dev))
6915 vco_table = pnv_vco;
6916 else if (IS_G33(dev))
6917 vco_table = blb_vco;
6918 else
6919 return 0;
6920
6921 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6922
6923 vco = vco_table[tmp & 0x7];
6924 if (vco == 0)
6925 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6926 else
6927 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6928
6929 return vco;
6930}
6931
6932static int gm45_get_display_clock_speed(struct drm_device *dev)
6933{
6934 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6935 uint16_t tmp = 0;
6936
6937 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6938
6939 cdclk_sel = (tmp >> 12) & 0x1;
6940
6941 switch (vco) {
6942 case 2666667:
6943 case 4000000:
6944 case 5333333:
6945 return cdclk_sel ? 333333 : 222222;
6946 case 3200000:
6947 return cdclk_sel ? 320000 : 228571;
6948 default:
6949 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6950 return 222222;
6951 }
6952}
6953
6954static int i965gm_get_display_clock_speed(struct drm_device *dev)
6955{
6956 static const uint8_t div_3200[] = { 16, 10, 8 };
6957 static const uint8_t div_4000[] = { 20, 12, 10 };
6958 static const uint8_t div_5333[] = { 24, 16, 14 };
6959 const uint8_t *div_table;
6960 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6961 uint16_t tmp = 0;
6962
6963 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6964
6965 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6966
6967 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6968 goto fail;
6969
6970 switch (vco) {
6971 case 3200000:
6972 div_table = div_3200;
6973 break;
6974 case 4000000:
6975 div_table = div_4000;
6976 break;
6977 case 5333333:
6978 div_table = div_5333;
6979 break;
6980 default:
6981 goto fail;
6982 }
6983
6984 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6985
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006986fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006987 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6988 return 200000;
6989}
6990
6991static int g33_get_display_clock_speed(struct drm_device *dev)
6992{
6993 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6994 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6995 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6996 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6997 const uint8_t *div_table;
6998 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6999 uint16_t tmp = 0;
7000
7001 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7002
7003 cdclk_sel = (tmp >> 4) & 0x7;
7004
7005 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7006 goto fail;
7007
7008 switch (vco) {
7009 case 3200000:
7010 div_table = div_3200;
7011 break;
7012 case 4000000:
7013 div_table = div_4000;
7014 break;
7015 case 4800000:
7016 div_table = div_4800;
7017 break;
7018 case 5333333:
7019 div_table = div_5333;
7020 break;
7021 default:
7022 goto fail;
7023 }
7024
7025 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7026
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007027fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007028 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7029 return 190476;
7030}
7031
Zhenyu Wang2c072452009-06-05 15:38:42 +08007032static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007033intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007034{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007035 while (*num > DATA_LINK_M_N_MASK ||
7036 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007037 *num >>= 1;
7038 *den >>= 1;
7039 }
7040}
7041
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007042static void compute_m_n(unsigned int m, unsigned int n,
7043 uint32_t *ret_m, uint32_t *ret_n)
7044{
7045 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7046 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7047 intel_reduce_m_n_ratio(ret_m, ret_n);
7048}
7049
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007050void
7051intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7052 int pixel_clock, int link_clock,
7053 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007054{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007055 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007056
7057 compute_m_n(bits_per_pixel * pixel_clock,
7058 link_clock * nlanes * 8,
7059 &m_n->gmch_m, &m_n->gmch_n);
7060
7061 compute_m_n(pixel_clock, link_clock,
7062 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007063}
7064
Chris Wilsona7615032011-01-12 17:04:08 +00007065static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7066{
Jani Nikulad330a952014-01-21 11:24:25 +02007067 if (i915.panel_use_ssc >= 0)
7068 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007069 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007070 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007071}
7072
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007073static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7074 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007075{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007076 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007077 struct drm_i915_private *dev_priv = dev->dev_private;
7078 int refclk;
7079
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007080 WARN_ON(!crtc_state->base.state);
7081
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007082 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007083 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007084 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007085 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007086 refclk = dev_priv->vbt.lvds_ssc_freq;
7087 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007088 } else if (!IS_GEN2(dev)) {
7089 refclk = 96000;
7090 } else {
7091 refclk = 48000;
7092 }
7093
7094 return refclk;
7095}
7096
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007097static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007098{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007099 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007100}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007101
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007102static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7103{
7104 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007105}
7106
Daniel Vetterf47709a2013-03-28 10:42:02 +01007107static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007108 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007109 intel_clock_t *reduced_clock)
7110{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007111 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007112 u32 fp, fp2 = 0;
7113
7114 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007115 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007116 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007117 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007118 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007119 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007120 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007121 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007122 }
7123
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007124 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007125
Daniel Vetterf47709a2013-03-28 10:42:02 +01007126 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007127 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007128 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007129 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007130 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007131 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007132 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007133 }
7134}
7135
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007136static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7137 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007138{
7139 u32 reg_val;
7140
7141 /*
7142 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7143 * and set it to a reasonable value instead.
7144 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007145 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007146 reg_val &= 0xffffff00;
7147 reg_val |= 0x00000030;
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 &= 0x8cffffff;
7152 reg_val = 0x8c000000;
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
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007155 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007156 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007157 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007158
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007159 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007160 reg_val &= 0x00ffffff;
7161 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007162 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007163}
7164
Daniel Vetterb5518422013-05-03 11:49:48 +02007165static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7166 struct intel_link_m_n *m_n)
7167{
7168 struct drm_device *dev = crtc->base.dev;
7169 struct drm_i915_private *dev_priv = dev->dev_private;
7170 int pipe = crtc->pipe;
7171
Daniel Vettere3b95f12013-05-03 11:49:49 +02007172 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7173 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7174 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7175 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007176}
7177
7178static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007179 struct intel_link_m_n *m_n,
7180 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007181{
7182 struct drm_device *dev = crtc->base.dev;
7183 struct drm_i915_private *dev_priv = dev->dev_private;
7184 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007185 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007186
7187 if (INTEL_INFO(dev)->gen >= 5) {
7188 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7189 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7190 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7191 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007192 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7193 * for gen < 8) and if DRRS is supported (to make sure the
7194 * registers are not unnecessarily accessed).
7195 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307196 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007197 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007198 I915_WRITE(PIPE_DATA_M2(transcoder),
7199 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7200 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7201 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7202 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7203 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007204 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007205 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7206 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7207 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7208 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007209 }
7210}
7211
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307212void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007213{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307214 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7215
7216 if (m_n == M1_N1) {
7217 dp_m_n = &crtc->config->dp_m_n;
7218 dp_m2_n2 = &crtc->config->dp_m2_n2;
7219 } else if (m_n == M2_N2) {
7220
7221 /*
7222 * M2_N2 registers are not supported. Hence m2_n2 divider value
7223 * needs to be programmed into M1_N1.
7224 */
7225 dp_m_n = &crtc->config->dp_m2_n2;
7226 } else {
7227 DRM_ERROR("Unsupported divider value\n");
7228 return;
7229 }
7230
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007231 if (crtc->config->has_pch_encoder)
7232 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007233 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307234 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007235}
7236
Ville Syrjäläd288f652014-10-28 13:20:22 +02007237static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007238 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007239{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007240 u32 dpll, dpll_md;
7241
7242 /*
7243 * Enable DPIO clock input. We should never disable the reference
7244 * clock for pipe B, since VGA hotplug / manual detection depends
7245 * on it.
7246 */
7247 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7248 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7249 /* We should never disable this, set it here for state tracking */
7250 if (crtc->pipe == PIPE_B)
7251 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7252 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007253 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007254
Ville Syrjäläd288f652014-10-28 13:20:22 +02007255 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007256 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007257 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007258}
7259
Ville Syrjäläd288f652014-10-28 13:20:22 +02007260static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007261 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007262{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007263 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007264 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007265 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007266 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007267 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007268 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007269
Ville Syrjäläa5805162015-05-26 20:42:30 +03007270 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007271
Ville Syrjäläd288f652014-10-28 13:20:22 +02007272 bestn = pipe_config->dpll.n;
7273 bestm1 = pipe_config->dpll.m1;
7274 bestm2 = pipe_config->dpll.m2;
7275 bestp1 = pipe_config->dpll.p1;
7276 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007277
Jesse Barnes89b667f2013-04-18 14:51:36 -07007278 /* See eDP HDMI DPIO driver vbios notes doc */
7279
7280 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007281 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007282 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007283
7284 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007285 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007286
7287 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007288 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007289 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007290 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007291
7292 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007293 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007294
7295 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007296 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7297 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7298 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007299 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007300
7301 /*
7302 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7303 * but we don't support that).
7304 * Note: don't use the DAC post divider as it seems unstable.
7305 */
7306 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007307 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007308
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007309 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007310 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007311
Jesse Barnes89b667f2013-04-18 14:51:36 -07007312 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007313 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007314 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7315 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007316 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007317 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007318 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007319 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007320 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007321
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007322 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007323 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007324 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007325 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007326 0x0df40000);
7327 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007328 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007329 0x0df70000);
7330 } else { /* HDMI or VGA */
7331 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007332 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007333 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007334 0x0df70000);
7335 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007336 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007337 0x0df40000);
7338 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007339
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007340 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007341 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007342 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7343 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007344 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007345 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007346
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007347 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007348 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007349}
7350
Ville Syrjäläd288f652014-10-28 13:20:22 +02007351static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007352 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007353{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007354 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007355 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7356 DPLL_VCO_ENABLE;
7357 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007358 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007359
Ville Syrjäläd288f652014-10-28 13:20:22 +02007360 pipe_config->dpll_hw_state.dpll_md =
7361 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007362}
7363
Ville Syrjäläd288f652014-10-28 13:20:22 +02007364static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007365 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007366{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007367 struct drm_device *dev = crtc->base.dev;
7368 struct drm_i915_private *dev_priv = dev->dev_private;
7369 int pipe = crtc->pipe;
7370 int dpll_reg = DPLL(crtc->pipe);
7371 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307372 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007373 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307374 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307375 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007376
Ville Syrjäläd288f652014-10-28 13:20:22 +02007377 bestn = pipe_config->dpll.n;
7378 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7379 bestm1 = pipe_config->dpll.m1;
7380 bestm2 = pipe_config->dpll.m2 >> 22;
7381 bestp1 = pipe_config->dpll.p1;
7382 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307383 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307384 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307385 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007386
7387 /*
7388 * Enable Refclk and SSC
7389 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007390 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007391 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007392
Ville Syrjäläa5805162015-05-26 20:42:30 +03007393 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007394
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007395 /* p1 and p2 divider */
7396 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7397 5 << DPIO_CHV_S1_DIV_SHIFT |
7398 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7399 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7400 1 << DPIO_CHV_K_DIV_SHIFT);
7401
7402 /* Feedback post-divider - m2 */
7403 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7404
7405 /* Feedback refclk divider - n and m1 */
7406 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7407 DPIO_CHV_M1_DIV_BY_2 |
7408 1 << DPIO_CHV_N_DIV_SHIFT);
7409
7410 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307411 if (bestm2_frac)
7412 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007413
7414 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307415 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7416 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7417 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7418 if (bestm2_frac)
7419 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7420 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007421
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307422 /* Program digital lock detect threshold */
7423 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7424 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7425 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7426 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7427 if (!bestm2_frac)
7428 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7429 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7430
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007431 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307432 if (vco == 5400000) {
7433 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7434 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7435 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7436 tribuf_calcntr = 0x9;
7437 } else if (vco <= 6200000) {
7438 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7439 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7440 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7441 tribuf_calcntr = 0x9;
7442 } else if (vco <= 6480000) {
7443 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7444 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7445 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7446 tribuf_calcntr = 0x8;
7447 } else {
7448 /* Not supported. Apply the same limits as in the max case */
7449 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7450 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7451 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7452 tribuf_calcntr = 0;
7453 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007454 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7455
Ville Syrjälä968040b2015-03-11 22:52:08 +02007456 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307457 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7458 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7459 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7460
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007461 /* AFC Recal */
7462 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7463 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7464 DPIO_AFC_RECAL);
7465
Ville Syrjäläa5805162015-05-26 20:42:30 +03007466 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007467}
7468
Ville Syrjäläd288f652014-10-28 13:20:22 +02007469/**
7470 * vlv_force_pll_on - forcibly enable just the PLL
7471 * @dev_priv: i915 private structure
7472 * @pipe: pipe PLL to enable
7473 * @dpll: PLL configuration
7474 *
7475 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7476 * in cases where we need the PLL enabled even when @pipe is not going to
7477 * be enabled.
7478 */
7479void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7480 const struct dpll *dpll)
7481{
7482 struct intel_crtc *crtc =
7483 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007484 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007485 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007486 .pixel_multiplier = 1,
7487 .dpll = *dpll,
7488 };
7489
7490 if (IS_CHERRYVIEW(dev)) {
7491 chv_update_pll(crtc, &pipe_config);
7492 chv_prepare_pll(crtc, &pipe_config);
7493 chv_enable_pll(crtc, &pipe_config);
7494 } else {
7495 vlv_update_pll(crtc, &pipe_config);
7496 vlv_prepare_pll(crtc, &pipe_config);
7497 vlv_enable_pll(crtc, &pipe_config);
7498 }
7499}
7500
7501/**
7502 * vlv_force_pll_off - forcibly disable just the PLL
7503 * @dev_priv: i915 private structure
7504 * @pipe: pipe PLL to disable
7505 *
7506 * Disable the PLL for @pipe. To be used in cases where we need
7507 * the PLL enabled even when @pipe is not going to be enabled.
7508 */
7509void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7510{
7511 if (IS_CHERRYVIEW(dev))
7512 chv_disable_pll(to_i915(dev), pipe);
7513 else
7514 vlv_disable_pll(to_i915(dev), pipe);
7515}
7516
Daniel Vetterf47709a2013-03-28 10:42:02 +01007517static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007518 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007519 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007520 int num_connectors)
7521{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007522 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007523 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007524 u32 dpll;
7525 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007526 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007527
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007528 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307529
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007530 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7531 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007532
7533 dpll = DPLL_VGA_MODE_DIS;
7534
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007535 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007536 dpll |= DPLLB_MODE_LVDS;
7537 else
7538 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007539
Daniel Vetteref1b4602013-06-01 17:17:04 +02007540 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007541 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007542 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007543 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007544
7545 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007546 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007547
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007548 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007549 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007550
7551 /* compute bitmask from p1 value */
7552 if (IS_PINEVIEW(dev))
7553 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7554 else {
7555 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7556 if (IS_G4X(dev) && reduced_clock)
7557 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7558 }
7559 switch (clock->p2) {
7560 case 5:
7561 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7562 break;
7563 case 7:
7564 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7565 break;
7566 case 10:
7567 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7568 break;
7569 case 14:
7570 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7571 break;
7572 }
7573 if (INTEL_INFO(dev)->gen >= 4)
7574 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7575
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007576 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007577 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007578 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007579 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7580 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7581 else
7582 dpll |= PLL_REF_INPUT_DREFCLK;
7583
7584 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007585 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007586
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007587 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007588 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007589 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007590 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007591 }
7592}
7593
Daniel Vetterf47709a2013-03-28 10:42:02 +01007594static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007595 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007596 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007597 int num_connectors)
7598{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007599 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007600 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007601 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007602 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007603
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007604 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307605
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007606 dpll = DPLL_VGA_MODE_DIS;
7607
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007608 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007609 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7610 } else {
7611 if (clock->p1 == 2)
7612 dpll |= PLL_P1_DIVIDE_BY_TWO;
7613 else
7614 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7615 if (clock->p2 == 4)
7616 dpll |= PLL_P2_DIVIDE_BY_4;
7617 }
7618
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007619 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007620 dpll |= DPLL_DVO_2X_MODE;
7621
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007622 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007623 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7624 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7625 else
7626 dpll |= PLL_REF_INPUT_DREFCLK;
7627
7628 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007629 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007630}
7631
Daniel Vetter8a654f32013-06-01 17:16:22 +02007632static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007633{
7634 struct drm_device *dev = intel_crtc->base.dev;
7635 struct drm_i915_private *dev_priv = dev->dev_private;
7636 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007637 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007638 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007639 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007640 uint32_t crtc_vtotal, crtc_vblank_end;
7641 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007642
7643 /* We need to be careful not to changed the adjusted mode, for otherwise
7644 * the hw state checker will get angry at the mismatch. */
7645 crtc_vtotal = adjusted_mode->crtc_vtotal;
7646 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007647
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007648 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007649 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007650 crtc_vtotal -= 1;
7651 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007652
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007653 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007654 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7655 else
7656 vsyncshift = adjusted_mode->crtc_hsync_start -
7657 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007658 if (vsyncshift < 0)
7659 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007660 }
7661
7662 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007663 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007664
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007665 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007666 (adjusted_mode->crtc_hdisplay - 1) |
7667 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007668 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007669 (adjusted_mode->crtc_hblank_start - 1) |
7670 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007671 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007672 (adjusted_mode->crtc_hsync_start - 1) |
7673 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7674
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007675 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007676 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007677 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007678 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007679 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007680 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007681 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007682 (adjusted_mode->crtc_vsync_start - 1) |
7683 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7684
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007685 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7686 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7687 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7688 * bits. */
7689 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7690 (pipe == PIPE_B || pipe == PIPE_C))
7691 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7692
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007693 /* pipesrc controls the size that is scaled from, which should
7694 * always be the user's requested size.
7695 */
7696 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007697 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7698 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007699}
7700
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007701static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007702 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007703{
7704 struct drm_device *dev = crtc->base.dev;
7705 struct drm_i915_private *dev_priv = dev->dev_private;
7706 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7707 uint32_t tmp;
7708
7709 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007710 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7711 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007712 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007713 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7714 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007715 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007716 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7717 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007718
7719 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007720 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7721 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007722 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007723 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7724 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007725 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007726 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7727 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007728
7729 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007730 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7731 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7732 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007733 }
7734
7735 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007736 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7737 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7738
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007739 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7740 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007741}
7742
Daniel Vetterf6a83282014-02-11 15:28:57 -08007743void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007744 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007745{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007746 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7747 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7748 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7749 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007750
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007751 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7752 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7753 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7754 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007755
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007756 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007757
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007758 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7759 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007760}
7761
Daniel Vetter84b046f2013-02-19 18:48:54 +01007762static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7763{
7764 struct drm_device *dev = intel_crtc->base.dev;
7765 struct drm_i915_private *dev_priv = dev->dev_private;
7766 uint32_t pipeconf;
7767
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007768 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007769
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007770 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7771 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7772 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007773
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007774 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007775 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007776
Daniel Vetterff9ce462013-04-24 14:57:17 +02007777 /* only g4x and later have fancy bpc/dither controls */
7778 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007779 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007780 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007781 pipeconf |= PIPECONF_DITHER_EN |
7782 PIPECONF_DITHER_TYPE_SP;
7783
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007784 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007785 case 18:
7786 pipeconf |= PIPECONF_6BPC;
7787 break;
7788 case 24:
7789 pipeconf |= PIPECONF_8BPC;
7790 break;
7791 case 30:
7792 pipeconf |= PIPECONF_10BPC;
7793 break;
7794 default:
7795 /* Case prevented by intel_choose_pipe_bpp_dither. */
7796 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007797 }
7798 }
7799
7800 if (HAS_PIPE_CXSR(dev)) {
7801 if (intel_crtc->lowfreq_avail) {
7802 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7803 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7804 } else {
7805 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007806 }
7807 }
7808
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007809 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007810 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007811 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007812 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7813 else
7814 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7815 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007816 pipeconf |= PIPECONF_PROGRESSIVE;
7817
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007818 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007819 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007820
Daniel Vetter84b046f2013-02-19 18:48:54 +01007821 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7822 POSTING_READ(PIPECONF(intel_crtc->pipe));
7823}
7824
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007825static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7826 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007827{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007828 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007829 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007830 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007831 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007832 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007833 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007834 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007835 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007836 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007837 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007838 struct drm_connector_state *connector_state;
7839 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007840
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007841 memset(&crtc_state->dpll_hw_state, 0,
7842 sizeof(crtc_state->dpll_hw_state));
7843
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007844 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007845 if (connector_state->crtc != &crtc->base)
7846 continue;
7847
7848 encoder = to_intel_encoder(connector_state->best_encoder);
7849
Chris Wilson5eddb702010-09-11 13:48:45 +01007850 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007851 case INTEL_OUTPUT_LVDS:
7852 is_lvds = true;
7853 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007854 case INTEL_OUTPUT_DSI:
7855 is_dsi = true;
7856 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007857 default:
7858 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007859 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007860
Eric Anholtc751ce42010-03-25 11:48:48 -07007861 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007862 }
7863
Jani Nikulaf2335332013-09-13 11:03:09 +03007864 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007865 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007866
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007867 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007868 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007869
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007870 /*
7871 * Returns a set of divisors for the desired target clock with
7872 * the given refclk, or FALSE. The returned values represent
7873 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7874 * 2) / p1 / p2.
7875 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007876 limit = intel_limit(crtc_state, refclk);
7877 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007878 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007879 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007880 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007881 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7882 return -EINVAL;
7883 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007884
Jani Nikulaf2335332013-09-13 11:03:09 +03007885 if (is_lvds && dev_priv->lvds_downclock_avail) {
7886 /*
7887 * Ensure we match the reduced clock's P to the target
7888 * clock. If the clocks don't match, we can't switch
7889 * the display clock by using the FP0/FP1. In such case
7890 * we will disable the LVDS downclock feature.
7891 */
7892 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007893 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007894 dev_priv->lvds_downclock,
7895 refclk, &clock,
7896 &reduced_clock);
7897 }
7898 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007899 crtc_state->dpll.n = clock.n;
7900 crtc_state->dpll.m1 = clock.m1;
7901 crtc_state->dpll.m2 = clock.m2;
7902 crtc_state->dpll.p1 = clock.p1;
7903 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007904 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007905
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007906 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007907 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307908 has_reduced_clock ? &reduced_clock : NULL,
7909 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007910 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007911 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007912 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007913 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007914 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007915 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007916 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007917 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007918 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007919
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007920 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007921}
7922
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007923static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007924 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007925{
7926 struct drm_device *dev = crtc->base.dev;
7927 struct drm_i915_private *dev_priv = dev->dev_private;
7928 uint32_t tmp;
7929
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007930 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7931 return;
7932
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007933 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007934 if (!(tmp & PFIT_ENABLE))
7935 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007936
Daniel Vetter06922822013-07-11 13:35:40 +02007937 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007938 if (INTEL_INFO(dev)->gen < 4) {
7939 if (crtc->pipe != PIPE_B)
7940 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007941 } else {
7942 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7943 return;
7944 }
7945
Daniel Vetter06922822013-07-11 13:35:40 +02007946 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007947 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7948 if (INTEL_INFO(dev)->gen < 5)
7949 pipe_config->gmch_pfit.lvds_border_bits =
7950 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7951}
7952
Jesse Barnesacbec812013-09-20 11:29:32 -07007953static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007954 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007955{
7956 struct drm_device *dev = crtc->base.dev;
7957 struct drm_i915_private *dev_priv = dev->dev_private;
7958 int pipe = pipe_config->cpu_transcoder;
7959 intel_clock_t clock;
7960 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007961 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007962
Shobhit Kumarf573de52014-07-30 20:32:37 +05307963 /* In case of MIPI DPLL will not even be used */
7964 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7965 return;
7966
Ville Syrjäläa5805162015-05-26 20:42:30 +03007967 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007968 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007969 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007970
7971 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7972 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7973 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7974 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7975 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7976
Ville Syrjäläf6466282013-10-14 14:50:31 +03007977 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007978
Ville Syrjäläf6466282013-10-14 14:50:31 +03007979 /* clock.dot is the fast clock */
7980 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007981}
7982
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007983static void
7984i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7985 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007986{
7987 struct drm_device *dev = crtc->base.dev;
7988 struct drm_i915_private *dev_priv = dev->dev_private;
7989 u32 val, base, offset;
7990 int pipe = crtc->pipe, plane = crtc->plane;
7991 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007992 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007993 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007994 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007995
Damien Lespiau42a7b082015-02-05 19:35:13 +00007996 val = I915_READ(DSPCNTR(plane));
7997 if (!(val & DISPLAY_PLANE_ENABLE))
7998 return;
7999
Damien Lespiaud9806c92015-01-21 14:07:19 +00008000 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008001 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008002 DRM_DEBUG_KMS("failed to alloc fb\n");
8003 return;
8004 }
8005
Damien Lespiau1b842c82015-01-21 13:50:54 +00008006 fb = &intel_fb->base;
8007
Daniel Vetter18c52472015-02-10 17:16:09 +00008008 if (INTEL_INFO(dev)->gen >= 4) {
8009 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008010 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008011 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8012 }
8013 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008014
8015 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008016 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008017 fb->pixel_format = fourcc;
8018 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008019
8020 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008021 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008022 offset = I915_READ(DSPTILEOFF(plane));
8023 else
8024 offset = I915_READ(DSPLINOFF(plane));
8025 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8026 } else {
8027 base = I915_READ(DSPADDR(plane));
8028 }
8029 plane_config->base = base;
8030
8031 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008032 fb->width = ((val >> 16) & 0xfff) + 1;
8033 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008034
8035 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008036 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008037
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008038 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008039 fb->pixel_format,
8040 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008041
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008042 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008043
Damien Lespiau2844a922015-01-20 12:51:48 +00008044 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8045 pipe_name(pipe), plane, fb->width, fb->height,
8046 fb->bits_per_pixel, base, fb->pitches[0],
8047 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008048
Damien Lespiau2d140302015-02-05 17:22:18 +00008049 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008050}
8051
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008052static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008053 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008054{
8055 struct drm_device *dev = crtc->base.dev;
8056 struct drm_i915_private *dev_priv = dev->dev_private;
8057 int pipe = pipe_config->cpu_transcoder;
8058 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8059 intel_clock_t clock;
8060 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8061 int refclk = 100000;
8062
Ville Syrjäläa5805162015-05-26 20:42:30 +03008063 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008064 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8065 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8066 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8067 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008068 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008069
8070 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8071 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8072 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8073 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8074 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8075
8076 chv_clock(refclk, &clock);
8077
8078 /* clock.dot is the fast clock */
8079 pipe_config->port_clock = clock.dot / 5;
8080}
8081
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008082static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008083 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008084{
8085 struct drm_device *dev = crtc->base.dev;
8086 struct drm_i915_private *dev_priv = dev->dev_private;
8087 uint32_t tmp;
8088
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008089 if (!intel_display_power_is_enabled(dev_priv,
8090 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008091 return false;
8092
Daniel Vettere143a212013-07-04 12:01:15 +02008093 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008094 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008095
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008096 tmp = I915_READ(PIPECONF(crtc->pipe));
8097 if (!(tmp & PIPECONF_ENABLE))
8098 return false;
8099
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008100 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8101 switch (tmp & PIPECONF_BPC_MASK) {
8102 case PIPECONF_6BPC:
8103 pipe_config->pipe_bpp = 18;
8104 break;
8105 case PIPECONF_8BPC:
8106 pipe_config->pipe_bpp = 24;
8107 break;
8108 case PIPECONF_10BPC:
8109 pipe_config->pipe_bpp = 30;
8110 break;
8111 default:
8112 break;
8113 }
8114 }
8115
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008116 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8117 pipe_config->limited_color_range = true;
8118
Ville Syrjälä282740f2013-09-04 18:30:03 +03008119 if (INTEL_INFO(dev)->gen < 4)
8120 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8121
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008122 intel_get_pipe_timings(crtc, pipe_config);
8123
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008124 i9xx_get_pfit_config(crtc, pipe_config);
8125
Daniel Vetter6c49f242013-06-06 12:45:25 +02008126 if (INTEL_INFO(dev)->gen >= 4) {
8127 tmp = I915_READ(DPLL_MD(crtc->pipe));
8128 pipe_config->pixel_multiplier =
8129 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8130 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008131 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008132 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8133 tmp = I915_READ(DPLL(crtc->pipe));
8134 pipe_config->pixel_multiplier =
8135 ((tmp & SDVO_MULTIPLIER_MASK)
8136 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8137 } else {
8138 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8139 * port and will be fixed up in the encoder->get_config
8140 * function. */
8141 pipe_config->pixel_multiplier = 1;
8142 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008143 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8144 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008145 /*
8146 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8147 * on 830. Filter it out here so that we don't
8148 * report errors due to that.
8149 */
8150 if (IS_I830(dev))
8151 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8152
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008153 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8154 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008155 } else {
8156 /* Mask out read-only status bits. */
8157 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8158 DPLL_PORTC_READY_MASK |
8159 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008160 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008161
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008162 if (IS_CHERRYVIEW(dev))
8163 chv_crtc_clock_get(crtc, pipe_config);
8164 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008165 vlv_crtc_clock_get(crtc, pipe_config);
8166 else
8167 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008168
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008169 return true;
8170}
8171
Paulo Zanonidde86e22012-12-01 12:04:25 -02008172static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008173{
8174 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008175 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008176 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008177 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008178 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008179 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008180 bool has_ck505 = false;
8181 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008182
8183 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008184 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008185 switch (encoder->type) {
8186 case INTEL_OUTPUT_LVDS:
8187 has_panel = true;
8188 has_lvds = true;
8189 break;
8190 case INTEL_OUTPUT_EDP:
8191 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008192 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008193 has_cpu_edp = true;
8194 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008195 default:
8196 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008197 }
8198 }
8199
Keith Packard99eb6a02011-09-26 14:29:12 -07008200 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008201 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008202 can_ssc = has_ck505;
8203 } else {
8204 has_ck505 = false;
8205 can_ssc = true;
8206 }
8207
Imre Deak2de69052013-05-08 13:14:04 +03008208 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8209 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008210
8211 /* Ironlake: try to setup display ref clock before DPLL
8212 * enabling. This is only under driver's control after
8213 * PCH B stepping, previous chipset stepping should be
8214 * ignoring this setting.
8215 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008216 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008217
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008218 /* As we must carefully and slowly disable/enable each source in turn,
8219 * compute the final state we want first and check if we need to
8220 * make any changes at all.
8221 */
8222 final = val;
8223 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008224 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008225 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008226 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008227 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8228
8229 final &= ~DREF_SSC_SOURCE_MASK;
8230 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8231 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008232
Keith Packard199e5d72011-09-22 12:01:57 -07008233 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008234 final |= DREF_SSC_SOURCE_ENABLE;
8235
8236 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8237 final |= DREF_SSC1_ENABLE;
8238
8239 if (has_cpu_edp) {
8240 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8241 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8242 else
8243 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8244 } else
8245 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8246 } else {
8247 final |= DREF_SSC_SOURCE_DISABLE;
8248 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8249 }
8250
8251 if (final == val)
8252 return;
8253
8254 /* Always enable nonspread source */
8255 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8256
8257 if (has_ck505)
8258 val |= DREF_NONSPREAD_CK505_ENABLE;
8259 else
8260 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8261
8262 if (has_panel) {
8263 val &= ~DREF_SSC_SOURCE_MASK;
8264 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008265
Keith Packard199e5d72011-09-22 12:01:57 -07008266 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008267 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008268 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008269 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008270 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008271 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008272
8273 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008274 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008275 POSTING_READ(PCH_DREF_CONTROL);
8276 udelay(200);
8277
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008278 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008279
8280 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008281 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008282 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008283 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008284 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008285 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008286 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008287 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008288 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008289
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008290 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008291 POSTING_READ(PCH_DREF_CONTROL);
8292 udelay(200);
8293 } else {
8294 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8295
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008296 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008297
8298 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008299 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008300
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008301 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008302 POSTING_READ(PCH_DREF_CONTROL);
8303 udelay(200);
8304
8305 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008306 val &= ~DREF_SSC_SOURCE_MASK;
8307 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008308
8309 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008310 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008311
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008312 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008313 POSTING_READ(PCH_DREF_CONTROL);
8314 udelay(200);
8315 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008316
8317 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008318}
8319
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008320static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008321{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008322 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008323
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008324 tmp = I915_READ(SOUTH_CHICKEN2);
8325 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8326 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008327
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008328 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8329 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8330 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008331
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008332 tmp = I915_READ(SOUTH_CHICKEN2);
8333 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8334 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008335
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008336 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8337 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8338 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008339}
8340
8341/* WaMPhyProgramming:hsw */
8342static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8343{
8344 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008345
8346 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8347 tmp &= ~(0xFF << 24);
8348 tmp |= (0x12 << 24);
8349 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8350
Paulo Zanonidde86e22012-12-01 12:04:25 -02008351 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8352 tmp |= (1 << 11);
8353 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8354
8355 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8356 tmp |= (1 << 11);
8357 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8358
Paulo Zanonidde86e22012-12-01 12:04:25 -02008359 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8360 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8361 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8362
8363 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8364 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8365 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8366
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008367 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8368 tmp &= ~(7 << 13);
8369 tmp |= (5 << 13);
8370 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008371
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008372 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8373 tmp &= ~(7 << 13);
8374 tmp |= (5 << 13);
8375 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008376
8377 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8378 tmp &= ~0xFF;
8379 tmp |= 0x1C;
8380 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8381
8382 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8383 tmp &= ~0xFF;
8384 tmp |= 0x1C;
8385 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8386
8387 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8388 tmp &= ~(0xFF << 16);
8389 tmp |= (0x1C << 16);
8390 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8391
8392 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8393 tmp &= ~(0xFF << 16);
8394 tmp |= (0x1C << 16);
8395 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8396
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008397 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8398 tmp |= (1 << 27);
8399 intel_sbi_write(dev_priv, 0x20C4, 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, 0x21C4, SBI_MPHY);
8402 tmp |= (1 << 27);
8403 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008404
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008405 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8406 tmp &= ~(0xF << 28);
8407 tmp |= (4 << 28);
8408 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008409
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008410 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8411 tmp &= ~(0xF << 28);
8412 tmp |= (4 << 28);
8413 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008414}
8415
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008416/* Implements 3 different sequences from BSpec chapter "Display iCLK
8417 * Programming" based on the parameters passed:
8418 * - Sequence to enable CLKOUT_DP
8419 * - Sequence to enable CLKOUT_DP without spread
8420 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8421 */
8422static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8423 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008424{
8425 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008426 uint32_t reg, tmp;
8427
8428 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8429 with_spread = true;
8430 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8431 with_fdi, "LP PCH doesn't have FDI\n"))
8432 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008433
Ville Syrjäläa5805162015-05-26 20:42:30 +03008434 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008435
8436 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8437 tmp &= ~SBI_SSCCTL_DISABLE;
8438 tmp |= SBI_SSCCTL_PATHALT;
8439 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8440
8441 udelay(24);
8442
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008443 if (with_spread) {
8444 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8445 tmp &= ~SBI_SSCCTL_PATHALT;
8446 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008447
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008448 if (with_fdi) {
8449 lpt_reset_fdi_mphy(dev_priv);
8450 lpt_program_fdi_mphy(dev_priv);
8451 }
8452 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008453
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008454 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8455 SBI_GEN0 : SBI_DBUFF0;
8456 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8457 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8458 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008459
Ville Syrjäläa5805162015-05-26 20:42:30 +03008460 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008461}
8462
Paulo Zanoni47701c32013-07-23 11:19:25 -03008463/* Sequence to disable CLKOUT_DP */
8464static void lpt_disable_clkout_dp(struct drm_device *dev)
8465{
8466 struct drm_i915_private *dev_priv = dev->dev_private;
8467 uint32_t reg, tmp;
8468
Ville Syrjäläa5805162015-05-26 20:42:30 +03008469 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008470
8471 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8472 SBI_GEN0 : SBI_DBUFF0;
8473 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8474 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8475 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8476
8477 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8478 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8479 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8480 tmp |= SBI_SSCCTL_PATHALT;
8481 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8482 udelay(32);
8483 }
8484 tmp |= SBI_SSCCTL_DISABLE;
8485 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8486 }
8487
Ville Syrjäläa5805162015-05-26 20:42:30 +03008488 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008489}
8490
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008491static void lpt_init_pch_refclk(struct drm_device *dev)
8492{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008493 struct intel_encoder *encoder;
8494 bool has_vga = false;
8495
Damien Lespiaub2784e12014-08-05 11:29:37 +01008496 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008497 switch (encoder->type) {
8498 case INTEL_OUTPUT_ANALOG:
8499 has_vga = true;
8500 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008501 default:
8502 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008503 }
8504 }
8505
Paulo Zanoni47701c32013-07-23 11:19:25 -03008506 if (has_vga)
8507 lpt_enable_clkout_dp(dev, true, true);
8508 else
8509 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008510}
8511
Paulo Zanonidde86e22012-12-01 12:04:25 -02008512/*
8513 * Initialize reference clocks when the driver loads
8514 */
8515void intel_init_pch_refclk(struct drm_device *dev)
8516{
8517 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8518 ironlake_init_pch_refclk(dev);
8519 else if (HAS_PCH_LPT(dev))
8520 lpt_init_pch_refclk(dev);
8521}
8522
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008523static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008524{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008525 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008526 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008527 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008528 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008529 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008530 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008531 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008532 bool is_lvds = false;
8533
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008534 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008535 if (connector_state->crtc != crtc_state->base.crtc)
8536 continue;
8537
8538 encoder = to_intel_encoder(connector_state->best_encoder);
8539
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008540 switch (encoder->type) {
8541 case INTEL_OUTPUT_LVDS:
8542 is_lvds = true;
8543 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008544 default:
8545 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008546 }
8547 num_connectors++;
8548 }
8549
8550 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008551 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008552 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008553 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008554 }
8555
8556 return 120000;
8557}
8558
Daniel Vetter6ff93602013-04-19 11:24:36 +02008559static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008560{
8561 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8562 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8563 int pipe = intel_crtc->pipe;
8564 uint32_t val;
8565
Daniel Vetter78114072013-06-13 00:54:57 +02008566 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008567
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008568 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008569 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008570 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008571 break;
8572 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008573 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008574 break;
8575 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008576 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008577 break;
8578 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008579 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008580 break;
8581 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008582 /* Case prevented by intel_choose_pipe_bpp_dither. */
8583 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008584 }
8585
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008586 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008587 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8588
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008589 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008590 val |= PIPECONF_INTERLACED_ILK;
8591 else
8592 val |= PIPECONF_PROGRESSIVE;
8593
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008594 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008595 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008596
Paulo Zanonic8203562012-09-12 10:06:29 -03008597 I915_WRITE(PIPECONF(pipe), val);
8598 POSTING_READ(PIPECONF(pipe));
8599}
8600
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008601/*
8602 * Set up the pipe CSC unit.
8603 *
8604 * Currently only full range RGB to limited range RGB conversion
8605 * is supported, but eventually this should handle various
8606 * RGB<->YCbCr scenarios as well.
8607 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008608static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008609{
8610 struct drm_device *dev = crtc->dev;
8611 struct drm_i915_private *dev_priv = dev->dev_private;
8612 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8613 int pipe = intel_crtc->pipe;
8614 uint16_t coeff = 0x7800; /* 1.0 */
8615
8616 /*
8617 * TODO: Check what kind of values actually come out of the pipe
8618 * with these coeff/postoff values and adjust to get the best
8619 * accuracy. Perhaps we even need to take the bpc value into
8620 * consideration.
8621 */
8622
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008623 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008624 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8625
8626 /*
8627 * GY/GU and RY/RU should be the other way around according
8628 * to BSpec, but reality doesn't agree. Just set them up in
8629 * a way that results in the correct picture.
8630 */
8631 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8632 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8633
8634 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8635 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8636
8637 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8638 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8639
8640 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8641 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8642 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8643
8644 if (INTEL_INFO(dev)->gen > 6) {
8645 uint16_t postoff = 0;
8646
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008647 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008648 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008649
8650 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8651 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8652 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8653
8654 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8655 } else {
8656 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8657
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008658 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008659 mode |= CSC_BLACK_SCREEN_OFFSET;
8660
8661 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8662 }
8663}
8664
Daniel Vetter6ff93602013-04-19 11:24:36 +02008665static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008666{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008667 struct drm_device *dev = crtc->dev;
8668 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008670 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008671 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008672 uint32_t val;
8673
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008674 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008675
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008676 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008677 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8678
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008679 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008680 val |= PIPECONF_INTERLACED_ILK;
8681 else
8682 val |= PIPECONF_PROGRESSIVE;
8683
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008684 I915_WRITE(PIPECONF(cpu_transcoder), val);
8685 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008686
8687 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8688 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008689
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308690 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008691 val = 0;
8692
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008693 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008694 case 18:
8695 val |= PIPEMISC_DITHER_6_BPC;
8696 break;
8697 case 24:
8698 val |= PIPEMISC_DITHER_8_BPC;
8699 break;
8700 case 30:
8701 val |= PIPEMISC_DITHER_10_BPC;
8702 break;
8703 case 36:
8704 val |= PIPEMISC_DITHER_12_BPC;
8705 break;
8706 default:
8707 /* Case prevented by pipe_config_set_bpp. */
8708 BUG();
8709 }
8710
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008711 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008712 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8713
8714 I915_WRITE(PIPEMISC(pipe), val);
8715 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008716}
8717
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008718static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008719 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008720 intel_clock_t *clock,
8721 bool *has_reduced_clock,
8722 intel_clock_t *reduced_clock)
8723{
8724 struct drm_device *dev = crtc->dev;
8725 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008726 int refclk;
8727 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008728 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008729
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008730 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008731
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008732 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008733
8734 /*
8735 * Returns a set of divisors for the desired target clock with the given
8736 * refclk, or FALSE. The returned values represent the clock equation:
8737 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8738 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008739 limit = intel_limit(crtc_state, refclk);
8740 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008741 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008742 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008743 if (!ret)
8744 return false;
8745
8746 if (is_lvds && dev_priv->lvds_downclock_avail) {
8747 /*
8748 * Ensure we match the reduced clock's P to the target clock.
8749 * If the clocks don't match, we can't switch the display clock
8750 * by using the FP0/FP1. In such case we will disable the LVDS
8751 * downclock feature.
8752 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008753 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008754 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008755 dev_priv->lvds_downclock,
8756 refclk, clock,
8757 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008758 }
8759
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008760 return true;
8761}
8762
Paulo Zanonid4b19312012-11-29 11:29:32 -02008763int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8764{
8765 /*
8766 * Account for spread spectrum to avoid
8767 * oversubscribing the link. Max center spread
8768 * is 2.5%; use 5% for safety's sake.
8769 */
8770 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008771 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008772}
8773
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008774static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008775{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008776 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008777}
8778
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008779static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008780 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008781 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008782 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008783{
8784 struct drm_crtc *crtc = &intel_crtc->base;
8785 struct drm_device *dev = crtc->dev;
8786 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008787 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008788 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008789 struct drm_connector_state *connector_state;
8790 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008791 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008792 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008793 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008794
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008795 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008796 if (connector_state->crtc != crtc_state->base.crtc)
8797 continue;
8798
8799 encoder = to_intel_encoder(connector_state->best_encoder);
8800
8801 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008802 case INTEL_OUTPUT_LVDS:
8803 is_lvds = true;
8804 break;
8805 case INTEL_OUTPUT_SDVO:
8806 case INTEL_OUTPUT_HDMI:
8807 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008808 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008809 default:
8810 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008811 }
8812
8813 num_connectors++;
8814 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008815
Chris Wilsonc1858122010-12-03 21:35:48 +00008816 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008817 factor = 21;
8818 if (is_lvds) {
8819 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008820 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008821 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008822 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008823 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008824 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008825
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008826 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008827 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008828
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008829 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8830 *fp2 |= FP_CB_TUNE;
8831
Chris Wilson5eddb702010-09-11 13:48:45 +01008832 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008833
Eric Anholta07d6782011-03-30 13:01:08 -07008834 if (is_lvds)
8835 dpll |= DPLLB_MODE_LVDS;
8836 else
8837 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008838
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008839 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008840 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008841
8842 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008843 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008844 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008845 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008846
Eric Anholta07d6782011-03-30 13:01:08 -07008847 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008848 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008849 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008850 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008851
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008852 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008853 case 5:
8854 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8855 break;
8856 case 7:
8857 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8858 break;
8859 case 10:
8860 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8861 break;
8862 case 14:
8863 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8864 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008865 }
8866
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008867 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008868 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008869 else
8870 dpll |= PLL_REF_INPUT_DREFCLK;
8871
Daniel Vetter959e16d2013-06-05 13:34:21 +02008872 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008873}
8874
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008875static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8876 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008877{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008878 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008879 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008880 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008881 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008882 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008883 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008884
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008885 memset(&crtc_state->dpll_hw_state, 0,
8886 sizeof(crtc_state->dpll_hw_state));
8887
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008888 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008889
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008890 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8891 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8892
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008893 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008894 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008895 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008896 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8897 return -EINVAL;
8898 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008899 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008900 if (!crtc_state->clock_set) {
8901 crtc_state->dpll.n = clock.n;
8902 crtc_state->dpll.m1 = clock.m1;
8903 crtc_state->dpll.m2 = clock.m2;
8904 crtc_state->dpll.p1 = clock.p1;
8905 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008906 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008907
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008908 /* 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 +02008909 if (crtc_state->has_pch_encoder) {
8910 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008911 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008912 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008913
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008914 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008915 &fp, &reduced_clock,
8916 has_reduced_clock ? &fp2 : NULL);
8917
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008918 crtc_state->dpll_hw_state.dpll = dpll;
8919 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008920 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008921 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008922 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008923 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008924
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008925 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008926 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008927 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008928 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008929 return -EINVAL;
8930 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008931 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008932
Rodrigo Viviab585de2015-03-24 12:40:09 -07008933 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008934 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008935 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008936 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008937
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008938 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008939}
8940
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008941static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8942 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008943{
8944 struct drm_device *dev = crtc->base.dev;
8945 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008946 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008947
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008948 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8949 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8950 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8951 & ~TU_SIZE_MASK;
8952 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8953 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8954 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8955}
8956
8957static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8958 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008959 struct intel_link_m_n *m_n,
8960 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008961{
8962 struct drm_device *dev = crtc->base.dev;
8963 struct drm_i915_private *dev_priv = dev->dev_private;
8964 enum pipe pipe = crtc->pipe;
8965
8966 if (INTEL_INFO(dev)->gen >= 5) {
8967 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8968 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8969 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8970 & ~TU_SIZE_MASK;
8971 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8972 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8973 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008974 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8975 * gen < 8) and if DRRS is supported (to make sure the
8976 * registers are not unnecessarily read).
8977 */
8978 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008979 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008980 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8981 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8982 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8983 & ~TU_SIZE_MASK;
8984 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8985 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8986 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8987 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008988 } else {
8989 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8990 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8991 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8992 & ~TU_SIZE_MASK;
8993 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8994 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8995 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8996 }
8997}
8998
8999void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009000 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009001{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009002 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009003 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9004 else
9005 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009006 &pipe_config->dp_m_n,
9007 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009008}
9009
Daniel Vetter72419202013-04-04 13:28:53 +02009010static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009011 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009012{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009013 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009014 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009015}
9016
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009017static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009018 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009019{
9020 struct drm_device *dev = crtc->base.dev;
9021 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009022 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9023 uint32_t ps_ctrl = 0;
9024 int id = -1;
9025 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009026
Chandra Kondurua1b22782015-04-07 15:28:45 -07009027 /* find scaler attached to this pipe */
9028 for (i = 0; i < crtc->num_scalers; i++) {
9029 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9030 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9031 id = i;
9032 pipe_config->pch_pfit.enabled = true;
9033 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9034 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9035 break;
9036 }
9037 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009038
Chandra Kondurua1b22782015-04-07 15:28:45 -07009039 scaler_state->scaler_id = id;
9040 if (id >= 0) {
9041 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9042 } else {
9043 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009044 }
9045}
9046
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009047static void
9048skylake_get_initial_plane_config(struct intel_crtc *crtc,
9049 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009050{
9051 struct drm_device *dev = crtc->base.dev;
9052 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009053 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009054 int pipe = crtc->pipe;
9055 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009056 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009057 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009058 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009059
Damien Lespiaud9806c92015-01-21 14:07:19 +00009060 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009061 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009062 DRM_DEBUG_KMS("failed to alloc fb\n");
9063 return;
9064 }
9065
Damien Lespiau1b842c82015-01-21 13:50:54 +00009066 fb = &intel_fb->base;
9067
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009068 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009069 if (!(val & PLANE_CTL_ENABLE))
9070 goto error;
9071
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009072 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9073 fourcc = skl_format_to_fourcc(pixel_format,
9074 val & PLANE_CTL_ORDER_RGBX,
9075 val & PLANE_CTL_ALPHA_MASK);
9076 fb->pixel_format = fourcc;
9077 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9078
Damien Lespiau40f46282015-02-27 11:15:21 +00009079 tiling = val & PLANE_CTL_TILED_MASK;
9080 switch (tiling) {
9081 case PLANE_CTL_TILED_LINEAR:
9082 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9083 break;
9084 case PLANE_CTL_TILED_X:
9085 plane_config->tiling = I915_TILING_X;
9086 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9087 break;
9088 case PLANE_CTL_TILED_Y:
9089 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9090 break;
9091 case PLANE_CTL_TILED_YF:
9092 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9093 break;
9094 default:
9095 MISSING_CASE(tiling);
9096 goto error;
9097 }
9098
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009099 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9100 plane_config->base = base;
9101
9102 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9103
9104 val = I915_READ(PLANE_SIZE(pipe, 0));
9105 fb->height = ((val >> 16) & 0xfff) + 1;
9106 fb->width = ((val >> 0) & 0x1fff) + 1;
9107
9108 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009109 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9110 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009111 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9112
9113 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009114 fb->pixel_format,
9115 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009116
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009117 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009118
9119 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9120 pipe_name(pipe), fb->width, fb->height,
9121 fb->bits_per_pixel, base, fb->pitches[0],
9122 plane_config->size);
9123
Damien Lespiau2d140302015-02-05 17:22:18 +00009124 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009125 return;
9126
9127error:
9128 kfree(fb);
9129}
9130
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009131static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009132 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009133{
9134 struct drm_device *dev = crtc->base.dev;
9135 struct drm_i915_private *dev_priv = dev->dev_private;
9136 uint32_t tmp;
9137
9138 tmp = I915_READ(PF_CTL(crtc->pipe));
9139
9140 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009141 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009142 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9143 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009144
9145 /* We currently do not free assignements of panel fitters on
9146 * ivb/hsw (since we don't use the higher upscaling modes which
9147 * differentiates them) so just WARN about this case for now. */
9148 if (IS_GEN7(dev)) {
9149 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9150 PF_PIPE_SEL_IVB(crtc->pipe));
9151 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009152 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009153}
9154
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009155static void
9156ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9157 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009158{
9159 struct drm_device *dev = crtc->base.dev;
9160 struct drm_i915_private *dev_priv = dev->dev_private;
9161 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009162 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009163 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009164 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009165 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009166 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009167
Damien Lespiau42a7b082015-02-05 19:35:13 +00009168 val = I915_READ(DSPCNTR(pipe));
9169 if (!(val & DISPLAY_PLANE_ENABLE))
9170 return;
9171
Damien Lespiaud9806c92015-01-21 14:07:19 +00009172 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009173 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009174 DRM_DEBUG_KMS("failed to alloc fb\n");
9175 return;
9176 }
9177
Damien Lespiau1b842c82015-01-21 13:50:54 +00009178 fb = &intel_fb->base;
9179
Daniel Vetter18c52472015-02-10 17:16:09 +00009180 if (INTEL_INFO(dev)->gen >= 4) {
9181 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009182 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009183 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9184 }
9185 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009186
9187 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009188 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009189 fb->pixel_format = fourcc;
9190 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009192 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009193 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009194 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009195 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009196 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009197 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009198 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009199 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009200 }
9201 plane_config->base = base;
9202
9203 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009204 fb->width = ((val >> 16) & 0xfff) + 1;
9205 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009206
9207 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009208 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009209
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009210 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009211 fb->pixel_format,
9212 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009213
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009214 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009215
Damien Lespiau2844a922015-01-20 12:51:48 +00009216 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9217 pipe_name(pipe), fb->width, fb->height,
9218 fb->bits_per_pixel, base, fb->pitches[0],
9219 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009220
Damien Lespiau2d140302015-02-05 17:22:18 +00009221 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009222}
9223
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009224static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009225 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009226{
9227 struct drm_device *dev = crtc->base.dev;
9228 struct drm_i915_private *dev_priv = dev->dev_private;
9229 uint32_t tmp;
9230
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009231 if (!intel_display_power_is_enabled(dev_priv,
9232 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009233 return false;
9234
Daniel Vettere143a212013-07-04 12:01:15 +02009235 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009236 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009237
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009238 tmp = I915_READ(PIPECONF(crtc->pipe));
9239 if (!(tmp & PIPECONF_ENABLE))
9240 return false;
9241
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009242 switch (tmp & PIPECONF_BPC_MASK) {
9243 case PIPECONF_6BPC:
9244 pipe_config->pipe_bpp = 18;
9245 break;
9246 case PIPECONF_8BPC:
9247 pipe_config->pipe_bpp = 24;
9248 break;
9249 case PIPECONF_10BPC:
9250 pipe_config->pipe_bpp = 30;
9251 break;
9252 case PIPECONF_12BPC:
9253 pipe_config->pipe_bpp = 36;
9254 break;
9255 default:
9256 break;
9257 }
9258
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009259 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9260 pipe_config->limited_color_range = true;
9261
Daniel Vetterab9412b2013-05-03 11:49:46 +02009262 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009263 struct intel_shared_dpll *pll;
9264
Daniel Vetter88adfff2013-03-28 10:42:01 +01009265 pipe_config->has_pch_encoder = true;
9266
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009267 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9268 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9269 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009270
9271 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009272
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009273 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009274 pipe_config->shared_dpll =
9275 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009276 } else {
9277 tmp = I915_READ(PCH_DPLL_SEL);
9278 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9279 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9280 else
9281 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9282 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009283
9284 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9285
9286 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9287 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009288
9289 tmp = pipe_config->dpll_hw_state.dpll;
9290 pipe_config->pixel_multiplier =
9291 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9292 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009293
9294 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009295 } else {
9296 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009297 }
9298
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009299 intel_get_pipe_timings(crtc, pipe_config);
9300
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009301 ironlake_get_pfit_config(crtc, pipe_config);
9302
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009303 return true;
9304}
9305
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009306static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9307{
9308 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009309 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009310
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009311 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009312 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009313 pipe_name(crtc->pipe));
9314
Rob Clarke2c719b2014-12-15 13:56:32 -05009315 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9316 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9317 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9318 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9319 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9320 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009321 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009322 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009323 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009324 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009325 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009326 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009327 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009328 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009329 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009330
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009331 /*
9332 * In theory we can still leave IRQs enabled, as long as only the HPD
9333 * interrupts remain enabled. We used to check for that, but since it's
9334 * gen-specific and since we only disable LCPLL after we fully disable
9335 * the interrupts, the check below should be enough.
9336 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009337 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009338}
9339
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009340static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9341{
9342 struct drm_device *dev = dev_priv->dev;
9343
9344 if (IS_HASWELL(dev))
9345 return I915_READ(D_COMP_HSW);
9346 else
9347 return I915_READ(D_COMP_BDW);
9348}
9349
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009350static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9351{
9352 struct drm_device *dev = dev_priv->dev;
9353
9354 if (IS_HASWELL(dev)) {
9355 mutex_lock(&dev_priv->rps.hw_lock);
9356 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9357 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009358 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009359 mutex_unlock(&dev_priv->rps.hw_lock);
9360 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009361 I915_WRITE(D_COMP_BDW, val);
9362 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009363 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009364}
9365
9366/*
9367 * This function implements pieces of two sequences from BSpec:
9368 * - Sequence for display software to disable LCPLL
9369 * - Sequence for display software to allow package C8+
9370 * The steps implemented here are just the steps that actually touch the LCPLL
9371 * register. Callers should take care of disabling all the display engine
9372 * functions, doing the mode unset, fixing interrupts, etc.
9373 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009374static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9375 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009376{
9377 uint32_t val;
9378
9379 assert_can_disable_lcpll(dev_priv);
9380
9381 val = I915_READ(LCPLL_CTL);
9382
9383 if (switch_to_fclk) {
9384 val |= LCPLL_CD_SOURCE_FCLK;
9385 I915_WRITE(LCPLL_CTL, val);
9386
9387 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9388 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9389 DRM_ERROR("Switching to FCLK failed\n");
9390
9391 val = I915_READ(LCPLL_CTL);
9392 }
9393
9394 val |= LCPLL_PLL_DISABLE;
9395 I915_WRITE(LCPLL_CTL, val);
9396 POSTING_READ(LCPLL_CTL);
9397
9398 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9399 DRM_ERROR("LCPLL still locked\n");
9400
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009401 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009402 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009403 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009404 ndelay(100);
9405
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009406 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9407 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009408 DRM_ERROR("D_COMP RCOMP still in progress\n");
9409
9410 if (allow_power_down) {
9411 val = I915_READ(LCPLL_CTL);
9412 val |= LCPLL_POWER_DOWN_ALLOW;
9413 I915_WRITE(LCPLL_CTL, val);
9414 POSTING_READ(LCPLL_CTL);
9415 }
9416}
9417
9418/*
9419 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9420 * source.
9421 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009422static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009423{
9424 uint32_t val;
9425
9426 val = I915_READ(LCPLL_CTL);
9427
9428 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9429 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9430 return;
9431
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009432 /*
9433 * Make sure we're not on PC8 state before disabling PC8, otherwise
9434 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009435 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009436 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009437
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009438 if (val & LCPLL_POWER_DOWN_ALLOW) {
9439 val &= ~LCPLL_POWER_DOWN_ALLOW;
9440 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009441 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009442 }
9443
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009444 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009445 val |= D_COMP_COMP_FORCE;
9446 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009447 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009448
9449 val = I915_READ(LCPLL_CTL);
9450 val &= ~LCPLL_PLL_DISABLE;
9451 I915_WRITE(LCPLL_CTL, val);
9452
9453 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9454 DRM_ERROR("LCPLL not locked yet\n");
9455
9456 if (val & LCPLL_CD_SOURCE_FCLK) {
9457 val = I915_READ(LCPLL_CTL);
9458 val &= ~LCPLL_CD_SOURCE_FCLK;
9459 I915_WRITE(LCPLL_CTL, val);
9460
9461 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9462 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9463 DRM_ERROR("Switching back to LCPLL failed\n");
9464 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009465
Mika Kuoppala59bad942015-01-16 11:34:40 +02009466 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009467 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009468}
9469
Paulo Zanoni765dab672014-03-07 20:08:18 -03009470/*
9471 * Package states C8 and deeper are really deep PC states that can only be
9472 * reached when all the devices on the system allow it, so even if the graphics
9473 * device allows PC8+, it doesn't mean the system will actually get to these
9474 * states. Our driver only allows PC8+ when going into runtime PM.
9475 *
9476 * The requirements for PC8+ are that all the outputs are disabled, the power
9477 * well is disabled and most interrupts are disabled, and these are also
9478 * requirements for runtime PM. When these conditions are met, we manually do
9479 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9480 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9481 * hang the machine.
9482 *
9483 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9484 * the state of some registers, so when we come back from PC8+ we need to
9485 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9486 * need to take care of the registers kept by RC6. Notice that this happens even
9487 * if we don't put the device in PCI D3 state (which is what currently happens
9488 * because of the runtime PM support).
9489 *
9490 * For more, read "Display Sequences for Package C8" on the hardware
9491 * documentation.
9492 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009493void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009494{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009495 struct drm_device *dev = dev_priv->dev;
9496 uint32_t val;
9497
Paulo Zanonic67a4702013-08-19 13:18:09 -03009498 DRM_DEBUG_KMS("Enabling package C8+\n");
9499
Paulo Zanonic67a4702013-08-19 13:18:09 -03009500 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9501 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9502 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9503 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9504 }
9505
9506 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009507 hsw_disable_lcpll(dev_priv, true, true);
9508}
9509
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009510void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009511{
9512 struct drm_device *dev = dev_priv->dev;
9513 uint32_t val;
9514
Paulo Zanonic67a4702013-08-19 13:18:09 -03009515 DRM_DEBUG_KMS("Disabling package C8+\n");
9516
9517 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009518 lpt_init_pch_refclk(dev);
9519
9520 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9521 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9522 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9523 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9524 }
9525
9526 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009527}
9528
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009529static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309530{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009531 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309532 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009533 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309534 int req_cdclk;
9535
9536 /* see the comment in valleyview_modeset_global_resources */
9537 if (WARN_ON(max_pixclk < 0))
9538 return;
9539
9540 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9541
9542 if (req_cdclk != dev_priv->cdclk_freq)
9543 broxton_set_cdclk(dev, req_cdclk);
9544}
9545
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009546/* compute the max rate for new configuration */
9547static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9548{
9549 struct drm_device *dev = dev_priv->dev;
9550 struct intel_crtc *intel_crtc;
9551 struct drm_crtc *crtc;
9552 int max_pixel_rate = 0;
9553 int pixel_rate;
9554
9555 for_each_crtc(dev, crtc) {
9556 if (!crtc->state->enable)
9557 continue;
9558
9559 intel_crtc = to_intel_crtc(crtc);
9560 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9561
9562 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9563 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9564 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9565
9566 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9567 }
9568
9569 return max_pixel_rate;
9570}
9571
9572static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9573{
9574 struct drm_i915_private *dev_priv = dev->dev_private;
9575 uint32_t val, data;
9576 int ret;
9577
9578 if (WARN((I915_READ(LCPLL_CTL) &
9579 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9580 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9581 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9582 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9583 "trying to change cdclk frequency with cdclk not enabled\n"))
9584 return;
9585
9586 mutex_lock(&dev_priv->rps.hw_lock);
9587 ret = sandybridge_pcode_write(dev_priv,
9588 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9589 mutex_unlock(&dev_priv->rps.hw_lock);
9590 if (ret) {
9591 DRM_ERROR("failed to inform pcode about cdclk change\n");
9592 return;
9593 }
9594
9595 val = I915_READ(LCPLL_CTL);
9596 val |= LCPLL_CD_SOURCE_FCLK;
9597 I915_WRITE(LCPLL_CTL, val);
9598
9599 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9600 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9601 DRM_ERROR("Switching to FCLK failed\n");
9602
9603 val = I915_READ(LCPLL_CTL);
9604 val &= ~LCPLL_CLK_FREQ_MASK;
9605
9606 switch (cdclk) {
9607 case 450000:
9608 val |= LCPLL_CLK_FREQ_450;
9609 data = 0;
9610 break;
9611 case 540000:
9612 val |= LCPLL_CLK_FREQ_54O_BDW;
9613 data = 1;
9614 break;
9615 case 337500:
9616 val |= LCPLL_CLK_FREQ_337_5_BDW;
9617 data = 2;
9618 break;
9619 case 675000:
9620 val |= LCPLL_CLK_FREQ_675_BDW;
9621 data = 3;
9622 break;
9623 default:
9624 WARN(1, "invalid cdclk frequency\n");
9625 return;
9626 }
9627
9628 I915_WRITE(LCPLL_CTL, val);
9629
9630 val = I915_READ(LCPLL_CTL);
9631 val &= ~LCPLL_CD_SOURCE_FCLK;
9632 I915_WRITE(LCPLL_CTL, val);
9633
9634 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9635 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9636 DRM_ERROR("Switching back to LCPLL failed\n");
9637
9638 mutex_lock(&dev_priv->rps.hw_lock);
9639 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9640 mutex_unlock(&dev_priv->rps.hw_lock);
9641
9642 intel_update_cdclk(dev);
9643
9644 WARN(cdclk != dev_priv->cdclk_freq,
9645 "cdclk requested %d kHz but got %d kHz\n",
9646 cdclk, dev_priv->cdclk_freq);
9647}
9648
9649static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9650 int max_pixel_rate)
9651{
9652 int cdclk;
9653
9654 /*
9655 * FIXME should also account for plane ratio
9656 * once 64bpp pixel formats are supported.
9657 */
9658 if (max_pixel_rate > 540000)
9659 cdclk = 675000;
9660 else if (max_pixel_rate > 450000)
9661 cdclk = 540000;
9662 else if (max_pixel_rate > 337500)
9663 cdclk = 450000;
9664 else
9665 cdclk = 337500;
9666
9667 /*
9668 * FIXME move the cdclk caclulation to
9669 * compute_config() so we can fail gracegully.
9670 */
9671 if (cdclk > dev_priv->max_cdclk_freq) {
9672 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9673 cdclk, dev_priv->max_cdclk_freq);
9674 cdclk = dev_priv->max_cdclk_freq;
9675 }
9676
9677 return cdclk;
9678}
9679
9680static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9681{
9682 struct drm_i915_private *dev_priv = to_i915(state->dev);
9683 struct drm_crtc *crtc;
9684 struct drm_crtc_state *crtc_state;
9685 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9686 int cdclk, i;
9687
9688 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9689
9690 if (cdclk == dev_priv->cdclk_freq)
9691 return 0;
9692
9693 /* add all active pipes to the state */
9694 for_each_crtc(state->dev, crtc) {
9695 if (!crtc->state->enable)
9696 continue;
9697
9698 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9699 if (IS_ERR(crtc_state))
9700 return PTR_ERR(crtc_state);
9701 }
9702
9703 /* disable/enable all currently active pipes while we change cdclk */
9704 for_each_crtc_in_state(state, crtc, crtc_state, i)
9705 if (crtc_state->enable)
9706 crtc_state->mode_changed = true;
9707
9708 return 0;
9709}
9710
9711static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9712{
9713 struct drm_device *dev = state->dev;
9714 struct drm_i915_private *dev_priv = dev->dev_private;
9715 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9716 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9717
9718 if (req_cdclk != dev_priv->cdclk_freq)
9719 broadwell_set_cdclk(dev, req_cdclk);
9720}
9721
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009722static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9723 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009724{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009725 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009726 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009727
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009728 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009729
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009730 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009731}
9732
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309733static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9734 enum port port,
9735 struct intel_crtc_state *pipe_config)
9736{
9737 switch (port) {
9738 case PORT_A:
9739 pipe_config->ddi_pll_sel = SKL_DPLL0;
9740 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9741 break;
9742 case PORT_B:
9743 pipe_config->ddi_pll_sel = SKL_DPLL1;
9744 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9745 break;
9746 case PORT_C:
9747 pipe_config->ddi_pll_sel = SKL_DPLL2;
9748 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9749 break;
9750 default:
9751 DRM_ERROR("Incorrect port type\n");
9752 }
9753}
9754
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009755static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9756 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009757 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009758{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009759 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009760
9761 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9762 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9763
9764 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009765 case SKL_DPLL0:
9766 /*
9767 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9768 * of the shared DPLL framework and thus needs to be read out
9769 * separately
9770 */
9771 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9772 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9773 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009774 case SKL_DPLL1:
9775 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9776 break;
9777 case SKL_DPLL2:
9778 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9779 break;
9780 case SKL_DPLL3:
9781 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9782 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009783 }
9784}
9785
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009786static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9787 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009788 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009789{
9790 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9791
9792 switch (pipe_config->ddi_pll_sel) {
9793 case PORT_CLK_SEL_WRPLL1:
9794 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9795 break;
9796 case PORT_CLK_SEL_WRPLL2:
9797 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9798 break;
9799 }
9800}
9801
Daniel Vetter26804af2014-06-25 22:01:55 +03009802static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009803 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009804{
9805 struct drm_device *dev = crtc->base.dev;
9806 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009807 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009808 enum port port;
9809 uint32_t tmp;
9810
9811 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9812
9813 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9814
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009815 if (IS_SKYLAKE(dev))
9816 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309817 else if (IS_BROXTON(dev))
9818 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009819 else
9820 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009821
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009822 if (pipe_config->shared_dpll >= 0) {
9823 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9824
9825 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9826 &pipe_config->dpll_hw_state));
9827 }
9828
Daniel Vetter26804af2014-06-25 22:01:55 +03009829 /*
9830 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9831 * DDI E. So just check whether this pipe is wired to DDI E and whether
9832 * the PCH transcoder is on.
9833 */
Damien Lespiauca370452013-12-03 13:56:24 +00009834 if (INTEL_INFO(dev)->gen < 9 &&
9835 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009836 pipe_config->has_pch_encoder = true;
9837
9838 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9839 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9840 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9841
9842 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9843 }
9844}
9845
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009846static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009847 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009848{
9849 struct drm_device *dev = crtc->base.dev;
9850 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009851 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009852 uint32_t tmp;
9853
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009854 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009855 POWER_DOMAIN_PIPE(crtc->pipe)))
9856 return false;
9857
Daniel Vettere143a212013-07-04 12:01:15 +02009858 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009859 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9860
Daniel Vettereccb1402013-05-22 00:50:22 +02009861 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9862 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9863 enum pipe trans_edp_pipe;
9864 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9865 default:
9866 WARN(1, "unknown pipe linked to edp transcoder\n");
9867 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9868 case TRANS_DDI_EDP_INPUT_A_ON:
9869 trans_edp_pipe = PIPE_A;
9870 break;
9871 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9872 trans_edp_pipe = PIPE_B;
9873 break;
9874 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9875 trans_edp_pipe = PIPE_C;
9876 break;
9877 }
9878
9879 if (trans_edp_pipe == crtc->pipe)
9880 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9881 }
9882
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009883 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009884 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009885 return false;
9886
Daniel Vettereccb1402013-05-22 00:50:22 +02009887 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009888 if (!(tmp & PIPECONF_ENABLE))
9889 return false;
9890
Daniel Vetter26804af2014-06-25 22:01:55 +03009891 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009892
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009893 intel_get_pipe_timings(crtc, pipe_config);
9894
Chandra Kondurua1b22782015-04-07 15:28:45 -07009895 if (INTEL_INFO(dev)->gen >= 9) {
9896 skl_init_scalers(dev, crtc, pipe_config);
9897 }
9898
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009899 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009900
9901 if (INTEL_INFO(dev)->gen >= 9) {
9902 pipe_config->scaler_state.scaler_id = -1;
9903 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9904 }
9905
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009906 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009907 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009908 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009909 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009910 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009911 else
9912 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009913 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009914
Jesse Barnese59150d2014-01-07 13:30:45 -08009915 if (IS_HASWELL(dev))
9916 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9917 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009918
Clint Taylorebb69c92014-09-30 10:30:22 -07009919 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9920 pipe_config->pixel_multiplier =
9921 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9922 } else {
9923 pipe_config->pixel_multiplier = 1;
9924 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009925
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009926 return true;
9927}
9928
Chris Wilson560b85b2010-08-07 11:01:38 +01009929static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9930{
9931 struct drm_device *dev = crtc->dev;
9932 struct drm_i915_private *dev_priv = dev->dev_private;
9933 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009934 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009935
Ville Syrjälädc41c152014-08-13 11:57:05 +03009936 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009937 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9938 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009939 unsigned int stride = roundup_pow_of_two(width) * 4;
9940
9941 switch (stride) {
9942 default:
9943 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9944 width, stride);
9945 stride = 256;
9946 /* fallthrough */
9947 case 256:
9948 case 512:
9949 case 1024:
9950 case 2048:
9951 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009952 }
9953
Ville Syrjälädc41c152014-08-13 11:57:05 +03009954 cntl |= CURSOR_ENABLE |
9955 CURSOR_GAMMA_ENABLE |
9956 CURSOR_FORMAT_ARGB |
9957 CURSOR_STRIDE(stride);
9958
9959 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009960 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009961
Ville Syrjälädc41c152014-08-13 11:57:05 +03009962 if (intel_crtc->cursor_cntl != 0 &&
9963 (intel_crtc->cursor_base != base ||
9964 intel_crtc->cursor_size != size ||
9965 intel_crtc->cursor_cntl != cntl)) {
9966 /* On these chipsets we can only modify the base/size/stride
9967 * whilst the cursor is disabled.
9968 */
9969 I915_WRITE(_CURACNTR, 0);
9970 POSTING_READ(_CURACNTR);
9971 intel_crtc->cursor_cntl = 0;
9972 }
9973
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009974 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009975 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009976 intel_crtc->cursor_base = base;
9977 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009978
9979 if (intel_crtc->cursor_size != size) {
9980 I915_WRITE(CURSIZE, size);
9981 intel_crtc->cursor_size = size;
9982 }
9983
Chris Wilson4b0e3332014-05-30 16:35:26 +03009984 if (intel_crtc->cursor_cntl != cntl) {
9985 I915_WRITE(_CURACNTR, cntl);
9986 POSTING_READ(_CURACNTR);
9987 intel_crtc->cursor_cntl = cntl;
9988 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009989}
9990
9991static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9992{
9993 struct drm_device *dev = crtc->dev;
9994 struct drm_i915_private *dev_priv = dev->dev_private;
9995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9996 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009997 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009998
Chris Wilson4b0e3332014-05-30 16:35:26 +03009999 cntl = 0;
10000 if (base) {
10001 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -080010002 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010003 case 64:
10004 cntl |= CURSOR_MODE_64_ARGB_AX;
10005 break;
10006 case 128:
10007 cntl |= CURSOR_MODE_128_ARGB_AX;
10008 break;
10009 case 256:
10010 cntl |= CURSOR_MODE_256_ARGB_AX;
10011 break;
10012 default:
Matt Roper3dd512f2015-02-27 10:12:00 -080010013 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010014 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010015 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010016 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010017
10018 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10019 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +010010020 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010021
Matt Roper8e7d6882015-01-21 16:35:41 -080010022 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010023 cntl |= CURSOR_ROTATE_180;
10024
Chris Wilson4b0e3332014-05-30 16:35:26 +030010025 if (intel_crtc->cursor_cntl != cntl) {
10026 I915_WRITE(CURCNTR(pipe), cntl);
10027 POSTING_READ(CURCNTR(pipe));
10028 intel_crtc->cursor_cntl = cntl;
10029 }
10030
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010031 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010032 I915_WRITE(CURBASE(pipe), base);
10033 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010034
10035 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010036}
10037
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010038/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010039static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10040 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010041{
10042 struct drm_device *dev = crtc->dev;
10043 struct drm_i915_private *dev_priv = dev->dev_private;
10044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10045 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -070010046 int x = crtc->cursor_x;
10047 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010048 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010049
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010050 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010051 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010052
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010053 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010054 base = 0;
10055
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010056 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010057 base = 0;
10058
10059 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010060 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010061 base = 0;
10062
10063 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10064 x = -x;
10065 }
10066 pos |= x << CURSOR_X_SHIFT;
10067
10068 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010069 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010070 base = 0;
10071
10072 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10073 y = -y;
10074 }
10075 pos |= y << CURSOR_Y_SHIFT;
10076
Chris Wilson4b0e3332014-05-30 16:35:26 +030010077 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010078 return;
10079
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010080 I915_WRITE(CURPOS(pipe), pos);
10081
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010082 /* ILK+ do this automagically */
10083 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010084 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010085 base += (intel_crtc->base.cursor->state->crtc_h *
10086 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010087 }
10088
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010089 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010090 i845_update_cursor(crtc, base);
10091 else
10092 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010093}
10094
Ville Syrjälädc41c152014-08-13 11:57:05 +030010095static bool cursor_size_ok(struct drm_device *dev,
10096 uint32_t width, uint32_t height)
10097{
10098 if (width == 0 || height == 0)
10099 return false;
10100
10101 /*
10102 * 845g/865g are special in that they are only limited by
10103 * the width of their cursors, the height is arbitrary up to
10104 * the precision of the register. Everything else requires
10105 * square cursors, limited to a few power-of-two sizes.
10106 */
10107 if (IS_845G(dev) || IS_I865G(dev)) {
10108 if ((width & 63) != 0)
10109 return false;
10110
10111 if (width > (IS_845G(dev) ? 64 : 512))
10112 return false;
10113
10114 if (height > 1023)
10115 return false;
10116 } else {
10117 switch (width | height) {
10118 case 256:
10119 case 128:
10120 if (IS_GEN2(dev))
10121 return false;
10122 case 64:
10123 break;
10124 default:
10125 return false;
10126 }
10127 }
10128
10129 return true;
10130}
10131
Jesse Barnes79e53942008-11-07 14:24:08 -080010132static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010133 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010134{
James Simmons72034252010-08-03 01:33:19 +010010135 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010137
James Simmons72034252010-08-03 01:33:19 +010010138 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010139 intel_crtc->lut_r[i] = red[i] >> 8;
10140 intel_crtc->lut_g[i] = green[i] >> 8;
10141 intel_crtc->lut_b[i] = blue[i] >> 8;
10142 }
10143
10144 intel_crtc_load_lut(crtc);
10145}
10146
Jesse Barnes79e53942008-11-07 14:24:08 -080010147/* VESA 640x480x72Hz mode to set on the pipe */
10148static struct drm_display_mode load_detect_mode = {
10149 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10150 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10151};
10152
Daniel Vettera8bb6812014-02-10 18:00:39 +010010153struct drm_framebuffer *
10154__intel_framebuffer_create(struct drm_device *dev,
10155 struct drm_mode_fb_cmd2 *mode_cmd,
10156 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010157{
10158 struct intel_framebuffer *intel_fb;
10159 int ret;
10160
10161 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10162 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010163 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010164 return ERR_PTR(-ENOMEM);
10165 }
10166
10167 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010168 if (ret)
10169 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010170
10171 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010172err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010173 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010174 kfree(intel_fb);
10175
10176 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010177}
10178
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010179static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010180intel_framebuffer_create(struct drm_device *dev,
10181 struct drm_mode_fb_cmd2 *mode_cmd,
10182 struct drm_i915_gem_object *obj)
10183{
10184 struct drm_framebuffer *fb;
10185 int ret;
10186
10187 ret = i915_mutex_lock_interruptible(dev);
10188 if (ret)
10189 return ERR_PTR(ret);
10190 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10191 mutex_unlock(&dev->struct_mutex);
10192
10193 return fb;
10194}
10195
Chris Wilsond2dff872011-04-19 08:36:26 +010010196static u32
10197intel_framebuffer_pitch_for_width(int width, int bpp)
10198{
10199 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10200 return ALIGN(pitch, 64);
10201}
10202
10203static u32
10204intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10205{
10206 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010207 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010208}
10209
10210static struct drm_framebuffer *
10211intel_framebuffer_create_for_mode(struct drm_device *dev,
10212 struct drm_display_mode *mode,
10213 int depth, int bpp)
10214{
10215 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010216 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010217
10218 obj = i915_gem_alloc_object(dev,
10219 intel_framebuffer_size_for_mode(mode, bpp));
10220 if (obj == NULL)
10221 return ERR_PTR(-ENOMEM);
10222
10223 mode_cmd.width = mode->hdisplay;
10224 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010225 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10226 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010227 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010228
10229 return intel_framebuffer_create(dev, &mode_cmd, obj);
10230}
10231
10232static struct drm_framebuffer *
10233mode_fits_in_fbdev(struct drm_device *dev,
10234 struct drm_display_mode *mode)
10235{
Daniel Vetter4520f532013-10-09 09:18:51 +020010236#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010237 struct drm_i915_private *dev_priv = dev->dev_private;
10238 struct drm_i915_gem_object *obj;
10239 struct drm_framebuffer *fb;
10240
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010241 if (!dev_priv->fbdev)
10242 return NULL;
10243
10244 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010245 return NULL;
10246
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010247 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010248 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010249
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010250 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010251 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10252 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010253 return NULL;
10254
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010255 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010256 return NULL;
10257
10258 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010259#else
10260 return NULL;
10261#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010262}
10263
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010264static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10265 struct drm_crtc *crtc,
10266 struct drm_display_mode *mode,
10267 struct drm_framebuffer *fb,
10268 int x, int y)
10269{
10270 struct drm_plane_state *plane_state;
10271 int hdisplay, vdisplay;
10272 int ret;
10273
10274 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10275 if (IS_ERR(plane_state))
10276 return PTR_ERR(plane_state);
10277
10278 if (mode)
10279 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10280 else
10281 hdisplay = vdisplay = 0;
10282
10283 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10284 if (ret)
10285 return ret;
10286 drm_atomic_set_fb_for_plane(plane_state, fb);
10287 plane_state->crtc_x = 0;
10288 plane_state->crtc_y = 0;
10289 plane_state->crtc_w = hdisplay;
10290 plane_state->crtc_h = vdisplay;
10291 plane_state->src_x = x << 16;
10292 plane_state->src_y = y << 16;
10293 plane_state->src_w = hdisplay << 16;
10294 plane_state->src_h = vdisplay << 16;
10295
10296 return 0;
10297}
10298
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010299bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010300 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010301 struct intel_load_detect_pipe *old,
10302 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010303{
10304 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010305 struct intel_encoder *intel_encoder =
10306 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010307 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010308 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010309 struct drm_crtc *crtc = NULL;
10310 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010311 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010312 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010313 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010314 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010315 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010316 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010317
Chris Wilsond2dff872011-04-19 08:36:26 +010010318 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010319 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010320 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010321
Rob Clark51fd3712013-11-19 12:10:12 -050010322retry:
10323 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10324 if (ret)
10325 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010326
Jesse Barnes79e53942008-11-07 14:24:08 -080010327 /*
10328 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010329 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010330 * - if the connector already has an assigned crtc, use it (but make
10331 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010332 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010333 * - try to find the first unused crtc that can drive this connector,
10334 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010335 */
10336
10337 /* See if we already have a CRTC for this connector */
10338 if (encoder->crtc) {
10339 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010340
Rob Clark51fd3712013-11-19 12:10:12 -050010341 ret = drm_modeset_lock(&crtc->mutex, ctx);
10342 if (ret)
10343 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010344 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10345 if (ret)
10346 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010347
Daniel Vetter24218aa2012-08-12 19:27:11 +020010348 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010349 old->load_detect_temp = false;
10350
10351 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010352 if (connector->dpms != DRM_MODE_DPMS_ON)
10353 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010354
Chris Wilson71731882011-04-19 23:10:58 +010010355 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010356 }
10357
10358 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010359 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010360 i++;
10361 if (!(encoder->possible_crtcs & (1 << i)))
10362 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010363 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010364 continue;
10365 /* This can occur when applying the pipe A quirk on resume. */
10366 if (to_intel_crtc(possible_crtc)->new_enabled)
10367 continue;
10368
10369 crtc = possible_crtc;
10370 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010371 }
10372
10373 /*
10374 * If we didn't find an unused CRTC, don't use any.
10375 */
10376 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010377 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010378 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010379 }
10380
Rob Clark51fd3712013-11-19 12:10:12 -050010381 ret = drm_modeset_lock(&crtc->mutex, ctx);
10382 if (ret)
10383 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010384 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10385 if (ret)
10386 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010387 intel_encoder->new_crtc = to_intel_crtc(crtc);
10388 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010389
10390 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010391 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010392 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010393 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010394 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010395
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010396 state = drm_atomic_state_alloc(dev);
10397 if (!state)
10398 return false;
10399
10400 state->acquire_ctx = ctx;
10401
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010402 connector_state = drm_atomic_get_connector_state(state, connector);
10403 if (IS_ERR(connector_state)) {
10404 ret = PTR_ERR(connector_state);
10405 goto fail;
10406 }
10407
10408 connector_state->crtc = crtc;
10409 connector_state->best_encoder = &intel_encoder->base;
10410
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010411 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10412 if (IS_ERR(crtc_state)) {
10413 ret = PTR_ERR(crtc_state);
10414 goto fail;
10415 }
10416
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010417 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010418
Chris Wilson64927112011-04-20 07:25:26 +010010419 if (!mode)
10420 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010421
Chris Wilsond2dff872011-04-19 08:36:26 +010010422 /* We need a framebuffer large enough to accommodate all accesses
10423 * that the plane may generate whilst we perform load detection.
10424 * We can not rely on the fbcon either being present (we get called
10425 * during its initialisation to detect all boot displays, or it may
10426 * not even exist) or that it is large enough to satisfy the
10427 * requested mode.
10428 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010429 fb = mode_fits_in_fbdev(dev, mode);
10430 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010431 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010432 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10433 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010434 } else
10435 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010436 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010437 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010438 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010439 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010440
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010441 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10442 if (ret)
10443 goto fail;
10444
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010445 drm_mode_copy(&crtc_state->base.mode, mode);
10446
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010447 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010448 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010449 if (old->release_fb)
10450 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010451 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010452 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010453 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010454
Jesse Barnes79e53942008-11-07 14:24:08 -080010455 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010456 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010457 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010458
10459 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010460 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010461fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010462 drm_atomic_state_free(state);
10463 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010464
Rob Clark51fd3712013-11-19 12:10:12 -050010465 if (ret == -EDEADLK) {
10466 drm_modeset_backoff(ctx);
10467 goto retry;
10468 }
10469
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010470 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010471}
10472
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010473void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010474 struct intel_load_detect_pipe *old,
10475 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010476{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010477 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010478 struct intel_encoder *intel_encoder =
10479 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010480 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010481 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010482 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010483 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010484 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010485 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010486 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010487
Chris Wilsond2dff872011-04-19 08:36:26 +010010488 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010489 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010490 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010491
Chris Wilson8261b192011-04-19 23:18:09 +010010492 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010493 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010494 if (!state)
10495 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010496
10497 state->acquire_ctx = ctx;
10498
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010499 connector_state = drm_atomic_get_connector_state(state, connector);
10500 if (IS_ERR(connector_state))
10501 goto fail;
10502
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010503 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10504 if (IS_ERR(crtc_state))
10505 goto fail;
10506
Daniel Vetterfc303102012-07-09 10:40:58 +020010507 to_intel_connector(connector)->new_encoder = NULL;
10508 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010509 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010510
10511 connector_state->best_encoder = NULL;
10512 connector_state->crtc = NULL;
10513
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010514 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010515
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010516 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10517 0, 0);
10518 if (ret)
10519 goto fail;
10520
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010521 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010522 if (ret)
10523 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010524
Daniel Vetter36206362012-12-10 20:42:17 +010010525 if (old->release_fb) {
10526 drm_framebuffer_unregister_private(old->release_fb);
10527 drm_framebuffer_unreference(old->release_fb);
10528 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010529
Chris Wilson0622a532011-04-21 09:32:11 +010010530 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010531 }
10532
Eric Anholtc751ce42010-03-25 11:48:48 -070010533 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010534 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10535 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010536
10537 return;
10538fail:
10539 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10540 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010541}
10542
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010543static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010544 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010545{
10546 struct drm_i915_private *dev_priv = dev->dev_private;
10547 u32 dpll = pipe_config->dpll_hw_state.dpll;
10548
10549 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010550 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010551 else if (HAS_PCH_SPLIT(dev))
10552 return 120000;
10553 else if (!IS_GEN2(dev))
10554 return 96000;
10555 else
10556 return 48000;
10557}
10558
Jesse Barnes79e53942008-11-07 14:24:08 -080010559/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010560static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010561 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010562{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010563 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010564 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010565 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010566 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010567 u32 fp;
10568 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010569 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010570
10571 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010572 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010573 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010574 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010575
10576 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010577 if (IS_PINEVIEW(dev)) {
10578 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10579 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010580 } else {
10581 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10582 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10583 }
10584
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010585 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010586 if (IS_PINEVIEW(dev))
10587 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10588 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010589 else
10590 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010591 DPLL_FPA01_P1_POST_DIV_SHIFT);
10592
10593 switch (dpll & DPLL_MODE_MASK) {
10594 case DPLLB_MODE_DAC_SERIAL:
10595 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10596 5 : 10;
10597 break;
10598 case DPLLB_MODE_LVDS:
10599 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10600 7 : 14;
10601 break;
10602 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010603 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010604 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010605 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010606 }
10607
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010608 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010609 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010610 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010611 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010612 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010613 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010614 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010615
10616 if (is_lvds) {
10617 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10618 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010619
10620 if (lvds & LVDS_CLKB_POWER_UP)
10621 clock.p2 = 7;
10622 else
10623 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010624 } else {
10625 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10626 clock.p1 = 2;
10627 else {
10628 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10629 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10630 }
10631 if (dpll & PLL_P2_DIVIDE_BY_4)
10632 clock.p2 = 4;
10633 else
10634 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010635 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010636
10637 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010638 }
10639
Ville Syrjälä18442d02013-09-13 16:00:08 +030010640 /*
10641 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010642 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010643 * encoder's get_config() function.
10644 */
10645 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010646}
10647
Ville Syrjälä6878da02013-09-13 15:59:11 +030010648int intel_dotclock_calculate(int link_freq,
10649 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010650{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010651 /*
10652 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010653 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010654 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010655 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010656 *
10657 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010658 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010659 */
10660
Ville Syrjälä6878da02013-09-13 15:59:11 +030010661 if (!m_n->link_n)
10662 return 0;
10663
10664 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10665}
10666
Ville Syrjälä18442d02013-09-13 16:00:08 +030010667static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010668 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010669{
10670 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010671
10672 /* read out port_clock from the DPLL */
10673 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010674
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010675 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010676 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010677 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010678 * agree once we know their relationship in the encoder's
10679 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010680 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010681 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010682 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10683 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010684}
10685
10686/** Returns the currently programmed mode of the given pipe. */
10687struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10688 struct drm_crtc *crtc)
10689{
Jesse Barnes548f2452011-02-17 10:40:53 -080010690 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010692 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010693 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010694 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010695 int htot = I915_READ(HTOTAL(cpu_transcoder));
10696 int hsync = I915_READ(HSYNC(cpu_transcoder));
10697 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10698 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010699 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010700
10701 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10702 if (!mode)
10703 return NULL;
10704
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010705 /*
10706 * Construct a pipe_config sufficient for getting the clock info
10707 * back out of crtc_clock_get.
10708 *
10709 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10710 * to use a real value here instead.
10711 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010712 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010713 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010714 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10715 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10716 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010717 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10718
Ville Syrjälä773ae032013-09-23 17:48:20 +030010719 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010720 mode->hdisplay = (htot & 0xffff) + 1;
10721 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10722 mode->hsync_start = (hsync & 0xffff) + 1;
10723 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10724 mode->vdisplay = (vtot & 0xffff) + 1;
10725 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10726 mode->vsync_start = (vsync & 0xffff) + 1;
10727 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10728
10729 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010730
10731 return mode;
10732}
10733
Jesse Barnes652c3932009-08-17 13:31:43 -070010734static void intel_decrease_pllclock(struct drm_crtc *crtc)
10735{
10736 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010737 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010739
Sonika Jindalbaff2962014-07-22 11:16:35 +053010740 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010741 return;
10742
10743 if (!dev_priv->lvds_downclock_avail)
10744 return;
10745
10746 /*
10747 * Since this is called by a timer, we should never get here in
10748 * the manual case.
10749 */
10750 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010751 int pipe = intel_crtc->pipe;
10752 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010753 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010754
Zhao Yakui44d98a62009-10-09 11:39:40 +080010755 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010756
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010757 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010758
Chris Wilson074b5e12012-05-02 12:07:06 +010010759 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010760 dpll |= DISPLAY_RATE_SELECT_FPA1;
10761 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010762 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010763 dpll = I915_READ(dpll_reg);
10764 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010765 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010766 }
10767
10768}
10769
Chris Wilsonf047e392012-07-21 12:31:41 +010010770void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010771{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010772 struct drm_i915_private *dev_priv = dev->dev_private;
10773
Chris Wilsonf62a0072014-02-21 17:55:39 +000010774 if (dev_priv->mm.busy)
10775 return;
10776
Paulo Zanoni43694d62014-03-07 20:08:08 -030010777 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010778 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010779 if (INTEL_INFO(dev)->gen >= 6)
10780 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010781 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010782}
10783
10784void intel_mark_idle(struct drm_device *dev)
10785{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010786 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010787 struct drm_crtc *crtc;
10788
Chris Wilsonf62a0072014-02-21 17:55:39 +000010789 if (!dev_priv->mm.busy)
10790 return;
10791
10792 dev_priv->mm.busy = false;
10793
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010794 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010795 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010796 continue;
10797
10798 intel_decrease_pllclock(crtc);
10799 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010800
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010801 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010802 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010803
Paulo Zanoni43694d62014-03-07 20:08:08 -030010804 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010805}
10806
Jesse Barnes79e53942008-11-07 14:24:08 -080010807static void intel_crtc_destroy(struct drm_crtc *crtc)
10808{
10809 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010810 struct drm_device *dev = crtc->dev;
10811 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010812
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010813 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010814 work = intel_crtc->unpin_work;
10815 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010816 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010817
10818 if (work) {
10819 cancel_work_sync(&work->work);
10820 kfree(work);
10821 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010822
10823 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010824
Jesse Barnes79e53942008-11-07 14:24:08 -080010825 kfree(intel_crtc);
10826}
10827
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010828static void intel_unpin_work_fn(struct work_struct *__work)
10829{
10830 struct intel_unpin_work *work =
10831 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010832 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010833 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010834
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010835 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010836 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010837 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010838
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010839 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010840
10841 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010842 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010843 mutex_unlock(&dev->struct_mutex);
10844
Daniel Vetterf99d7062014-06-19 16:01:59 +020010845 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010846 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010847
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010848 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10849 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10850
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010851 kfree(work);
10852}
10853
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010854static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010855 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010856{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10858 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010859 unsigned long flags;
10860
10861 /* Ignore early vblank irqs */
10862 if (intel_crtc == NULL)
10863 return;
10864
Daniel Vetterf3260382014-09-15 14:55:23 +020010865 /*
10866 * This is called both by irq handlers and the reset code (to complete
10867 * lost pageflips) so needs the full irqsave spinlocks.
10868 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010869 spin_lock_irqsave(&dev->event_lock, flags);
10870 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010871
10872 /* Ensure we don't miss a work->pending update ... */
10873 smp_rmb();
10874
10875 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010876 spin_unlock_irqrestore(&dev->event_lock, flags);
10877 return;
10878 }
10879
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010880 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010881
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010882 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010883}
10884
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010885void intel_finish_page_flip(struct drm_device *dev, int pipe)
10886{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010887 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010888 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10889
Mario Kleiner49b14a52010-12-09 07:00:07 +010010890 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010891}
10892
10893void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10894{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010895 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010896 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10897
Mario Kleiner49b14a52010-12-09 07:00:07 +010010898 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010899}
10900
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010901/* Is 'a' after or equal to 'b'? */
10902static bool g4x_flip_count_after_eq(u32 a, u32 b)
10903{
10904 return !((a - b) & 0x80000000);
10905}
10906
10907static bool page_flip_finished(struct intel_crtc *crtc)
10908{
10909 struct drm_device *dev = crtc->base.dev;
10910 struct drm_i915_private *dev_priv = dev->dev_private;
10911
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010912 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10913 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10914 return true;
10915
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010916 /*
10917 * The relevant registers doen't exist on pre-ctg.
10918 * As the flip done interrupt doesn't trigger for mmio
10919 * flips on gmch platforms, a flip count check isn't
10920 * really needed there. But since ctg has the registers,
10921 * include it in the check anyway.
10922 */
10923 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10924 return true;
10925
10926 /*
10927 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10928 * used the same base address. In that case the mmio flip might
10929 * have completed, but the CS hasn't even executed the flip yet.
10930 *
10931 * A flip count check isn't enough as the CS might have updated
10932 * the base address just after start of vblank, but before we
10933 * managed to process the interrupt. This means we'd complete the
10934 * CS flip too soon.
10935 *
10936 * Combining both checks should get us a good enough result. It may
10937 * still happen that the CS flip has been executed, but has not
10938 * yet actually completed. But in case the base address is the same
10939 * anyway, we don't really care.
10940 */
10941 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10942 crtc->unpin_work->gtt_offset &&
10943 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10944 crtc->unpin_work->flip_count);
10945}
10946
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010947void intel_prepare_page_flip(struct drm_device *dev, int plane)
10948{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010949 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010950 struct intel_crtc *intel_crtc =
10951 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10952 unsigned long flags;
10953
Daniel Vetterf3260382014-09-15 14:55:23 +020010954
10955 /*
10956 * This is called both by irq handlers and the reset code (to complete
10957 * lost pageflips) so needs the full irqsave spinlocks.
10958 *
10959 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010960 * generate a page-flip completion irq, i.e. every modeset
10961 * is also accompanied by a spurious intel_prepare_page_flip().
10962 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010963 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010964 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010965 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010966 spin_unlock_irqrestore(&dev->event_lock, flags);
10967}
10968
Robin Schroereba905b2014-05-18 02:24:50 +020010969static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010970{
10971 /* Ensure that the work item is consistent when activating it ... */
10972 smp_wmb();
10973 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10974 /* and that it is marked active as soon as the irq could fire. */
10975 smp_wmb();
10976}
10977
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010978static int intel_gen2_queue_flip(struct drm_device *dev,
10979 struct drm_crtc *crtc,
10980 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010981 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010982 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010983 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010984{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010986 u32 flip_mask;
10987 int ret;
10988
Daniel Vetter6d90c952012-04-26 23:28:05 +020010989 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010990 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010991 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010992
10993 /* Can't queue multiple flips, so wait for the previous
10994 * one to finish before executing the next.
10995 */
10996 if (intel_crtc->plane)
10997 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10998 else
10999 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011000 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11001 intel_ring_emit(ring, MI_NOOP);
11002 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11003 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11004 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011005 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011006 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011007
11008 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011009 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011010 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011011}
11012
11013static int intel_gen3_queue_flip(struct drm_device *dev,
11014 struct drm_crtc *crtc,
11015 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011016 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011017 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011018 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011019{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011021 u32 flip_mask;
11022 int ret;
11023
Daniel Vetter6d90c952012-04-26 23:28:05 +020011024 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011025 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011026 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011027
11028 if (intel_crtc->plane)
11029 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11030 else
11031 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011032 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11033 intel_ring_emit(ring, MI_NOOP);
11034 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11035 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11036 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011037 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011038 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011039
Chris Wilsone7d841c2012-12-03 11:36:30 +000011040 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011041 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011042 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011043}
11044
11045static int intel_gen4_queue_flip(struct drm_device *dev,
11046 struct drm_crtc *crtc,
11047 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011048 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011049 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011050 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011051{
11052 struct drm_i915_private *dev_priv = dev->dev_private;
11053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11054 uint32_t pf, pipesrc;
11055 int ret;
11056
Daniel Vetter6d90c952012-04-26 23:28:05 +020011057 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011058 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011059 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011060
11061 /* i965+ uses the linear or tiled offsets from the
11062 * Display Registers (which do not change across a page-flip)
11063 * so we need only reprogram the base address.
11064 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011065 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11066 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11067 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011068 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011069 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011070
11071 /* XXX Enabling the panel-fitter across page-flip is so far
11072 * untested on non-native modes, so ignore it for now.
11073 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11074 */
11075 pf = 0;
11076 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011077 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011078
11079 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011080 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011081 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011082}
11083
11084static int intel_gen6_queue_flip(struct drm_device *dev,
11085 struct drm_crtc *crtc,
11086 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011087 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011088 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011089 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011090{
11091 struct drm_i915_private *dev_priv = dev->dev_private;
11092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11093 uint32_t pf, pipesrc;
11094 int ret;
11095
Daniel Vetter6d90c952012-04-26 23:28:05 +020011096 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011097 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011098 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011099
Daniel Vetter6d90c952012-04-26 23:28:05 +020011100 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11101 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11102 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011103 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011104
Chris Wilson99d9acd2012-04-17 20:37:00 +010011105 /* Contrary to the suggestions in the documentation,
11106 * "Enable Panel Fitter" does not seem to be required when page
11107 * flipping with a non-native mode, and worse causes a normal
11108 * modeset to fail.
11109 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11110 */
11111 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011112 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011113 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011114
11115 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011116 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011117 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011118}
11119
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011120static int intel_gen7_queue_flip(struct drm_device *dev,
11121 struct drm_crtc *crtc,
11122 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011123 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011124 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011125 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011126{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011128 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011129 int len, ret;
11130
Robin Schroereba905b2014-05-18 02:24:50 +020011131 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011132 case PLANE_A:
11133 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11134 break;
11135 case PLANE_B:
11136 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11137 break;
11138 case PLANE_C:
11139 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11140 break;
11141 default:
11142 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011143 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011144 }
11145
Chris Wilsonffe74d72013-08-26 20:58:12 +010011146 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011147 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011148 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011149 /*
11150 * On Gen 8, SRM is now taking an extra dword to accommodate
11151 * 48bits addresses, and we need a NOOP for the batch size to
11152 * stay even.
11153 */
11154 if (IS_GEN8(dev))
11155 len += 2;
11156 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011157
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011158 /*
11159 * BSpec MI_DISPLAY_FLIP for IVB:
11160 * "The full packet must be contained within the same cache line."
11161 *
11162 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11163 * cacheline, if we ever start emitting more commands before
11164 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11165 * then do the cacheline alignment, and finally emit the
11166 * MI_DISPLAY_FLIP.
11167 */
11168 ret = intel_ring_cacheline_align(ring);
11169 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011170 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011171
Chris Wilsonffe74d72013-08-26 20:58:12 +010011172 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011173 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011174 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011175
Chris Wilsonffe74d72013-08-26 20:58:12 +010011176 /* Unmask the flip-done completion message. Note that the bspec says that
11177 * we should do this for both the BCS and RCS, and that we must not unmask
11178 * more than one flip event at any time (or ensure that one flip message
11179 * can be sent by waiting for flip-done prior to queueing new flips).
11180 * Experimentation says that BCS works despite DERRMR masking all
11181 * flip-done completion events and that unmasking all planes at once
11182 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11183 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11184 */
11185 if (ring->id == RCS) {
11186 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11187 intel_ring_emit(ring, DERRMR);
11188 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11189 DERRMR_PIPEB_PRI_FLIP_DONE |
11190 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011191 if (IS_GEN8(dev))
11192 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11193 MI_SRM_LRM_GLOBAL_GTT);
11194 else
11195 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11196 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011197 intel_ring_emit(ring, DERRMR);
11198 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011199 if (IS_GEN8(dev)) {
11200 intel_ring_emit(ring, 0);
11201 intel_ring_emit(ring, MI_NOOP);
11202 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011203 }
11204
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011205 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011206 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011207 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011208 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011209
11210 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011211 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011212 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011213}
11214
Sourab Gupta84c33a62014-06-02 16:47:17 +053011215static bool use_mmio_flip(struct intel_engine_cs *ring,
11216 struct drm_i915_gem_object *obj)
11217{
11218 /*
11219 * This is not being used for older platforms, because
11220 * non-availability of flip done interrupt forces us to use
11221 * CS flips. Older platforms derive flip done using some clever
11222 * tricks involving the flip_pending status bits and vblank irqs.
11223 * So using MMIO flips there would disrupt this mechanism.
11224 */
11225
Chris Wilson8e09bf82014-07-08 10:40:30 +010011226 if (ring == NULL)
11227 return true;
11228
Sourab Gupta84c33a62014-06-02 16:47:17 +053011229 if (INTEL_INFO(ring->dev)->gen < 5)
11230 return false;
11231
11232 if (i915.use_mmio_flip < 0)
11233 return false;
11234 else if (i915.use_mmio_flip > 0)
11235 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011236 else if (i915.enable_execlists)
11237 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011238 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011239 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011240}
11241
Damien Lespiauff944562014-11-20 14:58:16 +000011242static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11243{
11244 struct drm_device *dev = intel_crtc->base.dev;
11245 struct drm_i915_private *dev_priv = dev->dev_private;
11246 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011247 const enum pipe pipe = intel_crtc->pipe;
11248 u32 ctl, stride;
11249
11250 ctl = I915_READ(PLANE_CTL(pipe, 0));
11251 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011252 switch (fb->modifier[0]) {
11253 case DRM_FORMAT_MOD_NONE:
11254 break;
11255 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011256 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011257 break;
11258 case I915_FORMAT_MOD_Y_TILED:
11259 ctl |= PLANE_CTL_TILED_Y;
11260 break;
11261 case I915_FORMAT_MOD_Yf_TILED:
11262 ctl |= PLANE_CTL_TILED_YF;
11263 break;
11264 default:
11265 MISSING_CASE(fb->modifier[0]);
11266 }
Damien Lespiauff944562014-11-20 14:58:16 +000011267
11268 /*
11269 * The stride is either expressed as a multiple of 64 bytes chunks for
11270 * linear buffers or in number of tiles for tiled buffers.
11271 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011272 stride = fb->pitches[0] /
11273 intel_fb_stride_alignment(dev, fb->modifier[0],
11274 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011275
11276 /*
11277 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11278 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11279 */
11280 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11281 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11282
11283 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11284 POSTING_READ(PLANE_SURF(pipe, 0));
11285}
11286
11287static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011288{
11289 struct drm_device *dev = intel_crtc->base.dev;
11290 struct drm_i915_private *dev_priv = dev->dev_private;
11291 struct intel_framebuffer *intel_fb =
11292 to_intel_framebuffer(intel_crtc->base.primary->fb);
11293 struct drm_i915_gem_object *obj = intel_fb->obj;
11294 u32 dspcntr;
11295 u32 reg;
11296
Sourab Gupta84c33a62014-06-02 16:47:17 +053011297 reg = DSPCNTR(intel_crtc->plane);
11298 dspcntr = I915_READ(reg);
11299
Damien Lespiauc5d97472014-10-25 00:11:11 +010011300 if (obj->tiling_mode != I915_TILING_NONE)
11301 dspcntr |= DISPPLANE_TILED;
11302 else
11303 dspcntr &= ~DISPPLANE_TILED;
11304
Sourab Gupta84c33a62014-06-02 16:47:17 +053011305 I915_WRITE(reg, dspcntr);
11306
11307 I915_WRITE(DSPSURF(intel_crtc->plane),
11308 intel_crtc->unpin_work->gtt_offset);
11309 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011310
Damien Lespiauff944562014-11-20 14:58:16 +000011311}
11312
11313/*
11314 * XXX: This is the temporary way to update the plane registers until we get
11315 * around to using the usual plane update functions for MMIO flips
11316 */
11317static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11318{
11319 struct drm_device *dev = intel_crtc->base.dev;
11320 bool atomic_update;
11321 u32 start_vbl_count;
11322
11323 intel_mark_page_flip_active(intel_crtc);
11324
11325 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11326
11327 if (INTEL_INFO(dev)->gen >= 9)
11328 skl_do_mmio_flip(intel_crtc);
11329 else
11330 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11331 ilk_do_mmio_flip(intel_crtc);
11332
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011333 if (atomic_update)
11334 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011335}
11336
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011337static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011338{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011339 struct intel_mmio_flip *mmio_flip =
11340 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011341
Daniel Vettereed29a52015-05-21 14:21:25 +020011342 if (mmio_flip->req)
11343 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011344 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011345 false, NULL,
11346 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011347
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011348 intel_do_mmio_flip(mmio_flip->crtc);
11349
Daniel Vettereed29a52015-05-21 14:21:25 +020011350 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011351 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011352}
11353
11354static int intel_queue_mmio_flip(struct drm_device *dev,
11355 struct drm_crtc *crtc,
11356 struct drm_framebuffer *fb,
11357 struct drm_i915_gem_object *obj,
11358 struct intel_engine_cs *ring,
11359 uint32_t flags)
11360{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011361 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011362
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011363 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11364 if (mmio_flip == NULL)
11365 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011366
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011367 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011368 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011369 mmio_flip->crtc = to_intel_crtc(crtc);
11370
11371 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11372 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011373
Sourab Gupta84c33a62014-06-02 16:47:17 +053011374 return 0;
11375}
11376
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011377static int intel_default_queue_flip(struct drm_device *dev,
11378 struct drm_crtc *crtc,
11379 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011380 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011381 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011382 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011383{
11384 return -ENODEV;
11385}
11386
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011387static bool __intel_pageflip_stall_check(struct drm_device *dev,
11388 struct drm_crtc *crtc)
11389{
11390 struct drm_i915_private *dev_priv = dev->dev_private;
11391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11392 struct intel_unpin_work *work = intel_crtc->unpin_work;
11393 u32 addr;
11394
11395 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11396 return true;
11397
11398 if (!work->enable_stall_check)
11399 return false;
11400
11401 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011402 if (work->flip_queued_req &&
11403 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011404 return false;
11405
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011406 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011407 }
11408
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011409 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011410 return false;
11411
11412 /* Potential stall - if we see that the flip has happened,
11413 * assume a missed interrupt. */
11414 if (INTEL_INFO(dev)->gen >= 4)
11415 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11416 else
11417 addr = I915_READ(DSPADDR(intel_crtc->plane));
11418
11419 /* There is a potential issue here with a false positive after a flip
11420 * to the same address. We could address this by checking for a
11421 * non-incrementing frame counter.
11422 */
11423 return addr == work->gtt_offset;
11424}
11425
11426void intel_check_page_flip(struct drm_device *dev, int pipe)
11427{
11428 struct drm_i915_private *dev_priv = dev->dev_private;
11429 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11430 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011431 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011432
Dave Gordon6c51d462015-03-06 15:34:26 +000011433 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011434
11435 if (crtc == NULL)
11436 return;
11437
Daniel Vetterf3260382014-09-15 14:55:23 +020011438 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011439 work = intel_crtc->unpin_work;
11440 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011441 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011442 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011443 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011444 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011445 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011446 if (work != NULL &&
11447 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11448 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011449 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011450}
11451
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011452static int intel_crtc_page_flip(struct drm_crtc *crtc,
11453 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011454 struct drm_pending_vblank_event *event,
11455 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011456{
11457 struct drm_device *dev = crtc->dev;
11458 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011459 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011460 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011462 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011463 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011464 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011465 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011466 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011467 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011468
Matt Roper2ff8fde2014-07-08 07:50:07 -070011469 /*
11470 * drm_mode_page_flip_ioctl() should already catch this, but double
11471 * check to be safe. In the future we may enable pageflipping from
11472 * a disabled primary plane.
11473 */
11474 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11475 return -EBUSY;
11476
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011477 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011478 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011479 return -EINVAL;
11480
11481 /*
11482 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11483 * Note that pitch changes could also affect these register.
11484 */
11485 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011486 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11487 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011488 return -EINVAL;
11489
Chris Wilsonf900db42014-02-20 09:26:13 +000011490 if (i915_terminally_wedged(&dev_priv->gpu_error))
11491 goto out_hang;
11492
Daniel Vetterb14c5672013-09-19 12:18:32 +020011493 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011494 if (work == NULL)
11495 return -ENOMEM;
11496
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011497 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011498 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011499 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011500 INIT_WORK(&work->work, intel_unpin_work_fn);
11501
Daniel Vetter87b6b102014-05-15 15:33:46 +020011502 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011503 if (ret)
11504 goto free_work;
11505
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011506 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011507 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011508 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011509 /* Before declaring the flip queue wedged, check if
11510 * the hardware completed the operation behind our backs.
11511 */
11512 if (__intel_pageflip_stall_check(dev, crtc)) {
11513 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11514 page_flip_completed(intel_crtc);
11515 } else {
11516 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011517 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011518
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011519 drm_crtc_vblank_put(crtc);
11520 kfree(work);
11521 return -EBUSY;
11522 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011523 }
11524 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011525 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011526
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011527 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11528 flush_workqueue(dev_priv->wq);
11529
Jesse Barnes75dfca82010-02-10 15:09:44 -080011530 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011531 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011532 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011533
Matt Roperf4510a22014-04-01 15:22:40 -070011534 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011535 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011536
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011537 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011538
Chris Wilson89ed88b2015-02-16 14:31:49 +000011539 ret = i915_mutex_lock_interruptible(dev);
11540 if (ret)
11541 goto cleanup;
11542
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011543 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011544 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011545
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011546 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011547 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011548
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011549 if (IS_VALLEYVIEW(dev)) {
11550 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011551 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011552 /* vlv: DISPLAY_FLIP fails to change tiling */
11553 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011554 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011555 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011556 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011557 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011558 if (ring == NULL || ring->id != RCS)
11559 ring = &dev_priv->ring[BCS];
11560 } else {
11561 ring = &dev_priv->ring[RCS];
11562 }
11563
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011564 mmio_flip = use_mmio_flip(ring, obj);
11565
11566 /* When using CS flips, we want to emit semaphores between rings.
11567 * However, when using mmio flips we will create a task to do the
11568 * synchronisation, so all we want here is to pin the framebuffer
11569 * into the display plane and skip any waits.
11570 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011571 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011572 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011573 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011574 if (ret)
11575 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011576
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011577 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11578 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011579
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011580 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011581 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11582 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011583 if (ret)
11584 goto cleanup_unpin;
11585
John Harrisonf06cc1b2014-11-24 18:49:37 +000011586 i915_gem_request_assign(&work->flip_queued_req,
11587 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011588 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011589 if (obj->last_write_req) {
11590 ret = i915_gem_check_olr(obj->last_write_req);
11591 if (ret)
11592 goto cleanup_unpin;
11593 }
11594
Sourab Gupta84c33a62014-06-02 16:47:17 +053011595 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011596 page_flip_flags);
11597 if (ret)
11598 goto cleanup_unpin;
11599
John Harrisonf06cc1b2014-11-24 18:49:37 +000011600 i915_gem_request_assign(&work->flip_queued_req,
11601 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011602 }
11603
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011604 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011605 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011606
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011607 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011608 INTEL_FRONTBUFFER_PRIMARY(pipe));
11609
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011610 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011611 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011612 mutex_unlock(&dev->struct_mutex);
11613
Jesse Barnese5510fa2010-07-01 16:48:37 -070011614 trace_i915_flip_request(intel_crtc->plane, obj);
11615
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011616 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011617
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011618cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011619 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011620cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011621 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011622 mutex_unlock(&dev->struct_mutex);
11623cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011624 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011625 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011626
Chris Wilson89ed88b2015-02-16 14:31:49 +000011627 drm_gem_object_unreference_unlocked(&obj->base);
11628 drm_framebuffer_unreference(work->old_fb);
11629
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011630 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011631 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011632 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011633
Daniel Vetter87b6b102014-05-15 15:33:46 +020011634 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011635free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011636 kfree(work);
11637
Chris Wilsonf900db42014-02-20 09:26:13 +000011638 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011639 struct drm_atomic_state *state;
11640 struct drm_plane_state *plane_state;
11641
Chris Wilsonf900db42014-02-20 09:26:13 +000011642out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011643 state = drm_atomic_state_alloc(dev);
11644 if (!state)
11645 return -ENOMEM;
11646 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11647
11648retry:
11649 plane_state = drm_atomic_get_plane_state(state, primary);
11650 ret = PTR_ERR_OR_ZERO(plane_state);
11651 if (!ret) {
11652 drm_atomic_set_fb_for_plane(plane_state, fb);
11653
11654 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11655 if (!ret)
11656 ret = drm_atomic_commit(state);
11657 }
11658
11659 if (ret == -EDEADLK) {
11660 drm_modeset_backoff(state->acquire_ctx);
11661 drm_atomic_state_clear(state);
11662 goto retry;
11663 }
11664
11665 if (ret)
11666 drm_atomic_state_free(state);
11667
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011668 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011669 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011670 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011671 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011672 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011673 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011674 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011675}
11676
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011677
11678/**
11679 * intel_wm_need_update - Check whether watermarks need updating
11680 * @plane: drm plane
11681 * @state: new plane state
11682 *
11683 * Check current plane state versus the new one to determine whether
11684 * watermarks need to be recalculated.
11685 *
11686 * Returns true or false.
11687 */
11688static bool intel_wm_need_update(struct drm_plane *plane,
11689 struct drm_plane_state *state)
11690{
11691 /* Update watermarks on tiling changes. */
11692 if (!plane->state->fb || !state->fb ||
11693 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11694 plane->state->rotation != state->rotation)
11695 return true;
11696
11697 if (plane->state->crtc_w != state->crtc_w)
11698 return true;
11699
11700 return false;
11701}
11702
11703int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11704 struct drm_plane_state *plane_state)
11705{
11706 struct drm_crtc *crtc = crtc_state->crtc;
11707 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11708 struct drm_plane *plane = plane_state->plane;
11709 struct drm_device *dev = crtc->dev;
11710 struct drm_i915_private *dev_priv = dev->dev_private;
11711 struct intel_plane_state *old_plane_state =
11712 to_intel_plane_state(plane->state);
11713 int idx = intel_crtc->base.base.id, ret;
11714 int i = drm_plane_index(plane);
11715 bool mode_changed = needs_modeset(crtc_state);
11716 bool was_crtc_enabled = crtc->state->active;
11717 bool is_crtc_enabled = crtc_state->active;
11718
11719 bool turn_off, turn_on, visible, was_visible;
11720 struct drm_framebuffer *fb = plane_state->fb;
11721
11722 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11723 plane->type != DRM_PLANE_TYPE_CURSOR) {
11724 ret = skl_update_scaler_plane(
11725 to_intel_crtc_state(crtc_state),
11726 to_intel_plane_state(plane_state));
11727 if (ret)
11728 return ret;
11729 }
11730
11731 /*
11732 * Disabling a plane is always okay; we just need to update
11733 * fb tracking in a special way since cleanup_fb() won't
11734 * get called by the plane helpers.
11735 */
11736 if (old_plane_state->base.fb && !fb)
11737 intel_crtc->atomic.disabled_planes |= 1 << i;
11738
11739 /* don't run rest during modeset yet */
11740 if (!intel_crtc->active || mode_changed)
11741 return 0;
11742
11743 was_visible = old_plane_state->visible;
11744 visible = to_intel_plane_state(plane_state)->visible;
11745
11746 if (!was_crtc_enabled && WARN_ON(was_visible))
11747 was_visible = false;
11748
11749 if (!is_crtc_enabled && WARN_ON(visible))
11750 visible = false;
11751
11752 if (!was_visible && !visible)
11753 return 0;
11754
11755 turn_off = was_visible && (!visible || mode_changed);
11756 turn_on = visible && (!was_visible || mode_changed);
11757
11758 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11759 plane->base.id, fb ? fb->base.id : -1);
11760
11761 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11762 plane->base.id, was_visible, visible,
11763 turn_off, turn_on, mode_changed);
11764
11765 if (intel_wm_need_update(plane, plane_state))
11766 intel_crtc->atomic.update_wm = true;
11767
11768 switch (plane->type) {
11769 case DRM_PLANE_TYPE_PRIMARY:
11770 if (visible)
11771 intel_crtc->atomic.fb_bits |=
11772 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
11773
11774 intel_crtc->atomic.wait_for_flips = true;
11775 intel_crtc->atomic.pre_disable_primary = turn_off;
11776 intel_crtc->atomic.post_enable_primary = turn_on;
11777
11778 if (turn_off)
11779 intel_crtc->atomic.disable_fbc = true;
11780
11781 /*
11782 * FBC does not work on some platforms for rotated
11783 * planes, so disable it when rotation is not 0 and
11784 * update it when rotation is set back to 0.
11785 *
11786 * FIXME: This is redundant with the fbc update done in
11787 * the primary plane enable function except that that
11788 * one is done too late. We eventually need to unify
11789 * this.
11790 */
11791
11792 if (visible &&
11793 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11794 dev_priv->fbc.crtc == intel_crtc &&
11795 plane_state->rotation != BIT(DRM_ROTATE_0))
11796 intel_crtc->atomic.disable_fbc = true;
11797
11798 /*
11799 * BDW signals flip done immediately if the plane
11800 * is disabled, even if the plane enable is already
11801 * armed to occur at the next vblank :(
11802 */
11803 if (turn_on && IS_BROADWELL(dev))
11804 intel_crtc->atomic.wait_vblank = true;
11805
11806 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11807 break;
11808 case DRM_PLANE_TYPE_CURSOR:
11809 if (visible)
11810 intel_crtc->atomic.fb_bits |=
11811 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
11812 break;
11813 case DRM_PLANE_TYPE_OVERLAY:
11814 /*
11815 * 'prepare' is never called when plane is being disabled, so
11816 * we need to handle frontbuffer tracking as a special case
11817 */
11818 if (visible)
11819 intel_crtc->atomic.fb_bits |=
11820 INTEL_FRONTBUFFER_SPRITE(intel_crtc->pipe);
11821
11822 if (turn_off && is_crtc_enabled) {
11823 intel_crtc->atomic.wait_vblank = true;
11824 intel_crtc->atomic.update_sprite_watermarks |=
11825 1 << i;
11826 }
11827 break;
11828 }
11829 return 0;
11830}
11831
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011832static bool encoders_cloneable(const struct intel_encoder *a,
11833 const struct intel_encoder *b)
11834{
11835 /* masks could be asymmetric, so check both ways */
11836 return a == b || (a->cloneable & (1 << b->type) &&
11837 b->cloneable & (1 << a->type));
11838}
11839
11840static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11841 struct intel_crtc *crtc,
11842 struct intel_encoder *encoder)
11843{
11844 struct intel_encoder *source_encoder;
11845 struct drm_connector *connector;
11846 struct drm_connector_state *connector_state;
11847 int i;
11848
11849 for_each_connector_in_state(state, connector, connector_state, i) {
11850 if (connector_state->crtc != &crtc->base)
11851 continue;
11852
11853 source_encoder =
11854 to_intel_encoder(connector_state->best_encoder);
11855 if (!encoders_cloneable(encoder, source_encoder))
11856 return false;
11857 }
11858
11859 return true;
11860}
11861
11862static bool check_encoder_cloning(struct drm_atomic_state *state,
11863 struct intel_crtc *crtc)
11864{
11865 struct intel_encoder *encoder;
11866 struct drm_connector *connector;
11867 struct drm_connector_state *connector_state;
11868 int i;
11869
11870 for_each_connector_in_state(state, connector, connector_state, i) {
11871 if (connector_state->crtc != &crtc->base)
11872 continue;
11873
11874 encoder = to_intel_encoder(connector_state->best_encoder);
11875 if (!check_single_encoder_cloning(state, crtc, encoder))
11876 return false;
11877 }
11878
11879 return true;
11880}
11881
11882static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11883 struct drm_crtc_state *crtc_state)
11884{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011885 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011886 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011888 struct intel_crtc_state *pipe_config =
11889 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011890 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011891 int ret, idx = crtc->base.id;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011892 bool mode_changed = needs_modeset(crtc_state);
11893
11894 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11895 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11896 return -EINVAL;
11897 }
11898
11899 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11900 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11901 idx, crtc->state->active, intel_crtc->active);
11902
Maarten Lankhorstad421372015-06-15 12:33:42 +020011903 if (mode_changed && crtc_state->enable &&
11904 dev_priv->display.crtc_compute_clock &&
11905 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11906 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11907 pipe_config);
11908 if (ret)
11909 return ret;
11910 }
11911
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011912 return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011913}
11914
Jani Nikula65b38e02015-04-13 11:26:56 +030011915static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011916 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11917 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011918 .atomic_begin = intel_begin_crtc_commit,
11919 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011920 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011921};
11922
Daniel Vetter9a935852012-07-05 22:34:27 +020011923/**
11924 * intel_modeset_update_staged_output_state
11925 *
11926 * Updates the staged output configuration state, e.g. after we've read out the
11927 * current hw state.
11928 */
11929static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11930{
Ville Syrjälä76688512014-01-10 11:28:06 +020011931 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011932 struct intel_encoder *encoder;
11933 struct intel_connector *connector;
11934
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011935 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011936 connector->new_encoder =
11937 to_intel_encoder(connector->base.encoder);
11938 }
11939
Damien Lespiaub2784e12014-08-05 11:29:37 +010011940 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011941 encoder->new_crtc =
11942 to_intel_crtc(encoder->base.crtc);
11943 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011944
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011945 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011946 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011947 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011948}
11949
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011950/* Transitional helper to copy current connector/encoder state to
11951 * connector->state. This is needed so that code that is partially
11952 * converted to atomic does the right thing.
11953 */
11954static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11955{
11956 struct intel_connector *connector;
11957
11958 for_each_intel_connector(dev, connector) {
11959 if (connector->base.encoder) {
11960 connector->base.state->best_encoder =
11961 connector->base.encoder;
11962 connector->base.state->crtc =
11963 connector->base.encoder->crtc;
11964 } else {
11965 connector->base.state->best_encoder = NULL;
11966 connector->base.state->crtc = NULL;
11967 }
11968 }
11969}
11970
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011971static void
Robin Schroereba905b2014-05-18 02:24:50 +020011972connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011973 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011974{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011975 int bpp = pipe_config->pipe_bpp;
11976
11977 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11978 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011979 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011980
11981 /* Don't use an invalid EDID bpc value */
11982 if (connector->base.display_info.bpc &&
11983 connector->base.display_info.bpc * 3 < bpp) {
11984 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11985 bpp, connector->base.display_info.bpc*3);
11986 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11987 }
11988
11989 /* Clamp bpp to 8 on screens without EDID 1.4 */
11990 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11991 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11992 bpp);
11993 pipe_config->pipe_bpp = 24;
11994 }
11995}
11996
11997static int
11998compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011999 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012000{
12001 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012002 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012003 struct drm_connector *connector;
12004 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012005 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012006
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012007 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012008 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012009 else if (INTEL_INFO(dev)->gen >= 5)
12010 bpp = 12*3;
12011 else
12012 bpp = 8*3;
12013
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012014
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012015 pipe_config->pipe_bpp = bpp;
12016
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012017 state = pipe_config->base.state;
12018
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012019 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012020 for_each_connector_in_state(state, connector, connector_state, i) {
12021 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020012022 continue;
12023
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012024 connected_sink_compute_bpp(to_intel_connector(connector),
12025 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012026 }
12027
12028 return bpp;
12029}
12030
Daniel Vetter644db712013-09-19 14:53:58 +020012031static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
12032{
12033 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
12034 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010012035 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020012036 mode->crtc_hdisplay, mode->crtc_hsync_start,
12037 mode->crtc_hsync_end, mode->crtc_htotal,
12038 mode->crtc_vdisplay, mode->crtc_vsync_start,
12039 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
12040}
12041
Daniel Vetterc0b03412013-05-28 12:05:54 +020012042static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012043 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012044 const char *context)
12045{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012046 struct drm_device *dev = crtc->base.dev;
12047 struct drm_plane *plane;
12048 struct intel_plane *intel_plane;
12049 struct intel_plane_state *state;
12050 struct drm_framebuffer *fb;
12051
12052 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
12053 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020012054
12055 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
12056 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
12057 pipe_config->pipe_bpp, pipe_config->dither);
12058 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12059 pipe_config->has_pch_encoder,
12060 pipe_config->fdi_lanes,
12061 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12062 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12063 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012064 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12065 pipe_config->has_dp_encoder,
12066 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12067 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12068 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012069
12070 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
12071 pipe_config->has_dp_encoder,
12072 pipe_config->dp_m2_n2.gmch_m,
12073 pipe_config->dp_m2_n2.gmch_n,
12074 pipe_config->dp_m2_n2.link_m,
12075 pipe_config->dp_m2_n2.link_n,
12076 pipe_config->dp_m2_n2.tu);
12077
Daniel Vetter55072d12014-11-20 16:10:28 +010012078 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12079 pipe_config->has_audio,
12080 pipe_config->has_infoframe);
12081
Daniel Vetterc0b03412013-05-28 12:05:54 +020012082 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012083 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012084 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012085 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12086 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012087 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012088 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12089 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012090 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12091 crtc->num_scalers,
12092 pipe_config->scaler_state.scaler_users,
12093 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012094 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12095 pipe_config->gmch_pfit.control,
12096 pipe_config->gmch_pfit.pgm_ratios,
12097 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012098 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012099 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012100 pipe_config->pch_pfit.size,
12101 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012102 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012103 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012104
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012105 if (IS_BROXTON(dev)) {
12106 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
12107 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
12108 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
12109 pipe_config->ddi_pll_sel,
12110 pipe_config->dpll_hw_state.ebb0,
12111 pipe_config->dpll_hw_state.pll0,
12112 pipe_config->dpll_hw_state.pll1,
12113 pipe_config->dpll_hw_state.pll2,
12114 pipe_config->dpll_hw_state.pll3,
12115 pipe_config->dpll_hw_state.pll6,
12116 pipe_config->dpll_hw_state.pll8,
12117 pipe_config->dpll_hw_state.pcsdw12);
12118 } else if (IS_SKYLAKE(dev)) {
12119 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12120 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12121 pipe_config->ddi_pll_sel,
12122 pipe_config->dpll_hw_state.ctrl1,
12123 pipe_config->dpll_hw_state.cfgcr1,
12124 pipe_config->dpll_hw_state.cfgcr2);
12125 } else if (HAS_DDI(dev)) {
12126 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12127 pipe_config->ddi_pll_sel,
12128 pipe_config->dpll_hw_state.wrpll);
12129 } else {
12130 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12131 "fp0: 0x%x, fp1: 0x%x\n",
12132 pipe_config->dpll_hw_state.dpll,
12133 pipe_config->dpll_hw_state.dpll_md,
12134 pipe_config->dpll_hw_state.fp0,
12135 pipe_config->dpll_hw_state.fp1);
12136 }
12137
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012138 DRM_DEBUG_KMS("planes on this crtc\n");
12139 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12140 intel_plane = to_intel_plane(plane);
12141 if (intel_plane->pipe != crtc->pipe)
12142 continue;
12143
12144 state = to_intel_plane_state(plane->state);
12145 fb = state->base.fb;
12146 if (!fb) {
12147 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12148 "disabled, scaler_id = %d\n",
12149 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12150 plane->base.id, intel_plane->pipe,
12151 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12152 drm_plane_index(plane), state->scaler_id);
12153 continue;
12154 }
12155
12156 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12157 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12158 plane->base.id, intel_plane->pipe,
12159 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12160 drm_plane_index(plane));
12161 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12162 fb->base.id, fb->width, fb->height, fb->pixel_format);
12163 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12164 state->scaler_id,
12165 state->src.x1 >> 16, state->src.y1 >> 16,
12166 drm_rect_width(&state->src) >> 16,
12167 drm_rect_height(&state->src) >> 16,
12168 state->dst.x1, state->dst.y1,
12169 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12170 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012171}
12172
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012173static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012174{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012175 struct drm_device *dev = state->dev;
12176 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012177 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012178 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012179 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012180 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012181
12182 /*
12183 * Walk the connector list instead of the encoder
12184 * list to detect the problem on ddi platforms
12185 * where there's just one encoder per digital port.
12186 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012187 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012188 if (!connector_state->best_encoder)
12189 continue;
12190
12191 encoder = to_intel_encoder(connector_state->best_encoder);
12192
12193 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012194
12195 switch (encoder->type) {
12196 unsigned int port_mask;
12197 case INTEL_OUTPUT_UNKNOWN:
12198 if (WARN_ON(!HAS_DDI(dev)))
12199 break;
12200 case INTEL_OUTPUT_DISPLAYPORT:
12201 case INTEL_OUTPUT_HDMI:
12202 case INTEL_OUTPUT_EDP:
12203 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12204
12205 /* the same port mustn't appear more than once */
12206 if (used_ports & port_mask)
12207 return false;
12208
12209 used_ports |= port_mask;
12210 default:
12211 break;
12212 }
12213 }
12214
12215 return true;
12216}
12217
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012218static void
12219clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12220{
12221 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012222 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012223 struct intel_dpll_hw_state dpll_hw_state;
12224 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012225 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012226
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012227 /* FIXME: before the switch to atomic started, a new pipe_config was
12228 * kzalloc'd. Code that depends on any field being zero should be
12229 * fixed, so that the crtc_state can be safely duplicated. For now,
12230 * only fields that are know to not cause problems are preserved. */
12231
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012232 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012233 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012234 shared_dpll = crtc_state->shared_dpll;
12235 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012236 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012237
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012238 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012239
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012240 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012241 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012242 crtc_state->shared_dpll = shared_dpll;
12243 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012244 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012245}
12246
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012247static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012248intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012249 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012250{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012251 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012252 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012253 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012254 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012255 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012256 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012257 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012258
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012259 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012260
Daniel Vettere143a212013-07-04 12:01:15 +020012261 pipe_config->cpu_transcoder =
12262 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012263
Imre Deak2960bc92013-07-30 13:36:32 +030012264 /*
12265 * Sanitize sync polarity flags based on requested ones. If neither
12266 * positive or negative polarity is requested, treat this as meaning
12267 * negative polarity.
12268 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012269 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012270 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012271 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012272
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012273 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012274 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012275 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012276
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012277 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12278 * plane pixel format and any sink constraints into account. Returns the
12279 * source plane bpp so that dithering can be selected on mismatches
12280 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012281 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12282 pipe_config);
12283 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012284 goto fail;
12285
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012286 /*
12287 * Determine the real pipe dimensions. Note that stereo modes can
12288 * increase the actual pipe size due to the frame doubling and
12289 * insertion of additional space for blanks between the frame. This
12290 * is stored in the crtc timings. We use the requested mode to do this
12291 * computation to clearly distinguish it from the adjusted mode, which
12292 * can be changed by the connectors in the below retry loop.
12293 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012294 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012295 &pipe_config->pipe_src_w,
12296 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012297
Daniel Vettere29c22c2013-02-21 00:00:16 +010012298encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012299 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012300 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012301 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012302
Daniel Vetter135c81b2013-07-21 21:37:09 +020012303 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012304 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12305 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012306
Daniel Vetter7758a112012-07-08 19:40:39 +020012307 /* Pass our mode to the connectors and the CRTC to give them a chance to
12308 * adjust it according to limitations or connector properties, and also
12309 * a chance to reject the mode entirely.
12310 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012311 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012312 if (connector_state->crtc != crtc)
12313 continue;
12314
12315 encoder = to_intel_encoder(connector_state->best_encoder);
12316
Daniel Vetterefea6e82013-07-21 21:36:59 +020012317 if (!(encoder->compute_config(encoder, pipe_config))) {
12318 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012319 goto fail;
12320 }
12321 }
12322
Daniel Vetterff9a6752013-06-01 17:16:21 +020012323 /* Set default port clock if not overwritten by the encoder. Needs to be
12324 * done afterwards in case the encoder adjusts the mode. */
12325 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012326 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012327 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012328
Daniel Vettera43f6e02013-06-07 23:10:32 +020012329 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012330 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012331 DRM_DEBUG_KMS("CRTC fixup failed\n");
12332 goto fail;
12333 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012334
12335 if (ret == RETRY) {
12336 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12337 ret = -EINVAL;
12338 goto fail;
12339 }
12340
12341 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12342 retry = false;
12343 goto encoder_retry;
12344 }
12345
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012346 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012347 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012348 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012349
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012350 /* Check if we need to force a modeset */
12351 if (pipe_config->has_audio !=
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012352 to_intel_crtc_state(crtc->state)->has_audio) {
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012353 pipe_config->base.mode_changed = true;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012354 ret = drm_atomic_add_affected_planes(state, crtc);
12355 }
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012356
12357 /*
12358 * Note we have an issue here with infoframes: current code
12359 * only updates them on the full mode set path per hw
12360 * requirements. So here we should be checking for any
12361 * required changes and forcing a mode set.
12362 */
Daniel Vetter7758a112012-07-08 19:40:39 +020012363fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012364 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012365}
12366
Daniel Vetterea9d7582012-07-10 10:42:52 +020012367static bool intel_crtc_in_use(struct drm_crtc *crtc)
12368{
12369 struct drm_encoder *encoder;
12370 struct drm_device *dev = crtc->dev;
12371
12372 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12373 if (encoder->crtc == crtc)
12374 return true;
12375
12376 return false;
12377}
12378
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012379static void
12380intel_modeset_update_state(struct drm_atomic_state *state)
12381{
12382 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012383 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012384 struct drm_crtc *crtc;
12385 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012386 struct drm_connector *connector;
12387
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012388 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012389
Damien Lespiaub2784e12014-08-05 11:29:37 +010012390 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012391 if (!intel_encoder->base.crtc)
12392 continue;
12393
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012394 crtc = intel_encoder->base.crtc;
12395 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12396 if (!crtc_state || !needs_modeset(crtc->state))
12397 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012398
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012399 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012400 }
12401
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012402 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorstf7217902015-06-10 10:24:20 +020012403 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012404
Ville Syrjälä76688512014-01-10 11:28:06 +020012405 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012406 for_each_crtc(dev, crtc) {
12407 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012408
12409 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012410
12411 /* Update hwmode for vblank functions */
12412 if (crtc->state->active)
12413 crtc->hwmode = crtc->state->adjusted_mode;
12414 else
12415 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012416 }
12417
12418 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12419 if (!connector->encoder || !connector->encoder->crtc)
12420 continue;
12421
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012422 crtc = connector->encoder->crtc;
12423 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12424 if (!crtc_state || !needs_modeset(crtc->state))
12425 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012426
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012427 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012428 struct drm_property *dpms_property =
12429 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012430
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012431 connector->dpms = DRM_MODE_DPMS_ON;
12432 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012433
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012434 intel_encoder = to_intel_encoder(connector->encoder);
12435 intel_encoder->connectors_active = true;
12436 } else
12437 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012438 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012439}
12440
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012441static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012442{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012443 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012444
12445 if (clock1 == clock2)
12446 return true;
12447
12448 if (!clock1 || !clock2)
12449 return false;
12450
12451 diff = abs(clock1 - clock2);
12452
12453 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12454 return true;
12455
12456 return false;
12457}
12458
Daniel Vetter25c5b262012-07-08 22:08:04 +020012459#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12460 list_for_each_entry((intel_crtc), \
12461 &(dev)->mode_config.crtc_list, \
12462 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012463 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012464
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012465static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012466intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012467 struct intel_crtc_state *current_config,
12468 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012469{
Daniel Vetter66e985c2013-06-05 13:34:20 +020012470#define PIPE_CONF_CHECK_X(name) \
12471 if (current_config->name != pipe_config->name) { \
12472 DRM_ERROR("mismatch in " #name " " \
12473 "(expected 0x%08x, found 0x%08x)\n", \
12474 current_config->name, \
12475 pipe_config->name); \
12476 return false; \
12477 }
12478
Daniel Vetter08a24032013-04-19 11:25:34 +020012479#define PIPE_CONF_CHECK_I(name) \
12480 if (current_config->name != pipe_config->name) { \
12481 DRM_ERROR("mismatch in " #name " " \
12482 "(expected %i, found %i)\n", \
12483 current_config->name, \
12484 pipe_config->name); \
12485 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012486 }
12487
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012488/* This is required for BDW+ where there is only one set of registers for
12489 * switching between high and low RR.
12490 * This macro can be used whenever a comparison has to be made between one
12491 * hw state and multiple sw state variables.
12492 */
12493#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12494 if ((current_config->name != pipe_config->name) && \
12495 (current_config->alt_name != pipe_config->name)) { \
12496 DRM_ERROR("mismatch in " #name " " \
12497 "(expected %i or %i, found %i)\n", \
12498 current_config->name, \
12499 current_config->alt_name, \
12500 pipe_config->name); \
12501 return false; \
12502 }
12503
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012504#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12505 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070012506 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012507 "(expected %i, found %i)\n", \
12508 current_config->name & (mask), \
12509 pipe_config->name & (mask)); \
12510 return false; \
12511 }
12512
Ville Syrjälä5e550652013-09-06 23:29:07 +030012513#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12514 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12515 DRM_ERROR("mismatch in " #name " " \
12516 "(expected %i, found %i)\n", \
12517 current_config->name, \
12518 pipe_config->name); \
12519 return false; \
12520 }
12521
Daniel Vetterbb760062013-06-06 14:55:52 +020012522#define PIPE_CONF_QUIRK(quirk) \
12523 ((current_config->quirks | pipe_config->quirks) & (quirk))
12524
Daniel Vettereccb1402013-05-22 00:50:22 +020012525 PIPE_CONF_CHECK_I(cpu_transcoder);
12526
Daniel Vetter08a24032013-04-19 11:25:34 +020012527 PIPE_CONF_CHECK_I(has_pch_encoder);
12528 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012529 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12530 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12531 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12532 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12533 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012534
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012535 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012536
12537 if (INTEL_INFO(dev)->gen < 8) {
12538 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12539 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12540 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12541 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12542 PIPE_CONF_CHECK_I(dp_m_n.tu);
12543
12544 if (current_config->has_drrs) {
12545 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12546 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12547 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12548 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12549 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12550 }
12551 } else {
12552 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12553 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12554 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12555 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12556 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12557 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012558
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012559 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12560 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12561 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12562 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12563 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12564 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012565
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012566 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12567 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12568 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12569 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12570 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12571 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012572
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012573 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012574 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012575 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12576 IS_VALLEYVIEW(dev))
12577 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012578 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012579
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012580 PIPE_CONF_CHECK_I(has_audio);
12581
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012582 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012583 DRM_MODE_FLAG_INTERLACE);
12584
Daniel Vetterbb760062013-06-06 14:55:52 +020012585 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012586 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012587 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012588 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012589 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012590 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012591 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012592 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012593 DRM_MODE_FLAG_NVSYNC);
12594 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012595
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012596 PIPE_CONF_CHECK_I(pipe_src_w);
12597 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012598
Daniel Vetter99535992014-04-13 12:00:33 +020012599 /*
12600 * FIXME: BIOS likes to set up a cloned config with lvds+external
12601 * screen. Since we don't yet re-compute the pipe config when moving
12602 * just the lvds port away to another pipe the sw tracking won't match.
12603 *
12604 * Proper atomic modesets with recomputed global state will fix this.
12605 * Until then just don't check gmch state for inherited modes.
12606 */
12607 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12608 PIPE_CONF_CHECK_I(gmch_pfit.control);
12609 /* pfit ratios are autocomputed by the hw on gen4+ */
12610 if (INTEL_INFO(dev)->gen < 4)
12611 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12612 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12613 }
12614
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012615 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12616 if (current_config->pch_pfit.enabled) {
12617 PIPE_CONF_CHECK_I(pch_pfit.pos);
12618 PIPE_CONF_CHECK_I(pch_pfit.size);
12619 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012620
Chandra Kondurua1b22782015-04-07 15:28:45 -070012621 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12622
Jesse Barnese59150d2014-01-07 13:30:45 -080012623 /* BDW+ don't expose a synchronous way to read the state */
12624 if (IS_HASWELL(dev))
12625 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012626
Ville Syrjälä282740f2013-09-04 18:30:03 +030012627 PIPE_CONF_CHECK_I(double_wide);
12628
Daniel Vetter26804af2014-06-25 22:01:55 +030012629 PIPE_CONF_CHECK_X(ddi_pll_sel);
12630
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012631 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012632 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012633 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012634 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12635 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012636 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012637 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12638 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12639 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012640
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012641 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12642 PIPE_CONF_CHECK_I(pipe_bpp);
12643
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012644 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012645 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012646
Daniel Vetter66e985c2013-06-05 13:34:20 +020012647#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012648#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012649#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012650#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012651#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012652#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012653
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012654 return true;
12655}
12656
Damien Lespiau08db6652014-11-04 17:06:52 +000012657static void check_wm_state(struct drm_device *dev)
12658{
12659 struct drm_i915_private *dev_priv = dev->dev_private;
12660 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12661 struct intel_crtc *intel_crtc;
12662 int plane;
12663
12664 if (INTEL_INFO(dev)->gen < 9)
12665 return;
12666
12667 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12668 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12669
12670 for_each_intel_crtc(dev, intel_crtc) {
12671 struct skl_ddb_entry *hw_entry, *sw_entry;
12672 const enum pipe pipe = intel_crtc->pipe;
12673
12674 if (!intel_crtc->active)
12675 continue;
12676
12677 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012678 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012679 hw_entry = &hw_ddb.plane[pipe][plane];
12680 sw_entry = &sw_ddb->plane[pipe][plane];
12681
12682 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12683 continue;
12684
12685 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12686 "(expected (%u,%u), found (%u,%u))\n",
12687 pipe_name(pipe), plane + 1,
12688 sw_entry->start, sw_entry->end,
12689 hw_entry->start, hw_entry->end);
12690 }
12691
12692 /* cursor */
12693 hw_entry = &hw_ddb.cursor[pipe];
12694 sw_entry = &sw_ddb->cursor[pipe];
12695
12696 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12697 continue;
12698
12699 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12700 "(expected (%u,%u), found (%u,%u))\n",
12701 pipe_name(pipe),
12702 sw_entry->start, sw_entry->end,
12703 hw_entry->start, hw_entry->end);
12704 }
12705}
12706
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012707static void
12708check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012709{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012710 struct intel_connector *connector;
12711
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012712 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012713 /* This also checks the encoder/connector hw state with the
12714 * ->get_hw_state callbacks. */
12715 intel_connector_check_state(connector);
12716
Rob Clarke2c719b2014-12-15 13:56:32 -050012717 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012718 "connector's staged encoder doesn't match current encoder\n");
12719 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012720}
12721
12722static void
12723check_encoder_state(struct drm_device *dev)
12724{
12725 struct intel_encoder *encoder;
12726 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012727
Damien Lespiaub2784e12014-08-05 11:29:37 +010012728 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012729 bool enabled = false;
12730 bool active = false;
12731 enum pipe pipe, tracked_pipe;
12732
12733 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12734 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012735 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012736
Rob Clarke2c719b2014-12-15 13:56:32 -050012737 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012738 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012739 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012740 "encoder's active_connectors set, but no crtc\n");
12741
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012742 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012743 if (connector->base.encoder != &encoder->base)
12744 continue;
12745 enabled = true;
12746 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12747 active = true;
12748 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012749 /*
12750 * for MST connectors if we unplug the connector is gone
12751 * away but the encoder is still connected to a crtc
12752 * until a modeset happens in response to the hotplug.
12753 */
12754 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12755 continue;
12756
Rob Clarke2c719b2014-12-15 13:56:32 -050012757 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012758 "encoder's enabled state mismatch "
12759 "(expected %i, found %i)\n",
12760 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012761 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012762 "active encoder with no crtc\n");
12763
Rob Clarke2c719b2014-12-15 13:56:32 -050012764 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012765 "encoder's computed active state doesn't match tracked active state "
12766 "(expected %i, found %i)\n", active, encoder->connectors_active);
12767
12768 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012769 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012770 "encoder's hw state doesn't match sw tracking "
12771 "(expected %i, found %i)\n",
12772 encoder->connectors_active, active);
12773
12774 if (!encoder->base.crtc)
12775 continue;
12776
12777 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012778 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012779 "active encoder's pipe doesn't match"
12780 "(expected %i, found %i)\n",
12781 tracked_pipe, pipe);
12782
12783 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012784}
12785
12786static void
12787check_crtc_state(struct drm_device *dev)
12788{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012789 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012790 struct intel_crtc *crtc;
12791 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012792 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012793
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012794 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012795 bool enabled = false;
12796 bool active = false;
12797
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012798 memset(&pipe_config, 0, sizeof(pipe_config));
12799
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012800 DRM_DEBUG_KMS("[CRTC:%d]\n",
12801 crtc->base.base.id);
12802
Matt Roper83d65732015-02-25 13:12:16 -080012803 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012804 "active crtc, but not enabled in sw tracking\n");
12805
Damien Lespiaub2784e12014-08-05 11:29:37 +010012806 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012807 if (encoder->base.crtc != &crtc->base)
12808 continue;
12809 enabled = true;
12810 if (encoder->connectors_active)
12811 active = true;
12812 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012813
Rob Clarke2c719b2014-12-15 13:56:32 -050012814 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012815 "crtc's computed active state doesn't match tracked active state "
12816 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012817 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012818 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012819 "(expected %i, found %i)\n", enabled,
12820 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012821
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012822 active = dev_priv->display.get_pipe_config(crtc,
12823 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012824
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012825 /* hw state is inconsistent with the pipe quirk */
12826 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12827 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012828 active = crtc->active;
12829
Damien Lespiaub2784e12014-08-05 11:29:37 +010012830 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012831 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012832 if (encoder->base.crtc != &crtc->base)
12833 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012834 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012835 encoder->get_config(encoder, &pipe_config);
12836 }
12837
Rob Clarke2c719b2014-12-15 13:56:32 -050012838 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012839 "crtc active state doesn't match with hw state "
12840 "(expected %i, found %i)\n", crtc->active, active);
12841
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012842 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12843 "transitional active state does not match atomic hw state "
12844 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12845
Daniel Vetterc0b03412013-05-28 12:05:54 +020012846 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012847 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012848 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012849 intel_dump_pipe_config(crtc, &pipe_config,
12850 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012851 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012852 "[sw state]");
12853 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012854 }
12855}
12856
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012857static void
12858check_shared_dpll_state(struct drm_device *dev)
12859{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012860 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012861 struct intel_crtc *crtc;
12862 struct intel_dpll_hw_state dpll_hw_state;
12863 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012864
12865 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12866 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12867 int enabled_crtcs = 0, active_crtcs = 0;
12868 bool active;
12869
12870 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12871
12872 DRM_DEBUG_KMS("%s\n", pll->name);
12873
12874 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12875
Rob Clarke2c719b2014-12-15 13:56:32 -050012876 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012877 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012878 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012879 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012880 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012881 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012882 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012883 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012884 "pll on state mismatch (expected %i, found %i)\n",
12885 pll->on, active);
12886
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012887 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012888 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012889 enabled_crtcs++;
12890 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12891 active_crtcs++;
12892 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012893 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012894 "pll active crtcs mismatch (expected %i, found %i)\n",
12895 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012896 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012897 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012898 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012899
Rob Clarke2c719b2014-12-15 13:56:32 -050012900 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012901 sizeof(dpll_hw_state)),
12902 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012903 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012904}
12905
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012906void
12907intel_modeset_check_state(struct drm_device *dev)
12908{
Damien Lespiau08db6652014-11-04 17:06:52 +000012909 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012910 check_connector_state(dev);
12911 check_encoder_state(dev);
12912 check_crtc_state(dev);
12913 check_shared_dpll_state(dev);
12914}
12915
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012916void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012917 int dotclock)
12918{
12919 /*
12920 * FDI already provided one idea for the dotclock.
12921 * Yell if the encoder disagrees.
12922 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012923 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012924 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012925 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012926}
12927
Ville Syrjälä80715b22014-05-15 20:23:23 +030012928static void update_scanline_offset(struct intel_crtc *crtc)
12929{
12930 struct drm_device *dev = crtc->base.dev;
12931
12932 /*
12933 * The scanline counter increments at the leading edge of hsync.
12934 *
12935 * On most platforms it starts counting from vtotal-1 on the
12936 * first active line. That means the scanline counter value is
12937 * always one less than what we would expect. Ie. just after
12938 * start of vblank, which also occurs at start of hsync (on the
12939 * last active line), the scanline counter will read vblank_start-1.
12940 *
12941 * On gen2 the scanline counter starts counting from 1 instead
12942 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12943 * to keep the value positive), instead of adding one.
12944 *
12945 * On HSW+ the behaviour of the scanline counter depends on the output
12946 * type. For DP ports it behaves like most other platforms, but on HDMI
12947 * there's an extra 1 line difference. So we need to add two instead of
12948 * one to the value.
12949 */
12950 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012951 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012952 int vtotal;
12953
12954 vtotal = mode->crtc_vtotal;
12955 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12956 vtotal /= 2;
12957
12958 crtc->scanline_offset = vtotal - 1;
12959 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012960 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012961 crtc->scanline_offset = 2;
12962 } else
12963 crtc->scanline_offset = 1;
12964}
12965
Maarten Lankhorstad421372015-06-15 12:33:42 +020012966static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012967{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012968 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012969 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012970 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012971 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012972 struct intel_crtc_state *intel_crtc_state;
12973 struct drm_crtc *crtc;
12974 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012975 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012976
12977 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012978 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012979
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012980 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012981 int dpll;
12982
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012983 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012984 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012985 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012986
Maarten Lankhorstad421372015-06-15 12:33:42 +020012987 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012988 continue;
12989
Maarten Lankhorstad421372015-06-15 12:33:42 +020012990 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012991
Maarten Lankhorstad421372015-06-15 12:33:42 +020012992 if (!shared_dpll)
12993 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12994
12995 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012996 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012997}
12998
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012999/*
13000 * This implements the workaround described in the "notes" section of the mode
13001 * set sequence documentation. When going from no pipes or single pipe to
13002 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13003 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13004 */
13005static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13006{
13007 struct drm_crtc_state *crtc_state;
13008 struct intel_crtc *intel_crtc;
13009 struct drm_crtc *crtc;
13010 struct intel_crtc_state *first_crtc_state = NULL;
13011 struct intel_crtc_state *other_crtc_state = NULL;
13012 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13013 int i;
13014
13015 /* look at all crtc's that are going to be enabled in during modeset */
13016 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13017 intel_crtc = to_intel_crtc(crtc);
13018
13019 if (!crtc_state->active || !needs_modeset(crtc_state))
13020 continue;
13021
13022 if (first_crtc_state) {
13023 other_crtc_state = to_intel_crtc_state(crtc_state);
13024 break;
13025 } else {
13026 first_crtc_state = to_intel_crtc_state(crtc_state);
13027 first_pipe = intel_crtc->pipe;
13028 }
13029 }
13030
13031 /* No workaround needed? */
13032 if (!first_crtc_state)
13033 return 0;
13034
13035 /* w/a possibly needed, check how many crtc's are already enabled. */
13036 for_each_intel_crtc(state->dev, intel_crtc) {
13037 struct intel_crtc_state *pipe_config;
13038
13039 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13040 if (IS_ERR(pipe_config))
13041 return PTR_ERR(pipe_config);
13042
13043 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13044
13045 if (!pipe_config->base.active ||
13046 needs_modeset(&pipe_config->base))
13047 continue;
13048
13049 /* 2 or more enabled crtcs means no need for w/a */
13050 if (enabled_pipe != INVALID_PIPE)
13051 return 0;
13052
13053 enabled_pipe = intel_crtc->pipe;
13054 }
13055
13056 if (enabled_pipe != INVALID_PIPE)
13057 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13058 else if (other_crtc_state)
13059 other_crtc_state->hsw_workaround_pipe = first_pipe;
13060
13061 return 0;
13062}
13063
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013064/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013065static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013066{
13067 struct drm_device *dev = state->dev;
13068 int ret;
13069
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013070 if (!check_digital_port_conflicts(state)) {
13071 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13072 return -EINVAL;
13073 }
13074
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013075 /*
13076 * See if the config requires any additional preparation, e.g.
13077 * to adjust global state with pipes off. We need to do this
13078 * here so we can get the modeset_pipe updated config for the new
13079 * mode set on this crtc. For other crtcs we need to use the
13080 * adjusted_mode bits in the crtc directly.
13081 */
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030013082 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
13083 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
13084 ret = valleyview_modeset_global_pipes(state);
13085 else
13086 ret = broadwell_modeset_global_pipes(state);
13087
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013088 if (ret)
13089 return ret;
13090 }
13091
Maarten Lankhorstad421372015-06-15 12:33:42 +020013092 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013093
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013094 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013095 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013096
Maarten Lankhorstad421372015-06-15 12:33:42 +020013097 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013098}
13099
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013100static int
13101intel_modeset_compute_config(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013102{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013103 struct drm_crtc *crtc;
13104 struct drm_crtc_state *crtc_state;
13105 int ret, i;
13106
13107 ret = drm_atomic_helper_check_modeset(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013108 if (ret)
13109 return ret;
13110
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013111 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13112 if (!crtc_state->enable &&
13113 WARN_ON(crtc_state->active))
13114 crtc_state->active = false;
13115
13116 if (!crtc_state->enable)
13117 continue;
13118
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013119 if (!needs_modeset(crtc_state)) {
13120 ret = drm_atomic_add_affected_connectors(state, crtc);
13121 if (ret)
13122 return ret;
13123 }
13124
13125 ret = intel_modeset_pipe_config(crtc,
13126 to_intel_crtc_state(crtc_state));
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013127 if (ret)
13128 return ret;
13129
13130 intel_dump_pipe_config(to_intel_crtc(crtc),
13131 to_intel_crtc_state(crtc_state),
13132 "[modeset]");
13133 }
13134
13135 ret = intel_modeset_checks(state);
13136 if (ret)
13137 return ret;
13138
13139 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013140}
13141
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013142static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013143{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013144 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013145 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013146 struct drm_crtc *crtc;
13147 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013148 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013149 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020013150
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013151 ret = drm_atomic_helper_prepare_planes(dev, state);
13152 if (ret)
13153 return ret;
13154
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013155 drm_atomic_helper_swap_state(dev, state);
13156
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013157 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013158 if (!needs_modeset(crtc->state) || !crtc_state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013159 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010013160
Maarten Lankhorst69024de2015-06-01 12:49:46 +020013161 intel_crtc_disable_planes(crtc);
13162 dev_priv->display.crtc_disable(crtc);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013163 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013164
Daniel Vetterea9d7582012-07-10 10:42:52 +020013165 /* Only after disabling all output pipelines that will be changed can we
13166 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013167 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013168
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013169 /* The state has been swaped above, so state actually contains the
13170 * old state now. */
13171
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030013172 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013173
Daniel Vettera6778b32012-07-02 09:56:42 +020013174 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013175 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013176 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
13177
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020013178 if (!needs_modeset(crtc->state) || !crtc->state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013179 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013180
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013181 update_scanline_offset(to_intel_crtc(crtc));
13182
13183 dev_priv->display.crtc_enable(crtc);
13184 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013185 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013186
Daniel Vettera6778b32012-07-02 09:56:42 +020013187 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013188
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013189 drm_atomic_helper_cleanup_planes(dev, state);
13190
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013191 drm_atomic_state_free(state);
13192
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013193 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013194}
13195
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013196static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013197{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013198 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013199 int ret;
13200
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013201 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013202 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013203 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013204
13205 return ret;
13206}
13207
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013208static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013209{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013210 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013211
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013212 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013213 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013214 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013215
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013216 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013217}
13218
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013219void intel_crtc_restore_mode(struct drm_crtc *crtc)
13220{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013221 struct drm_device *dev = crtc->dev;
13222 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013223 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013224 struct intel_encoder *encoder;
13225 struct intel_connector *connector;
13226 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013227 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013228 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013229
13230 state = drm_atomic_state_alloc(dev);
13231 if (!state) {
13232 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13233 crtc->base.id);
13234 return;
13235 }
13236
13237 state->acquire_ctx = dev->mode_config.acquire_ctx;
13238
13239 /* The force restore path in the HW readout code relies on the staged
13240 * config still keeping the user requested config while the actual
13241 * state has been overwritten by the configuration read from HW. We
13242 * need to copy the staged config to the atomic state, otherwise the
13243 * mode set will just reapply the state the HW is already in. */
13244 for_each_intel_encoder(dev, encoder) {
13245 if (&encoder->new_crtc->base != crtc)
13246 continue;
13247
13248 for_each_intel_connector(dev, connector) {
13249 if (connector->new_encoder != encoder)
13250 continue;
13251
13252 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13253 if (IS_ERR(connector_state)) {
13254 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13255 connector->base.base.id,
13256 connector->base.name,
13257 PTR_ERR(connector_state));
13258 continue;
13259 }
13260
13261 connector_state->crtc = crtc;
13262 connector_state->best_encoder = &encoder->base;
13263 }
13264 }
13265
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013266 for_each_intel_crtc(dev, intel_crtc) {
13267 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13268 continue;
13269
13270 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13271 if (IS_ERR(crtc_state)) {
13272 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13273 intel_crtc->base.base.id,
13274 PTR_ERR(crtc_state));
13275 continue;
13276 }
13277
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013278 crtc_state->base.active = crtc_state->base.enable =
13279 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013280
13281 if (&intel_crtc->base == crtc)
13282 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013283 }
13284
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013285 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13286 crtc->primary->fb, crtc->x, crtc->y);
13287
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013288 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013289 if (ret)
13290 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013291}
13292
Daniel Vetter25c5b262012-07-08 22:08:04 +020013293#undef for_each_intel_crtc_masked
13294
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013295static bool intel_connector_in_mode_set(struct intel_connector *connector,
13296 struct drm_mode_set *set)
13297{
13298 int ro;
13299
13300 for (ro = 0; ro < set->num_connectors; ro++)
13301 if (set->connectors[ro] == &connector->base)
13302 return true;
13303
13304 return false;
13305}
13306
Daniel Vetter2e431052012-07-04 22:42:15 +020013307static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013308intel_modeset_stage_output_state(struct drm_device *dev,
13309 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013310 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013311{
Daniel Vetter9a935852012-07-05 22:34:27 +020013312 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013313 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013314 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013315 struct drm_crtc *crtc;
13316 struct drm_crtc_state *crtc_state;
13317 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013318
Damien Lespiau9abdda72013-02-13 13:29:23 +000013319 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013320 * of connectors. For paranoia, double-check this. */
13321 WARN_ON(!set->fb && (set->num_connectors != 0));
13322 WARN_ON(set->fb && (set->num_connectors == 0));
13323
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013324 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013325 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13326
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013327 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13328 continue;
13329
13330 connector_state =
13331 drm_atomic_get_connector_state(state, &connector->base);
13332 if (IS_ERR(connector_state))
13333 return PTR_ERR(connector_state);
13334
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013335 if (in_mode_set) {
13336 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013337 connector_state->best_encoder =
13338 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013339 }
13340
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013341 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013342 continue;
13343
Daniel Vetter9a935852012-07-05 22:34:27 +020013344 /* If we disable the crtc, disable all its connectors. Also, if
13345 * the connector is on the changing crtc but not on the new
13346 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013347 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013348 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013349
13350 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13351 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013352 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013353 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013354 }
13355 /* connector->new_encoder is now updated for all connectors. */
13356
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013357 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13358 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013359
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013360 if (!connector_state->best_encoder) {
13361 ret = drm_atomic_set_crtc_for_connector(connector_state,
13362 NULL);
13363 if (ret)
13364 return ret;
13365
Daniel Vetter50f56112012-07-02 09:35:43 +020013366 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013367 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013368
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013369 if (intel_connector_in_mode_set(connector, set)) {
13370 struct drm_crtc *crtc = connector->base.state->crtc;
13371
13372 /* If this connector was in a previous crtc, add it
13373 * to the state. We might need to disable it. */
13374 if (crtc) {
13375 crtc_state =
13376 drm_atomic_get_crtc_state(state, crtc);
13377 if (IS_ERR(crtc_state))
13378 return PTR_ERR(crtc_state);
13379 }
13380
13381 ret = drm_atomic_set_crtc_for_connector(connector_state,
13382 set->crtc);
13383 if (ret)
13384 return ret;
13385 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013386
13387 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013388 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13389 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013390 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013391 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013392
Daniel Vetter9a935852012-07-05 22:34:27 +020013393 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13394 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013395 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013396 connector_state->crtc->base.id);
13397
13398 if (connector_state->best_encoder != &connector->encoder->base)
13399 connector->encoder =
13400 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013401 }
13402
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013403 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013404 bool has_connectors;
13405
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013406 ret = drm_atomic_add_affected_connectors(state, crtc);
13407 if (ret)
13408 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013409
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013410 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13411 if (has_connectors != crtc_state->enable)
13412 crtc_state->enable =
13413 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013414 }
13415
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013416 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13417 set->fb, set->x, set->y);
13418 if (ret)
13419 return ret;
13420
13421 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13422 if (IS_ERR(crtc_state))
13423 return PTR_ERR(crtc_state);
13424
Matt Roperce522992015-06-05 15:08:24 -070013425 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13426 if (ret)
13427 return ret;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013428
13429 if (set->num_connectors)
13430 crtc_state->active = true;
13431
Daniel Vetter2e431052012-07-04 22:42:15 +020013432 return 0;
13433}
13434
13435static int intel_crtc_set_config(struct drm_mode_set *set)
13436{
13437 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013438 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013439 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013440
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013441 BUG_ON(!set);
13442 BUG_ON(!set->crtc);
13443 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013444
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013445 /* Enforce sane interface api - has been abused by the fb helper. */
13446 BUG_ON(!set->mode && set->fb);
13447 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013448
Daniel Vetter2e431052012-07-04 22:42:15 +020013449 if (set->fb) {
13450 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13451 set->crtc->base.id, set->fb->base.id,
13452 (int)set->num_connectors, set->x, set->y);
13453 } else {
13454 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013455 }
13456
13457 dev = set->crtc->dev;
13458
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013459 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013460 if (!state)
13461 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013462
13463 state->acquire_ctx = dev->mode_config.acquire_ctx;
13464
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013465 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013466 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013467 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013468
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013469 ret = intel_modeset_compute_config(state);
13470 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013471 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013472
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013473 intel_update_pipe_size(to_intel_crtc(set->crtc));
13474
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013475 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013476 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013477 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13478 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013479 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013480
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013481out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013482 if (ret)
13483 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013484 return ret;
13485}
13486
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013487static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013488 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013489 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013490 .destroy = intel_crtc_destroy,
13491 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013492 .atomic_duplicate_state = intel_crtc_duplicate_state,
13493 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013494};
13495
Daniel Vetter53589012013-06-05 13:34:16 +020013496static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13497 struct intel_shared_dpll *pll,
13498 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013499{
Daniel Vetter53589012013-06-05 13:34:16 +020013500 uint32_t val;
13501
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013502 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013503 return false;
13504
Daniel Vetter53589012013-06-05 13:34:16 +020013505 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013506 hw_state->dpll = val;
13507 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13508 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013509
13510 return val & DPLL_VCO_ENABLE;
13511}
13512
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013513static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13514 struct intel_shared_dpll *pll)
13515{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013516 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13517 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013518}
13519
Daniel Vettere7b903d2013-06-05 13:34:14 +020013520static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13521 struct intel_shared_dpll *pll)
13522{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013523 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013524 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013525
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013526 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013527
13528 /* Wait for the clocks to stabilize. */
13529 POSTING_READ(PCH_DPLL(pll->id));
13530 udelay(150);
13531
13532 /* The pixel multiplier can only be updated once the
13533 * DPLL is enabled and the clocks are stable.
13534 *
13535 * So write it again.
13536 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013537 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013538 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013539 udelay(200);
13540}
13541
13542static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13543 struct intel_shared_dpll *pll)
13544{
13545 struct drm_device *dev = dev_priv->dev;
13546 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013547
13548 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013549 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013550 if (intel_crtc_to_shared_dpll(crtc) == pll)
13551 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13552 }
13553
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013554 I915_WRITE(PCH_DPLL(pll->id), 0);
13555 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013556 udelay(200);
13557}
13558
Daniel Vetter46edb022013-06-05 13:34:12 +020013559static char *ibx_pch_dpll_names[] = {
13560 "PCH DPLL A",
13561 "PCH DPLL B",
13562};
13563
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013564static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013565{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013566 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013567 int i;
13568
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013569 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013570
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013571 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013572 dev_priv->shared_dplls[i].id = i;
13573 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013574 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013575 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13576 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013577 dev_priv->shared_dplls[i].get_hw_state =
13578 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013579 }
13580}
13581
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013582static void intel_shared_dpll_init(struct drm_device *dev)
13583{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013584 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013585
Ville Syrjäläb6283052015-06-03 15:45:07 +030013586 intel_update_cdclk(dev);
13587
Daniel Vetter9cd86932014-06-25 22:01:57 +030013588 if (HAS_DDI(dev))
13589 intel_ddi_pll_init(dev);
13590 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013591 ibx_pch_dpll_init(dev);
13592 else
13593 dev_priv->num_shared_dpll = 0;
13594
13595 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013596}
13597
Matt Roper6beb8c232014-12-01 15:40:14 -080013598/**
13599 * intel_prepare_plane_fb - Prepare fb for usage on plane
13600 * @plane: drm plane to prepare for
13601 * @fb: framebuffer to prepare for presentation
13602 *
13603 * Prepares a framebuffer for usage on a display plane. Generally this
13604 * involves pinning the underlying object and updating the frontbuffer tracking
13605 * bits. Some older platforms need special physical address handling for
13606 * cursor planes.
13607 *
13608 * Returns 0 on success, negative error code on failure.
13609 */
13610int
13611intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013612 struct drm_framebuffer *fb,
13613 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013614{
13615 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013616 struct intel_plane *intel_plane = to_intel_plane(plane);
13617 enum pipe pipe = intel_plane->pipe;
13618 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13619 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13620 unsigned frontbuffer_bits = 0;
13621 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013622
Matt Roperea2c67b2014-12-23 10:41:52 -080013623 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013624 return 0;
13625
Matt Roper6beb8c232014-12-01 15:40:14 -080013626 switch (plane->type) {
13627 case DRM_PLANE_TYPE_PRIMARY:
13628 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13629 break;
13630 case DRM_PLANE_TYPE_CURSOR:
13631 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13632 break;
13633 case DRM_PLANE_TYPE_OVERLAY:
13634 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13635 break;
13636 }
Matt Roper465c1202014-05-29 08:06:54 -070013637
Matt Roper4c345742014-07-09 16:22:10 -070013638 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013639
Matt Roper6beb8c232014-12-01 15:40:14 -080013640 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13641 INTEL_INFO(dev)->cursor_needs_physical) {
13642 int align = IS_I830(dev) ? 16 * 1024 : 256;
13643 ret = i915_gem_object_attach_phys(obj, align);
13644 if (ret)
13645 DRM_DEBUG_KMS("failed to attach phys object\n");
13646 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013647 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013648 }
13649
13650 if (ret == 0)
13651 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13652
13653 mutex_unlock(&dev->struct_mutex);
13654
13655 return ret;
13656}
13657
Matt Roper38f3ce32014-12-02 07:45:25 -080013658/**
13659 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13660 * @plane: drm plane to clean up for
13661 * @fb: old framebuffer that was on plane
13662 *
13663 * Cleans up a framebuffer that has just been removed from a plane.
13664 */
13665void
13666intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013667 struct drm_framebuffer *fb,
13668 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013669{
13670 struct drm_device *dev = plane->dev;
13671 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13672
13673 if (WARN_ON(!obj))
13674 return;
13675
13676 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13677 !INTEL_INFO(dev)->cursor_needs_physical) {
13678 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013679 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013680 mutex_unlock(&dev->struct_mutex);
13681 }
Matt Roper465c1202014-05-29 08:06:54 -070013682}
13683
Chandra Konduru6156a452015-04-27 13:48:39 -070013684int
13685skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13686{
13687 int max_scale;
13688 struct drm_device *dev;
13689 struct drm_i915_private *dev_priv;
13690 int crtc_clock, cdclk;
13691
13692 if (!intel_crtc || !crtc_state)
13693 return DRM_PLANE_HELPER_NO_SCALING;
13694
13695 dev = intel_crtc->base.dev;
13696 dev_priv = dev->dev_private;
13697 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13698 cdclk = dev_priv->display.get_display_clock_speed(dev);
13699
13700 if (!crtc_clock || !cdclk)
13701 return DRM_PLANE_HELPER_NO_SCALING;
13702
13703 /*
13704 * skl max scale is lower of:
13705 * close to 3 but not 3, -1 is for that purpose
13706 * or
13707 * cdclk/crtc_clock
13708 */
13709 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13710
13711 return max_scale;
13712}
13713
Matt Roper465c1202014-05-29 08:06:54 -070013714static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013715intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013716 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013717 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013718{
Matt Roper2b875c22014-12-01 15:40:13 -080013719 struct drm_crtc *crtc = state->base.crtc;
13720 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013721 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013722 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13723 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013724
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013725 /* use scaler when colorkey is not required */
13726 if (INTEL_INFO(plane->dev)->gen >= 9 &&
13727 to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13728 min_scale = 1;
13729 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013730 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013731 }
Sonika Jindald8106362015-04-10 14:37:28 +053013732
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013733 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13734 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013735 min_scale, max_scale,
13736 can_position, true,
13737 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013738}
13739
Gustavo Padovan14af2932014-10-24 14:51:31 +010013740static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013741intel_commit_primary_plane(struct drm_plane *plane,
13742 struct intel_plane_state *state)
13743{
Matt Roper2b875c22014-12-01 15:40:13 -080013744 struct drm_crtc *crtc = state->base.crtc;
13745 struct drm_framebuffer *fb = state->base.fb;
13746 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013747 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013748 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013749 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013750
Matt Roperea2c67b2014-12-23 10:41:52 -080013751 crtc = crtc ? crtc : plane->crtc;
13752 intel_crtc = to_intel_crtc(crtc);
13753
Matt Ropercf4c7c12014-12-04 10:27:42 -080013754 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013755 crtc->x = src->x1 >> 16;
13756 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013757
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013758 if (!intel_crtc->active)
13759 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013760
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013761 if (state->visible)
13762 /* FIXME: kill this fastboot hack */
13763 intel_update_pipe_size(intel_crtc);
13764
13765 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013766}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013767
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013768static void
13769intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013770 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013771{
13772 struct drm_device *dev = plane->dev;
13773 struct drm_i915_private *dev_priv = dev->dev_private;
13774
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013775 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13776}
13777
Matt Roper32b7eee2014-12-24 07:59:06 -080013778static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13779{
13780 struct drm_device *dev = crtc->dev;
13781 struct drm_i915_private *dev_priv = dev->dev_private;
13782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5c2db182015-06-01 12:50:11 +020013783 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
Matt Roperea2c67b2014-12-23 10:41:52 -080013784 struct intel_plane *intel_plane;
13785 struct drm_plane *p;
13786 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013787
Matt Roperea2c67b2014-12-23 10:41:52 -080013788 /* Track fb's for any planes being disabled */
13789 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13790 intel_plane = to_intel_plane(p);
13791
13792 if (intel_crtc->atomic.disabled_planes &
13793 (1 << drm_plane_index(p))) {
13794 switch (p->type) {
13795 case DRM_PLANE_TYPE_PRIMARY:
13796 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13797 break;
13798 case DRM_PLANE_TYPE_CURSOR:
13799 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13800 break;
13801 case DRM_PLANE_TYPE_OVERLAY:
13802 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13803 break;
13804 }
13805
13806 mutex_lock(&dev->struct_mutex);
13807 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13808 mutex_unlock(&dev->struct_mutex);
13809 }
13810 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013811
Matt Roper32b7eee2014-12-24 07:59:06 -080013812 if (intel_crtc->atomic.wait_for_flips)
13813 intel_crtc_wait_for_pending_flips(crtc);
13814
13815 if (intel_crtc->atomic.disable_fbc)
13816 intel_fbc_disable(dev);
13817
13818 if (intel_crtc->atomic.pre_disable_primary)
13819 intel_pre_disable_primary(crtc);
13820
13821 if (intel_crtc->atomic.update_wm)
13822 intel_update_watermarks(crtc);
13823
13824 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013825
13826 /* Perform vblank evasion around commit operation */
Maarten Lankhorst5c2db182015-06-01 12:50:11 +020013827 if (crtc_state->active && !needs_modeset(crtc_state))
Matt Roperc34c9ee2014-12-23 10:41:50 -080013828 intel_crtc->atomic.evade =
13829 intel_pipe_update_start(intel_crtc,
13830 &intel_crtc->atomic.start_vbl_count);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013831
13832 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13833 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013834}
13835
13836static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13837{
13838 struct drm_device *dev = crtc->dev;
13839 struct drm_i915_private *dev_priv = dev->dev_private;
13840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13841 struct drm_plane *p;
13842
Matt Roperc34c9ee2014-12-23 10:41:50 -080013843 if (intel_crtc->atomic.evade)
13844 intel_pipe_update_end(intel_crtc,
13845 intel_crtc->atomic.start_vbl_count);
13846
Matt Roper32b7eee2014-12-24 07:59:06 -080013847 intel_runtime_pm_put(dev_priv);
13848
Maarten Lankhorst8a8f7f42015-06-01 12:49:55 +020013849 if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
Matt Roper32b7eee2014-12-24 07:59:06 -080013850 intel_wait_for_vblank(dev, intel_crtc->pipe);
13851
13852 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13853
13854 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013855 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013856 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013857 mutex_unlock(&dev->struct_mutex);
13858 }
Matt Roper465c1202014-05-29 08:06:54 -070013859
Matt Roper32b7eee2014-12-24 07:59:06 -080013860 if (intel_crtc->atomic.post_enable_primary)
13861 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013862
Matt Roper32b7eee2014-12-24 07:59:06 -080013863 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13864 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13865 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13866 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013867
Matt Roper32b7eee2014-12-24 07:59:06 -080013868 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013869}
13870
Matt Ropercf4c7c12014-12-04 10:27:42 -080013871/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013872 * intel_plane_destroy - destroy a plane
13873 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013874 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013875 * Common destruction function for all types of planes (primary, cursor,
13876 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013877 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013878void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013879{
13880 struct intel_plane *intel_plane = to_intel_plane(plane);
13881 drm_plane_cleanup(plane);
13882 kfree(intel_plane);
13883}
13884
Matt Roper65a3fea2015-01-21 16:35:42 -080013885const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013886 .update_plane = drm_atomic_helper_update_plane,
13887 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013888 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013889 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013890 .atomic_get_property = intel_plane_atomic_get_property,
13891 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013892 .atomic_duplicate_state = intel_plane_duplicate_state,
13893 .atomic_destroy_state = intel_plane_destroy_state,
13894
Matt Roper465c1202014-05-29 08:06:54 -070013895};
13896
13897static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13898 int pipe)
13899{
13900 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013901 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013902 const uint32_t *intel_primary_formats;
13903 int num_formats;
13904
13905 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13906 if (primary == NULL)
13907 return NULL;
13908
Matt Roper8e7d6882015-01-21 16:35:41 -080013909 state = intel_create_plane_state(&primary->base);
13910 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013911 kfree(primary);
13912 return NULL;
13913 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013914 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013915
Matt Roper465c1202014-05-29 08:06:54 -070013916 primary->can_scale = false;
13917 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013918 if (INTEL_INFO(dev)->gen >= 9) {
13919 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013920 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013921 }
Matt Roper465c1202014-05-29 08:06:54 -070013922 primary->pipe = pipe;
13923 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013924 primary->check_plane = intel_check_primary_plane;
13925 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013926 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013927 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013928 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13929 primary->plane = !pipe;
13930
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013931 if (INTEL_INFO(dev)->gen >= 9) {
13932 intel_primary_formats = skl_primary_formats;
13933 num_formats = ARRAY_SIZE(skl_primary_formats);
13934 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013935 intel_primary_formats = i965_primary_formats;
13936 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013937 } else {
13938 intel_primary_formats = i8xx_primary_formats;
13939 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013940 }
13941
13942 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013943 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013944 intel_primary_formats, num_formats,
13945 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013946
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013947 if (INTEL_INFO(dev)->gen >= 4)
13948 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013949
Matt Roperea2c67b2014-12-23 10:41:52 -080013950 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13951
Matt Roper465c1202014-05-29 08:06:54 -070013952 return &primary->base;
13953}
13954
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013955void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13956{
13957 if (!dev->mode_config.rotation_property) {
13958 unsigned long flags = BIT(DRM_ROTATE_0) |
13959 BIT(DRM_ROTATE_180);
13960
13961 if (INTEL_INFO(dev)->gen >= 9)
13962 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13963
13964 dev->mode_config.rotation_property =
13965 drm_mode_create_rotation_property(dev, flags);
13966 }
13967 if (dev->mode_config.rotation_property)
13968 drm_object_attach_property(&plane->base.base,
13969 dev->mode_config.rotation_property,
13970 plane->base.state->rotation);
13971}
13972
Matt Roper3d7d6512014-06-10 08:28:13 -070013973static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013974intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013975 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013976 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013977{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013978 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013979 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013980 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013981 unsigned stride;
13982 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013983
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013984 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13985 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013986 DRM_PLANE_HELPER_NO_SCALING,
13987 DRM_PLANE_HELPER_NO_SCALING,
13988 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013989 if (ret)
13990 return ret;
13991
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013992 /* if we want to turn off the cursor ignore width and height */
13993 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013994 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013995
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013996 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013997 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013998 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13999 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014000 return -EINVAL;
14001 }
14002
Matt Roperea2c67b2014-12-23 10:41:52 -080014003 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14004 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014005 DRM_DEBUG_KMS("buffer is too small\n");
14006 return -ENOMEM;
14007 }
14008
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014009 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014010 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014011 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014012 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014013
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014014 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014015}
14016
Matt Roperf4a2cf22014-12-01 15:40:12 -080014017static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014018intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014019 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014020{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014021 intel_crtc_update_cursor(crtc, false);
14022}
14023
14024static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030014025intel_commit_cursor_plane(struct drm_plane *plane,
14026 struct intel_plane_state *state)
14027{
Matt Roper2b875c22014-12-01 15:40:13 -080014028 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080014029 struct drm_device *dev = plane->dev;
14030 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014031 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014032 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014033
Matt Roperea2c67b2014-12-23 10:41:52 -080014034 crtc = crtc ? crtc : plane->crtc;
14035 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070014036
Matt Roperea2c67b2014-12-23 10:41:52 -080014037 plane->fb = state->base.fb;
14038 crtc->cursor_x = state->base.crtc_x;
14039 crtc->cursor_y = state->base.crtc_y;
14040
Gustavo Padovana912f122014-12-01 15:40:10 -080014041 if (intel_crtc->cursor_bo == obj)
14042 goto update;
14043
Matt Roperf4a2cf22014-12-01 15:40:12 -080014044 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014045 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014046 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014047 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014048 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014049 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014050
Gustavo Padovana912f122014-12-01 15:40:10 -080014051 intel_crtc->cursor_addr = addr;
14052 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080014053
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020014054update:
Matt Roper32b7eee2014-12-24 07:59:06 -080014055 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014056 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014057}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014058
Matt Roper3d7d6512014-06-10 08:28:13 -070014059static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14060 int pipe)
14061{
14062 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014063 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014064
14065 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14066 if (cursor == NULL)
14067 return NULL;
14068
Matt Roper8e7d6882015-01-21 16:35:41 -080014069 state = intel_create_plane_state(&cursor->base);
14070 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014071 kfree(cursor);
14072 return NULL;
14073 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014074 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014075
Matt Roper3d7d6512014-06-10 08:28:13 -070014076 cursor->can_scale = false;
14077 cursor->max_downscale = 1;
14078 cursor->pipe = pipe;
14079 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080014080 cursor->check_plane = intel_check_cursor_plane;
14081 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014082 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014083
14084 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014085 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014086 intel_cursor_formats,
14087 ARRAY_SIZE(intel_cursor_formats),
14088 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014089
14090 if (INTEL_INFO(dev)->gen >= 4) {
14091 if (!dev->mode_config.rotation_property)
14092 dev->mode_config.rotation_property =
14093 drm_mode_create_rotation_property(dev,
14094 BIT(DRM_ROTATE_0) |
14095 BIT(DRM_ROTATE_180));
14096 if (dev->mode_config.rotation_property)
14097 drm_object_attach_property(&cursor->base.base,
14098 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014099 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014100 }
14101
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014102 if (INTEL_INFO(dev)->gen >=9)
14103 state->scaler_id = -1;
14104
Matt Roperea2c67b2014-12-23 10:41:52 -080014105 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14106
Matt Roper3d7d6512014-06-10 08:28:13 -070014107 return &cursor->base;
14108}
14109
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014110static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14111 struct intel_crtc_state *crtc_state)
14112{
14113 int i;
14114 struct intel_scaler *intel_scaler;
14115 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14116
14117 for (i = 0; i < intel_crtc->num_scalers; i++) {
14118 intel_scaler = &scaler_state->scalers[i];
14119 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014120 intel_scaler->mode = PS_SCALER_MODE_DYN;
14121 }
14122
14123 scaler_state->scaler_id = -1;
14124}
14125
Hannes Ederb358d0a2008-12-18 21:18:47 +010014126static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014127{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014128 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014129 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014130 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014131 struct drm_plane *primary = NULL;
14132 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014133 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014134
Daniel Vetter955382f2013-09-19 14:05:45 +020014135 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014136 if (intel_crtc == NULL)
14137 return;
14138
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014139 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14140 if (!crtc_state)
14141 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014142 intel_crtc->config = crtc_state;
14143 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014144 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014145
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014146 /* initialize shared scalers */
14147 if (INTEL_INFO(dev)->gen >= 9) {
14148 if (pipe == PIPE_C)
14149 intel_crtc->num_scalers = 1;
14150 else
14151 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14152
14153 skl_init_scalers(dev, intel_crtc, crtc_state);
14154 }
14155
Matt Roper465c1202014-05-29 08:06:54 -070014156 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014157 if (!primary)
14158 goto fail;
14159
14160 cursor = intel_cursor_plane_create(dev, pipe);
14161 if (!cursor)
14162 goto fail;
14163
Matt Roper465c1202014-05-29 08:06:54 -070014164 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014165 cursor, &intel_crtc_funcs);
14166 if (ret)
14167 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014168
14169 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014170 for (i = 0; i < 256; i++) {
14171 intel_crtc->lut_r[i] = i;
14172 intel_crtc->lut_g[i] = i;
14173 intel_crtc->lut_b[i] = i;
14174 }
14175
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014176 /*
14177 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014178 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014179 */
Jesse Barnes80824002009-09-10 15:28:06 -070014180 intel_crtc->pipe = pipe;
14181 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014182 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014183 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014184 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014185 }
14186
Chris Wilson4b0e3332014-05-30 16:35:26 +030014187 intel_crtc->cursor_base = ~0;
14188 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014189 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014190
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014191 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14192 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14193 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14194 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14195
Jesse Barnes79e53942008-11-07 14:24:08 -080014196 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014197
14198 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014199 return;
14200
14201fail:
14202 if (primary)
14203 drm_plane_cleanup(primary);
14204 if (cursor)
14205 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014206 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014207 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014208}
14209
Jesse Barnes752aa882013-10-31 18:55:49 +020014210enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14211{
14212 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014213 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014214
Rob Clark51fd3712013-11-19 12:10:12 -050014215 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014216
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014217 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014218 return INVALID_PIPE;
14219
14220 return to_intel_crtc(encoder->crtc)->pipe;
14221}
14222
Carl Worth08d7b3d2009-04-29 14:43:54 -070014223int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014224 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014225{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014226 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014227 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014228 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014229
Rob Clark7707e652014-07-17 23:30:04 -040014230 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014231
Rob Clark7707e652014-07-17 23:30:04 -040014232 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014233 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014234 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014235 }
14236
Rob Clark7707e652014-07-17 23:30:04 -040014237 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014238 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014239
Daniel Vetterc05422d2009-08-11 16:05:30 +020014240 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014241}
14242
Daniel Vetter66a92782012-07-12 20:08:18 +020014243static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014244{
Daniel Vetter66a92782012-07-12 20:08:18 +020014245 struct drm_device *dev = encoder->base.dev;
14246 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014247 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014248 int entry = 0;
14249
Damien Lespiaub2784e12014-08-05 11:29:37 +010014250 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014251 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014252 index_mask |= (1 << entry);
14253
Jesse Barnes79e53942008-11-07 14:24:08 -080014254 entry++;
14255 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014256
Jesse Barnes79e53942008-11-07 14:24:08 -080014257 return index_mask;
14258}
14259
Chris Wilson4d302442010-12-14 19:21:29 +000014260static bool has_edp_a(struct drm_device *dev)
14261{
14262 struct drm_i915_private *dev_priv = dev->dev_private;
14263
14264 if (!IS_MOBILE(dev))
14265 return false;
14266
14267 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14268 return false;
14269
Damien Lespiaue3589902014-02-07 19:12:50 +000014270 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014271 return false;
14272
14273 return true;
14274}
14275
Jesse Barnes84b4e042014-06-25 08:24:29 -070014276static bool intel_crt_present(struct drm_device *dev)
14277{
14278 struct drm_i915_private *dev_priv = dev->dev_private;
14279
Damien Lespiau884497e2013-12-03 13:56:23 +000014280 if (INTEL_INFO(dev)->gen >= 9)
14281 return false;
14282
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014283 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014284 return false;
14285
14286 if (IS_CHERRYVIEW(dev))
14287 return false;
14288
14289 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14290 return false;
14291
14292 return true;
14293}
14294
Jesse Barnes79e53942008-11-07 14:24:08 -080014295static void intel_setup_outputs(struct drm_device *dev)
14296{
Eric Anholt725e30a2009-01-22 13:01:02 -080014297 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014298 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014299 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014300
Daniel Vetterc9093352013-06-06 22:22:47 +020014301 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014302
Jesse Barnes84b4e042014-06-25 08:24:29 -070014303 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014304 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014305
Vandana Kannanc776eb22014-08-19 12:05:01 +053014306 if (IS_BROXTON(dev)) {
14307 /*
14308 * FIXME: Broxton doesn't support port detection via the
14309 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14310 * detect the ports.
14311 */
14312 intel_ddi_init(dev, PORT_A);
14313 intel_ddi_init(dev, PORT_B);
14314 intel_ddi_init(dev, PORT_C);
14315 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014316 int found;
14317
Jesse Barnesde31fac2015-03-06 15:53:32 -080014318 /*
14319 * Haswell uses DDI functions to detect digital outputs.
14320 * On SKL pre-D0 the strap isn't connected, so we assume
14321 * it's there.
14322 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014323 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014324 /* WaIgnoreDDIAStrap: skl */
14325 if (found ||
14326 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014327 intel_ddi_init(dev, PORT_A);
14328
14329 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14330 * register */
14331 found = I915_READ(SFUSE_STRAP);
14332
14333 if (found & SFUSE_STRAP_DDIB_DETECTED)
14334 intel_ddi_init(dev, PORT_B);
14335 if (found & SFUSE_STRAP_DDIC_DETECTED)
14336 intel_ddi_init(dev, PORT_C);
14337 if (found & SFUSE_STRAP_DDID_DETECTED)
14338 intel_ddi_init(dev, PORT_D);
14339 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014340 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014341 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014342
14343 if (has_edp_a(dev))
14344 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014345
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014346 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014347 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014348 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014349 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014350 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014351 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014352 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014353 }
14354
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014355 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014356 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014357
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014358 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014359 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014360
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014361 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014362 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014363
Daniel Vetter270b3042012-10-27 15:52:05 +020014364 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014365 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014366 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014367 /*
14368 * The DP_DETECTED bit is the latched state of the DDC
14369 * SDA pin at boot. However since eDP doesn't require DDC
14370 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14371 * eDP ports may have been muxed to an alternate function.
14372 * Thus we can't rely on the DP_DETECTED bit alone to detect
14373 * eDP ports. Consult the VBT as well as DP_DETECTED to
14374 * detect eDP ports.
14375 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014376 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14377 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014378 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14379 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014380 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14381 intel_dp_is_edp(dev, PORT_B))
14382 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014383
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014384 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14385 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014386 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14387 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014388 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14389 intel_dp_is_edp(dev, PORT_C))
14390 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014391
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014392 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014393 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014394 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14395 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014396 /* eDP not supported on port D, so don't check VBT */
14397 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14398 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014399 }
14400
Jani Nikula3cfca972013-08-27 15:12:26 +030014401 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014402 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014403 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014404
Paulo Zanonie2debe92013-02-18 19:00:27 -030014405 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014406 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014407 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014408 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14409 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014410 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014411 }
Ma Ling27185ae2009-08-24 13:50:23 +080014412
Imre Deake7281ea2013-05-08 13:14:08 +030014413 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014414 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014415 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014416
14417 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014418
Paulo Zanonie2debe92013-02-18 19:00:27 -030014419 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014420 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014421 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014422 }
Ma Ling27185ae2009-08-24 13:50:23 +080014423
Paulo Zanonie2debe92013-02-18 19:00:27 -030014424 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014425
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014426 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14427 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014428 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014429 }
Imre Deake7281ea2013-05-08 13:14:08 +030014430 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014431 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014432 }
Ma Ling27185ae2009-08-24 13:50:23 +080014433
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014434 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014435 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014436 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014437 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014438 intel_dvo_init(dev);
14439
Zhenyu Wang103a1962009-11-27 11:44:36 +080014440 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014441 intel_tv_init(dev);
14442
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014443 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014444
Damien Lespiaub2784e12014-08-05 11:29:37 +010014445 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014446 encoder->base.possible_crtcs = encoder->crtc_mask;
14447 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014448 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014449 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014450
Paulo Zanonidde86e22012-12-01 12:04:25 -020014451 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014452
14453 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014454}
14455
14456static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14457{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014458 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014459 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014460
Daniel Vetteref2d6332014-02-10 18:00:38 +010014461 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014462 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014463 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014464 drm_gem_object_unreference(&intel_fb->obj->base);
14465 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014466 kfree(intel_fb);
14467}
14468
14469static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014470 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014471 unsigned int *handle)
14472{
14473 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014474 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014475
Chris Wilson05394f32010-11-08 19:18:58 +000014476 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014477}
14478
14479static const struct drm_framebuffer_funcs intel_fb_funcs = {
14480 .destroy = intel_user_framebuffer_destroy,
14481 .create_handle = intel_user_framebuffer_create_handle,
14482};
14483
Damien Lespiaub3218032015-02-27 11:15:18 +000014484static
14485u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14486 uint32_t pixel_format)
14487{
14488 u32 gen = INTEL_INFO(dev)->gen;
14489
14490 if (gen >= 9) {
14491 /* "The stride in bytes must not exceed the of the size of 8K
14492 * pixels and 32K bytes."
14493 */
14494 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14495 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14496 return 32*1024;
14497 } else if (gen >= 4) {
14498 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14499 return 16*1024;
14500 else
14501 return 32*1024;
14502 } else if (gen >= 3) {
14503 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14504 return 8*1024;
14505 else
14506 return 16*1024;
14507 } else {
14508 /* XXX DSPC is limited to 4k tiled */
14509 return 8*1024;
14510 }
14511}
14512
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014513static int intel_framebuffer_init(struct drm_device *dev,
14514 struct intel_framebuffer *intel_fb,
14515 struct drm_mode_fb_cmd2 *mode_cmd,
14516 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014517{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014518 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014519 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014520 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014521
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014522 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14523
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014524 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14525 /* Enforce that fb modifier and tiling mode match, but only for
14526 * X-tiled. This is needed for FBC. */
14527 if (!!(obj->tiling_mode == I915_TILING_X) !=
14528 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14529 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14530 return -EINVAL;
14531 }
14532 } else {
14533 if (obj->tiling_mode == I915_TILING_X)
14534 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14535 else if (obj->tiling_mode == I915_TILING_Y) {
14536 DRM_DEBUG("No Y tiling for legacy addfb\n");
14537 return -EINVAL;
14538 }
14539 }
14540
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014541 /* Passed in modifier sanity checking. */
14542 switch (mode_cmd->modifier[0]) {
14543 case I915_FORMAT_MOD_Y_TILED:
14544 case I915_FORMAT_MOD_Yf_TILED:
14545 if (INTEL_INFO(dev)->gen < 9) {
14546 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14547 mode_cmd->modifier[0]);
14548 return -EINVAL;
14549 }
14550 case DRM_FORMAT_MOD_NONE:
14551 case I915_FORMAT_MOD_X_TILED:
14552 break;
14553 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014554 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14555 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014556 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014557 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014558
Damien Lespiaub3218032015-02-27 11:15:18 +000014559 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14560 mode_cmd->pixel_format);
14561 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14562 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14563 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014564 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014565 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014566
Damien Lespiaub3218032015-02-27 11:15:18 +000014567 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14568 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014569 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014570 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14571 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014572 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014573 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014574 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014575 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014576
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014577 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014578 mode_cmd->pitches[0] != obj->stride) {
14579 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14580 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014581 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014582 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014583
Ville Syrjälä57779d02012-10-31 17:50:14 +020014584 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014585 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014586 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014587 case DRM_FORMAT_RGB565:
14588 case DRM_FORMAT_XRGB8888:
14589 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014590 break;
14591 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014592 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014593 DRM_DEBUG("unsupported pixel format: %s\n",
14594 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014595 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014596 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014597 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014598 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014599 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14600 DRM_DEBUG("unsupported pixel format: %s\n",
14601 drm_get_format_name(mode_cmd->pixel_format));
14602 return -EINVAL;
14603 }
14604 break;
14605 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014606 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014607 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014608 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014609 DRM_DEBUG("unsupported pixel format: %s\n",
14610 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014611 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014612 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014613 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014614 case DRM_FORMAT_ABGR2101010:
14615 if (!IS_VALLEYVIEW(dev)) {
14616 DRM_DEBUG("unsupported pixel format: %s\n",
14617 drm_get_format_name(mode_cmd->pixel_format));
14618 return -EINVAL;
14619 }
14620 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014621 case DRM_FORMAT_YUYV:
14622 case DRM_FORMAT_UYVY:
14623 case DRM_FORMAT_YVYU:
14624 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014625 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014626 DRM_DEBUG("unsupported pixel format: %s\n",
14627 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014628 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014629 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014630 break;
14631 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014632 DRM_DEBUG("unsupported pixel format: %s\n",
14633 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014634 return -EINVAL;
14635 }
14636
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014637 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14638 if (mode_cmd->offsets[0] != 0)
14639 return -EINVAL;
14640
Damien Lespiauec2c9812015-01-20 12:51:45 +000014641 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014642 mode_cmd->pixel_format,
14643 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014644 /* FIXME drm helper for size checks (especially planar formats)? */
14645 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14646 return -EINVAL;
14647
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014648 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14649 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014650 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014651
Jesse Barnes79e53942008-11-07 14:24:08 -080014652 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14653 if (ret) {
14654 DRM_ERROR("framebuffer init failed %d\n", ret);
14655 return ret;
14656 }
14657
Jesse Barnes79e53942008-11-07 14:24:08 -080014658 return 0;
14659}
14660
Jesse Barnes79e53942008-11-07 14:24:08 -080014661static struct drm_framebuffer *
14662intel_user_framebuffer_create(struct drm_device *dev,
14663 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014664 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014665{
Chris Wilson05394f32010-11-08 19:18:58 +000014666 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014667
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014668 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14669 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014670 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014671 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014672
Chris Wilsond2dff872011-04-19 08:36:26 +010014673 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014674}
14675
Daniel Vetter4520f532013-10-09 09:18:51 +020014676#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014677static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014678{
14679}
14680#endif
14681
Jesse Barnes79e53942008-11-07 14:24:08 -080014682static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014683 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014684 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014685 .atomic_check = intel_atomic_check,
14686 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014687 .atomic_state_alloc = intel_atomic_state_alloc,
14688 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014689};
14690
Jesse Barnese70236a2009-09-21 10:42:27 -070014691/* Set up chip specific display functions */
14692static void intel_init_display(struct drm_device *dev)
14693{
14694 struct drm_i915_private *dev_priv = dev->dev_private;
14695
Daniel Vetteree9300b2013-06-03 22:40:22 +020014696 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14697 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014698 else if (IS_CHERRYVIEW(dev))
14699 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014700 else if (IS_VALLEYVIEW(dev))
14701 dev_priv->display.find_dpll = vlv_find_best_dpll;
14702 else if (IS_PINEVIEW(dev))
14703 dev_priv->display.find_dpll = pnv_find_best_dpll;
14704 else
14705 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14706
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014707 if (INTEL_INFO(dev)->gen >= 9) {
14708 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014709 dev_priv->display.get_initial_plane_config =
14710 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014711 dev_priv->display.crtc_compute_clock =
14712 haswell_crtc_compute_clock;
14713 dev_priv->display.crtc_enable = haswell_crtc_enable;
14714 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014715 dev_priv->display.update_primary_plane =
14716 skylake_update_primary_plane;
14717 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014718 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014719 dev_priv->display.get_initial_plane_config =
14720 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014721 dev_priv->display.crtc_compute_clock =
14722 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014723 dev_priv->display.crtc_enable = haswell_crtc_enable;
14724 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014725 dev_priv->display.update_primary_plane =
14726 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014727 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014728 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014729 dev_priv->display.get_initial_plane_config =
14730 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014731 dev_priv->display.crtc_compute_clock =
14732 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014733 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14734 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014735 dev_priv->display.update_primary_plane =
14736 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014737 } else if (IS_VALLEYVIEW(dev)) {
14738 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014739 dev_priv->display.get_initial_plane_config =
14740 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014741 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014742 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14743 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014744 dev_priv->display.update_primary_plane =
14745 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014746 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014747 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014748 dev_priv->display.get_initial_plane_config =
14749 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014750 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014751 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14752 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014753 dev_priv->display.update_primary_plane =
14754 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014755 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014756
Jesse Barnese70236a2009-09-21 10:42:27 -070014757 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014758 if (IS_SKYLAKE(dev))
14759 dev_priv->display.get_display_clock_speed =
14760 skylake_get_display_clock_speed;
14761 else if (IS_BROADWELL(dev))
14762 dev_priv->display.get_display_clock_speed =
14763 broadwell_get_display_clock_speed;
14764 else if (IS_HASWELL(dev))
14765 dev_priv->display.get_display_clock_speed =
14766 haswell_get_display_clock_speed;
14767 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014768 dev_priv->display.get_display_clock_speed =
14769 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014770 else if (IS_GEN5(dev))
14771 dev_priv->display.get_display_clock_speed =
14772 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014773 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014774 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014775 dev_priv->display.get_display_clock_speed =
14776 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014777 else if (IS_GM45(dev))
14778 dev_priv->display.get_display_clock_speed =
14779 gm45_get_display_clock_speed;
14780 else if (IS_CRESTLINE(dev))
14781 dev_priv->display.get_display_clock_speed =
14782 i965gm_get_display_clock_speed;
14783 else if (IS_PINEVIEW(dev))
14784 dev_priv->display.get_display_clock_speed =
14785 pnv_get_display_clock_speed;
14786 else if (IS_G33(dev) || IS_G4X(dev))
14787 dev_priv->display.get_display_clock_speed =
14788 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014789 else if (IS_I915G(dev))
14790 dev_priv->display.get_display_clock_speed =
14791 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014792 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014793 dev_priv->display.get_display_clock_speed =
14794 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014795 else if (IS_PINEVIEW(dev))
14796 dev_priv->display.get_display_clock_speed =
14797 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014798 else if (IS_I915GM(dev))
14799 dev_priv->display.get_display_clock_speed =
14800 i915gm_get_display_clock_speed;
14801 else if (IS_I865G(dev))
14802 dev_priv->display.get_display_clock_speed =
14803 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014804 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014805 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014806 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014807 else { /* 830 */
14808 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014809 dev_priv->display.get_display_clock_speed =
14810 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014811 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014812
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014813 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014814 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014815 } else if (IS_GEN6(dev)) {
14816 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014817 } else if (IS_IVYBRIDGE(dev)) {
14818 /* FIXME: detect B0+ stepping and use auto training */
14819 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014820 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014821 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030014822 if (IS_BROADWELL(dev))
14823 dev_priv->display.modeset_global_resources =
14824 broadwell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014825 } else if (IS_VALLEYVIEW(dev)) {
14826 dev_priv->display.modeset_global_resources =
14827 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014828 } else if (IS_BROXTON(dev)) {
14829 dev_priv->display.modeset_global_resources =
14830 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014831 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014832
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014833 switch (INTEL_INFO(dev)->gen) {
14834 case 2:
14835 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14836 break;
14837
14838 case 3:
14839 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14840 break;
14841
14842 case 4:
14843 case 5:
14844 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14845 break;
14846
14847 case 6:
14848 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14849 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014850 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014851 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014852 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14853 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014854 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014855 /* Drop through - unsupported since execlist only. */
14856 default:
14857 /* Default just returns -ENODEV to indicate unsupported */
14858 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014859 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014860
14861 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014862
14863 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014864}
14865
Jesse Barnesb690e962010-07-19 13:53:12 -070014866/*
14867 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14868 * resume, or other times. This quirk makes sure that's the case for
14869 * affected systems.
14870 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014871static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014872{
14873 struct drm_i915_private *dev_priv = dev->dev_private;
14874
14875 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014876 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014877}
14878
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014879static void quirk_pipeb_force(struct drm_device *dev)
14880{
14881 struct drm_i915_private *dev_priv = dev->dev_private;
14882
14883 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14884 DRM_INFO("applying pipe b force quirk\n");
14885}
14886
Keith Packard435793d2011-07-12 14:56:22 -070014887/*
14888 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14889 */
14890static void quirk_ssc_force_disable(struct drm_device *dev)
14891{
14892 struct drm_i915_private *dev_priv = dev->dev_private;
14893 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014894 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014895}
14896
Carsten Emde4dca20e2012-03-15 15:56:26 +010014897/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014898 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14899 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014900 */
14901static void quirk_invert_brightness(struct drm_device *dev)
14902{
14903 struct drm_i915_private *dev_priv = dev->dev_private;
14904 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014905 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014906}
14907
Scot Doyle9c72cc62014-07-03 23:27:50 +000014908/* Some VBT's incorrectly indicate no backlight is present */
14909static void quirk_backlight_present(struct drm_device *dev)
14910{
14911 struct drm_i915_private *dev_priv = dev->dev_private;
14912 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14913 DRM_INFO("applying backlight present quirk\n");
14914}
14915
Jesse Barnesb690e962010-07-19 13:53:12 -070014916struct intel_quirk {
14917 int device;
14918 int subsystem_vendor;
14919 int subsystem_device;
14920 void (*hook)(struct drm_device *dev);
14921};
14922
Egbert Eich5f85f172012-10-14 15:46:38 +020014923/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14924struct intel_dmi_quirk {
14925 void (*hook)(struct drm_device *dev);
14926 const struct dmi_system_id (*dmi_id_list)[];
14927};
14928
14929static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14930{
14931 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14932 return 1;
14933}
14934
14935static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14936 {
14937 .dmi_id_list = &(const struct dmi_system_id[]) {
14938 {
14939 .callback = intel_dmi_reverse_brightness,
14940 .ident = "NCR Corporation",
14941 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14942 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14943 },
14944 },
14945 { } /* terminating entry */
14946 },
14947 .hook = quirk_invert_brightness,
14948 },
14949};
14950
Ben Widawskyc43b5632012-04-16 14:07:40 -070014951static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014952 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14953 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14954
Jesse Barnesb690e962010-07-19 13:53:12 -070014955 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14956 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14957
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014958 /* 830 needs to leave pipe A & dpll A up */
14959 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14960
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014961 /* 830 needs to leave pipe B & dpll B up */
14962 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14963
Keith Packard435793d2011-07-12 14:56:22 -070014964 /* Lenovo U160 cannot use SSC on LVDS */
14965 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014966
14967 /* Sony Vaio Y cannot use SSC on LVDS */
14968 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014969
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014970 /* Acer Aspire 5734Z must invert backlight brightness */
14971 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14972
14973 /* Acer/eMachines G725 */
14974 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14975
14976 /* Acer/eMachines e725 */
14977 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14978
14979 /* Acer/Packard Bell NCL20 */
14980 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14981
14982 /* Acer Aspire 4736Z */
14983 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014984
14985 /* Acer Aspire 5336 */
14986 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014987
14988 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14989 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014990
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014991 /* Acer C720 Chromebook (Core i3 4005U) */
14992 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14993
jens steinb2a96012014-10-28 20:25:53 +010014994 /* Apple Macbook 2,1 (Core 2 T7400) */
14995 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14996
Scot Doyled4967d82014-07-03 23:27:52 +000014997 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14998 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014999
15000 /* HP Chromebook 14 (Celeron 2955U) */
15001 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015002
15003 /* Dell Chromebook 11 */
15004 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015005};
15006
15007static void intel_init_quirks(struct drm_device *dev)
15008{
15009 struct pci_dev *d = dev->pdev;
15010 int i;
15011
15012 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15013 struct intel_quirk *q = &intel_quirks[i];
15014
15015 if (d->device == q->device &&
15016 (d->subsystem_vendor == q->subsystem_vendor ||
15017 q->subsystem_vendor == PCI_ANY_ID) &&
15018 (d->subsystem_device == q->subsystem_device ||
15019 q->subsystem_device == PCI_ANY_ID))
15020 q->hook(dev);
15021 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015022 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15023 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15024 intel_dmi_quirks[i].hook(dev);
15025 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015026}
15027
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015028/* Disable the VGA plane that we never use */
15029static void i915_disable_vga(struct drm_device *dev)
15030{
15031 struct drm_i915_private *dev_priv = dev->dev_private;
15032 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015033 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015034
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015035 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015036 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015037 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015038 sr1 = inb(VGA_SR_DATA);
15039 outb(sr1 | 1<<5, VGA_SR_DATA);
15040 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15041 udelay(300);
15042
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015043 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015044 POSTING_READ(vga_reg);
15045}
15046
Daniel Vetterf8175862012-04-10 15:50:11 +020015047void intel_modeset_init_hw(struct drm_device *dev)
15048{
Ville Syrjäläb6283052015-06-03 15:45:07 +030015049 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030015050 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015051 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015052 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015053}
15054
Jesse Barnes79e53942008-11-07 14:24:08 -080015055void intel_modeset_init(struct drm_device *dev)
15056{
Jesse Barnes652c3932009-08-17 13:31:43 -070015057 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015058 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015059 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015060 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015061
15062 drm_mode_config_init(dev);
15063
15064 dev->mode_config.min_width = 0;
15065 dev->mode_config.min_height = 0;
15066
Dave Airlie019d96c2011-09-29 16:20:42 +010015067 dev->mode_config.preferred_depth = 24;
15068 dev->mode_config.prefer_shadow = 1;
15069
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015070 dev->mode_config.allow_fb_modifiers = true;
15071
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015072 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015073
Jesse Barnesb690e962010-07-19 13:53:12 -070015074 intel_init_quirks(dev);
15075
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015076 intel_init_pm(dev);
15077
Ben Widawskye3c74752013-04-05 13:12:39 -070015078 if (INTEL_INFO(dev)->num_pipes == 0)
15079 return;
15080
Jesse Barnese70236a2009-09-21 10:42:27 -070015081 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015082 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015083
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015084 if (IS_GEN2(dev)) {
15085 dev->mode_config.max_width = 2048;
15086 dev->mode_config.max_height = 2048;
15087 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015088 dev->mode_config.max_width = 4096;
15089 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015090 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015091 dev->mode_config.max_width = 8192;
15092 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015093 }
Damien Lespiau068be562014-03-28 14:17:49 +000015094
Ville Syrjälädc41c152014-08-13 11:57:05 +030015095 if (IS_845G(dev) || IS_I865G(dev)) {
15096 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15097 dev->mode_config.cursor_height = 1023;
15098 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015099 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15100 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15101 } else {
15102 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15103 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15104 }
15105
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015106 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015107
Zhao Yakui28c97732009-10-09 11:39:41 +080015108 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015109 INTEL_INFO(dev)->num_pipes,
15110 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015111
Damien Lespiau055e3932014-08-18 13:49:10 +010015112 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015113 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015114 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015115 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015116 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015117 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015118 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015119 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015120 }
15121
Jesse Barnesf42bb702013-12-16 16:34:23 -080015122 intel_init_dpio(dev);
15123
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015124 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015125
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015126 /* Just disable it once at startup */
15127 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015128 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015129
15130 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015131 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015132
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015133 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015134 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015135 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015136
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015137 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080015138 if (!crtc->active)
15139 continue;
15140
Jesse Barnes46f297f2014-03-07 08:57:48 -080015141 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015142 * Note that reserving the BIOS fb up front prevents us
15143 * from stuffing other stolen allocations like the ring
15144 * on top. This prevents some ugliness at boot time, and
15145 * can even allow for smooth boot transitions if the BIOS
15146 * fb is large enough for the active pipe configuration.
15147 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015148 if (dev_priv->display.get_initial_plane_config) {
15149 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080015150 &crtc->plane_config);
15151 /*
15152 * If the fb is shared between multiple heads, we'll
15153 * just get the first one.
15154 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010015155 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015156 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080015157 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015158}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015159
Daniel Vetter7fad7982012-07-04 17:51:47 +020015160static void intel_enable_pipe_a(struct drm_device *dev)
15161{
15162 struct intel_connector *connector;
15163 struct drm_connector *crt = NULL;
15164 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015165 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015166
15167 /* We can't just switch on the pipe A, we need to set things up with a
15168 * proper mode and output configuration. As a gross hack, enable pipe A
15169 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015170 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015171 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15172 crt = &connector->base;
15173 break;
15174 }
15175 }
15176
15177 if (!crt)
15178 return;
15179
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015180 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015181 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015182}
15183
Daniel Vetterfa555832012-10-10 23:14:00 +020015184static bool
15185intel_check_plane_mapping(struct intel_crtc *crtc)
15186{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015187 struct drm_device *dev = crtc->base.dev;
15188 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015189 u32 reg, val;
15190
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015191 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015192 return true;
15193
15194 reg = DSPCNTR(!crtc->plane);
15195 val = I915_READ(reg);
15196
15197 if ((val & DISPLAY_PLANE_ENABLE) &&
15198 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15199 return false;
15200
15201 return true;
15202}
15203
Daniel Vetter24929352012-07-02 20:28:59 +020015204static void intel_sanitize_crtc(struct intel_crtc *crtc)
15205{
15206 struct drm_device *dev = crtc->base.dev;
15207 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015208 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020015209 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015210 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020015211
Daniel Vetter24929352012-07-02 20:28:59 +020015212 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015213 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015214 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15215
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015216 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015217 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015218 if (crtc->active) {
15219 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015220 drm_crtc_vblank_on(&crtc->base);
15221 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015222
Daniel Vetter24929352012-07-02 20:28:59 +020015223 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015224 * disable the crtc (and hence change the state) if it is wrong. Note
15225 * that gen4+ has a fixed plane -> pipe mapping. */
15226 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015227 bool plane;
15228
Daniel Vetter24929352012-07-02 20:28:59 +020015229 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15230 crtc->base.base.id);
15231
15232 /* Pipe has the wrong plane attached and the plane is active.
15233 * Temporarily change the plane mapping and disable everything
15234 * ... */
15235 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015236 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015237 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015238 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015239 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015240 }
Daniel Vetter24929352012-07-02 20:28:59 +020015241
Daniel Vetter7fad7982012-07-04 17:51:47 +020015242 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15243 crtc->pipe == PIPE_A && !crtc->active) {
15244 /* BIOS forgot to enable pipe A, this mostly happens after
15245 * resume. Force-enable the pipe to fix this, the update_dpms
15246 * call below we restore the pipe to the right state, but leave
15247 * the required bits on. */
15248 intel_enable_pipe_a(dev);
15249 }
15250
Daniel Vetter24929352012-07-02 20:28:59 +020015251 /* Adjust the state of the output pipe according to whether we
15252 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015253 enable = false;
15254 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15255 enable |= encoder->connectors_active;
Daniel Vetter24929352012-07-02 20:28:59 +020015256
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015257 if (!enable)
15258 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015259
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015260 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015261
15262 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015263 * functions or because of calls to intel_crtc_disable_noatomic,
15264 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015265 * pipe A quirk. */
15266 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15267 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015268 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015269 crtc->active ? "enabled" : "disabled");
15270
Matt Roper83d65732015-02-25 13:12:16 -080015271 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015272 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015273 crtc->base.enabled = crtc->active;
15274
15275 /* Because we only establish the connector -> encoder ->
15276 * crtc links if something is active, this means the
15277 * crtc is now deactivated. Break the links. connector
15278 * -> encoder links are only establish when things are
15279 * actually up, hence no need to break them. */
15280 WARN_ON(crtc->active);
15281
15282 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15283 WARN_ON(encoder->connectors_active);
15284 encoder->base.crtc = NULL;
15285 }
15286 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015287
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015288 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015289 /*
15290 * We start out with underrun reporting disabled to avoid races.
15291 * For correct bookkeeping mark this on active crtcs.
15292 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015293 * Also on gmch platforms we dont have any hardware bits to
15294 * disable the underrun reporting. Which means we need to start
15295 * out with underrun reporting disabled also on inactive pipes,
15296 * since otherwise we'll complain about the garbage we read when
15297 * e.g. coming up after runtime pm.
15298 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015299 * No protection against concurrent access is required - at
15300 * worst a fifo underrun happens which also sets this to false.
15301 */
15302 crtc->cpu_fifo_underrun_disabled = true;
15303 crtc->pch_fifo_underrun_disabled = true;
15304 }
Daniel Vetter24929352012-07-02 20:28:59 +020015305}
15306
15307static void intel_sanitize_encoder(struct intel_encoder *encoder)
15308{
15309 struct intel_connector *connector;
15310 struct drm_device *dev = encoder->base.dev;
15311
15312 /* We need to check both for a crtc link (meaning that the
15313 * encoder is active and trying to read from a pipe) and the
15314 * pipe itself being active. */
15315 bool has_active_crtc = encoder->base.crtc &&
15316 to_intel_crtc(encoder->base.crtc)->active;
15317
15318 if (encoder->connectors_active && !has_active_crtc) {
15319 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15320 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015321 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015322
15323 /* Connector is active, but has no active pipe. This is
15324 * fallout from our resume register restoring. Disable
15325 * the encoder manually again. */
15326 if (encoder->base.crtc) {
15327 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15328 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015329 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015330 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015331 if (encoder->post_disable)
15332 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015333 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015334 encoder->base.crtc = NULL;
15335 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015336
15337 /* Inconsistent output/port/pipe state happens presumably due to
15338 * a bug in one of the get_hw_state functions. Or someplace else
15339 * in our code, like the register restore mess on resume. Clamp
15340 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015341 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015342 if (connector->encoder != encoder)
15343 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015344 connector->base.dpms = DRM_MODE_DPMS_OFF;
15345 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015346 }
15347 }
15348 /* Enabled encoders without active connectors will be fixed in
15349 * the crtc fixup. */
15350}
15351
Imre Deak04098752014-02-18 00:02:16 +020015352void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015353{
15354 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015355 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015356
Imre Deak04098752014-02-18 00:02:16 +020015357 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15358 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15359 i915_disable_vga(dev);
15360 }
15361}
15362
15363void i915_redisable_vga(struct drm_device *dev)
15364{
15365 struct drm_i915_private *dev_priv = dev->dev_private;
15366
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015367 /* This function can be called both from intel_modeset_setup_hw_state or
15368 * at a very early point in our resume sequence, where the power well
15369 * structures are not yet restored. Since this function is at a very
15370 * paranoid "someone might have enabled VGA while we were not looking"
15371 * level, just check if the power well is enabled instead of trying to
15372 * follow the "don't touch the power well if we don't need it" policy
15373 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015374 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015375 return;
15376
Imre Deak04098752014-02-18 00:02:16 +020015377 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015378}
15379
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015380static bool primary_get_hw_state(struct intel_crtc *crtc)
15381{
15382 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15383
15384 if (!crtc->active)
15385 return false;
15386
15387 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15388}
15389
Daniel Vetter30e984d2013-06-05 13:34:17 +020015390static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015391{
15392 struct drm_i915_private *dev_priv = dev->dev_private;
15393 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015394 struct intel_crtc *crtc;
15395 struct intel_encoder *encoder;
15396 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015397 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015398
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015399 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015400 struct drm_plane *primary = crtc->base.primary;
15401 struct intel_plane_state *plane_state;
15402
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015403 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015404 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015405
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015406 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015407
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015408 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015409 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015410
Matt Roper83d65732015-02-25 13:12:16 -080015411 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015412 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015413 crtc->base.enabled = crtc->active;
Maarten Lankhorstb8b7fad2015-06-12 11:15:41 +020015414 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015415
15416 plane_state = to_intel_plane_state(primary->state);
15417 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015418
15419 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15420 crtc->base.base.id,
15421 crtc->active ? "enabled" : "disabled");
15422 }
15423
Daniel Vetter53589012013-06-05 13:34:16 +020015424 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15425 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15426
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015427 pll->on = pll->get_hw_state(dev_priv, pll,
15428 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015429 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015430 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015431 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015432 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015433 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015434 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015435 }
Daniel Vetter53589012013-06-05 13:34:16 +020015436 }
Daniel Vetter53589012013-06-05 13:34:16 +020015437
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015438 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015439 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015440
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015441 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015442 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015443 }
15444
Damien Lespiaub2784e12014-08-05 11:29:37 +010015445 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015446 pipe = 0;
15447
15448 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015449 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15450 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015451 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015452 } else {
15453 encoder->base.crtc = NULL;
15454 }
15455
15456 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015457 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015458 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015459 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015460 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015461 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015462 }
15463
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015464 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015465 if (connector->get_hw_state(connector)) {
15466 connector->base.dpms = DRM_MODE_DPMS_ON;
15467 connector->encoder->connectors_active = true;
15468 connector->base.encoder = &connector->encoder->base;
15469 } else {
15470 connector->base.dpms = DRM_MODE_DPMS_OFF;
15471 connector->base.encoder = NULL;
15472 }
15473 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15474 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015475 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015476 connector->base.encoder ? "enabled" : "disabled");
15477 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015478}
15479
15480/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15481 * and i915 state tracking structures. */
15482void intel_modeset_setup_hw_state(struct drm_device *dev,
15483 bool force_restore)
15484{
15485 struct drm_i915_private *dev_priv = dev->dev_private;
15486 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015487 struct intel_crtc *crtc;
15488 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015489 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015490
15491 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015492
Jesse Barnesbabea612013-06-26 18:57:38 +030015493 /*
15494 * Now that we have the config, copy it to each CRTC struct
15495 * Note that this could go away if we move to using crtc_config
15496 * checking everywhere.
15497 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015498 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015499 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015500 intel_mode_from_pipe_config(&crtc->base.mode,
15501 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015502 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15503 crtc->base.base.id);
15504 drm_mode_debug_printmodeline(&crtc->base.mode);
15505 }
15506 }
15507
Daniel Vetter24929352012-07-02 20:28:59 +020015508 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015509 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015510 intel_sanitize_encoder(encoder);
15511 }
15512
Damien Lespiau055e3932014-08-18 13:49:10 +010015513 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015514 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15515 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015516 intel_dump_pipe_config(crtc, crtc->config,
15517 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015518 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015519
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015520 intel_modeset_update_connector_atomic_state(dev);
15521
Daniel Vetter35c95372013-07-17 06:55:04 +020015522 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15523 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15524
15525 if (!pll->on || pll->active)
15526 continue;
15527
15528 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15529
15530 pll->disable(dev_priv, pll);
15531 pll->on = false;
15532 }
15533
Pradeep Bhat30789992014-11-04 17:06:45 +000015534 if (IS_GEN9(dev))
15535 skl_wm_get_hw_state(dev);
15536 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015537 ilk_wm_get_hw_state(dev);
15538
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015539 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015540 i915_redisable_vga(dev);
15541
Daniel Vetterf30da182013-04-11 20:22:50 +020015542 /*
15543 * We need to use raw interfaces for restoring state to avoid
15544 * checking (bogus) intermediate states.
15545 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015546 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015547 struct drm_crtc *crtc =
15548 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015549
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015550 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015551 }
15552 } else {
15553 intel_modeset_update_staged_output_state(dev);
15554 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015555
15556 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015557}
15558
15559void intel_modeset_gem_init(struct drm_device *dev)
15560{
Jesse Barnes92122782014-10-09 12:57:42 -070015561 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015562 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015563 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015564 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015565
Imre Deakae484342014-03-31 15:10:44 +030015566 mutex_lock(&dev->struct_mutex);
15567 intel_init_gt_powersave(dev);
15568 mutex_unlock(&dev->struct_mutex);
15569
Jesse Barnes92122782014-10-09 12:57:42 -070015570 /*
15571 * There may be no VBT; and if the BIOS enabled SSC we can
15572 * just keep using it to avoid unnecessary flicker. Whereas if the
15573 * BIOS isn't using it, don't assume it will work even if the VBT
15574 * indicates as much.
15575 */
15576 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15577 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15578 DREF_SSC1_ENABLE);
15579
Chris Wilson1833b132012-05-09 11:56:28 +010015580 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015581
15582 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015583
15584 /*
15585 * Make sure any fbs we allocated at startup are properly
15586 * pinned & fenced. When we do the allocation it's too early
15587 * for this.
15588 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015589 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015590 obj = intel_fb_obj(c->primary->fb);
15591 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015592 continue;
15593
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015594 mutex_lock(&dev->struct_mutex);
15595 ret = intel_pin_and_fence_fb_obj(c->primary,
15596 c->primary->fb,
15597 c->primary->state,
15598 NULL);
15599 mutex_unlock(&dev->struct_mutex);
15600 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015601 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15602 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015603 drm_framebuffer_unreference(c->primary->fb);
15604 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015605 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015606 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015607 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015608 }
15609 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015610
15611 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015612}
15613
Imre Deak4932e2c2014-02-11 17:12:48 +020015614void intel_connector_unregister(struct intel_connector *intel_connector)
15615{
15616 struct drm_connector *connector = &intel_connector->base;
15617
15618 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015619 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015620}
15621
Jesse Barnes79e53942008-11-07 14:24:08 -080015622void intel_modeset_cleanup(struct drm_device *dev)
15623{
Jesse Barnes652c3932009-08-17 13:31:43 -070015624 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015625 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015626
Imre Deak2eb52522014-11-19 15:30:05 +020015627 intel_disable_gt_powersave(dev);
15628
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015629 intel_backlight_unregister(dev);
15630
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015631 /*
15632 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015633 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015634 * experience fancy races otherwise.
15635 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015636 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015637
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015638 /*
15639 * Due to the hpd irq storm handling the hotplug work can re-arm the
15640 * poll handlers. Hence disable polling after hpd handling is shut down.
15641 */
Keith Packardf87ea762010-10-03 19:36:26 -070015642 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015643
Jesse Barnes652c3932009-08-17 13:31:43 -070015644 mutex_lock(&dev->struct_mutex);
15645
Jesse Barnes723bfd72010-10-07 16:01:13 -070015646 intel_unregister_dsm_handler();
15647
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015648 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015649
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015650 mutex_unlock(&dev->struct_mutex);
15651
Chris Wilson1630fe72011-07-08 12:22:42 +010015652 /* flush any delayed tasks or pending work */
15653 flush_scheduled_work();
15654
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015655 /* destroy the backlight and sysfs files before encoders/connectors */
15656 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015657 struct intel_connector *intel_connector;
15658
15659 intel_connector = to_intel_connector(connector);
15660 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015661 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015662
Jesse Barnes79e53942008-11-07 14:24:08 -080015663 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015664
15665 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015666
15667 mutex_lock(&dev->struct_mutex);
15668 intel_cleanup_gt_powersave(dev);
15669 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015670}
15671
Dave Airlie28d52042009-09-21 14:33:58 +100015672/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015673 * Return which encoder is currently attached for connector.
15674 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015675struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015676{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015677 return &intel_attached_encoder(connector)->base;
15678}
Jesse Barnes79e53942008-11-07 14:24:08 -080015679
Chris Wilsondf0e9242010-09-09 16:20:55 +010015680void intel_connector_attach_encoder(struct intel_connector *connector,
15681 struct intel_encoder *encoder)
15682{
15683 connector->encoder = encoder;
15684 drm_mode_connector_attach_encoder(&connector->base,
15685 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015686}
Dave Airlie28d52042009-09-21 14:33:58 +100015687
15688/*
15689 * set vga decode state - true == enable VGA decode
15690 */
15691int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15692{
15693 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015694 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015695 u16 gmch_ctrl;
15696
Chris Wilson75fa0412014-02-07 18:37:02 -020015697 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15698 DRM_ERROR("failed to read control word\n");
15699 return -EIO;
15700 }
15701
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015702 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15703 return 0;
15704
Dave Airlie28d52042009-09-21 14:33:58 +100015705 if (state)
15706 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15707 else
15708 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015709
15710 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15711 DRM_ERROR("failed to write control word\n");
15712 return -EIO;
15713 }
15714
Dave Airlie28d52042009-09-21 14:33:58 +100015715 return 0;
15716}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015717
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015718struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015719
15720 u32 power_well_driver;
15721
Chris Wilson63b66e52013-08-08 15:12:06 +020015722 int num_transcoders;
15723
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015724 struct intel_cursor_error_state {
15725 u32 control;
15726 u32 position;
15727 u32 base;
15728 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015729 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015730
15731 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015732 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015733 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015734 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015735 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015736
15737 struct intel_plane_error_state {
15738 u32 control;
15739 u32 stride;
15740 u32 size;
15741 u32 pos;
15742 u32 addr;
15743 u32 surface;
15744 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015745 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015746
15747 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015748 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015749 enum transcoder cpu_transcoder;
15750
15751 u32 conf;
15752
15753 u32 htotal;
15754 u32 hblank;
15755 u32 hsync;
15756 u32 vtotal;
15757 u32 vblank;
15758 u32 vsync;
15759 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015760};
15761
15762struct intel_display_error_state *
15763intel_display_capture_error_state(struct drm_device *dev)
15764{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015765 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015766 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015767 int transcoders[] = {
15768 TRANSCODER_A,
15769 TRANSCODER_B,
15770 TRANSCODER_C,
15771 TRANSCODER_EDP,
15772 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015773 int i;
15774
Chris Wilson63b66e52013-08-08 15:12:06 +020015775 if (INTEL_INFO(dev)->num_pipes == 0)
15776 return NULL;
15777
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015778 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015779 if (error == NULL)
15780 return NULL;
15781
Imre Deak190be112013-11-25 17:15:31 +020015782 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015783 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15784
Damien Lespiau055e3932014-08-18 13:49:10 +010015785 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015786 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015787 __intel_display_power_is_enabled(dev_priv,
15788 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015789 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015790 continue;
15791
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015792 error->cursor[i].control = I915_READ(CURCNTR(i));
15793 error->cursor[i].position = I915_READ(CURPOS(i));
15794 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015795
15796 error->plane[i].control = I915_READ(DSPCNTR(i));
15797 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015798 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015799 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015800 error->plane[i].pos = I915_READ(DSPPOS(i));
15801 }
Paulo Zanonica291362013-03-06 20:03:14 -030015802 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15803 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015804 if (INTEL_INFO(dev)->gen >= 4) {
15805 error->plane[i].surface = I915_READ(DSPSURF(i));
15806 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15807 }
15808
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015809 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030015810
Sonika Jindal3abfce72014-07-21 15:23:43 +053015811 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030015812 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015813 }
15814
15815 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15816 if (HAS_DDI(dev_priv->dev))
15817 error->num_transcoders++; /* Account for eDP. */
15818
15819 for (i = 0; i < error->num_transcoders; i++) {
15820 enum transcoder cpu_transcoder = transcoders[i];
15821
Imre Deakddf9c532013-11-27 22:02:02 +020015822 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015823 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015824 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015825 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015826 continue;
15827
Chris Wilson63b66e52013-08-08 15:12:06 +020015828 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15829
15830 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15831 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15832 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15833 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15834 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15835 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15836 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015837 }
15838
15839 return error;
15840}
15841
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015842#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15843
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015844void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015845intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015846 struct drm_device *dev,
15847 struct intel_display_error_state *error)
15848{
Damien Lespiau055e3932014-08-18 13:49:10 +010015849 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015850 int i;
15851
Chris Wilson63b66e52013-08-08 15:12:06 +020015852 if (!error)
15853 return;
15854
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015855 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015856 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015857 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015858 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015859 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015860 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015861 err_printf(m, " Power: %s\n",
15862 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015863 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030015864 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015865
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015866 err_printf(m, "Plane [%d]:\n", i);
15867 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15868 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015869 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015870 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15871 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015872 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015873 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015874 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015875 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015876 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15877 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015878 }
15879
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015880 err_printf(m, "Cursor [%d]:\n", i);
15881 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15882 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15883 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015884 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015885
15886 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015887 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015888 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015889 err_printf(m, " Power: %s\n",
15890 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015891 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15892 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15893 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15894 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15895 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15896 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15897 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15898 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015899}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015900
15901void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15902{
15903 struct intel_crtc *crtc;
15904
15905 for_each_intel_crtc(dev, crtc) {
15906 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015907
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015908 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015909
15910 work = crtc->unpin_work;
15911
15912 if (work && work->event &&
15913 work->event->base.file_priv == file) {
15914 kfree(work->event);
15915 work->event = NULL;
15916 }
15917
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015918 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015919 }
15920}