blob: b7e61588a35db1a300a71c112d75e9031581f3a0 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010049static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010050 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070052 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070054};
55
56/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010062 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010071 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070072 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_XBGR2101010,
Kumar, Maheshea916ea2015-09-03 16:17:09 +053075 DRM_FORMAT_YUYV,
76 DRM_FORMAT_YVYU,
77 DRM_FORMAT_UYVY,
78 DRM_FORMAT_VYUY,
Matt Roper465c1202014-05-29 08:06:54 -070079};
80
Matt Roper3d7d6512014-06-10 08:28:13 -070081/* Cursor formats */
82static const uint32_t intel_cursor_formats[] = {
83 DRM_FORMAT_ARGB8888,
84};
85
Chris Wilson6b383a72010-09-13 13:54:26 +010086static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080087
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020089 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030090static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020091 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030092
Jesse Barneseb1bfe82014-02-12 12:26:25 -080093static int intel_framebuffer_init(struct drm_device *dev,
94 struct intel_framebuffer *ifb,
95 struct drm_mode_fb_cmd2 *mode_cmd,
96 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020097static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
98static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020099static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -0700100 struct intel_link_m_n *m_n,
101 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200102static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200103static void haswell_set_pipeconf(struct drm_crtc *crtc);
104static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200105static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200106 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200107static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200108 const struct intel_crtc_state *pipe_config);
Maarten Lankhorst613d2b22015-07-21 13:28:58 +0200109static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
110static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700111static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
112 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200113static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
114 int num_connectors);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200115static void intel_modeset_setup_hw_state(struct drm_device *dev);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100116
Jesse Barnes79e53942008-11-07 14:24:08 -0800117typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800119} intel_range_t;
120
121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int dot_limit;
123 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800124} intel_p2_t;
125
Ma Lingd4906092009-03-18 20:13:27 +0800126typedef struct intel_limit intel_limit_t;
127struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400128 intel_range_t dot, vco, n, m, m1, m2, p, p1;
129 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800130};
Jesse Barnes79e53942008-11-07 14:24:08 -0800131
Daniel Vetterd2acd212012-10-20 20:57:43 +0200132int
133intel_pch_rawclk(struct drm_device *dev)
134{
135 struct drm_i915_private *dev_priv = dev->dev_private;
136
137 WARN_ON(!HAS_PCH_SPLIT(dev));
138
139 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
140}
141
Jani Nikula79e50a42015-08-26 10:58:20 +0300142/* hrawclock is 1/4 the FSB frequency */
143int intel_hrawclk(struct drm_device *dev)
144{
145 struct drm_i915_private *dev_priv = dev->dev_private;
146 uint32_t clkcfg;
147
148 /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
149 if (IS_VALLEYVIEW(dev))
150 return 200;
151
152 clkcfg = I915_READ(CLKCFG);
153 switch (clkcfg & CLKCFG_FSB_MASK) {
154 case CLKCFG_FSB_400:
155 return 100;
156 case CLKCFG_FSB_533:
157 return 133;
158 case CLKCFG_FSB_667:
159 return 166;
160 case CLKCFG_FSB_800:
161 return 200;
162 case CLKCFG_FSB_1067:
163 return 266;
164 case CLKCFG_FSB_1333:
165 return 333;
166 /* these two are just a guess; one of them might be right */
167 case CLKCFG_FSB_1600:
168 case CLKCFG_FSB_1600_ALT:
169 return 400;
170 default:
171 return 133;
172 }
173}
174
Chris Wilson021357a2010-09-07 20:54:59 +0100175static inline u32 /* units of 100MHz */
176intel_fdi_link_freq(struct drm_device *dev)
177{
Chris Wilson8b99e682010-10-13 09:59:17 +0100178 if (IS_GEN5(dev)) {
179 struct drm_i915_private *dev_priv = dev->dev_private;
180 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
181 } else
182 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100183}
184
Daniel Vetter5d536e22013-07-06 12:52:06 +0200185static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200187 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200188 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400189 .m = { .min = 96, .max = 140 },
190 .m1 = { .min = 18, .max = 26 },
191 .m2 = { .min = 6, .max = 16 },
192 .p = { .min = 4, .max = 128 },
193 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700194 .p2 = { .dot_limit = 165000,
195 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700196};
197
Daniel Vetter5d536e22013-07-06 12:52:06 +0200198static const intel_limit_t intel_limits_i8xx_dvo = {
199 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200200 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200201 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200202 .m = { .min = 96, .max = 140 },
203 .m1 = { .min = 18, .max = 26 },
204 .m2 = { .min = 6, .max = 16 },
205 .p = { .min = 4, .max = 128 },
206 .p1 = { .min = 2, .max = 33 },
207 .p2 = { .dot_limit = 165000,
208 .p2_slow = 4, .p2_fast = 4 },
209};
210
Keith Packarde4b36692009-06-05 19:22:17 -0700211static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400212 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200213 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200214 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400215 .m = { .min = 96, .max = 140 },
216 .m1 = { .min = 18, .max = 26 },
217 .m2 = { .min = 6, .max = 16 },
218 .p = { .min = 4, .max = 128 },
219 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700220 .p2 = { .dot_limit = 165000,
221 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700222};
Eric Anholt273e27c2011-03-30 13:01:10 -0700223
Keith Packarde4b36692009-06-05 19:22:17 -0700224static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400225 .dot = { .min = 20000, .max = 400000 },
226 .vco = { .min = 1400000, .max = 2800000 },
227 .n = { .min = 1, .max = 6 },
228 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100229 .m1 = { .min = 8, .max = 18 },
230 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400231 .p = { .min = 5, .max = 80 },
232 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700233 .p2 = { .dot_limit = 200000,
234 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700235};
236
237static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400238 .dot = { .min = 20000, .max = 400000 },
239 .vco = { .min = 1400000, .max = 2800000 },
240 .n = { .min = 1, .max = 6 },
241 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100242 .m1 = { .min = 8, .max = 18 },
243 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400244 .p = { .min = 7, .max = 98 },
245 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700246 .p2 = { .dot_limit = 112000,
247 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700248};
249
Eric Anholt273e27c2011-03-30 13:01:10 -0700250
Keith Packarde4b36692009-06-05 19:22:17 -0700251static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700252 .dot = { .min = 25000, .max = 270000 },
253 .vco = { .min = 1750000, .max = 3500000},
254 .n = { .min = 1, .max = 4 },
255 .m = { .min = 104, .max = 138 },
256 .m1 = { .min = 17, .max = 23 },
257 .m2 = { .min = 5, .max = 11 },
258 .p = { .min = 10, .max = 30 },
259 .p1 = { .min = 1, .max = 3},
260 .p2 = { .dot_limit = 270000,
261 .p2_slow = 10,
262 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800263 },
Keith Packarde4b36692009-06-05 19:22:17 -0700264};
265
266static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700267 .dot = { .min = 22000, .max = 400000 },
268 .vco = { .min = 1750000, .max = 3500000},
269 .n = { .min = 1, .max = 4 },
270 .m = { .min = 104, .max = 138 },
271 .m1 = { .min = 16, .max = 23 },
272 .m2 = { .min = 5, .max = 11 },
273 .p = { .min = 5, .max = 80 },
274 .p1 = { .min = 1, .max = 8},
275 .p2 = { .dot_limit = 165000,
276 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700277};
278
279static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700280 .dot = { .min = 20000, .max = 115000 },
281 .vco = { .min = 1750000, .max = 3500000 },
282 .n = { .min = 1, .max = 3 },
283 .m = { .min = 104, .max = 138 },
284 .m1 = { .min = 17, .max = 23 },
285 .m2 = { .min = 5, .max = 11 },
286 .p = { .min = 28, .max = 112 },
287 .p1 = { .min = 2, .max = 8 },
288 .p2 = { .dot_limit = 0,
289 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800290 },
Keith Packarde4b36692009-06-05 19:22:17 -0700291};
292
293static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700294 .dot = { .min = 80000, .max = 224000 },
295 .vco = { .min = 1750000, .max = 3500000 },
296 .n = { .min = 1, .max = 3 },
297 .m = { .min = 104, .max = 138 },
298 .m1 = { .min = 17, .max = 23 },
299 .m2 = { .min = 5, .max = 11 },
300 .p = { .min = 14, .max = 42 },
301 .p1 = { .min = 2, .max = 6 },
302 .p2 = { .dot_limit = 0,
303 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800304 },
Keith Packarde4b36692009-06-05 19:22:17 -0700305};
306
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500307static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400308 .dot = { .min = 20000, .max = 400000},
309 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400311 .n = { .min = 3, .max = 6 },
312 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700313 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400314 .m1 = { .min = 0, .max = 0 },
315 .m2 = { .min = 0, .max = 254 },
316 .p = { .min = 5, .max = 80 },
317 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700318 .p2 = { .dot_limit = 200000,
319 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700320};
321
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500322static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400323 .dot = { .min = 20000, .max = 400000 },
324 .vco = { .min = 1700000, .max = 3500000 },
325 .n = { .min = 3, .max = 6 },
326 .m = { .min = 2, .max = 256 },
327 .m1 = { .min = 0, .max = 0 },
328 .m2 = { .min = 0, .max = 254 },
329 .p = { .min = 7, .max = 112 },
330 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700331 .p2 = { .dot_limit = 112000,
332 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700333};
334
Eric Anholt273e27c2011-03-30 13:01:10 -0700335/* Ironlake / Sandybridge
336 *
337 * We calculate clock using (register_value + 2) for N/M1/M2, so here
338 * the range value for them is (actual_value - 2).
339 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800340static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700341 .dot = { .min = 25000, .max = 350000 },
342 .vco = { .min = 1760000, .max = 3510000 },
343 .n = { .min = 1, .max = 5 },
344 .m = { .min = 79, .max = 127 },
345 .m1 = { .min = 12, .max = 22 },
346 .m2 = { .min = 5, .max = 9 },
347 .p = { .min = 5, .max = 80 },
348 .p1 = { .min = 1, .max = 8 },
349 .p2 = { .dot_limit = 225000,
350 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700351};
352
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800353static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700354 .dot = { .min = 25000, .max = 350000 },
355 .vco = { .min = 1760000, .max = 3510000 },
356 .n = { .min = 1, .max = 3 },
357 .m = { .min = 79, .max = 118 },
358 .m1 = { .min = 12, .max = 22 },
359 .m2 = { .min = 5, .max = 9 },
360 .p = { .min = 28, .max = 112 },
361 .p1 = { .min = 2, .max = 8 },
362 .p2 = { .dot_limit = 225000,
363 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800364};
365
366static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700367 .dot = { .min = 25000, .max = 350000 },
368 .vco = { .min = 1760000, .max = 3510000 },
369 .n = { .min = 1, .max = 3 },
370 .m = { .min = 79, .max = 127 },
371 .m1 = { .min = 12, .max = 22 },
372 .m2 = { .min = 5, .max = 9 },
373 .p = { .min = 14, .max = 56 },
374 .p1 = { .min = 2, .max = 8 },
375 .p2 = { .dot_limit = 225000,
376 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800377};
378
Eric Anholt273e27c2011-03-30 13:01:10 -0700379/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800380static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700381 .dot = { .min = 25000, .max = 350000 },
382 .vco = { .min = 1760000, .max = 3510000 },
383 .n = { .min = 1, .max = 2 },
384 .m = { .min = 79, .max = 126 },
385 .m1 = { .min = 12, .max = 22 },
386 .m2 = { .min = 5, .max = 9 },
387 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400388 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700389 .p2 = { .dot_limit = 225000,
390 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800391};
392
393static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700394 .dot = { .min = 25000, .max = 350000 },
395 .vco = { .min = 1760000, .max = 3510000 },
396 .n = { .min = 1, .max = 3 },
397 .m = { .min = 79, .max = 126 },
398 .m1 = { .min = 12, .max = 22 },
399 .m2 = { .min = 5, .max = 9 },
400 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400401 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700402 .p2 = { .dot_limit = 225000,
403 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800404};
405
Ville Syrjälädc730512013-09-24 21:26:30 +0300406static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300407 /*
408 * These are the data rate limits (measured in fast clocks)
409 * since those are the strictest limits we have. The fast
410 * clock and actual rate limits are more relaxed, so checking
411 * them would make no difference.
412 */
413 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200414 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700415 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700416 .m1 = { .min = 2, .max = 3 },
417 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300418 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300419 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700420};
421
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300422static const intel_limit_t intel_limits_chv = {
423 /*
424 * These are the data rate limits (measured in fast clocks)
425 * since those are the strictest limits we have. The fast
426 * clock and actual rate limits are more relaxed, so checking
427 * them would make no difference.
428 */
429 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200430 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300431 .n = { .min = 1, .max = 1 },
432 .m1 = { .min = 2, .max = 2 },
433 .m2 = { .min = 24 << 22, .max = 175 << 22 },
434 .p1 = { .min = 2, .max = 4 },
435 .p2 = { .p2_slow = 1, .p2_fast = 14 },
436};
437
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200438static const intel_limit_t intel_limits_bxt = {
439 /* FIXME: find real dot limits */
440 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530441 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200442 .n = { .min = 1, .max = 1 },
443 .m1 = { .min = 2, .max = 2 },
444 /* FIXME: find real m2 limits */
445 .m2 = { .min = 2 << 22, .max = 255 << 22 },
446 .p1 = { .min = 2, .max = 4 },
447 .p2 = { .p2_slow = 1, .p2_fast = 20 },
448};
449
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200450static bool
451needs_modeset(struct drm_crtc_state *state)
452{
Maarten Lankhorstfc596662015-07-21 13:28:57 +0200453 return drm_atomic_crtc_needs_modeset(state);
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200454}
455
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300456/**
457 * Returns whether any output on the specified pipe is of the specified type
458 */
Damien Lespiau40935612014-10-29 11:16:59 +0000459bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300460{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300461 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300462 struct intel_encoder *encoder;
463
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300464 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300465 if (encoder->type == type)
466 return true;
467
468 return false;
469}
470
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200471/**
472 * Returns whether any output on the specified pipe will have the specified
473 * type after a staged modeset is complete, i.e., the same as
474 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
475 * encoder->crtc.
476 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200477static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
478 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200479{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200480 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300481 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200482 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200483 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200484 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200485
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300486 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200487 if (connector_state->crtc != crtc_state->base.crtc)
488 continue;
489
490 num_connectors++;
491
492 encoder = to_intel_encoder(connector_state->best_encoder);
493 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200494 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200495 }
496
497 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200498
499 return false;
500}
501
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200502static const intel_limit_t *
503intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800504{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200505 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800506 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800507
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200508 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100509 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000510 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800511 limit = &intel_limits_ironlake_dual_lvds_100m;
512 else
513 limit = &intel_limits_ironlake_dual_lvds;
514 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000515 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800516 limit = &intel_limits_ironlake_single_lvds_100m;
517 else
518 limit = &intel_limits_ironlake_single_lvds;
519 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200520 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800521 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800522
523 return limit;
524}
525
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200526static const intel_limit_t *
527intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800528{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200529 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800530 const intel_limit_t *limit;
531
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200532 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100533 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700534 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800535 else
Keith Packarde4b36692009-06-05 19:22:17 -0700536 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200537 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
538 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700539 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200540 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700541 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800542 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700543 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800544
545 return limit;
546}
547
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548static const intel_limit_t *
549intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800550{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200551 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 const intel_limit_t *limit;
553
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200554 if (IS_BROXTON(dev))
555 limit = &intel_limits_bxt;
556 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200557 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800558 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200559 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500560 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500562 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800563 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500564 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300565 } else if (IS_CHERRYVIEW(dev)) {
566 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700567 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300568 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100569 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200570 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100571 limit = &intel_limits_i9xx_lvds;
572 else
573 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800574 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200575 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700576 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200577 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700578 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200579 else
580 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800581 }
582 return limit;
583}
584
Imre Deakdccbea32015-06-22 23:35:51 +0300585/*
586 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
587 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
588 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
589 * The helpers' return value is the rate of the clock that is fed to the
590 * display engine's pipe which can be the above fast dot clock rate or a
591 * divided-down version of it.
592 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500593/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300594static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800595{
Shaohua Li21778322009-02-23 15:19:16 +0800596 clock->m = clock->m2 + 2;
597 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200598 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300599 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300600 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
601 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300602
603 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800604}
605
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200606static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
607{
608 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
609}
610
Imre Deakdccbea32015-06-22 23:35:51 +0300611static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800612{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200613 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200615 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300616 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300617 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
618 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300619
620 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800621}
622
Imre Deakdccbea32015-06-22 23:35:51 +0300623static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300624{
625 clock->m = clock->m1 * clock->m2;
626 clock->p = clock->p1 * clock->p2;
627 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300628 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300629 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
630 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300631
632 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300633}
634
Imre Deakdccbea32015-06-22 23:35:51 +0300635int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300636{
637 clock->m = clock->m1 * clock->m2;
638 clock->p = clock->p1 * clock->p2;
639 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300640 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300641 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
642 clock->n << 22);
643 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300644
645 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300646}
647
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800648#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800649/**
650 * Returns whether the given set of divisors are valid for a given refclk with
651 * the given connectors.
652 */
653
Chris Wilson1b894b52010-12-14 20:04:54 +0000654static bool intel_PLL_is_valid(struct drm_device *dev,
655 const intel_limit_t *limit,
656 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800657{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300658 if (clock->n < limit->n.min || limit->n.max < clock->n)
659 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800660 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400661 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800662 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400663 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800664 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400665 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300666
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200667 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300668 if (clock->m1 <= clock->m2)
669 INTELPllInvalid("m1 <= m2\n");
670
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200671 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300672 if (clock->p < limit->p.min || limit->p.max < clock->p)
673 INTELPllInvalid("p out of range\n");
674 if (clock->m < limit->m.min || limit->m.max < clock->m)
675 INTELPllInvalid("m out of range\n");
676 }
677
Jesse Barnes79e53942008-11-07 14:24:08 -0800678 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400679 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800680 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
681 * connector, etc., rather than just a single range.
682 */
683 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400684 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800685
686 return true;
687}
688
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300689static int
690i9xx_select_p2_div(const intel_limit_t *limit,
691 const struct intel_crtc_state *crtc_state,
692 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800693{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300694 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800695
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200696 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800697 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100698 * For LVDS just rely on its current settings for dual-channel.
699 * We haven't figured out how to reliably set up different
700 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100702 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300703 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800704 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300705 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800706 } else {
707 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300708 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800709 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300710 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800711 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300712}
713
714static bool
715i9xx_find_best_dpll(const intel_limit_t *limit,
716 struct intel_crtc_state *crtc_state,
717 int target, int refclk, intel_clock_t *match_clock,
718 intel_clock_t *best_clock)
719{
720 struct drm_device *dev = crtc_state->base.crtc->dev;
721 intel_clock_t clock;
722 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800723
Akshay Joshi0206e352011-08-16 15:34:10 -0400724 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800725
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300726 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
727
Zhao Yakui42158662009-11-20 11:24:18 +0800728 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
729 clock.m1++) {
730 for (clock.m2 = limit->m2.min;
731 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200732 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800733 break;
734 for (clock.n = limit->n.min;
735 clock.n <= limit->n.max; clock.n++) {
736 for (clock.p1 = limit->p1.min;
737 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800738 int this_err;
739
Imre Deakdccbea32015-06-22 23:35:51 +0300740 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000741 if (!intel_PLL_is_valid(dev, limit,
742 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800743 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800744 if (match_clock &&
745 clock.p != match_clock->p)
746 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800747
748 this_err = abs(clock.dot - target);
749 if (this_err < err) {
750 *best_clock = clock;
751 err = this_err;
752 }
753 }
754 }
755 }
756 }
757
758 return (err != target);
759}
760
Ma Lingd4906092009-03-18 20:13:27 +0800761static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200762pnv_find_best_dpll(const intel_limit_t *limit,
763 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200764 int target, int refclk, intel_clock_t *match_clock,
765 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200766{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300767 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200768 intel_clock_t clock;
769 int err = target;
770
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200771 memset(best_clock, 0, sizeof(*best_clock));
772
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300773 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
774
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200775 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
776 clock.m1++) {
777 for (clock.m2 = limit->m2.min;
778 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200779 for (clock.n = limit->n.min;
780 clock.n <= limit->n.max; clock.n++) {
781 for (clock.p1 = limit->p1.min;
782 clock.p1 <= limit->p1.max; clock.p1++) {
783 int this_err;
784
Imre Deakdccbea32015-06-22 23:35:51 +0300785 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800786 if (!intel_PLL_is_valid(dev, limit,
787 &clock))
788 continue;
789 if (match_clock &&
790 clock.p != match_clock->p)
791 continue;
792
793 this_err = abs(clock.dot - target);
794 if (this_err < err) {
795 *best_clock = clock;
796 err = this_err;
797 }
798 }
799 }
800 }
801 }
802
803 return (err != target);
804}
805
Ma Lingd4906092009-03-18 20:13:27 +0800806static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200807g4x_find_best_dpll(const intel_limit_t *limit,
808 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200809 int target, int refclk, intel_clock_t *match_clock,
810 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800811{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300812 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800813 intel_clock_t clock;
814 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300815 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400816 /* approximately equals target * 0.00585 */
817 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800818
819 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300820
821 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
822
Ma Lingd4906092009-03-18 20:13:27 +0800823 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200824 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800825 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200826 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800827 for (clock.m1 = limit->m1.max;
828 clock.m1 >= limit->m1.min; clock.m1--) {
829 for (clock.m2 = limit->m2.max;
830 clock.m2 >= limit->m2.min; clock.m2--) {
831 for (clock.p1 = limit->p1.max;
832 clock.p1 >= limit->p1.min; clock.p1--) {
833 int this_err;
834
Imre Deakdccbea32015-06-22 23:35:51 +0300835 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000836 if (!intel_PLL_is_valid(dev, limit,
837 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800838 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000839
840 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800841 if (this_err < err_most) {
842 *best_clock = clock;
843 err_most = this_err;
844 max_n = clock.n;
845 found = true;
846 }
847 }
848 }
849 }
850 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800851 return found;
852}
Ma Lingd4906092009-03-18 20:13:27 +0800853
Imre Deakd5dd62b2015-03-17 11:40:03 +0200854/*
855 * Check if the calculated PLL configuration is more optimal compared to the
856 * best configuration and error found so far. Return the calculated error.
857 */
858static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
859 const intel_clock_t *calculated_clock,
860 const intel_clock_t *best_clock,
861 unsigned int best_error_ppm,
862 unsigned int *error_ppm)
863{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200864 /*
865 * For CHV ignore the error and consider only the P value.
866 * Prefer a bigger P value based on HW requirements.
867 */
868 if (IS_CHERRYVIEW(dev)) {
869 *error_ppm = 0;
870
871 return calculated_clock->p > best_clock->p;
872 }
873
Imre Deak24be4e42015-03-17 11:40:04 +0200874 if (WARN_ON_ONCE(!target_freq))
875 return false;
876
Imre Deakd5dd62b2015-03-17 11:40:03 +0200877 *error_ppm = div_u64(1000000ULL *
878 abs(target_freq - calculated_clock->dot),
879 target_freq);
880 /*
881 * Prefer a better P value over a better (smaller) error if the error
882 * is small. Ensure this preference for future configurations too by
883 * setting the error to 0.
884 */
885 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
886 *error_ppm = 0;
887
888 return true;
889 }
890
891 return *error_ppm + 10 < best_error_ppm;
892}
893
Zhenyu Wang2c072452009-06-05 15:38:42 +0800894static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200895vlv_find_best_dpll(const intel_limit_t *limit,
896 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200897 int target, int refclk, intel_clock_t *match_clock,
898 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700899{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200900 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300901 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300902 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300903 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300904 /* min update 19.2 MHz */
905 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300906 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700907
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300908 target *= 5; /* fast clock */
909
910 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700911
912 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300913 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300914 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300915 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300916 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300917 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700918 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300919 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200920 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300921
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300922 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
923 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300924
Imre Deakdccbea32015-06-22 23:35:51 +0300925 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300926
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300927 if (!intel_PLL_is_valid(dev, limit,
928 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300929 continue;
930
Imre Deakd5dd62b2015-03-17 11:40:03 +0200931 if (!vlv_PLL_is_optimal(dev, target,
932 &clock,
933 best_clock,
934 bestppm, &ppm))
935 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300936
Imre Deakd5dd62b2015-03-17 11:40:03 +0200937 *best_clock = clock;
938 bestppm = ppm;
939 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700940 }
941 }
942 }
943 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700944
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300945 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700946}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700947
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300948static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200949chv_find_best_dpll(const intel_limit_t *limit,
950 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300951 int target, int refclk, intel_clock_t *match_clock,
952 intel_clock_t *best_clock)
953{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200954 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300955 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200956 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300957 intel_clock_t clock;
958 uint64_t m2;
959 int found = false;
960
961 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200962 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300963
964 /*
965 * Based on hardware doc, the n always set to 1, and m1 always
966 * set to 2. If requires to support 200Mhz refclk, we need to
967 * revisit this because n may not 1 anymore.
968 */
969 clock.n = 1, clock.m1 = 2;
970 target *= 5; /* fast clock */
971
972 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
973 for (clock.p2 = limit->p2.p2_fast;
974 clock.p2 >= limit->p2.p2_slow;
975 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200976 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300977
978 clock.p = clock.p1 * clock.p2;
979
980 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
981 clock.n) << 22, refclk * clock.m1);
982
983 if (m2 > INT_MAX/clock.m1)
984 continue;
985
986 clock.m2 = m2;
987
Imre Deakdccbea32015-06-22 23:35:51 +0300988 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300989
990 if (!intel_PLL_is_valid(dev, limit, &clock))
991 continue;
992
Imre Deak9ca3ba02015-03-17 11:40:05 +0200993 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
994 best_error_ppm, &error_ppm))
995 continue;
996
997 *best_clock = clock;
998 best_error_ppm = error_ppm;
999 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +03001000 }
1001 }
1002
1003 return found;
1004}
1005
Imre Deak5ab7b0b2015-03-06 03:29:25 +02001006bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
1007 intel_clock_t *best_clock)
1008{
1009 int refclk = i9xx_get_refclk(crtc_state, 0);
1010
1011 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
1012 target_clock, refclk, NULL, best_clock);
1013}
1014
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001015bool intel_crtc_active(struct drm_crtc *crtc)
1016{
1017 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1018
1019 /* Be paranoid as we can arrive here with only partial
1020 * state retrieved from the hardware during setup.
1021 *
Damien Lespiau241bfc32013-09-25 16:45:37 +01001022 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001023 * as Haswell has gained clock readout/fastboot support.
1024 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001025 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001026 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001027 *
1028 * FIXME: The intel_crtc->active here should be switched to
1029 * crtc->state->active once we have proper CRTC states wired up
1030 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001031 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001032 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001033 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001034}
1035
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001036enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1037 enum pipe pipe)
1038{
1039 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1041
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001042 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001043}
1044
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001045static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1046{
1047 struct drm_i915_private *dev_priv = dev->dev_private;
1048 u32 reg = PIPEDSL(pipe);
1049 u32 line1, line2;
1050 u32 line_mask;
1051
1052 if (IS_GEN2(dev))
1053 line_mask = DSL_LINEMASK_GEN2;
1054 else
1055 line_mask = DSL_LINEMASK_GEN3;
1056
1057 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001058 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001059 line2 = I915_READ(reg) & line_mask;
1060
1061 return line1 == line2;
1062}
1063
Keith Packardab7ad7f2010-10-03 00:33:06 -07001064/*
1065 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001066 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001067 *
1068 * After disabling a pipe, we can't wait for vblank in the usual way,
1069 * spinning on the vblank interrupt status bit, since we won't actually
1070 * see an interrupt when the pipe is disabled.
1071 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001072 * On Gen4 and above:
1073 * wait for the pipe register state bit to turn off
1074 *
1075 * Otherwise:
1076 * wait for the display line value to settle (it usually
1077 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001078 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001079 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001080static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001081{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001082 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001083 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001084 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001085 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001086
Keith Packardab7ad7f2010-10-03 00:33:06 -07001087 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001088 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001089
Keith Packardab7ad7f2010-10-03 00:33:06 -07001090 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001091 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1092 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001093 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001094 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001095 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001096 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001097 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001098 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001099}
1100
Jesse Barnesb24e7172011-01-04 15:09:30 -08001101static const char *state_string(bool enabled)
1102{
1103 return enabled ? "on" : "off";
1104}
1105
1106/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001107void assert_pll(struct drm_i915_private *dev_priv,
1108 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001109{
1110 int reg;
1111 u32 val;
1112 bool cur_state;
1113
1114 reg = DPLL(pipe);
1115 val = I915_READ(reg);
1116 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001117 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001118 "PLL state assertion failure (expected %s, current %s)\n",
1119 state_string(state), state_string(cur_state));
1120}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001121
Jani Nikula23538ef2013-08-27 15:12:22 +03001122/* XXX: the dsi pll is shared between MIPI DSI ports */
1123static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1124{
1125 u32 val;
1126 bool cur_state;
1127
Ville Syrjäläa5805162015-05-26 20:42:30 +03001128 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001129 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001130 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001131
1132 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001133 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001134 "DSI PLL state assertion failure (expected %s, current %s)\n",
1135 state_string(state), state_string(cur_state));
1136}
1137#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1138#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1139
Daniel Vetter55607e82013-06-16 21:42:39 +02001140struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001141intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001142{
Daniel Vettere2b78262013-06-07 23:10:03 +02001143 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1144
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001145 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001146 return NULL;
1147
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001148 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001149}
1150
Jesse Barnesb24e7172011-01-04 15:09:30 -08001151/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001152void assert_shared_dpll(struct drm_i915_private *dev_priv,
1153 struct intel_shared_dpll *pll,
1154 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001155{
Jesse Barnes040484a2011-01-03 12:14:26 -08001156 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001157 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001158
Chris Wilson92b27b02012-05-20 18:10:50 +01001159 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001160 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001161 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001162
Daniel Vetter53589012013-06-05 13:34:16 +02001163 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001164 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001165 "%s assertion failure (expected %s, current %s)\n",
1166 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001167}
Jesse Barnes040484a2011-01-03 12:14:26 -08001168
1169static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1170 enum pipe pipe, bool state)
1171{
1172 int reg;
1173 u32 val;
1174 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001175 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1176 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001177
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001178 if (HAS_DDI(dev_priv->dev)) {
1179 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001180 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001181 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001182 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001183 } else {
1184 reg = FDI_TX_CTL(pipe);
1185 val = I915_READ(reg);
1186 cur_state = !!(val & FDI_TX_ENABLE);
1187 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001188 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001189 "FDI TX state assertion failure (expected %s, current %s)\n",
1190 state_string(state), state_string(cur_state));
1191}
1192#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1193#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1194
1195static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1196 enum pipe pipe, bool state)
1197{
1198 int reg;
1199 u32 val;
1200 bool cur_state;
1201
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001202 reg = FDI_RX_CTL(pipe);
1203 val = I915_READ(reg);
1204 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001205 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 "FDI RX state assertion failure (expected %s, current %s)\n",
1207 state_string(state), state_string(cur_state));
1208}
1209#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1210#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1211
1212static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1213 enum pipe pipe)
1214{
1215 int reg;
1216 u32 val;
1217
1218 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001219 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001220 return;
1221
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001222 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001223 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001224 return;
1225
Jesse Barnes040484a2011-01-03 12:14:26 -08001226 reg = FDI_TX_CTL(pipe);
1227 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001228 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 -08001229}
1230
Daniel Vetter55607e82013-06-16 21:42:39 +02001231void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001233{
1234 int reg;
1235 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001236 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001237
1238 reg = FDI_RX_CTL(pipe);
1239 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001240 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001241 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001242 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1243 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001244}
1245
Daniel Vetterb680c372014-09-19 18:27:27 +02001246void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1247 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001248{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001249 struct drm_device *dev = dev_priv->dev;
1250 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001251 u32 val;
1252 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001253 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001254
Jani Nikulabedd4db2014-08-22 15:04:13 +03001255 if (WARN_ON(HAS_DDI(dev)))
1256 return;
1257
1258 if (HAS_PCH_SPLIT(dev)) {
1259 u32 port_sel;
1260
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001262 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1263
1264 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1265 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1266 panel_pipe = PIPE_B;
1267 /* XXX: else fix for eDP */
1268 } else if (IS_VALLEYVIEW(dev)) {
1269 /* presumably write lock depends on pipe, not port select */
1270 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1271 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272 } else {
1273 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001274 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1275 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001276 }
1277
1278 val = I915_READ(pp_reg);
1279 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001280 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001281 locked = false;
1282
Rob Clarke2c719b2014-12-15 13:56:32 -05001283 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001284 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001285 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001286}
1287
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001288static void assert_cursor(struct drm_i915_private *dev_priv,
1289 enum pipe pipe, bool state)
1290{
1291 struct drm_device *dev = dev_priv->dev;
1292 bool cur_state;
1293
Paulo Zanonid9d82082014-02-27 16:30:56 -03001294 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001295 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001296 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001297 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001298
Rob Clarke2c719b2014-12-15 13:56:32 -05001299 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001300 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1301 pipe_name(pipe), state_string(state), state_string(cur_state));
1302}
1303#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1304#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1305
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001306void assert_pipe(struct drm_i915_private *dev_priv,
1307 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001308{
1309 int reg;
1310 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001311 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001312 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1313 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001314
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001315 /* if we need the pipe quirk it must be always on */
1316 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1317 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001318 state = true;
1319
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001320 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001321 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001322 cur_state = false;
1323 } else {
1324 reg = PIPECONF(cpu_transcoder);
1325 val = I915_READ(reg);
1326 cur_state = !!(val & PIPECONF_ENABLE);
1327 }
1328
Rob Clarke2c719b2014-12-15 13:56:32 -05001329 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001330 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001331 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001332}
1333
Chris Wilson931872f2012-01-16 23:01:13 +00001334static void assert_plane(struct drm_i915_private *dev_priv,
1335 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001336{
1337 int reg;
1338 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001339 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001340
1341 reg = DSPCNTR(plane);
1342 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001343 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001344 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001345 "plane %c assertion failure (expected %s, current %s)\n",
1346 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347}
1348
Chris Wilson931872f2012-01-16 23:01:13 +00001349#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1350#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1351
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1353 enum pipe pipe)
1354{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001355 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356 int reg, i;
1357 u32 val;
1358 int cur_pipe;
1359
Ville Syrjälä653e1022013-06-04 13:49:05 +03001360 /* Primary planes are fixed to pipes on gen4+ */
1361 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001362 reg = DSPCNTR(pipe);
1363 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001364 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001365 "plane %c assertion failure, should be disabled but not\n",
1366 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001367 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001368 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001369
Jesse Barnesb24e7172011-01-04 15:09:30 -08001370 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001371 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001372 reg = DSPCNTR(i);
1373 val = I915_READ(reg);
1374 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1375 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001376 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001377 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1378 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001379 }
1380}
1381
Jesse Barnes19332d72013-03-28 09:55:38 -07001382static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1383 enum pipe pipe)
1384{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001385 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001386 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001387 u32 val;
1388
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001389 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001390 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001391 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001392 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001393 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1394 sprite, pipe_name(pipe));
1395 }
1396 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001397 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001398 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001399 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001400 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001401 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001402 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001403 }
1404 } else if (INTEL_INFO(dev)->gen >= 7) {
1405 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001406 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001408 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001409 plane_name(pipe), pipe_name(pipe));
1410 } else if (INTEL_INFO(dev)->gen >= 5) {
1411 reg = DVSCNTR(pipe);
1412 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001413 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1415 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001416 }
1417}
1418
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001419static void assert_vblank_disabled(struct drm_crtc *crtc)
1420{
Rob Clarke2c719b2014-12-15 13:56:32 -05001421 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001422 drm_crtc_vblank_put(crtc);
1423}
1424
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001425static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001426{
1427 u32 val;
1428 bool enabled;
1429
Rob Clarke2c719b2014-12-15 13:56:32 -05001430 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001431
Jesse Barnes92f25842011-01-04 15:09:34 -08001432 val = I915_READ(PCH_DREF_CONTROL);
1433 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1434 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001435 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001436}
1437
Daniel Vetterab9412b2013-05-03 11:49:46 +02001438static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1439 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001440{
1441 int reg;
1442 u32 val;
1443 bool enabled;
1444
Daniel Vetterab9412b2013-05-03 11:49:46 +02001445 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001446 val = I915_READ(reg);
1447 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001448 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001449 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1450 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001451}
1452
Keith Packard4e634382011-08-06 10:39:45 -07001453static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1454 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001455{
1456 if ((val & DP_PORT_EN) == 0)
1457 return false;
1458
1459 if (HAS_PCH_CPT(dev_priv->dev)) {
1460 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1461 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1462 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1463 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001464 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1465 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1466 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001467 } else {
1468 if ((val & DP_PIPE_MASK) != (pipe << 30))
1469 return false;
1470 }
1471 return true;
1472}
1473
Keith Packard1519b992011-08-06 10:35:34 -07001474static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1475 enum pipe pipe, u32 val)
1476{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001477 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001478 return false;
1479
1480 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001481 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001482 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001483 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1484 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1485 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001486 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001487 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001488 return false;
1489 }
1490 return true;
1491}
1492
1493static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1494 enum pipe pipe, u32 val)
1495{
1496 if ((val & LVDS_PORT_EN) == 0)
1497 return false;
1498
1499 if (HAS_PCH_CPT(dev_priv->dev)) {
1500 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1501 return false;
1502 } else {
1503 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1504 return false;
1505 }
1506 return true;
1507}
1508
1509static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe, u32 val)
1511{
1512 if ((val & ADPA_DAC_ENABLE) == 0)
1513 return false;
1514 if (HAS_PCH_CPT(dev_priv->dev)) {
1515 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1516 return false;
1517 } else {
1518 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1519 return false;
1520 }
1521 return true;
1522}
1523
Jesse Barnes291906f2011-02-02 12:28:03 -08001524static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001525 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001526{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001527 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001528 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001529 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001530 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001531
Rob Clarke2c719b2014-12-15 13:56:32 -05001532 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001533 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001534 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001535}
1536
1537static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1538 enum pipe pipe, int reg)
1539{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001540 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001541 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001542 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001543 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001544
Rob Clarke2c719b2014-12-15 13:56:32 -05001545 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001546 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001547 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001548}
1549
1550static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1551 enum pipe pipe)
1552{
1553 int reg;
1554 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001555
Keith Packardf0575e92011-07-25 22:12:43 -07001556 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1557 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1558 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001559
1560 reg = PCH_ADPA;
1561 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001562 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001563 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001564 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001565
1566 reg = PCH_LVDS;
1567 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001568 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001569 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001570 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001571
Paulo Zanonie2debe92013-02-18 19:00:27 -03001572 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1573 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1574 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001575}
1576
Ville Syrjäläd288f652014-10-28 13:20:22 +02001577static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001578 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001579{
Daniel Vetter426115c2013-07-11 22:13:42 +02001580 struct drm_device *dev = crtc->base.dev;
1581 struct drm_i915_private *dev_priv = dev->dev_private;
1582 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001583 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001584
Daniel Vetter426115c2013-07-11 22:13:42 +02001585 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001586
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001587 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001588 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1589
1590 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001591 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001592 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001593
Daniel Vetter426115c2013-07-11 22:13:42 +02001594 I915_WRITE(reg, dpll);
1595 POSTING_READ(reg);
1596 udelay(150);
1597
1598 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1599 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1600
Ville Syrjäläd288f652014-10-28 13:20:22 +02001601 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001602 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001603
1604 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001605 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001606 POSTING_READ(reg);
1607 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001608 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001609 POSTING_READ(reg);
1610 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001611 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001612 POSTING_READ(reg);
1613 udelay(150); /* wait for warmup */
1614}
1615
Ville Syrjäläd288f652014-10-28 13:20:22 +02001616static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001617 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001618{
1619 struct drm_device *dev = crtc->base.dev;
1620 struct drm_i915_private *dev_priv = dev->dev_private;
1621 int pipe = crtc->pipe;
1622 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001623 u32 tmp;
1624
1625 assert_pipe_disabled(dev_priv, crtc->pipe);
1626
1627 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1628
Ville Syrjäläa5805162015-05-26 20:42:30 +03001629 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001630
1631 /* Enable back the 10bit clock to display controller */
1632 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1633 tmp |= DPIO_DCLKP_EN;
1634 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1635
Ville Syrjälä54433e92015-05-26 20:42:31 +03001636 mutex_unlock(&dev_priv->sb_lock);
1637
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001638 /*
1639 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1640 */
1641 udelay(1);
1642
1643 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001644 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001645
1646 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001647 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001648 DRM_ERROR("PLL %d failed to lock\n", pipe);
1649
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001650 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001651 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001652 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001653}
1654
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001655static int intel_num_dvo_pipes(struct drm_device *dev)
1656{
1657 struct intel_crtc *crtc;
1658 int count = 0;
1659
1660 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001661 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001662 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001663
1664 return count;
1665}
1666
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001667static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001668{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001669 struct drm_device *dev = crtc->base.dev;
1670 struct drm_i915_private *dev_priv = dev->dev_private;
1671 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001672 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001673
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001674 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001675
1676 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001677 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001678
1679 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001680 if (IS_MOBILE(dev) && !IS_I830(dev))
1681 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001682
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001683 /* Enable DVO 2x clock on both PLLs if necessary */
1684 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1685 /*
1686 * It appears to be important that we don't enable this
1687 * for the current pipe before otherwise configuring the
1688 * PLL. No idea how this should be handled if multiple
1689 * DVO outputs are enabled simultaneosly.
1690 */
1691 dpll |= DPLL_DVO_2X_MODE;
1692 I915_WRITE(DPLL(!crtc->pipe),
1693 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1694 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001695
1696 /* Wait for the clocks to stabilize. */
1697 POSTING_READ(reg);
1698 udelay(150);
1699
1700 if (INTEL_INFO(dev)->gen >= 4) {
1701 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001702 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001703 } else {
1704 /* The pixel multiplier can only be updated once the
1705 * DPLL is enabled and the clocks are stable.
1706 *
1707 * So write it again.
1708 */
1709 I915_WRITE(reg, dpll);
1710 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001711
1712 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001713 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001714 POSTING_READ(reg);
1715 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001716 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001717 POSTING_READ(reg);
1718 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001719 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001720 POSTING_READ(reg);
1721 udelay(150); /* wait for warmup */
1722}
1723
1724/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001725 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001726 * @dev_priv: i915 private structure
1727 * @pipe: pipe PLL to disable
1728 *
1729 * Disable the PLL for @pipe, making sure the pipe is off first.
1730 *
1731 * Note! This is for pre-ILK only.
1732 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001733static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001734{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001735 struct drm_device *dev = crtc->base.dev;
1736 struct drm_i915_private *dev_priv = dev->dev_private;
1737 enum pipe pipe = crtc->pipe;
1738
1739 /* Disable DVO 2x clock on both PLLs if necessary */
1740 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001741 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001742 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001743 I915_WRITE(DPLL(PIPE_B),
1744 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1745 I915_WRITE(DPLL(PIPE_A),
1746 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1747 }
1748
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001749 /* Don't disable pipe or pipe PLLs if needed */
1750 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1751 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001752 return;
1753
1754 /* Make sure the pipe isn't still relying on us */
1755 assert_pipe_disabled(dev_priv, pipe);
1756
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001757 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001758 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001759}
1760
Jesse Barnesf6071162013-10-01 10:41:38 -07001761static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1762{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001763 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001764
1765 /* Make sure the pipe isn't still relying on us */
1766 assert_pipe_disabled(dev_priv, pipe);
1767
Imre Deake5cbfbf2014-01-09 17:08:16 +02001768 /*
1769 * Leave integrated clock source and reference clock enabled for pipe B.
1770 * The latter is needed for VGA hotplug / manual detection.
1771 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001772 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001773 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001774 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001775 I915_WRITE(DPLL(pipe), val);
1776 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001777
1778}
1779
1780static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1781{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001782 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001783 u32 val;
1784
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001787
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001788 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001789 val = DPLL_SSC_REF_CLK_CHV |
1790 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001791 if (pipe != PIPE_A)
1792 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1793 I915_WRITE(DPLL(pipe), val);
1794 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001795
Ville Syrjäläa5805162015-05-26 20:42:30 +03001796 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001797
1798 /* Disable 10bit clock to display controller */
1799 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1800 val &= ~DPIO_DCLKP_EN;
1801 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1802
Ville Syrjäläa5805162015-05-26 20:42:30 +03001803 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001804}
1805
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001806void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001807 struct intel_digital_port *dport,
1808 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001809{
1810 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001811 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001812
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001813 switch (dport->port) {
1814 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001815 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001816 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001817 break;
1818 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001819 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001820 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001821 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001822 break;
1823 case PORT_D:
1824 port_mask = DPLL_PORTD_READY_MASK;
1825 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001826 break;
1827 default:
1828 BUG();
1829 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001830
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001831 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1832 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1833 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001834}
1835
Daniel Vetterb14b1052014-04-24 23:55:13 +02001836static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1837{
1838 struct drm_device *dev = crtc->base.dev;
1839 struct drm_i915_private *dev_priv = dev->dev_private;
1840 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1841
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001842 if (WARN_ON(pll == NULL))
1843 return;
1844
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001845 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001846 if (pll->active == 0) {
1847 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1848 WARN_ON(pll->on);
1849 assert_shared_dpll_disabled(dev_priv, pll);
1850
1851 pll->mode_set(dev_priv, pll);
1852 }
1853}
1854
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001855/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001856 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001857 * @dev_priv: i915 private structure
1858 * @pipe: pipe PLL to enable
1859 *
1860 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1861 * drives the transcoder clock.
1862 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001863static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001864{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001865 struct drm_device *dev = crtc->base.dev;
1866 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001867 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001868
Daniel Vetter87a875b2013-06-05 13:34:19 +02001869 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001870 return;
1871
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001872 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001873 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001874
Damien Lespiau74dd6922014-07-29 18:06:17 +01001875 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001876 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001877 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001878
Daniel Vettercdbd2312013-06-05 13:34:03 +02001879 if (pll->active++) {
1880 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001881 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001882 return;
1883 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001884 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001885
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001886 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1887
Daniel Vetter46edb022013-06-05 13:34:12 +02001888 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001889 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001890 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001891}
1892
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001893static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001894{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001895 struct drm_device *dev = crtc->base.dev;
1896 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001897 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001898
Jesse Barnes92f25842011-01-04 15:09:34 -08001899 /* PCH only available on ILK+ */
Jesse Barnes80aa9312015-08-03 13:09:11 -07001900 if (INTEL_INFO(dev)->gen < 5)
1901 return;
1902
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001903 if (pll == NULL)
1904 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001905
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001906 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001907 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001908
Daniel Vetter46edb022013-06-05 13:34:12 +02001909 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1910 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001911 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001912
Chris Wilson48da64a2012-05-13 20:16:12 +01001913 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001914 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001915 return;
1916 }
1917
Daniel Vettere9d69442013-06-05 13:34:15 +02001918 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001919 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001920 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001921 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001922
Daniel Vetter46edb022013-06-05 13:34:12 +02001923 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001924 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001925 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001926
1927 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001928}
1929
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001930static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1931 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001932{
Daniel Vetter23670b322012-11-01 09:15:30 +01001933 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001934 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001935 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001936 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001937
1938 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001939 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001940
1941 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001942 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001943 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001944
1945 /* FDI must be feeding us bits for PCH ports */
1946 assert_fdi_tx_enabled(dev_priv, pipe);
1947 assert_fdi_rx_enabled(dev_priv, pipe);
1948
Daniel Vetter23670b322012-11-01 09:15:30 +01001949 if (HAS_PCH_CPT(dev)) {
1950 /* Workaround: Set the timing override bit before enabling the
1951 * pch transcoder. */
1952 reg = TRANS_CHICKEN2(pipe);
1953 val = I915_READ(reg);
1954 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1955 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001956 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001957
Daniel Vetterab9412b2013-05-03 11:49:46 +02001958 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001959 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001960 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001961
1962 if (HAS_PCH_IBX(dev_priv->dev)) {
1963 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001964 * Make the BPC in transcoder be consistent with
1965 * that in pipeconf reg. For HDMI we must use 8bpc
1966 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07001967 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001968 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03001969 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1970 val |= PIPECONF_8BPC;
1971 else
1972 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001973 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001974
1975 val &= ~TRANS_INTERLACE_MASK;
1976 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001977 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001978 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001979 val |= TRANS_LEGACY_INTERLACED_ILK;
1980 else
1981 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001982 else
1983 val |= TRANS_PROGRESSIVE;
1984
Jesse Barnes040484a2011-01-03 12:14:26 -08001985 I915_WRITE(reg, val | TRANS_ENABLE);
1986 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001987 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001988}
1989
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001990static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001991 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001992{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001993 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001994
1995 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001996 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001997
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001998 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001999 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002000 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002001
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002002 /* Workaround: set timing override bit. */
2003 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002004 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002005 I915_WRITE(_TRANSA_CHICKEN2, val);
2006
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002007 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002008 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002009
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002010 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2011 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002012 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002013 else
2014 val |= TRANS_PROGRESSIVE;
2015
Daniel Vetterab9412b2013-05-03 11:49:46 +02002016 I915_WRITE(LPT_TRANSCONF, val);
2017 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002018 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002019}
2020
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002021static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2022 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002023{
Daniel Vetter23670b322012-11-01 09:15:30 +01002024 struct drm_device *dev = dev_priv->dev;
2025 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002026
2027 /* FDI relies on the transcoder */
2028 assert_fdi_tx_disabled(dev_priv, pipe);
2029 assert_fdi_rx_disabled(dev_priv, pipe);
2030
Jesse Barnes291906f2011-02-02 12:28:03 -08002031 /* Ports must be off as well */
2032 assert_pch_ports_disabled(dev_priv, pipe);
2033
Daniel Vetterab9412b2013-05-03 11:49:46 +02002034 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002035 val = I915_READ(reg);
2036 val &= ~TRANS_ENABLE;
2037 I915_WRITE(reg, val);
2038 /* wait for PCH transcoder off, transcoder state */
2039 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002040 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002041
2042 if (!HAS_PCH_IBX(dev)) {
2043 /* Workaround: Clear the timing override chicken bit again. */
2044 reg = TRANS_CHICKEN2(pipe);
2045 val = I915_READ(reg);
2046 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2047 I915_WRITE(reg, val);
2048 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002049}
2050
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002051static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002052{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002053 u32 val;
2054
Daniel Vetterab9412b2013-05-03 11:49:46 +02002055 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002056 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002057 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002058 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002059 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002060 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002061
2062 /* Workaround: clear timing override bit. */
2063 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002064 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002065 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002066}
2067
2068/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002069 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002070 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002071 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002072 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002073 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002074 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002075static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002076{
Paulo Zanoni03722642014-01-17 13:51:09 -02002077 struct drm_device *dev = crtc->base.dev;
2078 struct drm_i915_private *dev_priv = dev->dev_private;
2079 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002080 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2081 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002082 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002083 int reg;
2084 u32 val;
2085
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002086 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2087
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002088 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002089 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002090 assert_sprites_disabled(dev_priv, pipe);
2091
Paulo Zanoni681e5812012-12-06 11:12:38 -02002092 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002093 pch_transcoder = TRANSCODER_A;
2094 else
2095 pch_transcoder = pipe;
2096
Jesse Barnesb24e7172011-01-04 15:09:30 -08002097 /*
2098 * A pipe without a PLL won't actually be able to drive bits from
2099 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2100 * need the check.
2101 */
Imre Deak50360402015-01-16 00:55:16 -08002102 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002103 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002104 assert_dsi_pll_enabled(dev_priv);
2105 else
2106 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002107 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002108 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002109 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002110 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002111 assert_fdi_tx_pll_enabled(dev_priv,
2112 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002113 }
2114 /* FIXME: assert CPU port conditions for SNB+ */
2115 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002116
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002117 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002118 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002119 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002120 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2121 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002122 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002123 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002124
2125 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002126 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002127}
2128
2129/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002130 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002131 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002132 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002133 * Disable the pipe of @crtc, making sure that various hardware
2134 * specific requirements are met, if applicable, e.g. plane
2135 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002136 *
2137 * Will wait until the pipe has shut down before returning.
2138 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002139static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002140{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002141 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002142 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002143 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002144 int reg;
2145 u32 val;
2146
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002147 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2148
Jesse Barnesb24e7172011-01-04 15:09:30 -08002149 /*
2150 * Make sure planes won't keep trying to pump pixels to us,
2151 * or we might hang the display.
2152 */
2153 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002154 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002155 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002156
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002157 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002158 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002159 if ((val & PIPECONF_ENABLE) == 0)
2160 return;
2161
Ville Syrjälä67adc642014-08-15 01:21:57 +03002162 /*
2163 * Double wide has implications for planes
2164 * so best keep it disabled when not needed.
2165 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002166 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002167 val &= ~PIPECONF_DOUBLE_WIDE;
2168
2169 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002170 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2171 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002172 val &= ~PIPECONF_ENABLE;
2173
2174 I915_WRITE(reg, val);
2175 if ((val & PIPECONF_ENABLE) == 0)
2176 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002177}
2178
Chris Wilson693db182013-03-05 14:52:39 +00002179static bool need_vtd_wa(struct drm_device *dev)
2180{
2181#ifdef CONFIG_INTEL_IOMMU
2182 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2183 return true;
2184#endif
2185 return false;
2186}
2187
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002188unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002189intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2190 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002191{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002192 unsigned int tile_height;
2193 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002194
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002195 switch (fb_format_modifier) {
2196 case DRM_FORMAT_MOD_NONE:
2197 tile_height = 1;
2198 break;
2199 case I915_FORMAT_MOD_X_TILED:
2200 tile_height = IS_GEN2(dev) ? 16 : 8;
2201 break;
2202 case I915_FORMAT_MOD_Y_TILED:
2203 tile_height = 32;
2204 break;
2205 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002206 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2207 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002208 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002209 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002210 tile_height = 64;
2211 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002212 case 2:
2213 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002214 tile_height = 32;
2215 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002216 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002217 tile_height = 16;
2218 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002219 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002220 WARN_ONCE(1,
2221 "128-bit pixels are not supported for display!");
2222 tile_height = 16;
2223 break;
2224 }
2225 break;
2226 default:
2227 MISSING_CASE(fb_format_modifier);
2228 tile_height = 1;
2229 break;
2230 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002231
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002232 return tile_height;
2233}
2234
2235unsigned int
2236intel_fb_align_height(struct drm_device *dev, unsigned int height,
2237 uint32_t pixel_format, uint64_t fb_format_modifier)
2238{
2239 return ALIGN(height, intel_tile_height(dev, pixel_format,
2240 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002241}
2242
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002243static int
2244intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2245 const struct drm_plane_state *plane_state)
2246{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002247 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002248 unsigned int tile_height, tile_pitch;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002249
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002250 *view = i915_ggtt_view_normal;
2251
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002252 if (!plane_state)
2253 return 0;
2254
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002255 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002256 return 0;
2257
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002258 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002259
2260 info->height = fb->height;
2261 info->pixel_format = fb->pixel_format;
2262 info->pitch = fb->pitches[0];
2263 info->fb_modifier = fb->modifier[0];
2264
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002265 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2266 fb->modifier[0]);
2267 tile_pitch = PAGE_SIZE / tile_height;
2268 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2269 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2270 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2271
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002272 return 0;
2273}
2274
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002275static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2276{
2277 if (INTEL_INFO(dev_priv)->gen >= 9)
2278 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002279 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2280 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002281 return 128 * 1024;
2282 else if (INTEL_INFO(dev_priv)->gen >= 4)
2283 return 4 * 1024;
2284 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002285 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002286}
2287
Chris Wilson127bd2a2010-07-23 23:32:05 +01002288int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002289intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2290 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002291 const struct drm_plane_state *plane_state,
John Harrison91af1272015-06-18 13:14:56 +01002292 struct intel_engine_cs *pipelined,
2293 struct drm_i915_gem_request **pipelined_request)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002294{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002295 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002296 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002297 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002298 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002299 u32 alignment;
2300 int ret;
2301
Matt Roperebcdd392014-07-09 16:22:11 -07002302 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2303
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002304 switch (fb->modifier[0]) {
2305 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002306 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002307 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002308 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002309 if (INTEL_INFO(dev)->gen >= 9)
2310 alignment = 256 * 1024;
2311 else {
2312 /* pin() will align the object as required by fence */
2313 alignment = 0;
2314 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002315 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002316 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002317 case I915_FORMAT_MOD_Yf_TILED:
2318 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2319 "Y tiling bo slipped through, driver bug!\n"))
2320 return -EINVAL;
2321 alignment = 1 * 1024 * 1024;
2322 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002323 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002324 MISSING_CASE(fb->modifier[0]);
2325 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002326 }
2327
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002328 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2329 if (ret)
2330 return ret;
2331
Chris Wilson693db182013-03-05 14:52:39 +00002332 /* Note that the w/a also requires 64 PTE of padding following the
2333 * bo. We currently fill all unused PTE with the shadow page and so
2334 * we should always have valid PTE following the scanout preventing
2335 * the VT-d warning.
2336 */
2337 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2338 alignment = 256 * 1024;
2339
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002340 /*
2341 * Global gtt pte registers are special registers which actually forward
2342 * writes to a chunk of system memory. Which means that there is no risk
2343 * that the register values disappear as soon as we call
2344 * intel_runtime_pm_put(), so it is correct to wrap only the
2345 * pin/unpin/fence and not more.
2346 */
2347 intel_runtime_pm_get(dev_priv);
2348
Chris Wilsonce453d82011-02-21 14:43:56 +00002349 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002350 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
John Harrison91af1272015-06-18 13:14:56 +01002351 pipelined_request, &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002352 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002353 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002354
2355 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2356 * fence, whereas 965+ only requires a fence if using
2357 * framebuffer compression. For simplicity, we always install
2358 * a fence as the cost is not that onerous.
2359 */
Chris Wilson06d98132012-04-17 15:31:24 +01002360 ret = i915_gem_object_get_fence(obj);
Maarten Lankhorst842315e2015-08-05 12:37:11 +02002361 if (ret == -EDEADLK) {
2362 /*
2363 * -EDEADLK means there are no free fences
2364 * no pending flips.
2365 *
2366 * This is propagated to atomic, but it uses
2367 * -EDEADLK to force a locking recovery, so
2368 * change the returned error to -EBUSY.
2369 */
2370 ret = -EBUSY;
2371 goto err_unpin;
2372 } else if (ret)
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002373 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002374
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002375 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002376
Chris Wilsonce453d82011-02-21 14:43:56 +00002377 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002378 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002379 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002380
2381err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002382 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002383err_interruptible:
2384 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002385 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002386 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002387}
2388
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002389static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2390 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002391{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002392 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002393 struct i915_ggtt_view view;
2394 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002395
Matt Roperebcdd392014-07-09 16:22:11 -07002396 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2397
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002398 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2399 WARN_ONCE(ret, "Couldn't get view from plane state!");
2400
Chris Wilson1690e1e2011-12-14 13:57:08 +01002401 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002402 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002403}
2404
Daniel Vetterc2c75132012-07-05 12:17:30 +02002405/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2406 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002407unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2408 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002409 unsigned int tiling_mode,
2410 unsigned int cpp,
2411 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002412{
Chris Wilsonbc752862013-02-21 20:04:31 +00002413 if (tiling_mode != I915_TILING_NONE) {
2414 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002415
Chris Wilsonbc752862013-02-21 20:04:31 +00002416 tile_rows = *y / 8;
2417 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002418
Chris Wilsonbc752862013-02-21 20:04:31 +00002419 tiles = *x / (512/cpp);
2420 *x %= 512/cpp;
2421
2422 return tile_rows * pitch * 8 + tiles * 4096;
2423 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002424 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002425 unsigned int offset;
2426
2427 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002428 *y = (offset & alignment) / pitch;
2429 *x = ((offset & alignment) - *y * pitch) / cpp;
2430 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002431 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002432}
2433
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002434static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002435{
2436 switch (format) {
2437 case DISPPLANE_8BPP:
2438 return DRM_FORMAT_C8;
2439 case DISPPLANE_BGRX555:
2440 return DRM_FORMAT_XRGB1555;
2441 case DISPPLANE_BGRX565:
2442 return DRM_FORMAT_RGB565;
2443 default:
2444 case DISPPLANE_BGRX888:
2445 return DRM_FORMAT_XRGB8888;
2446 case DISPPLANE_RGBX888:
2447 return DRM_FORMAT_XBGR8888;
2448 case DISPPLANE_BGRX101010:
2449 return DRM_FORMAT_XRGB2101010;
2450 case DISPPLANE_RGBX101010:
2451 return DRM_FORMAT_XBGR2101010;
2452 }
2453}
2454
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002455static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2456{
2457 switch (format) {
2458 case PLANE_CTL_FORMAT_RGB_565:
2459 return DRM_FORMAT_RGB565;
2460 default:
2461 case PLANE_CTL_FORMAT_XRGB_8888:
2462 if (rgb_order) {
2463 if (alpha)
2464 return DRM_FORMAT_ABGR8888;
2465 else
2466 return DRM_FORMAT_XBGR8888;
2467 } else {
2468 if (alpha)
2469 return DRM_FORMAT_ARGB8888;
2470 else
2471 return DRM_FORMAT_XRGB8888;
2472 }
2473 case PLANE_CTL_FORMAT_XRGB_2101010:
2474 if (rgb_order)
2475 return DRM_FORMAT_XBGR2101010;
2476 else
2477 return DRM_FORMAT_XRGB2101010;
2478 }
2479}
2480
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002481static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002482intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2483 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002484{
2485 struct drm_device *dev = crtc->base.dev;
2486 struct drm_i915_gem_object *obj = NULL;
2487 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002488 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002489 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2490 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2491 PAGE_SIZE);
2492
2493 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002494
Chris Wilsonff2652e2014-03-10 08:07:02 +00002495 if (plane_config->size == 0)
2496 return false;
2497
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002498 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2499 base_aligned,
2500 base_aligned,
2501 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002502 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002503 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002504
Damien Lespiau49af4492015-01-20 12:51:44 +00002505 obj->tiling_mode = plane_config->tiling;
2506 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002507 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002508
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002509 mode_cmd.pixel_format = fb->pixel_format;
2510 mode_cmd.width = fb->width;
2511 mode_cmd.height = fb->height;
2512 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002513 mode_cmd.modifier[0] = fb->modifier[0];
2514 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002515
2516 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002517 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002518 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002519 DRM_DEBUG_KMS("intel fb init failed\n");
2520 goto out_unref_obj;
2521 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002522 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002523
Daniel Vetterf6936e22015-03-26 12:17:05 +01002524 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002525 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002526
2527out_unref_obj:
2528 drm_gem_object_unreference(&obj->base);
2529 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002530 return false;
2531}
2532
Matt Roperafd65eb2015-02-03 13:10:04 -08002533/* Update plane->state->fb to match plane->fb after driver-internal updates */
2534static void
2535update_state_fb(struct drm_plane *plane)
2536{
2537 if (plane->fb == plane->state->fb)
2538 return;
2539
2540 if (plane->state->fb)
2541 drm_framebuffer_unreference(plane->state->fb);
2542 plane->state->fb = plane->fb;
2543 if (plane->state->fb)
2544 drm_framebuffer_reference(plane->state->fb);
2545}
2546
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002547static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002548intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2549 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002550{
2551 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002552 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002553 struct drm_crtc *c;
2554 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002555 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002556 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002557 struct drm_plane_state *plane_state = primary->state;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002558 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559
Damien Lespiau2d140302015-02-05 17:22:18 +00002560 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002561 return;
2562
Daniel Vetterf6936e22015-03-26 12:17:05 +01002563 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002564 fb = &plane_config->fb->base;
2565 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002566 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567
Damien Lespiau2d140302015-02-05 17:22:18 +00002568 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002569
2570 /*
2571 * Failed to alloc the obj, check to see if we should share
2572 * an fb with another CRTC instead
2573 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002574 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002575 i = to_intel_crtc(c);
2576
2577 if (c == &intel_crtc->base)
2578 continue;
2579
Matt Roper2ff8fde2014-07-08 07:50:07 -07002580 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002581 continue;
2582
Daniel Vetter88595ac2015-03-26 12:42:24 +01002583 fb = c->primary->fb;
2584 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002585 continue;
2586
Daniel Vetter88595ac2015-03-26 12:42:24 +01002587 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002588 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002589 drm_framebuffer_reference(fb);
2590 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002591 }
2592 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002593
2594 return;
2595
2596valid_fb:
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002597 plane_state->src_x = plane_state->src_y = 0;
2598 plane_state->src_w = fb->width << 16;
2599 plane_state->src_h = fb->height << 16;
2600
2601 plane_state->crtc_x = plane_state->src_y = 0;
2602 plane_state->crtc_w = fb->width;
2603 plane_state->crtc_h = fb->height;
2604
Daniel Vetter88595ac2015-03-26 12:42:24 +01002605 obj = intel_fb_obj(fb);
2606 if (obj->tiling_mode != I915_TILING_NONE)
2607 dev_priv->preserve_bios_swizzle = true;
2608
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002609 drm_framebuffer_reference(fb);
2610 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002611 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002612 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002613 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002614}
2615
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002616static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2617 struct drm_framebuffer *fb,
2618 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002619{
2620 struct drm_device *dev = crtc->dev;
2621 struct drm_i915_private *dev_priv = dev->dev_private;
2622 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002623 struct drm_plane *primary = crtc->primary;
2624 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002625 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002626 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002627 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002628 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002629 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302630 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002631
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002632 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002633 I915_WRITE(reg, 0);
2634 if (INTEL_INFO(dev)->gen >= 4)
2635 I915_WRITE(DSPSURF(plane), 0);
2636 else
2637 I915_WRITE(DSPADDR(plane), 0);
2638 POSTING_READ(reg);
2639 return;
2640 }
2641
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002642 obj = intel_fb_obj(fb);
2643 if (WARN_ON(obj == NULL))
2644 return;
2645
2646 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2647
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002648 dspcntr = DISPPLANE_GAMMA_ENABLE;
2649
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002650 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002651
2652 if (INTEL_INFO(dev)->gen < 4) {
2653 if (intel_crtc->pipe == PIPE_B)
2654 dspcntr |= DISPPLANE_SEL_PIPE_B;
2655
2656 /* pipesrc and dspsize control the size that is scaled from,
2657 * which should always be the user's requested size.
2658 */
2659 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002660 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2661 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002662 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002663 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2664 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002665 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2666 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002667 I915_WRITE(PRIMPOS(plane), 0);
2668 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002669 }
2670
Ville Syrjälä57779d02012-10-31 17:50:14 +02002671 switch (fb->pixel_format) {
2672 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002673 dspcntr |= DISPPLANE_8BPP;
2674 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002675 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002676 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002677 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002678 case DRM_FORMAT_RGB565:
2679 dspcntr |= DISPPLANE_BGRX565;
2680 break;
2681 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002682 dspcntr |= DISPPLANE_BGRX888;
2683 break;
2684 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002685 dspcntr |= DISPPLANE_RGBX888;
2686 break;
2687 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002688 dspcntr |= DISPPLANE_BGRX101010;
2689 break;
2690 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002691 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002692 break;
2693 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002694 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002695 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002696
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002697 if (INTEL_INFO(dev)->gen >= 4 &&
2698 obj->tiling_mode != I915_TILING_NONE)
2699 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002700
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002701 if (IS_G4X(dev))
2702 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2703
Ville Syrjäläb98971272014-08-27 16:51:22 +03002704 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002705
Daniel Vetterc2c75132012-07-05 12:17:30 +02002706 if (INTEL_INFO(dev)->gen >= 4) {
2707 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002708 intel_gen4_compute_page_offset(dev_priv,
2709 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002710 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002711 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002712 linear_offset -= intel_crtc->dspaddr_offset;
2713 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002714 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002715 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002716
Matt Roper8e7d6882015-01-21 16:35:41 -08002717 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302718 dspcntr |= DISPPLANE_ROTATE_180;
2719
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002720 x += (intel_crtc->config->pipe_src_w - 1);
2721 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302722
2723 /* Finding the last pixel of the last line of the display
2724 data and adding to linear_offset*/
2725 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002726 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2727 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302728 }
2729
2730 I915_WRITE(reg, dspcntr);
2731
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002732 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002733 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002734 I915_WRITE(DSPSURF(plane),
2735 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002736 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002737 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002738 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002739 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002740 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002741}
2742
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002743static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2744 struct drm_framebuffer *fb,
2745 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002746{
2747 struct drm_device *dev = crtc->dev;
2748 struct drm_i915_private *dev_priv = dev->dev_private;
2749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002750 struct drm_plane *primary = crtc->primary;
2751 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002752 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002753 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002754 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002755 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002756 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302757 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002758
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002759 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002760 I915_WRITE(reg, 0);
2761 I915_WRITE(DSPSURF(plane), 0);
2762 POSTING_READ(reg);
2763 return;
2764 }
2765
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002766 obj = intel_fb_obj(fb);
2767 if (WARN_ON(obj == NULL))
2768 return;
2769
2770 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2771
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002772 dspcntr = DISPPLANE_GAMMA_ENABLE;
2773
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002774 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002775
2776 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2777 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2778
Ville Syrjälä57779d02012-10-31 17:50:14 +02002779 switch (fb->pixel_format) {
2780 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781 dspcntr |= DISPPLANE_8BPP;
2782 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002783 case DRM_FORMAT_RGB565:
2784 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002786 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002787 dspcntr |= DISPPLANE_BGRX888;
2788 break;
2789 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002790 dspcntr |= DISPPLANE_RGBX888;
2791 break;
2792 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002793 dspcntr |= DISPPLANE_BGRX101010;
2794 break;
2795 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002796 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002797 break;
2798 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002799 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002800 }
2801
2802 if (obj->tiling_mode != I915_TILING_NONE)
2803 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002804
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002805 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002806 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002807
Ville Syrjäläb98971272014-08-27 16:51:22 +03002808 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002809 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002810 intel_gen4_compute_page_offset(dev_priv,
2811 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002812 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002813 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002814 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002815 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302816 dspcntr |= DISPPLANE_ROTATE_180;
2817
2818 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002819 x += (intel_crtc->config->pipe_src_w - 1);
2820 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302821
2822 /* Finding the last pixel of the last line of the display
2823 data and adding to linear_offset*/
2824 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002825 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2826 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302827 }
2828 }
2829
2830 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002831
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002832 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002833 I915_WRITE(DSPSURF(plane),
2834 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002835 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002836 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2837 } else {
2838 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2839 I915_WRITE(DSPLINOFF(plane), linear_offset);
2840 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002841 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002842}
2843
Damien Lespiaub3218032015-02-27 11:15:18 +00002844u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2845 uint32_t pixel_format)
2846{
2847 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2848
2849 /*
2850 * The stride is either expressed as a multiple of 64 bytes
2851 * chunks for linear buffers or in number of tiles for tiled
2852 * buffers.
2853 */
2854 switch (fb_modifier) {
2855 case DRM_FORMAT_MOD_NONE:
2856 return 64;
2857 case I915_FORMAT_MOD_X_TILED:
2858 if (INTEL_INFO(dev)->gen == 2)
2859 return 128;
2860 return 512;
2861 case I915_FORMAT_MOD_Y_TILED:
2862 /* No need to check for old gens and Y tiling since this is
2863 * about the display engine and those will be blocked before
2864 * we get here.
2865 */
2866 return 128;
2867 case I915_FORMAT_MOD_Yf_TILED:
2868 if (bits_per_pixel == 8)
2869 return 64;
2870 else
2871 return 128;
2872 default:
2873 MISSING_CASE(fb_modifier);
2874 return 64;
2875 }
2876}
2877
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002878unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2879 struct drm_i915_gem_object *obj)
2880{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002881 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002882
2883 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002884 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002885
2886 return i915_gem_obj_ggtt_offset_view(obj, view);
2887}
2888
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002889static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2890{
2891 struct drm_device *dev = intel_crtc->base.dev;
2892 struct drm_i915_private *dev_priv = dev->dev_private;
2893
2894 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2895 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2896 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002897}
2898
Chandra Kondurua1b22782015-04-07 15:28:45 -07002899/*
2900 * This function detaches (aka. unbinds) unused scalers in hardware
2901 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002902static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002903{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002904 struct intel_crtc_scaler_state *scaler_state;
2905 int i;
2906
Chandra Kondurua1b22782015-04-07 15:28:45 -07002907 scaler_state = &intel_crtc->config->scaler_state;
2908
2909 /* loop through and disable scalers that aren't in use */
2910 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002911 if (!scaler_state->scalers[i].in_use)
2912 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002913 }
2914}
2915
Chandra Konduru6156a452015-04-27 13:48:39 -07002916u32 skl_plane_ctl_format(uint32_t pixel_format)
2917{
Chandra Konduru6156a452015-04-27 13:48:39 -07002918 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002919 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002920 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002921 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002922 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002923 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002924 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002925 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002926 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002927 /*
2928 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2929 * to be already pre-multiplied. We need to add a knob (or a different
2930 * DRM_FORMAT) for user-space to configure that.
2931 */
2932 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002933 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002934 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002935 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002936 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002938 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002939 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002940 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002941 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002942 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002943 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002951 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002953
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002954 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002955}
2956
2957u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2958{
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 switch (fb_modifier) {
2960 case DRM_FORMAT_MOD_NONE:
2961 break;
2962 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002963 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002964 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002965 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002966 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002967 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 default:
2969 MISSING_CASE(fb_modifier);
2970 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002971
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002972 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973}
2974
2975u32 skl_plane_ctl_rotation(unsigned int rotation)
2976{
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 switch (rotation) {
2978 case BIT(DRM_ROTATE_0):
2979 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302980 /*
2981 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2982 * while i915 HW rotation is clockwise, thats why this swapping.
2983 */
Chandra Konduru6156a452015-04-27 13:48:39 -07002984 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302985 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07002986 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002987 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07002988 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05302989 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07002990 default:
2991 MISSING_CASE(rotation);
2992 }
2993
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002994 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002995}
2996
Damien Lespiau70d21f02013-07-03 21:06:04 +01002997static void skylake_update_primary_plane(struct drm_crtc *crtc,
2998 struct drm_framebuffer *fb,
2999 int x, int y)
3000{
3001 struct drm_device *dev = crtc->dev;
3002 struct drm_i915_private *dev_priv = dev->dev_private;
3003 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003004 struct drm_plane *plane = crtc->primary;
3005 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003006 struct drm_i915_gem_object *obj;
3007 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303008 u32 plane_ctl, stride_div, stride;
3009 u32 tile_height, plane_offset, plane_size;
3010 unsigned int rotation;
3011 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003012 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 struct intel_crtc_state *crtc_state = intel_crtc->config;
3014 struct intel_plane_state *plane_state;
3015 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3016 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3017 int scaler_id = -1;
3018
Chandra Konduru6156a452015-04-27 13:48:39 -07003019 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003020
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003021 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003022 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3023 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3024 POSTING_READ(PLANE_CTL(pipe, 0));
3025 return;
3026 }
3027
3028 plane_ctl = PLANE_CTL_ENABLE |
3029 PLANE_CTL_PIPE_GAMMA_ENABLE |
3030 PLANE_CTL_PIPE_CSC_ENABLE;
3031
Chandra Konduru6156a452015-04-27 13:48:39 -07003032 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3033 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003034 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303035
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303036 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003037 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003038
Damien Lespiaub3218032015-02-27 11:15:18 +00003039 obj = intel_fb_obj(fb);
3040 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3041 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303042 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3043
Chandra Konduru6156a452015-04-27 13:48:39 -07003044 /*
3045 * FIXME: intel_plane_state->src, dst aren't set when transitional
3046 * update_plane helpers are called from legacy paths.
3047 * Once full atomic crtc is available, below check can be avoided.
3048 */
3049 if (drm_rect_width(&plane_state->src)) {
3050 scaler_id = plane_state->scaler_id;
3051 src_x = plane_state->src.x1 >> 16;
3052 src_y = plane_state->src.y1 >> 16;
3053 src_w = drm_rect_width(&plane_state->src) >> 16;
3054 src_h = drm_rect_height(&plane_state->src) >> 16;
3055 dst_x = plane_state->dst.x1;
3056 dst_y = plane_state->dst.y1;
3057 dst_w = drm_rect_width(&plane_state->dst);
3058 dst_h = drm_rect_height(&plane_state->dst);
3059
3060 WARN_ON(x != src_x || y != src_y);
3061 } else {
3062 src_w = intel_crtc->config->pipe_src_w;
3063 src_h = intel_crtc->config->pipe_src_h;
3064 }
3065
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303066 if (intel_rotation_90_or_270(rotation)) {
3067 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003068 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303069 fb->modifier[0]);
3070 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003071 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303072 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003073 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303074 } else {
3075 stride = fb->pitches[0] / stride_div;
3076 x_offset = x;
3077 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003078 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303079 }
3080 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003081
Damien Lespiau70d21f02013-07-03 21:06:04 +01003082 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303083 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3084 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3085 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003086
3087 if (scaler_id >= 0) {
3088 uint32_t ps_ctrl = 0;
3089
3090 WARN_ON(!dst_w || !dst_h);
3091 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3092 crtc_state->scaler_state.scalers[scaler_id].mode;
3093 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3094 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3095 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3096 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3097 I915_WRITE(PLANE_POS(pipe, 0), 0);
3098 } else {
3099 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3100 }
3101
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003102 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003103
3104 POSTING_READ(PLANE_SURF(pipe, 0));
3105}
3106
Jesse Barnes17638cd2011-06-24 12:19:23 -07003107/* Assume fb object is pinned & idle & fenced and just update base pointers */
3108static int
3109intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3110 int x, int y, enum mode_set_atomic state)
3111{
3112 struct drm_device *dev = crtc->dev;
3113 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003114
Paulo Zanoniff2a3112015-07-07 15:26:03 -03003115 if (dev_priv->fbc.disable_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03003116 dev_priv->fbc.disable_fbc(dev_priv);
Jesse Barnes81255562010-08-02 12:07:50 -07003117
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003118 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3119
3120 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003121}
3122
Ville Syrjälä75147472014-11-24 18:28:11 +02003123static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003124{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003125 struct drm_crtc *crtc;
3126
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003127 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3129 enum plane plane = intel_crtc->plane;
3130
3131 intel_prepare_page_flip(dev, plane);
3132 intel_finish_page_flip_plane(dev, plane);
3133 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003134}
3135
3136static void intel_update_primary_planes(struct drm_device *dev)
3137{
Ville Syrjälä75147472014-11-24 18:28:11 +02003138 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003139
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003140 for_each_crtc(dev, crtc) {
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003141 struct intel_plane *plane = to_intel_plane(crtc->primary);
3142 struct intel_plane_state *plane_state;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003143
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003144 drm_modeset_lock_crtc(crtc, &plane->base);
3145
3146 plane_state = to_intel_plane_state(plane->base.state);
3147
3148 if (plane_state->base.fb)
3149 plane->commit_plane(&plane->base, plane_state);
3150
3151 drm_modeset_unlock_crtc(crtc);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003152 }
3153}
3154
Ville Syrjälä75147472014-11-24 18:28:11 +02003155void intel_prepare_reset(struct drm_device *dev)
3156{
3157 /* no reset support for gen2 */
3158 if (IS_GEN2(dev))
3159 return;
3160
3161 /* reset doesn't touch the display */
3162 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3163 return;
3164
3165 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003166 /*
3167 * Disabling the crtcs gracefully seems nicer. Also the
3168 * g33 docs say we should at least disable all the planes.
3169 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003170 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003171}
3172
3173void intel_finish_reset(struct drm_device *dev)
3174{
3175 struct drm_i915_private *dev_priv = to_i915(dev);
3176
3177 /*
3178 * Flips in the rings will be nuked by the reset,
3179 * so complete all pending flips so that user space
3180 * will get its events and not get stuck.
3181 */
3182 intel_complete_page_flips(dev);
3183
3184 /* no reset support for gen2 */
3185 if (IS_GEN2(dev))
3186 return;
3187
3188 /* reset doesn't touch the display */
3189 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3190 /*
3191 * Flips in the rings have been nuked by the reset,
3192 * so update the base address of all primary
3193 * planes to the the last fb to make sure we're
3194 * showing the correct fb after a reset.
Maarten Lankhorst11c22da2015-09-10 16:07:58 +02003195 *
3196 * FIXME: Atomic will make this obsolete since we won't schedule
3197 * CS-based flips (which might get lost in gpu resets) any more.
Ville Syrjälä75147472014-11-24 18:28:11 +02003198 */
3199 intel_update_primary_planes(dev);
3200 return;
3201 }
3202
3203 /*
3204 * The display has been reset as well,
3205 * so need a full re-initialization.
3206 */
3207 intel_runtime_pm_disable_interrupts(dev_priv);
3208 intel_runtime_pm_enable_interrupts(dev_priv);
3209
3210 intel_modeset_init_hw(dev);
3211
3212 spin_lock_irq(&dev_priv->irq_lock);
3213 if (dev_priv->display.hpd_irq_setup)
3214 dev_priv->display.hpd_irq_setup(dev);
3215 spin_unlock_irq(&dev_priv->irq_lock);
3216
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003217 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003218
3219 intel_hpd_init(dev_priv);
3220
3221 drm_modeset_unlock_all(dev);
3222}
3223
Chris Wilson2e2f3512015-04-27 13:41:14 +01003224static void
Chris Wilson14667a42012-04-03 17:58:35 +01003225intel_finish_fb(struct drm_framebuffer *old_fb)
3226{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003227 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003228 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003229 bool was_interruptible = dev_priv->mm.interruptible;
3230 int ret;
3231
Chris Wilson14667a42012-04-03 17:58:35 +01003232 /* Big Hammer, we also need to ensure that any pending
3233 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3234 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003235 * framebuffer. Note that we rely on userspace rendering
3236 * into the buffer attached to the pipe they are waiting
3237 * on. If not, userspace generates a GPU hang with IPEHR
3238 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003239 *
3240 * This should only fail upon a hung GPU, in which case we
3241 * can safely continue.
3242 */
3243 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003244 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003245 dev_priv->mm.interruptible = was_interruptible;
3246
Chris Wilson2e2f3512015-04-27 13:41:14 +01003247 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003248}
3249
Chris Wilson7d5e3792014-03-04 13:15:08 +00003250static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3251{
3252 struct drm_device *dev = crtc->dev;
3253 struct drm_i915_private *dev_priv = dev->dev_private;
3254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003255 bool pending;
3256
3257 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3258 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3259 return false;
3260
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003261 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003262 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003263 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003264
3265 return pending;
3266}
3267
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003268static void intel_update_pipe_size(struct intel_crtc *crtc)
3269{
3270 struct drm_device *dev = crtc->base.dev;
3271 struct drm_i915_private *dev_priv = dev->dev_private;
3272 const struct drm_display_mode *adjusted_mode;
3273
3274 if (!i915.fastboot)
3275 return;
3276
3277 /*
3278 * Update pipe size and adjust fitter if needed: the reason for this is
3279 * that in compute_mode_changes we check the native mode (not the pfit
3280 * mode) to see if we can flip rather than do a full mode set. In the
3281 * fastboot case, we'll flip, but if we don't update the pipesrc and
3282 * pfit state, we'll end up with a big fb scanned out into the wrong
3283 * sized surface.
3284 *
3285 * To fix this properly, we need to hoist the checks up into
3286 * compute_mode_changes (or above), check the actual pfit state and
3287 * whether the platform allows pfit disable with pipe active, and only
3288 * then update the pipesrc and pfit state, even on the flip path.
3289 */
3290
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003291 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003292
3293 I915_WRITE(PIPESRC(crtc->pipe),
3294 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3295 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003296 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003297 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3298 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003299 I915_WRITE(PF_CTL(crtc->pipe), 0);
3300 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3301 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3302 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003303 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3304 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003305}
3306
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003307static void intel_fdi_normal_train(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);
3312 int pipe = intel_crtc->pipe;
3313 u32 reg, temp;
3314
3315 /* enable normal train */
3316 reg = FDI_TX_CTL(pipe);
3317 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003318 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003319 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3320 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003321 } else {
3322 temp &= ~FDI_LINK_TRAIN_NONE;
3323 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003324 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003325 I915_WRITE(reg, temp);
3326
3327 reg = FDI_RX_CTL(pipe);
3328 temp = I915_READ(reg);
3329 if (HAS_PCH_CPT(dev)) {
3330 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3331 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3332 } else {
3333 temp &= ~FDI_LINK_TRAIN_NONE;
3334 temp |= FDI_LINK_TRAIN_NONE;
3335 }
3336 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3337
3338 /* wait one idle pattern time */
3339 POSTING_READ(reg);
3340 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003341
3342 /* IVB wants error correction enabled */
3343 if (IS_IVYBRIDGE(dev))
3344 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3345 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003346}
3347
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003348/* The FDI link training functions for ILK/Ibexpeak. */
3349static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3350{
3351 struct drm_device *dev = crtc->dev;
3352 struct drm_i915_private *dev_priv = dev->dev_private;
3353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3354 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003355 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003356
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003357 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003358 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003359
Adam Jacksone1a44742010-06-25 15:32:14 -04003360 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3361 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003362 reg = FDI_RX_IMR(pipe);
3363 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003364 temp &= ~FDI_RX_SYMBOL_LOCK;
3365 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003366 I915_WRITE(reg, temp);
3367 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003368 udelay(150);
3369
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003370 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003371 reg = FDI_TX_CTL(pipe);
3372 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003373 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003374 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003375 temp &= ~FDI_LINK_TRAIN_NONE;
3376 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003377 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003378
Chris Wilson5eddb702010-09-11 13:48:45 +01003379 reg = FDI_RX_CTL(pipe);
3380 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381 temp &= ~FDI_LINK_TRAIN_NONE;
3382 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003383 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3384
3385 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003386 udelay(150);
3387
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003388 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003389 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3390 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3391 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003392
Chris Wilson5eddb702010-09-11 13:48:45 +01003393 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003394 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003395 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3397
3398 if ((temp & FDI_RX_BIT_LOCK)) {
3399 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003400 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003401 break;
3402 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003404 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003406
3407 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003408 reg = FDI_TX_CTL(pipe);
3409 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003410 temp &= ~FDI_LINK_TRAIN_NONE;
3411 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003412 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413
Chris Wilson5eddb702010-09-11 13:48:45 +01003414 reg = FDI_RX_CTL(pipe);
3415 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003416 temp &= ~FDI_LINK_TRAIN_NONE;
3417 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003418 I915_WRITE(reg, temp);
3419
3420 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003421 udelay(150);
3422
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003424 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003426 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3427
3428 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003429 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003430 DRM_DEBUG_KMS("FDI train 2 done.\n");
3431 break;
3432 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003434 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003435 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003436
3437 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003438
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439}
3440
Akshay Joshi0206e352011-08-16 15:34:10 -04003441static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3443 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3444 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3445 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3446};
3447
3448/* The FDI link training functions for SNB/Cougarpoint. */
3449static void gen6_fdi_link_train(struct drm_crtc *crtc)
3450{
3451 struct drm_device *dev = crtc->dev;
3452 struct drm_i915_private *dev_priv = dev->dev_private;
3453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3454 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003455 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456
Adam Jacksone1a44742010-06-25 15:32:14 -04003457 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3458 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 reg = FDI_RX_IMR(pipe);
3460 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003461 temp &= ~FDI_RX_SYMBOL_LOCK;
3462 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003463 I915_WRITE(reg, temp);
3464
3465 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003466 udelay(150);
3467
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 reg = FDI_TX_CTL(pipe);
3470 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003471 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003472 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 temp &= ~FDI_LINK_TRAIN_NONE;
3474 temp |= FDI_LINK_TRAIN_PATTERN_1;
3475 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3476 /* SNB-B */
3477 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479
Daniel Vetterd74cf322012-10-26 10:58:13 +02003480 I915_WRITE(FDI_RX_MISC(pipe),
3481 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3482
Chris Wilson5eddb702010-09-11 13:48:45 +01003483 reg = FDI_RX_CTL(pipe);
3484 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485 if (HAS_PCH_CPT(dev)) {
3486 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3487 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3488 } else {
3489 temp &= ~FDI_LINK_TRAIN_NONE;
3490 temp |= FDI_LINK_TRAIN_PATTERN_1;
3491 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3493
3494 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003495 udelay(150);
3496
Akshay Joshi0206e352011-08-16 15:34:10 -04003497 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003498 reg = FDI_TX_CTL(pipe);
3499 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003500 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3501 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 I915_WRITE(reg, temp);
3503
3504 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 udelay(500);
3506
Sean Paulfa37d392012-03-02 12:53:39 -05003507 for (retry = 0; retry < 5; retry++) {
3508 reg = FDI_RX_IIR(pipe);
3509 temp = I915_READ(reg);
3510 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3511 if (temp & FDI_RX_BIT_LOCK) {
3512 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3513 DRM_DEBUG_KMS("FDI train 1 done.\n");
3514 break;
3515 }
3516 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003517 }
Sean Paulfa37d392012-03-02 12:53:39 -05003518 if (retry < 5)
3519 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003520 }
3521 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003522 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003523
3524 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003525 reg = FDI_TX_CTL(pipe);
3526 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003527 temp &= ~FDI_LINK_TRAIN_NONE;
3528 temp |= FDI_LINK_TRAIN_PATTERN_2;
3529 if (IS_GEN6(dev)) {
3530 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3531 /* SNB-B */
3532 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3533 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003534 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003535
Chris Wilson5eddb702010-09-11 13:48:45 +01003536 reg = FDI_RX_CTL(pipe);
3537 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 if (HAS_PCH_CPT(dev)) {
3539 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3540 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3541 } else {
3542 temp &= ~FDI_LINK_TRAIN_NONE;
3543 temp |= FDI_LINK_TRAIN_PATTERN_2;
3544 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003545 I915_WRITE(reg, temp);
3546
3547 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548 udelay(150);
3549
Akshay Joshi0206e352011-08-16 15:34:10 -04003550 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003551 reg = FDI_TX_CTL(pipe);
3552 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003553 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3554 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003555 I915_WRITE(reg, temp);
3556
3557 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003558 udelay(500);
3559
Sean Paulfa37d392012-03-02 12:53:39 -05003560 for (retry = 0; retry < 5; retry++) {
3561 reg = FDI_RX_IIR(pipe);
3562 temp = I915_READ(reg);
3563 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3564 if (temp & FDI_RX_SYMBOL_LOCK) {
3565 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3566 DRM_DEBUG_KMS("FDI train 2 done.\n");
3567 break;
3568 }
3569 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003570 }
Sean Paulfa37d392012-03-02 12:53:39 -05003571 if (retry < 5)
3572 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003573 }
3574 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003575 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003576
3577 DRM_DEBUG_KMS("FDI train done.\n");
3578}
3579
Jesse Barnes357555c2011-04-28 15:09:55 -07003580/* Manual link training for Ivy Bridge A0 parts */
3581static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3582{
3583 struct drm_device *dev = crtc->dev;
3584 struct drm_i915_private *dev_priv = dev->dev_private;
3585 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3586 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003587 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003588
3589 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3590 for train result */
3591 reg = FDI_RX_IMR(pipe);
3592 temp = I915_READ(reg);
3593 temp &= ~FDI_RX_SYMBOL_LOCK;
3594 temp &= ~FDI_RX_BIT_LOCK;
3595 I915_WRITE(reg, temp);
3596
3597 POSTING_READ(reg);
3598 udelay(150);
3599
Daniel Vetter01a415f2012-10-27 15:58:40 +02003600 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3601 I915_READ(FDI_RX_IIR(pipe)));
3602
Jesse Barnes139ccd32013-08-19 11:04:55 -07003603 /* Try each vswing and preemphasis setting twice before moving on */
3604 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3605 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003606 reg = FDI_TX_CTL(pipe);
3607 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003608 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3609 temp &= ~FDI_TX_ENABLE;
3610 I915_WRITE(reg, temp);
3611
3612 reg = FDI_RX_CTL(pipe);
3613 temp = I915_READ(reg);
3614 temp &= ~FDI_LINK_TRAIN_AUTO;
3615 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3616 temp &= ~FDI_RX_ENABLE;
3617 I915_WRITE(reg, temp);
3618
3619 /* enable CPU FDI TX and PCH FDI RX */
3620 reg = FDI_TX_CTL(pipe);
3621 temp = I915_READ(reg);
3622 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003623 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003624 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003625 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003626 temp |= snb_b_fdi_train_param[j/2];
3627 temp |= FDI_COMPOSITE_SYNC;
3628 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3629
3630 I915_WRITE(FDI_RX_MISC(pipe),
3631 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3632
3633 reg = FDI_RX_CTL(pipe);
3634 temp = I915_READ(reg);
3635 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3636 temp |= FDI_COMPOSITE_SYNC;
3637 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3638
3639 POSTING_READ(reg);
3640 udelay(1); /* should be 0.5us */
3641
3642 for (i = 0; i < 4; i++) {
3643 reg = FDI_RX_IIR(pipe);
3644 temp = I915_READ(reg);
3645 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3646
3647 if (temp & FDI_RX_BIT_LOCK ||
3648 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3649 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3650 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3651 i);
3652 break;
3653 }
3654 udelay(1); /* should be 0.5us */
3655 }
3656 if (i == 4) {
3657 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3658 continue;
3659 }
3660
3661 /* Train 2 */
3662 reg = FDI_TX_CTL(pipe);
3663 temp = I915_READ(reg);
3664 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3665 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3666 I915_WRITE(reg, temp);
3667
3668 reg = FDI_RX_CTL(pipe);
3669 temp = I915_READ(reg);
3670 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3671 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003672 I915_WRITE(reg, temp);
3673
3674 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003675 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003676
Jesse Barnes139ccd32013-08-19 11:04:55 -07003677 for (i = 0; i < 4; i++) {
3678 reg = FDI_RX_IIR(pipe);
3679 temp = I915_READ(reg);
3680 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003681
Jesse Barnes139ccd32013-08-19 11:04:55 -07003682 if (temp & FDI_RX_SYMBOL_LOCK ||
3683 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3684 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3685 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3686 i);
3687 goto train_done;
3688 }
3689 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003690 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003691 if (i == 4)
3692 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003693 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003694
Jesse Barnes139ccd32013-08-19 11:04:55 -07003695train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003696 DRM_DEBUG_KMS("FDI train done.\n");
3697}
3698
Daniel Vetter88cefb62012-08-12 19:27:14 +02003699static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003700{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003701 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003702 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003703 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003704 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003705
Jesse Barnesc64e3112010-09-10 11:27:03 -07003706
Jesse Barnes0e23b992010-09-10 11:10:00 -07003707 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003708 reg = FDI_RX_CTL(pipe);
3709 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003710 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003711 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003712 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003713 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3714
3715 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003716 udelay(200);
3717
3718 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003719 temp = I915_READ(reg);
3720 I915_WRITE(reg, temp | FDI_PCDCLK);
3721
3722 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003723 udelay(200);
3724
Paulo Zanoni20749732012-11-23 15:30:38 -02003725 /* Enable CPU FDI TX PLL, always on for Ironlake */
3726 reg = FDI_TX_CTL(pipe);
3727 temp = I915_READ(reg);
3728 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3729 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003730
Paulo Zanoni20749732012-11-23 15:30:38 -02003731 POSTING_READ(reg);
3732 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003733 }
3734}
3735
Daniel Vetter88cefb62012-08-12 19:27:14 +02003736static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3737{
3738 struct drm_device *dev = intel_crtc->base.dev;
3739 struct drm_i915_private *dev_priv = dev->dev_private;
3740 int pipe = intel_crtc->pipe;
3741 u32 reg, temp;
3742
3743 /* Switch from PCDclk to Rawclk */
3744 reg = FDI_RX_CTL(pipe);
3745 temp = I915_READ(reg);
3746 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3747
3748 /* Disable CPU FDI TX PLL */
3749 reg = FDI_TX_CTL(pipe);
3750 temp = I915_READ(reg);
3751 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3752
3753 POSTING_READ(reg);
3754 udelay(100);
3755
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3759
3760 /* Wait for the clocks to turn off. */
3761 POSTING_READ(reg);
3762 udelay(100);
3763}
3764
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003765static void ironlake_fdi_disable(struct drm_crtc *crtc)
3766{
3767 struct drm_device *dev = crtc->dev;
3768 struct drm_i915_private *dev_priv = dev->dev_private;
3769 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3770 int pipe = intel_crtc->pipe;
3771 u32 reg, temp;
3772
3773 /* disable CPU FDI tx and PCH FDI rx */
3774 reg = FDI_TX_CTL(pipe);
3775 temp = I915_READ(reg);
3776 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3777 POSTING_READ(reg);
3778
3779 reg = FDI_RX_CTL(pipe);
3780 temp = I915_READ(reg);
3781 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003782 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003783 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3784
3785 POSTING_READ(reg);
3786 udelay(100);
3787
3788 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003789 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003790 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003791
3792 /* still set train pattern 1 */
3793 reg = FDI_TX_CTL(pipe);
3794 temp = I915_READ(reg);
3795 temp &= ~FDI_LINK_TRAIN_NONE;
3796 temp |= FDI_LINK_TRAIN_PATTERN_1;
3797 I915_WRITE(reg, temp);
3798
3799 reg = FDI_RX_CTL(pipe);
3800 temp = I915_READ(reg);
3801 if (HAS_PCH_CPT(dev)) {
3802 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3803 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3804 } else {
3805 temp &= ~FDI_LINK_TRAIN_NONE;
3806 temp |= FDI_LINK_TRAIN_PATTERN_1;
3807 }
3808 /* BPC in FDI rx is consistent with that in PIPECONF */
3809 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003810 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003811 I915_WRITE(reg, temp);
3812
3813 POSTING_READ(reg);
3814 udelay(100);
3815}
3816
Chris Wilson5dce5b932014-01-20 10:17:36 +00003817bool intel_has_pending_fb_unpin(struct drm_device *dev)
3818{
3819 struct intel_crtc *crtc;
3820
3821 /* Note that we don't need to be called with mode_config.lock here
3822 * as our list of CRTC objects is static for the lifetime of the
3823 * device and so cannot disappear as we iterate. Similarly, we can
3824 * happily treat the predicates as racy, atomic checks as userspace
3825 * cannot claim and pin a new fb without at least acquring the
3826 * struct_mutex and so serialising with us.
3827 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003828 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003829 if (atomic_read(&crtc->unpin_work_count) == 0)
3830 continue;
3831
3832 if (crtc->unpin_work)
3833 intel_wait_for_vblank(dev, crtc->pipe);
3834
3835 return true;
3836 }
3837
3838 return false;
3839}
3840
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003841static void page_flip_completed(struct intel_crtc *intel_crtc)
3842{
3843 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3844 struct intel_unpin_work *work = intel_crtc->unpin_work;
3845
3846 /* ensure that the unpin work is consistent wrt ->pending. */
3847 smp_rmb();
3848 intel_crtc->unpin_work = NULL;
3849
3850 if (work->event)
3851 drm_send_vblank_event(intel_crtc->base.dev,
3852 intel_crtc->pipe,
3853 work->event);
3854
3855 drm_crtc_vblank_put(&intel_crtc->base);
3856
3857 wake_up_all(&dev_priv->pending_flip_queue);
3858 queue_work(dev_priv->wq, &work->work);
3859
3860 trace_i915_flip_complete(intel_crtc->plane,
3861 work->pending_flip_obj);
3862}
3863
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003864void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003865{
Chris Wilson0f911282012-04-17 10:05:38 +01003866 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003867 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003868
Daniel Vetter2c10d572012-12-20 21:24:07 +01003869 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003870 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3871 !intel_crtc_has_pending_flip(crtc),
3872 60*HZ) == 0)) {
3873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003874
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003875 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003876 if (intel_crtc->unpin_work) {
3877 WARN_ONCE(1, "Removing stuck page flip\n");
3878 page_flip_completed(intel_crtc);
3879 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003880 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003881 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003882
Chris Wilson975d5682014-08-20 13:13:34 +01003883 if (crtc->primary->fb) {
3884 mutex_lock(&dev->struct_mutex);
3885 intel_finish_fb(crtc->primary->fb);
3886 mutex_unlock(&dev->struct_mutex);
3887 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003888}
3889
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003890/* Program iCLKIP clock to the desired frequency */
3891static void lpt_program_iclkip(struct drm_crtc *crtc)
3892{
3893 struct drm_device *dev = crtc->dev;
3894 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003895 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003896 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3897 u32 temp;
3898
Ville Syrjäläa5805162015-05-26 20:42:30 +03003899 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003900
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003901 /* It is necessary to ungate the pixclk gate prior to programming
3902 * the divisors, and gate it back when it is done.
3903 */
3904 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3905
3906 /* Disable SSCCTL */
3907 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003908 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3909 SBI_SSCCTL_DISABLE,
3910 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003911
3912 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003913 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003914 auxdiv = 1;
3915 divsel = 0x41;
3916 phaseinc = 0x20;
3917 } else {
3918 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003919 * but the adjusted_mode->crtc_clock in in KHz. To get the
3920 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003921 * convert the virtual clock precision to KHz here for higher
3922 * precision.
3923 */
3924 u32 iclk_virtual_root_freq = 172800 * 1000;
3925 u32 iclk_pi_range = 64;
3926 u32 desired_divisor, msb_divisor_value, pi_value;
3927
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003928 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003929 msb_divisor_value = desired_divisor / iclk_pi_range;
3930 pi_value = desired_divisor % iclk_pi_range;
3931
3932 auxdiv = 0;
3933 divsel = msb_divisor_value - 2;
3934 phaseinc = pi_value;
3935 }
3936
3937 /* This should not happen with any sane values */
3938 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3939 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3940 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3941 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3942
3943 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 +03003944 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003945 auxdiv,
3946 divsel,
3947 phasedir,
3948 phaseinc);
3949
3950 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003951 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003952 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3953 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3954 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3955 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3956 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3957 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003958 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003959
3960 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003961 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003962 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3963 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003964 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003965
3966 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003967 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003968 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003969 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003970
3971 /* Wait for initialization time */
3972 udelay(24);
3973
3974 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003975
Ville Syrjäläa5805162015-05-26 20:42:30 +03003976 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003977}
3978
Daniel Vetter275f01b22013-05-03 11:49:47 +02003979static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3980 enum pipe pch_transcoder)
3981{
3982 struct drm_device *dev = crtc->base.dev;
3983 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003984 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003985
3986 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3987 I915_READ(HTOTAL(cpu_transcoder)));
3988 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3989 I915_READ(HBLANK(cpu_transcoder)));
3990 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3991 I915_READ(HSYNC(cpu_transcoder)));
3992
3993 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3994 I915_READ(VTOTAL(cpu_transcoder)));
3995 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3996 I915_READ(VBLANK(cpu_transcoder)));
3997 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3998 I915_READ(VSYNC(cpu_transcoder)));
3999 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4000 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4001}
4002
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004003static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004004{
4005 struct drm_i915_private *dev_priv = dev->dev_private;
4006 uint32_t temp;
4007
4008 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004009 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004010 return;
4011
4012 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4013 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4014
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004015 temp &= ~FDI_BC_BIFURCATION_SELECT;
4016 if (enable)
4017 temp |= FDI_BC_BIFURCATION_SELECT;
4018
4019 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004020 I915_WRITE(SOUTH_CHICKEN1, temp);
4021 POSTING_READ(SOUTH_CHICKEN1);
4022}
4023
4024static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4025{
4026 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004027
4028 switch (intel_crtc->pipe) {
4029 case PIPE_A:
4030 break;
4031 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004032 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004033 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004034 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004035 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004036
4037 break;
4038 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004039 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004040
4041 break;
4042 default:
4043 BUG();
4044 }
4045}
4046
Jesse Barnesf67a5592011-01-05 10:31:48 -08004047/*
4048 * Enable PCH resources required for PCH ports:
4049 * - PCH PLLs
4050 * - FDI training & RX/TX
4051 * - update transcoder timings
4052 * - DP transcoding bits
4053 * - transcoder
4054 */
4055static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004056{
4057 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004058 struct drm_i915_private *dev_priv = dev->dev_private;
4059 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4060 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004061 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004062
Daniel Vetterab9412b2013-05-03 11:49:46 +02004063 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004064
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004065 if (IS_IVYBRIDGE(dev))
4066 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4067
Daniel Vettercd986ab2012-10-26 10:58:12 +02004068 /* Write the TU size bits before fdi link training, so that error
4069 * detection works. */
4070 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4071 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4072
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004073 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004074 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004075
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004076 /* We need to program the right clock selection before writing the pixel
4077 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004078 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004079 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004080
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004081 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004082 temp |= TRANS_DPLL_ENABLE(pipe);
4083 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004084 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004085 temp |= sel;
4086 else
4087 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004088 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004089 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004090
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004091 /* XXX: pch pll's can be enabled any time before we enable the PCH
4092 * transcoder, and we actually should do this to not upset any PCH
4093 * transcoder that already use the clock when we share it.
4094 *
4095 * Note that enable_shared_dpll tries to do the right thing, but
4096 * get_shared_dpll unconditionally resets the pll - we need that to have
4097 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004098 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004099
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004100 /* set transcoder timing, panel must allow it */
4101 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004102 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004103
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004104 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004105
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004106 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004107 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004108 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004109 reg = TRANS_DP_CTL(pipe);
4110 temp = I915_READ(reg);
4111 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004112 TRANS_DP_SYNC_MASK |
4113 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004114 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004115 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004116
4117 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004118 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004119 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004120 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004121
4122 switch (intel_trans_dp_port_sel(crtc)) {
4123 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004124 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004125 break;
4126 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004127 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128 break;
4129 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004130 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004131 break;
4132 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004133 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004134 }
4135
Chris Wilson5eddb702010-09-11 13:48:45 +01004136 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137 }
4138
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004139 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004140}
4141
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004142static void lpt_pch_enable(struct drm_crtc *crtc)
4143{
4144 struct drm_device *dev = crtc->dev;
4145 struct drm_i915_private *dev_priv = dev->dev_private;
4146 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004147 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004148
Daniel Vetterab9412b2013-05-03 11:49:46 +02004149 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004150
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004151 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004152
Paulo Zanoni0540e482012-10-31 18:12:40 -02004153 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004154 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004155
Paulo Zanoni937bb612012-10-31 18:12:47 -02004156 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004157}
4158
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004159struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4160 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004161{
Daniel Vettere2b78262013-06-07 23:10:03 +02004162 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004163 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004164 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004165 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004166
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004167 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4168
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004169 if (HAS_PCH_IBX(dev_priv->dev)) {
4170 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004171 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004172 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004173
Daniel Vetter46edb022013-06-05 13:34:12 +02004174 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4175 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004176
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004177 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004178
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004179 goto found;
4180 }
4181
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304182 if (IS_BROXTON(dev_priv->dev)) {
4183 /* PLL is attached to port in bxt */
4184 struct intel_encoder *encoder;
4185 struct intel_digital_port *intel_dig_port;
4186
4187 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4188 if (WARN_ON(!encoder))
4189 return NULL;
4190
4191 intel_dig_port = enc_to_dig_port(&encoder->base);
4192 /* 1:1 mapping between ports and PLLs */
4193 i = (enum intel_dpll_id)intel_dig_port->port;
4194 pll = &dev_priv->shared_dplls[i];
4195 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4196 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004197 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304198
4199 goto found;
4200 }
4201
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004202 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4203 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004204
4205 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004206 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004207 continue;
4208
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004209 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004210 &shared_dpll[i].hw_state,
4211 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004212 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004213 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004214 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004215 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004216 goto found;
4217 }
4218 }
4219
4220 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004221 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4222 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004223 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004224 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4225 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004226 goto found;
4227 }
4228 }
4229
4230 return NULL;
4231
4232found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004233 if (shared_dpll[i].crtc_mask == 0)
4234 shared_dpll[i].hw_state =
4235 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004236
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004237 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004238 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4239 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004240
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004241 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004242
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004243 return pll;
4244}
4245
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004246static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004247{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004248 struct drm_i915_private *dev_priv = to_i915(state->dev);
4249 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004250 struct intel_shared_dpll *pll;
4251 enum intel_dpll_id i;
4252
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004253 if (!to_intel_atomic_state(state)->dpll_set)
4254 return;
4255
4256 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004257 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4258 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004259 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004260 }
4261}
4262
Daniel Vettera1520312013-05-03 11:49:50 +02004263static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004264{
4265 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004266 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004267 u32 temp;
4268
4269 temp = I915_READ(dslreg);
4270 udelay(500);
4271 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004272 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004273 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004274 }
4275}
4276
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004277static int
4278skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4279 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4280 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004281{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004282 struct intel_crtc_scaler_state *scaler_state =
4283 &crtc_state->scaler_state;
4284 struct intel_crtc *intel_crtc =
4285 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004286 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004287
4288 need_scaling = intel_rotation_90_or_270(rotation) ?
4289 (src_h != dst_w || src_w != dst_h):
4290 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004291
4292 /*
4293 * if plane is being disabled or scaler is no more required or force detach
4294 * - free scaler binded to this plane/crtc
4295 * - in order to do this, update crtc->scaler_usage
4296 *
4297 * Here scaler state in crtc_state is set free so that
4298 * scaler can be assigned to other user. Actual register
4299 * update to free the scaler is done in plane/panel-fit programming.
4300 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4301 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004302 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004303 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004304 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004305 scaler_state->scalers[*scaler_id].in_use = 0;
4306
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004307 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4308 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4309 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004310 scaler_state->scaler_users);
4311 *scaler_id = -1;
4312 }
4313 return 0;
4314 }
4315
4316 /* range checks */
4317 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4318 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4319
4320 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4321 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004322 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004323 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004324 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004325 return -EINVAL;
4326 }
4327
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004328 /* mark this plane as a scaler user in crtc_state */
4329 scaler_state->scaler_users |= (1 << scaler_user);
4330 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4331 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4332 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4333 scaler_state->scaler_users);
4334
4335 return 0;
4336}
4337
4338/**
4339 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4340 *
4341 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004342 *
4343 * Return
4344 * 0 - scaler_usage updated successfully
4345 * error - requested scaling cannot be supported or other error condition
4346 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004347int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004348{
4349 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4350 struct drm_display_mode *adjusted_mode =
4351 &state->base.adjusted_mode;
4352
4353 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4354 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4355
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004356 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004357 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4358 state->pipe_src_w, state->pipe_src_h,
Imre Deak8c6cda22015-06-23 20:40:27 +03004359 adjusted_mode->hdisplay, adjusted_mode->vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004360}
4361
4362/**
4363 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4364 *
4365 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004366 * @plane_state: atomic plane state to update
4367 *
4368 * Return
4369 * 0 - scaler_usage updated successfully
4370 * error - requested scaling cannot be supported or other error condition
4371 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004372static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4373 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004374{
4375
4376 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004377 struct intel_plane *intel_plane =
4378 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004379 struct drm_framebuffer *fb = plane_state->base.fb;
4380 int ret;
4381
4382 bool force_detach = !fb || !plane_state->visible;
4383
4384 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4385 intel_plane->base.base.id, intel_crtc->pipe,
4386 drm_plane_index(&intel_plane->base));
4387
4388 ret = skl_update_scaler(crtc_state, force_detach,
4389 drm_plane_index(&intel_plane->base),
4390 &plane_state->scaler_id,
4391 plane_state->base.rotation,
4392 drm_rect_width(&plane_state->src) >> 16,
4393 drm_rect_height(&plane_state->src) >> 16,
4394 drm_rect_width(&plane_state->dst),
4395 drm_rect_height(&plane_state->dst));
4396
4397 if (ret || plane_state->scaler_id < 0)
4398 return ret;
4399
Chandra Kondurua1b22782015-04-07 15:28:45 -07004400 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004401 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004402 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004403 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004404 return -EINVAL;
4405 }
4406
4407 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004408 switch (fb->pixel_format) {
4409 case DRM_FORMAT_RGB565:
4410 case DRM_FORMAT_XBGR8888:
4411 case DRM_FORMAT_XRGB8888:
4412 case DRM_FORMAT_ABGR8888:
4413 case DRM_FORMAT_ARGB8888:
4414 case DRM_FORMAT_XRGB2101010:
4415 case DRM_FORMAT_XBGR2101010:
4416 case DRM_FORMAT_YUYV:
4417 case DRM_FORMAT_YVYU:
4418 case DRM_FORMAT_UYVY:
4419 case DRM_FORMAT_VYUY:
4420 break;
4421 default:
4422 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4423 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4424 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004425 }
4426
Chandra Kondurua1b22782015-04-07 15:28:45 -07004427 return 0;
4428}
4429
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004430static void skylake_scaler_disable(struct intel_crtc *crtc)
4431{
4432 int i;
4433
4434 for (i = 0; i < crtc->num_scalers; i++)
4435 skl_detach_scaler(crtc, i);
4436}
4437
4438static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004439{
4440 struct drm_device *dev = crtc->base.dev;
4441 struct drm_i915_private *dev_priv = dev->dev_private;
4442 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004443 struct intel_crtc_scaler_state *scaler_state =
4444 &crtc->config->scaler_state;
4445
4446 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4447
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004448 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004449 int id;
4450
4451 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4452 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4453 return;
4454 }
4455
4456 id = scaler_state->scaler_id;
4457 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4458 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4459 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4460 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4461
4462 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004463 }
4464}
4465
Jesse Barnesb074cec2013-04-25 12:55:02 -07004466static void ironlake_pfit_enable(struct intel_crtc *crtc)
4467{
4468 struct drm_device *dev = crtc->base.dev;
4469 struct drm_i915_private *dev_priv = dev->dev_private;
4470 int pipe = crtc->pipe;
4471
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004472 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004473 /* Force use of hard-coded filter coefficients
4474 * as some pre-programmed values are broken,
4475 * e.g. x201.
4476 */
4477 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4478 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4479 PF_PIPE_SEL_IVB(pipe));
4480 else
4481 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004482 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4483 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004484 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004485}
4486
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004487void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004488{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004489 struct drm_device *dev = crtc->base.dev;
4490 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004491
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004492 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004493 return;
4494
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004495 /* We can only enable IPS after we enable a plane and wait for a vblank */
4496 intel_wait_for_vblank(dev, crtc->pipe);
4497
Paulo Zanonid77e4532013-09-24 13:52:55 -03004498 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004499 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004500 mutex_lock(&dev_priv->rps.hw_lock);
4501 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4502 mutex_unlock(&dev_priv->rps.hw_lock);
4503 /* Quoting Art Runyan: "its not safe to expect any particular
4504 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004505 * mailbox." Moreover, the mailbox may return a bogus state,
4506 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004507 */
4508 } else {
4509 I915_WRITE(IPS_CTL, IPS_ENABLE);
4510 /* The bit only becomes 1 in the next vblank, so this wait here
4511 * is essentially intel_wait_for_vblank. If we don't have this
4512 * and don't wait for vblanks until the end of crtc_enable, then
4513 * the HW state readout code will complain that the expected
4514 * IPS_CTL value is not the one we read. */
4515 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4516 DRM_ERROR("Timed out waiting for IPS enable\n");
4517 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004518}
4519
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004520void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004521{
4522 struct drm_device *dev = crtc->base.dev;
4523 struct drm_i915_private *dev_priv = dev->dev_private;
4524
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004525 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004526 return;
4527
4528 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004529 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004530 mutex_lock(&dev_priv->rps.hw_lock);
4531 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4532 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004533 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4534 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4535 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004536 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004537 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004538 POSTING_READ(IPS_CTL);
4539 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004540
4541 /* We need to wait for a vblank before we can disable the plane. */
4542 intel_wait_for_vblank(dev, crtc->pipe);
4543}
4544
4545/** Loads the palette/gamma unit for the CRTC with the prepared values */
4546static void intel_crtc_load_lut(struct drm_crtc *crtc)
4547{
4548 struct drm_device *dev = crtc->dev;
4549 struct drm_i915_private *dev_priv = dev->dev_private;
4550 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4551 enum pipe pipe = intel_crtc->pipe;
4552 int palreg = PALETTE(pipe);
4553 int i;
4554 bool reenable_ips = false;
4555
4556 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004557 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004558 return;
4559
Imre Deak50360402015-01-16 00:55:16 -08004560 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004561 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004562 assert_dsi_pll_enabled(dev_priv);
4563 else
4564 assert_pll_enabled(dev_priv, pipe);
4565 }
4566
4567 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304568 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004569 palreg = LGC_PALETTE(pipe);
4570
4571 /* Workaround : Do not read or write the pipe palette/gamma data while
4572 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4573 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004574 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004575 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4576 GAMMA_MODE_MODE_SPLIT)) {
4577 hsw_disable_ips(intel_crtc);
4578 reenable_ips = true;
4579 }
4580
4581 for (i = 0; i < 256; i++) {
4582 I915_WRITE(palreg + 4 * i,
4583 (intel_crtc->lut_r[i] << 16) |
4584 (intel_crtc->lut_g[i] << 8) |
4585 intel_crtc->lut_b[i]);
4586 }
4587
4588 if (reenable_ips)
4589 hsw_enable_ips(intel_crtc);
4590}
4591
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004592static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004593{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004594 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004595 struct drm_device *dev = intel_crtc->base.dev;
4596 struct drm_i915_private *dev_priv = dev->dev_private;
4597
4598 mutex_lock(&dev->struct_mutex);
4599 dev_priv->mm.interruptible = false;
4600 (void) intel_overlay_switch_off(intel_crtc->overlay);
4601 dev_priv->mm.interruptible = true;
4602 mutex_unlock(&dev->struct_mutex);
4603 }
4604
4605 /* Let userspace switch the overlay on again. In most cases userspace
4606 * has to recompute where to put it anyway.
4607 */
4608}
4609
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004610/**
4611 * intel_post_enable_primary - Perform operations after enabling primary plane
4612 * @crtc: the CRTC whose primary plane was just enabled
4613 *
4614 * Performs potentially sleeping operations that must be done after the primary
4615 * plane is enabled, such as updating FBC and IPS. Note that this may be
4616 * called due to an explicit primary plane update, or due to an implicit
4617 * re-enable that is caused when a sprite plane is updated to no longer
4618 * completely hide the primary plane.
4619 */
4620static void
4621intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004622{
4623 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004624 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4626 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004627
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004628 /*
4629 * BDW signals flip done immediately if the plane
4630 * is disabled, even if the plane enable is already
4631 * armed to occur at the next vblank :(
4632 */
4633 if (IS_BROADWELL(dev))
4634 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004635
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004636 /*
4637 * FIXME IPS should be fine as long as one plane is
4638 * enabled, but in practice it seems to have problems
4639 * when going from primary only to sprite only and vice
4640 * versa.
4641 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004642 hsw_enable_ips(intel_crtc);
4643
Daniel Vetterf99d7062014-06-19 16:01:59 +02004644 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004645 * Gen2 reports pipe underruns whenever all planes are disabled.
4646 * So don't enable underrun reporting before at least some planes
4647 * are enabled.
4648 * FIXME: Need to fix the logic to work when we turn off all planes
4649 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004650 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004651 if (IS_GEN2(dev))
4652 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4653
4654 /* Underruns don't raise interrupts, so check manually. */
4655 if (HAS_GMCH_DISPLAY(dev))
4656 i9xx_check_fifo_underruns(dev_priv);
4657}
4658
4659/**
4660 * intel_pre_disable_primary - Perform operations before disabling primary plane
4661 * @crtc: the CRTC whose primary plane is to be disabled
4662 *
4663 * Performs potentially sleeping operations that must be done before the
4664 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4665 * be called due to an explicit primary plane update, or due to an implicit
4666 * disable that is caused when a sprite plane completely hides the primary
4667 * plane.
4668 */
4669static void
4670intel_pre_disable_primary(struct drm_crtc *crtc)
4671{
4672 struct drm_device *dev = crtc->dev;
4673 struct drm_i915_private *dev_priv = dev->dev_private;
4674 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4675 int pipe = intel_crtc->pipe;
4676
4677 /*
4678 * Gen2 reports pipe underruns whenever all planes are disabled.
4679 * So diasble underrun reporting before all the planes get disabled.
4680 * FIXME: Need to fix the logic to work when we turn off all planes
4681 * but leave the pipe running.
4682 */
4683 if (IS_GEN2(dev))
4684 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4685
4686 /*
4687 * Vblank time updates from the shadow to live plane control register
4688 * are blocked if the memory self-refresh mode is active at that
4689 * moment. So to make sure the plane gets truly disabled, disable
4690 * first the self-refresh mode. The self-refresh enable bit in turn
4691 * will be checked/applied by the HW only at the next frame start
4692 * event which is after the vblank start event, so we need to have a
4693 * wait-for-vblank between disabling the plane and the pipe.
4694 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004695 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004696 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004697 dev_priv->wm.vlv.cxsr = false;
4698 intel_wait_for_vblank(dev, pipe);
4699 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004700
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004701 /*
4702 * FIXME IPS should be fine as long as one plane is
4703 * enabled, but in practice it seems to have problems
4704 * when going from primary only to sprite only and vice
4705 * versa.
4706 */
4707 hsw_disable_ips(intel_crtc);
4708}
4709
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004710static void intel_post_plane_update(struct intel_crtc *crtc)
4711{
4712 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4713 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -03004714 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004715 struct drm_plane *plane;
4716
4717 if (atomic->wait_vblank)
4718 intel_wait_for_vblank(dev, crtc->pipe);
4719
4720 intel_frontbuffer_flip(dev, atomic->fb_bits);
4721
Ville Syrjälä852eb002015-06-24 22:00:07 +03004722 if (atomic->disable_cxsr)
4723 crtc->wm.cxsr_allowed = true;
4724
Ville Syrjäläf015c552015-06-24 22:00:02 +03004725 if (crtc->atomic.update_wm_post)
4726 intel_update_watermarks(&crtc->base);
4727
Paulo Zanonic80ac852015-07-02 19:25:13 -03004728 if (atomic->update_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03004729 intel_fbc_update(dev_priv);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004730
4731 if (atomic->post_enable_primary)
4732 intel_post_enable_primary(&crtc->base);
4733
4734 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4735 intel_update_sprite_watermarks(plane, &crtc->base,
4736 0, 0, 0, false, false);
4737
4738 memset(atomic, 0, sizeof(*atomic));
4739}
4740
4741static void intel_pre_plane_update(struct intel_crtc *crtc)
4742{
4743 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004744 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004745 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4746 struct drm_plane *p;
4747
4748 /* Track fb's for any planes being disabled */
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004749 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4750 struct intel_plane *plane = to_intel_plane(p);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004751
4752 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03004753 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4754 plane->frontbuffer_bit);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004755 mutex_unlock(&dev->struct_mutex);
4756 }
4757
4758 if (atomic->wait_for_flips)
4759 intel_crtc_wait_for_pending_flips(&crtc->base);
4760
Paulo Zanonic80ac852015-07-02 19:25:13 -03004761 if (atomic->disable_fbc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03004762 intel_fbc_disable_crtc(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004763
Rodrigo Vivi066cf552015-06-26 13:55:54 -07004764 if (crtc->atomic.disable_ips)
4765 hsw_disable_ips(crtc);
4766
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004767 if (atomic->pre_disable_primary)
4768 intel_pre_disable_primary(&crtc->base);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004769
4770 if (atomic->disable_cxsr) {
4771 crtc->wm.cxsr_allowed = false;
4772 intel_set_memory_cxsr(dev_priv, false);
4773 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004774}
4775
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004776static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004777{
4778 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004779 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004780 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004781 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004782
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004783 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004784
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004785 drm_for_each_plane_mask(p, dev, plane_mask)
4786 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004787
Daniel Vetterf99d7062014-06-19 16:01:59 +02004788 /*
4789 * FIXME: Once we grow proper nuclear flip support out of this we need
4790 * to compute the mask of flip planes precisely. For the time being
4791 * consider this a flip to a NULL plane.
4792 */
4793 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004794}
4795
Jesse Barnesf67a5592011-01-05 10:31:48 -08004796static void ironlake_crtc_enable(struct drm_crtc *crtc)
4797{
4798 struct drm_device *dev = crtc->dev;
4799 struct drm_i915_private *dev_priv = dev->dev_private;
4800 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004801 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004802 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004803
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004804 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004805 return;
4806
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004807 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004808 intel_prepare_shared_dpll(intel_crtc);
4809
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004810 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304811 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004812
4813 intel_set_pipe_timings(intel_crtc);
4814
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004815 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004816 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004817 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004818 }
4819
4820 ironlake_set_pipeconf(crtc);
4821
Jesse Barnesf67a5592011-01-05 10:31:48 -08004822 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004823
Daniel Vettera72e4c92014-09-30 10:56:47 +02004824 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4825 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004826
Daniel Vetterf6736a12013-06-05 13:34:30 +02004827 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004828 if (encoder->pre_enable)
4829 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004830
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004831 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004832 /* Note: FDI PLL enabling _must_ be done before we enable the
4833 * cpu pipes, hence this is separate from all the other fdi/pch
4834 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004835 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004836 } else {
4837 assert_fdi_tx_disabled(dev_priv, pipe);
4838 assert_fdi_rx_disabled(dev_priv, pipe);
4839 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004840
Jesse Barnesb074cec2013-04-25 12:55:02 -07004841 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004842
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004843 /*
4844 * On ILK+ LUT must be loaded before the pipe is running but with
4845 * clocks enabled
4846 */
4847 intel_crtc_load_lut(crtc);
4848
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004849 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004850 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004851
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004852 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004853 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004854
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004855 assert_vblank_disabled(crtc);
4856 drm_crtc_vblank_on(crtc);
4857
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004858 for_each_encoder_on_crtc(dev, crtc, encoder)
4859 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004860
4861 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004862 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004863}
4864
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004865/* IPS only exists on ULT machines and is tied to pipe A. */
4866static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4867{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004868 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004869}
4870
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004871static void haswell_crtc_enable(struct drm_crtc *crtc)
4872{
4873 struct drm_device *dev = crtc->dev;
4874 struct drm_i915_private *dev_priv = dev->dev_private;
4875 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4876 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004877 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4878 struct intel_crtc_state *pipe_config =
4879 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004880
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004881 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004882 return;
4883
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004884 if (intel_crtc_to_shared_dpll(intel_crtc))
4885 intel_enable_shared_dpll(intel_crtc);
4886
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004887 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304888 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004889
4890 intel_set_pipe_timings(intel_crtc);
4891
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004892 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4893 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4894 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004895 }
4896
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004897 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004898 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004899 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004900 }
4901
4902 haswell_set_pipeconf(crtc);
4903
4904 intel_set_pipe_csc(crtc);
4905
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004906 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004907
Daniel Vettera72e4c92014-09-30 10:56:47 +02004908 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004909 for_each_encoder_on_crtc(dev, crtc, encoder)
4910 if (encoder->pre_enable)
4911 encoder->pre_enable(encoder);
4912
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004913 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004914 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4915 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004916 dev_priv->display.fdi_link_train(crtc);
4917 }
4918
Paulo Zanoni1f544382012-10-24 11:32:00 -02004919 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004920
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004921 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004922 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004923 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004924 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004925
4926 /*
4927 * On ILK+ LUT must be loaded before the pipe is running but with
4928 * clocks enabled
4929 */
4930 intel_crtc_load_lut(crtc);
4931
Paulo Zanoni1f544382012-10-24 11:32:00 -02004932 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004933 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004934
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004935 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004936 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004937
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004938 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004939 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004940
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004941 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004942 intel_ddi_set_vc_payload_alloc(crtc, true);
4943
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004944 assert_vblank_disabled(crtc);
4945 drm_crtc_vblank_on(crtc);
4946
Jani Nikula8807e552013-08-30 19:40:32 +03004947 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004948 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004949 intel_opregion_notify_encoder(encoder, true);
4950 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004951
Paulo Zanonie4916942013-09-20 16:21:19 -03004952 /* If we change the relative order between pipe/planes enabling, we need
4953 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004954 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4955 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4956 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4957 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4958 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004959}
4960
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004961static void ironlake_pfit_disable(struct intel_crtc *crtc)
4962{
4963 struct drm_device *dev = crtc->base.dev;
4964 struct drm_i915_private *dev_priv = dev->dev_private;
4965 int pipe = crtc->pipe;
4966
4967 /* To avoid upsetting the power well on haswell only disable the pfit if
4968 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004969 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004970 I915_WRITE(PF_CTL(pipe), 0);
4971 I915_WRITE(PF_WIN_POS(pipe), 0);
4972 I915_WRITE(PF_WIN_SZ(pipe), 0);
4973 }
4974}
4975
Jesse Barnes6be4a602010-09-10 10:26:01 -07004976static void ironlake_crtc_disable(struct drm_crtc *crtc)
4977{
4978 struct drm_device *dev = crtc->dev;
4979 struct drm_i915_private *dev_priv = dev->dev_private;
4980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004981 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004982 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004983 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004984
Daniel Vetterea9d7582012-07-10 10:42:52 +02004985 for_each_encoder_on_crtc(dev, crtc, encoder)
4986 encoder->disable(encoder);
4987
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004988 drm_crtc_vblank_off(crtc);
4989 assert_vblank_disabled(crtc);
4990
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004991 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004992 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004993
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004994 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004995
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004996 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004997
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004998 if (intel_crtc->config->has_pch_encoder)
4999 ironlake_fdi_disable(crtc);
5000
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005001 for_each_encoder_on_crtc(dev, crtc, encoder)
5002 if (encoder->post_disable)
5003 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005004
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005005 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005006 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005007
Daniel Vetterd925c592013-06-05 13:34:04 +02005008 if (HAS_PCH_CPT(dev)) {
5009 /* disable TRANS_DP_CTL */
5010 reg = TRANS_DP_CTL(pipe);
5011 temp = I915_READ(reg);
5012 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5013 TRANS_DP_PORT_SEL_MASK);
5014 temp |= TRANS_DP_PORT_SEL_NONE;
5015 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005016
Daniel Vetterd925c592013-06-05 13:34:04 +02005017 /* disable DPLL_SEL */
5018 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005019 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005020 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005021 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005022
Daniel Vetterd925c592013-06-05 13:34:04 +02005023 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005024 }
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02005025
5026 intel_crtc->active = false;
5027 intel_update_watermarks(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005028}
5029
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005030static void haswell_crtc_disable(struct drm_crtc *crtc)
5031{
5032 struct drm_device *dev = crtc->dev;
5033 struct drm_i915_private *dev_priv = dev->dev_private;
5034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5035 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005036 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005037
Jani Nikula8807e552013-08-30 19:40:32 +03005038 for_each_encoder_on_crtc(dev, crtc, encoder) {
5039 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005040 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005041 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005042
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005043 drm_crtc_vblank_off(crtc);
5044 assert_vblank_disabled(crtc);
5045
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005046 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005047 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5048 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005049 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005050
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005051 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005052 intel_ddi_set_vc_payload_alloc(crtc, false);
5053
Paulo Zanoniad80a812012-10-24 16:06:19 -02005054 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005055
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005056 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005057 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005058 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005059 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005060
Paulo Zanoni1f544382012-10-24 11:32:00 -02005061 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005062
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005063 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005064 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005065 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005066 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067
Imre Deak97b040a2014-06-25 22:01:50 +03005068 for_each_encoder_on_crtc(dev, crtc, encoder)
5069 if (encoder->post_disable)
5070 encoder->post_disable(encoder);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02005071
5072 intel_crtc->active = false;
5073 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005074}
5075
Jesse Barnes2dd24552013-04-25 12:55:01 -07005076static void i9xx_pfit_enable(struct intel_crtc *crtc)
5077{
5078 struct drm_device *dev = crtc->base.dev;
5079 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005080 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005081
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005082 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005083 return;
5084
Daniel Vetterc0b03412013-05-28 12:05:54 +02005085 /*
5086 * The panel fitter should only be adjusted whilst the pipe is disabled,
5087 * according to register description and PRM.
5088 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005089 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5090 assert_pipe_disabled(dev_priv, crtc->pipe);
5091
Jesse Barnesb074cec2013-04-25 12:55:02 -07005092 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5093 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005094
5095 /* Border color in case we don't scale up to the full screen. Black by
5096 * default, change to something else for debugging. */
5097 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005098}
5099
Dave Airlied05410f2014-06-05 13:22:59 +10005100static enum intel_display_power_domain port_to_power_domain(enum port port)
5101{
5102 switch (port) {
5103 case PORT_A:
5104 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5105 case PORT_B:
5106 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5107 case PORT_C:
5108 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5109 case PORT_D:
5110 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005111 case PORT_E:
5112 return POWER_DOMAIN_PORT_DDI_E_2_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005113 default:
5114 WARN_ON_ONCE(1);
5115 return POWER_DOMAIN_PORT_OTHER;
5116 }
5117}
5118
Imre Deak77d22dc2014-03-05 16:20:52 +02005119#define for_each_power_domain(domain, mask) \
5120 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5121 if ((1 << (domain)) & (mask))
5122
Imre Deak319be8a2014-03-04 19:22:57 +02005123enum intel_display_power_domain
5124intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005125{
Imre Deak319be8a2014-03-04 19:22:57 +02005126 struct drm_device *dev = intel_encoder->base.dev;
5127 struct intel_digital_port *intel_dig_port;
5128
5129 switch (intel_encoder->type) {
5130 case INTEL_OUTPUT_UNKNOWN:
5131 /* Only DDI platforms should ever use this output type */
5132 WARN_ON_ONCE(!HAS_DDI(dev));
5133 case INTEL_OUTPUT_DISPLAYPORT:
5134 case INTEL_OUTPUT_HDMI:
5135 case INTEL_OUTPUT_EDP:
5136 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005137 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005138 case INTEL_OUTPUT_DP_MST:
5139 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5140 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005141 case INTEL_OUTPUT_ANALOG:
5142 return POWER_DOMAIN_PORT_CRT;
5143 case INTEL_OUTPUT_DSI:
5144 return POWER_DOMAIN_PORT_DSI;
5145 default:
5146 return POWER_DOMAIN_PORT_OTHER;
5147 }
5148}
5149
5150static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5151{
5152 struct drm_device *dev = crtc->dev;
5153 struct intel_encoder *intel_encoder;
5154 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5155 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005156 unsigned long mask;
5157 enum transcoder transcoder;
5158
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005159 if (!crtc->state->active)
5160 return 0;
5161
Imre Deak77d22dc2014-03-05 16:20:52 +02005162 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5163
5164 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5165 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005166 if (intel_crtc->config->pch_pfit.enabled ||
5167 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005168 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5169
Imre Deak319be8a2014-03-04 19:22:57 +02005170 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5171 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5172
Imre Deak77d22dc2014-03-05 16:20:52 +02005173 return mask;
5174}
5175
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005176static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
5177{
5178 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5180 enum intel_display_power_domain domain;
5181 unsigned long domains, new_domains, old_domains;
5182
5183 old_domains = intel_crtc->enabled_power_domains;
5184 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
5185
5186 domains = new_domains & ~old_domains;
5187
5188 for_each_power_domain(domain, domains)
5189 intel_display_power_get(dev_priv, domain);
5190
5191 return old_domains & ~new_domains;
5192}
5193
5194static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5195 unsigned long domains)
5196{
5197 enum intel_display_power_domain domain;
5198
5199 for_each_power_domain(domain, domains)
5200 intel_display_power_put(dev_priv, domain);
5201}
5202
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005203static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005204{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005205 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005206 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005207 unsigned long put_domains[I915_MAX_PIPES] = {};
5208 struct drm_crtc_state *crtc_state;
5209 struct drm_crtc *crtc;
5210 int i;
Imre Deak77d22dc2014-03-05 16:20:52 +02005211
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005212 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5213 if (needs_modeset(crtc->state))
5214 put_domains[to_intel_crtc(crtc)->pipe] =
5215 modeset_get_crtc_power_domains(crtc);
Imre Deak77d22dc2014-03-05 16:20:52 +02005216 }
5217
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005218 if (dev_priv->display.modeset_commit_cdclk) {
5219 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5220
5221 if (cdclk != dev_priv->cdclk_freq &&
5222 !WARN_ON(!state->allow_modeset))
5223 dev_priv->display.modeset_commit_cdclk(state);
5224 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005225
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005226 for (i = 0; i < I915_MAX_PIPES; i++)
5227 if (put_domains[i])
5228 modeset_put_power_domains(dev_priv, put_domains[i]);
Imre Deak77d22dc2014-03-05 16:20:52 +02005229}
5230
Mika Kaholaadafdc62015-08-18 14:36:59 +03005231static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5232{
5233 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5234
5235 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5236 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5237 return max_cdclk_freq;
5238 else if (IS_CHERRYVIEW(dev_priv))
5239 return max_cdclk_freq*95/100;
5240 else if (INTEL_INFO(dev_priv)->gen < 4)
5241 return 2*max_cdclk_freq*90/100;
5242 else
5243 return max_cdclk_freq*90/100;
5244}
5245
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005246static void intel_update_max_cdclk(struct drm_device *dev)
5247{
5248 struct drm_i915_private *dev_priv = dev->dev_private;
5249
5250 if (IS_SKYLAKE(dev)) {
5251 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5252
5253 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5254 dev_priv->max_cdclk_freq = 675000;
5255 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5256 dev_priv->max_cdclk_freq = 540000;
5257 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5258 dev_priv->max_cdclk_freq = 450000;
5259 else
5260 dev_priv->max_cdclk_freq = 337500;
5261 } else if (IS_BROADWELL(dev)) {
5262 /*
5263 * FIXME with extra cooling we can allow
5264 * 540 MHz for ULX and 675 Mhz for ULT.
5265 * How can we know if extra cooling is
5266 * available? PCI ID, VTB, something else?
5267 */
5268 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5269 dev_priv->max_cdclk_freq = 450000;
5270 else if (IS_BDW_ULX(dev))
5271 dev_priv->max_cdclk_freq = 450000;
5272 else if (IS_BDW_ULT(dev))
5273 dev_priv->max_cdclk_freq = 540000;
5274 else
5275 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005276 } else if (IS_CHERRYVIEW(dev)) {
5277 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005278 } else if (IS_VALLEYVIEW(dev)) {
5279 dev_priv->max_cdclk_freq = 400000;
5280 } else {
5281 /* otherwise assume cdclk is fixed */
5282 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5283 }
5284
Mika Kaholaadafdc62015-08-18 14:36:59 +03005285 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5286
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005287 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5288 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005289
5290 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5291 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005292}
5293
5294static void intel_update_cdclk(struct drm_device *dev)
5295{
5296 struct drm_i915_private *dev_priv = dev->dev_private;
5297
5298 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5299 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5300 dev_priv->cdclk_freq);
5301
5302 /*
5303 * Program the gmbus_freq based on the cdclk frequency.
5304 * BSpec erroneously claims we should aim for 4MHz, but
5305 * in fact 1MHz is the correct frequency.
5306 */
5307 if (IS_VALLEYVIEW(dev)) {
5308 /*
5309 * Program the gmbus_freq based on the cdclk frequency.
5310 * BSpec erroneously claims we should aim for 4MHz, but
5311 * in fact 1MHz is the correct frequency.
5312 */
5313 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5314 }
5315
5316 if (dev_priv->max_cdclk_freq == 0)
5317 intel_update_max_cdclk(dev);
5318}
5319
Damien Lespiau70d0c572015-06-04 18:21:29 +01005320static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305321{
5322 struct drm_i915_private *dev_priv = dev->dev_private;
5323 uint32_t divider;
5324 uint32_t ratio;
5325 uint32_t current_freq;
5326 int ret;
5327
5328 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5329 switch (frequency) {
5330 case 144000:
5331 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5332 ratio = BXT_DE_PLL_RATIO(60);
5333 break;
5334 case 288000:
5335 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5336 ratio = BXT_DE_PLL_RATIO(60);
5337 break;
5338 case 384000:
5339 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5340 ratio = BXT_DE_PLL_RATIO(60);
5341 break;
5342 case 576000:
5343 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5344 ratio = BXT_DE_PLL_RATIO(60);
5345 break;
5346 case 624000:
5347 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5348 ratio = BXT_DE_PLL_RATIO(65);
5349 break;
5350 case 19200:
5351 /*
5352 * Bypass frequency with DE PLL disabled. Init ratio, divider
5353 * to suppress GCC warning.
5354 */
5355 ratio = 0;
5356 divider = 0;
5357 break;
5358 default:
5359 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5360
5361 return;
5362 }
5363
5364 mutex_lock(&dev_priv->rps.hw_lock);
5365 /* Inform power controller of upcoming frequency change */
5366 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5367 0x80000000);
5368 mutex_unlock(&dev_priv->rps.hw_lock);
5369
5370 if (ret) {
5371 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5372 ret, frequency);
5373 return;
5374 }
5375
5376 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5377 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5378 current_freq = current_freq * 500 + 1000;
5379
5380 /*
5381 * DE PLL has to be disabled when
5382 * - setting to 19.2MHz (bypass, PLL isn't used)
5383 * - before setting to 624MHz (PLL needs toggling)
5384 * - before setting to any frequency from 624MHz (PLL needs toggling)
5385 */
5386 if (frequency == 19200 || frequency == 624000 ||
5387 current_freq == 624000) {
5388 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5389 /* Timeout 200us */
5390 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5391 1))
5392 DRM_ERROR("timout waiting for DE PLL unlock\n");
5393 }
5394
5395 if (frequency != 19200) {
5396 uint32_t val;
5397
5398 val = I915_READ(BXT_DE_PLL_CTL);
5399 val &= ~BXT_DE_PLL_RATIO_MASK;
5400 val |= ratio;
5401 I915_WRITE(BXT_DE_PLL_CTL, val);
5402
5403 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5404 /* Timeout 200us */
5405 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5406 DRM_ERROR("timeout waiting for DE PLL lock\n");
5407
5408 val = I915_READ(CDCLK_CTL);
5409 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5410 val |= divider;
5411 /*
5412 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5413 * enable otherwise.
5414 */
5415 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5416 if (frequency >= 500000)
5417 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5418
5419 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5420 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5421 val |= (frequency - 1000) / 500;
5422 I915_WRITE(CDCLK_CTL, val);
5423 }
5424
5425 mutex_lock(&dev_priv->rps.hw_lock);
5426 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5427 DIV_ROUND_UP(frequency, 25000));
5428 mutex_unlock(&dev_priv->rps.hw_lock);
5429
5430 if (ret) {
5431 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5432 ret, frequency);
5433 return;
5434 }
5435
Damien Lespiaua47871b2015-06-04 18:21:34 +01005436 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305437}
5438
5439void broxton_init_cdclk(struct drm_device *dev)
5440{
5441 struct drm_i915_private *dev_priv = dev->dev_private;
5442 uint32_t val;
5443
5444 /*
5445 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5446 * or else the reset will hang because there is no PCH to respond.
5447 * Move the handshake programming to initialization sequence.
5448 * Previously was left up to BIOS.
5449 */
5450 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5451 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5452 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5453
5454 /* Enable PG1 for cdclk */
5455 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5456
5457 /* check if cd clock is enabled */
5458 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5459 DRM_DEBUG_KMS("Display already initialized\n");
5460 return;
5461 }
5462
5463 /*
5464 * FIXME:
5465 * - The initial CDCLK needs to be read from VBT.
5466 * Need to make this change after VBT has changes for BXT.
5467 * - check if setting the max (or any) cdclk freq is really necessary
5468 * here, it belongs to modeset time
5469 */
5470 broxton_set_cdclk(dev, 624000);
5471
5472 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005473 POSTING_READ(DBUF_CTL);
5474
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305475 udelay(10);
5476
5477 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5478 DRM_ERROR("DBuf power enable timeout!\n");
5479}
5480
5481void broxton_uninit_cdclk(struct drm_device *dev)
5482{
5483 struct drm_i915_private *dev_priv = dev->dev_private;
5484
5485 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005486 POSTING_READ(DBUF_CTL);
5487
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305488 udelay(10);
5489
5490 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5491 DRM_ERROR("DBuf power disable timeout!\n");
5492
5493 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5494 broxton_set_cdclk(dev, 19200);
5495
5496 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5497}
5498
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005499static const struct skl_cdclk_entry {
5500 unsigned int freq;
5501 unsigned int vco;
5502} skl_cdclk_frequencies[] = {
5503 { .freq = 308570, .vco = 8640 },
5504 { .freq = 337500, .vco = 8100 },
5505 { .freq = 432000, .vco = 8640 },
5506 { .freq = 450000, .vco = 8100 },
5507 { .freq = 540000, .vco = 8100 },
5508 { .freq = 617140, .vco = 8640 },
5509 { .freq = 675000, .vco = 8100 },
5510};
5511
5512static unsigned int skl_cdclk_decimal(unsigned int freq)
5513{
5514 return (freq - 1000) / 500;
5515}
5516
5517static unsigned int skl_cdclk_get_vco(unsigned int freq)
5518{
5519 unsigned int i;
5520
5521 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5522 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5523
5524 if (e->freq == freq)
5525 return e->vco;
5526 }
5527
5528 return 8100;
5529}
5530
5531static void
5532skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5533{
5534 unsigned int min_freq;
5535 u32 val;
5536
5537 /* select the minimum CDCLK before enabling DPLL 0 */
5538 val = I915_READ(CDCLK_CTL);
5539 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5540 val |= CDCLK_FREQ_337_308;
5541
5542 if (required_vco == 8640)
5543 min_freq = 308570;
5544 else
5545 min_freq = 337500;
5546
5547 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5548
5549 I915_WRITE(CDCLK_CTL, val);
5550 POSTING_READ(CDCLK_CTL);
5551
5552 /*
5553 * We always enable DPLL0 with the lowest link rate possible, but still
5554 * taking into account the VCO required to operate the eDP panel at the
5555 * desired frequency. The usual DP link rates operate with a VCO of
5556 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5557 * The modeset code is responsible for the selection of the exact link
5558 * rate later on, with the constraint of choosing a frequency that
5559 * works with required_vco.
5560 */
5561 val = I915_READ(DPLL_CTRL1);
5562
5563 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5564 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5565 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5566 if (required_vco == 8640)
5567 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5568 SKL_DPLL0);
5569 else
5570 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5571 SKL_DPLL0);
5572
5573 I915_WRITE(DPLL_CTRL1, val);
5574 POSTING_READ(DPLL_CTRL1);
5575
5576 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5577
5578 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5579 DRM_ERROR("DPLL0 not locked\n");
5580}
5581
5582static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5583{
5584 int ret;
5585 u32 val;
5586
5587 /* inform PCU we want to change CDCLK */
5588 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5589 mutex_lock(&dev_priv->rps.hw_lock);
5590 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5591 mutex_unlock(&dev_priv->rps.hw_lock);
5592
5593 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5594}
5595
5596static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5597{
5598 unsigned int i;
5599
5600 for (i = 0; i < 15; i++) {
5601 if (skl_cdclk_pcu_ready(dev_priv))
5602 return true;
5603 udelay(10);
5604 }
5605
5606 return false;
5607}
5608
5609static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5610{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005611 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005612 u32 freq_select, pcu_ack;
5613
5614 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5615
5616 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5617 DRM_ERROR("failed to inform PCU about cdclk change\n");
5618 return;
5619 }
5620
5621 /* set CDCLK_CTL */
5622 switch(freq) {
5623 case 450000:
5624 case 432000:
5625 freq_select = CDCLK_FREQ_450_432;
5626 pcu_ack = 1;
5627 break;
5628 case 540000:
5629 freq_select = CDCLK_FREQ_540;
5630 pcu_ack = 2;
5631 break;
5632 case 308570:
5633 case 337500:
5634 default:
5635 freq_select = CDCLK_FREQ_337_308;
5636 pcu_ack = 0;
5637 break;
5638 case 617140:
5639 case 675000:
5640 freq_select = CDCLK_FREQ_675_617;
5641 pcu_ack = 3;
5642 break;
5643 }
5644
5645 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5646 POSTING_READ(CDCLK_CTL);
5647
5648 /* inform PCU of the change */
5649 mutex_lock(&dev_priv->rps.hw_lock);
5650 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5651 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005652
5653 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005654}
5655
5656void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5657{
5658 /* disable DBUF power */
5659 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5660 POSTING_READ(DBUF_CTL);
5661
5662 udelay(10);
5663
5664 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5665 DRM_ERROR("DBuf power disable timeout\n");
5666
5667 /* disable DPLL0 */
5668 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5669 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5670 DRM_ERROR("Couldn't disable DPLL0\n");
5671
5672 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5673}
5674
5675void skl_init_cdclk(struct drm_i915_private *dev_priv)
5676{
5677 u32 val;
5678 unsigned int required_vco;
5679
5680 /* enable PCH reset handshake */
5681 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5682 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5683
5684 /* enable PG1 and Misc I/O */
5685 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5686
Gary Wang39d9b852015-08-28 16:40:34 +08005687 /* DPLL0 not enabled (happens on early BIOS versions) */
5688 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5689 /* enable DPLL0 */
5690 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5691 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005692 }
5693
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005694 /* set CDCLK to the frequency the BIOS chose */
5695 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5696
5697 /* enable DBUF power */
5698 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5699 POSTING_READ(DBUF_CTL);
5700
5701 udelay(10);
5702
5703 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5704 DRM_ERROR("DBuf power enable timeout\n");
5705}
5706
Ville Syrjälädfcab172014-06-13 13:37:47 +03005707/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005708static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005709{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005710 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005711
Jesse Barnes586f49d2013-11-04 16:06:59 -08005712 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005713 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005714 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5715 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005716 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005717
Ville Syrjälädfcab172014-06-13 13:37:47 +03005718 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005719}
5720
5721/* Adjust CDclk dividers to allow high res or save power if possible */
5722static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5723{
5724 struct drm_i915_private *dev_priv = dev->dev_private;
5725 u32 val, cmd;
5726
Vandana Kannan164dfd22014-11-24 13:37:41 +05305727 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5728 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005729
Ville Syrjälädfcab172014-06-13 13:37:47 +03005730 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005731 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005732 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005733 cmd = 1;
5734 else
5735 cmd = 0;
5736
5737 mutex_lock(&dev_priv->rps.hw_lock);
5738 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5739 val &= ~DSPFREQGUAR_MASK;
5740 val |= (cmd << DSPFREQGUAR_SHIFT);
5741 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5742 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5743 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5744 50)) {
5745 DRM_ERROR("timed out waiting for CDclk change\n");
5746 }
5747 mutex_unlock(&dev_priv->rps.hw_lock);
5748
Ville Syrjälä54433e92015-05-26 20:42:31 +03005749 mutex_lock(&dev_priv->sb_lock);
5750
Ville Syrjälädfcab172014-06-13 13:37:47 +03005751 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005752 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005753
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005754 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005755
Jesse Barnes30a970c2013-11-04 13:48:12 -08005756 /* adjust cdclk divider */
5757 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005758 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005759 val |= divider;
5760 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005761
5762 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5763 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5764 50))
5765 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005766 }
5767
Jesse Barnes30a970c2013-11-04 13:48:12 -08005768 /* adjust self-refresh exit latency value */
5769 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5770 val &= ~0x7f;
5771
5772 /*
5773 * For high bandwidth configs, we set a higher latency in the bunit
5774 * so that the core display fetch happens in time to avoid underruns.
5775 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005776 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005777 val |= 4500 / 250; /* 4.5 usec */
5778 else
5779 val |= 3000 / 250; /* 3.0 usec */
5780 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005781
Ville Syrjäläa5805162015-05-26 20:42:30 +03005782 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005783
Ville Syrjäläb6283052015-06-03 15:45:07 +03005784 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005785}
5786
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005787static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5788{
5789 struct drm_i915_private *dev_priv = dev->dev_private;
5790 u32 val, cmd;
5791
Vandana Kannan164dfd22014-11-24 13:37:41 +05305792 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5793 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005794
5795 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005796 case 333333:
5797 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005798 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005799 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005800 break;
5801 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005802 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005803 return;
5804 }
5805
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005806 /*
5807 * Specs are full of misinformation, but testing on actual
5808 * hardware has shown that we just need to write the desired
5809 * CCK divider into the Punit register.
5810 */
5811 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5812
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005813 mutex_lock(&dev_priv->rps.hw_lock);
5814 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5815 val &= ~DSPFREQGUAR_MASK_CHV;
5816 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5817 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5818 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5819 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5820 50)) {
5821 DRM_ERROR("timed out waiting for CDclk change\n");
5822 }
5823 mutex_unlock(&dev_priv->rps.hw_lock);
5824
Ville Syrjäläb6283052015-06-03 15:45:07 +03005825 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005826}
5827
Jesse Barnes30a970c2013-11-04 13:48:12 -08005828static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5829 int max_pixclk)
5830{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005831 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005832 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005833
Jesse Barnes30a970c2013-11-04 13:48:12 -08005834 /*
5835 * Really only a few cases to deal with, as only 4 CDclks are supported:
5836 * 200MHz
5837 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005838 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005839 * 400MHz (VLV only)
5840 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5841 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005842 *
5843 * We seem to get an unstable or solid color picture at 200MHz.
5844 * Not sure what's wrong. For now use 200MHz only when all pipes
5845 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005846 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005847 if (!IS_CHERRYVIEW(dev_priv) &&
5848 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005849 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005850 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005851 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005852 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005853 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005854 else
5855 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856}
5857
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305858static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5859 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005860{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305861 /*
5862 * FIXME:
5863 * - remove the guardband, it's not needed on BXT
5864 * - set 19.2MHz bypass frequency if there are no active pipes
5865 */
5866 if (max_pixclk > 576000*9/10)
5867 return 624000;
5868 else if (max_pixclk > 384000*9/10)
5869 return 576000;
5870 else if (max_pixclk > 288000*9/10)
5871 return 384000;
5872 else if (max_pixclk > 144000*9/10)
5873 return 288000;
5874 else
5875 return 144000;
5876}
5877
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005878/* Compute the max pixel clock for new configuration. Uses atomic state if
5879 * that's non-NULL, look at current state otherwise. */
5880static int intel_mode_max_pixclk(struct drm_device *dev,
5881 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005882{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005883 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005884 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005885 int max_pixclk = 0;
5886
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005887 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005888 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005889 if (IS_ERR(crtc_state))
5890 return PTR_ERR(crtc_state);
5891
5892 if (!crtc_state->base.enable)
5893 continue;
5894
5895 max_pixclk = max(max_pixclk,
5896 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005897 }
5898
5899 return max_pixclk;
5900}
5901
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005902static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005903{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005904 struct drm_device *dev = state->dev;
5905 struct drm_i915_private *dev_priv = dev->dev_private;
5906 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005907
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005908 if (max_pixclk < 0)
5909 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005910
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005911 to_intel_atomic_state(state)->cdclk =
5912 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305913
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005914 return 0;
5915}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005916
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005917static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5918{
5919 struct drm_device *dev = state->dev;
5920 struct drm_i915_private *dev_priv = dev->dev_private;
5921 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005922
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005923 if (max_pixclk < 0)
5924 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005925
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005926 to_intel_atomic_state(state)->cdclk =
5927 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005928
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005929 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005930}
5931
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005932static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5933{
5934 unsigned int credits, default_credits;
5935
5936 if (IS_CHERRYVIEW(dev_priv))
5937 default_credits = PFI_CREDIT(12);
5938 else
5939 default_credits = PFI_CREDIT(8);
5940
Vandana Kannan164dfd22014-11-24 13:37:41 +05305941 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005942 /* CHV suggested value is 31 or 63 */
5943 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005944 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005945 else
5946 credits = PFI_CREDIT(15);
5947 } else {
5948 credits = default_credits;
5949 }
5950
5951 /*
5952 * WA - write default credits before re-programming
5953 * FIXME: should we also set the resend bit here?
5954 */
5955 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5956 default_credits);
5957
5958 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5959 credits | PFI_CREDIT_RESEND);
5960
5961 /*
5962 * FIXME is this guaranteed to clear
5963 * immediately or should we poll for it?
5964 */
5965 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5966}
5967
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005968static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005969{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005970 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005971 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005972 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005973
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005974 /*
5975 * FIXME: We can end up here with all power domains off, yet
5976 * with a CDCLK frequency other than the minimum. To account
5977 * for this take the PIPE-A power domain, which covers the HW
5978 * blocks needed for the following programming. This can be
5979 * removed once it's guaranteed that we get here either with
5980 * the minimum CDCLK set, or the required power domains
5981 * enabled.
5982 */
5983 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005984
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005985 if (IS_CHERRYVIEW(dev))
5986 cherryview_set_cdclk(dev, req_cdclk);
5987 else
5988 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005989
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005990 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02005991
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005992 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005993}
5994
Jesse Barnes89b667f2013-04-18 14:51:36 -07005995static void valleyview_crtc_enable(struct drm_crtc *crtc)
5996{
5997 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005998 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005999 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6000 struct intel_encoder *encoder;
6001 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006002 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006003
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006004 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006005 return;
6006
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006007 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306008
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006009 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306010 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006011
6012 intel_set_pipe_timings(intel_crtc);
6013
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006014 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6015 struct drm_i915_private *dev_priv = dev->dev_private;
6016
6017 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6018 I915_WRITE(CHV_CANVAS(pipe), 0);
6019 }
6020
Daniel Vetter5b18e572014-04-24 23:55:06 +02006021 i9xx_set_pipeconf(intel_crtc);
6022
Jesse Barnes89b667f2013-04-18 14:51:36 -07006023 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006024
Daniel Vettera72e4c92014-09-30 10:56:47 +02006025 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006026
Jesse Barnes89b667f2013-04-18 14:51:36 -07006027 for_each_encoder_on_crtc(dev, crtc, encoder)
6028 if (encoder->pre_pll_enable)
6029 encoder->pre_pll_enable(encoder);
6030
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006031 if (!is_dsi) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006032 if (IS_CHERRYVIEW(dev)) {
6033 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006034 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006035 } else {
6036 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006037 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006038 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006039 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006040
6041 for_each_encoder_on_crtc(dev, crtc, encoder)
6042 if (encoder->pre_enable)
6043 encoder->pre_enable(encoder);
6044
Jesse Barnes2dd24552013-04-25 12:55:01 -07006045 i9xx_pfit_enable(intel_crtc);
6046
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006047 intel_crtc_load_lut(crtc);
6048
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006049 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006050
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006051 assert_vblank_disabled(crtc);
6052 drm_crtc_vblank_on(crtc);
6053
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006054 for_each_encoder_on_crtc(dev, crtc, encoder)
6055 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006056}
6057
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006058static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6059{
6060 struct drm_device *dev = crtc->base.dev;
6061 struct drm_i915_private *dev_priv = dev->dev_private;
6062
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006063 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6064 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006065}
6066
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006067static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006068{
6069 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006070 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006072 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006073 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006074
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006075 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006076 return;
6077
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006078 i9xx_set_pll_dividers(intel_crtc);
6079
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006080 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306081 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006082
6083 intel_set_pipe_timings(intel_crtc);
6084
Daniel Vetter5b18e572014-04-24 23:55:06 +02006085 i9xx_set_pipeconf(intel_crtc);
6086
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006087 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006088
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006089 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006090 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006091
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006092 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006093 if (encoder->pre_enable)
6094 encoder->pre_enable(encoder);
6095
Daniel Vetterf6736a12013-06-05 13:34:30 +02006096 i9xx_enable_pll(intel_crtc);
6097
Jesse Barnes2dd24552013-04-25 12:55:01 -07006098 i9xx_pfit_enable(intel_crtc);
6099
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006100 intel_crtc_load_lut(crtc);
6101
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006102 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006103 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006104
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006105 assert_vblank_disabled(crtc);
6106 drm_crtc_vblank_on(crtc);
6107
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006108 for_each_encoder_on_crtc(dev, crtc, encoder)
6109 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006110}
6111
Daniel Vetter87476d62013-04-11 16:29:06 +02006112static void i9xx_pfit_disable(struct intel_crtc *crtc)
6113{
6114 struct drm_device *dev = crtc->base.dev;
6115 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006116
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006117 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006118 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006119
6120 assert_pipe_disabled(dev_priv, crtc->pipe);
6121
Daniel Vetter328d8e82013-05-08 10:36:31 +02006122 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6123 I915_READ(PFIT_CONTROL));
6124 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006125}
6126
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006127static void i9xx_crtc_disable(struct drm_crtc *crtc)
6128{
6129 struct drm_device *dev = crtc->dev;
6130 struct drm_i915_private *dev_priv = dev->dev_private;
6131 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006132 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006133 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006134
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006135 /*
6136 * On gen2 planes are double buffered but the pipe isn't, so we must
6137 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006138 * We also need to wait on all gmch platforms because of the
6139 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006140 */
Imre Deak564ed192014-06-13 14:54:21 +03006141 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006142
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006143 for_each_encoder_on_crtc(dev, crtc, encoder)
6144 encoder->disable(encoder);
6145
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006146 drm_crtc_vblank_off(crtc);
6147 assert_vblank_disabled(crtc);
6148
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006149 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006150
Daniel Vetter87476d62013-04-11 16:29:06 +02006151 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006152
Jesse Barnes89b667f2013-04-18 14:51:36 -07006153 for_each_encoder_on_crtc(dev, crtc, encoder)
6154 if (encoder->post_disable)
6155 encoder->post_disable(encoder);
6156
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006157 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006158 if (IS_CHERRYVIEW(dev))
6159 chv_disable_pll(dev_priv, pipe);
6160 else if (IS_VALLEYVIEW(dev))
6161 vlv_disable_pll(dev_priv, pipe);
6162 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006163 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006164 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006165
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006166 for_each_encoder_on_crtc(dev, crtc, encoder)
6167 if (encoder->post_pll_disable)
6168 encoder->post_pll_disable(encoder);
6169
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006170 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006171 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02006172
6173 intel_crtc->active = false;
6174 intel_update_watermarks(crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006175}
6176
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006177static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006178{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006180 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006181 enum intel_display_power_domain domain;
6182 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006183
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006184 if (!intel_crtc->active)
6185 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006186
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006187 if (to_intel_plane_state(crtc->primary->state)->visible) {
6188 intel_crtc_wait_for_pending_flips(crtc);
6189 intel_pre_disable_primary(crtc);
6190 }
6191
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006192 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006193 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006194 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006195
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006196 domains = intel_crtc->enabled_power_domains;
6197 for_each_power_domain(domain, domains)
6198 intel_display_power_put(dev_priv, domain);
6199 intel_crtc->enabled_power_domains = 0;
6200}
6201
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006202/*
6203 * turn all crtc's off, but do not adjust state
6204 * This has to be paired with a call to intel_modeset_setup_hw_state.
6205 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006206int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006207{
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006208 struct drm_mode_config *config = &dev->mode_config;
6209 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6210 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006211 struct drm_crtc *crtc;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006212 unsigned crtc_mask = 0;
6213 int ret = 0;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006214
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006215 if (WARN_ON(!ctx))
6216 return 0;
6217
6218 lockdep_assert_held(&ctx->ww_ctx);
6219 state = drm_atomic_state_alloc(dev);
6220 if (WARN_ON(!state))
6221 return -ENOMEM;
6222
6223 state->acquire_ctx = ctx;
6224 state->allow_modeset = true;
6225
6226 for_each_crtc(dev, crtc) {
6227 struct drm_crtc_state *crtc_state =
6228 drm_atomic_get_crtc_state(state, crtc);
6229
6230 ret = PTR_ERR_OR_ZERO(crtc_state);
6231 if (ret)
6232 goto free;
6233
6234 if (!crtc_state->active)
6235 continue;
6236
6237 crtc_state->active = false;
6238 crtc_mask |= 1 << drm_crtc_index(crtc);
6239 }
6240
6241 if (crtc_mask) {
Maarten Lankhorst74c090b2015-07-13 16:30:30 +02006242 ret = drm_atomic_commit(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006243
6244 if (!ret) {
6245 for_each_crtc(dev, crtc)
6246 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6247 crtc->state->active = true;
6248
6249 return ret;
6250 }
6251 }
6252
6253free:
6254 if (ret)
6255 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6256 drm_atomic_state_free(state);
6257 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006258}
6259
Chris Wilsonea5b2132010-08-04 13:50:23 +01006260void intel_encoder_destroy(struct drm_encoder *encoder)
6261{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006262 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006263
Chris Wilsonea5b2132010-08-04 13:50:23 +01006264 drm_encoder_cleanup(encoder);
6265 kfree(intel_encoder);
6266}
6267
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006268/* Cross check the actual hw state with our own modeset state tracking (and it's
6269 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006270static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006271{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006272 struct drm_crtc *crtc = connector->base.state->crtc;
6273
6274 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6275 connector->base.base.id,
6276 connector->base.name);
6277
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006278 if (connector->get_hw_state(connector)) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006279 struct drm_encoder *encoder = &connector->encoder->base;
6280 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006281
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006282 I915_STATE_WARN(!crtc,
6283 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006284
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006285 if (!crtc)
6286 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006287
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006288 I915_STATE_WARN(!crtc->state->active,
6289 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006290
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006291 if (!encoder)
6292 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006293
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006294 I915_STATE_WARN(conn_state->best_encoder != encoder,
6295 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006296
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006297 I915_STATE_WARN(conn_state->crtc != encoder->crtc,
6298 "attached encoder crtc differs from connector crtc\n");
6299 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006300 I915_STATE_WARN(crtc && crtc->state->active,
6301 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006302 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6303 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006304 }
6305}
6306
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006307int intel_connector_init(struct intel_connector *connector)
6308{
6309 struct drm_connector_state *connector_state;
6310
6311 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6312 if (!connector_state)
6313 return -ENOMEM;
6314
6315 connector->base.state = connector_state;
6316 return 0;
6317}
6318
6319struct intel_connector *intel_connector_alloc(void)
6320{
6321 struct intel_connector *connector;
6322
6323 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6324 if (!connector)
6325 return NULL;
6326
6327 if (intel_connector_init(connector) < 0) {
6328 kfree(connector);
6329 return NULL;
6330 }
6331
6332 return connector;
6333}
6334
Daniel Vetterf0947c32012-07-02 13:10:34 +02006335/* Simple connector->get_hw_state implementation for encoders that support only
6336 * one connector and no cloning and hence the encoder state determines the state
6337 * of the connector. */
6338bool intel_connector_get_hw_state(struct intel_connector *connector)
6339{
Daniel Vetter24929352012-07-02 20:28:59 +02006340 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006341 struct intel_encoder *encoder = connector->encoder;
6342
6343 return encoder->get_hw_state(encoder, &pipe);
6344}
6345
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006346static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006347{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006348 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6349 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006350
6351 return 0;
6352}
6353
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006354static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006355 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006356{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006357 struct drm_atomic_state *state = pipe_config->base.state;
6358 struct intel_crtc *other_crtc;
6359 struct intel_crtc_state *other_crtc_state;
6360
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006361 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6362 pipe_name(pipe), pipe_config->fdi_lanes);
6363 if (pipe_config->fdi_lanes > 4) {
6364 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6365 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006366 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006367 }
6368
Paulo Zanonibafb6552013-11-02 21:07:44 -07006369 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006370 if (pipe_config->fdi_lanes > 2) {
6371 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6372 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006373 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006374 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006375 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006376 }
6377 }
6378
6379 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006380 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006381
6382 /* Ivybridge 3 pipe is really complicated */
6383 switch (pipe) {
6384 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006385 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006386 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006387 if (pipe_config->fdi_lanes <= 2)
6388 return 0;
6389
6390 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6391 other_crtc_state =
6392 intel_atomic_get_crtc_state(state, other_crtc);
6393 if (IS_ERR(other_crtc_state))
6394 return PTR_ERR(other_crtc_state);
6395
6396 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006397 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6398 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006399 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006400 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006401 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006402 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006403 if (pipe_config->fdi_lanes > 2) {
6404 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6405 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006406 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006407 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006408
6409 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6410 other_crtc_state =
6411 intel_atomic_get_crtc_state(state, other_crtc);
6412 if (IS_ERR(other_crtc_state))
6413 return PTR_ERR(other_crtc_state);
6414
6415 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006416 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006417 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006418 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006419 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006420 default:
6421 BUG();
6422 }
6423}
6424
Daniel Vettere29c22c2013-02-21 00:00:16 +01006425#define RETRY 1
6426static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006427 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006428{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006429 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006430 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006431 int lane, link_bw, fdi_dotclock, ret;
6432 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006433
Daniel Vettere29c22c2013-02-21 00:00:16 +01006434retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006435 /* FDI is a binary signal running at ~2.7GHz, encoding
6436 * each output octet as 10 bits. The actual frequency
6437 * is stored as a divider into a 100MHz clock, and the
6438 * mode pixel clock is stored in units of 1KHz.
6439 * Hence the bw of each lane in terms of the mode signal
6440 * is:
6441 */
6442 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6443
Damien Lespiau241bfc32013-09-25 16:45:37 +01006444 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006445
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006446 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006447 pipe_config->pipe_bpp);
6448
6449 pipe_config->fdi_lanes = lane;
6450
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006451 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006452 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006453
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006454 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6455 intel_crtc->pipe, pipe_config);
6456 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006457 pipe_config->pipe_bpp -= 2*3;
6458 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6459 pipe_config->pipe_bpp);
6460 needs_recompute = true;
6461 pipe_config->bw_constrained = true;
6462
6463 goto retry;
6464 }
6465
6466 if (needs_recompute)
6467 return RETRY;
6468
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006469 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006470}
6471
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006472static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6473 struct intel_crtc_state *pipe_config)
6474{
6475 if (pipe_config->pipe_bpp > 24)
6476 return false;
6477
6478 /* HSW can handle pixel rate up to cdclk? */
6479 if (IS_HASWELL(dev_priv->dev))
6480 return true;
6481
6482 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006483 * We compare against max which means we must take
6484 * the increased cdclk requirement into account when
6485 * calculating the new cdclk.
6486 *
6487 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006488 */
6489 return ilk_pipe_pixel_rate(pipe_config) <=
6490 dev_priv->max_cdclk_freq * 95 / 100;
6491}
6492
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006493static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006494 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006495{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006496 struct drm_device *dev = crtc->base.dev;
6497 struct drm_i915_private *dev_priv = dev->dev_private;
6498
Jani Nikulad330a952014-01-21 11:24:25 +02006499 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006500 hsw_crtc_supports_ips(crtc) &&
6501 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006502}
6503
Daniel Vettera43f6e02013-06-07 23:10:32 +02006504static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006505 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006506{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006507 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006508 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006509 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006510
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006511 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006512 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006513 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006514
6515 /*
6516 * Enable pixel doubling when the dot clock
6517 * is > 90% of the (display) core speed.
6518 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006519 * GDG double wide on either pipe,
6520 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006521 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006522 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006523 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006524 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006525 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006526 }
6527
Damien Lespiau241bfc32013-09-25 16:45:37 +01006528 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006529 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006530 }
Chris Wilson89749352010-09-12 18:25:19 +01006531
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006532 /*
6533 * Pipe horizontal size must be even in:
6534 * - DVO ganged mode
6535 * - LVDS dual channel mode
6536 * - Double wide pipe
6537 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006538 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006539 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6540 pipe_config->pipe_src_w &= ~1;
6541
Damien Lespiau8693a822013-05-03 18:48:11 +01006542 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6543 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006544 */
6545 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6546 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006547 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006548
Damien Lespiauf5adf942013-06-24 18:29:34 +01006549 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006550 hsw_compute_ips_config(crtc, pipe_config);
6551
Daniel Vetter877d48d2013-04-19 11:24:43 +02006552 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006553 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006554
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006555 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006556}
6557
Ville Syrjälä1652d192015-03-31 14:12:01 +03006558static int skylake_get_display_clock_speed(struct drm_device *dev)
6559{
6560 struct drm_i915_private *dev_priv = to_i915(dev);
6561 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6562 uint32_t cdctl = I915_READ(CDCLK_CTL);
6563 uint32_t linkrate;
6564
Damien Lespiau414355a2015-06-04 18:21:31 +01006565 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006566 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006567
6568 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6569 return 540000;
6570
6571 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006572 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006573
Damien Lespiau71cd8422015-04-30 16:39:17 +01006574 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6575 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006576 /* vco 8640 */
6577 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6578 case CDCLK_FREQ_450_432:
6579 return 432000;
6580 case CDCLK_FREQ_337_308:
6581 return 308570;
6582 case CDCLK_FREQ_675_617:
6583 return 617140;
6584 default:
6585 WARN(1, "Unknown cd freq selection\n");
6586 }
6587 } else {
6588 /* vco 8100 */
6589 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6590 case CDCLK_FREQ_450_432:
6591 return 450000;
6592 case CDCLK_FREQ_337_308:
6593 return 337500;
6594 case CDCLK_FREQ_675_617:
6595 return 675000;
6596 default:
6597 WARN(1, "Unknown cd freq selection\n");
6598 }
6599 }
6600
6601 /* error case, do as if DPLL0 isn't enabled */
6602 return 24000;
6603}
6604
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006605static int broxton_get_display_clock_speed(struct drm_device *dev)
6606{
6607 struct drm_i915_private *dev_priv = to_i915(dev);
6608 uint32_t cdctl = I915_READ(CDCLK_CTL);
6609 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6610 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6611 int cdclk;
6612
6613 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6614 return 19200;
6615
6616 cdclk = 19200 * pll_ratio / 2;
6617
6618 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6619 case BXT_CDCLK_CD2X_DIV_SEL_1:
6620 return cdclk; /* 576MHz or 624MHz */
6621 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6622 return cdclk * 2 / 3; /* 384MHz */
6623 case BXT_CDCLK_CD2X_DIV_SEL_2:
6624 return cdclk / 2; /* 288MHz */
6625 case BXT_CDCLK_CD2X_DIV_SEL_4:
6626 return cdclk / 4; /* 144MHz */
6627 }
6628
6629 /* error case, do as if DE PLL isn't enabled */
6630 return 19200;
6631}
6632
Ville Syrjälä1652d192015-03-31 14:12:01 +03006633static int broadwell_get_display_clock_speed(struct drm_device *dev)
6634{
6635 struct drm_i915_private *dev_priv = dev->dev_private;
6636 uint32_t lcpll = I915_READ(LCPLL_CTL);
6637 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6638
6639 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6640 return 800000;
6641 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6642 return 450000;
6643 else if (freq == LCPLL_CLK_FREQ_450)
6644 return 450000;
6645 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6646 return 540000;
6647 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6648 return 337500;
6649 else
6650 return 675000;
6651}
6652
6653static int haswell_get_display_clock_speed(struct drm_device *dev)
6654{
6655 struct drm_i915_private *dev_priv = dev->dev_private;
6656 uint32_t lcpll = I915_READ(LCPLL_CTL);
6657 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6658
6659 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6660 return 800000;
6661 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6662 return 450000;
6663 else if (freq == LCPLL_CLK_FREQ_450)
6664 return 450000;
6665 else if (IS_HSW_ULT(dev))
6666 return 337500;
6667 else
6668 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006669}
6670
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006671static int valleyview_get_display_clock_speed(struct drm_device *dev)
6672{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006673 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006674 u32 val;
6675 int divider;
6676
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006677 if (dev_priv->hpll_freq == 0)
6678 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6679
Ville Syrjäläa5805162015-05-26 20:42:30 +03006680 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006681 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006682 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006683
6684 divider = val & DISPLAY_FREQUENCY_VALUES;
6685
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006686 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6687 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6688 "cdclk change in progress\n");
6689
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006690 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006691}
6692
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006693static int ilk_get_display_clock_speed(struct drm_device *dev)
6694{
6695 return 450000;
6696}
6697
Jesse Barnese70236a2009-09-21 10:42:27 -07006698static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006699{
Jesse Barnese70236a2009-09-21 10:42:27 -07006700 return 400000;
6701}
Jesse Barnes79e53942008-11-07 14:24:08 -08006702
Jesse Barnese70236a2009-09-21 10:42:27 -07006703static int i915_get_display_clock_speed(struct drm_device *dev)
6704{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006705 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006706}
Jesse Barnes79e53942008-11-07 14:24:08 -08006707
Jesse Barnese70236a2009-09-21 10:42:27 -07006708static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6709{
6710 return 200000;
6711}
Jesse Barnes79e53942008-11-07 14:24:08 -08006712
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006713static int pnv_get_display_clock_speed(struct drm_device *dev)
6714{
6715 u16 gcfgc = 0;
6716
6717 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6718
6719 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6720 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006721 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006722 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006723 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006724 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006725 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006726 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6727 return 200000;
6728 default:
6729 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6730 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006731 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006732 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006733 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006734 }
6735}
6736
Jesse Barnese70236a2009-09-21 10:42:27 -07006737static int i915gm_get_display_clock_speed(struct drm_device *dev)
6738{
6739 u16 gcfgc = 0;
6740
6741 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6742
6743 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006744 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006745 else {
6746 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6747 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006748 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006749 default:
6750 case GC_DISPLAY_CLOCK_190_200_MHZ:
6751 return 190000;
6752 }
6753 }
6754}
Jesse Barnes79e53942008-11-07 14:24:08 -08006755
Jesse Barnese70236a2009-09-21 10:42:27 -07006756static int i865_get_display_clock_speed(struct drm_device *dev)
6757{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006758 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006759}
6760
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006761static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006762{
6763 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006764
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006765 /*
6766 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6767 * encoding is different :(
6768 * FIXME is this the right way to detect 852GM/852GMV?
6769 */
6770 if (dev->pdev->revision == 0x1)
6771 return 133333;
6772
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006773 pci_bus_read_config_word(dev->pdev->bus,
6774 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6775
Jesse Barnese70236a2009-09-21 10:42:27 -07006776 /* Assume that the hardware is in the high speed state. This
6777 * should be the default.
6778 */
6779 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6780 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006781 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006782 case GC_CLOCK_100_200:
6783 return 200000;
6784 case GC_CLOCK_166_250:
6785 return 250000;
6786 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006787 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006788 case GC_CLOCK_133_266:
6789 case GC_CLOCK_133_266_2:
6790 case GC_CLOCK_166_266:
6791 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006792 }
6793
6794 /* Shouldn't happen */
6795 return 0;
6796}
6797
6798static int i830_get_display_clock_speed(struct drm_device *dev)
6799{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006800 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006801}
6802
Ville Syrjälä34edce22015-05-22 11:22:33 +03006803static unsigned int intel_hpll_vco(struct drm_device *dev)
6804{
6805 struct drm_i915_private *dev_priv = dev->dev_private;
6806 static const unsigned int blb_vco[8] = {
6807 [0] = 3200000,
6808 [1] = 4000000,
6809 [2] = 5333333,
6810 [3] = 4800000,
6811 [4] = 6400000,
6812 };
6813 static const unsigned int pnv_vco[8] = {
6814 [0] = 3200000,
6815 [1] = 4000000,
6816 [2] = 5333333,
6817 [3] = 4800000,
6818 [4] = 2666667,
6819 };
6820 static const unsigned int cl_vco[8] = {
6821 [0] = 3200000,
6822 [1] = 4000000,
6823 [2] = 5333333,
6824 [3] = 6400000,
6825 [4] = 3333333,
6826 [5] = 3566667,
6827 [6] = 4266667,
6828 };
6829 static const unsigned int elk_vco[8] = {
6830 [0] = 3200000,
6831 [1] = 4000000,
6832 [2] = 5333333,
6833 [3] = 4800000,
6834 };
6835 static const unsigned int ctg_vco[8] = {
6836 [0] = 3200000,
6837 [1] = 4000000,
6838 [2] = 5333333,
6839 [3] = 6400000,
6840 [4] = 2666667,
6841 [5] = 4266667,
6842 };
6843 const unsigned int *vco_table;
6844 unsigned int vco;
6845 uint8_t tmp = 0;
6846
6847 /* FIXME other chipsets? */
6848 if (IS_GM45(dev))
6849 vco_table = ctg_vco;
6850 else if (IS_G4X(dev))
6851 vco_table = elk_vco;
6852 else if (IS_CRESTLINE(dev))
6853 vco_table = cl_vco;
6854 else if (IS_PINEVIEW(dev))
6855 vco_table = pnv_vco;
6856 else if (IS_G33(dev))
6857 vco_table = blb_vco;
6858 else
6859 return 0;
6860
6861 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6862
6863 vco = vco_table[tmp & 0x7];
6864 if (vco == 0)
6865 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6866 else
6867 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6868
6869 return vco;
6870}
6871
6872static int gm45_get_display_clock_speed(struct drm_device *dev)
6873{
6874 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6875 uint16_t tmp = 0;
6876
6877 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6878
6879 cdclk_sel = (tmp >> 12) & 0x1;
6880
6881 switch (vco) {
6882 case 2666667:
6883 case 4000000:
6884 case 5333333:
6885 return cdclk_sel ? 333333 : 222222;
6886 case 3200000:
6887 return cdclk_sel ? 320000 : 228571;
6888 default:
6889 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6890 return 222222;
6891 }
6892}
6893
6894static int i965gm_get_display_clock_speed(struct drm_device *dev)
6895{
6896 static const uint8_t div_3200[] = { 16, 10, 8 };
6897 static const uint8_t div_4000[] = { 20, 12, 10 };
6898 static const uint8_t div_5333[] = { 24, 16, 14 };
6899 const uint8_t *div_table;
6900 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6901 uint16_t tmp = 0;
6902
6903 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6904
6905 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6906
6907 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6908 goto fail;
6909
6910 switch (vco) {
6911 case 3200000:
6912 div_table = div_3200;
6913 break;
6914 case 4000000:
6915 div_table = div_4000;
6916 break;
6917 case 5333333:
6918 div_table = div_5333;
6919 break;
6920 default:
6921 goto fail;
6922 }
6923
6924 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6925
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006926fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006927 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6928 return 200000;
6929}
6930
6931static int g33_get_display_clock_speed(struct drm_device *dev)
6932{
6933 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6934 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6935 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6936 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6937 const uint8_t *div_table;
6938 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6939 uint16_t tmp = 0;
6940
6941 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6942
6943 cdclk_sel = (tmp >> 4) & 0x7;
6944
6945 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6946 goto fail;
6947
6948 switch (vco) {
6949 case 3200000:
6950 div_table = div_3200;
6951 break;
6952 case 4000000:
6953 div_table = div_4000;
6954 break;
6955 case 4800000:
6956 div_table = div_4800;
6957 break;
6958 case 5333333:
6959 div_table = div_5333;
6960 break;
6961 default:
6962 goto fail;
6963 }
6964
6965 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6966
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006967fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006968 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6969 return 190476;
6970}
6971
Zhenyu Wang2c072452009-06-05 15:38:42 +08006972static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006973intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006974{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006975 while (*num > DATA_LINK_M_N_MASK ||
6976 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006977 *num >>= 1;
6978 *den >>= 1;
6979 }
6980}
6981
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006982static void compute_m_n(unsigned int m, unsigned int n,
6983 uint32_t *ret_m, uint32_t *ret_n)
6984{
6985 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6986 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6987 intel_reduce_m_n_ratio(ret_m, ret_n);
6988}
6989
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006990void
6991intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6992 int pixel_clock, int link_clock,
6993 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006994{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006995 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006996
6997 compute_m_n(bits_per_pixel * pixel_clock,
6998 link_clock * nlanes * 8,
6999 &m_n->gmch_m, &m_n->gmch_n);
7000
7001 compute_m_n(pixel_clock, link_clock,
7002 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007003}
7004
Chris Wilsona7615032011-01-12 17:04:08 +00007005static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7006{
Jani Nikulad330a952014-01-21 11:24:25 +02007007 if (i915.panel_use_ssc >= 0)
7008 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007009 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007010 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007011}
7012
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007013static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7014 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007015{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007016 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007017 struct drm_i915_private *dev_priv = dev->dev_private;
7018 int refclk;
7019
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007020 WARN_ON(!crtc_state->base.state);
7021
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007022 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007023 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007024 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007025 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007026 refclk = dev_priv->vbt.lvds_ssc_freq;
7027 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007028 } else if (!IS_GEN2(dev)) {
7029 refclk = 96000;
7030 } else {
7031 refclk = 48000;
7032 }
7033
7034 return refclk;
7035}
7036
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007037static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007038{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007039 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007040}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007041
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007042static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7043{
7044 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007045}
7046
Daniel Vetterf47709a2013-03-28 10:42:02 +01007047static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007048 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007049 intel_clock_t *reduced_clock)
7050{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007051 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007052 u32 fp, fp2 = 0;
7053
7054 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007055 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007056 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007057 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007058 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007059 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007060 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007061 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007062 }
7063
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007064 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007065
Daniel Vetterf47709a2013-03-28 10:42:02 +01007066 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007067 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007068 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007069 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007070 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007071 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007072 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007073 }
7074}
7075
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007076static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7077 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007078{
7079 u32 reg_val;
7080
7081 /*
7082 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7083 * and set it to a reasonable value instead.
7084 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007085 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007086 reg_val &= 0xffffff00;
7087 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007088 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007089
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007090 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007091 reg_val &= 0x8cffffff;
7092 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007093 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007094
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007095 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007096 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007097 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007098
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007099 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007100 reg_val &= 0x00ffffff;
7101 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007102 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007103}
7104
Daniel Vetterb5518422013-05-03 11:49:48 +02007105static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7106 struct intel_link_m_n *m_n)
7107{
7108 struct drm_device *dev = crtc->base.dev;
7109 struct drm_i915_private *dev_priv = dev->dev_private;
7110 int pipe = crtc->pipe;
7111
Daniel Vettere3b95f12013-05-03 11:49:49 +02007112 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7113 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7114 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7115 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007116}
7117
7118static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007119 struct intel_link_m_n *m_n,
7120 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007121{
7122 struct drm_device *dev = crtc->base.dev;
7123 struct drm_i915_private *dev_priv = dev->dev_private;
7124 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007125 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007126
7127 if (INTEL_INFO(dev)->gen >= 5) {
7128 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7129 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7130 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7131 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007132 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7133 * for gen < 8) and if DRRS is supported (to make sure the
7134 * registers are not unnecessarily accessed).
7135 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307136 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007137 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007138 I915_WRITE(PIPE_DATA_M2(transcoder),
7139 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7140 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7141 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7142 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7143 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007144 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007145 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7146 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7147 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7148 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007149 }
7150}
7151
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307152void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007153{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307154 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7155
7156 if (m_n == M1_N1) {
7157 dp_m_n = &crtc->config->dp_m_n;
7158 dp_m2_n2 = &crtc->config->dp_m2_n2;
7159 } else if (m_n == M2_N2) {
7160
7161 /*
7162 * M2_N2 registers are not supported. Hence m2_n2 divider value
7163 * needs to be programmed into M1_N1.
7164 */
7165 dp_m_n = &crtc->config->dp_m2_n2;
7166 } else {
7167 DRM_ERROR("Unsupported divider value\n");
7168 return;
7169 }
7170
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007171 if (crtc->config->has_pch_encoder)
7172 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007173 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307174 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007175}
7176
Daniel Vetter251ac862015-06-18 10:30:24 +02007177static void vlv_compute_dpll(struct intel_crtc *crtc,
7178 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007179{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007180 u32 dpll, dpll_md;
7181
7182 /*
7183 * Enable DPIO clock input. We should never disable the reference
7184 * clock for pipe B, since VGA hotplug / manual detection depends
7185 * on it.
7186 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007187 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7188 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007189 /* We should never disable this, set it here for state tracking */
7190 if (crtc->pipe == PIPE_B)
7191 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7192 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007193 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007194
Ville Syrjäläd288f652014-10-28 13:20:22 +02007195 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007196 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007197 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007198}
7199
Ville Syrjäläd288f652014-10-28 13:20:22 +02007200static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007201 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007202{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007203 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007204 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007205 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007206 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007207 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007208 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007209
Ville Syrjäläa5805162015-05-26 20:42:30 +03007210 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007211
Ville Syrjäläd288f652014-10-28 13:20:22 +02007212 bestn = pipe_config->dpll.n;
7213 bestm1 = pipe_config->dpll.m1;
7214 bestm2 = pipe_config->dpll.m2;
7215 bestp1 = pipe_config->dpll.p1;
7216 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007217
Jesse Barnes89b667f2013-04-18 14:51:36 -07007218 /* See eDP HDMI DPIO driver vbios notes doc */
7219
7220 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007221 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007222 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007223
7224 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007225 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007226
7227 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007228 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007229 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007230 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007231
7232 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007233 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007234
7235 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007236 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7237 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7238 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007239 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007240
7241 /*
7242 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7243 * but we don't support that).
7244 * Note: don't use the DAC post divider as it seems unstable.
7245 */
7246 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007247 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007248
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007249 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007250 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007251
Jesse Barnes89b667f2013-04-18 14:51:36 -07007252 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007253 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007254 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7255 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007256 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007257 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007258 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007260 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007261
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007262 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007263 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007264 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007265 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007266 0x0df40000);
7267 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007268 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007269 0x0df70000);
7270 } else { /* HDMI or VGA */
7271 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007272 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007273 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007274 0x0df70000);
7275 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007276 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007277 0x0df40000);
7278 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007279
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007280 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007281 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007282 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7283 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007284 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007285 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007286
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007287 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007288 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007289}
7290
Daniel Vetter251ac862015-06-18 10:30:24 +02007291static void chv_compute_dpll(struct intel_crtc *crtc,
7292 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007293{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007294 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7295 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007296 DPLL_VCO_ENABLE;
7297 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007298 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007299
Ville Syrjäläd288f652014-10-28 13:20:22 +02007300 pipe_config->dpll_hw_state.dpll_md =
7301 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007302}
7303
Ville Syrjäläd288f652014-10-28 13:20:22 +02007304static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007305 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007306{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007307 struct drm_device *dev = crtc->base.dev;
7308 struct drm_i915_private *dev_priv = dev->dev_private;
7309 int pipe = crtc->pipe;
7310 int dpll_reg = DPLL(crtc->pipe);
7311 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307312 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007313 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307314 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307315 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007316
Ville Syrjäläd288f652014-10-28 13:20:22 +02007317 bestn = pipe_config->dpll.n;
7318 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7319 bestm1 = pipe_config->dpll.m1;
7320 bestm2 = pipe_config->dpll.m2 >> 22;
7321 bestp1 = pipe_config->dpll.p1;
7322 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307323 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307324 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307325 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007326
7327 /*
7328 * Enable Refclk and SSC
7329 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007330 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007331 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007332
Ville Syrjäläa5805162015-05-26 20:42:30 +03007333 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007334
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007335 /* p1 and p2 divider */
7336 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7337 5 << DPIO_CHV_S1_DIV_SHIFT |
7338 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7339 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7340 1 << DPIO_CHV_K_DIV_SHIFT);
7341
7342 /* Feedback post-divider - m2 */
7343 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7344
7345 /* Feedback refclk divider - n and m1 */
7346 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7347 DPIO_CHV_M1_DIV_BY_2 |
7348 1 << DPIO_CHV_N_DIV_SHIFT);
7349
7350 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007351 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007352
7353 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307354 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7355 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7356 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7357 if (bestm2_frac)
7358 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7359 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007360
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307361 /* Program digital lock detect threshold */
7362 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7363 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7364 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7365 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7366 if (!bestm2_frac)
7367 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7368 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7369
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007370 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307371 if (vco == 5400000) {
7372 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7373 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7374 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7375 tribuf_calcntr = 0x9;
7376 } else if (vco <= 6200000) {
7377 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7378 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7379 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7380 tribuf_calcntr = 0x9;
7381 } else if (vco <= 6480000) {
7382 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7383 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7384 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7385 tribuf_calcntr = 0x8;
7386 } else {
7387 /* Not supported. Apply the same limits as in the max case */
7388 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7389 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7390 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7391 tribuf_calcntr = 0;
7392 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007393 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7394
Ville Syrjälä968040b2015-03-11 22:52:08 +02007395 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307396 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7397 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7398 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7399
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007400 /* AFC Recal */
7401 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7402 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7403 DPIO_AFC_RECAL);
7404
Ville Syrjäläa5805162015-05-26 20:42:30 +03007405 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007406}
7407
Ville Syrjäläd288f652014-10-28 13:20:22 +02007408/**
7409 * vlv_force_pll_on - forcibly enable just the PLL
7410 * @dev_priv: i915 private structure
7411 * @pipe: pipe PLL to enable
7412 * @dpll: PLL configuration
7413 *
7414 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7415 * in cases where we need the PLL enabled even when @pipe is not going to
7416 * be enabled.
7417 */
7418void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7419 const struct dpll *dpll)
7420{
7421 struct intel_crtc *crtc =
7422 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007423 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007424 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007425 .pixel_multiplier = 1,
7426 .dpll = *dpll,
7427 };
7428
7429 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007430 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007431 chv_prepare_pll(crtc, &pipe_config);
7432 chv_enable_pll(crtc, &pipe_config);
7433 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007434 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007435 vlv_prepare_pll(crtc, &pipe_config);
7436 vlv_enable_pll(crtc, &pipe_config);
7437 }
7438}
7439
7440/**
7441 * vlv_force_pll_off - forcibly disable just the PLL
7442 * @dev_priv: i915 private structure
7443 * @pipe: pipe PLL to disable
7444 *
7445 * Disable the PLL for @pipe. To be used in cases where we need
7446 * the PLL enabled even when @pipe is not going to be enabled.
7447 */
7448void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7449{
7450 if (IS_CHERRYVIEW(dev))
7451 chv_disable_pll(to_i915(dev), pipe);
7452 else
7453 vlv_disable_pll(to_i915(dev), pipe);
7454}
7455
Daniel Vetter251ac862015-06-18 10:30:24 +02007456static void i9xx_compute_dpll(struct intel_crtc *crtc,
7457 struct intel_crtc_state *crtc_state,
7458 intel_clock_t *reduced_clock,
7459 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007460{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007461 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007462 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007463 u32 dpll;
7464 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007465 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007466
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007467 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307468
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007469 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7470 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007471
7472 dpll = DPLL_VGA_MODE_DIS;
7473
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007474 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007475 dpll |= DPLLB_MODE_LVDS;
7476 else
7477 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007478
Daniel Vetteref1b4602013-06-01 17:17:04 +02007479 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007480 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007481 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007482 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007483
7484 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007485 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007486
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007487 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007488 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007489
7490 /* compute bitmask from p1 value */
7491 if (IS_PINEVIEW(dev))
7492 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7493 else {
7494 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7495 if (IS_G4X(dev) && reduced_clock)
7496 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7497 }
7498 switch (clock->p2) {
7499 case 5:
7500 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7501 break;
7502 case 7:
7503 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7504 break;
7505 case 10:
7506 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7507 break;
7508 case 14:
7509 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7510 break;
7511 }
7512 if (INTEL_INFO(dev)->gen >= 4)
7513 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7514
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007515 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007516 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007517 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007518 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7519 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7520 else
7521 dpll |= PLL_REF_INPUT_DREFCLK;
7522
7523 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007524 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007525
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007526 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007527 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007528 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007529 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007530 }
7531}
7532
Daniel Vetter251ac862015-06-18 10:30:24 +02007533static void i8xx_compute_dpll(struct intel_crtc *crtc,
7534 struct intel_crtc_state *crtc_state,
7535 intel_clock_t *reduced_clock,
7536 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007537{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007538 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007539 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007540 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007541 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007542
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007543 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307544
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007545 dpll = DPLL_VGA_MODE_DIS;
7546
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007547 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007548 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7549 } else {
7550 if (clock->p1 == 2)
7551 dpll |= PLL_P1_DIVIDE_BY_TWO;
7552 else
7553 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7554 if (clock->p2 == 4)
7555 dpll |= PLL_P2_DIVIDE_BY_4;
7556 }
7557
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007558 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007559 dpll |= DPLL_DVO_2X_MODE;
7560
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007561 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007562 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7563 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7564 else
7565 dpll |= PLL_REF_INPUT_DREFCLK;
7566
7567 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007568 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007569}
7570
Daniel Vetter8a654f32013-06-01 17:16:22 +02007571static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007572{
7573 struct drm_device *dev = intel_crtc->base.dev;
7574 struct drm_i915_private *dev_priv = dev->dev_private;
7575 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007576 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007577 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007578 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007579 uint32_t crtc_vtotal, crtc_vblank_end;
7580 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007581
7582 /* We need to be careful not to changed the adjusted mode, for otherwise
7583 * the hw state checker will get angry at the mismatch. */
7584 crtc_vtotal = adjusted_mode->crtc_vtotal;
7585 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007586
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007587 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007588 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007589 crtc_vtotal -= 1;
7590 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007591
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007592 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007593 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7594 else
7595 vsyncshift = adjusted_mode->crtc_hsync_start -
7596 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007597 if (vsyncshift < 0)
7598 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007599 }
7600
7601 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007602 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007603
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007604 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007605 (adjusted_mode->crtc_hdisplay - 1) |
7606 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007607 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007608 (adjusted_mode->crtc_hblank_start - 1) |
7609 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007610 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007611 (adjusted_mode->crtc_hsync_start - 1) |
7612 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7613
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007614 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007615 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007616 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007617 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007618 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007619 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007620 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007621 (adjusted_mode->crtc_vsync_start - 1) |
7622 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7623
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007624 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7625 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7626 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7627 * bits. */
7628 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7629 (pipe == PIPE_B || pipe == PIPE_C))
7630 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7631
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007632 /* pipesrc controls the size that is scaled from, which should
7633 * always be the user's requested size.
7634 */
7635 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007636 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7637 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007638}
7639
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007640static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007641 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007642{
7643 struct drm_device *dev = crtc->base.dev;
7644 struct drm_i915_private *dev_priv = dev->dev_private;
7645 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7646 uint32_t tmp;
7647
7648 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007649 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7650 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007651 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007652 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7653 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007654 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007655 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7656 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007657
7658 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007659 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7660 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007661 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007662 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7663 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007664 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007665 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7666 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007667
7668 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007669 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7670 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7671 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007672 }
7673
7674 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007675 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7676 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7677
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007678 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7679 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007680}
7681
Daniel Vetterf6a83282014-02-11 15:28:57 -08007682void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007683 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007684{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007685 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7686 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7687 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7688 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007689
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007690 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7691 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7692 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7693 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007694
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007695 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007696 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007697
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007698 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7699 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007700
7701 mode->hsync = drm_mode_hsync(mode);
7702 mode->vrefresh = drm_mode_vrefresh(mode);
7703 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007704}
7705
Daniel Vetter84b046f2013-02-19 18:48:54 +01007706static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7707{
7708 struct drm_device *dev = intel_crtc->base.dev;
7709 struct drm_i915_private *dev_priv = dev->dev_private;
7710 uint32_t pipeconf;
7711
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007712 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007713
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007714 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7715 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7716 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007717
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007718 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007719 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007720
Daniel Vetterff9ce462013-04-24 14:57:17 +02007721 /* only g4x and later have fancy bpc/dither controls */
7722 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007723 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007724 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007725 pipeconf |= PIPECONF_DITHER_EN |
7726 PIPECONF_DITHER_TYPE_SP;
7727
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007728 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007729 case 18:
7730 pipeconf |= PIPECONF_6BPC;
7731 break;
7732 case 24:
7733 pipeconf |= PIPECONF_8BPC;
7734 break;
7735 case 30:
7736 pipeconf |= PIPECONF_10BPC;
7737 break;
7738 default:
7739 /* Case prevented by intel_choose_pipe_bpp_dither. */
7740 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007741 }
7742 }
7743
7744 if (HAS_PIPE_CXSR(dev)) {
7745 if (intel_crtc->lowfreq_avail) {
7746 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7747 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7748 } else {
7749 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007750 }
7751 }
7752
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007753 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007754 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007755 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007756 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7757 else
7758 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7759 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007760 pipeconf |= PIPECONF_PROGRESSIVE;
7761
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007762 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007763 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007764
Daniel Vetter84b046f2013-02-19 18:48:54 +01007765 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7766 POSTING_READ(PIPECONF(intel_crtc->pipe));
7767}
7768
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007769static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7770 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007771{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007772 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007773 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007774 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007775 intel_clock_t clock;
7776 bool ok;
7777 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007778 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007779 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007780 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007781 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007782 struct drm_connector_state *connector_state;
7783 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007784
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007785 memset(&crtc_state->dpll_hw_state, 0,
7786 sizeof(crtc_state->dpll_hw_state));
7787
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007788 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007789 if (connector_state->crtc != &crtc->base)
7790 continue;
7791
7792 encoder = to_intel_encoder(connector_state->best_encoder);
7793
Chris Wilson5eddb702010-09-11 13:48:45 +01007794 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007795 case INTEL_OUTPUT_DSI:
7796 is_dsi = true;
7797 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007798 default:
7799 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007800 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007801
Eric Anholtc751ce42010-03-25 11:48:48 -07007802 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007803 }
7804
Jani Nikulaf2335332013-09-13 11:03:09 +03007805 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007806 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007807
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007808 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007809 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007810
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007811 /*
7812 * Returns a set of divisors for the desired target clock with
7813 * the given refclk, or FALSE. The returned values represent
7814 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7815 * 2) / p1 / p2.
7816 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007817 limit = intel_limit(crtc_state, refclk);
7818 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007819 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007820 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007821 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007822 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7823 return -EINVAL;
7824 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007825
Jani Nikulaf2335332013-09-13 11:03:09 +03007826 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007827 crtc_state->dpll.n = clock.n;
7828 crtc_state->dpll.m1 = clock.m1;
7829 crtc_state->dpll.m2 = clock.m2;
7830 crtc_state->dpll.p1 = clock.p1;
7831 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007832 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007833
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007834 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007835 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007836 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007837 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007838 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007839 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007840 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007841 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007842 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007843 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007844 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007845
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007846 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007847}
7848
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007849static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007850 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007851{
7852 struct drm_device *dev = crtc->base.dev;
7853 struct drm_i915_private *dev_priv = dev->dev_private;
7854 uint32_t tmp;
7855
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007856 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7857 return;
7858
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007859 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007860 if (!(tmp & PFIT_ENABLE))
7861 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007862
Daniel Vetter06922822013-07-11 13:35:40 +02007863 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007864 if (INTEL_INFO(dev)->gen < 4) {
7865 if (crtc->pipe != PIPE_B)
7866 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007867 } else {
7868 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7869 return;
7870 }
7871
Daniel Vetter06922822013-07-11 13:35:40 +02007872 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007873 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7874 if (INTEL_INFO(dev)->gen < 5)
7875 pipe_config->gmch_pfit.lvds_border_bits =
7876 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7877}
7878
Jesse Barnesacbec812013-09-20 11:29:32 -07007879static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007880 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007881{
7882 struct drm_device *dev = crtc->base.dev;
7883 struct drm_i915_private *dev_priv = dev->dev_private;
7884 int pipe = pipe_config->cpu_transcoder;
7885 intel_clock_t clock;
7886 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007887 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007888
Shobhit Kumarf573de52014-07-30 20:32:37 +05307889 /* In case of MIPI DPLL will not even be used */
7890 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7891 return;
7892
Ville Syrjäläa5805162015-05-26 20:42:30 +03007893 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007894 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007895 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007896
7897 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7898 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7899 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7900 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7901 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7902
Imre Deakdccbea32015-06-22 23:35:51 +03007903 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007904}
7905
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007906static void
7907i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7908 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007909{
7910 struct drm_device *dev = crtc->base.dev;
7911 struct drm_i915_private *dev_priv = dev->dev_private;
7912 u32 val, base, offset;
7913 int pipe = crtc->pipe, plane = crtc->plane;
7914 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007915 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007916 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007917 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007918
Damien Lespiau42a7b082015-02-05 19:35:13 +00007919 val = I915_READ(DSPCNTR(plane));
7920 if (!(val & DISPLAY_PLANE_ENABLE))
7921 return;
7922
Damien Lespiaud9806c92015-01-21 14:07:19 +00007923 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007924 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007925 DRM_DEBUG_KMS("failed to alloc fb\n");
7926 return;
7927 }
7928
Damien Lespiau1b842c82015-01-21 13:50:54 +00007929 fb = &intel_fb->base;
7930
Daniel Vetter18c52472015-02-10 17:16:09 +00007931 if (INTEL_INFO(dev)->gen >= 4) {
7932 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007933 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007934 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7935 }
7936 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007937
7938 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007939 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007940 fb->pixel_format = fourcc;
7941 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007942
7943 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007944 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007945 offset = I915_READ(DSPTILEOFF(plane));
7946 else
7947 offset = I915_READ(DSPLINOFF(plane));
7948 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7949 } else {
7950 base = I915_READ(DSPADDR(plane));
7951 }
7952 plane_config->base = base;
7953
7954 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007955 fb->width = ((val >> 16) & 0xfff) + 1;
7956 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007957
7958 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007959 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007960
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007961 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007962 fb->pixel_format,
7963 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007964
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007965 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007966
Damien Lespiau2844a922015-01-20 12:51:48 +00007967 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7968 pipe_name(pipe), plane, fb->width, fb->height,
7969 fb->bits_per_pixel, base, fb->pitches[0],
7970 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007971
Damien Lespiau2d140302015-02-05 17:22:18 +00007972 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007973}
7974
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007975static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007976 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007977{
7978 struct drm_device *dev = crtc->base.dev;
7979 struct drm_i915_private *dev_priv = dev->dev_private;
7980 int pipe = pipe_config->cpu_transcoder;
7981 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7982 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007983 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007984 int refclk = 100000;
7985
Ville Syrjäläa5805162015-05-26 20:42:30 +03007986 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007987 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7988 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7989 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7990 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007991 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007992 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007993
7994 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007995 clock.m2 = (pll_dw0 & 0xff) << 22;
7996 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7997 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007998 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7999 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8000 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8001
Imre Deakdccbea32015-06-22 23:35:51 +03008002 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008003}
8004
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008005static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008006 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008007{
8008 struct drm_device *dev = crtc->base.dev;
8009 struct drm_i915_private *dev_priv = dev->dev_private;
8010 uint32_t tmp;
8011
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008012 if (!intel_display_power_is_enabled(dev_priv,
8013 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008014 return false;
8015
Daniel Vettere143a212013-07-04 12:01:15 +02008016 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008017 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008018
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008019 tmp = I915_READ(PIPECONF(crtc->pipe));
8020 if (!(tmp & PIPECONF_ENABLE))
8021 return false;
8022
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008023 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8024 switch (tmp & PIPECONF_BPC_MASK) {
8025 case PIPECONF_6BPC:
8026 pipe_config->pipe_bpp = 18;
8027 break;
8028 case PIPECONF_8BPC:
8029 pipe_config->pipe_bpp = 24;
8030 break;
8031 case PIPECONF_10BPC:
8032 pipe_config->pipe_bpp = 30;
8033 break;
8034 default:
8035 break;
8036 }
8037 }
8038
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008039 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8040 pipe_config->limited_color_range = true;
8041
Ville Syrjälä282740f2013-09-04 18:30:03 +03008042 if (INTEL_INFO(dev)->gen < 4)
8043 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8044
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008045 intel_get_pipe_timings(crtc, pipe_config);
8046
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008047 i9xx_get_pfit_config(crtc, pipe_config);
8048
Daniel Vetter6c49f242013-06-06 12:45:25 +02008049 if (INTEL_INFO(dev)->gen >= 4) {
8050 tmp = I915_READ(DPLL_MD(crtc->pipe));
8051 pipe_config->pixel_multiplier =
8052 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8053 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008054 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008055 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8056 tmp = I915_READ(DPLL(crtc->pipe));
8057 pipe_config->pixel_multiplier =
8058 ((tmp & SDVO_MULTIPLIER_MASK)
8059 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8060 } else {
8061 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8062 * port and will be fixed up in the encoder->get_config
8063 * function. */
8064 pipe_config->pixel_multiplier = 1;
8065 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008066 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8067 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008068 /*
8069 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8070 * on 830. Filter it out here so that we don't
8071 * report errors due to that.
8072 */
8073 if (IS_I830(dev))
8074 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8075
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008076 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8077 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008078 } else {
8079 /* Mask out read-only status bits. */
8080 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8081 DPLL_PORTC_READY_MASK |
8082 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008083 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008084
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008085 if (IS_CHERRYVIEW(dev))
8086 chv_crtc_clock_get(crtc, pipe_config);
8087 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008088 vlv_crtc_clock_get(crtc, pipe_config);
8089 else
8090 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008091
Ville Syrjälä0f646142015-08-26 19:39:18 +03008092 /*
8093 * Normally the dotclock is filled in by the encoder .get_config()
8094 * but in case the pipe is enabled w/o any ports we need a sane
8095 * default.
8096 */
8097 pipe_config->base.adjusted_mode.crtc_clock =
8098 pipe_config->port_clock / pipe_config->pixel_multiplier;
8099
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008100 return true;
8101}
8102
Paulo Zanonidde86e22012-12-01 12:04:25 -02008103static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008104{
8105 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008106 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008107 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008108 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008109 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008110 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008111 bool has_ck505 = false;
8112 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008113
8114 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008115 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008116 switch (encoder->type) {
8117 case INTEL_OUTPUT_LVDS:
8118 has_panel = true;
8119 has_lvds = true;
8120 break;
8121 case INTEL_OUTPUT_EDP:
8122 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008123 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008124 has_cpu_edp = true;
8125 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008126 default:
8127 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008128 }
8129 }
8130
Keith Packard99eb6a02011-09-26 14:29:12 -07008131 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008132 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008133 can_ssc = has_ck505;
8134 } else {
8135 has_ck505 = false;
8136 can_ssc = true;
8137 }
8138
Imre Deak2de69052013-05-08 13:14:04 +03008139 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8140 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008141
8142 /* Ironlake: try to setup display ref clock before DPLL
8143 * enabling. This is only under driver's control after
8144 * PCH B stepping, previous chipset stepping should be
8145 * ignoring this setting.
8146 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008147 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008148
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008149 /* As we must carefully and slowly disable/enable each source in turn,
8150 * compute the final state we want first and check if we need to
8151 * make any changes at all.
8152 */
8153 final = val;
8154 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008155 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008156 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008157 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008158 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8159
8160 final &= ~DREF_SSC_SOURCE_MASK;
8161 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8162 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008163
Keith Packard199e5d72011-09-22 12:01:57 -07008164 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008165 final |= DREF_SSC_SOURCE_ENABLE;
8166
8167 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8168 final |= DREF_SSC1_ENABLE;
8169
8170 if (has_cpu_edp) {
8171 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8172 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8173 else
8174 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8175 } else
8176 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8177 } else {
8178 final |= DREF_SSC_SOURCE_DISABLE;
8179 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8180 }
8181
8182 if (final == val)
8183 return;
8184
8185 /* Always enable nonspread source */
8186 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8187
8188 if (has_ck505)
8189 val |= DREF_NONSPREAD_CK505_ENABLE;
8190 else
8191 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8192
8193 if (has_panel) {
8194 val &= ~DREF_SSC_SOURCE_MASK;
8195 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008196
Keith Packard199e5d72011-09-22 12:01:57 -07008197 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008198 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008199 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008200 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008201 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008202 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008203
8204 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008205 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008206 POSTING_READ(PCH_DREF_CONTROL);
8207 udelay(200);
8208
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008209 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008210
8211 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008212 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008213 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008214 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008215 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008216 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008217 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008218 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008219 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008220
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008221 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008222 POSTING_READ(PCH_DREF_CONTROL);
8223 udelay(200);
8224 } else {
8225 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8226
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008227 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008228
8229 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008230 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008231
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008232 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008233 POSTING_READ(PCH_DREF_CONTROL);
8234 udelay(200);
8235
8236 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008237 val &= ~DREF_SSC_SOURCE_MASK;
8238 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008239
8240 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008241 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008242
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008243 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008244 POSTING_READ(PCH_DREF_CONTROL);
8245 udelay(200);
8246 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008247
8248 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008249}
8250
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008251static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008252{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008253 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008254
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008255 tmp = I915_READ(SOUTH_CHICKEN2);
8256 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8257 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008258
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008259 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8260 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8261 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008262
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008263 tmp = I915_READ(SOUTH_CHICKEN2);
8264 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8265 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008266
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008267 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8268 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8269 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008270}
8271
8272/* WaMPhyProgramming:hsw */
8273static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8274{
8275 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008276
8277 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8278 tmp &= ~(0xFF << 24);
8279 tmp |= (0x12 << 24);
8280 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8281
Paulo Zanonidde86e22012-12-01 12:04:25 -02008282 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8283 tmp |= (1 << 11);
8284 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8285
8286 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8287 tmp |= (1 << 11);
8288 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8289
Paulo Zanonidde86e22012-12-01 12:04:25 -02008290 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8291 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8292 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8293
8294 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8295 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8296 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8297
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008298 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8299 tmp &= ~(7 << 13);
8300 tmp |= (5 << 13);
8301 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008302
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008303 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8304 tmp &= ~(7 << 13);
8305 tmp |= (5 << 13);
8306 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008307
8308 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8309 tmp &= ~0xFF;
8310 tmp |= 0x1C;
8311 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8312
8313 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8314 tmp &= ~0xFF;
8315 tmp |= 0x1C;
8316 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8317
8318 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8319 tmp &= ~(0xFF << 16);
8320 tmp |= (0x1C << 16);
8321 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8322
8323 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8324 tmp &= ~(0xFF << 16);
8325 tmp |= (0x1C << 16);
8326 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8327
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008328 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8329 tmp |= (1 << 27);
8330 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008331
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008332 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8333 tmp |= (1 << 27);
8334 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008335
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008336 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8337 tmp &= ~(0xF << 28);
8338 tmp |= (4 << 28);
8339 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008340
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008341 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8342 tmp &= ~(0xF << 28);
8343 tmp |= (4 << 28);
8344 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008345}
8346
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008347/* Implements 3 different sequences from BSpec chapter "Display iCLK
8348 * Programming" based on the parameters passed:
8349 * - Sequence to enable CLKOUT_DP
8350 * - Sequence to enable CLKOUT_DP without spread
8351 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8352 */
8353static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8354 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008355{
8356 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008357 uint32_t reg, tmp;
8358
8359 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8360 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008361 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008362 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008363
Ville Syrjäläa5805162015-05-26 20:42:30 +03008364 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008365
8366 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8367 tmp &= ~SBI_SSCCTL_DISABLE;
8368 tmp |= SBI_SSCCTL_PATHALT;
8369 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8370
8371 udelay(24);
8372
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008373 if (with_spread) {
8374 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8375 tmp &= ~SBI_SSCCTL_PATHALT;
8376 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008377
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008378 if (with_fdi) {
8379 lpt_reset_fdi_mphy(dev_priv);
8380 lpt_program_fdi_mphy(dev_priv);
8381 }
8382 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008383
Ville Syrjäläc2699522015-08-27 23:55:59 +03008384 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008385 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8386 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8387 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008388
Ville Syrjäläa5805162015-05-26 20:42:30 +03008389 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008390}
8391
Paulo Zanoni47701c32013-07-23 11:19:25 -03008392/* Sequence to disable CLKOUT_DP */
8393static void lpt_disable_clkout_dp(struct drm_device *dev)
8394{
8395 struct drm_i915_private *dev_priv = dev->dev_private;
8396 uint32_t reg, tmp;
8397
Ville Syrjäläa5805162015-05-26 20:42:30 +03008398 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008399
Ville Syrjäläc2699522015-08-27 23:55:59 +03008400 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008401 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8402 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8403 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8404
8405 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8406 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8407 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8408 tmp |= SBI_SSCCTL_PATHALT;
8409 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8410 udelay(32);
8411 }
8412 tmp |= SBI_SSCCTL_DISABLE;
8413 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8414 }
8415
Ville Syrjäläa5805162015-05-26 20:42:30 +03008416 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008417}
8418
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008419static void lpt_init_pch_refclk(struct drm_device *dev)
8420{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008421 struct intel_encoder *encoder;
8422 bool has_vga = false;
8423
Damien Lespiaub2784e12014-08-05 11:29:37 +01008424 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008425 switch (encoder->type) {
8426 case INTEL_OUTPUT_ANALOG:
8427 has_vga = true;
8428 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008429 default:
8430 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008431 }
8432 }
8433
Paulo Zanoni47701c32013-07-23 11:19:25 -03008434 if (has_vga)
8435 lpt_enable_clkout_dp(dev, true, true);
8436 else
8437 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008438}
8439
Paulo Zanonidde86e22012-12-01 12:04:25 -02008440/*
8441 * Initialize reference clocks when the driver loads
8442 */
8443void intel_init_pch_refclk(struct drm_device *dev)
8444{
8445 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8446 ironlake_init_pch_refclk(dev);
8447 else if (HAS_PCH_LPT(dev))
8448 lpt_init_pch_refclk(dev);
8449}
8450
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008451static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008452{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008453 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008454 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008455 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008456 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008457 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008458 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008459 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008460 bool is_lvds = false;
8461
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008462 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008463 if (connector_state->crtc != crtc_state->base.crtc)
8464 continue;
8465
8466 encoder = to_intel_encoder(connector_state->best_encoder);
8467
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008468 switch (encoder->type) {
8469 case INTEL_OUTPUT_LVDS:
8470 is_lvds = true;
8471 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008472 default:
8473 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008474 }
8475 num_connectors++;
8476 }
8477
8478 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008479 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008480 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008481 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008482 }
8483
8484 return 120000;
8485}
8486
Daniel Vetter6ff93602013-04-19 11:24:36 +02008487static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008488{
8489 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8491 int pipe = intel_crtc->pipe;
8492 uint32_t val;
8493
Daniel Vetter78114072013-06-13 00:54:57 +02008494 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008495
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008496 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008497 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008498 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008499 break;
8500 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008501 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008502 break;
8503 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008504 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008505 break;
8506 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008507 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008508 break;
8509 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008510 /* Case prevented by intel_choose_pipe_bpp_dither. */
8511 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008512 }
8513
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008514 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008515 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8516
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008517 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008518 val |= PIPECONF_INTERLACED_ILK;
8519 else
8520 val |= PIPECONF_PROGRESSIVE;
8521
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008522 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008523 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008524
Paulo Zanonic8203562012-09-12 10:06:29 -03008525 I915_WRITE(PIPECONF(pipe), val);
8526 POSTING_READ(PIPECONF(pipe));
8527}
8528
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008529/*
8530 * Set up the pipe CSC unit.
8531 *
8532 * Currently only full range RGB to limited range RGB conversion
8533 * is supported, but eventually this should handle various
8534 * RGB<->YCbCr scenarios as well.
8535 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008536static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008537{
8538 struct drm_device *dev = crtc->dev;
8539 struct drm_i915_private *dev_priv = dev->dev_private;
8540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8541 int pipe = intel_crtc->pipe;
8542 uint16_t coeff = 0x7800; /* 1.0 */
8543
8544 /*
8545 * TODO: Check what kind of values actually come out of the pipe
8546 * with these coeff/postoff values and adjust to get the best
8547 * accuracy. Perhaps we even need to take the bpc value into
8548 * consideration.
8549 */
8550
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008551 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008552 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8553
8554 /*
8555 * GY/GU and RY/RU should be the other way around according
8556 * to BSpec, but reality doesn't agree. Just set them up in
8557 * a way that results in the correct picture.
8558 */
8559 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8560 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8561
8562 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8563 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8564
8565 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8566 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8567
8568 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8569 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8570 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8571
8572 if (INTEL_INFO(dev)->gen > 6) {
8573 uint16_t postoff = 0;
8574
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008575 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008576 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008577
8578 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8579 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8580 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8581
8582 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8583 } else {
8584 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8585
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008586 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008587 mode |= CSC_BLACK_SCREEN_OFFSET;
8588
8589 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8590 }
8591}
8592
Daniel Vetter6ff93602013-04-19 11:24:36 +02008593static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008594{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008595 struct drm_device *dev = crtc->dev;
8596 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008598 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008599 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008600 uint32_t val;
8601
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008602 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008603
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008604 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008605 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8606
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008607 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008608 val |= PIPECONF_INTERLACED_ILK;
8609 else
8610 val |= PIPECONF_PROGRESSIVE;
8611
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008612 I915_WRITE(PIPECONF(cpu_transcoder), val);
8613 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008614
8615 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8616 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008617
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308618 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008619 val = 0;
8620
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008621 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008622 case 18:
8623 val |= PIPEMISC_DITHER_6_BPC;
8624 break;
8625 case 24:
8626 val |= PIPEMISC_DITHER_8_BPC;
8627 break;
8628 case 30:
8629 val |= PIPEMISC_DITHER_10_BPC;
8630 break;
8631 case 36:
8632 val |= PIPEMISC_DITHER_12_BPC;
8633 break;
8634 default:
8635 /* Case prevented by pipe_config_set_bpp. */
8636 BUG();
8637 }
8638
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008639 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008640 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8641
8642 I915_WRITE(PIPEMISC(pipe), val);
8643 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008644}
8645
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008646static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008647 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008648 intel_clock_t *clock,
8649 bool *has_reduced_clock,
8650 intel_clock_t *reduced_clock)
8651{
8652 struct drm_device *dev = crtc->dev;
8653 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008654 int refclk;
8655 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008656 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008657
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008658 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008659
8660 /*
8661 * Returns a set of divisors for the desired target clock with the given
8662 * refclk, or FALSE. The returned values represent the clock equation:
8663 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8664 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008665 limit = intel_limit(crtc_state, refclk);
8666 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008667 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008668 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008669 if (!ret)
8670 return false;
8671
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008672 return true;
8673}
8674
Paulo Zanonid4b19312012-11-29 11:29:32 -02008675int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8676{
8677 /*
8678 * Account for spread spectrum to avoid
8679 * oversubscribing the link. Max center spread
8680 * is 2.5%; use 5% for safety's sake.
8681 */
8682 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008683 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008684}
8685
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008686static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008687{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008688 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008689}
8690
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008691static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008692 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008693 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008694 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008695{
8696 struct drm_crtc *crtc = &intel_crtc->base;
8697 struct drm_device *dev = crtc->dev;
8698 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008699 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008700 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008701 struct drm_connector_state *connector_state;
8702 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008703 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008704 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008705 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008706
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008707 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008708 if (connector_state->crtc != crtc_state->base.crtc)
8709 continue;
8710
8711 encoder = to_intel_encoder(connector_state->best_encoder);
8712
8713 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008714 case INTEL_OUTPUT_LVDS:
8715 is_lvds = true;
8716 break;
8717 case INTEL_OUTPUT_SDVO:
8718 case INTEL_OUTPUT_HDMI:
8719 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008720 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008721 default:
8722 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008723 }
8724
8725 num_connectors++;
8726 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008727
Chris Wilsonc1858122010-12-03 21:35:48 +00008728 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008729 factor = 21;
8730 if (is_lvds) {
8731 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008732 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008733 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008734 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008735 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008736 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008737
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008738 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008739 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008740
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008741 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8742 *fp2 |= FP_CB_TUNE;
8743
Chris Wilson5eddb702010-09-11 13:48:45 +01008744 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008745
Eric Anholta07d6782011-03-30 13:01:08 -07008746 if (is_lvds)
8747 dpll |= DPLLB_MODE_LVDS;
8748 else
8749 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008750
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008751 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008752 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008753
8754 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008755 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008756 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008757 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008758
Eric Anholta07d6782011-03-30 13:01:08 -07008759 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008760 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008761 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008762 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008763
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008764 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008765 case 5:
8766 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8767 break;
8768 case 7:
8769 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8770 break;
8771 case 10:
8772 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8773 break;
8774 case 14:
8775 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8776 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008777 }
8778
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008779 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008780 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008781 else
8782 dpll |= PLL_REF_INPUT_DREFCLK;
8783
Daniel Vetter959e16d2013-06-05 13:34:21 +02008784 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008785}
8786
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008787static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8788 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008789{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008790 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008791 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008792 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008793 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008794 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008795 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008796
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008797 memset(&crtc_state->dpll_hw_state, 0,
8798 sizeof(crtc_state->dpll_hw_state));
8799
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008800 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008801
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008802 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8803 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8804
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008805 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008806 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008807 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008808 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8809 return -EINVAL;
8810 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008811 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008812 if (!crtc_state->clock_set) {
8813 crtc_state->dpll.n = clock.n;
8814 crtc_state->dpll.m1 = clock.m1;
8815 crtc_state->dpll.m2 = clock.m2;
8816 crtc_state->dpll.p1 = clock.p1;
8817 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008818 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008819
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008820 /* 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 +02008821 if (crtc_state->has_pch_encoder) {
8822 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008823 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008824 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008825
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008826 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008827 &fp, &reduced_clock,
8828 has_reduced_clock ? &fp2 : NULL);
8829
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008830 crtc_state->dpll_hw_state.dpll = dpll;
8831 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008832 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008833 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008834 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008835 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008836
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008837 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008838 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008839 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008840 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008841 return -EINVAL;
8842 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008843 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008844
Rodrigo Viviab585de2015-03-24 12:40:09 -07008845 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008846 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008847 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008848 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008849
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008850 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008851}
8852
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008853static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8854 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008855{
8856 struct drm_device *dev = crtc->base.dev;
8857 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008858 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008859
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008860 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8861 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8862 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8863 & ~TU_SIZE_MASK;
8864 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8865 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8866 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8867}
8868
8869static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8870 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008871 struct intel_link_m_n *m_n,
8872 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008873{
8874 struct drm_device *dev = crtc->base.dev;
8875 struct drm_i915_private *dev_priv = dev->dev_private;
8876 enum pipe pipe = crtc->pipe;
8877
8878 if (INTEL_INFO(dev)->gen >= 5) {
8879 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8880 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8881 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8882 & ~TU_SIZE_MASK;
8883 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8884 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8885 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008886 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8887 * gen < 8) and if DRRS is supported (to make sure the
8888 * registers are not unnecessarily read).
8889 */
8890 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008891 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008892 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8893 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8894 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8895 & ~TU_SIZE_MASK;
8896 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8897 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8898 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8899 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008900 } else {
8901 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8902 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8903 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8904 & ~TU_SIZE_MASK;
8905 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8906 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8907 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8908 }
8909}
8910
8911void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008912 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008913{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008914 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008915 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8916 else
8917 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008918 &pipe_config->dp_m_n,
8919 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008920}
8921
Daniel Vetter72419202013-04-04 13:28:53 +02008922static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008923 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008924{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008925 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008926 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008927}
8928
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008929static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008930 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008931{
8932 struct drm_device *dev = crtc->base.dev;
8933 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008934 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8935 uint32_t ps_ctrl = 0;
8936 int id = -1;
8937 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008938
Chandra Kondurua1b22782015-04-07 15:28:45 -07008939 /* find scaler attached to this pipe */
8940 for (i = 0; i < crtc->num_scalers; i++) {
8941 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8942 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8943 id = i;
8944 pipe_config->pch_pfit.enabled = true;
8945 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8946 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8947 break;
8948 }
8949 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008950
Chandra Kondurua1b22782015-04-07 15:28:45 -07008951 scaler_state->scaler_id = id;
8952 if (id >= 0) {
8953 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8954 } else {
8955 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008956 }
8957}
8958
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008959static void
8960skylake_get_initial_plane_config(struct intel_crtc *crtc,
8961 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008962{
8963 struct drm_device *dev = crtc->base.dev;
8964 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008965 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008966 int pipe = crtc->pipe;
8967 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008968 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008969 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008970 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008971
Damien Lespiaud9806c92015-01-21 14:07:19 +00008972 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008973 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008974 DRM_DEBUG_KMS("failed to alloc fb\n");
8975 return;
8976 }
8977
Damien Lespiau1b842c82015-01-21 13:50:54 +00008978 fb = &intel_fb->base;
8979
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008980 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008981 if (!(val & PLANE_CTL_ENABLE))
8982 goto error;
8983
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008984 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8985 fourcc = skl_format_to_fourcc(pixel_format,
8986 val & PLANE_CTL_ORDER_RGBX,
8987 val & PLANE_CTL_ALPHA_MASK);
8988 fb->pixel_format = fourcc;
8989 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8990
Damien Lespiau40f46282015-02-27 11:15:21 +00008991 tiling = val & PLANE_CTL_TILED_MASK;
8992 switch (tiling) {
8993 case PLANE_CTL_TILED_LINEAR:
8994 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8995 break;
8996 case PLANE_CTL_TILED_X:
8997 plane_config->tiling = I915_TILING_X;
8998 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8999 break;
9000 case PLANE_CTL_TILED_Y:
9001 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9002 break;
9003 case PLANE_CTL_TILED_YF:
9004 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9005 break;
9006 default:
9007 MISSING_CASE(tiling);
9008 goto error;
9009 }
9010
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009011 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9012 plane_config->base = base;
9013
9014 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9015
9016 val = I915_READ(PLANE_SIZE(pipe, 0));
9017 fb->height = ((val >> 16) & 0xfff) + 1;
9018 fb->width = ((val >> 0) & 0x1fff) + 1;
9019
9020 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009021 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9022 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009023 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9024
9025 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009026 fb->pixel_format,
9027 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009028
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009029 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009030
9031 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9032 pipe_name(pipe), fb->width, fb->height,
9033 fb->bits_per_pixel, base, fb->pitches[0],
9034 plane_config->size);
9035
Damien Lespiau2d140302015-02-05 17:22:18 +00009036 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009037 return;
9038
9039error:
9040 kfree(fb);
9041}
9042
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009043static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009044 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009045{
9046 struct drm_device *dev = crtc->base.dev;
9047 struct drm_i915_private *dev_priv = dev->dev_private;
9048 uint32_t tmp;
9049
9050 tmp = I915_READ(PF_CTL(crtc->pipe));
9051
9052 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009053 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009054 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9055 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009056
9057 /* We currently do not free assignements of panel fitters on
9058 * ivb/hsw (since we don't use the higher upscaling modes which
9059 * differentiates them) so just WARN about this case for now. */
9060 if (IS_GEN7(dev)) {
9061 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9062 PF_PIPE_SEL_IVB(crtc->pipe));
9063 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009064 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009065}
9066
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009067static void
9068ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9069 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009070{
9071 struct drm_device *dev = crtc->base.dev;
9072 struct drm_i915_private *dev_priv = dev->dev_private;
9073 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009074 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009075 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009076 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009077 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009078 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009079
Damien Lespiau42a7b082015-02-05 19:35:13 +00009080 val = I915_READ(DSPCNTR(pipe));
9081 if (!(val & DISPLAY_PLANE_ENABLE))
9082 return;
9083
Damien Lespiaud9806c92015-01-21 14:07:19 +00009084 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009085 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009086 DRM_DEBUG_KMS("failed to alloc fb\n");
9087 return;
9088 }
9089
Damien Lespiau1b842c82015-01-21 13:50:54 +00009090 fb = &intel_fb->base;
9091
Daniel Vetter18c52472015-02-10 17:16:09 +00009092 if (INTEL_INFO(dev)->gen >= 4) {
9093 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009094 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009095 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9096 }
9097 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009098
9099 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009100 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009101 fb->pixel_format = fourcc;
9102 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009103
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009104 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009105 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009106 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009107 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009108 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009109 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009110 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009111 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009112 }
9113 plane_config->base = base;
9114
9115 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009116 fb->width = ((val >> 16) & 0xfff) + 1;
9117 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009118
9119 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009120 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009121
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009122 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009123 fb->pixel_format,
9124 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009125
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009126 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009127
Damien Lespiau2844a922015-01-20 12:51:48 +00009128 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9129 pipe_name(pipe), fb->width, fb->height,
9130 fb->bits_per_pixel, base, fb->pitches[0],
9131 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009132
Damien Lespiau2d140302015-02-05 17:22:18 +00009133 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009134}
9135
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009136static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009137 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009138{
9139 struct drm_device *dev = crtc->base.dev;
9140 struct drm_i915_private *dev_priv = dev->dev_private;
9141 uint32_t tmp;
9142
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009143 if (!intel_display_power_is_enabled(dev_priv,
9144 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009145 return false;
9146
Daniel Vettere143a212013-07-04 12:01:15 +02009147 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009148 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009149
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009150 tmp = I915_READ(PIPECONF(crtc->pipe));
9151 if (!(tmp & PIPECONF_ENABLE))
9152 return false;
9153
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009154 switch (tmp & PIPECONF_BPC_MASK) {
9155 case PIPECONF_6BPC:
9156 pipe_config->pipe_bpp = 18;
9157 break;
9158 case PIPECONF_8BPC:
9159 pipe_config->pipe_bpp = 24;
9160 break;
9161 case PIPECONF_10BPC:
9162 pipe_config->pipe_bpp = 30;
9163 break;
9164 case PIPECONF_12BPC:
9165 pipe_config->pipe_bpp = 36;
9166 break;
9167 default:
9168 break;
9169 }
9170
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009171 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9172 pipe_config->limited_color_range = true;
9173
Daniel Vetterab9412b2013-05-03 11:49:46 +02009174 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009175 struct intel_shared_dpll *pll;
9176
Daniel Vetter88adfff2013-03-28 10:42:01 +01009177 pipe_config->has_pch_encoder = true;
9178
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009179 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9180 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9181 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009182
9183 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009184
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009185 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009186 pipe_config->shared_dpll =
9187 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009188 } else {
9189 tmp = I915_READ(PCH_DPLL_SEL);
9190 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9191 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9192 else
9193 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9194 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009195
9196 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9197
9198 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9199 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009200
9201 tmp = pipe_config->dpll_hw_state.dpll;
9202 pipe_config->pixel_multiplier =
9203 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9204 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009205
9206 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009207 } else {
9208 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009209 }
9210
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009211 intel_get_pipe_timings(crtc, pipe_config);
9212
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009213 ironlake_get_pfit_config(crtc, pipe_config);
9214
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009215 return true;
9216}
9217
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009218static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9219{
9220 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009221 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009222
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009223 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009224 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009225 pipe_name(crtc->pipe));
9226
Rob Clarke2c719b2014-12-15 13:56:32 -05009227 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9228 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9229 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9230 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9231 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9232 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009233 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009234 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009235 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009236 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009237 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009238 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009239 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009240 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009241 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009242
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009243 /*
9244 * In theory we can still leave IRQs enabled, as long as only the HPD
9245 * interrupts remain enabled. We used to check for that, but since it's
9246 * gen-specific and since we only disable LCPLL after we fully disable
9247 * the interrupts, the check below should be enough.
9248 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009249 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009250}
9251
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009252static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9253{
9254 struct drm_device *dev = dev_priv->dev;
9255
9256 if (IS_HASWELL(dev))
9257 return I915_READ(D_COMP_HSW);
9258 else
9259 return I915_READ(D_COMP_BDW);
9260}
9261
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009262static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9263{
9264 struct drm_device *dev = dev_priv->dev;
9265
9266 if (IS_HASWELL(dev)) {
9267 mutex_lock(&dev_priv->rps.hw_lock);
9268 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9269 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009270 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009271 mutex_unlock(&dev_priv->rps.hw_lock);
9272 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009273 I915_WRITE(D_COMP_BDW, val);
9274 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009275 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009276}
9277
9278/*
9279 * This function implements pieces of two sequences from BSpec:
9280 * - Sequence for display software to disable LCPLL
9281 * - Sequence for display software to allow package C8+
9282 * The steps implemented here are just the steps that actually touch the LCPLL
9283 * register. Callers should take care of disabling all the display engine
9284 * functions, doing the mode unset, fixing interrupts, etc.
9285 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009286static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9287 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009288{
9289 uint32_t val;
9290
9291 assert_can_disable_lcpll(dev_priv);
9292
9293 val = I915_READ(LCPLL_CTL);
9294
9295 if (switch_to_fclk) {
9296 val |= LCPLL_CD_SOURCE_FCLK;
9297 I915_WRITE(LCPLL_CTL, val);
9298
9299 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9300 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9301 DRM_ERROR("Switching to FCLK failed\n");
9302
9303 val = I915_READ(LCPLL_CTL);
9304 }
9305
9306 val |= LCPLL_PLL_DISABLE;
9307 I915_WRITE(LCPLL_CTL, val);
9308 POSTING_READ(LCPLL_CTL);
9309
9310 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9311 DRM_ERROR("LCPLL still locked\n");
9312
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009313 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009314 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009315 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009316 ndelay(100);
9317
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009318 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9319 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009320 DRM_ERROR("D_COMP RCOMP still in progress\n");
9321
9322 if (allow_power_down) {
9323 val = I915_READ(LCPLL_CTL);
9324 val |= LCPLL_POWER_DOWN_ALLOW;
9325 I915_WRITE(LCPLL_CTL, val);
9326 POSTING_READ(LCPLL_CTL);
9327 }
9328}
9329
9330/*
9331 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9332 * source.
9333 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009334static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009335{
9336 uint32_t val;
9337
9338 val = I915_READ(LCPLL_CTL);
9339
9340 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9341 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9342 return;
9343
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009344 /*
9345 * Make sure we're not on PC8 state before disabling PC8, otherwise
9346 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009347 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009348 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009349
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009350 if (val & LCPLL_POWER_DOWN_ALLOW) {
9351 val &= ~LCPLL_POWER_DOWN_ALLOW;
9352 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009353 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009354 }
9355
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009356 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009357 val |= D_COMP_COMP_FORCE;
9358 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009359 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009360
9361 val = I915_READ(LCPLL_CTL);
9362 val &= ~LCPLL_PLL_DISABLE;
9363 I915_WRITE(LCPLL_CTL, val);
9364
9365 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9366 DRM_ERROR("LCPLL not locked yet\n");
9367
9368 if (val & LCPLL_CD_SOURCE_FCLK) {
9369 val = I915_READ(LCPLL_CTL);
9370 val &= ~LCPLL_CD_SOURCE_FCLK;
9371 I915_WRITE(LCPLL_CTL, val);
9372
9373 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9374 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9375 DRM_ERROR("Switching back to LCPLL failed\n");
9376 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009377
Mika Kuoppala59bad942015-01-16 11:34:40 +02009378 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009379 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009380}
9381
Paulo Zanoni765dab672014-03-07 20:08:18 -03009382/*
9383 * Package states C8 and deeper are really deep PC states that can only be
9384 * reached when all the devices on the system allow it, so even if the graphics
9385 * device allows PC8+, it doesn't mean the system will actually get to these
9386 * states. Our driver only allows PC8+ when going into runtime PM.
9387 *
9388 * The requirements for PC8+ are that all the outputs are disabled, the power
9389 * well is disabled and most interrupts are disabled, and these are also
9390 * requirements for runtime PM. When these conditions are met, we manually do
9391 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9392 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9393 * hang the machine.
9394 *
9395 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9396 * the state of some registers, so when we come back from PC8+ we need to
9397 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9398 * need to take care of the registers kept by RC6. Notice that this happens even
9399 * if we don't put the device in PCI D3 state (which is what currently happens
9400 * because of the runtime PM support).
9401 *
9402 * For more, read "Display Sequences for Package C8" on the hardware
9403 * documentation.
9404 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009405void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009406{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009407 struct drm_device *dev = dev_priv->dev;
9408 uint32_t val;
9409
Paulo Zanonic67a4702013-08-19 13:18:09 -03009410 DRM_DEBUG_KMS("Enabling package C8+\n");
9411
Ville Syrjäläc2699522015-08-27 23:55:59 +03009412 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009413 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9414 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9415 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9416 }
9417
9418 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009419 hsw_disable_lcpll(dev_priv, true, true);
9420}
9421
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009422void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009423{
9424 struct drm_device *dev = dev_priv->dev;
9425 uint32_t val;
9426
Paulo Zanonic67a4702013-08-19 13:18:09 -03009427 DRM_DEBUG_KMS("Disabling package C8+\n");
9428
9429 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009430 lpt_init_pch_refclk(dev);
9431
Ville Syrjäläc2699522015-08-27 23:55:59 +03009432 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009433 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9434 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9435 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9436 }
9437
9438 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009439}
9440
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009441static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309442{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009443 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009444 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309445
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009446 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309447}
9448
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009449/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009450static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009451{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009452 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009453 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009454 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009455
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009456 for_each_intel_crtc(state->dev, intel_crtc) {
9457 int pixel_rate;
9458
9459 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9460 if (IS_ERR(crtc_state))
9461 return PTR_ERR(crtc_state);
9462
9463 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009464 continue;
9465
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009466 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009467
9468 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009469 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009470 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9471
9472 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9473 }
9474
9475 return max_pixel_rate;
9476}
9477
9478static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9479{
9480 struct drm_i915_private *dev_priv = dev->dev_private;
9481 uint32_t val, data;
9482 int ret;
9483
9484 if (WARN((I915_READ(LCPLL_CTL) &
9485 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9486 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9487 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9488 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9489 "trying to change cdclk frequency with cdclk not enabled\n"))
9490 return;
9491
9492 mutex_lock(&dev_priv->rps.hw_lock);
9493 ret = sandybridge_pcode_write(dev_priv,
9494 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9495 mutex_unlock(&dev_priv->rps.hw_lock);
9496 if (ret) {
9497 DRM_ERROR("failed to inform pcode about cdclk change\n");
9498 return;
9499 }
9500
9501 val = I915_READ(LCPLL_CTL);
9502 val |= LCPLL_CD_SOURCE_FCLK;
9503 I915_WRITE(LCPLL_CTL, val);
9504
9505 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9506 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9507 DRM_ERROR("Switching to FCLK failed\n");
9508
9509 val = I915_READ(LCPLL_CTL);
9510 val &= ~LCPLL_CLK_FREQ_MASK;
9511
9512 switch (cdclk) {
9513 case 450000:
9514 val |= LCPLL_CLK_FREQ_450;
9515 data = 0;
9516 break;
9517 case 540000:
9518 val |= LCPLL_CLK_FREQ_54O_BDW;
9519 data = 1;
9520 break;
9521 case 337500:
9522 val |= LCPLL_CLK_FREQ_337_5_BDW;
9523 data = 2;
9524 break;
9525 case 675000:
9526 val |= LCPLL_CLK_FREQ_675_BDW;
9527 data = 3;
9528 break;
9529 default:
9530 WARN(1, "invalid cdclk frequency\n");
9531 return;
9532 }
9533
9534 I915_WRITE(LCPLL_CTL, val);
9535
9536 val = I915_READ(LCPLL_CTL);
9537 val &= ~LCPLL_CD_SOURCE_FCLK;
9538 I915_WRITE(LCPLL_CTL, val);
9539
9540 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9541 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9542 DRM_ERROR("Switching back to LCPLL failed\n");
9543
9544 mutex_lock(&dev_priv->rps.hw_lock);
9545 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9546 mutex_unlock(&dev_priv->rps.hw_lock);
9547
9548 intel_update_cdclk(dev);
9549
9550 WARN(cdclk != dev_priv->cdclk_freq,
9551 "cdclk requested %d kHz but got %d kHz\n",
9552 cdclk, dev_priv->cdclk_freq);
9553}
9554
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009555static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009556{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009557 struct drm_i915_private *dev_priv = to_i915(state->dev);
9558 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009559 int cdclk;
9560
9561 /*
9562 * FIXME should also account for plane ratio
9563 * once 64bpp pixel formats are supported.
9564 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009565 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009566 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009567 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009568 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009569 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009570 cdclk = 450000;
9571 else
9572 cdclk = 337500;
9573
9574 /*
9575 * FIXME move the cdclk caclulation to
9576 * compute_config() so we can fail gracegully.
9577 */
9578 if (cdclk > dev_priv->max_cdclk_freq) {
9579 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9580 cdclk, dev_priv->max_cdclk_freq);
9581 cdclk = dev_priv->max_cdclk_freq;
9582 }
9583
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009584 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009585
9586 return 0;
9587}
9588
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009589static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009590{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009591 struct drm_device *dev = old_state->dev;
9592 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009593
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009594 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009595}
9596
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009597static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9598 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009599{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009600 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009601 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009602
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009603 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009604
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009605 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009606}
9607
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309608static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9609 enum port port,
9610 struct intel_crtc_state *pipe_config)
9611{
9612 switch (port) {
9613 case PORT_A:
9614 pipe_config->ddi_pll_sel = SKL_DPLL0;
9615 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9616 break;
9617 case PORT_B:
9618 pipe_config->ddi_pll_sel = SKL_DPLL1;
9619 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9620 break;
9621 case PORT_C:
9622 pipe_config->ddi_pll_sel = SKL_DPLL2;
9623 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9624 break;
9625 default:
9626 DRM_ERROR("Incorrect port type\n");
9627 }
9628}
9629
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009630static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9631 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009632 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009633{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009634 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009635
9636 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9637 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9638
9639 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009640 case SKL_DPLL0:
9641 /*
9642 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9643 * of the shared DPLL framework and thus needs to be read out
9644 * separately
9645 */
9646 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9647 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9648 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009649 case SKL_DPLL1:
9650 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9651 break;
9652 case SKL_DPLL2:
9653 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9654 break;
9655 case SKL_DPLL3:
9656 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9657 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009658 }
9659}
9660
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009661static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9662 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009663 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009664{
9665 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9666
9667 switch (pipe_config->ddi_pll_sel) {
9668 case PORT_CLK_SEL_WRPLL1:
9669 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9670 break;
9671 case PORT_CLK_SEL_WRPLL2:
9672 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9673 break;
9674 }
9675}
9676
Daniel Vetter26804af2014-06-25 22:01:55 +03009677static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009678 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009679{
9680 struct drm_device *dev = crtc->base.dev;
9681 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009682 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009683 enum port port;
9684 uint32_t tmp;
9685
9686 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9687
9688 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9689
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009690 if (IS_SKYLAKE(dev))
9691 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309692 else if (IS_BROXTON(dev))
9693 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009694 else
9695 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009696
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009697 if (pipe_config->shared_dpll >= 0) {
9698 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9699
9700 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9701 &pipe_config->dpll_hw_state));
9702 }
9703
Daniel Vetter26804af2014-06-25 22:01:55 +03009704 /*
9705 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9706 * DDI E. So just check whether this pipe is wired to DDI E and whether
9707 * the PCH transcoder is on.
9708 */
Damien Lespiauca370452013-12-03 13:56:24 +00009709 if (INTEL_INFO(dev)->gen < 9 &&
9710 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009711 pipe_config->has_pch_encoder = true;
9712
9713 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9714 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9715 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9716
9717 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9718 }
9719}
9720
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009721static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009722 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009723{
9724 struct drm_device *dev = crtc->base.dev;
9725 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009726 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009727 uint32_t tmp;
9728
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009729 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009730 POWER_DOMAIN_PIPE(crtc->pipe)))
9731 return false;
9732
Daniel Vettere143a212013-07-04 12:01:15 +02009733 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009734 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9735
Daniel Vettereccb1402013-05-22 00:50:22 +02009736 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9737 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9738 enum pipe trans_edp_pipe;
9739 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9740 default:
9741 WARN(1, "unknown pipe linked to edp transcoder\n");
9742 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9743 case TRANS_DDI_EDP_INPUT_A_ON:
9744 trans_edp_pipe = PIPE_A;
9745 break;
9746 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9747 trans_edp_pipe = PIPE_B;
9748 break;
9749 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9750 trans_edp_pipe = PIPE_C;
9751 break;
9752 }
9753
9754 if (trans_edp_pipe == crtc->pipe)
9755 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9756 }
9757
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009758 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009759 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009760 return false;
9761
Daniel Vettereccb1402013-05-22 00:50:22 +02009762 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009763 if (!(tmp & PIPECONF_ENABLE))
9764 return false;
9765
Daniel Vetter26804af2014-06-25 22:01:55 +03009766 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009767
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009768 intel_get_pipe_timings(crtc, pipe_config);
9769
Chandra Kondurua1b22782015-04-07 15:28:45 -07009770 if (INTEL_INFO(dev)->gen >= 9) {
9771 skl_init_scalers(dev, crtc, pipe_config);
9772 }
9773
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009774 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009775
9776 if (INTEL_INFO(dev)->gen >= 9) {
9777 pipe_config->scaler_state.scaler_id = -1;
9778 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9779 }
9780
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009781 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009782 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009783 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009784 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009785 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009786 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009787
Jesse Barnese59150d2014-01-07 13:30:45 -08009788 if (IS_HASWELL(dev))
9789 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9790 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009791
Clint Taylorebb69c92014-09-30 10:30:22 -07009792 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9793 pipe_config->pixel_multiplier =
9794 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9795 } else {
9796 pipe_config->pixel_multiplier = 1;
9797 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009798
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009799 return true;
9800}
9801
Chris Wilson560b85b2010-08-07 11:01:38 +01009802static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9803{
9804 struct drm_device *dev = crtc->dev;
9805 struct drm_i915_private *dev_priv = dev->dev_private;
9806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009807 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009808
Ville Syrjälädc41c152014-08-13 11:57:05 +03009809 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009810 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9811 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009812 unsigned int stride = roundup_pow_of_two(width) * 4;
9813
9814 switch (stride) {
9815 default:
9816 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9817 width, stride);
9818 stride = 256;
9819 /* fallthrough */
9820 case 256:
9821 case 512:
9822 case 1024:
9823 case 2048:
9824 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009825 }
9826
Ville Syrjälädc41c152014-08-13 11:57:05 +03009827 cntl |= CURSOR_ENABLE |
9828 CURSOR_GAMMA_ENABLE |
9829 CURSOR_FORMAT_ARGB |
9830 CURSOR_STRIDE(stride);
9831
9832 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009833 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009834
Ville Syrjälädc41c152014-08-13 11:57:05 +03009835 if (intel_crtc->cursor_cntl != 0 &&
9836 (intel_crtc->cursor_base != base ||
9837 intel_crtc->cursor_size != size ||
9838 intel_crtc->cursor_cntl != cntl)) {
9839 /* On these chipsets we can only modify the base/size/stride
9840 * whilst the cursor is disabled.
9841 */
9842 I915_WRITE(_CURACNTR, 0);
9843 POSTING_READ(_CURACNTR);
9844 intel_crtc->cursor_cntl = 0;
9845 }
9846
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009847 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009848 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009849 intel_crtc->cursor_base = base;
9850 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009851
9852 if (intel_crtc->cursor_size != size) {
9853 I915_WRITE(CURSIZE, size);
9854 intel_crtc->cursor_size = size;
9855 }
9856
Chris Wilson4b0e3332014-05-30 16:35:26 +03009857 if (intel_crtc->cursor_cntl != cntl) {
9858 I915_WRITE(_CURACNTR, cntl);
9859 POSTING_READ(_CURACNTR);
9860 intel_crtc->cursor_cntl = cntl;
9861 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009862}
9863
9864static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9865{
9866 struct drm_device *dev = crtc->dev;
9867 struct drm_i915_private *dev_priv = dev->dev_private;
9868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9869 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009870 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009871
Chris Wilson4b0e3332014-05-30 16:35:26 +03009872 cntl = 0;
9873 if (base) {
9874 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009875 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309876 case 64:
9877 cntl |= CURSOR_MODE_64_ARGB_AX;
9878 break;
9879 case 128:
9880 cntl |= CURSOR_MODE_128_ARGB_AX;
9881 break;
9882 case 256:
9883 cntl |= CURSOR_MODE_256_ARGB_AX;
9884 break;
9885 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009886 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309887 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009888 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009889 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009890
9891 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9892 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009893 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009894
Matt Roper8e7d6882015-01-21 16:35:41 -08009895 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009896 cntl |= CURSOR_ROTATE_180;
9897
Chris Wilson4b0e3332014-05-30 16:35:26 +03009898 if (intel_crtc->cursor_cntl != cntl) {
9899 I915_WRITE(CURCNTR(pipe), cntl);
9900 POSTING_READ(CURCNTR(pipe));
9901 intel_crtc->cursor_cntl = cntl;
9902 }
9903
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009904 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009905 I915_WRITE(CURBASE(pipe), base);
9906 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009907
9908 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009909}
9910
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009911/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009912static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9913 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009914{
9915 struct drm_device *dev = crtc->dev;
9916 struct drm_i915_private *dev_priv = dev->dev_private;
9917 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9918 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009919 int x = crtc->cursor_x;
9920 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009921 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009922
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009923 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009924 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009925
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009926 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009927 base = 0;
9928
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009929 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009930 base = 0;
9931
9932 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009933 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009934 base = 0;
9935
9936 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9937 x = -x;
9938 }
9939 pos |= x << CURSOR_X_SHIFT;
9940
9941 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009942 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009943 base = 0;
9944
9945 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9946 y = -y;
9947 }
9948 pos |= y << CURSOR_Y_SHIFT;
9949
Chris Wilson4b0e3332014-05-30 16:35:26 +03009950 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009951 return;
9952
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009953 I915_WRITE(CURPOS(pipe), pos);
9954
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009955 /* ILK+ do this automagically */
9956 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009957 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009958 base += (intel_crtc->base.cursor->state->crtc_h *
9959 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009960 }
9961
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009962 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009963 i845_update_cursor(crtc, base);
9964 else
9965 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009966}
9967
Ville Syrjälädc41c152014-08-13 11:57:05 +03009968static bool cursor_size_ok(struct drm_device *dev,
9969 uint32_t width, uint32_t height)
9970{
9971 if (width == 0 || height == 0)
9972 return false;
9973
9974 /*
9975 * 845g/865g are special in that they are only limited by
9976 * the width of their cursors, the height is arbitrary up to
9977 * the precision of the register. Everything else requires
9978 * square cursors, limited to a few power-of-two sizes.
9979 */
9980 if (IS_845G(dev) || IS_I865G(dev)) {
9981 if ((width & 63) != 0)
9982 return false;
9983
9984 if (width > (IS_845G(dev) ? 64 : 512))
9985 return false;
9986
9987 if (height > 1023)
9988 return false;
9989 } else {
9990 switch (width | height) {
9991 case 256:
9992 case 128:
9993 if (IS_GEN2(dev))
9994 return false;
9995 case 64:
9996 break;
9997 default:
9998 return false;
9999 }
10000 }
10001
10002 return true;
10003}
10004
Jesse Barnes79e53942008-11-07 14:24:08 -080010005static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010006 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010007{
James Simmons72034252010-08-03 01:33:19 +010010008 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010010
James Simmons72034252010-08-03 01:33:19 +010010011 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010012 intel_crtc->lut_r[i] = red[i] >> 8;
10013 intel_crtc->lut_g[i] = green[i] >> 8;
10014 intel_crtc->lut_b[i] = blue[i] >> 8;
10015 }
10016
10017 intel_crtc_load_lut(crtc);
10018}
10019
Jesse Barnes79e53942008-11-07 14:24:08 -080010020/* VESA 640x480x72Hz mode to set on the pipe */
10021static struct drm_display_mode load_detect_mode = {
10022 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10023 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10024};
10025
Daniel Vettera8bb6812014-02-10 18:00:39 +010010026struct drm_framebuffer *
10027__intel_framebuffer_create(struct drm_device *dev,
10028 struct drm_mode_fb_cmd2 *mode_cmd,
10029 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010030{
10031 struct intel_framebuffer *intel_fb;
10032 int ret;
10033
10034 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10035 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010036 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010037 return ERR_PTR(-ENOMEM);
10038 }
10039
10040 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010041 if (ret)
10042 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010043
10044 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010045err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010046 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010047 kfree(intel_fb);
10048
10049 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010050}
10051
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010052static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010053intel_framebuffer_create(struct drm_device *dev,
10054 struct drm_mode_fb_cmd2 *mode_cmd,
10055 struct drm_i915_gem_object *obj)
10056{
10057 struct drm_framebuffer *fb;
10058 int ret;
10059
10060 ret = i915_mutex_lock_interruptible(dev);
10061 if (ret)
10062 return ERR_PTR(ret);
10063 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10064 mutex_unlock(&dev->struct_mutex);
10065
10066 return fb;
10067}
10068
Chris Wilsond2dff872011-04-19 08:36:26 +010010069static u32
10070intel_framebuffer_pitch_for_width(int width, int bpp)
10071{
10072 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10073 return ALIGN(pitch, 64);
10074}
10075
10076static u32
10077intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10078{
10079 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010080 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010081}
10082
10083static struct drm_framebuffer *
10084intel_framebuffer_create_for_mode(struct drm_device *dev,
10085 struct drm_display_mode *mode,
10086 int depth, int bpp)
10087{
10088 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010089 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010090
10091 obj = i915_gem_alloc_object(dev,
10092 intel_framebuffer_size_for_mode(mode, bpp));
10093 if (obj == NULL)
10094 return ERR_PTR(-ENOMEM);
10095
10096 mode_cmd.width = mode->hdisplay;
10097 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010098 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10099 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010100 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010101
10102 return intel_framebuffer_create(dev, &mode_cmd, obj);
10103}
10104
10105static struct drm_framebuffer *
10106mode_fits_in_fbdev(struct drm_device *dev,
10107 struct drm_display_mode *mode)
10108{
Daniel Vetter06957262015-08-10 13:34:08 +020010109#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010110 struct drm_i915_private *dev_priv = dev->dev_private;
10111 struct drm_i915_gem_object *obj;
10112 struct drm_framebuffer *fb;
10113
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010114 if (!dev_priv->fbdev)
10115 return NULL;
10116
10117 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010118 return NULL;
10119
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010120 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010121 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010122
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010123 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010124 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10125 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010126 return NULL;
10127
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010128 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010129 return NULL;
10130
10131 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010132#else
10133 return NULL;
10134#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010135}
10136
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010137static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10138 struct drm_crtc *crtc,
10139 struct drm_display_mode *mode,
10140 struct drm_framebuffer *fb,
10141 int x, int y)
10142{
10143 struct drm_plane_state *plane_state;
10144 int hdisplay, vdisplay;
10145 int ret;
10146
10147 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10148 if (IS_ERR(plane_state))
10149 return PTR_ERR(plane_state);
10150
10151 if (mode)
10152 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10153 else
10154 hdisplay = vdisplay = 0;
10155
10156 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10157 if (ret)
10158 return ret;
10159 drm_atomic_set_fb_for_plane(plane_state, fb);
10160 plane_state->crtc_x = 0;
10161 plane_state->crtc_y = 0;
10162 plane_state->crtc_w = hdisplay;
10163 plane_state->crtc_h = vdisplay;
10164 plane_state->src_x = x << 16;
10165 plane_state->src_y = y << 16;
10166 plane_state->src_w = hdisplay << 16;
10167 plane_state->src_h = vdisplay << 16;
10168
10169 return 0;
10170}
10171
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010172bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010173 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010174 struct intel_load_detect_pipe *old,
10175 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010176{
10177 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010178 struct intel_encoder *intel_encoder =
10179 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010180 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010181 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010182 struct drm_crtc *crtc = NULL;
10183 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010184 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010185 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010186 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010187 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010188 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010189 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010190
Chris Wilsond2dff872011-04-19 08:36:26 +010010191 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010192 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010193 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010194
Rob Clark51fd3712013-11-19 12:10:12 -050010195retry:
10196 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10197 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010198 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010199
Jesse Barnes79e53942008-11-07 14:24:08 -080010200 /*
10201 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010202 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010203 * - if the connector already has an assigned crtc, use it (but make
10204 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010205 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010206 * - try to find the first unused crtc that can drive this connector,
10207 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010208 */
10209
10210 /* See if we already have a CRTC for this connector */
10211 if (encoder->crtc) {
10212 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010213
Rob Clark51fd3712013-11-19 12:10:12 -050010214 ret = drm_modeset_lock(&crtc->mutex, ctx);
10215 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010216 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010217 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10218 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010219 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010220
Daniel Vetter24218aa2012-08-12 19:27:11 +020010221 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010222 old->load_detect_temp = false;
10223
10224 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010225 if (connector->dpms != DRM_MODE_DPMS_ON)
10226 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010227
Chris Wilson71731882011-04-19 23:10:58 +010010228 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010229 }
10230
10231 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010232 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010233 i++;
10234 if (!(encoder->possible_crtcs & (1 << i)))
10235 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010236 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010237 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010238
10239 crtc = possible_crtc;
10240 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010241 }
10242
10243 /*
10244 * If we didn't find an unused CRTC, don't use any.
10245 */
10246 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010247 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010248 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010249 }
10250
Rob Clark51fd3712013-11-19 12:10:12 -050010251 ret = drm_modeset_lock(&crtc->mutex, ctx);
10252 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010253 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010254 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10255 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010256 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010257
10258 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010259 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010260 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010261 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010262
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010263 state = drm_atomic_state_alloc(dev);
10264 if (!state)
10265 return false;
10266
10267 state->acquire_ctx = ctx;
10268
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010269 connector_state = drm_atomic_get_connector_state(state, connector);
10270 if (IS_ERR(connector_state)) {
10271 ret = PTR_ERR(connector_state);
10272 goto fail;
10273 }
10274
10275 connector_state->crtc = crtc;
10276 connector_state->best_encoder = &intel_encoder->base;
10277
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010278 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10279 if (IS_ERR(crtc_state)) {
10280 ret = PTR_ERR(crtc_state);
10281 goto fail;
10282 }
10283
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010284 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010285
Chris Wilson64927112011-04-20 07:25:26 +010010286 if (!mode)
10287 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010288
Chris Wilsond2dff872011-04-19 08:36:26 +010010289 /* We need a framebuffer large enough to accommodate all accesses
10290 * that the plane may generate whilst we perform load detection.
10291 * We can not rely on the fbcon either being present (we get called
10292 * during its initialisation to detect all boot displays, or it may
10293 * not even exist) or that it is large enough to satisfy the
10294 * requested mode.
10295 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010296 fb = mode_fits_in_fbdev(dev, mode);
10297 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010298 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010299 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10300 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010301 } else
10302 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010303 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010304 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010305 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010306 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010307
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010308 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10309 if (ret)
10310 goto fail;
10311
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010312 drm_mode_copy(&crtc_state->base.mode, mode);
10313
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010314 if (drm_atomic_commit(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010315 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010316 if (old->release_fb)
10317 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010318 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010319 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010320 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010321
Jesse Barnes79e53942008-11-07 14:24:08 -080010322 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010323 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010324 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010325
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010326fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010327 drm_atomic_state_free(state);
10328 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010329
Rob Clark51fd3712013-11-19 12:10:12 -050010330 if (ret == -EDEADLK) {
10331 drm_modeset_backoff(ctx);
10332 goto retry;
10333 }
10334
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010335 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010336}
10337
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010338void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010339 struct intel_load_detect_pipe *old,
10340 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010341{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010342 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010343 struct intel_encoder *intel_encoder =
10344 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010345 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010346 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010347 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010348 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010349 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010350 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010351 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010352
Chris Wilsond2dff872011-04-19 08:36:26 +010010353 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010354 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010355 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010356
Chris Wilson8261b192011-04-19 23:18:09 +010010357 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010358 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010359 if (!state)
10360 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010361
10362 state->acquire_ctx = ctx;
10363
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010364 connector_state = drm_atomic_get_connector_state(state, connector);
10365 if (IS_ERR(connector_state))
10366 goto fail;
10367
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010368 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10369 if (IS_ERR(crtc_state))
10370 goto fail;
10371
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010372 connector_state->best_encoder = NULL;
10373 connector_state->crtc = NULL;
10374
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010375 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010376
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010377 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10378 0, 0);
10379 if (ret)
10380 goto fail;
10381
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010382 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010383 if (ret)
10384 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010385
Daniel Vetter36206362012-12-10 20:42:17 +010010386 if (old->release_fb) {
10387 drm_framebuffer_unregister_private(old->release_fb);
10388 drm_framebuffer_unreference(old->release_fb);
10389 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010390
Chris Wilson0622a532011-04-21 09:32:11 +010010391 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010392 }
10393
Eric Anholtc751ce42010-03-25 11:48:48 -070010394 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010395 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10396 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010397
10398 return;
10399fail:
10400 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10401 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010402}
10403
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010404static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010405 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010406{
10407 struct drm_i915_private *dev_priv = dev->dev_private;
10408 u32 dpll = pipe_config->dpll_hw_state.dpll;
10409
10410 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010411 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010412 else if (HAS_PCH_SPLIT(dev))
10413 return 120000;
10414 else if (!IS_GEN2(dev))
10415 return 96000;
10416 else
10417 return 48000;
10418}
10419
Jesse Barnes79e53942008-11-07 14:24:08 -080010420/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010421static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010422 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010423{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010424 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010425 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010426 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010427 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010428 u32 fp;
10429 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010430 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010431 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010432
10433 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010434 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010435 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010436 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010437
10438 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010439 if (IS_PINEVIEW(dev)) {
10440 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10441 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010442 } else {
10443 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10444 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10445 }
10446
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010447 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010448 if (IS_PINEVIEW(dev))
10449 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10450 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010451 else
10452 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010453 DPLL_FPA01_P1_POST_DIV_SHIFT);
10454
10455 switch (dpll & DPLL_MODE_MASK) {
10456 case DPLLB_MODE_DAC_SERIAL:
10457 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10458 5 : 10;
10459 break;
10460 case DPLLB_MODE_LVDS:
10461 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10462 7 : 14;
10463 break;
10464 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010465 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010466 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010467 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010468 }
10469
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010470 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010471 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010472 else
Imre Deakdccbea32015-06-22 23:35:51 +030010473 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010474 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010475 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010476 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010477
10478 if (is_lvds) {
10479 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10480 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010481
10482 if (lvds & LVDS_CLKB_POWER_UP)
10483 clock.p2 = 7;
10484 else
10485 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010486 } else {
10487 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10488 clock.p1 = 2;
10489 else {
10490 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10491 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10492 }
10493 if (dpll & PLL_P2_DIVIDE_BY_4)
10494 clock.p2 = 4;
10495 else
10496 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010497 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010498
Imre Deakdccbea32015-06-22 23:35:51 +030010499 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010500 }
10501
Ville Syrjälä18442d02013-09-13 16:00:08 +030010502 /*
10503 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010504 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010505 * encoder's get_config() function.
10506 */
Imre Deakdccbea32015-06-22 23:35:51 +030010507 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010508}
10509
Ville Syrjälä6878da02013-09-13 15:59:11 +030010510int intel_dotclock_calculate(int link_freq,
10511 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010512{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010513 /*
10514 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010515 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010516 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010517 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010518 *
10519 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010520 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010521 */
10522
Ville Syrjälä6878da02013-09-13 15:59:11 +030010523 if (!m_n->link_n)
10524 return 0;
10525
10526 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10527}
10528
Ville Syrjälä18442d02013-09-13 16:00:08 +030010529static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010530 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010531{
10532 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010533
10534 /* read out port_clock from the DPLL */
10535 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010536
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010537 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010538 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010539 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010540 * agree once we know their relationship in the encoder's
10541 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010542 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010543 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010544 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10545 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010546}
10547
10548/** Returns the currently programmed mode of the given pipe. */
10549struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10550 struct drm_crtc *crtc)
10551{
Jesse Barnes548f2452011-02-17 10:40:53 -080010552 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010553 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010554 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010555 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010556 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010557 int htot = I915_READ(HTOTAL(cpu_transcoder));
10558 int hsync = I915_READ(HSYNC(cpu_transcoder));
10559 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10560 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010561 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010562
10563 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10564 if (!mode)
10565 return NULL;
10566
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010567 /*
10568 * Construct a pipe_config sufficient for getting the clock info
10569 * back out of crtc_clock_get.
10570 *
10571 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10572 * to use a real value here instead.
10573 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010574 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010575 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010576 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10577 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10578 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010579 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10580
Ville Syrjälä773ae032013-09-23 17:48:20 +030010581 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010582 mode->hdisplay = (htot & 0xffff) + 1;
10583 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10584 mode->hsync_start = (hsync & 0xffff) + 1;
10585 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10586 mode->vdisplay = (vtot & 0xffff) + 1;
10587 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10588 mode->vsync_start = (vsync & 0xffff) + 1;
10589 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10590
10591 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010592
10593 return mode;
10594}
10595
Chris Wilsonf047e392012-07-21 12:31:41 +010010596void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010597{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010598 struct drm_i915_private *dev_priv = dev->dev_private;
10599
Chris Wilsonf62a0072014-02-21 17:55:39 +000010600 if (dev_priv->mm.busy)
10601 return;
10602
Paulo Zanoni43694d62014-03-07 20:08:08 -030010603 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010604 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010605 if (INTEL_INFO(dev)->gen >= 6)
10606 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010607 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010608}
10609
10610void intel_mark_idle(struct drm_device *dev)
10611{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010612 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010613
Chris Wilsonf62a0072014-02-21 17:55:39 +000010614 if (!dev_priv->mm.busy)
10615 return;
10616
10617 dev_priv->mm.busy = false;
10618
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010619 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010620 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010621
Paulo Zanoni43694d62014-03-07 20:08:08 -030010622 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010623}
10624
Jesse Barnes79e53942008-11-07 14:24:08 -080010625static void intel_crtc_destroy(struct drm_crtc *crtc)
10626{
10627 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010628 struct drm_device *dev = crtc->dev;
10629 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010630
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010631 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010632 work = intel_crtc->unpin_work;
10633 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010634 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010635
10636 if (work) {
10637 cancel_work_sync(&work->work);
10638 kfree(work);
10639 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010640
10641 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010642
Jesse Barnes79e53942008-11-07 14:24:08 -080010643 kfree(intel_crtc);
10644}
10645
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010646static void intel_unpin_work_fn(struct work_struct *__work)
10647{
10648 struct intel_unpin_work *work =
10649 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010650 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10651 struct drm_device *dev = crtc->base.dev;
10652 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010653
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010654 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010655 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010656 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010657
John Harrisonf06cc1b2014-11-24 18:49:37 +000010658 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010659 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010660 mutex_unlock(&dev->struct_mutex);
10661
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010662 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010663 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010664
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010665 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10666 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010667
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010668 kfree(work);
10669}
10670
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010671static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010672 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010673{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010674 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10675 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010676 unsigned long flags;
10677
10678 /* Ignore early vblank irqs */
10679 if (intel_crtc == NULL)
10680 return;
10681
Daniel Vetterf3260382014-09-15 14:55:23 +020010682 /*
10683 * This is called both by irq handlers and the reset code (to complete
10684 * lost pageflips) so needs the full irqsave spinlocks.
10685 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010686 spin_lock_irqsave(&dev->event_lock, flags);
10687 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010688
10689 /* Ensure we don't miss a work->pending update ... */
10690 smp_rmb();
10691
10692 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010693 spin_unlock_irqrestore(&dev->event_lock, flags);
10694 return;
10695 }
10696
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010697 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010698
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010699 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010700}
10701
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010702void intel_finish_page_flip(struct drm_device *dev, int pipe)
10703{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010704 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010705 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10706
Mario Kleiner49b14a52010-12-09 07:00:07 +010010707 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010708}
10709
10710void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10711{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010712 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010713 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10714
Mario Kleiner49b14a52010-12-09 07:00:07 +010010715 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010716}
10717
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010718/* Is 'a' after or equal to 'b'? */
10719static bool g4x_flip_count_after_eq(u32 a, u32 b)
10720{
10721 return !((a - b) & 0x80000000);
10722}
10723
10724static bool page_flip_finished(struct intel_crtc *crtc)
10725{
10726 struct drm_device *dev = crtc->base.dev;
10727 struct drm_i915_private *dev_priv = dev->dev_private;
10728
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010729 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10730 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10731 return true;
10732
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010733 /*
10734 * The relevant registers doen't exist on pre-ctg.
10735 * As the flip done interrupt doesn't trigger for mmio
10736 * flips on gmch platforms, a flip count check isn't
10737 * really needed there. But since ctg has the registers,
10738 * include it in the check anyway.
10739 */
10740 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10741 return true;
10742
10743 /*
10744 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10745 * used the same base address. In that case the mmio flip might
10746 * have completed, but the CS hasn't even executed the flip yet.
10747 *
10748 * A flip count check isn't enough as the CS might have updated
10749 * the base address just after start of vblank, but before we
10750 * managed to process the interrupt. This means we'd complete the
10751 * CS flip too soon.
10752 *
10753 * Combining both checks should get us a good enough result. It may
10754 * still happen that the CS flip has been executed, but has not
10755 * yet actually completed. But in case the base address is the same
10756 * anyway, we don't really care.
10757 */
10758 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10759 crtc->unpin_work->gtt_offset &&
10760 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10761 crtc->unpin_work->flip_count);
10762}
10763
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010764void intel_prepare_page_flip(struct drm_device *dev, int plane)
10765{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010766 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010767 struct intel_crtc *intel_crtc =
10768 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10769 unsigned long flags;
10770
Daniel Vetterf3260382014-09-15 14:55:23 +020010771
10772 /*
10773 * This is called both by irq handlers and the reset code (to complete
10774 * lost pageflips) so needs the full irqsave spinlocks.
10775 *
10776 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010777 * generate a page-flip completion irq, i.e. every modeset
10778 * is also accompanied by a spurious intel_prepare_page_flip().
10779 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010780 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010781 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010782 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010783 spin_unlock_irqrestore(&dev->event_lock, flags);
10784}
10785
Robin Schroereba905b2014-05-18 02:24:50 +020010786static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010787{
10788 /* Ensure that the work item is consistent when activating it ... */
10789 smp_wmb();
10790 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10791 /* and that it is marked active as soon as the irq could fire. */
10792 smp_wmb();
10793}
10794
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010795static int intel_gen2_queue_flip(struct drm_device *dev,
10796 struct drm_crtc *crtc,
10797 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010798 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010799 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010800 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010801{
John Harrison6258fbe2015-05-29 17:43:48 +010010802 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010804 u32 flip_mask;
10805 int ret;
10806
John Harrison5fb9de12015-05-29 17:44:07 +010010807 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010808 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010809 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010810
10811 /* Can't queue multiple flips, so wait for the previous
10812 * one to finish before executing the next.
10813 */
10814 if (intel_crtc->plane)
10815 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10816 else
10817 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010818 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10819 intel_ring_emit(ring, MI_NOOP);
10820 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10821 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10822 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010823 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010824 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010825
10826 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010827 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010828}
10829
10830static int intel_gen3_queue_flip(struct drm_device *dev,
10831 struct drm_crtc *crtc,
10832 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010833 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010834 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010835 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010836{
John Harrison6258fbe2015-05-29 17:43:48 +010010837 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010839 u32 flip_mask;
10840 int ret;
10841
John Harrison5fb9de12015-05-29 17:44:07 +010010842 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010843 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010844 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010845
10846 if (intel_crtc->plane)
10847 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10848 else
10849 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010850 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10851 intel_ring_emit(ring, MI_NOOP);
10852 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10853 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10854 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010855 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010856 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010857
Chris Wilsone7d841c2012-12-03 11:36:30 +000010858 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010859 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010860}
10861
10862static int intel_gen4_queue_flip(struct drm_device *dev,
10863 struct drm_crtc *crtc,
10864 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010865 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010866 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010867 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010868{
John Harrison6258fbe2015-05-29 17:43:48 +010010869 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010870 struct drm_i915_private *dev_priv = dev->dev_private;
10871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10872 uint32_t pf, pipesrc;
10873 int ret;
10874
John Harrison5fb9de12015-05-29 17:44:07 +010010875 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010876 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010877 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010878
10879 /* i965+ uses the linear or tiled offsets from the
10880 * Display Registers (which do not change across a page-flip)
10881 * so we need only reprogram the base address.
10882 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010883 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10884 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10885 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010886 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010887 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010888
10889 /* XXX Enabling the panel-fitter across page-flip is so far
10890 * untested on non-native modes, so ignore it for now.
10891 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10892 */
10893 pf = 0;
10894 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010895 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010896
10897 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010898 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010899}
10900
10901static int intel_gen6_queue_flip(struct drm_device *dev,
10902 struct drm_crtc *crtc,
10903 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010904 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010905 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010906 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010907{
John Harrison6258fbe2015-05-29 17:43:48 +010010908 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010909 struct drm_i915_private *dev_priv = dev->dev_private;
10910 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10911 uint32_t pf, pipesrc;
10912 int ret;
10913
John Harrison5fb9de12015-05-29 17:44:07 +010010914 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010915 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010916 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010917
Daniel Vetter6d90c952012-04-26 23:28:05 +020010918 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10919 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10920 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010921 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010922
Chris Wilson99d9acd2012-04-17 20:37:00 +010010923 /* Contrary to the suggestions in the documentation,
10924 * "Enable Panel Fitter" does not seem to be required when page
10925 * flipping with a non-native mode, and worse causes a normal
10926 * modeset to fail.
10927 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10928 */
10929 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010930 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010931 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010932
10933 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010934 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010935}
10936
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010937static int intel_gen7_queue_flip(struct drm_device *dev,
10938 struct drm_crtc *crtc,
10939 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010940 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010941 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010942 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010943{
John Harrison6258fbe2015-05-29 17:43:48 +010010944 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010945 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010946 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010947 int len, ret;
10948
Robin Schroereba905b2014-05-18 02:24:50 +020010949 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010950 case PLANE_A:
10951 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10952 break;
10953 case PLANE_B:
10954 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10955 break;
10956 case PLANE_C:
10957 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10958 break;
10959 default:
10960 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010961 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010962 }
10963
Chris Wilsonffe74d72013-08-26 20:58:12 +010010964 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010965 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010966 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010967 /*
10968 * On Gen 8, SRM is now taking an extra dword to accommodate
10969 * 48bits addresses, and we need a NOOP for the batch size to
10970 * stay even.
10971 */
10972 if (IS_GEN8(dev))
10973 len += 2;
10974 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010975
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010976 /*
10977 * BSpec MI_DISPLAY_FLIP for IVB:
10978 * "The full packet must be contained within the same cache line."
10979 *
10980 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10981 * cacheline, if we ever start emitting more commands before
10982 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10983 * then do the cacheline alignment, and finally emit the
10984 * MI_DISPLAY_FLIP.
10985 */
John Harrisonbba09b12015-05-29 17:44:06 +010010986 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010987 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010988 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010989
John Harrison5fb9de12015-05-29 17:44:07 +010010990 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010991 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010992 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010993
Chris Wilsonffe74d72013-08-26 20:58:12 +010010994 /* Unmask the flip-done completion message. Note that the bspec says that
10995 * we should do this for both the BCS and RCS, and that we must not unmask
10996 * more than one flip event at any time (or ensure that one flip message
10997 * can be sent by waiting for flip-done prior to queueing new flips).
10998 * Experimentation says that BCS works despite DERRMR masking all
10999 * flip-done completion events and that unmasking all planes at once
11000 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11001 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11002 */
11003 if (ring->id == RCS) {
11004 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11005 intel_ring_emit(ring, DERRMR);
11006 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11007 DERRMR_PIPEB_PRI_FLIP_DONE |
11008 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011009 if (IS_GEN8(dev))
Arun Siluveryf1afe242015-08-04 16:22:20 +010011010 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011011 MI_SRM_LRM_GLOBAL_GTT);
11012 else
Arun Siluveryf1afe242015-08-04 16:22:20 +010011013 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011014 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011015 intel_ring_emit(ring, DERRMR);
11016 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011017 if (IS_GEN8(dev)) {
11018 intel_ring_emit(ring, 0);
11019 intel_ring_emit(ring, MI_NOOP);
11020 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011021 }
11022
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011023 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011024 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011025 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011026 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011027
11028 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011029 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011030}
11031
Sourab Gupta84c33a62014-06-02 16:47:17 +053011032static bool use_mmio_flip(struct intel_engine_cs *ring,
11033 struct drm_i915_gem_object *obj)
11034{
11035 /*
11036 * This is not being used for older platforms, because
11037 * non-availability of flip done interrupt forces us to use
11038 * CS flips. Older platforms derive flip done using some clever
11039 * tricks involving the flip_pending status bits and vblank irqs.
11040 * So using MMIO flips there would disrupt this mechanism.
11041 */
11042
Chris Wilson8e09bf82014-07-08 10:40:30 +010011043 if (ring == NULL)
11044 return true;
11045
Sourab Gupta84c33a62014-06-02 16:47:17 +053011046 if (INTEL_INFO(ring->dev)->gen < 5)
11047 return false;
11048
11049 if (i915.use_mmio_flip < 0)
11050 return false;
11051 else if (i915.use_mmio_flip > 0)
11052 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011053 else if (i915.enable_execlists)
11054 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011055 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011056 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011057}
11058
Damien Lespiauff944562014-11-20 14:58:16 +000011059static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11060{
11061 struct drm_device *dev = intel_crtc->base.dev;
11062 struct drm_i915_private *dev_priv = dev->dev_private;
11063 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011064 const enum pipe pipe = intel_crtc->pipe;
11065 u32 ctl, stride;
11066
11067 ctl = I915_READ(PLANE_CTL(pipe, 0));
11068 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011069 switch (fb->modifier[0]) {
11070 case DRM_FORMAT_MOD_NONE:
11071 break;
11072 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011073 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011074 break;
11075 case I915_FORMAT_MOD_Y_TILED:
11076 ctl |= PLANE_CTL_TILED_Y;
11077 break;
11078 case I915_FORMAT_MOD_Yf_TILED:
11079 ctl |= PLANE_CTL_TILED_YF;
11080 break;
11081 default:
11082 MISSING_CASE(fb->modifier[0]);
11083 }
Damien Lespiauff944562014-11-20 14:58:16 +000011084
11085 /*
11086 * The stride is either expressed as a multiple of 64 bytes chunks for
11087 * linear buffers or in number of tiles for tiled buffers.
11088 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011089 stride = fb->pitches[0] /
11090 intel_fb_stride_alignment(dev, fb->modifier[0],
11091 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011092
11093 /*
11094 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11095 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11096 */
11097 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11098 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11099
11100 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11101 POSTING_READ(PLANE_SURF(pipe, 0));
11102}
11103
11104static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011105{
11106 struct drm_device *dev = intel_crtc->base.dev;
11107 struct drm_i915_private *dev_priv = dev->dev_private;
11108 struct intel_framebuffer *intel_fb =
11109 to_intel_framebuffer(intel_crtc->base.primary->fb);
11110 struct drm_i915_gem_object *obj = intel_fb->obj;
11111 u32 dspcntr;
11112 u32 reg;
11113
Sourab Gupta84c33a62014-06-02 16:47:17 +053011114 reg = DSPCNTR(intel_crtc->plane);
11115 dspcntr = I915_READ(reg);
11116
Damien Lespiauc5d97472014-10-25 00:11:11 +010011117 if (obj->tiling_mode != I915_TILING_NONE)
11118 dspcntr |= DISPPLANE_TILED;
11119 else
11120 dspcntr &= ~DISPPLANE_TILED;
11121
Sourab Gupta84c33a62014-06-02 16:47:17 +053011122 I915_WRITE(reg, dspcntr);
11123
11124 I915_WRITE(DSPSURF(intel_crtc->plane),
11125 intel_crtc->unpin_work->gtt_offset);
11126 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011127
Damien Lespiauff944562014-11-20 14:58:16 +000011128}
11129
11130/*
11131 * XXX: This is the temporary way to update the plane registers until we get
11132 * around to using the usual plane update functions for MMIO flips
11133 */
11134static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11135{
11136 struct drm_device *dev = intel_crtc->base.dev;
Damien Lespiauff944562014-11-20 14:58:16 +000011137
11138 intel_mark_page_flip_active(intel_crtc);
11139
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020011140 intel_pipe_update_start(intel_crtc);
Damien Lespiauff944562014-11-20 14:58:16 +000011141
11142 if (INTEL_INFO(dev)->gen >= 9)
11143 skl_do_mmio_flip(intel_crtc);
11144 else
11145 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11146 ilk_do_mmio_flip(intel_crtc);
11147
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020011148 intel_pipe_update_end(intel_crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011149}
11150
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011151static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011152{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011153 struct intel_mmio_flip *mmio_flip =
11154 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011155
Daniel Vettereed29a52015-05-21 14:21:25 +020011156 if (mmio_flip->req)
11157 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011158 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011159 false, NULL,
11160 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011161
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011162 intel_do_mmio_flip(mmio_flip->crtc);
11163
Daniel Vettereed29a52015-05-21 14:21:25 +020011164 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011165 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011166}
11167
11168static int intel_queue_mmio_flip(struct drm_device *dev,
11169 struct drm_crtc *crtc,
11170 struct drm_framebuffer *fb,
11171 struct drm_i915_gem_object *obj,
11172 struct intel_engine_cs *ring,
11173 uint32_t flags)
11174{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011175 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011176
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011177 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11178 if (mmio_flip == NULL)
11179 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011180
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011181 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011182 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011183 mmio_flip->crtc = to_intel_crtc(crtc);
11184
11185 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11186 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011187
Sourab Gupta84c33a62014-06-02 16:47:17 +053011188 return 0;
11189}
11190
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011191static int intel_default_queue_flip(struct drm_device *dev,
11192 struct drm_crtc *crtc,
11193 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011194 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011195 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011196 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011197{
11198 return -ENODEV;
11199}
11200
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011201static bool __intel_pageflip_stall_check(struct drm_device *dev,
11202 struct drm_crtc *crtc)
11203{
11204 struct drm_i915_private *dev_priv = dev->dev_private;
11205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11206 struct intel_unpin_work *work = intel_crtc->unpin_work;
11207 u32 addr;
11208
11209 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11210 return true;
11211
Chris Wilson908565c2015-08-12 13:08:22 +010011212 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11213 return false;
11214
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011215 if (!work->enable_stall_check)
11216 return false;
11217
11218 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011219 if (work->flip_queued_req &&
11220 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011221 return false;
11222
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011223 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011224 }
11225
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011226 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011227 return false;
11228
11229 /* Potential stall - if we see that the flip has happened,
11230 * assume a missed interrupt. */
11231 if (INTEL_INFO(dev)->gen >= 4)
11232 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11233 else
11234 addr = I915_READ(DSPADDR(intel_crtc->plane));
11235
11236 /* There is a potential issue here with a false positive after a flip
11237 * to the same address. We could address this by checking for a
11238 * non-incrementing frame counter.
11239 */
11240 return addr == work->gtt_offset;
11241}
11242
11243void intel_check_page_flip(struct drm_device *dev, int pipe)
11244{
11245 struct drm_i915_private *dev_priv = dev->dev_private;
11246 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011248 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011249
Dave Gordon6c51d462015-03-06 15:34:26 +000011250 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011251
11252 if (crtc == NULL)
11253 return;
11254
Daniel Vetterf3260382014-09-15 14:55:23 +020011255 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011256 work = intel_crtc->unpin_work;
11257 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011258 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011259 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011260 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011261 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011262 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011263 if (work != NULL &&
11264 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11265 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011266 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011267}
11268
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011269static int intel_crtc_page_flip(struct drm_crtc *crtc,
11270 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011271 struct drm_pending_vblank_event *event,
11272 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011273{
11274 struct drm_device *dev = crtc->dev;
11275 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011276 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011277 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011278 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011279 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011280 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011281 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011282 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011283 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011284 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011285 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011286
Matt Roper2ff8fde2014-07-08 07:50:07 -070011287 /*
11288 * drm_mode_page_flip_ioctl() should already catch this, but double
11289 * check to be safe. In the future we may enable pageflipping from
11290 * a disabled primary plane.
11291 */
11292 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11293 return -EBUSY;
11294
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011295 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011296 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011297 return -EINVAL;
11298
11299 /*
11300 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11301 * Note that pitch changes could also affect these register.
11302 */
11303 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011304 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11305 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011306 return -EINVAL;
11307
Chris Wilsonf900db42014-02-20 09:26:13 +000011308 if (i915_terminally_wedged(&dev_priv->gpu_error))
11309 goto out_hang;
11310
Daniel Vetterb14c5672013-09-19 12:18:32 +020011311 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011312 if (work == NULL)
11313 return -ENOMEM;
11314
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011315 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011316 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011317 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011318 INIT_WORK(&work->work, intel_unpin_work_fn);
11319
Daniel Vetter87b6b102014-05-15 15:33:46 +020011320 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011321 if (ret)
11322 goto free_work;
11323
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011324 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011325 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011326 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011327 /* Before declaring the flip queue wedged, check if
11328 * the hardware completed the operation behind our backs.
11329 */
11330 if (__intel_pageflip_stall_check(dev, crtc)) {
11331 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11332 page_flip_completed(intel_crtc);
11333 } else {
11334 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011335 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011336
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011337 drm_crtc_vblank_put(crtc);
11338 kfree(work);
11339 return -EBUSY;
11340 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011341 }
11342 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011343 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011344
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011345 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11346 flush_workqueue(dev_priv->wq);
11347
Jesse Barnes75dfca82010-02-10 15:09:44 -080011348 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011349 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011350 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011351
Matt Roperf4510a22014-04-01 15:22:40 -070011352 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011353 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011354
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011355 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011356
Chris Wilson89ed88b2015-02-16 14:31:49 +000011357 ret = i915_mutex_lock_interruptible(dev);
11358 if (ret)
11359 goto cleanup;
11360
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011361 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011362 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011363
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011364 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011365 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011366
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011367 if (IS_VALLEYVIEW(dev)) {
11368 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011369 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011370 /* vlv: DISPLAY_FLIP fails to change tiling */
11371 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011372 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011373 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011374 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011375 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011376 if (ring == NULL || ring->id != RCS)
11377 ring = &dev_priv->ring[BCS];
11378 } else {
11379 ring = &dev_priv->ring[RCS];
11380 }
11381
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011382 mmio_flip = use_mmio_flip(ring, obj);
11383
11384 /* When using CS flips, we want to emit semaphores between rings.
11385 * However, when using mmio flips we will create a task to do the
11386 * synchronisation, so all we want here is to pin the framebuffer
11387 * into the display plane and skip any waits.
11388 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011389 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011390 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011391 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011392 if (ret)
11393 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011394
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011395 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11396 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011397
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011398 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011399 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11400 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011401 if (ret)
11402 goto cleanup_unpin;
11403
John Harrisonf06cc1b2014-11-24 18:49:37 +000011404 i915_gem_request_assign(&work->flip_queued_req,
11405 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011406 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011407 if (!request) {
11408 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11409 if (ret)
11410 goto cleanup_unpin;
11411 }
11412
11413 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011414 page_flip_flags);
11415 if (ret)
11416 goto cleanup_unpin;
11417
John Harrison6258fbe2015-05-29 17:43:48 +010011418 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011419 }
11420
John Harrison91af1272015-06-18 13:14:56 +010011421 if (request)
John Harrison75289872015-05-29 17:43:49 +010011422 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011423
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011424 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011425 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011426
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011427 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011428 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011429 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011430
Paulo Zanoni4e1e26f2015-07-14 16:29:13 -030011431 intel_fbc_disable_crtc(intel_crtc);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011432 intel_frontbuffer_flip_prepare(dev,
11433 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011434
Jesse Barnese5510fa2010-07-01 16:48:37 -070011435 trace_i915_flip_request(intel_crtc->plane, obj);
11436
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011437 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011438
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011439cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011440 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011441cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011442 if (request)
11443 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011444 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011445 mutex_unlock(&dev->struct_mutex);
11446cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011447 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011448 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011449
Chris Wilson89ed88b2015-02-16 14:31:49 +000011450 drm_gem_object_unreference_unlocked(&obj->base);
11451 drm_framebuffer_unreference(work->old_fb);
11452
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011453 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011454 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011455 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011456
Daniel Vetter87b6b102014-05-15 15:33:46 +020011457 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011458free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011459 kfree(work);
11460
Chris Wilsonf900db42014-02-20 09:26:13 +000011461 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011462 struct drm_atomic_state *state;
11463 struct drm_plane_state *plane_state;
11464
Chris Wilsonf900db42014-02-20 09:26:13 +000011465out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011466 state = drm_atomic_state_alloc(dev);
11467 if (!state)
11468 return -ENOMEM;
11469 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11470
11471retry:
11472 plane_state = drm_atomic_get_plane_state(state, primary);
11473 ret = PTR_ERR_OR_ZERO(plane_state);
11474 if (!ret) {
11475 drm_atomic_set_fb_for_plane(plane_state, fb);
11476
11477 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11478 if (!ret)
11479 ret = drm_atomic_commit(state);
11480 }
11481
11482 if (ret == -EDEADLK) {
11483 drm_modeset_backoff(state->acquire_ctx);
11484 drm_atomic_state_clear(state);
11485 goto retry;
11486 }
11487
11488 if (ret)
11489 drm_atomic_state_free(state);
11490
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011491 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011492 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011493 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011494 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011495 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011496 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011497 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011498}
11499
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011500
11501/**
11502 * intel_wm_need_update - Check whether watermarks need updating
11503 * @plane: drm plane
11504 * @state: new plane state
11505 *
11506 * Check current plane state versus the new one to determine whether
11507 * watermarks need to be recalculated.
11508 *
11509 * Returns true or false.
11510 */
11511static bool intel_wm_need_update(struct drm_plane *plane,
11512 struct drm_plane_state *state)
11513{
11514 /* Update watermarks on tiling changes. */
11515 if (!plane->state->fb || !state->fb ||
11516 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11517 plane->state->rotation != state->rotation)
11518 return true;
11519
11520 if (plane->state->crtc_w != state->crtc_w)
11521 return true;
11522
11523 return false;
11524}
11525
11526int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11527 struct drm_plane_state *plane_state)
11528{
11529 struct drm_crtc *crtc = crtc_state->crtc;
11530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11531 struct drm_plane *plane = plane_state->plane;
11532 struct drm_device *dev = crtc->dev;
11533 struct drm_i915_private *dev_priv = dev->dev_private;
11534 struct intel_plane_state *old_plane_state =
11535 to_intel_plane_state(plane->state);
11536 int idx = intel_crtc->base.base.id, ret;
11537 int i = drm_plane_index(plane);
11538 bool mode_changed = needs_modeset(crtc_state);
11539 bool was_crtc_enabled = crtc->state->active;
11540 bool is_crtc_enabled = crtc_state->active;
11541
11542 bool turn_off, turn_on, visible, was_visible;
11543 struct drm_framebuffer *fb = plane_state->fb;
11544
11545 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11546 plane->type != DRM_PLANE_TYPE_CURSOR) {
11547 ret = skl_update_scaler_plane(
11548 to_intel_crtc_state(crtc_state),
11549 to_intel_plane_state(plane_state));
11550 if (ret)
11551 return ret;
11552 }
11553
11554 /*
11555 * Disabling a plane is always okay; we just need to update
11556 * fb tracking in a special way since cleanup_fb() won't
11557 * get called by the plane helpers.
11558 */
11559 if (old_plane_state->base.fb && !fb)
11560 intel_crtc->atomic.disabled_planes |= 1 << i;
11561
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011562 was_visible = old_plane_state->visible;
11563 visible = to_intel_plane_state(plane_state)->visible;
11564
11565 if (!was_crtc_enabled && WARN_ON(was_visible))
11566 was_visible = false;
11567
11568 if (!is_crtc_enabled && WARN_ON(visible))
11569 visible = false;
11570
11571 if (!was_visible && !visible)
11572 return 0;
11573
11574 turn_off = was_visible && (!visible || mode_changed);
11575 turn_on = visible && (!was_visible || mode_changed);
11576
11577 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11578 plane->base.id, fb ? fb->base.id : -1);
11579
11580 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11581 plane->base.id, was_visible, visible,
11582 turn_off, turn_on, mode_changed);
11583
Ville Syrjälä852eb002015-06-24 22:00:07 +030011584 if (turn_on) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011585 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011586 /* must disable cxsr around plane enable/disable */
11587 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11588 intel_crtc->atomic.disable_cxsr = true;
11589 /* to potentially re-enable cxsr */
11590 intel_crtc->atomic.wait_vblank = true;
11591 intel_crtc->atomic.update_wm_post = true;
11592 }
11593 } else if (turn_off) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011594 intel_crtc->atomic.update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011595 /* must disable cxsr around plane enable/disable */
11596 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11597 if (is_crtc_enabled)
11598 intel_crtc->atomic.wait_vblank = true;
11599 intel_crtc->atomic.disable_cxsr = true;
11600 }
11601 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011602 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011603 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011604
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011605 if (visible || was_visible)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011606 intel_crtc->atomic.fb_bits |=
11607 to_intel_plane(plane)->frontbuffer_bit;
11608
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011609 switch (plane->type) {
11610 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011611 intel_crtc->atomic.wait_for_flips = true;
11612 intel_crtc->atomic.pre_disable_primary = turn_off;
11613 intel_crtc->atomic.post_enable_primary = turn_on;
11614
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011615 if (turn_off) {
11616 /*
11617 * FIXME: Actually if we will still have any other
11618 * plane enabled on the pipe we could let IPS enabled
11619 * still, but for now lets consider that when we make
11620 * primary invisible by setting DSPCNTR to 0 on
11621 * update_primary_plane function IPS needs to be
11622 * disable.
11623 */
11624 intel_crtc->atomic.disable_ips = true;
11625
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011626 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011627 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011628
11629 /*
11630 * FBC does not work on some platforms for rotated
11631 * planes, so disable it when rotation is not 0 and
11632 * update it when rotation is set back to 0.
11633 *
11634 * FIXME: This is redundant with the fbc update done in
11635 * the primary plane enable function except that that
11636 * one is done too late. We eventually need to unify
11637 * this.
11638 */
11639
11640 if (visible &&
11641 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11642 dev_priv->fbc.crtc == intel_crtc &&
11643 plane_state->rotation != BIT(DRM_ROTATE_0))
11644 intel_crtc->atomic.disable_fbc = true;
11645
11646 /*
11647 * BDW signals flip done immediately if the plane
11648 * is disabled, even if the plane enable is already
11649 * armed to occur at the next vblank :(
11650 */
11651 if (turn_on && IS_BROADWELL(dev))
11652 intel_crtc->atomic.wait_vblank = true;
11653
11654 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11655 break;
11656 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011657 break;
11658 case DRM_PLANE_TYPE_OVERLAY:
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011659 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011660 intel_crtc->atomic.wait_vblank = true;
11661 intel_crtc->atomic.update_sprite_watermarks |=
11662 1 << i;
11663 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011664 }
11665 return 0;
11666}
11667
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011668static bool encoders_cloneable(const struct intel_encoder *a,
11669 const struct intel_encoder *b)
11670{
11671 /* masks could be asymmetric, so check both ways */
11672 return a == b || (a->cloneable & (1 << b->type) &&
11673 b->cloneable & (1 << a->type));
11674}
11675
11676static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11677 struct intel_crtc *crtc,
11678 struct intel_encoder *encoder)
11679{
11680 struct intel_encoder *source_encoder;
11681 struct drm_connector *connector;
11682 struct drm_connector_state *connector_state;
11683 int i;
11684
11685 for_each_connector_in_state(state, connector, connector_state, i) {
11686 if (connector_state->crtc != &crtc->base)
11687 continue;
11688
11689 source_encoder =
11690 to_intel_encoder(connector_state->best_encoder);
11691 if (!encoders_cloneable(encoder, source_encoder))
11692 return false;
11693 }
11694
11695 return true;
11696}
11697
11698static bool check_encoder_cloning(struct drm_atomic_state *state,
11699 struct intel_crtc *crtc)
11700{
11701 struct intel_encoder *encoder;
11702 struct drm_connector *connector;
11703 struct drm_connector_state *connector_state;
11704 int i;
11705
11706 for_each_connector_in_state(state, connector, connector_state, i) {
11707 if (connector_state->crtc != &crtc->base)
11708 continue;
11709
11710 encoder = to_intel_encoder(connector_state->best_encoder);
11711 if (!check_single_encoder_cloning(state, crtc, encoder))
11712 return false;
11713 }
11714
11715 return true;
11716}
11717
11718static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11719 struct drm_crtc_state *crtc_state)
11720{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011721 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011722 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011724 struct intel_crtc_state *pipe_config =
11725 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011726 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011727 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011728 bool mode_changed = needs_modeset(crtc_state);
11729
11730 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11731 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11732 return -EINVAL;
11733 }
11734
Ville Syrjälä852eb002015-06-24 22:00:07 +030011735 if (mode_changed && !crtc_state->active)
11736 intel_crtc->atomic.update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011737
Maarten Lankhorstad421372015-06-15 12:33:42 +020011738 if (mode_changed && crtc_state->enable &&
11739 dev_priv->display.crtc_compute_clock &&
11740 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11741 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11742 pipe_config);
11743 if (ret)
11744 return ret;
11745 }
11746
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011747 ret = 0;
11748 if (INTEL_INFO(dev)->gen >= 9) {
11749 if (mode_changed)
11750 ret = skl_update_scaler_crtc(pipe_config);
11751
11752 if (!ret)
11753 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11754 pipe_config);
11755 }
11756
11757 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011758}
11759
Jani Nikula65b38e02015-04-13 11:26:56 +030011760static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011761 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11762 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011763 .atomic_begin = intel_begin_crtc_commit,
11764 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011765 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011766};
11767
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011768static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11769{
11770 struct intel_connector *connector;
11771
11772 for_each_intel_connector(dev, connector) {
11773 if (connector->base.encoder) {
11774 connector->base.state->best_encoder =
11775 connector->base.encoder;
11776 connector->base.state->crtc =
11777 connector->base.encoder->crtc;
11778 } else {
11779 connector->base.state->best_encoder = NULL;
11780 connector->base.state->crtc = NULL;
11781 }
11782 }
11783}
11784
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011785static void
Robin Schroereba905b2014-05-18 02:24:50 +020011786connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011787 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011788{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011789 int bpp = pipe_config->pipe_bpp;
11790
11791 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11792 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011793 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011794
11795 /* Don't use an invalid EDID bpc value */
11796 if (connector->base.display_info.bpc &&
11797 connector->base.display_info.bpc * 3 < bpp) {
11798 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11799 bpp, connector->base.display_info.bpc*3);
11800 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11801 }
11802
11803 /* Clamp bpp to 8 on screens without EDID 1.4 */
11804 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11805 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11806 bpp);
11807 pipe_config->pipe_bpp = 24;
11808 }
11809}
11810
11811static int
11812compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011813 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011814{
11815 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011816 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011817 struct drm_connector *connector;
11818 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011819 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011820
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011821 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011822 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011823 else if (INTEL_INFO(dev)->gen >= 5)
11824 bpp = 12*3;
11825 else
11826 bpp = 8*3;
11827
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011828
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011829 pipe_config->pipe_bpp = bpp;
11830
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011831 state = pipe_config->base.state;
11832
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011833 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011834 for_each_connector_in_state(state, connector, connector_state, i) {
11835 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011836 continue;
11837
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011838 connected_sink_compute_bpp(to_intel_connector(connector),
11839 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011840 }
11841
11842 return bpp;
11843}
11844
Daniel Vetter644db712013-09-19 14:53:58 +020011845static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11846{
11847 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11848 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011849 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011850 mode->crtc_hdisplay, mode->crtc_hsync_start,
11851 mode->crtc_hsync_end, mode->crtc_htotal,
11852 mode->crtc_vdisplay, mode->crtc_vsync_start,
11853 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11854}
11855
Daniel Vetterc0b03412013-05-28 12:05:54 +020011856static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011857 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011858 const char *context)
11859{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011860 struct drm_device *dev = crtc->base.dev;
11861 struct drm_plane *plane;
11862 struct intel_plane *intel_plane;
11863 struct intel_plane_state *state;
11864 struct drm_framebuffer *fb;
11865
11866 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11867 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011868
11869 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11870 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11871 pipe_config->pipe_bpp, pipe_config->dither);
11872 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11873 pipe_config->has_pch_encoder,
11874 pipe_config->fdi_lanes,
11875 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11876 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11877 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011878 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011879 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011880 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011881 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11882 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11883 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011884
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011885 DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011886 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011887 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011888 pipe_config->dp_m2_n2.gmch_m,
11889 pipe_config->dp_m2_n2.gmch_n,
11890 pipe_config->dp_m2_n2.link_m,
11891 pipe_config->dp_m2_n2.link_n,
11892 pipe_config->dp_m2_n2.tu);
11893
Daniel Vetter55072d12014-11-20 16:10:28 +010011894 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11895 pipe_config->has_audio,
11896 pipe_config->has_infoframe);
11897
Daniel Vetterc0b03412013-05-28 12:05:54 +020011898 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011899 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011900 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011901 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11902 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011903 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011904 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11905 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011906 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11907 crtc->num_scalers,
11908 pipe_config->scaler_state.scaler_users,
11909 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011910 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11911 pipe_config->gmch_pfit.control,
11912 pipe_config->gmch_pfit.pgm_ratios,
11913 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011914 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011915 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011916 pipe_config->pch_pfit.size,
11917 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011918 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011919 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011920
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011921 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030011922 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011923 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030011924 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011925 pipe_config->ddi_pll_sel,
11926 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030011927 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011928 pipe_config->dpll_hw_state.pll0,
11929 pipe_config->dpll_hw_state.pll1,
11930 pipe_config->dpll_hw_state.pll2,
11931 pipe_config->dpll_hw_state.pll3,
11932 pipe_config->dpll_hw_state.pll6,
11933 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030011934 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030011935 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011936 pipe_config->dpll_hw_state.pcsdw12);
11937 } else if (IS_SKYLAKE(dev)) {
11938 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11939 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11940 pipe_config->ddi_pll_sel,
11941 pipe_config->dpll_hw_state.ctrl1,
11942 pipe_config->dpll_hw_state.cfgcr1,
11943 pipe_config->dpll_hw_state.cfgcr2);
11944 } else if (HAS_DDI(dev)) {
11945 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11946 pipe_config->ddi_pll_sel,
11947 pipe_config->dpll_hw_state.wrpll);
11948 } else {
11949 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11950 "fp0: 0x%x, fp1: 0x%x\n",
11951 pipe_config->dpll_hw_state.dpll,
11952 pipe_config->dpll_hw_state.dpll_md,
11953 pipe_config->dpll_hw_state.fp0,
11954 pipe_config->dpll_hw_state.fp1);
11955 }
11956
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011957 DRM_DEBUG_KMS("planes on this crtc\n");
11958 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11959 intel_plane = to_intel_plane(plane);
11960 if (intel_plane->pipe != crtc->pipe)
11961 continue;
11962
11963 state = to_intel_plane_state(plane->state);
11964 fb = state->base.fb;
11965 if (!fb) {
11966 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11967 "disabled, scaler_id = %d\n",
11968 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11969 plane->base.id, intel_plane->pipe,
11970 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11971 drm_plane_index(plane), state->scaler_id);
11972 continue;
11973 }
11974
11975 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11976 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11977 plane->base.id, intel_plane->pipe,
11978 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11979 drm_plane_index(plane));
11980 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11981 fb->base.id, fb->width, fb->height, fb->pixel_format);
11982 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11983 state->scaler_id,
11984 state->src.x1 >> 16, state->src.y1 >> 16,
11985 drm_rect_width(&state->src) >> 16,
11986 drm_rect_height(&state->src) >> 16,
11987 state->dst.x1, state->dst.y1,
11988 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11989 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011990}
11991
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011992static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011993{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011994 struct drm_device *dev = state->dev;
11995 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011996 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011997 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011998 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011999 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012000
12001 /*
12002 * Walk the connector list instead of the encoder
12003 * list to detect the problem on ddi platforms
12004 * where there's just one encoder per digital port.
12005 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012006 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012007 if (!connector_state->best_encoder)
12008 continue;
12009
12010 encoder = to_intel_encoder(connector_state->best_encoder);
12011
12012 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012013
12014 switch (encoder->type) {
12015 unsigned int port_mask;
12016 case INTEL_OUTPUT_UNKNOWN:
12017 if (WARN_ON(!HAS_DDI(dev)))
12018 break;
12019 case INTEL_OUTPUT_DISPLAYPORT:
12020 case INTEL_OUTPUT_HDMI:
12021 case INTEL_OUTPUT_EDP:
12022 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12023
12024 /* the same port mustn't appear more than once */
12025 if (used_ports & port_mask)
12026 return false;
12027
12028 used_ports |= port_mask;
12029 default:
12030 break;
12031 }
12032 }
12033
12034 return true;
12035}
12036
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012037static void
12038clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12039{
12040 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012041 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012042 struct intel_dpll_hw_state dpll_hw_state;
12043 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012044 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012045 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012046
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012047 /* FIXME: before the switch to atomic started, a new pipe_config was
12048 * kzalloc'd. Code that depends on any field being zero should be
12049 * fixed, so that the crtc_state can be safely duplicated. For now,
12050 * only fields that are know to not cause problems are preserved. */
12051
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012052 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012053 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012054 shared_dpll = crtc_state->shared_dpll;
12055 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012056 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012057 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012058
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012059 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012060
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012061 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012062 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012063 crtc_state->shared_dpll = shared_dpll;
12064 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012065 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012066 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012067}
12068
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012069static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012070intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012071 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012072{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012073 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012074 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012075 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012076 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012077 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012078 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012079 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012080
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012081 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012082
Daniel Vettere143a212013-07-04 12:01:15 +020012083 pipe_config->cpu_transcoder =
12084 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012085
Imre Deak2960bc92013-07-30 13:36:32 +030012086 /*
12087 * Sanitize sync polarity flags based on requested ones. If neither
12088 * positive or negative polarity is requested, treat this as meaning
12089 * negative polarity.
12090 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012091 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012092 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012093 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012094
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012095 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012096 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012097 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012098
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012099 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12100 pipe_config);
12101 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012102 goto fail;
12103
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012104 /*
12105 * Determine the real pipe dimensions. Note that stereo modes can
12106 * increase the actual pipe size due to the frame doubling and
12107 * insertion of additional space for blanks between the frame. This
12108 * is stored in the crtc timings. We use the requested mode to do this
12109 * computation to clearly distinguish it from the adjusted mode, which
12110 * can be changed by the connectors in the below retry loop.
12111 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012112 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012113 &pipe_config->pipe_src_w,
12114 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012115
Daniel Vettere29c22c2013-02-21 00:00:16 +010012116encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012117 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012118 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012119 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012120
Daniel Vetter135c81b2013-07-21 21:37:09 +020012121 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012122 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12123 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012124
Daniel Vetter7758a112012-07-08 19:40:39 +020012125 /* Pass our mode to the connectors and the CRTC to give them a chance to
12126 * adjust it according to limitations or connector properties, and also
12127 * a chance to reject the mode entirely.
12128 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012129 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012130 if (connector_state->crtc != crtc)
12131 continue;
12132
12133 encoder = to_intel_encoder(connector_state->best_encoder);
12134
Daniel Vetterefea6e82013-07-21 21:36:59 +020012135 if (!(encoder->compute_config(encoder, pipe_config))) {
12136 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012137 goto fail;
12138 }
12139 }
12140
Daniel Vetterff9a6752013-06-01 17:16:21 +020012141 /* Set default port clock if not overwritten by the encoder. Needs to be
12142 * done afterwards in case the encoder adjusts the mode. */
12143 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012144 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012145 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012146
Daniel Vettera43f6e02013-06-07 23:10:32 +020012147 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012148 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012149 DRM_DEBUG_KMS("CRTC fixup failed\n");
12150 goto fail;
12151 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012152
12153 if (ret == RETRY) {
12154 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12155 ret = -EINVAL;
12156 goto fail;
12157 }
12158
12159 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12160 retry = false;
12161 goto encoder_retry;
12162 }
12163
Daniel Vettere8fa4272015-08-12 11:43:34 +020012164 /* Dithering seems to not pass-through bits correctly when it should, so
12165 * only enable it on 6bpc panels. */
12166 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012167 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012168 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012169
Daniel Vetter7758a112012-07-08 19:40:39 +020012170fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012171 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012172}
12173
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012174static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012175intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012176{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012177 struct drm_crtc *crtc;
12178 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012179 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012180
Ville Syrjälä76688512014-01-10 11:28:06 +020012181 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012182 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012183 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012184
12185 /* Update hwmode for vblank functions */
12186 if (crtc->state->active)
12187 crtc->hwmode = crtc->state->adjusted_mode;
12188 else
12189 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012190 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012191}
12192
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012193static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012194{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012195 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012196
12197 if (clock1 == clock2)
12198 return true;
12199
12200 if (!clock1 || !clock2)
12201 return false;
12202
12203 diff = abs(clock1 - clock2);
12204
12205 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12206 return true;
12207
12208 return false;
12209}
12210
Daniel Vetter25c5b262012-07-08 22:08:04 +020012211#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12212 list_for_each_entry((intel_crtc), \
12213 &(dev)->mode_config.crtc_list, \
12214 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012215 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012216
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012217
12218static bool
12219intel_compare_m_n(unsigned int m, unsigned int n,
12220 unsigned int m2, unsigned int n2,
12221 bool exact)
12222{
12223 if (m == m2 && n == n2)
12224 return true;
12225
12226 if (exact || !m || !n || !m2 || !n2)
12227 return false;
12228
12229 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12230
12231 if (m > m2) {
12232 while (m > m2) {
12233 m2 <<= 1;
12234 n2 <<= 1;
12235 }
12236 } else if (m < m2) {
12237 while (m < m2) {
12238 m <<= 1;
12239 n <<= 1;
12240 }
12241 }
12242
12243 return m == m2 && n == n2;
12244}
12245
12246static bool
12247intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12248 struct intel_link_m_n *m2_n2,
12249 bool adjust)
12250{
12251 if (m_n->tu == m2_n2->tu &&
12252 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12253 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12254 intel_compare_m_n(m_n->link_m, m_n->link_n,
12255 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12256 if (adjust)
12257 *m2_n2 = *m_n;
12258
12259 return true;
12260 }
12261
12262 return false;
12263}
12264
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012265static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012266intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012267 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012268 struct intel_crtc_state *pipe_config,
12269 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012270{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012271 bool ret = true;
12272
12273#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12274 do { \
12275 if (!adjust) \
12276 DRM_ERROR(fmt, ##__VA_ARGS__); \
12277 else \
12278 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12279 } while (0)
12280
Daniel Vetter66e985c2013-06-05 13:34:20 +020012281#define PIPE_CONF_CHECK_X(name) \
12282 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012283 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012284 "(expected 0x%08x, found 0x%08x)\n", \
12285 current_config->name, \
12286 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012287 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012288 }
12289
Daniel Vetter08a24032013-04-19 11:25:34 +020012290#define PIPE_CONF_CHECK_I(name) \
12291 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012292 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012293 "(expected %i, found %i)\n", \
12294 current_config->name, \
12295 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012296 ret = false; \
12297 }
12298
12299#define PIPE_CONF_CHECK_M_N(name) \
12300 if (!intel_compare_link_m_n(&current_config->name, \
12301 &pipe_config->name,\
12302 adjust)) { \
12303 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12304 "(expected tu %i gmch %i/%i link %i/%i, " \
12305 "found tu %i, gmch %i/%i link %i/%i)\n", \
12306 current_config->name.tu, \
12307 current_config->name.gmch_m, \
12308 current_config->name.gmch_n, \
12309 current_config->name.link_m, \
12310 current_config->name.link_n, \
12311 pipe_config->name.tu, \
12312 pipe_config->name.gmch_m, \
12313 pipe_config->name.gmch_n, \
12314 pipe_config->name.link_m, \
12315 pipe_config->name.link_n); \
12316 ret = false; \
12317 }
12318
12319#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12320 if (!intel_compare_link_m_n(&current_config->name, \
12321 &pipe_config->name, adjust) && \
12322 !intel_compare_link_m_n(&current_config->alt_name, \
12323 &pipe_config->name, adjust)) { \
12324 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12325 "(expected tu %i gmch %i/%i link %i/%i, " \
12326 "or tu %i gmch %i/%i link %i/%i, " \
12327 "found tu %i, gmch %i/%i link %i/%i)\n", \
12328 current_config->name.tu, \
12329 current_config->name.gmch_m, \
12330 current_config->name.gmch_n, \
12331 current_config->name.link_m, \
12332 current_config->name.link_n, \
12333 current_config->alt_name.tu, \
12334 current_config->alt_name.gmch_m, \
12335 current_config->alt_name.gmch_n, \
12336 current_config->alt_name.link_m, \
12337 current_config->alt_name.link_n, \
12338 pipe_config->name.tu, \
12339 pipe_config->name.gmch_m, \
12340 pipe_config->name.gmch_n, \
12341 pipe_config->name.link_m, \
12342 pipe_config->name.link_n); \
12343 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012344 }
12345
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012346/* This is required for BDW+ where there is only one set of registers for
12347 * switching between high and low RR.
12348 * This macro can be used whenever a comparison has to be made between one
12349 * hw state and multiple sw state variables.
12350 */
12351#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12352 if ((current_config->name != pipe_config->name) && \
12353 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012354 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012355 "(expected %i or %i, found %i)\n", \
12356 current_config->name, \
12357 current_config->alt_name, \
12358 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012359 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012360 }
12361
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012362#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12363 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012364 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012365 "(expected %i, found %i)\n", \
12366 current_config->name & (mask), \
12367 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012368 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012369 }
12370
Ville Syrjälä5e550652013-09-06 23:29:07 +030012371#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12372 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012373 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012374 "(expected %i, found %i)\n", \
12375 current_config->name, \
12376 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012377 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012378 }
12379
Daniel Vetterbb760062013-06-06 14:55:52 +020012380#define PIPE_CONF_QUIRK(quirk) \
12381 ((current_config->quirks | pipe_config->quirks) & (quirk))
12382
Daniel Vettereccb1402013-05-22 00:50:22 +020012383 PIPE_CONF_CHECK_I(cpu_transcoder);
12384
Daniel Vetter08a24032013-04-19 11:25:34 +020012385 PIPE_CONF_CHECK_I(has_pch_encoder);
12386 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012387 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012388
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012389 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012390 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012391
12392 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012393 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012394
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012395 PIPE_CONF_CHECK_I(has_drrs);
12396 if (current_config->has_drrs)
12397 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12398 } else
12399 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012400
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012401 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12402 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12403 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12404 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12405 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12406 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012407
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012408 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12409 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12410 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12411 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12412 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12413 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012414
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012415 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012416 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012417 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12418 IS_VALLEYVIEW(dev))
12419 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012420 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012421
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012422 PIPE_CONF_CHECK_I(has_audio);
12423
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012424 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012425 DRM_MODE_FLAG_INTERLACE);
12426
Daniel Vetterbb760062013-06-06 14:55:52 +020012427 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012428 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012429 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012430 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012431 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012432 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012433 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012434 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012435 DRM_MODE_FLAG_NVSYNC);
12436 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012437
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012438 PIPE_CONF_CHECK_I(pipe_src_w);
12439 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012440
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012441 PIPE_CONF_CHECK_X(gmch_pfit.control);
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012442 /* pfit ratios are autocomputed by the hw on gen4+ */
12443 if (INTEL_INFO(dev)->gen < 4)
12444 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012445 PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012446
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012447 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12448 if (current_config->pch_pfit.enabled) {
Ville Syrjälä333b8ca2015-09-03 21:50:16 +030012449 PIPE_CONF_CHECK_X(pch_pfit.pos);
12450 PIPE_CONF_CHECK_X(pch_pfit.size);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012451 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012452
Chandra Kondurua1b22782015-04-07 15:28:45 -070012453 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12454
Jesse Barnese59150d2014-01-07 13:30:45 -080012455 /* BDW+ don't expose a synchronous way to read the state */
12456 if (IS_HASWELL(dev))
12457 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012458
Ville Syrjälä282740f2013-09-04 18:30:03 +030012459 PIPE_CONF_CHECK_I(double_wide);
12460
Daniel Vetter26804af2014-06-25 22:01:55 +030012461 PIPE_CONF_CHECK_X(ddi_pll_sel);
12462
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012463 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012464 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012465 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012466 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12467 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012468 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012469 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12470 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12471 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012472
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012473 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12474 PIPE_CONF_CHECK_I(pipe_bpp);
12475
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012476 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012477 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012478
Daniel Vetter66e985c2013-06-05 13:34:20 +020012479#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012480#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012481#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012482#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012483#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012484#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012485#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012486
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012487 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012488}
12489
Damien Lespiau08db6652014-11-04 17:06:52 +000012490static void check_wm_state(struct drm_device *dev)
12491{
12492 struct drm_i915_private *dev_priv = dev->dev_private;
12493 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12494 struct intel_crtc *intel_crtc;
12495 int plane;
12496
12497 if (INTEL_INFO(dev)->gen < 9)
12498 return;
12499
12500 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12501 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12502
12503 for_each_intel_crtc(dev, intel_crtc) {
12504 struct skl_ddb_entry *hw_entry, *sw_entry;
12505 const enum pipe pipe = intel_crtc->pipe;
12506
12507 if (!intel_crtc->active)
12508 continue;
12509
12510 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012511 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012512 hw_entry = &hw_ddb.plane[pipe][plane];
12513 sw_entry = &sw_ddb->plane[pipe][plane];
12514
12515 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12516 continue;
12517
12518 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12519 "(expected (%u,%u), found (%u,%u))\n",
12520 pipe_name(pipe), plane + 1,
12521 sw_entry->start, sw_entry->end,
12522 hw_entry->start, hw_entry->end);
12523 }
12524
12525 /* cursor */
12526 hw_entry = &hw_ddb.cursor[pipe];
12527 sw_entry = &sw_ddb->cursor[pipe];
12528
12529 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12530 continue;
12531
12532 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12533 "(expected (%u,%u), found (%u,%u))\n",
12534 pipe_name(pipe),
12535 sw_entry->start, sw_entry->end,
12536 hw_entry->start, hw_entry->end);
12537 }
12538}
12539
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012540static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012541check_connector_state(struct drm_device *dev,
12542 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012543{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012544 struct drm_connector_state *old_conn_state;
12545 struct drm_connector *connector;
12546 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012547
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012548 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12549 struct drm_encoder *encoder = connector->encoder;
12550 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012551
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012552 /* This also checks the encoder/connector hw state with the
12553 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012554 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012555
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012556 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012557 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012558 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012559}
12560
12561static void
12562check_encoder_state(struct drm_device *dev)
12563{
12564 struct intel_encoder *encoder;
12565 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012566
Damien Lespiaub2784e12014-08-05 11:29:37 +010012567 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012568 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012569 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012570
12571 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12572 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012573 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012574
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012575 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012576 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012577 continue;
12578 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012579
12580 I915_STATE_WARN(connector->base.state->crtc !=
12581 encoder->base.crtc,
12582 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012583 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012584
Rob Clarke2c719b2014-12-15 13:56:32 -050012585 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012586 "encoder's enabled state mismatch "
12587 "(expected %i, found %i)\n",
12588 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012589
12590 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012591 bool active;
12592
12593 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012594 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012595 "encoder detached but still enabled on pipe %c.\n",
12596 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012597 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012598 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012599}
12600
12601static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012602check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012603{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012604 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012605 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012606 struct drm_crtc_state *old_crtc_state;
12607 struct drm_crtc *crtc;
12608 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012609
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012610 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12612 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012613 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012614
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012615 if (!needs_modeset(crtc->state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012616 continue;
12617
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012618 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12619 pipe_config = to_intel_crtc_state(old_crtc_state);
12620 memset(pipe_config, 0, sizeof(*pipe_config));
12621 pipe_config->base.crtc = crtc;
12622 pipe_config->base.state = old_state;
12623
12624 DRM_DEBUG_KMS("[CRTC:%d]\n",
12625 crtc->base.id);
12626
12627 active = dev_priv->display.get_pipe_config(intel_crtc,
12628 pipe_config);
12629
12630 /* hw state is inconsistent with the pipe quirk */
12631 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12632 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12633 active = crtc->state->active;
12634
12635 I915_STATE_WARN(crtc->state->active != active,
12636 "crtc active state doesn't match with hw state "
12637 "(expected %i, found %i)\n", crtc->state->active, active);
12638
12639 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12640 "transitional active state does not match atomic hw state "
12641 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12642
12643 for_each_encoder_on_crtc(dev, crtc, encoder) {
12644 enum pipe pipe;
12645
12646 active = encoder->get_hw_state(encoder, &pipe);
12647 I915_STATE_WARN(active != crtc->state->active,
12648 "[ENCODER:%i] active %i with crtc active %i\n",
12649 encoder->base.base.id, active, crtc->state->active);
12650
12651 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12652 "Encoder connected to wrong pipe %c\n",
12653 pipe_name(pipe));
12654
12655 if (active)
12656 encoder->get_config(encoder, pipe_config);
12657 }
12658
12659 if (!crtc->state->active)
12660 continue;
12661
12662 sw_config = to_intel_crtc_state(crtc->state);
12663 if (!intel_pipe_config_compare(dev, sw_config,
12664 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012665 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012666 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012667 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012668 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012669 "[sw state]");
12670 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012671 }
12672}
12673
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012674static void
12675check_shared_dpll_state(struct drm_device *dev)
12676{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012677 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012678 struct intel_crtc *crtc;
12679 struct intel_dpll_hw_state dpll_hw_state;
12680 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012681
12682 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12683 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12684 int enabled_crtcs = 0, active_crtcs = 0;
12685 bool active;
12686
12687 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12688
12689 DRM_DEBUG_KMS("%s\n", pll->name);
12690
12691 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12692
Rob Clarke2c719b2014-12-15 13:56:32 -050012693 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012694 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012695 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012696 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012697 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012698 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012699 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012700 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012701 "pll on state mismatch (expected %i, found %i)\n",
12702 pll->on, active);
12703
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012704 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012705 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012706 enabled_crtcs++;
12707 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12708 active_crtcs++;
12709 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012710 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012711 "pll active crtcs mismatch (expected %i, found %i)\n",
12712 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012713 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012714 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012715 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012716
Rob Clarke2c719b2014-12-15 13:56:32 -050012717 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012718 sizeof(dpll_hw_state)),
12719 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012720 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012721}
12722
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012723static void
12724intel_modeset_check_state(struct drm_device *dev,
12725 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012726{
Damien Lespiau08db6652014-11-04 17:06:52 +000012727 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012728 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012729 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012730 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012731 check_shared_dpll_state(dev);
12732}
12733
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012734void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012735 int dotclock)
12736{
12737 /*
12738 * FDI already provided one idea for the dotclock.
12739 * Yell if the encoder disagrees.
12740 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012741 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012742 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012743 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012744}
12745
Ville Syrjälä80715b22014-05-15 20:23:23 +030012746static void update_scanline_offset(struct intel_crtc *crtc)
12747{
12748 struct drm_device *dev = crtc->base.dev;
12749
12750 /*
12751 * The scanline counter increments at the leading edge of hsync.
12752 *
12753 * On most platforms it starts counting from vtotal-1 on the
12754 * first active line. That means the scanline counter value is
12755 * always one less than what we would expect. Ie. just after
12756 * start of vblank, which also occurs at start of hsync (on the
12757 * last active line), the scanline counter will read vblank_start-1.
12758 *
12759 * On gen2 the scanline counter starts counting from 1 instead
12760 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12761 * to keep the value positive), instead of adding one.
12762 *
12763 * On HSW+ the behaviour of the scanline counter depends on the output
12764 * type. For DP ports it behaves like most other platforms, but on HDMI
12765 * there's an extra 1 line difference. So we need to add two instead of
12766 * one to the value.
12767 */
12768 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012769 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012770 int vtotal;
12771
12772 vtotal = mode->crtc_vtotal;
12773 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12774 vtotal /= 2;
12775
12776 crtc->scanline_offset = vtotal - 1;
12777 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012778 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012779 crtc->scanline_offset = 2;
12780 } else
12781 crtc->scanline_offset = 1;
12782}
12783
Maarten Lankhorstad421372015-06-15 12:33:42 +020012784static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012785{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012786 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012787 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012788 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012789 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012790 struct intel_crtc_state *intel_crtc_state;
12791 struct drm_crtc *crtc;
12792 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012793 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012794
12795 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012796 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012797
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012798 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012799 int dpll;
12800
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012801 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012802 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012803 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012804
Maarten Lankhorstad421372015-06-15 12:33:42 +020012805 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012806 continue;
12807
Maarten Lankhorstad421372015-06-15 12:33:42 +020012808 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012809
Maarten Lankhorstad421372015-06-15 12:33:42 +020012810 if (!shared_dpll)
12811 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12812
12813 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012814 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012815}
12816
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012817/*
12818 * This implements the workaround described in the "notes" section of the mode
12819 * set sequence documentation. When going from no pipes or single pipe to
12820 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12821 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12822 */
12823static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12824{
12825 struct drm_crtc_state *crtc_state;
12826 struct intel_crtc *intel_crtc;
12827 struct drm_crtc *crtc;
12828 struct intel_crtc_state *first_crtc_state = NULL;
12829 struct intel_crtc_state *other_crtc_state = NULL;
12830 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12831 int i;
12832
12833 /* look at all crtc's that are going to be enabled in during modeset */
12834 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12835 intel_crtc = to_intel_crtc(crtc);
12836
12837 if (!crtc_state->active || !needs_modeset(crtc_state))
12838 continue;
12839
12840 if (first_crtc_state) {
12841 other_crtc_state = to_intel_crtc_state(crtc_state);
12842 break;
12843 } else {
12844 first_crtc_state = to_intel_crtc_state(crtc_state);
12845 first_pipe = intel_crtc->pipe;
12846 }
12847 }
12848
12849 /* No workaround needed? */
12850 if (!first_crtc_state)
12851 return 0;
12852
12853 /* w/a possibly needed, check how many crtc's are already enabled. */
12854 for_each_intel_crtc(state->dev, intel_crtc) {
12855 struct intel_crtc_state *pipe_config;
12856
12857 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12858 if (IS_ERR(pipe_config))
12859 return PTR_ERR(pipe_config);
12860
12861 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12862
12863 if (!pipe_config->base.active ||
12864 needs_modeset(&pipe_config->base))
12865 continue;
12866
12867 /* 2 or more enabled crtcs means no need for w/a */
12868 if (enabled_pipe != INVALID_PIPE)
12869 return 0;
12870
12871 enabled_pipe = intel_crtc->pipe;
12872 }
12873
12874 if (enabled_pipe != INVALID_PIPE)
12875 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12876 else if (other_crtc_state)
12877 other_crtc_state->hsw_workaround_pipe = first_pipe;
12878
12879 return 0;
12880}
12881
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012882static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12883{
12884 struct drm_crtc *crtc;
12885 struct drm_crtc_state *crtc_state;
12886 int ret = 0;
12887
12888 /* add all active pipes to the state */
12889 for_each_crtc(state->dev, crtc) {
12890 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12891 if (IS_ERR(crtc_state))
12892 return PTR_ERR(crtc_state);
12893
12894 if (!crtc_state->active || needs_modeset(crtc_state))
12895 continue;
12896
12897 crtc_state->mode_changed = true;
12898
12899 ret = drm_atomic_add_affected_connectors(state, crtc);
12900 if (ret)
12901 break;
12902
12903 ret = drm_atomic_add_affected_planes(state, crtc);
12904 if (ret)
12905 break;
12906 }
12907
12908 return ret;
12909}
12910
12911
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012912static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012913{
12914 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012915 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012916 int ret;
12917
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012918 if (!check_digital_port_conflicts(state)) {
12919 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12920 return -EINVAL;
12921 }
12922
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012923 /*
12924 * See if the config requires any additional preparation, e.g.
12925 * to adjust global state with pipes off. We need to do this
12926 * here so we can get the modeset_pipe updated config for the new
12927 * mode set on this crtc. For other crtcs we need to use the
12928 * adjusted_mode bits in the crtc directly.
12929 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012930 if (dev_priv->display.modeset_calc_cdclk) {
12931 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012932
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012933 ret = dev_priv->display.modeset_calc_cdclk(state);
12934
12935 cdclk = to_intel_atomic_state(state)->cdclk;
12936 if (!ret && cdclk != dev_priv->cdclk_freq)
12937 ret = intel_modeset_all_pipes(state);
12938
12939 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012940 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012941 } else
12942 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012943
Maarten Lankhorstad421372015-06-15 12:33:42 +020012944 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012945
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012946 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012947 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012948
Maarten Lankhorstad421372015-06-15 12:33:42 +020012949 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012950}
12951
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012952/**
12953 * intel_atomic_check - validate state object
12954 * @dev: drm device
12955 * @state: state to validate
12956 */
12957static int intel_atomic_check(struct drm_device *dev,
12958 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012959{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012960 struct drm_crtc *crtc;
12961 struct drm_crtc_state *crtc_state;
12962 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012963 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012964
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012965 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012966 if (ret)
12967 return ret;
12968
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012969 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012970 struct intel_crtc_state *pipe_config =
12971 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012972
12973 /* Catch I915_MODE_FLAG_INHERITED */
12974 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
12975 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012976
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012977 if (!crtc_state->enable) {
12978 if (needs_modeset(crtc_state))
12979 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012980 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012981 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012982
Daniel Vetter26495482015-07-15 14:15:52 +020012983 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012984 continue;
12985
Daniel Vetter26495482015-07-15 14:15:52 +020012986 /* FIXME: For only active_changed we shouldn't need to do any
12987 * state recomputation at all. */
12988
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012989 ret = drm_atomic_add_affected_connectors(state, crtc);
12990 if (ret)
12991 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012992
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012993 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012994 if (ret)
12995 return ret;
12996
Daniel Vetter26495482015-07-15 14:15:52 +020012997 if (i915.fastboot &&
12998 intel_pipe_config_compare(state->dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012999 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013000 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013001 crtc_state->mode_changed = false;
13002 }
13003
13004 if (needs_modeset(crtc_state)) {
13005 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013006
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013007 ret = drm_atomic_add_affected_planes(state, crtc);
13008 if (ret)
13009 return ret;
13010 }
13011
Daniel Vetter26495482015-07-15 14:15:52 +020013012 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13013 needs_modeset(crtc_state) ?
13014 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013015 }
13016
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013017 if (any_ms) {
13018 ret = intel_modeset_checks(state);
13019
13020 if (ret)
13021 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013022 } else
13023 to_intel_atomic_state(state)->cdclk =
13024 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013025
13026 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013027}
13028
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013029/**
13030 * intel_atomic_commit - commit validated state object
13031 * @dev: DRM device
13032 * @state: the top-level driver state object
13033 * @async: asynchronous commit
13034 *
13035 * This function commits a top-level state object that has been validated
13036 * with drm_atomic_helper_check().
13037 *
13038 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13039 * we can only handle plane-related operations and do not yet support
13040 * asynchronous commit.
13041 *
13042 * RETURNS
13043 * Zero for success or -errno.
13044 */
13045static int intel_atomic_commit(struct drm_device *dev,
13046 struct drm_atomic_state *state,
13047 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013048{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013049 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013050 struct drm_crtc *crtc;
13051 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013052 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013053 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013054 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013055
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013056 if (async) {
13057 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13058 return -EINVAL;
13059 }
13060
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013061 ret = drm_atomic_helper_prepare_planes(dev, state);
13062 if (ret)
13063 return ret;
13064
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013065 drm_atomic_helper_swap_state(dev, state);
13066
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013067 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13069
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013070 if (!needs_modeset(crtc->state))
13071 continue;
13072
13073 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013074 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013075
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013076 if (crtc_state->active) {
13077 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13078 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013079 intel_crtc->active = false;
13080 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013081 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013082 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013083
Daniel Vetterea9d7582012-07-10 10:42:52 +020013084 /* Only after disabling all output pipelines that will be changed can we
13085 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013086 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013087
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013088 if (any_ms) {
13089 intel_shared_dpll_commit(state);
13090
13091 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013092 modeset_update_crtc_power_domains(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013093 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013094
Daniel Vettera6778b32012-07-02 09:56:42 +020013095 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013096 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13098 bool modeset = needs_modeset(crtc->state);
13099
13100 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013101 update_scanline_offset(to_intel_crtc(crtc));
13102 dev_priv->display.crtc_enable(crtc);
13103 }
13104
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013105 if (!modeset)
13106 intel_pre_plane_update(intel_crtc);
13107
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013108 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013109 intel_post_plane_update(intel_crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013110 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013111
Daniel Vettera6778b32012-07-02 09:56:42 +020013112 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013113
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013114 drm_atomic_helper_wait_for_vblanks(dev, state);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013115 drm_atomic_helper_cleanup_planes(dev, state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013116
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013117 if (any_ms)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013118 intel_modeset_check_state(dev, state);
13119
13120 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013121
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013122 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013123}
13124
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013125void intel_crtc_restore_mode(struct drm_crtc *crtc)
13126{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013127 struct drm_device *dev = crtc->dev;
13128 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013129 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013130 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013131
13132 state = drm_atomic_state_alloc(dev);
13133 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013134 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013135 crtc->base.id);
13136 return;
13137 }
13138
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013139 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013140
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013141retry:
13142 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13143 ret = PTR_ERR_OR_ZERO(crtc_state);
13144 if (!ret) {
13145 if (!crtc_state->active)
13146 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013147
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013148 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013149 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013150 }
13151
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013152 if (ret == -EDEADLK) {
13153 drm_atomic_state_clear(state);
13154 drm_modeset_backoff(state->acquire_ctx);
13155 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013156 }
13157
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013158 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013159out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013160 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013161}
13162
Daniel Vetter25c5b262012-07-08 22:08:04 +020013163#undef for_each_intel_crtc_masked
13164
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013165static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013166 .gamma_set = intel_crtc_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013167 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013168 .destroy = intel_crtc_destroy,
13169 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013170 .atomic_duplicate_state = intel_crtc_duplicate_state,
13171 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013172};
13173
Daniel Vetter53589012013-06-05 13:34:16 +020013174static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13175 struct intel_shared_dpll *pll,
13176 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013177{
Daniel Vetter53589012013-06-05 13:34:16 +020013178 uint32_t val;
13179
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013180 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013181 return false;
13182
Daniel Vetter53589012013-06-05 13:34:16 +020013183 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013184 hw_state->dpll = val;
13185 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13186 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013187
13188 return val & DPLL_VCO_ENABLE;
13189}
13190
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013191static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13192 struct intel_shared_dpll *pll)
13193{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013194 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13195 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013196}
13197
Daniel Vettere7b903d2013-06-05 13:34:14 +020013198static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13199 struct intel_shared_dpll *pll)
13200{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013201 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013202 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013203
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013204 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013205
13206 /* Wait for the clocks to stabilize. */
13207 POSTING_READ(PCH_DPLL(pll->id));
13208 udelay(150);
13209
13210 /* The pixel multiplier can only be updated once the
13211 * DPLL is enabled and the clocks are stable.
13212 *
13213 * So write it again.
13214 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013215 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013216 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013217 udelay(200);
13218}
13219
13220static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13221 struct intel_shared_dpll *pll)
13222{
13223 struct drm_device *dev = dev_priv->dev;
13224 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013225
13226 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013227 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013228 if (intel_crtc_to_shared_dpll(crtc) == pll)
13229 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13230 }
13231
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013232 I915_WRITE(PCH_DPLL(pll->id), 0);
13233 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013234 udelay(200);
13235}
13236
Daniel Vetter46edb022013-06-05 13:34:12 +020013237static char *ibx_pch_dpll_names[] = {
13238 "PCH DPLL A",
13239 "PCH DPLL B",
13240};
13241
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013242static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013243{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013244 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013245 int i;
13246
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013247 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013248
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013249 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013250 dev_priv->shared_dplls[i].id = i;
13251 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013252 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013253 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13254 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013255 dev_priv->shared_dplls[i].get_hw_state =
13256 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013257 }
13258}
13259
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013260static void intel_shared_dpll_init(struct drm_device *dev)
13261{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013262 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013263
Ville Syrjäläb6283052015-06-03 15:45:07 +030013264 intel_update_cdclk(dev);
13265
Daniel Vetter9cd86932014-06-25 22:01:57 +030013266 if (HAS_DDI(dev))
13267 intel_ddi_pll_init(dev);
13268 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013269 ibx_pch_dpll_init(dev);
13270 else
13271 dev_priv->num_shared_dpll = 0;
13272
13273 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013274}
13275
Matt Roper6beb8c232014-12-01 15:40:14 -080013276/**
13277 * intel_prepare_plane_fb - Prepare fb for usage on plane
13278 * @plane: drm plane to prepare for
13279 * @fb: framebuffer to prepare for presentation
13280 *
13281 * Prepares a framebuffer for usage on a display plane. Generally this
13282 * involves pinning the underlying object and updating the frontbuffer tracking
13283 * bits. Some older platforms need special physical address handling for
13284 * cursor planes.
13285 *
13286 * Returns 0 on success, negative error code on failure.
13287 */
13288int
13289intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013290 struct drm_framebuffer *fb,
13291 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013292{
13293 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013294 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013295 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13296 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013297 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013298
Matt Roperea2c67b2014-12-23 10:41:52 -080013299 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013300 return 0;
13301
Matt Roper4c345742014-07-09 16:22:10 -070013302 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013303
Matt Roper6beb8c232014-12-01 15:40:14 -080013304 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13305 INTEL_INFO(dev)->cursor_needs_physical) {
13306 int align = IS_I830(dev) ? 16 * 1024 : 256;
13307 ret = i915_gem_object_attach_phys(obj, align);
13308 if (ret)
13309 DRM_DEBUG_KMS("failed to attach phys object\n");
13310 } else {
John Harrison91af1272015-06-18 13:14:56 +010013311 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013312 }
13313
13314 if (ret == 0)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013315 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Matt Roper6beb8c232014-12-01 15:40:14 -080013316
13317 mutex_unlock(&dev->struct_mutex);
13318
13319 return ret;
13320}
13321
Matt Roper38f3ce32014-12-02 07:45:25 -080013322/**
13323 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13324 * @plane: drm plane to clean up for
13325 * @fb: old framebuffer that was on plane
13326 *
13327 * Cleans up a framebuffer that has just been removed from a plane.
13328 */
13329void
13330intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013331 struct drm_framebuffer *fb,
13332 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013333{
13334 struct drm_device *dev = plane->dev;
13335 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13336
13337 if (WARN_ON(!obj))
13338 return;
13339
13340 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13341 !INTEL_INFO(dev)->cursor_needs_physical) {
13342 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013343 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013344 mutex_unlock(&dev->struct_mutex);
13345 }
Matt Roper465c1202014-05-29 08:06:54 -070013346}
13347
Chandra Konduru6156a452015-04-27 13:48:39 -070013348int
13349skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13350{
13351 int max_scale;
13352 struct drm_device *dev;
13353 struct drm_i915_private *dev_priv;
13354 int crtc_clock, cdclk;
13355
13356 if (!intel_crtc || !crtc_state)
13357 return DRM_PLANE_HELPER_NO_SCALING;
13358
13359 dev = intel_crtc->base.dev;
13360 dev_priv = dev->dev_private;
13361 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013362 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013363
13364 if (!crtc_clock || !cdclk)
13365 return DRM_PLANE_HELPER_NO_SCALING;
13366
13367 /*
13368 * skl max scale is lower of:
13369 * close to 3 but not 3, -1 is for that purpose
13370 * or
13371 * cdclk/crtc_clock
13372 */
13373 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13374
13375 return max_scale;
13376}
13377
Matt Roper465c1202014-05-29 08:06:54 -070013378static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013379intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013380 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013381 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013382{
Matt Roper2b875c22014-12-01 15:40:13 -080013383 struct drm_crtc *crtc = state->base.crtc;
13384 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013385 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013386 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13387 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013388
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013389 /* use scaler when colorkey is not required */
13390 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013391 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013392 min_scale = 1;
13393 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013394 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013395 }
Sonika Jindald8106362015-04-10 14:37:28 +053013396
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013397 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13398 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013399 min_scale, max_scale,
13400 can_position, true,
13401 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013402}
13403
Gustavo Padovan14af2932014-10-24 14:51:31 +010013404static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013405intel_commit_primary_plane(struct drm_plane *plane,
13406 struct intel_plane_state *state)
13407{
Matt Roper2b875c22014-12-01 15:40:13 -080013408 struct drm_crtc *crtc = state->base.crtc;
13409 struct drm_framebuffer *fb = state->base.fb;
13410 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013411 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013412 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013413 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013414
Matt Roperea2c67b2014-12-23 10:41:52 -080013415 crtc = crtc ? crtc : plane->crtc;
13416 intel_crtc = to_intel_crtc(crtc);
13417
Matt Ropercf4c7c12014-12-04 10:27:42 -080013418 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013419 crtc->x = src->x1 >> 16;
13420 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013421
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013422 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013423 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013424
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013425 if (state->visible)
13426 /* FIXME: kill this fastboot hack */
13427 intel_update_pipe_size(intel_crtc);
13428
Maarten Lankhorstd4b08632015-09-10 16:07:56 +020013429 dev_priv->display.update_primary_plane(crtc, fb,
13430 state->src.x1 >> 16,
13431 state->src.y1 >> 16);
Matt Roper32b7eee2014-12-24 07:59:06 -080013432}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013433
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013434static void
13435intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013436 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013437{
13438 struct drm_device *dev = plane->dev;
13439 struct drm_i915_private *dev_priv = dev->dev_private;
13440
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013441 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13442}
13443
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013444static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13445 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013446{
13447 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013449
Ville Syrjäläf015c552015-06-24 22:00:02 +030013450 if (intel_crtc->atomic.update_wm_pre)
Matt Roper32b7eee2014-12-24 07:59:06 -080013451 intel_update_watermarks(crtc);
13452
Matt Roperc34c9ee2014-12-23 10:41:50 -080013453 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013454 if (crtc->state->active)
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020013455 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013456
13457 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13458 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013459}
13460
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013461static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13462 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013463{
Matt Roper32b7eee2014-12-24 07:59:06 -080013464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013465
Maarten Lankhorst8f539a82015-07-13 16:30:32 +020013466 if (crtc->state->active)
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020013467 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013468}
13469
Matt Ropercf4c7c12014-12-04 10:27:42 -080013470/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013471 * intel_plane_destroy - destroy a plane
13472 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013473 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013474 * Common destruction function for all types of planes (primary, cursor,
13475 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013476 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013477void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013478{
13479 struct intel_plane *intel_plane = to_intel_plane(plane);
13480 drm_plane_cleanup(plane);
13481 kfree(intel_plane);
13482}
13483
Matt Roper65a3fea2015-01-21 16:35:42 -080013484const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013485 .update_plane = drm_atomic_helper_update_plane,
13486 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013487 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013488 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013489 .atomic_get_property = intel_plane_atomic_get_property,
13490 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013491 .atomic_duplicate_state = intel_plane_duplicate_state,
13492 .atomic_destroy_state = intel_plane_destroy_state,
13493
Matt Roper465c1202014-05-29 08:06:54 -070013494};
13495
13496static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13497 int pipe)
13498{
13499 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013500 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013501 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013502 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013503
13504 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13505 if (primary == NULL)
13506 return NULL;
13507
Matt Roper8e7d6882015-01-21 16:35:41 -080013508 state = intel_create_plane_state(&primary->base);
13509 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013510 kfree(primary);
13511 return NULL;
13512 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013513 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013514
Matt Roper465c1202014-05-29 08:06:54 -070013515 primary->can_scale = false;
13516 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013517 if (INTEL_INFO(dev)->gen >= 9) {
13518 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013519 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013520 }
Matt Roper465c1202014-05-29 08:06:54 -070013521 primary->pipe = pipe;
13522 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013523 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013524 primary->check_plane = intel_check_primary_plane;
13525 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013526 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013527 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13528 primary->plane = !pipe;
13529
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013530 if (INTEL_INFO(dev)->gen >= 9) {
13531 intel_primary_formats = skl_primary_formats;
13532 num_formats = ARRAY_SIZE(skl_primary_formats);
13533 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013534 intel_primary_formats = i965_primary_formats;
13535 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013536 } else {
13537 intel_primary_formats = i8xx_primary_formats;
13538 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013539 }
13540
13541 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013542 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013543 intel_primary_formats, num_formats,
13544 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013545
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013546 if (INTEL_INFO(dev)->gen >= 4)
13547 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013548
Matt Roperea2c67b2014-12-23 10:41:52 -080013549 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13550
Matt Roper465c1202014-05-29 08:06:54 -070013551 return &primary->base;
13552}
13553
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013554void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13555{
13556 if (!dev->mode_config.rotation_property) {
13557 unsigned long flags = BIT(DRM_ROTATE_0) |
13558 BIT(DRM_ROTATE_180);
13559
13560 if (INTEL_INFO(dev)->gen >= 9)
13561 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13562
13563 dev->mode_config.rotation_property =
13564 drm_mode_create_rotation_property(dev, flags);
13565 }
13566 if (dev->mode_config.rotation_property)
13567 drm_object_attach_property(&plane->base.base,
13568 dev->mode_config.rotation_property,
13569 plane->base.state->rotation);
13570}
13571
Matt Roper3d7d6512014-06-10 08:28:13 -070013572static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013573intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013574 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013575 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013576{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013577 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013578 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013579 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013580 unsigned stride;
13581 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013582
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013583 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13584 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013585 DRM_PLANE_HELPER_NO_SCALING,
13586 DRM_PLANE_HELPER_NO_SCALING,
13587 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013588 if (ret)
13589 return ret;
13590
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013591 /* if we want to turn off the cursor ignore width and height */
13592 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013593 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013594
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013595 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013596 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013597 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13598 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013599 return -EINVAL;
13600 }
13601
Matt Roperea2c67b2014-12-23 10:41:52 -080013602 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13603 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013604 DRM_DEBUG_KMS("buffer is too small\n");
13605 return -ENOMEM;
13606 }
13607
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013608 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013609 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013610 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013611 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013612
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013613 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013614}
13615
Matt Roperf4a2cf22014-12-01 15:40:12 -080013616static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013617intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013618 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013619{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013620 intel_crtc_update_cursor(crtc, false);
13621}
13622
13623static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013624intel_commit_cursor_plane(struct drm_plane *plane,
13625 struct intel_plane_state *state)
13626{
Matt Roper2b875c22014-12-01 15:40:13 -080013627 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013628 struct drm_device *dev = plane->dev;
13629 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013630 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013631 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013632
Matt Roperea2c67b2014-12-23 10:41:52 -080013633 crtc = crtc ? crtc : plane->crtc;
13634 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013635
Matt Roperea2c67b2014-12-23 10:41:52 -080013636 plane->fb = state->base.fb;
13637 crtc->cursor_x = state->base.crtc_x;
13638 crtc->cursor_y = state->base.crtc_y;
13639
Gustavo Padovana912f122014-12-01 15:40:10 -080013640 if (intel_crtc->cursor_bo == obj)
13641 goto update;
13642
Matt Roperf4a2cf22014-12-01 15:40:12 -080013643 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013644 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013645 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013646 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013647 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013648 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013649
Gustavo Padovana912f122014-12-01 15:40:10 -080013650 intel_crtc->cursor_addr = addr;
13651 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080013652
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013653update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013654 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013655 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013656}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013657
Matt Roper3d7d6512014-06-10 08:28:13 -070013658static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13659 int pipe)
13660{
13661 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013662 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013663
13664 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13665 if (cursor == NULL)
13666 return NULL;
13667
Matt Roper8e7d6882015-01-21 16:35:41 -080013668 state = intel_create_plane_state(&cursor->base);
13669 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013670 kfree(cursor);
13671 return NULL;
13672 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013673 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013674
Matt Roper3d7d6512014-06-10 08:28:13 -070013675 cursor->can_scale = false;
13676 cursor->max_downscale = 1;
13677 cursor->pipe = pipe;
13678 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013679 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013680 cursor->check_plane = intel_check_cursor_plane;
13681 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013682 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013683
13684 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013685 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013686 intel_cursor_formats,
13687 ARRAY_SIZE(intel_cursor_formats),
13688 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013689
13690 if (INTEL_INFO(dev)->gen >= 4) {
13691 if (!dev->mode_config.rotation_property)
13692 dev->mode_config.rotation_property =
13693 drm_mode_create_rotation_property(dev,
13694 BIT(DRM_ROTATE_0) |
13695 BIT(DRM_ROTATE_180));
13696 if (dev->mode_config.rotation_property)
13697 drm_object_attach_property(&cursor->base.base,
13698 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013699 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013700 }
13701
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013702 if (INTEL_INFO(dev)->gen >=9)
13703 state->scaler_id = -1;
13704
Matt Roperea2c67b2014-12-23 10:41:52 -080013705 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13706
Matt Roper3d7d6512014-06-10 08:28:13 -070013707 return &cursor->base;
13708}
13709
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013710static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13711 struct intel_crtc_state *crtc_state)
13712{
13713 int i;
13714 struct intel_scaler *intel_scaler;
13715 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13716
13717 for (i = 0; i < intel_crtc->num_scalers; i++) {
13718 intel_scaler = &scaler_state->scalers[i];
13719 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013720 intel_scaler->mode = PS_SCALER_MODE_DYN;
13721 }
13722
13723 scaler_state->scaler_id = -1;
13724}
13725
Hannes Ederb358d0a2008-12-18 21:18:47 +010013726static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013727{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013728 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013729 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013730 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013731 struct drm_plane *primary = NULL;
13732 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013733 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013734
Daniel Vetter955382f2013-09-19 14:05:45 +020013735 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013736 if (intel_crtc == NULL)
13737 return;
13738
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013739 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13740 if (!crtc_state)
13741 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013742 intel_crtc->config = crtc_state;
13743 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013744 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013745
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013746 /* initialize shared scalers */
13747 if (INTEL_INFO(dev)->gen >= 9) {
13748 if (pipe == PIPE_C)
13749 intel_crtc->num_scalers = 1;
13750 else
13751 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13752
13753 skl_init_scalers(dev, intel_crtc, crtc_state);
13754 }
13755
Matt Roper465c1202014-05-29 08:06:54 -070013756 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013757 if (!primary)
13758 goto fail;
13759
13760 cursor = intel_cursor_plane_create(dev, pipe);
13761 if (!cursor)
13762 goto fail;
13763
Matt Roper465c1202014-05-29 08:06:54 -070013764 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013765 cursor, &intel_crtc_funcs);
13766 if (ret)
13767 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013768
13769 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013770 for (i = 0; i < 256; i++) {
13771 intel_crtc->lut_r[i] = i;
13772 intel_crtc->lut_g[i] = i;
13773 intel_crtc->lut_b[i] = i;
13774 }
13775
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013776 /*
13777 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013778 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013779 */
Jesse Barnes80824002009-09-10 15:28:06 -070013780 intel_crtc->pipe = pipe;
13781 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013782 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013783 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013784 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013785 }
13786
Chris Wilson4b0e3332014-05-30 16:35:26 +030013787 intel_crtc->cursor_base = ~0;
13788 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013789 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013790
Ville Syrjälä852eb002015-06-24 22:00:07 +030013791 intel_crtc->wm.cxsr_allowed = true;
13792
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013793 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13794 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13795 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13796 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13797
Jesse Barnes79e53942008-11-07 14:24:08 -080013798 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013799
13800 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013801 return;
13802
13803fail:
13804 if (primary)
13805 drm_plane_cleanup(primary);
13806 if (cursor)
13807 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013808 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013809 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013810}
13811
Jesse Barnes752aa882013-10-31 18:55:49 +020013812enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13813{
13814 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013815 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013816
Rob Clark51fd3712013-11-19 12:10:12 -050013817 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013818
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013819 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013820 return INVALID_PIPE;
13821
13822 return to_intel_crtc(encoder->crtc)->pipe;
13823}
13824
Carl Worth08d7b3d2009-04-29 14:43:54 -070013825int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013826 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013827{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013828 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013829 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013830 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013831
Rob Clark7707e652014-07-17 23:30:04 -040013832 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013833
Rob Clark7707e652014-07-17 23:30:04 -040013834 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013835 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013836 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013837 }
13838
Rob Clark7707e652014-07-17 23:30:04 -040013839 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013840 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013841
Daniel Vetterc05422d2009-08-11 16:05:30 +020013842 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013843}
13844
Daniel Vetter66a92782012-07-12 20:08:18 +020013845static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013846{
Daniel Vetter66a92782012-07-12 20:08:18 +020013847 struct drm_device *dev = encoder->base.dev;
13848 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013849 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013850 int entry = 0;
13851
Damien Lespiaub2784e12014-08-05 11:29:37 +010013852 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013853 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013854 index_mask |= (1 << entry);
13855
Jesse Barnes79e53942008-11-07 14:24:08 -080013856 entry++;
13857 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013858
Jesse Barnes79e53942008-11-07 14:24:08 -080013859 return index_mask;
13860}
13861
Chris Wilson4d302442010-12-14 19:21:29 +000013862static bool has_edp_a(struct drm_device *dev)
13863{
13864 struct drm_i915_private *dev_priv = dev->dev_private;
13865
13866 if (!IS_MOBILE(dev))
13867 return false;
13868
13869 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13870 return false;
13871
Damien Lespiaue3589902014-02-07 19:12:50 +000013872 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013873 return false;
13874
13875 return true;
13876}
13877
Jesse Barnes84b4e042014-06-25 08:24:29 -070013878static bool intel_crt_present(struct drm_device *dev)
13879{
13880 struct drm_i915_private *dev_priv = dev->dev_private;
13881
Damien Lespiau884497e2013-12-03 13:56:23 +000013882 if (INTEL_INFO(dev)->gen >= 9)
13883 return false;
13884
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013885 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013886 return false;
13887
13888 if (IS_CHERRYVIEW(dev))
13889 return false;
13890
13891 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13892 return false;
13893
13894 return true;
13895}
13896
Jesse Barnes79e53942008-11-07 14:24:08 -080013897static void intel_setup_outputs(struct drm_device *dev)
13898{
Eric Anholt725e30a2009-01-22 13:01:02 -080013899 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013900 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013901 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013902
Daniel Vetterc9093352013-06-06 22:22:47 +020013903 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013904
Jesse Barnes84b4e042014-06-25 08:24:29 -070013905 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013906 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013907
Vandana Kannanc776eb22014-08-19 12:05:01 +053013908 if (IS_BROXTON(dev)) {
13909 /*
13910 * FIXME: Broxton doesn't support port detection via the
13911 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13912 * detect the ports.
13913 */
13914 intel_ddi_init(dev, PORT_A);
13915 intel_ddi_init(dev, PORT_B);
13916 intel_ddi_init(dev, PORT_C);
13917 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013918 int found;
13919
Jesse Barnesde31fac2015-03-06 15:53:32 -080013920 /*
13921 * Haswell uses DDI functions to detect digital outputs.
13922 * On SKL pre-D0 the strap isn't connected, so we assume
13923 * it's there.
13924 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013925 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013926 /* WaIgnoreDDIAStrap: skl */
Jani Nikula5a2376d2015-08-14 10:53:17 +030013927 if (found || IS_SKYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013928 intel_ddi_init(dev, PORT_A);
13929
13930 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13931 * register */
13932 found = I915_READ(SFUSE_STRAP);
13933
13934 if (found & SFUSE_STRAP_DDIB_DETECTED)
13935 intel_ddi_init(dev, PORT_B);
13936 if (found & SFUSE_STRAP_DDIC_DETECTED)
13937 intel_ddi_init(dev, PORT_C);
13938 if (found & SFUSE_STRAP_DDID_DETECTED)
13939 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070013940 /*
13941 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
13942 */
13943 if (IS_SKYLAKE(dev) &&
13944 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
13945 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
13946 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
13947 intel_ddi_init(dev, PORT_E);
13948
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013949 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013950 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013951 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013952
13953 if (has_edp_a(dev))
13954 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013955
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013956 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013957 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013958 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013959 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013960 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013961 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013962 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013963 }
13964
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013965 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013966 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013967
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013968 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013969 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013970
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013971 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013972 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013973
Daniel Vetter270b3042012-10-27 15:52:05 +020013974 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013975 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013976 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013977 /*
13978 * The DP_DETECTED bit is the latched state of the DDC
13979 * SDA pin at boot. However since eDP doesn't require DDC
13980 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13981 * eDP ports may have been muxed to an alternate function.
13982 * Thus we can't rely on the DP_DETECTED bit alone to detect
13983 * eDP ports. Consult the VBT as well as DP_DETECTED to
13984 * detect eDP ports.
13985 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013986 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13987 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013988 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13989 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013990 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13991 intel_dp_is_edp(dev, PORT_B))
13992 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013993
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013994 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13995 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013996 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13997 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013998 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13999 intel_dp_is_edp(dev, PORT_C))
14000 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014001
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014002 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014003 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014004 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14005 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014006 /* eDP not supported on port D, so don't check VBT */
14007 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14008 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014009 }
14010
Jani Nikula3cfca972013-08-27 15:12:26 +030014011 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014012 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014013 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014014
Paulo Zanonie2debe92013-02-18 19:00:27 -030014015 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014016 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014017 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014018 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014019 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014020 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014021 }
Ma Ling27185ae2009-08-24 13:50:23 +080014022
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014023 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014024 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014025 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014026
14027 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014028
Paulo Zanonie2debe92013-02-18 19:00:27 -030014029 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014030 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014031 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014032 }
Ma Ling27185ae2009-08-24 13:50:23 +080014033
Paulo Zanonie2debe92013-02-18 19:00:27 -030014034 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014035
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014036 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014037 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014038 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014039 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014040 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014041 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014042 }
Ma Ling27185ae2009-08-24 13:50:23 +080014043
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014044 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014045 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014046 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014047 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014048 intel_dvo_init(dev);
14049
Zhenyu Wang103a1962009-11-27 11:44:36 +080014050 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014051 intel_tv_init(dev);
14052
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014053 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014054
Damien Lespiaub2784e12014-08-05 11:29:37 +010014055 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014056 encoder->base.possible_crtcs = encoder->crtc_mask;
14057 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014058 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014059 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014060
Paulo Zanonidde86e22012-12-01 12:04:25 -020014061 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014062
14063 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014064}
14065
14066static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14067{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014068 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014069 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014070
Daniel Vetteref2d6332014-02-10 18:00:38 +010014071 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014072 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014073 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014074 drm_gem_object_unreference(&intel_fb->obj->base);
14075 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014076 kfree(intel_fb);
14077}
14078
14079static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014080 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014081 unsigned int *handle)
14082{
14083 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014084 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014085
Chris Wilson05394f32010-11-08 19:18:58 +000014086 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014087}
14088
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014089static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14090 struct drm_file *file,
14091 unsigned flags, unsigned color,
14092 struct drm_clip_rect *clips,
14093 unsigned num_clips)
14094{
14095 struct drm_device *dev = fb->dev;
14096 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14097 struct drm_i915_gem_object *obj = intel_fb->obj;
14098
14099 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014100 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014101 mutex_unlock(&dev->struct_mutex);
14102
14103 return 0;
14104}
14105
Jesse Barnes79e53942008-11-07 14:24:08 -080014106static const struct drm_framebuffer_funcs intel_fb_funcs = {
14107 .destroy = intel_user_framebuffer_destroy,
14108 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014109 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014110};
14111
Damien Lespiaub3218032015-02-27 11:15:18 +000014112static
14113u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14114 uint32_t pixel_format)
14115{
14116 u32 gen = INTEL_INFO(dev)->gen;
14117
14118 if (gen >= 9) {
14119 /* "The stride in bytes must not exceed the of the size of 8K
14120 * pixels and 32K bytes."
14121 */
14122 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14123 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14124 return 32*1024;
14125 } else if (gen >= 4) {
14126 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14127 return 16*1024;
14128 else
14129 return 32*1024;
14130 } else if (gen >= 3) {
14131 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14132 return 8*1024;
14133 else
14134 return 16*1024;
14135 } else {
14136 /* XXX DSPC is limited to 4k tiled */
14137 return 8*1024;
14138 }
14139}
14140
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014141static int intel_framebuffer_init(struct drm_device *dev,
14142 struct intel_framebuffer *intel_fb,
14143 struct drm_mode_fb_cmd2 *mode_cmd,
14144 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014145{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014146 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014147 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014148 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014149
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014150 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14151
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014152 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14153 /* Enforce that fb modifier and tiling mode match, but only for
14154 * X-tiled. This is needed for FBC. */
14155 if (!!(obj->tiling_mode == I915_TILING_X) !=
14156 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14157 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14158 return -EINVAL;
14159 }
14160 } else {
14161 if (obj->tiling_mode == I915_TILING_X)
14162 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14163 else if (obj->tiling_mode == I915_TILING_Y) {
14164 DRM_DEBUG("No Y tiling for legacy addfb\n");
14165 return -EINVAL;
14166 }
14167 }
14168
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014169 /* Passed in modifier sanity checking. */
14170 switch (mode_cmd->modifier[0]) {
14171 case I915_FORMAT_MOD_Y_TILED:
14172 case I915_FORMAT_MOD_Yf_TILED:
14173 if (INTEL_INFO(dev)->gen < 9) {
14174 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14175 mode_cmd->modifier[0]);
14176 return -EINVAL;
14177 }
14178 case DRM_FORMAT_MOD_NONE:
14179 case I915_FORMAT_MOD_X_TILED:
14180 break;
14181 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014182 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14183 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014184 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014185 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014186
Damien Lespiaub3218032015-02-27 11:15:18 +000014187 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14188 mode_cmd->pixel_format);
14189 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14190 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14191 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014192 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014193 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014194
Damien Lespiaub3218032015-02-27 11:15:18 +000014195 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14196 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014197 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014198 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14199 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014200 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014201 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014202 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014203 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014204
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014205 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014206 mode_cmd->pitches[0] != obj->stride) {
14207 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14208 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014209 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014210 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014211
Ville Syrjälä57779d02012-10-31 17:50:14 +020014212 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014213 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014214 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014215 case DRM_FORMAT_RGB565:
14216 case DRM_FORMAT_XRGB8888:
14217 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014218 break;
14219 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014220 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014221 DRM_DEBUG("unsupported pixel format: %s\n",
14222 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014223 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014224 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014225 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014226 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014227 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14228 DRM_DEBUG("unsupported pixel format: %s\n",
14229 drm_get_format_name(mode_cmd->pixel_format));
14230 return -EINVAL;
14231 }
14232 break;
14233 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014234 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014235 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014236 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014237 DRM_DEBUG("unsupported pixel format: %s\n",
14238 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014239 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014240 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014241 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014242 case DRM_FORMAT_ABGR2101010:
14243 if (!IS_VALLEYVIEW(dev)) {
14244 DRM_DEBUG("unsupported pixel format: %s\n",
14245 drm_get_format_name(mode_cmd->pixel_format));
14246 return -EINVAL;
14247 }
14248 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014249 case DRM_FORMAT_YUYV:
14250 case DRM_FORMAT_UYVY:
14251 case DRM_FORMAT_YVYU:
14252 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014253 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014254 DRM_DEBUG("unsupported pixel format: %s\n",
14255 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014256 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014257 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014258 break;
14259 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014260 DRM_DEBUG("unsupported pixel format: %s\n",
14261 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014262 return -EINVAL;
14263 }
14264
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014265 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14266 if (mode_cmd->offsets[0] != 0)
14267 return -EINVAL;
14268
Damien Lespiauec2c9812015-01-20 12:51:45 +000014269 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014270 mode_cmd->pixel_format,
14271 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014272 /* FIXME drm helper for size checks (especially planar formats)? */
14273 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14274 return -EINVAL;
14275
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014276 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14277 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014278 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014279
Jesse Barnes79e53942008-11-07 14:24:08 -080014280 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14281 if (ret) {
14282 DRM_ERROR("framebuffer init failed %d\n", ret);
14283 return ret;
14284 }
14285
Jesse Barnes79e53942008-11-07 14:24:08 -080014286 return 0;
14287}
14288
Jesse Barnes79e53942008-11-07 14:24:08 -080014289static struct drm_framebuffer *
14290intel_user_framebuffer_create(struct drm_device *dev,
14291 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014292 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014293{
Chris Wilson05394f32010-11-08 19:18:58 +000014294 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014295
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014296 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14297 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014298 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014299 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014300
Chris Wilsond2dff872011-04-19 08:36:26 +010014301 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014302}
14303
Daniel Vetter06957262015-08-10 13:34:08 +020014304#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014305static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014306{
14307}
14308#endif
14309
Jesse Barnes79e53942008-11-07 14:24:08 -080014310static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014311 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014312 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014313 .atomic_check = intel_atomic_check,
14314 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014315 .atomic_state_alloc = intel_atomic_state_alloc,
14316 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014317};
14318
Jesse Barnese70236a2009-09-21 10:42:27 -070014319/* Set up chip specific display functions */
14320static void intel_init_display(struct drm_device *dev)
14321{
14322 struct drm_i915_private *dev_priv = dev->dev_private;
14323
Daniel Vetteree9300b2013-06-03 22:40:22 +020014324 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14325 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014326 else if (IS_CHERRYVIEW(dev))
14327 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014328 else if (IS_VALLEYVIEW(dev))
14329 dev_priv->display.find_dpll = vlv_find_best_dpll;
14330 else if (IS_PINEVIEW(dev))
14331 dev_priv->display.find_dpll = pnv_find_best_dpll;
14332 else
14333 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14334
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014335 if (INTEL_INFO(dev)->gen >= 9) {
14336 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014337 dev_priv->display.get_initial_plane_config =
14338 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014339 dev_priv->display.crtc_compute_clock =
14340 haswell_crtc_compute_clock;
14341 dev_priv->display.crtc_enable = haswell_crtc_enable;
14342 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014343 dev_priv->display.update_primary_plane =
14344 skylake_update_primary_plane;
14345 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014346 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014347 dev_priv->display.get_initial_plane_config =
14348 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014349 dev_priv->display.crtc_compute_clock =
14350 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014351 dev_priv->display.crtc_enable = haswell_crtc_enable;
14352 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014353 dev_priv->display.update_primary_plane =
14354 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014355 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014356 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014357 dev_priv->display.get_initial_plane_config =
14358 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014359 dev_priv->display.crtc_compute_clock =
14360 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014361 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14362 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014363 dev_priv->display.update_primary_plane =
14364 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014365 } else if (IS_VALLEYVIEW(dev)) {
14366 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014367 dev_priv->display.get_initial_plane_config =
14368 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014369 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014370 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14371 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014372 dev_priv->display.update_primary_plane =
14373 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014374 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014375 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014376 dev_priv->display.get_initial_plane_config =
14377 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014378 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014379 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14380 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014381 dev_priv->display.update_primary_plane =
14382 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014383 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014384
Jesse Barnese70236a2009-09-21 10:42:27 -070014385 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014386 if (IS_SKYLAKE(dev))
14387 dev_priv->display.get_display_clock_speed =
14388 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014389 else if (IS_BROXTON(dev))
14390 dev_priv->display.get_display_clock_speed =
14391 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014392 else if (IS_BROADWELL(dev))
14393 dev_priv->display.get_display_clock_speed =
14394 broadwell_get_display_clock_speed;
14395 else if (IS_HASWELL(dev))
14396 dev_priv->display.get_display_clock_speed =
14397 haswell_get_display_clock_speed;
14398 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014399 dev_priv->display.get_display_clock_speed =
14400 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014401 else if (IS_GEN5(dev))
14402 dev_priv->display.get_display_clock_speed =
14403 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014404 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014405 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014406 dev_priv->display.get_display_clock_speed =
14407 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014408 else if (IS_GM45(dev))
14409 dev_priv->display.get_display_clock_speed =
14410 gm45_get_display_clock_speed;
14411 else if (IS_CRESTLINE(dev))
14412 dev_priv->display.get_display_clock_speed =
14413 i965gm_get_display_clock_speed;
14414 else if (IS_PINEVIEW(dev))
14415 dev_priv->display.get_display_clock_speed =
14416 pnv_get_display_clock_speed;
14417 else if (IS_G33(dev) || IS_G4X(dev))
14418 dev_priv->display.get_display_clock_speed =
14419 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014420 else if (IS_I915G(dev))
14421 dev_priv->display.get_display_clock_speed =
14422 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014423 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014424 dev_priv->display.get_display_clock_speed =
14425 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014426 else if (IS_PINEVIEW(dev))
14427 dev_priv->display.get_display_clock_speed =
14428 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014429 else if (IS_I915GM(dev))
14430 dev_priv->display.get_display_clock_speed =
14431 i915gm_get_display_clock_speed;
14432 else if (IS_I865G(dev))
14433 dev_priv->display.get_display_clock_speed =
14434 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014435 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014436 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014437 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014438 else { /* 830 */
14439 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014440 dev_priv->display.get_display_clock_speed =
14441 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014442 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014443
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014444 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014445 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014446 } else if (IS_GEN6(dev)) {
14447 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014448 } else if (IS_IVYBRIDGE(dev)) {
14449 /* FIXME: detect B0+ stepping and use auto training */
14450 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014451 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014452 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014453 if (IS_BROADWELL(dev)) {
14454 dev_priv->display.modeset_commit_cdclk =
14455 broadwell_modeset_commit_cdclk;
14456 dev_priv->display.modeset_calc_cdclk =
14457 broadwell_modeset_calc_cdclk;
14458 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014459 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014460 dev_priv->display.modeset_commit_cdclk =
14461 valleyview_modeset_commit_cdclk;
14462 dev_priv->display.modeset_calc_cdclk =
14463 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014464 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014465 dev_priv->display.modeset_commit_cdclk =
14466 broxton_modeset_commit_cdclk;
14467 dev_priv->display.modeset_calc_cdclk =
14468 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014469 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014470
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014471 switch (INTEL_INFO(dev)->gen) {
14472 case 2:
14473 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14474 break;
14475
14476 case 3:
14477 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14478 break;
14479
14480 case 4:
14481 case 5:
14482 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14483 break;
14484
14485 case 6:
14486 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14487 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014488 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014489 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014490 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14491 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014492 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014493 /* Drop through - unsupported since execlist only. */
14494 default:
14495 /* Default just returns -ENODEV to indicate unsupported */
14496 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014497 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014498
14499 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014500
14501 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014502}
14503
Jesse Barnesb690e962010-07-19 13:53:12 -070014504/*
14505 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14506 * resume, or other times. This quirk makes sure that's the case for
14507 * affected systems.
14508 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014509static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014510{
14511 struct drm_i915_private *dev_priv = dev->dev_private;
14512
14513 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014514 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014515}
14516
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014517static void quirk_pipeb_force(struct drm_device *dev)
14518{
14519 struct drm_i915_private *dev_priv = dev->dev_private;
14520
14521 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14522 DRM_INFO("applying pipe b force quirk\n");
14523}
14524
Keith Packard435793d2011-07-12 14:56:22 -070014525/*
14526 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14527 */
14528static void quirk_ssc_force_disable(struct drm_device *dev)
14529{
14530 struct drm_i915_private *dev_priv = dev->dev_private;
14531 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014532 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014533}
14534
Carsten Emde4dca20e2012-03-15 15:56:26 +010014535/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014536 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14537 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014538 */
14539static void quirk_invert_brightness(struct drm_device *dev)
14540{
14541 struct drm_i915_private *dev_priv = dev->dev_private;
14542 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014543 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014544}
14545
Scot Doyle9c72cc62014-07-03 23:27:50 +000014546/* Some VBT's incorrectly indicate no backlight is present */
14547static void quirk_backlight_present(struct drm_device *dev)
14548{
14549 struct drm_i915_private *dev_priv = dev->dev_private;
14550 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14551 DRM_INFO("applying backlight present quirk\n");
14552}
14553
Jesse Barnesb690e962010-07-19 13:53:12 -070014554struct intel_quirk {
14555 int device;
14556 int subsystem_vendor;
14557 int subsystem_device;
14558 void (*hook)(struct drm_device *dev);
14559};
14560
Egbert Eich5f85f172012-10-14 15:46:38 +020014561/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14562struct intel_dmi_quirk {
14563 void (*hook)(struct drm_device *dev);
14564 const struct dmi_system_id (*dmi_id_list)[];
14565};
14566
14567static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14568{
14569 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14570 return 1;
14571}
14572
14573static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14574 {
14575 .dmi_id_list = &(const struct dmi_system_id[]) {
14576 {
14577 .callback = intel_dmi_reverse_brightness,
14578 .ident = "NCR Corporation",
14579 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14580 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14581 },
14582 },
14583 { } /* terminating entry */
14584 },
14585 .hook = quirk_invert_brightness,
14586 },
14587};
14588
Ben Widawskyc43b5632012-04-16 14:07:40 -070014589static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014590 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14591 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14592
Jesse Barnesb690e962010-07-19 13:53:12 -070014593 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14594 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14595
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014596 /* 830 needs to leave pipe A & dpll A up */
14597 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14598
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014599 /* 830 needs to leave pipe B & dpll B up */
14600 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14601
Keith Packard435793d2011-07-12 14:56:22 -070014602 /* Lenovo U160 cannot use SSC on LVDS */
14603 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014604
14605 /* Sony Vaio Y cannot use SSC on LVDS */
14606 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014607
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014608 /* Acer Aspire 5734Z must invert backlight brightness */
14609 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14610
14611 /* Acer/eMachines G725 */
14612 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14613
14614 /* Acer/eMachines e725 */
14615 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14616
14617 /* Acer/Packard Bell NCL20 */
14618 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14619
14620 /* Acer Aspire 4736Z */
14621 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014622
14623 /* Acer Aspire 5336 */
14624 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014625
14626 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14627 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014628
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014629 /* Acer C720 Chromebook (Core i3 4005U) */
14630 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14631
jens steinb2a96012014-10-28 20:25:53 +010014632 /* Apple Macbook 2,1 (Core 2 T7400) */
14633 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14634
Scot Doyled4967d82014-07-03 23:27:52 +000014635 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14636 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014637
14638 /* HP Chromebook 14 (Celeron 2955U) */
14639 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014640
14641 /* Dell Chromebook 11 */
14642 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014643};
14644
14645static void intel_init_quirks(struct drm_device *dev)
14646{
14647 struct pci_dev *d = dev->pdev;
14648 int i;
14649
14650 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14651 struct intel_quirk *q = &intel_quirks[i];
14652
14653 if (d->device == q->device &&
14654 (d->subsystem_vendor == q->subsystem_vendor ||
14655 q->subsystem_vendor == PCI_ANY_ID) &&
14656 (d->subsystem_device == q->subsystem_device ||
14657 q->subsystem_device == PCI_ANY_ID))
14658 q->hook(dev);
14659 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014660 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14661 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14662 intel_dmi_quirks[i].hook(dev);
14663 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014664}
14665
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014666/* Disable the VGA plane that we never use */
14667static void i915_disable_vga(struct drm_device *dev)
14668{
14669 struct drm_i915_private *dev_priv = dev->dev_private;
14670 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014671 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014672
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014673 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014674 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014675 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014676 sr1 = inb(VGA_SR_DATA);
14677 outb(sr1 | 1<<5, VGA_SR_DATA);
14678 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14679 udelay(300);
14680
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014681 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014682 POSTING_READ(vga_reg);
14683}
14684
Daniel Vetterf8175862012-04-10 15:50:11 +020014685void intel_modeset_init_hw(struct drm_device *dev)
14686{
Ville Syrjäläb6283052015-06-03 15:45:07 +030014687 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014688 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014689 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014690 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014691}
14692
Jesse Barnes79e53942008-11-07 14:24:08 -080014693void intel_modeset_init(struct drm_device *dev)
14694{
Jesse Barnes652c3932009-08-17 13:31:43 -070014695 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014696 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014697 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014698 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014699
14700 drm_mode_config_init(dev);
14701
14702 dev->mode_config.min_width = 0;
14703 dev->mode_config.min_height = 0;
14704
Dave Airlie019d96c2011-09-29 16:20:42 +010014705 dev->mode_config.preferred_depth = 24;
14706 dev->mode_config.prefer_shadow = 1;
14707
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014708 dev->mode_config.allow_fb_modifiers = true;
14709
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014710 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014711
Jesse Barnesb690e962010-07-19 13:53:12 -070014712 intel_init_quirks(dev);
14713
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014714 intel_init_pm(dev);
14715
Ben Widawskye3c74752013-04-05 13:12:39 -070014716 if (INTEL_INFO(dev)->num_pipes == 0)
14717 return;
14718
Lukas Wunner69f92f62015-07-15 13:57:35 +020014719 /*
14720 * There may be no VBT; and if the BIOS enabled SSC we can
14721 * just keep using it to avoid unnecessary flicker. Whereas if the
14722 * BIOS isn't using it, don't assume it will work even if the VBT
14723 * indicates as much.
14724 */
14725 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
14726 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14727 DREF_SSC1_ENABLE);
14728
14729 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14730 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14731 bios_lvds_use_ssc ? "en" : "dis",
14732 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14733 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14734 }
14735 }
14736
Jesse Barnese70236a2009-09-21 10:42:27 -070014737 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014738 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014739
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014740 if (IS_GEN2(dev)) {
14741 dev->mode_config.max_width = 2048;
14742 dev->mode_config.max_height = 2048;
14743 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014744 dev->mode_config.max_width = 4096;
14745 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014746 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014747 dev->mode_config.max_width = 8192;
14748 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014749 }
Damien Lespiau068be562014-03-28 14:17:49 +000014750
Ville Syrjälädc41c152014-08-13 11:57:05 +030014751 if (IS_845G(dev) || IS_I865G(dev)) {
14752 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14753 dev->mode_config.cursor_height = 1023;
14754 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014755 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14756 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14757 } else {
14758 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14759 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14760 }
14761
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014762 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014763
Zhao Yakui28c97732009-10-09 11:39:41 +080014764 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014765 INTEL_INFO(dev)->num_pipes,
14766 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014767
Damien Lespiau055e3932014-08-18 13:49:10 +010014768 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014769 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014770 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014771 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014772 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014773 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014774 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014775 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014776 }
14777
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014778 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014779
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014780 /* Just disable it once at startup */
14781 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014782 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014783
14784 /* Just in case the BIOS is doing something questionable. */
Paulo Zanoni7733b492015-07-07 15:26:04 -030014785 intel_fbc_disable(dev_priv);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014786
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014787 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020014788 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014789 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014790
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014791 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014792 struct intel_initial_plane_config plane_config = {};
14793
Jesse Barnes46f297f2014-03-07 08:57:48 -080014794 if (!crtc->active)
14795 continue;
14796
Jesse Barnes46f297f2014-03-07 08:57:48 -080014797 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014798 * Note that reserving the BIOS fb up front prevents us
14799 * from stuffing other stolen allocations like the ring
14800 * on top. This prevents some ugliness at boot time, and
14801 * can even allow for smooth boot transitions if the BIOS
14802 * fb is large enough for the active pipe configuration.
14803 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014804 dev_priv->display.get_initial_plane_config(crtc,
14805 &plane_config);
14806
14807 /*
14808 * If the fb is shared between multiple heads, we'll
14809 * just get the first one.
14810 */
14811 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014812 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014813}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014814
Daniel Vetter7fad7982012-07-04 17:51:47 +020014815static void intel_enable_pipe_a(struct drm_device *dev)
14816{
14817 struct intel_connector *connector;
14818 struct drm_connector *crt = NULL;
14819 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014820 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014821
14822 /* We can't just switch on the pipe A, we need to set things up with a
14823 * proper mode and output configuration. As a gross hack, enable pipe A
14824 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014825 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014826 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14827 crt = &connector->base;
14828 break;
14829 }
14830 }
14831
14832 if (!crt)
14833 return;
14834
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014835 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014836 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014837}
14838
Daniel Vetterfa555832012-10-10 23:14:00 +020014839static bool
14840intel_check_plane_mapping(struct intel_crtc *crtc)
14841{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014842 struct drm_device *dev = crtc->base.dev;
14843 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014844 u32 reg, val;
14845
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014846 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014847 return true;
14848
14849 reg = DSPCNTR(!crtc->plane);
14850 val = I915_READ(reg);
14851
14852 if ((val & DISPLAY_PLANE_ENABLE) &&
14853 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14854 return false;
14855
14856 return true;
14857}
14858
Ville Syrjälä02e93c32015-08-26 19:39:19 +030014859static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
14860{
14861 struct drm_device *dev = crtc->base.dev;
14862 struct intel_encoder *encoder;
14863
14864 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
14865 return true;
14866
14867 return false;
14868}
14869
Daniel Vetter24929352012-07-02 20:28:59 +020014870static void intel_sanitize_crtc(struct intel_crtc *crtc)
14871{
14872 struct drm_device *dev = crtc->base.dev;
14873 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014874 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014875
Daniel Vetter24929352012-07-02 20:28:59 +020014876 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014877 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014878 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14879
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014880 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014881 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014882 if (crtc->active) {
Maarten Lankhorst3a03dfb2015-07-14 13:46:40 +020014883 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014884 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014885 drm_crtc_vblank_on(&crtc->base);
14886 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014887
Daniel Vetter24929352012-07-02 20:28:59 +020014888 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014889 * disable the crtc (and hence change the state) if it is wrong. Note
14890 * that gen4+ has a fixed plane -> pipe mapping. */
14891 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014892 bool plane;
14893
Daniel Vetter24929352012-07-02 20:28:59 +020014894 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14895 crtc->base.base.id);
14896
14897 /* Pipe has the wrong plane attached and the plane is active.
14898 * Temporarily change the plane mapping and disable everything
14899 * ... */
14900 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014901 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014902 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014903 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014904 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020014905 }
Daniel Vetter24929352012-07-02 20:28:59 +020014906
Daniel Vetter7fad7982012-07-04 17:51:47 +020014907 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14908 crtc->pipe == PIPE_A && !crtc->active) {
14909 /* BIOS forgot to enable pipe A, this mostly happens after
14910 * resume. Force-enable the pipe to fix this, the update_dpms
14911 * call below we restore the pipe to the right state, but leave
14912 * the required bits on. */
14913 intel_enable_pipe_a(dev);
14914 }
14915
Daniel Vetter24929352012-07-02 20:28:59 +020014916 /* Adjust the state of the output pipe according to whether we
14917 * have active connectors/encoders. */
Ville Syrjälä02e93c32015-08-26 19:39:19 +030014918 if (!intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014919 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014920
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020014921 if (crtc->active != crtc->base.state->active) {
Ville Syrjälä02e93c32015-08-26 19:39:19 +030014922 struct intel_encoder *encoder;
Daniel Vetter24929352012-07-02 20:28:59 +020014923
14924 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014925 * functions or because of calls to intel_crtc_disable_noatomic,
14926 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020014927 * pipe A quirk. */
14928 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14929 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014930 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014931 crtc->active ? "enabled" : "disabled");
14932
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020014933 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014934 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014935 crtc->base.enabled = crtc->active;
14936
14937 /* Because we only establish the connector -> encoder ->
14938 * crtc links if something is active, this means the
14939 * crtc is now deactivated. Break the links. connector
14940 * -> encoder links are only establish when things are
14941 * actually up, hence no need to break them. */
14942 WARN_ON(crtc->active);
14943
Maarten Lankhorst2d406bb2015-08-05 12:37:09 +020014944 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetter24929352012-07-02 20:28:59 +020014945 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014946 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014947
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014948 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014949 /*
14950 * We start out with underrun reporting disabled to avoid races.
14951 * For correct bookkeeping mark this on active crtcs.
14952 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014953 * Also on gmch platforms we dont have any hardware bits to
14954 * disable the underrun reporting. Which means we need to start
14955 * out with underrun reporting disabled also on inactive pipes,
14956 * since otherwise we'll complain about the garbage we read when
14957 * e.g. coming up after runtime pm.
14958 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014959 * No protection against concurrent access is required - at
14960 * worst a fifo underrun happens which also sets this to false.
14961 */
14962 crtc->cpu_fifo_underrun_disabled = true;
14963 crtc->pch_fifo_underrun_disabled = true;
14964 }
Daniel Vetter24929352012-07-02 20:28:59 +020014965}
14966
14967static void intel_sanitize_encoder(struct intel_encoder *encoder)
14968{
14969 struct intel_connector *connector;
14970 struct drm_device *dev = encoder->base.dev;
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020014971 bool active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014972
14973 /* We need to check both for a crtc link (meaning that the
14974 * encoder is active and trying to read from a pipe) and the
14975 * pipe itself being active. */
14976 bool has_active_crtc = encoder->base.crtc &&
14977 to_intel_crtc(encoder->base.crtc)->active;
14978
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020014979 for_each_intel_connector(dev, connector) {
14980 if (connector->base.encoder != &encoder->base)
14981 continue;
14982
14983 active = true;
14984 break;
14985 }
14986
14987 if (active && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020014988 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14989 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014990 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014991
14992 /* Connector is active, but has no active pipe. This is
14993 * fallout from our resume register restoring. Disable
14994 * the encoder manually again. */
14995 if (encoder->base.crtc) {
14996 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14997 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014998 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014999 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015000 if (encoder->post_disable)
15001 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015002 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015003 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015004
15005 /* Inconsistent output/port/pipe state happens presumably due to
15006 * a bug in one of the get_hw_state functions. Or someplace else
15007 * in our code, like the register restore mess on resume. Clamp
15008 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015009 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015010 if (connector->encoder != encoder)
15011 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015012 connector->base.dpms = DRM_MODE_DPMS_OFF;
15013 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015014 }
15015 }
15016 /* Enabled encoders without active connectors will be fixed in
15017 * the crtc fixup. */
15018}
15019
Imre Deak04098752014-02-18 00:02:16 +020015020void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015021{
15022 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015023 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015024
Imre Deak04098752014-02-18 00:02:16 +020015025 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15026 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15027 i915_disable_vga(dev);
15028 }
15029}
15030
15031void i915_redisable_vga(struct drm_device *dev)
15032{
15033 struct drm_i915_private *dev_priv = dev->dev_private;
15034
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015035 /* This function can be called both from intel_modeset_setup_hw_state or
15036 * at a very early point in our resume sequence, where the power well
15037 * structures are not yet restored. Since this function is at a very
15038 * paranoid "someone might have enabled VGA while we were not looking"
15039 * level, just check if the power well is enabled instead of trying to
15040 * follow the "don't touch the power well if we don't need it" policy
15041 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015042 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015043 return;
15044
Imre Deak04098752014-02-18 00:02:16 +020015045 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015046}
15047
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015048static bool primary_get_hw_state(struct intel_crtc *crtc)
15049{
15050 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15051
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015052 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15053}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015054
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015055static void readout_plane_state(struct intel_crtc *crtc,
15056 struct intel_crtc_state *crtc_state)
15057{
15058 struct intel_plane *p;
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015059 struct intel_plane_state *plane_state;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015060 bool active = crtc_state->base.active;
15061
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015062 for_each_intel_plane(crtc->base.dev, p) {
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015063 if (crtc->pipe != p->pipe)
15064 continue;
15065
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015066 plane_state = to_intel_plane_state(p->base.state);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015067
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015068 if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
15069 plane_state->visible = primary_get_hw_state(crtc);
15070 else {
15071 if (active)
15072 p->disable_plane(&p->base, &crtc->base);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015073
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015074 plane_state->visible = false;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015075 }
15076 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015077}
15078
Daniel Vetter30e984d2013-06-05 13:34:17 +020015079static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015080{
15081 struct drm_i915_private *dev_priv = dev->dev_private;
15082 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015083 struct intel_crtc *crtc;
15084 struct intel_encoder *encoder;
15085 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015086 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015087
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015088 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb06f8b02015-07-14 13:42:49 +020015089 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015090 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015091 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015092
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015093 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015094 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015095
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015096 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015097 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015098
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015099 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15100 if (crtc->base.state->active) {
15101 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15102 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15103 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15104
15105 /*
15106 * The initial mode needs to be set in order to keep
15107 * the atomic core happy. It wants a valid mode if the
15108 * crtc's enabled, so we do the above call.
15109 *
15110 * At this point some state updated by the connectors
15111 * in their ->detect() callback has not run yet, so
15112 * no recalculation can be done yet.
15113 *
15114 * Even if we could do a recalculation and modeset
15115 * right now it would cause a double modeset if
15116 * fbdev or userspace chooses a different initial mode.
15117 *
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015118 * If that happens, someone indicated they wanted a
15119 * mode change, which means it's safe to do a full
15120 * recalculation.
15121 */
Daniel Vetter1ed51de2015-07-15 14:15:51 +020015122 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015123 }
15124
15125 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015126 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015127
15128 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15129 crtc->base.base.id,
15130 crtc->active ? "enabled" : "disabled");
15131 }
15132
Daniel Vetter53589012013-06-05 13:34:16 +020015133 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15134 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15135
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015136 pll->on = pll->get_hw_state(dev_priv, pll,
15137 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015138 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015139 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015140 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015141 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015142 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015143 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015144 }
Daniel Vetter53589012013-06-05 13:34:16 +020015145 }
Daniel Vetter53589012013-06-05 13:34:16 +020015146
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015147 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015148 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015149
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015150 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015151 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015152 }
15153
Damien Lespiaub2784e12014-08-05 11:29:37 +010015154 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015155 pipe = 0;
15156
15157 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015158 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15159 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015160 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015161 } else {
15162 encoder->base.crtc = NULL;
15163 }
15164
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015165 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015166 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015167 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015168 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015169 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015170 }
15171
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015172 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015173 if (connector->get_hw_state(connector)) {
15174 connector->base.dpms = DRM_MODE_DPMS_ON;
Daniel Vetter24929352012-07-02 20:28:59 +020015175 connector->base.encoder = &connector->encoder->base;
15176 } else {
15177 connector->base.dpms = DRM_MODE_DPMS_OFF;
15178 connector->base.encoder = NULL;
15179 }
15180 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15181 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015182 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015183 connector->base.encoder ? "enabled" : "disabled");
15184 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015185}
15186
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015187/* Scan out the current hw modeset state,
15188 * and sanitizes it to the current state
15189 */
15190static void
15191intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015192{
15193 struct drm_i915_private *dev_priv = dev->dev_private;
15194 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015195 struct intel_crtc *crtc;
15196 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015197 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015198
15199 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015200
15201 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015202 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015203 intel_sanitize_encoder(encoder);
15204 }
15205
Damien Lespiau055e3932014-08-18 13:49:10 +010015206 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015207 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15208 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015209 intel_dump_pipe_config(crtc, crtc->config,
15210 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015211 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015212
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015213 intel_modeset_update_connector_atomic_state(dev);
15214
Daniel Vetter35c95372013-07-17 06:55:04 +020015215 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15216 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15217
15218 if (!pll->on || pll->active)
15219 continue;
15220
15221 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15222
15223 pll->disable(dev_priv, pll);
15224 pll->on = false;
15225 }
15226
Ville Syrjälä26e1fe42015-06-24 22:00:06 +030015227 if (IS_VALLEYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015228 vlv_wm_get_hw_state(dev);
15229 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015230 skl_wm_get_hw_state(dev);
15231 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015232 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015233
15234 for_each_intel_crtc(dev, crtc) {
15235 unsigned long put_domains;
15236
15237 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15238 if (WARN_ON(put_domains))
15239 modeset_put_power_domains(dev_priv, put_domains);
15240 }
15241 intel_display_set_init_power(dev_priv, false);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015242}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015243
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015244void intel_display_resume(struct drm_device *dev)
15245{
15246 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15247 struct intel_connector *conn;
15248 struct intel_plane *plane;
15249 struct drm_crtc *crtc;
15250 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015251
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015252 if (!state)
15253 return;
15254
15255 state->acquire_ctx = dev->mode_config.acquire_ctx;
15256
15257 /* preserve complete old state, including dpll */
15258 intel_atomic_get_shared_dpll_state(state);
15259
15260 for_each_crtc(dev, crtc) {
15261 struct drm_crtc_state *crtc_state =
15262 drm_atomic_get_crtc_state(state, crtc);
15263
15264 ret = PTR_ERR_OR_ZERO(crtc_state);
15265 if (ret)
15266 goto err;
15267
15268 /* force a restore */
15269 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015270 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015271
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015272 for_each_intel_plane(dev, plane) {
15273 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15274 if (ret)
15275 goto err;
15276 }
15277
15278 for_each_intel_connector(dev, conn) {
15279 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15280 if (ret)
15281 goto err;
15282 }
15283
15284 intel_modeset_setup_hw_state(dev);
15285
15286 i915_redisable_vga(dev);
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020015287 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015288 if (!ret)
15289 return;
15290
15291err:
15292 DRM_ERROR("Restoring old state failed with %i\n", ret);
15293 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015294}
15295
15296void intel_modeset_gem_init(struct drm_device *dev)
15297{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015298 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015299 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015300 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015301
Imre Deakae484342014-03-31 15:10:44 +030015302 mutex_lock(&dev->struct_mutex);
15303 intel_init_gt_powersave(dev);
15304 mutex_unlock(&dev->struct_mutex);
15305
Chris Wilson1833b132012-05-09 11:56:28 +010015306 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015307
15308 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015309
15310 /*
15311 * Make sure any fbs we allocated at startup are properly
15312 * pinned & fenced. When we do the allocation it's too early
15313 * for this.
15314 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015315 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015316 obj = intel_fb_obj(c->primary->fb);
15317 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015318 continue;
15319
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015320 mutex_lock(&dev->struct_mutex);
15321 ret = intel_pin_and_fence_fb_obj(c->primary,
15322 c->primary->fb,
15323 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015324 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015325 mutex_unlock(&dev->struct_mutex);
15326 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015327 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15328 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015329 drm_framebuffer_unreference(c->primary->fb);
15330 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015331 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015332 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015333 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015334 }
15335 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015336
15337 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015338}
15339
Imre Deak4932e2c2014-02-11 17:12:48 +020015340void intel_connector_unregister(struct intel_connector *intel_connector)
15341{
15342 struct drm_connector *connector = &intel_connector->base;
15343
15344 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015345 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015346}
15347
Jesse Barnes79e53942008-11-07 14:24:08 -080015348void intel_modeset_cleanup(struct drm_device *dev)
15349{
Jesse Barnes652c3932009-08-17 13:31:43 -070015350 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015351 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015352
Imre Deak2eb52522014-11-19 15:30:05 +020015353 intel_disable_gt_powersave(dev);
15354
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015355 intel_backlight_unregister(dev);
15356
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015357 /*
15358 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015359 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015360 * experience fancy races otherwise.
15361 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015362 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015363
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015364 /*
15365 * Due to the hpd irq storm handling the hotplug work can re-arm the
15366 * poll handlers. Hence disable polling after hpd handling is shut down.
15367 */
Keith Packardf87ea762010-10-03 19:36:26 -070015368 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015369
Jesse Barnes723bfd72010-10-07 16:01:13 -070015370 intel_unregister_dsm_handler();
15371
Paulo Zanoni7733b492015-07-07 15:26:04 -030015372 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015373
Chris Wilson1630fe72011-07-08 12:22:42 +010015374 /* flush any delayed tasks or pending work */
15375 flush_scheduled_work();
15376
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015377 /* destroy the backlight and sysfs files before encoders/connectors */
15378 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015379 struct intel_connector *intel_connector;
15380
15381 intel_connector = to_intel_connector(connector);
15382 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015383 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015384
Jesse Barnes79e53942008-11-07 14:24:08 -080015385 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015386
15387 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015388
15389 mutex_lock(&dev->struct_mutex);
15390 intel_cleanup_gt_powersave(dev);
15391 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015392}
15393
Dave Airlie28d52042009-09-21 14:33:58 +100015394/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015395 * Return which encoder is currently attached for connector.
15396 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015397struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015398{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015399 return &intel_attached_encoder(connector)->base;
15400}
Jesse Barnes79e53942008-11-07 14:24:08 -080015401
Chris Wilsondf0e9242010-09-09 16:20:55 +010015402void intel_connector_attach_encoder(struct intel_connector *connector,
15403 struct intel_encoder *encoder)
15404{
15405 connector->encoder = encoder;
15406 drm_mode_connector_attach_encoder(&connector->base,
15407 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015408}
Dave Airlie28d52042009-09-21 14:33:58 +100015409
15410/*
15411 * set vga decode state - true == enable VGA decode
15412 */
15413int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15414{
15415 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015416 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015417 u16 gmch_ctrl;
15418
Chris Wilson75fa0412014-02-07 18:37:02 -020015419 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15420 DRM_ERROR("failed to read control word\n");
15421 return -EIO;
15422 }
15423
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015424 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15425 return 0;
15426
Dave Airlie28d52042009-09-21 14:33:58 +100015427 if (state)
15428 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15429 else
15430 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015431
15432 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15433 DRM_ERROR("failed to write control word\n");
15434 return -EIO;
15435 }
15436
Dave Airlie28d52042009-09-21 14:33:58 +100015437 return 0;
15438}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015439
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015440struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015441
15442 u32 power_well_driver;
15443
Chris Wilson63b66e52013-08-08 15:12:06 +020015444 int num_transcoders;
15445
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015446 struct intel_cursor_error_state {
15447 u32 control;
15448 u32 position;
15449 u32 base;
15450 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015451 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015452
15453 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015454 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015455 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015456 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015457 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015458
15459 struct intel_plane_error_state {
15460 u32 control;
15461 u32 stride;
15462 u32 size;
15463 u32 pos;
15464 u32 addr;
15465 u32 surface;
15466 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015467 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015468
15469 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015470 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015471 enum transcoder cpu_transcoder;
15472
15473 u32 conf;
15474
15475 u32 htotal;
15476 u32 hblank;
15477 u32 hsync;
15478 u32 vtotal;
15479 u32 vblank;
15480 u32 vsync;
15481 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015482};
15483
15484struct intel_display_error_state *
15485intel_display_capture_error_state(struct drm_device *dev)
15486{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015487 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015488 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015489 int transcoders[] = {
15490 TRANSCODER_A,
15491 TRANSCODER_B,
15492 TRANSCODER_C,
15493 TRANSCODER_EDP,
15494 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015495 int i;
15496
Chris Wilson63b66e52013-08-08 15:12:06 +020015497 if (INTEL_INFO(dev)->num_pipes == 0)
15498 return NULL;
15499
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015500 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015501 if (error == NULL)
15502 return NULL;
15503
Imre Deak190be112013-11-25 17:15:31 +020015504 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015505 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15506
Damien Lespiau055e3932014-08-18 13:49:10 +010015507 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015508 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015509 __intel_display_power_is_enabled(dev_priv,
15510 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015511 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015512 continue;
15513
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015514 error->cursor[i].control = I915_READ(CURCNTR(i));
15515 error->cursor[i].position = I915_READ(CURPOS(i));
15516 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015517
15518 error->plane[i].control = I915_READ(DSPCNTR(i));
15519 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015520 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015521 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015522 error->plane[i].pos = I915_READ(DSPPOS(i));
15523 }
Paulo Zanonica291362013-03-06 20:03:14 -030015524 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15525 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015526 if (INTEL_INFO(dev)->gen >= 4) {
15527 error->plane[i].surface = I915_READ(DSPSURF(i));
15528 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15529 }
15530
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015531 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015532
Sonika Jindal3abfce72014-07-21 15:23:43 +053015533 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015534 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015535 }
15536
15537 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15538 if (HAS_DDI(dev_priv->dev))
15539 error->num_transcoders++; /* Account for eDP. */
15540
15541 for (i = 0; i < error->num_transcoders; i++) {
15542 enum transcoder cpu_transcoder = transcoders[i];
15543
Imre Deakddf9c532013-11-27 22:02:02 +020015544 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015545 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015546 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015547 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015548 continue;
15549
Chris Wilson63b66e52013-08-08 15:12:06 +020015550 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15551
15552 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15553 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15554 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15555 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15556 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15557 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15558 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015559 }
15560
15561 return error;
15562}
15563
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015564#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15565
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015566void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015567intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015568 struct drm_device *dev,
15569 struct intel_display_error_state *error)
15570{
Damien Lespiau055e3932014-08-18 13:49:10 +010015571 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015572 int i;
15573
Chris Wilson63b66e52013-08-08 15:12:06 +020015574 if (!error)
15575 return;
15576
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015577 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015578 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015579 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015580 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015581 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015582 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015583 err_printf(m, " Power: %s\n",
15584 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015585 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015586 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015587
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015588 err_printf(m, "Plane [%d]:\n", i);
15589 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15590 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015591 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015592 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15593 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015594 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015595 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015596 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015597 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015598 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15599 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015600 }
15601
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015602 err_printf(m, "Cursor [%d]:\n", i);
15603 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15604 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15605 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015606 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015607
15608 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015609 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015610 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015611 err_printf(m, " Power: %s\n",
15612 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015613 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15614 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15615 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15616 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15617 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15618 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15619 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15620 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015621}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015622
15623void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15624{
15625 struct intel_crtc *crtc;
15626
15627 for_each_intel_crtc(dev, crtc) {
15628 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015629
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015630 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015631
15632 work = crtc->unpin_work;
15633
15634 if (work && work->event &&
15635 work->event->base.file_priv == file) {
15636 kfree(work->event);
15637 work->event = NULL;
15638 }
15639
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015640 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015641 }
15642}