blob: cd9a7545b0d3b65083c212e0d36329fdf5e04a2e [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)
1701 count += crtc->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) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001782 intel_num_dvo_pipes(dev) == 1) {
1783 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 /*
2011 * make the BPC in transcoder be consistent with
2012 * that in pipeconf reg.
2013 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002014 val &= ~PIPECONF_BPC_MASK;
2015 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002016 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002017
2018 val &= ~TRANS_INTERLACE_MASK;
2019 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002020 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002021 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002022 val |= TRANS_LEGACY_INTERLACED_ILK;
2023 else
2024 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002025 else
2026 val |= TRANS_PROGRESSIVE;
2027
Jesse Barnes040484a2011-01-03 12:14:26 -08002028 I915_WRITE(reg, val | TRANS_ENABLE);
2029 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002030 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002031}
2032
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002033static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002034 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002035{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002037
2038 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002039 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002041 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002042 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002043 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002044
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002045 /* Workaround: set timing override bit. */
2046 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002047 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002048 I915_WRITE(_TRANSA_CHICKEN2, val);
2049
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002050 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002051 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002052
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002053 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2054 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002055 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002056 else
2057 val |= TRANS_PROGRESSIVE;
2058
Daniel Vetterab9412b2013-05-03 11:49:46 +02002059 I915_WRITE(LPT_TRANSCONF, val);
2060 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002061 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002062}
2063
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002064static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2065 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002066{
Daniel Vetter23670b322012-11-01 09:15:30 +01002067 struct drm_device *dev = dev_priv->dev;
2068 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002069
2070 /* FDI relies on the transcoder */
2071 assert_fdi_tx_disabled(dev_priv, pipe);
2072 assert_fdi_rx_disabled(dev_priv, pipe);
2073
Jesse Barnes291906f2011-02-02 12:28:03 -08002074 /* Ports must be off as well */
2075 assert_pch_ports_disabled(dev_priv, pipe);
2076
Daniel Vetterab9412b2013-05-03 11:49:46 +02002077 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002078 val = I915_READ(reg);
2079 val &= ~TRANS_ENABLE;
2080 I915_WRITE(reg, val);
2081 /* wait for PCH transcoder off, transcoder state */
2082 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002083 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002084
2085 if (!HAS_PCH_IBX(dev)) {
2086 /* Workaround: Clear the timing override chicken bit again. */
2087 reg = TRANS_CHICKEN2(pipe);
2088 val = I915_READ(reg);
2089 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2090 I915_WRITE(reg, val);
2091 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002092}
2093
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002094static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002095{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002096 u32 val;
2097
Daniel Vetterab9412b2013-05-03 11:49:46 +02002098 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002099 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002100 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002101 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002102 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002103 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002104
2105 /* Workaround: clear timing override bit. */
2106 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002107 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002108 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002109}
2110
2111/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002112 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002113 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002114 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002115 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002116 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002117 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002118static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002119{
Paulo Zanoni03722642014-01-17 13:51:09 -02002120 struct drm_device *dev = crtc->base.dev;
2121 struct drm_i915_private *dev_priv = dev->dev_private;
2122 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002123 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2124 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002125 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002126 int reg;
2127 u32 val;
2128
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002129 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002130 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002131 assert_sprites_disabled(dev_priv, pipe);
2132
Paulo Zanoni681e5812012-12-06 11:12:38 -02002133 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002134 pch_transcoder = TRANSCODER_A;
2135 else
2136 pch_transcoder = pipe;
2137
Jesse Barnesb24e7172011-01-04 15:09:30 -08002138 /*
2139 * A pipe without a PLL won't actually be able to drive bits from
2140 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2141 * need the check.
2142 */
Imre Deak50360402015-01-16 00:55:16 -08002143 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002144 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002145 assert_dsi_pll_enabled(dev_priv);
2146 else
2147 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002148 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002149 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002150 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002151 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002152 assert_fdi_tx_pll_enabled(dev_priv,
2153 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002154 }
2155 /* FIXME: assert CPU port conditions for SNB+ */
2156 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002157
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002158 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002160 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002161 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2162 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002163 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002164 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002165
2166 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002167 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002168}
2169
2170/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002171 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002172 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002173 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002174 * Disable the pipe of @crtc, making sure that various hardware
2175 * specific requirements are met, if applicable, e.g. plane
2176 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002177 *
2178 * Will wait until the pipe has shut down before returning.
2179 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002180static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002181{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002182 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002183 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002184 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002185 int reg;
2186 u32 val;
2187
2188 /*
2189 * Make sure planes won't keep trying to pump pixels to us,
2190 * or we might hang the display.
2191 */
2192 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002193 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002194 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002195
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002196 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002197 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002198 if ((val & PIPECONF_ENABLE) == 0)
2199 return;
2200
Ville Syrjälä67adc642014-08-15 01:21:57 +03002201 /*
2202 * Double wide has implications for planes
2203 * so best keep it disabled when not needed.
2204 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002205 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002206 val &= ~PIPECONF_DOUBLE_WIDE;
2207
2208 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002209 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2210 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002211 val &= ~PIPECONF_ENABLE;
2212
2213 I915_WRITE(reg, val);
2214 if ((val & PIPECONF_ENABLE) == 0)
2215 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002216}
2217
2218/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002219 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002220 * @plane: plane to be enabled
2221 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002222 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002223 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002224 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002225static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2226 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002227{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002228 struct drm_device *dev = plane->dev;
2229 struct drm_i915_private *dev_priv = dev->dev_private;
2230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002231
2232 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002233 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002234 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002235
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002236 dev_priv->display.update_primary_plane(crtc, plane->fb,
2237 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002238}
2239
Chris Wilson693db182013-03-05 14:52:39 +00002240static bool need_vtd_wa(struct drm_device *dev)
2241{
2242#ifdef CONFIG_INTEL_IOMMU
2243 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2244 return true;
2245#endif
2246 return false;
2247}
2248
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002249unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002250intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2251 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002252{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002253 unsigned int tile_height;
2254 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002255
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002256 switch (fb_format_modifier) {
2257 case DRM_FORMAT_MOD_NONE:
2258 tile_height = 1;
2259 break;
2260 case I915_FORMAT_MOD_X_TILED:
2261 tile_height = IS_GEN2(dev) ? 16 : 8;
2262 break;
2263 case I915_FORMAT_MOD_Y_TILED:
2264 tile_height = 32;
2265 break;
2266 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002267 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2268 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002269 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002270 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002271 tile_height = 64;
2272 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002273 case 2:
2274 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002275 tile_height = 32;
2276 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002277 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002278 tile_height = 16;
2279 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002280 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002281 WARN_ONCE(1,
2282 "128-bit pixels are not supported for display!");
2283 tile_height = 16;
2284 break;
2285 }
2286 break;
2287 default:
2288 MISSING_CASE(fb_format_modifier);
2289 tile_height = 1;
2290 break;
2291 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002292
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002293 return tile_height;
2294}
2295
2296unsigned int
2297intel_fb_align_height(struct drm_device *dev, unsigned int height,
2298 uint32_t pixel_format, uint64_t fb_format_modifier)
2299{
2300 return ALIGN(height, intel_tile_height(dev, pixel_format,
2301 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002302}
2303
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002304static int
2305intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2306 const struct drm_plane_state *plane_state)
2307{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002308 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002309
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002310 *view = i915_ggtt_view_normal;
2311
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002312 if (!plane_state)
2313 return 0;
2314
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002315 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002316 return 0;
2317
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002318 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002319
2320 info->height = fb->height;
2321 info->pixel_format = fb->pixel_format;
2322 info->pitch = fb->pitches[0];
2323 info->fb_modifier = fb->modifier[0];
2324
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002325 return 0;
2326}
2327
Chris Wilson127bd2a2010-07-23 23:32:05 +01002328int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002329intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2330 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002331 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002332 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002333{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002334 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002335 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002336 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002337 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002338 u32 alignment;
2339 int ret;
2340
Matt Roperebcdd392014-07-09 16:22:11 -07002341 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2342
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002343 switch (fb->modifier[0]) {
2344 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002345 if (INTEL_INFO(dev)->gen >= 9)
2346 alignment = 256 * 1024;
2347 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002348 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002349 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002350 alignment = 4 * 1024;
2351 else
2352 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002353 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002354 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002355 if (INTEL_INFO(dev)->gen >= 9)
2356 alignment = 256 * 1024;
2357 else {
2358 /* pin() will align the object as required by fence */
2359 alignment = 0;
2360 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002361 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002362 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002363 case I915_FORMAT_MOD_Yf_TILED:
2364 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2365 "Y tiling bo slipped through, driver bug!\n"))
2366 return -EINVAL;
2367 alignment = 1 * 1024 * 1024;
2368 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002369 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002370 MISSING_CASE(fb->modifier[0]);
2371 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002372 }
2373
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002374 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2375 if (ret)
2376 return ret;
2377
Chris Wilson693db182013-03-05 14:52:39 +00002378 /* Note that the w/a also requires 64 PTE of padding following the
2379 * bo. We currently fill all unused PTE with the shadow page and so
2380 * we should always have valid PTE following the scanout preventing
2381 * the VT-d warning.
2382 */
2383 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2384 alignment = 256 * 1024;
2385
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002386 /*
2387 * Global gtt pte registers are special registers which actually forward
2388 * writes to a chunk of system memory. Which means that there is no risk
2389 * that the register values disappear as soon as we call
2390 * intel_runtime_pm_put(), so it is correct to wrap only the
2391 * pin/unpin/fence and not more.
2392 */
2393 intel_runtime_pm_get(dev_priv);
2394
Chris Wilsonce453d82011-02-21 14:43:56 +00002395 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002396 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002397 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002398 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002399 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002400
2401 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2402 * fence, whereas 965+ only requires a fence if using
2403 * framebuffer compression. For simplicity, we always install
2404 * a fence as the cost is not that onerous.
2405 */
Chris Wilson06d98132012-04-17 15:31:24 +01002406 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002407 if (ret)
2408 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002409
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002410 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002411
Chris Wilsonce453d82011-02-21 14:43:56 +00002412 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002413 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002414 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002415
2416err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002417 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002418err_interruptible:
2419 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002420 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002421 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002422}
2423
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002424static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2425 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002426{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002427 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002428 struct i915_ggtt_view view;
2429 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002430
Matt Roperebcdd392014-07-09 16:22:11 -07002431 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2432
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002433 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2434 WARN_ONCE(ret, "Couldn't get view from plane state!");
2435
Chris Wilson1690e1e2011-12-14 13:57:08 +01002436 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002437 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002438}
2439
Daniel Vetterc2c75132012-07-05 12:17:30 +02002440/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2441 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002442unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2443 unsigned int tiling_mode,
2444 unsigned int cpp,
2445 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002446{
Chris Wilsonbc752862013-02-21 20:04:31 +00002447 if (tiling_mode != I915_TILING_NONE) {
2448 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002449
Chris Wilsonbc752862013-02-21 20:04:31 +00002450 tile_rows = *y / 8;
2451 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002452
Chris Wilsonbc752862013-02-21 20:04:31 +00002453 tiles = *x / (512/cpp);
2454 *x %= 512/cpp;
2455
2456 return tile_rows * pitch * 8 + tiles * 4096;
2457 } else {
2458 unsigned int offset;
2459
2460 offset = *y * pitch + *x * cpp;
2461 *y = 0;
2462 *x = (offset & 4095) / cpp;
2463 return offset & -4096;
2464 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002465}
2466
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002467static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002468{
2469 switch (format) {
2470 case DISPPLANE_8BPP:
2471 return DRM_FORMAT_C8;
2472 case DISPPLANE_BGRX555:
2473 return DRM_FORMAT_XRGB1555;
2474 case DISPPLANE_BGRX565:
2475 return DRM_FORMAT_RGB565;
2476 default:
2477 case DISPPLANE_BGRX888:
2478 return DRM_FORMAT_XRGB8888;
2479 case DISPPLANE_RGBX888:
2480 return DRM_FORMAT_XBGR8888;
2481 case DISPPLANE_BGRX101010:
2482 return DRM_FORMAT_XRGB2101010;
2483 case DISPPLANE_RGBX101010:
2484 return DRM_FORMAT_XBGR2101010;
2485 }
2486}
2487
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002488static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2489{
2490 switch (format) {
2491 case PLANE_CTL_FORMAT_RGB_565:
2492 return DRM_FORMAT_RGB565;
2493 default:
2494 case PLANE_CTL_FORMAT_XRGB_8888:
2495 if (rgb_order) {
2496 if (alpha)
2497 return DRM_FORMAT_ABGR8888;
2498 else
2499 return DRM_FORMAT_XBGR8888;
2500 } else {
2501 if (alpha)
2502 return DRM_FORMAT_ARGB8888;
2503 else
2504 return DRM_FORMAT_XRGB8888;
2505 }
2506 case PLANE_CTL_FORMAT_XRGB_2101010:
2507 if (rgb_order)
2508 return DRM_FORMAT_XBGR2101010;
2509 else
2510 return DRM_FORMAT_XRGB2101010;
2511 }
2512}
2513
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002514static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002515intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2516 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002517{
2518 struct drm_device *dev = crtc->base.dev;
2519 struct drm_i915_gem_object *obj = NULL;
2520 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002521 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002522 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2523 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2524 PAGE_SIZE);
2525
2526 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002527
Chris Wilsonff2652e2014-03-10 08:07:02 +00002528 if (plane_config->size == 0)
2529 return false;
2530
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002531 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2532 base_aligned,
2533 base_aligned,
2534 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002535 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002536 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002537
Damien Lespiau49af4492015-01-20 12:51:44 +00002538 obj->tiling_mode = plane_config->tiling;
2539 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002540 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002541
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002542 mode_cmd.pixel_format = fb->pixel_format;
2543 mode_cmd.width = fb->width;
2544 mode_cmd.height = fb->height;
2545 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002546 mode_cmd.modifier[0] = fb->modifier[0];
2547 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002548
2549 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002550 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002551 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002552 DRM_DEBUG_KMS("intel fb init failed\n");
2553 goto out_unref_obj;
2554 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002555 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002556
Daniel Vetterf6936e22015-03-26 12:17:05 +01002557 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002559
2560out_unref_obj:
2561 drm_gem_object_unreference(&obj->base);
2562 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002563 return false;
2564}
2565
Matt Roperafd65eb2015-02-03 13:10:04 -08002566/* Update plane->state->fb to match plane->fb after driver-internal updates */
2567static void
2568update_state_fb(struct drm_plane *plane)
2569{
2570 if (plane->fb == plane->state->fb)
2571 return;
2572
2573 if (plane->state->fb)
2574 drm_framebuffer_unreference(plane->state->fb);
2575 plane->state->fb = plane->fb;
2576 if (plane->state->fb)
2577 drm_framebuffer_reference(plane->state->fb);
2578}
2579
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002580static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002581intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2582 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002583{
2584 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002585 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002586 struct drm_crtc *c;
2587 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002588 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002589 struct drm_plane *primary = intel_crtc->base.primary;
2590 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002591
Damien Lespiau2d140302015-02-05 17:22:18 +00002592 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002593 return;
2594
Daniel Vetterf6936e22015-03-26 12:17:05 +01002595 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002596 fb = &plane_config->fb->base;
2597 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002598 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002599
Damien Lespiau2d140302015-02-05 17:22:18 +00002600 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002601
2602 /*
2603 * Failed to alloc the obj, check to see if we should share
2604 * an fb with another CRTC instead
2605 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002606 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002607 i = to_intel_crtc(c);
2608
2609 if (c == &intel_crtc->base)
2610 continue;
2611
Matt Roper2ff8fde2014-07-08 07:50:07 -07002612 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002613 continue;
2614
Daniel Vetter88595ac2015-03-26 12:42:24 +01002615 fb = c->primary->fb;
2616 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002617 continue;
2618
Daniel Vetter88595ac2015-03-26 12:42:24 +01002619 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002620 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002621 drm_framebuffer_reference(fb);
2622 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002623 }
2624 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002625
2626 return;
2627
2628valid_fb:
2629 obj = intel_fb_obj(fb);
2630 if (obj->tiling_mode != I915_TILING_NONE)
2631 dev_priv->preserve_bios_swizzle = true;
2632
2633 primary->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002634 primary->crtc = primary->state->crtc = &intel_crtc->base;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002635 update_state_fb(primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002636 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Daniel Vetter88595ac2015-03-26 12:42:24 +01002637 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002638}
2639
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002640static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2641 struct drm_framebuffer *fb,
2642 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002643{
2644 struct drm_device *dev = crtc->dev;
2645 struct drm_i915_private *dev_priv = dev->dev_private;
2646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002647 struct drm_plane *primary = crtc->primary;
2648 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002649 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002650 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002651 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002652 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002653 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302654 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002655
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002656 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002657 I915_WRITE(reg, 0);
2658 if (INTEL_INFO(dev)->gen >= 4)
2659 I915_WRITE(DSPSURF(plane), 0);
2660 else
2661 I915_WRITE(DSPADDR(plane), 0);
2662 POSTING_READ(reg);
2663 return;
2664 }
2665
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002666 obj = intel_fb_obj(fb);
2667 if (WARN_ON(obj == NULL))
2668 return;
2669
2670 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2671
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002672 dspcntr = DISPPLANE_GAMMA_ENABLE;
2673
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002674 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002675
2676 if (INTEL_INFO(dev)->gen < 4) {
2677 if (intel_crtc->pipe == PIPE_B)
2678 dspcntr |= DISPPLANE_SEL_PIPE_B;
2679
2680 /* pipesrc and dspsize control the size that is scaled from,
2681 * which should always be the user's requested size.
2682 */
2683 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002684 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2685 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002686 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002687 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2688 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002689 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2690 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002691 I915_WRITE(PRIMPOS(plane), 0);
2692 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002693 }
2694
Ville Syrjälä57779d02012-10-31 17:50:14 +02002695 switch (fb->pixel_format) {
2696 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002697 dspcntr |= DISPPLANE_8BPP;
2698 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002699 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002700 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002701 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002702 case DRM_FORMAT_RGB565:
2703 dspcntr |= DISPPLANE_BGRX565;
2704 break;
2705 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002706 dspcntr |= DISPPLANE_BGRX888;
2707 break;
2708 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002709 dspcntr |= DISPPLANE_RGBX888;
2710 break;
2711 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002712 dspcntr |= DISPPLANE_BGRX101010;
2713 break;
2714 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002715 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002716 break;
2717 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002718 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002719 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002720
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002721 if (INTEL_INFO(dev)->gen >= 4 &&
2722 obj->tiling_mode != I915_TILING_NONE)
2723 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002724
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002725 if (IS_G4X(dev))
2726 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2727
Ville Syrjäläb98971272014-08-27 16:51:22 +03002728 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002729
Daniel Vetterc2c75132012-07-05 12:17:30 +02002730 if (INTEL_INFO(dev)->gen >= 4) {
2731 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002732 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002733 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002734 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002735 linear_offset -= intel_crtc->dspaddr_offset;
2736 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002737 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002738 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002739
Matt Roper8e7d6882015-01-21 16:35:41 -08002740 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302741 dspcntr |= DISPPLANE_ROTATE_180;
2742
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002743 x += (intel_crtc->config->pipe_src_w - 1);
2744 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302745
2746 /* Finding the last pixel of the last line of the display
2747 data and adding to linear_offset*/
2748 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002749 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2750 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302751 }
2752
2753 I915_WRITE(reg, dspcntr);
2754
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002755 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002756 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002757 I915_WRITE(DSPSURF(plane),
2758 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002759 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002760 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002761 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002762 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002763 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002764}
2765
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002766static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2767 struct drm_framebuffer *fb,
2768 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002769{
2770 struct drm_device *dev = crtc->dev;
2771 struct drm_i915_private *dev_priv = dev->dev_private;
2772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002773 struct drm_plane *primary = crtc->primary;
2774 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002775 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002777 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002779 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302780 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002782 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002783 I915_WRITE(reg, 0);
2784 I915_WRITE(DSPSURF(plane), 0);
2785 POSTING_READ(reg);
2786 return;
2787 }
2788
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002789 obj = intel_fb_obj(fb);
2790 if (WARN_ON(obj == NULL))
2791 return;
2792
2793 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2794
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002795 dspcntr = DISPPLANE_GAMMA_ENABLE;
2796
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002797 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002798
2799 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2800 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2801
Ville Syrjälä57779d02012-10-31 17:50:14 +02002802 switch (fb->pixel_format) {
2803 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002804 dspcntr |= DISPPLANE_8BPP;
2805 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002806 case DRM_FORMAT_RGB565:
2807 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002808 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002809 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002810 dspcntr |= DISPPLANE_BGRX888;
2811 break;
2812 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002813 dspcntr |= DISPPLANE_RGBX888;
2814 break;
2815 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002816 dspcntr |= DISPPLANE_BGRX101010;
2817 break;
2818 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002819 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820 break;
2821 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002822 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002823 }
2824
2825 if (obj->tiling_mode != I915_TILING_NONE)
2826 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002827
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002828 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002829 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002830
Ville Syrjäläb98971272014-08-27 16:51:22 +03002831 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002832 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002833 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002834 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002835 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002836 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002837 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302838 dspcntr |= DISPPLANE_ROTATE_180;
2839
2840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002841 x += (intel_crtc->config->pipe_src_w - 1);
2842 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302843
2844 /* Finding the last pixel of the last line of the display
2845 data and adding to linear_offset*/
2846 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002847 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2848 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302849 }
2850 }
2851
2852 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002853
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002854 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002855 I915_WRITE(DSPSURF(plane),
2856 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002857 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002858 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2859 } else {
2860 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2861 I915_WRITE(DSPLINOFF(plane), linear_offset);
2862 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002863 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002864}
2865
Damien Lespiaub3218032015-02-27 11:15:18 +00002866u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2867 uint32_t pixel_format)
2868{
2869 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2870
2871 /*
2872 * The stride is either expressed as a multiple of 64 bytes
2873 * chunks for linear buffers or in number of tiles for tiled
2874 * buffers.
2875 */
2876 switch (fb_modifier) {
2877 case DRM_FORMAT_MOD_NONE:
2878 return 64;
2879 case I915_FORMAT_MOD_X_TILED:
2880 if (INTEL_INFO(dev)->gen == 2)
2881 return 128;
2882 return 512;
2883 case I915_FORMAT_MOD_Y_TILED:
2884 /* No need to check for old gens and Y tiling since this is
2885 * about the display engine and those will be blocked before
2886 * we get here.
2887 */
2888 return 128;
2889 case I915_FORMAT_MOD_Yf_TILED:
2890 if (bits_per_pixel == 8)
2891 return 64;
2892 else
2893 return 128;
2894 default:
2895 MISSING_CASE(fb_modifier);
2896 return 64;
2897 }
2898}
2899
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002900unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2901 struct drm_i915_gem_object *obj)
2902{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002903 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002904
2905 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002906 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002907
2908 return i915_gem_obj_ggtt_offset_view(obj, view);
2909}
2910
Chandra Kondurua1b22782015-04-07 15:28:45 -07002911/*
2912 * This function detaches (aka. unbinds) unused scalers in hardware
2913 */
2914void skl_detach_scalers(struct intel_crtc *intel_crtc)
2915{
2916 struct drm_device *dev;
2917 struct drm_i915_private *dev_priv;
2918 struct intel_crtc_scaler_state *scaler_state;
2919 int i;
2920
2921 if (!intel_crtc || !intel_crtc->config)
2922 return;
2923
2924 dev = intel_crtc->base.dev;
2925 dev_priv = dev->dev_private;
2926 scaler_state = &intel_crtc->config->scaler_state;
2927
2928 /* loop through and disable scalers that aren't in use */
2929 for (i = 0; i < intel_crtc->num_scalers; i++) {
2930 if (!scaler_state->scalers[i].in_use) {
2931 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2932 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2933 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2934 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2935 intel_crtc->base.base.id, intel_crtc->pipe, i);
2936 }
2937 }
2938}
2939
Chandra Konduru6156a452015-04-27 13:48:39 -07002940u32 skl_plane_ctl_format(uint32_t pixel_format)
2941{
Chandra Konduru6156a452015-04-27 13:48:39 -07002942 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002943 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002944 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002945 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002946 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002947 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002948 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002949 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002950 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002951 /*
2952 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2953 * to be already pre-multiplied. We need to add a knob (or a different
2954 * DRM_FORMAT) for user-space to configure that.
2955 */
2956 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002963 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002964 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002965 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002967 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002969 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002971 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002975 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002977
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979}
2980
2981u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2982{
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 switch (fb_modifier) {
2984 case DRM_FORMAT_MOD_NONE:
2985 break;
2986 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002987 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002988 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002989 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002990 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002991 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002992 default:
2993 MISSING_CASE(fb_modifier);
2994 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002995
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002996 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002997}
2998
2999u32 skl_plane_ctl_rotation(unsigned int rotation)
3000{
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 switch (rotation) {
3002 case BIT(DRM_ROTATE_0):
3003 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303004 /*
3005 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3006 * while i915 HW rotation is clockwise, thats why this swapping.
3007 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003008 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303009 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003010 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003011 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003012 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303013 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003014 default:
3015 MISSING_CASE(rotation);
3016 }
3017
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003018 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003019}
3020
Damien Lespiau70d21f02013-07-03 21:06:04 +01003021static void skylake_update_primary_plane(struct drm_crtc *crtc,
3022 struct drm_framebuffer *fb,
3023 int x, int y)
3024{
3025 struct drm_device *dev = crtc->dev;
3026 struct drm_i915_private *dev_priv = dev->dev_private;
3027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003028 struct drm_plane *plane = crtc->primary;
3029 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003030 struct drm_i915_gem_object *obj;
3031 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303032 u32 plane_ctl, stride_div, stride;
3033 u32 tile_height, plane_offset, plane_size;
3034 unsigned int rotation;
3035 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003036 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003037 struct intel_crtc_state *crtc_state = intel_crtc->config;
3038 struct intel_plane_state *plane_state;
3039 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3040 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3041 int scaler_id = -1;
3042
Chandra Konduru6156a452015-04-27 13:48:39 -07003043 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003044
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003045 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003046 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3047 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3048 POSTING_READ(PLANE_CTL(pipe, 0));
3049 return;
3050 }
3051
3052 plane_ctl = PLANE_CTL_ENABLE |
3053 PLANE_CTL_PIPE_GAMMA_ENABLE |
3054 PLANE_CTL_PIPE_CSC_ENABLE;
3055
Chandra Konduru6156a452015-04-27 13:48:39 -07003056 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3057 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003058 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303059
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303060 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003061 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003062
Damien Lespiaub3218032015-02-27 11:15:18 +00003063 obj = intel_fb_obj(fb);
3064 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3065 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303066 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3067
Chandra Konduru6156a452015-04-27 13:48:39 -07003068 /*
3069 * FIXME: intel_plane_state->src, dst aren't set when transitional
3070 * update_plane helpers are called from legacy paths.
3071 * Once full atomic crtc is available, below check can be avoided.
3072 */
3073 if (drm_rect_width(&plane_state->src)) {
3074 scaler_id = plane_state->scaler_id;
3075 src_x = plane_state->src.x1 >> 16;
3076 src_y = plane_state->src.y1 >> 16;
3077 src_w = drm_rect_width(&plane_state->src) >> 16;
3078 src_h = drm_rect_height(&plane_state->src) >> 16;
3079 dst_x = plane_state->dst.x1;
3080 dst_y = plane_state->dst.y1;
3081 dst_w = drm_rect_width(&plane_state->dst);
3082 dst_h = drm_rect_height(&plane_state->dst);
3083
3084 WARN_ON(x != src_x || y != src_y);
3085 } else {
3086 src_w = intel_crtc->config->pipe_src_w;
3087 src_h = intel_crtc->config->pipe_src_h;
3088 }
3089
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303090 if (intel_rotation_90_or_270(rotation)) {
3091 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003092 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303093 fb->modifier[0]);
3094 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003095 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303096 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003097 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303098 } else {
3099 stride = fb->pitches[0] / stride_div;
3100 x_offset = x;
3101 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003102 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303103 }
3104 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003105
Damien Lespiau70d21f02013-07-03 21:06:04 +01003106 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303107 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3108 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3109 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003110
3111 if (scaler_id >= 0) {
3112 uint32_t ps_ctrl = 0;
3113
3114 WARN_ON(!dst_w || !dst_h);
3115 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3116 crtc_state->scaler_state.scalers[scaler_id].mode;
3117 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3118 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3119 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3120 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3121 I915_WRITE(PLANE_POS(pipe, 0), 0);
3122 } else {
3123 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3124 }
3125
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003126 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003127
3128 POSTING_READ(PLANE_SURF(pipe, 0));
3129}
3130
Jesse Barnes17638cd2011-06-24 12:19:23 -07003131/* Assume fb object is pinned & idle & fenced and just update base pointers */
3132static int
3133intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3134 int x, int y, enum mode_set_atomic state)
3135{
3136 struct drm_device *dev = crtc->dev;
3137 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003138
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003139 if (dev_priv->display.disable_fbc)
3140 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003141
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003142 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3143
3144 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003145}
3146
Ville Syrjälä75147472014-11-24 18:28:11 +02003147static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003148{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003149 struct drm_crtc *crtc;
3150
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003151 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3153 enum plane plane = intel_crtc->plane;
3154
3155 intel_prepare_page_flip(dev, plane);
3156 intel_finish_page_flip_plane(dev, plane);
3157 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003158}
3159
3160static void intel_update_primary_planes(struct drm_device *dev)
3161{
3162 struct drm_i915_private *dev_priv = dev->dev_private;
3163 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003164
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003165 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3167
Rob Clark51fd3712013-11-19 12:10:12 -05003168 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003169 /*
3170 * FIXME: Once we have proper support for primary planes (and
3171 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003172 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003173 */
Matt Roperf4510a22014-04-01 15:22:40 -07003174 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003175 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003176 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003177 crtc->x,
3178 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003179 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003180 }
3181}
3182
Ville Syrjälä75147472014-11-24 18:28:11 +02003183void intel_prepare_reset(struct drm_device *dev)
3184{
3185 /* no reset support for gen2 */
3186 if (IS_GEN2(dev))
3187 return;
3188
3189 /* reset doesn't touch the display */
3190 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3191 return;
3192
3193 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003194 /*
3195 * Disabling the crtcs gracefully seems nicer. Also the
3196 * g33 docs say we should at least disable all the planes.
3197 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003198 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003199}
3200
3201void intel_finish_reset(struct drm_device *dev)
3202{
3203 struct drm_i915_private *dev_priv = to_i915(dev);
3204
3205 /*
3206 * Flips in the rings will be nuked by the reset,
3207 * so complete all pending flips so that user space
3208 * will get its events and not get stuck.
3209 */
3210 intel_complete_page_flips(dev);
3211
3212 /* no reset support for gen2 */
3213 if (IS_GEN2(dev))
3214 return;
3215
3216 /* reset doesn't touch the display */
3217 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3218 /*
3219 * Flips in the rings have been nuked by the reset,
3220 * so update the base address of all primary
3221 * planes to the the last fb to make sure we're
3222 * showing the correct fb after a reset.
3223 */
3224 intel_update_primary_planes(dev);
3225 return;
3226 }
3227
3228 /*
3229 * The display has been reset as well,
3230 * so need a full re-initialization.
3231 */
3232 intel_runtime_pm_disable_interrupts(dev_priv);
3233 intel_runtime_pm_enable_interrupts(dev_priv);
3234
3235 intel_modeset_init_hw(dev);
3236
3237 spin_lock_irq(&dev_priv->irq_lock);
3238 if (dev_priv->display.hpd_irq_setup)
3239 dev_priv->display.hpd_irq_setup(dev);
3240 spin_unlock_irq(&dev_priv->irq_lock);
3241
3242 intel_modeset_setup_hw_state(dev, true);
3243
3244 intel_hpd_init(dev_priv);
3245
3246 drm_modeset_unlock_all(dev);
3247}
3248
Chris Wilson2e2f3512015-04-27 13:41:14 +01003249static void
Chris Wilson14667a42012-04-03 17:58:35 +01003250intel_finish_fb(struct drm_framebuffer *old_fb)
3251{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003252 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003253 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003254 bool was_interruptible = dev_priv->mm.interruptible;
3255 int ret;
3256
Chris Wilson14667a42012-04-03 17:58:35 +01003257 /* Big Hammer, we also need to ensure that any pending
3258 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3259 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003260 * framebuffer. Note that we rely on userspace rendering
3261 * into the buffer attached to the pipe they are waiting
3262 * on. If not, userspace generates a GPU hang with IPEHR
3263 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003264 *
3265 * This should only fail upon a hung GPU, in which case we
3266 * can safely continue.
3267 */
3268 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003269 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003270 dev_priv->mm.interruptible = was_interruptible;
3271
Chris Wilson2e2f3512015-04-27 13:41:14 +01003272 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003273}
3274
Chris Wilson7d5e3792014-03-04 13:15:08 +00003275static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3276{
3277 struct drm_device *dev = crtc->dev;
3278 struct drm_i915_private *dev_priv = dev->dev_private;
3279 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003280 bool pending;
3281
3282 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3283 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3284 return false;
3285
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003286 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003287 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003288 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003289
3290 return pending;
3291}
3292
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003293static void intel_update_pipe_size(struct intel_crtc *crtc)
3294{
3295 struct drm_device *dev = crtc->base.dev;
3296 struct drm_i915_private *dev_priv = dev->dev_private;
3297 const struct drm_display_mode *adjusted_mode;
3298
3299 if (!i915.fastboot)
3300 return;
3301
3302 /*
3303 * Update pipe size and adjust fitter if needed: the reason for this is
3304 * that in compute_mode_changes we check the native mode (not the pfit
3305 * mode) to see if we can flip rather than do a full mode set. In the
3306 * fastboot case, we'll flip, but if we don't update the pipesrc and
3307 * pfit state, we'll end up with a big fb scanned out into the wrong
3308 * sized surface.
3309 *
3310 * To fix this properly, we need to hoist the checks up into
3311 * compute_mode_changes (or above), check the actual pfit state and
3312 * whether the platform allows pfit disable with pipe active, and only
3313 * then update the pipesrc and pfit state, even on the flip path.
3314 */
3315
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003316 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003317
3318 I915_WRITE(PIPESRC(crtc->pipe),
3319 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3320 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003321 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003322 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3323 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003324 I915_WRITE(PF_CTL(crtc->pipe), 0);
3325 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3326 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3327 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003328 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3329 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003330}
3331
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003332static void intel_fdi_normal_train(struct drm_crtc *crtc)
3333{
3334 struct drm_device *dev = crtc->dev;
3335 struct drm_i915_private *dev_priv = dev->dev_private;
3336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3337 int pipe = intel_crtc->pipe;
3338 u32 reg, temp;
3339
3340 /* enable normal train */
3341 reg = FDI_TX_CTL(pipe);
3342 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003343 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003344 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3345 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003346 } else {
3347 temp &= ~FDI_LINK_TRAIN_NONE;
3348 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003349 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003350 I915_WRITE(reg, temp);
3351
3352 reg = FDI_RX_CTL(pipe);
3353 temp = I915_READ(reg);
3354 if (HAS_PCH_CPT(dev)) {
3355 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3356 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3357 } else {
3358 temp &= ~FDI_LINK_TRAIN_NONE;
3359 temp |= FDI_LINK_TRAIN_NONE;
3360 }
3361 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3362
3363 /* wait one idle pattern time */
3364 POSTING_READ(reg);
3365 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003366
3367 /* IVB wants error correction enabled */
3368 if (IS_IVYBRIDGE(dev))
3369 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3370 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003371}
3372
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003373/* The FDI link training functions for ILK/Ibexpeak. */
3374static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3375{
3376 struct drm_device *dev = crtc->dev;
3377 struct drm_i915_private *dev_priv = dev->dev_private;
3378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3379 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003382 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003383 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003384
Adam Jacksone1a44742010-06-25 15:32:14 -04003385 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3386 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003387 reg = FDI_RX_IMR(pipe);
3388 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003389 temp &= ~FDI_RX_SYMBOL_LOCK;
3390 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003391 I915_WRITE(reg, temp);
3392 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003393 udelay(150);
3394
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003395 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 reg = FDI_TX_CTL(pipe);
3397 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003398 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003399 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003400 temp &= ~FDI_LINK_TRAIN_NONE;
3401 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 reg = FDI_RX_CTL(pipe);
3405 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406 temp &= ~FDI_LINK_TRAIN_NONE;
3407 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003408 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3409
3410 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003411 udelay(150);
3412
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003413 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003414 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3415 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3416 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003417
Chris Wilson5eddb702010-09-11 13:48:45 +01003418 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003419 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003421 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3422
3423 if ((temp & FDI_RX_BIT_LOCK)) {
3424 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003426 break;
3427 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003429 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003431
3432 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003433 reg = FDI_TX_CTL(pipe);
3434 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435 temp &= ~FDI_LINK_TRAIN_NONE;
3436 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438
Chris Wilson5eddb702010-09-11 13:48:45 +01003439 reg = FDI_RX_CTL(pipe);
3440 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441 temp &= ~FDI_LINK_TRAIN_NONE;
3442 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003443 I915_WRITE(reg, temp);
3444
3445 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003446 udelay(150);
3447
Chris Wilson5eddb702010-09-11 13:48:45 +01003448 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003449 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3452
3453 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003454 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003455 DRM_DEBUG_KMS("FDI train 2 done.\n");
3456 break;
3457 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003458 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003459 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003461
3462 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003463
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464}
3465
Akshay Joshi0206e352011-08-16 15:34:10 -04003466static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3468 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3469 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3470 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3471};
3472
3473/* The FDI link training functions for SNB/Cougarpoint. */
3474static void gen6_fdi_link_train(struct drm_crtc *crtc)
3475{
3476 struct drm_device *dev = crtc->dev;
3477 struct drm_i915_private *dev_priv = dev->dev_private;
3478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3479 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003480 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003481
Adam Jacksone1a44742010-06-25 15:32:14 -04003482 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3483 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 reg = FDI_RX_IMR(pipe);
3485 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003486 temp &= ~FDI_RX_SYMBOL_LOCK;
3487 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 I915_WRITE(reg, temp);
3489
3490 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003491 udelay(150);
3492
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 reg = FDI_TX_CTL(pipe);
3495 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003496 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003497 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003498 temp &= ~FDI_LINK_TRAIN_NONE;
3499 temp |= FDI_LINK_TRAIN_PATTERN_1;
3500 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3501 /* SNB-B */
3502 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003504
Daniel Vetterd74cf322012-10-26 10:58:13 +02003505 I915_WRITE(FDI_RX_MISC(pipe),
3506 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3507
Chris Wilson5eddb702010-09-11 13:48:45 +01003508 reg = FDI_RX_CTL(pipe);
3509 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003510 if (HAS_PCH_CPT(dev)) {
3511 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3512 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3513 } else {
3514 temp &= ~FDI_LINK_TRAIN_NONE;
3515 temp |= FDI_LINK_TRAIN_PATTERN_1;
3516 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3518
3519 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003520 udelay(150);
3521
Akshay Joshi0206e352011-08-16 15:34:10 -04003522 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003523 reg = FDI_TX_CTL(pipe);
3524 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3526 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003527 I915_WRITE(reg, temp);
3528
3529 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530 udelay(500);
3531
Sean Paulfa37d392012-03-02 12:53:39 -05003532 for (retry = 0; retry < 5; retry++) {
3533 reg = FDI_RX_IIR(pipe);
3534 temp = I915_READ(reg);
3535 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3536 if (temp & FDI_RX_BIT_LOCK) {
3537 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3538 DRM_DEBUG_KMS("FDI train 1 done.\n");
3539 break;
3540 }
3541 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542 }
Sean Paulfa37d392012-03-02 12:53:39 -05003543 if (retry < 5)
3544 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003545 }
3546 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003547 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548
3549 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003550 reg = FDI_TX_CTL(pipe);
3551 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003552 temp &= ~FDI_LINK_TRAIN_NONE;
3553 temp |= FDI_LINK_TRAIN_PATTERN_2;
3554 if (IS_GEN6(dev)) {
3555 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3556 /* SNB-B */
3557 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3558 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003559 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003560
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 reg = FDI_RX_CTL(pipe);
3562 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003563 if (HAS_PCH_CPT(dev)) {
3564 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3565 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3566 } else {
3567 temp &= ~FDI_LINK_TRAIN_NONE;
3568 temp |= FDI_LINK_TRAIN_PATTERN_2;
3569 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003570 I915_WRITE(reg, temp);
3571
3572 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003573 udelay(150);
3574
Akshay Joshi0206e352011-08-16 15:34:10 -04003575 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003576 reg = FDI_TX_CTL(pipe);
3577 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003578 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3579 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003580 I915_WRITE(reg, temp);
3581
3582 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003583 udelay(500);
3584
Sean Paulfa37d392012-03-02 12:53:39 -05003585 for (retry = 0; retry < 5; retry++) {
3586 reg = FDI_RX_IIR(pipe);
3587 temp = I915_READ(reg);
3588 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3589 if (temp & FDI_RX_SYMBOL_LOCK) {
3590 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3591 DRM_DEBUG_KMS("FDI train 2 done.\n");
3592 break;
3593 }
3594 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003595 }
Sean Paulfa37d392012-03-02 12:53:39 -05003596 if (retry < 5)
3597 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003598 }
3599 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003600 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003601
3602 DRM_DEBUG_KMS("FDI train done.\n");
3603}
3604
Jesse Barnes357555c2011-04-28 15:09:55 -07003605/* Manual link training for Ivy Bridge A0 parts */
3606static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3607{
3608 struct drm_device *dev = crtc->dev;
3609 struct drm_i915_private *dev_priv = dev->dev_private;
3610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3611 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003612 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003613
3614 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3615 for train result */
3616 reg = FDI_RX_IMR(pipe);
3617 temp = I915_READ(reg);
3618 temp &= ~FDI_RX_SYMBOL_LOCK;
3619 temp &= ~FDI_RX_BIT_LOCK;
3620 I915_WRITE(reg, temp);
3621
3622 POSTING_READ(reg);
3623 udelay(150);
3624
Daniel Vetter01a415f2012-10-27 15:58:40 +02003625 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3626 I915_READ(FDI_RX_IIR(pipe)));
3627
Jesse Barnes139ccd32013-08-19 11:04:55 -07003628 /* Try each vswing and preemphasis setting twice before moving on */
3629 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3630 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003631 reg = FDI_TX_CTL(pipe);
3632 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003633 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3634 temp &= ~FDI_TX_ENABLE;
3635 I915_WRITE(reg, temp);
3636
3637 reg = FDI_RX_CTL(pipe);
3638 temp = I915_READ(reg);
3639 temp &= ~FDI_LINK_TRAIN_AUTO;
3640 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3641 temp &= ~FDI_RX_ENABLE;
3642 I915_WRITE(reg, temp);
3643
3644 /* enable CPU FDI TX and PCH FDI RX */
3645 reg = FDI_TX_CTL(pipe);
3646 temp = I915_READ(reg);
3647 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003648 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003649 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003650 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003651 temp |= snb_b_fdi_train_param[j/2];
3652 temp |= FDI_COMPOSITE_SYNC;
3653 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3654
3655 I915_WRITE(FDI_RX_MISC(pipe),
3656 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3657
3658 reg = FDI_RX_CTL(pipe);
3659 temp = I915_READ(reg);
3660 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3661 temp |= FDI_COMPOSITE_SYNC;
3662 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3663
3664 POSTING_READ(reg);
3665 udelay(1); /* should be 0.5us */
3666
3667 for (i = 0; i < 4; i++) {
3668 reg = FDI_RX_IIR(pipe);
3669 temp = I915_READ(reg);
3670 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3671
3672 if (temp & FDI_RX_BIT_LOCK ||
3673 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3674 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3675 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3676 i);
3677 break;
3678 }
3679 udelay(1); /* should be 0.5us */
3680 }
3681 if (i == 4) {
3682 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3683 continue;
3684 }
3685
3686 /* Train 2 */
3687 reg = FDI_TX_CTL(pipe);
3688 temp = I915_READ(reg);
3689 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3690 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3691 I915_WRITE(reg, temp);
3692
3693 reg = FDI_RX_CTL(pipe);
3694 temp = I915_READ(reg);
3695 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3696 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003697 I915_WRITE(reg, temp);
3698
3699 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003700 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003701
Jesse Barnes139ccd32013-08-19 11:04:55 -07003702 for (i = 0; i < 4; i++) {
3703 reg = FDI_RX_IIR(pipe);
3704 temp = I915_READ(reg);
3705 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003706
Jesse Barnes139ccd32013-08-19 11:04:55 -07003707 if (temp & FDI_RX_SYMBOL_LOCK ||
3708 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3709 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3710 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3711 i);
3712 goto train_done;
3713 }
3714 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003715 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003716 if (i == 4)
3717 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003718 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003719
Jesse Barnes139ccd32013-08-19 11:04:55 -07003720train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003721 DRM_DEBUG_KMS("FDI train done.\n");
3722}
3723
Daniel Vetter88cefb62012-08-12 19:27:14 +02003724static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003725{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003726 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003727 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003728 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003729 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003730
Jesse Barnesc64e3112010-09-10 11:27:03 -07003731
Jesse Barnes0e23b992010-09-10 11:10:00 -07003732 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003733 reg = FDI_RX_CTL(pipe);
3734 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003735 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003736 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003737 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003738 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3739
3740 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003741 udelay(200);
3742
3743 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003744 temp = I915_READ(reg);
3745 I915_WRITE(reg, temp | FDI_PCDCLK);
3746
3747 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003748 udelay(200);
3749
Paulo Zanoni20749732012-11-23 15:30:38 -02003750 /* Enable CPU FDI TX PLL, always on for Ironlake */
3751 reg = FDI_TX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3754 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003755
Paulo Zanoni20749732012-11-23 15:30:38 -02003756 POSTING_READ(reg);
3757 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003758 }
3759}
3760
Daniel Vetter88cefb62012-08-12 19:27:14 +02003761static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3762{
3763 struct drm_device *dev = intel_crtc->base.dev;
3764 struct drm_i915_private *dev_priv = dev->dev_private;
3765 int pipe = intel_crtc->pipe;
3766 u32 reg, temp;
3767
3768 /* Switch from PCDclk to Rawclk */
3769 reg = FDI_RX_CTL(pipe);
3770 temp = I915_READ(reg);
3771 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3772
3773 /* Disable CPU FDI TX PLL */
3774 reg = FDI_TX_CTL(pipe);
3775 temp = I915_READ(reg);
3776 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3777
3778 POSTING_READ(reg);
3779 udelay(100);
3780
3781 reg = FDI_RX_CTL(pipe);
3782 temp = I915_READ(reg);
3783 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3784
3785 /* Wait for the clocks to turn off. */
3786 POSTING_READ(reg);
3787 udelay(100);
3788}
3789
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003790static void ironlake_fdi_disable(struct drm_crtc *crtc)
3791{
3792 struct drm_device *dev = crtc->dev;
3793 struct drm_i915_private *dev_priv = dev->dev_private;
3794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3795 int pipe = intel_crtc->pipe;
3796 u32 reg, temp;
3797
3798 /* disable CPU FDI tx and PCH FDI rx */
3799 reg = FDI_TX_CTL(pipe);
3800 temp = I915_READ(reg);
3801 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3802 POSTING_READ(reg);
3803
3804 reg = FDI_RX_CTL(pipe);
3805 temp = I915_READ(reg);
3806 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003807 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003808 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3809
3810 POSTING_READ(reg);
3811 udelay(100);
3812
3813 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003814 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003815 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003816
3817 /* still set train pattern 1 */
3818 reg = FDI_TX_CTL(pipe);
3819 temp = I915_READ(reg);
3820 temp &= ~FDI_LINK_TRAIN_NONE;
3821 temp |= FDI_LINK_TRAIN_PATTERN_1;
3822 I915_WRITE(reg, temp);
3823
3824 reg = FDI_RX_CTL(pipe);
3825 temp = I915_READ(reg);
3826 if (HAS_PCH_CPT(dev)) {
3827 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3828 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3829 } else {
3830 temp &= ~FDI_LINK_TRAIN_NONE;
3831 temp |= FDI_LINK_TRAIN_PATTERN_1;
3832 }
3833 /* BPC in FDI rx is consistent with that in PIPECONF */
3834 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003835 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003836 I915_WRITE(reg, temp);
3837
3838 POSTING_READ(reg);
3839 udelay(100);
3840}
3841
Chris Wilson5dce5b932014-01-20 10:17:36 +00003842bool intel_has_pending_fb_unpin(struct drm_device *dev)
3843{
3844 struct intel_crtc *crtc;
3845
3846 /* Note that we don't need to be called with mode_config.lock here
3847 * as our list of CRTC objects is static for the lifetime of the
3848 * device and so cannot disappear as we iterate. Similarly, we can
3849 * happily treat the predicates as racy, atomic checks as userspace
3850 * cannot claim and pin a new fb without at least acquring the
3851 * struct_mutex and so serialising with us.
3852 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003853 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003854 if (atomic_read(&crtc->unpin_work_count) == 0)
3855 continue;
3856
3857 if (crtc->unpin_work)
3858 intel_wait_for_vblank(dev, crtc->pipe);
3859
3860 return true;
3861 }
3862
3863 return false;
3864}
3865
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003866static void page_flip_completed(struct intel_crtc *intel_crtc)
3867{
3868 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3869 struct intel_unpin_work *work = intel_crtc->unpin_work;
3870
3871 /* ensure that the unpin work is consistent wrt ->pending. */
3872 smp_rmb();
3873 intel_crtc->unpin_work = NULL;
3874
3875 if (work->event)
3876 drm_send_vblank_event(intel_crtc->base.dev,
3877 intel_crtc->pipe,
3878 work->event);
3879
3880 drm_crtc_vblank_put(&intel_crtc->base);
3881
3882 wake_up_all(&dev_priv->pending_flip_queue);
3883 queue_work(dev_priv->wq, &work->work);
3884
3885 trace_i915_flip_complete(intel_crtc->plane,
3886 work->pending_flip_obj);
3887}
3888
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003889void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003890{
Chris Wilson0f911282012-04-17 10:05:38 +01003891 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003892 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003893
Daniel Vetter2c10d572012-12-20 21:24:07 +01003894 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003895 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3896 !intel_crtc_has_pending_flip(crtc),
3897 60*HZ) == 0)) {
3898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003899
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003900 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003901 if (intel_crtc->unpin_work) {
3902 WARN_ONCE(1, "Removing stuck page flip\n");
3903 page_flip_completed(intel_crtc);
3904 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003905 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003906 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003907
Chris Wilson975d5682014-08-20 13:13:34 +01003908 if (crtc->primary->fb) {
3909 mutex_lock(&dev->struct_mutex);
3910 intel_finish_fb(crtc->primary->fb);
3911 mutex_unlock(&dev->struct_mutex);
3912 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003913}
3914
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003915/* Program iCLKIP clock to the desired frequency */
3916static void lpt_program_iclkip(struct drm_crtc *crtc)
3917{
3918 struct drm_device *dev = crtc->dev;
3919 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003920 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003921 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3922 u32 temp;
3923
Ville Syrjäläa5805162015-05-26 20:42:30 +03003924 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003925
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003926 /* It is necessary to ungate the pixclk gate prior to programming
3927 * the divisors, and gate it back when it is done.
3928 */
3929 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3930
3931 /* Disable SSCCTL */
3932 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003933 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3934 SBI_SSCCTL_DISABLE,
3935 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003936
3937 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003938 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939 auxdiv = 1;
3940 divsel = 0x41;
3941 phaseinc = 0x20;
3942 } else {
3943 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003944 * but the adjusted_mode->crtc_clock in in KHz. To get the
3945 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003946 * convert the virtual clock precision to KHz here for higher
3947 * precision.
3948 */
3949 u32 iclk_virtual_root_freq = 172800 * 1000;
3950 u32 iclk_pi_range = 64;
3951 u32 desired_divisor, msb_divisor_value, pi_value;
3952
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003953 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003954 msb_divisor_value = desired_divisor / iclk_pi_range;
3955 pi_value = desired_divisor % iclk_pi_range;
3956
3957 auxdiv = 0;
3958 divsel = msb_divisor_value - 2;
3959 phaseinc = pi_value;
3960 }
3961
3962 /* This should not happen with any sane values */
3963 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3964 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3965 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3966 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3967
3968 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 +03003969 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003970 auxdiv,
3971 divsel,
3972 phasedir,
3973 phaseinc);
3974
3975 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003976 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003977 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3978 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3979 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3980 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3981 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3982 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003983 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003984
3985 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003986 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003987 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3988 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003989 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990
3991 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003992 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003993 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003994 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003995
3996 /* Wait for initialization time */
3997 udelay(24);
3998
3999 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004000
Ville Syrjäläa5805162015-05-26 20:42:30 +03004001 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004002}
4003
Daniel Vetter275f01b22013-05-03 11:49:47 +02004004static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4005 enum pipe pch_transcoder)
4006{
4007 struct drm_device *dev = crtc->base.dev;
4008 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004009 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004010
4011 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4012 I915_READ(HTOTAL(cpu_transcoder)));
4013 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4014 I915_READ(HBLANK(cpu_transcoder)));
4015 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4016 I915_READ(HSYNC(cpu_transcoder)));
4017
4018 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4019 I915_READ(VTOTAL(cpu_transcoder)));
4020 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4021 I915_READ(VBLANK(cpu_transcoder)));
4022 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4023 I915_READ(VSYNC(cpu_transcoder)));
4024 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4025 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4026}
4027
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004028static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004029{
4030 struct drm_i915_private *dev_priv = dev->dev_private;
4031 uint32_t temp;
4032
4033 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004034 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004035 return;
4036
4037 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4038 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4039
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004040 temp &= ~FDI_BC_BIFURCATION_SELECT;
4041 if (enable)
4042 temp |= FDI_BC_BIFURCATION_SELECT;
4043
4044 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004045 I915_WRITE(SOUTH_CHICKEN1, temp);
4046 POSTING_READ(SOUTH_CHICKEN1);
4047}
4048
4049static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4050{
4051 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004052
4053 switch (intel_crtc->pipe) {
4054 case PIPE_A:
4055 break;
4056 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004057 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004058 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004059 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004060 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004061
4062 break;
4063 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004064 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004065
4066 break;
4067 default:
4068 BUG();
4069 }
4070}
4071
Jesse Barnesf67a5592011-01-05 10:31:48 -08004072/*
4073 * Enable PCH resources required for PCH ports:
4074 * - PCH PLLs
4075 * - FDI training & RX/TX
4076 * - update transcoder timings
4077 * - DP transcoding bits
4078 * - transcoder
4079 */
4080static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004081{
4082 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004083 struct drm_i915_private *dev_priv = dev->dev_private;
4084 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4085 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004086 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004087
Daniel Vetterab9412b2013-05-03 11:49:46 +02004088 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004089
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004090 if (IS_IVYBRIDGE(dev))
4091 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4092
Daniel Vettercd986ab2012-10-26 10:58:12 +02004093 /* Write the TU size bits before fdi link training, so that error
4094 * detection works. */
4095 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4096 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4097
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004098 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004099 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004100
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004101 /* We need to program the right clock selection before writing the pixel
4102 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004103 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004104 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004105
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004106 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004107 temp |= TRANS_DPLL_ENABLE(pipe);
4108 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004109 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004110 temp |= sel;
4111 else
4112 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004113 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004114 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004115
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004116 /* XXX: pch pll's can be enabled any time before we enable the PCH
4117 * transcoder, and we actually should do this to not upset any PCH
4118 * transcoder that already use the clock when we share it.
4119 *
4120 * Note that enable_shared_dpll tries to do the right thing, but
4121 * get_shared_dpll unconditionally resets the pll - we need that to have
4122 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004123 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004124
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004125 /* set transcoder timing, panel must allow it */
4126 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004127 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004129 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004130
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004131 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004132 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004133 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004134 reg = TRANS_DP_CTL(pipe);
4135 temp = I915_READ(reg);
4136 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004137 TRANS_DP_SYNC_MASK |
4138 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004139 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004140 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004141
4142 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004143 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004144 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004145 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146
4147 switch (intel_trans_dp_port_sel(crtc)) {
4148 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004149 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150 break;
4151 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004152 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153 break;
4154 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004155 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004156 break;
4157 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004158 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159 }
4160
Chris Wilson5eddb702010-09-11 13:48:45 +01004161 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004162 }
4163
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004164 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004165}
4166
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004167static void lpt_pch_enable(struct drm_crtc *crtc)
4168{
4169 struct drm_device *dev = crtc->dev;
4170 struct drm_i915_private *dev_priv = dev->dev_private;
4171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004172 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004173
Daniel Vetterab9412b2013-05-03 11:49:46 +02004174 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004175
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004176 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004177
Paulo Zanoni0540e482012-10-31 18:12:40 -02004178 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004179 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004180
Paulo Zanoni937bb612012-10-31 18:12:47 -02004181 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004182}
4183
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004184struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4185 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004186{
Daniel Vettere2b78262013-06-07 23:10:03 +02004187 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004188 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004189 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004190 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004191
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004192 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4193
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004194 if (HAS_PCH_IBX(dev_priv->dev)) {
4195 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004196 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004197 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004198
Daniel Vetter46edb022013-06-05 13:34:12 +02004199 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4200 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004201
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004202 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004203
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004204 goto found;
4205 }
4206
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304207 if (IS_BROXTON(dev_priv->dev)) {
4208 /* PLL is attached to port in bxt */
4209 struct intel_encoder *encoder;
4210 struct intel_digital_port *intel_dig_port;
4211
4212 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4213 if (WARN_ON(!encoder))
4214 return NULL;
4215
4216 intel_dig_port = enc_to_dig_port(&encoder->base);
4217 /* 1:1 mapping between ports and PLLs */
4218 i = (enum intel_dpll_id)intel_dig_port->port;
4219 pll = &dev_priv->shared_dplls[i];
4220 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4221 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004222 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304223
4224 goto found;
4225 }
4226
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004227 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4228 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004229
4230 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004231 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004232 continue;
4233
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004234 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004235 &shared_dpll[i].hw_state,
4236 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004237 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004238 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004239 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004240 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004241 goto found;
4242 }
4243 }
4244
4245 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004246 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4247 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004248 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004249 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4250 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004251 goto found;
4252 }
4253 }
4254
4255 return NULL;
4256
4257found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004258 if (shared_dpll[i].crtc_mask == 0)
4259 shared_dpll[i].hw_state =
4260 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004261
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004262 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004263 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4264 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004265
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004266 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004267
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004268 return pll;
4269}
4270
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004271static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004272{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004273 struct drm_i915_private *dev_priv = to_i915(state->dev);
4274 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004275 struct intel_shared_dpll *pll;
4276 enum intel_dpll_id i;
4277
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004278 if (!to_intel_atomic_state(state)->dpll_set)
4279 return;
4280
4281 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004282 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4283 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004284 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004285 }
4286}
4287
Daniel Vettera1520312013-05-03 11:49:50 +02004288static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004289{
4290 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004291 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004292 u32 temp;
4293
4294 temp = I915_READ(dslreg);
4295 udelay(500);
4296 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004297 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004298 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004299 }
4300}
4301
Chandra Kondurua1b22782015-04-07 15:28:45 -07004302/**
4303 * skl_update_scaler_users - Stages update to crtc's scaler state
4304 * @intel_crtc: crtc
4305 * @crtc_state: crtc_state
4306 * @plane: plane (NULL indicates crtc is requesting update)
4307 * @plane_state: plane's state
4308 * @force_detach: request unconditional detachment of scaler
4309 *
4310 * This function updates scaler state for requested plane or crtc.
4311 * To request scaler usage update for a plane, caller shall pass plane pointer.
4312 * To request scaler usage update for crtc, caller shall pass plane pointer
4313 * as NULL.
4314 *
4315 * Return
4316 * 0 - scaler_usage updated successfully
4317 * error - requested scaling cannot be supported or other error condition
4318 */
4319int
4320skl_update_scaler_users(
4321 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4322 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4323 int force_detach)
4324{
4325 int need_scaling;
4326 int idx;
4327 int src_w, src_h, dst_w, dst_h;
4328 int *scaler_id;
4329 struct drm_framebuffer *fb;
4330 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004331 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004332
4333 if (!intel_crtc || !crtc_state)
4334 return 0;
4335
4336 scaler_state = &crtc_state->scaler_state;
4337
4338 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4339 fb = intel_plane ? plane_state->base.fb : NULL;
4340
4341 if (intel_plane) {
4342 src_w = drm_rect_width(&plane_state->src) >> 16;
4343 src_h = drm_rect_height(&plane_state->src) >> 16;
4344 dst_w = drm_rect_width(&plane_state->dst);
4345 dst_h = drm_rect_height(&plane_state->dst);
4346 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004347 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004348 } else {
4349 struct drm_display_mode *adjusted_mode =
4350 &crtc_state->base.adjusted_mode;
4351 src_w = crtc_state->pipe_src_w;
4352 src_h = crtc_state->pipe_src_h;
4353 dst_w = adjusted_mode->hdisplay;
4354 dst_h = adjusted_mode->vdisplay;
4355 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004356 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004357 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004358
4359 need_scaling = intel_rotation_90_or_270(rotation) ?
4360 (src_h != dst_w || src_w != dst_h):
4361 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004362
4363 /*
4364 * if plane is being disabled or scaler is no more required or force detach
4365 * - free scaler binded to this plane/crtc
4366 * - in order to do this, update crtc->scaler_usage
4367 *
4368 * Here scaler state in crtc_state is set free so that
4369 * scaler can be assigned to other user. Actual register
4370 * update to free the scaler is done in plane/panel-fit programming.
4371 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4372 */
4373 if (force_detach || !need_scaling || (intel_plane &&
4374 (!fb || !plane_state->visible))) {
4375 if (*scaler_id >= 0) {
4376 scaler_state->scaler_users &= ~(1 << idx);
4377 scaler_state->scalers[*scaler_id].in_use = 0;
4378
4379 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4380 "crtc_state = %p scaler_users = 0x%x\n",
4381 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4382 intel_plane ? intel_plane->base.base.id :
4383 intel_crtc->base.base.id, crtc_state,
4384 scaler_state->scaler_users);
4385 *scaler_id = -1;
4386 }
4387 return 0;
4388 }
4389
4390 /* range checks */
4391 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4392 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4393
4394 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4395 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4396 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4397 "size is out of scaler range\n",
4398 intel_plane ? "PLANE" : "CRTC",
4399 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4400 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4401 return -EINVAL;
4402 }
4403
4404 /* check colorkey */
Chandra Konduru225c2282015-05-18 16:18:44 -07004405 if (WARN_ON(intel_plane &&
4406 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4407 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4408 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004409 return -EINVAL;
4410 }
4411
4412 /* Check src format */
4413 if (intel_plane) {
4414 switch (fb->pixel_format) {
4415 case DRM_FORMAT_RGB565:
4416 case DRM_FORMAT_XBGR8888:
4417 case DRM_FORMAT_XRGB8888:
4418 case DRM_FORMAT_ABGR8888:
4419 case DRM_FORMAT_ARGB8888:
4420 case DRM_FORMAT_XRGB2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004421 case DRM_FORMAT_XBGR2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004422 case DRM_FORMAT_YUYV:
4423 case DRM_FORMAT_YVYU:
4424 case DRM_FORMAT_UYVY:
4425 case DRM_FORMAT_VYUY:
4426 break;
4427 default:
4428 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4429 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4430 return -EINVAL;
4431 }
4432 }
4433
4434 /* mark this plane as a scaler user in crtc_state */
4435 scaler_state->scaler_users |= (1 << idx);
4436 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4437 "crtc_state = %p scaler_users = 0x%x\n",
4438 intel_plane ? "PLANE" : "CRTC",
4439 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4440 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4441 return 0;
4442}
4443
4444static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004445{
4446 struct drm_device *dev = crtc->base.dev;
4447 struct drm_i915_private *dev_priv = dev->dev_private;
4448 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004449 struct intel_crtc_scaler_state *scaler_state =
4450 &crtc->config->scaler_state;
4451
4452 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4453
4454 /* To update pfit, first update scaler state */
4455 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4456 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4457 skl_detach_scalers(crtc);
4458 if (!enable)
4459 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004460
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004461 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004462 int id;
4463
4464 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4465 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4466 return;
4467 }
4468
4469 id = scaler_state->scaler_id;
4470 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4471 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4472 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4473 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4474
4475 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004476 }
4477}
4478
Jesse Barnesb074cec2013-04-25 12:55:02 -07004479static void ironlake_pfit_enable(struct intel_crtc *crtc)
4480{
4481 struct drm_device *dev = crtc->base.dev;
4482 struct drm_i915_private *dev_priv = dev->dev_private;
4483 int pipe = crtc->pipe;
4484
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004485 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004486 /* Force use of hard-coded filter coefficients
4487 * as some pre-programmed values are broken,
4488 * e.g. x201.
4489 */
4490 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4491 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4492 PF_PIPE_SEL_IVB(pipe));
4493 else
4494 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004495 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4496 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004497 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004498}
4499
Matt Roper4a3b8762014-12-23 10:41:51 -08004500static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004501{
4502 struct drm_device *dev = crtc->dev;
4503 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004504 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004505 struct intel_plane *intel_plane;
4506
Matt Roperaf2b6532014-04-01 15:22:32 -07004507 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4508 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004509 if (intel_plane->pipe == pipe)
4510 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004511 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004512}
4513
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004514void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004515{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004516 struct drm_device *dev = crtc->base.dev;
4517 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004518
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004519 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004520 return;
4521
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004522 /* We can only enable IPS after we enable a plane and wait for a vblank */
4523 intel_wait_for_vblank(dev, crtc->pipe);
4524
Paulo Zanonid77e4532013-09-24 13:52:55 -03004525 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004526 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004527 mutex_lock(&dev_priv->rps.hw_lock);
4528 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4529 mutex_unlock(&dev_priv->rps.hw_lock);
4530 /* Quoting Art Runyan: "its not safe to expect any particular
4531 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004532 * mailbox." Moreover, the mailbox may return a bogus state,
4533 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004534 */
4535 } else {
4536 I915_WRITE(IPS_CTL, IPS_ENABLE);
4537 /* The bit only becomes 1 in the next vblank, so this wait here
4538 * is essentially intel_wait_for_vblank. If we don't have this
4539 * and don't wait for vblanks until the end of crtc_enable, then
4540 * the HW state readout code will complain that the expected
4541 * IPS_CTL value is not the one we read. */
4542 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4543 DRM_ERROR("Timed out waiting for IPS enable\n");
4544 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004545}
4546
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004547void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004548{
4549 struct drm_device *dev = crtc->base.dev;
4550 struct drm_i915_private *dev_priv = dev->dev_private;
4551
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004552 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004553 return;
4554
4555 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004556 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004557 mutex_lock(&dev_priv->rps.hw_lock);
4558 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4559 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004560 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4561 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4562 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004563 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004564 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004565 POSTING_READ(IPS_CTL);
4566 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004567
4568 /* We need to wait for a vblank before we can disable the plane. */
4569 intel_wait_for_vblank(dev, crtc->pipe);
4570}
4571
4572/** Loads the palette/gamma unit for the CRTC with the prepared values */
4573static void intel_crtc_load_lut(struct drm_crtc *crtc)
4574{
4575 struct drm_device *dev = crtc->dev;
4576 struct drm_i915_private *dev_priv = dev->dev_private;
4577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4578 enum pipe pipe = intel_crtc->pipe;
4579 int palreg = PALETTE(pipe);
4580 int i;
4581 bool reenable_ips = false;
4582
4583 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004584 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004585 return;
4586
Imre Deak50360402015-01-16 00:55:16 -08004587 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004588 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004589 assert_dsi_pll_enabled(dev_priv);
4590 else
4591 assert_pll_enabled(dev_priv, pipe);
4592 }
4593
4594 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304595 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004596 palreg = LGC_PALETTE(pipe);
4597
4598 /* Workaround : Do not read or write the pipe palette/gamma data while
4599 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4600 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004601 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004602 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4603 GAMMA_MODE_MODE_SPLIT)) {
4604 hsw_disable_ips(intel_crtc);
4605 reenable_ips = true;
4606 }
4607
4608 for (i = 0; i < 256; i++) {
4609 I915_WRITE(palreg + 4 * i,
4610 (intel_crtc->lut_r[i] << 16) |
4611 (intel_crtc->lut_g[i] << 8) |
4612 intel_crtc->lut_b[i]);
4613 }
4614
4615 if (reenable_ips)
4616 hsw_enable_ips(intel_crtc);
4617}
4618
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004619static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004620{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004621 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004622 struct drm_device *dev = intel_crtc->base.dev;
4623 struct drm_i915_private *dev_priv = dev->dev_private;
4624
4625 mutex_lock(&dev->struct_mutex);
4626 dev_priv->mm.interruptible = false;
4627 (void) intel_overlay_switch_off(intel_crtc->overlay);
4628 dev_priv->mm.interruptible = true;
4629 mutex_unlock(&dev->struct_mutex);
4630 }
4631
4632 /* Let userspace switch the overlay on again. In most cases userspace
4633 * has to recompute where to put it anyway.
4634 */
4635}
4636
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004637/**
4638 * intel_post_enable_primary - Perform operations after enabling primary plane
4639 * @crtc: the CRTC whose primary plane was just enabled
4640 *
4641 * Performs potentially sleeping operations that must be done after the primary
4642 * plane is enabled, such as updating FBC and IPS. Note that this may be
4643 * called due to an explicit primary plane update, or due to an implicit
4644 * re-enable that is caused when a sprite plane is updated to no longer
4645 * completely hide the primary plane.
4646 */
4647static void
4648intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004649{
4650 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004651 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4653 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004654
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004655 /*
4656 * BDW signals flip done immediately if the plane
4657 * is disabled, even if the plane enable is already
4658 * armed to occur at the next vblank :(
4659 */
4660 if (IS_BROADWELL(dev))
4661 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004662
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004663 /*
4664 * FIXME IPS should be fine as long as one plane is
4665 * enabled, but in practice it seems to have problems
4666 * when going from primary only to sprite only and vice
4667 * versa.
4668 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004669 hsw_enable_ips(intel_crtc);
4670
4671 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004672 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004673 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004674
4675 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004676 * Gen2 reports pipe underruns whenever all planes are disabled.
4677 * So don't enable underrun reporting before at least some planes
4678 * are enabled.
4679 * FIXME: Need to fix the logic to work when we turn off all planes
4680 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004681 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004682 if (IS_GEN2(dev))
4683 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4684
4685 /* Underruns don't raise interrupts, so check manually. */
4686 if (HAS_GMCH_DISPLAY(dev))
4687 i9xx_check_fifo_underruns(dev_priv);
4688}
4689
4690/**
4691 * intel_pre_disable_primary - Perform operations before disabling primary plane
4692 * @crtc: the CRTC whose primary plane is to be disabled
4693 *
4694 * Performs potentially sleeping operations that must be done before the
4695 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4696 * be called due to an explicit primary plane update, or due to an implicit
4697 * disable that is caused when a sprite plane completely hides the primary
4698 * plane.
4699 */
4700static void
4701intel_pre_disable_primary(struct drm_crtc *crtc)
4702{
4703 struct drm_device *dev = crtc->dev;
4704 struct drm_i915_private *dev_priv = dev->dev_private;
4705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4706 int pipe = intel_crtc->pipe;
4707
4708 /*
4709 * Gen2 reports pipe underruns whenever all planes are disabled.
4710 * So diasble underrun reporting before all the planes get disabled.
4711 * FIXME: Need to fix the logic to work when we turn off all planes
4712 * but leave the pipe running.
4713 */
4714 if (IS_GEN2(dev))
4715 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4716
4717 /*
4718 * Vblank time updates from the shadow to live plane control register
4719 * are blocked if the memory self-refresh mode is active at that
4720 * moment. So to make sure the plane gets truly disabled, disable
4721 * first the self-refresh mode. The self-refresh enable bit in turn
4722 * will be checked/applied by the HW only at the next frame start
4723 * event which is after the vblank start event, so we need to have a
4724 * wait-for-vblank between disabling the plane and the pipe.
4725 */
4726 if (HAS_GMCH_DISPLAY(dev))
4727 intel_set_memory_cxsr(dev_priv, false);
4728
4729 mutex_lock(&dev->struct_mutex);
4730 if (dev_priv->fbc.crtc == intel_crtc)
4731 intel_fbc_disable(dev);
4732 mutex_unlock(&dev->struct_mutex);
4733
4734 /*
4735 * FIXME IPS should be fine as long as one plane is
4736 * enabled, but in practice it seems to have problems
4737 * when going from primary only to sprite only and vice
4738 * versa.
4739 */
4740 hsw_disable_ips(intel_crtc);
4741}
4742
4743static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4744{
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004745 struct drm_device *dev = crtc->dev;
4746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4747 int pipe = intel_crtc->pipe;
4748
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004749 intel_enable_primary_hw_plane(crtc->primary, crtc);
4750 intel_enable_sprite_planes(crtc);
4751 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004752
4753 intel_post_enable_primary(crtc);
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004754
4755 /*
4756 * FIXME: Once we grow proper nuclear flip support out of this we need
4757 * to compute the mask of flip planes precisely. For the time being
4758 * consider this a flip to a NULL plane.
4759 */
4760 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004761}
4762
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004763static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004764{
4765 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004767 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004768 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004769
4770 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004771
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004772 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004773
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004774 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004775 for_each_intel_plane(dev, intel_plane) {
4776 if (intel_plane->pipe == pipe) {
4777 struct drm_crtc *from = intel_plane->base.crtc;
4778
4779 intel_plane->disable_plane(&intel_plane->base,
4780 from ?: crtc, true);
4781 }
4782 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004783
Daniel Vetterf99d7062014-06-19 16:01:59 +02004784 /*
4785 * FIXME: Once we grow proper nuclear flip support out of this we need
4786 * to compute the mask of flip planes precisely. For the time being
4787 * consider this a flip to a NULL plane.
4788 */
4789 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004790}
4791
Jesse Barnesf67a5592011-01-05 10:31:48 -08004792static void ironlake_crtc_enable(struct drm_crtc *crtc)
4793{
4794 struct drm_device *dev = crtc->dev;
4795 struct drm_i915_private *dev_priv = dev->dev_private;
4796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004797 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004798 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004799
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004800 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004801 return;
4802
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004803 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004804 intel_prepare_shared_dpll(intel_crtc);
4805
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004806 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304807 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004808
4809 intel_set_pipe_timings(intel_crtc);
4810
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004811 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004812 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004813 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004814 }
4815
4816 ironlake_set_pipeconf(crtc);
4817
Jesse Barnesf67a5592011-01-05 10:31:48 -08004818 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004819
Daniel Vettera72e4c92014-09-30 10:56:47 +02004820 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4821 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004822
Daniel Vetterf6736a12013-06-05 13:34:30 +02004823 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004824 if (encoder->pre_enable)
4825 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004826
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004827 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004828 /* Note: FDI PLL enabling _must_ be done before we enable the
4829 * cpu pipes, hence this is separate from all the other fdi/pch
4830 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004831 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004832 } else {
4833 assert_fdi_tx_disabled(dev_priv, pipe);
4834 assert_fdi_rx_disabled(dev_priv, pipe);
4835 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004836
Jesse Barnesb074cec2013-04-25 12:55:02 -07004837 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004838
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004839 /*
4840 * On ILK+ LUT must be loaded before the pipe is running but with
4841 * clocks enabled
4842 */
4843 intel_crtc_load_lut(crtc);
4844
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004845 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004846 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004847
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004848 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004849 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004850
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004851 assert_vblank_disabled(crtc);
4852 drm_crtc_vblank_on(crtc);
4853
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004854 for_each_encoder_on_crtc(dev, crtc, encoder)
4855 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004856
4857 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004858 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004859}
4860
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004861/* IPS only exists on ULT machines and is tied to pipe A. */
4862static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4863{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004864 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004865}
4866
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004867static void haswell_crtc_enable(struct drm_crtc *crtc)
4868{
4869 struct drm_device *dev = crtc->dev;
4870 struct drm_i915_private *dev_priv = dev->dev_private;
4871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4872 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004873 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4874 struct intel_crtc_state *pipe_config =
4875 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004876
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004877 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004878 return;
4879
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004880 if (intel_crtc_to_shared_dpll(intel_crtc))
4881 intel_enable_shared_dpll(intel_crtc);
4882
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004883 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304884 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004885
4886 intel_set_pipe_timings(intel_crtc);
4887
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004888 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4889 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4890 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004891 }
4892
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004893 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004894 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004895 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004896 }
4897
4898 haswell_set_pipeconf(crtc);
4899
4900 intel_set_pipe_csc(crtc);
4901
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004902 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004903
Daniel Vettera72e4c92014-09-30 10:56:47 +02004904 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004905 for_each_encoder_on_crtc(dev, crtc, encoder)
4906 if (encoder->pre_enable)
4907 encoder->pre_enable(encoder);
4908
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004909 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004910 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4911 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004912 dev_priv->display.fdi_link_train(crtc);
4913 }
4914
Paulo Zanoni1f544382012-10-24 11:32:00 -02004915 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004916
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004917 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004918 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004919 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004920 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004921 else
4922 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004923
4924 /*
4925 * On ILK+ LUT must be loaded before the pipe is running but with
4926 * clocks enabled
4927 */
4928 intel_crtc_load_lut(crtc);
4929
Paulo Zanoni1f544382012-10-24 11:32:00 -02004930 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004931 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004932
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004933 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004934 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004935
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004936 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004937 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004938
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004939 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004940 intel_ddi_set_vc_payload_alloc(crtc, true);
4941
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004942 assert_vblank_disabled(crtc);
4943 drm_crtc_vblank_on(crtc);
4944
Jani Nikula8807e552013-08-30 19:40:32 +03004945 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004946 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004947 intel_opregion_notify_encoder(encoder, true);
4948 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004949
Paulo Zanonie4916942013-09-20 16:21:19 -03004950 /* If we change the relative order between pipe/planes enabling, we need
4951 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004952 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4953 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4954 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4955 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4956 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004957}
4958
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004959static void ironlake_pfit_disable(struct intel_crtc *crtc)
4960{
4961 struct drm_device *dev = crtc->base.dev;
4962 struct drm_i915_private *dev_priv = dev->dev_private;
4963 int pipe = crtc->pipe;
4964
4965 /* To avoid upsetting the power well on haswell only disable the pfit if
4966 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004967 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004968 I915_WRITE(PF_CTL(pipe), 0);
4969 I915_WRITE(PF_WIN_POS(pipe), 0);
4970 I915_WRITE(PF_WIN_SZ(pipe), 0);
4971 }
4972}
4973
Jesse Barnes6be4a602010-09-10 10:26:01 -07004974static void ironlake_crtc_disable(struct drm_crtc *crtc)
4975{
4976 struct drm_device *dev = crtc->dev;
4977 struct drm_i915_private *dev_priv = dev->dev_private;
4978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004979 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004980 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004981 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004982
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004983 if (WARN_ON(!intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004984 return;
4985
Daniel Vetterea9d7582012-07-10 10:42:52 +02004986 for_each_encoder_on_crtc(dev, crtc, encoder)
4987 encoder->disable(encoder);
4988
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004989 drm_crtc_vblank_off(crtc);
4990 assert_vblank_disabled(crtc);
4991
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004992 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004993 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004994
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004995 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004996
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004997 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004998
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004999 if (intel_crtc->config->has_pch_encoder)
5000 ironlake_fdi_disable(crtc);
5001
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005002 for_each_encoder_on_crtc(dev, crtc, encoder)
5003 if (encoder->post_disable)
5004 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005005
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005006 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005007 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005008
Daniel Vetterd925c592013-06-05 13:34:04 +02005009 if (HAS_PCH_CPT(dev)) {
5010 /* disable TRANS_DP_CTL */
5011 reg = TRANS_DP_CTL(pipe);
5012 temp = I915_READ(reg);
5013 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5014 TRANS_DP_PORT_SEL_MASK);
5015 temp |= TRANS_DP_PORT_SEL_NONE;
5016 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005017
Daniel Vetterd925c592013-06-05 13:34:04 +02005018 /* disable DPLL_SEL */
5019 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005020 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005021 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005022 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005023
5024 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005025 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005026
5027 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005028 }
5029
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005030 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005031 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005032
5033 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005034 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005035 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005036}
5037
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005038static void haswell_crtc_disable(struct drm_crtc *crtc)
5039{
5040 struct drm_device *dev = crtc->dev;
5041 struct drm_i915_private *dev_priv = dev->dev_private;
5042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5043 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005044 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005045
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005046 if (WARN_ON(!intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005047 return;
5048
Jani Nikula8807e552013-08-30 19:40:32 +03005049 for_each_encoder_on_crtc(dev, crtc, encoder) {
5050 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005051 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005052 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005053
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005054 drm_crtc_vblank_off(crtc);
5055 assert_vblank_disabled(crtc);
5056
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005057 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005058 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5059 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005060 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005061
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005062 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005063 intel_ddi_set_vc_payload_alloc(crtc, false);
5064
Paulo Zanoniad80a812012-10-24 16:06:19 -02005065 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005066
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005067 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005068 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005069 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005070 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005071 else
5072 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005073
Paulo Zanoni1f544382012-10-24 11:32:00 -02005074 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005075
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005076 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005077 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005078 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005079 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005080
Imre Deak97b040a2014-06-25 22:01:50 +03005081 for_each_encoder_on_crtc(dev, crtc, encoder)
5082 if (encoder->post_disable)
5083 encoder->post_disable(encoder);
5084
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005085 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005086 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005087
5088 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005089 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005090 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005091
5092 if (intel_crtc_to_shared_dpll(intel_crtc))
5093 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005094}
5095
Jesse Barnes2dd24552013-04-25 12:55:01 -07005096static void i9xx_pfit_enable(struct intel_crtc *crtc)
5097{
5098 struct drm_device *dev = crtc->base.dev;
5099 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005100 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005101
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005102 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005103 return;
5104
Daniel Vetterc0b03412013-05-28 12:05:54 +02005105 /*
5106 * The panel fitter should only be adjusted whilst the pipe is disabled,
5107 * according to register description and PRM.
5108 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005109 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5110 assert_pipe_disabled(dev_priv, crtc->pipe);
5111
Jesse Barnesb074cec2013-04-25 12:55:02 -07005112 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5113 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005114
5115 /* Border color in case we don't scale up to the full screen. Black by
5116 * default, change to something else for debugging. */
5117 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005118}
5119
Dave Airlied05410f2014-06-05 13:22:59 +10005120static enum intel_display_power_domain port_to_power_domain(enum port port)
5121{
5122 switch (port) {
5123 case PORT_A:
5124 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5125 case PORT_B:
5126 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5127 case PORT_C:
5128 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5129 case PORT_D:
5130 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5131 default:
5132 WARN_ON_ONCE(1);
5133 return POWER_DOMAIN_PORT_OTHER;
5134 }
5135}
5136
Imre Deak77d22dc2014-03-05 16:20:52 +02005137#define for_each_power_domain(domain, mask) \
5138 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5139 if ((1 << (domain)) & (mask))
5140
Imre Deak319be8a2014-03-04 19:22:57 +02005141enum intel_display_power_domain
5142intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005143{
Imre Deak319be8a2014-03-04 19:22:57 +02005144 struct drm_device *dev = intel_encoder->base.dev;
5145 struct intel_digital_port *intel_dig_port;
5146
5147 switch (intel_encoder->type) {
5148 case INTEL_OUTPUT_UNKNOWN:
5149 /* Only DDI platforms should ever use this output type */
5150 WARN_ON_ONCE(!HAS_DDI(dev));
5151 case INTEL_OUTPUT_DISPLAYPORT:
5152 case INTEL_OUTPUT_HDMI:
5153 case INTEL_OUTPUT_EDP:
5154 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005155 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005156 case INTEL_OUTPUT_DP_MST:
5157 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5158 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005159 case INTEL_OUTPUT_ANALOG:
5160 return POWER_DOMAIN_PORT_CRT;
5161 case INTEL_OUTPUT_DSI:
5162 return POWER_DOMAIN_PORT_DSI;
5163 default:
5164 return POWER_DOMAIN_PORT_OTHER;
5165 }
5166}
5167
5168static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5169{
5170 struct drm_device *dev = crtc->dev;
5171 struct intel_encoder *intel_encoder;
5172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5173 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005174 unsigned long mask;
5175 enum transcoder transcoder;
5176
5177 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5178
5179 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5180 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005181 if (intel_crtc->config->pch_pfit.enabled ||
5182 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005183 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5184
Imre Deak319be8a2014-03-04 19:22:57 +02005185 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5186 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5187
Imre Deak77d22dc2014-03-05 16:20:52 +02005188 return mask;
5189}
5190
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005191static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005192{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005193 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005194 struct drm_i915_private *dev_priv = dev->dev_private;
5195 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5196 struct intel_crtc *crtc;
5197
5198 /*
5199 * First get all needed power domains, then put all unneeded, to avoid
5200 * any unnecessary toggling of the power wells.
5201 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005202 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005203 enum intel_display_power_domain domain;
5204
Matt Roper83d65732015-02-25 13:12:16 -08005205 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005206 continue;
5207
Imre Deak319be8a2014-03-04 19:22:57 +02005208 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005209
5210 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5211 intel_display_power_get(dev_priv, domain);
5212 }
5213
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005214 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005215 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005216
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005217 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005218 enum intel_display_power_domain domain;
5219
5220 for_each_power_domain(domain, crtc->enabled_power_domains)
5221 intel_display_power_put(dev_priv, domain);
5222
5223 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5224 }
5225
5226 intel_display_set_init_power(dev_priv, false);
5227}
5228
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005229static void intel_update_max_cdclk(struct drm_device *dev)
5230{
5231 struct drm_i915_private *dev_priv = dev->dev_private;
5232
5233 if (IS_SKYLAKE(dev)) {
5234 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5235
5236 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5237 dev_priv->max_cdclk_freq = 675000;
5238 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5239 dev_priv->max_cdclk_freq = 540000;
5240 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5241 dev_priv->max_cdclk_freq = 450000;
5242 else
5243 dev_priv->max_cdclk_freq = 337500;
5244 } else if (IS_BROADWELL(dev)) {
5245 /*
5246 * FIXME with extra cooling we can allow
5247 * 540 MHz for ULX and 675 Mhz for ULT.
5248 * How can we know if extra cooling is
5249 * available? PCI ID, VTB, something else?
5250 */
5251 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5252 dev_priv->max_cdclk_freq = 450000;
5253 else if (IS_BDW_ULX(dev))
5254 dev_priv->max_cdclk_freq = 450000;
5255 else if (IS_BDW_ULT(dev))
5256 dev_priv->max_cdclk_freq = 540000;
5257 else
5258 dev_priv->max_cdclk_freq = 675000;
5259 } else if (IS_VALLEYVIEW(dev)) {
5260 dev_priv->max_cdclk_freq = 400000;
5261 } else {
5262 /* otherwise assume cdclk is fixed */
5263 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5264 }
5265
5266 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5267 dev_priv->max_cdclk_freq);
5268}
5269
5270static void intel_update_cdclk(struct drm_device *dev)
5271{
5272 struct drm_i915_private *dev_priv = dev->dev_private;
5273
5274 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5275 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5276 dev_priv->cdclk_freq);
5277
5278 /*
5279 * Program the gmbus_freq based on the cdclk frequency.
5280 * BSpec erroneously claims we should aim for 4MHz, but
5281 * in fact 1MHz is the correct frequency.
5282 */
5283 if (IS_VALLEYVIEW(dev)) {
5284 /*
5285 * Program the gmbus_freq based on the cdclk frequency.
5286 * BSpec erroneously claims we should aim for 4MHz, but
5287 * in fact 1MHz is the correct frequency.
5288 */
5289 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5290 }
5291
5292 if (dev_priv->max_cdclk_freq == 0)
5293 intel_update_max_cdclk(dev);
5294}
5295
Damien Lespiau70d0c572015-06-04 18:21:29 +01005296static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305297{
5298 struct drm_i915_private *dev_priv = dev->dev_private;
5299 uint32_t divider;
5300 uint32_t ratio;
5301 uint32_t current_freq;
5302 int ret;
5303
5304 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5305 switch (frequency) {
5306 case 144000:
5307 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5308 ratio = BXT_DE_PLL_RATIO(60);
5309 break;
5310 case 288000:
5311 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5312 ratio = BXT_DE_PLL_RATIO(60);
5313 break;
5314 case 384000:
5315 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5316 ratio = BXT_DE_PLL_RATIO(60);
5317 break;
5318 case 576000:
5319 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5320 ratio = BXT_DE_PLL_RATIO(60);
5321 break;
5322 case 624000:
5323 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5324 ratio = BXT_DE_PLL_RATIO(65);
5325 break;
5326 case 19200:
5327 /*
5328 * Bypass frequency with DE PLL disabled. Init ratio, divider
5329 * to suppress GCC warning.
5330 */
5331 ratio = 0;
5332 divider = 0;
5333 break;
5334 default:
5335 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5336
5337 return;
5338 }
5339
5340 mutex_lock(&dev_priv->rps.hw_lock);
5341 /* Inform power controller of upcoming frequency change */
5342 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5343 0x80000000);
5344 mutex_unlock(&dev_priv->rps.hw_lock);
5345
5346 if (ret) {
5347 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5348 ret, frequency);
5349 return;
5350 }
5351
5352 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5353 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5354 current_freq = current_freq * 500 + 1000;
5355
5356 /*
5357 * DE PLL has to be disabled when
5358 * - setting to 19.2MHz (bypass, PLL isn't used)
5359 * - before setting to 624MHz (PLL needs toggling)
5360 * - before setting to any frequency from 624MHz (PLL needs toggling)
5361 */
5362 if (frequency == 19200 || frequency == 624000 ||
5363 current_freq == 624000) {
5364 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5365 /* Timeout 200us */
5366 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5367 1))
5368 DRM_ERROR("timout waiting for DE PLL unlock\n");
5369 }
5370
5371 if (frequency != 19200) {
5372 uint32_t val;
5373
5374 val = I915_READ(BXT_DE_PLL_CTL);
5375 val &= ~BXT_DE_PLL_RATIO_MASK;
5376 val |= ratio;
5377 I915_WRITE(BXT_DE_PLL_CTL, val);
5378
5379 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5380 /* Timeout 200us */
5381 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5382 DRM_ERROR("timeout waiting for DE PLL lock\n");
5383
5384 val = I915_READ(CDCLK_CTL);
5385 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5386 val |= divider;
5387 /*
5388 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5389 * enable otherwise.
5390 */
5391 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5392 if (frequency >= 500000)
5393 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5394
5395 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5396 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5397 val |= (frequency - 1000) / 500;
5398 I915_WRITE(CDCLK_CTL, val);
5399 }
5400
5401 mutex_lock(&dev_priv->rps.hw_lock);
5402 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5403 DIV_ROUND_UP(frequency, 25000));
5404 mutex_unlock(&dev_priv->rps.hw_lock);
5405
5406 if (ret) {
5407 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5408 ret, frequency);
5409 return;
5410 }
5411
Damien Lespiaua47871b2015-06-04 18:21:34 +01005412 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305413}
5414
5415void broxton_init_cdclk(struct drm_device *dev)
5416{
5417 struct drm_i915_private *dev_priv = dev->dev_private;
5418 uint32_t val;
5419
5420 /*
5421 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5422 * or else the reset will hang because there is no PCH to respond.
5423 * Move the handshake programming to initialization sequence.
5424 * Previously was left up to BIOS.
5425 */
5426 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5427 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5428 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5429
5430 /* Enable PG1 for cdclk */
5431 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5432
5433 /* check if cd clock is enabled */
5434 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5435 DRM_DEBUG_KMS("Display already initialized\n");
5436 return;
5437 }
5438
5439 /*
5440 * FIXME:
5441 * - The initial CDCLK needs to be read from VBT.
5442 * Need to make this change after VBT has changes for BXT.
5443 * - check if setting the max (or any) cdclk freq is really necessary
5444 * here, it belongs to modeset time
5445 */
5446 broxton_set_cdclk(dev, 624000);
5447
5448 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005449 POSTING_READ(DBUF_CTL);
5450
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305451 udelay(10);
5452
5453 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5454 DRM_ERROR("DBuf power enable timeout!\n");
5455}
5456
5457void broxton_uninit_cdclk(struct drm_device *dev)
5458{
5459 struct drm_i915_private *dev_priv = dev->dev_private;
5460
5461 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005462 POSTING_READ(DBUF_CTL);
5463
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305464 udelay(10);
5465
5466 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5467 DRM_ERROR("DBuf power disable timeout!\n");
5468
5469 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5470 broxton_set_cdclk(dev, 19200);
5471
5472 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5473}
5474
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005475static const struct skl_cdclk_entry {
5476 unsigned int freq;
5477 unsigned int vco;
5478} skl_cdclk_frequencies[] = {
5479 { .freq = 308570, .vco = 8640 },
5480 { .freq = 337500, .vco = 8100 },
5481 { .freq = 432000, .vco = 8640 },
5482 { .freq = 450000, .vco = 8100 },
5483 { .freq = 540000, .vco = 8100 },
5484 { .freq = 617140, .vco = 8640 },
5485 { .freq = 675000, .vco = 8100 },
5486};
5487
5488static unsigned int skl_cdclk_decimal(unsigned int freq)
5489{
5490 return (freq - 1000) / 500;
5491}
5492
5493static unsigned int skl_cdclk_get_vco(unsigned int freq)
5494{
5495 unsigned int i;
5496
5497 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5498 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5499
5500 if (e->freq == freq)
5501 return e->vco;
5502 }
5503
5504 return 8100;
5505}
5506
5507static void
5508skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5509{
5510 unsigned int min_freq;
5511 u32 val;
5512
5513 /* select the minimum CDCLK before enabling DPLL 0 */
5514 val = I915_READ(CDCLK_CTL);
5515 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5516 val |= CDCLK_FREQ_337_308;
5517
5518 if (required_vco == 8640)
5519 min_freq = 308570;
5520 else
5521 min_freq = 337500;
5522
5523 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5524
5525 I915_WRITE(CDCLK_CTL, val);
5526 POSTING_READ(CDCLK_CTL);
5527
5528 /*
5529 * We always enable DPLL0 with the lowest link rate possible, but still
5530 * taking into account the VCO required to operate the eDP panel at the
5531 * desired frequency. The usual DP link rates operate with a VCO of
5532 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5533 * The modeset code is responsible for the selection of the exact link
5534 * rate later on, with the constraint of choosing a frequency that
5535 * works with required_vco.
5536 */
5537 val = I915_READ(DPLL_CTRL1);
5538
5539 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5540 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5541 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5542 if (required_vco == 8640)
5543 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5544 SKL_DPLL0);
5545 else
5546 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5547 SKL_DPLL0);
5548
5549 I915_WRITE(DPLL_CTRL1, val);
5550 POSTING_READ(DPLL_CTRL1);
5551
5552 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5553
5554 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5555 DRM_ERROR("DPLL0 not locked\n");
5556}
5557
5558static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5559{
5560 int ret;
5561 u32 val;
5562
5563 /* inform PCU we want to change CDCLK */
5564 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5565 mutex_lock(&dev_priv->rps.hw_lock);
5566 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5567 mutex_unlock(&dev_priv->rps.hw_lock);
5568
5569 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5570}
5571
5572static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5573{
5574 unsigned int i;
5575
5576 for (i = 0; i < 15; i++) {
5577 if (skl_cdclk_pcu_ready(dev_priv))
5578 return true;
5579 udelay(10);
5580 }
5581
5582 return false;
5583}
5584
5585static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5586{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005587 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005588 u32 freq_select, pcu_ack;
5589
5590 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5591
5592 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5593 DRM_ERROR("failed to inform PCU about cdclk change\n");
5594 return;
5595 }
5596
5597 /* set CDCLK_CTL */
5598 switch(freq) {
5599 case 450000:
5600 case 432000:
5601 freq_select = CDCLK_FREQ_450_432;
5602 pcu_ack = 1;
5603 break;
5604 case 540000:
5605 freq_select = CDCLK_FREQ_540;
5606 pcu_ack = 2;
5607 break;
5608 case 308570:
5609 case 337500:
5610 default:
5611 freq_select = CDCLK_FREQ_337_308;
5612 pcu_ack = 0;
5613 break;
5614 case 617140:
5615 case 675000:
5616 freq_select = CDCLK_FREQ_675_617;
5617 pcu_ack = 3;
5618 break;
5619 }
5620
5621 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5622 POSTING_READ(CDCLK_CTL);
5623
5624 /* inform PCU of the change */
5625 mutex_lock(&dev_priv->rps.hw_lock);
5626 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5627 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005628
5629 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005630}
5631
5632void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5633{
5634 /* disable DBUF power */
5635 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5636 POSTING_READ(DBUF_CTL);
5637
5638 udelay(10);
5639
5640 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5641 DRM_ERROR("DBuf power disable timeout\n");
5642
5643 /* disable DPLL0 */
5644 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5645 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5646 DRM_ERROR("Couldn't disable DPLL0\n");
5647
5648 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5649}
5650
5651void skl_init_cdclk(struct drm_i915_private *dev_priv)
5652{
5653 u32 val;
5654 unsigned int required_vco;
5655
5656 /* enable PCH reset handshake */
5657 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5658 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5659
5660 /* enable PG1 and Misc I/O */
5661 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5662
5663 /* DPLL0 already enabed !? */
5664 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5665 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5666 return;
5667 }
5668
5669 /* enable DPLL0 */
5670 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5671 skl_dpll0_enable(dev_priv, required_vco);
5672
5673 /* set CDCLK to the frequency the BIOS chose */
5674 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5675
5676 /* enable DBUF power */
5677 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5678 POSTING_READ(DBUF_CTL);
5679
5680 udelay(10);
5681
5682 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5683 DRM_ERROR("DBuf power enable timeout\n");
5684}
5685
Ville Syrjälädfcab172014-06-13 13:37:47 +03005686/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005687static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005688{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005689 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005690
Jesse Barnes586f49d2013-11-04 16:06:59 -08005691 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005692 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005693 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5694 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005695 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005696
Ville Syrjälädfcab172014-06-13 13:37:47 +03005697 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005698}
5699
5700/* Adjust CDclk dividers to allow high res or save power if possible */
5701static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5702{
5703 struct drm_i915_private *dev_priv = dev->dev_private;
5704 u32 val, cmd;
5705
Vandana Kannan164dfd22014-11-24 13:37:41 +05305706 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5707 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005708
Ville Syrjälädfcab172014-06-13 13:37:47 +03005709 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005710 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005711 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005712 cmd = 1;
5713 else
5714 cmd = 0;
5715
5716 mutex_lock(&dev_priv->rps.hw_lock);
5717 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5718 val &= ~DSPFREQGUAR_MASK;
5719 val |= (cmd << DSPFREQGUAR_SHIFT);
5720 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5721 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5722 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5723 50)) {
5724 DRM_ERROR("timed out waiting for CDclk change\n");
5725 }
5726 mutex_unlock(&dev_priv->rps.hw_lock);
5727
Ville Syrjälä54433e92015-05-26 20:42:31 +03005728 mutex_lock(&dev_priv->sb_lock);
5729
Ville Syrjälädfcab172014-06-13 13:37:47 +03005730 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005731 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005732
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005733 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005734
Jesse Barnes30a970c2013-11-04 13:48:12 -08005735 /* adjust cdclk divider */
5736 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005737 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005738 val |= divider;
5739 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005740
5741 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5742 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5743 50))
5744 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005745 }
5746
Jesse Barnes30a970c2013-11-04 13:48:12 -08005747 /* adjust self-refresh exit latency value */
5748 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5749 val &= ~0x7f;
5750
5751 /*
5752 * For high bandwidth configs, we set a higher latency in the bunit
5753 * so that the core display fetch happens in time to avoid underruns.
5754 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005755 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005756 val |= 4500 / 250; /* 4.5 usec */
5757 else
5758 val |= 3000 / 250; /* 3.0 usec */
5759 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005760
Ville Syrjäläa5805162015-05-26 20:42:30 +03005761 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005762
Ville Syrjäläb6283052015-06-03 15:45:07 +03005763 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005764}
5765
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005766static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5767{
5768 struct drm_i915_private *dev_priv = dev->dev_private;
5769 u32 val, cmd;
5770
Vandana Kannan164dfd22014-11-24 13:37:41 +05305771 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5772 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005773
5774 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005775 case 333333:
5776 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005777 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005778 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005779 break;
5780 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005781 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005782 return;
5783 }
5784
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005785 /*
5786 * Specs are full of misinformation, but testing on actual
5787 * hardware has shown that we just need to write the desired
5788 * CCK divider into the Punit register.
5789 */
5790 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5791
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005792 mutex_lock(&dev_priv->rps.hw_lock);
5793 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5794 val &= ~DSPFREQGUAR_MASK_CHV;
5795 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5796 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5797 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5798 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5799 50)) {
5800 DRM_ERROR("timed out waiting for CDclk change\n");
5801 }
5802 mutex_unlock(&dev_priv->rps.hw_lock);
5803
Ville Syrjäläb6283052015-06-03 15:45:07 +03005804 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005805}
5806
Jesse Barnes30a970c2013-11-04 13:48:12 -08005807static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5808 int max_pixclk)
5809{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005810 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005811 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005812
Jesse Barnes30a970c2013-11-04 13:48:12 -08005813 /*
5814 * Really only a few cases to deal with, as only 4 CDclks are supported:
5815 * 200MHz
5816 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005817 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005818 * 400MHz (VLV only)
5819 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5820 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005821 *
5822 * We seem to get an unstable or solid color picture at 200MHz.
5823 * Not sure what's wrong. For now use 200MHz only when all pipes
5824 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005825 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005826 if (!IS_CHERRYVIEW(dev_priv) &&
5827 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005828 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005829 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005830 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005831 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005832 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005833 else
5834 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005835}
5836
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305837static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5838 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005839{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305840 /*
5841 * FIXME:
5842 * - remove the guardband, it's not needed on BXT
5843 * - set 19.2MHz bypass frequency if there are no active pipes
5844 */
5845 if (max_pixclk > 576000*9/10)
5846 return 624000;
5847 else if (max_pixclk > 384000*9/10)
5848 return 576000;
5849 else if (max_pixclk > 288000*9/10)
5850 return 384000;
5851 else if (max_pixclk > 144000*9/10)
5852 return 288000;
5853 else
5854 return 144000;
5855}
5856
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005857/* Compute the max pixel clock for new configuration. Uses atomic state if
5858 * that's non-NULL, look at current state otherwise. */
5859static int intel_mode_max_pixclk(struct drm_device *dev,
5860 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005861{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005862 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005863 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005864 int max_pixclk = 0;
5865
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005866 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005867 if (state)
5868 crtc_state =
5869 intel_atomic_get_crtc_state(state, intel_crtc);
5870 else
5871 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005872 if (IS_ERR(crtc_state))
5873 return PTR_ERR(crtc_state);
5874
5875 if (!crtc_state->base.enable)
5876 continue;
5877
5878 max_pixclk = max(max_pixclk,
5879 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880 }
5881
5882 return max_pixclk;
5883}
5884
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005885static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005886{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005887 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005888 struct drm_crtc *crtc;
5889 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005890 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005891 int cdclk, ret = 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005892
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005893 if (max_pixclk < 0)
5894 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005895
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305896 if (IS_VALLEYVIEW(dev_priv))
5897 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5898 else
5899 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5900
5901 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005902 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005903
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005904 /* add all active pipes to the state */
5905 for_each_crtc(state->dev, crtc) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005906 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5907 if (IS_ERR(crtc_state))
5908 return PTR_ERR(crtc_state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005909
5910 if (!crtc_state->active || needs_modeset(crtc_state))
5911 continue;
5912
5913 crtc_state->mode_changed = true;
5914
5915 ret = drm_atomic_add_affected_connectors(state, crtc);
5916 if (ret)
5917 break;
5918
5919 ret = drm_atomic_add_affected_planes(state, crtc);
5920 if (ret)
5921 break;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005922 }
5923
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005924 return ret;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005925}
5926
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005927static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5928{
5929 unsigned int credits, default_credits;
5930
5931 if (IS_CHERRYVIEW(dev_priv))
5932 default_credits = PFI_CREDIT(12);
5933 else
5934 default_credits = PFI_CREDIT(8);
5935
Vandana Kannan164dfd22014-11-24 13:37:41 +05305936 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005937 /* CHV suggested value is 31 or 63 */
5938 if (IS_CHERRYVIEW(dev_priv))
5939 credits = PFI_CREDIT_31;
5940 else
5941 credits = PFI_CREDIT(15);
5942 } else {
5943 credits = default_credits;
5944 }
5945
5946 /*
5947 * WA - write default credits before re-programming
5948 * FIXME: should we also set the resend bit here?
5949 */
5950 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5951 default_credits);
5952
5953 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5954 credits | PFI_CREDIT_RESEND);
5955
5956 /*
5957 * FIXME is this guaranteed to clear
5958 * immediately or should we poll for it?
5959 */
5960 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5961}
5962
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005963static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005964{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005965 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005966 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005967 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005968 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005969
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005970 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5971 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005972 if (WARN_ON(max_pixclk < 0))
5973 return;
5974
5975 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005976
Vandana Kannan164dfd22014-11-24 13:37:41 +05305977 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005978 /*
5979 * FIXME: We can end up here with all power domains off, yet
5980 * with a CDCLK frequency other than the minimum. To account
5981 * for this take the PIPE-A power domain, which covers the HW
5982 * blocks needed for the following programming. This can be
5983 * removed once it's guaranteed that we get here either with
5984 * the minimum CDCLK set, or the required power domains
5985 * enabled.
5986 */
5987 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5988
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005989 if (IS_CHERRYVIEW(dev))
5990 cherryview_set_cdclk(dev, req_cdclk);
5991 else
5992 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005993
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005994 vlv_program_pfi_credits(dev_priv);
5995
Imre Deak738c05c2014-11-19 16:25:37 +02005996 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005997 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005998}
5999
Jesse Barnes89b667f2013-04-18 14:51:36 -07006000static void valleyview_crtc_enable(struct drm_crtc *crtc)
6001{
6002 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006003 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006004 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6005 struct intel_encoder *encoder;
6006 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006007 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006008
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006009 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006010 return;
6011
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006012 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306013
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006014 if (!is_dsi) {
6015 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006016 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006017 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006018 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006019 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006020
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006021 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306022 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006023
6024 intel_set_pipe_timings(intel_crtc);
6025
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006026 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6027 struct drm_i915_private *dev_priv = dev->dev_private;
6028
6029 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6030 I915_WRITE(CHV_CANVAS(pipe), 0);
6031 }
6032
Daniel Vetter5b18e572014-04-24 23:55:06 +02006033 i9xx_set_pipeconf(intel_crtc);
6034
Jesse Barnes89b667f2013-04-18 14:51:36 -07006035 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006036
Daniel Vettera72e4c92014-09-30 10:56:47 +02006037 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006038
Jesse Barnes89b667f2013-04-18 14:51:36 -07006039 for_each_encoder_on_crtc(dev, crtc, encoder)
6040 if (encoder->pre_pll_enable)
6041 encoder->pre_pll_enable(encoder);
6042
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006043 if (!is_dsi) {
6044 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006045 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006046 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006047 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006048 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006049
6050 for_each_encoder_on_crtc(dev, crtc, encoder)
6051 if (encoder->pre_enable)
6052 encoder->pre_enable(encoder);
6053
Jesse Barnes2dd24552013-04-25 12:55:01 -07006054 i9xx_pfit_enable(intel_crtc);
6055
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006056 intel_crtc_load_lut(crtc);
6057
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006058 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006059 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006060
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006061 assert_vblank_disabled(crtc);
6062 drm_crtc_vblank_on(crtc);
6063
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006064 for_each_encoder_on_crtc(dev, crtc, encoder)
6065 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006066}
6067
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006068static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6069{
6070 struct drm_device *dev = crtc->base.dev;
6071 struct drm_i915_private *dev_priv = dev->dev_private;
6072
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006073 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6074 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006075}
6076
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006077static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006078{
6079 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006080 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006082 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006083 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006084
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006085 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006086 return;
6087
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006088 i9xx_set_pll_dividers(intel_crtc);
6089
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006090 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306091 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006092
6093 intel_set_pipe_timings(intel_crtc);
6094
Daniel Vetter5b18e572014-04-24 23:55:06 +02006095 i9xx_set_pipeconf(intel_crtc);
6096
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006097 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006098
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006099 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006100 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006101
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006102 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006103 if (encoder->pre_enable)
6104 encoder->pre_enable(encoder);
6105
Daniel Vetterf6736a12013-06-05 13:34:30 +02006106 i9xx_enable_pll(intel_crtc);
6107
Jesse Barnes2dd24552013-04-25 12:55:01 -07006108 i9xx_pfit_enable(intel_crtc);
6109
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006110 intel_crtc_load_lut(crtc);
6111
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006112 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006113 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006114
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006115 assert_vblank_disabled(crtc);
6116 drm_crtc_vblank_on(crtc);
6117
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006118 for_each_encoder_on_crtc(dev, crtc, encoder)
6119 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006120}
6121
Daniel Vetter87476d62013-04-11 16:29:06 +02006122static void i9xx_pfit_disable(struct intel_crtc *crtc)
6123{
6124 struct drm_device *dev = crtc->base.dev;
6125 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006126
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006127 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006128 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006129
6130 assert_pipe_disabled(dev_priv, crtc->pipe);
6131
Daniel Vetter328d8e82013-05-08 10:36:31 +02006132 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6133 I915_READ(PFIT_CONTROL));
6134 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006135}
6136
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006137static void i9xx_crtc_disable(struct drm_crtc *crtc)
6138{
6139 struct drm_device *dev = crtc->dev;
6140 struct drm_i915_private *dev_priv = dev->dev_private;
6141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006142 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006143 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006144
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006145 if (WARN_ON(!intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006146 return;
6147
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006148 /*
6149 * On gen2 planes are double buffered but the pipe isn't, so we must
6150 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006151 * We also need to wait on all gmch platforms because of the
6152 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006153 */
Imre Deak564ed192014-06-13 14:54:21 +03006154 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006155
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006156 for_each_encoder_on_crtc(dev, crtc, encoder)
6157 encoder->disable(encoder);
6158
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006159 drm_crtc_vblank_off(crtc);
6160 assert_vblank_disabled(crtc);
6161
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006162 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006163
Daniel Vetter87476d62013-04-11 16:29:06 +02006164 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006165
Jesse Barnes89b667f2013-04-18 14:51:36 -07006166 for_each_encoder_on_crtc(dev, crtc, encoder)
6167 if (encoder->post_disable)
6168 encoder->post_disable(encoder);
6169
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006170 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006171 if (IS_CHERRYVIEW(dev))
6172 chv_disable_pll(dev_priv, pipe);
6173 else if (IS_VALLEYVIEW(dev))
6174 vlv_disable_pll(dev_priv, pipe);
6175 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006176 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006177 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006178
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006179 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006180 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006181
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006182 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006183 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006184
Daniel Vetterefa96242014-04-24 23:55:02 +02006185 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006186 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006187 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006188}
6189
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006190/*
6191 * turn all crtc's off, but do not adjust state
6192 * This has to be paired with a call to intel_modeset_setup_hw_state.
6193 */
Maarten Lankhorst06ea0b02015-06-01 12:50:05 +02006194int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006195{
Maarten Lankhorst06ea0b02015-06-01 12:50:05 +02006196 struct drm_mode_config *config = &dev->mode_config;
6197 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6198 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006199 struct drm_crtc *crtc;
Maarten Lankhorst06ea0b02015-06-01 12:50:05 +02006200 unsigned crtc_mask = 0;
6201 int ret = 0;
6202
6203 if (WARN_ON(!ctx))
6204 return 0;
6205
6206 lockdep_assert_held(&ctx->ww_ctx);
6207 state = drm_atomic_state_alloc(dev);
6208 if (WARN_ON(!state))
6209 return -ENOMEM;
6210
6211 state->acquire_ctx = ctx;
6212 state->allow_modeset = true;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006213
6214 for_each_crtc(dev, crtc) {
Maarten Lankhorst06ea0b02015-06-01 12:50:05 +02006215 struct drm_crtc_state *crtc_state =
6216 drm_atomic_get_crtc_state(state, crtc);
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006217
Maarten Lankhorst06ea0b02015-06-01 12:50:05 +02006218 ret = PTR_ERR_OR_ZERO(crtc_state);
6219 if (ret)
6220 goto free;
6221
6222 if (!crtc_state->active)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006223 continue;
6224
Maarten Lankhorst06ea0b02015-06-01 12:50:05 +02006225 crtc_state->active = false;
6226 crtc_mask |= 1 << drm_crtc_index(crtc);
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006227 }
Maarten Lankhorst06ea0b02015-06-01 12:50:05 +02006228
6229 if (crtc_mask) {
6230 ret = intel_set_mode(state);
6231
6232 if (!ret) {
6233 for_each_crtc(dev, crtc)
6234 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6235 crtc->state->active = true;
6236
6237 return ret;
6238 }
6239 }
6240
6241free:
6242 if (ret)
6243 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6244 drm_atomic_state_free(state);
6245 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006246}
6247
Borun Fub04c5bd2014-07-12 10:02:27 +05306248/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006249int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006250{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006251 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006252 struct drm_mode_config *config = &dev->mode_config;
6253 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006255 struct intel_crtc_state *pipe_config;
6256 struct drm_atomic_state *state;
6257 int ret;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006258
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006259 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006260 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006261
6262 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006263 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006264
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006265 /* this function should be called with drm_modeset_lock_all for now */
6266 if (WARN_ON(!ctx))
6267 return -EIO;
6268 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006269
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006270 state = drm_atomic_state_alloc(dev);
6271 if (WARN_ON(!state))
6272 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006273
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006274 state->acquire_ctx = ctx;
6275 state->allow_modeset = true;
6276
6277 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6278 if (IS_ERR(pipe_config)) {
6279 ret = PTR_ERR(pipe_config);
6280 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006281 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006282 pipe_config->base.active = enable;
6283
6284 ret = intel_set_mode(state);
6285 if (!ret)
6286 return ret;
6287
6288err:
6289 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6290 drm_atomic_state_free(state);
6291 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306292}
6293
6294/**
6295 * Sets the power management mode of the pipe and plane.
6296 */
6297void intel_crtc_update_dpms(struct drm_crtc *crtc)
6298{
6299 struct drm_device *dev = crtc->dev;
6300 struct intel_encoder *intel_encoder;
6301 bool enable = false;
6302
6303 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6304 enable |= intel_encoder->connectors_active;
6305
6306 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006307}
6308
Chris Wilsonea5b2132010-08-04 13:50:23 +01006309void intel_encoder_destroy(struct drm_encoder *encoder)
6310{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006311 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006312
Chris Wilsonea5b2132010-08-04 13:50:23 +01006313 drm_encoder_cleanup(encoder);
6314 kfree(intel_encoder);
6315}
6316
Damien Lespiau92373292013-08-08 22:28:57 +01006317/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006318 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6319 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006320static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006321{
6322 if (mode == DRM_MODE_DPMS_ON) {
6323 encoder->connectors_active = true;
6324
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006325 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006326 } else {
6327 encoder->connectors_active = false;
6328
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006329 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006330 }
6331}
6332
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006333/* Cross check the actual hw state with our own modeset state tracking (and it's
6334 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006335static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006336{
6337 if (connector->get_hw_state(connector)) {
6338 struct intel_encoder *encoder = connector->encoder;
6339 struct drm_crtc *crtc;
6340 bool encoder_enabled;
6341 enum pipe pipe;
6342
6343 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6344 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006345 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006346
Dave Airlie0e32b392014-05-02 14:02:48 +10006347 /* there is no real hw state for MST connectors */
6348 if (connector->mst_port)
6349 return;
6350
Rob Clarke2c719b2014-12-15 13:56:32 -05006351 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006352 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006353 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006354 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006355
Dave Airlie36cd7442014-05-02 13:44:18 +10006356 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006357 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006358 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006359
Dave Airlie36cd7442014-05-02 13:44:18 +10006360 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006361 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6362 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006363 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006364
Dave Airlie36cd7442014-05-02 13:44:18 +10006365 crtc = encoder->base.crtc;
6366
Matt Roper83d65732015-02-25 13:12:16 -08006367 I915_STATE_WARN(!crtc->state->enable,
6368 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006369 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6370 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006371 "encoder active on the wrong pipe\n");
6372 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006373 }
6374}
6375
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006376int intel_connector_init(struct intel_connector *connector)
6377{
6378 struct drm_connector_state *connector_state;
6379
6380 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6381 if (!connector_state)
6382 return -ENOMEM;
6383
6384 connector->base.state = connector_state;
6385 return 0;
6386}
6387
6388struct intel_connector *intel_connector_alloc(void)
6389{
6390 struct intel_connector *connector;
6391
6392 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6393 if (!connector)
6394 return NULL;
6395
6396 if (intel_connector_init(connector) < 0) {
6397 kfree(connector);
6398 return NULL;
6399 }
6400
6401 return connector;
6402}
6403
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006404/* Even simpler default implementation, if there's really no special case to
6405 * consider. */
6406void intel_connector_dpms(struct drm_connector *connector, int mode)
6407{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006408 /* All the simple cases only support two dpms states. */
6409 if (mode != DRM_MODE_DPMS_ON)
6410 mode = DRM_MODE_DPMS_OFF;
6411
6412 if (mode == connector->dpms)
6413 return;
6414
6415 connector->dpms = mode;
6416
6417 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006418 if (connector->encoder)
6419 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006420
Daniel Vetterb9805142012-08-31 17:37:33 +02006421 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006422}
6423
Daniel Vetterf0947c32012-07-02 13:10:34 +02006424/* Simple connector->get_hw_state implementation for encoders that support only
6425 * one connector and no cloning and hence the encoder state determines the state
6426 * of the connector. */
6427bool intel_connector_get_hw_state(struct intel_connector *connector)
6428{
Daniel Vetter24929352012-07-02 20:28:59 +02006429 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006430 struct intel_encoder *encoder = connector->encoder;
6431
6432 return encoder->get_hw_state(encoder, &pipe);
6433}
6434
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006435static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006436{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006437 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6438 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006439
6440 return 0;
6441}
6442
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006443static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006444 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006445{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006446 struct drm_atomic_state *state = pipe_config->base.state;
6447 struct intel_crtc *other_crtc;
6448 struct intel_crtc_state *other_crtc_state;
6449
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006450 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6451 pipe_name(pipe), pipe_config->fdi_lanes);
6452 if (pipe_config->fdi_lanes > 4) {
6453 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6454 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006455 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006456 }
6457
Paulo Zanonibafb6552013-11-02 21:07:44 -07006458 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006459 if (pipe_config->fdi_lanes > 2) {
6460 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6461 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006462 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006463 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006464 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006465 }
6466 }
6467
6468 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006469 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006470
6471 /* Ivybridge 3 pipe is really complicated */
6472 switch (pipe) {
6473 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006474 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006475 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006476 if (pipe_config->fdi_lanes <= 2)
6477 return 0;
6478
6479 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6480 other_crtc_state =
6481 intel_atomic_get_crtc_state(state, other_crtc);
6482 if (IS_ERR(other_crtc_state))
6483 return PTR_ERR(other_crtc_state);
6484
6485 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006486 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6487 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006488 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006489 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006490 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006491 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006492 if (pipe_config->fdi_lanes > 2) {
6493 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6494 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006495 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006496 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006497
6498 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6499 other_crtc_state =
6500 intel_atomic_get_crtc_state(state, other_crtc);
6501 if (IS_ERR(other_crtc_state))
6502 return PTR_ERR(other_crtc_state);
6503
6504 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006505 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006506 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006507 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006508 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006509 default:
6510 BUG();
6511 }
6512}
6513
Daniel Vettere29c22c2013-02-21 00:00:16 +01006514#define RETRY 1
6515static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006516 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006517{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006518 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006519 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006520 int lane, link_bw, fdi_dotclock, ret;
6521 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006522
Daniel Vettere29c22c2013-02-21 00:00:16 +01006523retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006524 /* FDI is a binary signal running at ~2.7GHz, encoding
6525 * each output octet as 10 bits. The actual frequency
6526 * is stored as a divider into a 100MHz clock, and the
6527 * mode pixel clock is stored in units of 1KHz.
6528 * Hence the bw of each lane in terms of the mode signal
6529 * is:
6530 */
6531 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6532
Damien Lespiau241bfc32013-09-25 16:45:37 +01006533 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006534
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006535 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006536 pipe_config->pipe_bpp);
6537
6538 pipe_config->fdi_lanes = lane;
6539
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006540 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006541 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006542
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006543 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6544 intel_crtc->pipe, pipe_config);
6545 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006546 pipe_config->pipe_bpp -= 2*3;
6547 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6548 pipe_config->pipe_bpp);
6549 needs_recompute = true;
6550 pipe_config->bw_constrained = true;
6551
6552 goto retry;
6553 }
6554
6555 if (needs_recompute)
6556 return RETRY;
6557
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006558 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006559}
6560
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006561static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6562 struct intel_crtc_state *pipe_config)
6563{
6564 if (pipe_config->pipe_bpp > 24)
6565 return false;
6566
6567 /* HSW can handle pixel rate up to cdclk? */
6568 if (IS_HASWELL(dev_priv->dev))
6569 return true;
6570
6571 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006572 * We compare against max which means we must take
6573 * the increased cdclk requirement into account when
6574 * calculating the new cdclk.
6575 *
6576 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006577 */
6578 return ilk_pipe_pixel_rate(pipe_config) <=
6579 dev_priv->max_cdclk_freq * 95 / 100;
6580}
6581
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006582static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006583 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006584{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006585 struct drm_device *dev = crtc->base.dev;
6586 struct drm_i915_private *dev_priv = dev->dev_private;
6587
Jani Nikulad330a952014-01-21 11:24:25 +02006588 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006589 hsw_crtc_supports_ips(crtc) &&
6590 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006591}
6592
Daniel Vettera43f6e02013-06-07 23:10:32 +02006593static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006594 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006595{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006596 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006597 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006598 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006599 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006600
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006601 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006602 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006603 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006604
6605 /*
6606 * Enable pixel doubling when the dot clock
6607 * is > 90% of the (display) core speed.
6608 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006609 * GDG double wide on either pipe,
6610 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006611 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006612 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006613 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006614 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006615 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006616 }
6617
Damien Lespiau241bfc32013-09-25 16:45:37 +01006618 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006619 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006620 }
Chris Wilson89749352010-09-12 18:25:19 +01006621
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006622 /*
6623 * Pipe horizontal size must be even in:
6624 * - DVO ganged mode
6625 * - LVDS dual channel mode
6626 * - Double wide pipe
6627 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006628 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006629 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6630 pipe_config->pipe_src_w &= ~1;
6631
Damien Lespiau8693a822013-05-03 18:48:11 +01006632 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6633 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006634 */
6635 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6636 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006637 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006638
Damien Lespiauf5adf942013-06-24 18:29:34 +01006639 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006640 hsw_compute_ips_config(crtc, pipe_config);
6641
Daniel Vetter877d48d2013-04-19 11:24:43 +02006642 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006643 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006644
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006645 /* FIXME: remove below call once atomic mode set is place and all crtc
6646 * related checks called from atomic_crtc_check function */
6647 ret = 0;
6648 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6649 crtc, pipe_config->base.state);
6650 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6651
6652 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006653}
6654
Ville Syrjälä1652d192015-03-31 14:12:01 +03006655static int skylake_get_display_clock_speed(struct drm_device *dev)
6656{
6657 struct drm_i915_private *dev_priv = to_i915(dev);
6658 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6659 uint32_t cdctl = I915_READ(CDCLK_CTL);
6660 uint32_t linkrate;
6661
Damien Lespiau414355a2015-06-04 18:21:31 +01006662 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006663 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006664
6665 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6666 return 540000;
6667
6668 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006669 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006670
Damien Lespiau71cd8422015-04-30 16:39:17 +01006671 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6672 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006673 /* vco 8640 */
6674 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6675 case CDCLK_FREQ_450_432:
6676 return 432000;
6677 case CDCLK_FREQ_337_308:
6678 return 308570;
6679 case CDCLK_FREQ_675_617:
6680 return 617140;
6681 default:
6682 WARN(1, "Unknown cd freq selection\n");
6683 }
6684 } else {
6685 /* vco 8100 */
6686 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6687 case CDCLK_FREQ_450_432:
6688 return 450000;
6689 case CDCLK_FREQ_337_308:
6690 return 337500;
6691 case CDCLK_FREQ_675_617:
6692 return 675000;
6693 default:
6694 WARN(1, "Unknown cd freq selection\n");
6695 }
6696 }
6697
6698 /* error case, do as if DPLL0 isn't enabled */
6699 return 24000;
6700}
6701
6702static int broadwell_get_display_clock_speed(struct drm_device *dev)
6703{
6704 struct drm_i915_private *dev_priv = dev->dev_private;
6705 uint32_t lcpll = I915_READ(LCPLL_CTL);
6706 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6707
6708 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6709 return 800000;
6710 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6711 return 450000;
6712 else if (freq == LCPLL_CLK_FREQ_450)
6713 return 450000;
6714 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6715 return 540000;
6716 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6717 return 337500;
6718 else
6719 return 675000;
6720}
6721
6722static int haswell_get_display_clock_speed(struct drm_device *dev)
6723{
6724 struct drm_i915_private *dev_priv = dev->dev_private;
6725 uint32_t lcpll = I915_READ(LCPLL_CTL);
6726 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6727
6728 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6729 return 800000;
6730 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6731 return 450000;
6732 else if (freq == LCPLL_CLK_FREQ_450)
6733 return 450000;
6734 else if (IS_HSW_ULT(dev))
6735 return 337500;
6736 else
6737 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006738}
6739
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006740static int valleyview_get_display_clock_speed(struct drm_device *dev)
6741{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006742 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006743 u32 val;
6744 int divider;
6745
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006746 if (dev_priv->hpll_freq == 0)
6747 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6748
Ville Syrjäläa5805162015-05-26 20:42:30 +03006749 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006750 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006751 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006752
6753 divider = val & DISPLAY_FREQUENCY_VALUES;
6754
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006755 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6756 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6757 "cdclk change in progress\n");
6758
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006759 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006760}
6761
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006762static int ilk_get_display_clock_speed(struct drm_device *dev)
6763{
6764 return 450000;
6765}
6766
Jesse Barnese70236a2009-09-21 10:42:27 -07006767static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006768{
Jesse Barnese70236a2009-09-21 10:42:27 -07006769 return 400000;
6770}
Jesse Barnes79e53942008-11-07 14:24:08 -08006771
Jesse Barnese70236a2009-09-21 10:42:27 -07006772static int i915_get_display_clock_speed(struct drm_device *dev)
6773{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006774 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006775}
Jesse Barnes79e53942008-11-07 14:24:08 -08006776
Jesse Barnese70236a2009-09-21 10:42:27 -07006777static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6778{
6779 return 200000;
6780}
Jesse Barnes79e53942008-11-07 14:24:08 -08006781
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006782static int pnv_get_display_clock_speed(struct drm_device *dev)
6783{
6784 u16 gcfgc = 0;
6785
6786 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6787
6788 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6789 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006790 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006791 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006792 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006793 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006794 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006795 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6796 return 200000;
6797 default:
6798 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6799 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006800 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006801 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006802 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006803 }
6804}
6805
Jesse Barnese70236a2009-09-21 10:42:27 -07006806static int i915gm_get_display_clock_speed(struct drm_device *dev)
6807{
6808 u16 gcfgc = 0;
6809
6810 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6811
6812 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006813 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006814 else {
6815 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6816 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006817 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006818 default:
6819 case GC_DISPLAY_CLOCK_190_200_MHZ:
6820 return 190000;
6821 }
6822 }
6823}
Jesse Barnes79e53942008-11-07 14:24:08 -08006824
Jesse Barnese70236a2009-09-21 10:42:27 -07006825static int i865_get_display_clock_speed(struct drm_device *dev)
6826{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006827 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006828}
6829
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006830static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006831{
6832 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006833
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006834 /*
6835 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6836 * encoding is different :(
6837 * FIXME is this the right way to detect 852GM/852GMV?
6838 */
6839 if (dev->pdev->revision == 0x1)
6840 return 133333;
6841
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006842 pci_bus_read_config_word(dev->pdev->bus,
6843 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6844
Jesse Barnese70236a2009-09-21 10:42:27 -07006845 /* Assume that the hardware is in the high speed state. This
6846 * should be the default.
6847 */
6848 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6849 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006850 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006851 case GC_CLOCK_100_200:
6852 return 200000;
6853 case GC_CLOCK_166_250:
6854 return 250000;
6855 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006856 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006857 case GC_CLOCK_133_266:
6858 case GC_CLOCK_133_266_2:
6859 case GC_CLOCK_166_266:
6860 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006861 }
6862
6863 /* Shouldn't happen */
6864 return 0;
6865}
6866
6867static int i830_get_display_clock_speed(struct drm_device *dev)
6868{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006869 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006870}
6871
Ville Syrjälä34edce22015-05-22 11:22:33 +03006872static unsigned int intel_hpll_vco(struct drm_device *dev)
6873{
6874 struct drm_i915_private *dev_priv = dev->dev_private;
6875 static const unsigned int blb_vco[8] = {
6876 [0] = 3200000,
6877 [1] = 4000000,
6878 [2] = 5333333,
6879 [3] = 4800000,
6880 [4] = 6400000,
6881 };
6882 static const unsigned int pnv_vco[8] = {
6883 [0] = 3200000,
6884 [1] = 4000000,
6885 [2] = 5333333,
6886 [3] = 4800000,
6887 [4] = 2666667,
6888 };
6889 static const unsigned int cl_vco[8] = {
6890 [0] = 3200000,
6891 [1] = 4000000,
6892 [2] = 5333333,
6893 [3] = 6400000,
6894 [4] = 3333333,
6895 [5] = 3566667,
6896 [6] = 4266667,
6897 };
6898 static const unsigned int elk_vco[8] = {
6899 [0] = 3200000,
6900 [1] = 4000000,
6901 [2] = 5333333,
6902 [3] = 4800000,
6903 };
6904 static const unsigned int ctg_vco[8] = {
6905 [0] = 3200000,
6906 [1] = 4000000,
6907 [2] = 5333333,
6908 [3] = 6400000,
6909 [4] = 2666667,
6910 [5] = 4266667,
6911 };
6912 const unsigned int *vco_table;
6913 unsigned int vco;
6914 uint8_t tmp = 0;
6915
6916 /* FIXME other chipsets? */
6917 if (IS_GM45(dev))
6918 vco_table = ctg_vco;
6919 else if (IS_G4X(dev))
6920 vco_table = elk_vco;
6921 else if (IS_CRESTLINE(dev))
6922 vco_table = cl_vco;
6923 else if (IS_PINEVIEW(dev))
6924 vco_table = pnv_vco;
6925 else if (IS_G33(dev))
6926 vco_table = blb_vco;
6927 else
6928 return 0;
6929
6930 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6931
6932 vco = vco_table[tmp & 0x7];
6933 if (vco == 0)
6934 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6935 else
6936 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6937
6938 return vco;
6939}
6940
6941static int gm45_get_display_clock_speed(struct drm_device *dev)
6942{
6943 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6944 uint16_t tmp = 0;
6945
6946 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6947
6948 cdclk_sel = (tmp >> 12) & 0x1;
6949
6950 switch (vco) {
6951 case 2666667:
6952 case 4000000:
6953 case 5333333:
6954 return cdclk_sel ? 333333 : 222222;
6955 case 3200000:
6956 return cdclk_sel ? 320000 : 228571;
6957 default:
6958 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6959 return 222222;
6960 }
6961}
6962
6963static int i965gm_get_display_clock_speed(struct drm_device *dev)
6964{
6965 static const uint8_t div_3200[] = { 16, 10, 8 };
6966 static const uint8_t div_4000[] = { 20, 12, 10 };
6967 static const uint8_t div_5333[] = { 24, 16, 14 };
6968 const uint8_t *div_table;
6969 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6970 uint16_t tmp = 0;
6971
6972 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6973
6974 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6975
6976 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6977 goto fail;
6978
6979 switch (vco) {
6980 case 3200000:
6981 div_table = div_3200;
6982 break;
6983 case 4000000:
6984 div_table = div_4000;
6985 break;
6986 case 5333333:
6987 div_table = div_5333;
6988 break;
6989 default:
6990 goto fail;
6991 }
6992
6993 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6994
6995 fail:
6996 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6997 return 200000;
6998}
6999
7000static int g33_get_display_clock_speed(struct drm_device *dev)
7001{
7002 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7003 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7004 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7005 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7006 const uint8_t *div_table;
7007 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7008 uint16_t tmp = 0;
7009
7010 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7011
7012 cdclk_sel = (tmp >> 4) & 0x7;
7013
7014 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7015 goto fail;
7016
7017 switch (vco) {
7018 case 3200000:
7019 div_table = div_3200;
7020 break;
7021 case 4000000:
7022 div_table = div_4000;
7023 break;
7024 case 4800000:
7025 div_table = div_4800;
7026 break;
7027 case 5333333:
7028 div_table = div_5333;
7029 break;
7030 default:
7031 goto fail;
7032 }
7033
7034 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7035
7036 fail:
7037 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7038 return 190476;
7039}
7040
Zhenyu Wang2c072452009-06-05 15:38:42 +08007041static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007042intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007043{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007044 while (*num > DATA_LINK_M_N_MASK ||
7045 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007046 *num >>= 1;
7047 *den >>= 1;
7048 }
7049}
7050
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007051static void compute_m_n(unsigned int m, unsigned int n,
7052 uint32_t *ret_m, uint32_t *ret_n)
7053{
7054 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7055 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7056 intel_reduce_m_n_ratio(ret_m, ret_n);
7057}
7058
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007059void
7060intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7061 int pixel_clock, int link_clock,
7062 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007063{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007064 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007065
7066 compute_m_n(bits_per_pixel * pixel_clock,
7067 link_clock * nlanes * 8,
7068 &m_n->gmch_m, &m_n->gmch_n);
7069
7070 compute_m_n(pixel_clock, link_clock,
7071 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007072}
7073
Chris Wilsona7615032011-01-12 17:04:08 +00007074static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7075{
Jani Nikulad330a952014-01-21 11:24:25 +02007076 if (i915.panel_use_ssc >= 0)
7077 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007078 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007079 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007080}
7081
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007082static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7083 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007084{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007085 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007086 struct drm_i915_private *dev_priv = dev->dev_private;
7087 int refclk;
7088
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007089 WARN_ON(!crtc_state->base.state);
7090
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007091 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007092 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007093 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007094 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007095 refclk = dev_priv->vbt.lvds_ssc_freq;
7096 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007097 } else if (!IS_GEN2(dev)) {
7098 refclk = 96000;
7099 } else {
7100 refclk = 48000;
7101 }
7102
7103 return refclk;
7104}
7105
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007106static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007107{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007108 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007109}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007110
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007111static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7112{
7113 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007114}
7115
Daniel Vetterf47709a2013-03-28 10:42:02 +01007116static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007117 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007118 intel_clock_t *reduced_clock)
7119{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007120 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007121 u32 fp, fp2 = 0;
7122
7123 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007124 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007125 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007126 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007127 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007128 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007129 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007130 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007131 }
7132
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007133 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007134
Daniel Vetterf47709a2013-03-28 10:42:02 +01007135 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007136 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007137 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007138 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007139 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007140 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007141 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007142 }
7143}
7144
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007145static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7146 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007147{
7148 u32 reg_val;
7149
7150 /*
7151 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7152 * and set it to a reasonable value instead.
7153 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007154 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007155 reg_val &= 0xffffff00;
7156 reg_val |= 0x00000030;
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 &= 0x8cffffff;
7161 reg_val = 0x8c000000;
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
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007164 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007165 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007166 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007167
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007168 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007169 reg_val &= 0x00ffffff;
7170 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007171 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007172}
7173
Daniel Vetterb5518422013-05-03 11:49:48 +02007174static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7175 struct intel_link_m_n *m_n)
7176{
7177 struct drm_device *dev = crtc->base.dev;
7178 struct drm_i915_private *dev_priv = dev->dev_private;
7179 int pipe = crtc->pipe;
7180
Daniel Vettere3b95f12013-05-03 11:49:49 +02007181 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7182 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7183 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7184 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007185}
7186
7187static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007188 struct intel_link_m_n *m_n,
7189 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007190{
7191 struct drm_device *dev = crtc->base.dev;
7192 struct drm_i915_private *dev_priv = dev->dev_private;
7193 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007194 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007195
7196 if (INTEL_INFO(dev)->gen >= 5) {
7197 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7198 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7199 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7200 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007201 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7202 * for gen < 8) and if DRRS is supported (to make sure the
7203 * registers are not unnecessarily accessed).
7204 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307205 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007206 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007207 I915_WRITE(PIPE_DATA_M2(transcoder),
7208 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7209 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7210 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7211 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7212 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007213 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007214 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7215 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7216 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7217 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007218 }
7219}
7220
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307221void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007222{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307223 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7224
7225 if (m_n == M1_N1) {
7226 dp_m_n = &crtc->config->dp_m_n;
7227 dp_m2_n2 = &crtc->config->dp_m2_n2;
7228 } else if (m_n == M2_N2) {
7229
7230 /*
7231 * M2_N2 registers are not supported. Hence m2_n2 divider value
7232 * needs to be programmed into M1_N1.
7233 */
7234 dp_m_n = &crtc->config->dp_m2_n2;
7235 } else {
7236 DRM_ERROR("Unsupported divider value\n");
7237 return;
7238 }
7239
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007240 if (crtc->config->has_pch_encoder)
7241 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007242 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307243 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007244}
7245
Ville Syrjäläd288f652014-10-28 13:20:22 +02007246static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007247 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007248{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007249 u32 dpll, dpll_md;
7250
7251 /*
7252 * Enable DPIO clock input. We should never disable the reference
7253 * clock for pipe B, since VGA hotplug / manual detection depends
7254 * on it.
7255 */
7256 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7257 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7258 /* We should never disable this, set it here for state tracking */
7259 if (crtc->pipe == PIPE_B)
7260 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7261 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007262 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007263
Ville Syrjäläd288f652014-10-28 13:20:22 +02007264 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007265 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007266 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007267}
7268
Ville Syrjäläd288f652014-10-28 13:20:22 +02007269static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007270 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007271{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007272 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007273 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007274 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007275 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007276 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007277 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007278
Ville Syrjäläa5805162015-05-26 20:42:30 +03007279 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007280
Ville Syrjäläd288f652014-10-28 13:20:22 +02007281 bestn = pipe_config->dpll.n;
7282 bestm1 = pipe_config->dpll.m1;
7283 bestm2 = pipe_config->dpll.m2;
7284 bestp1 = pipe_config->dpll.p1;
7285 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007286
Jesse Barnes89b667f2013-04-18 14:51:36 -07007287 /* See eDP HDMI DPIO driver vbios notes doc */
7288
7289 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007290 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007291 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007292
7293 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007294 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007295
7296 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007297 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007298 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007299 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007300
7301 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007302 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007303
7304 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007305 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7306 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7307 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007308 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007309
7310 /*
7311 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7312 * but we don't support that).
7313 * Note: don't use the DAC post divider as it seems unstable.
7314 */
7315 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007316 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007317
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007318 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007319 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007320
Jesse Barnes89b667f2013-04-18 14:51:36 -07007321 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007322 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007323 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7324 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007325 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007326 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007327 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007328 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007329 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007330
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007331 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007332 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007333 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007334 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007335 0x0df40000);
7336 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007337 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007338 0x0df70000);
7339 } else { /* HDMI or VGA */
7340 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007341 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007342 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007343 0x0df70000);
7344 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007345 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007346 0x0df40000);
7347 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007348
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007349 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007350 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007351 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7352 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007353 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007354 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007355
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007356 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007357 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007358}
7359
Ville Syrjäläd288f652014-10-28 13:20:22 +02007360static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007361 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007362{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007363 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007364 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7365 DPLL_VCO_ENABLE;
7366 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007367 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007368
Ville Syrjäläd288f652014-10-28 13:20:22 +02007369 pipe_config->dpll_hw_state.dpll_md =
7370 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007371}
7372
Ville Syrjäläd288f652014-10-28 13:20:22 +02007373static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007374 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007375{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007376 struct drm_device *dev = crtc->base.dev;
7377 struct drm_i915_private *dev_priv = dev->dev_private;
7378 int pipe = crtc->pipe;
7379 int dpll_reg = DPLL(crtc->pipe);
7380 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307381 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007382 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307383 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307384 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007385
Ville Syrjäläd288f652014-10-28 13:20:22 +02007386 bestn = pipe_config->dpll.n;
7387 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7388 bestm1 = pipe_config->dpll.m1;
7389 bestm2 = pipe_config->dpll.m2 >> 22;
7390 bestp1 = pipe_config->dpll.p1;
7391 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307392 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307393 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307394 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007395
7396 /*
7397 * Enable Refclk and SSC
7398 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007399 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007400 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007401
Ville Syrjäläa5805162015-05-26 20:42:30 +03007402 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007403
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007404 /* p1 and p2 divider */
7405 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7406 5 << DPIO_CHV_S1_DIV_SHIFT |
7407 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7408 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7409 1 << DPIO_CHV_K_DIV_SHIFT);
7410
7411 /* Feedback post-divider - m2 */
7412 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7413
7414 /* Feedback refclk divider - n and m1 */
7415 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7416 DPIO_CHV_M1_DIV_BY_2 |
7417 1 << DPIO_CHV_N_DIV_SHIFT);
7418
7419 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307420 if (bestm2_frac)
7421 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007422
7423 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307424 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7425 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7426 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7427 if (bestm2_frac)
7428 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7429 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007430
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307431 /* Program digital lock detect threshold */
7432 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7433 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7434 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7435 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7436 if (!bestm2_frac)
7437 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7438 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7439
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007440 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307441 if (vco == 5400000) {
7442 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7443 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7444 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7445 tribuf_calcntr = 0x9;
7446 } else if (vco <= 6200000) {
7447 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7448 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7449 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7450 tribuf_calcntr = 0x9;
7451 } else if (vco <= 6480000) {
7452 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7453 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7454 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7455 tribuf_calcntr = 0x8;
7456 } else {
7457 /* Not supported. Apply the same limits as in the max case */
7458 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7459 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7460 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7461 tribuf_calcntr = 0;
7462 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007463 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7464
Ville Syrjälä968040b2015-03-11 22:52:08 +02007465 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307466 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7467 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7468 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7469
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007470 /* AFC Recal */
7471 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7472 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7473 DPIO_AFC_RECAL);
7474
Ville Syrjäläa5805162015-05-26 20:42:30 +03007475 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007476}
7477
Ville Syrjäläd288f652014-10-28 13:20:22 +02007478/**
7479 * vlv_force_pll_on - forcibly enable just the PLL
7480 * @dev_priv: i915 private structure
7481 * @pipe: pipe PLL to enable
7482 * @dpll: PLL configuration
7483 *
7484 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7485 * in cases where we need the PLL enabled even when @pipe is not going to
7486 * be enabled.
7487 */
7488void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7489 const struct dpll *dpll)
7490{
7491 struct intel_crtc *crtc =
7492 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007493 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007494 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007495 .pixel_multiplier = 1,
7496 .dpll = *dpll,
7497 };
7498
7499 if (IS_CHERRYVIEW(dev)) {
7500 chv_update_pll(crtc, &pipe_config);
7501 chv_prepare_pll(crtc, &pipe_config);
7502 chv_enable_pll(crtc, &pipe_config);
7503 } else {
7504 vlv_update_pll(crtc, &pipe_config);
7505 vlv_prepare_pll(crtc, &pipe_config);
7506 vlv_enable_pll(crtc, &pipe_config);
7507 }
7508}
7509
7510/**
7511 * vlv_force_pll_off - forcibly disable just the PLL
7512 * @dev_priv: i915 private structure
7513 * @pipe: pipe PLL to disable
7514 *
7515 * Disable the PLL for @pipe. To be used in cases where we need
7516 * the PLL enabled even when @pipe is not going to be enabled.
7517 */
7518void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7519{
7520 if (IS_CHERRYVIEW(dev))
7521 chv_disable_pll(to_i915(dev), pipe);
7522 else
7523 vlv_disable_pll(to_i915(dev), pipe);
7524}
7525
Daniel Vetterf47709a2013-03-28 10:42:02 +01007526static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007527 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007528 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007529 int num_connectors)
7530{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007531 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007532 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007533 u32 dpll;
7534 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007535 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007536
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007537 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307538
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007539 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7540 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007541
7542 dpll = DPLL_VGA_MODE_DIS;
7543
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007544 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007545 dpll |= DPLLB_MODE_LVDS;
7546 else
7547 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007548
Daniel Vetteref1b4602013-06-01 17:17:04 +02007549 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007550 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007551 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007552 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007553
7554 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007555 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007556
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007557 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007558 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007559
7560 /* compute bitmask from p1 value */
7561 if (IS_PINEVIEW(dev))
7562 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7563 else {
7564 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7565 if (IS_G4X(dev) && reduced_clock)
7566 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7567 }
7568 switch (clock->p2) {
7569 case 5:
7570 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7571 break;
7572 case 7:
7573 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7574 break;
7575 case 10:
7576 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7577 break;
7578 case 14:
7579 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7580 break;
7581 }
7582 if (INTEL_INFO(dev)->gen >= 4)
7583 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7584
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007585 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007586 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007587 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007588 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7589 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7590 else
7591 dpll |= PLL_REF_INPUT_DREFCLK;
7592
7593 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007594 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007595
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007596 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007597 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007598 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007599 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007600 }
7601}
7602
Daniel Vetterf47709a2013-03-28 10:42:02 +01007603static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007604 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007605 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007606 int num_connectors)
7607{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007608 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007609 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007610 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007611 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007612
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007613 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307614
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007615 dpll = DPLL_VGA_MODE_DIS;
7616
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007617 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007618 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7619 } else {
7620 if (clock->p1 == 2)
7621 dpll |= PLL_P1_DIVIDE_BY_TWO;
7622 else
7623 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7624 if (clock->p2 == 4)
7625 dpll |= PLL_P2_DIVIDE_BY_4;
7626 }
7627
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007628 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007629 dpll |= DPLL_DVO_2X_MODE;
7630
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007631 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007632 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7633 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7634 else
7635 dpll |= PLL_REF_INPUT_DREFCLK;
7636
7637 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007638 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007639}
7640
Daniel Vetter8a654f32013-06-01 17:16:22 +02007641static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007642{
7643 struct drm_device *dev = intel_crtc->base.dev;
7644 struct drm_i915_private *dev_priv = dev->dev_private;
7645 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007646 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007647 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007648 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007649 uint32_t crtc_vtotal, crtc_vblank_end;
7650 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007651
7652 /* We need to be careful not to changed the adjusted mode, for otherwise
7653 * the hw state checker will get angry at the mismatch. */
7654 crtc_vtotal = adjusted_mode->crtc_vtotal;
7655 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007656
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007657 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007658 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007659 crtc_vtotal -= 1;
7660 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007661
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007662 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007663 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7664 else
7665 vsyncshift = adjusted_mode->crtc_hsync_start -
7666 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007667 if (vsyncshift < 0)
7668 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007669 }
7670
7671 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007672 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007673
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007674 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007675 (adjusted_mode->crtc_hdisplay - 1) |
7676 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007677 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007678 (adjusted_mode->crtc_hblank_start - 1) |
7679 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007680 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007681 (adjusted_mode->crtc_hsync_start - 1) |
7682 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7683
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007684 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007685 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007686 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007687 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007688 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007689 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007690 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007691 (adjusted_mode->crtc_vsync_start - 1) |
7692 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7693
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007694 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7695 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7696 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7697 * bits. */
7698 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7699 (pipe == PIPE_B || pipe == PIPE_C))
7700 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7701
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007702 /* pipesrc controls the size that is scaled from, which should
7703 * always be the user's requested size.
7704 */
7705 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007706 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7707 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007708}
7709
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007710static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007711 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007712{
7713 struct drm_device *dev = crtc->base.dev;
7714 struct drm_i915_private *dev_priv = dev->dev_private;
7715 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7716 uint32_t tmp;
7717
7718 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007719 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7720 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007721 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007722 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7723 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007724 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007725 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7726 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007727
7728 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007729 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7730 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007731 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007732 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7733 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007734 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007735 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7736 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007737
7738 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007739 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7740 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7741 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007742 }
7743
7744 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007745 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7746 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7747
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007748 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7749 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007750}
7751
Daniel Vetterf6a83282014-02-11 15:28:57 -08007752void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007753 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007754{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007755 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7756 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7757 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7758 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007759
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007760 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7761 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7762 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7763 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007764
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007765 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007766
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007767 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7768 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007769}
7770
Daniel Vetter84b046f2013-02-19 18:48:54 +01007771static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7772{
7773 struct drm_device *dev = intel_crtc->base.dev;
7774 struct drm_i915_private *dev_priv = dev->dev_private;
7775 uint32_t pipeconf;
7776
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007777 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007778
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007779 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7780 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7781 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007782
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007783 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007784 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007785
Daniel Vetterff9ce462013-04-24 14:57:17 +02007786 /* only g4x and later have fancy bpc/dither controls */
7787 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007788 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007789 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007790 pipeconf |= PIPECONF_DITHER_EN |
7791 PIPECONF_DITHER_TYPE_SP;
7792
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007793 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007794 case 18:
7795 pipeconf |= PIPECONF_6BPC;
7796 break;
7797 case 24:
7798 pipeconf |= PIPECONF_8BPC;
7799 break;
7800 case 30:
7801 pipeconf |= PIPECONF_10BPC;
7802 break;
7803 default:
7804 /* Case prevented by intel_choose_pipe_bpp_dither. */
7805 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007806 }
7807 }
7808
7809 if (HAS_PIPE_CXSR(dev)) {
7810 if (intel_crtc->lowfreq_avail) {
7811 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7812 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7813 } else {
7814 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007815 }
7816 }
7817
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007818 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007819 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007820 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007821 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7822 else
7823 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7824 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007825 pipeconf |= PIPECONF_PROGRESSIVE;
7826
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007827 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007828 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007829
Daniel Vetter84b046f2013-02-19 18:48:54 +01007830 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7831 POSTING_READ(PIPECONF(intel_crtc->pipe));
7832}
7833
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007834static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7835 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007836{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007837 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007838 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007839 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007840 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007841 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007842 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007843 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007844 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007845 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007846 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007847 struct drm_connector_state *connector_state;
7848 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007849
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007850 memset(&crtc_state->dpll_hw_state, 0,
7851 sizeof(crtc_state->dpll_hw_state));
7852
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007853 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007854 if (connector_state->crtc != &crtc->base)
7855 continue;
7856
7857 encoder = to_intel_encoder(connector_state->best_encoder);
7858
Chris Wilson5eddb702010-09-11 13:48:45 +01007859 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007860 case INTEL_OUTPUT_LVDS:
7861 is_lvds = true;
7862 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007863 case INTEL_OUTPUT_DSI:
7864 is_dsi = true;
7865 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007866 default:
7867 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007868 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007869
Eric Anholtc751ce42010-03-25 11:48:48 -07007870 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007871 }
7872
Jani Nikulaf2335332013-09-13 11:03:09 +03007873 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007874 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007875
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007876 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007877 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007878
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007879 /*
7880 * Returns a set of divisors for the desired target clock with
7881 * the given refclk, or FALSE. The returned values represent
7882 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7883 * 2) / p1 / p2.
7884 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007885 limit = intel_limit(crtc_state, refclk);
7886 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007887 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007888 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007889 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007890 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7891 return -EINVAL;
7892 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007893
Jani Nikulaf2335332013-09-13 11:03:09 +03007894 if (is_lvds && dev_priv->lvds_downclock_avail) {
7895 /*
7896 * Ensure we match the reduced clock's P to the target
7897 * clock. If the clocks don't match, we can't switch
7898 * the display clock by using the FP0/FP1. In such case
7899 * we will disable the LVDS downclock feature.
7900 */
7901 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007902 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007903 dev_priv->lvds_downclock,
7904 refclk, &clock,
7905 &reduced_clock);
7906 }
7907 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007908 crtc_state->dpll.n = clock.n;
7909 crtc_state->dpll.m1 = clock.m1;
7910 crtc_state->dpll.m2 = clock.m2;
7911 crtc_state->dpll.p1 = clock.p1;
7912 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007913 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007914
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007915 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007916 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307917 has_reduced_clock ? &reduced_clock : NULL,
7918 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007919 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007920 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007921 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007922 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007923 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007924 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007925 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007926 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007927 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007928
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007929 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007930}
7931
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007932static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007933 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007934{
7935 struct drm_device *dev = crtc->base.dev;
7936 struct drm_i915_private *dev_priv = dev->dev_private;
7937 uint32_t tmp;
7938
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007939 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7940 return;
7941
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007942 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007943 if (!(tmp & PFIT_ENABLE))
7944 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007945
Daniel Vetter06922822013-07-11 13:35:40 +02007946 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007947 if (INTEL_INFO(dev)->gen < 4) {
7948 if (crtc->pipe != PIPE_B)
7949 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007950 } else {
7951 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7952 return;
7953 }
7954
Daniel Vetter06922822013-07-11 13:35:40 +02007955 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007956 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7957 if (INTEL_INFO(dev)->gen < 5)
7958 pipe_config->gmch_pfit.lvds_border_bits =
7959 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7960}
7961
Jesse Barnesacbec812013-09-20 11:29:32 -07007962static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007963 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007964{
7965 struct drm_device *dev = crtc->base.dev;
7966 struct drm_i915_private *dev_priv = dev->dev_private;
7967 int pipe = pipe_config->cpu_transcoder;
7968 intel_clock_t clock;
7969 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007970 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007971
Shobhit Kumarf573de52014-07-30 20:32:37 +05307972 /* In case of MIPI DPLL will not even be used */
7973 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7974 return;
7975
Ville Syrjäläa5805162015-05-26 20:42:30 +03007976 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007977 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007978 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007979
7980 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7981 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7982 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7983 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7984 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7985
Ville Syrjäläf6466282013-10-14 14:50:31 +03007986 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007987
Ville Syrjäläf6466282013-10-14 14:50:31 +03007988 /* clock.dot is the fast clock */
7989 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007990}
7991
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007992static void
7993i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7994 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007995{
7996 struct drm_device *dev = crtc->base.dev;
7997 struct drm_i915_private *dev_priv = dev->dev_private;
7998 u32 val, base, offset;
7999 int pipe = crtc->pipe, plane = crtc->plane;
8000 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008001 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008002 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008003 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008004
Damien Lespiau42a7b082015-02-05 19:35:13 +00008005 val = I915_READ(DSPCNTR(plane));
8006 if (!(val & DISPLAY_PLANE_ENABLE))
8007 return;
8008
Damien Lespiaud9806c92015-01-21 14:07:19 +00008009 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008010 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008011 DRM_DEBUG_KMS("failed to alloc fb\n");
8012 return;
8013 }
8014
Damien Lespiau1b842c82015-01-21 13:50:54 +00008015 fb = &intel_fb->base;
8016
Daniel Vetter18c52472015-02-10 17:16:09 +00008017 if (INTEL_INFO(dev)->gen >= 4) {
8018 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008019 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008020 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8021 }
8022 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008023
8024 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008025 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008026 fb->pixel_format = fourcc;
8027 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008028
8029 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008030 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008031 offset = I915_READ(DSPTILEOFF(plane));
8032 else
8033 offset = I915_READ(DSPLINOFF(plane));
8034 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8035 } else {
8036 base = I915_READ(DSPADDR(plane));
8037 }
8038 plane_config->base = base;
8039
8040 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008041 fb->width = ((val >> 16) & 0xfff) + 1;
8042 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008043
8044 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008045 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008046
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008047 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008048 fb->pixel_format,
8049 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008050
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008051 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008052
Damien Lespiau2844a922015-01-20 12:51:48 +00008053 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8054 pipe_name(pipe), plane, fb->width, fb->height,
8055 fb->bits_per_pixel, base, fb->pitches[0],
8056 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008057
Damien Lespiau2d140302015-02-05 17:22:18 +00008058 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008059}
8060
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008061static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008062 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008063{
8064 struct drm_device *dev = crtc->base.dev;
8065 struct drm_i915_private *dev_priv = dev->dev_private;
8066 int pipe = pipe_config->cpu_transcoder;
8067 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8068 intel_clock_t clock;
8069 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8070 int refclk = 100000;
8071
Ville Syrjäläa5805162015-05-26 20:42:30 +03008072 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008073 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8074 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8075 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8076 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008077 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008078
8079 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8080 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8081 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8082 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8083 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8084
8085 chv_clock(refclk, &clock);
8086
8087 /* clock.dot is the fast clock */
8088 pipe_config->port_clock = clock.dot / 5;
8089}
8090
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008091static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008092 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008093{
8094 struct drm_device *dev = crtc->base.dev;
8095 struct drm_i915_private *dev_priv = dev->dev_private;
8096 uint32_t tmp;
8097
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008098 if (!intel_display_power_is_enabled(dev_priv,
8099 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008100 return false;
8101
Daniel Vettere143a212013-07-04 12:01:15 +02008102 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008103 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008104
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008105 tmp = I915_READ(PIPECONF(crtc->pipe));
8106 if (!(tmp & PIPECONF_ENABLE))
8107 return false;
8108
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008109 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8110 switch (tmp & PIPECONF_BPC_MASK) {
8111 case PIPECONF_6BPC:
8112 pipe_config->pipe_bpp = 18;
8113 break;
8114 case PIPECONF_8BPC:
8115 pipe_config->pipe_bpp = 24;
8116 break;
8117 case PIPECONF_10BPC:
8118 pipe_config->pipe_bpp = 30;
8119 break;
8120 default:
8121 break;
8122 }
8123 }
8124
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008125 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8126 pipe_config->limited_color_range = true;
8127
Ville Syrjälä282740f2013-09-04 18:30:03 +03008128 if (INTEL_INFO(dev)->gen < 4)
8129 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8130
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008131 intel_get_pipe_timings(crtc, pipe_config);
8132
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008133 i9xx_get_pfit_config(crtc, pipe_config);
8134
Daniel Vetter6c49f242013-06-06 12:45:25 +02008135 if (INTEL_INFO(dev)->gen >= 4) {
8136 tmp = I915_READ(DPLL_MD(crtc->pipe));
8137 pipe_config->pixel_multiplier =
8138 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8139 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008140 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008141 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8142 tmp = I915_READ(DPLL(crtc->pipe));
8143 pipe_config->pixel_multiplier =
8144 ((tmp & SDVO_MULTIPLIER_MASK)
8145 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8146 } else {
8147 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8148 * port and will be fixed up in the encoder->get_config
8149 * function. */
8150 pipe_config->pixel_multiplier = 1;
8151 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008152 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8153 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008154 /*
8155 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8156 * on 830. Filter it out here so that we don't
8157 * report errors due to that.
8158 */
8159 if (IS_I830(dev))
8160 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8161
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008162 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8163 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008164 } else {
8165 /* Mask out read-only status bits. */
8166 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8167 DPLL_PORTC_READY_MASK |
8168 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008169 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008170
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008171 if (IS_CHERRYVIEW(dev))
8172 chv_crtc_clock_get(crtc, pipe_config);
8173 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008174 vlv_crtc_clock_get(crtc, pipe_config);
8175 else
8176 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008177
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008178 return true;
8179}
8180
Paulo Zanonidde86e22012-12-01 12:04:25 -02008181static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008182{
8183 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008184 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008185 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008186 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008187 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008188 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008189 bool has_ck505 = false;
8190 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008191
8192 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008193 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008194 switch (encoder->type) {
8195 case INTEL_OUTPUT_LVDS:
8196 has_panel = true;
8197 has_lvds = true;
8198 break;
8199 case INTEL_OUTPUT_EDP:
8200 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008201 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008202 has_cpu_edp = true;
8203 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008204 default:
8205 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008206 }
8207 }
8208
Keith Packard99eb6a02011-09-26 14:29:12 -07008209 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008210 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008211 can_ssc = has_ck505;
8212 } else {
8213 has_ck505 = false;
8214 can_ssc = true;
8215 }
8216
Imre Deak2de69052013-05-08 13:14:04 +03008217 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8218 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008219
8220 /* Ironlake: try to setup display ref clock before DPLL
8221 * enabling. This is only under driver's control after
8222 * PCH B stepping, previous chipset stepping should be
8223 * ignoring this setting.
8224 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008225 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008226
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008227 /* As we must carefully and slowly disable/enable each source in turn,
8228 * compute the final state we want first and check if we need to
8229 * make any changes at all.
8230 */
8231 final = val;
8232 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008233 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008234 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008235 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008236 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8237
8238 final &= ~DREF_SSC_SOURCE_MASK;
8239 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8240 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008241
Keith Packard199e5d72011-09-22 12:01:57 -07008242 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008243 final |= DREF_SSC_SOURCE_ENABLE;
8244
8245 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8246 final |= DREF_SSC1_ENABLE;
8247
8248 if (has_cpu_edp) {
8249 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8250 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8251 else
8252 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8253 } else
8254 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8255 } else {
8256 final |= DREF_SSC_SOURCE_DISABLE;
8257 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8258 }
8259
8260 if (final == val)
8261 return;
8262
8263 /* Always enable nonspread source */
8264 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8265
8266 if (has_ck505)
8267 val |= DREF_NONSPREAD_CK505_ENABLE;
8268 else
8269 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8270
8271 if (has_panel) {
8272 val &= ~DREF_SSC_SOURCE_MASK;
8273 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008274
Keith Packard199e5d72011-09-22 12:01:57 -07008275 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008276 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008277 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008278 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008279 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008280 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008281
8282 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008283 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008284 POSTING_READ(PCH_DREF_CONTROL);
8285 udelay(200);
8286
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008287 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008288
8289 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008290 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008291 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008292 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008293 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008294 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008295 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008296 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008297 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008298
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008299 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008300 POSTING_READ(PCH_DREF_CONTROL);
8301 udelay(200);
8302 } else {
8303 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8304
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008305 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008306
8307 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008308 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008309
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008310 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008311 POSTING_READ(PCH_DREF_CONTROL);
8312 udelay(200);
8313
8314 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008315 val &= ~DREF_SSC_SOURCE_MASK;
8316 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008317
8318 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008319 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008320
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008321 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008322 POSTING_READ(PCH_DREF_CONTROL);
8323 udelay(200);
8324 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008325
8326 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008327}
8328
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008329static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008330{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008331 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008332
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008333 tmp = I915_READ(SOUTH_CHICKEN2);
8334 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8335 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008336
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008337 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8338 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8339 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008340
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008341 tmp = I915_READ(SOUTH_CHICKEN2);
8342 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8343 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008344
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008345 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8346 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8347 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008348}
8349
8350/* WaMPhyProgramming:hsw */
8351static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8352{
8353 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008354
8355 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8356 tmp &= ~(0xFF << 24);
8357 tmp |= (0x12 << 24);
8358 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8359
Paulo Zanonidde86e22012-12-01 12:04:25 -02008360 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8361 tmp |= (1 << 11);
8362 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8363
8364 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8365 tmp |= (1 << 11);
8366 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8367
Paulo Zanonidde86e22012-12-01 12:04:25 -02008368 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8369 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8370 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8371
8372 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8373 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8374 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8375
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008376 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8377 tmp &= ~(7 << 13);
8378 tmp |= (5 << 13);
8379 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008380
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008381 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8382 tmp &= ~(7 << 13);
8383 tmp |= (5 << 13);
8384 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008385
8386 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8387 tmp &= ~0xFF;
8388 tmp |= 0x1C;
8389 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8390
8391 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8392 tmp &= ~0xFF;
8393 tmp |= 0x1C;
8394 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8395
8396 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8397 tmp &= ~(0xFF << 16);
8398 tmp |= (0x1C << 16);
8399 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8400
8401 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8402 tmp &= ~(0xFF << 16);
8403 tmp |= (0x1C << 16);
8404 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8405
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008406 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8407 tmp |= (1 << 27);
8408 intel_sbi_write(dev_priv, 0x20C4, 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, 0x21C4, SBI_MPHY);
8411 tmp |= (1 << 27);
8412 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008413
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008414 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8415 tmp &= ~(0xF << 28);
8416 tmp |= (4 << 28);
8417 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008418
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008419 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8420 tmp &= ~(0xF << 28);
8421 tmp |= (4 << 28);
8422 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008423}
8424
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008425/* Implements 3 different sequences from BSpec chapter "Display iCLK
8426 * Programming" based on the parameters passed:
8427 * - Sequence to enable CLKOUT_DP
8428 * - Sequence to enable CLKOUT_DP without spread
8429 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8430 */
8431static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8432 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008433{
8434 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008435 uint32_t reg, tmp;
8436
8437 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8438 with_spread = true;
8439 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8440 with_fdi, "LP PCH doesn't have FDI\n"))
8441 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008442
Ville Syrjäläa5805162015-05-26 20:42:30 +03008443 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008444
8445 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8446 tmp &= ~SBI_SSCCTL_DISABLE;
8447 tmp |= SBI_SSCCTL_PATHALT;
8448 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8449
8450 udelay(24);
8451
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008452 if (with_spread) {
8453 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8454 tmp &= ~SBI_SSCCTL_PATHALT;
8455 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008456
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008457 if (with_fdi) {
8458 lpt_reset_fdi_mphy(dev_priv);
8459 lpt_program_fdi_mphy(dev_priv);
8460 }
8461 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008462
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008463 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8464 SBI_GEN0 : SBI_DBUFF0;
8465 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8466 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8467 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008468
Ville Syrjäläa5805162015-05-26 20:42:30 +03008469 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008470}
8471
Paulo Zanoni47701c32013-07-23 11:19:25 -03008472/* Sequence to disable CLKOUT_DP */
8473static void lpt_disable_clkout_dp(struct drm_device *dev)
8474{
8475 struct drm_i915_private *dev_priv = dev->dev_private;
8476 uint32_t reg, tmp;
8477
Ville Syrjäläa5805162015-05-26 20:42:30 +03008478 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008479
8480 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8481 SBI_GEN0 : SBI_DBUFF0;
8482 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8483 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8484 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8485
8486 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8487 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8488 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8489 tmp |= SBI_SSCCTL_PATHALT;
8490 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8491 udelay(32);
8492 }
8493 tmp |= SBI_SSCCTL_DISABLE;
8494 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8495 }
8496
Ville Syrjäläa5805162015-05-26 20:42:30 +03008497 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008498}
8499
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008500static void lpt_init_pch_refclk(struct drm_device *dev)
8501{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008502 struct intel_encoder *encoder;
8503 bool has_vga = false;
8504
Damien Lespiaub2784e12014-08-05 11:29:37 +01008505 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008506 switch (encoder->type) {
8507 case INTEL_OUTPUT_ANALOG:
8508 has_vga = true;
8509 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008510 default:
8511 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008512 }
8513 }
8514
Paulo Zanoni47701c32013-07-23 11:19:25 -03008515 if (has_vga)
8516 lpt_enable_clkout_dp(dev, true, true);
8517 else
8518 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008519}
8520
Paulo Zanonidde86e22012-12-01 12:04:25 -02008521/*
8522 * Initialize reference clocks when the driver loads
8523 */
8524void intel_init_pch_refclk(struct drm_device *dev)
8525{
8526 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8527 ironlake_init_pch_refclk(dev);
8528 else if (HAS_PCH_LPT(dev))
8529 lpt_init_pch_refclk(dev);
8530}
8531
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008532static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008533{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008534 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008535 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008536 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008537 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008538 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008539 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008540 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008541 bool is_lvds = false;
8542
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008543 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008544 if (connector_state->crtc != crtc_state->base.crtc)
8545 continue;
8546
8547 encoder = to_intel_encoder(connector_state->best_encoder);
8548
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008549 switch (encoder->type) {
8550 case INTEL_OUTPUT_LVDS:
8551 is_lvds = true;
8552 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008553 default:
8554 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008555 }
8556 num_connectors++;
8557 }
8558
8559 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008560 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008561 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008562 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008563 }
8564
8565 return 120000;
8566}
8567
Daniel Vetter6ff93602013-04-19 11:24:36 +02008568static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008569{
8570 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8571 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8572 int pipe = intel_crtc->pipe;
8573 uint32_t val;
8574
Daniel Vetter78114072013-06-13 00:54:57 +02008575 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008576
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008577 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008578 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008579 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008580 break;
8581 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008582 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008583 break;
8584 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008585 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008586 break;
8587 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008588 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008589 break;
8590 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008591 /* Case prevented by intel_choose_pipe_bpp_dither. */
8592 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008593 }
8594
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008595 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008596 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8597
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008598 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008599 val |= PIPECONF_INTERLACED_ILK;
8600 else
8601 val |= PIPECONF_PROGRESSIVE;
8602
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008603 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008604 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008605
Paulo Zanonic8203562012-09-12 10:06:29 -03008606 I915_WRITE(PIPECONF(pipe), val);
8607 POSTING_READ(PIPECONF(pipe));
8608}
8609
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008610/*
8611 * Set up the pipe CSC unit.
8612 *
8613 * Currently only full range RGB to limited range RGB conversion
8614 * is supported, but eventually this should handle various
8615 * RGB<->YCbCr scenarios as well.
8616 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008617static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008618{
8619 struct drm_device *dev = crtc->dev;
8620 struct drm_i915_private *dev_priv = dev->dev_private;
8621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8622 int pipe = intel_crtc->pipe;
8623 uint16_t coeff = 0x7800; /* 1.0 */
8624
8625 /*
8626 * TODO: Check what kind of values actually come out of the pipe
8627 * with these coeff/postoff values and adjust to get the best
8628 * accuracy. Perhaps we even need to take the bpc value into
8629 * consideration.
8630 */
8631
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008632 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008633 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8634
8635 /*
8636 * GY/GU and RY/RU should be the other way around according
8637 * to BSpec, but reality doesn't agree. Just set them up in
8638 * a way that results in the correct picture.
8639 */
8640 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8641 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8642
8643 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8644 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8645
8646 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8647 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8648
8649 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8650 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8651 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8652
8653 if (INTEL_INFO(dev)->gen > 6) {
8654 uint16_t postoff = 0;
8655
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008656 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008657 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008658
8659 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8660 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8661 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8662
8663 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8664 } else {
8665 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8666
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008667 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008668 mode |= CSC_BLACK_SCREEN_OFFSET;
8669
8670 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8671 }
8672}
8673
Daniel Vetter6ff93602013-04-19 11:24:36 +02008674static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008675{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008676 struct drm_device *dev = crtc->dev;
8677 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008679 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008680 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008681 uint32_t val;
8682
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008683 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008684
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008685 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008686 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8687
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008688 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008689 val |= PIPECONF_INTERLACED_ILK;
8690 else
8691 val |= PIPECONF_PROGRESSIVE;
8692
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008693 I915_WRITE(PIPECONF(cpu_transcoder), val);
8694 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008695
8696 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8697 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008698
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308699 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008700 val = 0;
8701
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008702 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008703 case 18:
8704 val |= PIPEMISC_DITHER_6_BPC;
8705 break;
8706 case 24:
8707 val |= PIPEMISC_DITHER_8_BPC;
8708 break;
8709 case 30:
8710 val |= PIPEMISC_DITHER_10_BPC;
8711 break;
8712 case 36:
8713 val |= PIPEMISC_DITHER_12_BPC;
8714 break;
8715 default:
8716 /* Case prevented by pipe_config_set_bpp. */
8717 BUG();
8718 }
8719
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008720 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008721 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8722
8723 I915_WRITE(PIPEMISC(pipe), val);
8724 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008725}
8726
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008727static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008728 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008729 intel_clock_t *clock,
8730 bool *has_reduced_clock,
8731 intel_clock_t *reduced_clock)
8732{
8733 struct drm_device *dev = crtc->dev;
8734 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008735 int refclk;
8736 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008737 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008738
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008739 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008740
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008741 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008742
8743 /*
8744 * Returns a set of divisors for the desired target clock with the given
8745 * refclk, or FALSE. The returned values represent the clock equation:
8746 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8747 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008748 limit = intel_limit(crtc_state, refclk);
8749 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008750 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008751 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008752 if (!ret)
8753 return false;
8754
8755 if (is_lvds && dev_priv->lvds_downclock_avail) {
8756 /*
8757 * Ensure we match the reduced clock's P to the target clock.
8758 * If the clocks don't match, we can't switch the display clock
8759 * by using the FP0/FP1. In such case we will disable the LVDS
8760 * downclock feature.
8761 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008762 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008763 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008764 dev_priv->lvds_downclock,
8765 refclk, clock,
8766 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008767 }
8768
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008769 return true;
8770}
8771
Paulo Zanonid4b19312012-11-29 11:29:32 -02008772int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8773{
8774 /*
8775 * Account for spread spectrum to avoid
8776 * oversubscribing the link. Max center spread
8777 * is 2.5%; use 5% for safety's sake.
8778 */
8779 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008780 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008781}
8782
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008783static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008784{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008785 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008786}
8787
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008788static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008789 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008790 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008791 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008792{
8793 struct drm_crtc *crtc = &intel_crtc->base;
8794 struct drm_device *dev = crtc->dev;
8795 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008796 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008797 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008798 struct drm_connector_state *connector_state;
8799 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008800 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008801 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008802 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008803
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008804 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008805 if (connector_state->crtc != crtc_state->base.crtc)
8806 continue;
8807
8808 encoder = to_intel_encoder(connector_state->best_encoder);
8809
8810 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008811 case INTEL_OUTPUT_LVDS:
8812 is_lvds = true;
8813 break;
8814 case INTEL_OUTPUT_SDVO:
8815 case INTEL_OUTPUT_HDMI:
8816 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008817 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008818 default:
8819 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008820 }
8821
8822 num_connectors++;
8823 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008824
Chris Wilsonc1858122010-12-03 21:35:48 +00008825 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008826 factor = 21;
8827 if (is_lvds) {
8828 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008829 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008830 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008831 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008832 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008833 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008834
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008835 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008836 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008837
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008838 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8839 *fp2 |= FP_CB_TUNE;
8840
Chris Wilson5eddb702010-09-11 13:48:45 +01008841 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008842
Eric Anholta07d6782011-03-30 13:01:08 -07008843 if (is_lvds)
8844 dpll |= DPLLB_MODE_LVDS;
8845 else
8846 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008847
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008848 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008849 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008850
8851 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008852 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008853 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008854 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008855
Eric Anholta07d6782011-03-30 13:01:08 -07008856 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008857 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008858 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008859 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008860
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008861 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008862 case 5:
8863 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8864 break;
8865 case 7:
8866 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8867 break;
8868 case 10:
8869 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8870 break;
8871 case 14:
8872 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8873 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008874 }
8875
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008876 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008877 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008878 else
8879 dpll |= PLL_REF_INPUT_DREFCLK;
8880
Daniel Vetter959e16d2013-06-05 13:34:21 +02008881 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008882}
8883
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008884static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8885 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008886{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008887 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008888 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008889 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008890 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008891 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008892 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008893
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008894 memset(&crtc_state->dpll_hw_state, 0,
8895 sizeof(crtc_state->dpll_hw_state));
8896
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008897 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008898
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008899 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8900 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8901
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008902 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008903 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008904 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008905 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8906 return -EINVAL;
8907 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008908 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008909 if (!crtc_state->clock_set) {
8910 crtc_state->dpll.n = clock.n;
8911 crtc_state->dpll.m1 = clock.m1;
8912 crtc_state->dpll.m2 = clock.m2;
8913 crtc_state->dpll.p1 = clock.p1;
8914 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008915 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008916
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008917 /* 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 +02008918 if (crtc_state->has_pch_encoder) {
8919 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008920 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008921 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008922
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008923 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008924 &fp, &reduced_clock,
8925 has_reduced_clock ? &fp2 : NULL);
8926
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008927 crtc_state->dpll_hw_state.dpll = dpll;
8928 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008929 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008930 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008931 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008932 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008933
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008934 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008935 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008936 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008937 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008938 return -EINVAL;
8939 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008940 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008941
Rodrigo Viviab585de2015-03-24 12:40:09 -07008942 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008943 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008944 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008945 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008946
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008947 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008948}
8949
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008950static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8951 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008952{
8953 struct drm_device *dev = crtc->base.dev;
8954 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008955 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008956
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008957 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8958 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8959 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8960 & ~TU_SIZE_MASK;
8961 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8962 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8963 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8964}
8965
8966static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8967 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008968 struct intel_link_m_n *m_n,
8969 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008970{
8971 struct drm_device *dev = crtc->base.dev;
8972 struct drm_i915_private *dev_priv = dev->dev_private;
8973 enum pipe pipe = crtc->pipe;
8974
8975 if (INTEL_INFO(dev)->gen >= 5) {
8976 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8977 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8978 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8979 & ~TU_SIZE_MASK;
8980 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8981 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8982 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008983 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8984 * gen < 8) and if DRRS is supported (to make sure the
8985 * registers are not unnecessarily read).
8986 */
8987 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008988 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008989 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8990 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8991 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8992 & ~TU_SIZE_MASK;
8993 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8994 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8995 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8996 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008997 } else {
8998 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8999 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9000 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9001 & ~TU_SIZE_MASK;
9002 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9003 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9004 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9005 }
9006}
9007
9008void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009009 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009010{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009011 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009012 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9013 else
9014 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009015 &pipe_config->dp_m_n,
9016 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009017}
9018
Daniel Vetter72419202013-04-04 13:28:53 +02009019static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009020 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009021{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009022 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009023 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009024}
9025
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009026static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009027 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009028{
9029 struct drm_device *dev = crtc->base.dev;
9030 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009031 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9032 uint32_t ps_ctrl = 0;
9033 int id = -1;
9034 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009035
Chandra Kondurua1b22782015-04-07 15:28:45 -07009036 /* find scaler attached to this pipe */
9037 for (i = 0; i < crtc->num_scalers; i++) {
9038 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9039 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9040 id = i;
9041 pipe_config->pch_pfit.enabled = true;
9042 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9043 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9044 break;
9045 }
9046 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009047
Chandra Kondurua1b22782015-04-07 15:28:45 -07009048 scaler_state->scaler_id = id;
9049 if (id >= 0) {
9050 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9051 } else {
9052 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009053 }
9054}
9055
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009056static void
9057skylake_get_initial_plane_config(struct intel_crtc *crtc,
9058 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009059{
9060 struct drm_device *dev = crtc->base.dev;
9061 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009062 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009063 int pipe = crtc->pipe;
9064 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009065 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009066 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009067 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009068
Damien Lespiaud9806c92015-01-21 14:07:19 +00009069 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009070 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009071 DRM_DEBUG_KMS("failed to alloc fb\n");
9072 return;
9073 }
9074
Damien Lespiau1b842c82015-01-21 13:50:54 +00009075 fb = &intel_fb->base;
9076
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009077 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009078 if (!(val & PLANE_CTL_ENABLE))
9079 goto error;
9080
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009081 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9082 fourcc = skl_format_to_fourcc(pixel_format,
9083 val & PLANE_CTL_ORDER_RGBX,
9084 val & PLANE_CTL_ALPHA_MASK);
9085 fb->pixel_format = fourcc;
9086 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9087
Damien Lespiau40f46282015-02-27 11:15:21 +00009088 tiling = val & PLANE_CTL_TILED_MASK;
9089 switch (tiling) {
9090 case PLANE_CTL_TILED_LINEAR:
9091 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9092 break;
9093 case PLANE_CTL_TILED_X:
9094 plane_config->tiling = I915_TILING_X;
9095 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9096 break;
9097 case PLANE_CTL_TILED_Y:
9098 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9099 break;
9100 case PLANE_CTL_TILED_YF:
9101 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9102 break;
9103 default:
9104 MISSING_CASE(tiling);
9105 goto error;
9106 }
9107
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009108 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9109 plane_config->base = base;
9110
9111 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9112
9113 val = I915_READ(PLANE_SIZE(pipe, 0));
9114 fb->height = ((val >> 16) & 0xfff) + 1;
9115 fb->width = ((val >> 0) & 0x1fff) + 1;
9116
9117 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009118 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9119 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009120 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9121
9122 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009123 fb->pixel_format,
9124 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009125
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009126 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009127
9128 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9129 pipe_name(pipe), fb->width, fb->height,
9130 fb->bits_per_pixel, base, fb->pitches[0],
9131 plane_config->size);
9132
Damien Lespiau2d140302015-02-05 17:22:18 +00009133 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009134 return;
9135
9136error:
9137 kfree(fb);
9138}
9139
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009140static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009141 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009142{
9143 struct drm_device *dev = crtc->base.dev;
9144 struct drm_i915_private *dev_priv = dev->dev_private;
9145 uint32_t tmp;
9146
9147 tmp = I915_READ(PF_CTL(crtc->pipe));
9148
9149 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009150 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009151 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9152 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009153
9154 /* We currently do not free assignements of panel fitters on
9155 * ivb/hsw (since we don't use the higher upscaling modes which
9156 * differentiates them) so just WARN about this case for now. */
9157 if (IS_GEN7(dev)) {
9158 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9159 PF_PIPE_SEL_IVB(crtc->pipe));
9160 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009161 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009162}
9163
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009164static void
9165ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9166 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009167{
9168 struct drm_device *dev = crtc->base.dev;
9169 struct drm_i915_private *dev_priv = dev->dev_private;
9170 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009171 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009172 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009173 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009174 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009175 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009176
Damien Lespiau42a7b082015-02-05 19:35:13 +00009177 val = I915_READ(DSPCNTR(pipe));
9178 if (!(val & DISPLAY_PLANE_ENABLE))
9179 return;
9180
Damien Lespiaud9806c92015-01-21 14:07:19 +00009181 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009182 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009183 DRM_DEBUG_KMS("failed to alloc fb\n");
9184 return;
9185 }
9186
Damien Lespiau1b842c82015-01-21 13:50:54 +00009187 fb = &intel_fb->base;
9188
Daniel Vetter18c52472015-02-10 17:16:09 +00009189 if (INTEL_INFO(dev)->gen >= 4) {
9190 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009191 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009192 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9193 }
9194 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009195
9196 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009197 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009198 fb->pixel_format = fourcc;
9199 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009200
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009201 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009202 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009203 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009204 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009205 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009206 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009207 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009208 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009209 }
9210 plane_config->base = base;
9211
9212 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009213 fb->width = ((val >> 16) & 0xfff) + 1;
9214 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009215
9216 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009217 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009218
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009219 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009220 fb->pixel_format,
9221 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009222
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009223 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009224
Damien Lespiau2844a922015-01-20 12:51:48 +00009225 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9226 pipe_name(pipe), fb->width, fb->height,
9227 fb->bits_per_pixel, base, fb->pitches[0],
9228 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009229
Damien Lespiau2d140302015-02-05 17:22:18 +00009230 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009231}
9232
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009233static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009234 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009235{
9236 struct drm_device *dev = crtc->base.dev;
9237 struct drm_i915_private *dev_priv = dev->dev_private;
9238 uint32_t tmp;
9239
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009240 if (!intel_display_power_is_enabled(dev_priv,
9241 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009242 return false;
9243
Daniel Vettere143a212013-07-04 12:01:15 +02009244 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009245 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009246
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009247 tmp = I915_READ(PIPECONF(crtc->pipe));
9248 if (!(tmp & PIPECONF_ENABLE))
9249 return false;
9250
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009251 switch (tmp & PIPECONF_BPC_MASK) {
9252 case PIPECONF_6BPC:
9253 pipe_config->pipe_bpp = 18;
9254 break;
9255 case PIPECONF_8BPC:
9256 pipe_config->pipe_bpp = 24;
9257 break;
9258 case PIPECONF_10BPC:
9259 pipe_config->pipe_bpp = 30;
9260 break;
9261 case PIPECONF_12BPC:
9262 pipe_config->pipe_bpp = 36;
9263 break;
9264 default:
9265 break;
9266 }
9267
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009268 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9269 pipe_config->limited_color_range = true;
9270
Daniel Vetterab9412b2013-05-03 11:49:46 +02009271 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009272 struct intel_shared_dpll *pll;
9273
Daniel Vetter88adfff2013-03-28 10:42:01 +01009274 pipe_config->has_pch_encoder = true;
9275
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009276 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9277 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9278 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009279
9280 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009281
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009282 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009283 pipe_config->shared_dpll =
9284 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009285 } else {
9286 tmp = I915_READ(PCH_DPLL_SEL);
9287 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9288 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9289 else
9290 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9291 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009292
9293 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9294
9295 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9296 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009297
9298 tmp = pipe_config->dpll_hw_state.dpll;
9299 pipe_config->pixel_multiplier =
9300 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9301 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009302
9303 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009304 } else {
9305 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009306 }
9307
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009308 intel_get_pipe_timings(crtc, pipe_config);
9309
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009310 ironlake_get_pfit_config(crtc, pipe_config);
9311
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009312 return true;
9313}
9314
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009315static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9316{
9317 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009318 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009319
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009320 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009321 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009322 pipe_name(crtc->pipe));
9323
Rob Clarke2c719b2014-12-15 13:56:32 -05009324 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9325 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9326 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9327 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9328 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9329 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009330 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009331 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009332 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009333 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009334 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009335 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009336 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009337 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009338 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009339
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009340 /*
9341 * In theory we can still leave IRQs enabled, as long as only the HPD
9342 * interrupts remain enabled. We used to check for that, but since it's
9343 * gen-specific and since we only disable LCPLL after we fully disable
9344 * the interrupts, the check below should be enough.
9345 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009346 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009347}
9348
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009349static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9350{
9351 struct drm_device *dev = dev_priv->dev;
9352
9353 if (IS_HASWELL(dev))
9354 return I915_READ(D_COMP_HSW);
9355 else
9356 return I915_READ(D_COMP_BDW);
9357}
9358
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009359static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9360{
9361 struct drm_device *dev = dev_priv->dev;
9362
9363 if (IS_HASWELL(dev)) {
9364 mutex_lock(&dev_priv->rps.hw_lock);
9365 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9366 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009367 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009368 mutex_unlock(&dev_priv->rps.hw_lock);
9369 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009370 I915_WRITE(D_COMP_BDW, val);
9371 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009372 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009373}
9374
9375/*
9376 * This function implements pieces of two sequences from BSpec:
9377 * - Sequence for display software to disable LCPLL
9378 * - Sequence for display software to allow package C8+
9379 * The steps implemented here are just the steps that actually touch the LCPLL
9380 * register. Callers should take care of disabling all the display engine
9381 * functions, doing the mode unset, fixing interrupts, etc.
9382 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009383static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9384 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009385{
9386 uint32_t val;
9387
9388 assert_can_disable_lcpll(dev_priv);
9389
9390 val = I915_READ(LCPLL_CTL);
9391
9392 if (switch_to_fclk) {
9393 val |= LCPLL_CD_SOURCE_FCLK;
9394 I915_WRITE(LCPLL_CTL, val);
9395
9396 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9397 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9398 DRM_ERROR("Switching to FCLK failed\n");
9399
9400 val = I915_READ(LCPLL_CTL);
9401 }
9402
9403 val |= LCPLL_PLL_DISABLE;
9404 I915_WRITE(LCPLL_CTL, val);
9405 POSTING_READ(LCPLL_CTL);
9406
9407 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9408 DRM_ERROR("LCPLL still locked\n");
9409
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009410 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009411 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009412 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009413 ndelay(100);
9414
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009415 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9416 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009417 DRM_ERROR("D_COMP RCOMP still in progress\n");
9418
9419 if (allow_power_down) {
9420 val = I915_READ(LCPLL_CTL);
9421 val |= LCPLL_POWER_DOWN_ALLOW;
9422 I915_WRITE(LCPLL_CTL, val);
9423 POSTING_READ(LCPLL_CTL);
9424 }
9425}
9426
9427/*
9428 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9429 * source.
9430 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009431static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009432{
9433 uint32_t val;
9434
9435 val = I915_READ(LCPLL_CTL);
9436
9437 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9438 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9439 return;
9440
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009441 /*
9442 * Make sure we're not on PC8 state before disabling PC8, otherwise
9443 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009444 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009445 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009446
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009447 if (val & LCPLL_POWER_DOWN_ALLOW) {
9448 val &= ~LCPLL_POWER_DOWN_ALLOW;
9449 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009450 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009451 }
9452
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009453 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009454 val |= D_COMP_COMP_FORCE;
9455 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009456 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009457
9458 val = I915_READ(LCPLL_CTL);
9459 val &= ~LCPLL_PLL_DISABLE;
9460 I915_WRITE(LCPLL_CTL, val);
9461
9462 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9463 DRM_ERROR("LCPLL not locked yet\n");
9464
9465 if (val & LCPLL_CD_SOURCE_FCLK) {
9466 val = I915_READ(LCPLL_CTL);
9467 val &= ~LCPLL_CD_SOURCE_FCLK;
9468 I915_WRITE(LCPLL_CTL, val);
9469
9470 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9471 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9472 DRM_ERROR("Switching back to LCPLL failed\n");
9473 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009474
Mika Kuoppala59bad942015-01-16 11:34:40 +02009475 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009476 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009477}
9478
Paulo Zanoni765dab672014-03-07 20:08:18 -03009479/*
9480 * Package states C8 and deeper are really deep PC states that can only be
9481 * reached when all the devices on the system allow it, so even if the graphics
9482 * device allows PC8+, it doesn't mean the system will actually get to these
9483 * states. Our driver only allows PC8+ when going into runtime PM.
9484 *
9485 * The requirements for PC8+ are that all the outputs are disabled, the power
9486 * well is disabled and most interrupts are disabled, and these are also
9487 * requirements for runtime PM. When these conditions are met, we manually do
9488 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9489 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9490 * hang the machine.
9491 *
9492 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9493 * the state of some registers, so when we come back from PC8+ we need to
9494 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9495 * need to take care of the registers kept by RC6. Notice that this happens even
9496 * if we don't put the device in PCI D3 state (which is what currently happens
9497 * because of the runtime PM support).
9498 *
9499 * For more, read "Display Sequences for Package C8" on the hardware
9500 * documentation.
9501 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009502void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009503{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009504 struct drm_device *dev = dev_priv->dev;
9505 uint32_t val;
9506
Paulo Zanonic67a4702013-08-19 13:18:09 -03009507 DRM_DEBUG_KMS("Enabling package C8+\n");
9508
Paulo Zanonic67a4702013-08-19 13:18:09 -03009509 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9510 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9511 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9512 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9513 }
9514
9515 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009516 hsw_disable_lcpll(dev_priv, true, true);
9517}
9518
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009519void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009520{
9521 struct drm_device *dev = dev_priv->dev;
9522 uint32_t val;
9523
Paulo Zanonic67a4702013-08-19 13:18:09 -03009524 DRM_DEBUG_KMS("Disabling package C8+\n");
9525
9526 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009527 lpt_init_pch_refclk(dev);
9528
9529 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9530 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9531 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9532 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9533 }
9534
9535 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009536}
9537
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009538static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309539{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009540 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309541 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009542 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309543 int req_cdclk;
9544
9545 /* see the comment in valleyview_modeset_global_resources */
9546 if (WARN_ON(max_pixclk < 0))
9547 return;
9548
9549 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9550
9551 if (req_cdclk != dev_priv->cdclk_freq)
9552 broxton_set_cdclk(dev, req_cdclk);
9553}
9554
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009555/* compute the max rate for new configuration */
9556static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9557{
9558 struct drm_device *dev = dev_priv->dev;
9559 struct intel_crtc *intel_crtc;
9560 struct drm_crtc *crtc;
9561 int max_pixel_rate = 0;
9562 int pixel_rate;
9563
9564 for_each_crtc(dev, crtc) {
9565 if (!crtc->state->enable)
9566 continue;
9567
9568 intel_crtc = to_intel_crtc(crtc);
9569 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9570
9571 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9572 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9573 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9574
9575 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9576 }
9577
9578 return max_pixel_rate;
9579}
9580
9581static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9582{
9583 struct drm_i915_private *dev_priv = dev->dev_private;
9584 uint32_t val, data;
9585 int ret;
9586
9587 if (WARN((I915_READ(LCPLL_CTL) &
9588 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9589 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9590 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9591 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9592 "trying to change cdclk frequency with cdclk not enabled\n"))
9593 return;
9594
9595 mutex_lock(&dev_priv->rps.hw_lock);
9596 ret = sandybridge_pcode_write(dev_priv,
9597 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9598 mutex_unlock(&dev_priv->rps.hw_lock);
9599 if (ret) {
9600 DRM_ERROR("failed to inform pcode about cdclk change\n");
9601 return;
9602 }
9603
9604 val = I915_READ(LCPLL_CTL);
9605 val |= LCPLL_CD_SOURCE_FCLK;
9606 I915_WRITE(LCPLL_CTL, val);
9607
9608 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9609 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9610 DRM_ERROR("Switching to FCLK failed\n");
9611
9612 val = I915_READ(LCPLL_CTL);
9613 val &= ~LCPLL_CLK_FREQ_MASK;
9614
9615 switch (cdclk) {
9616 case 450000:
9617 val |= LCPLL_CLK_FREQ_450;
9618 data = 0;
9619 break;
9620 case 540000:
9621 val |= LCPLL_CLK_FREQ_54O_BDW;
9622 data = 1;
9623 break;
9624 case 337500:
9625 val |= LCPLL_CLK_FREQ_337_5_BDW;
9626 data = 2;
9627 break;
9628 case 675000:
9629 val |= LCPLL_CLK_FREQ_675_BDW;
9630 data = 3;
9631 break;
9632 default:
9633 WARN(1, "invalid cdclk frequency\n");
9634 return;
9635 }
9636
9637 I915_WRITE(LCPLL_CTL, val);
9638
9639 val = I915_READ(LCPLL_CTL);
9640 val &= ~LCPLL_CD_SOURCE_FCLK;
9641 I915_WRITE(LCPLL_CTL, val);
9642
9643 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9644 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9645 DRM_ERROR("Switching back to LCPLL failed\n");
9646
9647 mutex_lock(&dev_priv->rps.hw_lock);
9648 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9649 mutex_unlock(&dev_priv->rps.hw_lock);
9650
9651 intel_update_cdclk(dev);
9652
9653 WARN(cdclk != dev_priv->cdclk_freq,
9654 "cdclk requested %d kHz but got %d kHz\n",
9655 cdclk, dev_priv->cdclk_freq);
9656}
9657
9658static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9659 int max_pixel_rate)
9660{
9661 int cdclk;
9662
9663 /*
9664 * FIXME should also account for plane ratio
9665 * once 64bpp pixel formats are supported.
9666 */
9667 if (max_pixel_rate > 540000)
9668 cdclk = 675000;
9669 else if (max_pixel_rate > 450000)
9670 cdclk = 540000;
9671 else if (max_pixel_rate > 337500)
9672 cdclk = 450000;
9673 else
9674 cdclk = 337500;
9675
9676 /*
9677 * FIXME move the cdclk caclulation to
9678 * compute_config() so we can fail gracegully.
9679 */
9680 if (cdclk > dev_priv->max_cdclk_freq) {
9681 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9682 cdclk, dev_priv->max_cdclk_freq);
9683 cdclk = dev_priv->max_cdclk_freq;
9684 }
9685
9686 return cdclk;
9687}
9688
9689static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9690{
9691 struct drm_i915_private *dev_priv = to_i915(state->dev);
9692 struct drm_crtc *crtc;
9693 struct drm_crtc_state *crtc_state;
9694 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9695 int cdclk, i;
9696
9697 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9698
9699 if (cdclk == dev_priv->cdclk_freq)
9700 return 0;
9701
9702 /* add all active pipes to the state */
9703 for_each_crtc(state->dev, crtc) {
9704 if (!crtc->state->enable)
9705 continue;
9706
9707 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9708 if (IS_ERR(crtc_state))
9709 return PTR_ERR(crtc_state);
9710 }
9711
9712 /* disable/enable all currently active pipes while we change cdclk */
9713 for_each_crtc_in_state(state, crtc, crtc_state, i)
9714 if (crtc_state->enable)
9715 crtc_state->mode_changed = true;
9716
9717 return 0;
9718}
9719
9720static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9721{
9722 struct drm_device *dev = state->dev;
9723 struct drm_i915_private *dev_priv = dev->dev_private;
9724 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9725 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9726
9727 if (req_cdclk != dev_priv->cdclk_freq)
9728 broadwell_set_cdclk(dev, req_cdclk);
9729}
9730
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009731static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9732 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009733{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009734 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009735 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009736
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009737 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009738
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009739 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009740}
9741
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309742static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9743 enum port port,
9744 struct intel_crtc_state *pipe_config)
9745{
9746 switch (port) {
9747 case PORT_A:
9748 pipe_config->ddi_pll_sel = SKL_DPLL0;
9749 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9750 break;
9751 case PORT_B:
9752 pipe_config->ddi_pll_sel = SKL_DPLL1;
9753 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9754 break;
9755 case PORT_C:
9756 pipe_config->ddi_pll_sel = SKL_DPLL2;
9757 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9758 break;
9759 default:
9760 DRM_ERROR("Incorrect port type\n");
9761 }
9762}
9763
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009764static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9765 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009766 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009767{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009768 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009769
9770 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9771 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9772
9773 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009774 case SKL_DPLL0:
9775 /*
9776 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9777 * of the shared DPLL framework and thus needs to be read out
9778 * separately
9779 */
9780 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9781 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9782 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009783 case SKL_DPLL1:
9784 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9785 break;
9786 case SKL_DPLL2:
9787 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9788 break;
9789 case SKL_DPLL3:
9790 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9791 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009792 }
9793}
9794
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009795static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9796 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009797 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009798{
9799 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9800
9801 switch (pipe_config->ddi_pll_sel) {
9802 case PORT_CLK_SEL_WRPLL1:
9803 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9804 break;
9805 case PORT_CLK_SEL_WRPLL2:
9806 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9807 break;
9808 }
9809}
9810
Daniel Vetter26804af2014-06-25 22:01:55 +03009811static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009812 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009813{
9814 struct drm_device *dev = crtc->base.dev;
9815 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009816 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009817 enum port port;
9818 uint32_t tmp;
9819
9820 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9821
9822 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9823
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009824 if (IS_SKYLAKE(dev))
9825 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309826 else if (IS_BROXTON(dev))
9827 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009828 else
9829 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009830
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009831 if (pipe_config->shared_dpll >= 0) {
9832 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9833
9834 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9835 &pipe_config->dpll_hw_state));
9836 }
9837
Daniel Vetter26804af2014-06-25 22:01:55 +03009838 /*
9839 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9840 * DDI E. So just check whether this pipe is wired to DDI E and whether
9841 * the PCH transcoder is on.
9842 */
Damien Lespiauca370452013-12-03 13:56:24 +00009843 if (INTEL_INFO(dev)->gen < 9 &&
9844 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009845 pipe_config->has_pch_encoder = true;
9846
9847 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9848 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9849 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9850
9851 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9852 }
9853}
9854
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009855static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009856 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009857{
9858 struct drm_device *dev = crtc->base.dev;
9859 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009860 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009861 uint32_t tmp;
9862
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009863 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009864 POWER_DOMAIN_PIPE(crtc->pipe)))
9865 return false;
9866
Daniel Vettere143a212013-07-04 12:01:15 +02009867 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009868 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9869
Daniel Vettereccb1402013-05-22 00:50:22 +02009870 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9871 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9872 enum pipe trans_edp_pipe;
9873 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9874 default:
9875 WARN(1, "unknown pipe linked to edp transcoder\n");
9876 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9877 case TRANS_DDI_EDP_INPUT_A_ON:
9878 trans_edp_pipe = PIPE_A;
9879 break;
9880 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9881 trans_edp_pipe = PIPE_B;
9882 break;
9883 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9884 trans_edp_pipe = PIPE_C;
9885 break;
9886 }
9887
9888 if (trans_edp_pipe == crtc->pipe)
9889 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9890 }
9891
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009892 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009893 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009894 return false;
9895
Daniel Vettereccb1402013-05-22 00:50:22 +02009896 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009897 if (!(tmp & PIPECONF_ENABLE))
9898 return false;
9899
Daniel Vetter26804af2014-06-25 22:01:55 +03009900 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009901
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009902 intel_get_pipe_timings(crtc, pipe_config);
9903
Chandra Kondurua1b22782015-04-07 15:28:45 -07009904 if (INTEL_INFO(dev)->gen >= 9) {
9905 skl_init_scalers(dev, crtc, pipe_config);
9906 }
9907
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009908 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009909
9910 if (INTEL_INFO(dev)->gen >= 9) {
9911 pipe_config->scaler_state.scaler_id = -1;
9912 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9913 }
9914
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009915 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009916 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009917 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009918 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009919 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009920 else
9921 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009922 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009923
Jesse Barnese59150d2014-01-07 13:30:45 -08009924 if (IS_HASWELL(dev))
9925 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9926 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009927
Clint Taylorebb69c92014-09-30 10:30:22 -07009928 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9929 pipe_config->pixel_multiplier =
9930 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9931 } else {
9932 pipe_config->pixel_multiplier = 1;
9933 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009934
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009935 return true;
9936}
9937
Chris Wilson560b85b2010-08-07 11:01:38 +01009938static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9939{
9940 struct drm_device *dev = crtc->dev;
9941 struct drm_i915_private *dev_priv = dev->dev_private;
9942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009943 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009944
Ville Syrjälädc41c152014-08-13 11:57:05 +03009945 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009946 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9947 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009948 unsigned int stride = roundup_pow_of_two(width) * 4;
9949
9950 switch (stride) {
9951 default:
9952 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9953 width, stride);
9954 stride = 256;
9955 /* fallthrough */
9956 case 256:
9957 case 512:
9958 case 1024:
9959 case 2048:
9960 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009961 }
9962
Ville Syrjälädc41c152014-08-13 11:57:05 +03009963 cntl |= CURSOR_ENABLE |
9964 CURSOR_GAMMA_ENABLE |
9965 CURSOR_FORMAT_ARGB |
9966 CURSOR_STRIDE(stride);
9967
9968 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009969 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009970
Ville Syrjälädc41c152014-08-13 11:57:05 +03009971 if (intel_crtc->cursor_cntl != 0 &&
9972 (intel_crtc->cursor_base != base ||
9973 intel_crtc->cursor_size != size ||
9974 intel_crtc->cursor_cntl != cntl)) {
9975 /* On these chipsets we can only modify the base/size/stride
9976 * whilst the cursor is disabled.
9977 */
9978 I915_WRITE(_CURACNTR, 0);
9979 POSTING_READ(_CURACNTR);
9980 intel_crtc->cursor_cntl = 0;
9981 }
9982
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009983 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009984 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009985 intel_crtc->cursor_base = base;
9986 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009987
9988 if (intel_crtc->cursor_size != size) {
9989 I915_WRITE(CURSIZE, size);
9990 intel_crtc->cursor_size = size;
9991 }
9992
Chris Wilson4b0e3332014-05-30 16:35:26 +03009993 if (intel_crtc->cursor_cntl != cntl) {
9994 I915_WRITE(_CURACNTR, cntl);
9995 POSTING_READ(_CURACNTR);
9996 intel_crtc->cursor_cntl = cntl;
9997 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009998}
9999
10000static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
10001{
10002 struct drm_device *dev = crtc->dev;
10003 struct drm_i915_private *dev_priv = dev->dev_private;
10004 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10005 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010006 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +010010007
Chris Wilson4b0e3332014-05-30 16:35:26 +030010008 cntl = 0;
10009 if (base) {
10010 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -080010011 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010012 case 64:
10013 cntl |= CURSOR_MODE_64_ARGB_AX;
10014 break;
10015 case 128:
10016 cntl |= CURSOR_MODE_128_ARGB_AX;
10017 break;
10018 case 256:
10019 cntl |= CURSOR_MODE_256_ARGB_AX;
10020 break;
10021 default:
Matt Roper3dd512f2015-02-27 10:12:00 -080010022 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010023 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010024 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010025 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010026
10027 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10028 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +010010029 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010030
Matt Roper8e7d6882015-01-21 16:35:41 -080010031 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010032 cntl |= CURSOR_ROTATE_180;
10033
Chris Wilson4b0e3332014-05-30 16:35:26 +030010034 if (intel_crtc->cursor_cntl != cntl) {
10035 I915_WRITE(CURCNTR(pipe), cntl);
10036 POSTING_READ(CURCNTR(pipe));
10037 intel_crtc->cursor_cntl = cntl;
10038 }
10039
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010040 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010041 I915_WRITE(CURBASE(pipe), base);
10042 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010043
10044 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010045}
10046
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010047/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010048static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10049 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010050{
10051 struct drm_device *dev = crtc->dev;
10052 struct drm_i915_private *dev_priv = dev->dev_private;
10053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10054 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -070010055 int x = crtc->cursor_x;
10056 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010057 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010058
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010059 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010060 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010061
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010062 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010063 base = 0;
10064
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010065 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010066 base = 0;
10067
10068 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010069 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010070 base = 0;
10071
10072 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10073 x = -x;
10074 }
10075 pos |= x << CURSOR_X_SHIFT;
10076
10077 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010078 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010079 base = 0;
10080
10081 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10082 y = -y;
10083 }
10084 pos |= y << CURSOR_Y_SHIFT;
10085
Chris Wilson4b0e3332014-05-30 16:35:26 +030010086 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010087 return;
10088
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010089 I915_WRITE(CURPOS(pipe), pos);
10090
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010091 /* ILK+ do this automagically */
10092 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010093 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010094 base += (intel_crtc->base.cursor->state->crtc_h *
10095 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010096 }
10097
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010098 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010099 i845_update_cursor(crtc, base);
10100 else
10101 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010102}
10103
Ville Syrjälädc41c152014-08-13 11:57:05 +030010104static bool cursor_size_ok(struct drm_device *dev,
10105 uint32_t width, uint32_t height)
10106{
10107 if (width == 0 || height == 0)
10108 return false;
10109
10110 /*
10111 * 845g/865g are special in that they are only limited by
10112 * the width of their cursors, the height is arbitrary up to
10113 * the precision of the register. Everything else requires
10114 * square cursors, limited to a few power-of-two sizes.
10115 */
10116 if (IS_845G(dev) || IS_I865G(dev)) {
10117 if ((width & 63) != 0)
10118 return false;
10119
10120 if (width > (IS_845G(dev) ? 64 : 512))
10121 return false;
10122
10123 if (height > 1023)
10124 return false;
10125 } else {
10126 switch (width | height) {
10127 case 256:
10128 case 128:
10129 if (IS_GEN2(dev))
10130 return false;
10131 case 64:
10132 break;
10133 default:
10134 return false;
10135 }
10136 }
10137
10138 return true;
10139}
10140
Jesse Barnes79e53942008-11-07 14:24:08 -080010141static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010142 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010143{
James Simmons72034252010-08-03 01:33:19 +010010144 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010145 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010146
James Simmons72034252010-08-03 01:33:19 +010010147 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010148 intel_crtc->lut_r[i] = red[i] >> 8;
10149 intel_crtc->lut_g[i] = green[i] >> 8;
10150 intel_crtc->lut_b[i] = blue[i] >> 8;
10151 }
10152
10153 intel_crtc_load_lut(crtc);
10154}
10155
Jesse Barnes79e53942008-11-07 14:24:08 -080010156/* VESA 640x480x72Hz mode to set on the pipe */
10157static struct drm_display_mode load_detect_mode = {
10158 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10159 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10160};
10161
Daniel Vettera8bb6812014-02-10 18:00:39 +010010162struct drm_framebuffer *
10163__intel_framebuffer_create(struct drm_device *dev,
10164 struct drm_mode_fb_cmd2 *mode_cmd,
10165 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010166{
10167 struct intel_framebuffer *intel_fb;
10168 int ret;
10169
10170 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10171 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010172 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010173 return ERR_PTR(-ENOMEM);
10174 }
10175
10176 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010177 if (ret)
10178 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010179
10180 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010181err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010182 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010183 kfree(intel_fb);
10184
10185 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010186}
10187
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010188static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010189intel_framebuffer_create(struct drm_device *dev,
10190 struct drm_mode_fb_cmd2 *mode_cmd,
10191 struct drm_i915_gem_object *obj)
10192{
10193 struct drm_framebuffer *fb;
10194 int ret;
10195
10196 ret = i915_mutex_lock_interruptible(dev);
10197 if (ret)
10198 return ERR_PTR(ret);
10199 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10200 mutex_unlock(&dev->struct_mutex);
10201
10202 return fb;
10203}
10204
Chris Wilsond2dff872011-04-19 08:36:26 +010010205static u32
10206intel_framebuffer_pitch_for_width(int width, int bpp)
10207{
10208 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10209 return ALIGN(pitch, 64);
10210}
10211
10212static u32
10213intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10214{
10215 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010216 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010217}
10218
10219static struct drm_framebuffer *
10220intel_framebuffer_create_for_mode(struct drm_device *dev,
10221 struct drm_display_mode *mode,
10222 int depth, int bpp)
10223{
10224 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010225 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010226
10227 obj = i915_gem_alloc_object(dev,
10228 intel_framebuffer_size_for_mode(mode, bpp));
10229 if (obj == NULL)
10230 return ERR_PTR(-ENOMEM);
10231
10232 mode_cmd.width = mode->hdisplay;
10233 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010234 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10235 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010236 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010237
10238 return intel_framebuffer_create(dev, &mode_cmd, obj);
10239}
10240
10241static struct drm_framebuffer *
10242mode_fits_in_fbdev(struct drm_device *dev,
10243 struct drm_display_mode *mode)
10244{
Daniel Vetter4520f532013-10-09 09:18:51 +020010245#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010246 struct drm_i915_private *dev_priv = dev->dev_private;
10247 struct drm_i915_gem_object *obj;
10248 struct drm_framebuffer *fb;
10249
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010250 if (!dev_priv->fbdev)
10251 return NULL;
10252
10253 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010254 return NULL;
10255
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010256 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010257 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010258
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010259 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010260 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10261 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010262 return NULL;
10263
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010264 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010265 return NULL;
10266
10267 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010268#else
10269 return NULL;
10270#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010271}
10272
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010273static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10274 struct drm_crtc *crtc,
10275 struct drm_display_mode *mode,
10276 struct drm_framebuffer *fb,
10277 int x, int y)
10278{
10279 struct drm_plane_state *plane_state;
10280 int hdisplay, vdisplay;
10281 int ret;
10282
10283 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10284 if (IS_ERR(plane_state))
10285 return PTR_ERR(plane_state);
10286
10287 if (mode)
10288 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10289 else
10290 hdisplay = vdisplay = 0;
10291
10292 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10293 if (ret)
10294 return ret;
10295 drm_atomic_set_fb_for_plane(plane_state, fb);
10296 plane_state->crtc_x = 0;
10297 plane_state->crtc_y = 0;
10298 plane_state->crtc_w = hdisplay;
10299 plane_state->crtc_h = vdisplay;
10300 plane_state->src_x = x << 16;
10301 plane_state->src_y = y << 16;
10302 plane_state->src_w = hdisplay << 16;
10303 plane_state->src_h = vdisplay << 16;
10304
10305 return 0;
10306}
10307
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010308bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010309 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010310 struct intel_load_detect_pipe *old,
10311 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010312{
10313 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010314 struct intel_encoder *intel_encoder =
10315 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010316 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010317 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010318 struct drm_crtc *crtc = NULL;
10319 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010320 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010321 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010322 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010323 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010324 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010325 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010326
Chris Wilsond2dff872011-04-19 08:36:26 +010010327 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010328 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010329 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010330
Rob Clark51fd3712013-11-19 12:10:12 -050010331retry:
10332 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10333 if (ret)
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020010334 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010335
Jesse Barnes79e53942008-11-07 14:24:08 -080010336 /*
10337 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010338 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010339 * - if the connector already has an assigned crtc, use it (but make
10340 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010341 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010342 * - try to find the first unused crtc that can drive this connector,
10343 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010344 */
10345
10346 /* See if we already have a CRTC for this connector */
10347 if (encoder->crtc) {
10348 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010349
Rob Clark51fd3712013-11-19 12:10:12 -050010350 ret = drm_modeset_lock(&crtc->mutex, ctx);
10351 if (ret)
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020010352 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010353 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10354 if (ret)
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020010355 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010356
Daniel Vetter24218aa2012-08-12 19:27:11 +020010357 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010358 old->load_detect_temp = false;
10359
10360 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010361 if (connector->dpms != DRM_MODE_DPMS_ON)
10362 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010363
Chris Wilson71731882011-04-19 23:10:58 +010010364 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010365 }
10366
10367 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010368 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010369 i++;
10370 if (!(encoder->possible_crtcs & (1 << i)))
10371 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010372 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010373 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010374
10375 crtc = possible_crtc;
10376 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010377 }
10378
10379 /*
10380 * If we didn't find an unused CRTC, don't use any.
10381 */
10382 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010383 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020010384 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010385 }
10386
Rob Clark51fd3712013-11-19 12:10:12 -050010387 ret = drm_modeset_lock(&crtc->mutex, ctx);
10388 if (ret)
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020010389 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010390 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10391 if (ret)
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020010392 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010393
10394 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010395 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010396 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010397 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010398
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010399 state = drm_atomic_state_alloc(dev);
10400 if (!state)
10401 return false;
10402
10403 state->acquire_ctx = ctx;
10404
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010405 connector_state = drm_atomic_get_connector_state(state, connector);
10406 if (IS_ERR(connector_state)) {
10407 ret = PTR_ERR(connector_state);
10408 goto fail;
10409 }
10410
10411 connector_state->crtc = crtc;
10412 connector_state->best_encoder = &intel_encoder->base;
10413
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010414 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10415 if (IS_ERR(crtc_state)) {
10416 ret = PTR_ERR(crtc_state);
10417 goto fail;
10418 }
10419
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010420 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010421
Chris Wilson64927112011-04-20 07:25:26 +010010422 if (!mode)
10423 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010424
Chris Wilsond2dff872011-04-19 08:36:26 +010010425 /* We need a framebuffer large enough to accommodate all accesses
10426 * that the plane may generate whilst we perform load detection.
10427 * We can not rely on the fbcon either being present (we get called
10428 * during its initialisation to detect all boot displays, or it may
10429 * not even exist) or that it is large enough to satisfy the
10430 * requested mode.
10431 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010432 fb = mode_fits_in_fbdev(dev, mode);
10433 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010434 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010435 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10436 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010437 } else
10438 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010439 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010440 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010441 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010442 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010443
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010444 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10445 if (ret)
10446 goto fail;
10447
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010448 drm_mode_copy(&crtc_state->base.mode, mode);
10449
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010450 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010451 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010452 if (old->release_fb)
10453 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010454 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010455 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010456 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010457
Jesse Barnes79e53942008-11-07 14:24:08 -080010458 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010459 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010460 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010461
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020010462fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010463 drm_atomic_state_free(state);
10464 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010465
Rob Clark51fd3712013-11-19 12:10:12 -050010466 if (ret == -EDEADLK) {
10467 drm_modeset_backoff(ctx);
10468 goto retry;
10469 }
10470
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010471 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010472}
10473
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010474void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010475 struct intel_load_detect_pipe *old,
10476 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010477{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010478 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010479 struct intel_encoder *intel_encoder =
10480 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010481 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010482 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010483 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010484 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010485 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010486 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010487 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010488
Chris Wilsond2dff872011-04-19 08:36:26 +010010489 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010490 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010491 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010492
Chris Wilson8261b192011-04-19 23:18:09 +010010493 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010494 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010495 if (!state)
10496 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010497
10498 state->acquire_ctx = ctx;
10499
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010500 connector_state = drm_atomic_get_connector_state(state, connector);
10501 if (IS_ERR(connector_state))
10502 goto fail;
10503
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010504 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10505 if (IS_ERR(crtc_state))
10506 goto fail;
10507
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010508 connector_state->best_encoder = NULL;
10509 connector_state->crtc = NULL;
10510
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010511 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010512
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010513 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10514 0, 0);
10515 if (ret)
10516 goto fail;
10517
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010518 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010519 if (ret)
10520 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010521
Daniel Vetter36206362012-12-10 20:42:17 +010010522 if (old->release_fb) {
10523 drm_framebuffer_unregister_private(old->release_fb);
10524 drm_framebuffer_unreference(old->release_fb);
10525 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010526
Chris Wilson0622a532011-04-21 09:32:11 +010010527 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010528 }
10529
Eric Anholtc751ce42010-03-25 11:48:48 -070010530 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010531 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10532 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010533
10534 return;
10535fail:
10536 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10537 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010538}
10539
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010540static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010541 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010542{
10543 struct drm_i915_private *dev_priv = dev->dev_private;
10544 u32 dpll = pipe_config->dpll_hw_state.dpll;
10545
10546 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010547 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010548 else if (HAS_PCH_SPLIT(dev))
10549 return 120000;
10550 else if (!IS_GEN2(dev))
10551 return 96000;
10552 else
10553 return 48000;
10554}
10555
Jesse Barnes79e53942008-11-07 14:24:08 -080010556/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010557static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010558 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010559{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010560 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010561 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010562 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010563 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010564 u32 fp;
10565 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010566 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010567
10568 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010569 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010570 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010571 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010572
10573 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010574 if (IS_PINEVIEW(dev)) {
10575 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10576 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010577 } else {
10578 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10579 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10580 }
10581
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010582 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010583 if (IS_PINEVIEW(dev))
10584 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10585 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010586 else
10587 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010588 DPLL_FPA01_P1_POST_DIV_SHIFT);
10589
10590 switch (dpll & DPLL_MODE_MASK) {
10591 case DPLLB_MODE_DAC_SERIAL:
10592 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10593 5 : 10;
10594 break;
10595 case DPLLB_MODE_LVDS:
10596 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10597 7 : 14;
10598 break;
10599 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010600 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010601 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010602 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010603 }
10604
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010605 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010606 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010607 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010608 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010609 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010610 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010611 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010612
10613 if (is_lvds) {
10614 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10615 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010616
10617 if (lvds & LVDS_CLKB_POWER_UP)
10618 clock.p2 = 7;
10619 else
10620 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010621 } else {
10622 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10623 clock.p1 = 2;
10624 else {
10625 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10626 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10627 }
10628 if (dpll & PLL_P2_DIVIDE_BY_4)
10629 clock.p2 = 4;
10630 else
10631 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010632 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010633
10634 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010635 }
10636
Ville Syrjälä18442d02013-09-13 16:00:08 +030010637 /*
10638 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010639 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010640 * encoder's get_config() function.
10641 */
10642 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010643}
10644
Ville Syrjälä6878da02013-09-13 15:59:11 +030010645int intel_dotclock_calculate(int link_freq,
10646 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010647{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010648 /*
10649 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010650 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010651 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010652 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010653 *
10654 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010655 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010656 */
10657
Ville Syrjälä6878da02013-09-13 15:59:11 +030010658 if (!m_n->link_n)
10659 return 0;
10660
10661 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10662}
10663
Ville Syrjälä18442d02013-09-13 16:00:08 +030010664static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010665 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010666{
10667 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010668
10669 /* read out port_clock from the DPLL */
10670 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010671
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010672 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010673 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010674 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010675 * agree once we know their relationship in the encoder's
10676 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010677 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010678 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010679 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10680 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010681}
10682
10683/** Returns the currently programmed mode of the given pipe. */
10684struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10685 struct drm_crtc *crtc)
10686{
Jesse Barnes548f2452011-02-17 10:40:53 -080010687 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010689 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010690 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010691 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010692 int htot = I915_READ(HTOTAL(cpu_transcoder));
10693 int hsync = I915_READ(HSYNC(cpu_transcoder));
10694 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10695 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010696 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010697
10698 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10699 if (!mode)
10700 return NULL;
10701
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010702 /*
10703 * Construct a pipe_config sufficient for getting the clock info
10704 * back out of crtc_clock_get.
10705 *
10706 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10707 * to use a real value here instead.
10708 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010709 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010710 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010711 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10712 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10713 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010714 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10715
Ville Syrjälä773ae032013-09-23 17:48:20 +030010716 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010717 mode->hdisplay = (htot & 0xffff) + 1;
10718 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10719 mode->hsync_start = (hsync & 0xffff) + 1;
10720 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10721 mode->vdisplay = (vtot & 0xffff) + 1;
10722 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10723 mode->vsync_start = (vsync & 0xffff) + 1;
10724 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10725
10726 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010727
10728 return mode;
10729}
10730
Jesse Barnes652c3932009-08-17 13:31:43 -070010731static void intel_decrease_pllclock(struct drm_crtc *crtc)
10732{
10733 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010734 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010736
Sonika Jindalbaff2962014-07-22 11:16:35 +053010737 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010738 return;
10739
10740 if (!dev_priv->lvds_downclock_avail)
10741 return;
10742
10743 /*
10744 * Since this is called by a timer, we should never get here in
10745 * the manual case.
10746 */
10747 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010748 int pipe = intel_crtc->pipe;
10749 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010750 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010751
Zhao Yakui44d98a62009-10-09 11:39:40 +080010752 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010753
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010754 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010755
Chris Wilson074b5e12012-05-02 12:07:06 +010010756 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010757 dpll |= DISPLAY_RATE_SELECT_FPA1;
10758 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010759 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010760 dpll = I915_READ(dpll_reg);
10761 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010762 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010763 }
10764
10765}
10766
Chris Wilsonf047e392012-07-21 12:31:41 +010010767void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010768{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010769 struct drm_i915_private *dev_priv = dev->dev_private;
10770
Chris Wilsonf62a0072014-02-21 17:55:39 +000010771 if (dev_priv->mm.busy)
10772 return;
10773
Paulo Zanoni43694d62014-03-07 20:08:08 -030010774 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010775 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010776 if (INTEL_INFO(dev)->gen >= 6)
10777 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010778 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010779}
10780
10781void intel_mark_idle(struct drm_device *dev)
10782{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010783 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010784 struct drm_crtc *crtc;
10785
Chris Wilsonf62a0072014-02-21 17:55:39 +000010786 if (!dev_priv->mm.busy)
10787 return;
10788
10789 dev_priv->mm.busy = false;
10790
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010791 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010792 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010793 continue;
10794
10795 intel_decrease_pllclock(crtc);
10796 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010797
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010798 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010799 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010800
Paulo Zanoni43694d62014-03-07 20:08:08 -030010801 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010802}
10803
Jesse Barnes79e53942008-11-07 14:24:08 -080010804static void intel_crtc_destroy(struct drm_crtc *crtc)
10805{
10806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010807 struct drm_device *dev = crtc->dev;
10808 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010809
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010810 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010811 work = intel_crtc->unpin_work;
10812 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010813 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010814
10815 if (work) {
10816 cancel_work_sync(&work->work);
10817 kfree(work);
10818 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010819
10820 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010821
Jesse Barnes79e53942008-11-07 14:24:08 -080010822 kfree(intel_crtc);
10823}
10824
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010825static void intel_unpin_work_fn(struct work_struct *__work)
10826{
10827 struct intel_unpin_work *work =
10828 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010829 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010830 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010831
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010832 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010833 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010834 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010835
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010836 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010837
10838 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010839 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010840 mutex_unlock(&dev->struct_mutex);
10841
Daniel Vetterf99d7062014-06-19 16:01:59 +020010842 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010843 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010844
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010845 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10846 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10847
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010848 kfree(work);
10849}
10850
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010851static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010852 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010853{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10855 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010856 unsigned long flags;
10857
10858 /* Ignore early vblank irqs */
10859 if (intel_crtc == NULL)
10860 return;
10861
Daniel Vetterf3260382014-09-15 14:55:23 +020010862 /*
10863 * This is called both by irq handlers and the reset code (to complete
10864 * lost pageflips) so needs the full irqsave spinlocks.
10865 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010866 spin_lock_irqsave(&dev->event_lock, flags);
10867 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010868
10869 /* Ensure we don't miss a work->pending update ... */
10870 smp_rmb();
10871
10872 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010873 spin_unlock_irqrestore(&dev->event_lock, flags);
10874 return;
10875 }
10876
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010877 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010878
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010879 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010880}
10881
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010882void intel_finish_page_flip(struct drm_device *dev, int pipe)
10883{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010884 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010885 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10886
Mario Kleiner49b14a52010-12-09 07:00:07 +010010887 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010888}
10889
10890void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10891{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010892 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010893 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10894
Mario Kleiner49b14a52010-12-09 07:00:07 +010010895 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010896}
10897
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010898/* Is 'a' after or equal to 'b'? */
10899static bool g4x_flip_count_after_eq(u32 a, u32 b)
10900{
10901 return !((a - b) & 0x80000000);
10902}
10903
10904static bool page_flip_finished(struct intel_crtc *crtc)
10905{
10906 struct drm_device *dev = crtc->base.dev;
10907 struct drm_i915_private *dev_priv = dev->dev_private;
10908
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010909 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10910 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10911 return true;
10912
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010913 /*
10914 * The relevant registers doen't exist on pre-ctg.
10915 * As the flip done interrupt doesn't trigger for mmio
10916 * flips on gmch platforms, a flip count check isn't
10917 * really needed there. But since ctg has the registers,
10918 * include it in the check anyway.
10919 */
10920 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10921 return true;
10922
10923 /*
10924 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10925 * used the same base address. In that case the mmio flip might
10926 * have completed, but the CS hasn't even executed the flip yet.
10927 *
10928 * A flip count check isn't enough as the CS might have updated
10929 * the base address just after start of vblank, but before we
10930 * managed to process the interrupt. This means we'd complete the
10931 * CS flip too soon.
10932 *
10933 * Combining both checks should get us a good enough result. It may
10934 * still happen that the CS flip has been executed, but has not
10935 * yet actually completed. But in case the base address is the same
10936 * anyway, we don't really care.
10937 */
10938 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10939 crtc->unpin_work->gtt_offset &&
10940 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10941 crtc->unpin_work->flip_count);
10942}
10943
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010944void intel_prepare_page_flip(struct drm_device *dev, int plane)
10945{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010946 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010947 struct intel_crtc *intel_crtc =
10948 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10949 unsigned long flags;
10950
Daniel Vetterf3260382014-09-15 14:55:23 +020010951
10952 /*
10953 * This is called both by irq handlers and the reset code (to complete
10954 * lost pageflips) so needs the full irqsave spinlocks.
10955 *
10956 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010957 * generate a page-flip completion irq, i.e. every modeset
10958 * is also accompanied by a spurious intel_prepare_page_flip().
10959 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010960 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010961 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010962 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010963 spin_unlock_irqrestore(&dev->event_lock, flags);
10964}
10965
Robin Schroereba905b2014-05-18 02:24:50 +020010966static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010967{
10968 /* Ensure that the work item is consistent when activating it ... */
10969 smp_wmb();
10970 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10971 /* and that it is marked active as soon as the irq could fire. */
10972 smp_wmb();
10973}
10974
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010975static int intel_gen2_queue_flip(struct drm_device *dev,
10976 struct drm_crtc *crtc,
10977 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010978 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010979 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010980 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010981{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010983 u32 flip_mask;
10984 int ret;
10985
Daniel Vetter6d90c952012-04-26 23:28:05 +020010986 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010987 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010988 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010989
10990 /* Can't queue multiple flips, so wait for the previous
10991 * one to finish before executing the next.
10992 */
10993 if (intel_crtc->plane)
10994 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10995 else
10996 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010997 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10998 intel_ring_emit(ring, MI_NOOP);
10999 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11000 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11001 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011002 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011003 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011004
11005 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011006 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011007 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011008}
11009
11010static int intel_gen3_queue_flip(struct drm_device *dev,
11011 struct drm_crtc *crtc,
11012 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011013 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011014 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011015 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011016{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011018 u32 flip_mask;
11019 int ret;
11020
Daniel Vetter6d90c952012-04-26 23:28:05 +020011021 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011022 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011023 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011024
11025 if (intel_crtc->plane)
11026 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11027 else
11028 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011029 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11030 intel_ring_emit(ring, MI_NOOP);
11031 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11032 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11033 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011034 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011035 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011036
Chris Wilsone7d841c2012-12-03 11:36:30 +000011037 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011038 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011039 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011040}
11041
11042static int intel_gen4_queue_flip(struct drm_device *dev,
11043 struct drm_crtc *crtc,
11044 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011045 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011046 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011047 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011048{
11049 struct drm_i915_private *dev_priv = dev->dev_private;
11050 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11051 uint32_t pf, pipesrc;
11052 int ret;
11053
Daniel Vetter6d90c952012-04-26 23:28:05 +020011054 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011055 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011056 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011057
11058 /* i965+ uses the linear or tiled offsets from the
11059 * Display Registers (which do not change across a page-flip)
11060 * so we need only reprogram the base address.
11061 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011062 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11063 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11064 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011065 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011066 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011067
11068 /* XXX Enabling the panel-fitter across page-flip is so far
11069 * untested on non-native modes, so ignore it for now.
11070 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11071 */
11072 pf = 0;
11073 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011074 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011075
11076 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011077 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011078 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011079}
11080
11081static int intel_gen6_queue_flip(struct drm_device *dev,
11082 struct drm_crtc *crtc,
11083 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011084 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011085 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011086 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011087{
11088 struct drm_i915_private *dev_priv = dev->dev_private;
11089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11090 uint32_t pf, pipesrc;
11091 int ret;
11092
Daniel Vetter6d90c952012-04-26 23:28:05 +020011093 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011094 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011095 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011096
Daniel Vetter6d90c952012-04-26 23:28:05 +020011097 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11098 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11099 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011100 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011101
Chris Wilson99d9acd2012-04-17 20:37:00 +010011102 /* Contrary to the suggestions in the documentation,
11103 * "Enable Panel Fitter" does not seem to be required when page
11104 * flipping with a non-native mode, and worse causes a normal
11105 * modeset to fail.
11106 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11107 */
11108 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011109 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011110 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011111
11112 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011113 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011114 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011115}
11116
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011117static int intel_gen7_queue_flip(struct drm_device *dev,
11118 struct drm_crtc *crtc,
11119 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011120 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011121 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011122 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011123{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011124 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011125 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011126 int len, ret;
11127
Robin Schroereba905b2014-05-18 02:24:50 +020011128 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011129 case PLANE_A:
11130 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11131 break;
11132 case PLANE_B:
11133 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11134 break;
11135 case PLANE_C:
11136 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11137 break;
11138 default:
11139 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011140 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011141 }
11142
Chris Wilsonffe74d72013-08-26 20:58:12 +010011143 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011144 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011145 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011146 /*
11147 * On Gen 8, SRM is now taking an extra dword to accommodate
11148 * 48bits addresses, and we need a NOOP for the batch size to
11149 * stay even.
11150 */
11151 if (IS_GEN8(dev))
11152 len += 2;
11153 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011154
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011155 /*
11156 * BSpec MI_DISPLAY_FLIP for IVB:
11157 * "The full packet must be contained within the same cache line."
11158 *
11159 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11160 * cacheline, if we ever start emitting more commands before
11161 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11162 * then do the cacheline alignment, and finally emit the
11163 * MI_DISPLAY_FLIP.
11164 */
11165 ret = intel_ring_cacheline_align(ring);
11166 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011167 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011168
Chris Wilsonffe74d72013-08-26 20:58:12 +010011169 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011170 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011171 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011172
Chris Wilsonffe74d72013-08-26 20:58:12 +010011173 /* Unmask the flip-done completion message. Note that the bspec says that
11174 * we should do this for both the BCS and RCS, and that we must not unmask
11175 * more than one flip event at any time (or ensure that one flip message
11176 * can be sent by waiting for flip-done prior to queueing new flips).
11177 * Experimentation says that BCS works despite DERRMR masking all
11178 * flip-done completion events and that unmasking all planes at once
11179 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11180 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11181 */
11182 if (ring->id == RCS) {
11183 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11184 intel_ring_emit(ring, DERRMR);
11185 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11186 DERRMR_PIPEB_PRI_FLIP_DONE |
11187 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011188 if (IS_GEN8(dev))
11189 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11190 MI_SRM_LRM_GLOBAL_GTT);
11191 else
11192 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11193 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011194 intel_ring_emit(ring, DERRMR);
11195 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011196 if (IS_GEN8(dev)) {
11197 intel_ring_emit(ring, 0);
11198 intel_ring_emit(ring, MI_NOOP);
11199 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011200 }
11201
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011202 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011203 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011204 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011205 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011206
11207 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011208 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011209 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011210}
11211
Sourab Gupta84c33a62014-06-02 16:47:17 +053011212static bool use_mmio_flip(struct intel_engine_cs *ring,
11213 struct drm_i915_gem_object *obj)
11214{
11215 /*
11216 * This is not being used for older platforms, because
11217 * non-availability of flip done interrupt forces us to use
11218 * CS flips. Older platforms derive flip done using some clever
11219 * tricks involving the flip_pending status bits and vblank irqs.
11220 * So using MMIO flips there would disrupt this mechanism.
11221 */
11222
Chris Wilson8e09bf82014-07-08 10:40:30 +010011223 if (ring == NULL)
11224 return true;
11225
Sourab Gupta84c33a62014-06-02 16:47:17 +053011226 if (INTEL_INFO(ring->dev)->gen < 5)
11227 return false;
11228
11229 if (i915.use_mmio_flip < 0)
11230 return false;
11231 else if (i915.use_mmio_flip > 0)
11232 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011233 else if (i915.enable_execlists)
11234 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011235 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011236 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011237}
11238
Damien Lespiauff944562014-11-20 14:58:16 +000011239static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11240{
11241 struct drm_device *dev = intel_crtc->base.dev;
11242 struct drm_i915_private *dev_priv = dev->dev_private;
11243 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011244 const enum pipe pipe = intel_crtc->pipe;
11245 u32 ctl, stride;
11246
11247 ctl = I915_READ(PLANE_CTL(pipe, 0));
11248 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011249 switch (fb->modifier[0]) {
11250 case DRM_FORMAT_MOD_NONE:
11251 break;
11252 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011253 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011254 break;
11255 case I915_FORMAT_MOD_Y_TILED:
11256 ctl |= PLANE_CTL_TILED_Y;
11257 break;
11258 case I915_FORMAT_MOD_Yf_TILED:
11259 ctl |= PLANE_CTL_TILED_YF;
11260 break;
11261 default:
11262 MISSING_CASE(fb->modifier[0]);
11263 }
Damien Lespiauff944562014-11-20 14:58:16 +000011264
11265 /*
11266 * The stride is either expressed as a multiple of 64 bytes chunks for
11267 * linear buffers or in number of tiles for tiled buffers.
11268 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011269 stride = fb->pitches[0] /
11270 intel_fb_stride_alignment(dev, fb->modifier[0],
11271 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011272
11273 /*
11274 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11275 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11276 */
11277 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11278 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11279
11280 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11281 POSTING_READ(PLANE_SURF(pipe, 0));
11282}
11283
11284static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011285{
11286 struct drm_device *dev = intel_crtc->base.dev;
11287 struct drm_i915_private *dev_priv = dev->dev_private;
11288 struct intel_framebuffer *intel_fb =
11289 to_intel_framebuffer(intel_crtc->base.primary->fb);
11290 struct drm_i915_gem_object *obj = intel_fb->obj;
11291 u32 dspcntr;
11292 u32 reg;
11293
Sourab Gupta84c33a62014-06-02 16:47:17 +053011294 reg = DSPCNTR(intel_crtc->plane);
11295 dspcntr = I915_READ(reg);
11296
Damien Lespiauc5d97472014-10-25 00:11:11 +010011297 if (obj->tiling_mode != I915_TILING_NONE)
11298 dspcntr |= DISPPLANE_TILED;
11299 else
11300 dspcntr &= ~DISPPLANE_TILED;
11301
Sourab Gupta84c33a62014-06-02 16:47:17 +053011302 I915_WRITE(reg, dspcntr);
11303
11304 I915_WRITE(DSPSURF(intel_crtc->plane),
11305 intel_crtc->unpin_work->gtt_offset);
11306 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011307
Damien Lespiauff944562014-11-20 14:58:16 +000011308}
11309
11310/*
11311 * XXX: This is the temporary way to update the plane registers until we get
11312 * around to using the usual plane update functions for MMIO flips
11313 */
11314static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11315{
11316 struct drm_device *dev = intel_crtc->base.dev;
11317 bool atomic_update;
11318 u32 start_vbl_count;
11319
11320 intel_mark_page_flip_active(intel_crtc);
11321
11322 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11323
11324 if (INTEL_INFO(dev)->gen >= 9)
11325 skl_do_mmio_flip(intel_crtc);
11326 else
11327 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11328 ilk_do_mmio_flip(intel_crtc);
11329
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011330 if (atomic_update)
11331 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011332}
11333
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011334static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011335{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011336 struct intel_mmio_flip *mmio_flip =
11337 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011338
Daniel Vettereed29a52015-05-21 14:21:25 +020011339 if (mmio_flip->req)
11340 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011341 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011342 false, NULL,
11343 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011344
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011345 intel_do_mmio_flip(mmio_flip->crtc);
11346
Daniel Vettereed29a52015-05-21 14:21:25 +020011347 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011348 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011349}
11350
11351static int intel_queue_mmio_flip(struct drm_device *dev,
11352 struct drm_crtc *crtc,
11353 struct drm_framebuffer *fb,
11354 struct drm_i915_gem_object *obj,
11355 struct intel_engine_cs *ring,
11356 uint32_t flags)
11357{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011358 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011359
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011360 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11361 if (mmio_flip == NULL)
11362 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011363
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011364 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011365 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011366 mmio_flip->crtc = to_intel_crtc(crtc);
11367
11368 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11369 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011370
Sourab Gupta84c33a62014-06-02 16:47:17 +053011371 return 0;
11372}
11373
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011374static int intel_default_queue_flip(struct drm_device *dev,
11375 struct drm_crtc *crtc,
11376 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011377 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011378 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011379 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011380{
11381 return -ENODEV;
11382}
11383
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011384static bool __intel_pageflip_stall_check(struct drm_device *dev,
11385 struct drm_crtc *crtc)
11386{
11387 struct drm_i915_private *dev_priv = dev->dev_private;
11388 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11389 struct intel_unpin_work *work = intel_crtc->unpin_work;
11390 u32 addr;
11391
11392 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11393 return true;
11394
11395 if (!work->enable_stall_check)
11396 return false;
11397
11398 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011399 if (work->flip_queued_req &&
11400 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011401 return false;
11402
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011403 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011404 }
11405
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011406 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011407 return false;
11408
11409 /* Potential stall - if we see that the flip has happened,
11410 * assume a missed interrupt. */
11411 if (INTEL_INFO(dev)->gen >= 4)
11412 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11413 else
11414 addr = I915_READ(DSPADDR(intel_crtc->plane));
11415
11416 /* There is a potential issue here with a false positive after a flip
11417 * to the same address. We could address this by checking for a
11418 * non-incrementing frame counter.
11419 */
11420 return addr == work->gtt_offset;
11421}
11422
11423void intel_check_page_flip(struct drm_device *dev, int pipe)
11424{
11425 struct drm_i915_private *dev_priv = dev->dev_private;
11426 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11427 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011428 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011429
Dave Gordon6c51d462015-03-06 15:34:26 +000011430 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011431
11432 if (crtc == NULL)
11433 return;
11434
Daniel Vetterf3260382014-09-15 14:55:23 +020011435 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011436 work = intel_crtc->unpin_work;
11437 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011438 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011439 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011440 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011441 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011442 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011443 if (work != NULL &&
11444 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11445 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011446 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011447}
11448
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011449static int intel_crtc_page_flip(struct drm_crtc *crtc,
11450 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011451 struct drm_pending_vblank_event *event,
11452 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011453{
11454 struct drm_device *dev = crtc->dev;
11455 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011456 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011457 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011459 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011460 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011461 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011462 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011463 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011464 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011465
Matt Roper2ff8fde2014-07-08 07:50:07 -070011466 /*
11467 * drm_mode_page_flip_ioctl() should already catch this, but double
11468 * check to be safe. In the future we may enable pageflipping from
11469 * a disabled primary plane.
11470 */
11471 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11472 return -EBUSY;
11473
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011474 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011475 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011476 return -EINVAL;
11477
11478 /*
11479 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11480 * Note that pitch changes could also affect these register.
11481 */
11482 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011483 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11484 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011485 return -EINVAL;
11486
Chris Wilsonf900db42014-02-20 09:26:13 +000011487 if (i915_terminally_wedged(&dev_priv->gpu_error))
11488 goto out_hang;
11489
Daniel Vetterb14c5672013-09-19 12:18:32 +020011490 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011491 if (work == NULL)
11492 return -ENOMEM;
11493
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011494 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011495 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011496 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011497 INIT_WORK(&work->work, intel_unpin_work_fn);
11498
Daniel Vetter87b6b102014-05-15 15:33:46 +020011499 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011500 if (ret)
11501 goto free_work;
11502
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011503 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011504 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011505 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011506 /* Before declaring the flip queue wedged, check if
11507 * the hardware completed the operation behind our backs.
11508 */
11509 if (__intel_pageflip_stall_check(dev, crtc)) {
11510 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11511 page_flip_completed(intel_crtc);
11512 } else {
11513 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011514 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011515
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011516 drm_crtc_vblank_put(crtc);
11517 kfree(work);
11518 return -EBUSY;
11519 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011520 }
11521 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011522 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011523
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011524 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11525 flush_workqueue(dev_priv->wq);
11526
Jesse Barnes75dfca82010-02-10 15:09:44 -080011527 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011528 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011529 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011530
Matt Roperf4510a22014-04-01 15:22:40 -070011531 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011532 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011533
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011534 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011535
Chris Wilson89ed88b2015-02-16 14:31:49 +000011536 ret = i915_mutex_lock_interruptible(dev);
11537 if (ret)
11538 goto cleanup;
11539
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011540 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011541 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011542
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011543 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011544 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011545
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011546 if (IS_VALLEYVIEW(dev)) {
11547 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011548 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011549 /* vlv: DISPLAY_FLIP fails to change tiling */
11550 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011551 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011552 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011553 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011554 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011555 if (ring == NULL || ring->id != RCS)
11556 ring = &dev_priv->ring[BCS];
11557 } else {
11558 ring = &dev_priv->ring[RCS];
11559 }
11560
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011561 mmio_flip = use_mmio_flip(ring, obj);
11562
11563 /* When using CS flips, we want to emit semaphores between rings.
11564 * However, when using mmio flips we will create a task to do the
11565 * synchronisation, so all we want here is to pin the framebuffer
11566 * into the display plane and skip any waits.
11567 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011568 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011569 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011570 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011571 if (ret)
11572 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011573
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011574 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11575 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011576
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011577 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011578 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11579 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011580 if (ret)
11581 goto cleanup_unpin;
11582
John Harrisonf06cc1b2014-11-24 18:49:37 +000011583 i915_gem_request_assign(&work->flip_queued_req,
11584 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011585 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011586 if (obj->last_write_req) {
11587 ret = i915_gem_check_olr(obj->last_write_req);
11588 if (ret)
11589 goto cleanup_unpin;
11590 }
11591
Sourab Gupta84c33a62014-06-02 16:47:17 +053011592 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011593 page_flip_flags);
11594 if (ret)
11595 goto cleanup_unpin;
11596
John Harrisonf06cc1b2014-11-24 18:49:37 +000011597 i915_gem_request_assign(&work->flip_queued_req,
11598 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011599 }
11600
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011601 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011602 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011603
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011604 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011605 INTEL_FRONTBUFFER_PRIMARY(pipe));
11606
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011607 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011608 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011609 mutex_unlock(&dev->struct_mutex);
11610
Jesse Barnese5510fa2010-07-01 16:48:37 -070011611 trace_i915_flip_request(intel_crtc->plane, obj);
11612
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011613 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011614
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011615cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011616 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011617cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011618 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011619 mutex_unlock(&dev->struct_mutex);
11620cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011621 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011622 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011623
Chris Wilson89ed88b2015-02-16 14:31:49 +000011624 drm_gem_object_unreference_unlocked(&obj->base);
11625 drm_framebuffer_unreference(work->old_fb);
11626
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011627 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011628 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011629 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011630
Daniel Vetter87b6b102014-05-15 15:33:46 +020011631 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011632free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011633 kfree(work);
11634
Chris Wilsonf900db42014-02-20 09:26:13 +000011635 if (ret == -EIO) {
11636out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011637 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011638 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011639 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011640 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011641 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011642 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011643 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011644 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011645}
11646
Jani Nikula65b38e02015-04-13 11:26:56 +030011647static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011648 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11649 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011650 .atomic_begin = intel_begin_crtc_commit,
11651 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011652};
11653
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011654/* Transitional helper to copy current connector/encoder state to
11655 * connector->state. This is needed so that code that is partially
11656 * converted to atomic does the right thing.
11657 */
11658static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11659{
11660 struct intel_connector *connector;
11661
11662 for_each_intel_connector(dev, connector) {
11663 if (connector->base.encoder) {
11664 connector->base.state->best_encoder =
11665 connector->base.encoder;
11666 connector->base.state->crtc =
11667 connector->base.encoder->crtc;
11668 } else {
11669 connector->base.state->best_encoder = NULL;
11670 connector->base.state->crtc = NULL;
11671 }
11672 }
11673}
11674
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011675static void
Robin Schroereba905b2014-05-18 02:24:50 +020011676connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011677 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011678{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011679 int bpp = pipe_config->pipe_bpp;
11680
11681 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11682 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011683 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011684
11685 /* Don't use an invalid EDID bpc value */
11686 if (connector->base.display_info.bpc &&
11687 connector->base.display_info.bpc * 3 < bpp) {
11688 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11689 bpp, connector->base.display_info.bpc*3);
11690 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11691 }
11692
11693 /* Clamp bpp to 8 on screens without EDID 1.4 */
11694 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11695 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11696 bpp);
11697 pipe_config->pipe_bpp = 24;
11698 }
11699}
11700
11701static int
11702compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011703 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011704{
11705 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011706 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011707 struct drm_connector *connector;
11708 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011709 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011710
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011711 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011712 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011713 else if (INTEL_INFO(dev)->gen >= 5)
11714 bpp = 12*3;
11715 else
11716 bpp = 8*3;
11717
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011718
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011719 pipe_config->pipe_bpp = bpp;
11720
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011721 state = pipe_config->base.state;
11722
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011723 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011724 for_each_connector_in_state(state, connector, connector_state, i) {
11725 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011726 continue;
11727
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011728 connected_sink_compute_bpp(to_intel_connector(connector),
11729 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011730 }
11731
11732 return bpp;
11733}
11734
Daniel Vetter644db712013-09-19 14:53:58 +020011735static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11736{
11737 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11738 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011739 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011740 mode->crtc_hdisplay, mode->crtc_hsync_start,
11741 mode->crtc_hsync_end, mode->crtc_htotal,
11742 mode->crtc_vdisplay, mode->crtc_vsync_start,
11743 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11744}
11745
Daniel Vetterc0b03412013-05-28 12:05:54 +020011746static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011747 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011748 const char *context)
11749{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011750 struct drm_device *dev = crtc->base.dev;
11751 struct drm_plane *plane;
11752 struct intel_plane *intel_plane;
11753 struct intel_plane_state *state;
11754 struct drm_framebuffer *fb;
11755
11756 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11757 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011758
11759 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11760 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11761 pipe_config->pipe_bpp, pipe_config->dither);
11762 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11763 pipe_config->has_pch_encoder,
11764 pipe_config->fdi_lanes,
11765 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11766 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11767 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011768 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11769 pipe_config->has_dp_encoder,
11770 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11771 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11772 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011773
11774 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11775 pipe_config->has_dp_encoder,
11776 pipe_config->dp_m2_n2.gmch_m,
11777 pipe_config->dp_m2_n2.gmch_n,
11778 pipe_config->dp_m2_n2.link_m,
11779 pipe_config->dp_m2_n2.link_n,
11780 pipe_config->dp_m2_n2.tu);
11781
Daniel Vetter55072d12014-11-20 16:10:28 +010011782 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11783 pipe_config->has_audio,
11784 pipe_config->has_infoframe);
11785
Daniel Vetterc0b03412013-05-28 12:05:54 +020011786 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011787 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011788 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011789 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11790 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011791 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011792 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11793 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011794 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11795 crtc->num_scalers,
11796 pipe_config->scaler_state.scaler_users,
11797 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011798 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11799 pipe_config->gmch_pfit.control,
11800 pipe_config->gmch_pfit.pgm_ratios,
11801 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011802 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011803 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011804 pipe_config->pch_pfit.size,
11805 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011806 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011807 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011808
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011809 if (IS_BROXTON(dev)) {
11810 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11811 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11812 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11813 pipe_config->ddi_pll_sel,
11814 pipe_config->dpll_hw_state.ebb0,
11815 pipe_config->dpll_hw_state.pll0,
11816 pipe_config->dpll_hw_state.pll1,
11817 pipe_config->dpll_hw_state.pll2,
11818 pipe_config->dpll_hw_state.pll3,
11819 pipe_config->dpll_hw_state.pll6,
11820 pipe_config->dpll_hw_state.pll8,
11821 pipe_config->dpll_hw_state.pcsdw12);
11822 } else if (IS_SKYLAKE(dev)) {
11823 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11824 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11825 pipe_config->ddi_pll_sel,
11826 pipe_config->dpll_hw_state.ctrl1,
11827 pipe_config->dpll_hw_state.cfgcr1,
11828 pipe_config->dpll_hw_state.cfgcr2);
11829 } else if (HAS_DDI(dev)) {
11830 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11831 pipe_config->ddi_pll_sel,
11832 pipe_config->dpll_hw_state.wrpll);
11833 } else {
11834 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11835 "fp0: 0x%x, fp1: 0x%x\n",
11836 pipe_config->dpll_hw_state.dpll,
11837 pipe_config->dpll_hw_state.dpll_md,
11838 pipe_config->dpll_hw_state.fp0,
11839 pipe_config->dpll_hw_state.fp1);
11840 }
11841
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011842 DRM_DEBUG_KMS("planes on this crtc\n");
11843 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11844 intel_plane = to_intel_plane(plane);
11845 if (intel_plane->pipe != crtc->pipe)
11846 continue;
11847
11848 state = to_intel_plane_state(plane->state);
11849 fb = state->base.fb;
11850 if (!fb) {
11851 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11852 "disabled, scaler_id = %d\n",
11853 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11854 plane->base.id, intel_plane->pipe,
11855 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11856 drm_plane_index(plane), state->scaler_id);
11857 continue;
11858 }
11859
11860 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11861 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11862 plane->base.id, intel_plane->pipe,
11863 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11864 drm_plane_index(plane));
11865 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11866 fb->base.id, fb->width, fb->height, fb->pixel_format);
11867 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11868 state->scaler_id,
11869 state->src.x1 >> 16, state->src.y1 >> 16,
11870 drm_rect_width(&state->src) >> 16,
11871 drm_rect_height(&state->src) >> 16,
11872 state->dst.x1, state->dst.y1,
11873 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11874 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011875}
11876
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011877static bool encoders_cloneable(const struct intel_encoder *a,
11878 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011879{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011880 /* masks could be asymmetric, so check both ways */
11881 return a == b || (a->cloneable & (1 << b->type) &&
11882 b->cloneable & (1 << a->type));
11883}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011884
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011885static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11886 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011887 struct intel_encoder *encoder)
11888{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011889 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011890 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011891 struct drm_connector_state *connector_state;
11892 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011893
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011894 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011895 if (connector_state->crtc != &crtc->base)
11896 continue;
11897
11898 source_encoder =
11899 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011900 if (!encoders_cloneable(encoder, source_encoder))
11901 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011902 }
11903
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011904 return true;
11905}
11906
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011907static bool check_encoder_cloning(struct drm_atomic_state *state,
11908 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011909{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011910 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011911 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011912 struct drm_connector_state *connector_state;
11913 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011914
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011915 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011916 if (connector_state->crtc != &crtc->base)
11917 continue;
11918
11919 encoder = to_intel_encoder(connector_state->best_encoder);
11920 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011921 return false;
11922 }
11923
11924 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011925}
11926
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011927static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011928{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011929 struct drm_device *dev = state->dev;
11930 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011931 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011932 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011933 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011934 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011935
11936 /*
11937 * Walk the connector list instead of the encoder
11938 * list to detect the problem on ddi platforms
11939 * where there's just one encoder per digital port.
11940 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011941 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011942 if (!connector_state->best_encoder)
11943 continue;
11944
11945 encoder = to_intel_encoder(connector_state->best_encoder);
11946
11947 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011948
11949 switch (encoder->type) {
11950 unsigned int port_mask;
11951 case INTEL_OUTPUT_UNKNOWN:
11952 if (WARN_ON(!HAS_DDI(dev)))
11953 break;
11954 case INTEL_OUTPUT_DISPLAYPORT:
11955 case INTEL_OUTPUT_HDMI:
11956 case INTEL_OUTPUT_EDP:
11957 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11958
11959 /* the same port mustn't appear more than once */
11960 if (used_ports & port_mask)
11961 return false;
11962
11963 used_ports |= port_mask;
11964 default:
11965 break;
11966 }
11967 }
11968
11969 return true;
11970}
11971
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011972static void
11973clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11974{
11975 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011976 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011977 struct intel_dpll_hw_state dpll_hw_state;
11978 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011979 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011980
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011981 /* FIXME: before the switch to atomic started, a new pipe_config was
11982 * kzalloc'd. Code that depends on any field being zero should be
11983 * fixed, so that the crtc_state can be safely duplicated. For now,
11984 * only fields that are know to not cause problems are preserved. */
11985
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011986 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011987 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011988 shared_dpll = crtc_state->shared_dpll;
11989 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011990 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011991
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011992 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011993
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011994 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011995 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011996 crtc_state->shared_dpll = shared_dpll;
11997 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011998 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011999}
12000
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012001static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012002intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020012003 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020012004{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020012005 struct drm_crtc_state *crtc_state;
12006 struct intel_crtc_state *pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020012007 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012008 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012009 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012010 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012011 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012012 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012013
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012014 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012015 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012016 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012017 }
12018
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012019 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012020 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012021 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012022 }
12023
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020012024 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12025 if (WARN_ON(!crtc_state))
12026 return -EINVAL;
12027
12028 pipe_config = to_intel_crtc_state(crtc_state);
12029
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012030 /*
12031 * XXX: Add all connectors to make the crtc state match the encoders.
12032 */
12033 if (!needs_modeset(&pipe_config->base)) {
12034 ret = drm_atomic_add_affected_connectors(state, crtc);
12035 if (ret)
12036 return ret;
12037 }
12038
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012039 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012040
Daniel Vettere143a212013-07-04 12:01:15 +020012041 pipe_config->cpu_transcoder =
12042 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012043
Imre Deak2960bc92013-07-30 13:36:32 +030012044 /*
12045 * Sanitize sync polarity flags based on requested ones. If neither
12046 * positive or negative polarity is requested, treat this as meaning
12047 * negative polarity.
12048 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012049 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012050 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012051 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012052
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012053 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012054 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012055 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012056
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012057 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12058 * plane pixel format and any sink constraints into account. Returns the
12059 * source plane bpp so that dithering can be selected on mismatches
12060 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012061 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12062 pipe_config);
12063 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012064 goto fail;
12065
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012066 /*
12067 * Determine the real pipe dimensions. Note that stereo modes can
12068 * increase the actual pipe size due to the frame doubling and
12069 * insertion of additional space for blanks between the frame. This
12070 * is stored in the crtc timings. We use the requested mode to do this
12071 * computation to clearly distinguish it from the adjusted mode, which
12072 * can be changed by the connectors in the below retry loop.
12073 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012074 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012075 &pipe_config->pipe_src_w,
12076 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012077
Daniel Vettere29c22c2013-02-21 00:00:16 +010012078encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012079 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012080 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012081 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012082
Daniel Vetter135c81b2013-07-21 21:37:09 +020012083 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012084 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12085 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012086
Daniel Vetter7758a112012-07-08 19:40:39 +020012087 /* Pass our mode to the connectors and the CRTC to give them a chance to
12088 * adjust it according to limitations or connector properties, and also
12089 * a chance to reject the mode entirely.
12090 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012091 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012092 if (connector_state->crtc != crtc)
12093 continue;
12094
12095 encoder = to_intel_encoder(connector_state->best_encoder);
12096
Daniel Vetterefea6e82013-07-21 21:36:59 +020012097 if (!(encoder->compute_config(encoder, pipe_config))) {
12098 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012099 goto fail;
12100 }
12101 }
12102
Daniel Vetterff9a6752013-06-01 17:16:21 +020012103 /* Set default port clock if not overwritten by the encoder. Needs to be
12104 * done afterwards in case the encoder adjusts the mode. */
12105 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012106 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012107 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012108
Daniel Vettera43f6e02013-06-07 23:10:32 +020012109 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012110 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012111 DRM_DEBUG_KMS("CRTC fixup failed\n");
12112 goto fail;
12113 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012114
12115 if (ret == RETRY) {
12116 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12117 ret = -EINVAL;
12118 goto fail;
12119 }
12120
12121 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12122 retry = false;
12123 goto encoder_retry;
12124 }
12125
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012126 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012127 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012128 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012129
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012130 /* Check if we need to force a modeset */
12131 if (pipe_config->has_audio !=
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012132 to_intel_crtc_state(crtc->state)->has_audio) {
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012133 pipe_config->base.mode_changed = true;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +020012134 ret = drm_atomic_add_affected_planes(state, crtc);
12135 }
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012136
12137 /*
12138 * Note we have an issue here with infoframes: current code
12139 * only updates them on the full mode set path per hw
12140 * requirements. So here we should be checking for any
12141 * required changes and forcing a mode set.
12142 */
Daniel Vetter7758a112012-07-08 19:40:39 +020012143fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012144 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012145}
12146
Daniel Vetterea9d7582012-07-10 10:42:52 +020012147static bool intel_crtc_in_use(struct drm_crtc *crtc)
12148{
12149 struct drm_encoder *encoder;
12150 struct drm_device *dev = crtc->dev;
12151
12152 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12153 if (encoder->crtc == crtc)
12154 return true;
12155
12156 return false;
12157}
12158
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012159static void
12160intel_modeset_update_state(struct drm_atomic_state *state)
12161{
12162 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012163 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012164 struct drm_crtc *crtc;
12165 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012166 struct drm_connector *connector;
12167
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012168 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012169
Damien Lespiaub2784e12014-08-05 11:29:37 +010012170 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012171 if (!intel_encoder->base.crtc)
12172 continue;
12173
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012174 crtc = intel_encoder->base.crtc;
12175 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12176 if (!crtc_state || !needs_modeset(crtc->state))
12177 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012178
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012179 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012180 }
12181
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012182 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012183
Ville Syrjälä76688512014-01-10 11:28:06 +020012184 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012185 for_each_crtc(dev, crtc) {
12186 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012187
12188 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012189
12190 /* Update hwmode for vblank functions */
12191 if (crtc->state->active)
12192 crtc->hwmode = crtc->state->adjusted_mode;
12193 else
12194 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012195 }
12196
12197 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12198 if (!connector->encoder || !connector->encoder->crtc)
12199 continue;
12200
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012201 crtc = connector->encoder->crtc;
12202 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12203 if (!crtc_state || !needs_modeset(crtc->state))
12204 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012205
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012206 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012207 struct drm_property *dpms_property =
12208 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012209
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012210 connector->dpms = DRM_MODE_DPMS_ON;
12211 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012212
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012213 intel_encoder = to_intel_encoder(connector->encoder);
12214 intel_encoder->connectors_active = true;
12215 } else
12216 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012217 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012218}
12219
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012220static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012221{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012222 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012223
12224 if (clock1 == clock2)
12225 return true;
12226
12227 if (!clock1 || !clock2)
12228 return false;
12229
12230 diff = abs(clock1 - clock2);
12231
12232 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12233 return true;
12234
12235 return false;
12236}
12237
Daniel Vetter25c5b262012-07-08 22:08:04 +020012238#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12239 list_for_each_entry((intel_crtc), \
12240 &(dev)->mode_config.crtc_list, \
12241 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012242 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012243
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012244static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012245intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012246 struct intel_crtc_state *current_config,
12247 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012248{
Daniel Vetter66e985c2013-06-05 13:34:20 +020012249#define PIPE_CONF_CHECK_X(name) \
12250 if (current_config->name != pipe_config->name) { \
12251 DRM_ERROR("mismatch in " #name " " \
12252 "(expected 0x%08x, found 0x%08x)\n", \
12253 current_config->name, \
12254 pipe_config->name); \
12255 return false; \
12256 }
12257
Daniel Vetter08a24032013-04-19 11:25:34 +020012258#define PIPE_CONF_CHECK_I(name) \
12259 if (current_config->name != pipe_config->name) { \
12260 DRM_ERROR("mismatch in " #name " " \
12261 "(expected %i, found %i)\n", \
12262 current_config->name, \
12263 pipe_config->name); \
12264 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012265 }
12266
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012267/* This is required for BDW+ where there is only one set of registers for
12268 * switching between high and low RR.
12269 * This macro can be used whenever a comparison has to be made between one
12270 * hw state and multiple sw state variables.
12271 */
12272#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12273 if ((current_config->name != pipe_config->name) && \
12274 (current_config->alt_name != pipe_config->name)) { \
12275 DRM_ERROR("mismatch in " #name " " \
12276 "(expected %i or %i, found %i)\n", \
12277 current_config->name, \
12278 current_config->alt_name, \
12279 pipe_config->name); \
12280 return false; \
12281 }
12282
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012283#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12284 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070012285 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012286 "(expected %i, found %i)\n", \
12287 current_config->name & (mask), \
12288 pipe_config->name & (mask)); \
12289 return false; \
12290 }
12291
Ville Syrjälä5e550652013-09-06 23:29:07 +030012292#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12293 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12294 DRM_ERROR("mismatch in " #name " " \
12295 "(expected %i, found %i)\n", \
12296 current_config->name, \
12297 pipe_config->name); \
12298 return false; \
12299 }
12300
Daniel Vetterbb760062013-06-06 14:55:52 +020012301#define PIPE_CONF_QUIRK(quirk) \
12302 ((current_config->quirks | pipe_config->quirks) & (quirk))
12303
Daniel Vettereccb1402013-05-22 00:50:22 +020012304 PIPE_CONF_CHECK_I(cpu_transcoder);
12305
Daniel Vetter08a24032013-04-19 11:25:34 +020012306 PIPE_CONF_CHECK_I(has_pch_encoder);
12307 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012308 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12309 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12310 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12311 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12312 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012313
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012314 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012315
12316 if (INTEL_INFO(dev)->gen < 8) {
12317 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12318 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12319 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12320 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12321 PIPE_CONF_CHECK_I(dp_m_n.tu);
12322
12323 if (current_config->has_drrs) {
12324 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12325 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12326 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12327 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12328 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12329 }
12330 } else {
12331 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12332 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12333 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12334 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12335 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12336 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012337
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012338 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12339 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12340 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12341 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12342 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12343 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012344
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012345 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12346 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12347 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12348 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12349 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12350 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012351
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012352 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012353 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012354 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12355 IS_VALLEYVIEW(dev))
12356 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012357 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012358
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012359 PIPE_CONF_CHECK_I(has_audio);
12360
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012361 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012362 DRM_MODE_FLAG_INTERLACE);
12363
Daniel Vetterbb760062013-06-06 14:55:52 +020012364 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012365 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012366 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012367 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012368 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012369 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012370 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012371 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012372 DRM_MODE_FLAG_NVSYNC);
12373 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012374
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012375 PIPE_CONF_CHECK_I(pipe_src_w);
12376 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012377
Daniel Vetter99535992014-04-13 12:00:33 +020012378 /*
12379 * FIXME: BIOS likes to set up a cloned config with lvds+external
12380 * screen. Since we don't yet re-compute the pipe config when moving
12381 * just the lvds port away to another pipe the sw tracking won't match.
12382 *
12383 * Proper atomic modesets with recomputed global state will fix this.
12384 * Until then just don't check gmch state for inherited modes.
12385 */
12386 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12387 PIPE_CONF_CHECK_I(gmch_pfit.control);
12388 /* pfit ratios are autocomputed by the hw on gen4+ */
12389 if (INTEL_INFO(dev)->gen < 4)
12390 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12391 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12392 }
12393
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012394 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12395 if (current_config->pch_pfit.enabled) {
12396 PIPE_CONF_CHECK_I(pch_pfit.pos);
12397 PIPE_CONF_CHECK_I(pch_pfit.size);
12398 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012399
Chandra Kondurua1b22782015-04-07 15:28:45 -070012400 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12401
Jesse Barnese59150d2014-01-07 13:30:45 -080012402 /* BDW+ don't expose a synchronous way to read the state */
12403 if (IS_HASWELL(dev))
12404 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012405
Ville Syrjälä282740f2013-09-04 18:30:03 +030012406 PIPE_CONF_CHECK_I(double_wide);
12407
Daniel Vetter26804af2014-06-25 22:01:55 +030012408 PIPE_CONF_CHECK_X(ddi_pll_sel);
12409
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012410 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012411 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012412 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012413 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12414 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012415 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012416 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12417 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12418 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012419
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012420 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12421 PIPE_CONF_CHECK_I(pipe_bpp);
12422
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012423 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012424 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012425
Daniel Vetter66e985c2013-06-05 13:34:20 +020012426#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012427#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012428#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012429#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012430#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012431#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012432
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012433 return true;
12434}
12435
Damien Lespiau08db6652014-11-04 17:06:52 +000012436static void check_wm_state(struct drm_device *dev)
12437{
12438 struct drm_i915_private *dev_priv = dev->dev_private;
12439 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12440 struct intel_crtc *intel_crtc;
12441 int plane;
12442
12443 if (INTEL_INFO(dev)->gen < 9)
12444 return;
12445
12446 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12447 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12448
12449 for_each_intel_crtc(dev, intel_crtc) {
12450 struct skl_ddb_entry *hw_entry, *sw_entry;
12451 const enum pipe pipe = intel_crtc->pipe;
12452
12453 if (!intel_crtc->active)
12454 continue;
12455
12456 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012457 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012458 hw_entry = &hw_ddb.plane[pipe][plane];
12459 sw_entry = &sw_ddb->plane[pipe][plane];
12460
12461 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12462 continue;
12463
12464 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12465 "(expected (%u,%u), found (%u,%u))\n",
12466 pipe_name(pipe), plane + 1,
12467 sw_entry->start, sw_entry->end,
12468 hw_entry->start, hw_entry->end);
12469 }
12470
12471 /* cursor */
12472 hw_entry = &hw_ddb.cursor[pipe];
12473 sw_entry = &sw_ddb->cursor[pipe];
12474
12475 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12476 continue;
12477
12478 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12479 "(expected (%u,%u), found (%u,%u))\n",
12480 pipe_name(pipe),
12481 sw_entry->start, sw_entry->end,
12482 hw_entry->start, hw_entry->end);
12483 }
12484}
12485
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012486static void
12487check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012488{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012489 struct intel_connector *connector;
12490
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012491 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020012492 struct drm_encoder *encoder = connector->base.encoder;
12493 struct drm_connector_state *state = connector->base.state;
12494
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012495 /* This also checks the encoder/connector hw state with the
12496 * ->get_hw_state callbacks. */
12497 intel_connector_check_state(connector);
12498
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020012499 I915_STATE_WARN(state->best_encoder != encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012500 "connector's staged encoder doesn't match current encoder\n");
12501 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012502}
12503
12504static void
12505check_encoder_state(struct drm_device *dev)
12506{
12507 struct intel_encoder *encoder;
12508 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012509
Damien Lespiaub2784e12014-08-05 11:29:37 +010012510 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012511 bool enabled = false;
12512 bool active = false;
12513 enum pipe pipe, tracked_pipe;
12514
12515 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12516 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012517 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012518
Rob Clarke2c719b2014-12-15 13:56:32 -050012519 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012520 "encoder's active_connectors set, but no crtc\n");
12521
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012522 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012523 if (connector->base.encoder != &encoder->base)
12524 continue;
12525 enabled = true;
12526 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12527 active = true;
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020012528
12529 I915_STATE_WARN(connector->base.state->crtc != encoder->base.crtc,
12530 "encoder's stage crtc doesn't match current crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012531 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012532 /*
12533 * for MST connectors if we unplug the connector is gone
12534 * away but the encoder is still connected to a crtc
12535 * until a modeset happens in response to the hotplug.
12536 */
12537 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12538 continue;
12539
Rob Clarke2c719b2014-12-15 13:56:32 -050012540 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012541 "encoder's enabled state mismatch "
12542 "(expected %i, found %i)\n",
12543 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012544 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012545 "active encoder with no crtc\n");
12546
Rob Clarke2c719b2014-12-15 13:56:32 -050012547 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012548 "encoder's computed active state doesn't match tracked active state "
12549 "(expected %i, found %i)\n", active, encoder->connectors_active);
12550
12551 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012552 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012553 "encoder's hw state doesn't match sw tracking "
12554 "(expected %i, found %i)\n",
12555 encoder->connectors_active, active);
12556
12557 if (!encoder->base.crtc)
12558 continue;
12559
12560 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012561 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012562 "active encoder's pipe doesn't match"
12563 "(expected %i, found %i)\n",
12564 tracked_pipe, pipe);
12565
12566 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012567}
12568
12569static void
12570check_crtc_state(struct drm_device *dev)
12571{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012572 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012573 struct intel_crtc *crtc;
12574 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012575 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012576
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012577 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012578 bool enabled = false;
12579 bool active = false;
12580
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012581 memset(&pipe_config, 0, sizeof(pipe_config));
12582
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012583 DRM_DEBUG_KMS("[CRTC:%d]\n",
12584 crtc->base.base.id);
12585
Matt Roper83d65732015-02-25 13:12:16 -080012586 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012587 "active crtc, but not enabled in sw tracking\n");
12588
Damien Lespiaub2784e12014-08-05 11:29:37 +010012589 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012590 if (encoder->base.crtc != &crtc->base)
12591 continue;
12592 enabled = true;
12593 if (encoder->connectors_active)
12594 active = true;
12595 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012596
Rob Clarke2c719b2014-12-15 13:56:32 -050012597 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012598 "crtc's computed active state doesn't match tracked active state "
12599 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012600 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012601 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012602 "(expected %i, found %i)\n", enabled,
12603 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012604
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012605 active = dev_priv->display.get_pipe_config(crtc,
12606 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012607
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012608 /* hw state is inconsistent with the pipe quirk */
12609 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12610 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012611 active = crtc->active;
12612
Damien Lespiaub2784e12014-08-05 11:29:37 +010012613 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012614 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012615 if (encoder->base.crtc != &crtc->base)
12616 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012617 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012618 encoder->get_config(encoder, &pipe_config);
12619 }
12620
Rob Clarke2c719b2014-12-15 13:56:32 -050012621 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012622 "crtc active state doesn't match with hw state "
12623 "(expected %i, found %i)\n", crtc->active, active);
12624
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012625 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12626 "transitional active state does not match atomic hw state "
12627 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12628
Daniel Vetterc0b03412013-05-28 12:05:54 +020012629 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012630 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012631 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012632 intel_dump_pipe_config(crtc, &pipe_config,
12633 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012634 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012635 "[sw state]");
12636 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012637 }
12638}
12639
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012640static void
12641check_shared_dpll_state(struct drm_device *dev)
12642{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012643 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012644 struct intel_crtc *crtc;
12645 struct intel_dpll_hw_state dpll_hw_state;
12646 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012647
12648 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12649 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12650 int enabled_crtcs = 0, active_crtcs = 0;
12651 bool active;
12652
12653 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12654
12655 DRM_DEBUG_KMS("%s\n", pll->name);
12656
12657 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12658
Rob Clarke2c719b2014-12-15 13:56:32 -050012659 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012660 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012661 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012662 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012663 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012664 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012665 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012666 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012667 "pll on state mismatch (expected %i, found %i)\n",
12668 pll->on, active);
12669
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012670 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012671 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012672 enabled_crtcs++;
12673 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12674 active_crtcs++;
12675 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012676 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012677 "pll active crtcs mismatch (expected %i, found %i)\n",
12678 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012679 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012680 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012681 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012682
Rob Clarke2c719b2014-12-15 13:56:32 -050012683 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012684 sizeof(dpll_hw_state)),
12685 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012686 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012687}
12688
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012689void
12690intel_modeset_check_state(struct drm_device *dev)
12691{
Damien Lespiau08db6652014-11-04 17:06:52 +000012692 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012693 check_connector_state(dev);
12694 check_encoder_state(dev);
12695 check_crtc_state(dev);
12696 check_shared_dpll_state(dev);
12697}
12698
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012699void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012700 int dotclock)
12701{
12702 /*
12703 * FDI already provided one idea for the dotclock.
12704 * Yell if the encoder disagrees.
12705 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012706 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012707 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012708 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012709}
12710
Ville Syrjälä80715b22014-05-15 20:23:23 +030012711static void update_scanline_offset(struct intel_crtc *crtc)
12712{
12713 struct drm_device *dev = crtc->base.dev;
12714
12715 /*
12716 * The scanline counter increments at the leading edge of hsync.
12717 *
12718 * On most platforms it starts counting from vtotal-1 on the
12719 * first active line. That means the scanline counter value is
12720 * always one less than what we would expect. Ie. just after
12721 * start of vblank, which also occurs at start of hsync (on the
12722 * last active line), the scanline counter will read vblank_start-1.
12723 *
12724 * On gen2 the scanline counter starts counting from 1 instead
12725 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12726 * to keep the value positive), instead of adding one.
12727 *
12728 * On HSW+ the behaviour of the scanline counter depends on the output
12729 * type. For DP ports it behaves like most other platforms, but on HDMI
12730 * there's an extra 1 line difference. So we need to add two instead of
12731 * one to the value.
12732 */
12733 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012734 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012735 int vtotal;
12736
12737 vtotal = mode->crtc_vtotal;
12738 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12739 vtotal /= 2;
12740
12741 crtc->scanline_offset = vtotal - 1;
12742 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012743 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012744 crtc->scanline_offset = 2;
12745 } else
12746 crtc->scanline_offset = 1;
12747}
12748
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012749static int intel_modeset_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012750{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012751 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012752 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012753 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012754 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012755 struct intel_crtc_state *intel_crtc_state;
12756 struct drm_crtc *crtc;
12757 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012758 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012759 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012760
12761 if (!dev_priv->display.crtc_compute_clock)
12762 return 0;
12763
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012764 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12765 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012766 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012767
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012768 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012769 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012770 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012771 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012772 }
12773
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012774 if (clear_pipes) {
12775 struct intel_shared_dpll_config *shared_dpll =
12776 intel_atomic_get_shared_dpll_state(state);
12777
12778 for (i = 0; i < dev_priv->num_shared_dpll; i++)
12779 shared_dpll[i].crtc_mask &= ~clear_pipes;
12780 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012781
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012782 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12783 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012784 continue;
12785
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012786 intel_crtc = to_intel_crtc(crtc);
12787 intel_crtc_state = to_intel_crtc_state(crtc_state);
12788
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012789 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012790 intel_crtc_state);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012791 if (ret)
12792 return ret;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012793 }
12794
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012795 return ret;
12796}
12797
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012798/*
12799 * This implements the workaround described in the "notes" section of the mode
12800 * set sequence documentation. When going from no pipes or single pipe to
12801 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12802 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12803 */
12804static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12805{
12806 struct drm_crtc_state *crtc_state;
12807 struct intel_crtc *intel_crtc;
12808 struct drm_crtc *crtc;
12809 struct intel_crtc_state *first_crtc_state = NULL;
12810 struct intel_crtc_state *other_crtc_state = NULL;
12811 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12812 int i;
12813
12814 /* look at all crtc's that are going to be enabled in during modeset */
12815 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12816 intel_crtc = to_intel_crtc(crtc);
12817
12818 if (!crtc_state->active || !needs_modeset(crtc_state))
12819 continue;
12820
12821 if (first_crtc_state) {
12822 other_crtc_state = to_intel_crtc_state(crtc_state);
12823 break;
12824 } else {
12825 first_crtc_state = to_intel_crtc_state(crtc_state);
12826 first_pipe = intel_crtc->pipe;
12827 }
12828 }
12829
12830 /* No workaround needed? */
12831 if (!first_crtc_state)
12832 return 0;
12833
12834 /* w/a possibly needed, check how many crtc's are already enabled. */
12835 for_each_intel_crtc(state->dev, intel_crtc) {
12836 struct intel_crtc_state *pipe_config;
12837
12838 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12839 if (IS_ERR(pipe_config))
12840 return PTR_ERR(pipe_config);
12841
12842 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12843
12844 if (!pipe_config->base.active ||
12845 needs_modeset(&pipe_config->base))
12846 continue;
12847
12848 /* 2 or more enabled crtcs means no need for w/a */
12849 if (enabled_pipe != INVALID_PIPE)
12850 return 0;
12851
12852 enabled_pipe = intel_crtc->pipe;
12853 }
12854
12855 if (enabled_pipe != INVALID_PIPE)
12856 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12857 else if (other_crtc_state)
12858 other_crtc_state->hsw_workaround_pipe = first_pipe;
12859
12860 return 0;
12861}
12862
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012863/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012864static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012865{
12866 struct drm_device *dev = state->dev;
12867 int ret;
12868
12869 /*
12870 * See if the config requires any additional preparation, e.g.
12871 * to adjust global state with pipes off. We need to do this
12872 * here so we can get the modeset_pipe updated config for the new
12873 * mode set on this crtc. For other crtcs we need to use the
12874 * adjusted_mode bits in the crtc directly.
12875 */
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012876 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
12877 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
12878 ret = valleyview_modeset_global_pipes(state);
12879 else
12880 ret = broadwell_modeset_global_pipes(state);
12881
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012882 if (ret)
12883 return ret;
12884 }
12885
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012886 ret = intel_modeset_setup_plls(state);
12887 if (ret)
12888 return ret;
12889
12890 if (IS_HASWELL(dev))
12891 ret = haswell_mode_set_planes_workaround(state);
12892
12893 return ret;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012894}
12895
12896static int
12897intel_modeset_compute_config(struct drm_atomic_state *state)
12898{
12899 struct drm_crtc *crtc;
12900 struct drm_crtc_state *crtc_state;
12901 int ret, i;
12902
12903 ret = drm_atomic_helper_check_modeset(state->dev, state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012904 if (ret)
12905 return ret;
12906
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012907 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12908 if (!crtc_state->enable &&
12909 WARN_ON(crtc_state->active))
12910 crtc_state->active = false;
12911
12912 if (!crtc_state->enable)
12913 continue;
12914
12915 ret = intel_modeset_pipe_config(crtc, state);
12916 if (ret)
12917 return ret;
12918
12919 intel_dump_pipe_config(to_intel_crtc(crtc),
12920 to_intel_crtc_state(crtc_state),
12921 "[modeset]");
12922 }
12923
12924 ret = intel_modeset_checks(state);
12925 if (ret)
12926 return ret;
12927
12928 return drm_atomic_helper_check_planes(state->dev, state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012929}
12930
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020012931static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012932{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020012933 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012934 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012935 struct drm_crtc *crtc;
12936 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012937 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012938 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012939
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012940 ret = drm_atomic_helper_prepare_planes(dev, state);
12941 if (ret)
12942 return ret;
12943
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020012944 drm_atomic_helper_swap_state(dev, state);
12945
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012946 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020012947 if (!needs_modeset(crtc->state) || !crtc_state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012948 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012949
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012950 intel_crtc_disable_planes(crtc);
12951 dev_priv->display.crtc_disable(crtc);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012952 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012953
Daniel Vetterea9d7582012-07-10 10:42:52 +020012954 /* Only after disabling all output pipelines that will be changed can we
12955 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012956 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012957
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012958 /* The state has been swaped above, so state actually contains the
12959 * old state now. */
12960
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012961 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012962
Daniel Vettera6778b32012-07-02 09:56:42 +020012963 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012964 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020012965 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
12966
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012967 if (!needs_modeset(crtc->state) || !crtc->state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012968 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012969
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012970 update_scanline_offset(to_intel_crtc(crtc));
12971
12972 dev_priv->display.crtc_enable(crtc);
12973 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012974 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012975
Daniel Vettera6778b32012-07-02 09:56:42 +020012976 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012977
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012978 drm_atomic_helper_cleanup_planes(dev, state);
12979
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012980 drm_atomic_state_free(state);
12981
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012982 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012983}
12984
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020012985static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012986{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020012987 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012988 int ret;
12989
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020012990 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012991 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020012992 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012993
12994 return ret;
12995}
12996
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020012997static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012998{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020012999 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013000
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013001 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013002 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013003 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013004
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013005 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013006}
13007
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013008void intel_crtc_restore_mode(struct drm_crtc *crtc)
13009{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013010 struct drm_device *dev = crtc->dev;
13011 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013012 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013013 struct intel_encoder *encoder;
13014 struct intel_connector *connector;
13015 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013016 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013017 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013018
13019 state = drm_atomic_state_alloc(dev);
13020 if (!state) {
13021 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13022 crtc->base.id);
13023 return;
13024 }
13025
13026 state->acquire_ctx = dev->mode_config.acquire_ctx;
13027
13028 /* The force restore path in the HW readout code relies on the staged
13029 * config still keeping the user requested config while the actual
13030 * state has been overwritten by the configuration read from HW. We
13031 * need to copy the staged config to the atomic state, otherwise the
13032 * mode set will just reapply the state the HW is already in. */
13033 for_each_intel_encoder(dev, encoder) {
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020013034 if (encoder->base.crtc != crtc)
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013035 continue;
13036
13037 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020013038 if (connector->base.state->best_encoder != &encoder->base)
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013039 continue;
13040
13041 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13042 if (IS_ERR(connector_state)) {
13043 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13044 connector->base.base.id,
13045 connector->base.name,
13046 PTR_ERR(connector_state));
13047 continue;
13048 }
13049
13050 connector_state->crtc = crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013051 }
13052 }
13053
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013054 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013055 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13056 if (IS_ERR(crtc_state)) {
13057 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13058 intel_crtc->base.base.id,
13059 PTR_ERR(crtc_state));
13060 continue;
13061 }
13062
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013063 if (&intel_crtc->base == crtc)
13064 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013065 }
13066
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013067 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13068 crtc->primary->fb, crtc->x, crtc->y);
13069
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013070 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013071 if (ret)
13072 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013073}
13074
Daniel Vetter25c5b262012-07-08 22:08:04 +020013075#undef for_each_intel_crtc_masked
13076
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013077static bool intel_connector_in_mode_set(struct intel_connector *connector,
13078 struct drm_mode_set *set)
13079{
13080 int ro;
13081
13082 for (ro = 0; ro < set->num_connectors; ro++)
13083 if (set->connectors[ro] == &connector->base)
13084 return true;
13085
13086 return false;
13087}
13088
Daniel Vetter2e431052012-07-04 22:42:15 +020013089static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013090intel_modeset_stage_output_state(struct drm_device *dev,
13091 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013092 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013093{
Daniel Vetter9a935852012-07-05 22:34:27 +020013094 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013095 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013096 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013097 struct drm_crtc *crtc;
13098 struct drm_crtc_state *crtc_state;
13099 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013100
Damien Lespiau9abdda72013-02-13 13:29:23 +000013101 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013102 * of connectors. For paranoia, double-check this. */
13103 WARN_ON(!set->fb && (set->num_connectors != 0));
13104 WARN_ON(set->fb && (set->num_connectors == 0));
13105
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013106 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013107 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13108
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013109 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13110 continue;
13111
13112 connector_state =
13113 drm_atomic_get_connector_state(state, &connector->base);
13114 if (IS_ERR(connector_state))
13115 return PTR_ERR(connector_state);
13116
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013117 if (in_mode_set) {
13118 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013119 connector_state->best_encoder =
13120 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013121 }
13122
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013123 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013124 continue;
13125
Daniel Vetter9a935852012-07-05 22:34:27 +020013126 /* If we disable the crtc, disable all its connectors. Also, if
13127 * the connector is on the changing crtc but not on the new
13128 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013129 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013130 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013131
13132 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13133 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013134 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013135 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013136 }
13137 /* connector->new_encoder is now updated for all connectors. */
13138
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013139 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13140 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013141
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013142 if (!connector_state->best_encoder) {
13143 ret = drm_atomic_set_crtc_for_connector(connector_state,
13144 NULL);
13145 if (ret)
13146 return ret;
13147
Daniel Vetter50f56112012-07-02 09:35:43 +020013148 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013149 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013150
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013151 if (intel_connector_in_mode_set(connector, set)) {
13152 struct drm_crtc *crtc = connector->base.state->crtc;
13153
13154 /* If this connector was in a previous crtc, add it
13155 * to the state. We might need to disable it. */
13156 if (crtc) {
13157 crtc_state =
13158 drm_atomic_get_crtc_state(state, crtc);
13159 if (IS_ERR(crtc_state))
13160 return PTR_ERR(crtc_state);
13161 }
13162
13163 ret = drm_atomic_set_crtc_for_connector(connector_state,
13164 set->crtc);
13165 if (ret)
13166 return ret;
13167 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013168
13169 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013170 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13171 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013172 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013173 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013174
Daniel Vetter9a935852012-07-05 22:34:27 +020013175 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13176 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013177 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013178 connector_state->crtc->base.id);
13179
13180 if (connector_state->best_encoder != &connector->encoder->base)
13181 connector->encoder =
13182 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013183 }
13184
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013185 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013186 bool has_connectors;
13187
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013188 ret = drm_atomic_add_affected_connectors(state, crtc);
13189 if (ret)
13190 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013191
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013192 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13193 if (has_connectors != crtc_state->enable)
13194 crtc_state->enable =
13195 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013196 }
13197
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013198 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13199 set->fb, set->x, set->y);
13200 if (ret)
13201 return ret;
13202
13203 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13204 if (IS_ERR(crtc_state))
13205 return PTR_ERR(crtc_state);
13206
13207 if (set->mode)
13208 drm_mode_copy(&crtc_state->mode, set->mode);
13209
13210 if (set->num_connectors)
13211 crtc_state->active = true;
13212
Daniel Vetter2e431052012-07-04 22:42:15 +020013213 return 0;
13214}
13215
13216static int intel_crtc_set_config(struct drm_mode_set *set)
13217{
13218 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013219 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013220 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013221
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013222 BUG_ON(!set);
13223 BUG_ON(!set->crtc);
13224 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013225
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013226 /* Enforce sane interface api - has been abused by the fb helper. */
13227 BUG_ON(!set->mode && set->fb);
13228 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013229
Daniel Vetter2e431052012-07-04 22:42:15 +020013230 if (set->fb) {
13231 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13232 set->crtc->base.id, set->fb->base.id,
13233 (int)set->num_connectors, set->x, set->y);
13234 } else {
13235 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013236 }
13237
13238 dev = set->crtc->dev;
13239
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013240 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013241 if (!state)
13242 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013243
13244 state->acquire_ctx = dev->mode_config.acquire_ctx;
13245
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013246 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013247 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013248 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013249
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013250 ret = intel_modeset_compute_config(state);
13251 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013252 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013253
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013254 intel_update_pipe_size(to_intel_crtc(set->crtc));
13255
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013256 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013257 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013258 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13259 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013260 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013261
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013262out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013263 if (ret)
13264 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013265 return ret;
13266}
13267
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013268static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013269 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013270 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013271 .destroy = intel_crtc_destroy,
13272 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013273 .atomic_duplicate_state = intel_crtc_duplicate_state,
13274 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013275};
13276
Daniel Vetter53589012013-06-05 13:34:16 +020013277static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13278 struct intel_shared_dpll *pll,
13279 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013280{
Daniel Vetter53589012013-06-05 13:34:16 +020013281 uint32_t val;
13282
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013283 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013284 return false;
13285
Daniel Vetter53589012013-06-05 13:34:16 +020013286 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013287 hw_state->dpll = val;
13288 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13289 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013290
13291 return val & DPLL_VCO_ENABLE;
13292}
13293
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013294static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13295 struct intel_shared_dpll *pll)
13296{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013297 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13298 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013299}
13300
Daniel Vettere7b903d2013-06-05 13:34:14 +020013301static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13302 struct intel_shared_dpll *pll)
13303{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013304 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013305 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013306
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013307 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013308
13309 /* Wait for the clocks to stabilize. */
13310 POSTING_READ(PCH_DPLL(pll->id));
13311 udelay(150);
13312
13313 /* The pixel multiplier can only be updated once the
13314 * DPLL is enabled and the clocks are stable.
13315 *
13316 * So write it again.
13317 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013318 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013319 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013320 udelay(200);
13321}
13322
13323static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13324 struct intel_shared_dpll *pll)
13325{
13326 struct drm_device *dev = dev_priv->dev;
13327 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013328
13329 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013330 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013331 if (intel_crtc_to_shared_dpll(crtc) == pll)
13332 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13333 }
13334
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013335 I915_WRITE(PCH_DPLL(pll->id), 0);
13336 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013337 udelay(200);
13338}
13339
Daniel Vetter46edb022013-06-05 13:34:12 +020013340static char *ibx_pch_dpll_names[] = {
13341 "PCH DPLL A",
13342 "PCH DPLL B",
13343};
13344
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013345static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013346{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013347 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013348 int i;
13349
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013350 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013351
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013352 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013353 dev_priv->shared_dplls[i].id = i;
13354 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013355 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013356 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13357 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013358 dev_priv->shared_dplls[i].get_hw_state =
13359 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013360 }
13361}
13362
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013363static void intel_shared_dpll_init(struct drm_device *dev)
13364{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013365 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013366
Ville Syrjäläb6283052015-06-03 15:45:07 +030013367 intel_update_cdclk(dev);
13368
Daniel Vetter9cd86932014-06-25 22:01:57 +030013369 if (HAS_DDI(dev))
13370 intel_ddi_pll_init(dev);
13371 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013372 ibx_pch_dpll_init(dev);
13373 else
13374 dev_priv->num_shared_dpll = 0;
13375
13376 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013377}
13378
Matt Roper6beb8c232014-12-01 15:40:14 -080013379/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013380 * intel_wm_need_update - Check whether watermarks need updating
13381 * @plane: drm plane
13382 * @state: new plane state
13383 *
13384 * Check current plane state versus the new one to determine whether
13385 * watermarks need to be recalculated.
13386 *
13387 * Returns true or false.
13388 */
13389bool intel_wm_need_update(struct drm_plane *plane,
13390 struct drm_plane_state *state)
13391{
13392 /* Update watermarks on tiling changes. */
13393 if (!plane->state->fb || !state->fb ||
13394 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13395 plane->state->rotation != state->rotation)
13396 return true;
13397
13398 return false;
13399}
13400
13401/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013402 * intel_prepare_plane_fb - Prepare fb for usage on plane
13403 * @plane: drm plane to prepare for
13404 * @fb: framebuffer to prepare for presentation
13405 *
13406 * Prepares a framebuffer for usage on a display plane. Generally this
13407 * involves pinning the underlying object and updating the frontbuffer tracking
13408 * bits. Some older platforms need special physical address handling for
13409 * cursor planes.
13410 *
13411 * Returns 0 on success, negative error code on failure.
13412 */
13413int
13414intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013415 struct drm_framebuffer *fb,
13416 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013417{
13418 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013419 struct intel_plane *intel_plane = to_intel_plane(plane);
13420 enum pipe pipe = intel_plane->pipe;
13421 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13422 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13423 unsigned frontbuffer_bits = 0;
13424 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013425
Matt Roperea2c67b2014-12-23 10:41:52 -080013426 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013427 return 0;
13428
Matt Roper6beb8c232014-12-01 15:40:14 -080013429 switch (plane->type) {
13430 case DRM_PLANE_TYPE_PRIMARY:
13431 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13432 break;
13433 case DRM_PLANE_TYPE_CURSOR:
13434 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13435 break;
13436 case DRM_PLANE_TYPE_OVERLAY:
13437 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13438 break;
13439 }
Matt Roper465c1202014-05-29 08:06:54 -070013440
Matt Roper4c345742014-07-09 16:22:10 -070013441 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013442
Matt Roper6beb8c232014-12-01 15:40:14 -080013443 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13444 INTEL_INFO(dev)->cursor_needs_physical) {
13445 int align = IS_I830(dev) ? 16 * 1024 : 256;
13446 ret = i915_gem_object_attach_phys(obj, align);
13447 if (ret)
13448 DRM_DEBUG_KMS("failed to attach phys object\n");
13449 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013450 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013451 }
13452
13453 if (ret == 0)
13454 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13455
13456 mutex_unlock(&dev->struct_mutex);
13457
13458 return ret;
13459}
13460
Matt Roper38f3ce32014-12-02 07:45:25 -080013461/**
13462 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13463 * @plane: drm plane to clean up for
13464 * @fb: old framebuffer that was on plane
13465 *
13466 * Cleans up a framebuffer that has just been removed from a plane.
13467 */
13468void
13469intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013470 struct drm_framebuffer *fb,
13471 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013472{
13473 struct drm_device *dev = plane->dev;
13474 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13475
13476 if (WARN_ON(!obj))
13477 return;
13478
13479 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13480 !INTEL_INFO(dev)->cursor_needs_physical) {
13481 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013482 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013483 mutex_unlock(&dev->struct_mutex);
13484 }
Matt Roper465c1202014-05-29 08:06:54 -070013485}
13486
Chandra Konduru6156a452015-04-27 13:48:39 -070013487int
13488skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13489{
13490 int max_scale;
13491 struct drm_device *dev;
13492 struct drm_i915_private *dev_priv;
13493 int crtc_clock, cdclk;
13494
13495 if (!intel_crtc || !crtc_state)
13496 return DRM_PLANE_HELPER_NO_SCALING;
13497
13498 dev = intel_crtc->base.dev;
13499 dev_priv = dev->dev_private;
13500 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13501 cdclk = dev_priv->display.get_display_clock_speed(dev);
13502
13503 if (!crtc_clock || !cdclk)
13504 return DRM_PLANE_HELPER_NO_SCALING;
13505
13506 /*
13507 * skl max scale is lower of:
13508 * close to 3 but not 3, -1 is for that purpose
13509 * or
13510 * cdclk/crtc_clock
13511 */
13512 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13513
13514 return max_scale;
13515}
13516
Matt Roper465c1202014-05-29 08:06:54 -070013517static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013518intel_check_primary_plane(struct drm_plane *plane,
13519 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013520{
Matt Roper32b7eee2014-12-24 07:59:06 -080013521 struct drm_device *dev = plane->dev;
13522 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013523 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013524 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013525 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013526 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013527 struct drm_rect *dest = &state->dst;
13528 struct drm_rect *src = &state->src;
13529 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013530 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013531 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13532 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013533 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013534
Matt Roperea2c67b2014-12-23 10:41:52 -080013535 crtc = crtc ? crtc : plane->crtc;
13536 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013537 crtc_state = state->base.state ?
13538 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013539
Chandra Konduru6156a452015-04-27 13:48:39 -070013540 if (INTEL_INFO(dev)->gen >= 9) {
Chandra Konduru225c2282015-05-18 16:18:44 -070013541 /* use scaler when colorkey is not required */
13542 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13543 min_scale = 1;
13544 max_scale = skl_max_scale(intel_crtc, crtc_state);
13545 }
Sonika Jindald8106362015-04-10 14:37:28 +053013546 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013547 }
Sonika Jindald8106362015-04-10 14:37:28 +053013548
Matt Roperc59cb172014-12-01 15:40:16 -080013549 ret = drm_plane_helper_check_update(plane, crtc, fb,
13550 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013551 min_scale,
13552 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013553 can_position, true,
13554 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013555 if (ret)
13556 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013557
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013558 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013559 struct intel_plane_state *old_state =
13560 to_intel_plane_state(plane->state);
13561
Matt Roper32b7eee2014-12-24 07:59:06 -080013562 intel_crtc->atomic.wait_for_flips = true;
13563
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013564 /*
13565 * FBC does not work on some platforms for rotated
13566 * planes, so disable it when rotation is not 0 and
13567 * update it when rotation is set back to 0.
13568 *
13569 * FIXME: This is redundant with the fbc update done in
13570 * the primary plane enable function except that that
13571 * one is done too late. We eventually need to unify
13572 * this.
13573 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013574 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013575 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013576 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013577 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013578 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013579 }
13580
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013581 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013582 /*
13583 * BDW signals flip done immediately if the plane
13584 * is disabled, even if the plane enable is already
13585 * armed to occur at the next vblank :(
13586 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013587 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013588 intel_crtc->atomic.wait_vblank = true;
Maarten Lankhorstfb9d6cf2015-06-01 12:49:56 +020013589
13590 if (crtc_state && !needs_modeset(&crtc_state->base))
13591 intel_crtc->atomic.post_enable_primary = true;
Matt Roper32b7eee2014-12-24 07:59:06 -080013592 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013593
Maarten Lankhorstfb9d6cf2015-06-01 12:49:56 +020013594 if (!state->visible && old_state->visible &&
13595 crtc_state && !needs_modeset(&crtc_state->base))
13596 intel_crtc->atomic.pre_disable_primary = true;
13597
Matt Roper32b7eee2014-12-24 07:59:06 -080013598 intel_crtc->atomic.fb_bits |=
13599 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13600
13601 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013602
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013603 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013604 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013605 }
13606
Chandra Konduru6156a452015-04-27 13:48:39 -070013607 if (INTEL_INFO(dev)->gen >= 9) {
13608 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13609 to_intel_plane(plane), state, 0);
13610 if (ret)
13611 return ret;
13612 }
13613
Matt Roperc59cb172014-12-01 15:40:16 -080013614 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013615}
13616
Sonika Jindal48404c12014-08-22 14:06:04 +053013617static void
13618intel_commit_primary_plane(struct drm_plane *plane,
13619 struct intel_plane_state *state)
13620{
Matt Roper2b875c22014-12-01 15:40:13 -080013621 struct drm_crtc *crtc = state->base.crtc;
13622 struct drm_framebuffer *fb = state->base.fb;
13623 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013624 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013625 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013626 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013627
Matt Roperea2c67b2014-12-23 10:41:52 -080013628 crtc = crtc ? crtc : plane->crtc;
13629 intel_crtc = to_intel_crtc(crtc);
13630
Matt Ropercf4c7c12014-12-04 10:27:42 -080013631 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013632 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013633 crtc->y = src->y1 >> 16;
13634
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013635 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013636 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013637 /* FIXME: kill this fastboot hack */
13638 intel_update_pipe_size(intel_crtc);
13639
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013640 dev_priv->display.update_primary_plane(crtc, plane->fb,
13641 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013642 }
13643}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013644
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013645static void
13646intel_disable_primary_plane(struct drm_plane *plane,
13647 struct drm_crtc *crtc,
13648 bool force)
13649{
13650 struct drm_device *dev = plane->dev;
13651 struct drm_i915_private *dev_priv = dev->dev_private;
13652
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013653 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13654}
13655
Matt Roper32b7eee2014-12-24 07:59:06 -080013656static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13657{
13658 struct drm_device *dev = crtc->dev;
13659 struct drm_i915_private *dev_priv = dev->dev_private;
13660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013661 struct intel_plane *intel_plane;
13662 struct drm_plane *p;
13663 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013664
Matt Roperea2c67b2014-12-23 10:41:52 -080013665 /* Track fb's for any planes being disabled */
13666 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13667 intel_plane = to_intel_plane(p);
13668
13669 if (intel_crtc->atomic.disabled_planes &
13670 (1 << drm_plane_index(p))) {
13671 switch (p->type) {
13672 case DRM_PLANE_TYPE_PRIMARY:
13673 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13674 break;
13675 case DRM_PLANE_TYPE_CURSOR:
13676 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13677 break;
13678 case DRM_PLANE_TYPE_OVERLAY:
13679 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13680 break;
13681 }
13682
13683 mutex_lock(&dev->struct_mutex);
13684 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13685 mutex_unlock(&dev->struct_mutex);
13686 }
13687 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013688
Matt Roper32b7eee2014-12-24 07:59:06 -080013689 if (intel_crtc->atomic.wait_for_flips)
13690 intel_crtc_wait_for_pending_flips(crtc);
13691
13692 if (intel_crtc->atomic.disable_fbc)
13693 intel_fbc_disable(dev);
13694
13695 if (intel_crtc->atomic.pre_disable_primary)
13696 intel_pre_disable_primary(crtc);
13697
13698 if (intel_crtc->atomic.update_wm)
13699 intel_update_watermarks(crtc);
13700
13701 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013702
13703 /* Perform vblank evasion around commit operation */
13704 if (intel_crtc->active)
13705 intel_crtc->atomic.evade =
13706 intel_pipe_update_start(intel_crtc,
13707 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013708}
13709
13710static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13711{
13712 struct drm_device *dev = crtc->dev;
13713 struct drm_i915_private *dev_priv = dev->dev_private;
13714 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13715 struct drm_plane *p;
13716
Matt Roperc34c9ee2014-12-23 10:41:50 -080013717 if (intel_crtc->atomic.evade)
13718 intel_pipe_update_end(intel_crtc,
13719 intel_crtc->atomic.start_vbl_count);
13720
Matt Roper32b7eee2014-12-24 07:59:06 -080013721 intel_runtime_pm_put(dev_priv);
13722
Maarten Lankhorst8a8f7f42015-06-01 12:49:55 +020013723 if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
Matt Roper32b7eee2014-12-24 07:59:06 -080013724 intel_wait_for_vblank(dev, intel_crtc->pipe);
13725
13726 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13727
13728 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013729 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013730 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013731 mutex_unlock(&dev->struct_mutex);
13732 }
Matt Roper465c1202014-05-29 08:06:54 -070013733
Matt Roper32b7eee2014-12-24 07:59:06 -080013734 if (intel_crtc->atomic.post_enable_primary)
13735 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013736
Matt Roper32b7eee2014-12-24 07:59:06 -080013737 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13738 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13739 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13740 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013741
Matt Roper32b7eee2014-12-24 07:59:06 -080013742 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013743}
13744
Matt Ropercf4c7c12014-12-04 10:27:42 -080013745/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013746 * intel_plane_destroy - destroy a plane
13747 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013748 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013749 * Common destruction function for all types of planes (primary, cursor,
13750 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013751 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013752void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013753{
13754 struct intel_plane *intel_plane = to_intel_plane(plane);
13755 drm_plane_cleanup(plane);
13756 kfree(intel_plane);
13757}
13758
Matt Roper65a3fea2015-01-21 16:35:42 -080013759const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013760 .update_plane = drm_atomic_helper_update_plane,
13761 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013762 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013763 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013764 .atomic_get_property = intel_plane_atomic_get_property,
13765 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013766 .atomic_duplicate_state = intel_plane_duplicate_state,
13767 .atomic_destroy_state = intel_plane_destroy_state,
13768
Matt Roper465c1202014-05-29 08:06:54 -070013769};
13770
13771static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13772 int pipe)
13773{
13774 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013775 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013776 const uint32_t *intel_primary_formats;
13777 int num_formats;
13778
13779 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13780 if (primary == NULL)
13781 return NULL;
13782
Matt Roper8e7d6882015-01-21 16:35:41 -080013783 state = intel_create_plane_state(&primary->base);
13784 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013785 kfree(primary);
13786 return NULL;
13787 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013788 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013789
Matt Roper465c1202014-05-29 08:06:54 -070013790 primary->can_scale = false;
13791 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013792 if (INTEL_INFO(dev)->gen >= 9) {
13793 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013794 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013795 }
Matt Roper465c1202014-05-29 08:06:54 -070013796 primary->pipe = pipe;
13797 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013798 primary->check_plane = intel_check_primary_plane;
13799 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013800 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013801 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013802 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13803 primary->plane = !pipe;
13804
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013805 if (INTEL_INFO(dev)->gen >= 9) {
13806 intel_primary_formats = skl_primary_formats;
13807 num_formats = ARRAY_SIZE(skl_primary_formats);
13808 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013809 intel_primary_formats = i965_primary_formats;
13810 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013811 } else {
13812 intel_primary_formats = i8xx_primary_formats;
13813 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013814 }
13815
13816 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013817 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013818 intel_primary_formats, num_formats,
13819 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013820
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013821 if (INTEL_INFO(dev)->gen >= 4)
13822 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013823
Matt Roperea2c67b2014-12-23 10:41:52 -080013824 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13825
Matt Roper465c1202014-05-29 08:06:54 -070013826 return &primary->base;
13827}
13828
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013829void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13830{
13831 if (!dev->mode_config.rotation_property) {
13832 unsigned long flags = BIT(DRM_ROTATE_0) |
13833 BIT(DRM_ROTATE_180);
13834
13835 if (INTEL_INFO(dev)->gen >= 9)
13836 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13837
13838 dev->mode_config.rotation_property =
13839 drm_mode_create_rotation_property(dev, flags);
13840 }
13841 if (dev->mode_config.rotation_property)
13842 drm_object_attach_property(&plane->base.base,
13843 dev->mode_config.rotation_property,
13844 plane->base.state->rotation);
13845}
13846
Matt Roper3d7d6512014-06-10 08:28:13 -070013847static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013848intel_check_cursor_plane(struct drm_plane *plane,
13849 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013850{
Matt Roper2b875c22014-12-01 15:40:13 -080013851 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013852 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013853 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013854 struct drm_rect *dest = &state->dst;
13855 struct drm_rect *src = &state->src;
13856 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013857 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013858 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013859 unsigned stride;
13860 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013861
Matt Roperea2c67b2014-12-23 10:41:52 -080013862 crtc = crtc ? crtc : plane->crtc;
13863 intel_crtc = to_intel_crtc(crtc);
13864
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013865 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013866 src, dest, clip,
13867 DRM_PLANE_HELPER_NO_SCALING,
13868 DRM_PLANE_HELPER_NO_SCALING,
13869 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013870 if (ret)
13871 return ret;
13872
13873
13874 /* if we want to turn off the cursor ignore width and height */
13875 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013876 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013877
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013878 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013879 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13880 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13881 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013882 return -EINVAL;
13883 }
13884
Matt Roperea2c67b2014-12-23 10:41:52 -080013885 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13886 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013887 DRM_DEBUG_KMS("buffer is too small\n");
13888 return -ENOMEM;
13889 }
13890
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013891 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013892 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13893 ret = -EINVAL;
13894 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013895
Matt Roper32b7eee2014-12-24 07:59:06 -080013896finish:
13897 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013898 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013899 intel_crtc->atomic.update_wm = true;
13900
13901 intel_crtc->atomic.fb_bits |=
13902 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13903 }
13904
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013905 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013906}
13907
Matt Roperf4a2cf22014-12-01 15:40:12 -080013908static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013909intel_disable_cursor_plane(struct drm_plane *plane,
13910 struct drm_crtc *crtc,
13911 bool force)
13912{
13913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13914
13915 if (!force) {
13916 plane->fb = NULL;
13917 intel_crtc->cursor_bo = NULL;
13918 intel_crtc->cursor_addr = 0;
13919 }
13920
13921 intel_crtc_update_cursor(crtc, false);
13922}
13923
13924static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013925intel_commit_cursor_plane(struct drm_plane *plane,
13926 struct intel_plane_state *state)
13927{
Matt Roper2b875c22014-12-01 15:40:13 -080013928 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013929 struct drm_device *dev = plane->dev;
13930 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013931 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013932 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013933
Matt Roperea2c67b2014-12-23 10:41:52 -080013934 crtc = crtc ? crtc : plane->crtc;
13935 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013936
Matt Roperea2c67b2014-12-23 10:41:52 -080013937 plane->fb = state->base.fb;
13938 crtc->cursor_x = state->base.crtc_x;
13939 crtc->cursor_y = state->base.crtc_y;
13940
Gustavo Padovana912f122014-12-01 15:40:10 -080013941 if (intel_crtc->cursor_bo == obj)
13942 goto update;
13943
Matt Roperf4a2cf22014-12-01 15:40:12 -080013944 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013945 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013946 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013947 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013948 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013949 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013950
Gustavo Padovana912f122014-12-01 15:40:10 -080013951 intel_crtc->cursor_addr = addr;
13952 intel_crtc->cursor_bo = obj;
13953update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013954
Matt Roper32b7eee2014-12-24 07:59:06 -080013955 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013956 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013957}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013958
Matt Roper3d7d6512014-06-10 08:28:13 -070013959static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13960 int pipe)
13961{
13962 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013963 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013964
13965 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13966 if (cursor == NULL)
13967 return NULL;
13968
Matt Roper8e7d6882015-01-21 16:35:41 -080013969 state = intel_create_plane_state(&cursor->base);
13970 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013971 kfree(cursor);
13972 return NULL;
13973 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013974 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013975
Matt Roper3d7d6512014-06-10 08:28:13 -070013976 cursor->can_scale = false;
13977 cursor->max_downscale = 1;
13978 cursor->pipe = pipe;
13979 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013980 cursor->check_plane = intel_check_cursor_plane;
13981 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013982 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013983
13984 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013985 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013986 intel_cursor_formats,
13987 ARRAY_SIZE(intel_cursor_formats),
13988 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013989
13990 if (INTEL_INFO(dev)->gen >= 4) {
13991 if (!dev->mode_config.rotation_property)
13992 dev->mode_config.rotation_property =
13993 drm_mode_create_rotation_property(dev,
13994 BIT(DRM_ROTATE_0) |
13995 BIT(DRM_ROTATE_180));
13996 if (dev->mode_config.rotation_property)
13997 drm_object_attach_property(&cursor->base.base,
13998 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013999 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014000 }
14001
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014002 if (INTEL_INFO(dev)->gen >=9)
14003 state->scaler_id = -1;
14004
Matt Roperea2c67b2014-12-23 10:41:52 -080014005 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14006
Matt Roper3d7d6512014-06-10 08:28:13 -070014007 return &cursor->base;
14008}
14009
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014010static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14011 struct intel_crtc_state *crtc_state)
14012{
14013 int i;
14014 struct intel_scaler *intel_scaler;
14015 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14016
14017 for (i = 0; i < intel_crtc->num_scalers; i++) {
14018 intel_scaler = &scaler_state->scalers[i];
14019 intel_scaler->in_use = 0;
14020 intel_scaler->id = i;
14021
14022 intel_scaler->mode = PS_SCALER_MODE_DYN;
14023 }
14024
14025 scaler_state->scaler_id = -1;
14026}
14027
Hannes Ederb358d0a2008-12-18 21:18:47 +010014028static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014029{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014030 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014031 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014032 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014033 struct drm_plane *primary = NULL;
14034 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014035 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014036
Daniel Vetter955382f2013-09-19 14:05:45 +020014037 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014038 if (intel_crtc == NULL)
14039 return;
14040
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014041 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14042 if (!crtc_state)
14043 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014044 intel_crtc->config = crtc_state;
14045 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014046 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014047
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014048 /* initialize shared scalers */
14049 if (INTEL_INFO(dev)->gen >= 9) {
14050 if (pipe == PIPE_C)
14051 intel_crtc->num_scalers = 1;
14052 else
14053 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14054
14055 skl_init_scalers(dev, intel_crtc, crtc_state);
14056 }
14057
Matt Roper465c1202014-05-29 08:06:54 -070014058 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014059 if (!primary)
14060 goto fail;
14061
14062 cursor = intel_cursor_plane_create(dev, pipe);
14063 if (!cursor)
14064 goto fail;
14065
Matt Roper465c1202014-05-29 08:06:54 -070014066 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014067 cursor, &intel_crtc_funcs);
14068 if (ret)
14069 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014070
14071 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014072 for (i = 0; i < 256; i++) {
14073 intel_crtc->lut_r[i] = i;
14074 intel_crtc->lut_g[i] = i;
14075 intel_crtc->lut_b[i] = i;
14076 }
14077
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014078 /*
14079 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014080 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014081 */
Jesse Barnes80824002009-09-10 15:28:06 -070014082 intel_crtc->pipe = pipe;
14083 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014084 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014085 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014086 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014087 }
14088
Chris Wilson4b0e3332014-05-30 16:35:26 +030014089 intel_crtc->cursor_base = ~0;
14090 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014091 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014092
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014093 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14094 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14095 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14096 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14097
Jesse Barnes79e53942008-11-07 14:24:08 -080014098 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014099
14100 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014101 return;
14102
14103fail:
14104 if (primary)
14105 drm_plane_cleanup(primary);
14106 if (cursor)
14107 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014108 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014109 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014110}
14111
Jesse Barnes752aa882013-10-31 18:55:49 +020014112enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14113{
14114 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014115 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014116
Rob Clark51fd3712013-11-19 12:10:12 -050014117 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014118
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014119 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014120 return INVALID_PIPE;
14121
14122 return to_intel_crtc(encoder->crtc)->pipe;
14123}
14124
Carl Worth08d7b3d2009-04-29 14:43:54 -070014125int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014126 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014127{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014128 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014129 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014130 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014131
Rob Clark7707e652014-07-17 23:30:04 -040014132 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014133
Rob Clark7707e652014-07-17 23:30:04 -040014134 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014135 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014136 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014137 }
14138
Rob Clark7707e652014-07-17 23:30:04 -040014139 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014140 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014141
Daniel Vetterc05422d2009-08-11 16:05:30 +020014142 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014143}
14144
Daniel Vetter66a92782012-07-12 20:08:18 +020014145static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014146{
Daniel Vetter66a92782012-07-12 20:08:18 +020014147 struct drm_device *dev = encoder->base.dev;
14148 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014149 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014150 int entry = 0;
14151
Damien Lespiaub2784e12014-08-05 11:29:37 +010014152 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014153 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014154 index_mask |= (1 << entry);
14155
Jesse Barnes79e53942008-11-07 14:24:08 -080014156 entry++;
14157 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014158
Jesse Barnes79e53942008-11-07 14:24:08 -080014159 return index_mask;
14160}
14161
Chris Wilson4d302442010-12-14 19:21:29 +000014162static bool has_edp_a(struct drm_device *dev)
14163{
14164 struct drm_i915_private *dev_priv = dev->dev_private;
14165
14166 if (!IS_MOBILE(dev))
14167 return false;
14168
14169 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14170 return false;
14171
Damien Lespiaue3589902014-02-07 19:12:50 +000014172 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014173 return false;
14174
14175 return true;
14176}
14177
Jesse Barnes84b4e042014-06-25 08:24:29 -070014178static bool intel_crt_present(struct drm_device *dev)
14179{
14180 struct drm_i915_private *dev_priv = dev->dev_private;
14181
Damien Lespiau884497e2013-12-03 13:56:23 +000014182 if (INTEL_INFO(dev)->gen >= 9)
14183 return false;
14184
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014185 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014186 return false;
14187
14188 if (IS_CHERRYVIEW(dev))
14189 return false;
14190
14191 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14192 return false;
14193
14194 return true;
14195}
14196
Jesse Barnes79e53942008-11-07 14:24:08 -080014197static void intel_setup_outputs(struct drm_device *dev)
14198{
Eric Anholt725e30a2009-01-22 13:01:02 -080014199 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014200 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014201 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014202
Daniel Vetterc9093352013-06-06 22:22:47 +020014203 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014204
Jesse Barnes84b4e042014-06-25 08:24:29 -070014205 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014206 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014207
Vandana Kannanc776eb22014-08-19 12:05:01 +053014208 if (IS_BROXTON(dev)) {
14209 /*
14210 * FIXME: Broxton doesn't support port detection via the
14211 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14212 * detect the ports.
14213 */
14214 intel_ddi_init(dev, PORT_A);
14215 intel_ddi_init(dev, PORT_B);
14216 intel_ddi_init(dev, PORT_C);
14217 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014218 int found;
14219
Jesse Barnesde31fac2015-03-06 15:53:32 -080014220 /*
14221 * Haswell uses DDI functions to detect digital outputs.
14222 * On SKL pre-D0 the strap isn't connected, so we assume
14223 * it's there.
14224 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014225 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014226 /* WaIgnoreDDIAStrap: skl */
14227 if (found ||
14228 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014229 intel_ddi_init(dev, PORT_A);
14230
14231 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14232 * register */
14233 found = I915_READ(SFUSE_STRAP);
14234
14235 if (found & SFUSE_STRAP_DDIB_DETECTED)
14236 intel_ddi_init(dev, PORT_B);
14237 if (found & SFUSE_STRAP_DDIC_DETECTED)
14238 intel_ddi_init(dev, PORT_C);
14239 if (found & SFUSE_STRAP_DDID_DETECTED)
14240 intel_ddi_init(dev, PORT_D);
14241 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014242 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014243 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014244
14245 if (has_edp_a(dev))
14246 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014247
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014248 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014249 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014250 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014251 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014252 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014253 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014254 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014255 }
14256
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014257 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014258 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014259
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014260 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014261 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014262
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014263 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014264 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014265
Daniel Vetter270b3042012-10-27 15:52:05 +020014266 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014267 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014268 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014269 /*
14270 * The DP_DETECTED bit is the latched state of the DDC
14271 * SDA pin at boot. However since eDP doesn't require DDC
14272 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14273 * eDP ports may have been muxed to an alternate function.
14274 * Thus we can't rely on the DP_DETECTED bit alone to detect
14275 * eDP ports. Consult the VBT as well as DP_DETECTED to
14276 * detect eDP ports.
14277 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014278 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14279 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014280 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14281 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014282 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14283 intel_dp_is_edp(dev, PORT_B))
14284 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014285
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014286 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14287 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014288 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14289 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014290 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14291 intel_dp_is_edp(dev, PORT_C))
14292 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014293
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014294 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014295 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014296 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14297 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014298 /* eDP not supported on port D, so don't check VBT */
14299 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14300 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014301 }
14302
Jani Nikula3cfca972013-08-27 15:12:26 +030014303 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014304 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014305 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014306
Paulo Zanonie2debe92013-02-18 19:00:27 -030014307 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014308 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014309 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014310 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14311 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014312 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014313 }
Ma Ling27185ae2009-08-24 13:50:23 +080014314
Imre Deake7281ea2013-05-08 13:14:08 +030014315 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014316 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014317 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014318
14319 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014320
Paulo Zanonie2debe92013-02-18 19:00:27 -030014321 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014322 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014323 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014324 }
Ma Ling27185ae2009-08-24 13:50:23 +080014325
Paulo Zanonie2debe92013-02-18 19:00:27 -030014326 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014327
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014328 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14329 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014330 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014331 }
Imre Deake7281ea2013-05-08 13:14:08 +030014332 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014333 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014334 }
Ma Ling27185ae2009-08-24 13:50:23 +080014335
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014336 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014337 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014338 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014339 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014340 intel_dvo_init(dev);
14341
Zhenyu Wang103a1962009-11-27 11:44:36 +080014342 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014343 intel_tv_init(dev);
14344
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014345 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014346
Damien Lespiaub2784e12014-08-05 11:29:37 +010014347 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014348 encoder->base.possible_crtcs = encoder->crtc_mask;
14349 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014350 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014351 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014352
Paulo Zanonidde86e22012-12-01 12:04:25 -020014353 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014354
14355 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014356}
14357
14358static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14359{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014360 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014361 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014362
Daniel Vetteref2d6332014-02-10 18:00:38 +010014363 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014364 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014365 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014366 drm_gem_object_unreference(&intel_fb->obj->base);
14367 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014368 kfree(intel_fb);
14369}
14370
14371static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014372 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014373 unsigned int *handle)
14374{
14375 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014376 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014377
Chris Wilson05394f32010-11-08 19:18:58 +000014378 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014379}
14380
14381static const struct drm_framebuffer_funcs intel_fb_funcs = {
14382 .destroy = intel_user_framebuffer_destroy,
14383 .create_handle = intel_user_framebuffer_create_handle,
14384};
14385
Damien Lespiaub3218032015-02-27 11:15:18 +000014386static
14387u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14388 uint32_t pixel_format)
14389{
14390 u32 gen = INTEL_INFO(dev)->gen;
14391
14392 if (gen >= 9) {
14393 /* "The stride in bytes must not exceed the of the size of 8K
14394 * pixels and 32K bytes."
14395 */
14396 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14397 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14398 return 32*1024;
14399 } else if (gen >= 4) {
14400 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14401 return 16*1024;
14402 else
14403 return 32*1024;
14404 } else if (gen >= 3) {
14405 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14406 return 8*1024;
14407 else
14408 return 16*1024;
14409 } else {
14410 /* XXX DSPC is limited to 4k tiled */
14411 return 8*1024;
14412 }
14413}
14414
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014415static int intel_framebuffer_init(struct drm_device *dev,
14416 struct intel_framebuffer *intel_fb,
14417 struct drm_mode_fb_cmd2 *mode_cmd,
14418 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014419{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014420 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014421 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014422 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014423
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014424 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14425
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014426 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14427 /* Enforce that fb modifier and tiling mode match, but only for
14428 * X-tiled. This is needed for FBC. */
14429 if (!!(obj->tiling_mode == I915_TILING_X) !=
14430 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14431 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14432 return -EINVAL;
14433 }
14434 } else {
14435 if (obj->tiling_mode == I915_TILING_X)
14436 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14437 else if (obj->tiling_mode == I915_TILING_Y) {
14438 DRM_DEBUG("No Y tiling for legacy addfb\n");
14439 return -EINVAL;
14440 }
14441 }
14442
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014443 /* Passed in modifier sanity checking. */
14444 switch (mode_cmd->modifier[0]) {
14445 case I915_FORMAT_MOD_Y_TILED:
14446 case I915_FORMAT_MOD_Yf_TILED:
14447 if (INTEL_INFO(dev)->gen < 9) {
14448 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14449 mode_cmd->modifier[0]);
14450 return -EINVAL;
14451 }
14452 case DRM_FORMAT_MOD_NONE:
14453 case I915_FORMAT_MOD_X_TILED:
14454 break;
14455 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014456 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14457 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014458 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014459 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014460
Damien Lespiaub3218032015-02-27 11:15:18 +000014461 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14462 mode_cmd->pixel_format);
14463 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14464 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14465 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014466 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014467 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014468
Damien Lespiaub3218032015-02-27 11:15:18 +000014469 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14470 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014471 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014472 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14473 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014474 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014475 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014476 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014477 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014478
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014479 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014480 mode_cmd->pitches[0] != obj->stride) {
14481 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14482 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014483 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014484 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014485
Ville Syrjälä57779d02012-10-31 17:50:14 +020014486 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014487 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014488 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014489 case DRM_FORMAT_RGB565:
14490 case DRM_FORMAT_XRGB8888:
14491 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014492 break;
14493 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014494 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014495 DRM_DEBUG("unsupported pixel format: %s\n",
14496 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014497 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014498 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014499 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014500 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014501 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14502 DRM_DEBUG("unsupported pixel format: %s\n",
14503 drm_get_format_name(mode_cmd->pixel_format));
14504 return -EINVAL;
14505 }
14506 break;
14507 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014508 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014509 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014510 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014511 DRM_DEBUG("unsupported pixel format: %s\n",
14512 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014513 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014514 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014515 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014516 case DRM_FORMAT_ABGR2101010:
14517 if (!IS_VALLEYVIEW(dev)) {
14518 DRM_DEBUG("unsupported pixel format: %s\n",
14519 drm_get_format_name(mode_cmd->pixel_format));
14520 return -EINVAL;
14521 }
14522 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014523 case DRM_FORMAT_YUYV:
14524 case DRM_FORMAT_UYVY:
14525 case DRM_FORMAT_YVYU:
14526 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014527 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014528 DRM_DEBUG("unsupported pixel format: %s\n",
14529 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014530 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014531 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014532 break;
14533 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014534 DRM_DEBUG("unsupported pixel format: %s\n",
14535 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014536 return -EINVAL;
14537 }
14538
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014539 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14540 if (mode_cmd->offsets[0] != 0)
14541 return -EINVAL;
14542
Damien Lespiauec2c9812015-01-20 12:51:45 +000014543 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014544 mode_cmd->pixel_format,
14545 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014546 /* FIXME drm helper for size checks (especially planar formats)? */
14547 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14548 return -EINVAL;
14549
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014550 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14551 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014552 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014553
Jesse Barnes79e53942008-11-07 14:24:08 -080014554 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14555 if (ret) {
14556 DRM_ERROR("framebuffer init failed %d\n", ret);
14557 return ret;
14558 }
14559
Jesse Barnes79e53942008-11-07 14:24:08 -080014560 return 0;
14561}
14562
Jesse Barnes79e53942008-11-07 14:24:08 -080014563static struct drm_framebuffer *
14564intel_user_framebuffer_create(struct drm_device *dev,
14565 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014566 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014567{
Chris Wilson05394f32010-11-08 19:18:58 +000014568 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014569
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014570 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14571 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014572 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014573 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014574
Chris Wilsond2dff872011-04-19 08:36:26 +010014575 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014576}
14577
Daniel Vetter4520f532013-10-09 09:18:51 +020014578#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014579static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014580{
14581}
14582#endif
14583
Jesse Barnes79e53942008-11-07 14:24:08 -080014584static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014585 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014586 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014587 .atomic_check = intel_atomic_check,
14588 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014589 .atomic_state_alloc = intel_atomic_state_alloc,
14590 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014591};
14592
Jesse Barnese70236a2009-09-21 10:42:27 -070014593/* Set up chip specific display functions */
14594static void intel_init_display(struct drm_device *dev)
14595{
14596 struct drm_i915_private *dev_priv = dev->dev_private;
14597
Daniel Vetteree9300b2013-06-03 22:40:22 +020014598 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14599 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014600 else if (IS_CHERRYVIEW(dev))
14601 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014602 else if (IS_VALLEYVIEW(dev))
14603 dev_priv->display.find_dpll = vlv_find_best_dpll;
14604 else if (IS_PINEVIEW(dev))
14605 dev_priv->display.find_dpll = pnv_find_best_dpll;
14606 else
14607 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14608
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014609 if (INTEL_INFO(dev)->gen >= 9) {
14610 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014611 dev_priv->display.get_initial_plane_config =
14612 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014613 dev_priv->display.crtc_compute_clock =
14614 haswell_crtc_compute_clock;
14615 dev_priv->display.crtc_enable = haswell_crtc_enable;
14616 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014617 dev_priv->display.update_primary_plane =
14618 skylake_update_primary_plane;
14619 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014620 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014621 dev_priv->display.get_initial_plane_config =
14622 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014623 dev_priv->display.crtc_compute_clock =
14624 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014625 dev_priv->display.crtc_enable = haswell_crtc_enable;
14626 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014627 dev_priv->display.update_primary_plane =
14628 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014629 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014630 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014631 dev_priv->display.get_initial_plane_config =
14632 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014633 dev_priv->display.crtc_compute_clock =
14634 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014635 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14636 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014637 dev_priv->display.update_primary_plane =
14638 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014639 } else if (IS_VALLEYVIEW(dev)) {
14640 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014641 dev_priv->display.get_initial_plane_config =
14642 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014643 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014644 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14645 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014646 dev_priv->display.update_primary_plane =
14647 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014648 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014649 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014650 dev_priv->display.get_initial_plane_config =
14651 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014652 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014653 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14654 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014655 dev_priv->display.update_primary_plane =
14656 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014657 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014658
Jesse Barnese70236a2009-09-21 10:42:27 -070014659 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014660 if (IS_SKYLAKE(dev))
14661 dev_priv->display.get_display_clock_speed =
14662 skylake_get_display_clock_speed;
14663 else if (IS_BROADWELL(dev))
14664 dev_priv->display.get_display_clock_speed =
14665 broadwell_get_display_clock_speed;
14666 else if (IS_HASWELL(dev))
14667 dev_priv->display.get_display_clock_speed =
14668 haswell_get_display_clock_speed;
14669 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014670 dev_priv->display.get_display_clock_speed =
14671 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014672 else if (IS_GEN5(dev))
14673 dev_priv->display.get_display_clock_speed =
14674 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014675 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014676 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014677 dev_priv->display.get_display_clock_speed =
14678 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014679 else if (IS_GM45(dev))
14680 dev_priv->display.get_display_clock_speed =
14681 gm45_get_display_clock_speed;
14682 else if (IS_CRESTLINE(dev))
14683 dev_priv->display.get_display_clock_speed =
14684 i965gm_get_display_clock_speed;
14685 else if (IS_PINEVIEW(dev))
14686 dev_priv->display.get_display_clock_speed =
14687 pnv_get_display_clock_speed;
14688 else if (IS_G33(dev) || IS_G4X(dev))
14689 dev_priv->display.get_display_clock_speed =
14690 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014691 else if (IS_I915G(dev))
14692 dev_priv->display.get_display_clock_speed =
14693 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014694 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014695 dev_priv->display.get_display_clock_speed =
14696 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014697 else if (IS_PINEVIEW(dev))
14698 dev_priv->display.get_display_clock_speed =
14699 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014700 else if (IS_I915GM(dev))
14701 dev_priv->display.get_display_clock_speed =
14702 i915gm_get_display_clock_speed;
14703 else if (IS_I865G(dev))
14704 dev_priv->display.get_display_clock_speed =
14705 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014706 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014707 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014708 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014709 else { /* 830 */
14710 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014711 dev_priv->display.get_display_clock_speed =
14712 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014713 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014714
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014715 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014716 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014717 } else if (IS_GEN6(dev)) {
14718 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014719 } else if (IS_IVYBRIDGE(dev)) {
14720 /* FIXME: detect B0+ stepping and use auto training */
14721 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014722 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014723 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030014724 if (IS_BROADWELL(dev))
14725 dev_priv->display.modeset_global_resources =
14726 broadwell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014727 } else if (IS_VALLEYVIEW(dev)) {
14728 dev_priv->display.modeset_global_resources =
14729 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014730 } else if (IS_BROXTON(dev)) {
14731 dev_priv->display.modeset_global_resources =
14732 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014733 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014734
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014735 switch (INTEL_INFO(dev)->gen) {
14736 case 2:
14737 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14738 break;
14739
14740 case 3:
14741 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14742 break;
14743
14744 case 4:
14745 case 5:
14746 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14747 break;
14748
14749 case 6:
14750 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14751 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014752 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014753 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014754 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14755 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014756 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014757 /* Drop through - unsupported since execlist only. */
14758 default:
14759 /* Default just returns -ENODEV to indicate unsupported */
14760 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014761 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014762
14763 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014764
14765 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014766}
14767
Jesse Barnesb690e962010-07-19 13:53:12 -070014768/*
14769 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14770 * resume, or other times. This quirk makes sure that's the case for
14771 * affected systems.
14772 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014773static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014774{
14775 struct drm_i915_private *dev_priv = dev->dev_private;
14776
14777 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014778 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014779}
14780
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014781static void quirk_pipeb_force(struct drm_device *dev)
14782{
14783 struct drm_i915_private *dev_priv = dev->dev_private;
14784
14785 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14786 DRM_INFO("applying pipe b force quirk\n");
14787}
14788
Keith Packard435793d2011-07-12 14:56:22 -070014789/*
14790 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14791 */
14792static void quirk_ssc_force_disable(struct drm_device *dev)
14793{
14794 struct drm_i915_private *dev_priv = dev->dev_private;
14795 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014796 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014797}
14798
Carsten Emde4dca20e2012-03-15 15:56:26 +010014799/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014800 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14801 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014802 */
14803static void quirk_invert_brightness(struct drm_device *dev)
14804{
14805 struct drm_i915_private *dev_priv = dev->dev_private;
14806 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014807 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014808}
14809
Scot Doyle9c72cc62014-07-03 23:27:50 +000014810/* Some VBT's incorrectly indicate no backlight is present */
14811static void quirk_backlight_present(struct drm_device *dev)
14812{
14813 struct drm_i915_private *dev_priv = dev->dev_private;
14814 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14815 DRM_INFO("applying backlight present quirk\n");
14816}
14817
Jesse Barnesb690e962010-07-19 13:53:12 -070014818struct intel_quirk {
14819 int device;
14820 int subsystem_vendor;
14821 int subsystem_device;
14822 void (*hook)(struct drm_device *dev);
14823};
14824
Egbert Eich5f85f172012-10-14 15:46:38 +020014825/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14826struct intel_dmi_quirk {
14827 void (*hook)(struct drm_device *dev);
14828 const struct dmi_system_id (*dmi_id_list)[];
14829};
14830
14831static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14832{
14833 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14834 return 1;
14835}
14836
14837static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14838 {
14839 .dmi_id_list = &(const struct dmi_system_id[]) {
14840 {
14841 .callback = intel_dmi_reverse_brightness,
14842 .ident = "NCR Corporation",
14843 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14844 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14845 },
14846 },
14847 { } /* terminating entry */
14848 },
14849 .hook = quirk_invert_brightness,
14850 },
14851};
14852
Ben Widawskyc43b5632012-04-16 14:07:40 -070014853static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014854 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14855 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14856
Jesse Barnesb690e962010-07-19 13:53:12 -070014857 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14858 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14859
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014860 /* 830 needs to leave pipe A & dpll A up */
14861 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14862
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014863 /* 830 needs to leave pipe B & dpll B up */
14864 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14865
Keith Packard435793d2011-07-12 14:56:22 -070014866 /* Lenovo U160 cannot use SSC on LVDS */
14867 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014868
14869 /* Sony Vaio Y cannot use SSC on LVDS */
14870 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014871
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014872 /* Acer Aspire 5734Z must invert backlight brightness */
14873 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14874
14875 /* Acer/eMachines G725 */
14876 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14877
14878 /* Acer/eMachines e725 */
14879 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14880
14881 /* Acer/Packard Bell NCL20 */
14882 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14883
14884 /* Acer Aspire 4736Z */
14885 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014886
14887 /* Acer Aspire 5336 */
14888 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014889
14890 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14891 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014892
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014893 /* Acer C720 Chromebook (Core i3 4005U) */
14894 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14895
jens steinb2a96012014-10-28 20:25:53 +010014896 /* Apple Macbook 2,1 (Core 2 T7400) */
14897 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14898
Scot Doyled4967d82014-07-03 23:27:52 +000014899 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14900 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014901
14902 /* HP Chromebook 14 (Celeron 2955U) */
14903 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014904
14905 /* Dell Chromebook 11 */
14906 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014907};
14908
14909static void intel_init_quirks(struct drm_device *dev)
14910{
14911 struct pci_dev *d = dev->pdev;
14912 int i;
14913
14914 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14915 struct intel_quirk *q = &intel_quirks[i];
14916
14917 if (d->device == q->device &&
14918 (d->subsystem_vendor == q->subsystem_vendor ||
14919 q->subsystem_vendor == PCI_ANY_ID) &&
14920 (d->subsystem_device == q->subsystem_device ||
14921 q->subsystem_device == PCI_ANY_ID))
14922 q->hook(dev);
14923 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014924 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14925 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14926 intel_dmi_quirks[i].hook(dev);
14927 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014928}
14929
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014930/* Disable the VGA plane that we never use */
14931static void i915_disable_vga(struct drm_device *dev)
14932{
14933 struct drm_i915_private *dev_priv = dev->dev_private;
14934 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014935 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014936
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014937 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014938 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014939 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014940 sr1 = inb(VGA_SR_DATA);
14941 outb(sr1 | 1<<5, VGA_SR_DATA);
14942 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14943 udelay(300);
14944
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014945 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014946 POSTING_READ(vga_reg);
14947}
14948
Daniel Vetterf8175862012-04-10 15:50:11 +020014949void intel_modeset_init_hw(struct drm_device *dev)
14950{
Ville Syrjäläb6283052015-06-03 15:45:07 +030014951 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014952 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014953 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014954 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014955}
14956
Jesse Barnes79e53942008-11-07 14:24:08 -080014957void intel_modeset_init(struct drm_device *dev)
14958{
Jesse Barnes652c3932009-08-17 13:31:43 -070014959 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014960 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014961 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014962 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014963
14964 drm_mode_config_init(dev);
14965
14966 dev->mode_config.min_width = 0;
14967 dev->mode_config.min_height = 0;
14968
Dave Airlie019d96c2011-09-29 16:20:42 +010014969 dev->mode_config.preferred_depth = 24;
14970 dev->mode_config.prefer_shadow = 1;
14971
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014972 dev->mode_config.allow_fb_modifiers = true;
14973
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014974 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014975
Jesse Barnesb690e962010-07-19 13:53:12 -070014976 intel_init_quirks(dev);
14977
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014978 intel_init_pm(dev);
14979
Ben Widawskye3c74752013-04-05 13:12:39 -070014980 if (INTEL_INFO(dev)->num_pipes == 0)
14981 return;
14982
Jesse Barnese70236a2009-09-21 10:42:27 -070014983 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014984 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014985
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014986 if (IS_GEN2(dev)) {
14987 dev->mode_config.max_width = 2048;
14988 dev->mode_config.max_height = 2048;
14989 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014990 dev->mode_config.max_width = 4096;
14991 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014992 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014993 dev->mode_config.max_width = 8192;
14994 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014995 }
Damien Lespiau068be562014-03-28 14:17:49 +000014996
Ville Syrjälädc41c152014-08-13 11:57:05 +030014997 if (IS_845G(dev) || IS_I865G(dev)) {
14998 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14999 dev->mode_config.cursor_height = 1023;
15000 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015001 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15002 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15003 } else {
15004 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15005 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15006 }
15007
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015008 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015009
Zhao Yakui28c97732009-10-09 11:39:41 +080015010 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015011 INTEL_INFO(dev)->num_pipes,
15012 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015013
Damien Lespiau055e3932014-08-18 13:49:10 +010015014 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015015 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015016 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015017 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015018 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015019 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015020 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015021 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015022 }
15023
Jesse Barnesf42bb702013-12-16 16:34:23 -080015024 intel_init_dpio(dev);
15025
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015026 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015027
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015028 /* Just disable it once at startup */
15029 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015030 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015031
15032 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015033 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015034
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015035 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015036 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015037 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015038
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015039 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080015040 if (!crtc->active)
15041 continue;
15042
Jesse Barnes46f297f2014-03-07 08:57:48 -080015043 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015044 * Note that reserving the BIOS fb up front prevents us
15045 * from stuffing other stolen allocations like the ring
15046 * on top. This prevents some ugliness at boot time, and
15047 * can even allow for smooth boot transitions if the BIOS
15048 * fb is large enough for the active pipe configuration.
15049 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015050 if (dev_priv->display.get_initial_plane_config) {
15051 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080015052 &crtc->plane_config);
15053 /*
15054 * If the fb is shared between multiple heads, we'll
15055 * just get the first one.
15056 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010015057 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015058 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080015059 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015060}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015061
Daniel Vetter7fad7982012-07-04 17:51:47 +020015062static void intel_enable_pipe_a(struct drm_device *dev)
15063{
15064 struct intel_connector *connector;
15065 struct drm_connector *crt = NULL;
15066 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015067 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015068
15069 /* We can't just switch on the pipe A, we need to set things up with a
15070 * proper mode and output configuration. As a gross hack, enable pipe A
15071 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015072 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015073 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15074 crt = &connector->base;
15075 break;
15076 }
15077 }
15078
15079 if (!crt)
15080 return;
15081
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015082 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015083 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015084}
15085
Daniel Vetterfa555832012-10-10 23:14:00 +020015086static bool
15087intel_check_plane_mapping(struct intel_crtc *crtc)
15088{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015089 struct drm_device *dev = crtc->base.dev;
15090 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015091 u32 reg, val;
15092
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015093 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015094 return true;
15095
15096 reg = DSPCNTR(!crtc->plane);
15097 val = I915_READ(reg);
15098
15099 if ((val & DISPLAY_PLANE_ENABLE) &&
15100 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15101 return false;
15102
15103 return true;
15104}
15105
Daniel Vetter24929352012-07-02 20:28:59 +020015106static void intel_sanitize_crtc(struct intel_crtc *crtc)
15107{
15108 struct drm_device *dev = crtc->base.dev;
15109 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015110 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020015111
Daniel Vetter24929352012-07-02 20:28:59 +020015112 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015113 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015114 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15115
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015116 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015117 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015118 if (crtc->active) {
15119 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015120 drm_crtc_vblank_on(&crtc->base);
15121 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015122
Daniel Vetter24929352012-07-02 20:28:59 +020015123 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015124 * disable the crtc (and hence change the state) if it is wrong. Note
15125 * that gen4+ has a fixed plane -> pipe mapping. */
15126 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015127 struct intel_connector *connector;
15128 bool plane;
15129
Daniel Vetter24929352012-07-02 20:28:59 +020015130 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15131 crtc->base.base.id);
15132
15133 /* Pipe has the wrong plane attached and the plane is active.
15134 * Temporarily change the plane mapping and disable everything
15135 * ... */
15136 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015137 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015138 crtc->base.primary->crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015139 crtc->plane = !plane;
Maarten Lankhorst1b509252015-06-01 12:49:48 +020015140 intel_crtc_control(&crtc->base, false);
Daniel Vetter24929352012-07-02 20:28:59 +020015141 crtc->plane = plane;
15142
15143 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015144 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015145 if (connector->encoder->base.crtc != &crtc->base)
15146 continue;
15147
Egbert Eich7f1950f2014-04-25 10:56:22 +020015148 connector->base.dpms = DRM_MODE_DPMS_OFF;
15149 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015150 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015151 /* multiple connectors may have the same encoder:
15152 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015153 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020015154 if (connector->encoder->base.crtc == &crtc->base) {
15155 connector->encoder->base.crtc = NULL;
15156 connector->encoder->connectors_active = false;
15157 }
Daniel Vetter24929352012-07-02 20:28:59 +020015158
15159 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080015160 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015161 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015162 crtc->base.enabled = false;
15163 }
Daniel Vetter24929352012-07-02 20:28:59 +020015164
Daniel Vetter7fad7982012-07-04 17:51:47 +020015165 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15166 crtc->pipe == PIPE_A && !crtc->active) {
15167 /* BIOS forgot to enable pipe A, this mostly happens after
15168 * resume. Force-enable the pipe to fix this, the update_dpms
15169 * call below we restore the pipe to the right state, but leave
15170 * the required bits on. */
15171 intel_enable_pipe_a(dev);
15172 }
15173
Daniel Vetter24929352012-07-02 20:28:59 +020015174 /* Adjust the state of the output pipe according to whether we
15175 * have active connectors/encoders. */
15176 intel_crtc_update_dpms(&crtc->base);
15177
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015178 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015179 struct intel_encoder *encoder;
15180
15181 /* This can happen either due to bugs in the get_hw_state
15182 * functions or because the pipe is force-enabled due to the
15183 * pipe A quirk. */
15184 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15185 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015186 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015187 crtc->active ? "enabled" : "disabled");
15188
Matt Roper83d65732015-02-25 13:12:16 -080015189 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015190 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015191 crtc->base.enabled = crtc->active;
15192
15193 /* Because we only establish the connector -> encoder ->
15194 * crtc links if something is active, this means the
15195 * crtc is now deactivated. Break the links. connector
15196 * -> encoder links are only establish when things are
15197 * actually up, hence no need to break them. */
15198 WARN_ON(crtc->active);
15199
15200 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15201 WARN_ON(encoder->connectors_active);
15202 encoder->base.crtc = NULL;
15203 }
15204 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015205
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015206 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015207 /*
15208 * We start out with underrun reporting disabled to avoid races.
15209 * For correct bookkeeping mark this on active crtcs.
15210 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015211 * Also on gmch platforms we dont have any hardware bits to
15212 * disable the underrun reporting. Which means we need to start
15213 * out with underrun reporting disabled also on inactive pipes,
15214 * since otherwise we'll complain about the garbage we read when
15215 * e.g. coming up after runtime pm.
15216 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015217 * No protection against concurrent access is required - at
15218 * worst a fifo underrun happens which also sets this to false.
15219 */
15220 crtc->cpu_fifo_underrun_disabled = true;
15221 crtc->pch_fifo_underrun_disabled = true;
15222 }
Daniel Vetter24929352012-07-02 20:28:59 +020015223}
15224
15225static void intel_sanitize_encoder(struct intel_encoder *encoder)
15226{
15227 struct intel_connector *connector;
15228 struct drm_device *dev = encoder->base.dev;
15229
15230 /* We need to check both for a crtc link (meaning that the
15231 * encoder is active and trying to read from a pipe) and the
15232 * pipe itself being active. */
15233 bool has_active_crtc = encoder->base.crtc &&
15234 to_intel_crtc(encoder->base.crtc)->active;
15235
15236 if (encoder->connectors_active && !has_active_crtc) {
15237 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15238 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015239 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015240
15241 /* Connector is active, but has no active pipe. This is
15242 * fallout from our resume register restoring. Disable
15243 * the encoder manually again. */
15244 if (encoder->base.crtc) {
15245 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15246 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015247 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015248 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015249 if (encoder->post_disable)
15250 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015251 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015252 encoder->base.crtc = NULL;
15253 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015254
15255 /* Inconsistent output/port/pipe state happens presumably due to
15256 * a bug in one of the get_hw_state functions. Or someplace else
15257 * in our code, like the register restore mess on resume. Clamp
15258 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015259 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015260 if (connector->encoder != encoder)
15261 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015262 connector->base.dpms = DRM_MODE_DPMS_OFF;
15263 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015264 }
15265 }
15266 /* Enabled encoders without active connectors will be fixed in
15267 * the crtc fixup. */
15268}
15269
Imre Deak04098752014-02-18 00:02:16 +020015270void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015271{
15272 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015273 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015274
Imre Deak04098752014-02-18 00:02:16 +020015275 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15276 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15277 i915_disable_vga(dev);
15278 }
15279}
15280
15281void i915_redisable_vga(struct drm_device *dev)
15282{
15283 struct drm_i915_private *dev_priv = dev->dev_private;
15284
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015285 /* This function can be called both from intel_modeset_setup_hw_state or
15286 * at a very early point in our resume sequence, where the power well
15287 * structures are not yet restored. Since this function is at a very
15288 * paranoid "someone might have enabled VGA while we were not looking"
15289 * level, just check if the power well is enabled instead of trying to
15290 * follow the "don't touch the power well if we don't need it" policy
15291 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015292 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015293 return;
15294
Imre Deak04098752014-02-18 00:02:16 +020015295 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015296}
15297
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015298static bool primary_get_hw_state(struct intel_crtc *crtc)
15299{
15300 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15301
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015302 if (!crtc->base.enabled)
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015303 return false;
15304
15305 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15306}
15307
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015308static int readout_hw_crtc_state(struct drm_atomic_state *state,
15309 struct intel_crtc *crtc)
Daniel Vetter24929352012-07-02 20:28:59 +020015310{
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015311 struct drm_i915_private *dev_priv = to_i915(state->dev);
15312 struct intel_crtc_state *crtc_state;
15313 struct drm_plane *primary = crtc->base.primary;
15314 struct drm_plane_state *drm_plane_state;
15315 struct intel_plane_state *plane_state;
15316 int ret;
15317
15318 crtc_state = intel_atomic_get_crtc_state(state, crtc);
15319 if (IS_ERR(crtc_state))
15320 return PTR_ERR(crtc_state);
15321
15322 ret = drm_atomic_add_affected_planes(state, &crtc->base);
15323 if (ret)
15324 return ret;
15325
15326 memset(crtc_state, 0, sizeof(*crtc_state));
15327 crtc_state->base.crtc = &crtc->base;
15328 crtc_state->base.state = state;
15329
15330 crtc_state->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
15331
15332 crtc_state->base.enable = crtc_state->base.active =
15333 crtc->base.enabled = dev_priv->display.get_pipe_config(crtc, crtc_state);
15334
15335 /* update transitional state */
15336 crtc->active = crtc_state->base.active;
15337 crtc->config = crtc_state;
15338
15339 drm_plane_state = drm_atomic_get_plane_state(state, primary);
15340 if (IS_ERR(drm_plane_state))
15341 return PTR_ERR(drm_plane_state);
15342
15343 plane_state = to_intel_plane_state(drm_plane_state);
15344 plane_state->visible = primary_get_hw_state(crtc);
15345
15346 if (plane_state->visible) {
15347 primary->crtc = &crtc->base;
15348 crtc_state->base.plane_mask |= 1 << drm_plane_index(primary);
15349 } else
15350 crtc_state->base.plane_mask &= ~(1 << drm_plane_index(primary));
15351
15352 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15353 crtc->base.base.id,
15354 crtc_state->base.active ? "enabled" : "disabled");
15355
15356 return 0;
15357}
15358
15359static int readout_hw_pll_state(struct drm_atomic_state *state)
15360{
15361 struct drm_i915_private *dev_priv = to_i915(state->dev);
15362 struct intel_shared_dpll_config *shared_dpll;
Daniel Vetter24929352012-07-02 20:28:59 +020015363 struct intel_crtc *crtc;
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015364 struct intel_crtc_state *crtc_state;
Daniel Vetter53589012013-06-05 13:34:16 +020015365 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015366
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015367 shared_dpll = intel_atomic_get_shared_dpll_state(state);
Daniel Vetter53589012013-06-05 13:34:16 +020015368 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15369 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15370
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015371 pll->on = pll->get_hw_state(dev_priv, pll,
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015372 &shared_dpll[i].hw_state);
15373
Daniel Vetter53589012013-06-05 13:34:16 +020015374 pll->active = 0;
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015375 shared_dpll[i].crtc_mask = 0;
15376
15377 for_each_intel_crtc(state->dev, crtc) {
15378 crtc_state = intel_atomic_get_crtc_state(state, crtc);
15379 if (IS_ERR(crtc_state))
15380 return PTR_ERR(crtc_state);
15381
15382 if (crtc_state->base.active &&
15383 crtc_state->shared_dpll == i) {
Daniel Vetter53589012013-06-05 13:34:16 +020015384 pll->active++;
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015385 shared_dpll[i].crtc_mask |=
15386 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015387 }
Daniel Vetter53589012013-06-05 13:34:16 +020015388 }
Daniel Vetter53589012013-06-05 13:34:16 +020015389
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015390 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015391 pll->name, shared_dpll[i].crtc_mask,
15392 pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015393
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015394 if (shared_dpll[i].crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015395 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015396 }
15397
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015398 return 0;
15399}
15400
15401static struct drm_connector_state *
15402get_connector_state_for_encoder(struct drm_atomic_state *state,
15403 struct intel_encoder *encoder)
15404{
15405 struct drm_connector *connector;
15406 struct drm_connector_state *connector_state;
15407 int i;
15408
15409 for_each_connector_in_state(state, connector, connector_state, i)
15410 if (connector_state->best_encoder == &encoder->base)
15411 return connector_state;
15412
15413 return NULL;
15414}
15415
15416static int readout_hw_connector_encoder_state(struct drm_atomic_state *state)
15417{
15418 struct drm_device *dev = state->dev;
15419 struct drm_i915_private *dev_priv = to_i915(state->dev);
15420 struct intel_crtc *crtc;
15421 struct drm_crtc_state *drm_crtc_state;
15422 struct intel_crtc_state *crtc_state;
15423 struct intel_encoder *encoder;
15424 struct intel_connector *connector;
15425 struct drm_connector_state *connector_state;
15426 enum pipe pipe;
15427
15428 for_each_intel_connector(dev, connector) {
15429 connector_state =
15430 drm_atomic_get_connector_state(state, &connector->base);
15431 if (IS_ERR(connector_state))
15432 return PTR_ERR(connector_state);
15433
15434 if (connector->get_hw_state(connector)) {
15435 connector->base.dpms = DRM_MODE_DPMS_ON;
15436 connector->base.encoder = &connector->encoder->base;
15437 } else {
15438 connector->base.dpms = DRM_MODE_DPMS_OFF;
15439 connector->base.encoder = NULL;
15440 }
15441
15442 /* We'll update the crtc field when reading encoder state */
15443 connector_state->crtc = NULL;
15444
15445 connector_state->best_encoder = connector->base.encoder;
15446
15447 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15448 connector->base.base.id,
15449 connector->base.name,
15450 connector->base.encoder ? "enabled" : "disabled");
15451 }
15452
Damien Lespiaub2784e12014-08-05 11:29:37 +010015453 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015454 pipe = 0;
15455
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015456 connector_state =
15457 get_connector_state_for_encoder(state, encoder);
15458
15459 encoder->connectors_active = !!connector_state;
15460
Daniel Vetter24929352012-07-02 20:28:59 +020015461 if (encoder->get_hw_state(encoder, &pipe)) {
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015462 encoder->base.crtc =
15463 dev_priv->pipe_to_crtc_mapping[pipe];
15464 crtc = to_intel_crtc(encoder->base.crtc);
15465
15466 drm_crtc_state =
15467 state->crtc_states[drm_crtc_index(&crtc->base)];
15468 crtc_state = to_intel_crtc_state(drm_crtc_state);
15469
15470 encoder->get_config(encoder, crtc_state);
15471
15472 if (connector_state)
15473 connector_state->crtc = &crtc->base;
Daniel Vetter24929352012-07-02 20:28:59 +020015474 } else {
15475 encoder->base.crtc = NULL;
15476 }
15477
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015478 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015479 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015480 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015481 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015482 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015483 }
15484
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015485 return 0;
15486}
15487
15488static struct drm_atomic_state *
15489intel_modeset_readout_hw_state(struct drm_device *dev)
15490{
15491 struct intel_crtc *crtc;
15492 int ret = 0;
15493
15494 struct drm_atomic_state *state;
15495
15496 state = drm_atomic_state_alloc(dev);
15497 if (!state)
15498 return ERR_PTR(-ENOMEM);
15499
15500 state->acquire_ctx = dev->mode_config.acquire_ctx;
15501
15502 for_each_intel_crtc(dev, crtc) {
15503 ret = readout_hw_crtc_state(state, crtc);
15504 if (ret)
15505 goto err_free;
Daniel Vetter24929352012-07-02 20:28:59 +020015506 }
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015507
15508 ret = readout_hw_pll_state(state);
15509 if (ret)
15510 goto err_free;
15511
15512 ret = readout_hw_connector_encoder_state(state);
15513 if (ret)
15514 goto err_free;
15515
15516 return state;
15517
15518err_free:
15519 drm_atomic_state_free(state);
15520 return ERR_PTR(ret);
Daniel Vetter30e984d2013-06-05 13:34:17 +020015521}
15522
15523/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15524 * and i915 state tracking structures. */
15525void intel_modeset_setup_hw_state(struct drm_device *dev,
15526 bool force_restore)
15527{
15528 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015529 struct drm_crtc *crtc;
15530 struct drm_crtc_state *crtc_state;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015531 struct intel_encoder *encoder;
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015532 struct drm_atomic_state *state;
15533 struct intel_shared_dpll_config shared_dplls[I915_NUM_PLLS];
Daniel Vetter35c95372013-07-17 06:55:04 +020015534 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015535
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015536 state = intel_modeset_readout_hw_state(dev);
15537 if (IS_ERR(state)) {
15538 DRM_ERROR("Failed to read out hw state\n");
15539 return;
Jesse Barnesbabea612013-06-26 18:57:38 +030015540 }
15541
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015542 drm_atomic_helper_swap_state(dev, state);
15543
15544 /* swap sw/hw dpll state */
15545 intel_atomic_duplicate_dpll_state(dev_priv, shared_dplls);
15546 intel_shared_dpll_commit(state);
15547 memcpy(to_intel_atomic_state(state)->shared_dpll,
15548 shared_dplls, sizeof(*shared_dplls) * dev_priv->num_shared_dpll);
15549
Daniel Vetter24929352012-07-02 20:28:59 +020015550 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015551 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015552 intel_sanitize_encoder(encoder);
15553 }
15554
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015555 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15557
15558 /* prevent unnneeded restores with force_restore */
15559 crtc_state->active_changed =
15560 crtc_state->mode_changed =
15561 crtc_state->planes_changed = false;
15562
15563 if (crtc->enabled) {
15564 intel_mode_from_pipe_config(&crtc->state->mode,
15565 to_intel_crtc_state(crtc->state));
15566
15567 drm_mode_copy(&crtc->mode, &crtc->state->mode);
15568 drm_mode_copy(&crtc->hwmode,
15569 &crtc->state->adjusted_mode);
15570 }
15571
15572 intel_sanitize_crtc(intel_crtc);
15573
15574 /*
15575 * sanitize_crtc may have forced an update of crtc->state,
15576 * so reload in intel_dump_pipe_config
15577 */
15578 intel_dump_pipe_config(intel_crtc,
15579 to_intel_crtc_state(crtc->state),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015580 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015581 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015582
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015583 intel_modeset_update_connector_atomic_state(dev);
15584
Daniel Vetter35c95372013-07-17 06:55:04 +020015585 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15586 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15587
15588 if (!pll->on || pll->active)
15589 continue;
15590
15591 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15592
15593 pll->disable(dev_priv, pll);
15594 pll->on = false;
15595 }
15596
Pradeep Bhat30789992014-11-04 17:06:45 +000015597 if (IS_GEN9(dev))
15598 skl_wm_get_hw_state(dev);
15599 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015600 ilk_wm_get_hw_state(dev);
15601
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015602 if (force_restore) {
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015603 int ret;
15604
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015605 i915_redisable_vga(dev);
15606
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015607 ret = intel_set_mode(state);
15608 if (ret) {
15609 DRM_ERROR("Failed to restore previous mode\n");
15610 drm_atomic_state_free(state);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015611 }
15612 } else {
Ander Conselvan de Oliveira37ade412015-06-01 12:50:03 +020015613 drm_atomic_state_free(state);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015614 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015615
15616 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015617}
15618
15619void intel_modeset_gem_init(struct drm_device *dev)
15620{
Jesse Barnes92122782014-10-09 12:57:42 -070015621 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015622 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015623 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015624 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015625
Imre Deakae484342014-03-31 15:10:44 +030015626 mutex_lock(&dev->struct_mutex);
15627 intel_init_gt_powersave(dev);
15628 mutex_unlock(&dev->struct_mutex);
15629
Jesse Barnes92122782014-10-09 12:57:42 -070015630 /*
15631 * There may be no VBT; and if the BIOS enabled SSC we can
15632 * just keep using it to avoid unnecessary flicker. Whereas if the
15633 * BIOS isn't using it, don't assume it will work even if the VBT
15634 * indicates as much.
15635 */
15636 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15637 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15638 DREF_SSC1_ENABLE);
15639
Chris Wilson1833b132012-05-09 11:56:28 +010015640 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015641
15642 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015643
15644 /*
15645 * Make sure any fbs we allocated at startup are properly
15646 * pinned & fenced. When we do the allocation it's too early
15647 * for this.
15648 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015649 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015650 obj = intel_fb_obj(c->primary->fb);
15651 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015652 continue;
15653
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015654 mutex_lock(&dev->struct_mutex);
15655 ret = intel_pin_and_fence_fb_obj(c->primary,
15656 c->primary->fb,
15657 c->primary->state,
15658 NULL);
15659 mutex_unlock(&dev->struct_mutex);
15660 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015661 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15662 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015663 drm_framebuffer_unreference(c->primary->fb);
15664 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015665 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015666 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015667 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015668 }
15669 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015670
15671 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015672}
15673
Imre Deak4932e2c2014-02-11 17:12:48 +020015674void intel_connector_unregister(struct intel_connector *intel_connector)
15675{
15676 struct drm_connector *connector = &intel_connector->base;
15677
15678 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015679 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015680}
15681
Jesse Barnes79e53942008-11-07 14:24:08 -080015682void intel_modeset_cleanup(struct drm_device *dev)
15683{
Jesse Barnes652c3932009-08-17 13:31:43 -070015684 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015685 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015686
Imre Deak2eb52522014-11-19 15:30:05 +020015687 intel_disable_gt_powersave(dev);
15688
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015689 intel_backlight_unregister(dev);
15690
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015691 /*
15692 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015693 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015694 * experience fancy races otherwise.
15695 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015696 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015697
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015698 /*
15699 * Due to the hpd irq storm handling the hotplug work can re-arm the
15700 * poll handlers. Hence disable polling after hpd handling is shut down.
15701 */
Keith Packardf87ea762010-10-03 19:36:26 -070015702 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015703
Jesse Barnes652c3932009-08-17 13:31:43 -070015704 mutex_lock(&dev->struct_mutex);
15705
Jesse Barnes723bfd72010-10-07 16:01:13 -070015706 intel_unregister_dsm_handler();
15707
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015708 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015709
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015710 mutex_unlock(&dev->struct_mutex);
15711
Chris Wilson1630fe72011-07-08 12:22:42 +010015712 /* flush any delayed tasks or pending work */
15713 flush_scheduled_work();
15714
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015715 /* destroy the backlight and sysfs files before encoders/connectors */
15716 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015717 struct intel_connector *intel_connector;
15718
15719 intel_connector = to_intel_connector(connector);
15720 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015721 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015722
Jesse Barnes79e53942008-11-07 14:24:08 -080015723 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015724
15725 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015726
15727 mutex_lock(&dev->struct_mutex);
15728 intel_cleanup_gt_powersave(dev);
15729 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015730}
15731
Dave Airlie28d52042009-09-21 14:33:58 +100015732/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015733 * Return which encoder is currently attached for connector.
15734 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015735struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015736{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015737 return &intel_attached_encoder(connector)->base;
15738}
Jesse Barnes79e53942008-11-07 14:24:08 -080015739
Chris Wilsondf0e9242010-09-09 16:20:55 +010015740void intel_connector_attach_encoder(struct intel_connector *connector,
15741 struct intel_encoder *encoder)
15742{
15743 connector->encoder = encoder;
15744 drm_mode_connector_attach_encoder(&connector->base,
15745 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015746}
Dave Airlie28d52042009-09-21 14:33:58 +100015747
15748/*
15749 * set vga decode state - true == enable VGA decode
15750 */
15751int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15752{
15753 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015754 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015755 u16 gmch_ctrl;
15756
Chris Wilson75fa0412014-02-07 18:37:02 -020015757 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15758 DRM_ERROR("failed to read control word\n");
15759 return -EIO;
15760 }
15761
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015762 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15763 return 0;
15764
Dave Airlie28d52042009-09-21 14:33:58 +100015765 if (state)
15766 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15767 else
15768 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015769
15770 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15771 DRM_ERROR("failed to write control word\n");
15772 return -EIO;
15773 }
15774
Dave Airlie28d52042009-09-21 14:33:58 +100015775 return 0;
15776}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015777
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015778struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015779
15780 u32 power_well_driver;
15781
Chris Wilson63b66e52013-08-08 15:12:06 +020015782 int num_transcoders;
15783
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015784 struct intel_cursor_error_state {
15785 u32 control;
15786 u32 position;
15787 u32 base;
15788 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015789 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015790
15791 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015792 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015793 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015794 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015795 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015796
15797 struct intel_plane_error_state {
15798 u32 control;
15799 u32 stride;
15800 u32 size;
15801 u32 pos;
15802 u32 addr;
15803 u32 surface;
15804 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015805 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015806
15807 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015808 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015809 enum transcoder cpu_transcoder;
15810
15811 u32 conf;
15812
15813 u32 htotal;
15814 u32 hblank;
15815 u32 hsync;
15816 u32 vtotal;
15817 u32 vblank;
15818 u32 vsync;
15819 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015820};
15821
15822struct intel_display_error_state *
15823intel_display_capture_error_state(struct drm_device *dev)
15824{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015825 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015826 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015827 int transcoders[] = {
15828 TRANSCODER_A,
15829 TRANSCODER_B,
15830 TRANSCODER_C,
15831 TRANSCODER_EDP,
15832 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015833 int i;
15834
Chris Wilson63b66e52013-08-08 15:12:06 +020015835 if (INTEL_INFO(dev)->num_pipes == 0)
15836 return NULL;
15837
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015838 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015839 if (error == NULL)
15840 return NULL;
15841
Imre Deak190be112013-11-25 17:15:31 +020015842 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015843 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15844
Damien Lespiau055e3932014-08-18 13:49:10 +010015845 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015846 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015847 __intel_display_power_is_enabled(dev_priv,
15848 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015849 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015850 continue;
15851
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015852 error->cursor[i].control = I915_READ(CURCNTR(i));
15853 error->cursor[i].position = I915_READ(CURPOS(i));
15854 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015855
15856 error->plane[i].control = I915_READ(DSPCNTR(i));
15857 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015858 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015859 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015860 error->plane[i].pos = I915_READ(DSPPOS(i));
15861 }
Paulo Zanonica291362013-03-06 20:03:14 -030015862 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15863 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015864 if (INTEL_INFO(dev)->gen >= 4) {
15865 error->plane[i].surface = I915_READ(DSPSURF(i));
15866 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15867 }
15868
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015869 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015870
Sonika Jindal3abfce72014-07-21 15:23:43 +053015871 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015872 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015873 }
15874
15875 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15876 if (HAS_DDI(dev_priv->dev))
15877 error->num_transcoders++; /* Account for eDP. */
15878
15879 for (i = 0; i < error->num_transcoders; i++) {
15880 enum transcoder cpu_transcoder = transcoders[i];
15881
Imre Deakddf9c532013-11-27 22:02:02 +020015882 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015883 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015884 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015885 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015886 continue;
15887
Chris Wilson63b66e52013-08-08 15:12:06 +020015888 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15889
15890 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15891 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15892 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15893 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15894 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15895 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15896 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015897 }
15898
15899 return error;
15900}
15901
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015902#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15903
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015904void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015905intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015906 struct drm_device *dev,
15907 struct intel_display_error_state *error)
15908{
Damien Lespiau055e3932014-08-18 13:49:10 +010015909 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015910 int i;
15911
Chris Wilson63b66e52013-08-08 15:12:06 +020015912 if (!error)
15913 return;
15914
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015915 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015916 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015917 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015918 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015919 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015920 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015921 err_printf(m, " Power: %s\n",
15922 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015923 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015924 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015925
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015926 err_printf(m, "Plane [%d]:\n", i);
15927 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15928 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015929 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015930 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15931 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015932 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015933 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015934 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015935 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015936 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15937 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015938 }
15939
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015940 err_printf(m, "Cursor [%d]:\n", i);
15941 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15942 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15943 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015944 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015945
15946 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015947 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015948 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015949 err_printf(m, " Power: %s\n",
15950 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015951 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15952 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15953 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15954 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15955 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15956 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15957 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15958 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015959}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015960
15961void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15962{
15963 struct intel_crtc *crtc;
15964
15965 for_each_intel_crtc(dev, crtc) {
15966 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015967
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015968 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015969
15970 work = crtc->unpin_work;
15971
15972 if (work && work->event &&
15973 work->event->base.file_priv == file) {
15974 kfree(work->event);
15975 work->event = NULL;
15976 }
15977
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015978 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015979 }
15980}