blob: 9543f947084ab9439750408de0bbe8b87310ba62 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010049static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010050 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070052 DRM_FORMAT_XRGB1555,
53 DRM_FORMAT_ARGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010054 DRM_FORMAT_XRGB8888,
55 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070056};
57
58/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010059static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010060 DRM_FORMAT_C8,
61 DRM_FORMAT_RGB565,
62 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070063 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010064 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070065 DRM_FORMAT_ABGR8888,
66 DRM_FORMAT_XRGB2101010,
67 DRM_FORMAT_ARGB2101010,
68 DRM_FORMAT_XBGR2101010,
69 DRM_FORMAT_ABGR2101010,
70};
71
Matt Roper3d7d6512014-06-10 08:28:13 -070072/* Cursor formats */
73static const uint32_t intel_cursor_formats[] = {
74 DRM_FORMAT_ARGB8888,
75};
76
Chris Wilson6b383a72010-09-13 13:54:26 +010077static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080078
Jesse Barnesf1f644d2013-06-27 00:39:25 +030079static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020080 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030081static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020082 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030083
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030084static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020085 struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080086static int intel_framebuffer_init(struct drm_device *dev,
87 struct intel_framebuffer *ifb,
88 struct drm_mode_fb_cmd2 *mode_cmd,
89 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020090static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
91static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020092static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070093 struct intel_link_m_n *m_n,
94 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020095static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020096static void haswell_set_pipeconf(struct drm_crtc *crtc);
97static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +020098static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020099 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200100static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200101 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800102static void intel_begin_crtc_commit(struct drm_crtc *crtc);
103static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700104static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
105 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200106static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
107 int num_connectors);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +0300108static void intel_crtc_enable_planes(struct drm_crtc *crtc);
109static void intel_crtc_disable_planes(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100110
Dave Airlie0e32b392014-05-02 14:02:48 +1000111static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
112{
113 if (!connector->mst_port)
114 return connector->encoder;
115 else
116 return &connector->mst_port->mst_encoders[pipe]->base;
117}
118
Jesse Barnes79e53942008-11-07 14:24:08 -0800119typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400120 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800121} intel_range_t;
122
123typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400124 int dot_limit;
125 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800126} intel_p2_t;
127
Ma Lingd4906092009-03-18 20:13:27 +0800128typedef struct intel_limit intel_limit_t;
129struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400130 intel_range_t dot, vco, n, m, m1, m2, p, p1;
131 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800132};
Jesse Barnes79e53942008-11-07 14:24:08 -0800133
Daniel Vetterd2acd212012-10-20 20:57:43 +0200134int
135intel_pch_rawclk(struct drm_device *dev)
136{
137 struct drm_i915_private *dev_priv = dev->dev_private;
138
139 WARN_ON(!HAS_PCH_SPLIT(dev));
140
141 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
142}
143
Chris Wilson021357a2010-09-07 20:54:59 +0100144static inline u32 /* units of 100MHz */
145intel_fdi_link_freq(struct drm_device *dev)
146{
Chris Wilson8b99e682010-10-13 09:59:17 +0100147 if (IS_GEN5(dev)) {
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
150 } else
151 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100152}
153
Daniel Vetter5d536e22013-07-06 12:52:06 +0200154static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400155 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200156 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200157 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400158 .m = { .min = 96, .max = 140 },
159 .m1 = { .min = 18, .max = 26 },
160 .m2 = { .min = 6, .max = 16 },
161 .p = { .min = 4, .max = 128 },
162 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700163 .p2 = { .dot_limit = 165000,
164 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700165};
166
Daniel Vetter5d536e22013-07-06 12:52:06 +0200167static const intel_limit_t intel_limits_i8xx_dvo = {
168 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200169 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200170 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200171 .m = { .min = 96, .max = 140 },
172 .m1 = { .min = 18, .max = 26 },
173 .m2 = { .min = 6, .max = 16 },
174 .p = { .min = 4, .max = 128 },
175 .p1 = { .min = 2, .max = 33 },
176 .p2 = { .dot_limit = 165000,
177 .p2_slow = 4, .p2_fast = 4 },
178};
179
Keith Packarde4b36692009-06-05 19:22:17 -0700180static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400181 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200182 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200183 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400184 .m = { .min = 96, .max = 140 },
185 .m1 = { .min = 18, .max = 26 },
186 .m2 = { .min = 6, .max = 16 },
187 .p = { .min = 4, .max = 128 },
188 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700189 .p2 = { .dot_limit = 165000,
190 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700191};
Eric Anholt273e27c2011-03-30 13:01:10 -0700192
Keith Packarde4b36692009-06-05 19:22:17 -0700193static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400194 .dot = { .min = 20000, .max = 400000 },
195 .vco = { .min = 1400000, .max = 2800000 },
196 .n = { .min = 1, .max = 6 },
197 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100198 .m1 = { .min = 8, .max = 18 },
199 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400200 .p = { .min = 5, .max = 80 },
201 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700202 .p2 = { .dot_limit = 200000,
203 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700204};
205
206static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400207 .dot = { .min = 20000, .max = 400000 },
208 .vco = { .min = 1400000, .max = 2800000 },
209 .n = { .min = 1, .max = 6 },
210 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100211 .m1 = { .min = 8, .max = 18 },
212 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400213 .p = { .min = 7, .max = 98 },
214 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700215 .p2 = { .dot_limit = 112000,
216 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700217};
218
Eric Anholt273e27c2011-03-30 13:01:10 -0700219
Keith Packarde4b36692009-06-05 19:22:17 -0700220static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700221 .dot = { .min = 25000, .max = 270000 },
222 .vco = { .min = 1750000, .max = 3500000},
223 .n = { .min = 1, .max = 4 },
224 .m = { .min = 104, .max = 138 },
225 .m1 = { .min = 17, .max = 23 },
226 .m2 = { .min = 5, .max = 11 },
227 .p = { .min = 10, .max = 30 },
228 .p1 = { .min = 1, .max = 3},
229 .p2 = { .dot_limit = 270000,
230 .p2_slow = 10,
231 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800232 },
Keith Packarde4b36692009-06-05 19:22:17 -0700233};
234
235static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700236 .dot = { .min = 22000, .max = 400000 },
237 .vco = { .min = 1750000, .max = 3500000},
238 .n = { .min = 1, .max = 4 },
239 .m = { .min = 104, .max = 138 },
240 .m1 = { .min = 16, .max = 23 },
241 .m2 = { .min = 5, .max = 11 },
242 .p = { .min = 5, .max = 80 },
243 .p1 = { .min = 1, .max = 8},
244 .p2 = { .dot_limit = 165000,
245 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700246};
247
248static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700249 .dot = { .min = 20000, .max = 115000 },
250 .vco = { .min = 1750000, .max = 3500000 },
251 .n = { .min = 1, .max = 3 },
252 .m = { .min = 104, .max = 138 },
253 .m1 = { .min = 17, .max = 23 },
254 .m2 = { .min = 5, .max = 11 },
255 .p = { .min = 28, .max = 112 },
256 .p1 = { .min = 2, .max = 8 },
257 .p2 = { .dot_limit = 0,
258 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800259 },
Keith Packarde4b36692009-06-05 19:22:17 -0700260};
261
262static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700263 .dot = { .min = 80000, .max = 224000 },
264 .vco = { .min = 1750000, .max = 3500000 },
265 .n = { .min = 1, .max = 3 },
266 .m = { .min = 104, .max = 138 },
267 .m1 = { .min = 17, .max = 23 },
268 .m2 = { .min = 5, .max = 11 },
269 .p = { .min = 14, .max = 42 },
270 .p1 = { .min = 2, .max = 6 },
271 .p2 = { .dot_limit = 0,
272 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800273 },
Keith Packarde4b36692009-06-05 19:22:17 -0700274};
275
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500276static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400277 .dot = { .min = 20000, .max = 400000},
278 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700279 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400280 .n = { .min = 3, .max = 6 },
281 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700282 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400283 .m1 = { .min = 0, .max = 0 },
284 .m2 = { .min = 0, .max = 254 },
285 .p = { .min = 5, .max = 80 },
286 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 .p2 = { .dot_limit = 200000,
288 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700289};
290
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500291static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .dot = { .min = 20000, .max = 400000 },
293 .vco = { .min = 1700000, .max = 3500000 },
294 .n = { .min = 3, .max = 6 },
295 .m = { .min = 2, .max = 256 },
296 .m1 = { .min = 0, .max = 0 },
297 .m2 = { .min = 0, .max = 254 },
298 .p = { .min = 7, .max = 112 },
299 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .p2 = { .dot_limit = 112000,
301 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700302};
303
Eric Anholt273e27c2011-03-30 13:01:10 -0700304/* Ironlake / Sandybridge
305 *
306 * We calculate clock using (register_value + 2) for N/M1/M2, so here
307 * the range value for them is (actual_value - 2).
308 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800309static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .dot = { .min = 25000, .max = 350000 },
311 .vco = { .min = 1760000, .max = 3510000 },
312 .n = { .min = 1, .max = 5 },
313 .m = { .min = 79, .max = 127 },
314 .m1 = { .min = 12, .max = 22 },
315 .m2 = { .min = 5, .max = 9 },
316 .p = { .min = 5, .max = 80 },
317 .p1 = { .min = 1, .max = 8 },
318 .p2 = { .dot_limit = 225000,
319 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700320};
321
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800322static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .dot = { .min = 25000, .max = 350000 },
324 .vco = { .min = 1760000, .max = 3510000 },
325 .n = { .min = 1, .max = 3 },
326 .m = { .min = 79, .max = 118 },
327 .m1 = { .min = 12, .max = 22 },
328 .m2 = { .min = 5, .max = 9 },
329 .p = { .min = 28, .max = 112 },
330 .p1 = { .min = 2, .max = 8 },
331 .p2 = { .dot_limit = 225000,
332 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800333};
334
335static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700336 .dot = { .min = 25000, .max = 350000 },
337 .vco = { .min = 1760000, .max = 3510000 },
338 .n = { .min = 1, .max = 3 },
339 .m = { .min = 79, .max = 127 },
340 .m1 = { .min = 12, .max = 22 },
341 .m2 = { .min = 5, .max = 9 },
342 .p = { .min = 14, .max = 56 },
343 .p1 = { .min = 2, .max = 8 },
344 .p2 = { .dot_limit = 225000,
345 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800346};
347
Eric Anholt273e27c2011-03-30 13:01:10 -0700348/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800349static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700350 .dot = { .min = 25000, .max = 350000 },
351 .vco = { .min = 1760000, .max = 3510000 },
352 .n = { .min = 1, .max = 2 },
353 .m = { .min = 79, .max = 126 },
354 .m1 = { .min = 12, .max = 22 },
355 .m2 = { .min = 5, .max = 9 },
356 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400357 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .p2 = { .dot_limit = 225000,
359 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800360};
361
362static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 .dot = { .min = 25000, .max = 350000 },
364 .vco = { .min = 1760000, .max = 3510000 },
365 .n = { .min = 1, .max = 3 },
366 .m = { .min = 79, .max = 126 },
367 .m1 = { .min = 12, .max = 22 },
368 .m2 = { .min = 5, .max = 9 },
369 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400370 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .p2 = { .dot_limit = 225000,
372 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800373};
374
Ville Syrjälädc730512013-09-24 21:26:30 +0300375static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300376 /*
377 * These are the data rate limits (measured in fast clocks)
378 * since those are the strictest limits we have. The fast
379 * clock and actual rate limits are more relaxed, so checking
380 * them would make no difference.
381 */
382 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200383 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700384 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700385 .m1 = { .min = 2, .max = 3 },
386 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300387 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300388 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700389};
390
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300391static const intel_limit_t intel_limits_chv = {
392 /*
393 * These are the data rate limits (measured in fast clocks)
394 * since those are the strictest limits we have. The fast
395 * clock and actual rate limits are more relaxed, so checking
396 * them would make no difference.
397 */
398 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200399 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300400 .n = { .min = 1, .max = 1 },
401 .m1 = { .min = 2, .max = 2 },
402 .m2 = { .min = 24 << 22, .max = 175 << 22 },
403 .p1 = { .min = 2, .max = 4 },
404 .p2 = { .p2_slow = 1, .p2_fast = 14 },
405};
406
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200407static const intel_limit_t intel_limits_bxt = {
408 /* FIXME: find real dot limits */
409 .dot = { .min = 0, .max = INT_MAX },
410 .vco = { .min = 4800000, .max = 6480000 },
411 .n = { .min = 1, .max = 1 },
412 .m1 = { .min = 2, .max = 2 },
413 /* FIXME: find real m2 limits */
414 .m2 = { .min = 2 << 22, .max = 255 << 22 },
415 .p1 = { .min = 2, .max = 4 },
416 .p2 = { .p2_slow = 1, .p2_fast = 20 },
417};
418
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300419static void vlv_clock(int refclk, intel_clock_t *clock)
420{
421 clock->m = clock->m1 * clock->m2;
422 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200423 if (WARN_ON(clock->n == 0 || clock->p == 0))
424 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300425 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
426 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300427}
428
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300429/**
430 * Returns whether any output on the specified pipe is of the specified type
431 */
Damien Lespiau40935612014-10-29 11:16:59 +0000432bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300433{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300434 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300435 struct intel_encoder *encoder;
436
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300437 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300438 if (encoder->type == type)
439 return true;
440
441 return false;
442}
443
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200444/**
445 * Returns whether any output on the specified pipe will have the specified
446 * type after a staged modeset is complete, i.e., the same as
447 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
448 * encoder->crtc.
449 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200450static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
451 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200452{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200453 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300454 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200455 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200456 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200457 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200458
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300459 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200460 if (connector_state->crtc != crtc_state->base.crtc)
461 continue;
462
463 num_connectors++;
464
465 encoder = to_intel_encoder(connector_state->best_encoder);
466 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200467 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200468 }
469
470 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200471
472 return false;
473}
474
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200475static const intel_limit_t *
476intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800477{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200478 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800479 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800480
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200481 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100482 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000483 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800484 limit = &intel_limits_ironlake_dual_lvds_100m;
485 else
486 limit = &intel_limits_ironlake_dual_lvds;
487 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000488 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800489 limit = &intel_limits_ironlake_single_lvds_100m;
490 else
491 limit = &intel_limits_ironlake_single_lvds;
492 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200493 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800494 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800495
496 return limit;
497}
498
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200499static const intel_limit_t *
500intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800501{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200502 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800503 const intel_limit_t *limit;
504
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200505 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100506 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700507 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800508 else
Keith Packarde4b36692009-06-05 19:22:17 -0700509 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200510 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
511 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200513 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800515 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700516 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800517
518 return limit;
519}
520
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200521static const intel_limit_t *
522intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800523{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200524 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800525 const intel_limit_t *limit;
526
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200527 if (IS_BROXTON(dev))
528 limit = &intel_limits_bxt;
529 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200530 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800531 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200532 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500533 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200534 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500535 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800536 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500537 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300538 } else if (IS_CHERRYVIEW(dev)) {
539 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700540 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300541 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100542 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200543 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100544 limit = &intel_limits_i9xx_lvds;
545 else
546 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800547 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700549 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200550 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700551 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200552 else
553 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800554 }
555 return limit;
556}
557
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500558/* m1 is reserved as 0 in Pineview, n is a ring counter */
559static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800560{
Shaohua Li21778322009-02-23 15:19:16 +0800561 clock->m = clock->m2 + 2;
562 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200563 if (WARN_ON(clock->n == 0 || clock->p == 0))
564 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300565 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
566 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800567}
568
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200569static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
570{
571 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
572}
573
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200574static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800575{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200576 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800577 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200578 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
579 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300580 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
581 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800582}
583
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300584static void chv_clock(int refclk, intel_clock_t *clock)
585{
586 clock->m = clock->m1 * clock->m2;
587 clock->p = clock->p1 * clock->p2;
588 if (WARN_ON(clock->n == 0 || clock->p == 0))
589 return;
590 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
591 clock->n << 22);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
593}
594
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800595#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800596/**
597 * Returns whether the given set of divisors are valid for a given refclk with
598 * the given connectors.
599 */
600
Chris Wilson1b894b52010-12-14 20:04:54 +0000601static bool intel_PLL_is_valid(struct drm_device *dev,
602 const intel_limit_t *limit,
603 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800604{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300605 if (clock->n < limit->n.min || limit->n.max < clock->n)
606 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800607 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400608 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800609 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400610 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800611 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400612 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300613
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200614 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300615 if (clock->m1 <= clock->m2)
616 INTELPllInvalid("m1 <= m2\n");
617
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200618 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300619 if (clock->p < limit->p.min || limit->p.max < clock->p)
620 INTELPllInvalid("p out of range\n");
621 if (clock->m < limit->m.min || limit->m.max < clock->m)
622 INTELPllInvalid("m out of range\n");
623 }
624
Jesse Barnes79e53942008-11-07 14:24:08 -0800625 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400626 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800627 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
628 * connector, etc., rather than just a single range.
629 */
630 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400631 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800632
633 return true;
634}
635
Ma Lingd4906092009-03-18 20:13:27 +0800636static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200637i9xx_find_best_dpll(const intel_limit_t *limit,
638 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800639 int target, int refclk, intel_clock_t *match_clock,
640 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800641{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200642 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300643 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800644 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800645 int err = target;
646
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200647 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800648 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100649 * For LVDS just rely on its current settings for dual-channel.
650 * We haven't figured out how to reliably set up different
651 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100653 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800654 clock.p2 = limit->p2.p2_fast;
655 else
656 clock.p2 = limit->p2.p2_slow;
657 } else {
658 if (target < limit->p2.dot_limit)
659 clock.p2 = limit->p2.p2_slow;
660 else
661 clock.p2 = limit->p2.p2_fast;
662 }
663
Akshay Joshi0206e352011-08-16 15:34:10 -0400664 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800665
Zhao Yakui42158662009-11-20 11:24:18 +0800666 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
667 clock.m1++) {
668 for (clock.m2 = limit->m2.min;
669 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200670 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800671 break;
672 for (clock.n = limit->n.min;
673 clock.n <= limit->n.max; clock.n++) {
674 for (clock.p1 = limit->p1.min;
675 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800676 int this_err;
677
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200678 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000679 if (!intel_PLL_is_valid(dev, limit,
680 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800682 if (match_clock &&
683 clock.p != match_clock->p)
684 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800685
686 this_err = abs(clock.dot - target);
687 if (this_err < err) {
688 *best_clock = clock;
689 err = this_err;
690 }
691 }
692 }
693 }
694 }
695
696 return (err != target);
697}
698
Ma Lingd4906092009-03-18 20:13:27 +0800699static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200700pnv_find_best_dpll(const intel_limit_t *limit,
701 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200702 int target, int refclk, intel_clock_t *match_clock,
703 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200704{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200705 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300706 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200707 intel_clock_t clock;
708 int err = target;
709
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200710 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200711 /*
712 * For LVDS just rely on its current settings for dual-channel.
713 * We haven't figured out how to reliably set up different
714 * single/dual channel state, if we even can.
715 */
716 if (intel_is_dual_link_lvds(dev))
717 clock.p2 = limit->p2.p2_fast;
718 else
719 clock.p2 = limit->p2.p2_slow;
720 } else {
721 if (target < limit->p2.dot_limit)
722 clock.p2 = limit->p2.p2_slow;
723 else
724 clock.p2 = limit->p2.p2_fast;
725 }
726
727 memset(best_clock, 0, sizeof(*best_clock));
728
729 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
730 clock.m1++) {
731 for (clock.m2 = limit->m2.min;
732 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200733 for (clock.n = limit->n.min;
734 clock.n <= limit->n.max; clock.n++) {
735 for (clock.p1 = limit->p1.min;
736 clock.p1 <= limit->p1.max; clock.p1++) {
737 int this_err;
738
739 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800740 if (!intel_PLL_is_valid(dev, limit,
741 &clock))
742 continue;
743 if (match_clock &&
744 clock.p != match_clock->p)
745 continue;
746
747 this_err = abs(clock.dot - target);
748 if (this_err < err) {
749 *best_clock = clock;
750 err = this_err;
751 }
752 }
753 }
754 }
755 }
756
757 return (err != target);
758}
759
Ma Lingd4906092009-03-18 20:13:27 +0800760static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200761g4x_find_best_dpll(const intel_limit_t *limit,
762 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200763 int target, int refclk, intel_clock_t *match_clock,
764 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800765{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200766 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300767 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800768 intel_clock_t clock;
769 int max_n;
770 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400771 /* approximately equals target * 0.00585 */
772 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800773 found = false;
774
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200775 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100776 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800777 clock.p2 = limit->p2.p2_fast;
778 else
779 clock.p2 = limit->p2.p2_slow;
780 } else {
781 if (target < limit->p2.dot_limit)
782 clock.p2 = limit->p2.p2_slow;
783 else
784 clock.p2 = limit->p2.p2_fast;
785 }
786
787 memset(best_clock, 0, sizeof(*best_clock));
788 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200789 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800790 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200791 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800792 for (clock.m1 = limit->m1.max;
793 clock.m1 >= limit->m1.min; clock.m1--) {
794 for (clock.m2 = limit->m2.max;
795 clock.m2 >= limit->m2.min; clock.m2--) {
796 for (clock.p1 = limit->p1.max;
797 clock.p1 >= limit->p1.min; clock.p1--) {
798 int this_err;
799
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200800 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000801 if (!intel_PLL_is_valid(dev, limit,
802 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800803 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000804
805 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800806 if (this_err < err_most) {
807 *best_clock = clock;
808 err_most = this_err;
809 max_n = clock.n;
810 found = true;
811 }
812 }
813 }
814 }
815 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800816 return found;
817}
Ma Lingd4906092009-03-18 20:13:27 +0800818
Imre Deakd5dd62b2015-03-17 11:40:03 +0200819/*
820 * Check if the calculated PLL configuration is more optimal compared to the
821 * best configuration and error found so far. Return the calculated error.
822 */
823static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
824 const intel_clock_t *calculated_clock,
825 const intel_clock_t *best_clock,
826 unsigned int best_error_ppm,
827 unsigned int *error_ppm)
828{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200829 /*
830 * For CHV ignore the error and consider only the P value.
831 * Prefer a bigger P value based on HW requirements.
832 */
833 if (IS_CHERRYVIEW(dev)) {
834 *error_ppm = 0;
835
836 return calculated_clock->p > best_clock->p;
837 }
838
Imre Deak24be4e42015-03-17 11:40:04 +0200839 if (WARN_ON_ONCE(!target_freq))
840 return false;
841
Imre Deakd5dd62b2015-03-17 11:40:03 +0200842 *error_ppm = div_u64(1000000ULL *
843 abs(target_freq - calculated_clock->dot),
844 target_freq);
845 /*
846 * Prefer a better P value over a better (smaller) error if the error
847 * is small. Ensure this preference for future configurations too by
848 * setting the error to 0.
849 */
850 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
851 *error_ppm = 0;
852
853 return true;
854 }
855
856 return *error_ppm + 10 < best_error_ppm;
857}
858
Zhenyu Wang2c072452009-06-05 15:38:42 +0800859static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200860vlv_find_best_dpll(const intel_limit_t *limit,
861 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200862 int target, int refclk, intel_clock_t *match_clock,
863 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700864{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200865 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300866 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300867 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300868 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300869 /* min update 19.2 MHz */
870 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300871 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700872
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300873 target *= 5; /* fast clock */
874
875 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700876
877 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300878 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300879 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300880 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300881 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300882 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700883 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300884 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200885 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300886
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300887 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
888 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300889
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300890 vlv_clock(refclk, &clock);
891
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300892 if (!intel_PLL_is_valid(dev, limit,
893 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300894 continue;
895
Imre Deakd5dd62b2015-03-17 11:40:03 +0200896 if (!vlv_PLL_is_optimal(dev, target,
897 &clock,
898 best_clock,
899 bestppm, &ppm))
900 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300901
Imre Deakd5dd62b2015-03-17 11:40:03 +0200902 *best_clock = clock;
903 bestppm = ppm;
904 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700905 }
906 }
907 }
908 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700909
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300910 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700911}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700912
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300913static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200914chv_find_best_dpll(const intel_limit_t *limit,
915 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300916 int target, int refclk, intel_clock_t *match_clock,
917 intel_clock_t *best_clock)
918{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200919 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300920 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200921 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300922 intel_clock_t clock;
923 uint64_t m2;
924 int found = false;
925
926 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200927 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300928
929 /*
930 * Based on hardware doc, the n always set to 1, and m1 always
931 * set to 2. If requires to support 200Mhz refclk, we need to
932 * revisit this because n may not 1 anymore.
933 */
934 clock.n = 1, clock.m1 = 2;
935 target *= 5; /* fast clock */
936
937 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
938 for (clock.p2 = limit->p2.p2_fast;
939 clock.p2 >= limit->p2.p2_slow;
940 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200941 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300942
943 clock.p = clock.p1 * clock.p2;
944
945 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
946 clock.n) << 22, refclk * clock.m1);
947
948 if (m2 > INT_MAX/clock.m1)
949 continue;
950
951 clock.m2 = m2;
952
953 chv_clock(refclk, &clock);
954
955 if (!intel_PLL_is_valid(dev, limit, &clock))
956 continue;
957
Imre Deak9ca3ba02015-03-17 11:40:05 +0200958 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
959 best_error_ppm, &error_ppm))
960 continue;
961
962 *best_clock = clock;
963 best_error_ppm = error_ppm;
964 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300965 }
966 }
967
968 return found;
969}
970
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200971bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
972 intel_clock_t *best_clock)
973{
974 int refclk = i9xx_get_refclk(crtc_state, 0);
975
976 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
977 target_clock, refclk, NULL, best_clock);
978}
979
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300980bool intel_crtc_active(struct drm_crtc *crtc)
981{
982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
983
984 /* Be paranoid as we can arrive here with only partial
985 * state retrieved from the hardware during setup.
986 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100987 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300988 * as Haswell has gained clock readout/fastboot support.
989 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000990 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300991 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700992 *
993 * FIXME: The intel_crtc->active here should be switched to
994 * crtc->state->active once we have proper CRTC states wired up
995 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300996 */
Matt Roperc3d1f432015-03-09 10:19:23 -0700997 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200998 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300999}
1000
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001001enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1002 enum pipe pipe)
1003{
1004 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1006
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001007 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001008}
1009
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001010static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1011{
1012 struct drm_i915_private *dev_priv = dev->dev_private;
1013 u32 reg = PIPEDSL(pipe);
1014 u32 line1, line2;
1015 u32 line_mask;
1016
1017 if (IS_GEN2(dev))
1018 line_mask = DSL_LINEMASK_GEN2;
1019 else
1020 line_mask = DSL_LINEMASK_GEN3;
1021
1022 line1 = I915_READ(reg) & line_mask;
1023 mdelay(5);
1024 line2 = I915_READ(reg) & line_mask;
1025
1026 return line1 == line2;
1027}
1028
Keith Packardab7ad7f2010-10-03 00:33:06 -07001029/*
1030 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001031 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001032 *
1033 * After disabling a pipe, we can't wait for vblank in the usual way,
1034 * spinning on the vblank interrupt status bit, since we won't actually
1035 * see an interrupt when the pipe is disabled.
1036 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001037 * On Gen4 and above:
1038 * wait for the pipe register state bit to turn off
1039 *
1040 * Otherwise:
1041 * wait for the display line value to settle (it usually
1042 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001043 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001044 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001045static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001046{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001047 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001048 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001049 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001050 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001051
Keith Packardab7ad7f2010-10-03 00:33:06 -07001052 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001053 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001054
Keith Packardab7ad7f2010-10-03 00:33:06 -07001055 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001056 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1057 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001058 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001059 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001060 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001061 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001062 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001063 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001064}
1065
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001066/*
1067 * ibx_digital_port_connected - is the specified port connected?
1068 * @dev_priv: i915 private structure
1069 * @port: the port to test
1070 *
1071 * Returns true if @port is connected, false otherwise.
1072 */
1073bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1074 struct intel_digital_port *port)
1075{
1076 u32 bit;
1077
Damien Lespiauc36346e2012-12-13 16:09:03 +00001078 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001079 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001080 case PORT_B:
1081 bit = SDE_PORTB_HOTPLUG;
1082 break;
1083 case PORT_C:
1084 bit = SDE_PORTC_HOTPLUG;
1085 break;
1086 case PORT_D:
1087 bit = SDE_PORTD_HOTPLUG;
1088 break;
1089 default:
1090 return true;
1091 }
1092 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001093 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001094 case PORT_B:
1095 bit = SDE_PORTB_HOTPLUG_CPT;
1096 break;
1097 case PORT_C:
1098 bit = SDE_PORTC_HOTPLUG_CPT;
1099 break;
1100 case PORT_D:
1101 bit = SDE_PORTD_HOTPLUG_CPT;
1102 break;
1103 default:
1104 return true;
1105 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001106 }
1107
1108 return I915_READ(SDEISR) & bit;
1109}
1110
Jesse Barnesb24e7172011-01-04 15:09:30 -08001111static const char *state_string(bool enabled)
1112{
1113 return enabled ? "on" : "off";
1114}
1115
1116/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001117void assert_pll(struct drm_i915_private *dev_priv,
1118 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001119{
1120 int reg;
1121 u32 val;
1122 bool cur_state;
1123
1124 reg = DPLL(pipe);
1125 val = I915_READ(reg);
1126 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001127 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001128 "PLL state assertion failure (expected %s, current %s)\n",
1129 state_string(state), state_string(cur_state));
1130}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001131
Jani Nikula23538ef2013-08-27 15:12:22 +03001132/* XXX: the dsi pll is shared between MIPI DSI ports */
1133static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1134{
1135 u32 val;
1136 bool cur_state;
1137
1138 mutex_lock(&dev_priv->dpio_lock);
1139 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1140 mutex_unlock(&dev_priv->dpio_lock);
1141
1142 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001143 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001144 "DSI PLL state assertion failure (expected %s, current %s)\n",
1145 state_string(state), state_string(cur_state));
1146}
1147#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1148#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1149
Daniel Vetter55607e82013-06-16 21:42:39 +02001150struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001151intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001152{
Daniel Vettere2b78262013-06-07 23:10:03 +02001153 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1154
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001155 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001156 return NULL;
1157
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001158 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001159}
1160
Jesse Barnesb24e7172011-01-04 15:09:30 -08001161/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001162void assert_shared_dpll(struct drm_i915_private *dev_priv,
1163 struct intel_shared_dpll *pll,
1164 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001165{
Jesse Barnes040484a2011-01-03 12:14:26 -08001166 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001167 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001168
Chris Wilson92b27b02012-05-20 18:10:50 +01001169 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001170 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001171 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001172
Daniel Vetter53589012013-06-05 13:34:16 +02001173 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001174 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001175 "%s assertion failure (expected %s, current %s)\n",
1176 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001177}
Jesse Barnes040484a2011-01-03 12:14:26 -08001178
1179static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1180 enum pipe pipe, bool state)
1181{
1182 int reg;
1183 u32 val;
1184 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001185 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1186 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001187
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001188 if (HAS_DDI(dev_priv->dev)) {
1189 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001190 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001191 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001192 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001193 } else {
1194 reg = FDI_TX_CTL(pipe);
1195 val = I915_READ(reg);
1196 cur_state = !!(val & FDI_TX_ENABLE);
1197 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001198 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001199 "FDI TX state assertion failure (expected %s, current %s)\n",
1200 state_string(state), state_string(cur_state));
1201}
1202#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1203#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1204
1205static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1206 enum pipe pipe, bool state)
1207{
1208 int reg;
1209 u32 val;
1210 bool cur_state;
1211
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001212 reg = FDI_RX_CTL(pipe);
1213 val = I915_READ(reg);
1214 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001215 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001216 "FDI RX state assertion failure (expected %s, current %s)\n",
1217 state_string(state), state_string(cur_state));
1218}
1219#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1220#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1221
1222static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1223 enum pipe pipe)
1224{
1225 int reg;
1226 u32 val;
1227
1228 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001229 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001230 return;
1231
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001232 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001233 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001234 return;
1235
Jesse Barnes040484a2011-01-03 12:14:26 -08001236 reg = FDI_TX_CTL(pipe);
1237 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001238 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 -08001239}
1240
Daniel Vetter55607e82013-06-16 21:42:39 +02001241void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1242 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001243{
1244 int reg;
1245 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001246 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001247
1248 reg = FDI_RX_CTL(pipe);
1249 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001250 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001251 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001252 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1253 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001254}
1255
Daniel Vetterb680c372014-09-19 18:27:27 +02001256void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1257 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001258{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001259 struct drm_device *dev = dev_priv->dev;
1260 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261 u32 val;
1262 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001263 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001264
Jani Nikulabedd4db2014-08-22 15:04:13 +03001265 if (WARN_ON(HAS_DDI(dev)))
1266 return;
1267
1268 if (HAS_PCH_SPLIT(dev)) {
1269 u32 port_sel;
1270
Jesse Barnesea0760c2011-01-04 15:09:32 -08001271 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001272 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1273
1274 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1275 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1276 panel_pipe = PIPE_B;
1277 /* XXX: else fix for eDP */
1278 } else if (IS_VALLEYVIEW(dev)) {
1279 /* presumably write lock depends on pipe, not port select */
1280 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1281 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001282 } else {
1283 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001284 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1285 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001286 }
1287
1288 val = I915_READ(pp_reg);
1289 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001290 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001291 locked = false;
1292
Rob Clarke2c719b2014-12-15 13:56:32 -05001293 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001294 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001295 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001296}
1297
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001298static void assert_cursor(struct drm_i915_private *dev_priv,
1299 enum pipe pipe, bool state)
1300{
1301 struct drm_device *dev = dev_priv->dev;
1302 bool cur_state;
1303
Paulo Zanonid9d82082014-02-27 16:30:56 -03001304 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001305 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001306 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001307 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001308
Rob Clarke2c719b2014-12-15 13:56:32 -05001309 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001310 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1311 pipe_name(pipe), state_string(state), state_string(cur_state));
1312}
1313#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1314#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1315
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001316void assert_pipe(struct drm_i915_private *dev_priv,
1317 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001318{
1319 int reg;
1320 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001321 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001322 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1323 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001324
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001325 /* if we need the pipe quirk it must be always on */
1326 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1327 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001328 state = true;
1329
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001330 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001331 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001332 cur_state = false;
1333 } else {
1334 reg = PIPECONF(cpu_transcoder);
1335 val = I915_READ(reg);
1336 cur_state = !!(val & PIPECONF_ENABLE);
1337 }
1338
Rob Clarke2c719b2014-12-15 13:56:32 -05001339 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001340 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001341 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001342}
1343
Chris Wilson931872f2012-01-16 23:01:13 +00001344static void assert_plane(struct drm_i915_private *dev_priv,
1345 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001346{
1347 int reg;
1348 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001349 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001350
1351 reg = DSPCNTR(plane);
1352 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001353 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001354 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001355 "plane %c assertion failure (expected %s, current %s)\n",
1356 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001357}
1358
Chris Wilson931872f2012-01-16 23:01:13 +00001359#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1360#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1361
Jesse Barnesb24e7172011-01-04 15:09:30 -08001362static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1363 enum pipe pipe)
1364{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001365 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001366 int reg, i;
1367 u32 val;
1368 int cur_pipe;
1369
Ville Syrjälä653e1022013-06-04 13:49:05 +03001370 /* Primary planes are fixed to pipes on gen4+ */
1371 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001372 reg = DSPCNTR(pipe);
1373 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001374 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001375 "plane %c assertion failure, should be disabled but not\n",
1376 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001377 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001378 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001379
Jesse Barnesb24e7172011-01-04 15:09:30 -08001380 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001381 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001382 reg = DSPCNTR(i);
1383 val = I915_READ(reg);
1384 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1385 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001386 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001387 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1388 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001389 }
1390}
1391
Jesse Barnes19332d72013-03-28 09:55:38 -07001392static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1393 enum pipe pipe)
1394{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001395 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001396 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001397 u32 val;
1398
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001399 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001400 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001401 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001402 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001403 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1404 sprite, pipe_name(pipe));
1405 }
1406 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001407 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001408 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001409 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001410 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001411 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001412 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001413 }
1414 } else if (INTEL_INFO(dev)->gen >= 7) {
1415 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001416 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001417 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001418 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001419 plane_name(pipe), pipe_name(pipe));
1420 } else if (INTEL_INFO(dev)->gen >= 5) {
1421 reg = DVSCNTR(pipe);
1422 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001423 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001424 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1425 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001426 }
1427}
1428
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001429static void assert_vblank_disabled(struct drm_crtc *crtc)
1430{
Rob Clarke2c719b2014-12-15 13:56:32 -05001431 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001432 drm_crtc_vblank_put(crtc);
1433}
1434
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001435static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001436{
1437 u32 val;
1438 bool enabled;
1439
Rob Clarke2c719b2014-12-15 13:56:32 -05001440 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001441
Jesse Barnes92f25842011-01-04 15:09:34 -08001442 val = I915_READ(PCH_DREF_CONTROL);
1443 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1444 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001445 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001446}
1447
Daniel Vetterab9412b2013-05-03 11:49:46 +02001448static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1449 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001450{
1451 int reg;
1452 u32 val;
1453 bool enabled;
1454
Daniel Vetterab9412b2013-05-03 11:49:46 +02001455 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001456 val = I915_READ(reg);
1457 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001458 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001459 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1460 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001461}
1462
Keith Packard4e634382011-08-06 10:39:45 -07001463static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1464 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001465{
1466 if ((val & DP_PORT_EN) == 0)
1467 return false;
1468
1469 if (HAS_PCH_CPT(dev_priv->dev)) {
1470 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1471 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1472 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1473 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001474 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1475 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1476 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001477 } else {
1478 if ((val & DP_PIPE_MASK) != (pipe << 30))
1479 return false;
1480 }
1481 return true;
1482}
1483
Keith Packard1519b992011-08-06 10:35:34 -07001484static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1485 enum pipe pipe, u32 val)
1486{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001487 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001488 return false;
1489
1490 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001491 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001492 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001493 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1494 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1495 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001496 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001497 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001498 return false;
1499 }
1500 return true;
1501}
1502
1503static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1504 enum pipe pipe, u32 val)
1505{
1506 if ((val & LVDS_PORT_EN) == 0)
1507 return false;
1508
1509 if (HAS_PCH_CPT(dev_priv->dev)) {
1510 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1511 return false;
1512 } else {
1513 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1514 return false;
1515 }
1516 return true;
1517}
1518
1519static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1520 enum pipe pipe, u32 val)
1521{
1522 if ((val & ADPA_DAC_ENABLE) == 0)
1523 return false;
1524 if (HAS_PCH_CPT(dev_priv->dev)) {
1525 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1526 return false;
1527 } else {
1528 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1529 return false;
1530 }
1531 return true;
1532}
1533
Jesse Barnes291906f2011-02-02 12:28:03 -08001534static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001535 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001536{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001537 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001538 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001539 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001540 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001541
Rob Clarke2c719b2014-12-15 13:56:32 -05001542 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001543 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001544 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001545}
1546
1547static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1548 enum pipe pipe, int reg)
1549{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001550 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001551 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001552 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001553 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001554
Rob Clarke2c719b2014-12-15 13:56:32 -05001555 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001556 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001557 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001558}
1559
1560static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1561 enum pipe pipe)
1562{
1563 int reg;
1564 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001565
Keith Packardf0575e92011-07-25 22:12:43 -07001566 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1567 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1568 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001569
1570 reg = PCH_ADPA;
1571 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001572 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001573 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001574 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001575
1576 reg = PCH_LVDS;
1577 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001578 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001579 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001580 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001581
Paulo Zanonie2debe92013-02-18 19:00:27 -03001582 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1583 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1584 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001585}
1586
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001587static void intel_init_dpio(struct drm_device *dev)
1588{
1589 struct drm_i915_private *dev_priv = dev->dev_private;
1590
1591 if (!IS_VALLEYVIEW(dev))
1592 return;
1593
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001594 /*
1595 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1596 * CHV x1 PHY (DP/HDMI D)
1597 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1598 */
1599 if (IS_CHERRYVIEW(dev)) {
1600 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1601 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1602 } else {
1603 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1604 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001605}
1606
Ville Syrjäläd288f652014-10-28 13:20:22 +02001607static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001608 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001609{
Daniel Vetter426115c2013-07-11 22:13:42 +02001610 struct drm_device *dev = crtc->base.dev;
1611 struct drm_i915_private *dev_priv = dev->dev_private;
1612 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001613 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001614
Daniel Vetter426115c2013-07-11 22:13:42 +02001615 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001616
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001617 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001618 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1619
1620 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001621 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001622 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001623
Daniel Vetter426115c2013-07-11 22:13:42 +02001624 I915_WRITE(reg, dpll);
1625 POSTING_READ(reg);
1626 udelay(150);
1627
1628 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1629 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1630
Ville Syrjäläd288f652014-10-28 13:20:22 +02001631 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001632 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001633
1634 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001635 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001636 POSTING_READ(reg);
1637 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001638 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001639 POSTING_READ(reg);
1640 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001641 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001642 POSTING_READ(reg);
1643 udelay(150); /* wait for warmup */
1644}
1645
Ville Syrjäläd288f652014-10-28 13:20:22 +02001646static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001647 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001648{
1649 struct drm_device *dev = crtc->base.dev;
1650 struct drm_i915_private *dev_priv = dev->dev_private;
1651 int pipe = crtc->pipe;
1652 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001653 u32 tmp;
1654
1655 assert_pipe_disabled(dev_priv, crtc->pipe);
1656
1657 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1658
1659 mutex_lock(&dev_priv->dpio_lock);
1660
1661 /* Enable back the 10bit clock to display controller */
1662 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1663 tmp |= DPIO_DCLKP_EN;
1664 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1665
1666 /*
1667 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1668 */
1669 udelay(1);
1670
1671 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001672 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001673
1674 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001675 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001676 DRM_ERROR("PLL %d failed to lock\n", pipe);
1677
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001678 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001679 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001680 POSTING_READ(DPLL_MD(pipe));
1681
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001682 mutex_unlock(&dev_priv->dpio_lock);
1683}
1684
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001685static int intel_num_dvo_pipes(struct drm_device *dev)
1686{
1687 struct intel_crtc *crtc;
1688 int count = 0;
1689
1690 for_each_intel_crtc(dev, crtc)
1691 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001692 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001693
1694 return count;
1695}
1696
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001697static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001698{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001699 struct drm_device *dev = crtc->base.dev;
1700 struct drm_i915_private *dev_priv = dev->dev_private;
1701 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001702 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001703
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001704 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001705
1706 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001707 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001708
1709 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001710 if (IS_MOBILE(dev) && !IS_I830(dev))
1711 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001712
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001713 /* Enable DVO 2x clock on both PLLs if necessary */
1714 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1715 /*
1716 * It appears to be important that we don't enable this
1717 * for the current pipe before otherwise configuring the
1718 * PLL. No idea how this should be handled if multiple
1719 * DVO outputs are enabled simultaneosly.
1720 */
1721 dpll |= DPLL_DVO_2X_MODE;
1722 I915_WRITE(DPLL(!crtc->pipe),
1723 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1724 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001725
1726 /* Wait for the clocks to stabilize. */
1727 POSTING_READ(reg);
1728 udelay(150);
1729
1730 if (INTEL_INFO(dev)->gen >= 4) {
1731 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001732 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001733 } else {
1734 /* The pixel multiplier can only be updated once the
1735 * DPLL is enabled and the clocks are stable.
1736 *
1737 * So write it again.
1738 */
1739 I915_WRITE(reg, dpll);
1740 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001741
1742 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001743 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001744 POSTING_READ(reg);
1745 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001746 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001747 POSTING_READ(reg);
1748 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001749 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001750 POSTING_READ(reg);
1751 udelay(150); /* wait for warmup */
1752}
1753
1754/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001755 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001756 * @dev_priv: i915 private structure
1757 * @pipe: pipe PLL to disable
1758 *
1759 * Disable the PLL for @pipe, making sure the pipe is off first.
1760 *
1761 * Note! This is for pre-ILK only.
1762 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001763static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001764{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001765 struct drm_device *dev = crtc->base.dev;
1766 struct drm_i915_private *dev_priv = dev->dev_private;
1767 enum pipe pipe = crtc->pipe;
1768
1769 /* Disable DVO 2x clock on both PLLs if necessary */
1770 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001771 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001772 intel_num_dvo_pipes(dev) == 1) {
1773 I915_WRITE(DPLL(PIPE_B),
1774 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1775 I915_WRITE(DPLL(PIPE_A),
1776 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1777 }
1778
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001779 /* Don't disable pipe or pipe PLLs if needed */
1780 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1781 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001782 return;
1783
1784 /* Make sure the pipe isn't still relying on us */
1785 assert_pipe_disabled(dev_priv, pipe);
1786
Daniel Vetter50b44a42013-06-05 13:34:33 +02001787 I915_WRITE(DPLL(pipe), 0);
1788 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001789}
1790
Jesse Barnesf6071162013-10-01 10:41:38 -07001791static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1792{
1793 u32 val = 0;
1794
1795 /* Make sure the pipe isn't still relying on us */
1796 assert_pipe_disabled(dev_priv, pipe);
1797
Imre Deake5cbfbf2014-01-09 17:08:16 +02001798 /*
1799 * Leave integrated clock source and reference clock enabled for pipe B.
1800 * The latter is needed for VGA hotplug / manual detection.
1801 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001802 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001803 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001804 I915_WRITE(DPLL(pipe), val);
1805 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001806
1807}
1808
1809static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1810{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001811 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001812 u32 val;
1813
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001814 /* Make sure the pipe isn't still relying on us */
1815 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001816
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001817 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001818 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001819 if (pipe != PIPE_A)
1820 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1821 I915_WRITE(DPLL(pipe), val);
1822 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001823
1824 mutex_lock(&dev_priv->dpio_lock);
1825
1826 /* Disable 10bit clock to display controller */
1827 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1828 val &= ~DPIO_DCLKP_EN;
1829 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1830
Ville Syrjälä61407f62014-05-27 16:32:55 +03001831 /* disable left/right clock distribution */
1832 if (pipe != PIPE_B) {
1833 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1834 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1835 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1836 } else {
1837 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1838 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1839 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1840 }
1841
Ville Syrjäläd7520482014-04-09 13:28:59 +03001842 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001843}
1844
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001845void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001846 struct intel_digital_port *dport,
1847 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001848{
1849 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001850 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001851
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001852 switch (dport->port) {
1853 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001854 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001855 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001856 break;
1857 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001858 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001859 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001860 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001861 break;
1862 case PORT_D:
1863 port_mask = DPLL_PORTD_READY_MASK;
1864 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001865 break;
1866 default:
1867 BUG();
1868 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001869
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001870 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1871 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1872 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001873}
1874
Daniel Vetterb14b1052014-04-24 23:55:13 +02001875static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1876{
1877 struct drm_device *dev = crtc->base.dev;
1878 struct drm_i915_private *dev_priv = dev->dev_private;
1879 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1880
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001881 if (WARN_ON(pll == NULL))
1882 return;
1883
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001884 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001885 if (pll->active == 0) {
1886 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1887 WARN_ON(pll->on);
1888 assert_shared_dpll_disabled(dev_priv, pll);
1889
1890 pll->mode_set(dev_priv, pll);
1891 }
1892}
1893
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001894/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001895 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001896 * @dev_priv: i915 private structure
1897 * @pipe: pipe PLL to enable
1898 *
1899 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1900 * drives the transcoder clock.
1901 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001902static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001903{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001904 struct drm_device *dev = crtc->base.dev;
1905 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001906 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001907
Daniel Vetter87a875b2013-06-05 13:34:19 +02001908 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001909 return;
1910
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001911 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001912 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001913
Damien Lespiau74dd6922014-07-29 18:06:17 +01001914 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001915 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001916 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001917
Daniel Vettercdbd2312013-06-05 13:34:03 +02001918 if (pll->active++) {
1919 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001920 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001921 return;
1922 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001923 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001924
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001925 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1926
Daniel Vetter46edb022013-06-05 13:34:12 +02001927 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001928 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001929 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001930}
1931
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001932static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001933{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001934 struct drm_device *dev = crtc->base.dev;
1935 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001936 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001937
Jesse Barnes92f25842011-01-04 15:09:34 -08001938 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001939 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001940 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001941 return;
1942
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001943 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001944 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001945
Daniel Vetter46edb022013-06-05 13:34:12 +02001946 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1947 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001948 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001949
Chris Wilson48da64a2012-05-13 20:16:12 +01001950 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001951 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001952 return;
1953 }
1954
Daniel Vettere9d69442013-06-05 13:34:15 +02001955 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001956 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001957 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001958 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001959
Daniel Vetter46edb022013-06-05 13:34:12 +02001960 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001961 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001962 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001963
1964 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001965}
1966
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001967static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1968 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001969{
Daniel Vetter23670b322012-11-01 09:15:30 +01001970 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001971 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001973 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001974
1975 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001976 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001977
1978 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001979 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001980 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001981
1982 /* FDI must be feeding us bits for PCH ports */
1983 assert_fdi_tx_enabled(dev_priv, pipe);
1984 assert_fdi_rx_enabled(dev_priv, pipe);
1985
Daniel Vetter23670b322012-11-01 09:15:30 +01001986 if (HAS_PCH_CPT(dev)) {
1987 /* Workaround: Set the timing override bit before enabling the
1988 * pch transcoder. */
1989 reg = TRANS_CHICKEN2(pipe);
1990 val = I915_READ(reg);
1991 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1992 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001993 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001994
Daniel Vetterab9412b2013-05-03 11:49:46 +02001995 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001996 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001997 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001998
1999 if (HAS_PCH_IBX(dev_priv->dev)) {
2000 /*
2001 * make the BPC in transcoder be consistent with
2002 * that in pipeconf reg.
2003 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002004 val &= ~PIPECONF_BPC_MASK;
2005 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002006 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002007
2008 val &= ~TRANS_INTERLACE_MASK;
2009 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002010 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002011 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002012 val |= TRANS_LEGACY_INTERLACED_ILK;
2013 else
2014 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002015 else
2016 val |= TRANS_PROGRESSIVE;
2017
Jesse Barnes040484a2011-01-03 12:14:26 -08002018 I915_WRITE(reg, val | TRANS_ENABLE);
2019 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002020 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002021}
2022
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002023static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002024 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002025{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002026 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002027
2028 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002029 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002030
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002031 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002032 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002033 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002034
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002035 /* Workaround: set timing override bit. */
2036 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002037 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002038 I915_WRITE(_TRANSA_CHICKEN2, val);
2039
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002040 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002041 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002042
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002043 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2044 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002045 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002046 else
2047 val |= TRANS_PROGRESSIVE;
2048
Daniel Vetterab9412b2013-05-03 11:49:46 +02002049 I915_WRITE(LPT_TRANSCONF, val);
2050 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002051 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002052}
2053
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002054static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2055 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002056{
Daniel Vetter23670b322012-11-01 09:15:30 +01002057 struct drm_device *dev = dev_priv->dev;
2058 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002059
2060 /* FDI relies on the transcoder */
2061 assert_fdi_tx_disabled(dev_priv, pipe);
2062 assert_fdi_rx_disabled(dev_priv, pipe);
2063
Jesse Barnes291906f2011-02-02 12:28:03 -08002064 /* Ports must be off as well */
2065 assert_pch_ports_disabled(dev_priv, pipe);
2066
Daniel Vetterab9412b2013-05-03 11:49:46 +02002067 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002068 val = I915_READ(reg);
2069 val &= ~TRANS_ENABLE;
2070 I915_WRITE(reg, val);
2071 /* wait for PCH transcoder off, transcoder state */
2072 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002073 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002074
2075 if (!HAS_PCH_IBX(dev)) {
2076 /* Workaround: Clear the timing override chicken bit again. */
2077 reg = TRANS_CHICKEN2(pipe);
2078 val = I915_READ(reg);
2079 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2080 I915_WRITE(reg, val);
2081 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002082}
2083
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002084static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002085{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002086 u32 val;
2087
Daniel Vetterab9412b2013-05-03 11:49:46 +02002088 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002089 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002090 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002091 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002092 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002093 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002094
2095 /* Workaround: clear timing override bit. */
2096 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002097 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002098 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002099}
2100
2101/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002102 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002103 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002104 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002105 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002106 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002107 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002108static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002109{
Paulo Zanoni03722642014-01-17 13:51:09 -02002110 struct drm_device *dev = crtc->base.dev;
2111 struct drm_i915_private *dev_priv = dev->dev_private;
2112 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002113 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2114 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002115 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002116 int reg;
2117 u32 val;
2118
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002119 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002120 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002121 assert_sprites_disabled(dev_priv, pipe);
2122
Paulo Zanoni681e5812012-12-06 11:12:38 -02002123 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002124 pch_transcoder = TRANSCODER_A;
2125 else
2126 pch_transcoder = pipe;
2127
Jesse Barnesb24e7172011-01-04 15:09:30 -08002128 /*
2129 * A pipe without a PLL won't actually be able to drive bits from
2130 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2131 * need the check.
2132 */
Imre Deak50360402015-01-16 00:55:16 -08002133 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002134 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002135 assert_dsi_pll_enabled(dev_priv);
2136 else
2137 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002138 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002139 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002140 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002141 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002142 assert_fdi_tx_pll_enabled(dev_priv,
2143 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002144 }
2145 /* FIXME: assert CPU port conditions for SNB+ */
2146 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002147
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002148 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002149 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002150 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002151 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2152 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002153 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002154 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002155
2156 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002157 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002158}
2159
2160/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002161 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002162 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002163 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002164 * Disable the pipe of @crtc, making sure that various hardware
2165 * specific requirements are met, if applicable, e.g. plane
2166 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002167 *
2168 * Will wait until the pipe has shut down before returning.
2169 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002170static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002171{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002172 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002173 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002174 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002175 int reg;
2176 u32 val;
2177
2178 /*
2179 * Make sure planes won't keep trying to pump pixels to us,
2180 * or we might hang the display.
2181 */
2182 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002183 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002184 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002185
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002186 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002187 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002188 if ((val & PIPECONF_ENABLE) == 0)
2189 return;
2190
Ville Syrjälä67adc642014-08-15 01:21:57 +03002191 /*
2192 * Double wide has implications for planes
2193 * so best keep it disabled when not needed.
2194 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002195 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002196 val &= ~PIPECONF_DOUBLE_WIDE;
2197
2198 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002199 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2200 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002201 val &= ~PIPECONF_ENABLE;
2202
2203 I915_WRITE(reg, val);
2204 if ((val & PIPECONF_ENABLE) == 0)
2205 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002206}
2207
Keith Packardd74362c2011-07-28 14:47:14 -07002208/*
2209 * Plane regs are double buffered, going from enabled->disabled needs a
2210 * trigger in order to latch. The display address reg provides this.
2211 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002212void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2213 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002214{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002215 struct drm_device *dev = dev_priv->dev;
2216 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002217
2218 I915_WRITE(reg, I915_READ(reg));
2219 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002220}
2221
Jesse Barnesb24e7172011-01-04 15:09:30 -08002222/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002223 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002224 * @plane: plane to be enabled
2225 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002226 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002227 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002228 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002229static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2230 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002231{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002232 struct drm_device *dev = plane->dev;
2233 struct drm_i915_private *dev_priv = dev->dev_private;
2234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002235
2236 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002237 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002238 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002239
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002240 dev_priv->display.update_primary_plane(crtc, plane->fb,
2241 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002242}
2243
Chris Wilson693db182013-03-05 14:52:39 +00002244static bool need_vtd_wa(struct drm_device *dev)
2245{
2246#ifdef CONFIG_INTEL_IOMMU
2247 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2248 return true;
2249#endif
2250 return false;
2251}
2252
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002253unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002254intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2255 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002256{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002257 unsigned int tile_height;
2258 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002259
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002260 switch (fb_format_modifier) {
2261 case DRM_FORMAT_MOD_NONE:
2262 tile_height = 1;
2263 break;
2264 case I915_FORMAT_MOD_X_TILED:
2265 tile_height = IS_GEN2(dev) ? 16 : 8;
2266 break;
2267 case I915_FORMAT_MOD_Y_TILED:
2268 tile_height = 32;
2269 break;
2270 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002271 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2272 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002273 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002274 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002275 tile_height = 64;
2276 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002277 case 2:
2278 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002279 tile_height = 32;
2280 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002281 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002282 tile_height = 16;
2283 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002284 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002285 WARN_ONCE(1,
2286 "128-bit pixels are not supported for display!");
2287 tile_height = 16;
2288 break;
2289 }
2290 break;
2291 default:
2292 MISSING_CASE(fb_format_modifier);
2293 tile_height = 1;
2294 break;
2295 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002296
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002297 return tile_height;
2298}
2299
2300unsigned int
2301intel_fb_align_height(struct drm_device *dev, unsigned int height,
2302 uint32_t pixel_format, uint64_t fb_format_modifier)
2303{
2304 return ALIGN(height, intel_tile_height(dev, pixel_format,
2305 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002306}
2307
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002308static int
2309intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2310 const struct drm_plane_state *plane_state)
2311{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002312 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002313
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002314 *view = i915_ggtt_view_normal;
2315
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002316 if (!plane_state)
2317 return 0;
2318
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002319 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002320 return 0;
2321
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002322 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002323
2324 info->height = fb->height;
2325 info->pixel_format = fb->pixel_format;
2326 info->pitch = fb->pitches[0];
2327 info->fb_modifier = fb->modifier[0];
2328
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002329 return 0;
2330}
2331
Chris Wilson127bd2a2010-07-23 23:32:05 +01002332int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002333intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2334 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002335 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002336 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002337{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002338 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002339 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002340 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002341 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002342 u32 alignment;
2343 int ret;
2344
Matt Roperebcdd392014-07-09 16:22:11 -07002345 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2346
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002347 switch (fb->modifier[0]) {
2348 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002349 if (INTEL_INFO(dev)->gen >= 9)
2350 alignment = 256 * 1024;
2351 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002352 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002353 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002354 alignment = 4 * 1024;
2355 else
2356 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002357 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002358 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002359 if (INTEL_INFO(dev)->gen >= 9)
2360 alignment = 256 * 1024;
2361 else {
2362 /* pin() will align the object as required by fence */
2363 alignment = 0;
2364 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002365 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002366 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002367 case I915_FORMAT_MOD_Yf_TILED:
2368 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2369 "Y tiling bo slipped through, driver bug!\n"))
2370 return -EINVAL;
2371 alignment = 1 * 1024 * 1024;
2372 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002373 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002374 MISSING_CASE(fb->modifier[0]);
2375 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002376 }
2377
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002378 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2379 if (ret)
2380 return ret;
2381
Chris Wilson693db182013-03-05 14:52:39 +00002382 /* Note that the w/a also requires 64 PTE of padding following the
2383 * bo. We currently fill all unused PTE with the shadow page and so
2384 * we should always have valid PTE following the scanout preventing
2385 * the VT-d warning.
2386 */
2387 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2388 alignment = 256 * 1024;
2389
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002390 /*
2391 * Global gtt pte registers are special registers which actually forward
2392 * writes to a chunk of system memory. Which means that there is no risk
2393 * that the register values disappear as soon as we call
2394 * intel_runtime_pm_put(), so it is correct to wrap only the
2395 * pin/unpin/fence and not more.
2396 */
2397 intel_runtime_pm_get(dev_priv);
2398
Chris Wilsonce453d82011-02-21 14:43:56 +00002399 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002400 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002401 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002402 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002403 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002404
2405 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2406 * fence, whereas 965+ only requires a fence if using
2407 * framebuffer compression. For simplicity, we always install
2408 * a fence as the cost is not that onerous.
2409 */
Chris Wilson06d98132012-04-17 15:31:24 +01002410 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002411 if (ret)
2412 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002413
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002414 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002415
Chris Wilsonce453d82011-02-21 14:43:56 +00002416 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002417 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002418 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002419
2420err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002421 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002422err_interruptible:
2423 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002424 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002425 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002426}
2427
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002428static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2429 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002430{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002431 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002432 struct i915_ggtt_view view;
2433 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002434
Matt Roperebcdd392014-07-09 16:22:11 -07002435 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2436
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002437 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2438 WARN_ONCE(ret, "Couldn't get view from plane state!");
2439
Chris Wilson1690e1e2011-12-14 13:57:08 +01002440 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002441 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002442}
2443
Daniel Vetterc2c75132012-07-05 12:17:30 +02002444/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2445 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002446unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2447 unsigned int tiling_mode,
2448 unsigned int cpp,
2449 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002450{
Chris Wilsonbc752862013-02-21 20:04:31 +00002451 if (tiling_mode != I915_TILING_NONE) {
2452 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002453
Chris Wilsonbc752862013-02-21 20:04:31 +00002454 tile_rows = *y / 8;
2455 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002456
Chris Wilsonbc752862013-02-21 20:04:31 +00002457 tiles = *x / (512/cpp);
2458 *x %= 512/cpp;
2459
2460 return tile_rows * pitch * 8 + tiles * 4096;
2461 } else {
2462 unsigned int offset;
2463
2464 offset = *y * pitch + *x * cpp;
2465 *y = 0;
2466 *x = (offset & 4095) / cpp;
2467 return offset & -4096;
2468 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002469}
2470
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002471static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002472{
2473 switch (format) {
2474 case DISPPLANE_8BPP:
2475 return DRM_FORMAT_C8;
2476 case DISPPLANE_BGRX555:
2477 return DRM_FORMAT_XRGB1555;
2478 case DISPPLANE_BGRX565:
2479 return DRM_FORMAT_RGB565;
2480 default:
2481 case DISPPLANE_BGRX888:
2482 return DRM_FORMAT_XRGB8888;
2483 case DISPPLANE_RGBX888:
2484 return DRM_FORMAT_XBGR8888;
2485 case DISPPLANE_BGRX101010:
2486 return DRM_FORMAT_XRGB2101010;
2487 case DISPPLANE_RGBX101010:
2488 return DRM_FORMAT_XBGR2101010;
2489 }
2490}
2491
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002492static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2493{
2494 switch (format) {
2495 case PLANE_CTL_FORMAT_RGB_565:
2496 return DRM_FORMAT_RGB565;
2497 default:
2498 case PLANE_CTL_FORMAT_XRGB_8888:
2499 if (rgb_order) {
2500 if (alpha)
2501 return DRM_FORMAT_ABGR8888;
2502 else
2503 return DRM_FORMAT_XBGR8888;
2504 } else {
2505 if (alpha)
2506 return DRM_FORMAT_ARGB8888;
2507 else
2508 return DRM_FORMAT_XRGB8888;
2509 }
2510 case PLANE_CTL_FORMAT_XRGB_2101010:
2511 if (rgb_order)
2512 return DRM_FORMAT_XBGR2101010;
2513 else
2514 return DRM_FORMAT_XRGB2101010;
2515 }
2516}
2517
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002518static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002519intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2520 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002521{
2522 struct drm_device *dev = crtc->base.dev;
2523 struct drm_i915_gem_object *obj = NULL;
2524 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002525 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002526 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2527 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2528 PAGE_SIZE);
2529
2530 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002531
Chris Wilsonff2652e2014-03-10 08:07:02 +00002532 if (plane_config->size == 0)
2533 return false;
2534
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002535 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2536 base_aligned,
2537 base_aligned,
2538 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002539 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002540 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002541
Damien Lespiau49af4492015-01-20 12:51:44 +00002542 obj->tiling_mode = plane_config->tiling;
2543 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002544 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002545
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002546 mode_cmd.pixel_format = fb->pixel_format;
2547 mode_cmd.width = fb->width;
2548 mode_cmd.height = fb->height;
2549 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002550 mode_cmd.modifier[0] = fb->modifier[0];
2551 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002552
2553 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002554 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002555 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002556 DRM_DEBUG_KMS("intel fb init failed\n");
2557 goto out_unref_obj;
2558 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002559 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002560
Daniel Vetterf6936e22015-03-26 12:17:05 +01002561 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002562 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002563
2564out_unref_obj:
2565 drm_gem_object_unreference(&obj->base);
2566 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567 return false;
2568}
2569
Matt Roperafd65eb2015-02-03 13:10:04 -08002570/* Update plane->state->fb to match plane->fb after driver-internal updates */
2571static void
2572update_state_fb(struct drm_plane *plane)
2573{
2574 if (plane->fb == plane->state->fb)
2575 return;
2576
2577 if (plane->state->fb)
2578 drm_framebuffer_unreference(plane->state->fb);
2579 plane->state->fb = plane->fb;
2580 if (plane->state->fb)
2581 drm_framebuffer_reference(plane->state->fb);
2582}
2583
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002584static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002585intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2586 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002587{
2588 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002589 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002590 struct drm_crtc *c;
2591 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002592 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002593 struct drm_plane *primary = intel_crtc->base.primary;
2594 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002595
Damien Lespiau2d140302015-02-05 17:22:18 +00002596 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002597 return;
2598
Daniel Vetterf6936e22015-03-26 12:17:05 +01002599 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002600 fb = &plane_config->fb->base;
2601 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002602 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002603
Damien Lespiau2d140302015-02-05 17:22:18 +00002604 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002605
2606 /*
2607 * Failed to alloc the obj, check to see if we should share
2608 * an fb with another CRTC instead
2609 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002610 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002611 i = to_intel_crtc(c);
2612
2613 if (c == &intel_crtc->base)
2614 continue;
2615
Matt Roper2ff8fde2014-07-08 07:50:07 -07002616 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002617 continue;
2618
Daniel Vetter88595ac2015-03-26 12:42:24 +01002619 fb = c->primary->fb;
2620 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002621 continue;
2622
Daniel Vetter88595ac2015-03-26 12:42:24 +01002623 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002624 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002625 drm_framebuffer_reference(fb);
2626 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002627 }
2628 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002629
2630 return;
2631
2632valid_fb:
2633 obj = intel_fb_obj(fb);
2634 if (obj->tiling_mode != I915_TILING_NONE)
2635 dev_priv->preserve_bios_swizzle = true;
2636
2637 primary->fb = fb;
2638 primary->state->crtc = &intel_crtc->base;
2639 primary->crtc = &intel_crtc->base;
2640 update_state_fb(primary);
2641 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002642}
2643
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002644static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2645 struct drm_framebuffer *fb,
2646 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002647{
2648 struct drm_device *dev = crtc->dev;
2649 struct drm_i915_private *dev_priv = dev->dev_private;
2650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002651 struct drm_plane *primary = crtc->primary;
2652 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002653 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002654 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002655 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002656 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002657 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302658 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002659
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002660 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002661 I915_WRITE(reg, 0);
2662 if (INTEL_INFO(dev)->gen >= 4)
2663 I915_WRITE(DSPSURF(plane), 0);
2664 else
2665 I915_WRITE(DSPADDR(plane), 0);
2666 POSTING_READ(reg);
2667 return;
2668 }
2669
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002670 obj = intel_fb_obj(fb);
2671 if (WARN_ON(obj == NULL))
2672 return;
2673
2674 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2675
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002676 dspcntr = DISPPLANE_GAMMA_ENABLE;
2677
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002678 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002679
2680 if (INTEL_INFO(dev)->gen < 4) {
2681 if (intel_crtc->pipe == PIPE_B)
2682 dspcntr |= DISPPLANE_SEL_PIPE_B;
2683
2684 /* pipesrc and dspsize control the size that is scaled from,
2685 * which should always be the user's requested size.
2686 */
2687 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002688 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2689 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002690 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002691 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2692 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002693 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2694 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002695 I915_WRITE(PRIMPOS(plane), 0);
2696 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002697 }
2698
Ville Syrjälä57779d02012-10-31 17:50:14 +02002699 switch (fb->pixel_format) {
2700 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002701 dspcntr |= DISPPLANE_8BPP;
2702 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002703 case DRM_FORMAT_XRGB1555:
2704 case DRM_FORMAT_ARGB1555:
2705 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002706 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707 case DRM_FORMAT_RGB565:
2708 dspcntr |= DISPPLANE_BGRX565;
2709 break;
2710 case DRM_FORMAT_XRGB8888:
2711 case DRM_FORMAT_ARGB8888:
2712 dspcntr |= DISPPLANE_BGRX888;
2713 break;
2714 case DRM_FORMAT_XBGR8888:
2715 case DRM_FORMAT_ABGR8888:
2716 dspcntr |= DISPPLANE_RGBX888;
2717 break;
2718 case DRM_FORMAT_XRGB2101010:
2719 case DRM_FORMAT_ARGB2101010:
2720 dspcntr |= DISPPLANE_BGRX101010;
2721 break;
2722 case DRM_FORMAT_XBGR2101010:
2723 case DRM_FORMAT_ABGR2101010:
2724 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002725 break;
2726 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002727 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002728 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002729
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002730 if (INTEL_INFO(dev)->gen >= 4 &&
2731 obj->tiling_mode != I915_TILING_NONE)
2732 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002733
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002734 if (IS_G4X(dev))
2735 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2736
Ville Syrjäläb98971272014-08-27 16:51:22 +03002737 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002738
Daniel Vetterc2c75132012-07-05 12:17:30 +02002739 if (INTEL_INFO(dev)->gen >= 4) {
2740 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002741 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002742 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002743 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002744 linear_offset -= intel_crtc->dspaddr_offset;
2745 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002746 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002747 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002748
Matt Roper8e7d6882015-01-21 16:35:41 -08002749 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302750 dspcntr |= DISPPLANE_ROTATE_180;
2751
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002752 x += (intel_crtc->config->pipe_src_w - 1);
2753 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302754
2755 /* Finding the last pixel of the last line of the display
2756 data and adding to linear_offset*/
2757 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002758 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2759 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302760 }
2761
2762 I915_WRITE(reg, dspcntr);
2763
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002764 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002765 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002766 I915_WRITE(DSPSURF(plane),
2767 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002769 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002770 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002771 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002772 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773}
2774
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002775static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2776 struct drm_framebuffer *fb,
2777 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778{
2779 struct drm_device *dev = crtc->dev;
2780 struct drm_i915_private *dev_priv = dev->dev_private;
2781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002782 struct drm_plane *primary = crtc->primary;
2783 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002784 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002786 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002787 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002788 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302789 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002790
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002791 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002792 I915_WRITE(reg, 0);
2793 I915_WRITE(DSPSURF(plane), 0);
2794 POSTING_READ(reg);
2795 return;
2796 }
2797
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002798 obj = intel_fb_obj(fb);
2799 if (WARN_ON(obj == NULL))
2800 return;
2801
2802 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2803
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002804 dspcntr = DISPPLANE_GAMMA_ENABLE;
2805
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002806 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002807
2808 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2809 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2810
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 switch (fb->pixel_format) {
2812 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813 dspcntr |= DISPPLANE_8BPP;
2814 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002815 case DRM_FORMAT_RGB565:
2816 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002817 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002818 case DRM_FORMAT_XRGB8888:
2819 case DRM_FORMAT_ARGB8888:
2820 dspcntr |= DISPPLANE_BGRX888;
2821 break;
2822 case DRM_FORMAT_XBGR8888:
2823 case DRM_FORMAT_ABGR8888:
2824 dspcntr |= DISPPLANE_RGBX888;
2825 break;
2826 case DRM_FORMAT_XRGB2101010:
2827 case DRM_FORMAT_ARGB2101010:
2828 dspcntr |= DISPPLANE_BGRX101010;
2829 break;
2830 case DRM_FORMAT_XBGR2101010:
2831 case DRM_FORMAT_ABGR2101010:
2832 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002833 break;
2834 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002835 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002836 }
2837
2838 if (obj->tiling_mode != I915_TILING_NONE)
2839 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002840
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002841 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002842 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002843
Ville Syrjäläb98971272014-08-27 16:51:22 +03002844 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002845 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002846 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002847 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002848 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002849 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002850 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302851 dspcntr |= DISPPLANE_ROTATE_180;
2852
2853 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002854 x += (intel_crtc->config->pipe_src_w - 1);
2855 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302856
2857 /* Finding the last pixel of the last line of the display
2858 data and adding to linear_offset*/
2859 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002860 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2861 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302862 }
2863 }
2864
2865 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002866
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002867 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002868 I915_WRITE(DSPSURF(plane),
2869 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002870 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002871 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2872 } else {
2873 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2874 I915_WRITE(DSPLINOFF(plane), linear_offset);
2875 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002876 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002877}
2878
Damien Lespiaub3218032015-02-27 11:15:18 +00002879u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2880 uint32_t pixel_format)
2881{
2882 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2883
2884 /*
2885 * The stride is either expressed as a multiple of 64 bytes
2886 * chunks for linear buffers or in number of tiles for tiled
2887 * buffers.
2888 */
2889 switch (fb_modifier) {
2890 case DRM_FORMAT_MOD_NONE:
2891 return 64;
2892 case I915_FORMAT_MOD_X_TILED:
2893 if (INTEL_INFO(dev)->gen == 2)
2894 return 128;
2895 return 512;
2896 case I915_FORMAT_MOD_Y_TILED:
2897 /* No need to check for old gens and Y tiling since this is
2898 * about the display engine and those will be blocked before
2899 * we get here.
2900 */
2901 return 128;
2902 case I915_FORMAT_MOD_Yf_TILED:
2903 if (bits_per_pixel == 8)
2904 return 64;
2905 else
2906 return 128;
2907 default:
2908 MISSING_CASE(fb_modifier);
2909 return 64;
2910 }
2911}
2912
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002913unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2914 struct drm_i915_gem_object *obj)
2915{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002916 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002917
2918 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002919 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002920
2921 return i915_gem_obj_ggtt_offset_view(obj, view);
2922}
2923
Chandra Kondurua1b22782015-04-07 15:28:45 -07002924/*
2925 * This function detaches (aka. unbinds) unused scalers in hardware
2926 */
2927void skl_detach_scalers(struct intel_crtc *intel_crtc)
2928{
2929 struct drm_device *dev;
2930 struct drm_i915_private *dev_priv;
2931 struct intel_crtc_scaler_state *scaler_state;
2932 int i;
2933
2934 if (!intel_crtc || !intel_crtc->config)
2935 return;
2936
2937 dev = intel_crtc->base.dev;
2938 dev_priv = dev->dev_private;
2939 scaler_state = &intel_crtc->config->scaler_state;
2940
2941 /* loop through and disable scalers that aren't in use */
2942 for (i = 0; i < intel_crtc->num_scalers; i++) {
2943 if (!scaler_state->scalers[i].in_use) {
2944 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2945 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2946 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2947 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2948 intel_crtc->base.base.id, intel_crtc->pipe, i);
2949 }
2950 }
2951}
2952
Chandra Konduru6156a452015-04-27 13:48:39 -07002953u32 skl_plane_ctl_format(uint32_t pixel_format)
2954{
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002956 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002961 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002963 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002964 /*
2965 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2966 * to be already pre-multiplied. We need to add a knob (or a different
2967 * DRM_FORMAT) for user-space to configure that.
2968 */
2969 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002970 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002976 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002982 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002984 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002985 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002986 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002988 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002989 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002990
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002991 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002992}
2993
2994u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2995{
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 switch (fb_modifier) {
2997 case DRM_FORMAT_MOD_NONE:
2998 break;
2999 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003000 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003002 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003003 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003004 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003005 default:
3006 MISSING_CASE(fb_modifier);
3007 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003008
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003009 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003010}
3011
3012u32 skl_plane_ctl_rotation(unsigned int rotation)
3013{
Chandra Konduru6156a452015-04-27 13:48:39 -07003014 switch (rotation) {
3015 case BIT(DRM_ROTATE_0):
3016 break;
3017 case BIT(DRM_ROTATE_90):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003018 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003019 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003020 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003021 case BIT(DRM_ROTATE_270):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003022 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003023 default:
3024 MISSING_CASE(rotation);
3025 }
3026
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003027 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003028}
3029
Damien Lespiau70d21f02013-07-03 21:06:04 +01003030static void skylake_update_primary_plane(struct drm_crtc *crtc,
3031 struct drm_framebuffer *fb,
3032 int x, int y)
3033{
3034 struct drm_device *dev = crtc->dev;
3035 struct drm_i915_private *dev_priv = dev->dev_private;
3036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003037 struct drm_plane *plane = crtc->primary;
3038 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003039 struct drm_i915_gem_object *obj;
3040 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303041 u32 plane_ctl, stride_div, stride;
3042 u32 tile_height, plane_offset, plane_size;
3043 unsigned int rotation;
3044 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003045 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003046 struct intel_crtc_state *crtc_state = intel_crtc->config;
3047 struct intel_plane_state *plane_state;
3048 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3049 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3050 int scaler_id = -1;
3051
Chandra Konduru6156a452015-04-27 13:48:39 -07003052 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003053
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003054 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3056 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3057 POSTING_READ(PLANE_CTL(pipe, 0));
3058 return;
3059 }
3060
3061 plane_ctl = PLANE_CTL_ENABLE |
3062 PLANE_CTL_PIPE_GAMMA_ENABLE |
3063 PLANE_CTL_PIPE_CSC_ENABLE;
3064
Chandra Konduru6156a452015-04-27 13:48:39 -07003065 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3066 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003067 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303068
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303069 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003070 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003071
Damien Lespiaub3218032015-02-27 11:15:18 +00003072 obj = intel_fb_obj(fb);
3073 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3074 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303075 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3076
Chandra Konduru6156a452015-04-27 13:48:39 -07003077 /*
3078 * FIXME: intel_plane_state->src, dst aren't set when transitional
3079 * update_plane helpers are called from legacy paths.
3080 * Once full atomic crtc is available, below check can be avoided.
3081 */
3082 if (drm_rect_width(&plane_state->src)) {
3083 scaler_id = plane_state->scaler_id;
3084 src_x = plane_state->src.x1 >> 16;
3085 src_y = plane_state->src.y1 >> 16;
3086 src_w = drm_rect_width(&plane_state->src) >> 16;
3087 src_h = drm_rect_height(&plane_state->src) >> 16;
3088 dst_x = plane_state->dst.x1;
3089 dst_y = plane_state->dst.y1;
3090 dst_w = drm_rect_width(&plane_state->dst);
3091 dst_h = drm_rect_height(&plane_state->dst);
3092
3093 WARN_ON(x != src_x || y != src_y);
3094 } else {
3095 src_w = intel_crtc->config->pipe_src_w;
3096 src_h = intel_crtc->config->pipe_src_h;
3097 }
3098
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303099 if (intel_rotation_90_or_270(rotation)) {
3100 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003101 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303102 fb->modifier[0]);
3103 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003104 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303105 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003106 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303107 } else {
3108 stride = fb->pitches[0] / stride_div;
3109 x_offset = x;
3110 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003111 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303112 }
3113 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003114
Damien Lespiau70d21f02013-07-03 21:06:04 +01003115 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303116 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3117 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3118 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003119
3120 if (scaler_id >= 0) {
3121 uint32_t ps_ctrl = 0;
3122
3123 WARN_ON(!dst_w || !dst_h);
3124 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3125 crtc_state->scaler_state.scalers[scaler_id].mode;
3126 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3127 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3128 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3129 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3130 I915_WRITE(PLANE_POS(pipe, 0), 0);
3131 } else {
3132 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3133 }
3134
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003135 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003136
3137 POSTING_READ(PLANE_SURF(pipe, 0));
3138}
3139
Jesse Barnes17638cd2011-06-24 12:19:23 -07003140/* Assume fb object is pinned & idle & fenced and just update base pointers */
3141static int
3142intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3143 int x, int y, enum mode_set_atomic state)
3144{
3145 struct drm_device *dev = crtc->dev;
3146 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003147
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003148 if (dev_priv->display.disable_fbc)
3149 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003150
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003151 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3152
3153 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003154}
3155
Ville Syrjälä75147472014-11-24 18:28:11 +02003156static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003157{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003158 struct drm_crtc *crtc;
3159
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003160 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3162 enum plane plane = intel_crtc->plane;
3163
3164 intel_prepare_page_flip(dev, plane);
3165 intel_finish_page_flip_plane(dev, plane);
3166 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003167}
3168
3169static void intel_update_primary_planes(struct drm_device *dev)
3170{
3171 struct drm_i915_private *dev_priv = dev->dev_private;
3172 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003173
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003174 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3176
Rob Clark51fd3712013-11-19 12:10:12 -05003177 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003178 /*
3179 * FIXME: Once we have proper support for primary planes (and
3180 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003181 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003182 */
Matt Roperf4510a22014-04-01 15:22:40 -07003183 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003184 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003185 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003186 crtc->x,
3187 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003188 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003189 }
3190}
3191
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003192void intel_crtc_reset(struct intel_crtc *crtc)
3193{
3194 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3195
3196 if (!crtc->active)
3197 return;
3198
3199 intel_crtc_disable_planes(&crtc->base);
3200 dev_priv->display.crtc_disable(&crtc->base);
3201 dev_priv->display.crtc_enable(&crtc->base);
3202 intel_crtc_enable_planes(&crtc->base);
3203}
3204
Ville Syrjälä75147472014-11-24 18:28:11 +02003205void intel_prepare_reset(struct drm_device *dev)
3206{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003207 struct drm_i915_private *dev_priv = to_i915(dev);
3208 struct intel_crtc *crtc;
3209
Ville Syrjälä75147472014-11-24 18:28:11 +02003210 /* no reset support for gen2 */
3211 if (IS_GEN2(dev))
3212 return;
3213
3214 /* reset doesn't touch the display */
3215 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3216 return;
3217
3218 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003219
3220 /*
3221 * Disabling the crtcs gracefully seems nicer. Also the
3222 * g33 docs say we should at least disable all the planes.
3223 */
3224 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003225 if (!crtc->active)
3226 continue;
3227
3228 intel_crtc_disable_planes(&crtc->base);
3229 dev_priv->display.crtc_disable(&crtc->base);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003230 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003231}
3232
3233void intel_finish_reset(struct drm_device *dev)
3234{
3235 struct drm_i915_private *dev_priv = to_i915(dev);
3236
3237 /*
3238 * Flips in the rings will be nuked by the reset,
3239 * so complete all pending flips so that user space
3240 * will get its events and not get stuck.
3241 */
3242 intel_complete_page_flips(dev);
3243
3244 /* no reset support for gen2 */
3245 if (IS_GEN2(dev))
3246 return;
3247
3248 /* reset doesn't touch the display */
3249 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3250 /*
3251 * Flips in the rings have been nuked by the reset,
3252 * so update the base address of all primary
3253 * planes to the the last fb to make sure we're
3254 * showing the correct fb after a reset.
3255 */
3256 intel_update_primary_planes(dev);
3257 return;
3258 }
3259
3260 /*
3261 * The display has been reset as well,
3262 * so need a full re-initialization.
3263 */
3264 intel_runtime_pm_disable_interrupts(dev_priv);
3265 intel_runtime_pm_enable_interrupts(dev_priv);
3266
3267 intel_modeset_init_hw(dev);
3268
3269 spin_lock_irq(&dev_priv->irq_lock);
3270 if (dev_priv->display.hpd_irq_setup)
3271 dev_priv->display.hpd_irq_setup(dev);
3272 spin_unlock_irq(&dev_priv->irq_lock);
3273
3274 intel_modeset_setup_hw_state(dev, true);
3275
3276 intel_hpd_init(dev_priv);
3277
3278 drm_modeset_unlock_all(dev);
3279}
3280
Chris Wilson2e2f3512015-04-27 13:41:14 +01003281static void
Chris Wilson14667a42012-04-03 17:58:35 +01003282intel_finish_fb(struct drm_framebuffer *old_fb)
3283{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003284 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003285 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003286 bool was_interruptible = dev_priv->mm.interruptible;
3287 int ret;
3288
Chris Wilson14667a42012-04-03 17:58:35 +01003289 /* Big Hammer, we also need to ensure that any pending
3290 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3291 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003292 * framebuffer. Note that we rely on userspace rendering
3293 * into the buffer attached to the pipe they are waiting
3294 * on. If not, userspace generates a GPU hang with IPEHR
3295 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003296 *
3297 * This should only fail upon a hung GPU, in which case we
3298 * can safely continue.
3299 */
3300 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003301 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003302 dev_priv->mm.interruptible = was_interruptible;
3303
Chris Wilson2e2f3512015-04-27 13:41:14 +01003304 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003305}
3306
Chris Wilson7d5e3792014-03-04 13:15:08 +00003307static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3308{
3309 struct drm_device *dev = crtc->dev;
3310 struct drm_i915_private *dev_priv = dev->dev_private;
3311 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003312 bool pending;
3313
3314 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3315 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3316 return false;
3317
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003318 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003319 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003320 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003321
3322 return pending;
3323}
3324
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003325static void intel_update_pipe_size(struct intel_crtc *crtc)
3326{
3327 struct drm_device *dev = crtc->base.dev;
3328 struct drm_i915_private *dev_priv = dev->dev_private;
3329 const struct drm_display_mode *adjusted_mode;
3330
3331 if (!i915.fastboot)
3332 return;
3333
3334 /*
3335 * Update pipe size and adjust fitter if needed: the reason for this is
3336 * that in compute_mode_changes we check the native mode (not the pfit
3337 * mode) to see if we can flip rather than do a full mode set. In the
3338 * fastboot case, we'll flip, but if we don't update the pipesrc and
3339 * pfit state, we'll end up with a big fb scanned out into the wrong
3340 * sized surface.
3341 *
3342 * To fix this properly, we need to hoist the checks up into
3343 * compute_mode_changes (or above), check the actual pfit state and
3344 * whether the platform allows pfit disable with pipe active, and only
3345 * then update the pipesrc and pfit state, even on the flip path.
3346 */
3347
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003348 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003349
3350 I915_WRITE(PIPESRC(crtc->pipe),
3351 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3352 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003353 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003354 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3355 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003356 I915_WRITE(PF_CTL(crtc->pipe), 0);
3357 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3358 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3359 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003360 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3361 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003362}
3363
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003364static void intel_fdi_normal_train(struct drm_crtc *crtc)
3365{
3366 struct drm_device *dev = crtc->dev;
3367 struct drm_i915_private *dev_priv = dev->dev_private;
3368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3369 int pipe = intel_crtc->pipe;
3370 u32 reg, temp;
3371
3372 /* enable normal train */
3373 reg = FDI_TX_CTL(pipe);
3374 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003375 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003376 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3377 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003378 } else {
3379 temp &= ~FDI_LINK_TRAIN_NONE;
3380 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003381 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003382 I915_WRITE(reg, temp);
3383
3384 reg = FDI_RX_CTL(pipe);
3385 temp = I915_READ(reg);
3386 if (HAS_PCH_CPT(dev)) {
3387 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3388 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3389 } else {
3390 temp &= ~FDI_LINK_TRAIN_NONE;
3391 temp |= FDI_LINK_TRAIN_NONE;
3392 }
3393 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3394
3395 /* wait one idle pattern time */
3396 POSTING_READ(reg);
3397 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003398
3399 /* IVB wants error correction enabled */
3400 if (IS_IVYBRIDGE(dev))
3401 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3402 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003403}
3404
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003405/* The FDI link training functions for ILK/Ibexpeak. */
3406static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3407{
3408 struct drm_device *dev = crtc->dev;
3409 struct drm_i915_private *dev_priv = dev->dev_private;
3410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3411 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003412 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003414 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003415 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003416
Adam Jacksone1a44742010-06-25 15:32:14 -04003417 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3418 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 reg = FDI_RX_IMR(pipe);
3420 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003421 temp &= ~FDI_RX_SYMBOL_LOCK;
3422 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 I915_WRITE(reg, temp);
3424 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003425 udelay(150);
3426
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003428 reg = FDI_TX_CTL(pipe);
3429 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003430 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003431 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432 temp &= ~FDI_LINK_TRAIN_NONE;
3433 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 reg = FDI_RX_CTL(pipe);
3437 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 temp &= ~FDI_LINK_TRAIN_NONE;
3439 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3441
3442 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443 udelay(150);
3444
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003445 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003446 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3447 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3448 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003449
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003451 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3454
3455 if ((temp & FDI_RX_BIT_LOCK)) {
3456 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003458 break;
3459 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003461 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463
3464 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 reg = FDI_TX_CTL(pipe);
3466 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 temp &= ~FDI_LINK_TRAIN_NONE;
3468 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 reg = FDI_RX_CTL(pipe);
3472 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 temp &= ~FDI_LINK_TRAIN_NONE;
3474 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003475 I915_WRITE(reg, temp);
3476
3477 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003478 udelay(150);
3479
Chris Wilson5eddb702010-09-11 13:48:45 +01003480 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003481 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003483 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3484
3485 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003486 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487 DRM_DEBUG_KMS("FDI train 2 done.\n");
3488 break;
3489 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003491 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493
3494 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003495
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496}
3497
Akshay Joshi0206e352011-08-16 15:34:10 -04003498static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3500 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3501 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3502 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3503};
3504
3505/* The FDI link training functions for SNB/Cougarpoint. */
3506static void gen6_fdi_link_train(struct drm_crtc *crtc)
3507{
3508 struct drm_device *dev = crtc->dev;
3509 struct drm_i915_private *dev_priv = dev->dev_private;
3510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3511 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003512 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513
Adam Jacksone1a44742010-06-25 15:32:14 -04003514 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3515 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003516 reg = FDI_RX_IMR(pipe);
3517 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003518 temp &= ~FDI_RX_SYMBOL_LOCK;
3519 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003520 I915_WRITE(reg, temp);
3521
3522 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003523 udelay(150);
3524
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003526 reg = FDI_TX_CTL(pipe);
3527 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003528 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003529 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530 temp &= ~FDI_LINK_TRAIN_NONE;
3531 temp |= FDI_LINK_TRAIN_PATTERN_1;
3532 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3533 /* SNB-B */
3534 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003536
Daniel Vetterd74cf322012-10-26 10:58:13 +02003537 I915_WRITE(FDI_RX_MISC(pipe),
3538 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3539
Chris Wilson5eddb702010-09-11 13:48:45 +01003540 reg = FDI_RX_CTL(pipe);
3541 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542 if (HAS_PCH_CPT(dev)) {
3543 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3544 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3545 } else {
3546 temp &= ~FDI_LINK_TRAIN_NONE;
3547 temp |= FDI_LINK_TRAIN_PATTERN_1;
3548 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003549 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3550
3551 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003552 udelay(150);
3553
Akshay Joshi0206e352011-08-16 15:34:10 -04003554 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003555 reg = FDI_TX_CTL(pipe);
3556 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003557 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3558 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003559 I915_WRITE(reg, temp);
3560
3561 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003562 udelay(500);
3563
Sean Paulfa37d392012-03-02 12:53:39 -05003564 for (retry = 0; retry < 5; retry++) {
3565 reg = FDI_RX_IIR(pipe);
3566 temp = I915_READ(reg);
3567 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3568 if (temp & FDI_RX_BIT_LOCK) {
3569 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3570 DRM_DEBUG_KMS("FDI train 1 done.\n");
3571 break;
3572 }
3573 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 }
Sean Paulfa37d392012-03-02 12:53:39 -05003575 if (retry < 5)
3576 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003577 }
3578 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003579 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003580
3581 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003582 reg = FDI_TX_CTL(pipe);
3583 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584 temp &= ~FDI_LINK_TRAIN_NONE;
3585 temp |= FDI_LINK_TRAIN_PATTERN_2;
3586 if (IS_GEN6(dev)) {
3587 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3588 /* SNB-B */
3589 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3590 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003591 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003592
Chris Wilson5eddb702010-09-11 13:48:45 +01003593 reg = FDI_RX_CTL(pipe);
3594 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003595 if (HAS_PCH_CPT(dev)) {
3596 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3597 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3598 } else {
3599 temp &= ~FDI_LINK_TRAIN_NONE;
3600 temp |= FDI_LINK_TRAIN_PATTERN_2;
3601 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003602 I915_WRITE(reg, temp);
3603
3604 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003605 udelay(150);
3606
Akshay Joshi0206e352011-08-16 15:34:10 -04003607 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003608 reg = FDI_TX_CTL(pipe);
3609 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003610 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3611 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003612 I915_WRITE(reg, temp);
3613
3614 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003615 udelay(500);
3616
Sean Paulfa37d392012-03-02 12:53:39 -05003617 for (retry = 0; retry < 5; retry++) {
3618 reg = FDI_RX_IIR(pipe);
3619 temp = I915_READ(reg);
3620 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3621 if (temp & FDI_RX_SYMBOL_LOCK) {
3622 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3623 DRM_DEBUG_KMS("FDI train 2 done.\n");
3624 break;
3625 }
3626 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003627 }
Sean Paulfa37d392012-03-02 12:53:39 -05003628 if (retry < 5)
3629 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003630 }
3631 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003632 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003633
3634 DRM_DEBUG_KMS("FDI train done.\n");
3635}
3636
Jesse Barnes357555c2011-04-28 15:09:55 -07003637/* Manual link training for Ivy Bridge A0 parts */
3638static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3639{
3640 struct drm_device *dev = crtc->dev;
3641 struct drm_i915_private *dev_priv = dev->dev_private;
3642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3643 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003644 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003645
3646 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3647 for train result */
3648 reg = FDI_RX_IMR(pipe);
3649 temp = I915_READ(reg);
3650 temp &= ~FDI_RX_SYMBOL_LOCK;
3651 temp &= ~FDI_RX_BIT_LOCK;
3652 I915_WRITE(reg, temp);
3653
3654 POSTING_READ(reg);
3655 udelay(150);
3656
Daniel Vetter01a415f2012-10-27 15:58:40 +02003657 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3658 I915_READ(FDI_RX_IIR(pipe)));
3659
Jesse Barnes139ccd32013-08-19 11:04:55 -07003660 /* Try each vswing and preemphasis setting twice before moving on */
3661 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3662 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003663 reg = FDI_TX_CTL(pipe);
3664 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003665 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3666 temp &= ~FDI_TX_ENABLE;
3667 I915_WRITE(reg, temp);
3668
3669 reg = FDI_RX_CTL(pipe);
3670 temp = I915_READ(reg);
3671 temp &= ~FDI_LINK_TRAIN_AUTO;
3672 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3673 temp &= ~FDI_RX_ENABLE;
3674 I915_WRITE(reg, temp);
3675
3676 /* enable CPU FDI TX and PCH FDI RX */
3677 reg = FDI_TX_CTL(pipe);
3678 temp = I915_READ(reg);
3679 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003680 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003681 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003682 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003683 temp |= snb_b_fdi_train_param[j/2];
3684 temp |= FDI_COMPOSITE_SYNC;
3685 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3686
3687 I915_WRITE(FDI_RX_MISC(pipe),
3688 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3689
3690 reg = FDI_RX_CTL(pipe);
3691 temp = I915_READ(reg);
3692 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3693 temp |= FDI_COMPOSITE_SYNC;
3694 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3695
3696 POSTING_READ(reg);
3697 udelay(1); /* should be 0.5us */
3698
3699 for (i = 0; i < 4; i++) {
3700 reg = FDI_RX_IIR(pipe);
3701 temp = I915_READ(reg);
3702 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3703
3704 if (temp & FDI_RX_BIT_LOCK ||
3705 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3706 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3707 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3708 i);
3709 break;
3710 }
3711 udelay(1); /* should be 0.5us */
3712 }
3713 if (i == 4) {
3714 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3715 continue;
3716 }
3717
3718 /* Train 2 */
3719 reg = FDI_TX_CTL(pipe);
3720 temp = I915_READ(reg);
3721 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3722 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3723 I915_WRITE(reg, temp);
3724
3725 reg = FDI_RX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3728 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003729 I915_WRITE(reg, temp);
3730
3731 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003732 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003733
Jesse Barnes139ccd32013-08-19 11:04:55 -07003734 for (i = 0; i < 4; i++) {
3735 reg = FDI_RX_IIR(pipe);
3736 temp = I915_READ(reg);
3737 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003738
Jesse Barnes139ccd32013-08-19 11:04:55 -07003739 if (temp & FDI_RX_SYMBOL_LOCK ||
3740 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3741 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3742 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3743 i);
3744 goto train_done;
3745 }
3746 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003747 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003748 if (i == 4)
3749 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003750 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003751
Jesse Barnes139ccd32013-08-19 11:04:55 -07003752train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003753 DRM_DEBUG_KMS("FDI train done.\n");
3754}
3755
Daniel Vetter88cefb62012-08-12 19:27:14 +02003756static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003757{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003758 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003759 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003760 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003761 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003762
Jesse Barnesc64e3112010-09-10 11:27:03 -07003763
Jesse Barnes0e23b992010-09-10 11:10:00 -07003764 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003765 reg = FDI_RX_CTL(pipe);
3766 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003767 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003768 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003769 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003770 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3771
3772 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003773 udelay(200);
3774
3775 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003776 temp = I915_READ(reg);
3777 I915_WRITE(reg, temp | FDI_PCDCLK);
3778
3779 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003780 udelay(200);
3781
Paulo Zanoni20749732012-11-23 15:30:38 -02003782 /* Enable CPU FDI TX PLL, always on for Ironlake */
3783 reg = FDI_TX_CTL(pipe);
3784 temp = I915_READ(reg);
3785 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3786 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003787
Paulo Zanoni20749732012-11-23 15:30:38 -02003788 POSTING_READ(reg);
3789 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003790 }
3791}
3792
Daniel Vetter88cefb62012-08-12 19:27:14 +02003793static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3794{
3795 struct drm_device *dev = intel_crtc->base.dev;
3796 struct drm_i915_private *dev_priv = dev->dev_private;
3797 int pipe = intel_crtc->pipe;
3798 u32 reg, temp;
3799
3800 /* Switch from PCDclk to Rawclk */
3801 reg = FDI_RX_CTL(pipe);
3802 temp = I915_READ(reg);
3803 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3804
3805 /* Disable CPU FDI TX PLL */
3806 reg = FDI_TX_CTL(pipe);
3807 temp = I915_READ(reg);
3808 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3809
3810 POSTING_READ(reg);
3811 udelay(100);
3812
3813 reg = FDI_RX_CTL(pipe);
3814 temp = I915_READ(reg);
3815 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3816
3817 /* Wait for the clocks to turn off. */
3818 POSTING_READ(reg);
3819 udelay(100);
3820}
3821
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003822static void ironlake_fdi_disable(struct drm_crtc *crtc)
3823{
3824 struct drm_device *dev = crtc->dev;
3825 struct drm_i915_private *dev_priv = dev->dev_private;
3826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3827 int pipe = intel_crtc->pipe;
3828 u32 reg, temp;
3829
3830 /* disable CPU FDI tx and PCH FDI rx */
3831 reg = FDI_TX_CTL(pipe);
3832 temp = I915_READ(reg);
3833 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3834 POSTING_READ(reg);
3835
3836 reg = FDI_RX_CTL(pipe);
3837 temp = I915_READ(reg);
3838 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003839 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003840 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3841
3842 POSTING_READ(reg);
3843 udelay(100);
3844
3845 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003846 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003847 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003848
3849 /* still set train pattern 1 */
3850 reg = FDI_TX_CTL(pipe);
3851 temp = I915_READ(reg);
3852 temp &= ~FDI_LINK_TRAIN_NONE;
3853 temp |= FDI_LINK_TRAIN_PATTERN_1;
3854 I915_WRITE(reg, temp);
3855
3856 reg = FDI_RX_CTL(pipe);
3857 temp = I915_READ(reg);
3858 if (HAS_PCH_CPT(dev)) {
3859 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3860 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3861 } else {
3862 temp &= ~FDI_LINK_TRAIN_NONE;
3863 temp |= FDI_LINK_TRAIN_PATTERN_1;
3864 }
3865 /* BPC in FDI rx is consistent with that in PIPECONF */
3866 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003867 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003868 I915_WRITE(reg, temp);
3869
3870 POSTING_READ(reg);
3871 udelay(100);
3872}
3873
Chris Wilson5dce5b932014-01-20 10:17:36 +00003874bool intel_has_pending_fb_unpin(struct drm_device *dev)
3875{
3876 struct intel_crtc *crtc;
3877
3878 /* Note that we don't need to be called with mode_config.lock here
3879 * as our list of CRTC objects is static for the lifetime of the
3880 * device and so cannot disappear as we iterate. Similarly, we can
3881 * happily treat the predicates as racy, atomic checks as userspace
3882 * cannot claim and pin a new fb without at least acquring the
3883 * struct_mutex and so serialising with us.
3884 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003885 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003886 if (atomic_read(&crtc->unpin_work_count) == 0)
3887 continue;
3888
3889 if (crtc->unpin_work)
3890 intel_wait_for_vblank(dev, crtc->pipe);
3891
3892 return true;
3893 }
3894
3895 return false;
3896}
3897
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003898static void page_flip_completed(struct intel_crtc *intel_crtc)
3899{
3900 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3901 struct intel_unpin_work *work = intel_crtc->unpin_work;
3902
3903 /* ensure that the unpin work is consistent wrt ->pending. */
3904 smp_rmb();
3905 intel_crtc->unpin_work = NULL;
3906
3907 if (work->event)
3908 drm_send_vblank_event(intel_crtc->base.dev,
3909 intel_crtc->pipe,
3910 work->event);
3911
3912 drm_crtc_vblank_put(&intel_crtc->base);
3913
3914 wake_up_all(&dev_priv->pending_flip_queue);
3915 queue_work(dev_priv->wq, &work->work);
3916
3917 trace_i915_flip_complete(intel_crtc->plane,
3918 work->pending_flip_obj);
3919}
3920
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003921void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003922{
Chris Wilson0f911282012-04-17 10:05:38 +01003923 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003924 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003925
Daniel Vetter2c10d572012-12-20 21:24:07 +01003926 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003927 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3928 !intel_crtc_has_pending_flip(crtc),
3929 60*HZ) == 0)) {
3930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003931
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003932 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003933 if (intel_crtc->unpin_work) {
3934 WARN_ONCE(1, "Removing stuck page flip\n");
3935 page_flip_completed(intel_crtc);
3936 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003937 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003938 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003939
Chris Wilson975d5682014-08-20 13:13:34 +01003940 if (crtc->primary->fb) {
3941 mutex_lock(&dev->struct_mutex);
3942 intel_finish_fb(crtc->primary->fb);
3943 mutex_unlock(&dev->struct_mutex);
3944 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003945}
3946
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947/* Program iCLKIP clock to the desired frequency */
3948static void lpt_program_iclkip(struct drm_crtc *crtc)
3949{
3950 struct drm_device *dev = crtc->dev;
3951 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003952 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003953 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3954 u32 temp;
3955
Daniel Vetter09153002012-12-12 14:06:44 +01003956 mutex_lock(&dev_priv->dpio_lock);
3957
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003958 /* It is necessary to ungate the pixclk gate prior to programming
3959 * the divisors, and gate it back when it is done.
3960 */
3961 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3962
3963 /* Disable SSCCTL */
3964 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003965 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3966 SBI_SSCCTL_DISABLE,
3967 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003968
3969 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003970 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003971 auxdiv = 1;
3972 divsel = 0x41;
3973 phaseinc = 0x20;
3974 } else {
3975 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003976 * but the adjusted_mode->crtc_clock in in KHz. To get the
3977 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003978 * convert the virtual clock precision to KHz here for higher
3979 * precision.
3980 */
3981 u32 iclk_virtual_root_freq = 172800 * 1000;
3982 u32 iclk_pi_range = 64;
3983 u32 desired_divisor, msb_divisor_value, pi_value;
3984
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003985 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003986 msb_divisor_value = desired_divisor / iclk_pi_range;
3987 pi_value = desired_divisor % iclk_pi_range;
3988
3989 auxdiv = 0;
3990 divsel = msb_divisor_value - 2;
3991 phaseinc = pi_value;
3992 }
3993
3994 /* This should not happen with any sane values */
3995 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3996 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3997 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3998 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3999
4000 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 +03004001 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004002 auxdiv,
4003 divsel,
4004 phasedir,
4005 phaseinc);
4006
4007 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004008 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004009 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4010 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4011 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4012 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4013 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4014 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004015 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004016
4017 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004018 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004019 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4020 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004021 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004022
4023 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004024 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004025 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004026 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004027
4028 /* Wait for initialization time */
4029 udelay(24);
4030
4031 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004032
4033 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004034}
4035
Daniel Vetter275f01b22013-05-03 11:49:47 +02004036static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4037 enum pipe pch_transcoder)
4038{
4039 struct drm_device *dev = crtc->base.dev;
4040 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004041 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004042
4043 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4044 I915_READ(HTOTAL(cpu_transcoder)));
4045 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4046 I915_READ(HBLANK(cpu_transcoder)));
4047 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4048 I915_READ(HSYNC(cpu_transcoder)));
4049
4050 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4051 I915_READ(VTOTAL(cpu_transcoder)));
4052 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4053 I915_READ(VBLANK(cpu_transcoder)));
4054 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4055 I915_READ(VSYNC(cpu_transcoder)));
4056 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4057 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4058}
4059
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004060static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004061{
4062 struct drm_i915_private *dev_priv = dev->dev_private;
4063 uint32_t temp;
4064
4065 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004066 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004067 return;
4068
4069 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4070 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4071
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004072 temp &= ~FDI_BC_BIFURCATION_SELECT;
4073 if (enable)
4074 temp |= FDI_BC_BIFURCATION_SELECT;
4075
4076 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004077 I915_WRITE(SOUTH_CHICKEN1, temp);
4078 POSTING_READ(SOUTH_CHICKEN1);
4079}
4080
4081static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4082{
4083 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004084
4085 switch (intel_crtc->pipe) {
4086 case PIPE_A:
4087 break;
4088 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004089 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004090 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004091 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004092 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004093
4094 break;
4095 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004096 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004097
4098 break;
4099 default:
4100 BUG();
4101 }
4102}
4103
Jesse Barnesf67a5592011-01-05 10:31:48 -08004104/*
4105 * Enable PCH resources required for PCH ports:
4106 * - PCH PLLs
4107 * - FDI training & RX/TX
4108 * - update transcoder timings
4109 * - DP transcoding bits
4110 * - transcoder
4111 */
4112static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004113{
4114 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004115 struct drm_i915_private *dev_priv = dev->dev_private;
4116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4117 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004118 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004119
Daniel Vetterab9412b2013-05-03 11:49:46 +02004120 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004121
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004122 if (IS_IVYBRIDGE(dev))
4123 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4124
Daniel Vettercd986ab2012-10-26 10:58:12 +02004125 /* Write the TU size bits before fdi link training, so that error
4126 * detection works. */
4127 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4128 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4129
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004130 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004131 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004132
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004133 /* We need to program the right clock selection before writing the pixel
4134 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004135 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004136 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004137
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004138 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004139 temp |= TRANS_DPLL_ENABLE(pipe);
4140 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004141 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004142 temp |= sel;
4143 else
4144 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004145 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004147
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004148 /* XXX: pch pll's can be enabled any time before we enable the PCH
4149 * transcoder, and we actually should do this to not upset any PCH
4150 * transcoder that already use the clock when we share it.
4151 *
4152 * Note that enable_shared_dpll tries to do the right thing, but
4153 * get_shared_dpll unconditionally resets the pll - we need that to have
4154 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004155 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004156
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004157 /* set transcoder timing, panel must allow it */
4158 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004159 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004161 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004162
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004163 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004164 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004165 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004166 reg = TRANS_DP_CTL(pipe);
4167 temp = I915_READ(reg);
4168 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004169 TRANS_DP_SYNC_MASK |
4170 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004171 temp |= (TRANS_DP_OUTPUT_ENABLE |
4172 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004173 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004174
4175 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004176 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004177 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004178 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004179
4180 switch (intel_trans_dp_port_sel(crtc)) {
4181 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004182 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004183 break;
4184 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004185 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004186 break;
4187 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004188 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004189 break;
4190 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004191 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004192 }
4193
Chris Wilson5eddb702010-09-11 13:48:45 +01004194 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004195 }
4196
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004197 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004198}
4199
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004200static void lpt_pch_enable(struct drm_crtc *crtc)
4201{
4202 struct drm_device *dev = crtc->dev;
4203 struct drm_i915_private *dev_priv = dev->dev_private;
4204 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004205 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004206
Daniel Vetterab9412b2013-05-03 11:49:46 +02004207 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004208
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004209 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004210
Paulo Zanoni0540e482012-10-31 18:12:40 -02004211 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004212 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004213
Paulo Zanoni937bb612012-10-31 18:12:47 -02004214 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004215}
4216
Daniel Vetter716c2e52014-06-25 22:02:02 +03004217void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004218{
Daniel Vettere2b78262013-06-07 23:10:03 +02004219 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004220
4221 if (pll == NULL)
4222 return;
4223
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004224 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004225 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004226 return;
4227 }
4228
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004229 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4230 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004231 WARN_ON(pll->on);
4232 WARN_ON(pll->active);
4233 }
4234
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004235 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004236}
4237
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004238struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4239 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004240{
Daniel Vettere2b78262013-06-07 23:10:03 +02004241 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004242 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004243 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004244
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004245 if (HAS_PCH_IBX(dev_priv->dev)) {
4246 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004247 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004248 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004249
Daniel Vetter46edb022013-06-05 13:34:12 +02004250 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4251 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004252
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004253 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004254
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004255 goto found;
4256 }
4257
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304258 if (IS_BROXTON(dev_priv->dev)) {
4259 /* PLL is attached to port in bxt */
4260 struct intel_encoder *encoder;
4261 struct intel_digital_port *intel_dig_port;
4262
4263 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4264 if (WARN_ON(!encoder))
4265 return NULL;
4266
4267 intel_dig_port = enc_to_dig_port(&encoder->base);
4268 /* 1:1 mapping between ports and PLLs */
4269 i = (enum intel_dpll_id)intel_dig_port->port;
4270 pll = &dev_priv->shared_dplls[i];
4271 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4272 crtc->base.base.id, pll->name);
4273 WARN_ON(pll->new_config->crtc_mask);
4274
4275 goto found;
4276 }
4277
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004278 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4279 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004280
4281 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004282 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004283 continue;
4284
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004285 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004286 &pll->new_config->hw_state,
4287 sizeof(pll->new_config->hw_state)) == 0) {
4288 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004289 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004290 pll->new_config->crtc_mask,
4291 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004292 goto found;
4293 }
4294 }
4295
4296 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004297 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4298 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004299 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004300 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4301 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004302 goto found;
4303 }
4304 }
4305
4306 return NULL;
4307
4308found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004309 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004310 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004311
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004312 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004313 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4314 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004315
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004316 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004317
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004318 return pll;
4319}
4320
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004321/**
4322 * intel_shared_dpll_start_config - start a new PLL staged config
4323 * @dev_priv: DRM device
4324 * @clear_pipes: mask of pipes that will have their PLLs freed
4325 *
4326 * Starts a new PLL staged config, copying the current config but
4327 * releasing the references of pipes specified in clear_pipes.
4328 */
4329static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4330 unsigned clear_pipes)
4331{
4332 struct intel_shared_dpll *pll;
4333 enum intel_dpll_id i;
4334
4335 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4336 pll = &dev_priv->shared_dplls[i];
4337
4338 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4339 GFP_KERNEL);
4340 if (!pll->new_config)
4341 goto cleanup;
4342
4343 pll->new_config->crtc_mask &= ~clear_pipes;
4344 }
4345
4346 return 0;
4347
4348cleanup:
4349 while (--i >= 0) {
4350 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004351 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004352 pll->new_config = NULL;
4353 }
4354
4355 return -ENOMEM;
4356}
4357
4358static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4359{
4360 struct intel_shared_dpll *pll;
4361 enum intel_dpll_id i;
4362
4363 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4364 pll = &dev_priv->shared_dplls[i];
4365
4366 WARN_ON(pll->new_config == &pll->config);
4367
4368 pll->config = *pll->new_config;
4369 kfree(pll->new_config);
4370 pll->new_config = NULL;
4371 }
4372}
4373
4374static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4375{
4376 struct intel_shared_dpll *pll;
4377 enum intel_dpll_id i;
4378
4379 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4380 pll = &dev_priv->shared_dplls[i];
4381
4382 WARN_ON(pll->new_config == &pll->config);
4383
4384 kfree(pll->new_config);
4385 pll->new_config = NULL;
4386 }
4387}
4388
Daniel Vettera1520312013-05-03 11:49:50 +02004389static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004390{
4391 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004392 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004393 u32 temp;
4394
4395 temp = I915_READ(dslreg);
4396 udelay(500);
4397 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004398 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004399 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004400 }
4401}
4402
Chandra Kondurua1b22782015-04-07 15:28:45 -07004403/**
4404 * skl_update_scaler_users - Stages update to crtc's scaler state
4405 * @intel_crtc: crtc
4406 * @crtc_state: crtc_state
4407 * @plane: plane (NULL indicates crtc is requesting update)
4408 * @plane_state: plane's state
4409 * @force_detach: request unconditional detachment of scaler
4410 *
4411 * This function updates scaler state for requested plane or crtc.
4412 * To request scaler usage update for a plane, caller shall pass plane pointer.
4413 * To request scaler usage update for crtc, caller shall pass plane pointer
4414 * as NULL.
4415 *
4416 * Return
4417 * 0 - scaler_usage updated successfully
4418 * error - requested scaling cannot be supported or other error condition
4419 */
4420int
4421skl_update_scaler_users(
4422 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4423 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4424 int force_detach)
4425{
4426 int need_scaling;
4427 int idx;
4428 int src_w, src_h, dst_w, dst_h;
4429 int *scaler_id;
4430 struct drm_framebuffer *fb;
4431 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004432 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004433
4434 if (!intel_crtc || !crtc_state)
4435 return 0;
4436
4437 scaler_state = &crtc_state->scaler_state;
4438
4439 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4440 fb = intel_plane ? plane_state->base.fb : NULL;
4441
4442 if (intel_plane) {
4443 src_w = drm_rect_width(&plane_state->src) >> 16;
4444 src_h = drm_rect_height(&plane_state->src) >> 16;
4445 dst_w = drm_rect_width(&plane_state->dst);
4446 dst_h = drm_rect_height(&plane_state->dst);
4447 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004448 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004449 } else {
4450 struct drm_display_mode *adjusted_mode =
4451 &crtc_state->base.adjusted_mode;
4452 src_w = crtc_state->pipe_src_w;
4453 src_h = crtc_state->pipe_src_h;
4454 dst_w = adjusted_mode->hdisplay;
4455 dst_h = adjusted_mode->vdisplay;
4456 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004457 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004458 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004459
4460 need_scaling = intel_rotation_90_or_270(rotation) ?
4461 (src_h != dst_w || src_w != dst_h):
4462 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004463
4464 /*
4465 * if plane is being disabled or scaler is no more required or force detach
4466 * - free scaler binded to this plane/crtc
4467 * - in order to do this, update crtc->scaler_usage
4468 *
4469 * Here scaler state in crtc_state is set free so that
4470 * scaler can be assigned to other user. Actual register
4471 * update to free the scaler is done in plane/panel-fit programming.
4472 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4473 */
4474 if (force_detach || !need_scaling || (intel_plane &&
4475 (!fb || !plane_state->visible))) {
4476 if (*scaler_id >= 0) {
4477 scaler_state->scaler_users &= ~(1 << idx);
4478 scaler_state->scalers[*scaler_id].in_use = 0;
4479
4480 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4481 "crtc_state = %p scaler_users = 0x%x\n",
4482 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4483 intel_plane ? intel_plane->base.base.id :
4484 intel_crtc->base.base.id, crtc_state,
4485 scaler_state->scaler_users);
4486 *scaler_id = -1;
4487 }
4488 return 0;
4489 }
4490
4491 /* range checks */
4492 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4493 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4494
4495 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4496 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4497 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4498 "size is out of scaler range\n",
4499 intel_plane ? "PLANE" : "CRTC",
4500 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4501 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4502 return -EINVAL;
4503 }
4504
4505 /* check colorkey */
4506 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4507 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4508 intel_plane->base.base.id);
4509 return -EINVAL;
4510 }
4511
4512 /* Check src format */
4513 if (intel_plane) {
4514 switch (fb->pixel_format) {
4515 case DRM_FORMAT_RGB565:
4516 case DRM_FORMAT_XBGR8888:
4517 case DRM_FORMAT_XRGB8888:
4518 case DRM_FORMAT_ABGR8888:
4519 case DRM_FORMAT_ARGB8888:
4520 case DRM_FORMAT_XRGB2101010:
4521 case DRM_FORMAT_ARGB2101010:
4522 case DRM_FORMAT_XBGR2101010:
4523 case DRM_FORMAT_ABGR2101010:
4524 case DRM_FORMAT_YUYV:
4525 case DRM_FORMAT_YVYU:
4526 case DRM_FORMAT_UYVY:
4527 case DRM_FORMAT_VYUY:
4528 break;
4529 default:
4530 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4531 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4532 return -EINVAL;
4533 }
4534 }
4535
4536 /* mark this plane as a scaler user in crtc_state */
4537 scaler_state->scaler_users |= (1 << idx);
4538 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4539 "crtc_state = %p scaler_users = 0x%x\n",
4540 intel_plane ? "PLANE" : "CRTC",
4541 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4542 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4543 return 0;
4544}
4545
4546static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004547{
4548 struct drm_device *dev = crtc->base.dev;
4549 struct drm_i915_private *dev_priv = dev->dev_private;
4550 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004551 struct intel_crtc_scaler_state *scaler_state =
4552 &crtc->config->scaler_state;
4553
4554 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4555
4556 /* To update pfit, first update scaler state */
4557 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4558 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4559 skl_detach_scalers(crtc);
4560 if (!enable)
4561 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004562
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004563 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004564 int id;
4565
4566 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4567 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4568 return;
4569 }
4570
4571 id = scaler_state->scaler_id;
4572 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4573 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4574 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4575 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4576
4577 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004578 }
4579}
4580
Jesse Barnesb074cec2013-04-25 12:55:02 -07004581static void ironlake_pfit_enable(struct intel_crtc *crtc)
4582{
4583 struct drm_device *dev = crtc->base.dev;
4584 struct drm_i915_private *dev_priv = dev->dev_private;
4585 int pipe = crtc->pipe;
4586
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004587 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004588 /* Force use of hard-coded filter coefficients
4589 * as some pre-programmed values are broken,
4590 * e.g. x201.
4591 */
4592 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4593 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4594 PF_PIPE_SEL_IVB(pipe));
4595 else
4596 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004597 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4598 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004599 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004600}
4601
Matt Roper4a3b8762014-12-23 10:41:51 -08004602static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004603{
4604 struct drm_device *dev = crtc->dev;
4605 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004606 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004607 struct intel_plane *intel_plane;
4608
Matt Roperaf2b6532014-04-01 15:22:32 -07004609 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4610 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004611 if (intel_plane->pipe == pipe)
4612 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004613 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004614}
4615
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004616void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004617{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004618 struct drm_device *dev = crtc->base.dev;
4619 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004620
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004621 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004622 return;
4623
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004624 /* We can only enable IPS after we enable a plane and wait for a vblank */
4625 intel_wait_for_vblank(dev, crtc->pipe);
4626
Paulo Zanonid77e4532013-09-24 13:52:55 -03004627 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004628 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004629 mutex_lock(&dev_priv->rps.hw_lock);
4630 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4631 mutex_unlock(&dev_priv->rps.hw_lock);
4632 /* Quoting Art Runyan: "its not safe to expect any particular
4633 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004634 * mailbox." Moreover, the mailbox may return a bogus state,
4635 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004636 */
4637 } else {
4638 I915_WRITE(IPS_CTL, IPS_ENABLE);
4639 /* The bit only becomes 1 in the next vblank, so this wait here
4640 * is essentially intel_wait_for_vblank. If we don't have this
4641 * and don't wait for vblanks until the end of crtc_enable, then
4642 * the HW state readout code will complain that the expected
4643 * IPS_CTL value is not the one we read. */
4644 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4645 DRM_ERROR("Timed out waiting for IPS enable\n");
4646 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004647}
4648
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004649void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004650{
4651 struct drm_device *dev = crtc->base.dev;
4652 struct drm_i915_private *dev_priv = dev->dev_private;
4653
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004654 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004655 return;
4656
4657 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004658 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004659 mutex_lock(&dev_priv->rps.hw_lock);
4660 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4661 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004662 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4663 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4664 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004665 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004666 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004667 POSTING_READ(IPS_CTL);
4668 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004669
4670 /* We need to wait for a vblank before we can disable the plane. */
4671 intel_wait_for_vblank(dev, crtc->pipe);
4672}
4673
4674/** Loads the palette/gamma unit for the CRTC with the prepared values */
4675static void intel_crtc_load_lut(struct drm_crtc *crtc)
4676{
4677 struct drm_device *dev = crtc->dev;
4678 struct drm_i915_private *dev_priv = dev->dev_private;
4679 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4680 enum pipe pipe = intel_crtc->pipe;
4681 int palreg = PALETTE(pipe);
4682 int i;
4683 bool reenable_ips = false;
4684
4685 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004686 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004687 return;
4688
Imre Deak50360402015-01-16 00:55:16 -08004689 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004690 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004691 assert_dsi_pll_enabled(dev_priv);
4692 else
4693 assert_pll_enabled(dev_priv, pipe);
4694 }
4695
4696 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304697 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004698 palreg = LGC_PALETTE(pipe);
4699
4700 /* Workaround : Do not read or write the pipe palette/gamma data while
4701 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4702 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004703 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004704 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4705 GAMMA_MODE_MODE_SPLIT)) {
4706 hsw_disable_ips(intel_crtc);
4707 reenable_ips = true;
4708 }
4709
4710 for (i = 0; i < 256; i++) {
4711 I915_WRITE(palreg + 4 * i,
4712 (intel_crtc->lut_r[i] << 16) |
4713 (intel_crtc->lut_g[i] << 8) |
4714 intel_crtc->lut_b[i]);
4715 }
4716
4717 if (reenable_ips)
4718 hsw_enable_ips(intel_crtc);
4719}
4720
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004721static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004722{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004723 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004724 struct drm_device *dev = intel_crtc->base.dev;
4725 struct drm_i915_private *dev_priv = dev->dev_private;
4726
4727 mutex_lock(&dev->struct_mutex);
4728 dev_priv->mm.interruptible = false;
4729 (void) intel_overlay_switch_off(intel_crtc->overlay);
4730 dev_priv->mm.interruptible = true;
4731 mutex_unlock(&dev->struct_mutex);
4732 }
4733
4734 /* Let userspace switch the overlay on again. In most cases userspace
4735 * has to recompute where to put it anyway.
4736 */
4737}
4738
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004739/**
4740 * intel_post_enable_primary - Perform operations after enabling primary plane
4741 * @crtc: the CRTC whose primary plane was just enabled
4742 *
4743 * Performs potentially sleeping operations that must be done after the primary
4744 * plane is enabled, such as updating FBC and IPS. Note that this may be
4745 * called due to an explicit primary plane update, or due to an implicit
4746 * re-enable that is caused when a sprite plane is updated to no longer
4747 * completely hide the primary plane.
4748 */
4749static void
4750intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004751{
4752 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004753 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4755 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004756
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004757 /*
4758 * BDW signals flip done immediately if the plane
4759 * is disabled, even if the plane enable is already
4760 * armed to occur at the next vblank :(
4761 */
4762 if (IS_BROADWELL(dev))
4763 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004764
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004765 /*
4766 * FIXME IPS should be fine as long as one plane is
4767 * enabled, but in practice it seems to have problems
4768 * when going from primary only to sprite only and vice
4769 * versa.
4770 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004771 hsw_enable_ips(intel_crtc);
4772
4773 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004774 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004775 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004776
4777 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004778 * Gen2 reports pipe underruns whenever all planes are disabled.
4779 * So don't enable underrun reporting before at least some planes
4780 * are enabled.
4781 * FIXME: Need to fix the logic to work when we turn off all planes
4782 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004783 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004784 if (IS_GEN2(dev))
4785 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4786
4787 /* Underruns don't raise interrupts, so check manually. */
4788 if (HAS_GMCH_DISPLAY(dev))
4789 i9xx_check_fifo_underruns(dev_priv);
4790}
4791
4792/**
4793 * intel_pre_disable_primary - Perform operations before disabling primary plane
4794 * @crtc: the CRTC whose primary plane is to be disabled
4795 *
4796 * Performs potentially sleeping operations that must be done before the
4797 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4798 * be called due to an explicit primary plane update, or due to an implicit
4799 * disable that is caused when a sprite plane completely hides the primary
4800 * plane.
4801 */
4802static void
4803intel_pre_disable_primary(struct drm_crtc *crtc)
4804{
4805 struct drm_device *dev = crtc->dev;
4806 struct drm_i915_private *dev_priv = dev->dev_private;
4807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4808 int pipe = intel_crtc->pipe;
4809
4810 /*
4811 * Gen2 reports pipe underruns whenever all planes are disabled.
4812 * So diasble underrun reporting before all the planes get disabled.
4813 * FIXME: Need to fix the logic to work when we turn off all planes
4814 * but leave the pipe running.
4815 */
4816 if (IS_GEN2(dev))
4817 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4818
4819 /*
4820 * Vblank time updates from the shadow to live plane control register
4821 * are blocked if the memory self-refresh mode is active at that
4822 * moment. So to make sure the plane gets truly disabled, disable
4823 * first the self-refresh mode. The self-refresh enable bit in turn
4824 * will be checked/applied by the HW only at the next frame start
4825 * event which is after the vblank start event, so we need to have a
4826 * wait-for-vblank between disabling the plane and the pipe.
4827 */
4828 if (HAS_GMCH_DISPLAY(dev))
4829 intel_set_memory_cxsr(dev_priv, false);
4830
4831 mutex_lock(&dev->struct_mutex);
4832 if (dev_priv->fbc.crtc == intel_crtc)
4833 intel_fbc_disable(dev);
4834 mutex_unlock(&dev->struct_mutex);
4835
4836 /*
4837 * FIXME IPS should be fine as long as one plane is
4838 * enabled, but in practice it seems to have problems
4839 * when going from primary only to sprite only and vice
4840 * versa.
4841 */
4842 hsw_disable_ips(intel_crtc);
4843}
4844
4845static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4846{
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004847 intel_enable_primary_hw_plane(crtc->primary, crtc);
4848 intel_enable_sprite_planes(crtc);
4849 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004850
4851 intel_post_enable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004852}
4853
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004854static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004855{
4856 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004858 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004859 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004860
4861 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004862
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004863 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004864
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004865 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004866 for_each_intel_plane(dev, intel_plane) {
4867 if (intel_plane->pipe == pipe) {
4868 struct drm_crtc *from = intel_plane->base.crtc;
4869
4870 intel_plane->disable_plane(&intel_plane->base,
4871 from ?: crtc, true);
4872 }
4873 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004874
Daniel Vetterf99d7062014-06-19 16:01:59 +02004875 /*
4876 * FIXME: Once we grow proper nuclear flip support out of this we need
4877 * to compute the mask of flip planes precisely. For the time being
4878 * consider this a flip to a NULL plane.
4879 */
4880 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004881}
4882
Jesse Barnesf67a5592011-01-05 10:31:48 -08004883static void ironlake_crtc_enable(struct drm_crtc *crtc)
4884{
4885 struct drm_device *dev = crtc->dev;
4886 struct drm_i915_private *dev_priv = dev->dev_private;
4887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004888 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004889 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004890
Matt Roper83d65732015-02-25 13:12:16 -08004891 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004892
Jesse Barnesf67a5592011-01-05 10:31:48 -08004893 if (intel_crtc->active)
4894 return;
4895
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004896 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004897 intel_prepare_shared_dpll(intel_crtc);
4898
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004899 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304900 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004901
4902 intel_set_pipe_timings(intel_crtc);
4903
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004904 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004905 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004906 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004907 }
4908
4909 ironlake_set_pipeconf(crtc);
4910
Jesse Barnesf67a5592011-01-05 10:31:48 -08004911 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004912
Daniel Vettera72e4c92014-09-30 10:56:47 +02004913 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4914 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004915
Daniel Vetterf6736a12013-06-05 13:34:30 +02004916 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004917 if (encoder->pre_enable)
4918 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004919
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004920 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004921 /* Note: FDI PLL enabling _must_ be done before we enable the
4922 * cpu pipes, hence this is separate from all the other fdi/pch
4923 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004924 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004925 } else {
4926 assert_fdi_tx_disabled(dev_priv, pipe);
4927 assert_fdi_rx_disabled(dev_priv, pipe);
4928 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004929
Jesse Barnesb074cec2013-04-25 12:55:02 -07004930 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004931
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004932 /*
4933 * On ILK+ LUT must be loaded before the pipe is running but with
4934 * clocks enabled
4935 */
4936 intel_crtc_load_lut(crtc);
4937
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004938 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004939 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004940
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004941 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004942 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004943
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004944 assert_vblank_disabled(crtc);
4945 drm_crtc_vblank_on(crtc);
4946
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004947 for_each_encoder_on_crtc(dev, crtc, encoder)
4948 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004949
4950 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004951 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004952}
4953
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004954/* IPS only exists on ULT machines and is tied to pipe A. */
4955static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4956{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004957 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004958}
4959
Paulo Zanonie4916942013-09-20 16:21:19 -03004960/*
4961 * This implements the workaround described in the "notes" section of the mode
4962 * set sequence documentation. When going from no pipes or single pipe to
4963 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4964 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4965 */
4966static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4967{
4968 struct drm_device *dev = crtc->base.dev;
4969 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4970
4971 /* We want to get the other_active_crtc only if there's only 1 other
4972 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004973 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004974 if (!crtc_it->active || crtc_it == crtc)
4975 continue;
4976
4977 if (other_active_crtc)
4978 return;
4979
4980 other_active_crtc = crtc_it;
4981 }
4982 if (!other_active_crtc)
4983 return;
4984
4985 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4986 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4987}
4988
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004989static void haswell_crtc_enable(struct drm_crtc *crtc)
4990{
4991 struct drm_device *dev = crtc->dev;
4992 struct drm_i915_private *dev_priv = dev->dev_private;
4993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4994 struct intel_encoder *encoder;
4995 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004996
Matt Roper83d65732015-02-25 13:12:16 -08004997 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004998
4999 if (intel_crtc->active)
5000 return;
5001
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005002 if (intel_crtc_to_shared_dpll(intel_crtc))
5003 intel_enable_shared_dpll(intel_crtc);
5004
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005005 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305006 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005007
5008 intel_set_pipe_timings(intel_crtc);
5009
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005010 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5011 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5012 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005013 }
5014
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005015 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005016 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005017 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005018 }
5019
5020 haswell_set_pipeconf(crtc);
5021
5022 intel_set_pipe_csc(crtc);
5023
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005024 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005025
Daniel Vettera72e4c92014-09-30 10:56:47 +02005026 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005027 for_each_encoder_on_crtc(dev, crtc, encoder)
5028 if (encoder->pre_enable)
5029 encoder->pre_enable(encoder);
5030
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005031 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005032 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5033 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005034 dev_priv->display.fdi_link_train(crtc);
5035 }
5036
Paulo Zanoni1f544382012-10-24 11:32:00 -02005037 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005038
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005039 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005040 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005041 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005042 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005043 else
5044 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005045
5046 /*
5047 * On ILK+ LUT must be loaded before the pipe is running but with
5048 * clocks enabled
5049 */
5050 intel_crtc_load_lut(crtc);
5051
Paulo Zanoni1f544382012-10-24 11:32:00 -02005052 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005053 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005054
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005055 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005056 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005057
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005058 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005059 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005060
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005061 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005062 intel_ddi_set_vc_payload_alloc(crtc, true);
5063
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005064 assert_vblank_disabled(crtc);
5065 drm_crtc_vblank_on(crtc);
5066
Jani Nikula8807e552013-08-30 19:40:32 +03005067 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005068 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005069 intel_opregion_notify_encoder(encoder, true);
5070 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005071
Paulo Zanonie4916942013-09-20 16:21:19 -03005072 /* If we change the relative order between pipe/planes enabling, we need
5073 * to change the workaround. */
5074 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005075}
5076
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005077static void ironlake_pfit_disable(struct intel_crtc *crtc)
5078{
5079 struct drm_device *dev = crtc->base.dev;
5080 struct drm_i915_private *dev_priv = dev->dev_private;
5081 int pipe = crtc->pipe;
5082
5083 /* To avoid upsetting the power well on haswell only disable the pfit if
5084 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005085 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005086 I915_WRITE(PF_CTL(pipe), 0);
5087 I915_WRITE(PF_WIN_POS(pipe), 0);
5088 I915_WRITE(PF_WIN_SZ(pipe), 0);
5089 }
5090}
5091
Jesse Barnes6be4a602010-09-10 10:26:01 -07005092static void ironlake_crtc_disable(struct drm_crtc *crtc)
5093{
5094 struct drm_device *dev = crtc->dev;
5095 struct drm_i915_private *dev_priv = dev->dev_private;
5096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005097 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005098 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005099 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005100
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005101 if (!intel_crtc->active)
5102 return;
5103
Daniel Vetterea9d7582012-07-10 10:42:52 +02005104 for_each_encoder_on_crtc(dev, crtc, encoder)
5105 encoder->disable(encoder);
5106
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005107 drm_crtc_vblank_off(crtc);
5108 assert_vblank_disabled(crtc);
5109
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005110 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005111 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005112
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005113 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005114
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005115 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005116
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005117 for_each_encoder_on_crtc(dev, crtc, encoder)
5118 if (encoder->post_disable)
5119 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005120
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005121 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005122 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005123
Daniel Vetterd925c592013-06-05 13:34:04 +02005124 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005125
Daniel Vetterd925c592013-06-05 13:34:04 +02005126 if (HAS_PCH_CPT(dev)) {
5127 /* disable TRANS_DP_CTL */
5128 reg = TRANS_DP_CTL(pipe);
5129 temp = I915_READ(reg);
5130 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5131 TRANS_DP_PORT_SEL_MASK);
5132 temp |= TRANS_DP_PORT_SEL_NONE;
5133 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005134
Daniel Vetterd925c592013-06-05 13:34:04 +02005135 /* disable DPLL_SEL */
5136 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005137 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005138 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005139 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005140
5141 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005142 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005143
5144 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005145 }
5146
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005147 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005148 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005149
5150 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005151 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005152 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005153}
5154
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005155static void haswell_crtc_disable(struct drm_crtc *crtc)
5156{
5157 struct drm_device *dev = crtc->dev;
5158 struct drm_i915_private *dev_priv = dev->dev_private;
5159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5160 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005161 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005162
5163 if (!intel_crtc->active)
5164 return;
5165
Jani Nikula8807e552013-08-30 19:40:32 +03005166 for_each_encoder_on_crtc(dev, crtc, encoder) {
5167 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005168 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005169 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005170
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005171 drm_crtc_vblank_off(crtc);
5172 assert_vblank_disabled(crtc);
5173
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005174 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005175 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5176 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005177 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005178
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005179 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005180 intel_ddi_set_vc_payload_alloc(crtc, false);
5181
Paulo Zanoniad80a812012-10-24 16:06:19 -02005182 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005183
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005184 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005185 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005186 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005187 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005188 else
5189 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005190
Paulo Zanoni1f544382012-10-24 11:32:00 -02005191 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005192
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005193 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005194 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005195 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005196 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005197
Imre Deak97b040a2014-06-25 22:01:50 +03005198 for_each_encoder_on_crtc(dev, crtc, encoder)
5199 if (encoder->post_disable)
5200 encoder->post_disable(encoder);
5201
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005202 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005203 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005204
5205 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005206 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005207 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005208
5209 if (intel_crtc_to_shared_dpll(intel_crtc))
5210 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005211}
5212
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005213static void ironlake_crtc_off(struct drm_crtc *crtc)
5214{
5215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005216 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005217}
5218
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005219
Jesse Barnes2dd24552013-04-25 12:55:01 -07005220static void i9xx_pfit_enable(struct intel_crtc *crtc)
5221{
5222 struct drm_device *dev = crtc->base.dev;
5223 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005224 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005225
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005226 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005227 return;
5228
Daniel Vetterc0b03412013-05-28 12:05:54 +02005229 /*
5230 * The panel fitter should only be adjusted whilst the pipe is disabled,
5231 * according to register description and PRM.
5232 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005233 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5234 assert_pipe_disabled(dev_priv, crtc->pipe);
5235
Jesse Barnesb074cec2013-04-25 12:55:02 -07005236 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5237 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005238
5239 /* Border color in case we don't scale up to the full screen. Black by
5240 * default, change to something else for debugging. */
5241 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005242}
5243
Dave Airlied05410f2014-06-05 13:22:59 +10005244static enum intel_display_power_domain port_to_power_domain(enum port port)
5245{
5246 switch (port) {
5247 case PORT_A:
5248 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5249 case PORT_B:
5250 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5251 case PORT_C:
5252 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5253 case PORT_D:
5254 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5255 default:
5256 WARN_ON_ONCE(1);
5257 return POWER_DOMAIN_PORT_OTHER;
5258 }
5259}
5260
Imre Deak77d22dc2014-03-05 16:20:52 +02005261#define for_each_power_domain(domain, mask) \
5262 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5263 if ((1 << (domain)) & (mask))
5264
Imre Deak319be8a2014-03-04 19:22:57 +02005265enum intel_display_power_domain
5266intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005267{
Imre Deak319be8a2014-03-04 19:22:57 +02005268 struct drm_device *dev = intel_encoder->base.dev;
5269 struct intel_digital_port *intel_dig_port;
5270
5271 switch (intel_encoder->type) {
5272 case INTEL_OUTPUT_UNKNOWN:
5273 /* Only DDI platforms should ever use this output type */
5274 WARN_ON_ONCE(!HAS_DDI(dev));
5275 case INTEL_OUTPUT_DISPLAYPORT:
5276 case INTEL_OUTPUT_HDMI:
5277 case INTEL_OUTPUT_EDP:
5278 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005279 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005280 case INTEL_OUTPUT_DP_MST:
5281 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5282 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005283 case INTEL_OUTPUT_ANALOG:
5284 return POWER_DOMAIN_PORT_CRT;
5285 case INTEL_OUTPUT_DSI:
5286 return POWER_DOMAIN_PORT_DSI;
5287 default:
5288 return POWER_DOMAIN_PORT_OTHER;
5289 }
5290}
5291
5292static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5293{
5294 struct drm_device *dev = crtc->dev;
5295 struct intel_encoder *intel_encoder;
5296 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5297 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005298 unsigned long mask;
5299 enum transcoder transcoder;
5300
5301 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5302
5303 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5304 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005305 if (intel_crtc->config->pch_pfit.enabled ||
5306 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005307 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5308
Imre Deak319be8a2014-03-04 19:22:57 +02005309 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5310 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5311
Imre Deak77d22dc2014-03-05 16:20:52 +02005312 return mask;
5313}
5314
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005315static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005316{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005317 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005318 struct drm_i915_private *dev_priv = dev->dev_private;
5319 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5320 struct intel_crtc *crtc;
5321
5322 /*
5323 * First get all needed power domains, then put all unneeded, to avoid
5324 * any unnecessary toggling of the power wells.
5325 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005326 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005327 enum intel_display_power_domain domain;
5328
Matt Roper83d65732015-02-25 13:12:16 -08005329 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005330 continue;
5331
Imre Deak319be8a2014-03-04 19:22:57 +02005332 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005333
5334 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5335 intel_display_power_get(dev_priv, domain);
5336 }
5337
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005338 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005339 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005340
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005341 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005342 enum intel_display_power_domain domain;
5343
5344 for_each_power_domain(domain, crtc->enabled_power_domains)
5345 intel_display_power_put(dev_priv, domain);
5346
5347 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5348 }
5349
5350 intel_display_set_init_power(dev_priv, false);
5351}
5352
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305353void broxton_set_cdclk(struct drm_device *dev, int frequency)
5354{
5355 struct drm_i915_private *dev_priv = dev->dev_private;
5356 uint32_t divider;
5357 uint32_t ratio;
5358 uint32_t current_freq;
5359 int ret;
5360
5361 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5362 switch (frequency) {
5363 case 144000:
5364 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5365 ratio = BXT_DE_PLL_RATIO(60);
5366 break;
5367 case 288000:
5368 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5369 ratio = BXT_DE_PLL_RATIO(60);
5370 break;
5371 case 384000:
5372 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5373 ratio = BXT_DE_PLL_RATIO(60);
5374 break;
5375 case 576000:
5376 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5377 ratio = BXT_DE_PLL_RATIO(60);
5378 break;
5379 case 624000:
5380 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5381 ratio = BXT_DE_PLL_RATIO(65);
5382 break;
5383 case 19200:
5384 /*
5385 * Bypass frequency with DE PLL disabled. Init ratio, divider
5386 * to suppress GCC warning.
5387 */
5388 ratio = 0;
5389 divider = 0;
5390 break;
5391 default:
5392 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5393
5394 return;
5395 }
5396
5397 mutex_lock(&dev_priv->rps.hw_lock);
5398 /* Inform power controller of upcoming frequency change */
5399 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5400 0x80000000);
5401 mutex_unlock(&dev_priv->rps.hw_lock);
5402
5403 if (ret) {
5404 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5405 ret, frequency);
5406 return;
5407 }
5408
5409 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5410 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5411 current_freq = current_freq * 500 + 1000;
5412
5413 /*
5414 * DE PLL has to be disabled when
5415 * - setting to 19.2MHz (bypass, PLL isn't used)
5416 * - before setting to 624MHz (PLL needs toggling)
5417 * - before setting to any frequency from 624MHz (PLL needs toggling)
5418 */
5419 if (frequency == 19200 || frequency == 624000 ||
5420 current_freq == 624000) {
5421 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5422 /* Timeout 200us */
5423 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5424 1))
5425 DRM_ERROR("timout waiting for DE PLL unlock\n");
5426 }
5427
5428 if (frequency != 19200) {
5429 uint32_t val;
5430
5431 val = I915_READ(BXT_DE_PLL_CTL);
5432 val &= ~BXT_DE_PLL_RATIO_MASK;
5433 val |= ratio;
5434 I915_WRITE(BXT_DE_PLL_CTL, val);
5435
5436 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5437 /* Timeout 200us */
5438 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5439 DRM_ERROR("timeout waiting for DE PLL lock\n");
5440
5441 val = I915_READ(CDCLK_CTL);
5442 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5443 val |= divider;
5444 /*
5445 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5446 * enable otherwise.
5447 */
5448 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5449 if (frequency >= 500000)
5450 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5451
5452 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5453 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5454 val |= (frequency - 1000) / 500;
5455 I915_WRITE(CDCLK_CTL, val);
5456 }
5457
5458 mutex_lock(&dev_priv->rps.hw_lock);
5459 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5460 DIV_ROUND_UP(frequency, 25000));
5461 mutex_unlock(&dev_priv->rps.hw_lock);
5462
5463 if (ret) {
5464 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5465 ret, frequency);
5466 return;
5467 }
5468
5469 dev_priv->cdclk_freq = frequency;
5470}
5471
5472void broxton_init_cdclk(struct drm_device *dev)
5473{
5474 struct drm_i915_private *dev_priv = dev->dev_private;
5475 uint32_t val;
5476
5477 /*
5478 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5479 * or else the reset will hang because there is no PCH to respond.
5480 * Move the handshake programming to initialization sequence.
5481 * Previously was left up to BIOS.
5482 */
5483 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5484 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5485 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5486
5487 /* Enable PG1 for cdclk */
5488 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5489
5490 /* check if cd clock is enabled */
5491 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5492 DRM_DEBUG_KMS("Display already initialized\n");
5493 return;
5494 }
5495
5496 /*
5497 * FIXME:
5498 * - The initial CDCLK needs to be read from VBT.
5499 * Need to make this change after VBT has changes for BXT.
5500 * - check if setting the max (or any) cdclk freq is really necessary
5501 * here, it belongs to modeset time
5502 */
5503 broxton_set_cdclk(dev, 624000);
5504
5505 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005506 POSTING_READ(DBUF_CTL);
5507
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305508 udelay(10);
5509
5510 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5511 DRM_ERROR("DBuf power enable timeout!\n");
5512}
5513
5514void broxton_uninit_cdclk(struct drm_device *dev)
5515{
5516 struct drm_i915_private *dev_priv = dev->dev_private;
5517
5518 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005519 POSTING_READ(DBUF_CTL);
5520
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305521 udelay(10);
5522
5523 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5524 DRM_ERROR("DBuf power disable timeout!\n");
5525
5526 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5527 broxton_set_cdclk(dev, 19200);
5528
5529 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5530}
5531
Ville Syrjälädfcab172014-06-13 13:37:47 +03005532/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005533static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005534{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005535 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005536
Jesse Barnes586f49d2013-11-04 16:06:59 -08005537 /* Obtain SKU information */
5538 mutex_lock(&dev_priv->dpio_lock);
5539 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5540 CCK_FUSE_HPLL_FREQ_MASK;
5541 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005542
Ville Syrjälädfcab172014-06-13 13:37:47 +03005543 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005544}
5545
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005546static void vlv_update_cdclk(struct drm_device *dev)
5547{
5548 struct drm_i915_private *dev_priv = dev->dev_private;
5549
Vandana Kannan164dfd22014-11-24 13:37:41 +05305550 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005551 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305552 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005553
5554 /*
5555 * Program the gmbus_freq based on the cdclk frequency.
5556 * BSpec erroneously claims we should aim for 4MHz, but
5557 * in fact 1MHz is the correct frequency.
5558 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305559 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005560}
5561
Jesse Barnes30a970c2013-11-04 13:48:12 -08005562/* Adjust CDclk dividers to allow high res or save power if possible */
5563static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5564{
5565 struct drm_i915_private *dev_priv = dev->dev_private;
5566 u32 val, cmd;
5567
Vandana Kannan164dfd22014-11-24 13:37:41 +05305568 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5569 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005570
Ville Syrjälädfcab172014-06-13 13:37:47 +03005571 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005572 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005573 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005574 cmd = 1;
5575 else
5576 cmd = 0;
5577
5578 mutex_lock(&dev_priv->rps.hw_lock);
5579 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5580 val &= ~DSPFREQGUAR_MASK;
5581 val |= (cmd << DSPFREQGUAR_SHIFT);
5582 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5583 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5584 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5585 50)) {
5586 DRM_ERROR("timed out waiting for CDclk change\n");
5587 }
5588 mutex_unlock(&dev_priv->rps.hw_lock);
5589
Ville Syrjälädfcab172014-06-13 13:37:47 +03005590 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005591 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005592
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005593 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005594
5595 mutex_lock(&dev_priv->dpio_lock);
5596 /* adjust cdclk divider */
5597 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005598 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005599 val |= divider;
5600 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005601
5602 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5603 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5604 50))
5605 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005606 mutex_unlock(&dev_priv->dpio_lock);
5607 }
5608
5609 mutex_lock(&dev_priv->dpio_lock);
5610 /* adjust self-refresh exit latency value */
5611 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5612 val &= ~0x7f;
5613
5614 /*
5615 * For high bandwidth configs, we set a higher latency in the bunit
5616 * so that the core display fetch happens in time to avoid underruns.
5617 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005618 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005619 val |= 4500 / 250; /* 4.5 usec */
5620 else
5621 val |= 3000 / 250; /* 3.0 usec */
5622 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5623 mutex_unlock(&dev_priv->dpio_lock);
5624
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005625 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005626}
5627
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005628static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5629{
5630 struct drm_i915_private *dev_priv = dev->dev_private;
5631 u32 val, cmd;
5632
Vandana Kannan164dfd22014-11-24 13:37:41 +05305633 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5634 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005635
5636 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005637 case 333333:
5638 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005639 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005640 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005641 break;
5642 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005643 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005644 return;
5645 }
5646
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005647 /*
5648 * Specs are full of misinformation, but testing on actual
5649 * hardware has shown that we just need to write the desired
5650 * CCK divider into the Punit register.
5651 */
5652 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5653
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005654 mutex_lock(&dev_priv->rps.hw_lock);
5655 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5656 val &= ~DSPFREQGUAR_MASK_CHV;
5657 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5658 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5659 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5660 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5661 50)) {
5662 DRM_ERROR("timed out waiting for CDclk change\n");
5663 }
5664 mutex_unlock(&dev_priv->rps.hw_lock);
5665
5666 vlv_update_cdclk(dev);
5667}
5668
Jesse Barnes30a970c2013-11-04 13:48:12 -08005669static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5670 int max_pixclk)
5671{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005672 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005673 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005674
Jesse Barnes30a970c2013-11-04 13:48:12 -08005675 /*
5676 * Really only a few cases to deal with, as only 4 CDclks are supported:
5677 * 200MHz
5678 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005679 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005680 * 400MHz (VLV only)
5681 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5682 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005683 *
5684 * We seem to get an unstable or solid color picture at 200MHz.
5685 * Not sure what's wrong. For now use 200MHz only when all pipes
5686 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005687 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005688 if (!IS_CHERRYVIEW(dev_priv) &&
5689 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005690 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005691 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005692 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005693 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005694 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005695 else
5696 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005697}
5698
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305699static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5700 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005701{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305702 /*
5703 * FIXME:
5704 * - remove the guardband, it's not needed on BXT
5705 * - set 19.2MHz bypass frequency if there are no active pipes
5706 */
5707 if (max_pixclk > 576000*9/10)
5708 return 624000;
5709 else if (max_pixclk > 384000*9/10)
5710 return 576000;
5711 else if (max_pixclk > 288000*9/10)
5712 return 384000;
5713 else if (max_pixclk > 144000*9/10)
5714 return 288000;
5715 else
5716 return 144000;
5717}
5718
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005719/* Compute the max pixel clock for new configuration. Uses atomic state if
5720 * that's non-NULL, look at current state otherwise. */
5721static int intel_mode_max_pixclk(struct drm_device *dev,
5722 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005723{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005724 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005725 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005726 int max_pixclk = 0;
5727
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005728 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005729 if (state)
5730 crtc_state =
5731 intel_atomic_get_crtc_state(state, intel_crtc);
5732 else
5733 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005734 if (IS_ERR(crtc_state))
5735 return PTR_ERR(crtc_state);
5736
5737 if (!crtc_state->base.enable)
5738 continue;
5739
5740 max_pixclk = max(max_pixclk,
5741 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005742 }
5743
5744 return max_pixclk;
5745}
5746
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005747static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005748{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005749 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005750 struct drm_crtc *crtc;
5751 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005752 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005753 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005754
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005755 if (max_pixclk < 0)
5756 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005757
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305758 if (IS_VALLEYVIEW(dev_priv))
5759 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5760 else
5761 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5762
5763 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005764 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005765
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005766 /* add all active pipes to the state */
5767 for_each_crtc(state->dev, crtc) {
5768 if (!crtc->state->enable)
5769 continue;
5770
5771 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5772 if (IS_ERR(crtc_state))
5773 return PTR_ERR(crtc_state);
5774 }
5775
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005776 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005777 for_each_crtc_in_state(state, crtc, crtc_state, i)
5778 if (crtc_state->enable)
5779 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005780
5781 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005782}
5783
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005784static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5785{
5786 unsigned int credits, default_credits;
5787
5788 if (IS_CHERRYVIEW(dev_priv))
5789 default_credits = PFI_CREDIT(12);
5790 else
5791 default_credits = PFI_CREDIT(8);
5792
Vandana Kannan164dfd22014-11-24 13:37:41 +05305793 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005794 /* CHV suggested value is 31 or 63 */
5795 if (IS_CHERRYVIEW(dev_priv))
5796 credits = PFI_CREDIT_31;
5797 else
5798 credits = PFI_CREDIT(15);
5799 } else {
5800 credits = default_credits;
5801 }
5802
5803 /*
5804 * WA - write default credits before re-programming
5805 * FIXME: should we also set the resend bit here?
5806 */
5807 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5808 default_credits);
5809
5810 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5811 credits | PFI_CREDIT_RESEND);
5812
5813 /*
5814 * FIXME is this guaranteed to clear
5815 * immediately or should we poll for it?
5816 */
5817 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5818}
5819
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005820static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005821{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005822 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005823 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005824 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005825 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005826
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005827 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5828 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005829 if (WARN_ON(max_pixclk < 0))
5830 return;
5831
5832 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005833
Vandana Kannan164dfd22014-11-24 13:37:41 +05305834 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005835 /*
5836 * FIXME: We can end up here with all power domains off, yet
5837 * with a CDCLK frequency other than the minimum. To account
5838 * for this take the PIPE-A power domain, which covers the HW
5839 * blocks needed for the following programming. This can be
5840 * removed once it's guaranteed that we get here either with
5841 * the minimum CDCLK set, or the required power domains
5842 * enabled.
5843 */
5844 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5845
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005846 if (IS_CHERRYVIEW(dev))
5847 cherryview_set_cdclk(dev, req_cdclk);
5848 else
5849 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005850
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005851 vlv_program_pfi_credits(dev_priv);
5852
Imre Deak738c05c2014-11-19 16:25:37 +02005853 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005854 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005855}
5856
Jesse Barnes89b667f2013-04-18 14:51:36 -07005857static void valleyview_crtc_enable(struct drm_crtc *crtc)
5858{
5859 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005860 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005861 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5862 struct intel_encoder *encoder;
5863 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005864 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005865
Matt Roper83d65732015-02-25 13:12:16 -08005866 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005867
5868 if (intel_crtc->active)
5869 return;
5870
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005871 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305872
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005873 if (!is_dsi) {
5874 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005875 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005876 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005877 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005878 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005879
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005880 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305881 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005882
5883 intel_set_pipe_timings(intel_crtc);
5884
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005885 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5886 struct drm_i915_private *dev_priv = dev->dev_private;
5887
5888 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5889 I915_WRITE(CHV_CANVAS(pipe), 0);
5890 }
5891
Daniel Vetter5b18e572014-04-24 23:55:06 +02005892 i9xx_set_pipeconf(intel_crtc);
5893
Jesse Barnes89b667f2013-04-18 14:51:36 -07005894 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005895
Daniel Vettera72e4c92014-09-30 10:56:47 +02005896 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005897
Jesse Barnes89b667f2013-04-18 14:51:36 -07005898 for_each_encoder_on_crtc(dev, crtc, encoder)
5899 if (encoder->pre_pll_enable)
5900 encoder->pre_pll_enable(encoder);
5901
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005902 if (!is_dsi) {
5903 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005904 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005905 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005906 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005907 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005908
5909 for_each_encoder_on_crtc(dev, crtc, encoder)
5910 if (encoder->pre_enable)
5911 encoder->pre_enable(encoder);
5912
Jesse Barnes2dd24552013-04-25 12:55:01 -07005913 i9xx_pfit_enable(intel_crtc);
5914
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005915 intel_crtc_load_lut(crtc);
5916
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005917 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005918 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005919
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005920 assert_vblank_disabled(crtc);
5921 drm_crtc_vblank_on(crtc);
5922
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005923 for_each_encoder_on_crtc(dev, crtc, encoder)
5924 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005925}
5926
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005927static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5928{
5929 struct drm_device *dev = crtc->base.dev;
5930 struct drm_i915_private *dev_priv = dev->dev_private;
5931
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005932 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5933 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005934}
5935
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005936static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005937{
5938 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005939 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005940 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005941 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005942 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005943
Matt Roper83d65732015-02-25 13:12:16 -08005944 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005945
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005946 if (intel_crtc->active)
5947 return;
5948
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005949 i9xx_set_pll_dividers(intel_crtc);
5950
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005951 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305952 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005953
5954 intel_set_pipe_timings(intel_crtc);
5955
Daniel Vetter5b18e572014-04-24 23:55:06 +02005956 i9xx_set_pipeconf(intel_crtc);
5957
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005958 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005959
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005960 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005961 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005962
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005963 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005964 if (encoder->pre_enable)
5965 encoder->pre_enable(encoder);
5966
Daniel Vetterf6736a12013-06-05 13:34:30 +02005967 i9xx_enable_pll(intel_crtc);
5968
Jesse Barnes2dd24552013-04-25 12:55:01 -07005969 i9xx_pfit_enable(intel_crtc);
5970
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005971 intel_crtc_load_lut(crtc);
5972
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005973 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005974 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005975
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005976 assert_vblank_disabled(crtc);
5977 drm_crtc_vblank_on(crtc);
5978
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005979 for_each_encoder_on_crtc(dev, crtc, encoder)
5980 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005981}
5982
Daniel Vetter87476d62013-04-11 16:29:06 +02005983static void i9xx_pfit_disable(struct intel_crtc *crtc)
5984{
5985 struct drm_device *dev = crtc->base.dev;
5986 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005987
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005988 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005989 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005990
5991 assert_pipe_disabled(dev_priv, crtc->pipe);
5992
Daniel Vetter328d8e82013-05-08 10:36:31 +02005993 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5994 I915_READ(PFIT_CONTROL));
5995 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005996}
5997
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005998static void i9xx_crtc_disable(struct drm_crtc *crtc)
5999{
6000 struct drm_device *dev = crtc->dev;
6001 struct drm_i915_private *dev_priv = dev->dev_private;
6002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006003 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006004 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006005
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006006 if (!intel_crtc->active)
6007 return;
6008
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006009 /*
6010 * On gen2 planes are double buffered but the pipe isn't, so we must
6011 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006012 * We also need to wait on all gmch platforms because of the
6013 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006014 */
Imre Deak564ed192014-06-13 14:54:21 +03006015 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006016
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006017 for_each_encoder_on_crtc(dev, crtc, encoder)
6018 encoder->disable(encoder);
6019
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006020 drm_crtc_vblank_off(crtc);
6021 assert_vblank_disabled(crtc);
6022
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006023 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006024
Daniel Vetter87476d62013-04-11 16:29:06 +02006025 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006026
Jesse Barnes89b667f2013-04-18 14:51:36 -07006027 for_each_encoder_on_crtc(dev, crtc, encoder)
6028 if (encoder->post_disable)
6029 encoder->post_disable(encoder);
6030
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006031 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006032 if (IS_CHERRYVIEW(dev))
6033 chv_disable_pll(dev_priv, pipe);
6034 else if (IS_VALLEYVIEW(dev))
6035 vlv_disable_pll(dev_priv, pipe);
6036 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006037 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006038 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006039
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006040 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006041 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006042
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006043 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006044 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006045
Daniel Vetterefa96242014-04-24 23:55:02 +02006046 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006047 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006048 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006049}
6050
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006051static void i9xx_crtc_off(struct drm_crtc *crtc)
6052{
6053}
6054
Borun Fub04c5bd2014-07-12 10:02:27 +05306055/* Master function to enable/disable CRTC and corresponding power wells */
6056void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006057{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006058 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006059 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006060 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006061 enum intel_display_power_domain domain;
6062 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006063
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006064 if (enable) {
6065 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006066 domains = get_crtc_power_domains(crtc);
6067 for_each_power_domain(domain, domains)
6068 intel_display_power_get(dev_priv, domain);
6069 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006070
6071 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006072 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006073 }
6074 } else {
6075 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006076 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006077 dev_priv->display.crtc_disable(crtc);
6078
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006079 domains = intel_crtc->enabled_power_domains;
6080 for_each_power_domain(domain, domains)
6081 intel_display_power_put(dev_priv, domain);
6082 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006083 }
6084 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306085}
6086
6087/**
6088 * Sets the power management mode of the pipe and plane.
6089 */
6090void intel_crtc_update_dpms(struct drm_crtc *crtc)
6091{
6092 struct drm_device *dev = crtc->dev;
6093 struct intel_encoder *intel_encoder;
6094 bool enable = false;
6095
6096 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6097 enable |= intel_encoder->connectors_active;
6098
6099 intel_crtc_control(crtc, enable);
Ander Conselvan de Oliveira0f63cca2015-04-21 17:13:17 +03006100
6101 crtc->state->active = enable;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006102}
6103
Daniel Vetter976f8a22012-07-08 22:34:21 +02006104static void intel_crtc_disable(struct drm_crtc *crtc)
6105{
6106 struct drm_device *dev = crtc->dev;
6107 struct drm_connector *connector;
6108 struct drm_i915_private *dev_priv = dev->dev_private;
6109
6110 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006111 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006112
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006113 intel_crtc_disable_planes(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006114 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006115 dev_priv->display.off(crtc);
6116
Matt Roper70a101f2015-04-08 18:56:53 -07006117 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006118
6119 /* Update computed state. */
6120 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6121 if (!connector->encoder || !connector->encoder->crtc)
6122 continue;
6123
6124 if (connector->encoder->crtc != crtc)
6125 continue;
6126
6127 connector->dpms = DRM_MODE_DPMS_OFF;
6128 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006129 }
6130}
6131
Chris Wilsonea5b2132010-08-04 13:50:23 +01006132void intel_encoder_destroy(struct drm_encoder *encoder)
6133{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006134 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006135
Chris Wilsonea5b2132010-08-04 13:50:23 +01006136 drm_encoder_cleanup(encoder);
6137 kfree(intel_encoder);
6138}
6139
Damien Lespiau92373292013-08-08 22:28:57 +01006140/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006141 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6142 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006143static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006144{
6145 if (mode == DRM_MODE_DPMS_ON) {
6146 encoder->connectors_active = true;
6147
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006148 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006149 } else {
6150 encoder->connectors_active = false;
6151
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006152 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006153 }
6154}
6155
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006156/* Cross check the actual hw state with our own modeset state tracking (and it's
6157 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006158static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006159{
6160 if (connector->get_hw_state(connector)) {
6161 struct intel_encoder *encoder = connector->encoder;
6162 struct drm_crtc *crtc;
6163 bool encoder_enabled;
6164 enum pipe pipe;
6165
6166 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6167 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006168 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006169
Dave Airlie0e32b392014-05-02 14:02:48 +10006170 /* there is no real hw state for MST connectors */
6171 if (connector->mst_port)
6172 return;
6173
Rob Clarke2c719b2014-12-15 13:56:32 -05006174 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006175 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006176 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006177 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006178
Dave Airlie36cd7442014-05-02 13:44:18 +10006179 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006180 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006181 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006182
Dave Airlie36cd7442014-05-02 13:44:18 +10006183 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006184 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6185 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006186 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006187
Dave Airlie36cd7442014-05-02 13:44:18 +10006188 crtc = encoder->base.crtc;
6189
Matt Roper83d65732015-02-25 13:12:16 -08006190 I915_STATE_WARN(!crtc->state->enable,
6191 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006192 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6193 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006194 "encoder active on the wrong pipe\n");
6195 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006196 }
6197}
6198
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006199int intel_connector_init(struct intel_connector *connector)
6200{
6201 struct drm_connector_state *connector_state;
6202
6203 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6204 if (!connector_state)
6205 return -ENOMEM;
6206
6207 connector->base.state = connector_state;
6208 return 0;
6209}
6210
6211struct intel_connector *intel_connector_alloc(void)
6212{
6213 struct intel_connector *connector;
6214
6215 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6216 if (!connector)
6217 return NULL;
6218
6219 if (intel_connector_init(connector) < 0) {
6220 kfree(connector);
6221 return NULL;
6222 }
6223
6224 return connector;
6225}
6226
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006227/* Even simpler default implementation, if there's really no special case to
6228 * consider. */
6229void intel_connector_dpms(struct drm_connector *connector, int mode)
6230{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006231 /* All the simple cases only support two dpms states. */
6232 if (mode != DRM_MODE_DPMS_ON)
6233 mode = DRM_MODE_DPMS_OFF;
6234
6235 if (mode == connector->dpms)
6236 return;
6237
6238 connector->dpms = mode;
6239
6240 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006241 if (connector->encoder)
6242 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006243
Daniel Vetterb9805142012-08-31 17:37:33 +02006244 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006245}
6246
Daniel Vetterf0947c32012-07-02 13:10:34 +02006247/* Simple connector->get_hw_state implementation for encoders that support only
6248 * one connector and no cloning and hence the encoder state determines the state
6249 * of the connector. */
6250bool intel_connector_get_hw_state(struct intel_connector *connector)
6251{
Daniel Vetter24929352012-07-02 20:28:59 +02006252 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006253 struct intel_encoder *encoder = connector->encoder;
6254
6255 return encoder->get_hw_state(encoder, &pipe);
6256}
6257
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006258static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006259{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006260 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6261 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006262
6263 return 0;
6264}
6265
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006266static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006267 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006268{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006269 struct drm_atomic_state *state = pipe_config->base.state;
6270 struct intel_crtc *other_crtc;
6271 struct intel_crtc_state *other_crtc_state;
6272
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006273 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6274 pipe_name(pipe), pipe_config->fdi_lanes);
6275 if (pipe_config->fdi_lanes > 4) {
6276 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6277 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006278 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006279 }
6280
Paulo Zanonibafb6552013-11-02 21:07:44 -07006281 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006282 if (pipe_config->fdi_lanes > 2) {
6283 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6284 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006285 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006286 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006287 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006288 }
6289 }
6290
6291 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006292 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006293
6294 /* Ivybridge 3 pipe is really complicated */
6295 switch (pipe) {
6296 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006297 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006298 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006299 if (pipe_config->fdi_lanes <= 2)
6300 return 0;
6301
6302 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6303 other_crtc_state =
6304 intel_atomic_get_crtc_state(state, other_crtc);
6305 if (IS_ERR(other_crtc_state))
6306 return PTR_ERR(other_crtc_state);
6307
6308 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006309 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6310 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006311 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006312 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006313 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006314 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006315 if (pipe_config->fdi_lanes > 2) {
6316 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6317 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006318 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006319 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006320
6321 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6322 other_crtc_state =
6323 intel_atomic_get_crtc_state(state, other_crtc);
6324 if (IS_ERR(other_crtc_state))
6325 return PTR_ERR(other_crtc_state);
6326
6327 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006328 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006329 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006330 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006331 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006332 default:
6333 BUG();
6334 }
6335}
6336
Daniel Vettere29c22c2013-02-21 00:00:16 +01006337#define RETRY 1
6338static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006339 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006340{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006341 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006342 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006343 int lane, link_bw, fdi_dotclock, ret;
6344 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006345
Daniel Vettere29c22c2013-02-21 00:00:16 +01006346retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006347 /* FDI is a binary signal running at ~2.7GHz, encoding
6348 * each output octet as 10 bits. The actual frequency
6349 * is stored as a divider into a 100MHz clock, and the
6350 * mode pixel clock is stored in units of 1KHz.
6351 * Hence the bw of each lane in terms of the mode signal
6352 * is:
6353 */
6354 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6355
Damien Lespiau241bfc32013-09-25 16:45:37 +01006356 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006357
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006358 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006359 pipe_config->pipe_bpp);
6360
6361 pipe_config->fdi_lanes = lane;
6362
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006363 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006364 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006365
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006366 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6367 intel_crtc->pipe, pipe_config);
6368 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006369 pipe_config->pipe_bpp -= 2*3;
6370 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6371 pipe_config->pipe_bpp);
6372 needs_recompute = true;
6373 pipe_config->bw_constrained = true;
6374
6375 goto retry;
6376 }
6377
6378 if (needs_recompute)
6379 return RETRY;
6380
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006381 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006382}
6383
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006384static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006385 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006386{
Jani Nikulad330a952014-01-21 11:24:25 +02006387 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006388 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006389 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006390}
6391
Daniel Vettera43f6e02013-06-07 23:10:32 +02006392static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006393 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006394{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006395 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006396 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006397 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006398 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006399
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006400 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006401 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006402 int clock_limit =
6403 dev_priv->display.get_display_clock_speed(dev);
6404
6405 /*
6406 * Enable pixel doubling when the dot clock
6407 * is > 90% of the (display) core speed.
6408 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006409 * GDG double wide on either pipe,
6410 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006411 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006412 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006413 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006414 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006415 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006416 }
6417
Damien Lespiau241bfc32013-09-25 16:45:37 +01006418 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006419 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006420 }
Chris Wilson89749352010-09-12 18:25:19 +01006421
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006422 /*
6423 * Pipe horizontal size must be even in:
6424 * - DVO ganged mode
6425 * - LVDS dual channel mode
6426 * - Double wide pipe
6427 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006428 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006429 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6430 pipe_config->pipe_src_w &= ~1;
6431
Damien Lespiau8693a822013-05-03 18:48:11 +01006432 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6433 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006434 */
6435 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6436 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006437 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006438
Damien Lespiauf5adf942013-06-24 18:29:34 +01006439 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006440 hsw_compute_ips_config(crtc, pipe_config);
6441
Daniel Vetter877d48d2013-04-19 11:24:43 +02006442 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006443 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006444
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006445 /* FIXME: remove below call once atomic mode set is place and all crtc
6446 * related checks called from atomic_crtc_check function */
6447 ret = 0;
6448 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6449 crtc, pipe_config->base.state);
6450 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6451
6452 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006453}
6454
Ville Syrjälä1652d192015-03-31 14:12:01 +03006455static int skylake_get_display_clock_speed(struct drm_device *dev)
6456{
6457 struct drm_i915_private *dev_priv = to_i915(dev);
6458 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6459 uint32_t cdctl = I915_READ(CDCLK_CTL);
6460 uint32_t linkrate;
6461
6462 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6463 WARN(1, "LCPLL1 not enabled\n");
6464 return 24000; /* 24MHz is the cd freq with NSSC ref */
6465 }
6466
6467 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6468 return 540000;
6469
6470 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006471 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006472
Damien Lespiau71cd8422015-04-30 16:39:17 +01006473 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6474 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006475 /* vco 8640 */
6476 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6477 case CDCLK_FREQ_450_432:
6478 return 432000;
6479 case CDCLK_FREQ_337_308:
6480 return 308570;
6481 case CDCLK_FREQ_675_617:
6482 return 617140;
6483 default:
6484 WARN(1, "Unknown cd freq selection\n");
6485 }
6486 } else {
6487 /* vco 8100 */
6488 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6489 case CDCLK_FREQ_450_432:
6490 return 450000;
6491 case CDCLK_FREQ_337_308:
6492 return 337500;
6493 case CDCLK_FREQ_675_617:
6494 return 675000;
6495 default:
6496 WARN(1, "Unknown cd freq selection\n");
6497 }
6498 }
6499
6500 /* error case, do as if DPLL0 isn't enabled */
6501 return 24000;
6502}
6503
6504static int broadwell_get_display_clock_speed(struct drm_device *dev)
6505{
6506 struct drm_i915_private *dev_priv = dev->dev_private;
6507 uint32_t lcpll = I915_READ(LCPLL_CTL);
6508 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6509
6510 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6511 return 800000;
6512 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6513 return 450000;
6514 else if (freq == LCPLL_CLK_FREQ_450)
6515 return 450000;
6516 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6517 return 540000;
6518 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6519 return 337500;
6520 else
6521 return 675000;
6522}
6523
6524static int haswell_get_display_clock_speed(struct drm_device *dev)
6525{
6526 struct drm_i915_private *dev_priv = dev->dev_private;
6527 uint32_t lcpll = I915_READ(LCPLL_CTL);
6528 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6529
6530 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6531 return 800000;
6532 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6533 return 450000;
6534 else if (freq == LCPLL_CLK_FREQ_450)
6535 return 450000;
6536 else if (IS_HSW_ULT(dev))
6537 return 337500;
6538 else
6539 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006540}
6541
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006542static int valleyview_get_display_clock_speed(struct drm_device *dev)
6543{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006544 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006545 u32 val;
6546 int divider;
6547
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006548 if (dev_priv->hpll_freq == 0)
6549 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6550
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006551 mutex_lock(&dev_priv->dpio_lock);
6552 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6553 mutex_unlock(&dev_priv->dpio_lock);
6554
6555 divider = val & DISPLAY_FREQUENCY_VALUES;
6556
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006557 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6558 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6559 "cdclk change in progress\n");
6560
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006561 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006562}
6563
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006564static int ilk_get_display_clock_speed(struct drm_device *dev)
6565{
6566 return 450000;
6567}
6568
Jesse Barnese70236a2009-09-21 10:42:27 -07006569static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006570{
Jesse Barnese70236a2009-09-21 10:42:27 -07006571 return 400000;
6572}
Jesse Barnes79e53942008-11-07 14:24:08 -08006573
Jesse Barnese70236a2009-09-21 10:42:27 -07006574static int i915_get_display_clock_speed(struct drm_device *dev)
6575{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006576 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006577}
Jesse Barnes79e53942008-11-07 14:24:08 -08006578
Jesse Barnese70236a2009-09-21 10:42:27 -07006579static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6580{
6581 return 200000;
6582}
Jesse Barnes79e53942008-11-07 14:24:08 -08006583
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006584static int pnv_get_display_clock_speed(struct drm_device *dev)
6585{
6586 u16 gcfgc = 0;
6587
6588 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6589
6590 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6591 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006592 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006593 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006594 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006595 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006596 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006597 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6598 return 200000;
6599 default:
6600 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6601 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006602 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006603 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006604 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006605 }
6606}
6607
Jesse Barnese70236a2009-09-21 10:42:27 -07006608static int i915gm_get_display_clock_speed(struct drm_device *dev)
6609{
6610 u16 gcfgc = 0;
6611
6612 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6613
6614 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006615 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006616 else {
6617 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6618 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006619 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006620 default:
6621 case GC_DISPLAY_CLOCK_190_200_MHZ:
6622 return 190000;
6623 }
6624 }
6625}
Jesse Barnes79e53942008-11-07 14:24:08 -08006626
Jesse Barnese70236a2009-09-21 10:42:27 -07006627static int i865_get_display_clock_speed(struct drm_device *dev)
6628{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006629 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006630}
6631
6632static int i855_get_display_clock_speed(struct drm_device *dev)
6633{
6634 u16 hpllcc = 0;
6635 /* Assume that the hardware is in the high speed state. This
6636 * should be the default.
6637 */
6638 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6639 case GC_CLOCK_133_200:
6640 case GC_CLOCK_100_200:
6641 return 200000;
6642 case GC_CLOCK_166_250:
6643 return 250000;
6644 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006645 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006646 }
6647
6648 /* Shouldn't happen */
6649 return 0;
6650}
6651
6652static int i830_get_display_clock_speed(struct drm_device *dev)
6653{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006654 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006655}
6656
Zhenyu Wang2c072452009-06-05 15:38:42 +08006657static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006658intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006659{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006660 while (*num > DATA_LINK_M_N_MASK ||
6661 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006662 *num >>= 1;
6663 *den >>= 1;
6664 }
6665}
6666
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006667static void compute_m_n(unsigned int m, unsigned int n,
6668 uint32_t *ret_m, uint32_t *ret_n)
6669{
6670 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6671 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6672 intel_reduce_m_n_ratio(ret_m, ret_n);
6673}
6674
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006675void
6676intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6677 int pixel_clock, int link_clock,
6678 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006679{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006680 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006681
6682 compute_m_n(bits_per_pixel * pixel_clock,
6683 link_clock * nlanes * 8,
6684 &m_n->gmch_m, &m_n->gmch_n);
6685
6686 compute_m_n(pixel_clock, link_clock,
6687 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006688}
6689
Chris Wilsona7615032011-01-12 17:04:08 +00006690static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6691{
Jani Nikulad330a952014-01-21 11:24:25 +02006692 if (i915.panel_use_ssc >= 0)
6693 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006694 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006695 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006696}
6697
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006698static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6699 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006700{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006701 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006702 struct drm_i915_private *dev_priv = dev->dev_private;
6703 int refclk;
6704
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006705 WARN_ON(!crtc_state->base.state);
6706
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006707 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006708 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006709 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006710 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006711 refclk = dev_priv->vbt.lvds_ssc_freq;
6712 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006713 } else if (!IS_GEN2(dev)) {
6714 refclk = 96000;
6715 } else {
6716 refclk = 48000;
6717 }
6718
6719 return refclk;
6720}
6721
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006722static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006723{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006724 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006725}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006726
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006727static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6728{
6729 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006730}
6731
Daniel Vetterf47709a2013-03-28 10:42:02 +01006732static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006733 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006734 intel_clock_t *reduced_clock)
6735{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006736 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006737 u32 fp, fp2 = 0;
6738
6739 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006740 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006741 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006742 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006743 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006744 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006745 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006746 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006747 }
6748
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006749 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006750
Daniel Vetterf47709a2013-03-28 10:42:02 +01006751 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006752 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006753 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006754 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006755 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006756 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006757 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006758 }
6759}
6760
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006761static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6762 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006763{
6764 u32 reg_val;
6765
6766 /*
6767 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6768 * and set it to a reasonable value instead.
6769 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006770 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006771 reg_val &= 0xffffff00;
6772 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006773 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006774
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006775 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006776 reg_val &= 0x8cffffff;
6777 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006778 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006779
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006780 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006781 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006782 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006783
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006784 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006785 reg_val &= 0x00ffffff;
6786 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006787 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006788}
6789
Daniel Vetterb5518422013-05-03 11:49:48 +02006790static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6791 struct intel_link_m_n *m_n)
6792{
6793 struct drm_device *dev = crtc->base.dev;
6794 struct drm_i915_private *dev_priv = dev->dev_private;
6795 int pipe = crtc->pipe;
6796
Daniel Vettere3b95f12013-05-03 11:49:49 +02006797 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6798 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6799 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6800 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006801}
6802
6803static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006804 struct intel_link_m_n *m_n,
6805 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006806{
6807 struct drm_device *dev = crtc->base.dev;
6808 struct drm_i915_private *dev_priv = dev->dev_private;
6809 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006810 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006811
6812 if (INTEL_INFO(dev)->gen >= 5) {
6813 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6814 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6815 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6816 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006817 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6818 * for gen < 8) and if DRRS is supported (to make sure the
6819 * registers are not unnecessarily accessed).
6820 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306821 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006822 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006823 I915_WRITE(PIPE_DATA_M2(transcoder),
6824 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6825 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6826 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6827 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6828 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006829 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006830 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6831 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6832 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6833 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006834 }
6835}
6836
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306837void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006838{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306839 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6840
6841 if (m_n == M1_N1) {
6842 dp_m_n = &crtc->config->dp_m_n;
6843 dp_m2_n2 = &crtc->config->dp_m2_n2;
6844 } else if (m_n == M2_N2) {
6845
6846 /*
6847 * M2_N2 registers are not supported. Hence m2_n2 divider value
6848 * needs to be programmed into M1_N1.
6849 */
6850 dp_m_n = &crtc->config->dp_m2_n2;
6851 } else {
6852 DRM_ERROR("Unsupported divider value\n");
6853 return;
6854 }
6855
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006856 if (crtc->config->has_pch_encoder)
6857 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006858 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306859 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006860}
6861
Ville Syrjäläd288f652014-10-28 13:20:22 +02006862static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006863 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006864{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006865 u32 dpll, dpll_md;
6866
6867 /*
6868 * Enable DPIO clock input. We should never disable the reference
6869 * clock for pipe B, since VGA hotplug / manual detection depends
6870 * on it.
6871 */
6872 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6873 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6874 /* We should never disable this, set it here for state tracking */
6875 if (crtc->pipe == PIPE_B)
6876 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6877 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006878 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006879
Ville Syrjäläd288f652014-10-28 13:20:22 +02006880 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006881 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006882 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006883}
6884
Ville Syrjäläd288f652014-10-28 13:20:22 +02006885static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006886 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006887{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006888 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006889 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006890 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006891 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006892 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006893 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006894
Daniel Vetter09153002012-12-12 14:06:44 +01006895 mutex_lock(&dev_priv->dpio_lock);
6896
Ville Syrjäläd288f652014-10-28 13:20:22 +02006897 bestn = pipe_config->dpll.n;
6898 bestm1 = pipe_config->dpll.m1;
6899 bestm2 = pipe_config->dpll.m2;
6900 bestp1 = pipe_config->dpll.p1;
6901 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006902
Jesse Barnes89b667f2013-04-18 14:51:36 -07006903 /* See eDP HDMI DPIO driver vbios notes doc */
6904
6905 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006906 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006907 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006908
6909 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006910 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006911
6912 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006913 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006914 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006915 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006916
6917 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006918 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006919
6920 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006921 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6922 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6923 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006924 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006925
6926 /*
6927 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6928 * but we don't support that).
6929 * Note: don't use the DAC post divider as it seems unstable.
6930 */
6931 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006932 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006933
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006934 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006935 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006936
Jesse Barnes89b667f2013-04-18 14:51:36 -07006937 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006938 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006939 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6940 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006941 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006942 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006943 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006944 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006945 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006946
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006947 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006948 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006949 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006950 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006951 0x0df40000);
6952 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006953 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006954 0x0df70000);
6955 } else { /* HDMI or VGA */
6956 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006957 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006958 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006959 0x0df70000);
6960 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006961 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006962 0x0df40000);
6963 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006964
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006965 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006966 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006967 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6968 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006969 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006970 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006971
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006972 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006973 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006974}
6975
Ville Syrjäläd288f652014-10-28 13:20:22 +02006976static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006977 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006978{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006979 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006980 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6981 DPLL_VCO_ENABLE;
6982 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006983 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006984
Ville Syrjäläd288f652014-10-28 13:20:22 +02006985 pipe_config->dpll_hw_state.dpll_md =
6986 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006987}
6988
Ville Syrjäläd288f652014-10-28 13:20:22 +02006989static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006990 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006991{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006992 struct drm_device *dev = crtc->base.dev;
6993 struct drm_i915_private *dev_priv = dev->dev_private;
6994 int pipe = crtc->pipe;
6995 int dpll_reg = DPLL(crtc->pipe);
6996 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306997 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006998 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306999 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307000 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007001
Ville Syrjäläd288f652014-10-28 13:20:22 +02007002 bestn = pipe_config->dpll.n;
7003 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7004 bestm1 = pipe_config->dpll.m1;
7005 bestm2 = pipe_config->dpll.m2 >> 22;
7006 bestp1 = pipe_config->dpll.p1;
7007 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307008 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307009 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307010 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007011
7012 /*
7013 * Enable Refclk and SSC
7014 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007015 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007016 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007017
7018 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007019
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007020 /* p1 and p2 divider */
7021 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7022 5 << DPIO_CHV_S1_DIV_SHIFT |
7023 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7024 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7025 1 << DPIO_CHV_K_DIV_SHIFT);
7026
7027 /* Feedback post-divider - m2 */
7028 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7029
7030 /* Feedback refclk divider - n and m1 */
7031 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7032 DPIO_CHV_M1_DIV_BY_2 |
7033 1 << DPIO_CHV_N_DIV_SHIFT);
7034
7035 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307036 if (bestm2_frac)
7037 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007038
7039 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307040 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7041 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7042 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7043 if (bestm2_frac)
7044 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7045 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007046
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307047 /* Program digital lock detect threshold */
7048 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7049 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7050 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7051 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7052 if (!bestm2_frac)
7053 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7054 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7055
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007056 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307057 if (vco == 5400000) {
7058 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7059 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7060 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7061 tribuf_calcntr = 0x9;
7062 } else if (vco <= 6200000) {
7063 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7064 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7065 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7066 tribuf_calcntr = 0x9;
7067 } else if (vco <= 6480000) {
7068 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7069 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7070 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7071 tribuf_calcntr = 0x8;
7072 } else {
7073 /* Not supported. Apply the same limits as in the max case */
7074 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7075 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7076 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7077 tribuf_calcntr = 0;
7078 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007079 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7080
Ville Syrjälä968040b2015-03-11 22:52:08 +02007081 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307082 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7083 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7084 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7085
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007086 /* AFC Recal */
7087 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7088 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7089 DPIO_AFC_RECAL);
7090
7091 mutex_unlock(&dev_priv->dpio_lock);
7092}
7093
Ville Syrjäläd288f652014-10-28 13:20:22 +02007094/**
7095 * vlv_force_pll_on - forcibly enable just the PLL
7096 * @dev_priv: i915 private structure
7097 * @pipe: pipe PLL to enable
7098 * @dpll: PLL configuration
7099 *
7100 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7101 * in cases where we need the PLL enabled even when @pipe is not going to
7102 * be enabled.
7103 */
7104void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7105 const struct dpll *dpll)
7106{
7107 struct intel_crtc *crtc =
7108 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007109 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007110 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007111 .pixel_multiplier = 1,
7112 .dpll = *dpll,
7113 };
7114
7115 if (IS_CHERRYVIEW(dev)) {
7116 chv_update_pll(crtc, &pipe_config);
7117 chv_prepare_pll(crtc, &pipe_config);
7118 chv_enable_pll(crtc, &pipe_config);
7119 } else {
7120 vlv_update_pll(crtc, &pipe_config);
7121 vlv_prepare_pll(crtc, &pipe_config);
7122 vlv_enable_pll(crtc, &pipe_config);
7123 }
7124}
7125
7126/**
7127 * vlv_force_pll_off - forcibly disable just the PLL
7128 * @dev_priv: i915 private structure
7129 * @pipe: pipe PLL to disable
7130 *
7131 * Disable the PLL for @pipe. To be used in cases where we need
7132 * the PLL enabled even when @pipe is not going to be enabled.
7133 */
7134void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7135{
7136 if (IS_CHERRYVIEW(dev))
7137 chv_disable_pll(to_i915(dev), pipe);
7138 else
7139 vlv_disable_pll(to_i915(dev), pipe);
7140}
7141
Daniel Vetterf47709a2013-03-28 10:42:02 +01007142static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007143 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007144 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007145 int num_connectors)
7146{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007147 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007148 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007149 u32 dpll;
7150 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007151 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007152
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007153 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307154
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007155 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7156 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007157
7158 dpll = DPLL_VGA_MODE_DIS;
7159
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007160 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007161 dpll |= DPLLB_MODE_LVDS;
7162 else
7163 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007164
Daniel Vetteref1b4602013-06-01 17:17:04 +02007165 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007166 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007167 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007168 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007169
7170 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007171 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007172
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007173 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007174 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007175
7176 /* compute bitmask from p1 value */
7177 if (IS_PINEVIEW(dev))
7178 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7179 else {
7180 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7181 if (IS_G4X(dev) && reduced_clock)
7182 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7183 }
7184 switch (clock->p2) {
7185 case 5:
7186 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7187 break;
7188 case 7:
7189 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7190 break;
7191 case 10:
7192 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7193 break;
7194 case 14:
7195 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7196 break;
7197 }
7198 if (INTEL_INFO(dev)->gen >= 4)
7199 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7200
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007201 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007202 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007203 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007204 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7205 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7206 else
7207 dpll |= PLL_REF_INPUT_DREFCLK;
7208
7209 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007210 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007211
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007212 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007213 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007214 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007215 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007216 }
7217}
7218
Daniel Vetterf47709a2013-03-28 10:42:02 +01007219static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007220 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007221 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007222 int num_connectors)
7223{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007224 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007225 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007226 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007227 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007228
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007229 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307230
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007231 dpll = DPLL_VGA_MODE_DIS;
7232
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007233 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007234 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7235 } else {
7236 if (clock->p1 == 2)
7237 dpll |= PLL_P1_DIVIDE_BY_TWO;
7238 else
7239 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7240 if (clock->p2 == 4)
7241 dpll |= PLL_P2_DIVIDE_BY_4;
7242 }
7243
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007244 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007245 dpll |= DPLL_DVO_2X_MODE;
7246
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007247 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007248 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7249 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7250 else
7251 dpll |= PLL_REF_INPUT_DREFCLK;
7252
7253 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007254 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007255}
7256
Daniel Vetter8a654f32013-06-01 17:16:22 +02007257static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007258{
7259 struct drm_device *dev = intel_crtc->base.dev;
7260 struct drm_i915_private *dev_priv = dev->dev_private;
7261 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007262 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007263 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007264 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007265 uint32_t crtc_vtotal, crtc_vblank_end;
7266 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007267
7268 /* We need to be careful not to changed the adjusted mode, for otherwise
7269 * the hw state checker will get angry at the mismatch. */
7270 crtc_vtotal = adjusted_mode->crtc_vtotal;
7271 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007272
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007273 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007274 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007275 crtc_vtotal -= 1;
7276 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007277
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007278 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007279 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7280 else
7281 vsyncshift = adjusted_mode->crtc_hsync_start -
7282 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007283 if (vsyncshift < 0)
7284 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007285 }
7286
7287 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007288 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007289
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007290 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007291 (adjusted_mode->crtc_hdisplay - 1) |
7292 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007293 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007294 (adjusted_mode->crtc_hblank_start - 1) |
7295 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007296 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007297 (adjusted_mode->crtc_hsync_start - 1) |
7298 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7299
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007300 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007301 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007302 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007303 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007304 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007305 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007306 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007307 (adjusted_mode->crtc_vsync_start - 1) |
7308 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7309
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007310 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7311 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7312 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7313 * bits. */
7314 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7315 (pipe == PIPE_B || pipe == PIPE_C))
7316 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7317
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007318 /* pipesrc controls the size that is scaled from, which should
7319 * always be the user's requested size.
7320 */
7321 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007322 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7323 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007324}
7325
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007326static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007327 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007328{
7329 struct drm_device *dev = crtc->base.dev;
7330 struct drm_i915_private *dev_priv = dev->dev_private;
7331 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7332 uint32_t tmp;
7333
7334 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007335 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7336 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007337 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007338 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7339 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007340 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007341 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7342 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007343
7344 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007345 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7346 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007347 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007348 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7349 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007350 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007351 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7352 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007353
7354 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007355 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7356 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7357 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007358 }
7359
7360 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007361 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7362 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7363
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007364 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7365 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007366}
7367
Daniel Vetterf6a83282014-02-11 15:28:57 -08007368void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007369 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007370{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007371 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7372 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7373 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7374 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007375
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007376 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7377 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7378 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7379 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007380
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007381 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007382
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007383 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7384 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007385}
7386
Daniel Vetter84b046f2013-02-19 18:48:54 +01007387static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7388{
7389 struct drm_device *dev = intel_crtc->base.dev;
7390 struct drm_i915_private *dev_priv = dev->dev_private;
7391 uint32_t pipeconf;
7392
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007393 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007394
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007395 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7396 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7397 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007398
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007399 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007400 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007401
Daniel Vetterff9ce462013-04-24 14:57:17 +02007402 /* only g4x and later have fancy bpc/dither controls */
7403 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007404 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007405 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007406 pipeconf |= PIPECONF_DITHER_EN |
7407 PIPECONF_DITHER_TYPE_SP;
7408
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007409 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007410 case 18:
7411 pipeconf |= PIPECONF_6BPC;
7412 break;
7413 case 24:
7414 pipeconf |= PIPECONF_8BPC;
7415 break;
7416 case 30:
7417 pipeconf |= PIPECONF_10BPC;
7418 break;
7419 default:
7420 /* Case prevented by intel_choose_pipe_bpp_dither. */
7421 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007422 }
7423 }
7424
7425 if (HAS_PIPE_CXSR(dev)) {
7426 if (intel_crtc->lowfreq_avail) {
7427 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7428 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7429 } else {
7430 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007431 }
7432 }
7433
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007434 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007435 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007436 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007437 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7438 else
7439 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7440 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007441 pipeconf |= PIPECONF_PROGRESSIVE;
7442
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007443 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007444 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007445
Daniel Vetter84b046f2013-02-19 18:48:54 +01007446 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7447 POSTING_READ(PIPECONF(intel_crtc->pipe));
7448}
7449
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007450static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7451 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007452{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007453 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007454 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007455 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007456 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007457 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007458 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007459 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007460 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007461 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007462 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007463 struct drm_connector_state *connector_state;
7464 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007465
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007466 memset(&crtc_state->dpll_hw_state, 0,
7467 sizeof(crtc_state->dpll_hw_state));
7468
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007469 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007470 if (connector_state->crtc != &crtc->base)
7471 continue;
7472
7473 encoder = to_intel_encoder(connector_state->best_encoder);
7474
Chris Wilson5eddb702010-09-11 13:48:45 +01007475 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007476 case INTEL_OUTPUT_LVDS:
7477 is_lvds = true;
7478 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007479 case INTEL_OUTPUT_DSI:
7480 is_dsi = true;
7481 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007482 default:
7483 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007484 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007485
Eric Anholtc751ce42010-03-25 11:48:48 -07007486 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007487 }
7488
Jani Nikulaf2335332013-09-13 11:03:09 +03007489 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007490 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007491
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007492 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007493 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007494
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007495 /*
7496 * Returns a set of divisors for the desired target clock with
7497 * the given refclk, or FALSE. The returned values represent
7498 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7499 * 2) / p1 / p2.
7500 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007501 limit = intel_limit(crtc_state, refclk);
7502 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007503 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007504 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007505 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007506 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7507 return -EINVAL;
7508 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007509
Jani Nikulaf2335332013-09-13 11:03:09 +03007510 if (is_lvds && dev_priv->lvds_downclock_avail) {
7511 /*
7512 * Ensure we match the reduced clock's P to the target
7513 * clock. If the clocks don't match, we can't switch
7514 * the display clock by using the FP0/FP1. In such case
7515 * we will disable the LVDS downclock feature.
7516 */
7517 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007518 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007519 dev_priv->lvds_downclock,
7520 refclk, &clock,
7521 &reduced_clock);
7522 }
7523 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007524 crtc_state->dpll.n = clock.n;
7525 crtc_state->dpll.m1 = clock.m1;
7526 crtc_state->dpll.m2 = clock.m2;
7527 crtc_state->dpll.p1 = clock.p1;
7528 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007529 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007530
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007531 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007532 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307533 has_reduced_clock ? &reduced_clock : NULL,
7534 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007535 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007536 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007537 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007538 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007539 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007540 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007541 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007542 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007543 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007544
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007545 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007546}
7547
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007548static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007549 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007550{
7551 struct drm_device *dev = crtc->base.dev;
7552 struct drm_i915_private *dev_priv = dev->dev_private;
7553 uint32_t tmp;
7554
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007555 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7556 return;
7557
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007558 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007559 if (!(tmp & PFIT_ENABLE))
7560 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007561
Daniel Vetter06922822013-07-11 13:35:40 +02007562 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007563 if (INTEL_INFO(dev)->gen < 4) {
7564 if (crtc->pipe != PIPE_B)
7565 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007566 } else {
7567 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7568 return;
7569 }
7570
Daniel Vetter06922822013-07-11 13:35:40 +02007571 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007572 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7573 if (INTEL_INFO(dev)->gen < 5)
7574 pipe_config->gmch_pfit.lvds_border_bits =
7575 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7576}
7577
Jesse Barnesacbec812013-09-20 11:29:32 -07007578static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007579 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007580{
7581 struct drm_device *dev = crtc->base.dev;
7582 struct drm_i915_private *dev_priv = dev->dev_private;
7583 int pipe = pipe_config->cpu_transcoder;
7584 intel_clock_t clock;
7585 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007586 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007587
Shobhit Kumarf573de52014-07-30 20:32:37 +05307588 /* In case of MIPI DPLL will not even be used */
7589 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7590 return;
7591
Jesse Barnesacbec812013-09-20 11:29:32 -07007592 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007593 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007594 mutex_unlock(&dev_priv->dpio_lock);
7595
7596 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7597 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7598 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7599 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7600 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7601
Ville Syrjäläf6466282013-10-14 14:50:31 +03007602 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007603
Ville Syrjäläf6466282013-10-14 14:50:31 +03007604 /* clock.dot is the fast clock */
7605 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007606}
7607
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007608static void
7609i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7610 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007611{
7612 struct drm_device *dev = crtc->base.dev;
7613 struct drm_i915_private *dev_priv = dev->dev_private;
7614 u32 val, base, offset;
7615 int pipe = crtc->pipe, plane = crtc->plane;
7616 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007617 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007618 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007619 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007620
Damien Lespiau42a7b082015-02-05 19:35:13 +00007621 val = I915_READ(DSPCNTR(plane));
7622 if (!(val & DISPLAY_PLANE_ENABLE))
7623 return;
7624
Damien Lespiaud9806c92015-01-21 14:07:19 +00007625 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007626 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007627 DRM_DEBUG_KMS("failed to alloc fb\n");
7628 return;
7629 }
7630
Damien Lespiau1b842c82015-01-21 13:50:54 +00007631 fb = &intel_fb->base;
7632
Daniel Vetter18c52472015-02-10 17:16:09 +00007633 if (INTEL_INFO(dev)->gen >= 4) {
7634 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007635 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007636 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7637 }
7638 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007639
7640 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007641 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007642 fb->pixel_format = fourcc;
7643 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007644
7645 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007646 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007647 offset = I915_READ(DSPTILEOFF(plane));
7648 else
7649 offset = I915_READ(DSPLINOFF(plane));
7650 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7651 } else {
7652 base = I915_READ(DSPADDR(plane));
7653 }
7654 plane_config->base = base;
7655
7656 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007657 fb->width = ((val >> 16) & 0xfff) + 1;
7658 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007659
7660 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007661 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007662
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007663 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007664 fb->pixel_format,
7665 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007666
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007667 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007668
Damien Lespiau2844a922015-01-20 12:51:48 +00007669 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7670 pipe_name(pipe), plane, fb->width, fb->height,
7671 fb->bits_per_pixel, base, fb->pitches[0],
7672 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007673
Damien Lespiau2d140302015-02-05 17:22:18 +00007674 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007675}
7676
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007677static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007678 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007679{
7680 struct drm_device *dev = crtc->base.dev;
7681 struct drm_i915_private *dev_priv = dev->dev_private;
7682 int pipe = pipe_config->cpu_transcoder;
7683 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7684 intel_clock_t clock;
7685 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7686 int refclk = 100000;
7687
7688 mutex_lock(&dev_priv->dpio_lock);
7689 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7690 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7691 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7692 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7693 mutex_unlock(&dev_priv->dpio_lock);
7694
7695 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7696 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7697 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7698 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7699 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7700
7701 chv_clock(refclk, &clock);
7702
7703 /* clock.dot is the fast clock */
7704 pipe_config->port_clock = clock.dot / 5;
7705}
7706
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007707static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007708 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007709{
7710 struct drm_device *dev = crtc->base.dev;
7711 struct drm_i915_private *dev_priv = dev->dev_private;
7712 uint32_t tmp;
7713
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007714 if (!intel_display_power_is_enabled(dev_priv,
7715 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007716 return false;
7717
Daniel Vettere143a212013-07-04 12:01:15 +02007718 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007719 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007720
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007721 tmp = I915_READ(PIPECONF(crtc->pipe));
7722 if (!(tmp & PIPECONF_ENABLE))
7723 return false;
7724
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007725 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7726 switch (tmp & PIPECONF_BPC_MASK) {
7727 case PIPECONF_6BPC:
7728 pipe_config->pipe_bpp = 18;
7729 break;
7730 case PIPECONF_8BPC:
7731 pipe_config->pipe_bpp = 24;
7732 break;
7733 case PIPECONF_10BPC:
7734 pipe_config->pipe_bpp = 30;
7735 break;
7736 default:
7737 break;
7738 }
7739 }
7740
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007741 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7742 pipe_config->limited_color_range = true;
7743
Ville Syrjälä282740f2013-09-04 18:30:03 +03007744 if (INTEL_INFO(dev)->gen < 4)
7745 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7746
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007747 intel_get_pipe_timings(crtc, pipe_config);
7748
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007749 i9xx_get_pfit_config(crtc, pipe_config);
7750
Daniel Vetter6c49f242013-06-06 12:45:25 +02007751 if (INTEL_INFO(dev)->gen >= 4) {
7752 tmp = I915_READ(DPLL_MD(crtc->pipe));
7753 pipe_config->pixel_multiplier =
7754 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7755 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007756 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007757 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7758 tmp = I915_READ(DPLL(crtc->pipe));
7759 pipe_config->pixel_multiplier =
7760 ((tmp & SDVO_MULTIPLIER_MASK)
7761 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7762 } else {
7763 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7764 * port and will be fixed up in the encoder->get_config
7765 * function. */
7766 pipe_config->pixel_multiplier = 1;
7767 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007768 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7769 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007770 /*
7771 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7772 * on 830. Filter it out here so that we don't
7773 * report errors due to that.
7774 */
7775 if (IS_I830(dev))
7776 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7777
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007778 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7779 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007780 } else {
7781 /* Mask out read-only status bits. */
7782 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7783 DPLL_PORTC_READY_MASK |
7784 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007785 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007786
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007787 if (IS_CHERRYVIEW(dev))
7788 chv_crtc_clock_get(crtc, pipe_config);
7789 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007790 vlv_crtc_clock_get(crtc, pipe_config);
7791 else
7792 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007793
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007794 return true;
7795}
7796
Paulo Zanonidde86e22012-12-01 12:04:25 -02007797static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007798{
7799 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007800 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007801 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007802 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007803 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007804 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007805 bool has_ck505 = false;
7806 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007807
7808 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007809 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007810 switch (encoder->type) {
7811 case INTEL_OUTPUT_LVDS:
7812 has_panel = true;
7813 has_lvds = true;
7814 break;
7815 case INTEL_OUTPUT_EDP:
7816 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007817 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007818 has_cpu_edp = true;
7819 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007820 default:
7821 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007822 }
7823 }
7824
Keith Packard99eb6a02011-09-26 14:29:12 -07007825 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007826 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007827 can_ssc = has_ck505;
7828 } else {
7829 has_ck505 = false;
7830 can_ssc = true;
7831 }
7832
Imre Deak2de69052013-05-08 13:14:04 +03007833 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7834 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007835
7836 /* Ironlake: try to setup display ref clock before DPLL
7837 * enabling. This is only under driver's control after
7838 * PCH B stepping, previous chipset stepping should be
7839 * ignoring this setting.
7840 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007841 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007842
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007843 /* As we must carefully and slowly disable/enable each source in turn,
7844 * compute the final state we want first and check if we need to
7845 * make any changes at all.
7846 */
7847 final = val;
7848 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007849 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007850 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007851 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007852 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7853
7854 final &= ~DREF_SSC_SOURCE_MASK;
7855 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7856 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007857
Keith Packard199e5d72011-09-22 12:01:57 -07007858 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007859 final |= DREF_SSC_SOURCE_ENABLE;
7860
7861 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7862 final |= DREF_SSC1_ENABLE;
7863
7864 if (has_cpu_edp) {
7865 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7866 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7867 else
7868 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7869 } else
7870 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7871 } else {
7872 final |= DREF_SSC_SOURCE_DISABLE;
7873 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7874 }
7875
7876 if (final == val)
7877 return;
7878
7879 /* Always enable nonspread source */
7880 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7881
7882 if (has_ck505)
7883 val |= DREF_NONSPREAD_CK505_ENABLE;
7884 else
7885 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7886
7887 if (has_panel) {
7888 val &= ~DREF_SSC_SOURCE_MASK;
7889 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007890
Keith Packard199e5d72011-09-22 12:01:57 -07007891 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007892 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007893 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007894 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007895 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007896 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007897
7898 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007899 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007900 POSTING_READ(PCH_DREF_CONTROL);
7901 udelay(200);
7902
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007903 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007904
7905 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007906 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007907 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007908 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007909 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007910 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007911 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007912 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007913 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007914
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007915 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007916 POSTING_READ(PCH_DREF_CONTROL);
7917 udelay(200);
7918 } else {
7919 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7920
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007921 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007922
7923 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007924 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007925
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007926 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007927 POSTING_READ(PCH_DREF_CONTROL);
7928 udelay(200);
7929
7930 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007931 val &= ~DREF_SSC_SOURCE_MASK;
7932 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007933
7934 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007935 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007936
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007937 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007938 POSTING_READ(PCH_DREF_CONTROL);
7939 udelay(200);
7940 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007941
7942 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007943}
7944
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007945static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007946{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007947 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007948
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007949 tmp = I915_READ(SOUTH_CHICKEN2);
7950 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7951 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007952
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007953 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7954 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7955 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007956
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007957 tmp = I915_READ(SOUTH_CHICKEN2);
7958 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7959 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007960
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007961 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7962 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7963 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007964}
7965
7966/* WaMPhyProgramming:hsw */
7967static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7968{
7969 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007970
7971 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7972 tmp &= ~(0xFF << 24);
7973 tmp |= (0x12 << 24);
7974 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7975
Paulo Zanonidde86e22012-12-01 12:04:25 -02007976 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7977 tmp |= (1 << 11);
7978 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7979
7980 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7981 tmp |= (1 << 11);
7982 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7983
Paulo Zanonidde86e22012-12-01 12:04:25 -02007984 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7985 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7986 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7987
7988 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7989 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7990 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7991
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007992 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7993 tmp &= ~(7 << 13);
7994 tmp |= (5 << 13);
7995 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007996
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007997 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7998 tmp &= ~(7 << 13);
7999 tmp |= (5 << 13);
8000 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008001
8002 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8003 tmp &= ~0xFF;
8004 tmp |= 0x1C;
8005 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8006
8007 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8008 tmp &= ~0xFF;
8009 tmp |= 0x1C;
8010 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8011
8012 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8013 tmp &= ~(0xFF << 16);
8014 tmp |= (0x1C << 16);
8015 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8016
8017 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8018 tmp &= ~(0xFF << 16);
8019 tmp |= (0x1C << 16);
8020 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8021
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008022 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8023 tmp |= (1 << 27);
8024 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008025
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008026 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8027 tmp |= (1 << 27);
8028 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008029
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008030 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8031 tmp &= ~(0xF << 28);
8032 tmp |= (4 << 28);
8033 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008034
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008035 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8036 tmp &= ~(0xF << 28);
8037 tmp |= (4 << 28);
8038 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008039}
8040
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008041/* Implements 3 different sequences from BSpec chapter "Display iCLK
8042 * Programming" based on the parameters passed:
8043 * - Sequence to enable CLKOUT_DP
8044 * - Sequence to enable CLKOUT_DP without spread
8045 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8046 */
8047static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8048 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008049{
8050 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008051 uint32_t reg, tmp;
8052
8053 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8054 with_spread = true;
8055 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8056 with_fdi, "LP PCH doesn't have FDI\n"))
8057 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008058
8059 mutex_lock(&dev_priv->dpio_lock);
8060
8061 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8062 tmp &= ~SBI_SSCCTL_DISABLE;
8063 tmp |= SBI_SSCCTL_PATHALT;
8064 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8065
8066 udelay(24);
8067
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008068 if (with_spread) {
8069 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8070 tmp &= ~SBI_SSCCTL_PATHALT;
8071 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008072
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008073 if (with_fdi) {
8074 lpt_reset_fdi_mphy(dev_priv);
8075 lpt_program_fdi_mphy(dev_priv);
8076 }
8077 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008078
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008079 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8080 SBI_GEN0 : SBI_DBUFF0;
8081 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8082 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8083 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008084
8085 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008086}
8087
Paulo Zanoni47701c32013-07-23 11:19:25 -03008088/* Sequence to disable CLKOUT_DP */
8089static void lpt_disable_clkout_dp(struct drm_device *dev)
8090{
8091 struct drm_i915_private *dev_priv = dev->dev_private;
8092 uint32_t reg, tmp;
8093
8094 mutex_lock(&dev_priv->dpio_lock);
8095
8096 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8097 SBI_GEN0 : SBI_DBUFF0;
8098 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8099 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8100 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8101
8102 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8103 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8104 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8105 tmp |= SBI_SSCCTL_PATHALT;
8106 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8107 udelay(32);
8108 }
8109 tmp |= SBI_SSCCTL_DISABLE;
8110 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8111 }
8112
8113 mutex_unlock(&dev_priv->dpio_lock);
8114}
8115
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008116static void lpt_init_pch_refclk(struct drm_device *dev)
8117{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008118 struct intel_encoder *encoder;
8119 bool has_vga = false;
8120
Damien Lespiaub2784e12014-08-05 11:29:37 +01008121 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008122 switch (encoder->type) {
8123 case INTEL_OUTPUT_ANALOG:
8124 has_vga = true;
8125 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008126 default:
8127 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008128 }
8129 }
8130
Paulo Zanoni47701c32013-07-23 11:19:25 -03008131 if (has_vga)
8132 lpt_enable_clkout_dp(dev, true, true);
8133 else
8134 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008135}
8136
Paulo Zanonidde86e22012-12-01 12:04:25 -02008137/*
8138 * Initialize reference clocks when the driver loads
8139 */
8140void intel_init_pch_refclk(struct drm_device *dev)
8141{
8142 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8143 ironlake_init_pch_refclk(dev);
8144 else if (HAS_PCH_LPT(dev))
8145 lpt_init_pch_refclk(dev);
8146}
8147
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008148static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008149{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008150 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008151 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008152 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008153 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008154 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008155 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008156 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008157 bool is_lvds = false;
8158
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008159 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008160 if (connector_state->crtc != crtc_state->base.crtc)
8161 continue;
8162
8163 encoder = to_intel_encoder(connector_state->best_encoder);
8164
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008165 switch (encoder->type) {
8166 case INTEL_OUTPUT_LVDS:
8167 is_lvds = true;
8168 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008169 default:
8170 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008171 }
8172 num_connectors++;
8173 }
8174
8175 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008176 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008177 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008178 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008179 }
8180
8181 return 120000;
8182}
8183
Daniel Vetter6ff93602013-04-19 11:24:36 +02008184static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008185{
8186 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8188 int pipe = intel_crtc->pipe;
8189 uint32_t val;
8190
Daniel Vetter78114072013-06-13 00:54:57 +02008191 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008192
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008193 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008194 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008195 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008196 break;
8197 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008198 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008199 break;
8200 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008201 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008202 break;
8203 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008204 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008205 break;
8206 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008207 /* Case prevented by intel_choose_pipe_bpp_dither. */
8208 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008209 }
8210
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008211 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008212 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8213
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008214 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008215 val |= PIPECONF_INTERLACED_ILK;
8216 else
8217 val |= PIPECONF_PROGRESSIVE;
8218
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008219 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008220 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008221
Paulo Zanonic8203562012-09-12 10:06:29 -03008222 I915_WRITE(PIPECONF(pipe), val);
8223 POSTING_READ(PIPECONF(pipe));
8224}
8225
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008226/*
8227 * Set up the pipe CSC unit.
8228 *
8229 * Currently only full range RGB to limited range RGB conversion
8230 * is supported, but eventually this should handle various
8231 * RGB<->YCbCr scenarios as well.
8232 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008233static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008234{
8235 struct drm_device *dev = crtc->dev;
8236 struct drm_i915_private *dev_priv = dev->dev_private;
8237 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8238 int pipe = intel_crtc->pipe;
8239 uint16_t coeff = 0x7800; /* 1.0 */
8240
8241 /*
8242 * TODO: Check what kind of values actually come out of the pipe
8243 * with these coeff/postoff values and adjust to get the best
8244 * accuracy. Perhaps we even need to take the bpc value into
8245 * consideration.
8246 */
8247
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008248 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008249 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8250
8251 /*
8252 * GY/GU and RY/RU should be the other way around according
8253 * to BSpec, but reality doesn't agree. Just set them up in
8254 * a way that results in the correct picture.
8255 */
8256 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8257 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8258
8259 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8260 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8261
8262 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8263 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8264
8265 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8266 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8267 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8268
8269 if (INTEL_INFO(dev)->gen > 6) {
8270 uint16_t postoff = 0;
8271
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008272 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008273 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008274
8275 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8276 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8277 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8278
8279 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8280 } else {
8281 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8282
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008283 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008284 mode |= CSC_BLACK_SCREEN_OFFSET;
8285
8286 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8287 }
8288}
8289
Daniel Vetter6ff93602013-04-19 11:24:36 +02008290static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008291{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008292 struct drm_device *dev = crtc->dev;
8293 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008294 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008295 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008296 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008297 uint32_t val;
8298
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008299 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008300
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008301 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008302 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8303
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008304 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008305 val |= PIPECONF_INTERLACED_ILK;
8306 else
8307 val |= PIPECONF_PROGRESSIVE;
8308
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008309 I915_WRITE(PIPECONF(cpu_transcoder), val);
8310 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008311
8312 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8313 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008314
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308315 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008316 val = 0;
8317
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008318 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008319 case 18:
8320 val |= PIPEMISC_DITHER_6_BPC;
8321 break;
8322 case 24:
8323 val |= PIPEMISC_DITHER_8_BPC;
8324 break;
8325 case 30:
8326 val |= PIPEMISC_DITHER_10_BPC;
8327 break;
8328 case 36:
8329 val |= PIPEMISC_DITHER_12_BPC;
8330 break;
8331 default:
8332 /* Case prevented by pipe_config_set_bpp. */
8333 BUG();
8334 }
8335
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008336 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008337 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8338
8339 I915_WRITE(PIPEMISC(pipe), val);
8340 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008341}
8342
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008343static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008344 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008345 intel_clock_t *clock,
8346 bool *has_reduced_clock,
8347 intel_clock_t *reduced_clock)
8348{
8349 struct drm_device *dev = crtc->dev;
8350 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008351 int refclk;
8352 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008353 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008354
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008355 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008356
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008357 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008358
8359 /*
8360 * Returns a set of divisors for the desired target clock with the given
8361 * refclk, or FALSE. The returned values represent the clock equation:
8362 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8363 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008364 limit = intel_limit(crtc_state, refclk);
8365 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008366 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008367 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008368 if (!ret)
8369 return false;
8370
8371 if (is_lvds && dev_priv->lvds_downclock_avail) {
8372 /*
8373 * Ensure we match the reduced clock's P to the target clock.
8374 * If the clocks don't match, we can't switch the display clock
8375 * by using the FP0/FP1. In such case we will disable the LVDS
8376 * downclock feature.
8377 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008378 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008379 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008380 dev_priv->lvds_downclock,
8381 refclk, clock,
8382 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008383 }
8384
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008385 return true;
8386}
8387
Paulo Zanonid4b19312012-11-29 11:29:32 -02008388int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8389{
8390 /*
8391 * Account for spread spectrum to avoid
8392 * oversubscribing the link. Max center spread
8393 * is 2.5%; use 5% for safety's sake.
8394 */
8395 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008396 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008397}
8398
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008399static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008400{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008401 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008402}
8403
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008404static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008405 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008406 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008407 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008408{
8409 struct drm_crtc *crtc = &intel_crtc->base;
8410 struct drm_device *dev = crtc->dev;
8411 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008412 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008413 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008414 struct drm_connector_state *connector_state;
8415 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008416 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008417 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008418 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008419
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008420 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008421 if (connector_state->crtc != crtc_state->base.crtc)
8422 continue;
8423
8424 encoder = to_intel_encoder(connector_state->best_encoder);
8425
8426 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008427 case INTEL_OUTPUT_LVDS:
8428 is_lvds = true;
8429 break;
8430 case INTEL_OUTPUT_SDVO:
8431 case INTEL_OUTPUT_HDMI:
8432 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008433 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008434 default:
8435 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008436 }
8437
8438 num_connectors++;
8439 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008440
Chris Wilsonc1858122010-12-03 21:35:48 +00008441 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008442 factor = 21;
8443 if (is_lvds) {
8444 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008445 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008446 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008447 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008448 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008449 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008450
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008451 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008452 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008453
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008454 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8455 *fp2 |= FP_CB_TUNE;
8456
Chris Wilson5eddb702010-09-11 13:48:45 +01008457 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008458
Eric Anholta07d6782011-03-30 13:01:08 -07008459 if (is_lvds)
8460 dpll |= DPLLB_MODE_LVDS;
8461 else
8462 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008463
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008464 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008465 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008466
8467 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008468 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008469 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008470 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008471
Eric Anholta07d6782011-03-30 13:01:08 -07008472 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008473 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008474 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008475 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008476
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008477 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008478 case 5:
8479 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8480 break;
8481 case 7:
8482 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8483 break;
8484 case 10:
8485 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8486 break;
8487 case 14:
8488 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8489 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008490 }
8491
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008492 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008493 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008494 else
8495 dpll |= PLL_REF_INPUT_DREFCLK;
8496
Daniel Vetter959e16d2013-06-05 13:34:21 +02008497 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008498}
8499
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008500static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8501 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008502{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008503 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008504 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008505 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008506 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008507 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008508 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008509
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008510 memset(&crtc_state->dpll_hw_state, 0,
8511 sizeof(crtc_state->dpll_hw_state));
8512
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008513 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008514
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008515 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8516 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8517
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008518 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008519 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008520 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008521 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8522 return -EINVAL;
8523 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008524 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008525 if (!crtc_state->clock_set) {
8526 crtc_state->dpll.n = clock.n;
8527 crtc_state->dpll.m1 = clock.m1;
8528 crtc_state->dpll.m2 = clock.m2;
8529 crtc_state->dpll.p1 = clock.p1;
8530 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008531 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008532
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008533 /* 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 +02008534 if (crtc_state->has_pch_encoder) {
8535 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008536 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008537 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008538
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008539 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008540 &fp, &reduced_clock,
8541 has_reduced_clock ? &fp2 : NULL);
8542
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008543 crtc_state->dpll_hw_state.dpll = dpll;
8544 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008545 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008546 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008547 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008548 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008549
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008550 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008551 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008552 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008553 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008554 return -EINVAL;
8555 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008556 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008557
Rodrigo Viviab585de2015-03-24 12:40:09 -07008558 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008559 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008560 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008561 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008562
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008563 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008564}
8565
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008566static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8567 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008568{
8569 struct drm_device *dev = crtc->base.dev;
8570 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008571 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008572
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008573 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8574 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8575 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8576 & ~TU_SIZE_MASK;
8577 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8578 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8579 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8580}
8581
8582static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8583 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008584 struct intel_link_m_n *m_n,
8585 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008586{
8587 struct drm_device *dev = crtc->base.dev;
8588 struct drm_i915_private *dev_priv = dev->dev_private;
8589 enum pipe pipe = crtc->pipe;
8590
8591 if (INTEL_INFO(dev)->gen >= 5) {
8592 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8593 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8594 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8595 & ~TU_SIZE_MASK;
8596 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8597 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8598 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008599 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8600 * gen < 8) and if DRRS is supported (to make sure the
8601 * registers are not unnecessarily read).
8602 */
8603 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008604 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008605 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8606 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8607 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8608 & ~TU_SIZE_MASK;
8609 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8610 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8611 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8612 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008613 } else {
8614 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8615 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8616 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8617 & ~TU_SIZE_MASK;
8618 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8619 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8620 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8621 }
8622}
8623
8624void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008625 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008626{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008627 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008628 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8629 else
8630 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008631 &pipe_config->dp_m_n,
8632 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008633}
8634
Daniel Vetter72419202013-04-04 13:28:53 +02008635static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008636 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008637{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008638 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008639 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008640}
8641
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008642static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008643 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008644{
8645 struct drm_device *dev = crtc->base.dev;
8646 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008647 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8648 uint32_t ps_ctrl = 0;
8649 int id = -1;
8650 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008651
Chandra Kondurua1b22782015-04-07 15:28:45 -07008652 /* find scaler attached to this pipe */
8653 for (i = 0; i < crtc->num_scalers; i++) {
8654 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8655 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8656 id = i;
8657 pipe_config->pch_pfit.enabled = true;
8658 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8659 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8660 break;
8661 }
8662 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008663
Chandra Kondurua1b22782015-04-07 15:28:45 -07008664 scaler_state->scaler_id = id;
8665 if (id >= 0) {
8666 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8667 } else {
8668 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008669 }
8670}
8671
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008672static void
8673skylake_get_initial_plane_config(struct intel_crtc *crtc,
8674 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008675{
8676 struct drm_device *dev = crtc->base.dev;
8677 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008678 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008679 int pipe = crtc->pipe;
8680 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008681 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008682 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008683 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008684
Damien Lespiaud9806c92015-01-21 14:07:19 +00008685 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008686 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008687 DRM_DEBUG_KMS("failed to alloc fb\n");
8688 return;
8689 }
8690
Damien Lespiau1b842c82015-01-21 13:50:54 +00008691 fb = &intel_fb->base;
8692
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008693 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008694 if (!(val & PLANE_CTL_ENABLE))
8695 goto error;
8696
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008697 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8698 fourcc = skl_format_to_fourcc(pixel_format,
8699 val & PLANE_CTL_ORDER_RGBX,
8700 val & PLANE_CTL_ALPHA_MASK);
8701 fb->pixel_format = fourcc;
8702 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8703
Damien Lespiau40f46282015-02-27 11:15:21 +00008704 tiling = val & PLANE_CTL_TILED_MASK;
8705 switch (tiling) {
8706 case PLANE_CTL_TILED_LINEAR:
8707 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8708 break;
8709 case PLANE_CTL_TILED_X:
8710 plane_config->tiling = I915_TILING_X;
8711 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8712 break;
8713 case PLANE_CTL_TILED_Y:
8714 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8715 break;
8716 case PLANE_CTL_TILED_YF:
8717 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8718 break;
8719 default:
8720 MISSING_CASE(tiling);
8721 goto error;
8722 }
8723
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008724 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8725 plane_config->base = base;
8726
8727 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8728
8729 val = I915_READ(PLANE_SIZE(pipe, 0));
8730 fb->height = ((val >> 16) & 0xfff) + 1;
8731 fb->width = ((val >> 0) & 0x1fff) + 1;
8732
8733 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008734 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8735 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008736 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8737
8738 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008739 fb->pixel_format,
8740 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008741
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008742 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008743
8744 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8745 pipe_name(pipe), fb->width, fb->height,
8746 fb->bits_per_pixel, base, fb->pitches[0],
8747 plane_config->size);
8748
Damien Lespiau2d140302015-02-05 17:22:18 +00008749 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008750 return;
8751
8752error:
8753 kfree(fb);
8754}
8755
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008756static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008757 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008758{
8759 struct drm_device *dev = crtc->base.dev;
8760 struct drm_i915_private *dev_priv = dev->dev_private;
8761 uint32_t tmp;
8762
8763 tmp = I915_READ(PF_CTL(crtc->pipe));
8764
8765 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008766 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008767 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8768 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008769
8770 /* We currently do not free assignements of panel fitters on
8771 * ivb/hsw (since we don't use the higher upscaling modes which
8772 * differentiates them) so just WARN about this case for now. */
8773 if (IS_GEN7(dev)) {
8774 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8775 PF_PIPE_SEL_IVB(crtc->pipe));
8776 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008777 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008778}
8779
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008780static void
8781ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8782 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008783{
8784 struct drm_device *dev = crtc->base.dev;
8785 struct drm_i915_private *dev_priv = dev->dev_private;
8786 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008787 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008788 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008789 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008790 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008791 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008792
Damien Lespiau42a7b082015-02-05 19:35:13 +00008793 val = I915_READ(DSPCNTR(pipe));
8794 if (!(val & DISPLAY_PLANE_ENABLE))
8795 return;
8796
Damien Lespiaud9806c92015-01-21 14:07:19 +00008797 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008798 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008799 DRM_DEBUG_KMS("failed to alloc fb\n");
8800 return;
8801 }
8802
Damien Lespiau1b842c82015-01-21 13:50:54 +00008803 fb = &intel_fb->base;
8804
Daniel Vetter18c52472015-02-10 17:16:09 +00008805 if (INTEL_INFO(dev)->gen >= 4) {
8806 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008807 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008808 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8809 }
8810 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008811
8812 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008813 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008814 fb->pixel_format = fourcc;
8815 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008816
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008817 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008818 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008819 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008820 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008821 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008822 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008823 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008824 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008825 }
8826 plane_config->base = base;
8827
8828 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008829 fb->width = ((val >> 16) & 0xfff) + 1;
8830 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008831
8832 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008833 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008834
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008835 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008836 fb->pixel_format,
8837 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008838
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008839 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008840
Damien Lespiau2844a922015-01-20 12:51:48 +00008841 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8842 pipe_name(pipe), fb->width, fb->height,
8843 fb->bits_per_pixel, base, fb->pitches[0],
8844 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008845
Damien Lespiau2d140302015-02-05 17:22:18 +00008846 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008847}
8848
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008849static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008850 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008851{
8852 struct drm_device *dev = crtc->base.dev;
8853 struct drm_i915_private *dev_priv = dev->dev_private;
8854 uint32_t tmp;
8855
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008856 if (!intel_display_power_is_enabled(dev_priv,
8857 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008858 return false;
8859
Daniel Vettere143a212013-07-04 12:01:15 +02008860 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008861 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008862
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008863 tmp = I915_READ(PIPECONF(crtc->pipe));
8864 if (!(tmp & PIPECONF_ENABLE))
8865 return false;
8866
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008867 switch (tmp & PIPECONF_BPC_MASK) {
8868 case PIPECONF_6BPC:
8869 pipe_config->pipe_bpp = 18;
8870 break;
8871 case PIPECONF_8BPC:
8872 pipe_config->pipe_bpp = 24;
8873 break;
8874 case PIPECONF_10BPC:
8875 pipe_config->pipe_bpp = 30;
8876 break;
8877 case PIPECONF_12BPC:
8878 pipe_config->pipe_bpp = 36;
8879 break;
8880 default:
8881 break;
8882 }
8883
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008884 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8885 pipe_config->limited_color_range = true;
8886
Daniel Vetterab9412b2013-05-03 11:49:46 +02008887 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008888 struct intel_shared_dpll *pll;
8889
Daniel Vetter88adfff2013-03-28 10:42:01 +01008890 pipe_config->has_pch_encoder = true;
8891
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008892 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8893 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8894 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008895
8896 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008897
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008898 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008899 pipe_config->shared_dpll =
8900 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008901 } else {
8902 tmp = I915_READ(PCH_DPLL_SEL);
8903 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8904 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8905 else
8906 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8907 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008908
8909 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8910
8911 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8912 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008913
8914 tmp = pipe_config->dpll_hw_state.dpll;
8915 pipe_config->pixel_multiplier =
8916 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8917 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008918
8919 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008920 } else {
8921 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008922 }
8923
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008924 intel_get_pipe_timings(crtc, pipe_config);
8925
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008926 ironlake_get_pfit_config(crtc, pipe_config);
8927
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008928 return true;
8929}
8930
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008931static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8932{
8933 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008934 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008935
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008936 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008937 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008938 pipe_name(crtc->pipe));
8939
Rob Clarke2c719b2014-12-15 13:56:32 -05008940 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8941 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8942 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8943 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8944 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8945 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008946 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008947 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008948 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008949 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008950 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008951 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008952 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008953 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008954 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008955
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008956 /*
8957 * In theory we can still leave IRQs enabled, as long as only the HPD
8958 * interrupts remain enabled. We used to check for that, but since it's
8959 * gen-specific and since we only disable LCPLL after we fully disable
8960 * the interrupts, the check below should be enough.
8961 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008962 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008963}
8964
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008965static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8966{
8967 struct drm_device *dev = dev_priv->dev;
8968
8969 if (IS_HASWELL(dev))
8970 return I915_READ(D_COMP_HSW);
8971 else
8972 return I915_READ(D_COMP_BDW);
8973}
8974
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008975static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8976{
8977 struct drm_device *dev = dev_priv->dev;
8978
8979 if (IS_HASWELL(dev)) {
8980 mutex_lock(&dev_priv->rps.hw_lock);
8981 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8982 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008983 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008984 mutex_unlock(&dev_priv->rps.hw_lock);
8985 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008986 I915_WRITE(D_COMP_BDW, val);
8987 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008988 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008989}
8990
8991/*
8992 * This function implements pieces of two sequences from BSpec:
8993 * - Sequence for display software to disable LCPLL
8994 * - Sequence for display software to allow package C8+
8995 * The steps implemented here are just the steps that actually touch the LCPLL
8996 * register. Callers should take care of disabling all the display engine
8997 * functions, doing the mode unset, fixing interrupts, etc.
8998 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008999static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9000 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009001{
9002 uint32_t val;
9003
9004 assert_can_disable_lcpll(dev_priv);
9005
9006 val = I915_READ(LCPLL_CTL);
9007
9008 if (switch_to_fclk) {
9009 val |= LCPLL_CD_SOURCE_FCLK;
9010 I915_WRITE(LCPLL_CTL, val);
9011
9012 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9013 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9014 DRM_ERROR("Switching to FCLK failed\n");
9015
9016 val = I915_READ(LCPLL_CTL);
9017 }
9018
9019 val |= LCPLL_PLL_DISABLE;
9020 I915_WRITE(LCPLL_CTL, val);
9021 POSTING_READ(LCPLL_CTL);
9022
9023 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9024 DRM_ERROR("LCPLL still locked\n");
9025
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009026 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009027 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009028 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009029 ndelay(100);
9030
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009031 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9032 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009033 DRM_ERROR("D_COMP RCOMP still in progress\n");
9034
9035 if (allow_power_down) {
9036 val = I915_READ(LCPLL_CTL);
9037 val |= LCPLL_POWER_DOWN_ALLOW;
9038 I915_WRITE(LCPLL_CTL, val);
9039 POSTING_READ(LCPLL_CTL);
9040 }
9041}
9042
9043/*
9044 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9045 * source.
9046 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009047static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009048{
9049 uint32_t val;
9050
9051 val = I915_READ(LCPLL_CTL);
9052
9053 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9054 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9055 return;
9056
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009057 /*
9058 * Make sure we're not on PC8 state before disabling PC8, otherwise
9059 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009060 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009061 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009062
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009063 if (val & LCPLL_POWER_DOWN_ALLOW) {
9064 val &= ~LCPLL_POWER_DOWN_ALLOW;
9065 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009066 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009067 }
9068
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009069 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009070 val |= D_COMP_COMP_FORCE;
9071 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009072 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009073
9074 val = I915_READ(LCPLL_CTL);
9075 val &= ~LCPLL_PLL_DISABLE;
9076 I915_WRITE(LCPLL_CTL, val);
9077
9078 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9079 DRM_ERROR("LCPLL not locked yet\n");
9080
9081 if (val & LCPLL_CD_SOURCE_FCLK) {
9082 val = I915_READ(LCPLL_CTL);
9083 val &= ~LCPLL_CD_SOURCE_FCLK;
9084 I915_WRITE(LCPLL_CTL, val);
9085
9086 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9087 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9088 DRM_ERROR("Switching back to LCPLL failed\n");
9089 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009090
Mika Kuoppala59bad942015-01-16 11:34:40 +02009091 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009092}
9093
Paulo Zanoni765dab672014-03-07 20:08:18 -03009094/*
9095 * Package states C8 and deeper are really deep PC states that can only be
9096 * reached when all the devices on the system allow it, so even if the graphics
9097 * device allows PC8+, it doesn't mean the system will actually get to these
9098 * states. Our driver only allows PC8+ when going into runtime PM.
9099 *
9100 * The requirements for PC8+ are that all the outputs are disabled, the power
9101 * well is disabled and most interrupts are disabled, and these are also
9102 * requirements for runtime PM. When these conditions are met, we manually do
9103 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9104 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9105 * hang the machine.
9106 *
9107 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9108 * the state of some registers, so when we come back from PC8+ we need to
9109 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9110 * need to take care of the registers kept by RC6. Notice that this happens even
9111 * if we don't put the device in PCI D3 state (which is what currently happens
9112 * because of the runtime PM support).
9113 *
9114 * For more, read "Display Sequences for Package C8" on the hardware
9115 * documentation.
9116 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009117void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009118{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009119 struct drm_device *dev = dev_priv->dev;
9120 uint32_t val;
9121
Paulo Zanonic67a4702013-08-19 13:18:09 -03009122 DRM_DEBUG_KMS("Enabling package C8+\n");
9123
Paulo Zanonic67a4702013-08-19 13:18:09 -03009124 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9125 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9126 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9127 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9128 }
9129
9130 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009131 hsw_disable_lcpll(dev_priv, true, true);
9132}
9133
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009134void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009135{
9136 struct drm_device *dev = dev_priv->dev;
9137 uint32_t val;
9138
Paulo Zanonic67a4702013-08-19 13:18:09 -03009139 DRM_DEBUG_KMS("Disabling package C8+\n");
9140
9141 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009142 lpt_init_pch_refclk(dev);
9143
9144 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9145 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9146 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9147 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9148 }
9149
9150 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009151}
9152
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009153static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309154{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009155 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309156 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009157 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309158 int req_cdclk;
9159
9160 /* see the comment in valleyview_modeset_global_resources */
9161 if (WARN_ON(max_pixclk < 0))
9162 return;
9163
9164 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9165
9166 if (req_cdclk != dev_priv->cdclk_freq)
9167 broxton_set_cdclk(dev, req_cdclk);
9168}
9169
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009170static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9171 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009172{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009173 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009174 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009175
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009176 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009177
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009178 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009179}
9180
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309181static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9182 enum port port,
9183 struct intel_crtc_state *pipe_config)
9184{
9185 switch (port) {
9186 case PORT_A:
9187 pipe_config->ddi_pll_sel = SKL_DPLL0;
9188 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9189 break;
9190 case PORT_B:
9191 pipe_config->ddi_pll_sel = SKL_DPLL1;
9192 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9193 break;
9194 case PORT_C:
9195 pipe_config->ddi_pll_sel = SKL_DPLL2;
9196 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9197 break;
9198 default:
9199 DRM_ERROR("Incorrect port type\n");
9200 }
9201}
9202
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009203static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9204 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009205 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009206{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009207 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009208
9209 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9210 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9211
9212 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009213 case SKL_DPLL0:
9214 /*
9215 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9216 * of the shared DPLL framework and thus needs to be read out
9217 * separately
9218 */
9219 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9220 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9221 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009222 case SKL_DPLL1:
9223 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9224 break;
9225 case SKL_DPLL2:
9226 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9227 break;
9228 case SKL_DPLL3:
9229 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9230 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009231 }
9232}
9233
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009234static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9235 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009236 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009237{
9238 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9239
9240 switch (pipe_config->ddi_pll_sel) {
9241 case PORT_CLK_SEL_WRPLL1:
9242 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9243 break;
9244 case PORT_CLK_SEL_WRPLL2:
9245 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9246 break;
9247 }
9248}
9249
Daniel Vetter26804af2014-06-25 22:01:55 +03009250static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009251 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009252{
9253 struct drm_device *dev = crtc->base.dev;
9254 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009255 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009256 enum port port;
9257 uint32_t tmp;
9258
9259 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9260
9261 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9262
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009263 if (IS_SKYLAKE(dev))
9264 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309265 else if (IS_BROXTON(dev))
9266 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009267 else
9268 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009269
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009270 if (pipe_config->shared_dpll >= 0) {
9271 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9272
9273 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9274 &pipe_config->dpll_hw_state));
9275 }
9276
Daniel Vetter26804af2014-06-25 22:01:55 +03009277 /*
9278 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9279 * DDI E. So just check whether this pipe is wired to DDI E and whether
9280 * the PCH transcoder is on.
9281 */
Damien Lespiauca370452013-12-03 13:56:24 +00009282 if (INTEL_INFO(dev)->gen < 9 &&
9283 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009284 pipe_config->has_pch_encoder = true;
9285
9286 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9287 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9288 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9289
9290 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9291 }
9292}
9293
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009294static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009295 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009296{
9297 struct drm_device *dev = crtc->base.dev;
9298 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009299 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009300 uint32_t tmp;
9301
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009302 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009303 POWER_DOMAIN_PIPE(crtc->pipe)))
9304 return false;
9305
Daniel Vettere143a212013-07-04 12:01:15 +02009306 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009307 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9308
Daniel Vettereccb1402013-05-22 00:50:22 +02009309 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9310 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9311 enum pipe trans_edp_pipe;
9312 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9313 default:
9314 WARN(1, "unknown pipe linked to edp transcoder\n");
9315 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9316 case TRANS_DDI_EDP_INPUT_A_ON:
9317 trans_edp_pipe = PIPE_A;
9318 break;
9319 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9320 trans_edp_pipe = PIPE_B;
9321 break;
9322 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9323 trans_edp_pipe = PIPE_C;
9324 break;
9325 }
9326
9327 if (trans_edp_pipe == crtc->pipe)
9328 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9329 }
9330
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009331 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009332 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009333 return false;
9334
Daniel Vettereccb1402013-05-22 00:50:22 +02009335 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009336 if (!(tmp & PIPECONF_ENABLE))
9337 return false;
9338
Daniel Vetter26804af2014-06-25 22:01:55 +03009339 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009340
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009341 intel_get_pipe_timings(crtc, pipe_config);
9342
Chandra Kondurua1b22782015-04-07 15:28:45 -07009343 if (INTEL_INFO(dev)->gen >= 9) {
9344 skl_init_scalers(dev, crtc, pipe_config);
9345 }
9346
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009347 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009348
9349 if (INTEL_INFO(dev)->gen >= 9) {
9350 pipe_config->scaler_state.scaler_id = -1;
9351 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9352 }
9353
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009354 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009355 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009356 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009357 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009358 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009359 else
9360 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009361 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009362
Jesse Barnese59150d2014-01-07 13:30:45 -08009363 if (IS_HASWELL(dev))
9364 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9365 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009366
Clint Taylorebb69c92014-09-30 10:30:22 -07009367 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9368 pipe_config->pixel_multiplier =
9369 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9370 } else {
9371 pipe_config->pixel_multiplier = 1;
9372 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009373
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009374 return true;
9375}
9376
Chris Wilson560b85b2010-08-07 11:01:38 +01009377static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9378{
9379 struct drm_device *dev = crtc->dev;
9380 struct drm_i915_private *dev_priv = dev->dev_private;
9381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009382 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009383
Ville Syrjälädc41c152014-08-13 11:57:05 +03009384 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009385 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9386 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009387 unsigned int stride = roundup_pow_of_two(width) * 4;
9388
9389 switch (stride) {
9390 default:
9391 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9392 width, stride);
9393 stride = 256;
9394 /* fallthrough */
9395 case 256:
9396 case 512:
9397 case 1024:
9398 case 2048:
9399 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009400 }
9401
Ville Syrjälädc41c152014-08-13 11:57:05 +03009402 cntl |= CURSOR_ENABLE |
9403 CURSOR_GAMMA_ENABLE |
9404 CURSOR_FORMAT_ARGB |
9405 CURSOR_STRIDE(stride);
9406
9407 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009408 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009409
Ville Syrjälädc41c152014-08-13 11:57:05 +03009410 if (intel_crtc->cursor_cntl != 0 &&
9411 (intel_crtc->cursor_base != base ||
9412 intel_crtc->cursor_size != size ||
9413 intel_crtc->cursor_cntl != cntl)) {
9414 /* On these chipsets we can only modify the base/size/stride
9415 * whilst the cursor is disabled.
9416 */
9417 I915_WRITE(_CURACNTR, 0);
9418 POSTING_READ(_CURACNTR);
9419 intel_crtc->cursor_cntl = 0;
9420 }
9421
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009422 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009423 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009424 intel_crtc->cursor_base = base;
9425 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009426
9427 if (intel_crtc->cursor_size != size) {
9428 I915_WRITE(CURSIZE, size);
9429 intel_crtc->cursor_size = size;
9430 }
9431
Chris Wilson4b0e3332014-05-30 16:35:26 +03009432 if (intel_crtc->cursor_cntl != cntl) {
9433 I915_WRITE(_CURACNTR, cntl);
9434 POSTING_READ(_CURACNTR);
9435 intel_crtc->cursor_cntl = cntl;
9436 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009437}
9438
9439static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9440{
9441 struct drm_device *dev = crtc->dev;
9442 struct drm_i915_private *dev_priv = dev->dev_private;
9443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9444 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009445 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009446
Chris Wilson4b0e3332014-05-30 16:35:26 +03009447 cntl = 0;
9448 if (base) {
9449 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009450 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309451 case 64:
9452 cntl |= CURSOR_MODE_64_ARGB_AX;
9453 break;
9454 case 128:
9455 cntl |= CURSOR_MODE_128_ARGB_AX;
9456 break;
9457 case 256:
9458 cntl |= CURSOR_MODE_256_ARGB_AX;
9459 break;
9460 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009461 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309462 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009463 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009464 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009465
9466 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9467 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009468 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009469
Matt Roper8e7d6882015-01-21 16:35:41 -08009470 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009471 cntl |= CURSOR_ROTATE_180;
9472
Chris Wilson4b0e3332014-05-30 16:35:26 +03009473 if (intel_crtc->cursor_cntl != cntl) {
9474 I915_WRITE(CURCNTR(pipe), cntl);
9475 POSTING_READ(CURCNTR(pipe));
9476 intel_crtc->cursor_cntl = cntl;
9477 }
9478
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009479 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009480 I915_WRITE(CURBASE(pipe), base);
9481 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009482
9483 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009484}
9485
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009486/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009487static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9488 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009489{
9490 struct drm_device *dev = crtc->dev;
9491 struct drm_i915_private *dev_priv = dev->dev_private;
9492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9493 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009494 int x = crtc->cursor_x;
9495 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009496 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009497
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009498 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009499 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009500
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009501 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009502 base = 0;
9503
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009504 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009505 base = 0;
9506
9507 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009508 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009509 base = 0;
9510
9511 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9512 x = -x;
9513 }
9514 pos |= x << CURSOR_X_SHIFT;
9515
9516 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009517 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009518 base = 0;
9519
9520 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9521 y = -y;
9522 }
9523 pos |= y << CURSOR_Y_SHIFT;
9524
Chris Wilson4b0e3332014-05-30 16:35:26 +03009525 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009526 return;
9527
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009528 I915_WRITE(CURPOS(pipe), pos);
9529
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009530 /* ILK+ do this automagically */
9531 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009532 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009533 base += (intel_crtc->base.cursor->state->crtc_h *
9534 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009535 }
9536
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009537 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009538 i845_update_cursor(crtc, base);
9539 else
9540 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009541}
9542
Ville Syrjälädc41c152014-08-13 11:57:05 +03009543static bool cursor_size_ok(struct drm_device *dev,
9544 uint32_t width, uint32_t height)
9545{
9546 if (width == 0 || height == 0)
9547 return false;
9548
9549 /*
9550 * 845g/865g are special in that they are only limited by
9551 * the width of their cursors, the height is arbitrary up to
9552 * the precision of the register. Everything else requires
9553 * square cursors, limited to a few power-of-two sizes.
9554 */
9555 if (IS_845G(dev) || IS_I865G(dev)) {
9556 if ((width & 63) != 0)
9557 return false;
9558
9559 if (width > (IS_845G(dev) ? 64 : 512))
9560 return false;
9561
9562 if (height > 1023)
9563 return false;
9564 } else {
9565 switch (width | height) {
9566 case 256:
9567 case 128:
9568 if (IS_GEN2(dev))
9569 return false;
9570 case 64:
9571 break;
9572 default:
9573 return false;
9574 }
9575 }
9576
9577 return true;
9578}
9579
Jesse Barnes79e53942008-11-07 14:24:08 -08009580static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009581 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009582{
James Simmons72034252010-08-03 01:33:19 +01009583 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009584 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009585
James Simmons72034252010-08-03 01:33:19 +01009586 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009587 intel_crtc->lut_r[i] = red[i] >> 8;
9588 intel_crtc->lut_g[i] = green[i] >> 8;
9589 intel_crtc->lut_b[i] = blue[i] >> 8;
9590 }
9591
9592 intel_crtc_load_lut(crtc);
9593}
9594
Jesse Barnes79e53942008-11-07 14:24:08 -08009595/* VESA 640x480x72Hz mode to set on the pipe */
9596static struct drm_display_mode load_detect_mode = {
9597 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9598 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9599};
9600
Daniel Vettera8bb6812014-02-10 18:00:39 +01009601struct drm_framebuffer *
9602__intel_framebuffer_create(struct drm_device *dev,
9603 struct drm_mode_fb_cmd2 *mode_cmd,
9604 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009605{
9606 struct intel_framebuffer *intel_fb;
9607 int ret;
9608
9609 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9610 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009611 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009612 return ERR_PTR(-ENOMEM);
9613 }
9614
9615 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009616 if (ret)
9617 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009618
9619 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009620err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009621 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009622 kfree(intel_fb);
9623
9624 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009625}
9626
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009627static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009628intel_framebuffer_create(struct drm_device *dev,
9629 struct drm_mode_fb_cmd2 *mode_cmd,
9630 struct drm_i915_gem_object *obj)
9631{
9632 struct drm_framebuffer *fb;
9633 int ret;
9634
9635 ret = i915_mutex_lock_interruptible(dev);
9636 if (ret)
9637 return ERR_PTR(ret);
9638 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9639 mutex_unlock(&dev->struct_mutex);
9640
9641 return fb;
9642}
9643
Chris Wilsond2dff872011-04-19 08:36:26 +01009644static u32
9645intel_framebuffer_pitch_for_width(int width, int bpp)
9646{
9647 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9648 return ALIGN(pitch, 64);
9649}
9650
9651static u32
9652intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9653{
9654 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009655 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009656}
9657
9658static struct drm_framebuffer *
9659intel_framebuffer_create_for_mode(struct drm_device *dev,
9660 struct drm_display_mode *mode,
9661 int depth, int bpp)
9662{
9663 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009664 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009665
9666 obj = i915_gem_alloc_object(dev,
9667 intel_framebuffer_size_for_mode(mode, bpp));
9668 if (obj == NULL)
9669 return ERR_PTR(-ENOMEM);
9670
9671 mode_cmd.width = mode->hdisplay;
9672 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009673 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9674 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009675 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009676
9677 return intel_framebuffer_create(dev, &mode_cmd, obj);
9678}
9679
9680static struct drm_framebuffer *
9681mode_fits_in_fbdev(struct drm_device *dev,
9682 struct drm_display_mode *mode)
9683{
Daniel Vetter4520f532013-10-09 09:18:51 +02009684#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009685 struct drm_i915_private *dev_priv = dev->dev_private;
9686 struct drm_i915_gem_object *obj;
9687 struct drm_framebuffer *fb;
9688
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009689 if (!dev_priv->fbdev)
9690 return NULL;
9691
9692 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009693 return NULL;
9694
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009695 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009696 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009697
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009698 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009699 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9700 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009701 return NULL;
9702
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009703 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009704 return NULL;
9705
9706 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009707#else
9708 return NULL;
9709#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009710}
9711
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009712static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9713 struct drm_crtc *crtc,
9714 struct drm_display_mode *mode,
9715 struct drm_framebuffer *fb,
9716 int x, int y)
9717{
9718 struct drm_plane_state *plane_state;
9719 int hdisplay, vdisplay;
9720 int ret;
9721
9722 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9723 if (IS_ERR(plane_state))
9724 return PTR_ERR(plane_state);
9725
9726 if (mode)
9727 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
9728 else
9729 hdisplay = vdisplay = 0;
9730
9731 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9732 if (ret)
9733 return ret;
9734 drm_atomic_set_fb_for_plane(plane_state, fb);
9735 plane_state->crtc_x = 0;
9736 plane_state->crtc_y = 0;
9737 plane_state->crtc_w = hdisplay;
9738 plane_state->crtc_h = vdisplay;
9739 plane_state->src_x = x << 16;
9740 plane_state->src_y = y << 16;
9741 plane_state->src_w = hdisplay << 16;
9742 plane_state->src_h = vdisplay << 16;
9743
9744 return 0;
9745}
9746
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009747bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009748 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009749 struct intel_load_detect_pipe *old,
9750 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009751{
9752 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009753 struct intel_encoder *intel_encoder =
9754 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009755 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009756 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009757 struct drm_crtc *crtc = NULL;
9758 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009759 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009760 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009761 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009762 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009763 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009764 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009765
Chris Wilsond2dff872011-04-19 08:36:26 +01009766 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009767 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009768 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009769
Rob Clark51fd3712013-11-19 12:10:12 -05009770retry:
9771 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9772 if (ret)
9773 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009774
Jesse Barnes79e53942008-11-07 14:24:08 -08009775 /*
9776 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009777 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009778 * - if the connector already has an assigned crtc, use it (but make
9779 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009780 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009781 * - try to find the first unused crtc that can drive this connector,
9782 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009783 */
9784
9785 /* See if we already have a CRTC for this connector */
9786 if (encoder->crtc) {
9787 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009788
Rob Clark51fd3712013-11-19 12:10:12 -05009789 ret = drm_modeset_lock(&crtc->mutex, ctx);
9790 if (ret)
9791 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009792 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9793 if (ret)
9794 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009795
Daniel Vetter24218aa2012-08-12 19:27:11 +02009796 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009797 old->load_detect_temp = false;
9798
9799 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009800 if (connector->dpms != DRM_MODE_DPMS_ON)
9801 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009802
Chris Wilson71731882011-04-19 23:10:58 +01009803 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009804 }
9805
9806 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009807 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009808 i++;
9809 if (!(encoder->possible_crtcs & (1 << i)))
9810 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009811 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009812 continue;
9813 /* This can occur when applying the pipe A quirk on resume. */
9814 if (to_intel_crtc(possible_crtc)->new_enabled)
9815 continue;
9816
9817 crtc = possible_crtc;
9818 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009819 }
9820
9821 /*
9822 * If we didn't find an unused CRTC, don't use any.
9823 */
9824 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009825 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009826 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009827 }
9828
Rob Clark51fd3712013-11-19 12:10:12 -05009829 ret = drm_modeset_lock(&crtc->mutex, ctx);
9830 if (ret)
9831 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009832 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9833 if (ret)
9834 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009835 intel_encoder->new_crtc = to_intel_crtc(crtc);
9836 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009837
9838 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009839 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009840 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009841 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009842 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009843
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009844 state = drm_atomic_state_alloc(dev);
9845 if (!state)
9846 return false;
9847
9848 state->acquire_ctx = ctx;
9849
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009850 connector_state = drm_atomic_get_connector_state(state, connector);
9851 if (IS_ERR(connector_state)) {
9852 ret = PTR_ERR(connector_state);
9853 goto fail;
9854 }
9855
9856 connector_state->crtc = crtc;
9857 connector_state->best_encoder = &intel_encoder->base;
9858
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009859 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9860 if (IS_ERR(crtc_state)) {
9861 ret = PTR_ERR(crtc_state);
9862 goto fail;
9863 }
9864
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +02009865 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009866
Chris Wilson64927112011-04-20 07:25:26 +01009867 if (!mode)
9868 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009869
Chris Wilsond2dff872011-04-19 08:36:26 +01009870 /* We need a framebuffer large enough to accommodate all accesses
9871 * that the plane may generate whilst we perform load detection.
9872 * We can not rely on the fbcon either being present (we get called
9873 * during its initialisation to detect all boot displays, or it may
9874 * not even exist) or that it is large enough to satisfy the
9875 * requested mode.
9876 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009877 fb = mode_fits_in_fbdev(dev, mode);
9878 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009879 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009880 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9881 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009882 } else
9883 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009884 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009885 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009886 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009887 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009888
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009889 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9890 if (ret)
9891 goto fail;
9892
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +03009893 drm_mode_copy(&crtc_state->base.mode, mode);
9894
9895 if (intel_set_mode(crtc, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009896 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009897 if (old->release_fb)
9898 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009899 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009900 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009901 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009902
Jesse Barnes79e53942008-11-07 14:24:08 -08009903 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009904 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009905 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009906
9907 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009908 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009909fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +03009910 drm_atomic_state_free(state);
9911 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009912
Rob Clark51fd3712013-11-19 12:10:12 -05009913 if (ret == -EDEADLK) {
9914 drm_modeset_backoff(ctx);
9915 goto retry;
9916 }
9917
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009918 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009919}
9920
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009921void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009922 struct intel_load_detect_pipe *old,
9923 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009924{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009925 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009926 struct intel_encoder *intel_encoder =
9927 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009928 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009929 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009931 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009932 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009933 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009934 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009935
Chris Wilsond2dff872011-04-19 08:36:26 +01009936 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009937 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009938 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009939
Chris Wilson8261b192011-04-19 23:18:09 +01009940 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009941 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009942 if (!state)
9943 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009944
9945 state->acquire_ctx = ctx;
9946
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009947 connector_state = drm_atomic_get_connector_state(state, connector);
9948 if (IS_ERR(connector_state))
9949 goto fail;
9950
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009951 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9952 if (IS_ERR(crtc_state))
9953 goto fail;
9954
Daniel Vetterfc303102012-07-09 10:40:58 +02009955 to_intel_connector(connector)->new_encoder = NULL;
9956 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009957 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009958
9959 connector_state->best_encoder = NULL;
9960 connector_state->crtc = NULL;
9961
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +02009962 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009963
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009964 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
9965 0, 0);
9966 if (ret)
9967 goto fail;
9968
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +03009969 ret = intel_set_mode(crtc, state);
9970 if (ret)
9971 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +01009972
Daniel Vetter36206362012-12-10 20:42:17 +01009973 if (old->release_fb) {
9974 drm_framebuffer_unregister_private(old->release_fb);
9975 drm_framebuffer_unreference(old->release_fb);
9976 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009977
Chris Wilson0622a532011-04-21 09:32:11 +01009978 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009979 }
9980
Eric Anholtc751ce42010-03-25 11:48:48 -07009981 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009982 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9983 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009984
9985 return;
9986fail:
9987 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9988 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009989}
9990
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009991static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009992 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009993{
9994 struct drm_i915_private *dev_priv = dev->dev_private;
9995 u32 dpll = pipe_config->dpll_hw_state.dpll;
9996
9997 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009998 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009999 else if (HAS_PCH_SPLIT(dev))
10000 return 120000;
10001 else if (!IS_GEN2(dev))
10002 return 96000;
10003 else
10004 return 48000;
10005}
10006
Jesse Barnes79e53942008-11-07 14:24:08 -080010007/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010008static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010009 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010010{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010011 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010012 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010013 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010014 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010015 u32 fp;
10016 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010017 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010018
10019 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010020 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010021 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010022 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010023
10024 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010025 if (IS_PINEVIEW(dev)) {
10026 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10027 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010028 } else {
10029 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10030 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10031 }
10032
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010033 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010034 if (IS_PINEVIEW(dev))
10035 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10036 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010037 else
10038 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010039 DPLL_FPA01_P1_POST_DIV_SHIFT);
10040
10041 switch (dpll & DPLL_MODE_MASK) {
10042 case DPLLB_MODE_DAC_SERIAL:
10043 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10044 5 : 10;
10045 break;
10046 case DPLLB_MODE_LVDS:
10047 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10048 7 : 14;
10049 break;
10050 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010051 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010052 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010053 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010054 }
10055
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010056 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010057 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010058 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010059 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010060 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010061 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010062 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010063
10064 if (is_lvds) {
10065 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10066 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010067
10068 if (lvds & LVDS_CLKB_POWER_UP)
10069 clock.p2 = 7;
10070 else
10071 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010072 } else {
10073 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10074 clock.p1 = 2;
10075 else {
10076 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10077 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10078 }
10079 if (dpll & PLL_P2_DIVIDE_BY_4)
10080 clock.p2 = 4;
10081 else
10082 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010083 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010084
10085 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010086 }
10087
Ville Syrjälä18442d02013-09-13 16:00:08 +030010088 /*
10089 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010090 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010091 * encoder's get_config() function.
10092 */
10093 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010094}
10095
Ville Syrjälä6878da02013-09-13 15:59:11 +030010096int intel_dotclock_calculate(int link_freq,
10097 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010098{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010099 /*
10100 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010101 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010102 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010103 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010104 *
10105 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010106 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010107 */
10108
Ville Syrjälä6878da02013-09-13 15:59:11 +030010109 if (!m_n->link_n)
10110 return 0;
10111
10112 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10113}
10114
Ville Syrjälä18442d02013-09-13 16:00:08 +030010115static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010116 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010117{
10118 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010119
10120 /* read out port_clock from the DPLL */
10121 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010122
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010123 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010124 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010125 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010126 * agree once we know their relationship in the encoder's
10127 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010128 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010129 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010130 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10131 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010132}
10133
10134/** Returns the currently programmed mode of the given pipe. */
10135struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10136 struct drm_crtc *crtc)
10137{
Jesse Barnes548f2452011-02-17 10:40:53 -080010138 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010140 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010141 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010142 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010143 int htot = I915_READ(HTOTAL(cpu_transcoder));
10144 int hsync = I915_READ(HSYNC(cpu_transcoder));
10145 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10146 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010147 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010148
10149 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10150 if (!mode)
10151 return NULL;
10152
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010153 /*
10154 * Construct a pipe_config sufficient for getting the clock info
10155 * back out of crtc_clock_get.
10156 *
10157 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10158 * to use a real value here instead.
10159 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010160 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010161 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010162 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10163 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10164 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010165 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10166
Ville Syrjälä773ae032013-09-23 17:48:20 +030010167 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010168 mode->hdisplay = (htot & 0xffff) + 1;
10169 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10170 mode->hsync_start = (hsync & 0xffff) + 1;
10171 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10172 mode->vdisplay = (vtot & 0xffff) + 1;
10173 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10174 mode->vsync_start = (vsync & 0xffff) + 1;
10175 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10176
10177 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010178
10179 return mode;
10180}
10181
Jesse Barnes652c3932009-08-17 13:31:43 -070010182static void intel_decrease_pllclock(struct drm_crtc *crtc)
10183{
10184 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010185 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010187
Sonika Jindalbaff2962014-07-22 11:16:35 +053010188 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010189 return;
10190
10191 if (!dev_priv->lvds_downclock_avail)
10192 return;
10193
10194 /*
10195 * Since this is called by a timer, we should never get here in
10196 * the manual case.
10197 */
10198 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010199 int pipe = intel_crtc->pipe;
10200 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010201 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010202
Zhao Yakui44d98a62009-10-09 11:39:40 +080010203 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010204
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010205 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010206
Chris Wilson074b5e12012-05-02 12:07:06 +010010207 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010208 dpll |= DISPLAY_RATE_SELECT_FPA1;
10209 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010210 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010211 dpll = I915_READ(dpll_reg);
10212 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010213 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010214 }
10215
10216}
10217
Chris Wilsonf047e392012-07-21 12:31:41 +010010218void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010219{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010220 struct drm_i915_private *dev_priv = dev->dev_private;
10221
Chris Wilsonf62a0072014-02-21 17:55:39 +000010222 if (dev_priv->mm.busy)
10223 return;
10224
Paulo Zanoni43694d62014-03-07 20:08:08 -030010225 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010226 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010227 if (INTEL_INFO(dev)->gen >= 6)
10228 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010229 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010230}
10231
10232void intel_mark_idle(struct drm_device *dev)
10233{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010234 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010235 struct drm_crtc *crtc;
10236
Chris Wilsonf62a0072014-02-21 17:55:39 +000010237 if (!dev_priv->mm.busy)
10238 return;
10239
10240 dev_priv->mm.busy = false;
10241
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010242 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010243 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010244 continue;
10245
10246 intel_decrease_pllclock(crtc);
10247 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010248
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010249 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010250 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010251
Paulo Zanoni43694d62014-03-07 20:08:08 -030010252 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010253}
10254
Jesse Barnes79e53942008-11-07 14:24:08 -080010255static void intel_crtc_destroy(struct drm_crtc *crtc)
10256{
10257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010258 struct drm_device *dev = crtc->dev;
10259 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010260
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010261 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010262 work = intel_crtc->unpin_work;
10263 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010264 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010265
10266 if (work) {
10267 cancel_work_sync(&work->work);
10268 kfree(work);
10269 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010270
10271 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010272
Jesse Barnes79e53942008-11-07 14:24:08 -080010273 kfree(intel_crtc);
10274}
10275
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010276static void intel_unpin_work_fn(struct work_struct *__work)
10277{
10278 struct intel_unpin_work *work =
10279 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010280 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010281 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010282
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010283 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010284 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010285 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010286
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010287 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010288
10289 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010290 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010291 mutex_unlock(&dev->struct_mutex);
10292
Daniel Vetterf99d7062014-06-19 16:01:59 +020010293 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010294 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010295
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010296 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10297 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10298
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010299 kfree(work);
10300}
10301
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010302static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010303 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010304{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010305 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10306 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010307 unsigned long flags;
10308
10309 /* Ignore early vblank irqs */
10310 if (intel_crtc == NULL)
10311 return;
10312
Daniel Vetterf3260382014-09-15 14:55:23 +020010313 /*
10314 * This is called both by irq handlers and the reset code (to complete
10315 * lost pageflips) so needs the full irqsave spinlocks.
10316 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010317 spin_lock_irqsave(&dev->event_lock, flags);
10318 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010319
10320 /* Ensure we don't miss a work->pending update ... */
10321 smp_rmb();
10322
10323 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010324 spin_unlock_irqrestore(&dev->event_lock, flags);
10325 return;
10326 }
10327
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010328 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010329
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010330 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010331}
10332
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010333void intel_finish_page_flip(struct drm_device *dev, int pipe)
10334{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010335 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010336 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10337
Mario Kleiner49b14a52010-12-09 07:00:07 +010010338 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010339}
10340
10341void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10342{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010343 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010344 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10345
Mario Kleiner49b14a52010-12-09 07:00:07 +010010346 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010347}
10348
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010349/* Is 'a' after or equal to 'b'? */
10350static bool g4x_flip_count_after_eq(u32 a, u32 b)
10351{
10352 return !((a - b) & 0x80000000);
10353}
10354
10355static bool page_flip_finished(struct intel_crtc *crtc)
10356{
10357 struct drm_device *dev = crtc->base.dev;
10358 struct drm_i915_private *dev_priv = dev->dev_private;
10359
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010360 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10361 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10362 return true;
10363
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010364 /*
10365 * The relevant registers doen't exist on pre-ctg.
10366 * As the flip done interrupt doesn't trigger for mmio
10367 * flips on gmch platforms, a flip count check isn't
10368 * really needed there. But since ctg has the registers,
10369 * include it in the check anyway.
10370 */
10371 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10372 return true;
10373
10374 /*
10375 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10376 * used the same base address. In that case the mmio flip might
10377 * have completed, but the CS hasn't even executed the flip yet.
10378 *
10379 * A flip count check isn't enough as the CS might have updated
10380 * the base address just after start of vblank, but before we
10381 * managed to process the interrupt. This means we'd complete the
10382 * CS flip too soon.
10383 *
10384 * Combining both checks should get us a good enough result. It may
10385 * still happen that the CS flip has been executed, but has not
10386 * yet actually completed. But in case the base address is the same
10387 * anyway, we don't really care.
10388 */
10389 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10390 crtc->unpin_work->gtt_offset &&
10391 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10392 crtc->unpin_work->flip_count);
10393}
10394
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010395void intel_prepare_page_flip(struct drm_device *dev, int plane)
10396{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010397 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010398 struct intel_crtc *intel_crtc =
10399 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10400 unsigned long flags;
10401
Daniel Vetterf3260382014-09-15 14:55:23 +020010402
10403 /*
10404 * This is called both by irq handlers and the reset code (to complete
10405 * lost pageflips) so needs the full irqsave spinlocks.
10406 *
10407 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010408 * generate a page-flip completion irq, i.e. every modeset
10409 * is also accompanied by a spurious intel_prepare_page_flip().
10410 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010411 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010412 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010413 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010414 spin_unlock_irqrestore(&dev->event_lock, flags);
10415}
10416
Robin Schroereba905b2014-05-18 02:24:50 +020010417static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010418{
10419 /* Ensure that the work item is consistent when activating it ... */
10420 smp_wmb();
10421 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10422 /* and that it is marked active as soon as the irq could fire. */
10423 smp_wmb();
10424}
10425
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010426static int intel_gen2_queue_flip(struct drm_device *dev,
10427 struct drm_crtc *crtc,
10428 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010429 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010430 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010431 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010432{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010433 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010434 u32 flip_mask;
10435 int ret;
10436
Daniel Vetter6d90c952012-04-26 23:28:05 +020010437 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010438 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010439 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010440
10441 /* Can't queue multiple flips, so wait for the previous
10442 * one to finish before executing the next.
10443 */
10444 if (intel_crtc->plane)
10445 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10446 else
10447 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010448 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10449 intel_ring_emit(ring, MI_NOOP);
10450 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10451 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10452 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010453 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010454 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010455
10456 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010457 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010458 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010459}
10460
10461static int intel_gen3_queue_flip(struct drm_device *dev,
10462 struct drm_crtc *crtc,
10463 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010464 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010465 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010466 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010467{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010468 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010469 u32 flip_mask;
10470 int ret;
10471
Daniel Vetter6d90c952012-04-26 23:28:05 +020010472 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010473 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010474 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010475
10476 if (intel_crtc->plane)
10477 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10478 else
10479 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010480 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10481 intel_ring_emit(ring, MI_NOOP);
10482 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10483 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10484 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010485 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010486 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010487
Chris Wilsone7d841c2012-12-03 11:36:30 +000010488 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010489 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010490 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010491}
10492
10493static int intel_gen4_queue_flip(struct drm_device *dev,
10494 struct drm_crtc *crtc,
10495 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010496 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010497 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010498 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010499{
10500 struct drm_i915_private *dev_priv = dev->dev_private;
10501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10502 uint32_t pf, pipesrc;
10503 int ret;
10504
Daniel Vetter6d90c952012-04-26 23:28:05 +020010505 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010506 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010507 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010508
10509 /* i965+ uses the linear or tiled offsets from the
10510 * Display Registers (which do not change across a page-flip)
10511 * so we need only reprogram the base address.
10512 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010513 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10514 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10515 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010516 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010517 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010518
10519 /* XXX Enabling the panel-fitter across page-flip is so far
10520 * untested on non-native modes, so ignore it for now.
10521 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10522 */
10523 pf = 0;
10524 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010525 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010526
10527 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010528 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010529 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010530}
10531
10532static int intel_gen6_queue_flip(struct drm_device *dev,
10533 struct drm_crtc *crtc,
10534 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010535 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010536 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010537 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010538{
10539 struct drm_i915_private *dev_priv = dev->dev_private;
10540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10541 uint32_t pf, pipesrc;
10542 int ret;
10543
Daniel Vetter6d90c952012-04-26 23:28:05 +020010544 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010545 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010546 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010547
Daniel Vetter6d90c952012-04-26 23:28:05 +020010548 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10549 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10550 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010551 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010552
Chris Wilson99d9acd2012-04-17 20:37:00 +010010553 /* Contrary to the suggestions in the documentation,
10554 * "Enable Panel Fitter" does not seem to be required when page
10555 * flipping with a non-native mode, and worse causes a normal
10556 * modeset to fail.
10557 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10558 */
10559 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010560 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010561 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010562
10563 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010564 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010565 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010566}
10567
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010568static int intel_gen7_queue_flip(struct drm_device *dev,
10569 struct drm_crtc *crtc,
10570 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010571 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010572 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010573 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010574{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010575 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010576 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010577 int len, ret;
10578
Robin Schroereba905b2014-05-18 02:24:50 +020010579 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010580 case PLANE_A:
10581 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10582 break;
10583 case PLANE_B:
10584 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10585 break;
10586 case PLANE_C:
10587 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10588 break;
10589 default:
10590 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010591 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010592 }
10593
Chris Wilsonffe74d72013-08-26 20:58:12 +010010594 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010595 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010596 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010597 /*
10598 * On Gen 8, SRM is now taking an extra dword to accommodate
10599 * 48bits addresses, and we need a NOOP for the batch size to
10600 * stay even.
10601 */
10602 if (IS_GEN8(dev))
10603 len += 2;
10604 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010605
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010606 /*
10607 * BSpec MI_DISPLAY_FLIP for IVB:
10608 * "The full packet must be contained within the same cache line."
10609 *
10610 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10611 * cacheline, if we ever start emitting more commands before
10612 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10613 * then do the cacheline alignment, and finally emit the
10614 * MI_DISPLAY_FLIP.
10615 */
10616 ret = intel_ring_cacheline_align(ring);
10617 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010618 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010619
Chris Wilsonffe74d72013-08-26 20:58:12 +010010620 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010621 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010622 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010623
Chris Wilsonffe74d72013-08-26 20:58:12 +010010624 /* Unmask the flip-done completion message. Note that the bspec says that
10625 * we should do this for both the BCS and RCS, and that we must not unmask
10626 * more than one flip event at any time (or ensure that one flip message
10627 * can be sent by waiting for flip-done prior to queueing new flips).
10628 * Experimentation says that BCS works despite DERRMR masking all
10629 * flip-done completion events and that unmasking all planes at once
10630 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10631 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10632 */
10633 if (ring->id == RCS) {
10634 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10635 intel_ring_emit(ring, DERRMR);
10636 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10637 DERRMR_PIPEB_PRI_FLIP_DONE |
10638 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010639 if (IS_GEN8(dev))
10640 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10641 MI_SRM_LRM_GLOBAL_GTT);
10642 else
10643 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10644 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010645 intel_ring_emit(ring, DERRMR);
10646 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010647 if (IS_GEN8(dev)) {
10648 intel_ring_emit(ring, 0);
10649 intel_ring_emit(ring, MI_NOOP);
10650 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010651 }
10652
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010653 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010654 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010655 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010656 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010657
10658 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010659 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010660 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010661}
10662
Sourab Gupta84c33a62014-06-02 16:47:17 +053010663static bool use_mmio_flip(struct intel_engine_cs *ring,
10664 struct drm_i915_gem_object *obj)
10665{
10666 /*
10667 * This is not being used for older platforms, because
10668 * non-availability of flip done interrupt forces us to use
10669 * CS flips. Older platforms derive flip done using some clever
10670 * tricks involving the flip_pending status bits and vblank irqs.
10671 * So using MMIO flips there would disrupt this mechanism.
10672 */
10673
Chris Wilson8e09bf82014-07-08 10:40:30 +010010674 if (ring == NULL)
10675 return true;
10676
Sourab Gupta84c33a62014-06-02 16:47:17 +053010677 if (INTEL_INFO(ring->dev)->gen < 5)
10678 return false;
10679
10680 if (i915.use_mmio_flip < 0)
10681 return false;
10682 else if (i915.use_mmio_flip > 0)
10683 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010684 else if (i915.enable_execlists)
10685 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010686 else
John Harrison41c52412014-11-24 18:49:43 +000010687 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010688}
10689
Damien Lespiauff944562014-11-20 14:58:16 +000010690static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10691{
10692 struct drm_device *dev = intel_crtc->base.dev;
10693 struct drm_i915_private *dev_priv = dev->dev_private;
10694 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010695 const enum pipe pipe = intel_crtc->pipe;
10696 u32 ctl, stride;
10697
10698 ctl = I915_READ(PLANE_CTL(pipe, 0));
10699 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010700 switch (fb->modifier[0]) {
10701 case DRM_FORMAT_MOD_NONE:
10702 break;
10703 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010704 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010705 break;
10706 case I915_FORMAT_MOD_Y_TILED:
10707 ctl |= PLANE_CTL_TILED_Y;
10708 break;
10709 case I915_FORMAT_MOD_Yf_TILED:
10710 ctl |= PLANE_CTL_TILED_YF;
10711 break;
10712 default:
10713 MISSING_CASE(fb->modifier[0]);
10714 }
Damien Lespiauff944562014-11-20 14:58:16 +000010715
10716 /*
10717 * The stride is either expressed as a multiple of 64 bytes chunks for
10718 * linear buffers or in number of tiles for tiled buffers.
10719 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010720 stride = fb->pitches[0] /
10721 intel_fb_stride_alignment(dev, fb->modifier[0],
10722 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010723
10724 /*
10725 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10726 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10727 */
10728 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10729 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10730
10731 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10732 POSTING_READ(PLANE_SURF(pipe, 0));
10733}
10734
10735static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010736{
10737 struct drm_device *dev = intel_crtc->base.dev;
10738 struct drm_i915_private *dev_priv = dev->dev_private;
10739 struct intel_framebuffer *intel_fb =
10740 to_intel_framebuffer(intel_crtc->base.primary->fb);
10741 struct drm_i915_gem_object *obj = intel_fb->obj;
10742 u32 dspcntr;
10743 u32 reg;
10744
Sourab Gupta84c33a62014-06-02 16:47:17 +053010745 reg = DSPCNTR(intel_crtc->plane);
10746 dspcntr = I915_READ(reg);
10747
Damien Lespiauc5d97472014-10-25 00:11:11 +010010748 if (obj->tiling_mode != I915_TILING_NONE)
10749 dspcntr |= DISPPLANE_TILED;
10750 else
10751 dspcntr &= ~DISPPLANE_TILED;
10752
Sourab Gupta84c33a62014-06-02 16:47:17 +053010753 I915_WRITE(reg, dspcntr);
10754
10755 I915_WRITE(DSPSURF(intel_crtc->plane),
10756 intel_crtc->unpin_work->gtt_offset);
10757 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010758
Damien Lespiauff944562014-11-20 14:58:16 +000010759}
10760
10761/*
10762 * XXX: This is the temporary way to update the plane registers until we get
10763 * around to using the usual plane update functions for MMIO flips
10764 */
10765static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10766{
10767 struct drm_device *dev = intel_crtc->base.dev;
10768 bool atomic_update;
10769 u32 start_vbl_count;
10770
10771 intel_mark_page_flip_active(intel_crtc);
10772
10773 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10774
10775 if (INTEL_INFO(dev)->gen >= 9)
10776 skl_do_mmio_flip(intel_crtc);
10777 else
10778 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10779 ilk_do_mmio_flip(intel_crtc);
10780
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010781 if (atomic_update)
10782 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010783}
10784
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010785static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010786{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010787 struct intel_mmio_flip *mmio_flip =
10788 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010789
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010790 if (mmio_flip->rq)
10791 WARN_ON(__i915_wait_request(mmio_flip->rq,
10792 mmio_flip->crtc->reset_counter,
10793 false, NULL, NULL));
Sourab Gupta84c33a62014-06-02 16:47:17 +053010794
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010795 intel_do_mmio_flip(mmio_flip->crtc);
10796
10797 i915_gem_request_unreference__unlocked(mmio_flip->rq);
10798 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010799}
10800
10801static int intel_queue_mmio_flip(struct drm_device *dev,
10802 struct drm_crtc *crtc,
10803 struct drm_framebuffer *fb,
10804 struct drm_i915_gem_object *obj,
10805 struct intel_engine_cs *ring,
10806 uint32_t flags)
10807{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010808 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010809
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010810 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
10811 if (mmio_flip == NULL)
10812 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010813
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010814 mmio_flip->rq = i915_gem_request_reference(obj->last_write_req);
10815 mmio_flip->crtc = to_intel_crtc(crtc);
10816
10817 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
10818 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010819
Sourab Gupta84c33a62014-06-02 16:47:17 +053010820 return 0;
10821}
10822
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010823static int intel_default_queue_flip(struct drm_device *dev,
10824 struct drm_crtc *crtc,
10825 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010826 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010827 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010828 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010829{
10830 return -ENODEV;
10831}
10832
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010833static bool __intel_pageflip_stall_check(struct drm_device *dev,
10834 struct drm_crtc *crtc)
10835{
10836 struct drm_i915_private *dev_priv = dev->dev_private;
10837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10838 struct intel_unpin_work *work = intel_crtc->unpin_work;
10839 u32 addr;
10840
10841 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10842 return true;
10843
10844 if (!work->enable_stall_check)
10845 return false;
10846
10847 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010848 if (work->flip_queued_req &&
10849 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010850 return false;
10851
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010852 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010853 }
10854
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010855 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010856 return false;
10857
10858 /* Potential stall - if we see that the flip has happened,
10859 * assume a missed interrupt. */
10860 if (INTEL_INFO(dev)->gen >= 4)
10861 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10862 else
10863 addr = I915_READ(DSPADDR(intel_crtc->plane));
10864
10865 /* There is a potential issue here with a false positive after a flip
10866 * to the same address. We could address this by checking for a
10867 * non-incrementing frame counter.
10868 */
10869 return addr == work->gtt_offset;
10870}
10871
10872void intel_check_page_flip(struct drm_device *dev, int pipe)
10873{
10874 struct drm_i915_private *dev_priv = dev->dev_private;
10875 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010877 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010878
Dave Gordon6c51d462015-03-06 15:34:26 +000010879 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010880
10881 if (crtc == NULL)
10882 return;
10883
Daniel Vetterf3260382014-09-15 14:55:23 +020010884 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010885 work = intel_crtc->unpin_work;
10886 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010887 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010888 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010889 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010890 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010891 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010892 if (work != NULL &&
10893 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10894 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010895 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010896}
10897
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010898static int intel_crtc_page_flip(struct drm_crtc *crtc,
10899 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010900 struct drm_pending_vblank_event *event,
10901 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010902{
10903 struct drm_device *dev = crtc->dev;
10904 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010905 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010906 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010907 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010908 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010909 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010910 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010911 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010912 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010913 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010914
Matt Roper2ff8fde2014-07-08 07:50:07 -070010915 /*
10916 * drm_mode_page_flip_ioctl() should already catch this, but double
10917 * check to be safe. In the future we may enable pageflipping from
10918 * a disabled primary plane.
10919 */
10920 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10921 return -EBUSY;
10922
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010923 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010924 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010925 return -EINVAL;
10926
10927 /*
10928 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10929 * Note that pitch changes could also affect these register.
10930 */
10931 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010932 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10933 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010934 return -EINVAL;
10935
Chris Wilsonf900db42014-02-20 09:26:13 +000010936 if (i915_terminally_wedged(&dev_priv->gpu_error))
10937 goto out_hang;
10938
Daniel Vetterb14c5672013-09-19 12:18:32 +020010939 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010940 if (work == NULL)
10941 return -ENOMEM;
10942
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010943 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010944 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010945 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010946 INIT_WORK(&work->work, intel_unpin_work_fn);
10947
Daniel Vetter87b6b102014-05-15 15:33:46 +020010948 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010949 if (ret)
10950 goto free_work;
10951
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010952 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010953 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010954 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010955 /* Before declaring the flip queue wedged, check if
10956 * the hardware completed the operation behind our backs.
10957 */
10958 if (__intel_pageflip_stall_check(dev, crtc)) {
10959 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10960 page_flip_completed(intel_crtc);
10961 } else {
10962 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010963 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010964
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010965 drm_crtc_vblank_put(crtc);
10966 kfree(work);
10967 return -EBUSY;
10968 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010969 }
10970 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010971 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010972
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010973 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10974 flush_workqueue(dev_priv->wq);
10975
Jesse Barnes75dfca82010-02-10 15:09:44 -080010976 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010977 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010978 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010979
Matt Roperf4510a22014-04-01 15:22:40 -070010980 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010981 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010982
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010983 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010984
Chris Wilson89ed88b2015-02-16 14:31:49 +000010985 ret = i915_mutex_lock_interruptible(dev);
10986 if (ret)
10987 goto cleanup;
10988
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010989 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010990 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010991
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010992 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010993 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010994
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010995 if (IS_VALLEYVIEW(dev)) {
10996 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010997 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010998 /* vlv: DISPLAY_FLIP fails to change tiling */
10999 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011000 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011001 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011002 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000011003 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011004 if (ring == NULL || ring->id != RCS)
11005 ring = &dev_priv->ring[BCS];
11006 } else {
11007 ring = &dev_priv->ring[RCS];
11008 }
11009
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011010 mmio_flip = use_mmio_flip(ring, obj);
11011
11012 /* When using CS flips, we want to emit semaphores between rings.
11013 * However, when using mmio flips we will create a task to do the
11014 * synchronisation, so all we want here is to pin the framebuffer
11015 * into the display plane and skip any waits.
11016 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011017 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011018 crtc->primary->state,
11019 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011020 if (ret)
11021 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011022
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011023 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11024 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011025
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011026 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011027 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11028 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011029 if (ret)
11030 goto cleanup_unpin;
11031
John Harrisonf06cc1b2014-11-24 18:49:37 +000011032 i915_gem_request_assign(&work->flip_queued_req,
11033 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011034 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011035 if (obj->last_write_req) {
11036 ret = i915_gem_check_olr(obj->last_write_req);
11037 if (ret)
11038 goto cleanup_unpin;
11039 }
11040
Sourab Gupta84c33a62014-06-02 16:47:17 +053011041 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011042 page_flip_flags);
11043 if (ret)
11044 goto cleanup_unpin;
11045
John Harrisonf06cc1b2014-11-24 18:49:37 +000011046 i915_gem_request_assign(&work->flip_queued_req,
11047 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011048 }
11049
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011050 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011051 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011052
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011053 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011054 INTEL_FRONTBUFFER_PRIMARY(pipe));
11055
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011056 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011057 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011058 mutex_unlock(&dev->struct_mutex);
11059
Jesse Barnese5510fa2010-07-01 16:48:37 -070011060 trace_i915_flip_request(intel_crtc->plane, obj);
11061
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011062 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011063
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011064cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011065 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011066cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011067 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011068 mutex_unlock(&dev->struct_mutex);
11069cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011070 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011071 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011072
Chris Wilson89ed88b2015-02-16 14:31:49 +000011073 drm_gem_object_unreference_unlocked(&obj->base);
11074 drm_framebuffer_unreference(work->old_fb);
11075
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011076 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011077 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011078 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011079
Daniel Vetter87b6b102014-05-15 15:33:46 +020011080 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011081free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011082 kfree(work);
11083
Chris Wilsonf900db42014-02-20 09:26:13 +000011084 if (ret == -EIO) {
11085out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011086 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011087 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011088 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011089 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011090 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011091 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011092 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011093 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011094}
11095
Jani Nikula65b38e02015-04-13 11:26:56 +030011096static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011097 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11098 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011099 .atomic_begin = intel_begin_crtc_commit,
11100 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011101};
11102
Daniel Vetter9a935852012-07-05 22:34:27 +020011103/**
11104 * intel_modeset_update_staged_output_state
11105 *
11106 * Updates the staged output configuration state, e.g. after we've read out the
11107 * current hw state.
11108 */
11109static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11110{
Ville Syrjälä76688512014-01-10 11:28:06 +020011111 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011112 struct intel_encoder *encoder;
11113 struct intel_connector *connector;
11114
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011115 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011116 connector->new_encoder =
11117 to_intel_encoder(connector->base.encoder);
11118 }
11119
Damien Lespiaub2784e12014-08-05 11:29:37 +010011120 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011121 encoder->new_crtc =
11122 to_intel_crtc(encoder->base.crtc);
11123 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011124
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011125 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011126 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011127 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011128}
11129
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011130/* Transitional helper to copy current connector/encoder state to
11131 * connector->state. This is needed so that code that is partially
11132 * converted to atomic does the right thing.
11133 */
11134static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11135{
11136 struct intel_connector *connector;
11137
11138 for_each_intel_connector(dev, connector) {
11139 if (connector->base.encoder) {
11140 connector->base.state->best_encoder =
11141 connector->base.encoder;
11142 connector->base.state->crtc =
11143 connector->base.encoder->crtc;
11144 } else {
11145 connector->base.state->best_encoder = NULL;
11146 connector->base.state->crtc = NULL;
11147 }
11148 }
11149}
11150
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011151/* Fixup legacy state after an atomic state swap.
Daniel Vetter9a935852012-07-05 22:34:27 +020011152 */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011153static void intel_modeset_fixup_state(struct drm_atomic_state *state)
Daniel Vetter9a935852012-07-05 22:34:27 +020011154{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011155 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011156 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011157 struct intel_connector *connector;
Daniel Vetter9a935852012-07-05 22:34:27 +020011158
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011159 for_each_intel_connector(state->dev, connector) {
11160 connector->base.encoder = connector->base.state->best_encoder;
11161 if (connector->base.encoder)
11162 connector->base.encoder->crtc =
11163 connector->base.state->crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011164 }
11165
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011166 /* Update crtc of disabled encoders */
11167 for_each_intel_encoder(state->dev, encoder) {
11168 int num_connectors = 0;
11169
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011170 for_each_intel_connector(state->dev, connector)
11171 if (connector->base.encoder == &encoder->base)
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011172 num_connectors++;
11173
11174 if (num_connectors == 0)
11175 encoder->base.crtc = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020011176 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011177
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011178 for_each_intel_crtc(state->dev, crtc) {
11179 crtc->base.enabled = crtc->base.state->enable;
11180 crtc->config = to_intel_crtc_state(crtc->base.state);
Ville Syrjälä76688512014-01-10 11:28:06 +020011181 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011182
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011183 /* Copy the new configuration to the staged state, to keep the few
11184 * pieces of code that haven't been converted yet happy */
11185 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011186}
11187
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011188static void
Robin Schroereba905b2014-05-18 02:24:50 +020011189connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011190 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011191{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011192 int bpp = pipe_config->pipe_bpp;
11193
11194 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11195 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011196 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011197
11198 /* Don't use an invalid EDID bpc value */
11199 if (connector->base.display_info.bpc &&
11200 connector->base.display_info.bpc * 3 < bpp) {
11201 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11202 bpp, connector->base.display_info.bpc*3);
11203 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11204 }
11205
11206 /* Clamp bpp to 8 on screens without EDID 1.4 */
11207 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11208 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11209 bpp);
11210 pipe_config->pipe_bpp = 24;
11211 }
11212}
11213
11214static int
11215compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011216 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011217{
11218 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011219 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011220 struct drm_connector *connector;
11221 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011222 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011223
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011224 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011225 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011226 else if (INTEL_INFO(dev)->gen >= 5)
11227 bpp = 12*3;
11228 else
11229 bpp = 8*3;
11230
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011231
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011232 pipe_config->pipe_bpp = bpp;
11233
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011234 state = pipe_config->base.state;
11235
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011236 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011237 for_each_connector_in_state(state, connector, connector_state, i) {
11238 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011239 continue;
11240
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011241 connected_sink_compute_bpp(to_intel_connector(connector),
11242 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011243 }
11244
11245 return bpp;
11246}
11247
Daniel Vetter644db712013-09-19 14:53:58 +020011248static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11249{
11250 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11251 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011252 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011253 mode->crtc_hdisplay, mode->crtc_hsync_start,
11254 mode->crtc_hsync_end, mode->crtc_htotal,
11255 mode->crtc_vdisplay, mode->crtc_vsync_start,
11256 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11257}
11258
Daniel Vetterc0b03412013-05-28 12:05:54 +020011259static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011260 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011261 const char *context)
11262{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011263 struct drm_device *dev = crtc->base.dev;
11264 struct drm_plane *plane;
11265 struct intel_plane *intel_plane;
11266 struct intel_plane_state *state;
11267 struct drm_framebuffer *fb;
11268
11269 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11270 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011271
11272 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11273 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11274 pipe_config->pipe_bpp, pipe_config->dither);
11275 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11276 pipe_config->has_pch_encoder,
11277 pipe_config->fdi_lanes,
11278 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11279 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11280 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011281 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11282 pipe_config->has_dp_encoder,
11283 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11284 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11285 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011286
11287 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11288 pipe_config->has_dp_encoder,
11289 pipe_config->dp_m2_n2.gmch_m,
11290 pipe_config->dp_m2_n2.gmch_n,
11291 pipe_config->dp_m2_n2.link_m,
11292 pipe_config->dp_m2_n2.link_n,
11293 pipe_config->dp_m2_n2.tu);
11294
Daniel Vetter55072d12014-11-20 16:10:28 +010011295 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11296 pipe_config->has_audio,
11297 pipe_config->has_infoframe);
11298
Daniel Vetterc0b03412013-05-28 12:05:54 +020011299 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011300 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011301 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011302 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11303 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011304 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011305 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11306 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011307 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11308 crtc->num_scalers,
11309 pipe_config->scaler_state.scaler_users,
11310 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011311 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11312 pipe_config->gmch_pfit.control,
11313 pipe_config->gmch_pfit.pgm_ratios,
11314 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011315 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011316 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011317 pipe_config->pch_pfit.size,
11318 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011319 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011320 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011321
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011322 if (IS_BROXTON(dev)) {
11323 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11324 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11325 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11326 pipe_config->ddi_pll_sel,
11327 pipe_config->dpll_hw_state.ebb0,
11328 pipe_config->dpll_hw_state.pll0,
11329 pipe_config->dpll_hw_state.pll1,
11330 pipe_config->dpll_hw_state.pll2,
11331 pipe_config->dpll_hw_state.pll3,
11332 pipe_config->dpll_hw_state.pll6,
11333 pipe_config->dpll_hw_state.pll8,
11334 pipe_config->dpll_hw_state.pcsdw12);
11335 } else if (IS_SKYLAKE(dev)) {
11336 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11337 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11338 pipe_config->ddi_pll_sel,
11339 pipe_config->dpll_hw_state.ctrl1,
11340 pipe_config->dpll_hw_state.cfgcr1,
11341 pipe_config->dpll_hw_state.cfgcr2);
11342 } else if (HAS_DDI(dev)) {
11343 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11344 pipe_config->ddi_pll_sel,
11345 pipe_config->dpll_hw_state.wrpll);
11346 } else {
11347 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11348 "fp0: 0x%x, fp1: 0x%x\n",
11349 pipe_config->dpll_hw_state.dpll,
11350 pipe_config->dpll_hw_state.dpll_md,
11351 pipe_config->dpll_hw_state.fp0,
11352 pipe_config->dpll_hw_state.fp1);
11353 }
11354
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011355 DRM_DEBUG_KMS("planes on this crtc\n");
11356 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11357 intel_plane = to_intel_plane(plane);
11358 if (intel_plane->pipe != crtc->pipe)
11359 continue;
11360
11361 state = to_intel_plane_state(plane->state);
11362 fb = state->base.fb;
11363 if (!fb) {
11364 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11365 "disabled, scaler_id = %d\n",
11366 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11367 plane->base.id, intel_plane->pipe,
11368 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11369 drm_plane_index(plane), state->scaler_id);
11370 continue;
11371 }
11372
11373 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11374 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11375 plane->base.id, intel_plane->pipe,
11376 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11377 drm_plane_index(plane));
11378 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11379 fb->base.id, fb->width, fb->height, fb->pixel_format);
11380 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11381 state->scaler_id,
11382 state->src.x1 >> 16, state->src.y1 >> 16,
11383 drm_rect_width(&state->src) >> 16,
11384 drm_rect_height(&state->src) >> 16,
11385 state->dst.x1, state->dst.y1,
11386 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11387 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011388}
11389
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011390static bool encoders_cloneable(const struct intel_encoder *a,
11391 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011392{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011393 /* masks could be asymmetric, so check both ways */
11394 return a == b || (a->cloneable & (1 << b->type) &&
11395 b->cloneable & (1 << a->type));
11396}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011397
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011398static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11399 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011400 struct intel_encoder *encoder)
11401{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011402 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011403 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011404 struct drm_connector_state *connector_state;
11405 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011406
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011407 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011408 if (connector_state->crtc != &crtc->base)
11409 continue;
11410
11411 source_encoder =
11412 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011413 if (!encoders_cloneable(encoder, source_encoder))
11414 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011415 }
11416
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011417 return true;
11418}
11419
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011420static bool check_encoder_cloning(struct drm_atomic_state *state,
11421 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011422{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011423 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011424 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011425 struct drm_connector_state *connector_state;
11426 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011427
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011428 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011429 if (connector_state->crtc != &crtc->base)
11430 continue;
11431
11432 encoder = to_intel_encoder(connector_state->best_encoder);
11433 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011434 return false;
11435 }
11436
11437 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011438}
11439
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011440static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011441{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011442 struct drm_device *dev = state->dev;
11443 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011444 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011445 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011446 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011447 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011448
11449 /*
11450 * Walk the connector list instead of the encoder
11451 * list to detect the problem on ddi platforms
11452 * where there's just one encoder per digital port.
11453 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011454 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011455 if (!connector_state->best_encoder)
11456 continue;
11457
11458 encoder = to_intel_encoder(connector_state->best_encoder);
11459
11460 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011461
11462 switch (encoder->type) {
11463 unsigned int port_mask;
11464 case INTEL_OUTPUT_UNKNOWN:
11465 if (WARN_ON(!HAS_DDI(dev)))
11466 break;
11467 case INTEL_OUTPUT_DISPLAYPORT:
11468 case INTEL_OUTPUT_HDMI:
11469 case INTEL_OUTPUT_EDP:
11470 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11471
11472 /* the same port mustn't appear more than once */
11473 if (used_ports & port_mask)
11474 return false;
11475
11476 used_ports |= port_mask;
11477 default:
11478 break;
11479 }
11480 }
11481
11482 return true;
11483}
11484
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011485static void
11486clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11487{
11488 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011489 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011490 struct intel_dpll_hw_state dpll_hw_state;
11491 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011492 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011493
Chandra Konduru663a3642015-04-07 15:28:41 -070011494 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011495 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011496 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011497 shared_dpll = crtc_state->shared_dpll;
11498 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011499 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011500
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011501 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011502
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011503 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011504 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011505 crtc_state->shared_dpll = shared_dpll;
11506 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011507 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011508}
11509
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011510static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011511intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011512 struct drm_atomic_state *state,
11513 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011514{
Daniel Vetter7758a112012-07-08 19:40:39 +020011515 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011516 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011517 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011518 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011519 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011520 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011521
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011522 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011523 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011524 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011525 }
11526
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011527 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011528 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011529 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011530 }
11531
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011532 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011533
Daniel Vettere143a212013-07-04 12:01:15 +020011534 pipe_config->cpu_transcoder =
11535 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011536
Imre Deak2960bc92013-07-30 13:36:32 +030011537 /*
11538 * Sanitize sync polarity flags based on requested ones. If neither
11539 * positive or negative polarity is requested, treat this as meaning
11540 * negative polarity.
11541 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011542 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011543 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011544 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011545
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011546 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011547 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011548 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011549
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011550 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11551 * plane pixel format and any sink constraints into account. Returns the
11552 * source plane bpp so that dithering can be selected on mismatches
11553 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011554 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11555 pipe_config);
11556 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011557 goto fail;
11558
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011559 /*
11560 * Determine the real pipe dimensions. Note that stereo modes can
11561 * increase the actual pipe size due to the frame doubling and
11562 * insertion of additional space for blanks between the frame. This
11563 * is stored in the crtc timings. We use the requested mode to do this
11564 * computation to clearly distinguish it from the adjusted mode, which
11565 * can be changed by the connectors in the below retry loop.
11566 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011567 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011568 &pipe_config->pipe_src_w,
11569 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011570
Daniel Vettere29c22c2013-02-21 00:00:16 +010011571encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011572 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011573 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011574 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011575
Daniel Vetter135c81b2013-07-21 21:37:09 +020011576 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011577 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11578 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011579
Daniel Vetter7758a112012-07-08 19:40:39 +020011580 /* Pass our mode to the connectors and the CRTC to give them a chance to
11581 * adjust it according to limitations or connector properties, and also
11582 * a chance to reject the mode entirely.
11583 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011584 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011585 if (connector_state->crtc != crtc)
11586 continue;
11587
11588 encoder = to_intel_encoder(connector_state->best_encoder);
11589
Daniel Vetterefea6e82013-07-21 21:36:59 +020011590 if (!(encoder->compute_config(encoder, pipe_config))) {
11591 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011592 goto fail;
11593 }
11594 }
11595
Daniel Vetterff9a6752013-06-01 17:16:21 +020011596 /* Set default port clock if not overwritten by the encoder. Needs to be
11597 * done afterwards in case the encoder adjusts the mode. */
11598 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011599 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011600 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011601
Daniel Vettera43f6e02013-06-07 23:10:32 +020011602 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011603 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011604 DRM_DEBUG_KMS("CRTC fixup failed\n");
11605 goto fail;
11606 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011607
11608 if (ret == RETRY) {
11609 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11610 ret = -EINVAL;
11611 goto fail;
11612 }
11613
11614 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11615 retry = false;
11616 goto encoder_retry;
11617 }
11618
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011619 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011620 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011621 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011622
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011623 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020011624fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011625 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011626}
11627
Daniel Vetterea9d7582012-07-10 10:42:52 +020011628static bool intel_crtc_in_use(struct drm_crtc *crtc)
11629{
11630 struct drm_encoder *encoder;
11631 struct drm_device *dev = crtc->dev;
11632
11633 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11634 if (encoder->crtc == crtc)
11635 return true;
11636
11637 return false;
11638}
11639
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011640static bool
11641needs_modeset(struct drm_crtc_state *state)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011642{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011643 return state->mode_changed || state->active_changed;
11644}
11645
11646static void
11647intel_modeset_update_state(struct drm_atomic_state *state)
11648{
11649 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011650 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011651 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011652 struct drm_crtc *crtc;
11653 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011654 struct drm_connector *connector;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011655 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011656
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011657 intel_shared_dpll_commit(dev_priv);
11658
Damien Lespiaub2784e12014-08-05 11:29:37 +010011659 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011660 if (!intel_encoder->base.crtc)
11661 continue;
11662
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011663 for_each_crtc_in_state(state, crtc, crtc_state, i)
11664 if (crtc == intel_encoder->base.crtc)
11665 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011666
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011667 if (crtc != intel_encoder->base.crtc)
11668 continue;
11669
11670 if (crtc_state->enable && needs_modeset(crtc_state))
Daniel Vetterea9d7582012-07-10 10:42:52 +020011671 intel_encoder->connectors_active = false;
11672 }
11673
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011674 drm_atomic_helper_swap_state(state->dev, state);
11675 intel_modeset_fixup_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011676
Ville Syrjälä76688512014-01-10 11:28:06 +020011677 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011678 for_each_crtc(dev, crtc) {
11679 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011680 }
11681
11682 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11683 if (!connector->encoder || !connector->encoder->crtc)
11684 continue;
11685
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011686 for_each_crtc_in_state(state, crtc, crtc_state, i)
11687 if (crtc == connector->encoder->crtc)
11688 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011689
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011690 if (crtc != connector->encoder->crtc)
11691 continue;
11692
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011693 if (crtc->state->enable && needs_modeset(crtc->state)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011694 struct drm_property *dpms_property =
11695 dev->mode_config.dpms_property;
11696
Daniel Vetterea9d7582012-07-10 10:42:52 +020011697 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011698 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011699 dpms_property,
11700 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011701
11702 intel_encoder = to_intel_encoder(connector->encoder);
11703 intel_encoder->connectors_active = true;
11704 }
11705 }
11706
11707}
11708
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011709static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011710{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011711 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011712
11713 if (clock1 == clock2)
11714 return true;
11715
11716 if (!clock1 || !clock2)
11717 return false;
11718
11719 diff = abs(clock1 - clock2);
11720
11721 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11722 return true;
11723
11724 return false;
11725}
11726
Daniel Vetter25c5b262012-07-08 22:08:04 +020011727#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11728 list_for_each_entry((intel_crtc), \
11729 &(dev)->mode_config.crtc_list, \
11730 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011731 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011732
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011733static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011734intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011735 struct intel_crtc_state *current_config,
11736 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011737{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011738#define PIPE_CONF_CHECK_X(name) \
11739 if (current_config->name != pipe_config->name) { \
11740 DRM_ERROR("mismatch in " #name " " \
11741 "(expected 0x%08x, found 0x%08x)\n", \
11742 current_config->name, \
11743 pipe_config->name); \
11744 return false; \
11745 }
11746
Daniel Vetter08a24032013-04-19 11:25:34 +020011747#define PIPE_CONF_CHECK_I(name) \
11748 if (current_config->name != pipe_config->name) { \
11749 DRM_ERROR("mismatch in " #name " " \
11750 "(expected %i, found %i)\n", \
11751 current_config->name, \
11752 pipe_config->name); \
11753 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011754 }
11755
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011756/* This is required for BDW+ where there is only one set of registers for
11757 * switching between high and low RR.
11758 * This macro can be used whenever a comparison has to be made between one
11759 * hw state and multiple sw state variables.
11760 */
11761#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11762 if ((current_config->name != pipe_config->name) && \
11763 (current_config->alt_name != pipe_config->name)) { \
11764 DRM_ERROR("mismatch in " #name " " \
11765 "(expected %i or %i, found %i)\n", \
11766 current_config->name, \
11767 current_config->alt_name, \
11768 pipe_config->name); \
11769 return false; \
11770 }
11771
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011772#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11773 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011774 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011775 "(expected %i, found %i)\n", \
11776 current_config->name & (mask), \
11777 pipe_config->name & (mask)); \
11778 return false; \
11779 }
11780
Ville Syrjälä5e550652013-09-06 23:29:07 +030011781#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11782 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11783 DRM_ERROR("mismatch in " #name " " \
11784 "(expected %i, found %i)\n", \
11785 current_config->name, \
11786 pipe_config->name); \
11787 return false; \
11788 }
11789
Daniel Vetterbb760062013-06-06 14:55:52 +020011790#define PIPE_CONF_QUIRK(quirk) \
11791 ((current_config->quirks | pipe_config->quirks) & (quirk))
11792
Daniel Vettereccb1402013-05-22 00:50:22 +020011793 PIPE_CONF_CHECK_I(cpu_transcoder);
11794
Daniel Vetter08a24032013-04-19 11:25:34 +020011795 PIPE_CONF_CHECK_I(has_pch_encoder);
11796 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011797 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11798 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11799 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11800 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11801 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011802
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011803 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011804
11805 if (INTEL_INFO(dev)->gen < 8) {
11806 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11807 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11808 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11809 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11810 PIPE_CONF_CHECK_I(dp_m_n.tu);
11811
11812 if (current_config->has_drrs) {
11813 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11814 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11815 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11816 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11817 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11818 }
11819 } else {
11820 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11821 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11822 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11823 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11824 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11825 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011826
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011827 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11828 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11829 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11830 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11831 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11832 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011833
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011834 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11835 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11836 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11837 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11838 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11839 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011840
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011841 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011842 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011843 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11844 IS_VALLEYVIEW(dev))
11845 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011846 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011847
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011848 PIPE_CONF_CHECK_I(has_audio);
11849
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011850 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011851 DRM_MODE_FLAG_INTERLACE);
11852
Daniel Vetterbb760062013-06-06 14:55:52 +020011853 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011854 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011855 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011856 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011857 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011858 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011859 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011860 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011861 DRM_MODE_FLAG_NVSYNC);
11862 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011863
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011864 PIPE_CONF_CHECK_I(pipe_src_w);
11865 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011866
Daniel Vetter99535992014-04-13 12:00:33 +020011867 /*
11868 * FIXME: BIOS likes to set up a cloned config with lvds+external
11869 * screen. Since we don't yet re-compute the pipe config when moving
11870 * just the lvds port away to another pipe the sw tracking won't match.
11871 *
11872 * Proper atomic modesets with recomputed global state will fix this.
11873 * Until then just don't check gmch state for inherited modes.
11874 */
11875 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11876 PIPE_CONF_CHECK_I(gmch_pfit.control);
11877 /* pfit ratios are autocomputed by the hw on gen4+ */
11878 if (INTEL_INFO(dev)->gen < 4)
11879 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11880 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11881 }
11882
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011883 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11884 if (current_config->pch_pfit.enabled) {
11885 PIPE_CONF_CHECK_I(pch_pfit.pos);
11886 PIPE_CONF_CHECK_I(pch_pfit.size);
11887 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011888
Chandra Kondurua1b22782015-04-07 15:28:45 -070011889 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11890
Jesse Barnese59150d2014-01-07 13:30:45 -080011891 /* BDW+ don't expose a synchronous way to read the state */
11892 if (IS_HASWELL(dev))
11893 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011894
Ville Syrjälä282740f2013-09-04 18:30:03 +030011895 PIPE_CONF_CHECK_I(double_wide);
11896
Daniel Vetter26804af2014-06-25 22:01:55 +030011897 PIPE_CONF_CHECK_X(ddi_pll_sel);
11898
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011899 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011900 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011901 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011902 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11903 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011904 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011905 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11906 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11907 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011908
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011909 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11910 PIPE_CONF_CHECK_I(pipe_bpp);
11911
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011912 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011913 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011914
Daniel Vetter66e985c2013-06-05 13:34:20 +020011915#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011916#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011917#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011918#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011919#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011920#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011921
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011922 return true;
11923}
11924
Damien Lespiau08db6652014-11-04 17:06:52 +000011925static void check_wm_state(struct drm_device *dev)
11926{
11927 struct drm_i915_private *dev_priv = dev->dev_private;
11928 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11929 struct intel_crtc *intel_crtc;
11930 int plane;
11931
11932 if (INTEL_INFO(dev)->gen < 9)
11933 return;
11934
11935 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11936 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11937
11938 for_each_intel_crtc(dev, intel_crtc) {
11939 struct skl_ddb_entry *hw_entry, *sw_entry;
11940 const enum pipe pipe = intel_crtc->pipe;
11941
11942 if (!intel_crtc->active)
11943 continue;
11944
11945 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011946 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011947 hw_entry = &hw_ddb.plane[pipe][plane];
11948 sw_entry = &sw_ddb->plane[pipe][plane];
11949
11950 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11951 continue;
11952
11953 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11954 "(expected (%u,%u), found (%u,%u))\n",
11955 pipe_name(pipe), plane + 1,
11956 sw_entry->start, sw_entry->end,
11957 hw_entry->start, hw_entry->end);
11958 }
11959
11960 /* cursor */
11961 hw_entry = &hw_ddb.cursor[pipe];
11962 sw_entry = &sw_ddb->cursor[pipe];
11963
11964 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11965 continue;
11966
11967 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11968 "(expected (%u,%u), found (%u,%u))\n",
11969 pipe_name(pipe),
11970 sw_entry->start, sw_entry->end,
11971 hw_entry->start, hw_entry->end);
11972 }
11973}
11974
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011975static void
11976check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011977{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011978 struct intel_connector *connector;
11979
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011980 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011981 /* This also checks the encoder/connector hw state with the
11982 * ->get_hw_state callbacks. */
11983 intel_connector_check_state(connector);
11984
Rob Clarke2c719b2014-12-15 13:56:32 -050011985 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011986 "connector's staged encoder doesn't match current encoder\n");
11987 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011988}
11989
11990static void
11991check_encoder_state(struct drm_device *dev)
11992{
11993 struct intel_encoder *encoder;
11994 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011995
Damien Lespiaub2784e12014-08-05 11:29:37 +010011996 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011997 bool enabled = false;
11998 bool active = false;
11999 enum pipe pipe, tracked_pipe;
12000
12001 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12002 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012003 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012004
Rob Clarke2c719b2014-12-15 13:56:32 -050012005 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012006 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012007 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012008 "encoder's active_connectors set, but no crtc\n");
12009
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012010 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012011 if (connector->base.encoder != &encoder->base)
12012 continue;
12013 enabled = true;
12014 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12015 active = true;
12016 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012017 /*
12018 * for MST connectors if we unplug the connector is gone
12019 * away but the encoder is still connected to a crtc
12020 * until a modeset happens in response to the hotplug.
12021 */
12022 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12023 continue;
12024
Rob Clarke2c719b2014-12-15 13:56:32 -050012025 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012026 "encoder's enabled state mismatch "
12027 "(expected %i, found %i)\n",
12028 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012029 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012030 "active encoder with no crtc\n");
12031
Rob Clarke2c719b2014-12-15 13:56:32 -050012032 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012033 "encoder's computed active state doesn't match tracked active state "
12034 "(expected %i, found %i)\n", active, encoder->connectors_active);
12035
12036 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012037 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012038 "encoder's hw state doesn't match sw tracking "
12039 "(expected %i, found %i)\n",
12040 encoder->connectors_active, active);
12041
12042 if (!encoder->base.crtc)
12043 continue;
12044
12045 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012046 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012047 "active encoder's pipe doesn't match"
12048 "(expected %i, found %i)\n",
12049 tracked_pipe, pipe);
12050
12051 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012052}
12053
12054static void
12055check_crtc_state(struct drm_device *dev)
12056{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012057 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012058 struct intel_crtc *crtc;
12059 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012060 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012061
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012062 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012063 bool enabled = false;
12064 bool active = false;
12065
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012066 memset(&pipe_config, 0, sizeof(pipe_config));
12067
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012068 DRM_DEBUG_KMS("[CRTC:%d]\n",
12069 crtc->base.base.id);
12070
Matt Roper83d65732015-02-25 13:12:16 -080012071 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012072 "active crtc, but not enabled in sw tracking\n");
12073
Damien Lespiaub2784e12014-08-05 11:29:37 +010012074 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012075 if (encoder->base.crtc != &crtc->base)
12076 continue;
12077 enabled = true;
12078 if (encoder->connectors_active)
12079 active = true;
12080 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012081
Rob Clarke2c719b2014-12-15 13:56:32 -050012082 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012083 "crtc's computed active state doesn't match tracked active state "
12084 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012085 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012086 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012087 "(expected %i, found %i)\n", enabled,
12088 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012089
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012090 active = dev_priv->display.get_pipe_config(crtc,
12091 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012092
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012093 /* hw state is inconsistent with the pipe quirk */
12094 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12095 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012096 active = crtc->active;
12097
Damien Lespiaub2784e12014-08-05 11:29:37 +010012098 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012099 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012100 if (encoder->base.crtc != &crtc->base)
12101 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012102 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012103 encoder->get_config(encoder, &pipe_config);
12104 }
12105
Rob Clarke2c719b2014-12-15 13:56:32 -050012106 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012107 "crtc active state doesn't match with hw state "
12108 "(expected %i, found %i)\n", crtc->active, active);
12109
Daniel Vetterc0b03412013-05-28 12:05:54 +020012110 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012111 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012112 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012113 intel_dump_pipe_config(crtc, &pipe_config,
12114 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012115 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012116 "[sw state]");
12117 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012118 }
12119}
12120
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012121static void
12122check_shared_dpll_state(struct drm_device *dev)
12123{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012124 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012125 struct intel_crtc *crtc;
12126 struct intel_dpll_hw_state dpll_hw_state;
12127 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012128
12129 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12130 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12131 int enabled_crtcs = 0, active_crtcs = 0;
12132 bool active;
12133
12134 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12135
12136 DRM_DEBUG_KMS("%s\n", pll->name);
12137
12138 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12139
Rob Clarke2c719b2014-12-15 13:56:32 -050012140 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012141 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012142 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012143 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012144 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012145 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012146 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012147 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012148 "pll on state mismatch (expected %i, found %i)\n",
12149 pll->on, active);
12150
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012151 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012152 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012153 enabled_crtcs++;
12154 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12155 active_crtcs++;
12156 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012157 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012158 "pll active crtcs mismatch (expected %i, found %i)\n",
12159 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012160 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012161 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012162 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012163
Rob Clarke2c719b2014-12-15 13:56:32 -050012164 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012165 sizeof(dpll_hw_state)),
12166 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012167 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012168}
12169
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012170void
12171intel_modeset_check_state(struct drm_device *dev)
12172{
Damien Lespiau08db6652014-11-04 17:06:52 +000012173 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012174 check_connector_state(dev);
12175 check_encoder_state(dev);
12176 check_crtc_state(dev);
12177 check_shared_dpll_state(dev);
12178}
12179
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012180void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012181 int dotclock)
12182{
12183 /*
12184 * FDI already provided one idea for the dotclock.
12185 * Yell if the encoder disagrees.
12186 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012187 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012188 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012189 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012190}
12191
Ville Syrjälä80715b22014-05-15 20:23:23 +030012192static void update_scanline_offset(struct intel_crtc *crtc)
12193{
12194 struct drm_device *dev = crtc->base.dev;
12195
12196 /*
12197 * The scanline counter increments at the leading edge of hsync.
12198 *
12199 * On most platforms it starts counting from vtotal-1 on the
12200 * first active line. That means the scanline counter value is
12201 * always one less than what we would expect. Ie. just after
12202 * start of vblank, which also occurs at start of hsync (on the
12203 * last active line), the scanline counter will read vblank_start-1.
12204 *
12205 * On gen2 the scanline counter starts counting from 1 instead
12206 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12207 * to keep the value positive), instead of adding one.
12208 *
12209 * On HSW+ the behaviour of the scanline counter depends on the output
12210 * type. For DP ports it behaves like most other platforms, but on HDMI
12211 * there's an extra 1 line difference. So we need to add two instead of
12212 * one to the value.
12213 */
12214 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012215 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012216 int vtotal;
12217
12218 vtotal = mode->crtc_vtotal;
12219 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12220 vtotal /= 2;
12221
12222 crtc->scanline_offset = vtotal - 1;
12223 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012224 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012225 crtc->scanline_offset = 2;
12226 } else
12227 crtc->scanline_offset = 1;
12228}
12229
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012230static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012231intel_modeset_compute_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012232 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012233{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012234 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012235 int ret = 0;
12236
12237 ret = drm_atomic_add_affected_connectors(state, crtc);
12238 if (ret)
12239 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012240
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012241 ret = drm_atomic_helper_check_modeset(state->dev, state);
12242 if (ret)
12243 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012244
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012245 /*
12246 * Note this needs changes when we start tracking multiple modes
12247 * and crtcs. At that point we'll need to compute the whole config
12248 * (i.e. one pipe_config for each crtc) rather than just the one
12249 * for this crtc.
12250 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012251 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12252 if (IS_ERR(pipe_config))
12253 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012254
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012255 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012256 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012257
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012258 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012259 if (ret)
12260 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012261
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012262 /* Check things that can only be changed through modeset */
12263 if (pipe_config->has_audio !=
12264 to_intel_crtc(crtc)->config->has_audio)
12265 pipe_config->base.mode_changed = true;
12266
12267 /*
12268 * Note we have an issue here with infoframes: current code
12269 * only updates them on the full mode set path per hw
12270 * requirements. So here we should be checking for any
12271 * required changes and forcing a mode set.
12272 */
12273
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012274 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
12275
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012276 ret = drm_atomic_helper_check_planes(state->dev, state);
12277 if (ret)
12278 return ERR_PTR(ret);
12279
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012280 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012281}
12282
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012283static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012284{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012285 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012286 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012287 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012288 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012289 struct intel_crtc_state *intel_crtc_state;
12290 struct drm_crtc *crtc;
12291 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012292 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012293 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012294
12295 if (!dev_priv->display.crtc_compute_clock)
12296 return 0;
12297
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012298 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12299 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012300 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012301
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012302 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012303 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012304 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012305 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012306 }
12307
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012308 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12309 if (ret)
12310 goto done;
12311
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012312 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12313 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012314 continue;
12315
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012316 intel_crtc = to_intel_crtc(crtc);
12317 intel_crtc_state = to_intel_crtc_state(crtc_state);
12318
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012319 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012320 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012321 if (ret) {
12322 intel_shared_dpll_abort_config(dev_priv);
12323 goto done;
12324 }
12325 }
12326
12327done:
12328 return ret;
12329}
12330
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012331/* Code that should eventually be part of atomic_check() */
12332static int __intel_set_mode_checks(struct drm_atomic_state *state)
12333{
12334 struct drm_device *dev = state->dev;
12335 int ret;
12336
12337 /*
12338 * See if the config requires any additional preparation, e.g.
12339 * to adjust global state with pipes off. We need to do this
12340 * here so we can get the modeset_pipe updated config for the new
12341 * mode set on this crtc. For other crtcs we need to use the
12342 * adjusted_mode bits in the crtc directly.
12343 */
12344 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
12345 ret = valleyview_modeset_global_pipes(state);
12346 if (ret)
12347 return ret;
12348 }
12349
12350 ret = __intel_set_mode_setup_plls(state);
12351 if (ret)
12352 return ret;
12353
12354 return 0;
12355}
12356
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012357static int __intel_set_mode(struct drm_crtc *modeset_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012358 struct intel_crtc_state *pipe_config)
Daniel Vettera6778b32012-07-02 09:56:42 +020012359{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012360 struct drm_device *dev = modeset_crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012361 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012362 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012363 struct drm_crtc *crtc;
12364 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012365 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012366 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012367
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012368 ret = __intel_set_mode_checks(state);
12369 if (ret < 0)
12370 return ret;
12371
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012372 ret = drm_atomic_helper_prepare_planes(dev, state);
12373 if (ret)
12374 return ret;
12375
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012376 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12377 if (!needs_modeset(crtc_state))
12378 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012379
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012380 if (!crtc_state->enable) {
12381 intel_crtc_disable(crtc);
12382 } else if (crtc->state->enable) {
12383 intel_crtc_disable_planes(crtc);
12384 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012385 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012386 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012387
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012388 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12389 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012390 *
12391 * Note we'll need to fix this up when we start tracking multiple
12392 * pipes; here we assume a single modeset_pipe and only track the
12393 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012394 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012395 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012396 modeset_crtc->mode = pipe_config->base.mode;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012397
12398 /*
12399 * Calculate and store various constants which
12400 * are later needed by vblank and swap-completion
12401 * timestamping. They are derived from true hwmode.
12402 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012403 drm_calc_timestamping_constants(modeset_crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012404 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012405 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012406
Daniel Vetterea9d7582012-07-10 10:42:52 +020012407 /* Only after disabling all output pipelines that will be changed can we
12408 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012409 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012410
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012411 /* The state has been swaped above, so state actually contains the
12412 * old state now. */
12413
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012414 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012415
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012416 drm_atomic_helper_commit_planes(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012417
12418 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012419 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012420 if (!needs_modeset(crtc->state) || !crtc->state->enable)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012421 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012422
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012423 update_scanline_offset(to_intel_crtc(crtc));
12424
12425 dev_priv->display.crtc_enable(crtc);
12426 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012427 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012428
Daniel Vettera6778b32012-07-02 09:56:42 +020012429 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012430
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012431 drm_atomic_helper_cleanup_planes(dev, state);
12432
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012433 drm_atomic_state_free(state);
12434
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012435 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012436}
12437
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012438static int intel_set_mode_with_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012439 struct intel_crtc_state *pipe_config)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012440{
12441 int ret;
12442
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012443 ret = __intel_set_mode(crtc, pipe_config);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012444
12445 if (ret == 0)
12446 intel_modeset_check_state(crtc->dev);
12447
12448 return ret;
12449}
12450
Damien Lespiaue7457a92013-08-08 22:28:59 +010012451static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012452 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012453{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012454 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012455 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012456
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012457 pipe_config = intel_modeset_compute_config(crtc, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012458 if (IS_ERR(pipe_config)) {
12459 ret = PTR_ERR(pipe_config);
12460 goto out;
12461 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012462
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012463 ret = intel_set_mode_with_config(crtc, pipe_config);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012464 if (ret)
12465 goto out;
12466
12467out:
12468 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012469}
12470
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012471void intel_crtc_restore_mode(struct drm_crtc *crtc)
12472{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012473 struct drm_device *dev = crtc->dev;
12474 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012475 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012476 struct intel_encoder *encoder;
12477 struct intel_connector *connector;
12478 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012479 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012480 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012481
12482 state = drm_atomic_state_alloc(dev);
12483 if (!state) {
12484 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12485 crtc->base.id);
12486 return;
12487 }
12488
12489 state->acquire_ctx = dev->mode_config.acquire_ctx;
12490
12491 /* The force restore path in the HW readout code relies on the staged
12492 * config still keeping the user requested config while the actual
12493 * state has been overwritten by the configuration read from HW. We
12494 * need to copy the staged config to the atomic state, otherwise the
12495 * mode set will just reapply the state the HW is already in. */
12496 for_each_intel_encoder(dev, encoder) {
12497 if (&encoder->new_crtc->base != crtc)
12498 continue;
12499
12500 for_each_intel_connector(dev, connector) {
12501 if (connector->new_encoder != encoder)
12502 continue;
12503
12504 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12505 if (IS_ERR(connector_state)) {
12506 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12507 connector->base.base.id,
12508 connector->base.name,
12509 PTR_ERR(connector_state));
12510 continue;
12511 }
12512
12513 connector_state->crtc = crtc;
12514 connector_state->best_encoder = &encoder->base;
12515 }
12516 }
12517
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012518 for_each_intel_crtc(dev, intel_crtc) {
12519 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
12520 continue;
12521
12522 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12523 if (IS_ERR(crtc_state)) {
12524 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12525 intel_crtc->base.base.id,
12526 PTR_ERR(crtc_state));
12527 continue;
12528 }
12529
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012530 crtc_state->base.active = crtc_state->base.enable =
12531 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012532
12533 if (&intel_crtc->base == crtc)
12534 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012535 }
12536
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012537 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
12538 crtc->primary->fb, crtc->x, crtc->y);
12539
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012540 ret = intel_set_mode(crtc, state);
12541 if (ret)
12542 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012543}
12544
Daniel Vetter25c5b262012-07-08 22:08:04 +020012545#undef for_each_intel_crtc_masked
12546
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012547static bool intel_connector_in_mode_set(struct intel_connector *connector,
12548 struct drm_mode_set *set)
12549{
12550 int ro;
12551
12552 for (ro = 0; ro < set->num_connectors; ro++)
12553 if (set->connectors[ro] == &connector->base)
12554 return true;
12555
12556 return false;
12557}
12558
Daniel Vetter2e431052012-07-04 22:42:15 +020012559static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012560intel_modeset_stage_output_state(struct drm_device *dev,
12561 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012562 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012563{
Daniel Vetter9a935852012-07-05 22:34:27 +020012564 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012565 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012566 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012567 struct drm_crtc *crtc;
12568 struct drm_crtc_state *crtc_state;
12569 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020012570
Damien Lespiau9abdda72013-02-13 13:29:23 +000012571 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012572 * of connectors. For paranoia, double-check this. */
12573 WARN_ON(!set->fb && (set->num_connectors != 0));
12574 WARN_ON(set->fb && (set->num_connectors == 0));
12575
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012576 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012577 bool in_mode_set = intel_connector_in_mode_set(connector, set);
12578
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012579 if (!in_mode_set && connector->base.state->crtc != set->crtc)
12580 continue;
12581
12582 connector_state =
12583 drm_atomic_get_connector_state(state, &connector->base);
12584 if (IS_ERR(connector_state))
12585 return PTR_ERR(connector_state);
12586
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012587 if (in_mode_set) {
12588 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012589 connector_state->best_encoder =
12590 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020012591 }
12592
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012593 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012594 continue;
12595
Daniel Vetter9a935852012-07-05 22:34:27 +020012596 /* If we disable the crtc, disable all its connectors. Also, if
12597 * the connector is on the changing crtc but not on the new
12598 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012599 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012600 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020012601
12602 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12603 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012604 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012605 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012606 }
12607 /* connector->new_encoder is now updated for all connectors. */
12608
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012609 for_each_connector_in_state(state, drm_connector, connector_state, i) {
12610 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020012611
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012612 if (!connector_state->best_encoder) {
12613 ret = drm_atomic_set_crtc_for_connector(connector_state,
12614 NULL);
12615 if (ret)
12616 return ret;
12617
Daniel Vetter50f56112012-07-02 09:35:43 +020012618 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012619 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012620
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012621 if (intel_connector_in_mode_set(connector, set)) {
12622 struct drm_crtc *crtc = connector->base.state->crtc;
12623
12624 /* If this connector was in a previous crtc, add it
12625 * to the state. We might need to disable it. */
12626 if (crtc) {
12627 crtc_state =
12628 drm_atomic_get_crtc_state(state, crtc);
12629 if (IS_ERR(crtc_state))
12630 return PTR_ERR(crtc_state);
12631 }
12632
12633 ret = drm_atomic_set_crtc_for_connector(connector_state,
12634 set->crtc);
12635 if (ret)
12636 return ret;
12637 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012638
12639 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012640 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
12641 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012642 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012643 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012644
Daniel Vetter9a935852012-07-05 22:34:27 +020012645 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12646 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012647 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012648 connector_state->crtc->base.id);
12649
12650 if (connector_state->best_encoder != &connector->encoder->base)
12651 connector->encoder =
12652 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020012653 }
12654
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012655 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012656 bool has_connectors;
12657
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012658 ret = drm_atomic_add_affected_connectors(state, crtc);
12659 if (ret)
12660 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012661
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012662 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
12663 if (has_connectors != crtc_state->enable)
12664 crtc_state->enable =
12665 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020012666 }
12667
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012668 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
12669 set->fb, set->x, set->y);
12670 if (ret)
12671 return ret;
12672
12673 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
12674 if (IS_ERR(crtc_state))
12675 return PTR_ERR(crtc_state);
12676
12677 if (set->mode)
12678 drm_mode_copy(&crtc_state->mode, set->mode);
12679
12680 if (set->num_connectors)
12681 crtc_state->active = true;
12682
Daniel Vetter2e431052012-07-04 22:42:15 +020012683 return 0;
12684}
12685
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012686static bool primary_plane_visible(struct drm_crtc *crtc)
12687{
12688 struct intel_plane_state *plane_state =
12689 to_intel_plane_state(crtc->primary->state);
12690
12691 return plane_state->visible;
12692}
12693
Daniel Vetter2e431052012-07-04 22:42:15 +020012694static int intel_crtc_set_config(struct drm_mode_set *set)
12695{
12696 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012697 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012698 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012699 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020012700 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012701
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012702 BUG_ON(!set);
12703 BUG_ON(!set->crtc);
12704 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012705
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012706 /* Enforce sane interface api - has been abused by the fb helper. */
12707 BUG_ON(!set->mode && set->fb);
12708 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012709
Daniel Vetter2e431052012-07-04 22:42:15 +020012710 if (set->fb) {
12711 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12712 set->crtc->base.id, set->fb->base.id,
12713 (int)set->num_connectors, set->x, set->y);
12714 } else {
12715 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012716 }
12717
12718 dev = set->crtc->dev;
12719
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012720 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012721 if (!state)
12722 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012723
12724 state->acquire_ctx = dev->mode_config.acquire_ctx;
12725
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012726 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012727 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012728 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020012729
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012730 pipe_config = intel_modeset_compute_config(set->crtc, state);
Jesse Barnes20664592014-11-05 14:26:09 -080012731 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012732 ret = PTR_ERR(pipe_config);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012733 goto out;
Jesse Barnes20664592014-11-05 14:26:09 -080012734 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012735
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012736 intel_update_pipe_size(to_intel_crtc(set->crtc));
12737
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012738 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012739
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012740 ret = intel_set_mode_with_config(set->crtc, pipe_config);
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012741
12742 if (ret == 0 &&
12743 pipe_config->base.enable &&
12744 pipe_config->base.planes_changed &&
12745 !needs_modeset(&pipe_config->base)) {
12746 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012747
12748 /*
12749 * We need to make sure the primary plane is re-enabled if it
12750 * has previously been turned off.
12751 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012752 if (ret == 0 && !primary_plane_was_visible &&
12753 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070012754 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030012755 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012756 }
12757
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012758 /*
12759 * In the fastboot case this may be our only check of the
12760 * state after boot. It would be better to only do it on
12761 * the first update, but we don't have a nice way of doing that
12762 * (and really, set_config isn't used much for high freq page
12763 * flipping, so increasing its cost here shouldn't be a big
12764 * deal).
12765 */
Jani Nikulad330a952014-01-21 11:24:25 +020012766 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012767 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012768 }
12769
Chris Wilson2d05eae2013-05-03 17:36:25 +010012770 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012771 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12772 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010012773 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012774
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012775out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012776 if (ret)
12777 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020012778 return ret;
12779}
12780
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012781static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012782 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012783 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012784 .destroy = intel_crtc_destroy,
12785 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012786 .atomic_duplicate_state = intel_crtc_duplicate_state,
12787 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012788};
12789
Daniel Vetter53589012013-06-05 13:34:16 +020012790static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12791 struct intel_shared_dpll *pll,
12792 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012793{
Daniel Vetter53589012013-06-05 13:34:16 +020012794 uint32_t val;
12795
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012796 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012797 return false;
12798
Daniel Vetter53589012013-06-05 13:34:16 +020012799 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012800 hw_state->dpll = val;
12801 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12802 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012803
12804 return val & DPLL_VCO_ENABLE;
12805}
12806
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012807static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12808 struct intel_shared_dpll *pll)
12809{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012810 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12811 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012812}
12813
Daniel Vettere7b903d2013-06-05 13:34:14 +020012814static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12815 struct intel_shared_dpll *pll)
12816{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012817 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012818 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012819
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012820 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012821
12822 /* Wait for the clocks to stabilize. */
12823 POSTING_READ(PCH_DPLL(pll->id));
12824 udelay(150);
12825
12826 /* The pixel multiplier can only be updated once the
12827 * DPLL is enabled and the clocks are stable.
12828 *
12829 * So write it again.
12830 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012831 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012832 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012833 udelay(200);
12834}
12835
12836static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12837 struct intel_shared_dpll *pll)
12838{
12839 struct drm_device *dev = dev_priv->dev;
12840 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012841
12842 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012843 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020012844 if (intel_crtc_to_shared_dpll(crtc) == pll)
12845 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12846 }
12847
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012848 I915_WRITE(PCH_DPLL(pll->id), 0);
12849 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012850 udelay(200);
12851}
12852
Daniel Vetter46edb022013-06-05 13:34:12 +020012853static char *ibx_pch_dpll_names[] = {
12854 "PCH DPLL A",
12855 "PCH DPLL B",
12856};
12857
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012858static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012859{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012860 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012861 int i;
12862
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012863 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012864
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012865 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020012866 dev_priv->shared_dplls[i].id = i;
12867 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012868 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012869 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12870 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020012871 dev_priv->shared_dplls[i].get_hw_state =
12872 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012873 }
12874}
12875
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012876static void intel_shared_dpll_init(struct drm_device *dev)
12877{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012878 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012879
Daniel Vetter9cd86932014-06-25 22:01:57 +030012880 if (HAS_DDI(dev))
12881 intel_ddi_pll_init(dev);
12882 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012883 ibx_pch_dpll_init(dev);
12884 else
12885 dev_priv->num_shared_dpll = 0;
12886
12887 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012888}
12889
Matt Roper6beb8c232014-12-01 15:40:14 -080012890/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012891 * intel_wm_need_update - Check whether watermarks need updating
12892 * @plane: drm plane
12893 * @state: new plane state
12894 *
12895 * Check current plane state versus the new one to determine whether
12896 * watermarks need to be recalculated.
12897 *
12898 * Returns true or false.
12899 */
12900bool intel_wm_need_update(struct drm_plane *plane,
12901 struct drm_plane_state *state)
12902{
12903 /* Update watermarks on tiling changes. */
12904 if (!plane->state->fb || !state->fb ||
12905 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12906 plane->state->rotation != state->rotation)
12907 return true;
12908
12909 return false;
12910}
12911
12912/**
Matt Roper6beb8c232014-12-01 15:40:14 -080012913 * intel_prepare_plane_fb - Prepare fb for usage on plane
12914 * @plane: drm plane to prepare for
12915 * @fb: framebuffer to prepare for presentation
12916 *
12917 * Prepares a framebuffer for usage on a display plane. Generally this
12918 * involves pinning the underlying object and updating the frontbuffer tracking
12919 * bits. Some older platforms need special physical address handling for
12920 * cursor planes.
12921 *
12922 * Returns 0 on success, negative error code on failure.
12923 */
12924int
12925intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012926 struct drm_framebuffer *fb,
12927 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070012928{
12929 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080012930 struct intel_plane *intel_plane = to_intel_plane(plane);
12931 enum pipe pipe = intel_plane->pipe;
12932 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12933 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12934 unsigned frontbuffer_bits = 0;
12935 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070012936
Matt Roperea2c67b2014-12-23 10:41:52 -080012937 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070012938 return 0;
12939
Matt Roper6beb8c232014-12-01 15:40:14 -080012940 switch (plane->type) {
12941 case DRM_PLANE_TYPE_PRIMARY:
12942 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12943 break;
12944 case DRM_PLANE_TYPE_CURSOR:
12945 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12946 break;
12947 case DRM_PLANE_TYPE_OVERLAY:
12948 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12949 break;
12950 }
Matt Roper465c1202014-05-29 08:06:54 -070012951
Matt Roper4c345742014-07-09 16:22:10 -070012952 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070012953
Matt Roper6beb8c232014-12-01 15:40:14 -080012954 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12955 INTEL_INFO(dev)->cursor_needs_physical) {
12956 int align = IS_I830(dev) ? 16 * 1024 : 256;
12957 ret = i915_gem_object_attach_phys(obj, align);
12958 if (ret)
12959 DRM_DEBUG_KMS("failed to attach phys object\n");
12960 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012961 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080012962 }
12963
12964 if (ret == 0)
12965 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
12966
12967 mutex_unlock(&dev->struct_mutex);
12968
12969 return ret;
12970}
12971
Matt Roper38f3ce32014-12-02 07:45:25 -080012972/**
12973 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12974 * @plane: drm plane to clean up for
12975 * @fb: old framebuffer that was on plane
12976 *
12977 * Cleans up a framebuffer that has just been removed from a plane.
12978 */
12979void
12980intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000012981 struct drm_framebuffer *fb,
12982 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080012983{
12984 struct drm_device *dev = plane->dev;
12985 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12986
12987 if (WARN_ON(!obj))
12988 return;
12989
12990 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12991 !INTEL_INFO(dev)->cursor_needs_physical) {
12992 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000012993 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080012994 mutex_unlock(&dev->struct_mutex);
12995 }
Matt Roper465c1202014-05-29 08:06:54 -070012996}
12997
Chandra Konduru6156a452015-04-27 13:48:39 -070012998int
12999skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13000{
13001 int max_scale;
13002 struct drm_device *dev;
13003 struct drm_i915_private *dev_priv;
13004 int crtc_clock, cdclk;
13005
13006 if (!intel_crtc || !crtc_state)
13007 return DRM_PLANE_HELPER_NO_SCALING;
13008
13009 dev = intel_crtc->base.dev;
13010 dev_priv = dev->dev_private;
13011 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13012 cdclk = dev_priv->display.get_display_clock_speed(dev);
13013
13014 if (!crtc_clock || !cdclk)
13015 return DRM_PLANE_HELPER_NO_SCALING;
13016
13017 /*
13018 * skl max scale is lower of:
13019 * close to 3 but not 3, -1 is for that purpose
13020 * or
13021 * cdclk/crtc_clock
13022 */
13023 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13024
13025 return max_scale;
13026}
13027
Matt Roper465c1202014-05-29 08:06:54 -070013028static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013029intel_check_primary_plane(struct drm_plane *plane,
13030 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013031{
Matt Roper32b7eee2014-12-24 07:59:06 -080013032 struct drm_device *dev = plane->dev;
13033 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013034 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013035 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013036 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013037 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013038 struct drm_rect *dest = &state->dst;
13039 struct drm_rect *src = &state->src;
13040 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013041 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013042 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13043 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013044 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013045
Matt Roperea2c67b2014-12-23 10:41:52 -080013046 crtc = crtc ? crtc : plane->crtc;
13047 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013048 crtc_state = state->base.state ?
13049 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013050
Chandra Konduru6156a452015-04-27 13:48:39 -070013051 if (INTEL_INFO(dev)->gen >= 9) {
13052 min_scale = 1;
13053 max_scale = skl_max_scale(intel_crtc, crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013054 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013055 }
Sonika Jindald8106362015-04-10 14:37:28 +053013056
Matt Roperc59cb172014-12-01 15:40:16 -080013057 ret = drm_plane_helper_check_update(plane, crtc, fb,
13058 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013059 min_scale,
13060 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013061 can_position, true,
13062 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013063 if (ret)
13064 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013065
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013066 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013067 struct intel_plane_state *old_state =
13068 to_intel_plane_state(plane->state);
13069
Matt Roper32b7eee2014-12-24 07:59:06 -080013070 intel_crtc->atomic.wait_for_flips = true;
13071
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013072 /*
13073 * FBC does not work on some platforms for rotated
13074 * planes, so disable it when rotation is not 0 and
13075 * update it when rotation is set back to 0.
13076 *
13077 * FIXME: This is redundant with the fbc update done in
13078 * the primary plane enable function except that that
13079 * one is done too late. We eventually need to unify
13080 * this.
13081 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013082 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013083 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013084 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013085 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013086 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013087 }
13088
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013089 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013090 /*
13091 * BDW signals flip done immediately if the plane
13092 * is disabled, even if the plane enable is already
13093 * armed to occur at the next vblank :(
13094 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013095 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013096 intel_crtc->atomic.wait_vblank = true;
13097 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013098
Matt Roper32b7eee2014-12-24 07:59:06 -080013099 intel_crtc->atomic.fb_bits |=
13100 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13101
13102 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013103
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013104 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013105 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013106 }
13107
Chandra Konduru6156a452015-04-27 13:48:39 -070013108 if (INTEL_INFO(dev)->gen >= 9) {
13109 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13110 to_intel_plane(plane), state, 0);
13111 if (ret)
13112 return ret;
13113 }
13114
Matt Roperc59cb172014-12-01 15:40:16 -080013115 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013116}
13117
Sonika Jindal48404c12014-08-22 14:06:04 +053013118static void
13119intel_commit_primary_plane(struct drm_plane *plane,
13120 struct intel_plane_state *state)
13121{
Matt Roper2b875c22014-12-01 15:40:13 -080013122 struct drm_crtc *crtc = state->base.crtc;
13123 struct drm_framebuffer *fb = state->base.fb;
13124 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013125 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013126 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013127 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013128
Matt Roperea2c67b2014-12-23 10:41:52 -080013129 crtc = crtc ? crtc : plane->crtc;
13130 intel_crtc = to_intel_crtc(crtc);
13131
Matt Ropercf4c7c12014-12-04 10:27:42 -080013132 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013133 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013134 crtc->y = src->y1 >> 16;
13135
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013136 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013137 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013138 /* FIXME: kill this fastboot hack */
13139 intel_update_pipe_size(intel_crtc);
13140
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013141 dev_priv->display.update_primary_plane(crtc, plane->fb,
13142 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013143 }
13144}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013145
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013146static void
13147intel_disable_primary_plane(struct drm_plane *plane,
13148 struct drm_crtc *crtc,
13149 bool force)
13150{
13151 struct drm_device *dev = plane->dev;
13152 struct drm_i915_private *dev_priv = dev->dev_private;
13153
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013154 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13155}
13156
Matt Roper32b7eee2014-12-24 07:59:06 -080013157static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13158{
13159 struct drm_device *dev = crtc->dev;
13160 struct drm_i915_private *dev_priv = dev->dev_private;
13161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013162 struct intel_plane *intel_plane;
13163 struct drm_plane *p;
13164 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013165
Matt Roperea2c67b2014-12-23 10:41:52 -080013166 /* Track fb's for any planes being disabled */
13167 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13168 intel_plane = to_intel_plane(p);
13169
13170 if (intel_crtc->atomic.disabled_planes &
13171 (1 << drm_plane_index(p))) {
13172 switch (p->type) {
13173 case DRM_PLANE_TYPE_PRIMARY:
13174 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13175 break;
13176 case DRM_PLANE_TYPE_CURSOR:
13177 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13178 break;
13179 case DRM_PLANE_TYPE_OVERLAY:
13180 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13181 break;
13182 }
13183
13184 mutex_lock(&dev->struct_mutex);
13185 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13186 mutex_unlock(&dev->struct_mutex);
13187 }
13188 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013189
Matt Roper32b7eee2014-12-24 07:59:06 -080013190 if (intel_crtc->atomic.wait_for_flips)
13191 intel_crtc_wait_for_pending_flips(crtc);
13192
13193 if (intel_crtc->atomic.disable_fbc)
13194 intel_fbc_disable(dev);
13195
13196 if (intel_crtc->atomic.pre_disable_primary)
13197 intel_pre_disable_primary(crtc);
13198
13199 if (intel_crtc->atomic.update_wm)
13200 intel_update_watermarks(crtc);
13201
13202 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013203
13204 /* Perform vblank evasion around commit operation */
13205 if (intel_crtc->active)
13206 intel_crtc->atomic.evade =
13207 intel_pipe_update_start(intel_crtc,
13208 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013209}
13210
13211static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13212{
13213 struct drm_device *dev = crtc->dev;
13214 struct drm_i915_private *dev_priv = dev->dev_private;
13215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13216 struct drm_plane *p;
13217
Matt Roperc34c9ee2014-12-23 10:41:50 -080013218 if (intel_crtc->atomic.evade)
13219 intel_pipe_update_end(intel_crtc,
13220 intel_crtc->atomic.start_vbl_count);
13221
Matt Roper32b7eee2014-12-24 07:59:06 -080013222 intel_runtime_pm_put(dev_priv);
13223
13224 if (intel_crtc->atomic.wait_vblank)
13225 intel_wait_for_vblank(dev, intel_crtc->pipe);
13226
13227 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13228
13229 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013230 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013231 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013232 mutex_unlock(&dev->struct_mutex);
13233 }
Matt Roper465c1202014-05-29 08:06:54 -070013234
Matt Roper32b7eee2014-12-24 07:59:06 -080013235 if (intel_crtc->atomic.post_enable_primary)
13236 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013237
Matt Roper32b7eee2014-12-24 07:59:06 -080013238 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13239 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13240 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13241 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013242
Matt Roper32b7eee2014-12-24 07:59:06 -080013243 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013244}
13245
Matt Ropercf4c7c12014-12-04 10:27:42 -080013246/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013247 * intel_plane_destroy - destroy a plane
13248 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013249 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013250 * Common destruction function for all types of planes (primary, cursor,
13251 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013252 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013253void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013254{
13255 struct intel_plane *intel_plane = to_intel_plane(plane);
13256 drm_plane_cleanup(plane);
13257 kfree(intel_plane);
13258}
13259
Matt Roper65a3fea2015-01-21 16:35:42 -080013260const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013261 .update_plane = drm_atomic_helper_update_plane,
13262 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013263 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013264 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013265 .atomic_get_property = intel_plane_atomic_get_property,
13266 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013267 .atomic_duplicate_state = intel_plane_duplicate_state,
13268 .atomic_destroy_state = intel_plane_destroy_state,
13269
Matt Roper465c1202014-05-29 08:06:54 -070013270};
13271
13272static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13273 int pipe)
13274{
13275 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013276 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013277 const uint32_t *intel_primary_formats;
13278 int num_formats;
13279
13280 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13281 if (primary == NULL)
13282 return NULL;
13283
Matt Roper8e7d6882015-01-21 16:35:41 -080013284 state = intel_create_plane_state(&primary->base);
13285 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013286 kfree(primary);
13287 return NULL;
13288 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013289 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013290
Matt Roper465c1202014-05-29 08:06:54 -070013291 primary->can_scale = false;
13292 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013293 if (INTEL_INFO(dev)->gen >= 9) {
13294 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013295 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013296 }
Matt Roper465c1202014-05-29 08:06:54 -070013297 primary->pipe = pipe;
13298 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013299 primary->check_plane = intel_check_primary_plane;
13300 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013301 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013302 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013303 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13304 primary->plane = !pipe;
13305
13306 if (INTEL_INFO(dev)->gen <= 3) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013307 intel_primary_formats = i8xx_primary_formats;
13308 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013309 } else {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013310 intel_primary_formats = i965_primary_formats;
13311 num_formats = ARRAY_SIZE(i965_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013312 }
13313
13314 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013315 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013316 intel_primary_formats, num_formats,
13317 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013318
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013319 if (INTEL_INFO(dev)->gen >= 4)
13320 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013321
Matt Roperea2c67b2014-12-23 10:41:52 -080013322 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13323
Matt Roper465c1202014-05-29 08:06:54 -070013324 return &primary->base;
13325}
13326
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013327void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13328{
13329 if (!dev->mode_config.rotation_property) {
13330 unsigned long flags = BIT(DRM_ROTATE_0) |
13331 BIT(DRM_ROTATE_180);
13332
13333 if (INTEL_INFO(dev)->gen >= 9)
13334 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13335
13336 dev->mode_config.rotation_property =
13337 drm_mode_create_rotation_property(dev, flags);
13338 }
13339 if (dev->mode_config.rotation_property)
13340 drm_object_attach_property(&plane->base.base,
13341 dev->mode_config.rotation_property,
13342 plane->base.state->rotation);
13343}
13344
Matt Roper3d7d6512014-06-10 08:28:13 -070013345static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013346intel_check_cursor_plane(struct drm_plane *plane,
13347 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013348{
Matt Roper2b875c22014-12-01 15:40:13 -080013349 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013350 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013351 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013352 struct drm_rect *dest = &state->dst;
13353 struct drm_rect *src = &state->src;
13354 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013355 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013356 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013357 unsigned stride;
13358 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013359
Matt Roperea2c67b2014-12-23 10:41:52 -080013360 crtc = crtc ? crtc : plane->crtc;
13361 intel_crtc = to_intel_crtc(crtc);
13362
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013363 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013364 src, dest, clip,
13365 DRM_PLANE_HELPER_NO_SCALING,
13366 DRM_PLANE_HELPER_NO_SCALING,
13367 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013368 if (ret)
13369 return ret;
13370
13371
13372 /* if we want to turn off the cursor ignore width and height */
13373 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013374 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013375
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013376 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013377 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13378 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13379 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013380 return -EINVAL;
13381 }
13382
Matt Roperea2c67b2014-12-23 10:41:52 -080013383 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13384 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013385 DRM_DEBUG_KMS("buffer is too small\n");
13386 return -ENOMEM;
13387 }
13388
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013389 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013390 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13391 ret = -EINVAL;
13392 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013393
Matt Roper32b7eee2014-12-24 07:59:06 -080013394finish:
13395 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013396 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013397 intel_crtc->atomic.update_wm = true;
13398
13399 intel_crtc->atomic.fb_bits |=
13400 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13401 }
13402
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013403 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013404}
13405
Matt Roperf4a2cf22014-12-01 15:40:12 -080013406static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013407intel_disable_cursor_plane(struct drm_plane *plane,
13408 struct drm_crtc *crtc,
13409 bool force)
13410{
13411 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13412
13413 if (!force) {
13414 plane->fb = NULL;
13415 intel_crtc->cursor_bo = NULL;
13416 intel_crtc->cursor_addr = 0;
13417 }
13418
13419 intel_crtc_update_cursor(crtc, false);
13420}
13421
13422static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013423intel_commit_cursor_plane(struct drm_plane *plane,
13424 struct intel_plane_state *state)
13425{
Matt Roper2b875c22014-12-01 15:40:13 -080013426 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013427 struct drm_device *dev = plane->dev;
13428 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013429 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013430 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013431
Matt Roperea2c67b2014-12-23 10:41:52 -080013432 crtc = crtc ? crtc : plane->crtc;
13433 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013434
Matt Roperea2c67b2014-12-23 10:41:52 -080013435 plane->fb = state->base.fb;
13436 crtc->cursor_x = state->base.crtc_x;
13437 crtc->cursor_y = state->base.crtc_y;
13438
Gustavo Padovana912f122014-12-01 15:40:10 -080013439 if (intel_crtc->cursor_bo == obj)
13440 goto update;
13441
Matt Roperf4a2cf22014-12-01 15:40:12 -080013442 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013443 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013444 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013445 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013446 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013447 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013448
Gustavo Padovana912f122014-12-01 15:40:10 -080013449 intel_crtc->cursor_addr = addr;
13450 intel_crtc->cursor_bo = obj;
13451update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013452
Matt Roper32b7eee2014-12-24 07:59:06 -080013453 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013454 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013455}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013456
Matt Roper3d7d6512014-06-10 08:28:13 -070013457static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13458 int pipe)
13459{
13460 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013461 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013462
13463 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13464 if (cursor == NULL)
13465 return NULL;
13466
Matt Roper8e7d6882015-01-21 16:35:41 -080013467 state = intel_create_plane_state(&cursor->base);
13468 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013469 kfree(cursor);
13470 return NULL;
13471 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013472 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013473
Matt Roper3d7d6512014-06-10 08:28:13 -070013474 cursor->can_scale = false;
13475 cursor->max_downscale = 1;
13476 cursor->pipe = pipe;
13477 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013478 cursor->check_plane = intel_check_cursor_plane;
13479 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013480 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013481
13482 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013483 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013484 intel_cursor_formats,
13485 ARRAY_SIZE(intel_cursor_formats),
13486 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013487
13488 if (INTEL_INFO(dev)->gen >= 4) {
13489 if (!dev->mode_config.rotation_property)
13490 dev->mode_config.rotation_property =
13491 drm_mode_create_rotation_property(dev,
13492 BIT(DRM_ROTATE_0) |
13493 BIT(DRM_ROTATE_180));
13494 if (dev->mode_config.rotation_property)
13495 drm_object_attach_property(&cursor->base.base,
13496 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013497 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013498 }
13499
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013500 if (INTEL_INFO(dev)->gen >=9)
13501 state->scaler_id = -1;
13502
Matt Roperea2c67b2014-12-23 10:41:52 -080013503 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13504
Matt Roper3d7d6512014-06-10 08:28:13 -070013505 return &cursor->base;
13506}
13507
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013508static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13509 struct intel_crtc_state *crtc_state)
13510{
13511 int i;
13512 struct intel_scaler *intel_scaler;
13513 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13514
13515 for (i = 0; i < intel_crtc->num_scalers; i++) {
13516 intel_scaler = &scaler_state->scalers[i];
13517 intel_scaler->in_use = 0;
13518 intel_scaler->id = i;
13519
13520 intel_scaler->mode = PS_SCALER_MODE_DYN;
13521 }
13522
13523 scaler_state->scaler_id = -1;
13524}
13525
Hannes Ederb358d0a2008-12-18 21:18:47 +010013526static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013527{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013528 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013529 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013530 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013531 struct drm_plane *primary = NULL;
13532 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013533 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013534
Daniel Vetter955382f2013-09-19 14:05:45 +020013535 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013536 if (intel_crtc == NULL)
13537 return;
13538
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013539 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13540 if (!crtc_state)
13541 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013542 intel_crtc->config = crtc_state;
13543 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013544 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013545
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013546 /* initialize shared scalers */
13547 if (INTEL_INFO(dev)->gen >= 9) {
13548 if (pipe == PIPE_C)
13549 intel_crtc->num_scalers = 1;
13550 else
13551 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13552
13553 skl_init_scalers(dev, intel_crtc, crtc_state);
13554 }
13555
Matt Roper465c1202014-05-29 08:06:54 -070013556 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013557 if (!primary)
13558 goto fail;
13559
13560 cursor = intel_cursor_plane_create(dev, pipe);
13561 if (!cursor)
13562 goto fail;
13563
Matt Roper465c1202014-05-29 08:06:54 -070013564 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013565 cursor, &intel_crtc_funcs);
13566 if (ret)
13567 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013568
13569 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013570 for (i = 0; i < 256; i++) {
13571 intel_crtc->lut_r[i] = i;
13572 intel_crtc->lut_g[i] = i;
13573 intel_crtc->lut_b[i] = i;
13574 }
13575
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013576 /*
13577 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013578 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013579 */
Jesse Barnes80824002009-09-10 15:28:06 -070013580 intel_crtc->pipe = pipe;
13581 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013582 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013583 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013584 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013585 }
13586
Chris Wilson4b0e3332014-05-30 16:35:26 +030013587 intel_crtc->cursor_base = ~0;
13588 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013589 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013590
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013591 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13592 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13593 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13594 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13595
Jesse Barnes79e53942008-11-07 14:24:08 -080013596 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013597
13598 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013599 return;
13600
13601fail:
13602 if (primary)
13603 drm_plane_cleanup(primary);
13604 if (cursor)
13605 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013606 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013607 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013608}
13609
Jesse Barnes752aa882013-10-31 18:55:49 +020013610enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13611{
13612 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013613 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013614
Rob Clark51fd3712013-11-19 12:10:12 -050013615 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013616
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013617 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013618 return INVALID_PIPE;
13619
13620 return to_intel_crtc(encoder->crtc)->pipe;
13621}
13622
Carl Worth08d7b3d2009-04-29 14:43:54 -070013623int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013624 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013625{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013626 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013627 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013628 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013629
Rob Clark7707e652014-07-17 23:30:04 -040013630 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013631
Rob Clark7707e652014-07-17 23:30:04 -040013632 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013633 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013634 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013635 }
13636
Rob Clark7707e652014-07-17 23:30:04 -040013637 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013638 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013639
Daniel Vetterc05422d2009-08-11 16:05:30 +020013640 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013641}
13642
Daniel Vetter66a92782012-07-12 20:08:18 +020013643static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013644{
Daniel Vetter66a92782012-07-12 20:08:18 +020013645 struct drm_device *dev = encoder->base.dev;
13646 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013647 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013648 int entry = 0;
13649
Damien Lespiaub2784e12014-08-05 11:29:37 +010013650 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013651 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013652 index_mask |= (1 << entry);
13653
Jesse Barnes79e53942008-11-07 14:24:08 -080013654 entry++;
13655 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013656
Jesse Barnes79e53942008-11-07 14:24:08 -080013657 return index_mask;
13658}
13659
Chris Wilson4d302442010-12-14 19:21:29 +000013660static bool has_edp_a(struct drm_device *dev)
13661{
13662 struct drm_i915_private *dev_priv = dev->dev_private;
13663
13664 if (!IS_MOBILE(dev))
13665 return false;
13666
13667 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13668 return false;
13669
Damien Lespiaue3589902014-02-07 19:12:50 +000013670 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013671 return false;
13672
13673 return true;
13674}
13675
Jesse Barnes84b4e042014-06-25 08:24:29 -070013676static bool intel_crt_present(struct drm_device *dev)
13677{
13678 struct drm_i915_private *dev_priv = dev->dev_private;
13679
Damien Lespiau884497e2013-12-03 13:56:23 +000013680 if (INTEL_INFO(dev)->gen >= 9)
13681 return false;
13682
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013683 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013684 return false;
13685
13686 if (IS_CHERRYVIEW(dev))
13687 return false;
13688
13689 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13690 return false;
13691
13692 return true;
13693}
13694
Jesse Barnes79e53942008-11-07 14:24:08 -080013695static void intel_setup_outputs(struct drm_device *dev)
13696{
Eric Anholt725e30a2009-01-22 13:01:02 -080013697 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013698 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013699 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013700
Daniel Vetterc9093352013-06-06 22:22:47 +020013701 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013702
Jesse Barnes84b4e042014-06-25 08:24:29 -070013703 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013704 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013705
Vandana Kannanc776eb22014-08-19 12:05:01 +053013706 if (IS_BROXTON(dev)) {
13707 /*
13708 * FIXME: Broxton doesn't support port detection via the
13709 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13710 * detect the ports.
13711 */
13712 intel_ddi_init(dev, PORT_A);
13713 intel_ddi_init(dev, PORT_B);
13714 intel_ddi_init(dev, PORT_C);
13715 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013716 int found;
13717
Jesse Barnesde31fac2015-03-06 15:53:32 -080013718 /*
13719 * Haswell uses DDI functions to detect digital outputs.
13720 * On SKL pre-D0 the strap isn't connected, so we assume
13721 * it's there.
13722 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013723 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013724 /* WaIgnoreDDIAStrap: skl */
13725 if (found ||
13726 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013727 intel_ddi_init(dev, PORT_A);
13728
13729 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13730 * register */
13731 found = I915_READ(SFUSE_STRAP);
13732
13733 if (found & SFUSE_STRAP_DDIB_DETECTED)
13734 intel_ddi_init(dev, PORT_B);
13735 if (found & SFUSE_STRAP_DDIC_DETECTED)
13736 intel_ddi_init(dev, PORT_C);
13737 if (found & SFUSE_STRAP_DDID_DETECTED)
13738 intel_ddi_init(dev, PORT_D);
13739 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013740 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013741 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013742
13743 if (has_edp_a(dev))
13744 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013745
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013746 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013747 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013748 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013749 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013750 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013751 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013752 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013753 }
13754
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013755 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013756 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013757
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013758 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013759 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013760
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013761 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013762 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013763
Daniel Vetter270b3042012-10-27 15:52:05 +020013764 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013765 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013766 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013767 /*
13768 * The DP_DETECTED bit is the latched state of the DDC
13769 * SDA pin at boot. However since eDP doesn't require DDC
13770 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13771 * eDP ports may have been muxed to an alternate function.
13772 * Thus we can't rely on the DP_DETECTED bit alone to detect
13773 * eDP ports. Consult the VBT as well as DP_DETECTED to
13774 * detect eDP ports.
13775 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013776 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13777 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013778 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13779 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013780 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13781 intel_dp_is_edp(dev, PORT_B))
13782 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013783
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013784 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13785 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013786 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13787 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013788 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13789 intel_dp_is_edp(dev, PORT_C))
13790 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013791
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013792 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013793 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013794 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13795 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013796 /* eDP not supported on port D, so don't check VBT */
13797 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13798 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013799 }
13800
Jani Nikula3cfca972013-08-27 15:12:26 +030013801 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013802 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013803 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013804
Paulo Zanonie2debe92013-02-18 19:00:27 -030013805 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013806 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013807 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013808 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13809 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013810 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013811 }
Ma Ling27185ae2009-08-24 13:50:23 +080013812
Imre Deake7281ea2013-05-08 13:14:08 +030013813 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013814 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013815 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013816
13817 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013818
Paulo Zanonie2debe92013-02-18 19:00:27 -030013819 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013820 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013821 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013822 }
Ma Ling27185ae2009-08-24 13:50:23 +080013823
Paulo Zanonie2debe92013-02-18 19:00:27 -030013824 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013825
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013826 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13827 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013828 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013829 }
Imre Deake7281ea2013-05-08 13:14:08 +030013830 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013831 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080013832 }
Ma Ling27185ae2009-08-24 13:50:23 +080013833
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013834 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030013835 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013836 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070013837 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013838 intel_dvo_init(dev);
13839
Zhenyu Wang103a1962009-11-27 11:44:36 +080013840 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013841 intel_tv_init(dev);
13842
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080013843 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070013844
Damien Lespiaub2784e12014-08-05 11:29:37 +010013845 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010013846 encoder->base.possible_crtcs = encoder->crtc_mask;
13847 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020013848 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080013849 }
Chris Wilson47356eb2011-01-11 17:06:04 +000013850
Paulo Zanonidde86e22012-12-01 12:04:25 -020013851 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020013852
13853 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013854}
13855
13856static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13857{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013858 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080013859 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080013860
Daniel Vetteref2d6332014-02-10 18:00:38 +010013861 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013862 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010013863 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013864 drm_gem_object_unreference(&intel_fb->obj->base);
13865 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013866 kfree(intel_fb);
13867}
13868
13869static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000013870 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080013871 unsigned int *handle)
13872{
13873 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000013874 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013875
Chris Wilson05394f32010-11-08 19:18:58 +000013876 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080013877}
13878
13879static const struct drm_framebuffer_funcs intel_fb_funcs = {
13880 .destroy = intel_user_framebuffer_destroy,
13881 .create_handle = intel_user_framebuffer_create_handle,
13882};
13883
Damien Lespiaub3218032015-02-27 11:15:18 +000013884static
13885u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13886 uint32_t pixel_format)
13887{
13888 u32 gen = INTEL_INFO(dev)->gen;
13889
13890 if (gen >= 9) {
13891 /* "The stride in bytes must not exceed the of the size of 8K
13892 * pixels and 32K bytes."
13893 */
13894 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13895 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13896 return 32*1024;
13897 } else if (gen >= 4) {
13898 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13899 return 16*1024;
13900 else
13901 return 32*1024;
13902 } else if (gen >= 3) {
13903 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13904 return 8*1024;
13905 else
13906 return 16*1024;
13907 } else {
13908 /* XXX DSPC is limited to 4k tiled */
13909 return 8*1024;
13910 }
13911}
13912
Daniel Vetterb5ea6422014-03-02 21:18:00 +010013913static int intel_framebuffer_init(struct drm_device *dev,
13914 struct intel_framebuffer *intel_fb,
13915 struct drm_mode_fb_cmd2 *mode_cmd,
13916 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080013917{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000013918 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080013919 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000013920 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080013921
Daniel Vetterdd4916c2013-10-09 21:23:51 +020013922 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13923
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013924 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13925 /* Enforce that fb modifier and tiling mode match, but only for
13926 * X-tiled. This is needed for FBC. */
13927 if (!!(obj->tiling_mode == I915_TILING_X) !=
13928 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13929 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13930 return -EINVAL;
13931 }
13932 } else {
13933 if (obj->tiling_mode == I915_TILING_X)
13934 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13935 else if (obj->tiling_mode == I915_TILING_Y) {
13936 DRM_DEBUG("No Y tiling for legacy addfb\n");
13937 return -EINVAL;
13938 }
13939 }
13940
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000013941 /* Passed in modifier sanity checking. */
13942 switch (mode_cmd->modifier[0]) {
13943 case I915_FORMAT_MOD_Y_TILED:
13944 case I915_FORMAT_MOD_Yf_TILED:
13945 if (INTEL_INFO(dev)->gen < 9) {
13946 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13947 mode_cmd->modifier[0]);
13948 return -EINVAL;
13949 }
13950 case DRM_FORMAT_MOD_NONE:
13951 case I915_FORMAT_MOD_X_TILED:
13952 break;
13953 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070013954 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13955 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010013956 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013957 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013958
Damien Lespiaub3218032015-02-27 11:15:18 +000013959 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13960 mode_cmd->pixel_format);
13961 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13962 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13963 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010013964 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013965 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013966
Damien Lespiaub3218032015-02-27 11:15:18 +000013967 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13968 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013969 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000013970 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13971 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013972 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013973 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013974 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013975 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013976
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013977 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013978 mode_cmd->pitches[0] != obj->stride) {
13979 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13980 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013981 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013982 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013983
Ville Syrjälä57779d02012-10-31 17:50:14 +020013984 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013985 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020013986 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020013987 case DRM_FORMAT_RGB565:
13988 case DRM_FORMAT_XRGB8888:
13989 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020013990 break;
13991 case DRM_FORMAT_XRGB1555:
13992 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013993 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000013994 DRM_DEBUG("unsupported pixel format: %s\n",
13995 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020013996 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013997 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020013998 break;
13999 case DRM_FORMAT_XBGR8888:
14000 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014001 case DRM_FORMAT_XRGB2101010:
14002 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014003 case DRM_FORMAT_XBGR2101010:
14004 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014005 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014006 DRM_DEBUG("unsupported pixel format: %s\n",
14007 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014008 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014009 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014010 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014011 case DRM_FORMAT_YUYV:
14012 case DRM_FORMAT_UYVY:
14013 case DRM_FORMAT_YVYU:
14014 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014015 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014016 DRM_DEBUG("unsupported pixel format: %s\n",
14017 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014018 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014019 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014020 break;
14021 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014022 DRM_DEBUG("unsupported pixel format: %s\n",
14023 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014024 return -EINVAL;
14025 }
14026
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014027 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14028 if (mode_cmd->offsets[0] != 0)
14029 return -EINVAL;
14030
Damien Lespiauec2c9812015-01-20 12:51:45 +000014031 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014032 mode_cmd->pixel_format,
14033 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014034 /* FIXME drm helper for size checks (especially planar formats)? */
14035 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14036 return -EINVAL;
14037
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014038 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14039 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014040 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014041
Jesse Barnes79e53942008-11-07 14:24:08 -080014042 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14043 if (ret) {
14044 DRM_ERROR("framebuffer init failed %d\n", ret);
14045 return ret;
14046 }
14047
Jesse Barnes79e53942008-11-07 14:24:08 -080014048 return 0;
14049}
14050
Jesse Barnes79e53942008-11-07 14:24:08 -080014051static struct drm_framebuffer *
14052intel_user_framebuffer_create(struct drm_device *dev,
14053 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014054 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014055{
Chris Wilson05394f32010-11-08 19:18:58 +000014056 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014057
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014058 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14059 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014060 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014061 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014062
Chris Wilsond2dff872011-04-19 08:36:26 +010014063 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014064}
14065
Daniel Vetter4520f532013-10-09 09:18:51 +020014066#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014067static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014068{
14069}
14070#endif
14071
Jesse Barnes79e53942008-11-07 14:24:08 -080014072static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014073 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014074 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014075 .atomic_check = intel_atomic_check,
14076 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014077};
14078
Jesse Barnese70236a2009-09-21 10:42:27 -070014079/* Set up chip specific display functions */
14080static void intel_init_display(struct drm_device *dev)
14081{
14082 struct drm_i915_private *dev_priv = dev->dev_private;
14083
Daniel Vetteree9300b2013-06-03 22:40:22 +020014084 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14085 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014086 else if (IS_CHERRYVIEW(dev))
14087 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014088 else if (IS_VALLEYVIEW(dev))
14089 dev_priv->display.find_dpll = vlv_find_best_dpll;
14090 else if (IS_PINEVIEW(dev))
14091 dev_priv->display.find_dpll = pnv_find_best_dpll;
14092 else
14093 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14094
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014095 if (INTEL_INFO(dev)->gen >= 9) {
14096 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014097 dev_priv->display.get_initial_plane_config =
14098 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014099 dev_priv->display.crtc_compute_clock =
14100 haswell_crtc_compute_clock;
14101 dev_priv->display.crtc_enable = haswell_crtc_enable;
14102 dev_priv->display.crtc_disable = haswell_crtc_disable;
14103 dev_priv->display.off = ironlake_crtc_off;
14104 dev_priv->display.update_primary_plane =
14105 skylake_update_primary_plane;
14106 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014107 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014108 dev_priv->display.get_initial_plane_config =
14109 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014110 dev_priv->display.crtc_compute_clock =
14111 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014112 dev_priv->display.crtc_enable = haswell_crtc_enable;
14113 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014114 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014115 dev_priv->display.update_primary_plane =
14116 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014117 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014118 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014119 dev_priv->display.get_initial_plane_config =
14120 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014121 dev_priv->display.crtc_compute_clock =
14122 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014123 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14124 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014125 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014126 dev_priv->display.update_primary_plane =
14127 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014128 } else if (IS_VALLEYVIEW(dev)) {
14129 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014130 dev_priv->display.get_initial_plane_config =
14131 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014132 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014133 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14134 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14135 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014136 dev_priv->display.update_primary_plane =
14137 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014138 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014139 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014140 dev_priv->display.get_initial_plane_config =
14141 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014142 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014143 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14144 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014145 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014146 dev_priv->display.update_primary_plane =
14147 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014148 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014149
Jesse Barnese70236a2009-09-21 10:42:27 -070014150 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014151 if (IS_SKYLAKE(dev))
14152 dev_priv->display.get_display_clock_speed =
14153 skylake_get_display_clock_speed;
14154 else if (IS_BROADWELL(dev))
14155 dev_priv->display.get_display_clock_speed =
14156 broadwell_get_display_clock_speed;
14157 else if (IS_HASWELL(dev))
14158 dev_priv->display.get_display_clock_speed =
14159 haswell_get_display_clock_speed;
14160 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014161 dev_priv->display.get_display_clock_speed =
14162 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014163 else if (IS_GEN5(dev))
14164 dev_priv->display.get_display_clock_speed =
14165 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014166 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14167 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014168 dev_priv->display.get_display_clock_speed =
14169 i945_get_display_clock_speed;
14170 else if (IS_I915G(dev))
14171 dev_priv->display.get_display_clock_speed =
14172 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014173 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014174 dev_priv->display.get_display_clock_speed =
14175 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014176 else if (IS_PINEVIEW(dev))
14177 dev_priv->display.get_display_clock_speed =
14178 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014179 else if (IS_I915GM(dev))
14180 dev_priv->display.get_display_clock_speed =
14181 i915gm_get_display_clock_speed;
14182 else if (IS_I865G(dev))
14183 dev_priv->display.get_display_clock_speed =
14184 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014185 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014186 dev_priv->display.get_display_clock_speed =
14187 i855_get_display_clock_speed;
14188 else /* 852, 830 */
14189 dev_priv->display.get_display_clock_speed =
14190 i830_get_display_clock_speed;
14191
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014192 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014193 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014194 } else if (IS_GEN6(dev)) {
14195 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014196 } else if (IS_IVYBRIDGE(dev)) {
14197 /* FIXME: detect B0+ stepping and use auto training */
14198 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014199 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014200 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014201 } else if (IS_VALLEYVIEW(dev)) {
14202 dev_priv->display.modeset_global_resources =
14203 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014204 } else if (IS_BROXTON(dev)) {
14205 dev_priv->display.modeset_global_resources =
14206 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014207 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014208
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014209 switch (INTEL_INFO(dev)->gen) {
14210 case 2:
14211 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14212 break;
14213
14214 case 3:
14215 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14216 break;
14217
14218 case 4:
14219 case 5:
14220 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14221 break;
14222
14223 case 6:
14224 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14225 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014226 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014227 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014228 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14229 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014230 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014231 /* Drop through - unsupported since execlist only. */
14232 default:
14233 /* Default just returns -ENODEV to indicate unsupported */
14234 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014235 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014236
14237 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014238
14239 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014240}
14241
Jesse Barnesb690e962010-07-19 13:53:12 -070014242/*
14243 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14244 * resume, or other times. This quirk makes sure that's the case for
14245 * affected systems.
14246 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014247static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014248{
14249 struct drm_i915_private *dev_priv = dev->dev_private;
14250
14251 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014252 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014253}
14254
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014255static void quirk_pipeb_force(struct drm_device *dev)
14256{
14257 struct drm_i915_private *dev_priv = dev->dev_private;
14258
14259 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14260 DRM_INFO("applying pipe b force quirk\n");
14261}
14262
Keith Packard435793d2011-07-12 14:56:22 -070014263/*
14264 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14265 */
14266static void quirk_ssc_force_disable(struct drm_device *dev)
14267{
14268 struct drm_i915_private *dev_priv = dev->dev_private;
14269 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014270 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014271}
14272
Carsten Emde4dca20e2012-03-15 15:56:26 +010014273/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014274 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14275 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014276 */
14277static void quirk_invert_brightness(struct drm_device *dev)
14278{
14279 struct drm_i915_private *dev_priv = dev->dev_private;
14280 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014281 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014282}
14283
Scot Doyle9c72cc62014-07-03 23:27:50 +000014284/* Some VBT's incorrectly indicate no backlight is present */
14285static void quirk_backlight_present(struct drm_device *dev)
14286{
14287 struct drm_i915_private *dev_priv = dev->dev_private;
14288 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14289 DRM_INFO("applying backlight present quirk\n");
14290}
14291
Jesse Barnesb690e962010-07-19 13:53:12 -070014292struct intel_quirk {
14293 int device;
14294 int subsystem_vendor;
14295 int subsystem_device;
14296 void (*hook)(struct drm_device *dev);
14297};
14298
Egbert Eich5f85f172012-10-14 15:46:38 +020014299/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14300struct intel_dmi_quirk {
14301 void (*hook)(struct drm_device *dev);
14302 const struct dmi_system_id (*dmi_id_list)[];
14303};
14304
14305static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14306{
14307 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14308 return 1;
14309}
14310
14311static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14312 {
14313 .dmi_id_list = &(const struct dmi_system_id[]) {
14314 {
14315 .callback = intel_dmi_reverse_brightness,
14316 .ident = "NCR Corporation",
14317 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14318 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14319 },
14320 },
14321 { } /* terminating entry */
14322 },
14323 .hook = quirk_invert_brightness,
14324 },
14325};
14326
Ben Widawskyc43b5632012-04-16 14:07:40 -070014327static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014328 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14329 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14330
Jesse Barnesb690e962010-07-19 13:53:12 -070014331 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14332 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14333
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014334 /* 830 needs to leave pipe A & dpll A up */
14335 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14336
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014337 /* 830 needs to leave pipe B & dpll B up */
14338 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14339
Keith Packard435793d2011-07-12 14:56:22 -070014340 /* Lenovo U160 cannot use SSC on LVDS */
14341 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014342
14343 /* Sony Vaio Y cannot use SSC on LVDS */
14344 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014345
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014346 /* Acer Aspire 5734Z must invert backlight brightness */
14347 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14348
14349 /* Acer/eMachines G725 */
14350 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14351
14352 /* Acer/eMachines e725 */
14353 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14354
14355 /* Acer/Packard Bell NCL20 */
14356 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14357
14358 /* Acer Aspire 4736Z */
14359 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014360
14361 /* Acer Aspire 5336 */
14362 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014363
14364 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14365 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014366
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014367 /* Acer C720 Chromebook (Core i3 4005U) */
14368 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14369
jens steinb2a96012014-10-28 20:25:53 +010014370 /* Apple Macbook 2,1 (Core 2 T7400) */
14371 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14372
Scot Doyled4967d82014-07-03 23:27:52 +000014373 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14374 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014375
14376 /* HP Chromebook 14 (Celeron 2955U) */
14377 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014378
14379 /* Dell Chromebook 11 */
14380 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014381};
14382
14383static void intel_init_quirks(struct drm_device *dev)
14384{
14385 struct pci_dev *d = dev->pdev;
14386 int i;
14387
14388 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14389 struct intel_quirk *q = &intel_quirks[i];
14390
14391 if (d->device == q->device &&
14392 (d->subsystem_vendor == q->subsystem_vendor ||
14393 q->subsystem_vendor == PCI_ANY_ID) &&
14394 (d->subsystem_device == q->subsystem_device ||
14395 q->subsystem_device == PCI_ANY_ID))
14396 q->hook(dev);
14397 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014398 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14399 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14400 intel_dmi_quirks[i].hook(dev);
14401 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014402}
14403
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014404/* Disable the VGA plane that we never use */
14405static void i915_disable_vga(struct drm_device *dev)
14406{
14407 struct drm_i915_private *dev_priv = dev->dev_private;
14408 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014409 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014410
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014411 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014412 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014413 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014414 sr1 = inb(VGA_SR_DATA);
14415 outb(sr1 | 1<<5, VGA_SR_DATA);
14416 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14417 udelay(300);
14418
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014419 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014420 POSTING_READ(vga_reg);
14421}
14422
Daniel Vetterf8175862012-04-10 15:50:11 +020014423void intel_modeset_init_hw(struct drm_device *dev)
14424{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014425 intel_prepare_ddi(dev);
14426
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014427 if (IS_VALLEYVIEW(dev))
14428 vlv_update_cdclk(dev);
14429
Daniel Vetterf8175862012-04-10 15:50:11 +020014430 intel_init_clock_gating(dev);
14431
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014432 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014433}
14434
Jesse Barnes79e53942008-11-07 14:24:08 -080014435void intel_modeset_init(struct drm_device *dev)
14436{
Jesse Barnes652c3932009-08-17 13:31:43 -070014437 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014438 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014439 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014440 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014441
14442 drm_mode_config_init(dev);
14443
14444 dev->mode_config.min_width = 0;
14445 dev->mode_config.min_height = 0;
14446
Dave Airlie019d96c2011-09-29 16:20:42 +010014447 dev->mode_config.preferred_depth = 24;
14448 dev->mode_config.prefer_shadow = 1;
14449
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014450 dev->mode_config.allow_fb_modifiers = true;
14451
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014452 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014453
Jesse Barnesb690e962010-07-19 13:53:12 -070014454 intel_init_quirks(dev);
14455
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014456 intel_init_pm(dev);
14457
Ben Widawskye3c74752013-04-05 13:12:39 -070014458 if (INTEL_INFO(dev)->num_pipes == 0)
14459 return;
14460
Jesse Barnese70236a2009-09-21 10:42:27 -070014461 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014462 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014463
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014464 if (IS_GEN2(dev)) {
14465 dev->mode_config.max_width = 2048;
14466 dev->mode_config.max_height = 2048;
14467 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014468 dev->mode_config.max_width = 4096;
14469 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014470 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014471 dev->mode_config.max_width = 8192;
14472 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014473 }
Damien Lespiau068be562014-03-28 14:17:49 +000014474
Ville Syrjälädc41c152014-08-13 11:57:05 +030014475 if (IS_845G(dev) || IS_I865G(dev)) {
14476 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14477 dev->mode_config.cursor_height = 1023;
14478 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014479 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14480 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14481 } else {
14482 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14483 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14484 }
14485
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014486 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014487
Zhao Yakui28c97732009-10-09 11:39:41 +080014488 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014489 INTEL_INFO(dev)->num_pipes,
14490 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014491
Damien Lespiau055e3932014-08-18 13:49:10 +010014492 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014493 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014494 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014495 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014496 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014497 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014498 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014499 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014500 }
14501
Jesse Barnesf42bb702013-12-16 16:34:23 -080014502 intel_init_dpio(dev);
14503
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014504 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014505
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014506 /* Just disable it once at startup */
14507 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014508 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014509
14510 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014511 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014512
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014513 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014514 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014515 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014516
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014517 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014518 if (!crtc->active)
14519 continue;
14520
Jesse Barnes46f297f2014-03-07 08:57:48 -080014521 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014522 * Note that reserving the BIOS fb up front prevents us
14523 * from stuffing other stolen allocations like the ring
14524 * on top. This prevents some ugliness at boot time, and
14525 * can even allow for smooth boot transitions if the BIOS
14526 * fb is large enough for the active pipe configuration.
14527 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014528 if (dev_priv->display.get_initial_plane_config) {
14529 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014530 &crtc->plane_config);
14531 /*
14532 * If the fb is shared between multiple heads, we'll
14533 * just get the first one.
14534 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014535 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014536 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014537 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014538}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014539
Daniel Vetter7fad7982012-07-04 17:51:47 +020014540static void intel_enable_pipe_a(struct drm_device *dev)
14541{
14542 struct intel_connector *connector;
14543 struct drm_connector *crt = NULL;
14544 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014545 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014546
14547 /* We can't just switch on the pipe A, we need to set things up with a
14548 * proper mode and output configuration. As a gross hack, enable pipe A
14549 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014550 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014551 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14552 crt = &connector->base;
14553 break;
14554 }
14555 }
14556
14557 if (!crt)
14558 return;
14559
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014560 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014561 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014562}
14563
Daniel Vetterfa555832012-10-10 23:14:00 +020014564static bool
14565intel_check_plane_mapping(struct intel_crtc *crtc)
14566{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014567 struct drm_device *dev = crtc->base.dev;
14568 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014569 u32 reg, val;
14570
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014571 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014572 return true;
14573
14574 reg = DSPCNTR(!crtc->plane);
14575 val = I915_READ(reg);
14576
14577 if ((val & DISPLAY_PLANE_ENABLE) &&
14578 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14579 return false;
14580
14581 return true;
14582}
14583
Daniel Vetter24929352012-07-02 20:28:59 +020014584static void intel_sanitize_crtc(struct intel_crtc *crtc)
14585{
14586 struct drm_device *dev = crtc->base.dev;
14587 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014588 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014589
Daniel Vetter24929352012-07-02 20:28:59 +020014590 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014591 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014592 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14593
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014594 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014595 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014596 if (crtc->active) {
14597 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014598 drm_crtc_vblank_on(&crtc->base);
14599 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014600
Daniel Vetter24929352012-07-02 20:28:59 +020014601 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014602 * disable the crtc (and hence change the state) if it is wrong. Note
14603 * that gen4+ has a fixed plane -> pipe mapping. */
14604 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014605 struct intel_connector *connector;
14606 bool plane;
14607
Daniel Vetter24929352012-07-02 20:28:59 +020014608 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14609 crtc->base.base.id);
14610
14611 /* Pipe has the wrong plane attached and the plane is active.
14612 * Temporarily change the plane mapping and disable everything
14613 * ... */
14614 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014615 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014616 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030014617 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014618 dev_priv->display.crtc_disable(&crtc->base);
14619 crtc->plane = plane;
14620
14621 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014622 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014623 if (connector->encoder->base.crtc != &crtc->base)
14624 continue;
14625
Egbert Eich7f1950f2014-04-25 10:56:22 +020014626 connector->base.dpms = DRM_MODE_DPMS_OFF;
14627 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014628 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014629 /* multiple connectors may have the same encoder:
14630 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014631 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014632 if (connector->encoder->base.crtc == &crtc->base) {
14633 connector->encoder->base.crtc = NULL;
14634 connector->encoder->connectors_active = false;
14635 }
Daniel Vetter24929352012-07-02 20:28:59 +020014636
14637 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014638 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014639 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014640 crtc->base.enabled = false;
14641 }
Daniel Vetter24929352012-07-02 20:28:59 +020014642
Daniel Vetter7fad7982012-07-04 17:51:47 +020014643 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14644 crtc->pipe == PIPE_A && !crtc->active) {
14645 /* BIOS forgot to enable pipe A, this mostly happens after
14646 * resume. Force-enable the pipe to fix this, the update_dpms
14647 * call below we restore the pipe to the right state, but leave
14648 * the required bits on. */
14649 intel_enable_pipe_a(dev);
14650 }
14651
Daniel Vetter24929352012-07-02 20:28:59 +020014652 /* Adjust the state of the output pipe according to whether we
14653 * have active connectors/encoders. */
14654 intel_crtc_update_dpms(&crtc->base);
14655
Matt Roper83d65732015-02-25 13:12:16 -080014656 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014657 struct intel_encoder *encoder;
14658
14659 /* This can happen either due to bugs in the get_hw_state
14660 * functions or because the pipe is force-enabled due to the
14661 * pipe A quirk. */
14662 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14663 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014664 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014665 crtc->active ? "enabled" : "disabled");
14666
Matt Roper83d65732015-02-25 13:12:16 -080014667 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014668 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014669 crtc->base.enabled = crtc->active;
14670
14671 /* Because we only establish the connector -> encoder ->
14672 * crtc links if something is active, this means the
14673 * crtc is now deactivated. Break the links. connector
14674 * -> encoder links are only establish when things are
14675 * actually up, hence no need to break them. */
14676 WARN_ON(crtc->active);
14677
14678 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14679 WARN_ON(encoder->connectors_active);
14680 encoder->base.crtc = NULL;
14681 }
14682 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014683
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014684 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014685 /*
14686 * We start out with underrun reporting disabled to avoid races.
14687 * For correct bookkeeping mark this on active crtcs.
14688 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014689 * Also on gmch platforms we dont have any hardware bits to
14690 * disable the underrun reporting. Which means we need to start
14691 * out with underrun reporting disabled also on inactive pipes,
14692 * since otherwise we'll complain about the garbage we read when
14693 * e.g. coming up after runtime pm.
14694 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014695 * No protection against concurrent access is required - at
14696 * worst a fifo underrun happens which also sets this to false.
14697 */
14698 crtc->cpu_fifo_underrun_disabled = true;
14699 crtc->pch_fifo_underrun_disabled = true;
14700 }
Daniel Vetter24929352012-07-02 20:28:59 +020014701}
14702
14703static void intel_sanitize_encoder(struct intel_encoder *encoder)
14704{
14705 struct intel_connector *connector;
14706 struct drm_device *dev = encoder->base.dev;
14707
14708 /* We need to check both for a crtc link (meaning that the
14709 * encoder is active and trying to read from a pipe) and the
14710 * pipe itself being active. */
14711 bool has_active_crtc = encoder->base.crtc &&
14712 to_intel_crtc(encoder->base.crtc)->active;
14713
14714 if (encoder->connectors_active && !has_active_crtc) {
14715 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14716 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014717 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014718
14719 /* Connector is active, but has no active pipe. This is
14720 * fallout from our resume register restoring. Disable
14721 * the encoder manually again. */
14722 if (encoder->base.crtc) {
14723 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14724 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014725 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014726 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014727 if (encoder->post_disable)
14728 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014729 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014730 encoder->base.crtc = NULL;
14731 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014732
14733 /* Inconsistent output/port/pipe state happens presumably due to
14734 * a bug in one of the get_hw_state functions. Or someplace else
14735 * in our code, like the register restore mess on resume. Clamp
14736 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014737 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014738 if (connector->encoder != encoder)
14739 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014740 connector->base.dpms = DRM_MODE_DPMS_OFF;
14741 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014742 }
14743 }
14744 /* Enabled encoders without active connectors will be fixed in
14745 * the crtc fixup. */
14746}
14747
Imre Deak04098752014-02-18 00:02:16 +020014748void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014749{
14750 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014751 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014752
Imre Deak04098752014-02-18 00:02:16 +020014753 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14754 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14755 i915_disable_vga(dev);
14756 }
14757}
14758
14759void i915_redisable_vga(struct drm_device *dev)
14760{
14761 struct drm_i915_private *dev_priv = dev->dev_private;
14762
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014763 /* This function can be called both from intel_modeset_setup_hw_state or
14764 * at a very early point in our resume sequence, where the power well
14765 * structures are not yet restored. Since this function is at a very
14766 * paranoid "someone might have enabled VGA while we were not looking"
14767 * level, just check if the power well is enabled instead of trying to
14768 * follow the "don't touch the power well if we don't need it" policy
14769 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014770 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014771 return;
14772
Imre Deak04098752014-02-18 00:02:16 +020014773 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014774}
14775
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014776static bool primary_get_hw_state(struct intel_crtc *crtc)
14777{
14778 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14779
14780 if (!crtc->active)
14781 return false;
14782
14783 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14784}
14785
Daniel Vetter30e984d2013-06-05 13:34:17 +020014786static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014787{
14788 struct drm_i915_private *dev_priv = dev->dev_private;
14789 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014790 struct intel_crtc *crtc;
14791 struct intel_encoder *encoder;
14792 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014793 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014794
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014795 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014796 struct drm_plane *primary = crtc->base.primary;
14797 struct intel_plane_state *plane_state;
14798
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014799 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014800
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014801 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014802
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014803 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014804 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014805
Matt Roper83d65732015-02-25 13:12:16 -080014806 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014807 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014808 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014809
14810 plane_state = to_intel_plane_state(primary->state);
14811 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014812
14813 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14814 crtc->base.base.id,
14815 crtc->active ? "enabled" : "disabled");
14816 }
14817
Daniel Vetter53589012013-06-05 13:34:16 +020014818 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14819 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14820
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014821 pll->on = pll->get_hw_state(dev_priv, pll,
14822 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014823 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014824 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014825 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014826 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020014827 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014828 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014829 }
Daniel Vetter53589012013-06-05 13:34:16 +020014830 }
Daniel Vetter53589012013-06-05 13:34:16 +020014831
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014832 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014833 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014834
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014835 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014836 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020014837 }
14838
Damien Lespiaub2784e12014-08-05 11:29:37 +010014839 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014840 pipe = 0;
14841
14842 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070014843 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14844 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014845 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014846 } else {
14847 encoder->base.crtc = NULL;
14848 }
14849
14850 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014851 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020014852 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014853 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014854 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014855 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020014856 }
14857
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014858 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014859 if (connector->get_hw_state(connector)) {
14860 connector->base.dpms = DRM_MODE_DPMS_ON;
14861 connector->encoder->connectors_active = true;
14862 connector->base.encoder = &connector->encoder->base;
14863 } else {
14864 connector->base.dpms = DRM_MODE_DPMS_OFF;
14865 connector->base.encoder = NULL;
14866 }
14867 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14868 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030014869 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014870 connector->base.encoder ? "enabled" : "disabled");
14871 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020014872}
14873
14874/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14875 * and i915 state tracking structures. */
14876void intel_modeset_setup_hw_state(struct drm_device *dev,
14877 bool force_restore)
14878{
14879 struct drm_i915_private *dev_priv = dev->dev_private;
14880 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014881 struct intel_crtc *crtc;
14882 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020014883 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014884
14885 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020014886
Jesse Barnesbabea612013-06-26 18:57:38 +030014887 /*
14888 * Now that we have the config, copy it to each CRTC struct
14889 * Note that this could go away if we move to using crtc_config
14890 * checking everywhere.
14891 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014892 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020014893 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014894 intel_mode_from_pipe_config(&crtc->base.mode,
14895 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030014896 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14897 crtc->base.base.id);
14898 drm_mode_debug_printmodeline(&crtc->base.mode);
14899 }
14900 }
14901
Daniel Vetter24929352012-07-02 20:28:59 +020014902 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010014903 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014904 intel_sanitize_encoder(encoder);
14905 }
14906
Damien Lespiau055e3932014-08-18 13:49:10 +010014907 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020014908 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14909 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014910 intel_dump_pipe_config(crtc, crtc->config,
14911 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020014912 }
Daniel Vetter9a935852012-07-05 22:34:27 +020014913
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020014914 intel_modeset_update_connector_atomic_state(dev);
14915
Daniel Vetter35c95372013-07-17 06:55:04 +020014916 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14917 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14918
14919 if (!pll->on || pll->active)
14920 continue;
14921
14922 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14923
14924 pll->disable(dev_priv, pll);
14925 pll->on = false;
14926 }
14927
Pradeep Bhat30789992014-11-04 17:06:45 +000014928 if (IS_GEN9(dev))
14929 skl_wm_get_hw_state(dev);
14930 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030014931 ilk_wm_get_hw_state(dev);
14932
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014933 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030014934 i915_redisable_vga(dev);
14935
Daniel Vetterf30da182013-04-11 20:22:50 +020014936 /*
14937 * We need to use raw interfaces for restoring state to avoid
14938 * checking (bogus) intermediate states.
14939 */
Damien Lespiau055e3932014-08-18 13:49:10 +010014940 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070014941 struct drm_crtc *crtc =
14942 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020014943
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014944 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014945 }
14946 } else {
14947 intel_modeset_update_staged_output_state(dev);
14948 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020014949
14950 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010014951}
14952
14953void intel_modeset_gem_init(struct drm_device *dev)
14954{
Jesse Barnes92122782014-10-09 12:57:42 -070014955 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014956 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070014957 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010014958 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014959
Imre Deakae484342014-03-31 15:10:44 +030014960 mutex_lock(&dev->struct_mutex);
14961 intel_init_gt_powersave(dev);
14962 mutex_unlock(&dev->struct_mutex);
14963
Jesse Barnes92122782014-10-09 12:57:42 -070014964 /*
14965 * There may be no VBT; and if the BIOS enabled SSC we can
14966 * just keep using it to avoid unnecessary flicker. Whereas if the
14967 * BIOS isn't using it, don't assume it will work even if the VBT
14968 * indicates as much.
14969 */
14970 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14971 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14972 DREF_SSC1_ENABLE);
14973
Chris Wilson1833b132012-05-09 11:56:28 +010014974 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020014975
14976 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014977
14978 /*
14979 * Make sure any fbs we allocated at startup are properly
14980 * pinned & fenced. When we do the allocation it's too early
14981 * for this.
14982 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010014983 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070014984 obj = intel_fb_obj(c->primary->fb);
14985 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080014986 continue;
14987
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010014988 mutex_lock(&dev->struct_mutex);
14989 ret = intel_pin_and_fence_fb_obj(c->primary,
14990 c->primary->fb,
14991 c->primary->state,
14992 NULL);
14993 mutex_unlock(&dev->struct_mutex);
14994 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080014995 DRM_ERROR("failed to pin boot fb on pipe %d\n",
14996 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100014997 drm_framebuffer_unreference(c->primary->fb);
14998 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080014999 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015000 }
15001 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015002
15003 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015004}
15005
Imre Deak4932e2c2014-02-11 17:12:48 +020015006void intel_connector_unregister(struct intel_connector *intel_connector)
15007{
15008 struct drm_connector *connector = &intel_connector->base;
15009
15010 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015011 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015012}
15013
Jesse Barnes79e53942008-11-07 14:24:08 -080015014void intel_modeset_cleanup(struct drm_device *dev)
15015{
Jesse Barnes652c3932009-08-17 13:31:43 -070015016 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015017 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015018
Imre Deak2eb52522014-11-19 15:30:05 +020015019 intel_disable_gt_powersave(dev);
15020
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015021 intel_backlight_unregister(dev);
15022
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015023 /*
15024 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015025 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015026 * experience fancy races otherwise.
15027 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015028 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015029
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015030 /*
15031 * Due to the hpd irq storm handling the hotplug work can re-arm the
15032 * poll handlers. Hence disable polling after hpd handling is shut down.
15033 */
Keith Packardf87ea762010-10-03 19:36:26 -070015034 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015035
Jesse Barnes652c3932009-08-17 13:31:43 -070015036 mutex_lock(&dev->struct_mutex);
15037
Jesse Barnes723bfd72010-10-07 16:01:13 -070015038 intel_unregister_dsm_handler();
15039
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015040 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015041
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015042 mutex_unlock(&dev->struct_mutex);
15043
Chris Wilson1630fe72011-07-08 12:22:42 +010015044 /* flush any delayed tasks or pending work */
15045 flush_scheduled_work();
15046
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015047 /* destroy the backlight and sysfs files before encoders/connectors */
15048 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015049 struct intel_connector *intel_connector;
15050
15051 intel_connector = to_intel_connector(connector);
15052 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015053 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015054
Jesse Barnes79e53942008-11-07 14:24:08 -080015055 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015056
15057 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015058
15059 mutex_lock(&dev->struct_mutex);
15060 intel_cleanup_gt_powersave(dev);
15061 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015062}
15063
Dave Airlie28d52042009-09-21 14:33:58 +100015064/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015065 * Return which encoder is currently attached for connector.
15066 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015067struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015068{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015069 return &intel_attached_encoder(connector)->base;
15070}
Jesse Barnes79e53942008-11-07 14:24:08 -080015071
Chris Wilsondf0e9242010-09-09 16:20:55 +010015072void intel_connector_attach_encoder(struct intel_connector *connector,
15073 struct intel_encoder *encoder)
15074{
15075 connector->encoder = encoder;
15076 drm_mode_connector_attach_encoder(&connector->base,
15077 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015078}
Dave Airlie28d52042009-09-21 14:33:58 +100015079
15080/*
15081 * set vga decode state - true == enable VGA decode
15082 */
15083int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15084{
15085 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015086 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015087 u16 gmch_ctrl;
15088
Chris Wilson75fa0412014-02-07 18:37:02 -020015089 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15090 DRM_ERROR("failed to read control word\n");
15091 return -EIO;
15092 }
15093
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015094 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15095 return 0;
15096
Dave Airlie28d52042009-09-21 14:33:58 +100015097 if (state)
15098 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15099 else
15100 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015101
15102 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15103 DRM_ERROR("failed to write control word\n");
15104 return -EIO;
15105 }
15106
Dave Airlie28d52042009-09-21 14:33:58 +100015107 return 0;
15108}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015109
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015110struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015111
15112 u32 power_well_driver;
15113
Chris Wilson63b66e52013-08-08 15:12:06 +020015114 int num_transcoders;
15115
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015116 struct intel_cursor_error_state {
15117 u32 control;
15118 u32 position;
15119 u32 base;
15120 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015121 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015122
15123 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015124 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015125 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015126 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015127 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015128
15129 struct intel_plane_error_state {
15130 u32 control;
15131 u32 stride;
15132 u32 size;
15133 u32 pos;
15134 u32 addr;
15135 u32 surface;
15136 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015137 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015138
15139 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015140 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015141 enum transcoder cpu_transcoder;
15142
15143 u32 conf;
15144
15145 u32 htotal;
15146 u32 hblank;
15147 u32 hsync;
15148 u32 vtotal;
15149 u32 vblank;
15150 u32 vsync;
15151 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015152};
15153
15154struct intel_display_error_state *
15155intel_display_capture_error_state(struct drm_device *dev)
15156{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015157 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015158 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015159 int transcoders[] = {
15160 TRANSCODER_A,
15161 TRANSCODER_B,
15162 TRANSCODER_C,
15163 TRANSCODER_EDP,
15164 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015165 int i;
15166
Chris Wilson63b66e52013-08-08 15:12:06 +020015167 if (INTEL_INFO(dev)->num_pipes == 0)
15168 return NULL;
15169
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015170 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015171 if (error == NULL)
15172 return NULL;
15173
Imre Deak190be112013-11-25 17:15:31 +020015174 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015175 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15176
Damien Lespiau055e3932014-08-18 13:49:10 +010015177 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015178 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015179 __intel_display_power_is_enabled(dev_priv,
15180 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015181 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015182 continue;
15183
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015184 error->cursor[i].control = I915_READ(CURCNTR(i));
15185 error->cursor[i].position = I915_READ(CURPOS(i));
15186 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015187
15188 error->plane[i].control = I915_READ(DSPCNTR(i));
15189 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015190 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015191 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015192 error->plane[i].pos = I915_READ(DSPPOS(i));
15193 }
Paulo Zanonica291362013-03-06 20:03:14 -030015194 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15195 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015196 if (INTEL_INFO(dev)->gen >= 4) {
15197 error->plane[i].surface = I915_READ(DSPSURF(i));
15198 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15199 }
15200
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015201 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015202
Sonika Jindal3abfce72014-07-21 15:23:43 +053015203 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015204 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015205 }
15206
15207 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15208 if (HAS_DDI(dev_priv->dev))
15209 error->num_transcoders++; /* Account for eDP. */
15210
15211 for (i = 0; i < error->num_transcoders; i++) {
15212 enum transcoder cpu_transcoder = transcoders[i];
15213
Imre Deakddf9c532013-11-27 22:02:02 +020015214 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015215 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015216 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015217 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015218 continue;
15219
Chris Wilson63b66e52013-08-08 15:12:06 +020015220 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15221
15222 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15223 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15224 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15225 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15226 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15227 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15228 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015229 }
15230
15231 return error;
15232}
15233
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015234#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15235
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015236void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015237intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015238 struct drm_device *dev,
15239 struct intel_display_error_state *error)
15240{
Damien Lespiau055e3932014-08-18 13:49:10 +010015241 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015242 int i;
15243
Chris Wilson63b66e52013-08-08 15:12:06 +020015244 if (!error)
15245 return;
15246
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015247 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015248 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015249 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015250 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015251 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015252 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015253 err_printf(m, " Power: %s\n",
15254 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015255 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015256 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015257
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015258 err_printf(m, "Plane [%d]:\n", i);
15259 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15260 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015261 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015262 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15263 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015264 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015265 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015266 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015267 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015268 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15269 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015270 }
15271
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015272 err_printf(m, "Cursor [%d]:\n", i);
15273 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15274 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15275 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015276 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015277
15278 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015279 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015280 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015281 err_printf(m, " Power: %s\n",
15282 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015283 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15284 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15285 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15286 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15287 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15288 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15289 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15290 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015291}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015292
15293void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15294{
15295 struct intel_crtc *crtc;
15296
15297 for_each_intel_crtc(dev, crtc) {
15298 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015299
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015300 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015301
15302 work = crtc->unpin_work;
15303
15304 if (work && work->event &&
15305 work->event->base.file_priv == file) {
15306 kfree(work->event);
15307 work->event = NULL;
15308 }
15309
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015310 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015311 }
15312}