blob: b59717a6e6a6367c0833727b25a345a2e91281f1 [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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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{
3138 struct drm_i915_private *dev_priv = dev->dev_private;
3139 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003140
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003141 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3143
Rob Clark51fd3712013-11-19 12:10:12 -05003144 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003145 /*
3146 * FIXME: Once we have proper support for primary planes (and
3147 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003148 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003149 */
Matt Roperf4510a22014-04-01 15:22:40 -07003150 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003151 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003152 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003153 crtc->x,
3154 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003155 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003156 }
3157}
3158
Ville Syrjälä75147472014-11-24 18:28:11 +02003159void intel_prepare_reset(struct drm_device *dev)
3160{
3161 /* no reset support for gen2 */
3162 if (IS_GEN2(dev))
3163 return;
3164
3165 /* reset doesn't touch the display */
3166 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3167 return;
3168
3169 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003170 /*
3171 * Disabling the crtcs gracefully seems nicer. Also the
3172 * g33 docs say we should at least disable all the planes.
3173 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003174 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003175}
3176
3177void intel_finish_reset(struct drm_device *dev)
3178{
3179 struct drm_i915_private *dev_priv = to_i915(dev);
3180
3181 /*
3182 * Flips in the rings will be nuked by the reset,
3183 * so complete all pending flips so that user space
3184 * will get its events and not get stuck.
3185 */
3186 intel_complete_page_flips(dev);
3187
3188 /* no reset support for gen2 */
3189 if (IS_GEN2(dev))
3190 return;
3191
3192 /* reset doesn't touch the display */
3193 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3194 /*
3195 * Flips in the rings have been nuked by the reset,
3196 * so update the base address of all primary
3197 * planes to the the last fb to make sure we're
3198 * showing the correct fb after a reset.
3199 */
3200 intel_update_primary_planes(dev);
3201 return;
3202 }
3203
3204 /*
3205 * The display has been reset as well,
3206 * so need a full re-initialization.
3207 */
3208 intel_runtime_pm_disable_interrupts(dev_priv);
3209 intel_runtime_pm_enable_interrupts(dev_priv);
3210
3211 intel_modeset_init_hw(dev);
3212
3213 spin_lock_irq(&dev_priv->irq_lock);
3214 if (dev_priv->display.hpd_irq_setup)
3215 dev_priv->display.hpd_irq_setup(dev);
3216 spin_unlock_irq(&dev_priv->irq_lock);
3217
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003218 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003219
3220 intel_hpd_init(dev_priv);
3221
3222 drm_modeset_unlock_all(dev);
3223}
3224
Chris Wilson2e2f3512015-04-27 13:41:14 +01003225static void
Chris Wilson14667a42012-04-03 17:58:35 +01003226intel_finish_fb(struct drm_framebuffer *old_fb)
3227{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003228 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003229 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003230 bool was_interruptible = dev_priv->mm.interruptible;
3231 int ret;
3232
Chris Wilson14667a42012-04-03 17:58:35 +01003233 /* Big Hammer, we also need to ensure that any pending
3234 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3235 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003236 * framebuffer. Note that we rely on userspace rendering
3237 * into the buffer attached to the pipe they are waiting
3238 * on. If not, userspace generates a GPU hang with IPEHR
3239 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003240 *
3241 * This should only fail upon a hung GPU, in which case we
3242 * can safely continue.
3243 */
3244 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003245 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003246 dev_priv->mm.interruptible = was_interruptible;
3247
Chris Wilson2e2f3512015-04-27 13:41:14 +01003248 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003249}
3250
Chris Wilson7d5e3792014-03-04 13:15:08 +00003251static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3252{
3253 struct drm_device *dev = crtc->dev;
3254 struct drm_i915_private *dev_priv = dev->dev_private;
3255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003256 bool pending;
3257
3258 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3259 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3260 return false;
3261
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003262 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003263 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003264 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003265
3266 return pending;
3267}
3268
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003269static void intel_update_pipe_size(struct intel_crtc *crtc)
3270{
3271 struct drm_device *dev = crtc->base.dev;
3272 struct drm_i915_private *dev_priv = dev->dev_private;
3273 const struct drm_display_mode *adjusted_mode;
3274
3275 if (!i915.fastboot)
3276 return;
3277
3278 /*
3279 * Update pipe size and adjust fitter if needed: the reason for this is
3280 * that in compute_mode_changes we check the native mode (not the pfit
3281 * mode) to see if we can flip rather than do a full mode set. In the
3282 * fastboot case, we'll flip, but if we don't update the pipesrc and
3283 * pfit state, we'll end up with a big fb scanned out into the wrong
3284 * sized surface.
3285 *
3286 * To fix this properly, we need to hoist the checks up into
3287 * compute_mode_changes (or above), check the actual pfit state and
3288 * whether the platform allows pfit disable with pipe active, and only
3289 * then update the pipesrc and pfit state, even on the flip path.
3290 */
3291
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003292 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003293
3294 I915_WRITE(PIPESRC(crtc->pipe),
3295 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3296 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003297 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003298 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3299 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003300 I915_WRITE(PF_CTL(crtc->pipe), 0);
3301 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3302 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3303 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003304 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3305 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003306}
3307
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003308static void intel_fdi_normal_train(struct drm_crtc *crtc)
3309{
3310 struct drm_device *dev = crtc->dev;
3311 struct drm_i915_private *dev_priv = dev->dev_private;
3312 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3313 int pipe = intel_crtc->pipe;
3314 u32 reg, temp;
3315
3316 /* enable normal train */
3317 reg = FDI_TX_CTL(pipe);
3318 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003319 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003320 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3321 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003322 } else {
3323 temp &= ~FDI_LINK_TRAIN_NONE;
3324 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003325 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003326 I915_WRITE(reg, temp);
3327
3328 reg = FDI_RX_CTL(pipe);
3329 temp = I915_READ(reg);
3330 if (HAS_PCH_CPT(dev)) {
3331 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3332 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3333 } else {
3334 temp &= ~FDI_LINK_TRAIN_NONE;
3335 temp |= FDI_LINK_TRAIN_NONE;
3336 }
3337 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3338
3339 /* wait one idle pattern time */
3340 POSTING_READ(reg);
3341 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003342
3343 /* IVB wants error correction enabled */
3344 if (IS_IVYBRIDGE(dev))
3345 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3346 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003347}
3348
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003349/* The FDI link training functions for ILK/Ibexpeak. */
3350static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3351{
3352 struct drm_device *dev = crtc->dev;
3353 struct drm_i915_private *dev_priv = dev->dev_private;
3354 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3355 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003356 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003357
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003358 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003359 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003360
Adam Jacksone1a44742010-06-25 15:32:14 -04003361 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3362 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003363 reg = FDI_RX_IMR(pipe);
3364 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003365 temp &= ~FDI_RX_SYMBOL_LOCK;
3366 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003367 I915_WRITE(reg, temp);
3368 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003369 udelay(150);
3370
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003371 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003372 reg = FDI_TX_CTL(pipe);
3373 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003374 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003375 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003376 temp &= ~FDI_LINK_TRAIN_NONE;
3377 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003378 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003379
Chris Wilson5eddb702010-09-11 13:48:45 +01003380 reg = FDI_RX_CTL(pipe);
3381 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382 temp &= ~FDI_LINK_TRAIN_NONE;
3383 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003384 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3385
3386 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003387 udelay(150);
3388
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003389 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003390 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3391 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3392 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003393
Chris Wilson5eddb702010-09-11 13:48:45 +01003394 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003395 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003397 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3398
3399 if ((temp & FDI_RX_BIT_LOCK)) {
3400 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003401 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003402 break;
3403 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003404 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003405 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003406 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407
3408 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 reg = FDI_TX_CTL(pipe);
3410 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003411 temp &= ~FDI_LINK_TRAIN_NONE;
3412 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003414
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 reg = FDI_RX_CTL(pipe);
3416 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417 temp &= ~FDI_LINK_TRAIN_NONE;
3418 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 I915_WRITE(reg, temp);
3420
3421 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422 udelay(150);
3423
Chris Wilson5eddb702010-09-11 13:48:45 +01003424 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003425 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3428
3429 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003431 DRM_DEBUG_KMS("FDI train 2 done.\n");
3432 break;
3433 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003434 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003435 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437
3438 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003439
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440}
3441
Akshay Joshi0206e352011-08-16 15:34:10 -04003442static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3444 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3445 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3446 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3447};
3448
3449/* The FDI link training functions for SNB/Cougarpoint. */
3450static void gen6_fdi_link_train(struct drm_crtc *crtc)
3451{
3452 struct drm_device *dev = crtc->dev;
3453 struct drm_i915_private *dev_priv = dev->dev_private;
3454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3455 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003456 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003457
Adam Jacksone1a44742010-06-25 15:32:14 -04003458 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3459 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 reg = FDI_RX_IMR(pipe);
3461 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003462 temp &= ~FDI_RX_SYMBOL_LOCK;
3463 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003464 I915_WRITE(reg, temp);
3465
3466 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003467 udelay(150);
3468
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 reg = FDI_TX_CTL(pipe);
3471 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003472 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003473 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474 temp &= ~FDI_LINK_TRAIN_NONE;
3475 temp |= FDI_LINK_TRAIN_PATTERN_1;
3476 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3477 /* SNB-B */
3478 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003479 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480
Daniel Vetterd74cf322012-10-26 10:58:13 +02003481 I915_WRITE(FDI_RX_MISC(pipe),
3482 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3483
Chris Wilson5eddb702010-09-11 13:48:45 +01003484 reg = FDI_RX_CTL(pipe);
3485 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486 if (HAS_PCH_CPT(dev)) {
3487 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3488 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3489 } else {
3490 temp &= ~FDI_LINK_TRAIN_NONE;
3491 temp |= FDI_LINK_TRAIN_PATTERN_1;
3492 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3494
3495 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496 udelay(150);
3497
Akshay Joshi0206e352011-08-16 15:34:10 -04003498 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003499 reg = FDI_TX_CTL(pipe);
3500 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003501 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3502 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 I915_WRITE(reg, temp);
3504
3505 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 udelay(500);
3507
Sean Paulfa37d392012-03-02 12:53:39 -05003508 for (retry = 0; retry < 5; retry++) {
3509 reg = FDI_RX_IIR(pipe);
3510 temp = I915_READ(reg);
3511 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3512 if (temp & FDI_RX_BIT_LOCK) {
3513 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3514 DRM_DEBUG_KMS("FDI train 1 done.\n");
3515 break;
3516 }
3517 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003518 }
Sean Paulfa37d392012-03-02 12:53:39 -05003519 if (retry < 5)
3520 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521 }
3522 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003523 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003524
3525 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003526 reg = FDI_TX_CTL(pipe);
3527 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528 temp &= ~FDI_LINK_TRAIN_NONE;
3529 temp |= FDI_LINK_TRAIN_PATTERN_2;
3530 if (IS_GEN6(dev)) {
3531 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3532 /* SNB-B */
3533 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3534 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003536
Chris Wilson5eddb702010-09-11 13:48:45 +01003537 reg = FDI_RX_CTL(pipe);
3538 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003539 if (HAS_PCH_CPT(dev)) {
3540 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3541 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3542 } else {
3543 temp &= ~FDI_LINK_TRAIN_NONE;
3544 temp |= FDI_LINK_TRAIN_PATTERN_2;
3545 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003546 I915_WRITE(reg, temp);
3547
3548 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549 udelay(150);
3550
Akshay Joshi0206e352011-08-16 15:34:10 -04003551 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003552 reg = FDI_TX_CTL(pipe);
3553 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003554 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3555 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003556 I915_WRITE(reg, temp);
3557
3558 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003559 udelay(500);
3560
Sean Paulfa37d392012-03-02 12:53:39 -05003561 for (retry = 0; retry < 5; retry++) {
3562 reg = FDI_RX_IIR(pipe);
3563 temp = I915_READ(reg);
3564 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3565 if (temp & FDI_RX_SYMBOL_LOCK) {
3566 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3567 DRM_DEBUG_KMS("FDI train 2 done.\n");
3568 break;
3569 }
3570 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003571 }
Sean Paulfa37d392012-03-02 12:53:39 -05003572 if (retry < 5)
3573 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 }
3575 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003576 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003577
3578 DRM_DEBUG_KMS("FDI train done.\n");
3579}
3580
Jesse Barnes357555c2011-04-28 15:09:55 -07003581/* Manual link training for Ivy Bridge A0 parts */
3582static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3583{
3584 struct drm_device *dev = crtc->dev;
3585 struct drm_i915_private *dev_priv = dev->dev_private;
3586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3587 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003588 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003589
3590 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3591 for train result */
3592 reg = FDI_RX_IMR(pipe);
3593 temp = I915_READ(reg);
3594 temp &= ~FDI_RX_SYMBOL_LOCK;
3595 temp &= ~FDI_RX_BIT_LOCK;
3596 I915_WRITE(reg, temp);
3597
3598 POSTING_READ(reg);
3599 udelay(150);
3600
Daniel Vetter01a415f2012-10-27 15:58:40 +02003601 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3602 I915_READ(FDI_RX_IIR(pipe)));
3603
Jesse Barnes139ccd32013-08-19 11:04:55 -07003604 /* Try each vswing and preemphasis setting twice before moving on */
3605 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3606 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003607 reg = FDI_TX_CTL(pipe);
3608 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003609 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3610 temp &= ~FDI_TX_ENABLE;
3611 I915_WRITE(reg, temp);
3612
3613 reg = FDI_RX_CTL(pipe);
3614 temp = I915_READ(reg);
3615 temp &= ~FDI_LINK_TRAIN_AUTO;
3616 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3617 temp &= ~FDI_RX_ENABLE;
3618 I915_WRITE(reg, temp);
3619
3620 /* enable CPU FDI TX and PCH FDI RX */
3621 reg = FDI_TX_CTL(pipe);
3622 temp = I915_READ(reg);
3623 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003624 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003625 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003626 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003627 temp |= snb_b_fdi_train_param[j/2];
3628 temp |= FDI_COMPOSITE_SYNC;
3629 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3630
3631 I915_WRITE(FDI_RX_MISC(pipe),
3632 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3633
3634 reg = FDI_RX_CTL(pipe);
3635 temp = I915_READ(reg);
3636 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3637 temp |= FDI_COMPOSITE_SYNC;
3638 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3639
3640 POSTING_READ(reg);
3641 udelay(1); /* should be 0.5us */
3642
3643 for (i = 0; i < 4; i++) {
3644 reg = FDI_RX_IIR(pipe);
3645 temp = I915_READ(reg);
3646 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3647
3648 if (temp & FDI_RX_BIT_LOCK ||
3649 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3650 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3651 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3652 i);
3653 break;
3654 }
3655 udelay(1); /* should be 0.5us */
3656 }
3657 if (i == 4) {
3658 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3659 continue;
3660 }
3661
3662 /* Train 2 */
3663 reg = FDI_TX_CTL(pipe);
3664 temp = I915_READ(reg);
3665 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3666 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3667 I915_WRITE(reg, temp);
3668
3669 reg = FDI_RX_CTL(pipe);
3670 temp = I915_READ(reg);
3671 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3672 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003673 I915_WRITE(reg, temp);
3674
3675 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003676 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003677
Jesse Barnes139ccd32013-08-19 11:04:55 -07003678 for (i = 0; i < 4; i++) {
3679 reg = FDI_RX_IIR(pipe);
3680 temp = I915_READ(reg);
3681 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003682
Jesse Barnes139ccd32013-08-19 11:04:55 -07003683 if (temp & FDI_RX_SYMBOL_LOCK ||
3684 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3685 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3686 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3687 i);
3688 goto train_done;
3689 }
3690 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003691 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003692 if (i == 4)
3693 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003694 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003695
Jesse Barnes139ccd32013-08-19 11:04:55 -07003696train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003697 DRM_DEBUG_KMS("FDI train done.\n");
3698}
3699
Daniel Vetter88cefb62012-08-12 19:27:14 +02003700static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003701{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003702 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003703 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003704 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003705 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003706
Jesse Barnesc64e3112010-09-10 11:27:03 -07003707
Jesse Barnes0e23b992010-09-10 11:10:00 -07003708 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003709 reg = FDI_RX_CTL(pipe);
3710 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003711 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003712 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003713 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003714 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3715
3716 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003717 udelay(200);
3718
3719 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003720 temp = I915_READ(reg);
3721 I915_WRITE(reg, temp | FDI_PCDCLK);
3722
3723 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003724 udelay(200);
3725
Paulo Zanoni20749732012-11-23 15:30:38 -02003726 /* Enable CPU FDI TX PLL, always on for Ironlake */
3727 reg = FDI_TX_CTL(pipe);
3728 temp = I915_READ(reg);
3729 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3730 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003731
Paulo Zanoni20749732012-11-23 15:30:38 -02003732 POSTING_READ(reg);
3733 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003734 }
3735}
3736
Daniel Vetter88cefb62012-08-12 19:27:14 +02003737static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3738{
3739 struct drm_device *dev = intel_crtc->base.dev;
3740 struct drm_i915_private *dev_priv = dev->dev_private;
3741 int pipe = intel_crtc->pipe;
3742 u32 reg, temp;
3743
3744 /* Switch from PCDclk to Rawclk */
3745 reg = FDI_RX_CTL(pipe);
3746 temp = I915_READ(reg);
3747 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3748
3749 /* Disable CPU FDI TX PLL */
3750 reg = FDI_TX_CTL(pipe);
3751 temp = I915_READ(reg);
3752 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3753
3754 POSTING_READ(reg);
3755 udelay(100);
3756
3757 reg = FDI_RX_CTL(pipe);
3758 temp = I915_READ(reg);
3759 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3760
3761 /* Wait for the clocks to turn off. */
3762 POSTING_READ(reg);
3763 udelay(100);
3764}
3765
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003766static void ironlake_fdi_disable(struct drm_crtc *crtc)
3767{
3768 struct drm_device *dev = crtc->dev;
3769 struct drm_i915_private *dev_priv = dev->dev_private;
3770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3771 int pipe = intel_crtc->pipe;
3772 u32 reg, temp;
3773
3774 /* disable CPU FDI tx and PCH FDI rx */
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
3777 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3778 POSTING_READ(reg);
3779
3780 reg = FDI_RX_CTL(pipe);
3781 temp = I915_READ(reg);
3782 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003783 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003784 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3785
3786 POSTING_READ(reg);
3787 udelay(100);
3788
3789 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003790 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003791 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003792
3793 /* still set train pattern 1 */
3794 reg = FDI_TX_CTL(pipe);
3795 temp = I915_READ(reg);
3796 temp &= ~FDI_LINK_TRAIN_NONE;
3797 temp |= FDI_LINK_TRAIN_PATTERN_1;
3798 I915_WRITE(reg, temp);
3799
3800 reg = FDI_RX_CTL(pipe);
3801 temp = I915_READ(reg);
3802 if (HAS_PCH_CPT(dev)) {
3803 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3804 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3805 } else {
3806 temp &= ~FDI_LINK_TRAIN_NONE;
3807 temp |= FDI_LINK_TRAIN_PATTERN_1;
3808 }
3809 /* BPC in FDI rx is consistent with that in PIPECONF */
3810 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003811 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003812 I915_WRITE(reg, temp);
3813
3814 POSTING_READ(reg);
3815 udelay(100);
3816}
3817
Chris Wilson5dce5b932014-01-20 10:17:36 +00003818bool intel_has_pending_fb_unpin(struct drm_device *dev)
3819{
3820 struct intel_crtc *crtc;
3821
3822 /* Note that we don't need to be called with mode_config.lock here
3823 * as our list of CRTC objects is static for the lifetime of the
3824 * device and so cannot disappear as we iterate. Similarly, we can
3825 * happily treat the predicates as racy, atomic checks as userspace
3826 * cannot claim and pin a new fb without at least acquring the
3827 * struct_mutex and so serialising with us.
3828 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003829 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003830 if (atomic_read(&crtc->unpin_work_count) == 0)
3831 continue;
3832
3833 if (crtc->unpin_work)
3834 intel_wait_for_vblank(dev, crtc->pipe);
3835
3836 return true;
3837 }
3838
3839 return false;
3840}
3841
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003842static void page_flip_completed(struct intel_crtc *intel_crtc)
3843{
3844 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3845 struct intel_unpin_work *work = intel_crtc->unpin_work;
3846
3847 /* ensure that the unpin work is consistent wrt ->pending. */
3848 smp_rmb();
3849 intel_crtc->unpin_work = NULL;
3850
3851 if (work->event)
3852 drm_send_vblank_event(intel_crtc->base.dev,
3853 intel_crtc->pipe,
3854 work->event);
3855
3856 drm_crtc_vblank_put(&intel_crtc->base);
3857
3858 wake_up_all(&dev_priv->pending_flip_queue);
3859 queue_work(dev_priv->wq, &work->work);
3860
3861 trace_i915_flip_complete(intel_crtc->plane,
3862 work->pending_flip_obj);
3863}
3864
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003865void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003866{
Chris Wilson0f911282012-04-17 10:05:38 +01003867 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003868 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003869
Daniel Vetter2c10d572012-12-20 21:24:07 +01003870 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003871 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3872 !intel_crtc_has_pending_flip(crtc),
3873 60*HZ) == 0)) {
3874 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003875
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003876 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003877 if (intel_crtc->unpin_work) {
3878 WARN_ONCE(1, "Removing stuck page flip\n");
3879 page_flip_completed(intel_crtc);
3880 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003881 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003882 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003883
Chris Wilson975d5682014-08-20 13:13:34 +01003884 if (crtc->primary->fb) {
3885 mutex_lock(&dev->struct_mutex);
3886 intel_finish_fb(crtc->primary->fb);
3887 mutex_unlock(&dev->struct_mutex);
3888 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003889}
3890
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003891/* Program iCLKIP clock to the desired frequency */
3892static void lpt_program_iclkip(struct drm_crtc *crtc)
3893{
3894 struct drm_device *dev = crtc->dev;
3895 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003896 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003897 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3898 u32 temp;
3899
Ville Syrjäläa5805162015-05-26 20:42:30 +03003900 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003901
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003902 /* It is necessary to ungate the pixclk gate prior to programming
3903 * the divisors, and gate it back when it is done.
3904 */
3905 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3906
3907 /* Disable SSCCTL */
3908 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003909 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3910 SBI_SSCCTL_DISABLE,
3911 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003912
3913 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003914 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003915 auxdiv = 1;
3916 divsel = 0x41;
3917 phaseinc = 0x20;
3918 } else {
3919 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003920 * but the adjusted_mode->crtc_clock in in KHz. To get the
3921 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003922 * convert the virtual clock precision to KHz here for higher
3923 * precision.
3924 */
3925 u32 iclk_virtual_root_freq = 172800 * 1000;
3926 u32 iclk_pi_range = 64;
3927 u32 desired_divisor, msb_divisor_value, pi_value;
3928
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003929 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003930 msb_divisor_value = desired_divisor / iclk_pi_range;
3931 pi_value = desired_divisor % iclk_pi_range;
3932
3933 auxdiv = 0;
3934 divsel = msb_divisor_value - 2;
3935 phaseinc = pi_value;
3936 }
3937
3938 /* This should not happen with any sane values */
3939 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3940 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3941 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3942 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3943
3944 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 +03003945 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003946 auxdiv,
3947 divsel,
3948 phasedir,
3949 phaseinc);
3950
3951 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003952 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003953 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3954 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3955 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3956 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3957 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3958 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003959 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003960
3961 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003962 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003963 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3964 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003965 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003966
3967 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003968 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003969 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003970 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003971
3972 /* Wait for initialization time */
3973 udelay(24);
3974
3975 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003976
Ville Syrjäläa5805162015-05-26 20:42:30 +03003977 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003978}
3979
Daniel Vetter275f01b22013-05-03 11:49:47 +02003980static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3981 enum pipe pch_transcoder)
3982{
3983 struct drm_device *dev = crtc->base.dev;
3984 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003985 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003986
3987 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3988 I915_READ(HTOTAL(cpu_transcoder)));
3989 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3990 I915_READ(HBLANK(cpu_transcoder)));
3991 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3992 I915_READ(HSYNC(cpu_transcoder)));
3993
3994 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3995 I915_READ(VTOTAL(cpu_transcoder)));
3996 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3997 I915_READ(VBLANK(cpu_transcoder)));
3998 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3999 I915_READ(VSYNC(cpu_transcoder)));
4000 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4001 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4002}
4003
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004004static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004005{
4006 struct drm_i915_private *dev_priv = dev->dev_private;
4007 uint32_t temp;
4008
4009 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004010 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004011 return;
4012
4013 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4014 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4015
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004016 temp &= ~FDI_BC_BIFURCATION_SELECT;
4017 if (enable)
4018 temp |= FDI_BC_BIFURCATION_SELECT;
4019
4020 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004021 I915_WRITE(SOUTH_CHICKEN1, temp);
4022 POSTING_READ(SOUTH_CHICKEN1);
4023}
4024
4025static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4026{
4027 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004028
4029 switch (intel_crtc->pipe) {
4030 case PIPE_A:
4031 break;
4032 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004033 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004034 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004035 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004036 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004037
4038 break;
4039 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004040 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004041
4042 break;
4043 default:
4044 BUG();
4045 }
4046}
4047
Jesse Barnesf67a5592011-01-05 10:31:48 -08004048/*
4049 * Enable PCH resources required for PCH ports:
4050 * - PCH PLLs
4051 * - FDI training & RX/TX
4052 * - update transcoder timings
4053 * - DP transcoding bits
4054 * - transcoder
4055 */
4056static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004057{
4058 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004059 struct drm_i915_private *dev_priv = dev->dev_private;
4060 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4061 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004062 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004063
Daniel Vetterab9412b2013-05-03 11:49:46 +02004064 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004065
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004066 if (IS_IVYBRIDGE(dev))
4067 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4068
Daniel Vettercd986ab2012-10-26 10:58:12 +02004069 /* Write the TU size bits before fdi link training, so that error
4070 * detection works. */
4071 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4072 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4073
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004074 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004075 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004076
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004077 /* We need to program the right clock selection before writing the pixel
4078 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004079 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004080 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004081
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004082 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004083 temp |= TRANS_DPLL_ENABLE(pipe);
4084 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004085 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004086 temp |= sel;
4087 else
4088 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004089 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004090 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004091
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004092 /* XXX: pch pll's can be enabled any time before we enable the PCH
4093 * transcoder, and we actually should do this to not upset any PCH
4094 * transcoder that already use the clock when we share it.
4095 *
4096 * Note that enable_shared_dpll tries to do the right thing, but
4097 * get_shared_dpll unconditionally resets the pll - we need that to have
4098 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004099 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004100
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004101 /* set transcoder timing, panel must allow it */
4102 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004103 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004104
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004105 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004106
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004107 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004108 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004109 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004110 reg = TRANS_DP_CTL(pipe);
4111 temp = I915_READ(reg);
4112 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004113 TRANS_DP_SYNC_MASK |
4114 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004115 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004116 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004117
4118 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004119 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004120 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004121 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004122
4123 switch (intel_trans_dp_port_sel(crtc)) {
4124 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126 break;
4127 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004128 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004129 break;
4130 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004131 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 break;
4133 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004134 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135 }
4136
Chris Wilson5eddb702010-09-11 13:48:45 +01004137 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004138 }
4139
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004140 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004141}
4142
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004143static void lpt_pch_enable(struct drm_crtc *crtc)
4144{
4145 struct drm_device *dev = crtc->dev;
4146 struct drm_i915_private *dev_priv = dev->dev_private;
4147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004148 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004149
Daniel Vetterab9412b2013-05-03 11:49:46 +02004150 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004151
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004152 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004153
Paulo Zanoni0540e482012-10-31 18:12:40 -02004154 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004155 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004156
Paulo Zanoni937bb612012-10-31 18:12:47 -02004157 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004158}
4159
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004160struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4161 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004162{
Daniel Vettere2b78262013-06-07 23:10:03 +02004163 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004164 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004165 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004166 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004167
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004168 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4169
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004170 if (HAS_PCH_IBX(dev_priv->dev)) {
4171 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004172 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004173 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004174
Daniel Vetter46edb022013-06-05 13:34:12 +02004175 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4176 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004177
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004178 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004179
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004180 goto found;
4181 }
4182
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304183 if (IS_BROXTON(dev_priv->dev)) {
4184 /* PLL is attached to port in bxt */
4185 struct intel_encoder *encoder;
4186 struct intel_digital_port *intel_dig_port;
4187
4188 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4189 if (WARN_ON(!encoder))
4190 return NULL;
4191
4192 intel_dig_port = enc_to_dig_port(&encoder->base);
4193 /* 1:1 mapping between ports and PLLs */
4194 i = (enum intel_dpll_id)intel_dig_port->port;
4195 pll = &dev_priv->shared_dplls[i];
4196 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4197 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004198 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304199
4200 goto found;
4201 }
4202
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004203 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4204 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004205
4206 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004207 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004208 continue;
4209
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004210 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004211 &shared_dpll[i].hw_state,
4212 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004213 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004214 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004215 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004216 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004217 goto found;
4218 }
4219 }
4220
4221 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004222 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4223 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004224 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004225 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4226 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004227 goto found;
4228 }
4229 }
4230
4231 return NULL;
4232
4233found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004234 if (shared_dpll[i].crtc_mask == 0)
4235 shared_dpll[i].hw_state =
4236 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004237
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004238 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004239 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4240 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004241
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004242 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004243
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004244 return pll;
4245}
4246
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004247static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004248{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004249 struct drm_i915_private *dev_priv = to_i915(state->dev);
4250 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004251 struct intel_shared_dpll *pll;
4252 enum intel_dpll_id i;
4253
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004254 if (!to_intel_atomic_state(state)->dpll_set)
4255 return;
4256
4257 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004258 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4259 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004260 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004261 }
4262}
4263
Daniel Vettera1520312013-05-03 11:49:50 +02004264static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004265{
4266 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004267 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004268 u32 temp;
4269
4270 temp = I915_READ(dslreg);
4271 udelay(500);
4272 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004273 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004274 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004275 }
4276}
4277
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004278static int
4279skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4280 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4281 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004282{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004283 struct intel_crtc_scaler_state *scaler_state =
4284 &crtc_state->scaler_state;
4285 struct intel_crtc *intel_crtc =
4286 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004287 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004288
4289 need_scaling = intel_rotation_90_or_270(rotation) ?
4290 (src_h != dst_w || src_w != dst_h):
4291 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004292
4293 /*
4294 * if plane is being disabled or scaler is no more required or force detach
4295 * - free scaler binded to this plane/crtc
4296 * - in order to do this, update crtc->scaler_usage
4297 *
4298 * Here scaler state in crtc_state is set free so that
4299 * scaler can be assigned to other user. Actual register
4300 * update to free the scaler is done in plane/panel-fit programming.
4301 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4302 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004303 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004304 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004305 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004306 scaler_state->scalers[*scaler_id].in_use = 0;
4307
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004308 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4309 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4310 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004311 scaler_state->scaler_users);
4312 *scaler_id = -1;
4313 }
4314 return 0;
4315 }
4316
4317 /* range checks */
4318 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4319 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4320
4321 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4322 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004323 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004324 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004325 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004326 return -EINVAL;
4327 }
4328
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004329 /* mark this plane as a scaler user in crtc_state */
4330 scaler_state->scaler_users |= (1 << scaler_user);
4331 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4332 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4333 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4334 scaler_state->scaler_users);
4335
4336 return 0;
4337}
4338
4339/**
4340 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4341 *
4342 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004343 *
4344 * Return
4345 * 0 - scaler_usage updated successfully
4346 * error - requested scaling cannot be supported or other error condition
4347 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004348int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004349{
4350 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4351 struct drm_display_mode *adjusted_mode =
4352 &state->base.adjusted_mode;
4353
4354 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4355 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4356
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004357 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004358 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4359 state->pipe_src_w, state->pipe_src_h,
Imre Deak8c6cda22015-06-23 20:40:27 +03004360 adjusted_mode->hdisplay, adjusted_mode->vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004361}
4362
4363/**
4364 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4365 *
4366 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004367 * @plane_state: atomic plane state to update
4368 *
4369 * Return
4370 * 0 - scaler_usage updated successfully
4371 * error - requested scaling cannot be supported or other error condition
4372 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004373static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4374 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004375{
4376
4377 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004378 struct intel_plane *intel_plane =
4379 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004380 struct drm_framebuffer *fb = plane_state->base.fb;
4381 int ret;
4382
4383 bool force_detach = !fb || !plane_state->visible;
4384
4385 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4386 intel_plane->base.base.id, intel_crtc->pipe,
4387 drm_plane_index(&intel_plane->base));
4388
4389 ret = skl_update_scaler(crtc_state, force_detach,
4390 drm_plane_index(&intel_plane->base),
4391 &plane_state->scaler_id,
4392 plane_state->base.rotation,
4393 drm_rect_width(&plane_state->src) >> 16,
4394 drm_rect_height(&plane_state->src) >> 16,
4395 drm_rect_width(&plane_state->dst),
4396 drm_rect_height(&plane_state->dst));
4397
4398 if (ret || plane_state->scaler_id < 0)
4399 return ret;
4400
Chandra Kondurua1b22782015-04-07 15:28:45 -07004401 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004402 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004403 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004404 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004405 return -EINVAL;
4406 }
4407
4408 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004409 switch (fb->pixel_format) {
4410 case DRM_FORMAT_RGB565:
4411 case DRM_FORMAT_XBGR8888:
4412 case DRM_FORMAT_XRGB8888:
4413 case DRM_FORMAT_ABGR8888:
4414 case DRM_FORMAT_ARGB8888:
4415 case DRM_FORMAT_XRGB2101010:
4416 case DRM_FORMAT_XBGR2101010:
4417 case DRM_FORMAT_YUYV:
4418 case DRM_FORMAT_YVYU:
4419 case DRM_FORMAT_UYVY:
4420 case DRM_FORMAT_VYUY:
4421 break;
4422 default:
4423 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4424 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4425 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004426 }
4427
Chandra Kondurua1b22782015-04-07 15:28:45 -07004428 return 0;
4429}
4430
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004431static void skylake_scaler_disable(struct intel_crtc *crtc)
4432{
4433 int i;
4434
4435 for (i = 0; i < crtc->num_scalers; i++)
4436 skl_detach_scaler(crtc, i);
4437}
4438
4439static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004440{
4441 struct drm_device *dev = crtc->base.dev;
4442 struct drm_i915_private *dev_priv = dev->dev_private;
4443 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004444 struct intel_crtc_scaler_state *scaler_state =
4445 &crtc->config->scaler_state;
4446
4447 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4448
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004449 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004450 int id;
4451
4452 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4453 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4454 return;
4455 }
4456
4457 id = scaler_state->scaler_id;
4458 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4459 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4460 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4461 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4462
4463 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004464 }
4465}
4466
Jesse Barnesb074cec2013-04-25 12:55:02 -07004467static void ironlake_pfit_enable(struct intel_crtc *crtc)
4468{
4469 struct drm_device *dev = crtc->base.dev;
4470 struct drm_i915_private *dev_priv = dev->dev_private;
4471 int pipe = crtc->pipe;
4472
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004473 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004474 /* Force use of hard-coded filter coefficients
4475 * as some pre-programmed values are broken,
4476 * e.g. x201.
4477 */
4478 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4479 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4480 PF_PIPE_SEL_IVB(pipe));
4481 else
4482 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004483 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4484 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004485 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004486}
4487
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004488void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004489{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004490 struct drm_device *dev = crtc->base.dev;
4491 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004492
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004493 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004494 return;
4495
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004496 /* We can only enable IPS after we enable a plane and wait for a vblank */
4497 intel_wait_for_vblank(dev, crtc->pipe);
4498
Paulo Zanonid77e4532013-09-24 13:52:55 -03004499 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004500 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004501 mutex_lock(&dev_priv->rps.hw_lock);
4502 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4503 mutex_unlock(&dev_priv->rps.hw_lock);
4504 /* Quoting Art Runyan: "its not safe to expect any particular
4505 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004506 * mailbox." Moreover, the mailbox may return a bogus state,
4507 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004508 */
4509 } else {
4510 I915_WRITE(IPS_CTL, IPS_ENABLE);
4511 /* The bit only becomes 1 in the next vblank, so this wait here
4512 * is essentially intel_wait_for_vblank. If we don't have this
4513 * and don't wait for vblanks until the end of crtc_enable, then
4514 * the HW state readout code will complain that the expected
4515 * IPS_CTL value is not the one we read. */
4516 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4517 DRM_ERROR("Timed out waiting for IPS enable\n");
4518 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004519}
4520
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004521void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004522{
4523 struct drm_device *dev = crtc->base.dev;
4524 struct drm_i915_private *dev_priv = dev->dev_private;
4525
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004526 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004527 return;
4528
4529 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004530 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004531 mutex_lock(&dev_priv->rps.hw_lock);
4532 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4533 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004534 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4535 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4536 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004537 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004538 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004539 POSTING_READ(IPS_CTL);
4540 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004541
4542 /* We need to wait for a vblank before we can disable the plane. */
4543 intel_wait_for_vblank(dev, crtc->pipe);
4544}
4545
4546/** Loads the palette/gamma unit for the CRTC with the prepared values */
4547static void intel_crtc_load_lut(struct drm_crtc *crtc)
4548{
4549 struct drm_device *dev = crtc->dev;
4550 struct drm_i915_private *dev_priv = dev->dev_private;
4551 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4552 enum pipe pipe = intel_crtc->pipe;
4553 int palreg = PALETTE(pipe);
4554 int i;
4555 bool reenable_ips = false;
4556
4557 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004558 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004559 return;
4560
Imre Deak50360402015-01-16 00:55:16 -08004561 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004562 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004563 assert_dsi_pll_enabled(dev_priv);
4564 else
4565 assert_pll_enabled(dev_priv, pipe);
4566 }
4567
4568 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304569 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004570 palreg = LGC_PALETTE(pipe);
4571
4572 /* Workaround : Do not read or write the pipe palette/gamma data while
4573 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4574 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004575 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004576 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4577 GAMMA_MODE_MODE_SPLIT)) {
4578 hsw_disable_ips(intel_crtc);
4579 reenable_ips = true;
4580 }
4581
4582 for (i = 0; i < 256; i++) {
4583 I915_WRITE(palreg + 4 * i,
4584 (intel_crtc->lut_r[i] << 16) |
4585 (intel_crtc->lut_g[i] << 8) |
4586 intel_crtc->lut_b[i]);
4587 }
4588
4589 if (reenable_ips)
4590 hsw_enable_ips(intel_crtc);
4591}
4592
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004593static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004594{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004595 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004596 struct drm_device *dev = intel_crtc->base.dev;
4597 struct drm_i915_private *dev_priv = dev->dev_private;
4598
4599 mutex_lock(&dev->struct_mutex);
4600 dev_priv->mm.interruptible = false;
4601 (void) intel_overlay_switch_off(intel_crtc->overlay);
4602 dev_priv->mm.interruptible = true;
4603 mutex_unlock(&dev->struct_mutex);
4604 }
4605
4606 /* Let userspace switch the overlay on again. In most cases userspace
4607 * has to recompute where to put it anyway.
4608 */
4609}
4610
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004611/**
4612 * intel_post_enable_primary - Perform operations after enabling primary plane
4613 * @crtc: the CRTC whose primary plane was just enabled
4614 *
4615 * Performs potentially sleeping operations that must be done after the primary
4616 * plane is enabled, such as updating FBC and IPS. Note that this may be
4617 * called due to an explicit primary plane update, or due to an implicit
4618 * re-enable that is caused when a sprite plane is updated to no longer
4619 * completely hide the primary plane.
4620 */
4621static void
4622intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004623{
4624 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004625 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004626 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4627 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004628
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004629 /*
4630 * BDW signals flip done immediately if the plane
4631 * is disabled, even if the plane enable is already
4632 * armed to occur at the next vblank :(
4633 */
4634 if (IS_BROADWELL(dev))
4635 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004636
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004637 /*
4638 * FIXME IPS should be fine as long as one plane is
4639 * enabled, but in practice it seems to have problems
4640 * when going from primary only to sprite only and vice
4641 * versa.
4642 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004643 hsw_enable_ips(intel_crtc);
4644
Daniel Vetterf99d7062014-06-19 16:01:59 +02004645 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004646 * Gen2 reports pipe underruns whenever all planes are disabled.
4647 * So don't enable underrun reporting before at least some planes
4648 * are enabled.
4649 * FIXME: Need to fix the logic to work when we turn off all planes
4650 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004651 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004652 if (IS_GEN2(dev))
4653 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4654
4655 /* Underruns don't raise interrupts, so check manually. */
4656 if (HAS_GMCH_DISPLAY(dev))
4657 i9xx_check_fifo_underruns(dev_priv);
4658}
4659
4660/**
4661 * intel_pre_disable_primary - Perform operations before disabling primary plane
4662 * @crtc: the CRTC whose primary plane is to be disabled
4663 *
4664 * Performs potentially sleeping operations that must be done before the
4665 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4666 * be called due to an explicit primary plane update, or due to an implicit
4667 * disable that is caused when a sprite plane completely hides the primary
4668 * plane.
4669 */
4670static void
4671intel_pre_disable_primary(struct drm_crtc *crtc)
4672{
4673 struct drm_device *dev = crtc->dev;
4674 struct drm_i915_private *dev_priv = dev->dev_private;
4675 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4676 int pipe = intel_crtc->pipe;
4677
4678 /*
4679 * Gen2 reports pipe underruns whenever all planes are disabled.
4680 * So diasble underrun reporting before all the planes get disabled.
4681 * FIXME: Need to fix the logic to work when we turn off all planes
4682 * but leave the pipe running.
4683 */
4684 if (IS_GEN2(dev))
4685 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4686
4687 /*
4688 * Vblank time updates from the shadow to live plane control register
4689 * are blocked if the memory self-refresh mode is active at that
4690 * moment. So to make sure the plane gets truly disabled, disable
4691 * first the self-refresh mode. The self-refresh enable bit in turn
4692 * will be checked/applied by the HW only at the next frame start
4693 * event which is after the vblank start event, so we need to have a
4694 * wait-for-vblank between disabling the plane and the pipe.
4695 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004696 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004697 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004698 dev_priv->wm.vlv.cxsr = false;
4699 intel_wait_for_vblank(dev, pipe);
4700 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004701
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004702 /*
4703 * FIXME IPS should be fine as long as one plane is
4704 * enabled, but in practice it seems to have problems
4705 * when going from primary only to sprite only and vice
4706 * versa.
4707 */
4708 hsw_disable_ips(intel_crtc);
4709}
4710
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004711static void intel_post_plane_update(struct intel_crtc *crtc)
4712{
4713 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4714 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -03004715 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004716 struct drm_plane *plane;
4717
4718 if (atomic->wait_vblank)
4719 intel_wait_for_vblank(dev, crtc->pipe);
4720
4721 intel_frontbuffer_flip(dev, atomic->fb_bits);
4722
Ville Syrjälä852eb002015-06-24 22:00:07 +03004723 if (atomic->disable_cxsr)
4724 crtc->wm.cxsr_allowed = true;
4725
Ville Syrjäläf015c552015-06-24 22:00:02 +03004726 if (crtc->atomic.update_wm_post)
4727 intel_update_watermarks(&crtc->base);
4728
Paulo Zanonic80ac852015-07-02 19:25:13 -03004729 if (atomic->update_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03004730 intel_fbc_update(dev_priv);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004731
4732 if (atomic->post_enable_primary)
4733 intel_post_enable_primary(&crtc->base);
4734
4735 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4736 intel_update_sprite_watermarks(plane, &crtc->base,
4737 0, 0, 0, false, false);
4738
4739 memset(atomic, 0, sizeof(*atomic));
4740}
4741
4742static void intel_pre_plane_update(struct intel_crtc *crtc)
4743{
4744 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004745 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004746 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4747 struct drm_plane *p;
4748
4749 /* Track fb's for any planes being disabled */
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004750 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4751 struct intel_plane *plane = to_intel_plane(p);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004752
4753 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03004754 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4755 plane->frontbuffer_bit);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004756 mutex_unlock(&dev->struct_mutex);
4757 }
4758
4759 if (atomic->wait_for_flips)
4760 intel_crtc_wait_for_pending_flips(&crtc->base);
4761
Paulo Zanonic80ac852015-07-02 19:25:13 -03004762 if (atomic->disable_fbc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03004763 intel_fbc_disable_crtc(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004764
Rodrigo Vivi066cf552015-06-26 13:55:54 -07004765 if (crtc->atomic.disable_ips)
4766 hsw_disable_ips(crtc);
4767
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004768 if (atomic->pre_disable_primary)
4769 intel_pre_disable_primary(&crtc->base);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004770
4771 if (atomic->disable_cxsr) {
4772 crtc->wm.cxsr_allowed = false;
4773 intel_set_memory_cxsr(dev_priv, false);
4774 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004775}
4776
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004777static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004778{
4779 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004781 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004782 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004783
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004784 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004785
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004786 drm_for_each_plane_mask(p, dev, plane_mask)
4787 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004788
Daniel Vetterf99d7062014-06-19 16:01:59 +02004789 /*
4790 * FIXME: Once we grow proper nuclear flip support out of this we need
4791 * to compute the mask of flip planes precisely. For the time being
4792 * consider this a flip to a NULL plane.
4793 */
4794 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004795}
4796
Jesse Barnesf67a5592011-01-05 10:31:48 -08004797static void ironlake_crtc_enable(struct drm_crtc *crtc)
4798{
4799 struct drm_device *dev = crtc->dev;
4800 struct drm_i915_private *dev_priv = dev->dev_private;
4801 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004802 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004803 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004804
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004805 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004806 return;
4807
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004808 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004809 intel_prepare_shared_dpll(intel_crtc);
4810
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004811 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304812 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004813
4814 intel_set_pipe_timings(intel_crtc);
4815
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004816 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004817 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004818 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004819 }
4820
4821 ironlake_set_pipeconf(crtc);
4822
Jesse Barnesf67a5592011-01-05 10:31:48 -08004823 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004824
Daniel Vettera72e4c92014-09-30 10:56:47 +02004825 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4826 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004827
Daniel Vetterf6736a12013-06-05 13:34:30 +02004828 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004829 if (encoder->pre_enable)
4830 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004831
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004832 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004833 /* Note: FDI PLL enabling _must_ be done before we enable the
4834 * cpu pipes, hence this is separate from all the other fdi/pch
4835 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004836 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004837 } else {
4838 assert_fdi_tx_disabled(dev_priv, pipe);
4839 assert_fdi_rx_disabled(dev_priv, pipe);
4840 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004841
Jesse Barnesb074cec2013-04-25 12:55:02 -07004842 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004843
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004844 /*
4845 * On ILK+ LUT must be loaded before the pipe is running but with
4846 * clocks enabled
4847 */
4848 intel_crtc_load_lut(crtc);
4849
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004850 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004851 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004852
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004853 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004854 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004855
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004856 assert_vblank_disabled(crtc);
4857 drm_crtc_vblank_on(crtc);
4858
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004859 for_each_encoder_on_crtc(dev, crtc, encoder)
4860 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004861
4862 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004863 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004864}
4865
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004866/* IPS only exists on ULT machines and is tied to pipe A. */
4867static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4868{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004869 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004870}
4871
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004872static void haswell_crtc_enable(struct drm_crtc *crtc)
4873{
4874 struct drm_device *dev = crtc->dev;
4875 struct drm_i915_private *dev_priv = dev->dev_private;
4876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4877 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004878 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4879 struct intel_crtc_state *pipe_config =
4880 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004881
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004882 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004883 return;
4884
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004885 if (intel_crtc_to_shared_dpll(intel_crtc))
4886 intel_enable_shared_dpll(intel_crtc);
4887
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004888 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304889 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004890
4891 intel_set_pipe_timings(intel_crtc);
4892
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004893 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4894 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4895 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004896 }
4897
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004898 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004899 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004900 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004901 }
4902
4903 haswell_set_pipeconf(crtc);
4904
4905 intel_set_pipe_csc(crtc);
4906
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004907 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004908
Daniel Vettera72e4c92014-09-30 10:56:47 +02004909 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004910 for_each_encoder_on_crtc(dev, crtc, encoder)
4911 if (encoder->pre_enable)
4912 encoder->pre_enable(encoder);
4913
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004914 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004915 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4916 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004917 dev_priv->display.fdi_link_train(crtc);
4918 }
4919
Paulo Zanoni1f544382012-10-24 11:32:00 -02004920 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004921
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004922 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004923 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004924 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07004925 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004926
4927 /*
4928 * On ILK+ LUT must be loaded before the pipe is running but with
4929 * clocks enabled
4930 */
4931 intel_crtc_load_lut(crtc);
4932
Paulo Zanoni1f544382012-10-24 11:32:00 -02004933 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004934 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004935
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004936 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004937 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004938
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004939 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004940 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004941
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004942 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004943 intel_ddi_set_vc_payload_alloc(crtc, true);
4944
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004945 assert_vblank_disabled(crtc);
4946 drm_crtc_vblank_on(crtc);
4947
Jani Nikula8807e552013-08-30 19:40:32 +03004948 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004949 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004950 intel_opregion_notify_encoder(encoder, true);
4951 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004952
Paulo Zanonie4916942013-09-20 16:21:19 -03004953 /* If we change the relative order between pipe/planes enabling, we need
4954 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004955 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4956 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4957 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4958 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4959 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004960}
4961
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004962static void ironlake_pfit_disable(struct intel_crtc *crtc)
4963{
4964 struct drm_device *dev = crtc->base.dev;
4965 struct drm_i915_private *dev_priv = dev->dev_private;
4966 int pipe = crtc->pipe;
4967
4968 /* To avoid upsetting the power well on haswell only disable the pfit if
4969 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004970 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004971 I915_WRITE(PF_CTL(pipe), 0);
4972 I915_WRITE(PF_WIN_POS(pipe), 0);
4973 I915_WRITE(PF_WIN_SZ(pipe), 0);
4974 }
4975}
4976
Jesse Barnes6be4a602010-09-10 10:26:01 -07004977static void ironlake_crtc_disable(struct drm_crtc *crtc)
4978{
4979 struct drm_device *dev = crtc->dev;
4980 struct drm_i915_private *dev_priv = dev->dev_private;
4981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004982 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004983 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004984 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004985
Daniel Vetterea9d7582012-07-10 10:42:52 +02004986 for_each_encoder_on_crtc(dev, crtc, encoder)
4987 encoder->disable(encoder);
4988
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004989 drm_crtc_vblank_off(crtc);
4990 assert_vblank_disabled(crtc);
4991
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004992 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004993 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004994
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004995 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004996
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004997 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004998
Ville Syrjälä5a74f702015-05-05 17:17:38 +03004999 if (intel_crtc->config->has_pch_encoder)
5000 ironlake_fdi_disable(crtc);
5001
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005002 for_each_encoder_on_crtc(dev, crtc, encoder)
5003 if (encoder->post_disable)
5004 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005005
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005006 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005007 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005008
Daniel Vetterd925c592013-06-05 13:34:04 +02005009 if (HAS_PCH_CPT(dev)) {
5010 /* disable TRANS_DP_CTL */
5011 reg = TRANS_DP_CTL(pipe);
5012 temp = I915_READ(reg);
5013 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5014 TRANS_DP_PORT_SEL_MASK);
5015 temp |= TRANS_DP_PORT_SEL_NONE;
5016 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005017
Daniel Vetterd925c592013-06-05 13:34:04 +02005018 /* disable DPLL_SEL */
5019 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005020 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005021 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005022 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005023
Daniel Vetterd925c592013-06-05 13:34:04 +02005024 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005025 }
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02005026
5027 intel_crtc->active = false;
5028 intel_update_watermarks(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005029}
5030
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005031static void haswell_crtc_disable(struct drm_crtc *crtc)
5032{
5033 struct drm_device *dev = crtc->dev;
5034 struct drm_i915_private *dev_priv = dev->dev_private;
5035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5036 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005037 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005038
Jani Nikula8807e552013-08-30 19:40:32 +03005039 for_each_encoder_on_crtc(dev, crtc, encoder) {
5040 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005041 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005042 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005043
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005044 drm_crtc_vblank_off(crtc);
5045 assert_vblank_disabled(crtc);
5046
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005047 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005048 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5049 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005050 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005051
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005052 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005053 intel_ddi_set_vc_payload_alloc(crtc, false);
5054
Paulo Zanoniad80a812012-10-24 16:06:19 -02005055 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005056
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005057 if (INTEL_INFO(dev)->gen >= 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005058 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005059 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07005060 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005061
Paulo Zanoni1f544382012-10-24 11:32:00 -02005062 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005063
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005064 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005065 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005066 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005067 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005068
Imre Deak97b040a2014-06-25 22:01:50 +03005069 for_each_encoder_on_crtc(dev, crtc, encoder)
5070 if (encoder->post_disable)
5071 encoder->post_disable(encoder);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02005072
5073 intel_crtc->active = false;
5074 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005075}
5076
Jesse Barnes2dd24552013-04-25 12:55:01 -07005077static void i9xx_pfit_enable(struct intel_crtc *crtc)
5078{
5079 struct drm_device *dev = crtc->base.dev;
5080 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005081 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005082
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005083 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005084 return;
5085
Daniel Vetterc0b03412013-05-28 12:05:54 +02005086 /*
5087 * The panel fitter should only be adjusted whilst the pipe is disabled,
5088 * according to register description and PRM.
5089 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005090 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5091 assert_pipe_disabled(dev_priv, crtc->pipe);
5092
Jesse Barnesb074cec2013-04-25 12:55:02 -07005093 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5094 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005095
5096 /* Border color in case we don't scale up to the full screen. Black by
5097 * default, change to something else for debugging. */
5098 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005099}
5100
Dave Airlied05410f2014-06-05 13:22:59 +10005101static enum intel_display_power_domain port_to_power_domain(enum port port)
5102{
5103 switch (port) {
5104 case PORT_A:
5105 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5106 case PORT_B:
5107 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5108 case PORT_C:
5109 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5110 case PORT_D:
5111 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
Xiong Zhangd8e19f92015-08-13 18:00:12 +08005112 case PORT_E:
5113 return POWER_DOMAIN_PORT_DDI_E_2_LANES;
Dave Airlied05410f2014-06-05 13:22:59 +10005114 default:
5115 WARN_ON_ONCE(1);
5116 return POWER_DOMAIN_PORT_OTHER;
5117 }
5118}
5119
Imre Deak77d22dc2014-03-05 16:20:52 +02005120#define for_each_power_domain(domain, mask) \
5121 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5122 if ((1 << (domain)) & (mask))
5123
Imre Deak319be8a2014-03-04 19:22:57 +02005124enum intel_display_power_domain
5125intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005126{
Imre Deak319be8a2014-03-04 19:22:57 +02005127 struct drm_device *dev = intel_encoder->base.dev;
5128 struct intel_digital_port *intel_dig_port;
5129
5130 switch (intel_encoder->type) {
5131 case INTEL_OUTPUT_UNKNOWN:
5132 /* Only DDI platforms should ever use this output type */
5133 WARN_ON_ONCE(!HAS_DDI(dev));
5134 case INTEL_OUTPUT_DISPLAYPORT:
5135 case INTEL_OUTPUT_HDMI:
5136 case INTEL_OUTPUT_EDP:
5137 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005138 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005139 case INTEL_OUTPUT_DP_MST:
5140 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5141 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005142 case INTEL_OUTPUT_ANALOG:
5143 return POWER_DOMAIN_PORT_CRT;
5144 case INTEL_OUTPUT_DSI:
5145 return POWER_DOMAIN_PORT_DSI;
5146 default:
5147 return POWER_DOMAIN_PORT_OTHER;
5148 }
5149}
5150
5151static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5152{
5153 struct drm_device *dev = crtc->dev;
5154 struct intel_encoder *intel_encoder;
5155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5156 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005157 unsigned long mask;
5158 enum transcoder transcoder;
5159
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005160 if (!crtc->state->active)
5161 return 0;
5162
Imre Deak77d22dc2014-03-05 16:20:52 +02005163 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5164
5165 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5166 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005167 if (intel_crtc->config->pch_pfit.enabled ||
5168 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005169 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5170
Imre Deak319be8a2014-03-04 19:22:57 +02005171 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5172 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5173
Imre Deak77d22dc2014-03-05 16:20:52 +02005174 return mask;
5175}
5176
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005177static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
5178{
5179 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5181 enum intel_display_power_domain domain;
5182 unsigned long domains, new_domains, old_domains;
5183
5184 old_domains = intel_crtc->enabled_power_domains;
5185 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
5186
5187 domains = new_domains & ~old_domains;
5188
5189 for_each_power_domain(domain, domains)
5190 intel_display_power_get(dev_priv, domain);
5191
5192 return old_domains & ~new_domains;
5193}
5194
5195static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5196 unsigned long domains)
5197{
5198 enum intel_display_power_domain domain;
5199
5200 for_each_power_domain(domain, domains)
5201 intel_display_power_put(dev_priv, domain);
5202}
5203
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005204static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005205{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005206 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005207 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005208 unsigned long put_domains[I915_MAX_PIPES] = {};
5209 struct drm_crtc_state *crtc_state;
5210 struct drm_crtc *crtc;
5211 int i;
Imre Deak77d22dc2014-03-05 16:20:52 +02005212
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005213 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5214 if (needs_modeset(crtc->state))
5215 put_domains[to_intel_crtc(crtc)->pipe] =
5216 modeset_get_crtc_power_domains(crtc);
Imre Deak77d22dc2014-03-05 16:20:52 +02005217 }
5218
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005219 if (dev_priv->display.modeset_commit_cdclk) {
5220 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5221
5222 if (cdclk != dev_priv->cdclk_freq &&
5223 !WARN_ON(!state->allow_modeset))
5224 dev_priv->display.modeset_commit_cdclk(state);
5225 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005226
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005227 for (i = 0; i < I915_MAX_PIPES; i++)
5228 if (put_domains[i])
5229 modeset_put_power_domains(dev_priv, put_domains[i]);
Imre Deak77d22dc2014-03-05 16:20:52 +02005230}
5231
Mika Kaholaadafdc62015-08-18 14:36:59 +03005232static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
5233{
5234 int max_cdclk_freq = dev_priv->max_cdclk_freq;
5235
5236 if (INTEL_INFO(dev_priv)->gen >= 9 ||
5237 IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
5238 return max_cdclk_freq;
5239 else if (IS_CHERRYVIEW(dev_priv))
5240 return max_cdclk_freq*95/100;
5241 else if (INTEL_INFO(dev_priv)->gen < 4)
5242 return 2*max_cdclk_freq*90/100;
5243 else
5244 return max_cdclk_freq*90/100;
5245}
5246
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005247static void intel_update_max_cdclk(struct drm_device *dev)
5248{
5249 struct drm_i915_private *dev_priv = dev->dev_private;
5250
5251 if (IS_SKYLAKE(dev)) {
5252 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5253
5254 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5255 dev_priv->max_cdclk_freq = 675000;
5256 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5257 dev_priv->max_cdclk_freq = 540000;
5258 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5259 dev_priv->max_cdclk_freq = 450000;
5260 else
5261 dev_priv->max_cdclk_freq = 337500;
5262 } else if (IS_BROADWELL(dev)) {
5263 /*
5264 * FIXME with extra cooling we can allow
5265 * 540 MHz for ULX and 675 Mhz for ULT.
5266 * How can we know if extra cooling is
5267 * available? PCI ID, VTB, something else?
5268 */
5269 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5270 dev_priv->max_cdclk_freq = 450000;
5271 else if (IS_BDW_ULX(dev))
5272 dev_priv->max_cdclk_freq = 450000;
5273 else if (IS_BDW_ULT(dev))
5274 dev_priv->max_cdclk_freq = 540000;
5275 else
5276 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005277 } else if (IS_CHERRYVIEW(dev)) {
5278 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005279 } else if (IS_VALLEYVIEW(dev)) {
5280 dev_priv->max_cdclk_freq = 400000;
5281 } else {
5282 /* otherwise assume cdclk is fixed */
5283 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5284 }
5285
Mika Kaholaadafdc62015-08-18 14:36:59 +03005286 dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
5287
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005288 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5289 dev_priv->max_cdclk_freq);
Mika Kaholaadafdc62015-08-18 14:36:59 +03005290
5291 DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
5292 dev_priv->max_dotclk_freq);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005293}
5294
5295static void intel_update_cdclk(struct drm_device *dev)
5296{
5297 struct drm_i915_private *dev_priv = dev->dev_private;
5298
5299 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5300 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5301 dev_priv->cdclk_freq);
5302
5303 /*
5304 * Program the gmbus_freq based on the cdclk frequency.
5305 * BSpec erroneously claims we should aim for 4MHz, but
5306 * in fact 1MHz is the correct frequency.
5307 */
5308 if (IS_VALLEYVIEW(dev)) {
5309 /*
5310 * Program the gmbus_freq based on the cdclk frequency.
5311 * BSpec erroneously claims we should aim for 4MHz, but
5312 * in fact 1MHz is the correct frequency.
5313 */
5314 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5315 }
5316
5317 if (dev_priv->max_cdclk_freq == 0)
5318 intel_update_max_cdclk(dev);
5319}
5320
Damien Lespiau70d0c572015-06-04 18:21:29 +01005321static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305322{
5323 struct drm_i915_private *dev_priv = dev->dev_private;
5324 uint32_t divider;
5325 uint32_t ratio;
5326 uint32_t current_freq;
5327 int ret;
5328
5329 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5330 switch (frequency) {
5331 case 144000:
5332 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5333 ratio = BXT_DE_PLL_RATIO(60);
5334 break;
5335 case 288000:
5336 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5337 ratio = BXT_DE_PLL_RATIO(60);
5338 break;
5339 case 384000:
5340 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5341 ratio = BXT_DE_PLL_RATIO(60);
5342 break;
5343 case 576000:
5344 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5345 ratio = BXT_DE_PLL_RATIO(60);
5346 break;
5347 case 624000:
5348 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5349 ratio = BXT_DE_PLL_RATIO(65);
5350 break;
5351 case 19200:
5352 /*
5353 * Bypass frequency with DE PLL disabled. Init ratio, divider
5354 * to suppress GCC warning.
5355 */
5356 ratio = 0;
5357 divider = 0;
5358 break;
5359 default:
5360 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5361
5362 return;
5363 }
5364
5365 mutex_lock(&dev_priv->rps.hw_lock);
5366 /* Inform power controller of upcoming frequency change */
5367 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5368 0x80000000);
5369 mutex_unlock(&dev_priv->rps.hw_lock);
5370
5371 if (ret) {
5372 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5373 ret, frequency);
5374 return;
5375 }
5376
5377 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5378 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5379 current_freq = current_freq * 500 + 1000;
5380
5381 /*
5382 * DE PLL has to be disabled when
5383 * - setting to 19.2MHz (bypass, PLL isn't used)
5384 * - before setting to 624MHz (PLL needs toggling)
5385 * - before setting to any frequency from 624MHz (PLL needs toggling)
5386 */
5387 if (frequency == 19200 || frequency == 624000 ||
5388 current_freq == 624000) {
5389 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5390 /* Timeout 200us */
5391 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5392 1))
5393 DRM_ERROR("timout waiting for DE PLL unlock\n");
5394 }
5395
5396 if (frequency != 19200) {
5397 uint32_t val;
5398
5399 val = I915_READ(BXT_DE_PLL_CTL);
5400 val &= ~BXT_DE_PLL_RATIO_MASK;
5401 val |= ratio;
5402 I915_WRITE(BXT_DE_PLL_CTL, val);
5403
5404 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5405 /* Timeout 200us */
5406 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5407 DRM_ERROR("timeout waiting for DE PLL lock\n");
5408
5409 val = I915_READ(CDCLK_CTL);
5410 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5411 val |= divider;
5412 /*
5413 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5414 * enable otherwise.
5415 */
5416 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5417 if (frequency >= 500000)
5418 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5419
5420 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5421 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5422 val |= (frequency - 1000) / 500;
5423 I915_WRITE(CDCLK_CTL, val);
5424 }
5425
5426 mutex_lock(&dev_priv->rps.hw_lock);
5427 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5428 DIV_ROUND_UP(frequency, 25000));
5429 mutex_unlock(&dev_priv->rps.hw_lock);
5430
5431 if (ret) {
5432 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5433 ret, frequency);
5434 return;
5435 }
5436
Damien Lespiaua47871b2015-06-04 18:21:34 +01005437 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305438}
5439
5440void broxton_init_cdclk(struct drm_device *dev)
5441{
5442 struct drm_i915_private *dev_priv = dev->dev_private;
5443 uint32_t val;
5444
5445 /*
5446 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5447 * or else the reset will hang because there is no PCH to respond.
5448 * Move the handshake programming to initialization sequence.
5449 * Previously was left up to BIOS.
5450 */
5451 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5452 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5453 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5454
5455 /* Enable PG1 for cdclk */
5456 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5457
5458 /* check if cd clock is enabled */
5459 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5460 DRM_DEBUG_KMS("Display already initialized\n");
5461 return;
5462 }
5463
5464 /*
5465 * FIXME:
5466 * - The initial CDCLK needs to be read from VBT.
5467 * Need to make this change after VBT has changes for BXT.
5468 * - check if setting the max (or any) cdclk freq is really necessary
5469 * here, it belongs to modeset time
5470 */
5471 broxton_set_cdclk(dev, 624000);
5472
5473 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005474 POSTING_READ(DBUF_CTL);
5475
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305476 udelay(10);
5477
5478 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5479 DRM_ERROR("DBuf power enable timeout!\n");
5480}
5481
5482void broxton_uninit_cdclk(struct drm_device *dev)
5483{
5484 struct drm_i915_private *dev_priv = dev->dev_private;
5485
5486 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005487 POSTING_READ(DBUF_CTL);
5488
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305489 udelay(10);
5490
5491 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5492 DRM_ERROR("DBuf power disable timeout!\n");
5493
5494 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5495 broxton_set_cdclk(dev, 19200);
5496
5497 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5498}
5499
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005500static const struct skl_cdclk_entry {
5501 unsigned int freq;
5502 unsigned int vco;
5503} skl_cdclk_frequencies[] = {
5504 { .freq = 308570, .vco = 8640 },
5505 { .freq = 337500, .vco = 8100 },
5506 { .freq = 432000, .vco = 8640 },
5507 { .freq = 450000, .vco = 8100 },
5508 { .freq = 540000, .vco = 8100 },
5509 { .freq = 617140, .vco = 8640 },
5510 { .freq = 675000, .vco = 8100 },
5511};
5512
5513static unsigned int skl_cdclk_decimal(unsigned int freq)
5514{
5515 return (freq - 1000) / 500;
5516}
5517
5518static unsigned int skl_cdclk_get_vco(unsigned int freq)
5519{
5520 unsigned int i;
5521
5522 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5523 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5524
5525 if (e->freq == freq)
5526 return e->vco;
5527 }
5528
5529 return 8100;
5530}
5531
5532static void
5533skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5534{
5535 unsigned int min_freq;
5536 u32 val;
5537
5538 /* select the minimum CDCLK before enabling DPLL 0 */
5539 val = I915_READ(CDCLK_CTL);
5540 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5541 val |= CDCLK_FREQ_337_308;
5542
5543 if (required_vco == 8640)
5544 min_freq = 308570;
5545 else
5546 min_freq = 337500;
5547
5548 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5549
5550 I915_WRITE(CDCLK_CTL, val);
5551 POSTING_READ(CDCLK_CTL);
5552
5553 /*
5554 * We always enable DPLL0 with the lowest link rate possible, but still
5555 * taking into account the VCO required to operate the eDP panel at the
5556 * desired frequency. The usual DP link rates operate with a VCO of
5557 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5558 * The modeset code is responsible for the selection of the exact link
5559 * rate later on, with the constraint of choosing a frequency that
5560 * works with required_vco.
5561 */
5562 val = I915_READ(DPLL_CTRL1);
5563
5564 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5565 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5566 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5567 if (required_vco == 8640)
5568 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5569 SKL_DPLL0);
5570 else
5571 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5572 SKL_DPLL0);
5573
5574 I915_WRITE(DPLL_CTRL1, val);
5575 POSTING_READ(DPLL_CTRL1);
5576
5577 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5578
5579 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5580 DRM_ERROR("DPLL0 not locked\n");
5581}
5582
5583static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5584{
5585 int ret;
5586 u32 val;
5587
5588 /* inform PCU we want to change CDCLK */
5589 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5590 mutex_lock(&dev_priv->rps.hw_lock);
5591 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5592 mutex_unlock(&dev_priv->rps.hw_lock);
5593
5594 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5595}
5596
5597static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5598{
5599 unsigned int i;
5600
5601 for (i = 0; i < 15; i++) {
5602 if (skl_cdclk_pcu_ready(dev_priv))
5603 return true;
5604 udelay(10);
5605 }
5606
5607 return false;
5608}
5609
5610static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5611{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005612 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005613 u32 freq_select, pcu_ack;
5614
5615 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5616
5617 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5618 DRM_ERROR("failed to inform PCU about cdclk change\n");
5619 return;
5620 }
5621
5622 /* set CDCLK_CTL */
5623 switch(freq) {
5624 case 450000:
5625 case 432000:
5626 freq_select = CDCLK_FREQ_450_432;
5627 pcu_ack = 1;
5628 break;
5629 case 540000:
5630 freq_select = CDCLK_FREQ_540;
5631 pcu_ack = 2;
5632 break;
5633 case 308570:
5634 case 337500:
5635 default:
5636 freq_select = CDCLK_FREQ_337_308;
5637 pcu_ack = 0;
5638 break;
5639 case 617140:
5640 case 675000:
5641 freq_select = CDCLK_FREQ_675_617;
5642 pcu_ack = 3;
5643 break;
5644 }
5645
5646 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5647 POSTING_READ(CDCLK_CTL);
5648
5649 /* inform PCU of the change */
5650 mutex_lock(&dev_priv->rps.hw_lock);
5651 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5652 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005653
5654 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005655}
5656
5657void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5658{
5659 /* disable DBUF power */
5660 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5661 POSTING_READ(DBUF_CTL);
5662
5663 udelay(10);
5664
5665 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5666 DRM_ERROR("DBuf power disable timeout\n");
5667
5668 /* disable DPLL0 */
5669 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5670 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5671 DRM_ERROR("Couldn't disable DPLL0\n");
5672
5673 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5674}
5675
5676void skl_init_cdclk(struct drm_i915_private *dev_priv)
5677{
5678 u32 val;
5679 unsigned int required_vco;
5680
5681 /* enable PCH reset handshake */
5682 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5683 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5684
5685 /* enable PG1 and Misc I/O */
5686 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5687
Gary Wang39d9b852015-08-28 16:40:34 +08005688 /* DPLL0 not enabled (happens on early BIOS versions) */
5689 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
5690 /* enable DPLL0 */
5691 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5692 skl_dpll0_enable(dev_priv, required_vco);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005693 }
5694
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005695 /* set CDCLK to the frequency the BIOS chose */
5696 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5697
5698 /* enable DBUF power */
5699 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5700 POSTING_READ(DBUF_CTL);
5701
5702 udelay(10);
5703
5704 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5705 DRM_ERROR("DBuf power enable timeout\n");
5706}
5707
Ville Syrjälädfcab172014-06-13 13:37:47 +03005708/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005709static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005710{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005711 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005712
Jesse Barnes586f49d2013-11-04 16:06:59 -08005713 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005714 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005715 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5716 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005717 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005718
Ville Syrjälädfcab172014-06-13 13:37:47 +03005719 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005720}
5721
5722/* Adjust CDclk dividers to allow high res or save power if possible */
5723static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5724{
5725 struct drm_i915_private *dev_priv = dev->dev_private;
5726 u32 val, cmd;
5727
Vandana Kannan164dfd22014-11-24 13:37:41 +05305728 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5729 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005730
Ville Syrjälädfcab172014-06-13 13:37:47 +03005731 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005732 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005733 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005734 cmd = 1;
5735 else
5736 cmd = 0;
5737
5738 mutex_lock(&dev_priv->rps.hw_lock);
5739 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5740 val &= ~DSPFREQGUAR_MASK;
5741 val |= (cmd << DSPFREQGUAR_SHIFT);
5742 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5743 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5744 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5745 50)) {
5746 DRM_ERROR("timed out waiting for CDclk change\n");
5747 }
5748 mutex_unlock(&dev_priv->rps.hw_lock);
5749
Ville Syrjälä54433e92015-05-26 20:42:31 +03005750 mutex_lock(&dev_priv->sb_lock);
5751
Ville Syrjälädfcab172014-06-13 13:37:47 +03005752 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005753 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005754
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005755 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005756
Jesse Barnes30a970c2013-11-04 13:48:12 -08005757 /* adjust cdclk divider */
5758 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005759 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005760 val |= divider;
5761 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005762
5763 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5764 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5765 50))
5766 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005767 }
5768
Jesse Barnes30a970c2013-11-04 13:48:12 -08005769 /* adjust self-refresh exit latency value */
5770 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5771 val &= ~0x7f;
5772
5773 /*
5774 * For high bandwidth configs, we set a higher latency in the bunit
5775 * so that the core display fetch happens in time to avoid underruns.
5776 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005777 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005778 val |= 4500 / 250; /* 4.5 usec */
5779 else
5780 val |= 3000 / 250; /* 3.0 usec */
5781 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005782
Ville Syrjäläa5805162015-05-26 20:42:30 +03005783 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005784
Ville Syrjäläb6283052015-06-03 15:45:07 +03005785 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005786}
5787
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005788static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5789{
5790 struct drm_i915_private *dev_priv = dev->dev_private;
5791 u32 val, cmd;
5792
Vandana Kannan164dfd22014-11-24 13:37:41 +05305793 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5794 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005795
5796 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005797 case 333333:
5798 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005799 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005800 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005801 break;
5802 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005803 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005804 return;
5805 }
5806
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005807 /*
5808 * Specs are full of misinformation, but testing on actual
5809 * hardware has shown that we just need to write the desired
5810 * CCK divider into the Punit register.
5811 */
5812 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5813
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005814 mutex_lock(&dev_priv->rps.hw_lock);
5815 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5816 val &= ~DSPFREQGUAR_MASK_CHV;
5817 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5818 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5819 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5820 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5821 50)) {
5822 DRM_ERROR("timed out waiting for CDclk change\n");
5823 }
5824 mutex_unlock(&dev_priv->rps.hw_lock);
5825
Ville Syrjäläb6283052015-06-03 15:45:07 +03005826 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005827}
5828
Jesse Barnes30a970c2013-11-04 13:48:12 -08005829static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5830 int max_pixclk)
5831{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005832 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005833 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005834
Jesse Barnes30a970c2013-11-04 13:48:12 -08005835 /*
5836 * Really only a few cases to deal with, as only 4 CDclks are supported:
5837 * 200MHz
5838 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005839 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005840 * 400MHz (VLV only)
5841 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5842 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005843 *
5844 * We seem to get an unstable or solid color picture at 200MHz.
5845 * Not sure what's wrong. For now use 200MHz only when all pipes
5846 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005847 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005848 if (!IS_CHERRYVIEW(dev_priv) &&
5849 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005850 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005851 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005852 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005853 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005854 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005855 else
5856 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005857}
5858
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305859static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5860 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005861{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305862 /*
5863 * FIXME:
5864 * - remove the guardband, it's not needed on BXT
5865 * - set 19.2MHz bypass frequency if there are no active pipes
5866 */
5867 if (max_pixclk > 576000*9/10)
5868 return 624000;
5869 else if (max_pixclk > 384000*9/10)
5870 return 576000;
5871 else if (max_pixclk > 288000*9/10)
5872 return 384000;
5873 else if (max_pixclk > 144000*9/10)
5874 return 288000;
5875 else
5876 return 144000;
5877}
5878
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005879/* Compute the max pixel clock for new configuration. Uses atomic state if
5880 * that's non-NULL, look at current state otherwise. */
5881static int intel_mode_max_pixclk(struct drm_device *dev,
5882 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005883{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005884 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005885 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005886 int max_pixclk = 0;
5887
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005888 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005889 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005890 if (IS_ERR(crtc_state))
5891 return PTR_ERR(crtc_state);
5892
5893 if (!crtc_state->base.enable)
5894 continue;
5895
5896 max_pixclk = max(max_pixclk,
5897 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005898 }
5899
5900 return max_pixclk;
5901}
5902
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005903static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005904{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005905 struct drm_device *dev = state->dev;
5906 struct drm_i915_private *dev_priv = dev->dev_private;
5907 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005908
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005909 if (max_pixclk < 0)
5910 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005911
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005912 to_intel_atomic_state(state)->cdclk =
5913 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305914
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005915 return 0;
5916}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005917
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005918static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5919{
5920 struct drm_device *dev = state->dev;
5921 struct drm_i915_private *dev_priv = dev->dev_private;
5922 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005923
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005924 if (max_pixclk < 0)
5925 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005926
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005927 to_intel_atomic_state(state)->cdclk =
5928 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005929
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005930 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005931}
5932
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005933static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5934{
5935 unsigned int credits, default_credits;
5936
5937 if (IS_CHERRYVIEW(dev_priv))
5938 default_credits = PFI_CREDIT(12);
5939 else
5940 default_credits = PFI_CREDIT(8);
5941
Vandana Kannan164dfd22014-11-24 13:37:41 +05305942 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005943 /* CHV suggested value is 31 or 63 */
5944 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005945 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005946 else
5947 credits = PFI_CREDIT(15);
5948 } else {
5949 credits = default_credits;
5950 }
5951
5952 /*
5953 * WA - write default credits before re-programming
5954 * FIXME: should we also set the resend bit here?
5955 */
5956 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5957 default_credits);
5958
5959 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5960 credits | PFI_CREDIT_RESEND);
5961
5962 /*
5963 * FIXME is this guaranteed to clear
5964 * immediately or should we poll for it?
5965 */
5966 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5967}
5968
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005969static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005970{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005971 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005972 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005973 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005974
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005975 /*
5976 * FIXME: We can end up here with all power domains off, yet
5977 * with a CDCLK frequency other than the minimum. To account
5978 * for this take the PIPE-A power domain, which covers the HW
5979 * blocks needed for the following programming. This can be
5980 * removed once it's guaranteed that we get here either with
5981 * the minimum CDCLK set, or the required power domains
5982 * enabled.
5983 */
5984 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005985
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005986 if (IS_CHERRYVIEW(dev))
5987 cherryview_set_cdclk(dev, req_cdclk);
5988 else
5989 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005990
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005991 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02005992
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005993 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005994}
5995
Jesse Barnes89b667f2013-04-18 14:51:36 -07005996static void valleyview_crtc_enable(struct drm_crtc *crtc)
5997{
5998 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005999 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006000 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6001 struct intel_encoder *encoder;
6002 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006003 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006004
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006005 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006006 return;
6007
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006008 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306009
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006010 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306011 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006012
6013 intel_set_pipe_timings(intel_crtc);
6014
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006015 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6016 struct drm_i915_private *dev_priv = dev->dev_private;
6017
6018 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6019 I915_WRITE(CHV_CANVAS(pipe), 0);
6020 }
6021
Daniel Vetter5b18e572014-04-24 23:55:06 +02006022 i9xx_set_pipeconf(intel_crtc);
6023
Jesse Barnes89b667f2013-04-18 14:51:36 -07006024 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006025
Daniel Vettera72e4c92014-09-30 10:56:47 +02006026 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006027
Jesse Barnes89b667f2013-04-18 14:51:36 -07006028 for_each_encoder_on_crtc(dev, crtc, encoder)
6029 if (encoder->pre_pll_enable)
6030 encoder->pre_pll_enable(encoder);
6031
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006032 if (!is_dsi) {
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006033 if (IS_CHERRYVIEW(dev)) {
6034 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006035 chv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006036 } else {
6037 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006038 vlv_enable_pll(intel_crtc, intel_crtc->config);
Ville Syrjäläc0b4c662015-07-08 23:45:52 +03006039 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006040 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006041
6042 for_each_encoder_on_crtc(dev, crtc, encoder)
6043 if (encoder->pre_enable)
6044 encoder->pre_enable(encoder);
6045
Jesse Barnes2dd24552013-04-25 12:55:01 -07006046 i9xx_pfit_enable(intel_crtc);
6047
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006048 intel_crtc_load_lut(crtc);
6049
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006050 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006051
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006052 assert_vblank_disabled(crtc);
6053 drm_crtc_vblank_on(crtc);
6054
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006055 for_each_encoder_on_crtc(dev, crtc, encoder)
6056 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006057}
6058
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006059static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6060{
6061 struct drm_device *dev = crtc->base.dev;
6062 struct drm_i915_private *dev_priv = dev->dev_private;
6063
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006064 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6065 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006066}
6067
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006068static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006069{
6070 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006071 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006073 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006074 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006075
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006076 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006077 return;
6078
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006079 i9xx_set_pll_dividers(intel_crtc);
6080
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006081 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306082 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006083
6084 intel_set_pipe_timings(intel_crtc);
6085
Daniel Vetter5b18e572014-04-24 23:55:06 +02006086 i9xx_set_pipeconf(intel_crtc);
6087
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006088 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006089
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006090 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006091 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006092
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006093 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006094 if (encoder->pre_enable)
6095 encoder->pre_enable(encoder);
6096
Daniel Vetterf6736a12013-06-05 13:34:30 +02006097 i9xx_enable_pll(intel_crtc);
6098
Jesse Barnes2dd24552013-04-25 12:55:01 -07006099 i9xx_pfit_enable(intel_crtc);
6100
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006101 intel_crtc_load_lut(crtc);
6102
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006103 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006104 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006105
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006106 assert_vblank_disabled(crtc);
6107 drm_crtc_vblank_on(crtc);
6108
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006109 for_each_encoder_on_crtc(dev, crtc, encoder)
6110 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006111}
6112
Daniel Vetter87476d62013-04-11 16:29:06 +02006113static void i9xx_pfit_disable(struct intel_crtc *crtc)
6114{
6115 struct drm_device *dev = crtc->base.dev;
6116 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006117
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006118 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006119 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006120
6121 assert_pipe_disabled(dev_priv, crtc->pipe);
6122
Daniel Vetter328d8e82013-05-08 10:36:31 +02006123 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6124 I915_READ(PFIT_CONTROL));
6125 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006126}
6127
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006128static void i9xx_crtc_disable(struct drm_crtc *crtc)
6129{
6130 struct drm_device *dev = crtc->dev;
6131 struct drm_i915_private *dev_priv = dev->dev_private;
6132 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006133 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006134 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006135
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006136 /*
6137 * On gen2 planes are double buffered but the pipe isn't, so we must
6138 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006139 * We also need to wait on all gmch platforms because of the
6140 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006141 */
Imre Deak564ed192014-06-13 14:54:21 +03006142 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006143
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006144 for_each_encoder_on_crtc(dev, crtc, encoder)
6145 encoder->disable(encoder);
6146
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006147 drm_crtc_vblank_off(crtc);
6148 assert_vblank_disabled(crtc);
6149
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006150 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006151
Daniel Vetter87476d62013-04-11 16:29:06 +02006152 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006153
Jesse Barnes89b667f2013-04-18 14:51:36 -07006154 for_each_encoder_on_crtc(dev, crtc, encoder)
6155 if (encoder->post_disable)
6156 encoder->post_disable(encoder);
6157
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006158 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006159 if (IS_CHERRYVIEW(dev))
6160 chv_disable_pll(dev_priv, pipe);
6161 else if (IS_VALLEYVIEW(dev))
6162 vlv_disable_pll(dev_priv, pipe);
6163 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006164 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006165 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006166
Ville Syrjäläd6db9952015-07-08 23:45:49 +03006167 for_each_encoder_on_crtc(dev, crtc, encoder)
6168 if (encoder->post_pll_disable)
6169 encoder->post_pll_disable(encoder);
6170
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006171 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006172 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02006173
6174 intel_crtc->active = false;
6175 intel_update_watermarks(crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006176}
6177
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006178static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006179{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006181 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006182 enum intel_display_power_domain domain;
6183 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006184
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006185 if (!intel_crtc->active)
6186 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006187
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006188 if (to_intel_plane_state(crtc->primary->state)->visible) {
6189 intel_crtc_wait_for_pending_flips(crtc);
6190 intel_pre_disable_primary(crtc);
6191 }
6192
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006193 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006194 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006195 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006196
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006197 domains = intel_crtc->enabled_power_domains;
6198 for_each_power_domain(domain, domains)
6199 intel_display_power_put(dev_priv, domain);
6200 intel_crtc->enabled_power_domains = 0;
6201}
6202
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006203/*
6204 * turn all crtc's off, but do not adjust state
6205 * This has to be paired with a call to intel_modeset_setup_hw_state.
6206 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006207int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006208{
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006209 struct drm_mode_config *config = &dev->mode_config;
6210 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6211 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006212 struct drm_crtc *crtc;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006213 unsigned crtc_mask = 0;
6214 int ret = 0;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006215
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006216 if (WARN_ON(!ctx))
6217 return 0;
6218
6219 lockdep_assert_held(&ctx->ww_ctx);
6220 state = drm_atomic_state_alloc(dev);
6221 if (WARN_ON(!state))
6222 return -ENOMEM;
6223
6224 state->acquire_ctx = ctx;
6225 state->allow_modeset = true;
6226
6227 for_each_crtc(dev, crtc) {
6228 struct drm_crtc_state *crtc_state =
6229 drm_atomic_get_crtc_state(state, crtc);
6230
6231 ret = PTR_ERR_OR_ZERO(crtc_state);
6232 if (ret)
6233 goto free;
6234
6235 if (!crtc_state->active)
6236 continue;
6237
6238 crtc_state->active = false;
6239 crtc_mask |= 1 << drm_crtc_index(crtc);
6240 }
6241
6242 if (crtc_mask) {
Maarten Lankhorst74c090b2015-07-13 16:30:30 +02006243 ret = drm_atomic_commit(state);
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006244
6245 if (!ret) {
6246 for_each_crtc(dev, crtc)
6247 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6248 crtc->state->active = true;
6249
6250 return ret;
6251 }
6252 }
6253
6254free:
6255 if (ret)
6256 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6257 drm_atomic_state_free(state);
6258 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006259}
6260
Chris Wilsonea5b2132010-08-04 13:50:23 +01006261void intel_encoder_destroy(struct drm_encoder *encoder)
6262{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006263 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006264
Chris Wilsonea5b2132010-08-04 13:50:23 +01006265 drm_encoder_cleanup(encoder);
6266 kfree(intel_encoder);
6267}
6268
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006269/* Cross check the actual hw state with our own modeset state tracking (and it's
6270 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006271static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006272{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006273 struct drm_crtc *crtc = connector->base.state->crtc;
6274
6275 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6276 connector->base.base.id,
6277 connector->base.name);
6278
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006279 if (connector->get_hw_state(connector)) {
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006280 struct drm_encoder *encoder = &connector->encoder->base;
6281 struct drm_connector_state *conn_state = connector->base.state;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006282
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006283 I915_STATE_WARN(!crtc,
6284 "connector enabled without attached crtc\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006285
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006286 if (!crtc)
6287 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006288
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006289 I915_STATE_WARN(!crtc->state->active,
6290 "connector is active, but attached crtc isn't\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006291
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006292 if (!encoder)
6293 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006294
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006295 I915_STATE_WARN(conn_state->best_encoder != encoder,
6296 "atomic encoder doesn't match attached encoder\n");
Dave Airlie36cd7442014-05-02 13:44:18 +10006297
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006298 I915_STATE_WARN(conn_state->crtc != encoder->crtc,
6299 "attached encoder crtc differs from connector crtc\n");
6300 } else {
Maarten Lankhorst4d688a22015-08-05 12:37:06 +02006301 I915_STATE_WARN(crtc && crtc->state->active,
6302 "attached crtc is active, but connector isn't\n");
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +02006303 I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
6304 "best encoder set without crtc!\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006305 }
6306}
6307
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006308int intel_connector_init(struct intel_connector *connector)
6309{
6310 struct drm_connector_state *connector_state;
6311
6312 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6313 if (!connector_state)
6314 return -ENOMEM;
6315
6316 connector->base.state = connector_state;
6317 return 0;
6318}
6319
6320struct intel_connector *intel_connector_alloc(void)
6321{
6322 struct intel_connector *connector;
6323
6324 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6325 if (!connector)
6326 return NULL;
6327
6328 if (intel_connector_init(connector) < 0) {
6329 kfree(connector);
6330 return NULL;
6331 }
6332
6333 return connector;
6334}
6335
Daniel Vetterf0947c32012-07-02 13:10:34 +02006336/* Simple connector->get_hw_state implementation for encoders that support only
6337 * one connector and no cloning and hence the encoder state determines the state
6338 * of the connector. */
6339bool intel_connector_get_hw_state(struct intel_connector *connector)
6340{
Daniel Vetter24929352012-07-02 20:28:59 +02006341 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006342 struct intel_encoder *encoder = connector->encoder;
6343
6344 return encoder->get_hw_state(encoder, &pipe);
6345}
6346
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006347static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006348{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006349 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6350 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006351
6352 return 0;
6353}
6354
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006355static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006356 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006357{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006358 struct drm_atomic_state *state = pipe_config->base.state;
6359 struct intel_crtc *other_crtc;
6360 struct intel_crtc_state *other_crtc_state;
6361
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006362 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6363 pipe_name(pipe), pipe_config->fdi_lanes);
6364 if (pipe_config->fdi_lanes > 4) {
6365 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6366 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006367 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006368 }
6369
Paulo Zanonibafb6552013-11-02 21:07:44 -07006370 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006371 if (pipe_config->fdi_lanes > 2) {
6372 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6373 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006374 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006375 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006376 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006377 }
6378 }
6379
6380 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006381 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006382
6383 /* Ivybridge 3 pipe is really complicated */
6384 switch (pipe) {
6385 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006386 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006387 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006388 if (pipe_config->fdi_lanes <= 2)
6389 return 0;
6390
6391 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6392 other_crtc_state =
6393 intel_atomic_get_crtc_state(state, other_crtc);
6394 if (IS_ERR(other_crtc_state))
6395 return PTR_ERR(other_crtc_state);
6396
6397 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006398 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6399 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006400 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006401 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006402 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006403 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006404 if (pipe_config->fdi_lanes > 2) {
6405 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6406 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006407 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006408 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006409
6410 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6411 other_crtc_state =
6412 intel_atomic_get_crtc_state(state, other_crtc);
6413 if (IS_ERR(other_crtc_state))
6414 return PTR_ERR(other_crtc_state);
6415
6416 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006417 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006418 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006419 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006420 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006421 default:
6422 BUG();
6423 }
6424}
6425
Daniel Vettere29c22c2013-02-21 00:00:16 +01006426#define RETRY 1
6427static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006428 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006429{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006430 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006431 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006432 int lane, link_bw, fdi_dotclock, ret;
6433 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006434
Daniel Vettere29c22c2013-02-21 00:00:16 +01006435retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006436 /* FDI is a binary signal running at ~2.7GHz, encoding
6437 * each output octet as 10 bits. The actual frequency
6438 * is stored as a divider into a 100MHz clock, and the
6439 * mode pixel clock is stored in units of 1KHz.
6440 * Hence the bw of each lane in terms of the mode signal
6441 * is:
6442 */
6443 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6444
Damien Lespiau241bfc32013-09-25 16:45:37 +01006445 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006446
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006447 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006448 pipe_config->pipe_bpp);
6449
6450 pipe_config->fdi_lanes = lane;
6451
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006452 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006453 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006454
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006455 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6456 intel_crtc->pipe, pipe_config);
6457 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006458 pipe_config->pipe_bpp -= 2*3;
6459 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6460 pipe_config->pipe_bpp);
6461 needs_recompute = true;
6462 pipe_config->bw_constrained = true;
6463
6464 goto retry;
6465 }
6466
6467 if (needs_recompute)
6468 return RETRY;
6469
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006471}
6472
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006473static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6474 struct intel_crtc_state *pipe_config)
6475{
6476 if (pipe_config->pipe_bpp > 24)
6477 return false;
6478
6479 /* HSW can handle pixel rate up to cdclk? */
6480 if (IS_HASWELL(dev_priv->dev))
6481 return true;
6482
6483 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006484 * We compare against max which means we must take
6485 * the increased cdclk requirement into account when
6486 * calculating the new cdclk.
6487 *
6488 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006489 */
6490 return ilk_pipe_pixel_rate(pipe_config) <=
6491 dev_priv->max_cdclk_freq * 95 / 100;
6492}
6493
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006494static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006495 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006496{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006497 struct drm_device *dev = crtc->base.dev;
6498 struct drm_i915_private *dev_priv = dev->dev_private;
6499
Jani Nikulad330a952014-01-21 11:24:25 +02006500 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006501 hsw_crtc_supports_ips(crtc) &&
6502 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006503}
6504
Daniel Vettera43f6e02013-06-07 23:10:32 +02006505static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006506 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006507{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006508 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006509 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006510 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006511
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006512 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006513 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006514 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006515
6516 /*
6517 * Enable pixel doubling when the dot clock
6518 * is > 90% of the (display) core speed.
6519 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006520 * GDG double wide on either pipe,
6521 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006522 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006523 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006524 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006525 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006526 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006527 }
6528
Damien Lespiau241bfc32013-09-25 16:45:37 +01006529 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006530 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006531 }
Chris Wilson89749352010-09-12 18:25:19 +01006532
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006533 /*
6534 * Pipe horizontal size must be even in:
6535 * - DVO ganged mode
6536 * - LVDS dual channel mode
6537 * - Double wide pipe
6538 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006539 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006540 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6541 pipe_config->pipe_src_w &= ~1;
6542
Damien Lespiau8693a822013-05-03 18:48:11 +01006543 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6544 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006545 */
6546 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6547 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006548 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006549
Damien Lespiauf5adf942013-06-24 18:29:34 +01006550 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006551 hsw_compute_ips_config(crtc, pipe_config);
6552
Daniel Vetter877d48d2013-04-19 11:24:43 +02006553 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006554 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006555
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006556 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006557}
6558
Ville Syrjälä1652d192015-03-31 14:12:01 +03006559static int skylake_get_display_clock_speed(struct drm_device *dev)
6560{
6561 struct drm_i915_private *dev_priv = to_i915(dev);
6562 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6563 uint32_t cdctl = I915_READ(CDCLK_CTL);
6564 uint32_t linkrate;
6565
Damien Lespiau414355a2015-06-04 18:21:31 +01006566 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006567 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006568
6569 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6570 return 540000;
6571
6572 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006573 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006574
Damien Lespiau71cd8422015-04-30 16:39:17 +01006575 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6576 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006577 /* vco 8640 */
6578 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6579 case CDCLK_FREQ_450_432:
6580 return 432000;
6581 case CDCLK_FREQ_337_308:
6582 return 308570;
6583 case CDCLK_FREQ_675_617:
6584 return 617140;
6585 default:
6586 WARN(1, "Unknown cd freq selection\n");
6587 }
6588 } else {
6589 /* vco 8100 */
6590 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6591 case CDCLK_FREQ_450_432:
6592 return 450000;
6593 case CDCLK_FREQ_337_308:
6594 return 337500;
6595 case CDCLK_FREQ_675_617:
6596 return 675000;
6597 default:
6598 WARN(1, "Unknown cd freq selection\n");
6599 }
6600 }
6601
6602 /* error case, do as if DPLL0 isn't enabled */
6603 return 24000;
6604}
6605
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006606static int broxton_get_display_clock_speed(struct drm_device *dev)
6607{
6608 struct drm_i915_private *dev_priv = to_i915(dev);
6609 uint32_t cdctl = I915_READ(CDCLK_CTL);
6610 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6611 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6612 int cdclk;
6613
6614 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6615 return 19200;
6616
6617 cdclk = 19200 * pll_ratio / 2;
6618
6619 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6620 case BXT_CDCLK_CD2X_DIV_SEL_1:
6621 return cdclk; /* 576MHz or 624MHz */
6622 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6623 return cdclk * 2 / 3; /* 384MHz */
6624 case BXT_CDCLK_CD2X_DIV_SEL_2:
6625 return cdclk / 2; /* 288MHz */
6626 case BXT_CDCLK_CD2X_DIV_SEL_4:
6627 return cdclk / 4; /* 144MHz */
6628 }
6629
6630 /* error case, do as if DE PLL isn't enabled */
6631 return 19200;
6632}
6633
Ville Syrjälä1652d192015-03-31 14:12:01 +03006634static int broadwell_get_display_clock_speed(struct drm_device *dev)
6635{
6636 struct drm_i915_private *dev_priv = dev->dev_private;
6637 uint32_t lcpll = I915_READ(LCPLL_CTL);
6638 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6639
6640 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6641 return 800000;
6642 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6643 return 450000;
6644 else if (freq == LCPLL_CLK_FREQ_450)
6645 return 450000;
6646 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6647 return 540000;
6648 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6649 return 337500;
6650 else
6651 return 675000;
6652}
6653
6654static int haswell_get_display_clock_speed(struct drm_device *dev)
6655{
6656 struct drm_i915_private *dev_priv = dev->dev_private;
6657 uint32_t lcpll = I915_READ(LCPLL_CTL);
6658 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6659
6660 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6661 return 800000;
6662 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6663 return 450000;
6664 else if (freq == LCPLL_CLK_FREQ_450)
6665 return 450000;
6666 else if (IS_HSW_ULT(dev))
6667 return 337500;
6668 else
6669 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006670}
6671
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006672static int valleyview_get_display_clock_speed(struct drm_device *dev)
6673{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006674 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006675 u32 val;
6676 int divider;
6677
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006678 if (dev_priv->hpll_freq == 0)
6679 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6680
Ville Syrjäläa5805162015-05-26 20:42:30 +03006681 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006682 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006683 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006684
6685 divider = val & DISPLAY_FREQUENCY_VALUES;
6686
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006687 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6688 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6689 "cdclk change in progress\n");
6690
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006691 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006692}
6693
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006694static int ilk_get_display_clock_speed(struct drm_device *dev)
6695{
6696 return 450000;
6697}
6698
Jesse Barnese70236a2009-09-21 10:42:27 -07006699static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006700{
Jesse Barnese70236a2009-09-21 10:42:27 -07006701 return 400000;
6702}
Jesse Barnes79e53942008-11-07 14:24:08 -08006703
Jesse Barnese70236a2009-09-21 10:42:27 -07006704static int i915_get_display_clock_speed(struct drm_device *dev)
6705{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006706 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006707}
Jesse Barnes79e53942008-11-07 14:24:08 -08006708
Jesse Barnese70236a2009-09-21 10:42:27 -07006709static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6710{
6711 return 200000;
6712}
Jesse Barnes79e53942008-11-07 14:24:08 -08006713
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006714static int pnv_get_display_clock_speed(struct drm_device *dev)
6715{
6716 u16 gcfgc = 0;
6717
6718 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6719
6720 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6721 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006722 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006723 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006724 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006725 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006726 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006727 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6728 return 200000;
6729 default:
6730 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6731 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006732 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006733 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006734 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006735 }
6736}
6737
Jesse Barnese70236a2009-09-21 10:42:27 -07006738static int i915gm_get_display_clock_speed(struct drm_device *dev)
6739{
6740 u16 gcfgc = 0;
6741
6742 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6743
6744 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006745 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006746 else {
6747 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6748 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006749 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006750 default:
6751 case GC_DISPLAY_CLOCK_190_200_MHZ:
6752 return 190000;
6753 }
6754 }
6755}
Jesse Barnes79e53942008-11-07 14:24:08 -08006756
Jesse Barnese70236a2009-09-21 10:42:27 -07006757static int i865_get_display_clock_speed(struct drm_device *dev)
6758{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006759 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006760}
6761
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006762static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006763{
6764 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006765
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006766 /*
6767 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6768 * encoding is different :(
6769 * FIXME is this the right way to detect 852GM/852GMV?
6770 */
6771 if (dev->pdev->revision == 0x1)
6772 return 133333;
6773
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006774 pci_bus_read_config_word(dev->pdev->bus,
6775 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6776
Jesse Barnese70236a2009-09-21 10:42:27 -07006777 /* Assume that the hardware is in the high speed state. This
6778 * should be the default.
6779 */
6780 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6781 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006782 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006783 case GC_CLOCK_100_200:
6784 return 200000;
6785 case GC_CLOCK_166_250:
6786 return 250000;
6787 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006788 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006789 case GC_CLOCK_133_266:
6790 case GC_CLOCK_133_266_2:
6791 case GC_CLOCK_166_266:
6792 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006793 }
6794
6795 /* Shouldn't happen */
6796 return 0;
6797}
6798
6799static int i830_get_display_clock_speed(struct drm_device *dev)
6800{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006801 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006802}
6803
Ville Syrjälä34edce22015-05-22 11:22:33 +03006804static unsigned int intel_hpll_vco(struct drm_device *dev)
6805{
6806 struct drm_i915_private *dev_priv = dev->dev_private;
6807 static const unsigned int blb_vco[8] = {
6808 [0] = 3200000,
6809 [1] = 4000000,
6810 [2] = 5333333,
6811 [3] = 4800000,
6812 [4] = 6400000,
6813 };
6814 static const unsigned int pnv_vco[8] = {
6815 [0] = 3200000,
6816 [1] = 4000000,
6817 [2] = 5333333,
6818 [3] = 4800000,
6819 [4] = 2666667,
6820 };
6821 static const unsigned int cl_vco[8] = {
6822 [0] = 3200000,
6823 [1] = 4000000,
6824 [2] = 5333333,
6825 [3] = 6400000,
6826 [4] = 3333333,
6827 [5] = 3566667,
6828 [6] = 4266667,
6829 };
6830 static const unsigned int elk_vco[8] = {
6831 [0] = 3200000,
6832 [1] = 4000000,
6833 [2] = 5333333,
6834 [3] = 4800000,
6835 };
6836 static const unsigned int ctg_vco[8] = {
6837 [0] = 3200000,
6838 [1] = 4000000,
6839 [2] = 5333333,
6840 [3] = 6400000,
6841 [4] = 2666667,
6842 [5] = 4266667,
6843 };
6844 const unsigned int *vco_table;
6845 unsigned int vco;
6846 uint8_t tmp = 0;
6847
6848 /* FIXME other chipsets? */
6849 if (IS_GM45(dev))
6850 vco_table = ctg_vco;
6851 else if (IS_G4X(dev))
6852 vco_table = elk_vco;
6853 else if (IS_CRESTLINE(dev))
6854 vco_table = cl_vco;
6855 else if (IS_PINEVIEW(dev))
6856 vco_table = pnv_vco;
6857 else if (IS_G33(dev))
6858 vco_table = blb_vco;
6859 else
6860 return 0;
6861
6862 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6863
6864 vco = vco_table[tmp & 0x7];
6865 if (vco == 0)
6866 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6867 else
6868 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6869
6870 return vco;
6871}
6872
6873static int gm45_get_display_clock_speed(struct drm_device *dev)
6874{
6875 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6876 uint16_t tmp = 0;
6877
6878 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6879
6880 cdclk_sel = (tmp >> 12) & 0x1;
6881
6882 switch (vco) {
6883 case 2666667:
6884 case 4000000:
6885 case 5333333:
6886 return cdclk_sel ? 333333 : 222222;
6887 case 3200000:
6888 return cdclk_sel ? 320000 : 228571;
6889 default:
6890 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6891 return 222222;
6892 }
6893}
6894
6895static int i965gm_get_display_clock_speed(struct drm_device *dev)
6896{
6897 static const uint8_t div_3200[] = { 16, 10, 8 };
6898 static const uint8_t div_4000[] = { 20, 12, 10 };
6899 static const uint8_t div_5333[] = { 24, 16, 14 };
6900 const uint8_t *div_table;
6901 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6902 uint16_t tmp = 0;
6903
6904 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6905
6906 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6907
6908 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6909 goto fail;
6910
6911 switch (vco) {
6912 case 3200000:
6913 div_table = div_3200;
6914 break;
6915 case 4000000:
6916 div_table = div_4000;
6917 break;
6918 case 5333333:
6919 div_table = div_5333;
6920 break;
6921 default:
6922 goto fail;
6923 }
6924
6925 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6926
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006927fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006928 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6929 return 200000;
6930}
6931
6932static int g33_get_display_clock_speed(struct drm_device *dev)
6933{
6934 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6935 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6936 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6937 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6938 const uint8_t *div_table;
6939 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6940 uint16_t tmp = 0;
6941
6942 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6943
6944 cdclk_sel = (tmp >> 4) & 0x7;
6945
6946 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6947 goto fail;
6948
6949 switch (vco) {
6950 case 3200000:
6951 div_table = div_3200;
6952 break;
6953 case 4000000:
6954 div_table = div_4000;
6955 break;
6956 case 4800000:
6957 div_table = div_4800;
6958 break;
6959 case 5333333:
6960 div_table = div_5333;
6961 break;
6962 default:
6963 goto fail;
6964 }
6965
6966 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6967
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006968fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006969 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6970 return 190476;
6971}
6972
Zhenyu Wang2c072452009-06-05 15:38:42 +08006973static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006974intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006975{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006976 while (*num > DATA_LINK_M_N_MASK ||
6977 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006978 *num >>= 1;
6979 *den >>= 1;
6980 }
6981}
6982
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006983static void compute_m_n(unsigned int m, unsigned int n,
6984 uint32_t *ret_m, uint32_t *ret_n)
6985{
6986 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6987 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6988 intel_reduce_m_n_ratio(ret_m, ret_n);
6989}
6990
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006991void
6992intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6993 int pixel_clock, int link_clock,
6994 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006995{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006996 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006997
6998 compute_m_n(bits_per_pixel * pixel_clock,
6999 link_clock * nlanes * 8,
7000 &m_n->gmch_m, &m_n->gmch_n);
7001
7002 compute_m_n(pixel_clock, link_clock,
7003 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007004}
7005
Chris Wilsona7615032011-01-12 17:04:08 +00007006static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7007{
Jani Nikulad330a952014-01-21 11:24:25 +02007008 if (i915.panel_use_ssc >= 0)
7009 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007010 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007011 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007012}
7013
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007014static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7015 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007016{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007017 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007018 struct drm_i915_private *dev_priv = dev->dev_private;
7019 int refclk;
7020
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007021 WARN_ON(!crtc_state->base.state);
7022
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007023 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007024 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007025 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007026 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007027 refclk = dev_priv->vbt.lvds_ssc_freq;
7028 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007029 } else if (!IS_GEN2(dev)) {
7030 refclk = 96000;
7031 } else {
7032 refclk = 48000;
7033 }
7034
7035 return refclk;
7036}
7037
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007038static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007039{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007040 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007041}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007042
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007043static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7044{
7045 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007046}
7047
Daniel Vetterf47709a2013-03-28 10:42:02 +01007048static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007049 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007050 intel_clock_t *reduced_clock)
7051{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007052 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007053 u32 fp, fp2 = 0;
7054
7055 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007056 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007057 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007058 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007059 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007060 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007061 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007062 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007063 }
7064
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007065 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007066
Daniel Vetterf47709a2013-03-28 10:42:02 +01007067 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007068 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007069 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007070 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007071 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007072 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007073 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007074 }
7075}
7076
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007077static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7078 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007079{
7080 u32 reg_val;
7081
7082 /*
7083 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7084 * and set it to a reasonable value instead.
7085 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007086 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007087 reg_val &= 0xffffff00;
7088 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007089 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007090
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007091 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007092 reg_val &= 0x8cffffff;
7093 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007094 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007095
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007096 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007097 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007098 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007099
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007100 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007101 reg_val &= 0x00ffffff;
7102 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007103 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007104}
7105
Daniel Vetterb5518422013-05-03 11:49:48 +02007106static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7107 struct intel_link_m_n *m_n)
7108{
7109 struct drm_device *dev = crtc->base.dev;
7110 struct drm_i915_private *dev_priv = dev->dev_private;
7111 int pipe = crtc->pipe;
7112
Daniel Vettere3b95f12013-05-03 11:49:49 +02007113 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7114 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7115 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7116 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007117}
7118
7119static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007120 struct intel_link_m_n *m_n,
7121 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007122{
7123 struct drm_device *dev = crtc->base.dev;
7124 struct drm_i915_private *dev_priv = dev->dev_private;
7125 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007126 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007127
7128 if (INTEL_INFO(dev)->gen >= 5) {
7129 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7130 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7131 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7132 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007133 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7134 * for gen < 8) and if DRRS is supported (to make sure the
7135 * registers are not unnecessarily accessed).
7136 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307137 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007138 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007139 I915_WRITE(PIPE_DATA_M2(transcoder),
7140 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7141 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7142 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7143 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7144 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007145 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007146 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7147 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7148 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7149 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007150 }
7151}
7152
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307153void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007154{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307155 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7156
7157 if (m_n == M1_N1) {
7158 dp_m_n = &crtc->config->dp_m_n;
7159 dp_m2_n2 = &crtc->config->dp_m2_n2;
7160 } else if (m_n == M2_N2) {
7161
7162 /*
7163 * M2_N2 registers are not supported. Hence m2_n2 divider value
7164 * needs to be programmed into M1_N1.
7165 */
7166 dp_m_n = &crtc->config->dp_m2_n2;
7167 } else {
7168 DRM_ERROR("Unsupported divider value\n");
7169 return;
7170 }
7171
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007172 if (crtc->config->has_pch_encoder)
7173 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007174 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307175 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007176}
7177
Daniel Vetter251ac862015-06-18 10:30:24 +02007178static void vlv_compute_dpll(struct intel_crtc *crtc,
7179 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007180{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007181 u32 dpll, dpll_md;
7182
7183 /*
7184 * Enable DPIO clock input. We should never disable the reference
7185 * clock for pipe B, since VGA hotplug / manual detection depends
7186 * on it.
7187 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007188 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7189 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007190 /* We should never disable this, set it here for state tracking */
7191 if (crtc->pipe == PIPE_B)
7192 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7193 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007194 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007195
Ville Syrjäläd288f652014-10-28 13:20:22 +02007196 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007197 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007198 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007199}
7200
Ville Syrjäläd288f652014-10-28 13:20:22 +02007201static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007202 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007203{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007204 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007205 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007206 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007207 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007208 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007209 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007210
Ville Syrjäläa5805162015-05-26 20:42:30 +03007211 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007212
Ville Syrjäläd288f652014-10-28 13:20:22 +02007213 bestn = pipe_config->dpll.n;
7214 bestm1 = pipe_config->dpll.m1;
7215 bestm2 = pipe_config->dpll.m2;
7216 bestp1 = pipe_config->dpll.p1;
7217 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007218
Jesse Barnes89b667f2013-04-18 14:51:36 -07007219 /* See eDP HDMI DPIO driver vbios notes doc */
7220
7221 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007222 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007223 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007224
7225 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007226 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007227
7228 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007229 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007230 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007231 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007232
7233 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007234 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007235
7236 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007237 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7238 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7239 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007240 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007241
7242 /*
7243 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7244 * but we don't support that).
7245 * Note: don't use the DAC post divider as it seems unstable.
7246 */
7247 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007248 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007249
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007250 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007251 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007252
Jesse Barnes89b667f2013-04-18 14:51:36 -07007253 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007254 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007255 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7256 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007257 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007258 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007259 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007260 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007261 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007262
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007263 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007264 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007265 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007266 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007267 0x0df40000);
7268 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007269 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007270 0x0df70000);
7271 } else { /* HDMI or VGA */
7272 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007273 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007274 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007275 0x0df70000);
7276 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007277 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007278 0x0df40000);
7279 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007280
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007281 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007282 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007283 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7284 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007286 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007287
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007288 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007289 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007290}
7291
Daniel Vetter251ac862015-06-18 10:30:24 +02007292static void chv_compute_dpll(struct intel_crtc *crtc,
7293 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007294{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007295 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7296 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007297 DPLL_VCO_ENABLE;
7298 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007299 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007300
Ville Syrjäläd288f652014-10-28 13:20:22 +02007301 pipe_config->dpll_hw_state.dpll_md =
7302 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007303}
7304
Ville Syrjäläd288f652014-10-28 13:20:22 +02007305static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007306 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007307{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007308 struct drm_device *dev = crtc->base.dev;
7309 struct drm_i915_private *dev_priv = dev->dev_private;
7310 int pipe = crtc->pipe;
7311 int dpll_reg = DPLL(crtc->pipe);
7312 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307313 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007314 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307315 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307316 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007317
Ville Syrjäläd288f652014-10-28 13:20:22 +02007318 bestn = pipe_config->dpll.n;
7319 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7320 bestm1 = pipe_config->dpll.m1;
7321 bestm2 = pipe_config->dpll.m2 >> 22;
7322 bestp1 = pipe_config->dpll.p1;
7323 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307324 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307325 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307326 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007327
7328 /*
7329 * Enable Refclk and SSC
7330 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007331 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007332 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007333
Ville Syrjäläa5805162015-05-26 20:42:30 +03007334 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007335
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007336 /* p1 and p2 divider */
7337 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7338 5 << DPIO_CHV_S1_DIV_SHIFT |
7339 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7340 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7341 1 << DPIO_CHV_K_DIV_SHIFT);
7342
7343 /* Feedback post-divider - m2 */
7344 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7345
7346 /* Feedback refclk divider - n and m1 */
7347 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7348 DPIO_CHV_M1_DIV_BY_2 |
7349 1 << DPIO_CHV_N_DIV_SHIFT);
7350
7351 /* M2 fraction division */
Ville Syrjälä25a25df2015-07-08 23:45:47 +03007352 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007353
7354 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307355 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7356 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7357 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7358 if (bestm2_frac)
7359 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7360 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007361
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307362 /* Program digital lock detect threshold */
7363 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7364 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7365 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7366 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7367 if (!bestm2_frac)
7368 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7369 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7370
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007371 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307372 if (vco == 5400000) {
7373 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7374 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7375 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7376 tribuf_calcntr = 0x9;
7377 } else if (vco <= 6200000) {
7378 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7379 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7380 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7381 tribuf_calcntr = 0x9;
7382 } else if (vco <= 6480000) {
7383 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7384 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7385 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7386 tribuf_calcntr = 0x8;
7387 } else {
7388 /* Not supported. Apply the same limits as in the max case */
7389 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7390 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7391 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7392 tribuf_calcntr = 0;
7393 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007394 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7395
Ville Syrjälä968040b2015-03-11 22:52:08 +02007396 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307397 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7398 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7399 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7400
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007401 /* AFC Recal */
7402 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7403 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7404 DPIO_AFC_RECAL);
7405
Ville Syrjäläa5805162015-05-26 20:42:30 +03007406 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007407}
7408
Ville Syrjäläd288f652014-10-28 13:20:22 +02007409/**
7410 * vlv_force_pll_on - forcibly enable just the PLL
7411 * @dev_priv: i915 private structure
7412 * @pipe: pipe PLL to enable
7413 * @dpll: PLL configuration
7414 *
7415 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7416 * in cases where we need the PLL enabled even when @pipe is not going to
7417 * be enabled.
7418 */
7419void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7420 const struct dpll *dpll)
7421{
7422 struct intel_crtc *crtc =
7423 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007424 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007425 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007426 .pixel_multiplier = 1,
7427 .dpll = *dpll,
7428 };
7429
7430 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007431 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007432 chv_prepare_pll(crtc, &pipe_config);
7433 chv_enable_pll(crtc, &pipe_config);
7434 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007435 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007436 vlv_prepare_pll(crtc, &pipe_config);
7437 vlv_enable_pll(crtc, &pipe_config);
7438 }
7439}
7440
7441/**
7442 * vlv_force_pll_off - forcibly disable just the PLL
7443 * @dev_priv: i915 private structure
7444 * @pipe: pipe PLL to disable
7445 *
7446 * Disable the PLL for @pipe. To be used in cases where we need
7447 * the PLL enabled even when @pipe is not going to be enabled.
7448 */
7449void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7450{
7451 if (IS_CHERRYVIEW(dev))
7452 chv_disable_pll(to_i915(dev), pipe);
7453 else
7454 vlv_disable_pll(to_i915(dev), pipe);
7455}
7456
Daniel Vetter251ac862015-06-18 10:30:24 +02007457static void i9xx_compute_dpll(struct intel_crtc *crtc,
7458 struct intel_crtc_state *crtc_state,
7459 intel_clock_t *reduced_clock,
7460 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007461{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007462 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007463 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007464 u32 dpll;
7465 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007466 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007467
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007468 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307469
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007470 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7471 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007472
7473 dpll = DPLL_VGA_MODE_DIS;
7474
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007475 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007476 dpll |= DPLLB_MODE_LVDS;
7477 else
7478 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007479
Daniel Vetteref1b4602013-06-01 17:17:04 +02007480 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007481 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007482 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007483 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007484
7485 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007486 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007487
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007488 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007489 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007490
7491 /* compute bitmask from p1 value */
7492 if (IS_PINEVIEW(dev))
7493 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7494 else {
7495 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7496 if (IS_G4X(dev) && reduced_clock)
7497 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7498 }
7499 switch (clock->p2) {
7500 case 5:
7501 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7502 break;
7503 case 7:
7504 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7505 break;
7506 case 10:
7507 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7508 break;
7509 case 14:
7510 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7511 break;
7512 }
7513 if (INTEL_INFO(dev)->gen >= 4)
7514 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7515
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007516 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007517 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007518 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007519 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7520 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7521 else
7522 dpll |= PLL_REF_INPUT_DREFCLK;
7523
7524 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007525 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007526
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007527 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007528 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007529 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007530 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007531 }
7532}
7533
Daniel Vetter251ac862015-06-18 10:30:24 +02007534static void i8xx_compute_dpll(struct intel_crtc *crtc,
7535 struct intel_crtc_state *crtc_state,
7536 intel_clock_t *reduced_clock,
7537 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007538{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007539 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007540 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007541 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007542 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007543
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007544 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307545
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007546 dpll = DPLL_VGA_MODE_DIS;
7547
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007548 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007549 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7550 } else {
7551 if (clock->p1 == 2)
7552 dpll |= PLL_P1_DIVIDE_BY_TWO;
7553 else
7554 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7555 if (clock->p2 == 4)
7556 dpll |= PLL_P2_DIVIDE_BY_4;
7557 }
7558
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007559 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007560 dpll |= DPLL_DVO_2X_MODE;
7561
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007562 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007563 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7564 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7565 else
7566 dpll |= PLL_REF_INPUT_DREFCLK;
7567
7568 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007569 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007570}
7571
Daniel Vetter8a654f32013-06-01 17:16:22 +02007572static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007573{
7574 struct drm_device *dev = intel_crtc->base.dev;
7575 struct drm_i915_private *dev_priv = dev->dev_private;
7576 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007577 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007578 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007579 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007580 uint32_t crtc_vtotal, crtc_vblank_end;
7581 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007582
7583 /* We need to be careful not to changed the adjusted mode, for otherwise
7584 * the hw state checker will get angry at the mismatch. */
7585 crtc_vtotal = adjusted_mode->crtc_vtotal;
7586 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007587
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007588 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007589 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007590 crtc_vtotal -= 1;
7591 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007592
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007593 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007594 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7595 else
7596 vsyncshift = adjusted_mode->crtc_hsync_start -
7597 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007598 if (vsyncshift < 0)
7599 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007600 }
7601
7602 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007603 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007604
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007605 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007606 (adjusted_mode->crtc_hdisplay - 1) |
7607 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007608 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007609 (adjusted_mode->crtc_hblank_start - 1) |
7610 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007611 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007612 (adjusted_mode->crtc_hsync_start - 1) |
7613 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7614
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007615 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007616 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007617 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007618 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007619 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007620 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007621 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007622 (adjusted_mode->crtc_vsync_start - 1) |
7623 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7624
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007625 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7626 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7627 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7628 * bits. */
7629 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7630 (pipe == PIPE_B || pipe == PIPE_C))
7631 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7632
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007633 /* pipesrc controls the size that is scaled from, which should
7634 * always be the user's requested size.
7635 */
7636 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007637 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7638 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007639}
7640
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007641static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007642 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007643{
7644 struct drm_device *dev = crtc->base.dev;
7645 struct drm_i915_private *dev_priv = dev->dev_private;
7646 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7647 uint32_t tmp;
7648
7649 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007650 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7651 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007652 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007653 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7654 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007655 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007656 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7657 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007658
7659 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007660 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7661 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007662 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007663 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7664 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007665 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007666 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7667 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007668
7669 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007670 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7671 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7672 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007673 }
7674
7675 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007676 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7677 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7678
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007679 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7680 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007681}
7682
Daniel Vetterf6a83282014-02-11 15:28:57 -08007683void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007684 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007685{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007686 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7687 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7688 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7689 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007690
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007691 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7692 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7693 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7694 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007695
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007696 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007697 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007698
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007699 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7700 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007701
7702 mode->hsync = drm_mode_hsync(mode);
7703 mode->vrefresh = drm_mode_vrefresh(mode);
7704 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007705}
7706
Daniel Vetter84b046f2013-02-19 18:48:54 +01007707static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7708{
7709 struct drm_device *dev = intel_crtc->base.dev;
7710 struct drm_i915_private *dev_priv = dev->dev_private;
7711 uint32_t pipeconf;
7712
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007713 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007714
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007715 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7716 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7717 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007718
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007719 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007720 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007721
Daniel Vetterff9ce462013-04-24 14:57:17 +02007722 /* only g4x and later have fancy bpc/dither controls */
7723 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007724 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007725 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007726 pipeconf |= PIPECONF_DITHER_EN |
7727 PIPECONF_DITHER_TYPE_SP;
7728
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007729 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007730 case 18:
7731 pipeconf |= PIPECONF_6BPC;
7732 break;
7733 case 24:
7734 pipeconf |= PIPECONF_8BPC;
7735 break;
7736 case 30:
7737 pipeconf |= PIPECONF_10BPC;
7738 break;
7739 default:
7740 /* Case prevented by intel_choose_pipe_bpp_dither. */
7741 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007742 }
7743 }
7744
7745 if (HAS_PIPE_CXSR(dev)) {
7746 if (intel_crtc->lowfreq_avail) {
7747 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7748 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7749 } else {
7750 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007751 }
7752 }
7753
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007754 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007755 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007756 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007757 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7758 else
7759 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7760 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007761 pipeconf |= PIPECONF_PROGRESSIVE;
7762
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007763 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007764 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007765
Daniel Vetter84b046f2013-02-19 18:48:54 +01007766 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7767 POSTING_READ(PIPECONF(intel_crtc->pipe));
7768}
7769
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007770static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7771 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007772{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007773 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007774 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007775 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007776 intel_clock_t clock;
7777 bool ok;
7778 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007779 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007780 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007781 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007782 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007783 struct drm_connector_state *connector_state;
7784 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007785
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007786 memset(&crtc_state->dpll_hw_state, 0,
7787 sizeof(crtc_state->dpll_hw_state));
7788
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007789 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007790 if (connector_state->crtc != &crtc->base)
7791 continue;
7792
7793 encoder = to_intel_encoder(connector_state->best_encoder);
7794
Chris Wilson5eddb702010-09-11 13:48:45 +01007795 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007796 case INTEL_OUTPUT_DSI:
7797 is_dsi = true;
7798 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007799 default:
7800 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007801 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007802
Eric Anholtc751ce42010-03-25 11:48:48 -07007803 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007804 }
7805
Jani Nikulaf2335332013-09-13 11:03:09 +03007806 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007807 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007808
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007809 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007810 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007811
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007812 /*
7813 * Returns a set of divisors for the desired target clock with
7814 * the given refclk, or FALSE. The returned values represent
7815 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7816 * 2) / p1 / p2.
7817 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007818 limit = intel_limit(crtc_state, refclk);
7819 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007820 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007821 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007822 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007823 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7824 return -EINVAL;
7825 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007826
Jani Nikulaf2335332013-09-13 11:03:09 +03007827 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007828 crtc_state->dpll.n = clock.n;
7829 crtc_state->dpll.m1 = clock.m1;
7830 crtc_state->dpll.m2 = clock.m2;
7831 crtc_state->dpll.p1 = clock.p1;
7832 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007833 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007834
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007835 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007836 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007837 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007838 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007839 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007840 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007841 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007842 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007843 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007844 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007845 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007846
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007847 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007848}
7849
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007850static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007851 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007852{
7853 struct drm_device *dev = crtc->base.dev;
7854 struct drm_i915_private *dev_priv = dev->dev_private;
7855 uint32_t tmp;
7856
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007857 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7858 return;
7859
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007860 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007861 if (!(tmp & PFIT_ENABLE))
7862 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007863
Daniel Vetter06922822013-07-11 13:35:40 +02007864 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007865 if (INTEL_INFO(dev)->gen < 4) {
7866 if (crtc->pipe != PIPE_B)
7867 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007868 } else {
7869 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7870 return;
7871 }
7872
Daniel Vetter06922822013-07-11 13:35:40 +02007873 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007874 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7875 if (INTEL_INFO(dev)->gen < 5)
7876 pipe_config->gmch_pfit.lvds_border_bits =
7877 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7878}
7879
Jesse Barnesacbec812013-09-20 11:29:32 -07007880static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007881 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007882{
7883 struct drm_device *dev = crtc->base.dev;
7884 struct drm_i915_private *dev_priv = dev->dev_private;
7885 int pipe = pipe_config->cpu_transcoder;
7886 intel_clock_t clock;
7887 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007888 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007889
Shobhit Kumarf573de52014-07-30 20:32:37 +05307890 /* In case of MIPI DPLL will not even be used */
7891 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7892 return;
7893
Ville Syrjäläa5805162015-05-26 20:42:30 +03007894 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007895 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007896 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007897
7898 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7899 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7900 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7901 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7902 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7903
Imre Deakdccbea32015-06-22 23:35:51 +03007904 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007905}
7906
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007907static void
7908i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7909 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007910{
7911 struct drm_device *dev = crtc->base.dev;
7912 struct drm_i915_private *dev_priv = dev->dev_private;
7913 u32 val, base, offset;
7914 int pipe = crtc->pipe, plane = crtc->plane;
7915 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007916 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007917 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007918 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007919
Damien Lespiau42a7b082015-02-05 19:35:13 +00007920 val = I915_READ(DSPCNTR(plane));
7921 if (!(val & DISPLAY_PLANE_ENABLE))
7922 return;
7923
Damien Lespiaud9806c92015-01-21 14:07:19 +00007924 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007925 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007926 DRM_DEBUG_KMS("failed to alloc fb\n");
7927 return;
7928 }
7929
Damien Lespiau1b842c82015-01-21 13:50:54 +00007930 fb = &intel_fb->base;
7931
Daniel Vetter18c52472015-02-10 17:16:09 +00007932 if (INTEL_INFO(dev)->gen >= 4) {
7933 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007934 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007935 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7936 }
7937 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007938
7939 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007940 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007941 fb->pixel_format = fourcc;
7942 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007943
7944 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007945 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007946 offset = I915_READ(DSPTILEOFF(plane));
7947 else
7948 offset = I915_READ(DSPLINOFF(plane));
7949 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7950 } else {
7951 base = I915_READ(DSPADDR(plane));
7952 }
7953 plane_config->base = base;
7954
7955 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007956 fb->width = ((val >> 16) & 0xfff) + 1;
7957 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007958
7959 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007960 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007961
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007962 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007963 fb->pixel_format,
7964 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007965
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007966 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007967
Damien Lespiau2844a922015-01-20 12:51:48 +00007968 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7969 pipe_name(pipe), plane, fb->width, fb->height,
7970 fb->bits_per_pixel, base, fb->pitches[0],
7971 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007972
Damien Lespiau2d140302015-02-05 17:22:18 +00007973 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007974}
7975
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007976static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007977 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007978{
7979 struct drm_device *dev = crtc->base.dev;
7980 struct drm_i915_private *dev_priv = dev->dev_private;
7981 int pipe = pipe_config->cpu_transcoder;
7982 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7983 intel_clock_t clock;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007984 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007985 int refclk = 100000;
7986
Ville Syrjäläa5805162015-05-26 20:42:30 +03007987 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007988 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7989 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7990 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7991 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Imre Deak0d7b6b12015-07-02 14:29:58 +03007992 pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007993 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007994
7995 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
Imre Deak0d7b6b12015-07-02 14:29:58 +03007996 clock.m2 = (pll_dw0 & 0xff) << 22;
7997 if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
7998 clock.m2 |= pll_dw2 & 0x3fffff;
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007999 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8000 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8001 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8002
Imre Deakdccbea32015-06-22 23:35:51 +03008003 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008004}
8005
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008006static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008007 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008008{
8009 struct drm_device *dev = crtc->base.dev;
8010 struct drm_i915_private *dev_priv = dev->dev_private;
8011 uint32_t tmp;
8012
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008013 if (!intel_display_power_is_enabled(dev_priv,
8014 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008015 return false;
8016
Daniel Vettere143a212013-07-04 12:01:15 +02008017 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008018 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008019
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008020 tmp = I915_READ(PIPECONF(crtc->pipe));
8021 if (!(tmp & PIPECONF_ENABLE))
8022 return false;
8023
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008024 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8025 switch (tmp & PIPECONF_BPC_MASK) {
8026 case PIPECONF_6BPC:
8027 pipe_config->pipe_bpp = 18;
8028 break;
8029 case PIPECONF_8BPC:
8030 pipe_config->pipe_bpp = 24;
8031 break;
8032 case PIPECONF_10BPC:
8033 pipe_config->pipe_bpp = 30;
8034 break;
8035 default:
8036 break;
8037 }
8038 }
8039
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008040 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8041 pipe_config->limited_color_range = true;
8042
Ville Syrjälä282740f2013-09-04 18:30:03 +03008043 if (INTEL_INFO(dev)->gen < 4)
8044 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8045
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008046 intel_get_pipe_timings(crtc, pipe_config);
8047
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008048 i9xx_get_pfit_config(crtc, pipe_config);
8049
Daniel Vetter6c49f242013-06-06 12:45:25 +02008050 if (INTEL_INFO(dev)->gen >= 4) {
8051 tmp = I915_READ(DPLL_MD(crtc->pipe));
8052 pipe_config->pixel_multiplier =
8053 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8054 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008055 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008056 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8057 tmp = I915_READ(DPLL(crtc->pipe));
8058 pipe_config->pixel_multiplier =
8059 ((tmp & SDVO_MULTIPLIER_MASK)
8060 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8061 } else {
8062 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8063 * port and will be fixed up in the encoder->get_config
8064 * function. */
8065 pipe_config->pixel_multiplier = 1;
8066 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008067 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8068 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008069 /*
8070 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8071 * on 830. Filter it out here so that we don't
8072 * report errors due to that.
8073 */
8074 if (IS_I830(dev))
8075 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8076
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008077 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8078 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008079 } else {
8080 /* Mask out read-only status bits. */
8081 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8082 DPLL_PORTC_READY_MASK |
8083 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008084 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008085
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008086 if (IS_CHERRYVIEW(dev))
8087 chv_crtc_clock_get(crtc, pipe_config);
8088 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008089 vlv_crtc_clock_get(crtc, pipe_config);
8090 else
8091 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008092
Ville Syrjälä0f646142015-08-26 19:39:18 +03008093 /*
8094 * Normally the dotclock is filled in by the encoder .get_config()
8095 * but in case the pipe is enabled w/o any ports we need a sane
8096 * default.
8097 */
8098 pipe_config->base.adjusted_mode.crtc_clock =
8099 pipe_config->port_clock / pipe_config->pixel_multiplier;
8100
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008101 return true;
8102}
8103
Paulo Zanonidde86e22012-12-01 12:04:25 -02008104static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008105{
8106 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008107 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008108 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008109 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008110 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008111 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008112 bool has_ck505 = false;
8113 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008114
8115 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008116 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008117 switch (encoder->type) {
8118 case INTEL_OUTPUT_LVDS:
8119 has_panel = true;
8120 has_lvds = true;
8121 break;
8122 case INTEL_OUTPUT_EDP:
8123 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008124 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008125 has_cpu_edp = true;
8126 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008127 default:
8128 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008129 }
8130 }
8131
Keith Packard99eb6a02011-09-26 14:29:12 -07008132 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008133 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008134 can_ssc = has_ck505;
8135 } else {
8136 has_ck505 = false;
8137 can_ssc = true;
8138 }
8139
Imre Deak2de69052013-05-08 13:14:04 +03008140 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8141 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008142
8143 /* Ironlake: try to setup display ref clock before DPLL
8144 * enabling. This is only under driver's control after
8145 * PCH B stepping, previous chipset stepping should be
8146 * ignoring this setting.
8147 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008148 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008149
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008150 /* As we must carefully and slowly disable/enable each source in turn,
8151 * compute the final state we want first and check if we need to
8152 * make any changes at all.
8153 */
8154 final = val;
8155 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008156 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008157 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008158 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008159 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8160
8161 final &= ~DREF_SSC_SOURCE_MASK;
8162 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8163 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008164
Keith Packard199e5d72011-09-22 12:01:57 -07008165 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008166 final |= DREF_SSC_SOURCE_ENABLE;
8167
8168 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8169 final |= DREF_SSC1_ENABLE;
8170
8171 if (has_cpu_edp) {
8172 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8173 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8174 else
8175 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8176 } else
8177 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8178 } else {
8179 final |= DREF_SSC_SOURCE_DISABLE;
8180 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8181 }
8182
8183 if (final == val)
8184 return;
8185
8186 /* Always enable nonspread source */
8187 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8188
8189 if (has_ck505)
8190 val |= DREF_NONSPREAD_CK505_ENABLE;
8191 else
8192 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8193
8194 if (has_panel) {
8195 val &= ~DREF_SSC_SOURCE_MASK;
8196 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008197
Keith Packard199e5d72011-09-22 12:01:57 -07008198 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008199 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008200 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008201 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008202 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008203 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008204
8205 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008206 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008207 POSTING_READ(PCH_DREF_CONTROL);
8208 udelay(200);
8209
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008210 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008211
8212 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008213 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008214 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008215 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008216 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008217 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008218 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008219 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008220 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008221
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008222 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008223 POSTING_READ(PCH_DREF_CONTROL);
8224 udelay(200);
8225 } else {
8226 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8227
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008228 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008229
8230 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008231 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008232
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008233 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008234 POSTING_READ(PCH_DREF_CONTROL);
8235 udelay(200);
8236
8237 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008238 val &= ~DREF_SSC_SOURCE_MASK;
8239 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008240
8241 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008242 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008243
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008244 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008245 POSTING_READ(PCH_DREF_CONTROL);
8246 udelay(200);
8247 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008248
8249 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008250}
8251
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008252static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008253{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008254 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008255
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008256 tmp = I915_READ(SOUTH_CHICKEN2);
8257 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8258 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008259
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008260 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8261 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8262 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008263
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008264 tmp = I915_READ(SOUTH_CHICKEN2);
8265 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8266 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008267
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008268 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8269 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8270 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008271}
8272
8273/* WaMPhyProgramming:hsw */
8274static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8275{
8276 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008277
8278 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8279 tmp &= ~(0xFF << 24);
8280 tmp |= (0x12 << 24);
8281 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8282
Paulo Zanonidde86e22012-12-01 12:04:25 -02008283 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8284 tmp |= (1 << 11);
8285 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8286
8287 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8288 tmp |= (1 << 11);
8289 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8290
Paulo Zanonidde86e22012-12-01 12:04:25 -02008291 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8292 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8293 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8294
8295 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8296 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8297 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8298
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008299 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8300 tmp &= ~(7 << 13);
8301 tmp |= (5 << 13);
8302 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008303
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008304 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8305 tmp &= ~(7 << 13);
8306 tmp |= (5 << 13);
8307 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008308
8309 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8310 tmp &= ~0xFF;
8311 tmp |= 0x1C;
8312 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8313
8314 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8315 tmp &= ~0xFF;
8316 tmp |= 0x1C;
8317 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8318
8319 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8320 tmp &= ~(0xFF << 16);
8321 tmp |= (0x1C << 16);
8322 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8323
8324 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8325 tmp &= ~(0xFF << 16);
8326 tmp |= (0x1C << 16);
8327 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8328
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008329 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8330 tmp |= (1 << 27);
8331 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008332
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008333 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8334 tmp |= (1 << 27);
8335 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008336
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008337 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8338 tmp &= ~(0xF << 28);
8339 tmp |= (4 << 28);
8340 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008341
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008342 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8343 tmp &= ~(0xF << 28);
8344 tmp |= (4 << 28);
8345 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008346}
8347
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008348/* Implements 3 different sequences from BSpec chapter "Display iCLK
8349 * Programming" based on the parameters passed:
8350 * - Sequence to enable CLKOUT_DP
8351 * - Sequence to enable CLKOUT_DP without spread
8352 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8353 */
8354static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8355 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008356{
8357 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008358 uint32_t reg, tmp;
8359
8360 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8361 with_spread = true;
Ville Syrjäläc2699522015-08-27 23:55:59 +03008362 if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008363 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008364
Ville Syrjäläa5805162015-05-26 20:42:30 +03008365 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008366
8367 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8368 tmp &= ~SBI_SSCCTL_DISABLE;
8369 tmp |= SBI_SSCCTL_PATHALT;
8370 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8371
8372 udelay(24);
8373
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008374 if (with_spread) {
8375 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8376 tmp &= ~SBI_SSCCTL_PATHALT;
8377 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008378
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008379 if (with_fdi) {
8380 lpt_reset_fdi_mphy(dev_priv);
8381 lpt_program_fdi_mphy(dev_priv);
8382 }
8383 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008384
Ville Syrjäläc2699522015-08-27 23:55:59 +03008385 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008386 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8387 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8388 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008389
Ville Syrjäläa5805162015-05-26 20:42:30 +03008390 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008391}
8392
Paulo Zanoni47701c32013-07-23 11:19:25 -03008393/* Sequence to disable CLKOUT_DP */
8394static void lpt_disable_clkout_dp(struct drm_device *dev)
8395{
8396 struct drm_i915_private *dev_priv = dev->dev_private;
8397 uint32_t reg, tmp;
8398
Ville Syrjäläa5805162015-05-26 20:42:30 +03008399 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008400
Ville Syrjäläc2699522015-08-27 23:55:59 +03008401 reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
Paulo Zanoni47701c32013-07-23 11:19:25 -03008402 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8403 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8404 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8405
8406 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8407 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8408 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8409 tmp |= SBI_SSCCTL_PATHALT;
8410 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8411 udelay(32);
8412 }
8413 tmp |= SBI_SSCCTL_DISABLE;
8414 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8415 }
8416
Ville Syrjäläa5805162015-05-26 20:42:30 +03008417 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008418}
8419
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008420static void lpt_init_pch_refclk(struct drm_device *dev)
8421{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008422 struct intel_encoder *encoder;
8423 bool has_vga = false;
8424
Damien Lespiaub2784e12014-08-05 11:29:37 +01008425 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008426 switch (encoder->type) {
8427 case INTEL_OUTPUT_ANALOG:
8428 has_vga = true;
8429 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008430 default:
8431 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008432 }
8433 }
8434
Paulo Zanoni47701c32013-07-23 11:19:25 -03008435 if (has_vga)
8436 lpt_enable_clkout_dp(dev, true, true);
8437 else
8438 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008439}
8440
Paulo Zanonidde86e22012-12-01 12:04:25 -02008441/*
8442 * Initialize reference clocks when the driver loads
8443 */
8444void intel_init_pch_refclk(struct drm_device *dev)
8445{
8446 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8447 ironlake_init_pch_refclk(dev);
8448 else if (HAS_PCH_LPT(dev))
8449 lpt_init_pch_refclk(dev);
8450}
8451
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008452static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008453{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008454 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008455 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008456 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008457 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008458 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008459 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008460 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008461 bool is_lvds = false;
8462
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008463 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008464 if (connector_state->crtc != crtc_state->base.crtc)
8465 continue;
8466
8467 encoder = to_intel_encoder(connector_state->best_encoder);
8468
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008469 switch (encoder->type) {
8470 case INTEL_OUTPUT_LVDS:
8471 is_lvds = true;
8472 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008473 default:
8474 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008475 }
8476 num_connectors++;
8477 }
8478
8479 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008480 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008481 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008482 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008483 }
8484
8485 return 120000;
8486}
8487
Daniel Vetter6ff93602013-04-19 11:24:36 +02008488static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008489{
8490 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8491 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8492 int pipe = intel_crtc->pipe;
8493 uint32_t val;
8494
Daniel Vetter78114072013-06-13 00:54:57 +02008495 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008496
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008497 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008498 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008499 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008500 break;
8501 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008502 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008503 break;
8504 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008505 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008506 break;
8507 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008508 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008509 break;
8510 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008511 /* Case prevented by intel_choose_pipe_bpp_dither. */
8512 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008513 }
8514
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008515 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008516 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8517
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008518 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008519 val |= PIPECONF_INTERLACED_ILK;
8520 else
8521 val |= PIPECONF_PROGRESSIVE;
8522
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008523 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008524 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008525
Paulo Zanonic8203562012-09-12 10:06:29 -03008526 I915_WRITE(PIPECONF(pipe), val);
8527 POSTING_READ(PIPECONF(pipe));
8528}
8529
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008530/*
8531 * Set up the pipe CSC unit.
8532 *
8533 * Currently only full range RGB to limited range RGB conversion
8534 * is supported, but eventually this should handle various
8535 * RGB<->YCbCr scenarios as well.
8536 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008537static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008538{
8539 struct drm_device *dev = crtc->dev;
8540 struct drm_i915_private *dev_priv = dev->dev_private;
8541 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8542 int pipe = intel_crtc->pipe;
8543 uint16_t coeff = 0x7800; /* 1.0 */
8544
8545 /*
8546 * TODO: Check what kind of values actually come out of the pipe
8547 * with these coeff/postoff values and adjust to get the best
8548 * accuracy. Perhaps we even need to take the bpc value into
8549 * consideration.
8550 */
8551
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008552 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008553 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8554
8555 /*
8556 * GY/GU and RY/RU should be the other way around according
8557 * to BSpec, but reality doesn't agree. Just set them up in
8558 * a way that results in the correct picture.
8559 */
8560 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8561 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8562
8563 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8564 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8565
8566 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8567 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8568
8569 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8570 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8571 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8572
8573 if (INTEL_INFO(dev)->gen > 6) {
8574 uint16_t postoff = 0;
8575
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008576 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008577 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008578
8579 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8580 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8581 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8582
8583 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8584 } else {
8585 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8586
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008587 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008588 mode |= CSC_BLACK_SCREEN_OFFSET;
8589
8590 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8591 }
8592}
8593
Daniel Vetter6ff93602013-04-19 11:24:36 +02008594static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008595{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008596 struct drm_device *dev = crtc->dev;
8597 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008598 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008599 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008600 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008601 uint32_t val;
8602
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008603 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008604
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008605 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008606 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8607
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008608 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008609 val |= PIPECONF_INTERLACED_ILK;
8610 else
8611 val |= PIPECONF_PROGRESSIVE;
8612
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008613 I915_WRITE(PIPECONF(cpu_transcoder), val);
8614 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008615
8616 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8617 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008618
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308619 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008620 val = 0;
8621
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008622 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008623 case 18:
8624 val |= PIPEMISC_DITHER_6_BPC;
8625 break;
8626 case 24:
8627 val |= PIPEMISC_DITHER_8_BPC;
8628 break;
8629 case 30:
8630 val |= PIPEMISC_DITHER_10_BPC;
8631 break;
8632 case 36:
8633 val |= PIPEMISC_DITHER_12_BPC;
8634 break;
8635 default:
8636 /* Case prevented by pipe_config_set_bpp. */
8637 BUG();
8638 }
8639
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008640 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008641 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8642
8643 I915_WRITE(PIPEMISC(pipe), val);
8644 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008645}
8646
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008647static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008648 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008649 intel_clock_t *clock,
8650 bool *has_reduced_clock,
8651 intel_clock_t *reduced_clock)
8652{
8653 struct drm_device *dev = crtc->dev;
8654 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008655 int refclk;
8656 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008657 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008658
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008659 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008660
8661 /*
8662 * Returns a set of divisors for the desired target clock with the given
8663 * refclk, or FALSE. The returned values represent the clock equation:
8664 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8665 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008666 limit = intel_limit(crtc_state, refclk);
8667 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008668 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008669 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008670 if (!ret)
8671 return false;
8672
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008673 return true;
8674}
8675
Paulo Zanonid4b19312012-11-29 11:29:32 -02008676int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8677{
8678 /*
8679 * Account for spread spectrum to avoid
8680 * oversubscribing the link. Max center spread
8681 * is 2.5%; use 5% for safety's sake.
8682 */
8683 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008684 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008685}
8686
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008687static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008688{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008689 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008690}
8691
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008692static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008693 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008694 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008695 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008696{
8697 struct drm_crtc *crtc = &intel_crtc->base;
8698 struct drm_device *dev = crtc->dev;
8699 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008700 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008701 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008702 struct drm_connector_state *connector_state;
8703 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008704 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008705 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008706 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008707
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008708 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008709 if (connector_state->crtc != crtc_state->base.crtc)
8710 continue;
8711
8712 encoder = to_intel_encoder(connector_state->best_encoder);
8713
8714 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008715 case INTEL_OUTPUT_LVDS:
8716 is_lvds = true;
8717 break;
8718 case INTEL_OUTPUT_SDVO:
8719 case INTEL_OUTPUT_HDMI:
8720 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008721 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008722 default:
8723 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008724 }
8725
8726 num_connectors++;
8727 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008728
Chris Wilsonc1858122010-12-03 21:35:48 +00008729 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008730 factor = 21;
8731 if (is_lvds) {
8732 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008733 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008734 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008735 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008736 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008737 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008738
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008739 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008740 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008741
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008742 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8743 *fp2 |= FP_CB_TUNE;
8744
Chris Wilson5eddb702010-09-11 13:48:45 +01008745 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008746
Eric Anholta07d6782011-03-30 13:01:08 -07008747 if (is_lvds)
8748 dpll |= DPLLB_MODE_LVDS;
8749 else
8750 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008751
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008752 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008753 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008754
8755 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008756 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008757 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008758 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008759
Eric Anholta07d6782011-03-30 13:01:08 -07008760 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008761 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008762 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008763 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008764
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008765 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008766 case 5:
8767 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8768 break;
8769 case 7:
8770 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8771 break;
8772 case 10:
8773 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8774 break;
8775 case 14:
8776 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8777 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008778 }
8779
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008780 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008781 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008782 else
8783 dpll |= PLL_REF_INPUT_DREFCLK;
8784
Daniel Vetter959e16d2013-06-05 13:34:21 +02008785 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008786}
8787
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008788static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8789 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008790{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008791 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008792 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008793 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008794 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008795 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008796 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008797
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008798 memset(&crtc_state->dpll_hw_state, 0,
8799 sizeof(crtc_state->dpll_hw_state));
8800
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008801 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008802
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008803 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8804 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8805
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008806 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008807 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008808 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008809 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8810 return -EINVAL;
8811 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008812 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008813 if (!crtc_state->clock_set) {
8814 crtc_state->dpll.n = clock.n;
8815 crtc_state->dpll.m1 = clock.m1;
8816 crtc_state->dpll.m2 = clock.m2;
8817 crtc_state->dpll.p1 = clock.p1;
8818 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008819 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008820
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008821 /* 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 +02008822 if (crtc_state->has_pch_encoder) {
8823 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008824 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008825 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008826
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008827 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008828 &fp, &reduced_clock,
8829 has_reduced_clock ? &fp2 : NULL);
8830
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008831 crtc_state->dpll_hw_state.dpll = dpll;
8832 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008833 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008834 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008835 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008836 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008837
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008838 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008839 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008840 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008841 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008842 return -EINVAL;
8843 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008844 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008845
Rodrigo Viviab585de2015-03-24 12:40:09 -07008846 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008847 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008848 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008849 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008850
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008851 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008852}
8853
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008854static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8855 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008856{
8857 struct drm_device *dev = crtc->base.dev;
8858 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008859 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008860
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008861 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8862 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8863 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8864 & ~TU_SIZE_MASK;
8865 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8866 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8867 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8868}
8869
8870static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8871 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008872 struct intel_link_m_n *m_n,
8873 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008874{
8875 struct drm_device *dev = crtc->base.dev;
8876 struct drm_i915_private *dev_priv = dev->dev_private;
8877 enum pipe pipe = crtc->pipe;
8878
8879 if (INTEL_INFO(dev)->gen >= 5) {
8880 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8881 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8882 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8883 & ~TU_SIZE_MASK;
8884 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8885 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8886 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008887 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8888 * gen < 8) and if DRRS is supported (to make sure the
8889 * registers are not unnecessarily read).
8890 */
8891 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008892 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008893 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8894 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8895 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8896 & ~TU_SIZE_MASK;
8897 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8898 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8899 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8900 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008901 } else {
8902 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8903 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8904 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8905 & ~TU_SIZE_MASK;
8906 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8907 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8908 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8909 }
8910}
8911
8912void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008913 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008914{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008915 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008916 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8917 else
8918 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008919 &pipe_config->dp_m_n,
8920 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008921}
8922
Daniel Vetter72419202013-04-04 13:28:53 +02008923static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008924 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008925{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008926 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008927 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008928}
8929
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008930static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008931 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008932{
8933 struct drm_device *dev = crtc->base.dev;
8934 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008935 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8936 uint32_t ps_ctrl = 0;
8937 int id = -1;
8938 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008939
Chandra Kondurua1b22782015-04-07 15:28:45 -07008940 /* find scaler attached to this pipe */
8941 for (i = 0; i < crtc->num_scalers; i++) {
8942 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8943 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8944 id = i;
8945 pipe_config->pch_pfit.enabled = true;
8946 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8947 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8948 break;
8949 }
8950 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008951
Chandra Kondurua1b22782015-04-07 15:28:45 -07008952 scaler_state->scaler_id = id;
8953 if (id >= 0) {
8954 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8955 } else {
8956 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008957 }
8958}
8959
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008960static void
8961skylake_get_initial_plane_config(struct intel_crtc *crtc,
8962 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008963{
8964 struct drm_device *dev = crtc->base.dev;
8965 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008966 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008967 int pipe = crtc->pipe;
8968 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008969 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008970 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008971 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008972
Damien Lespiaud9806c92015-01-21 14:07:19 +00008973 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008974 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008975 DRM_DEBUG_KMS("failed to alloc fb\n");
8976 return;
8977 }
8978
Damien Lespiau1b842c82015-01-21 13:50:54 +00008979 fb = &intel_fb->base;
8980
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008981 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008982 if (!(val & PLANE_CTL_ENABLE))
8983 goto error;
8984
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008985 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8986 fourcc = skl_format_to_fourcc(pixel_format,
8987 val & PLANE_CTL_ORDER_RGBX,
8988 val & PLANE_CTL_ALPHA_MASK);
8989 fb->pixel_format = fourcc;
8990 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8991
Damien Lespiau40f46282015-02-27 11:15:21 +00008992 tiling = val & PLANE_CTL_TILED_MASK;
8993 switch (tiling) {
8994 case PLANE_CTL_TILED_LINEAR:
8995 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8996 break;
8997 case PLANE_CTL_TILED_X:
8998 plane_config->tiling = I915_TILING_X;
8999 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9000 break;
9001 case PLANE_CTL_TILED_Y:
9002 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9003 break;
9004 case PLANE_CTL_TILED_YF:
9005 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9006 break;
9007 default:
9008 MISSING_CASE(tiling);
9009 goto error;
9010 }
9011
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009012 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9013 plane_config->base = base;
9014
9015 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9016
9017 val = I915_READ(PLANE_SIZE(pipe, 0));
9018 fb->height = ((val >> 16) & 0xfff) + 1;
9019 fb->width = ((val >> 0) & 0x1fff) + 1;
9020
9021 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009022 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9023 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009024 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9025
9026 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009027 fb->pixel_format,
9028 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009029
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009030 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009031
9032 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9033 pipe_name(pipe), fb->width, fb->height,
9034 fb->bits_per_pixel, base, fb->pitches[0],
9035 plane_config->size);
9036
Damien Lespiau2d140302015-02-05 17:22:18 +00009037 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009038 return;
9039
9040error:
9041 kfree(fb);
9042}
9043
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009044static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009045 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009046{
9047 struct drm_device *dev = crtc->base.dev;
9048 struct drm_i915_private *dev_priv = dev->dev_private;
9049 uint32_t tmp;
9050
9051 tmp = I915_READ(PF_CTL(crtc->pipe));
9052
9053 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009054 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009055 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9056 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009057
9058 /* We currently do not free assignements of panel fitters on
9059 * ivb/hsw (since we don't use the higher upscaling modes which
9060 * differentiates them) so just WARN about this case for now. */
9061 if (IS_GEN7(dev)) {
9062 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9063 PF_PIPE_SEL_IVB(crtc->pipe));
9064 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009065 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009066}
9067
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009068static void
9069ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9070 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009071{
9072 struct drm_device *dev = crtc->base.dev;
9073 struct drm_i915_private *dev_priv = dev->dev_private;
9074 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009075 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009076 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009077 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009078 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009079 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009080
Damien Lespiau42a7b082015-02-05 19:35:13 +00009081 val = I915_READ(DSPCNTR(pipe));
9082 if (!(val & DISPLAY_PLANE_ENABLE))
9083 return;
9084
Damien Lespiaud9806c92015-01-21 14:07:19 +00009085 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009086 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009087 DRM_DEBUG_KMS("failed to alloc fb\n");
9088 return;
9089 }
9090
Damien Lespiau1b842c82015-01-21 13:50:54 +00009091 fb = &intel_fb->base;
9092
Daniel Vetter18c52472015-02-10 17:16:09 +00009093 if (INTEL_INFO(dev)->gen >= 4) {
9094 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009095 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009096 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9097 }
9098 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009099
9100 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009101 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009102 fb->pixel_format = fourcc;
9103 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009104
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009105 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009106 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009107 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009108 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009109 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009110 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009111 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009112 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009113 }
9114 plane_config->base = base;
9115
9116 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009117 fb->width = ((val >> 16) & 0xfff) + 1;
9118 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009119
9120 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009121 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009122
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009123 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009124 fb->pixel_format,
9125 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009126
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009127 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009128
Damien Lespiau2844a922015-01-20 12:51:48 +00009129 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9130 pipe_name(pipe), fb->width, fb->height,
9131 fb->bits_per_pixel, base, fb->pitches[0],
9132 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009133
Damien Lespiau2d140302015-02-05 17:22:18 +00009134 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009135}
9136
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009137static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009138 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009139{
9140 struct drm_device *dev = crtc->base.dev;
9141 struct drm_i915_private *dev_priv = dev->dev_private;
9142 uint32_t tmp;
9143
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009144 if (!intel_display_power_is_enabled(dev_priv,
9145 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009146 return false;
9147
Daniel Vettere143a212013-07-04 12:01:15 +02009148 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009149 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009150
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009151 tmp = I915_READ(PIPECONF(crtc->pipe));
9152 if (!(tmp & PIPECONF_ENABLE))
9153 return false;
9154
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009155 switch (tmp & PIPECONF_BPC_MASK) {
9156 case PIPECONF_6BPC:
9157 pipe_config->pipe_bpp = 18;
9158 break;
9159 case PIPECONF_8BPC:
9160 pipe_config->pipe_bpp = 24;
9161 break;
9162 case PIPECONF_10BPC:
9163 pipe_config->pipe_bpp = 30;
9164 break;
9165 case PIPECONF_12BPC:
9166 pipe_config->pipe_bpp = 36;
9167 break;
9168 default:
9169 break;
9170 }
9171
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009172 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9173 pipe_config->limited_color_range = true;
9174
Daniel Vetterab9412b2013-05-03 11:49:46 +02009175 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009176 struct intel_shared_dpll *pll;
9177
Daniel Vetter88adfff2013-03-28 10:42:01 +01009178 pipe_config->has_pch_encoder = true;
9179
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009180 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9181 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9182 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009183
9184 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009185
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009186 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009187 pipe_config->shared_dpll =
9188 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009189 } else {
9190 tmp = I915_READ(PCH_DPLL_SEL);
9191 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9192 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9193 else
9194 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9195 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009196
9197 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9198
9199 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9200 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009201
9202 tmp = pipe_config->dpll_hw_state.dpll;
9203 pipe_config->pixel_multiplier =
9204 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9205 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009206
9207 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009208 } else {
9209 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009210 }
9211
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009212 intel_get_pipe_timings(crtc, pipe_config);
9213
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009214 ironlake_get_pfit_config(crtc, pipe_config);
9215
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009216 return true;
9217}
9218
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009219static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9220{
9221 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009222 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009223
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009224 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009225 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009226 pipe_name(crtc->pipe));
9227
Rob Clarke2c719b2014-12-15 13:56:32 -05009228 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9229 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9230 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9231 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9232 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9233 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009234 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009235 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009236 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009237 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009238 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009239 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009240 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009241 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009242 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009243
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009244 /*
9245 * In theory we can still leave IRQs enabled, as long as only the HPD
9246 * interrupts remain enabled. We used to check for that, but since it's
9247 * gen-specific and since we only disable LCPLL after we fully disable
9248 * the interrupts, the check below should be enough.
9249 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009250 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009251}
9252
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009253static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9254{
9255 struct drm_device *dev = dev_priv->dev;
9256
9257 if (IS_HASWELL(dev))
9258 return I915_READ(D_COMP_HSW);
9259 else
9260 return I915_READ(D_COMP_BDW);
9261}
9262
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009263static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9264{
9265 struct drm_device *dev = dev_priv->dev;
9266
9267 if (IS_HASWELL(dev)) {
9268 mutex_lock(&dev_priv->rps.hw_lock);
9269 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9270 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009271 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009272 mutex_unlock(&dev_priv->rps.hw_lock);
9273 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009274 I915_WRITE(D_COMP_BDW, val);
9275 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009276 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009277}
9278
9279/*
9280 * This function implements pieces of two sequences from BSpec:
9281 * - Sequence for display software to disable LCPLL
9282 * - Sequence for display software to allow package C8+
9283 * The steps implemented here are just the steps that actually touch the LCPLL
9284 * register. Callers should take care of disabling all the display engine
9285 * functions, doing the mode unset, fixing interrupts, etc.
9286 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009287static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9288 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009289{
9290 uint32_t val;
9291
9292 assert_can_disable_lcpll(dev_priv);
9293
9294 val = I915_READ(LCPLL_CTL);
9295
9296 if (switch_to_fclk) {
9297 val |= LCPLL_CD_SOURCE_FCLK;
9298 I915_WRITE(LCPLL_CTL, val);
9299
9300 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9301 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9302 DRM_ERROR("Switching to FCLK failed\n");
9303
9304 val = I915_READ(LCPLL_CTL);
9305 }
9306
9307 val |= LCPLL_PLL_DISABLE;
9308 I915_WRITE(LCPLL_CTL, val);
9309 POSTING_READ(LCPLL_CTL);
9310
9311 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9312 DRM_ERROR("LCPLL still locked\n");
9313
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009314 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009315 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009316 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009317 ndelay(100);
9318
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009319 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9320 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009321 DRM_ERROR("D_COMP RCOMP still in progress\n");
9322
9323 if (allow_power_down) {
9324 val = I915_READ(LCPLL_CTL);
9325 val |= LCPLL_POWER_DOWN_ALLOW;
9326 I915_WRITE(LCPLL_CTL, val);
9327 POSTING_READ(LCPLL_CTL);
9328 }
9329}
9330
9331/*
9332 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9333 * source.
9334 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009335static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009336{
9337 uint32_t val;
9338
9339 val = I915_READ(LCPLL_CTL);
9340
9341 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9342 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9343 return;
9344
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009345 /*
9346 * Make sure we're not on PC8 state before disabling PC8, otherwise
9347 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009348 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009349 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009350
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009351 if (val & LCPLL_POWER_DOWN_ALLOW) {
9352 val &= ~LCPLL_POWER_DOWN_ALLOW;
9353 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009354 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009355 }
9356
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009357 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009358 val |= D_COMP_COMP_FORCE;
9359 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009360 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009361
9362 val = I915_READ(LCPLL_CTL);
9363 val &= ~LCPLL_PLL_DISABLE;
9364 I915_WRITE(LCPLL_CTL, val);
9365
9366 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9367 DRM_ERROR("LCPLL not locked yet\n");
9368
9369 if (val & LCPLL_CD_SOURCE_FCLK) {
9370 val = I915_READ(LCPLL_CTL);
9371 val &= ~LCPLL_CD_SOURCE_FCLK;
9372 I915_WRITE(LCPLL_CTL, val);
9373
9374 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9375 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9376 DRM_ERROR("Switching back to LCPLL failed\n");
9377 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009378
Mika Kuoppala59bad942015-01-16 11:34:40 +02009379 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009380 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009381}
9382
Paulo Zanoni765dab672014-03-07 20:08:18 -03009383/*
9384 * Package states C8 and deeper are really deep PC states that can only be
9385 * reached when all the devices on the system allow it, so even if the graphics
9386 * device allows PC8+, it doesn't mean the system will actually get to these
9387 * states. Our driver only allows PC8+ when going into runtime PM.
9388 *
9389 * The requirements for PC8+ are that all the outputs are disabled, the power
9390 * well is disabled and most interrupts are disabled, and these are also
9391 * requirements for runtime PM. When these conditions are met, we manually do
9392 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9393 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9394 * hang the machine.
9395 *
9396 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9397 * the state of some registers, so when we come back from PC8+ we need to
9398 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9399 * need to take care of the registers kept by RC6. Notice that this happens even
9400 * if we don't put the device in PCI D3 state (which is what currently happens
9401 * because of the runtime PM support).
9402 *
9403 * For more, read "Display Sequences for Package C8" on the hardware
9404 * documentation.
9405 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009406void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009407{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009408 struct drm_device *dev = dev_priv->dev;
9409 uint32_t val;
9410
Paulo Zanonic67a4702013-08-19 13:18:09 -03009411 DRM_DEBUG_KMS("Enabling package C8+\n");
9412
Ville Syrjäläc2699522015-08-27 23:55:59 +03009413 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009414 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9415 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9416 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9417 }
9418
9419 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009420 hsw_disable_lcpll(dev_priv, true, true);
9421}
9422
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009423void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009424{
9425 struct drm_device *dev = dev_priv->dev;
9426 uint32_t val;
9427
Paulo Zanonic67a4702013-08-19 13:18:09 -03009428 DRM_DEBUG_KMS("Disabling package C8+\n");
9429
9430 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009431 lpt_init_pch_refclk(dev);
9432
Ville Syrjäläc2699522015-08-27 23:55:59 +03009433 if (HAS_PCH_LPT_LP(dev)) {
Paulo Zanonic67a4702013-08-19 13:18:09 -03009434 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9435 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9436 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9437 }
9438
9439 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009440}
9441
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009442static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309443{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009444 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009445 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309446
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009447 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309448}
9449
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009450/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009451static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009452{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009453 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009454 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009455 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009456
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009457 for_each_intel_crtc(state->dev, intel_crtc) {
9458 int pixel_rate;
9459
9460 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9461 if (IS_ERR(crtc_state))
9462 return PTR_ERR(crtc_state);
9463
9464 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009465 continue;
9466
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009467 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009468
9469 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009470 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009471 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9472
9473 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9474 }
9475
9476 return max_pixel_rate;
9477}
9478
9479static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9480{
9481 struct drm_i915_private *dev_priv = dev->dev_private;
9482 uint32_t val, data;
9483 int ret;
9484
9485 if (WARN((I915_READ(LCPLL_CTL) &
9486 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9487 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9488 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9489 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9490 "trying to change cdclk frequency with cdclk not enabled\n"))
9491 return;
9492
9493 mutex_lock(&dev_priv->rps.hw_lock);
9494 ret = sandybridge_pcode_write(dev_priv,
9495 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9496 mutex_unlock(&dev_priv->rps.hw_lock);
9497 if (ret) {
9498 DRM_ERROR("failed to inform pcode about cdclk change\n");
9499 return;
9500 }
9501
9502 val = I915_READ(LCPLL_CTL);
9503 val |= LCPLL_CD_SOURCE_FCLK;
9504 I915_WRITE(LCPLL_CTL, val);
9505
9506 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9507 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9508 DRM_ERROR("Switching to FCLK failed\n");
9509
9510 val = I915_READ(LCPLL_CTL);
9511 val &= ~LCPLL_CLK_FREQ_MASK;
9512
9513 switch (cdclk) {
9514 case 450000:
9515 val |= LCPLL_CLK_FREQ_450;
9516 data = 0;
9517 break;
9518 case 540000:
9519 val |= LCPLL_CLK_FREQ_54O_BDW;
9520 data = 1;
9521 break;
9522 case 337500:
9523 val |= LCPLL_CLK_FREQ_337_5_BDW;
9524 data = 2;
9525 break;
9526 case 675000:
9527 val |= LCPLL_CLK_FREQ_675_BDW;
9528 data = 3;
9529 break;
9530 default:
9531 WARN(1, "invalid cdclk frequency\n");
9532 return;
9533 }
9534
9535 I915_WRITE(LCPLL_CTL, val);
9536
9537 val = I915_READ(LCPLL_CTL);
9538 val &= ~LCPLL_CD_SOURCE_FCLK;
9539 I915_WRITE(LCPLL_CTL, val);
9540
9541 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9542 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9543 DRM_ERROR("Switching back to LCPLL failed\n");
9544
9545 mutex_lock(&dev_priv->rps.hw_lock);
9546 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9547 mutex_unlock(&dev_priv->rps.hw_lock);
9548
9549 intel_update_cdclk(dev);
9550
9551 WARN(cdclk != dev_priv->cdclk_freq,
9552 "cdclk requested %d kHz but got %d kHz\n",
9553 cdclk, dev_priv->cdclk_freq);
9554}
9555
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009556static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009557{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009558 struct drm_i915_private *dev_priv = to_i915(state->dev);
9559 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009560 int cdclk;
9561
9562 /*
9563 * FIXME should also account for plane ratio
9564 * once 64bpp pixel formats are supported.
9565 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009566 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009567 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009568 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009569 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009570 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009571 cdclk = 450000;
9572 else
9573 cdclk = 337500;
9574
9575 /*
9576 * FIXME move the cdclk caclulation to
9577 * compute_config() so we can fail gracegully.
9578 */
9579 if (cdclk > dev_priv->max_cdclk_freq) {
9580 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9581 cdclk, dev_priv->max_cdclk_freq);
9582 cdclk = dev_priv->max_cdclk_freq;
9583 }
9584
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009585 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009586
9587 return 0;
9588}
9589
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009590static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009591{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009592 struct drm_device *dev = old_state->dev;
9593 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009594
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009595 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009596}
9597
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009598static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9599 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009600{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009601 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009602 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009603
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009604 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009605
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009606 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009607}
9608
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309609static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9610 enum port port,
9611 struct intel_crtc_state *pipe_config)
9612{
9613 switch (port) {
9614 case PORT_A:
9615 pipe_config->ddi_pll_sel = SKL_DPLL0;
9616 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9617 break;
9618 case PORT_B:
9619 pipe_config->ddi_pll_sel = SKL_DPLL1;
9620 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9621 break;
9622 case PORT_C:
9623 pipe_config->ddi_pll_sel = SKL_DPLL2;
9624 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9625 break;
9626 default:
9627 DRM_ERROR("Incorrect port type\n");
9628 }
9629}
9630
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009631static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9632 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009633 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009634{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009635 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009636
9637 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9638 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9639
9640 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009641 case SKL_DPLL0:
9642 /*
9643 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9644 * of the shared DPLL framework and thus needs to be read out
9645 * separately
9646 */
9647 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9648 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9649 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009650 case SKL_DPLL1:
9651 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9652 break;
9653 case SKL_DPLL2:
9654 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9655 break;
9656 case SKL_DPLL3:
9657 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9658 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009659 }
9660}
9661
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009662static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9663 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009664 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009665{
9666 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9667
9668 switch (pipe_config->ddi_pll_sel) {
9669 case PORT_CLK_SEL_WRPLL1:
9670 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9671 break;
9672 case PORT_CLK_SEL_WRPLL2:
9673 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9674 break;
9675 }
9676}
9677
Daniel Vetter26804af2014-06-25 22:01:55 +03009678static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009679 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009680{
9681 struct drm_device *dev = crtc->base.dev;
9682 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009683 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009684 enum port port;
9685 uint32_t tmp;
9686
9687 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9688
9689 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9690
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009691 if (IS_SKYLAKE(dev))
9692 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309693 else if (IS_BROXTON(dev))
9694 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009695 else
9696 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009697
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009698 if (pipe_config->shared_dpll >= 0) {
9699 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9700
9701 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9702 &pipe_config->dpll_hw_state));
9703 }
9704
Daniel Vetter26804af2014-06-25 22:01:55 +03009705 /*
9706 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9707 * DDI E. So just check whether this pipe is wired to DDI E and whether
9708 * the PCH transcoder is on.
9709 */
Damien Lespiauca370452013-12-03 13:56:24 +00009710 if (INTEL_INFO(dev)->gen < 9 &&
9711 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009712 pipe_config->has_pch_encoder = true;
9713
9714 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9715 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9716 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9717
9718 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9719 }
9720}
9721
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009722static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009723 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009724{
9725 struct drm_device *dev = crtc->base.dev;
9726 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009727 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009728 uint32_t tmp;
9729
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009730 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009731 POWER_DOMAIN_PIPE(crtc->pipe)))
9732 return false;
9733
Daniel Vettere143a212013-07-04 12:01:15 +02009734 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009735 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9736
Daniel Vettereccb1402013-05-22 00:50:22 +02009737 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9738 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9739 enum pipe trans_edp_pipe;
9740 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9741 default:
9742 WARN(1, "unknown pipe linked to edp transcoder\n");
9743 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9744 case TRANS_DDI_EDP_INPUT_A_ON:
9745 trans_edp_pipe = PIPE_A;
9746 break;
9747 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9748 trans_edp_pipe = PIPE_B;
9749 break;
9750 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9751 trans_edp_pipe = PIPE_C;
9752 break;
9753 }
9754
9755 if (trans_edp_pipe == crtc->pipe)
9756 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9757 }
9758
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009759 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009760 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009761 return false;
9762
Daniel Vettereccb1402013-05-22 00:50:22 +02009763 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009764 if (!(tmp & PIPECONF_ENABLE))
9765 return false;
9766
Daniel Vetter26804af2014-06-25 22:01:55 +03009767 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009768
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009769 intel_get_pipe_timings(crtc, pipe_config);
9770
Chandra Kondurua1b22782015-04-07 15:28:45 -07009771 if (INTEL_INFO(dev)->gen >= 9) {
9772 skl_init_scalers(dev, crtc, pipe_config);
9773 }
9774
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009775 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009776
9777 if (INTEL_INFO(dev)->gen >= 9) {
9778 pipe_config->scaler_state.scaler_id = -1;
9779 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9780 }
9781
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009782 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009783 if (INTEL_INFO(dev)->gen >= 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009784 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009785 else
Rodrigo Vivi1c132b42015-09-02 15:19:26 -07009786 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009787 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009788
Jesse Barnese59150d2014-01-07 13:30:45 -08009789 if (IS_HASWELL(dev))
9790 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9791 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009792
Clint Taylorebb69c92014-09-30 10:30:22 -07009793 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9794 pipe_config->pixel_multiplier =
9795 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9796 } else {
9797 pipe_config->pixel_multiplier = 1;
9798 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009799
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009800 return true;
9801}
9802
Chris Wilson560b85b2010-08-07 11:01:38 +01009803static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9804{
9805 struct drm_device *dev = crtc->dev;
9806 struct drm_i915_private *dev_priv = dev->dev_private;
9807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009808 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009809
Ville Syrjälädc41c152014-08-13 11:57:05 +03009810 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009811 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9812 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009813 unsigned int stride = roundup_pow_of_two(width) * 4;
9814
9815 switch (stride) {
9816 default:
9817 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9818 width, stride);
9819 stride = 256;
9820 /* fallthrough */
9821 case 256:
9822 case 512:
9823 case 1024:
9824 case 2048:
9825 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009826 }
9827
Ville Syrjälädc41c152014-08-13 11:57:05 +03009828 cntl |= CURSOR_ENABLE |
9829 CURSOR_GAMMA_ENABLE |
9830 CURSOR_FORMAT_ARGB |
9831 CURSOR_STRIDE(stride);
9832
9833 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009834 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009835
Ville Syrjälädc41c152014-08-13 11:57:05 +03009836 if (intel_crtc->cursor_cntl != 0 &&
9837 (intel_crtc->cursor_base != base ||
9838 intel_crtc->cursor_size != size ||
9839 intel_crtc->cursor_cntl != cntl)) {
9840 /* On these chipsets we can only modify the base/size/stride
9841 * whilst the cursor is disabled.
9842 */
9843 I915_WRITE(_CURACNTR, 0);
9844 POSTING_READ(_CURACNTR);
9845 intel_crtc->cursor_cntl = 0;
9846 }
9847
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009848 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009849 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009850 intel_crtc->cursor_base = base;
9851 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009852
9853 if (intel_crtc->cursor_size != size) {
9854 I915_WRITE(CURSIZE, size);
9855 intel_crtc->cursor_size = size;
9856 }
9857
Chris Wilson4b0e3332014-05-30 16:35:26 +03009858 if (intel_crtc->cursor_cntl != cntl) {
9859 I915_WRITE(_CURACNTR, cntl);
9860 POSTING_READ(_CURACNTR);
9861 intel_crtc->cursor_cntl = cntl;
9862 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009863}
9864
9865static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9866{
9867 struct drm_device *dev = crtc->dev;
9868 struct drm_i915_private *dev_priv = dev->dev_private;
9869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9870 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009871 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009872
Chris Wilson4b0e3332014-05-30 16:35:26 +03009873 cntl = 0;
9874 if (base) {
9875 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009876 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309877 case 64:
9878 cntl |= CURSOR_MODE_64_ARGB_AX;
9879 break;
9880 case 128:
9881 cntl |= CURSOR_MODE_128_ARGB_AX;
9882 break;
9883 case 256:
9884 cntl |= CURSOR_MODE_256_ARGB_AX;
9885 break;
9886 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009887 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309888 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009889 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009890 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009891
9892 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9893 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009894 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009895
Matt Roper8e7d6882015-01-21 16:35:41 -08009896 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009897 cntl |= CURSOR_ROTATE_180;
9898
Chris Wilson4b0e3332014-05-30 16:35:26 +03009899 if (intel_crtc->cursor_cntl != cntl) {
9900 I915_WRITE(CURCNTR(pipe), cntl);
9901 POSTING_READ(CURCNTR(pipe));
9902 intel_crtc->cursor_cntl = cntl;
9903 }
9904
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009905 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009906 I915_WRITE(CURBASE(pipe), base);
9907 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009908
9909 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009910}
9911
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009912/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009913static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9914 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009915{
9916 struct drm_device *dev = crtc->dev;
9917 struct drm_i915_private *dev_priv = dev->dev_private;
9918 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9919 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009920 int x = crtc->cursor_x;
9921 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009922 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009923
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009924 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009925 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009926
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009927 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009928 base = 0;
9929
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009930 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009931 base = 0;
9932
9933 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009934 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009935 base = 0;
9936
9937 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9938 x = -x;
9939 }
9940 pos |= x << CURSOR_X_SHIFT;
9941
9942 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009943 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009944 base = 0;
9945
9946 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9947 y = -y;
9948 }
9949 pos |= y << CURSOR_Y_SHIFT;
9950
Chris Wilson4b0e3332014-05-30 16:35:26 +03009951 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009952 return;
9953
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009954 I915_WRITE(CURPOS(pipe), pos);
9955
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009956 /* ILK+ do this automagically */
9957 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009958 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009959 base += (intel_crtc->base.cursor->state->crtc_h *
9960 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009961 }
9962
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009963 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009964 i845_update_cursor(crtc, base);
9965 else
9966 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009967}
9968
Ville Syrjälädc41c152014-08-13 11:57:05 +03009969static bool cursor_size_ok(struct drm_device *dev,
9970 uint32_t width, uint32_t height)
9971{
9972 if (width == 0 || height == 0)
9973 return false;
9974
9975 /*
9976 * 845g/865g are special in that they are only limited by
9977 * the width of their cursors, the height is arbitrary up to
9978 * the precision of the register. Everything else requires
9979 * square cursors, limited to a few power-of-two sizes.
9980 */
9981 if (IS_845G(dev) || IS_I865G(dev)) {
9982 if ((width & 63) != 0)
9983 return false;
9984
9985 if (width > (IS_845G(dev) ? 64 : 512))
9986 return false;
9987
9988 if (height > 1023)
9989 return false;
9990 } else {
9991 switch (width | height) {
9992 case 256:
9993 case 128:
9994 if (IS_GEN2(dev))
9995 return false;
9996 case 64:
9997 break;
9998 default:
9999 return false;
10000 }
10001 }
10002
10003 return true;
10004}
10005
Jesse Barnes79e53942008-11-07 14:24:08 -080010006static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010007 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010008{
James Simmons72034252010-08-03 01:33:19 +010010009 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010011
James Simmons72034252010-08-03 01:33:19 +010010012 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010013 intel_crtc->lut_r[i] = red[i] >> 8;
10014 intel_crtc->lut_g[i] = green[i] >> 8;
10015 intel_crtc->lut_b[i] = blue[i] >> 8;
10016 }
10017
10018 intel_crtc_load_lut(crtc);
10019}
10020
Jesse Barnes79e53942008-11-07 14:24:08 -080010021/* VESA 640x480x72Hz mode to set on the pipe */
10022static struct drm_display_mode load_detect_mode = {
10023 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10024 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10025};
10026
Daniel Vettera8bb6812014-02-10 18:00:39 +010010027struct drm_framebuffer *
10028__intel_framebuffer_create(struct drm_device *dev,
10029 struct drm_mode_fb_cmd2 *mode_cmd,
10030 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010031{
10032 struct intel_framebuffer *intel_fb;
10033 int ret;
10034
10035 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10036 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010037 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010038 return ERR_PTR(-ENOMEM);
10039 }
10040
10041 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010042 if (ret)
10043 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010044
10045 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010046err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010047 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010048 kfree(intel_fb);
10049
10050 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010051}
10052
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010053static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010054intel_framebuffer_create(struct drm_device *dev,
10055 struct drm_mode_fb_cmd2 *mode_cmd,
10056 struct drm_i915_gem_object *obj)
10057{
10058 struct drm_framebuffer *fb;
10059 int ret;
10060
10061 ret = i915_mutex_lock_interruptible(dev);
10062 if (ret)
10063 return ERR_PTR(ret);
10064 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10065 mutex_unlock(&dev->struct_mutex);
10066
10067 return fb;
10068}
10069
Chris Wilsond2dff872011-04-19 08:36:26 +010010070static u32
10071intel_framebuffer_pitch_for_width(int width, int bpp)
10072{
10073 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10074 return ALIGN(pitch, 64);
10075}
10076
10077static u32
10078intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10079{
10080 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010081 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010082}
10083
10084static struct drm_framebuffer *
10085intel_framebuffer_create_for_mode(struct drm_device *dev,
10086 struct drm_display_mode *mode,
10087 int depth, int bpp)
10088{
10089 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010090 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010091
10092 obj = i915_gem_alloc_object(dev,
10093 intel_framebuffer_size_for_mode(mode, bpp));
10094 if (obj == NULL)
10095 return ERR_PTR(-ENOMEM);
10096
10097 mode_cmd.width = mode->hdisplay;
10098 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010099 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10100 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010101 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010102
10103 return intel_framebuffer_create(dev, &mode_cmd, obj);
10104}
10105
10106static struct drm_framebuffer *
10107mode_fits_in_fbdev(struct drm_device *dev,
10108 struct drm_display_mode *mode)
10109{
Daniel Vetter06957262015-08-10 13:34:08 +020010110#ifdef CONFIG_DRM_FBDEV_EMULATION
Chris Wilsond2dff872011-04-19 08:36:26 +010010111 struct drm_i915_private *dev_priv = dev->dev_private;
10112 struct drm_i915_gem_object *obj;
10113 struct drm_framebuffer *fb;
10114
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010115 if (!dev_priv->fbdev)
10116 return NULL;
10117
10118 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010119 return NULL;
10120
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010121 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010122 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010123
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010124 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010125 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10126 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010127 return NULL;
10128
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010129 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010130 return NULL;
10131
10132 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010133#else
10134 return NULL;
10135#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010136}
10137
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010138static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10139 struct drm_crtc *crtc,
10140 struct drm_display_mode *mode,
10141 struct drm_framebuffer *fb,
10142 int x, int y)
10143{
10144 struct drm_plane_state *plane_state;
10145 int hdisplay, vdisplay;
10146 int ret;
10147
10148 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10149 if (IS_ERR(plane_state))
10150 return PTR_ERR(plane_state);
10151
10152 if (mode)
10153 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10154 else
10155 hdisplay = vdisplay = 0;
10156
10157 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10158 if (ret)
10159 return ret;
10160 drm_atomic_set_fb_for_plane(plane_state, fb);
10161 plane_state->crtc_x = 0;
10162 plane_state->crtc_y = 0;
10163 plane_state->crtc_w = hdisplay;
10164 plane_state->crtc_h = vdisplay;
10165 plane_state->src_x = x << 16;
10166 plane_state->src_y = y << 16;
10167 plane_state->src_w = hdisplay << 16;
10168 plane_state->src_h = vdisplay << 16;
10169
10170 return 0;
10171}
10172
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010173bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010174 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010175 struct intel_load_detect_pipe *old,
10176 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010177{
10178 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010179 struct intel_encoder *intel_encoder =
10180 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010181 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010182 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010183 struct drm_crtc *crtc = NULL;
10184 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010185 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010186 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010187 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010188 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010189 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010190 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010191
Chris Wilsond2dff872011-04-19 08:36:26 +010010192 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010193 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010194 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010195
Rob Clark51fd3712013-11-19 12:10:12 -050010196retry:
10197 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10198 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010199 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010200
Jesse Barnes79e53942008-11-07 14:24:08 -080010201 /*
10202 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010203 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010204 * - if the connector already has an assigned crtc, use it (but make
10205 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010206 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010207 * - try to find the first unused crtc that can drive this connector,
10208 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010209 */
10210
10211 /* See if we already have a CRTC for this connector */
10212 if (encoder->crtc) {
10213 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010214
Rob Clark51fd3712013-11-19 12:10:12 -050010215 ret = drm_modeset_lock(&crtc->mutex, ctx);
10216 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010217 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010218 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10219 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010220 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010221
Daniel Vetter24218aa2012-08-12 19:27:11 +020010222 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010223 old->load_detect_temp = false;
10224
10225 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010226 if (connector->dpms != DRM_MODE_DPMS_ON)
10227 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010228
Chris Wilson71731882011-04-19 23:10:58 +010010229 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010230 }
10231
10232 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010233 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010234 i++;
10235 if (!(encoder->possible_crtcs & (1 << i)))
10236 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010237 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010238 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010239
10240 crtc = possible_crtc;
10241 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010242 }
10243
10244 /*
10245 * If we didn't find an unused CRTC, don't use any.
10246 */
10247 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010248 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010249 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010250 }
10251
Rob Clark51fd3712013-11-19 12:10:12 -050010252 ret = drm_modeset_lock(&crtc->mutex, ctx);
10253 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010254 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010255 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10256 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010257 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010258
10259 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010260 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010261 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010262 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010263
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010264 state = drm_atomic_state_alloc(dev);
10265 if (!state)
10266 return false;
10267
10268 state->acquire_ctx = ctx;
10269
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010270 connector_state = drm_atomic_get_connector_state(state, connector);
10271 if (IS_ERR(connector_state)) {
10272 ret = PTR_ERR(connector_state);
10273 goto fail;
10274 }
10275
10276 connector_state->crtc = crtc;
10277 connector_state->best_encoder = &intel_encoder->base;
10278
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010279 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10280 if (IS_ERR(crtc_state)) {
10281 ret = PTR_ERR(crtc_state);
10282 goto fail;
10283 }
10284
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010285 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010286
Chris Wilson64927112011-04-20 07:25:26 +010010287 if (!mode)
10288 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010289
Chris Wilsond2dff872011-04-19 08:36:26 +010010290 /* We need a framebuffer large enough to accommodate all accesses
10291 * that the plane may generate whilst we perform load detection.
10292 * We can not rely on the fbcon either being present (we get called
10293 * during its initialisation to detect all boot displays, or it may
10294 * not even exist) or that it is large enough to satisfy the
10295 * requested mode.
10296 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010297 fb = mode_fits_in_fbdev(dev, mode);
10298 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010299 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010300 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10301 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010302 } else
10303 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010304 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010305 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010306 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010307 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010308
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010309 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10310 if (ret)
10311 goto fail;
10312
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010313 drm_mode_copy(&crtc_state->base.mode, mode);
10314
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010315 if (drm_atomic_commit(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010316 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010317 if (old->release_fb)
10318 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010319 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010320 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010321 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010322
Jesse Barnes79e53942008-11-07 14:24:08 -080010323 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010324 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010325 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010326
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010327fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010328 drm_atomic_state_free(state);
10329 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010330
Rob Clark51fd3712013-11-19 12:10:12 -050010331 if (ret == -EDEADLK) {
10332 drm_modeset_backoff(ctx);
10333 goto retry;
10334 }
10335
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010336 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010337}
10338
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010339void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010340 struct intel_load_detect_pipe *old,
10341 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010342{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010343 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010344 struct intel_encoder *intel_encoder =
10345 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010346 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010347 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010348 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010349 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010350 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010351 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010352 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010353
Chris Wilsond2dff872011-04-19 08:36:26 +010010354 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010355 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010356 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010357
Chris Wilson8261b192011-04-19 23:18:09 +010010358 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010359 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010360 if (!state)
10361 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010362
10363 state->acquire_ctx = ctx;
10364
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010365 connector_state = drm_atomic_get_connector_state(state, connector);
10366 if (IS_ERR(connector_state))
10367 goto fail;
10368
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010369 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10370 if (IS_ERR(crtc_state))
10371 goto fail;
10372
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010373 connector_state->best_encoder = NULL;
10374 connector_state->crtc = NULL;
10375
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010376 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010377
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010378 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10379 0, 0);
10380 if (ret)
10381 goto fail;
10382
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020010383 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010384 if (ret)
10385 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010386
Daniel Vetter36206362012-12-10 20:42:17 +010010387 if (old->release_fb) {
10388 drm_framebuffer_unregister_private(old->release_fb);
10389 drm_framebuffer_unreference(old->release_fb);
10390 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010391
Chris Wilson0622a532011-04-21 09:32:11 +010010392 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010393 }
10394
Eric Anholtc751ce42010-03-25 11:48:48 -070010395 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010396 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10397 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010398
10399 return;
10400fail:
10401 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10402 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010403}
10404
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010405static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010406 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010407{
10408 struct drm_i915_private *dev_priv = dev->dev_private;
10409 u32 dpll = pipe_config->dpll_hw_state.dpll;
10410
10411 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010412 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010413 else if (HAS_PCH_SPLIT(dev))
10414 return 120000;
10415 else if (!IS_GEN2(dev))
10416 return 96000;
10417 else
10418 return 48000;
10419}
10420
Jesse Barnes79e53942008-11-07 14:24:08 -080010421/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010422static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010423 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010424{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010425 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010426 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010427 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010428 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010429 u32 fp;
10430 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010431 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010432 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010433
10434 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010435 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010436 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010437 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010438
10439 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010440 if (IS_PINEVIEW(dev)) {
10441 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10442 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010443 } else {
10444 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10445 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10446 }
10447
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010448 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010449 if (IS_PINEVIEW(dev))
10450 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10451 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010452 else
10453 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010454 DPLL_FPA01_P1_POST_DIV_SHIFT);
10455
10456 switch (dpll & DPLL_MODE_MASK) {
10457 case DPLLB_MODE_DAC_SERIAL:
10458 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10459 5 : 10;
10460 break;
10461 case DPLLB_MODE_LVDS:
10462 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10463 7 : 14;
10464 break;
10465 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010466 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010467 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010468 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010469 }
10470
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010471 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010472 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010473 else
Imre Deakdccbea32015-06-22 23:35:51 +030010474 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010475 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010476 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010477 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010478
10479 if (is_lvds) {
10480 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10481 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010482
10483 if (lvds & LVDS_CLKB_POWER_UP)
10484 clock.p2 = 7;
10485 else
10486 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010487 } else {
10488 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10489 clock.p1 = 2;
10490 else {
10491 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10492 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10493 }
10494 if (dpll & PLL_P2_DIVIDE_BY_4)
10495 clock.p2 = 4;
10496 else
10497 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010498 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010499
Imre Deakdccbea32015-06-22 23:35:51 +030010500 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010501 }
10502
Ville Syrjälä18442d02013-09-13 16:00:08 +030010503 /*
10504 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010505 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010506 * encoder's get_config() function.
10507 */
Imre Deakdccbea32015-06-22 23:35:51 +030010508 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010509}
10510
Ville Syrjälä6878da02013-09-13 15:59:11 +030010511int intel_dotclock_calculate(int link_freq,
10512 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010513{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010514 /*
10515 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010516 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010517 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010518 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010519 *
10520 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010521 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010522 */
10523
Ville Syrjälä6878da02013-09-13 15:59:11 +030010524 if (!m_n->link_n)
10525 return 0;
10526
10527 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10528}
10529
Ville Syrjälä18442d02013-09-13 16:00:08 +030010530static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010531 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010532{
10533 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010534
10535 /* read out port_clock from the DPLL */
10536 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010537
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010538 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010539 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010540 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010541 * agree once we know their relationship in the encoder's
10542 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010543 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010544 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010545 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10546 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010547}
10548
10549/** Returns the currently programmed mode of the given pipe. */
10550struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10551 struct drm_crtc *crtc)
10552{
Jesse Barnes548f2452011-02-17 10:40:53 -080010553 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010554 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010555 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010556 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010557 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010558 int htot = I915_READ(HTOTAL(cpu_transcoder));
10559 int hsync = I915_READ(HSYNC(cpu_transcoder));
10560 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10561 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010562 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010563
10564 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10565 if (!mode)
10566 return NULL;
10567
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010568 /*
10569 * Construct a pipe_config sufficient for getting the clock info
10570 * back out of crtc_clock_get.
10571 *
10572 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10573 * to use a real value here instead.
10574 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010575 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010576 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010577 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10578 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10579 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010580 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10581
Ville Syrjälä773ae032013-09-23 17:48:20 +030010582 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010583 mode->hdisplay = (htot & 0xffff) + 1;
10584 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10585 mode->hsync_start = (hsync & 0xffff) + 1;
10586 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10587 mode->vdisplay = (vtot & 0xffff) + 1;
10588 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10589 mode->vsync_start = (vsync & 0xffff) + 1;
10590 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10591
10592 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010593
10594 return mode;
10595}
10596
Chris Wilsonf047e392012-07-21 12:31:41 +010010597void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010598{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010599 struct drm_i915_private *dev_priv = dev->dev_private;
10600
Chris Wilsonf62a0072014-02-21 17:55:39 +000010601 if (dev_priv->mm.busy)
10602 return;
10603
Paulo Zanoni43694d62014-03-07 20:08:08 -030010604 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010605 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010606 if (INTEL_INFO(dev)->gen >= 6)
10607 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010608 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010609}
10610
10611void intel_mark_idle(struct drm_device *dev)
10612{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010613 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010614
Chris Wilsonf62a0072014-02-21 17:55:39 +000010615 if (!dev_priv->mm.busy)
10616 return;
10617
10618 dev_priv->mm.busy = false;
10619
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010620 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010621 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010622
Paulo Zanoni43694d62014-03-07 20:08:08 -030010623 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010624}
10625
Jesse Barnes79e53942008-11-07 14:24:08 -080010626static void intel_crtc_destroy(struct drm_crtc *crtc)
10627{
10628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010629 struct drm_device *dev = crtc->dev;
10630 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010631
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010632 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010633 work = intel_crtc->unpin_work;
10634 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010635 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010636
10637 if (work) {
10638 cancel_work_sync(&work->work);
10639 kfree(work);
10640 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010641
10642 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010643
Jesse Barnes79e53942008-11-07 14:24:08 -080010644 kfree(intel_crtc);
10645}
10646
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010647static void intel_unpin_work_fn(struct work_struct *__work)
10648{
10649 struct intel_unpin_work *work =
10650 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010651 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10652 struct drm_device *dev = crtc->base.dev;
10653 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010654
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010655 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010656 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010657 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010658
John Harrisonf06cc1b2014-11-24 18:49:37 +000010659 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010660 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010661 mutex_unlock(&dev->struct_mutex);
10662
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010663 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010664 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010665
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010666 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10667 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010668
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010669 kfree(work);
10670}
10671
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010672static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010673 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010674{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010675 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10676 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010677 unsigned long flags;
10678
10679 /* Ignore early vblank irqs */
10680 if (intel_crtc == NULL)
10681 return;
10682
Daniel Vetterf3260382014-09-15 14:55:23 +020010683 /*
10684 * This is called both by irq handlers and the reset code (to complete
10685 * lost pageflips) so needs the full irqsave spinlocks.
10686 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010687 spin_lock_irqsave(&dev->event_lock, flags);
10688 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010689
10690 /* Ensure we don't miss a work->pending update ... */
10691 smp_rmb();
10692
10693 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010694 spin_unlock_irqrestore(&dev->event_lock, flags);
10695 return;
10696 }
10697
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010698 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010699
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010700 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010701}
10702
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010703void intel_finish_page_flip(struct drm_device *dev, int pipe)
10704{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010705 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010706 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10707
Mario Kleiner49b14a52010-12-09 07:00:07 +010010708 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010709}
10710
10711void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10712{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010713 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010714 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10715
Mario Kleiner49b14a52010-12-09 07:00:07 +010010716 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010717}
10718
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010719/* Is 'a' after or equal to 'b'? */
10720static bool g4x_flip_count_after_eq(u32 a, u32 b)
10721{
10722 return !((a - b) & 0x80000000);
10723}
10724
10725static bool page_flip_finished(struct intel_crtc *crtc)
10726{
10727 struct drm_device *dev = crtc->base.dev;
10728 struct drm_i915_private *dev_priv = dev->dev_private;
10729
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010730 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10731 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10732 return true;
10733
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010734 /*
10735 * The relevant registers doen't exist on pre-ctg.
10736 * As the flip done interrupt doesn't trigger for mmio
10737 * flips on gmch platforms, a flip count check isn't
10738 * really needed there. But since ctg has the registers,
10739 * include it in the check anyway.
10740 */
10741 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10742 return true;
10743
10744 /*
10745 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10746 * used the same base address. In that case the mmio flip might
10747 * have completed, but the CS hasn't even executed the flip yet.
10748 *
10749 * A flip count check isn't enough as the CS might have updated
10750 * the base address just after start of vblank, but before we
10751 * managed to process the interrupt. This means we'd complete the
10752 * CS flip too soon.
10753 *
10754 * Combining both checks should get us a good enough result. It may
10755 * still happen that the CS flip has been executed, but has not
10756 * yet actually completed. But in case the base address is the same
10757 * anyway, we don't really care.
10758 */
10759 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10760 crtc->unpin_work->gtt_offset &&
10761 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10762 crtc->unpin_work->flip_count);
10763}
10764
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010765void intel_prepare_page_flip(struct drm_device *dev, int plane)
10766{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010767 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010768 struct intel_crtc *intel_crtc =
10769 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10770 unsigned long flags;
10771
Daniel Vetterf3260382014-09-15 14:55:23 +020010772
10773 /*
10774 * This is called both by irq handlers and the reset code (to complete
10775 * lost pageflips) so needs the full irqsave spinlocks.
10776 *
10777 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010778 * generate a page-flip completion irq, i.e. every modeset
10779 * is also accompanied by a spurious intel_prepare_page_flip().
10780 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010781 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010782 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010783 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010784 spin_unlock_irqrestore(&dev->event_lock, flags);
10785}
10786
Robin Schroereba905b2014-05-18 02:24:50 +020010787static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010788{
10789 /* Ensure that the work item is consistent when activating it ... */
10790 smp_wmb();
10791 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10792 /* and that it is marked active as soon as the irq could fire. */
10793 smp_wmb();
10794}
10795
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010796static int intel_gen2_queue_flip(struct drm_device *dev,
10797 struct drm_crtc *crtc,
10798 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010799 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010800 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010801 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010802{
John Harrison6258fbe2015-05-29 17:43:48 +010010803 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010805 u32 flip_mask;
10806 int ret;
10807
John Harrison5fb9de12015-05-29 17:44:07 +010010808 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010809 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010810 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010811
10812 /* Can't queue multiple flips, so wait for the previous
10813 * one to finish before executing the next.
10814 */
10815 if (intel_crtc->plane)
10816 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10817 else
10818 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010819 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10820 intel_ring_emit(ring, MI_NOOP);
10821 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10822 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10823 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010824 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010825 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010826
10827 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010828 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010829}
10830
10831static int intel_gen3_queue_flip(struct drm_device *dev,
10832 struct drm_crtc *crtc,
10833 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010834 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010835 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010836 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010837{
John Harrison6258fbe2015-05-29 17:43:48 +010010838 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010840 u32 flip_mask;
10841 int ret;
10842
John Harrison5fb9de12015-05-29 17:44:07 +010010843 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010844 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010845 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010846
10847 if (intel_crtc->plane)
10848 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10849 else
10850 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010851 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10852 intel_ring_emit(ring, MI_NOOP);
10853 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10854 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10855 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010856 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010857 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010858
Chris Wilsone7d841c2012-12-03 11:36:30 +000010859 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010860 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010861}
10862
10863static int intel_gen4_queue_flip(struct drm_device *dev,
10864 struct drm_crtc *crtc,
10865 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010866 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010867 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010868 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010869{
John Harrison6258fbe2015-05-29 17:43:48 +010010870 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010871 struct drm_i915_private *dev_priv = dev->dev_private;
10872 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10873 uint32_t pf, pipesrc;
10874 int ret;
10875
John Harrison5fb9de12015-05-29 17:44:07 +010010876 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010877 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010878 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010879
10880 /* i965+ uses the linear or tiled offsets from the
10881 * Display Registers (which do not change across a page-flip)
10882 * so we need only reprogram the base address.
10883 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010884 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10885 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10886 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010887 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010888 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010889
10890 /* XXX Enabling the panel-fitter across page-flip is so far
10891 * untested on non-native modes, so ignore it for now.
10892 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10893 */
10894 pf = 0;
10895 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010896 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010897
10898 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010899 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010900}
10901
10902static int intel_gen6_queue_flip(struct drm_device *dev,
10903 struct drm_crtc *crtc,
10904 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010905 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010906 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010907 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010908{
John Harrison6258fbe2015-05-29 17:43:48 +010010909 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010910 struct drm_i915_private *dev_priv = dev->dev_private;
10911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10912 uint32_t pf, pipesrc;
10913 int ret;
10914
John Harrison5fb9de12015-05-29 17:44:07 +010010915 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010916 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010917 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010918
Daniel Vetter6d90c952012-04-26 23:28:05 +020010919 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10920 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10921 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010922 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010923
Chris Wilson99d9acd2012-04-17 20:37:00 +010010924 /* Contrary to the suggestions in the documentation,
10925 * "Enable Panel Fitter" does not seem to be required when page
10926 * flipping with a non-native mode, and worse causes a normal
10927 * modeset to fail.
10928 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10929 */
10930 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010931 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010932 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010933
10934 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010935 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010936}
10937
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010938static int intel_gen7_queue_flip(struct drm_device *dev,
10939 struct drm_crtc *crtc,
10940 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010941 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010942 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010943 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010944{
John Harrison6258fbe2015-05-29 17:43:48 +010010945 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010947 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010948 int len, ret;
10949
Robin Schroereba905b2014-05-18 02:24:50 +020010950 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010951 case PLANE_A:
10952 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10953 break;
10954 case PLANE_B:
10955 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10956 break;
10957 case PLANE_C:
10958 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10959 break;
10960 default:
10961 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010962 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010963 }
10964
Chris Wilsonffe74d72013-08-26 20:58:12 +010010965 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010966 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010967 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010968 /*
10969 * On Gen 8, SRM is now taking an extra dword to accommodate
10970 * 48bits addresses, and we need a NOOP for the batch size to
10971 * stay even.
10972 */
10973 if (IS_GEN8(dev))
10974 len += 2;
10975 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010976
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010977 /*
10978 * BSpec MI_DISPLAY_FLIP for IVB:
10979 * "The full packet must be contained within the same cache line."
10980 *
10981 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10982 * cacheline, if we ever start emitting more commands before
10983 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10984 * then do the cacheline alignment, and finally emit the
10985 * MI_DISPLAY_FLIP.
10986 */
John Harrisonbba09b12015-05-29 17:44:06 +010010987 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010988 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010989 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010990
John Harrison5fb9de12015-05-29 17:44:07 +010010991 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010992 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010993 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010994
Chris Wilsonffe74d72013-08-26 20:58:12 +010010995 /* Unmask the flip-done completion message. Note that the bspec says that
10996 * we should do this for both the BCS and RCS, and that we must not unmask
10997 * more than one flip event at any time (or ensure that one flip message
10998 * can be sent by waiting for flip-done prior to queueing new flips).
10999 * Experimentation says that BCS works despite DERRMR masking all
11000 * flip-done completion events and that unmasking all planes at once
11001 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11002 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11003 */
11004 if (ring->id == RCS) {
11005 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11006 intel_ring_emit(ring, DERRMR);
11007 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11008 DERRMR_PIPEB_PRI_FLIP_DONE |
11009 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011010 if (IS_GEN8(dev))
Arun Siluveryf1afe242015-08-04 16:22:20 +010011011 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
Damien Lespiauf4768282014-04-07 20:24:34 +010011012 MI_SRM_LRM_GLOBAL_GTT);
11013 else
Arun Siluveryf1afe242015-08-04 16:22:20 +010011014 intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
Damien Lespiauf4768282014-04-07 20:24:34 +010011015 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011016 intel_ring_emit(ring, DERRMR);
11017 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011018 if (IS_GEN8(dev)) {
11019 intel_ring_emit(ring, 0);
11020 intel_ring_emit(ring, MI_NOOP);
11021 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011022 }
11023
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011024 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011025 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011026 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011027 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011028
11029 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011030 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011031}
11032
Sourab Gupta84c33a62014-06-02 16:47:17 +053011033static bool use_mmio_flip(struct intel_engine_cs *ring,
11034 struct drm_i915_gem_object *obj)
11035{
11036 /*
11037 * This is not being used for older platforms, because
11038 * non-availability of flip done interrupt forces us to use
11039 * CS flips. Older platforms derive flip done using some clever
11040 * tricks involving the flip_pending status bits and vblank irqs.
11041 * So using MMIO flips there would disrupt this mechanism.
11042 */
11043
Chris Wilson8e09bf82014-07-08 10:40:30 +010011044 if (ring == NULL)
11045 return true;
11046
Sourab Gupta84c33a62014-06-02 16:47:17 +053011047 if (INTEL_INFO(ring->dev)->gen < 5)
11048 return false;
11049
11050 if (i915.use_mmio_flip < 0)
11051 return false;
11052 else if (i915.use_mmio_flip > 0)
11053 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011054 else if (i915.enable_execlists)
11055 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011056 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011057 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011058}
11059
Damien Lespiauff944562014-11-20 14:58:16 +000011060static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11061{
11062 struct drm_device *dev = intel_crtc->base.dev;
11063 struct drm_i915_private *dev_priv = dev->dev_private;
11064 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011065 const enum pipe pipe = intel_crtc->pipe;
11066 u32 ctl, stride;
11067
11068 ctl = I915_READ(PLANE_CTL(pipe, 0));
11069 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011070 switch (fb->modifier[0]) {
11071 case DRM_FORMAT_MOD_NONE:
11072 break;
11073 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011074 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011075 break;
11076 case I915_FORMAT_MOD_Y_TILED:
11077 ctl |= PLANE_CTL_TILED_Y;
11078 break;
11079 case I915_FORMAT_MOD_Yf_TILED:
11080 ctl |= PLANE_CTL_TILED_YF;
11081 break;
11082 default:
11083 MISSING_CASE(fb->modifier[0]);
11084 }
Damien Lespiauff944562014-11-20 14:58:16 +000011085
11086 /*
11087 * The stride is either expressed as a multiple of 64 bytes chunks for
11088 * linear buffers or in number of tiles for tiled buffers.
11089 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011090 stride = fb->pitches[0] /
11091 intel_fb_stride_alignment(dev, fb->modifier[0],
11092 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011093
11094 /*
11095 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11096 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11097 */
11098 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11099 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11100
11101 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11102 POSTING_READ(PLANE_SURF(pipe, 0));
11103}
11104
11105static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011106{
11107 struct drm_device *dev = intel_crtc->base.dev;
11108 struct drm_i915_private *dev_priv = dev->dev_private;
11109 struct intel_framebuffer *intel_fb =
11110 to_intel_framebuffer(intel_crtc->base.primary->fb);
11111 struct drm_i915_gem_object *obj = intel_fb->obj;
11112 u32 dspcntr;
11113 u32 reg;
11114
Sourab Gupta84c33a62014-06-02 16:47:17 +053011115 reg = DSPCNTR(intel_crtc->plane);
11116 dspcntr = I915_READ(reg);
11117
Damien Lespiauc5d97472014-10-25 00:11:11 +010011118 if (obj->tiling_mode != I915_TILING_NONE)
11119 dspcntr |= DISPPLANE_TILED;
11120 else
11121 dspcntr &= ~DISPPLANE_TILED;
11122
Sourab Gupta84c33a62014-06-02 16:47:17 +053011123 I915_WRITE(reg, dspcntr);
11124
11125 I915_WRITE(DSPSURF(intel_crtc->plane),
11126 intel_crtc->unpin_work->gtt_offset);
11127 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011128
Damien Lespiauff944562014-11-20 14:58:16 +000011129}
11130
11131/*
11132 * XXX: This is the temporary way to update the plane registers until we get
11133 * around to using the usual plane update functions for MMIO flips
11134 */
11135static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11136{
11137 struct drm_device *dev = intel_crtc->base.dev;
Damien Lespiauff944562014-11-20 14:58:16 +000011138
11139 intel_mark_page_flip_active(intel_crtc);
11140
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020011141 intel_pipe_update_start(intel_crtc);
Damien Lespiauff944562014-11-20 14:58:16 +000011142
11143 if (INTEL_INFO(dev)->gen >= 9)
11144 skl_do_mmio_flip(intel_crtc);
11145 else
11146 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11147 ilk_do_mmio_flip(intel_crtc);
11148
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020011149 intel_pipe_update_end(intel_crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011150}
11151
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011152static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011153{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011154 struct intel_mmio_flip *mmio_flip =
11155 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011156
Daniel Vettereed29a52015-05-21 14:21:25 +020011157 if (mmio_flip->req)
11158 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011159 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011160 false, NULL,
11161 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011162
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011163 intel_do_mmio_flip(mmio_flip->crtc);
11164
Daniel Vettereed29a52015-05-21 14:21:25 +020011165 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011166 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011167}
11168
11169static int intel_queue_mmio_flip(struct drm_device *dev,
11170 struct drm_crtc *crtc,
11171 struct drm_framebuffer *fb,
11172 struct drm_i915_gem_object *obj,
11173 struct intel_engine_cs *ring,
11174 uint32_t flags)
11175{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011176 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011177
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011178 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11179 if (mmio_flip == NULL)
11180 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011181
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011182 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011183 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011184 mmio_flip->crtc = to_intel_crtc(crtc);
11185
11186 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11187 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011188
Sourab Gupta84c33a62014-06-02 16:47:17 +053011189 return 0;
11190}
11191
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011192static int intel_default_queue_flip(struct drm_device *dev,
11193 struct drm_crtc *crtc,
11194 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011195 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011196 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011197 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011198{
11199 return -ENODEV;
11200}
11201
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011202static bool __intel_pageflip_stall_check(struct drm_device *dev,
11203 struct drm_crtc *crtc)
11204{
11205 struct drm_i915_private *dev_priv = dev->dev_private;
11206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11207 struct intel_unpin_work *work = intel_crtc->unpin_work;
11208 u32 addr;
11209
11210 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11211 return true;
11212
Chris Wilson908565c2015-08-12 13:08:22 +010011213 if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
11214 return false;
11215
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011216 if (!work->enable_stall_check)
11217 return false;
11218
11219 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011220 if (work->flip_queued_req &&
11221 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011222 return false;
11223
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011224 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011225 }
11226
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011227 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011228 return false;
11229
11230 /* Potential stall - if we see that the flip has happened,
11231 * assume a missed interrupt. */
11232 if (INTEL_INFO(dev)->gen >= 4)
11233 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11234 else
11235 addr = I915_READ(DSPADDR(intel_crtc->plane));
11236
11237 /* There is a potential issue here with a false positive after a flip
11238 * to the same address. We could address this by checking for a
11239 * non-incrementing frame counter.
11240 */
11241 return addr == work->gtt_offset;
11242}
11243
11244void intel_check_page_flip(struct drm_device *dev, int pipe)
11245{
11246 struct drm_i915_private *dev_priv = dev->dev_private;
11247 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011249 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011250
Dave Gordon6c51d462015-03-06 15:34:26 +000011251 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011252
11253 if (crtc == NULL)
11254 return;
11255
Daniel Vetterf3260382014-09-15 14:55:23 +020011256 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011257 work = intel_crtc->unpin_work;
11258 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011259 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011260 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011261 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011262 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011263 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011264 if (work != NULL &&
11265 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11266 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011267 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011268}
11269
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011270static int intel_crtc_page_flip(struct drm_crtc *crtc,
11271 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011272 struct drm_pending_vblank_event *event,
11273 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011274{
11275 struct drm_device *dev = crtc->dev;
11276 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011277 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011278 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011279 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011280 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011281 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011282 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011283 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011284 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011285 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011286 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011287
Matt Roper2ff8fde2014-07-08 07:50:07 -070011288 /*
11289 * drm_mode_page_flip_ioctl() should already catch this, but double
11290 * check to be safe. In the future we may enable pageflipping from
11291 * a disabled primary plane.
11292 */
11293 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11294 return -EBUSY;
11295
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011296 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011297 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011298 return -EINVAL;
11299
11300 /*
11301 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11302 * Note that pitch changes could also affect these register.
11303 */
11304 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011305 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11306 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011307 return -EINVAL;
11308
Chris Wilsonf900db42014-02-20 09:26:13 +000011309 if (i915_terminally_wedged(&dev_priv->gpu_error))
11310 goto out_hang;
11311
Daniel Vetterb14c5672013-09-19 12:18:32 +020011312 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011313 if (work == NULL)
11314 return -ENOMEM;
11315
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011316 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011317 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011318 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011319 INIT_WORK(&work->work, intel_unpin_work_fn);
11320
Daniel Vetter87b6b102014-05-15 15:33:46 +020011321 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011322 if (ret)
11323 goto free_work;
11324
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011325 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011326 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011327 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011328 /* Before declaring the flip queue wedged, check if
11329 * the hardware completed the operation behind our backs.
11330 */
11331 if (__intel_pageflip_stall_check(dev, crtc)) {
11332 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11333 page_flip_completed(intel_crtc);
11334 } else {
11335 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011336 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011337
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011338 drm_crtc_vblank_put(crtc);
11339 kfree(work);
11340 return -EBUSY;
11341 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011342 }
11343 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011344 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011345
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011346 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11347 flush_workqueue(dev_priv->wq);
11348
Jesse Barnes75dfca82010-02-10 15:09:44 -080011349 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011350 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011351 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011352
Matt Roperf4510a22014-04-01 15:22:40 -070011353 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011354 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011355
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011356 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011357
Chris Wilson89ed88b2015-02-16 14:31:49 +000011358 ret = i915_mutex_lock_interruptible(dev);
11359 if (ret)
11360 goto cleanup;
11361
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011362 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011363 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011364
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011365 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011366 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011367
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011368 if (IS_VALLEYVIEW(dev)) {
11369 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011370 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011371 /* vlv: DISPLAY_FLIP fails to change tiling */
11372 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011373 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011374 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011375 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011376 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011377 if (ring == NULL || ring->id != RCS)
11378 ring = &dev_priv->ring[BCS];
11379 } else {
11380 ring = &dev_priv->ring[RCS];
11381 }
11382
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011383 mmio_flip = use_mmio_flip(ring, obj);
11384
11385 /* When using CS flips, we want to emit semaphores between rings.
11386 * However, when using mmio flips we will create a task to do the
11387 * synchronisation, so all we want here is to pin the framebuffer
11388 * into the display plane and skip any waits.
11389 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011390 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011391 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011392 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011393 if (ret)
11394 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011395
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011396 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11397 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011398
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011399 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011400 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11401 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011402 if (ret)
11403 goto cleanup_unpin;
11404
John Harrisonf06cc1b2014-11-24 18:49:37 +000011405 i915_gem_request_assign(&work->flip_queued_req,
11406 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011407 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011408 if (!request) {
11409 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11410 if (ret)
11411 goto cleanup_unpin;
11412 }
11413
11414 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011415 page_flip_flags);
11416 if (ret)
11417 goto cleanup_unpin;
11418
John Harrison6258fbe2015-05-29 17:43:48 +010011419 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011420 }
11421
John Harrison91af1272015-06-18 13:14:56 +010011422 if (request)
John Harrison75289872015-05-29 17:43:49 +010011423 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011424
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011425 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011426 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011427
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011428 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011429 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011430 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011431
Paulo Zanoni4e1e26f2015-07-14 16:29:13 -030011432 intel_fbc_disable_crtc(intel_crtc);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011433 intel_frontbuffer_flip_prepare(dev,
11434 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011435
Jesse Barnese5510fa2010-07-01 16:48:37 -070011436 trace_i915_flip_request(intel_crtc->plane, obj);
11437
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011438 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011439
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011440cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011441 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011442cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011443 if (request)
11444 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011445 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011446 mutex_unlock(&dev->struct_mutex);
11447cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011448 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011449 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011450
Chris Wilson89ed88b2015-02-16 14:31:49 +000011451 drm_gem_object_unreference_unlocked(&obj->base);
11452 drm_framebuffer_unreference(work->old_fb);
11453
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011454 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011455 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011456 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011457
Daniel Vetter87b6b102014-05-15 15:33:46 +020011458 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011459free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011460 kfree(work);
11461
Chris Wilsonf900db42014-02-20 09:26:13 +000011462 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011463 struct drm_atomic_state *state;
11464 struct drm_plane_state *plane_state;
11465
Chris Wilsonf900db42014-02-20 09:26:13 +000011466out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011467 state = drm_atomic_state_alloc(dev);
11468 if (!state)
11469 return -ENOMEM;
11470 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11471
11472retry:
11473 plane_state = drm_atomic_get_plane_state(state, primary);
11474 ret = PTR_ERR_OR_ZERO(plane_state);
11475 if (!ret) {
11476 drm_atomic_set_fb_for_plane(plane_state, fb);
11477
11478 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11479 if (!ret)
11480 ret = drm_atomic_commit(state);
11481 }
11482
11483 if (ret == -EDEADLK) {
11484 drm_modeset_backoff(state->acquire_ctx);
11485 drm_atomic_state_clear(state);
11486 goto retry;
11487 }
11488
11489 if (ret)
11490 drm_atomic_state_free(state);
11491
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011492 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011493 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011494 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011495 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011496 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011497 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011498 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011499}
11500
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011501
11502/**
11503 * intel_wm_need_update - Check whether watermarks need updating
11504 * @plane: drm plane
11505 * @state: new plane state
11506 *
11507 * Check current plane state versus the new one to determine whether
11508 * watermarks need to be recalculated.
11509 *
11510 * Returns true or false.
11511 */
11512static bool intel_wm_need_update(struct drm_plane *plane,
11513 struct drm_plane_state *state)
11514{
11515 /* Update watermarks on tiling changes. */
11516 if (!plane->state->fb || !state->fb ||
11517 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11518 plane->state->rotation != state->rotation)
11519 return true;
11520
11521 if (plane->state->crtc_w != state->crtc_w)
11522 return true;
11523
11524 return false;
11525}
11526
11527int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11528 struct drm_plane_state *plane_state)
11529{
11530 struct drm_crtc *crtc = crtc_state->crtc;
11531 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11532 struct drm_plane *plane = plane_state->plane;
11533 struct drm_device *dev = crtc->dev;
11534 struct drm_i915_private *dev_priv = dev->dev_private;
11535 struct intel_plane_state *old_plane_state =
11536 to_intel_plane_state(plane->state);
11537 int idx = intel_crtc->base.base.id, ret;
11538 int i = drm_plane_index(plane);
11539 bool mode_changed = needs_modeset(crtc_state);
11540 bool was_crtc_enabled = crtc->state->active;
11541 bool is_crtc_enabled = crtc_state->active;
11542
11543 bool turn_off, turn_on, visible, was_visible;
11544 struct drm_framebuffer *fb = plane_state->fb;
11545
11546 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11547 plane->type != DRM_PLANE_TYPE_CURSOR) {
11548 ret = skl_update_scaler_plane(
11549 to_intel_crtc_state(crtc_state),
11550 to_intel_plane_state(plane_state));
11551 if (ret)
11552 return ret;
11553 }
11554
11555 /*
11556 * Disabling a plane is always okay; we just need to update
11557 * fb tracking in a special way since cleanup_fb() won't
11558 * get called by the plane helpers.
11559 */
11560 if (old_plane_state->base.fb && !fb)
11561 intel_crtc->atomic.disabled_planes |= 1 << i;
11562
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011563 was_visible = old_plane_state->visible;
11564 visible = to_intel_plane_state(plane_state)->visible;
11565
11566 if (!was_crtc_enabled && WARN_ON(was_visible))
11567 was_visible = false;
11568
11569 if (!is_crtc_enabled && WARN_ON(visible))
11570 visible = false;
11571
11572 if (!was_visible && !visible)
11573 return 0;
11574
11575 turn_off = was_visible && (!visible || mode_changed);
11576 turn_on = visible && (!was_visible || mode_changed);
11577
11578 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11579 plane->base.id, fb ? fb->base.id : -1);
11580
11581 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11582 plane->base.id, was_visible, visible,
11583 turn_off, turn_on, mode_changed);
11584
Ville Syrjälä852eb002015-06-24 22:00:07 +030011585 if (turn_on) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011586 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011587 /* must disable cxsr around plane enable/disable */
11588 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11589 intel_crtc->atomic.disable_cxsr = true;
11590 /* to potentially re-enable cxsr */
11591 intel_crtc->atomic.wait_vblank = true;
11592 intel_crtc->atomic.update_wm_post = true;
11593 }
11594 } else if (turn_off) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011595 intel_crtc->atomic.update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011596 /* must disable cxsr around plane enable/disable */
11597 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11598 if (is_crtc_enabled)
11599 intel_crtc->atomic.wait_vblank = true;
11600 intel_crtc->atomic.disable_cxsr = true;
11601 }
11602 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011603 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011604 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011605
Rodrigo Vivi8be6ca82015-08-24 16:38:23 -070011606 if (visible || was_visible)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011607 intel_crtc->atomic.fb_bits |=
11608 to_intel_plane(plane)->frontbuffer_bit;
11609
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011610 switch (plane->type) {
11611 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011612 intel_crtc->atomic.wait_for_flips = true;
11613 intel_crtc->atomic.pre_disable_primary = turn_off;
11614 intel_crtc->atomic.post_enable_primary = turn_on;
11615
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011616 if (turn_off) {
11617 /*
11618 * FIXME: Actually if we will still have any other
11619 * plane enabled on the pipe we could let IPS enabled
11620 * still, but for now lets consider that when we make
11621 * primary invisible by setting DSPCNTR to 0 on
11622 * update_primary_plane function IPS needs to be
11623 * disable.
11624 */
11625 intel_crtc->atomic.disable_ips = true;
11626
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011627 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011628 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011629
11630 /*
11631 * FBC does not work on some platforms for rotated
11632 * planes, so disable it when rotation is not 0 and
11633 * update it when rotation is set back to 0.
11634 *
11635 * FIXME: This is redundant with the fbc update done in
11636 * the primary plane enable function except that that
11637 * one is done too late. We eventually need to unify
11638 * this.
11639 */
11640
11641 if (visible &&
11642 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11643 dev_priv->fbc.crtc == intel_crtc &&
11644 plane_state->rotation != BIT(DRM_ROTATE_0))
11645 intel_crtc->atomic.disable_fbc = true;
11646
11647 /*
11648 * BDW signals flip done immediately if the plane
11649 * is disabled, even if the plane enable is already
11650 * armed to occur at the next vblank :(
11651 */
11652 if (turn_on && IS_BROADWELL(dev))
11653 intel_crtc->atomic.wait_vblank = true;
11654
11655 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11656 break;
11657 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011658 break;
11659 case DRM_PLANE_TYPE_OVERLAY:
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011660 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011661 intel_crtc->atomic.wait_vblank = true;
11662 intel_crtc->atomic.update_sprite_watermarks |=
11663 1 << i;
11664 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011665 }
11666 return 0;
11667}
11668
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011669static bool encoders_cloneable(const struct intel_encoder *a,
11670 const struct intel_encoder *b)
11671{
11672 /* masks could be asymmetric, so check both ways */
11673 return a == b || (a->cloneable & (1 << b->type) &&
11674 b->cloneable & (1 << a->type));
11675}
11676
11677static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11678 struct intel_crtc *crtc,
11679 struct intel_encoder *encoder)
11680{
11681 struct intel_encoder *source_encoder;
11682 struct drm_connector *connector;
11683 struct drm_connector_state *connector_state;
11684 int i;
11685
11686 for_each_connector_in_state(state, connector, connector_state, i) {
11687 if (connector_state->crtc != &crtc->base)
11688 continue;
11689
11690 source_encoder =
11691 to_intel_encoder(connector_state->best_encoder);
11692 if (!encoders_cloneable(encoder, source_encoder))
11693 return false;
11694 }
11695
11696 return true;
11697}
11698
11699static bool check_encoder_cloning(struct drm_atomic_state *state,
11700 struct intel_crtc *crtc)
11701{
11702 struct intel_encoder *encoder;
11703 struct drm_connector *connector;
11704 struct drm_connector_state *connector_state;
11705 int i;
11706
11707 for_each_connector_in_state(state, connector, connector_state, i) {
11708 if (connector_state->crtc != &crtc->base)
11709 continue;
11710
11711 encoder = to_intel_encoder(connector_state->best_encoder);
11712 if (!check_single_encoder_cloning(state, crtc, encoder))
11713 return false;
11714 }
11715
11716 return true;
11717}
11718
11719static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11720 struct drm_crtc_state *crtc_state)
11721{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011722 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011723 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011725 struct intel_crtc_state *pipe_config =
11726 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011727 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020011728 int ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011729 bool mode_changed = needs_modeset(crtc_state);
11730
11731 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11732 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11733 return -EINVAL;
11734 }
11735
Ville Syrjälä852eb002015-06-24 22:00:07 +030011736 if (mode_changed && !crtc_state->active)
11737 intel_crtc->atomic.update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011738
Maarten Lankhorstad421372015-06-15 12:33:42 +020011739 if (mode_changed && crtc_state->enable &&
11740 dev_priv->display.crtc_compute_clock &&
11741 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11742 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11743 pipe_config);
11744 if (ret)
11745 return ret;
11746 }
11747
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011748 ret = 0;
11749 if (INTEL_INFO(dev)->gen >= 9) {
11750 if (mode_changed)
11751 ret = skl_update_scaler_crtc(pipe_config);
11752
11753 if (!ret)
11754 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11755 pipe_config);
11756 }
11757
11758 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011759}
11760
Jani Nikula65b38e02015-04-13 11:26:56 +030011761static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011762 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11763 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011764 .atomic_begin = intel_begin_crtc_commit,
11765 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011766 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011767};
11768
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011769static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11770{
11771 struct intel_connector *connector;
11772
11773 for_each_intel_connector(dev, connector) {
11774 if (connector->base.encoder) {
11775 connector->base.state->best_encoder =
11776 connector->base.encoder;
11777 connector->base.state->crtc =
11778 connector->base.encoder->crtc;
11779 } else {
11780 connector->base.state->best_encoder = NULL;
11781 connector->base.state->crtc = NULL;
11782 }
11783 }
11784}
11785
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011786static void
Robin Schroereba905b2014-05-18 02:24:50 +020011787connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011788 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011789{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011790 int bpp = pipe_config->pipe_bpp;
11791
11792 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11793 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011794 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011795
11796 /* Don't use an invalid EDID bpc value */
11797 if (connector->base.display_info.bpc &&
11798 connector->base.display_info.bpc * 3 < bpp) {
11799 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11800 bpp, connector->base.display_info.bpc*3);
11801 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11802 }
11803
11804 /* Clamp bpp to 8 on screens without EDID 1.4 */
11805 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11806 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11807 bpp);
11808 pipe_config->pipe_bpp = 24;
11809 }
11810}
11811
11812static int
11813compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011814 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011815{
11816 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011817 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011818 struct drm_connector *connector;
11819 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011820 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011821
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011822 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011823 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011824 else if (INTEL_INFO(dev)->gen >= 5)
11825 bpp = 12*3;
11826 else
11827 bpp = 8*3;
11828
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011829
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011830 pipe_config->pipe_bpp = bpp;
11831
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011832 state = pipe_config->base.state;
11833
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011834 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011835 for_each_connector_in_state(state, connector, connector_state, i) {
11836 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011837 continue;
11838
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011839 connected_sink_compute_bpp(to_intel_connector(connector),
11840 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011841 }
11842
11843 return bpp;
11844}
11845
Daniel Vetter644db712013-09-19 14:53:58 +020011846static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11847{
11848 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11849 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011850 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011851 mode->crtc_hdisplay, mode->crtc_hsync_start,
11852 mode->crtc_hsync_end, mode->crtc_htotal,
11853 mode->crtc_vdisplay, mode->crtc_vsync_start,
11854 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11855}
11856
Daniel Vetterc0b03412013-05-28 12:05:54 +020011857static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011858 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011859 const char *context)
11860{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011861 struct drm_device *dev = crtc->base.dev;
11862 struct drm_plane *plane;
11863 struct intel_plane *intel_plane;
11864 struct intel_plane_state *state;
11865 struct drm_framebuffer *fb;
11866
11867 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11868 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011869
11870 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11871 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11872 pipe_config->pipe_bpp, pipe_config->dither);
11873 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11874 pipe_config->has_pch_encoder,
11875 pipe_config->fdi_lanes,
11876 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11877 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11878 pipe_config->fdi_m_n.tu);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011879 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 +030011880 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011881 pipe_config->lane_count,
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011882 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11883 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11884 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011885
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011886 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 -070011887 pipe_config->has_dp_encoder,
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030011888 pipe_config->lane_count,
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011889 pipe_config->dp_m2_n2.gmch_m,
11890 pipe_config->dp_m2_n2.gmch_n,
11891 pipe_config->dp_m2_n2.link_m,
11892 pipe_config->dp_m2_n2.link_n,
11893 pipe_config->dp_m2_n2.tu);
11894
Daniel Vetter55072d12014-11-20 16:10:28 +010011895 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11896 pipe_config->has_audio,
11897 pipe_config->has_infoframe);
11898
Daniel Vetterc0b03412013-05-28 12:05:54 +020011899 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011900 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011901 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011902 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11903 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011904 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011905 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11906 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011907 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11908 crtc->num_scalers,
11909 pipe_config->scaler_state.scaler_users,
11910 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011911 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11912 pipe_config->gmch_pfit.control,
11913 pipe_config->gmch_pfit.pgm_ratios,
11914 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011915 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011916 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011917 pipe_config->pch_pfit.size,
11918 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011919 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011920 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011921
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011922 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030011923 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011924 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030011925 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011926 pipe_config->ddi_pll_sel,
11927 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030011928 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011929 pipe_config->dpll_hw_state.pll0,
11930 pipe_config->dpll_hw_state.pll1,
11931 pipe_config->dpll_hw_state.pll2,
11932 pipe_config->dpll_hw_state.pll3,
11933 pipe_config->dpll_hw_state.pll6,
11934 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030011935 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030011936 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011937 pipe_config->dpll_hw_state.pcsdw12);
11938 } else if (IS_SKYLAKE(dev)) {
11939 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11940 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11941 pipe_config->ddi_pll_sel,
11942 pipe_config->dpll_hw_state.ctrl1,
11943 pipe_config->dpll_hw_state.cfgcr1,
11944 pipe_config->dpll_hw_state.cfgcr2);
11945 } else if (HAS_DDI(dev)) {
11946 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11947 pipe_config->ddi_pll_sel,
11948 pipe_config->dpll_hw_state.wrpll);
11949 } else {
11950 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11951 "fp0: 0x%x, fp1: 0x%x\n",
11952 pipe_config->dpll_hw_state.dpll,
11953 pipe_config->dpll_hw_state.dpll_md,
11954 pipe_config->dpll_hw_state.fp0,
11955 pipe_config->dpll_hw_state.fp1);
11956 }
11957
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011958 DRM_DEBUG_KMS("planes on this crtc\n");
11959 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11960 intel_plane = to_intel_plane(plane);
11961 if (intel_plane->pipe != crtc->pipe)
11962 continue;
11963
11964 state = to_intel_plane_state(plane->state);
11965 fb = state->base.fb;
11966 if (!fb) {
11967 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11968 "disabled, scaler_id = %d\n",
11969 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11970 plane->base.id, intel_plane->pipe,
11971 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11972 drm_plane_index(plane), state->scaler_id);
11973 continue;
11974 }
11975
11976 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11977 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11978 plane->base.id, intel_plane->pipe,
11979 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11980 drm_plane_index(plane));
11981 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11982 fb->base.id, fb->width, fb->height, fb->pixel_format);
11983 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11984 state->scaler_id,
11985 state->src.x1 >> 16, state->src.y1 >> 16,
11986 drm_rect_width(&state->src) >> 16,
11987 drm_rect_height(&state->src) >> 16,
11988 state->dst.x1, state->dst.y1,
11989 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11990 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011991}
11992
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011993static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011994{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011995 struct drm_device *dev = state->dev;
11996 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011997 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011998 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011999 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012000 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012001
12002 /*
12003 * Walk the connector list instead of the encoder
12004 * list to detect the problem on ddi platforms
12005 * where there's just one encoder per digital port.
12006 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012007 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012008 if (!connector_state->best_encoder)
12009 continue;
12010
12011 encoder = to_intel_encoder(connector_state->best_encoder);
12012
12013 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012014
12015 switch (encoder->type) {
12016 unsigned int port_mask;
12017 case INTEL_OUTPUT_UNKNOWN:
12018 if (WARN_ON(!HAS_DDI(dev)))
12019 break;
12020 case INTEL_OUTPUT_DISPLAYPORT:
12021 case INTEL_OUTPUT_HDMI:
12022 case INTEL_OUTPUT_EDP:
12023 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12024
12025 /* the same port mustn't appear more than once */
12026 if (used_ports & port_mask)
12027 return false;
12028
12029 used_ports |= port_mask;
12030 default:
12031 break;
12032 }
12033 }
12034
12035 return true;
12036}
12037
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012038static void
12039clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12040{
12041 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012042 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012043 struct intel_dpll_hw_state dpll_hw_state;
12044 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012045 uint32_t ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012046 bool force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012047
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012048 /* FIXME: before the switch to atomic started, a new pipe_config was
12049 * kzalloc'd. Code that depends on any field being zero should be
12050 * fixed, so that the crtc_state can be safely duplicated. For now,
12051 * only fields that are know to not cause problems are preserved. */
12052
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012053 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012054 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012055 shared_dpll = crtc_state->shared_dpll;
12056 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012057 ddi_pll_sel = crtc_state->ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012058 force_thru = crtc_state->pch_pfit.force_thru;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012059
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012060 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012061
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012062 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012063 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012064 crtc_state->shared_dpll = shared_dpll;
12065 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012066 crtc_state->ddi_pll_sel = ddi_pll_sel;
Maarten Lankhorstc4e2d042015-08-05 12:36:59 +020012067 crtc_state->pch_pfit.force_thru = force_thru;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012068}
12069
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012070static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012071intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012072 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012073{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012074 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012075 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012076 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012077 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012078 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012079 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012080 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012081
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012082 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012083
Daniel Vettere143a212013-07-04 12:01:15 +020012084 pipe_config->cpu_transcoder =
12085 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012086
Imre Deak2960bc92013-07-30 13:36:32 +030012087 /*
12088 * Sanitize sync polarity flags based on requested ones. If neither
12089 * positive or negative polarity is requested, treat this as meaning
12090 * negative polarity.
12091 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012092 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012093 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012094 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012095
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012096 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012097 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012098 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012099
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012100 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12101 pipe_config);
12102 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012103 goto fail;
12104
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012105 /*
12106 * Determine the real pipe dimensions. Note that stereo modes can
12107 * increase the actual pipe size due to the frame doubling and
12108 * insertion of additional space for blanks between the frame. This
12109 * is stored in the crtc timings. We use the requested mode to do this
12110 * computation to clearly distinguish it from the adjusted mode, which
12111 * can be changed by the connectors in the below retry loop.
12112 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012113 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012114 &pipe_config->pipe_src_w,
12115 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012116
Daniel Vettere29c22c2013-02-21 00:00:16 +010012117encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012118 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012119 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012120 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012121
Daniel Vetter135c81b2013-07-21 21:37:09 +020012122 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012123 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12124 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012125
Daniel Vetter7758a112012-07-08 19:40:39 +020012126 /* Pass our mode to the connectors and the CRTC to give them a chance to
12127 * adjust it according to limitations or connector properties, and also
12128 * a chance to reject the mode entirely.
12129 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012130 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012131 if (connector_state->crtc != crtc)
12132 continue;
12133
12134 encoder = to_intel_encoder(connector_state->best_encoder);
12135
Daniel Vetterefea6e82013-07-21 21:36:59 +020012136 if (!(encoder->compute_config(encoder, pipe_config))) {
12137 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012138 goto fail;
12139 }
12140 }
12141
Daniel Vetterff9a6752013-06-01 17:16:21 +020012142 /* Set default port clock if not overwritten by the encoder. Needs to be
12143 * done afterwards in case the encoder adjusts the mode. */
12144 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012145 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012146 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012147
Daniel Vettera43f6e02013-06-07 23:10:32 +020012148 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012149 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012150 DRM_DEBUG_KMS("CRTC fixup failed\n");
12151 goto fail;
12152 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012153
12154 if (ret == RETRY) {
12155 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12156 ret = -EINVAL;
12157 goto fail;
12158 }
12159
12160 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12161 retry = false;
12162 goto encoder_retry;
12163 }
12164
Daniel Vettere8fa4272015-08-12 11:43:34 +020012165 /* Dithering seems to not pass-through bits correctly when it should, so
12166 * only enable it on 6bpc panels. */
12167 pipe_config->dither = pipe_config->pipe_bpp == 6*3;
Daniel Vetter62f0ace2015-08-26 18:57:26 +020012168 DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012169 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012170
Daniel Vetter7758a112012-07-08 19:40:39 +020012171fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012172 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012173}
12174
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012175static void
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020012176intel_modeset_update_crtc_state(struct drm_atomic_state *state)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012177{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012178 struct drm_crtc *crtc;
12179 struct drm_crtc_state *crtc_state;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012180 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012181
Ville Syrjälä76688512014-01-10 11:28:06 +020012182 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012183 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012184 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012185
12186 /* Update hwmode for vblank functions */
12187 if (crtc->state->active)
12188 crtc->hwmode = crtc->state->adjusted_mode;
12189 else
12190 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012191 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012192}
12193
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012194static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012195{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012196 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012197
12198 if (clock1 == clock2)
12199 return true;
12200
12201 if (!clock1 || !clock2)
12202 return false;
12203
12204 diff = abs(clock1 - clock2);
12205
12206 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12207 return true;
12208
12209 return false;
12210}
12211
Daniel Vetter25c5b262012-07-08 22:08:04 +020012212#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12213 list_for_each_entry((intel_crtc), \
12214 &(dev)->mode_config.crtc_list, \
12215 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012216 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012217
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012218
12219static bool
12220intel_compare_m_n(unsigned int m, unsigned int n,
12221 unsigned int m2, unsigned int n2,
12222 bool exact)
12223{
12224 if (m == m2 && n == n2)
12225 return true;
12226
12227 if (exact || !m || !n || !m2 || !n2)
12228 return false;
12229
12230 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12231
12232 if (m > m2) {
12233 while (m > m2) {
12234 m2 <<= 1;
12235 n2 <<= 1;
12236 }
12237 } else if (m < m2) {
12238 while (m < m2) {
12239 m <<= 1;
12240 n <<= 1;
12241 }
12242 }
12243
12244 return m == m2 && n == n2;
12245}
12246
12247static bool
12248intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12249 struct intel_link_m_n *m2_n2,
12250 bool adjust)
12251{
12252 if (m_n->tu == m2_n2->tu &&
12253 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12254 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12255 intel_compare_m_n(m_n->link_m, m_n->link_n,
12256 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12257 if (adjust)
12258 *m2_n2 = *m_n;
12259
12260 return true;
12261 }
12262
12263 return false;
12264}
12265
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012266static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012267intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012268 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012269 struct intel_crtc_state *pipe_config,
12270 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012271{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012272 bool ret = true;
12273
12274#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12275 do { \
12276 if (!adjust) \
12277 DRM_ERROR(fmt, ##__VA_ARGS__); \
12278 else \
12279 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12280 } while (0)
12281
Daniel Vetter66e985c2013-06-05 13:34:20 +020012282#define PIPE_CONF_CHECK_X(name) \
12283 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012284 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012285 "(expected 0x%08x, found 0x%08x)\n", \
12286 current_config->name, \
12287 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012288 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012289 }
12290
Daniel Vetter08a24032013-04-19 11:25:34 +020012291#define PIPE_CONF_CHECK_I(name) \
12292 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012293 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012294 "(expected %i, found %i)\n", \
12295 current_config->name, \
12296 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012297 ret = false; \
12298 }
12299
12300#define PIPE_CONF_CHECK_M_N(name) \
12301 if (!intel_compare_link_m_n(&current_config->name, \
12302 &pipe_config->name,\
12303 adjust)) { \
12304 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12305 "(expected tu %i gmch %i/%i link %i/%i, " \
12306 "found tu %i, gmch %i/%i link %i/%i)\n", \
12307 current_config->name.tu, \
12308 current_config->name.gmch_m, \
12309 current_config->name.gmch_n, \
12310 current_config->name.link_m, \
12311 current_config->name.link_n, \
12312 pipe_config->name.tu, \
12313 pipe_config->name.gmch_m, \
12314 pipe_config->name.gmch_n, \
12315 pipe_config->name.link_m, \
12316 pipe_config->name.link_n); \
12317 ret = false; \
12318 }
12319
12320#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12321 if (!intel_compare_link_m_n(&current_config->name, \
12322 &pipe_config->name, adjust) && \
12323 !intel_compare_link_m_n(&current_config->alt_name, \
12324 &pipe_config->name, adjust)) { \
12325 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12326 "(expected tu %i gmch %i/%i link %i/%i, " \
12327 "or tu %i gmch %i/%i link %i/%i, " \
12328 "found tu %i, gmch %i/%i link %i/%i)\n", \
12329 current_config->name.tu, \
12330 current_config->name.gmch_m, \
12331 current_config->name.gmch_n, \
12332 current_config->name.link_m, \
12333 current_config->name.link_n, \
12334 current_config->alt_name.tu, \
12335 current_config->alt_name.gmch_m, \
12336 current_config->alt_name.gmch_n, \
12337 current_config->alt_name.link_m, \
12338 current_config->alt_name.link_n, \
12339 pipe_config->name.tu, \
12340 pipe_config->name.gmch_m, \
12341 pipe_config->name.gmch_n, \
12342 pipe_config->name.link_m, \
12343 pipe_config->name.link_n); \
12344 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012345 }
12346
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012347/* This is required for BDW+ where there is only one set of registers for
12348 * switching between high and low RR.
12349 * This macro can be used whenever a comparison has to be made between one
12350 * hw state and multiple sw state variables.
12351 */
12352#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12353 if ((current_config->name != pipe_config->name) && \
12354 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012355 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012356 "(expected %i or %i, found %i)\n", \
12357 current_config->name, \
12358 current_config->alt_name, \
12359 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012360 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012361 }
12362
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012363#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12364 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012365 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012366 "(expected %i, found %i)\n", \
12367 current_config->name & (mask), \
12368 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012369 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012370 }
12371
Ville Syrjälä5e550652013-09-06 23:29:07 +030012372#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12373 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012374 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012375 "(expected %i, found %i)\n", \
12376 current_config->name, \
12377 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012378 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012379 }
12380
Daniel Vetterbb760062013-06-06 14:55:52 +020012381#define PIPE_CONF_QUIRK(quirk) \
12382 ((current_config->quirks | pipe_config->quirks) & (quirk))
12383
Daniel Vettereccb1402013-05-22 00:50:22 +020012384 PIPE_CONF_CHECK_I(cpu_transcoder);
12385
Daniel Vetter08a24032013-04-19 11:25:34 +020012386 PIPE_CONF_CHECK_I(has_pch_encoder);
12387 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012388 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012389
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012390 PIPE_CONF_CHECK_I(has_dp_encoder);
Ville Syrjälä90a6b7b2015-07-06 16:39:15 +030012391 PIPE_CONF_CHECK_I(lane_count);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012392
12393 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012394 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012395
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012396 PIPE_CONF_CHECK_I(has_drrs);
12397 if (current_config->has_drrs)
12398 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12399 } else
12400 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012401
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012402 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12403 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12404 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12405 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12406 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12407 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012408
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012409 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12410 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12411 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12412 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12413 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12414 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012415
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012416 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012417 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012418 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12419 IS_VALLEYVIEW(dev))
12420 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012421 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012422
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012423 PIPE_CONF_CHECK_I(has_audio);
12424
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012425 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012426 DRM_MODE_FLAG_INTERLACE);
12427
Daniel Vetterbb760062013-06-06 14:55:52 +020012428 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012429 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012430 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012431 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012432 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012433 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012434 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012435 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012436 DRM_MODE_FLAG_NVSYNC);
12437 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012438
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012439 PIPE_CONF_CHECK_I(pipe_src_w);
12440 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012441
Daniel Vettere2ff2d42015-07-15 14:15:50 +020012442 PIPE_CONF_CHECK_I(gmch_pfit.control);
12443 /* pfit ratios are autocomputed by the hw on gen4+ */
12444 if (INTEL_INFO(dev)->gen < 4)
12445 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12446 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
Daniel Vetter99535992014-04-13 12:00:33 +020012447
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012448 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12449 if (current_config->pch_pfit.enabled) {
12450 PIPE_CONF_CHECK_I(pch_pfit.pos);
12451 PIPE_CONF_CHECK_I(pch_pfit.size);
12452 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012453
Chandra Kondurua1b22782015-04-07 15:28:45 -070012454 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12455
Jesse Barnese59150d2014-01-07 13:30:45 -080012456 /* BDW+ don't expose a synchronous way to read the state */
12457 if (IS_HASWELL(dev))
12458 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012459
Ville Syrjälä282740f2013-09-04 18:30:03 +030012460 PIPE_CONF_CHECK_I(double_wide);
12461
Daniel Vetter26804af2014-06-25 22:01:55 +030012462 PIPE_CONF_CHECK_X(ddi_pll_sel);
12463
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012464 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012465 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012466 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012467 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12468 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012469 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012470 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12471 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12472 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012473
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012474 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12475 PIPE_CONF_CHECK_I(pipe_bpp);
12476
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012477 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012478 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012479
Daniel Vetter66e985c2013-06-05 13:34:20 +020012480#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012481#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012482#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012483#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012484#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012485#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012486#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012487
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012488 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012489}
12490
Damien Lespiau08db6652014-11-04 17:06:52 +000012491static void check_wm_state(struct drm_device *dev)
12492{
12493 struct drm_i915_private *dev_priv = dev->dev_private;
12494 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12495 struct intel_crtc *intel_crtc;
12496 int plane;
12497
12498 if (INTEL_INFO(dev)->gen < 9)
12499 return;
12500
12501 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12502 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12503
12504 for_each_intel_crtc(dev, intel_crtc) {
12505 struct skl_ddb_entry *hw_entry, *sw_entry;
12506 const enum pipe pipe = intel_crtc->pipe;
12507
12508 if (!intel_crtc->active)
12509 continue;
12510
12511 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012512 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012513 hw_entry = &hw_ddb.plane[pipe][plane];
12514 sw_entry = &sw_ddb->plane[pipe][plane];
12515
12516 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12517 continue;
12518
12519 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12520 "(expected (%u,%u), found (%u,%u))\n",
12521 pipe_name(pipe), plane + 1,
12522 sw_entry->start, sw_entry->end,
12523 hw_entry->start, hw_entry->end);
12524 }
12525
12526 /* cursor */
12527 hw_entry = &hw_ddb.cursor[pipe];
12528 sw_entry = &sw_ddb->cursor[pipe];
12529
12530 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12531 continue;
12532
12533 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12534 "(expected (%u,%u), found (%u,%u))\n",
12535 pipe_name(pipe),
12536 sw_entry->start, sw_entry->end,
12537 hw_entry->start, hw_entry->end);
12538 }
12539}
12540
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012541static void
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012542check_connector_state(struct drm_device *dev,
12543 struct drm_atomic_state *old_state)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012544{
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012545 struct drm_connector_state *old_conn_state;
12546 struct drm_connector *connector;
12547 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012548
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012549 for_each_connector_in_state(old_state, connector, old_conn_state, i) {
12550 struct drm_encoder *encoder = connector->encoder;
12551 struct drm_connector_state *state = connector->state;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012552
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012553 /* This also checks the encoder/connector hw state with the
12554 * ->get_hw_state callbacks. */
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012555 intel_connector_check_state(to_intel_connector(connector));
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012556
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012557 I915_STATE_WARN(state->best_encoder != encoder,
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012558 "connector's atomic encoder doesn't match legacy encoder\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012559 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012560}
12561
12562static void
12563check_encoder_state(struct drm_device *dev)
12564{
12565 struct intel_encoder *encoder;
12566 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012567
Damien Lespiaub2784e12014-08-05 11:29:37 +010012568 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012569 bool enabled = false;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012570 enum pipe pipe;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012571
12572 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12573 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012574 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012575
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012576 for_each_intel_connector(dev, connector) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012577 if (connector->base.state->best_encoder != &encoder->base)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012578 continue;
12579 enabled = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012580
12581 I915_STATE_WARN(connector->base.state->crtc !=
12582 encoder->base.crtc,
12583 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012584 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012585
Rob Clarke2c719b2014-12-15 13:56:32 -050012586 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012587 "encoder's enabled state mismatch "
12588 "(expected %i, found %i)\n",
12589 !!encoder->base.crtc, enabled);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012590
12591 if (!encoder->base.crtc) {
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012592 bool active;
12593
12594 active = encoder->get_hw_state(encoder, &pipe);
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012595 I915_STATE_WARN(active,
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012596 "encoder detached but still enabled on pipe %c.\n",
12597 pipe_name(pipe));
Maarten Lankhorst7c60d192015-08-05 12:37:04 +020012598 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012599 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012600}
12601
12602static void
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012603check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012604{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012605 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012606 struct intel_encoder *encoder;
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012607 struct drm_crtc_state *old_crtc_state;
12608 struct drm_crtc *crtc;
12609 int i;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012610
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012611 for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
12612 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12613 struct intel_crtc_state *pipe_config, *sw_config;
Maarten Lankhorst7b89b8d2015-08-05 12:37:03 +020012614 bool active;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012615
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012616 if (!needs_modeset(crtc->state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012617 continue;
12618
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012619 __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
12620 pipe_config = to_intel_crtc_state(old_crtc_state);
12621 memset(pipe_config, 0, sizeof(*pipe_config));
12622 pipe_config->base.crtc = crtc;
12623 pipe_config->base.state = old_state;
12624
12625 DRM_DEBUG_KMS("[CRTC:%d]\n",
12626 crtc->base.id);
12627
12628 active = dev_priv->display.get_pipe_config(intel_crtc,
12629 pipe_config);
12630
12631 /* hw state is inconsistent with the pipe quirk */
12632 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12633 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
12634 active = crtc->state->active;
12635
12636 I915_STATE_WARN(crtc->state->active != active,
12637 "crtc active state doesn't match with hw state "
12638 "(expected %i, found %i)\n", crtc->state->active, active);
12639
12640 I915_STATE_WARN(intel_crtc->active != crtc->state->active,
12641 "transitional active state does not match atomic hw state "
12642 "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
12643
12644 for_each_encoder_on_crtc(dev, crtc, encoder) {
12645 enum pipe pipe;
12646
12647 active = encoder->get_hw_state(encoder, &pipe);
12648 I915_STATE_WARN(active != crtc->state->active,
12649 "[ENCODER:%i] active %i with crtc active %i\n",
12650 encoder->base.base.id, active, crtc->state->active);
12651
12652 I915_STATE_WARN(active && intel_crtc->pipe != pipe,
12653 "Encoder connected to wrong pipe %c\n",
12654 pipe_name(pipe));
12655
12656 if (active)
12657 encoder->get_config(encoder, pipe_config);
12658 }
12659
12660 if (!crtc->state->active)
12661 continue;
12662
12663 sw_config = to_intel_crtc_state(crtc->state);
12664 if (!intel_pipe_config_compare(dev, sw_config,
12665 pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012666 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012667 intel_dump_pipe_config(intel_crtc, pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012668 "[hw state]");
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012669 intel_dump_pipe_config(intel_crtc, sw_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012670 "[sw state]");
12671 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012672 }
12673}
12674
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012675static void
12676check_shared_dpll_state(struct drm_device *dev)
12677{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012678 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012679 struct intel_crtc *crtc;
12680 struct intel_dpll_hw_state dpll_hw_state;
12681 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012682
12683 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12684 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12685 int enabled_crtcs = 0, active_crtcs = 0;
12686 bool active;
12687
12688 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12689
12690 DRM_DEBUG_KMS("%s\n", pll->name);
12691
12692 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12693
Rob Clarke2c719b2014-12-15 13:56:32 -050012694 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012695 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012696 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012697 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012698 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012699 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012700 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012701 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012702 "pll on state mismatch (expected %i, found %i)\n",
12703 pll->on, active);
12704
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012705 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012706 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012707 enabled_crtcs++;
12708 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12709 active_crtcs++;
12710 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012711 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012712 "pll active crtcs mismatch (expected %i, found %i)\n",
12713 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012714 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012715 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012716 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012717
Rob Clarke2c719b2014-12-15 13:56:32 -050012718 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012719 sizeof(dpll_hw_state)),
12720 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012721 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012722}
12723
Maarten Lankhorstee165b12015-08-05 12:37:00 +020012724static void
12725intel_modeset_check_state(struct drm_device *dev,
12726 struct drm_atomic_state *old_state)
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012727{
Damien Lespiau08db6652014-11-04 17:06:52 +000012728 check_wm_state(dev);
Maarten Lankhorst35dd3c62015-08-06 13:49:22 +020012729 check_connector_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012730 check_encoder_state(dev);
Maarten Lankhorst4d20cd82015-08-05 12:37:05 +020012731 check_crtc_state(dev, old_state);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012732 check_shared_dpll_state(dev);
12733}
12734
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012735void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012736 int dotclock)
12737{
12738 /*
12739 * FDI already provided one idea for the dotclock.
12740 * Yell if the encoder disagrees.
12741 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012742 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012743 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012744 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012745}
12746
Ville Syrjälä80715b22014-05-15 20:23:23 +030012747static void update_scanline_offset(struct intel_crtc *crtc)
12748{
12749 struct drm_device *dev = crtc->base.dev;
12750
12751 /*
12752 * The scanline counter increments at the leading edge of hsync.
12753 *
12754 * On most platforms it starts counting from vtotal-1 on the
12755 * first active line. That means the scanline counter value is
12756 * always one less than what we would expect. Ie. just after
12757 * start of vblank, which also occurs at start of hsync (on the
12758 * last active line), the scanline counter will read vblank_start-1.
12759 *
12760 * On gen2 the scanline counter starts counting from 1 instead
12761 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12762 * to keep the value positive), instead of adding one.
12763 *
12764 * On HSW+ the behaviour of the scanline counter depends on the output
12765 * type. For DP ports it behaves like most other platforms, but on HDMI
12766 * there's an extra 1 line difference. So we need to add two instead of
12767 * one to the value.
12768 */
12769 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012770 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012771 int vtotal;
12772
12773 vtotal = mode->crtc_vtotal;
12774 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12775 vtotal /= 2;
12776
12777 crtc->scanline_offset = vtotal - 1;
12778 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012779 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012780 crtc->scanline_offset = 2;
12781 } else
12782 crtc->scanline_offset = 1;
12783}
12784
Maarten Lankhorstad421372015-06-15 12:33:42 +020012785static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012786{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012787 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012788 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012789 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012790 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012791 struct intel_crtc_state *intel_crtc_state;
12792 struct drm_crtc *crtc;
12793 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012794 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012795
12796 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012797 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012798
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012799 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012800 int dpll;
12801
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012802 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012803 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012804 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012805
Maarten Lankhorstad421372015-06-15 12:33:42 +020012806 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012807 continue;
12808
Maarten Lankhorstad421372015-06-15 12:33:42 +020012809 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012810
Maarten Lankhorstad421372015-06-15 12:33:42 +020012811 if (!shared_dpll)
12812 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12813
12814 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012815 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012816}
12817
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012818/*
12819 * This implements the workaround described in the "notes" section of the mode
12820 * set sequence documentation. When going from no pipes or single pipe to
12821 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12822 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12823 */
12824static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12825{
12826 struct drm_crtc_state *crtc_state;
12827 struct intel_crtc *intel_crtc;
12828 struct drm_crtc *crtc;
12829 struct intel_crtc_state *first_crtc_state = NULL;
12830 struct intel_crtc_state *other_crtc_state = NULL;
12831 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12832 int i;
12833
12834 /* look at all crtc's that are going to be enabled in during modeset */
12835 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12836 intel_crtc = to_intel_crtc(crtc);
12837
12838 if (!crtc_state->active || !needs_modeset(crtc_state))
12839 continue;
12840
12841 if (first_crtc_state) {
12842 other_crtc_state = to_intel_crtc_state(crtc_state);
12843 break;
12844 } else {
12845 first_crtc_state = to_intel_crtc_state(crtc_state);
12846 first_pipe = intel_crtc->pipe;
12847 }
12848 }
12849
12850 /* No workaround needed? */
12851 if (!first_crtc_state)
12852 return 0;
12853
12854 /* w/a possibly needed, check how many crtc's are already enabled. */
12855 for_each_intel_crtc(state->dev, intel_crtc) {
12856 struct intel_crtc_state *pipe_config;
12857
12858 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12859 if (IS_ERR(pipe_config))
12860 return PTR_ERR(pipe_config);
12861
12862 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12863
12864 if (!pipe_config->base.active ||
12865 needs_modeset(&pipe_config->base))
12866 continue;
12867
12868 /* 2 or more enabled crtcs means no need for w/a */
12869 if (enabled_pipe != INVALID_PIPE)
12870 return 0;
12871
12872 enabled_pipe = intel_crtc->pipe;
12873 }
12874
12875 if (enabled_pipe != INVALID_PIPE)
12876 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12877 else if (other_crtc_state)
12878 other_crtc_state->hsw_workaround_pipe = first_pipe;
12879
12880 return 0;
12881}
12882
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012883static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12884{
12885 struct drm_crtc *crtc;
12886 struct drm_crtc_state *crtc_state;
12887 int ret = 0;
12888
12889 /* add all active pipes to the state */
12890 for_each_crtc(state->dev, crtc) {
12891 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12892 if (IS_ERR(crtc_state))
12893 return PTR_ERR(crtc_state);
12894
12895 if (!crtc_state->active || needs_modeset(crtc_state))
12896 continue;
12897
12898 crtc_state->mode_changed = true;
12899
12900 ret = drm_atomic_add_affected_connectors(state, crtc);
12901 if (ret)
12902 break;
12903
12904 ret = drm_atomic_add_affected_planes(state, crtc);
12905 if (ret)
12906 break;
12907 }
12908
12909 return ret;
12910}
12911
12912
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012913static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012914{
12915 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012916 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012917 int ret;
12918
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012919 if (!check_digital_port_conflicts(state)) {
12920 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12921 return -EINVAL;
12922 }
12923
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012924 /*
12925 * See if the config requires any additional preparation, e.g.
12926 * to adjust global state with pipes off. We need to do this
12927 * here so we can get the modeset_pipe updated config for the new
12928 * mode set on this crtc. For other crtcs we need to use the
12929 * adjusted_mode bits in the crtc directly.
12930 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012931 if (dev_priv->display.modeset_calc_cdclk) {
12932 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012933
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012934 ret = dev_priv->display.modeset_calc_cdclk(state);
12935
12936 cdclk = to_intel_atomic_state(state)->cdclk;
12937 if (!ret && cdclk != dev_priv->cdclk_freq)
12938 ret = intel_modeset_all_pipes(state);
12939
12940 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012941 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020012942 } else
12943 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012944
Maarten Lankhorstad421372015-06-15 12:33:42 +020012945 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012946
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012947 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020012948 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012949
Maarten Lankhorstad421372015-06-15 12:33:42 +020012950 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012951}
12952
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012953/**
12954 * intel_atomic_check - validate state object
12955 * @dev: drm device
12956 * @state: state to validate
12957 */
12958static int intel_atomic_check(struct drm_device *dev,
12959 struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012960{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012961 struct drm_crtc *crtc;
12962 struct drm_crtc_state *crtc_state;
12963 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012964 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012965
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020012966 ret = drm_atomic_helper_check_modeset(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012967 if (ret)
12968 return ret;
12969
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012970 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012971 struct intel_crtc_state *pipe_config =
12972 to_intel_crtc_state(crtc_state);
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012973
12974 /* Catch I915_MODE_FLAG_INHERITED */
12975 if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
12976 crtc_state->mode_changed = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012977
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012978 if (!crtc_state->enable) {
12979 if (needs_modeset(crtc_state))
12980 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012981 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020012982 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012983
Daniel Vetter26495482015-07-15 14:15:52 +020012984 if (!needs_modeset(crtc_state))
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012985 continue;
12986
Daniel Vetter26495482015-07-15 14:15:52 +020012987 /* FIXME: For only active_changed we shouldn't need to do any
12988 * state recomputation at all. */
12989
Daniel Vetter1ed51de2015-07-15 14:15:51 +020012990 ret = drm_atomic_add_affected_connectors(state, crtc);
12991 if (ret)
12992 return ret;
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012993
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012994 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020012995 if (ret)
12996 return ret;
12997
Daniel Vetter26495482015-07-15 14:15:52 +020012998 if (i915.fastboot &&
12999 intel_pipe_config_compare(state->dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013000 to_intel_crtc_state(crtc->state),
Daniel Vetter1ed51de2015-07-15 14:15:51 +020013001 pipe_config, true)) {
Daniel Vetter26495482015-07-15 14:15:52 +020013002 crtc_state->mode_changed = false;
13003 }
13004
13005 if (needs_modeset(crtc_state)) {
13006 any_ms = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013007
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013008 ret = drm_atomic_add_affected_planes(state, crtc);
13009 if (ret)
13010 return ret;
13011 }
13012
Daniel Vetter26495482015-07-15 14:15:52 +020013013 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
13014 needs_modeset(crtc_state) ?
13015 "[modeset]" : "[fastset]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013016 }
13017
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013018 if (any_ms) {
13019 ret = intel_modeset_checks(state);
13020
13021 if (ret)
13022 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013023 } else
13024 to_intel_atomic_state(state)->cdclk =
13025 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013026
13027 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013028}
13029
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013030/**
13031 * intel_atomic_commit - commit validated state object
13032 * @dev: DRM device
13033 * @state: the top-level driver state object
13034 * @async: asynchronous commit
13035 *
13036 * This function commits a top-level state object that has been validated
13037 * with drm_atomic_helper_check().
13038 *
13039 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
13040 * we can only handle plane-related operations and do not yet support
13041 * asynchronous commit.
13042 *
13043 * RETURNS
13044 * Zero for success or -errno.
13045 */
13046static int intel_atomic_commit(struct drm_device *dev,
13047 struct drm_atomic_state *state,
13048 bool async)
Daniel Vettera6778b32012-07-02 09:56:42 +020013049{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013050 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013051 struct drm_crtc *crtc;
13052 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013053 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013054 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013055 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013056
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013057 if (async) {
13058 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
13059 return -EINVAL;
13060 }
13061
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013062 ret = drm_atomic_helper_prepare_planes(dev, state);
13063 if (ret)
13064 return ret;
13065
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013066 drm_atomic_helper_swap_state(dev, state);
13067
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013068 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013069 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13070
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013071 if (!needs_modeset(crtc->state))
13072 continue;
13073
13074 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013075 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013076
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013077 if (crtc_state->active) {
13078 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13079 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013080 intel_crtc->active = false;
13081 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013082 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013083 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013084
Daniel Vetterea9d7582012-07-10 10:42:52 +020013085 /* Only after disabling all output pipelines that will be changed can we
13086 * update the the output configuration. */
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013087 intel_modeset_update_crtc_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013088
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013089 if (any_ms) {
13090 intel_shared_dpll_commit(state);
13091
13092 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013093 modeset_update_crtc_power_domains(state);
Maarten Lankhorst4740b0f2015-08-05 12:37:10 +020013094 }
Daniel Vetter47fab732012-10-26 10:58:18 +020013095
Daniel Vettera6778b32012-07-02 09:56:42 +020013096 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013097 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13099 bool modeset = needs_modeset(crtc->state);
13100
13101 if (modeset && crtc->state->active) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013102 update_scanline_offset(to_intel_crtc(crtc));
13103 dev_priv->display.crtc_enable(crtc);
13104 }
13105
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013106 if (!modeset)
13107 intel_pre_plane_update(intel_crtc);
13108
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013109 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Maarten Lankhorstf6ac4b22015-07-13 16:30:31 +020013110 intel_post_plane_update(intel_crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013111 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013112
Daniel Vettera6778b32012-07-02 09:56:42 +020013113 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013114
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013115 drm_atomic_helper_wait_for_vblanks(dev, state);
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013116 drm_atomic_helper_cleanup_planes(dev, state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013117
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013118 if (any_ms)
Maarten Lankhorstee165b12015-08-05 12:37:00 +020013119 intel_modeset_check_state(dev, state);
13120
13121 drm_atomic_state_free(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013122
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013123 return 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013124}
13125
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013126void intel_crtc_restore_mode(struct drm_crtc *crtc)
13127{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013128 struct drm_device *dev = crtc->dev;
13129 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013130 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013131 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013132
13133 state = drm_atomic_state_alloc(dev);
13134 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013135 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013136 crtc->base.id);
13137 return;
13138 }
13139
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013140 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013141
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013142retry:
13143 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13144 ret = PTR_ERR_OR_ZERO(crtc_state);
13145 if (!ret) {
13146 if (!crtc_state->active)
13147 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013148
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013149 crtc_state->mode_changed = true;
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013150 ret = drm_atomic_commit(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013151 }
13152
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013153 if (ret == -EDEADLK) {
13154 drm_atomic_state_clear(state);
13155 drm_modeset_backoff(state->acquire_ctx);
13156 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013157 }
13158
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013159 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013160out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013161 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013162}
13163
Daniel Vetter25c5b262012-07-08 22:08:04 +020013164#undef for_each_intel_crtc_masked
13165
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013166static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013167 .gamma_set = intel_crtc_gamma_set,
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020013168 .set_config = drm_atomic_helper_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013169 .destroy = intel_crtc_destroy,
13170 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013171 .atomic_duplicate_state = intel_crtc_duplicate_state,
13172 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013173};
13174
Daniel Vetter53589012013-06-05 13:34:16 +020013175static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13176 struct intel_shared_dpll *pll,
13177 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013178{
Daniel Vetter53589012013-06-05 13:34:16 +020013179 uint32_t val;
13180
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013181 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013182 return false;
13183
Daniel Vetter53589012013-06-05 13:34:16 +020013184 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013185 hw_state->dpll = val;
13186 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13187 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013188
13189 return val & DPLL_VCO_ENABLE;
13190}
13191
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013192static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13193 struct intel_shared_dpll *pll)
13194{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013195 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13196 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013197}
13198
Daniel Vettere7b903d2013-06-05 13:34:14 +020013199static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13200 struct intel_shared_dpll *pll)
13201{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013202 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013203 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013204
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013205 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013206
13207 /* Wait for the clocks to stabilize. */
13208 POSTING_READ(PCH_DPLL(pll->id));
13209 udelay(150);
13210
13211 /* The pixel multiplier can only be updated once the
13212 * DPLL is enabled and the clocks are stable.
13213 *
13214 * So write it again.
13215 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013216 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013217 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013218 udelay(200);
13219}
13220
13221static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13222 struct intel_shared_dpll *pll)
13223{
13224 struct drm_device *dev = dev_priv->dev;
13225 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013226
13227 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013228 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013229 if (intel_crtc_to_shared_dpll(crtc) == pll)
13230 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13231 }
13232
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013233 I915_WRITE(PCH_DPLL(pll->id), 0);
13234 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013235 udelay(200);
13236}
13237
Daniel Vetter46edb022013-06-05 13:34:12 +020013238static char *ibx_pch_dpll_names[] = {
13239 "PCH DPLL A",
13240 "PCH DPLL B",
13241};
13242
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013243static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013244{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013245 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013246 int i;
13247
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013248 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013249
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013250 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013251 dev_priv->shared_dplls[i].id = i;
13252 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013253 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013254 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13255 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013256 dev_priv->shared_dplls[i].get_hw_state =
13257 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013258 }
13259}
13260
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013261static void intel_shared_dpll_init(struct drm_device *dev)
13262{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013263 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013264
Ville Syrjäläb6283052015-06-03 15:45:07 +030013265 intel_update_cdclk(dev);
13266
Daniel Vetter9cd86932014-06-25 22:01:57 +030013267 if (HAS_DDI(dev))
13268 intel_ddi_pll_init(dev);
13269 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013270 ibx_pch_dpll_init(dev);
13271 else
13272 dev_priv->num_shared_dpll = 0;
13273
13274 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013275}
13276
Matt Roper6beb8c232014-12-01 15:40:14 -080013277/**
13278 * intel_prepare_plane_fb - Prepare fb for usage on plane
13279 * @plane: drm plane to prepare for
13280 * @fb: framebuffer to prepare for presentation
13281 *
13282 * Prepares a framebuffer for usage on a display plane. Generally this
13283 * involves pinning the underlying object and updating the frontbuffer tracking
13284 * bits. Some older platforms need special physical address handling for
13285 * cursor planes.
13286 *
13287 * Returns 0 on success, negative error code on failure.
13288 */
13289int
13290intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013291 struct drm_framebuffer *fb,
13292 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013293{
13294 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013295 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013296 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13297 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013298 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013299
Matt Roperea2c67b2014-12-23 10:41:52 -080013300 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013301 return 0;
13302
Matt Roper4c345742014-07-09 16:22:10 -070013303 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013304
Matt Roper6beb8c232014-12-01 15:40:14 -080013305 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13306 INTEL_INFO(dev)->cursor_needs_physical) {
13307 int align = IS_I830(dev) ? 16 * 1024 : 256;
13308 ret = i915_gem_object_attach_phys(obj, align);
13309 if (ret)
13310 DRM_DEBUG_KMS("failed to attach phys object\n");
13311 } else {
John Harrison91af1272015-06-18 13:14:56 +010013312 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013313 }
13314
13315 if (ret == 0)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013316 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Matt Roper6beb8c232014-12-01 15:40:14 -080013317
13318 mutex_unlock(&dev->struct_mutex);
13319
13320 return ret;
13321}
13322
Matt Roper38f3ce32014-12-02 07:45:25 -080013323/**
13324 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13325 * @plane: drm plane to clean up for
13326 * @fb: old framebuffer that was on plane
13327 *
13328 * Cleans up a framebuffer that has just been removed from a plane.
13329 */
13330void
13331intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013332 struct drm_framebuffer *fb,
13333 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013334{
13335 struct drm_device *dev = plane->dev;
13336 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13337
13338 if (WARN_ON(!obj))
13339 return;
13340
13341 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13342 !INTEL_INFO(dev)->cursor_needs_physical) {
13343 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013344 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013345 mutex_unlock(&dev->struct_mutex);
13346 }
Matt Roper465c1202014-05-29 08:06:54 -070013347}
13348
Chandra Konduru6156a452015-04-27 13:48:39 -070013349int
13350skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13351{
13352 int max_scale;
13353 struct drm_device *dev;
13354 struct drm_i915_private *dev_priv;
13355 int crtc_clock, cdclk;
13356
13357 if (!intel_crtc || !crtc_state)
13358 return DRM_PLANE_HELPER_NO_SCALING;
13359
13360 dev = intel_crtc->base.dev;
13361 dev_priv = dev->dev_private;
13362 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013363 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013364
13365 if (!crtc_clock || !cdclk)
13366 return DRM_PLANE_HELPER_NO_SCALING;
13367
13368 /*
13369 * skl max scale is lower of:
13370 * close to 3 but not 3, -1 is for that purpose
13371 * or
13372 * cdclk/crtc_clock
13373 */
13374 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13375
13376 return max_scale;
13377}
13378
Matt Roper465c1202014-05-29 08:06:54 -070013379static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013380intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013381 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013382 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013383{
Matt Roper2b875c22014-12-01 15:40:13 -080013384 struct drm_crtc *crtc = state->base.crtc;
13385 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013386 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013387 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13388 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013389
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013390 /* use scaler when colorkey is not required */
13391 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013392 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013393 min_scale = 1;
13394 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013395 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013396 }
Sonika Jindald8106362015-04-10 14:37:28 +053013397
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013398 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13399 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013400 min_scale, max_scale,
13401 can_position, true,
13402 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013403}
13404
Gustavo Padovan14af2932014-10-24 14:51:31 +010013405static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013406intel_commit_primary_plane(struct drm_plane *plane,
13407 struct intel_plane_state *state)
13408{
Matt Roper2b875c22014-12-01 15:40:13 -080013409 struct drm_crtc *crtc = state->base.crtc;
13410 struct drm_framebuffer *fb = state->base.fb;
13411 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013412 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013413 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013414 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013415
Matt Roperea2c67b2014-12-23 10:41:52 -080013416 crtc = crtc ? crtc : plane->crtc;
13417 intel_crtc = to_intel_crtc(crtc);
13418
Matt Ropercf4c7c12014-12-04 10:27:42 -080013419 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013420 crtc->x = src->x1 >> 16;
13421 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013422
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013423 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013424 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013425
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013426 if (state->visible)
13427 /* FIXME: kill this fastboot hack */
13428 intel_update_pipe_size(intel_crtc);
13429
13430 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013431}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013432
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013433static void
13434intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013435 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013436{
13437 struct drm_device *dev = plane->dev;
13438 struct drm_i915_private *dev_priv = dev->dev_private;
13439
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013440 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13441}
13442
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013443static void intel_begin_crtc_commit(struct drm_crtc *crtc,
13444 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013445{
13446 struct drm_device *dev = crtc->dev;
Matt Roper32b7eee2014-12-24 07:59:06 -080013447 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013448
Ville Syrjäläf015c552015-06-24 22:00:02 +030013449 if (intel_crtc->atomic.update_wm_pre)
Matt Roper32b7eee2014-12-24 07:59:06 -080013450 intel_update_watermarks(crtc);
13451
Matt Roperc34c9ee2014-12-23 10:41:50 -080013452 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013453 if (crtc->state->active)
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020013454 intel_pipe_update_start(intel_crtc);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013455
13456 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13457 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013458}
13459
Maarten Lankhorst613d2b22015-07-21 13:28:58 +020013460static void intel_finish_crtc_commit(struct drm_crtc *crtc,
13461 struct drm_crtc_state *old_crtc_state)
Matt Roper32b7eee2014-12-24 07:59:06 -080013462{
Matt Roper32b7eee2014-12-24 07:59:06 -080013463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013464
Maarten Lankhorst8f539a832015-07-13 16:30:32 +020013465 if (crtc->state->active)
Maarten Lankhorst34e0adb2015-08-31 13:04:25 +020013466 intel_pipe_update_end(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013467}
13468
Matt Ropercf4c7c12014-12-04 10:27:42 -080013469/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013470 * intel_plane_destroy - destroy a plane
13471 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013472 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013473 * Common destruction function for all types of planes (primary, cursor,
13474 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013475 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013476void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013477{
13478 struct intel_plane *intel_plane = to_intel_plane(plane);
13479 drm_plane_cleanup(plane);
13480 kfree(intel_plane);
13481}
13482
Matt Roper65a3fea2015-01-21 16:35:42 -080013483const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013484 .update_plane = drm_atomic_helper_update_plane,
13485 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013486 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013487 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013488 .atomic_get_property = intel_plane_atomic_get_property,
13489 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013490 .atomic_duplicate_state = intel_plane_duplicate_state,
13491 .atomic_destroy_state = intel_plane_destroy_state,
13492
Matt Roper465c1202014-05-29 08:06:54 -070013493};
13494
13495static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13496 int pipe)
13497{
13498 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013499 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013500 const uint32_t *intel_primary_formats;
Thierry Reding45e37432015-08-12 16:54:28 +020013501 unsigned int num_formats;
Matt Roper465c1202014-05-29 08:06:54 -070013502
13503 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13504 if (primary == NULL)
13505 return NULL;
13506
Matt Roper8e7d6882015-01-21 16:35:41 -080013507 state = intel_create_plane_state(&primary->base);
13508 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013509 kfree(primary);
13510 return NULL;
13511 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013512 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013513
Matt Roper465c1202014-05-29 08:06:54 -070013514 primary->can_scale = false;
13515 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013516 if (INTEL_INFO(dev)->gen >= 9) {
13517 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013518 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013519 }
Matt Roper465c1202014-05-29 08:06:54 -070013520 primary->pipe = pipe;
13521 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013522 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013523 primary->check_plane = intel_check_primary_plane;
13524 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013525 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013526 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13527 primary->plane = !pipe;
13528
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013529 if (INTEL_INFO(dev)->gen >= 9) {
13530 intel_primary_formats = skl_primary_formats;
13531 num_formats = ARRAY_SIZE(skl_primary_formats);
13532 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013533 intel_primary_formats = i965_primary_formats;
13534 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013535 } else {
13536 intel_primary_formats = i8xx_primary_formats;
13537 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013538 }
13539
13540 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013541 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013542 intel_primary_formats, num_formats,
13543 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013544
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013545 if (INTEL_INFO(dev)->gen >= 4)
13546 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013547
Matt Roperea2c67b2014-12-23 10:41:52 -080013548 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13549
Matt Roper465c1202014-05-29 08:06:54 -070013550 return &primary->base;
13551}
13552
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013553void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13554{
13555 if (!dev->mode_config.rotation_property) {
13556 unsigned long flags = BIT(DRM_ROTATE_0) |
13557 BIT(DRM_ROTATE_180);
13558
13559 if (INTEL_INFO(dev)->gen >= 9)
13560 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13561
13562 dev->mode_config.rotation_property =
13563 drm_mode_create_rotation_property(dev, flags);
13564 }
13565 if (dev->mode_config.rotation_property)
13566 drm_object_attach_property(&plane->base.base,
13567 dev->mode_config.rotation_property,
13568 plane->base.state->rotation);
13569}
13570
Matt Roper3d7d6512014-06-10 08:28:13 -070013571static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013572intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013573 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013574 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013575{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013576 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013577 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013578 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013579 unsigned stride;
13580 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013581
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013582 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13583 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013584 DRM_PLANE_HELPER_NO_SCALING,
13585 DRM_PLANE_HELPER_NO_SCALING,
13586 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013587 if (ret)
13588 return ret;
13589
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013590 /* if we want to turn off the cursor ignore width and height */
13591 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013592 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013593
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013594 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013595 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013596 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13597 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013598 return -EINVAL;
13599 }
13600
Matt Roperea2c67b2014-12-23 10:41:52 -080013601 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13602 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013603 DRM_DEBUG_KMS("buffer is too small\n");
13604 return -ENOMEM;
13605 }
13606
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013607 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013608 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013609 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013610 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013611
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013612 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013613}
13614
Matt Roperf4a2cf22014-12-01 15:40:12 -080013615static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013616intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013617 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013618{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013619 intel_crtc_update_cursor(crtc, false);
13620}
13621
13622static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013623intel_commit_cursor_plane(struct drm_plane *plane,
13624 struct intel_plane_state *state)
13625{
Matt Roper2b875c22014-12-01 15:40:13 -080013626 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013627 struct drm_device *dev = plane->dev;
13628 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013629 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013630 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013631
Matt Roperea2c67b2014-12-23 10:41:52 -080013632 crtc = crtc ? crtc : plane->crtc;
13633 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013634
Matt Roperea2c67b2014-12-23 10:41:52 -080013635 plane->fb = state->base.fb;
13636 crtc->cursor_x = state->base.crtc_x;
13637 crtc->cursor_y = state->base.crtc_y;
13638
Gustavo Padovana912f122014-12-01 15:40:10 -080013639 if (intel_crtc->cursor_bo == obj)
13640 goto update;
13641
Matt Roperf4a2cf22014-12-01 15:40:12 -080013642 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013643 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013644 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013645 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013646 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013647 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013648
Gustavo Padovana912f122014-12-01 15:40:10 -080013649 intel_crtc->cursor_addr = addr;
13650 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080013651
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013652update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013653 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013654 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013655}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013656
Matt Roper3d7d6512014-06-10 08:28:13 -070013657static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13658 int pipe)
13659{
13660 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013661 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013662
13663 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13664 if (cursor == NULL)
13665 return NULL;
13666
Matt Roper8e7d6882015-01-21 16:35:41 -080013667 state = intel_create_plane_state(&cursor->base);
13668 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013669 kfree(cursor);
13670 return NULL;
13671 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013672 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013673
Matt Roper3d7d6512014-06-10 08:28:13 -070013674 cursor->can_scale = false;
13675 cursor->max_downscale = 1;
13676 cursor->pipe = pipe;
13677 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013678 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013679 cursor->check_plane = intel_check_cursor_plane;
13680 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013681 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013682
13683 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013684 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013685 intel_cursor_formats,
13686 ARRAY_SIZE(intel_cursor_formats),
13687 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013688
13689 if (INTEL_INFO(dev)->gen >= 4) {
13690 if (!dev->mode_config.rotation_property)
13691 dev->mode_config.rotation_property =
13692 drm_mode_create_rotation_property(dev,
13693 BIT(DRM_ROTATE_0) |
13694 BIT(DRM_ROTATE_180));
13695 if (dev->mode_config.rotation_property)
13696 drm_object_attach_property(&cursor->base.base,
13697 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013698 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013699 }
13700
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013701 if (INTEL_INFO(dev)->gen >=9)
13702 state->scaler_id = -1;
13703
Matt Roperea2c67b2014-12-23 10:41:52 -080013704 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13705
Matt Roper3d7d6512014-06-10 08:28:13 -070013706 return &cursor->base;
13707}
13708
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013709static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13710 struct intel_crtc_state *crtc_state)
13711{
13712 int i;
13713 struct intel_scaler *intel_scaler;
13714 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13715
13716 for (i = 0; i < intel_crtc->num_scalers; i++) {
13717 intel_scaler = &scaler_state->scalers[i];
13718 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013719 intel_scaler->mode = PS_SCALER_MODE_DYN;
13720 }
13721
13722 scaler_state->scaler_id = -1;
13723}
13724
Hannes Ederb358d0a2008-12-18 21:18:47 +010013725static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013726{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013727 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013728 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013729 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013730 struct drm_plane *primary = NULL;
13731 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013732 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013733
Daniel Vetter955382f2013-09-19 14:05:45 +020013734 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013735 if (intel_crtc == NULL)
13736 return;
13737
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013738 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13739 if (!crtc_state)
13740 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013741 intel_crtc->config = crtc_state;
13742 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013743 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013744
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013745 /* initialize shared scalers */
13746 if (INTEL_INFO(dev)->gen >= 9) {
13747 if (pipe == PIPE_C)
13748 intel_crtc->num_scalers = 1;
13749 else
13750 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13751
13752 skl_init_scalers(dev, intel_crtc, crtc_state);
13753 }
13754
Matt Roper465c1202014-05-29 08:06:54 -070013755 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013756 if (!primary)
13757 goto fail;
13758
13759 cursor = intel_cursor_plane_create(dev, pipe);
13760 if (!cursor)
13761 goto fail;
13762
Matt Roper465c1202014-05-29 08:06:54 -070013763 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013764 cursor, &intel_crtc_funcs);
13765 if (ret)
13766 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013767
13768 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013769 for (i = 0; i < 256; i++) {
13770 intel_crtc->lut_r[i] = i;
13771 intel_crtc->lut_g[i] = i;
13772 intel_crtc->lut_b[i] = i;
13773 }
13774
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013775 /*
13776 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013777 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013778 */
Jesse Barnes80824002009-09-10 15:28:06 -070013779 intel_crtc->pipe = pipe;
13780 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013781 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013782 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013783 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013784 }
13785
Chris Wilson4b0e3332014-05-30 16:35:26 +030013786 intel_crtc->cursor_base = ~0;
13787 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013788 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013789
Ville Syrjälä852eb002015-06-24 22:00:07 +030013790 intel_crtc->wm.cxsr_allowed = true;
13791
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013792 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13793 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13794 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13795 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13796
Jesse Barnes79e53942008-11-07 14:24:08 -080013797 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013798
13799 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013800 return;
13801
13802fail:
13803 if (primary)
13804 drm_plane_cleanup(primary);
13805 if (cursor)
13806 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013807 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013808 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013809}
13810
Jesse Barnes752aa882013-10-31 18:55:49 +020013811enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13812{
13813 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013814 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013815
Rob Clark51fd3712013-11-19 12:10:12 -050013816 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013817
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013818 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013819 return INVALID_PIPE;
13820
13821 return to_intel_crtc(encoder->crtc)->pipe;
13822}
13823
Carl Worth08d7b3d2009-04-29 14:43:54 -070013824int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013825 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013826{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013827 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013828 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013829 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013830
Rob Clark7707e652014-07-17 23:30:04 -040013831 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013832
Rob Clark7707e652014-07-17 23:30:04 -040013833 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013834 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013835 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013836 }
13837
Rob Clark7707e652014-07-17 23:30:04 -040013838 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013839 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013840
Daniel Vetterc05422d2009-08-11 16:05:30 +020013841 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013842}
13843
Daniel Vetter66a92782012-07-12 20:08:18 +020013844static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013845{
Daniel Vetter66a92782012-07-12 20:08:18 +020013846 struct drm_device *dev = encoder->base.dev;
13847 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013848 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013849 int entry = 0;
13850
Damien Lespiaub2784e12014-08-05 11:29:37 +010013851 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013852 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013853 index_mask |= (1 << entry);
13854
Jesse Barnes79e53942008-11-07 14:24:08 -080013855 entry++;
13856 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013857
Jesse Barnes79e53942008-11-07 14:24:08 -080013858 return index_mask;
13859}
13860
Chris Wilson4d302442010-12-14 19:21:29 +000013861static bool has_edp_a(struct drm_device *dev)
13862{
13863 struct drm_i915_private *dev_priv = dev->dev_private;
13864
13865 if (!IS_MOBILE(dev))
13866 return false;
13867
13868 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13869 return false;
13870
Damien Lespiaue3589902014-02-07 19:12:50 +000013871 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013872 return false;
13873
13874 return true;
13875}
13876
Jesse Barnes84b4e042014-06-25 08:24:29 -070013877static bool intel_crt_present(struct drm_device *dev)
13878{
13879 struct drm_i915_private *dev_priv = dev->dev_private;
13880
Damien Lespiau884497e2013-12-03 13:56:23 +000013881 if (INTEL_INFO(dev)->gen >= 9)
13882 return false;
13883
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013884 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013885 return false;
13886
13887 if (IS_CHERRYVIEW(dev))
13888 return false;
13889
13890 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13891 return false;
13892
13893 return true;
13894}
13895
Jesse Barnes79e53942008-11-07 14:24:08 -080013896static void intel_setup_outputs(struct drm_device *dev)
13897{
Eric Anholt725e30a2009-01-22 13:01:02 -080013898 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013899 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013900 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013901
Daniel Vetterc9093352013-06-06 22:22:47 +020013902 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013903
Jesse Barnes84b4e042014-06-25 08:24:29 -070013904 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013905 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013906
Vandana Kannanc776eb22014-08-19 12:05:01 +053013907 if (IS_BROXTON(dev)) {
13908 /*
13909 * FIXME: Broxton doesn't support port detection via the
13910 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13911 * detect the ports.
13912 */
13913 intel_ddi_init(dev, PORT_A);
13914 intel_ddi_init(dev, PORT_B);
13915 intel_ddi_init(dev, PORT_C);
13916 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013917 int found;
13918
Jesse Barnesde31fac2015-03-06 15:53:32 -080013919 /*
13920 * Haswell uses DDI functions to detect digital outputs.
13921 * On SKL pre-D0 the strap isn't connected, so we assume
13922 * it's there.
13923 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013924 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013925 /* WaIgnoreDDIAStrap: skl */
Jani Nikula5a2376d2015-08-14 10:53:17 +030013926 if (found || IS_SKYLAKE(dev))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013927 intel_ddi_init(dev, PORT_A);
13928
13929 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13930 * register */
13931 found = I915_READ(SFUSE_STRAP);
13932
13933 if (found & SFUSE_STRAP_DDIB_DETECTED)
13934 intel_ddi_init(dev, PORT_B);
13935 if (found & SFUSE_STRAP_DDIC_DETECTED)
13936 intel_ddi_init(dev, PORT_C);
13937 if (found & SFUSE_STRAP_DDID_DETECTED)
13938 intel_ddi_init(dev, PORT_D);
Rodrigo Vivi2800e4c2015-08-07 17:35:21 -070013939 /*
13940 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
13941 */
13942 if (IS_SKYLAKE(dev) &&
13943 (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
13944 dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
13945 dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
13946 intel_ddi_init(dev, PORT_E);
13947
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013948 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013949 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013950 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013951
13952 if (has_edp_a(dev))
13953 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013954
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013955 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013956 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013957 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013958 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013959 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013960 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013961 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013962 }
13963
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013964 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013965 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013966
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013967 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013968 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013969
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013970 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013971 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013972
Daniel Vetter270b3042012-10-27 15:52:05 +020013973 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013974 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013975 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013976 /*
13977 * The DP_DETECTED bit is the latched state of the DDC
13978 * SDA pin at boot. However since eDP doesn't require DDC
13979 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13980 * eDP ports may have been muxed to an alternate function.
13981 * Thus we can't rely on the DP_DETECTED bit alone to detect
13982 * eDP ports. Consult the VBT as well as DP_DETECTED to
13983 * detect eDP ports.
13984 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013985 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13986 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013987 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13988 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013989 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13990 intel_dp_is_edp(dev, PORT_B))
13991 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013992
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013993 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13994 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013995 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13996 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013997 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13998 intel_dp_is_edp(dev, PORT_C))
13999 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014000
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014001 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014002 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014003 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14004 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014005 /* eDP not supported on port D, so don't check VBT */
14006 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14007 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014008 }
14009
Jani Nikula3cfca972013-08-27 15:12:26 +030014010 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014011 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014012 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014013
Paulo Zanonie2debe92013-02-18 19:00:27 -030014014 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014015 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014016 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014017 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014018 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014019 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014020 }
Ma Ling27185ae2009-08-24 13:50:23 +080014021
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014022 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014023 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014024 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014025
14026 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014027
Paulo Zanonie2debe92013-02-18 19:00:27 -030014028 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014029 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014030 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014031 }
Ma Ling27185ae2009-08-24 13:50:23 +080014032
Paulo Zanonie2debe92013-02-18 19:00:27 -030014033 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014034
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014035 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014036 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014037 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014038 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014039 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014040 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014041 }
Ma Ling27185ae2009-08-24 13:50:23 +080014042
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014043 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014044 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014045 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014046 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014047 intel_dvo_init(dev);
14048
Zhenyu Wang103a1962009-11-27 11:44:36 +080014049 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014050 intel_tv_init(dev);
14051
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014052 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014053
Damien Lespiaub2784e12014-08-05 11:29:37 +010014054 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014055 encoder->base.possible_crtcs = encoder->crtc_mask;
14056 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014057 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014058 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014059
Paulo Zanonidde86e22012-12-01 12:04:25 -020014060 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014061
14062 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014063}
14064
14065static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14066{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014067 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014068 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014069
Daniel Vetteref2d6332014-02-10 18:00:38 +010014070 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014071 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014072 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014073 drm_gem_object_unreference(&intel_fb->obj->base);
14074 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014075 kfree(intel_fb);
14076}
14077
14078static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014079 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014080 unsigned int *handle)
14081{
14082 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014083 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014084
Chris Wilson05394f32010-11-08 19:18:58 +000014085 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014086}
14087
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014088static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14089 struct drm_file *file,
14090 unsigned flags, unsigned color,
14091 struct drm_clip_rect *clips,
14092 unsigned num_clips)
14093{
14094 struct drm_device *dev = fb->dev;
14095 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14096 struct drm_i915_gem_object *obj = intel_fb->obj;
14097
14098 mutex_lock(&dev->struct_mutex);
Paulo Zanoni74b4ea12015-07-14 16:29:14 -030014099 intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014100 mutex_unlock(&dev->struct_mutex);
14101
14102 return 0;
14103}
14104
Jesse Barnes79e53942008-11-07 14:24:08 -080014105static const struct drm_framebuffer_funcs intel_fb_funcs = {
14106 .destroy = intel_user_framebuffer_destroy,
14107 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014108 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014109};
14110
Damien Lespiaub3218032015-02-27 11:15:18 +000014111static
14112u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14113 uint32_t pixel_format)
14114{
14115 u32 gen = INTEL_INFO(dev)->gen;
14116
14117 if (gen >= 9) {
14118 /* "The stride in bytes must not exceed the of the size of 8K
14119 * pixels and 32K bytes."
14120 */
14121 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14122 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14123 return 32*1024;
14124 } else if (gen >= 4) {
14125 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14126 return 16*1024;
14127 else
14128 return 32*1024;
14129 } else if (gen >= 3) {
14130 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14131 return 8*1024;
14132 else
14133 return 16*1024;
14134 } else {
14135 /* XXX DSPC is limited to 4k tiled */
14136 return 8*1024;
14137 }
14138}
14139
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014140static int intel_framebuffer_init(struct drm_device *dev,
14141 struct intel_framebuffer *intel_fb,
14142 struct drm_mode_fb_cmd2 *mode_cmd,
14143 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014144{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014145 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014146 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014147 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014148
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014149 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14150
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014151 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14152 /* Enforce that fb modifier and tiling mode match, but only for
14153 * X-tiled. This is needed for FBC. */
14154 if (!!(obj->tiling_mode == I915_TILING_X) !=
14155 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14156 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14157 return -EINVAL;
14158 }
14159 } else {
14160 if (obj->tiling_mode == I915_TILING_X)
14161 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14162 else if (obj->tiling_mode == I915_TILING_Y) {
14163 DRM_DEBUG("No Y tiling for legacy addfb\n");
14164 return -EINVAL;
14165 }
14166 }
14167
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014168 /* Passed in modifier sanity checking. */
14169 switch (mode_cmd->modifier[0]) {
14170 case I915_FORMAT_MOD_Y_TILED:
14171 case I915_FORMAT_MOD_Yf_TILED:
14172 if (INTEL_INFO(dev)->gen < 9) {
14173 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14174 mode_cmd->modifier[0]);
14175 return -EINVAL;
14176 }
14177 case DRM_FORMAT_MOD_NONE:
14178 case I915_FORMAT_MOD_X_TILED:
14179 break;
14180 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014181 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14182 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014183 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014184 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014185
Damien Lespiaub3218032015-02-27 11:15:18 +000014186 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14187 mode_cmd->pixel_format);
14188 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14189 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14190 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014191 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014192 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014193
Damien Lespiaub3218032015-02-27 11:15:18 +000014194 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14195 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014196 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014197 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14198 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014199 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014200 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014201 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014202 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014203
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014204 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014205 mode_cmd->pitches[0] != obj->stride) {
14206 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14207 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014208 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014209 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014210
Ville Syrjälä57779d02012-10-31 17:50:14 +020014211 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014212 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014213 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014214 case DRM_FORMAT_RGB565:
14215 case DRM_FORMAT_XRGB8888:
14216 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014217 break;
14218 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014219 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014220 DRM_DEBUG("unsupported pixel format: %s\n",
14221 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014222 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014223 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014224 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014225 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014226 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14227 DRM_DEBUG("unsupported pixel format: %s\n",
14228 drm_get_format_name(mode_cmd->pixel_format));
14229 return -EINVAL;
14230 }
14231 break;
14232 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014233 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014234 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014235 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014236 DRM_DEBUG("unsupported pixel format: %s\n",
14237 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014238 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014239 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014240 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014241 case DRM_FORMAT_ABGR2101010:
14242 if (!IS_VALLEYVIEW(dev)) {
14243 DRM_DEBUG("unsupported pixel format: %s\n",
14244 drm_get_format_name(mode_cmd->pixel_format));
14245 return -EINVAL;
14246 }
14247 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014248 case DRM_FORMAT_YUYV:
14249 case DRM_FORMAT_UYVY:
14250 case DRM_FORMAT_YVYU:
14251 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014252 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014253 DRM_DEBUG("unsupported pixel format: %s\n",
14254 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014255 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014256 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014257 break;
14258 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014259 DRM_DEBUG("unsupported pixel format: %s\n",
14260 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014261 return -EINVAL;
14262 }
14263
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014264 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14265 if (mode_cmd->offsets[0] != 0)
14266 return -EINVAL;
14267
Damien Lespiauec2c9812015-01-20 12:51:45 +000014268 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014269 mode_cmd->pixel_format,
14270 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014271 /* FIXME drm helper for size checks (especially planar formats)? */
14272 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14273 return -EINVAL;
14274
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014275 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14276 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014277 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014278
Jesse Barnes79e53942008-11-07 14:24:08 -080014279 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14280 if (ret) {
14281 DRM_ERROR("framebuffer init failed %d\n", ret);
14282 return ret;
14283 }
14284
Jesse Barnes79e53942008-11-07 14:24:08 -080014285 return 0;
14286}
14287
Jesse Barnes79e53942008-11-07 14:24:08 -080014288static struct drm_framebuffer *
14289intel_user_framebuffer_create(struct drm_device *dev,
14290 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014291 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014292{
Chris Wilson05394f32010-11-08 19:18:58 +000014293 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014294
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014295 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14296 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014297 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014298 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014299
Chris Wilsond2dff872011-04-19 08:36:26 +010014300 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014301}
14302
Daniel Vetter06957262015-08-10 13:34:08 +020014303#ifndef CONFIG_DRM_FBDEV_EMULATION
Daniel Vetter0632fef2013-10-08 17:44:49 +020014304static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014305{
14306}
14307#endif
14308
Jesse Barnes79e53942008-11-07 14:24:08 -080014309static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014310 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014311 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014312 .atomic_check = intel_atomic_check,
14313 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014314 .atomic_state_alloc = intel_atomic_state_alloc,
14315 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014316};
14317
Jesse Barnese70236a2009-09-21 10:42:27 -070014318/* Set up chip specific display functions */
14319static void intel_init_display(struct drm_device *dev)
14320{
14321 struct drm_i915_private *dev_priv = dev->dev_private;
14322
Daniel Vetteree9300b2013-06-03 22:40:22 +020014323 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14324 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014325 else if (IS_CHERRYVIEW(dev))
14326 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014327 else if (IS_VALLEYVIEW(dev))
14328 dev_priv->display.find_dpll = vlv_find_best_dpll;
14329 else if (IS_PINEVIEW(dev))
14330 dev_priv->display.find_dpll = pnv_find_best_dpll;
14331 else
14332 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14333
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014334 if (INTEL_INFO(dev)->gen >= 9) {
14335 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014336 dev_priv->display.get_initial_plane_config =
14337 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014338 dev_priv->display.crtc_compute_clock =
14339 haswell_crtc_compute_clock;
14340 dev_priv->display.crtc_enable = haswell_crtc_enable;
14341 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014342 dev_priv->display.update_primary_plane =
14343 skylake_update_primary_plane;
14344 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014345 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014346 dev_priv->display.get_initial_plane_config =
14347 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014348 dev_priv->display.crtc_compute_clock =
14349 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014350 dev_priv->display.crtc_enable = haswell_crtc_enable;
14351 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014352 dev_priv->display.update_primary_plane =
14353 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014354 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014355 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014356 dev_priv->display.get_initial_plane_config =
14357 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014358 dev_priv->display.crtc_compute_clock =
14359 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014360 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14361 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014362 dev_priv->display.update_primary_plane =
14363 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014364 } else if (IS_VALLEYVIEW(dev)) {
14365 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014366 dev_priv->display.get_initial_plane_config =
14367 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014368 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014369 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14370 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014371 dev_priv->display.update_primary_plane =
14372 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014373 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014374 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014375 dev_priv->display.get_initial_plane_config =
14376 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014377 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014378 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14379 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014380 dev_priv->display.update_primary_plane =
14381 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014382 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014383
Jesse Barnese70236a2009-09-21 10:42:27 -070014384 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014385 if (IS_SKYLAKE(dev))
14386 dev_priv->display.get_display_clock_speed =
14387 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014388 else if (IS_BROXTON(dev))
14389 dev_priv->display.get_display_clock_speed =
14390 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014391 else if (IS_BROADWELL(dev))
14392 dev_priv->display.get_display_clock_speed =
14393 broadwell_get_display_clock_speed;
14394 else if (IS_HASWELL(dev))
14395 dev_priv->display.get_display_clock_speed =
14396 haswell_get_display_clock_speed;
14397 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014398 dev_priv->display.get_display_clock_speed =
14399 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014400 else if (IS_GEN5(dev))
14401 dev_priv->display.get_display_clock_speed =
14402 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014403 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014404 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014405 dev_priv->display.get_display_clock_speed =
14406 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014407 else if (IS_GM45(dev))
14408 dev_priv->display.get_display_clock_speed =
14409 gm45_get_display_clock_speed;
14410 else if (IS_CRESTLINE(dev))
14411 dev_priv->display.get_display_clock_speed =
14412 i965gm_get_display_clock_speed;
14413 else if (IS_PINEVIEW(dev))
14414 dev_priv->display.get_display_clock_speed =
14415 pnv_get_display_clock_speed;
14416 else if (IS_G33(dev) || IS_G4X(dev))
14417 dev_priv->display.get_display_clock_speed =
14418 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014419 else if (IS_I915G(dev))
14420 dev_priv->display.get_display_clock_speed =
14421 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014422 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014423 dev_priv->display.get_display_clock_speed =
14424 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014425 else if (IS_PINEVIEW(dev))
14426 dev_priv->display.get_display_clock_speed =
14427 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014428 else if (IS_I915GM(dev))
14429 dev_priv->display.get_display_clock_speed =
14430 i915gm_get_display_clock_speed;
14431 else if (IS_I865G(dev))
14432 dev_priv->display.get_display_clock_speed =
14433 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014434 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014435 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014436 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014437 else { /* 830 */
14438 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014439 dev_priv->display.get_display_clock_speed =
14440 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014441 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014442
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014443 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014444 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014445 } else if (IS_GEN6(dev)) {
14446 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014447 } else if (IS_IVYBRIDGE(dev)) {
14448 /* FIXME: detect B0+ stepping and use auto training */
14449 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014450 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014451 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014452 if (IS_BROADWELL(dev)) {
14453 dev_priv->display.modeset_commit_cdclk =
14454 broadwell_modeset_commit_cdclk;
14455 dev_priv->display.modeset_calc_cdclk =
14456 broadwell_modeset_calc_cdclk;
14457 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014458 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014459 dev_priv->display.modeset_commit_cdclk =
14460 valleyview_modeset_commit_cdclk;
14461 dev_priv->display.modeset_calc_cdclk =
14462 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014463 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014464 dev_priv->display.modeset_commit_cdclk =
14465 broxton_modeset_commit_cdclk;
14466 dev_priv->display.modeset_calc_cdclk =
14467 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014468 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014469
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014470 switch (INTEL_INFO(dev)->gen) {
14471 case 2:
14472 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14473 break;
14474
14475 case 3:
14476 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14477 break;
14478
14479 case 4:
14480 case 5:
14481 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14482 break;
14483
14484 case 6:
14485 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14486 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014487 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014488 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014489 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14490 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014491 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014492 /* Drop through - unsupported since execlist only. */
14493 default:
14494 /* Default just returns -ENODEV to indicate unsupported */
14495 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014496 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014497
14498 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014499
14500 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014501}
14502
Jesse Barnesb690e962010-07-19 13:53:12 -070014503/*
14504 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14505 * resume, or other times. This quirk makes sure that's the case for
14506 * affected systems.
14507 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014508static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014509{
14510 struct drm_i915_private *dev_priv = dev->dev_private;
14511
14512 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014513 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014514}
14515
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014516static void quirk_pipeb_force(struct drm_device *dev)
14517{
14518 struct drm_i915_private *dev_priv = dev->dev_private;
14519
14520 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14521 DRM_INFO("applying pipe b force quirk\n");
14522}
14523
Keith Packard435793d2011-07-12 14:56:22 -070014524/*
14525 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14526 */
14527static void quirk_ssc_force_disable(struct drm_device *dev)
14528{
14529 struct drm_i915_private *dev_priv = dev->dev_private;
14530 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014531 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014532}
14533
Carsten Emde4dca20e2012-03-15 15:56:26 +010014534/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014535 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14536 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014537 */
14538static void quirk_invert_brightness(struct drm_device *dev)
14539{
14540 struct drm_i915_private *dev_priv = dev->dev_private;
14541 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014542 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014543}
14544
Scot Doyle9c72cc62014-07-03 23:27:50 +000014545/* Some VBT's incorrectly indicate no backlight is present */
14546static void quirk_backlight_present(struct drm_device *dev)
14547{
14548 struct drm_i915_private *dev_priv = dev->dev_private;
14549 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14550 DRM_INFO("applying backlight present quirk\n");
14551}
14552
Jesse Barnesb690e962010-07-19 13:53:12 -070014553struct intel_quirk {
14554 int device;
14555 int subsystem_vendor;
14556 int subsystem_device;
14557 void (*hook)(struct drm_device *dev);
14558};
14559
Egbert Eich5f85f172012-10-14 15:46:38 +020014560/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14561struct intel_dmi_quirk {
14562 void (*hook)(struct drm_device *dev);
14563 const struct dmi_system_id (*dmi_id_list)[];
14564};
14565
14566static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14567{
14568 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14569 return 1;
14570}
14571
14572static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14573 {
14574 .dmi_id_list = &(const struct dmi_system_id[]) {
14575 {
14576 .callback = intel_dmi_reverse_brightness,
14577 .ident = "NCR Corporation",
14578 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14579 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14580 },
14581 },
14582 { } /* terminating entry */
14583 },
14584 .hook = quirk_invert_brightness,
14585 },
14586};
14587
Ben Widawskyc43b5632012-04-16 14:07:40 -070014588static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014589 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14590 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14591
Jesse Barnesb690e962010-07-19 13:53:12 -070014592 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14593 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14594
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014595 /* 830 needs to leave pipe A & dpll A up */
14596 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14597
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014598 /* 830 needs to leave pipe B & dpll B up */
14599 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14600
Keith Packard435793d2011-07-12 14:56:22 -070014601 /* Lenovo U160 cannot use SSC on LVDS */
14602 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014603
14604 /* Sony Vaio Y cannot use SSC on LVDS */
14605 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014606
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014607 /* Acer Aspire 5734Z must invert backlight brightness */
14608 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14609
14610 /* Acer/eMachines G725 */
14611 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14612
14613 /* Acer/eMachines e725 */
14614 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14615
14616 /* Acer/Packard Bell NCL20 */
14617 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14618
14619 /* Acer Aspire 4736Z */
14620 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014621
14622 /* Acer Aspire 5336 */
14623 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014624
14625 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14626 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014627
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014628 /* Acer C720 Chromebook (Core i3 4005U) */
14629 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14630
jens steinb2a96012014-10-28 20:25:53 +010014631 /* Apple Macbook 2,1 (Core 2 T7400) */
14632 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14633
Scot Doyled4967d82014-07-03 23:27:52 +000014634 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14635 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014636
14637 /* HP Chromebook 14 (Celeron 2955U) */
14638 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014639
14640 /* Dell Chromebook 11 */
14641 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014642};
14643
14644static void intel_init_quirks(struct drm_device *dev)
14645{
14646 struct pci_dev *d = dev->pdev;
14647 int i;
14648
14649 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14650 struct intel_quirk *q = &intel_quirks[i];
14651
14652 if (d->device == q->device &&
14653 (d->subsystem_vendor == q->subsystem_vendor ||
14654 q->subsystem_vendor == PCI_ANY_ID) &&
14655 (d->subsystem_device == q->subsystem_device ||
14656 q->subsystem_device == PCI_ANY_ID))
14657 q->hook(dev);
14658 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014659 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14660 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14661 intel_dmi_quirks[i].hook(dev);
14662 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014663}
14664
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014665/* Disable the VGA plane that we never use */
14666static void i915_disable_vga(struct drm_device *dev)
14667{
14668 struct drm_i915_private *dev_priv = dev->dev_private;
14669 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014670 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014671
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014672 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014673 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014674 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014675 sr1 = inb(VGA_SR_DATA);
14676 outb(sr1 | 1<<5, VGA_SR_DATA);
14677 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14678 udelay(300);
14679
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014680 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014681 POSTING_READ(vga_reg);
14682}
14683
Daniel Vetterf8175862012-04-10 15:50:11 +020014684void intel_modeset_init_hw(struct drm_device *dev)
14685{
Ville Syrjäläb6283052015-06-03 15:45:07 +030014686 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014687 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014688 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014689 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014690}
14691
Jesse Barnes79e53942008-11-07 14:24:08 -080014692void intel_modeset_init(struct drm_device *dev)
14693{
Jesse Barnes652c3932009-08-17 13:31:43 -070014694 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014695 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014696 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014697 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014698
14699 drm_mode_config_init(dev);
14700
14701 dev->mode_config.min_width = 0;
14702 dev->mode_config.min_height = 0;
14703
Dave Airlie019d96c2011-09-29 16:20:42 +010014704 dev->mode_config.preferred_depth = 24;
14705 dev->mode_config.prefer_shadow = 1;
14706
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014707 dev->mode_config.allow_fb_modifiers = true;
14708
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014709 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014710
Jesse Barnesb690e962010-07-19 13:53:12 -070014711 intel_init_quirks(dev);
14712
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014713 intel_init_pm(dev);
14714
Ben Widawskye3c74752013-04-05 13:12:39 -070014715 if (INTEL_INFO(dev)->num_pipes == 0)
14716 return;
14717
Lukas Wunner69f92f62015-07-15 13:57:35 +020014718 /*
14719 * There may be no VBT; and if the BIOS enabled SSC we can
14720 * just keep using it to avoid unnecessary flicker. Whereas if the
14721 * BIOS isn't using it, don't assume it will work even if the VBT
14722 * indicates as much.
14723 */
14724 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
14725 bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14726 DREF_SSC1_ENABLE);
14727
14728 if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
14729 DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
14730 bios_lvds_use_ssc ? "en" : "dis",
14731 dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
14732 dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
14733 }
14734 }
14735
Jesse Barnese70236a2009-09-21 10:42:27 -070014736 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014737 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014738
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014739 if (IS_GEN2(dev)) {
14740 dev->mode_config.max_width = 2048;
14741 dev->mode_config.max_height = 2048;
14742 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014743 dev->mode_config.max_width = 4096;
14744 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014745 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014746 dev->mode_config.max_width = 8192;
14747 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014748 }
Damien Lespiau068be562014-03-28 14:17:49 +000014749
Ville Syrjälädc41c152014-08-13 11:57:05 +030014750 if (IS_845G(dev) || IS_I865G(dev)) {
14751 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14752 dev->mode_config.cursor_height = 1023;
14753 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014754 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14755 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14756 } else {
14757 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14758 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14759 }
14760
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014761 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014762
Zhao Yakui28c97732009-10-09 11:39:41 +080014763 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014764 INTEL_INFO(dev)->num_pipes,
14765 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014766
Damien Lespiau055e3932014-08-18 13:49:10 +010014767 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014768 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014769 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014770 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014771 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014772 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014773 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014774 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014775 }
14776
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014777 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014778
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014779 /* Just disable it once at startup */
14780 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014781 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014782
14783 /* Just in case the BIOS is doing something questionable. */
Paulo Zanoni7733b492015-07-07 15:26:04 -030014784 intel_fbc_disable(dev_priv);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014785
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014786 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020014787 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014788 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014789
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014790 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014791 struct intel_initial_plane_config plane_config = {};
14792
Jesse Barnes46f297f2014-03-07 08:57:48 -080014793 if (!crtc->active)
14794 continue;
14795
Jesse Barnes46f297f2014-03-07 08:57:48 -080014796 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014797 * Note that reserving the BIOS fb up front prevents us
14798 * from stuffing other stolen allocations like the ring
14799 * on top. This prevents some ugliness at boot time, and
14800 * can even allow for smooth boot transitions if the BIOS
14801 * fb is large enough for the active pipe configuration.
14802 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020014803 dev_priv->display.get_initial_plane_config(crtc,
14804 &plane_config);
14805
14806 /*
14807 * If the fb is shared between multiple heads, we'll
14808 * just get the first one.
14809 */
14810 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014811 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014812}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014813
Daniel Vetter7fad7982012-07-04 17:51:47 +020014814static void intel_enable_pipe_a(struct drm_device *dev)
14815{
14816 struct intel_connector *connector;
14817 struct drm_connector *crt = NULL;
14818 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014819 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014820
14821 /* We can't just switch on the pipe A, we need to set things up with a
14822 * proper mode and output configuration. As a gross hack, enable pipe A
14823 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014824 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014825 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14826 crt = &connector->base;
14827 break;
14828 }
14829 }
14830
14831 if (!crt)
14832 return;
14833
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014834 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014835 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014836}
14837
Daniel Vetterfa555832012-10-10 23:14:00 +020014838static bool
14839intel_check_plane_mapping(struct intel_crtc *crtc)
14840{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014841 struct drm_device *dev = crtc->base.dev;
14842 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014843 u32 reg, val;
14844
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014845 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014846 return true;
14847
14848 reg = DSPCNTR(!crtc->plane);
14849 val = I915_READ(reg);
14850
14851 if ((val & DISPLAY_PLANE_ENABLE) &&
14852 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14853 return false;
14854
14855 return true;
14856}
14857
Ville Syrjälä02e93c32015-08-26 19:39:19 +030014858static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
14859{
14860 struct drm_device *dev = crtc->base.dev;
14861 struct intel_encoder *encoder;
14862
14863 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
14864 return true;
14865
14866 return false;
14867}
14868
Daniel Vetter24929352012-07-02 20:28:59 +020014869static void intel_sanitize_crtc(struct intel_crtc *crtc)
14870{
14871 struct drm_device *dev = crtc->base.dev;
14872 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014873 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014874
Daniel Vetter24929352012-07-02 20:28:59 +020014875 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014876 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014877 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14878
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014879 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014880 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014881 if (crtc->active) {
Maarten Lankhorst3a03dfb2015-07-14 13:46:40 +020014882 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014883 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014884 drm_crtc_vblank_on(&crtc->base);
14885 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014886
Daniel Vetter24929352012-07-02 20:28:59 +020014887 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014888 * disable the crtc (and hence change the state) if it is wrong. Note
14889 * that gen4+ has a fixed plane -> pipe mapping. */
14890 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014891 bool plane;
14892
Daniel Vetter24929352012-07-02 20:28:59 +020014893 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14894 crtc->base.base.id);
14895
14896 /* Pipe has the wrong plane attached and the plane is active.
14897 * Temporarily change the plane mapping and disable everything
14898 * ... */
14899 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014900 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014901 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014902 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014903 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020014904 }
Daniel Vetter24929352012-07-02 20:28:59 +020014905
Daniel Vetter7fad7982012-07-04 17:51:47 +020014906 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14907 crtc->pipe == PIPE_A && !crtc->active) {
14908 /* BIOS forgot to enable pipe A, this mostly happens after
14909 * resume. Force-enable the pipe to fix this, the update_dpms
14910 * call below we restore the pipe to the right state, but leave
14911 * the required bits on. */
14912 intel_enable_pipe_a(dev);
14913 }
14914
Daniel Vetter24929352012-07-02 20:28:59 +020014915 /* Adjust the state of the output pipe according to whether we
14916 * have active connectors/encoders. */
Ville Syrjälä02e93c32015-08-26 19:39:19 +030014917 if (!intel_crtc_has_encoders(crtc))
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014918 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014919
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020014920 if (crtc->active != crtc->base.state->active) {
Ville Syrjälä02e93c32015-08-26 19:39:19 +030014921 struct intel_encoder *encoder;
Daniel Vetter24929352012-07-02 20:28:59 +020014922
14923 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020014924 * functions or because of calls to intel_crtc_disable_noatomic,
14925 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020014926 * pipe A quirk. */
14927 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14928 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014929 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014930 crtc->active ? "enabled" : "disabled");
14931
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020014932 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014933 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014934 crtc->base.enabled = crtc->active;
14935
14936 /* Because we only establish the connector -> encoder ->
14937 * crtc links if something is active, this means the
14938 * crtc is now deactivated. Break the links. connector
14939 * -> encoder links are only establish when things are
14940 * actually up, hence no need to break them. */
14941 WARN_ON(crtc->active);
14942
Maarten Lankhorst2d406bb2015-08-05 12:37:09 +020014943 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Daniel Vetter24929352012-07-02 20:28:59 +020014944 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014945 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014946
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014947 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014948 /*
14949 * We start out with underrun reporting disabled to avoid races.
14950 * For correct bookkeeping mark this on active crtcs.
14951 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014952 * Also on gmch platforms we dont have any hardware bits to
14953 * disable the underrun reporting. Which means we need to start
14954 * out with underrun reporting disabled also on inactive pipes,
14955 * since otherwise we'll complain about the garbage we read when
14956 * e.g. coming up after runtime pm.
14957 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014958 * No protection against concurrent access is required - at
14959 * worst a fifo underrun happens which also sets this to false.
14960 */
14961 crtc->cpu_fifo_underrun_disabled = true;
14962 crtc->pch_fifo_underrun_disabled = true;
14963 }
Daniel Vetter24929352012-07-02 20:28:59 +020014964}
14965
14966static void intel_sanitize_encoder(struct intel_encoder *encoder)
14967{
14968 struct intel_connector *connector;
14969 struct drm_device *dev = encoder->base.dev;
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020014970 bool active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014971
14972 /* We need to check both for a crtc link (meaning that the
14973 * encoder is active and trying to read from a pipe) and the
14974 * pipe itself being active. */
14975 bool has_active_crtc = encoder->base.crtc &&
14976 to_intel_crtc(encoder->base.crtc)->active;
14977
Maarten Lankhorst873ffe62015-08-05 12:37:07 +020014978 for_each_intel_connector(dev, connector) {
14979 if (connector->base.encoder != &encoder->base)
14980 continue;
14981
14982 active = true;
14983 break;
14984 }
14985
14986 if (active && !has_active_crtc) {
Daniel Vetter24929352012-07-02 20:28:59 +020014987 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14988 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014989 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014990
14991 /* Connector is active, but has no active pipe. This is
14992 * fallout from our resume register restoring. Disable
14993 * the encoder manually again. */
14994 if (encoder->base.crtc) {
14995 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14996 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014997 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014998 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014999 if (encoder->post_disable)
15000 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015001 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015002 encoder->base.crtc = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015003
15004 /* Inconsistent output/port/pipe state happens presumably due to
15005 * a bug in one of the get_hw_state functions. Or someplace else
15006 * in our code, like the register restore mess on resume. Clamp
15007 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015008 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015009 if (connector->encoder != encoder)
15010 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015011 connector->base.dpms = DRM_MODE_DPMS_OFF;
15012 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015013 }
15014 }
15015 /* Enabled encoders without active connectors will be fixed in
15016 * the crtc fixup. */
15017}
15018
Imre Deak04098752014-02-18 00:02:16 +020015019void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015020{
15021 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015022 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015023
Imre Deak04098752014-02-18 00:02:16 +020015024 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15025 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15026 i915_disable_vga(dev);
15027 }
15028}
15029
15030void i915_redisable_vga(struct drm_device *dev)
15031{
15032 struct drm_i915_private *dev_priv = dev->dev_private;
15033
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015034 /* This function can be called both from intel_modeset_setup_hw_state or
15035 * at a very early point in our resume sequence, where the power well
15036 * structures are not yet restored. Since this function is at a very
15037 * paranoid "someone might have enabled VGA while we were not looking"
15038 * level, just check if the power well is enabled instead of trying to
15039 * follow the "don't touch the power well if we don't need it" policy
15040 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015041 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015042 return;
15043
Imre Deak04098752014-02-18 00:02:16 +020015044 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015045}
15046
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015047static bool primary_get_hw_state(struct intel_crtc *crtc)
15048{
15049 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15050
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015051 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15052}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015053
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015054static void readout_plane_state(struct intel_crtc *crtc,
15055 struct intel_crtc_state *crtc_state)
15056{
15057 struct intel_plane *p;
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015058 struct intel_plane_state *plane_state;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015059 bool active = crtc_state->base.active;
15060
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015061 for_each_intel_plane(crtc->base.dev, p) {
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015062 if (crtc->pipe != p->pipe)
15063 continue;
15064
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015065 plane_state = to_intel_plane_state(p->base.state);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015066
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015067 if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
15068 plane_state->visible = primary_get_hw_state(crtc);
15069 else {
15070 if (active)
15071 p->disable_plane(&p->base, &crtc->base);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015072
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015073 plane_state->visible = false;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015074 }
15075 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015076}
15077
Daniel Vetter30e984d2013-06-05 13:34:17 +020015078static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015079{
15080 struct drm_i915_private *dev_priv = dev->dev_private;
15081 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015082 struct intel_crtc *crtc;
15083 struct intel_encoder *encoder;
15084 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015085 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015086
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015087 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb06f8b02015-07-14 13:42:49 +020015088 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015089 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015090 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015091
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015092 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015093 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015094
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015095 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015096 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015097
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015098 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15099 if (crtc->base.state->active) {
15100 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15101 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15102 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15103
15104 /*
15105 * The initial mode needs to be set in order to keep
15106 * the atomic core happy. It wants a valid mode if the
15107 * crtc's enabled, so we do the above call.
15108 *
15109 * At this point some state updated by the connectors
15110 * in their ->detect() callback has not run yet, so
15111 * no recalculation can be done yet.
15112 *
15113 * Even if we could do a recalculation and modeset
15114 * right now it would cause a double modeset if
15115 * fbdev or userspace chooses a different initial mode.
15116 *
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015117 * If that happens, someone indicated they wanted a
15118 * mode change, which means it's safe to do a full
15119 * recalculation.
15120 */
Daniel Vetter1ed51de2015-07-15 14:15:51 +020015121 crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015122 }
15123
15124 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015125 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015126
15127 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15128 crtc->base.base.id,
15129 crtc->active ? "enabled" : "disabled");
15130 }
15131
Daniel Vetter53589012013-06-05 13:34:16 +020015132 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15133 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15134
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015135 pll->on = pll->get_hw_state(dev_priv, pll,
15136 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015137 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015138 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015139 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015140 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015141 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015142 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015143 }
Daniel Vetter53589012013-06-05 13:34:16 +020015144 }
Daniel Vetter53589012013-06-05 13:34:16 +020015145
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015146 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015147 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015148
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015149 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015150 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015151 }
15152
Damien Lespiaub2784e12014-08-05 11:29:37 +010015153 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015154 pipe = 0;
15155
15156 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015157 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15158 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015159 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015160 } else {
15161 encoder->base.crtc = NULL;
15162 }
15163
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015164 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015165 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015166 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015167 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015168 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015169 }
15170
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015171 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015172 if (connector->get_hw_state(connector)) {
15173 connector->base.dpms = DRM_MODE_DPMS_ON;
Daniel Vetter24929352012-07-02 20:28:59 +020015174 connector->base.encoder = &connector->encoder->base;
15175 } else {
15176 connector->base.dpms = DRM_MODE_DPMS_OFF;
15177 connector->base.encoder = NULL;
15178 }
15179 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15180 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015181 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015182 connector->base.encoder ? "enabled" : "disabled");
15183 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015184}
15185
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015186/* Scan out the current hw modeset state,
15187 * and sanitizes it to the current state
15188 */
15189static void
15190intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015191{
15192 struct drm_i915_private *dev_priv = dev->dev_private;
15193 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015194 struct intel_crtc *crtc;
15195 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015196 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015197
15198 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015199
15200 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015201 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015202 intel_sanitize_encoder(encoder);
15203 }
15204
Damien Lespiau055e3932014-08-18 13:49:10 +010015205 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015206 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15207 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015208 intel_dump_pipe_config(crtc, crtc->config,
15209 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015210 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015211
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015212 intel_modeset_update_connector_atomic_state(dev);
15213
Daniel Vetter35c95372013-07-17 06:55:04 +020015214 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15215 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15216
15217 if (!pll->on || pll->active)
15218 continue;
15219
15220 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15221
15222 pll->disable(dev_priv, pll);
15223 pll->on = false;
15224 }
15225
Ville Syrjälä26e1fe42015-06-24 22:00:06 +030015226 if (IS_VALLEYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015227 vlv_wm_get_hw_state(dev);
15228 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015229 skl_wm_get_hw_state(dev);
15230 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015231 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015232
15233 for_each_intel_crtc(dev, crtc) {
15234 unsigned long put_domains;
15235
15236 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15237 if (WARN_ON(put_domains))
15238 modeset_put_power_domains(dev_priv, put_domains);
15239 }
15240 intel_display_set_init_power(dev_priv, false);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015241}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015242
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015243void intel_display_resume(struct drm_device *dev)
15244{
15245 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15246 struct intel_connector *conn;
15247 struct intel_plane *plane;
15248 struct drm_crtc *crtc;
15249 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015250
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015251 if (!state)
15252 return;
15253
15254 state->acquire_ctx = dev->mode_config.acquire_ctx;
15255
15256 /* preserve complete old state, including dpll */
15257 intel_atomic_get_shared_dpll_state(state);
15258
15259 for_each_crtc(dev, crtc) {
15260 struct drm_crtc_state *crtc_state =
15261 drm_atomic_get_crtc_state(state, crtc);
15262
15263 ret = PTR_ERR_OR_ZERO(crtc_state);
15264 if (ret)
15265 goto err;
15266
15267 /* force a restore */
15268 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015269 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015270
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015271 for_each_intel_plane(dev, plane) {
15272 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15273 if (ret)
15274 goto err;
15275 }
15276
15277 for_each_intel_connector(dev, conn) {
15278 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15279 if (ret)
15280 goto err;
15281 }
15282
15283 intel_modeset_setup_hw_state(dev);
15284
15285 i915_redisable_vga(dev);
Maarten Lankhorst74c090b2015-07-13 16:30:30 +020015286 ret = drm_atomic_commit(state);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015287 if (!ret)
15288 return;
15289
15290err:
15291 DRM_ERROR("Restoring old state failed with %i\n", ret);
15292 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015293}
15294
15295void intel_modeset_gem_init(struct drm_device *dev)
15296{
Jesse Barnes484b41d2014-03-07 08:57:55 -080015297 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015298 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015299 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015300
Imre Deakae484342014-03-31 15:10:44 +030015301 mutex_lock(&dev->struct_mutex);
15302 intel_init_gt_powersave(dev);
15303 mutex_unlock(&dev->struct_mutex);
15304
Chris Wilson1833b132012-05-09 11:56:28 +010015305 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015306
15307 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015308
15309 /*
15310 * Make sure any fbs we allocated at startup are properly
15311 * pinned & fenced. When we do the allocation it's too early
15312 * for this.
15313 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015314 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015315 obj = intel_fb_obj(c->primary->fb);
15316 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015317 continue;
15318
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015319 mutex_lock(&dev->struct_mutex);
15320 ret = intel_pin_and_fence_fb_obj(c->primary,
15321 c->primary->fb,
15322 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015323 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015324 mutex_unlock(&dev->struct_mutex);
15325 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015326 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15327 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015328 drm_framebuffer_unreference(c->primary->fb);
15329 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015330 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015331 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015332 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015333 }
15334 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015335
15336 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015337}
15338
Imre Deak4932e2c2014-02-11 17:12:48 +020015339void intel_connector_unregister(struct intel_connector *intel_connector)
15340{
15341 struct drm_connector *connector = &intel_connector->base;
15342
15343 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015344 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015345}
15346
Jesse Barnes79e53942008-11-07 14:24:08 -080015347void intel_modeset_cleanup(struct drm_device *dev)
15348{
Jesse Barnes652c3932009-08-17 13:31:43 -070015349 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015350 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015351
Imre Deak2eb52522014-11-19 15:30:05 +020015352 intel_disable_gt_powersave(dev);
15353
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015354 intel_backlight_unregister(dev);
15355
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015356 /*
15357 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015358 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015359 * experience fancy races otherwise.
15360 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015361 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015362
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015363 /*
15364 * Due to the hpd irq storm handling the hotplug work can re-arm the
15365 * poll handlers. Hence disable polling after hpd handling is shut down.
15366 */
Keith Packardf87ea762010-10-03 19:36:26 -070015367 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015368
Jesse Barnes723bfd72010-10-07 16:01:13 -070015369 intel_unregister_dsm_handler();
15370
Paulo Zanoni7733b492015-07-07 15:26:04 -030015371 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015372
Chris Wilson1630fe72011-07-08 12:22:42 +010015373 /* flush any delayed tasks or pending work */
15374 flush_scheduled_work();
15375
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015376 /* destroy the backlight and sysfs files before encoders/connectors */
15377 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015378 struct intel_connector *intel_connector;
15379
15380 intel_connector = to_intel_connector(connector);
15381 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015382 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015383
Jesse Barnes79e53942008-11-07 14:24:08 -080015384 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015385
15386 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015387
15388 mutex_lock(&dev->struct_mutex);
15389 intel_cleanup_gt_powersave(dev);
15390 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015391}
15392
Dave Airlie28d52042009-09-21 14:33:58 +100015393/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015394 * Return which encoder is currently attached for connector.
15395 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015396struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015397{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015398 return &intel_attached_encoder(connector)->base;
15399}
Jesse Barnes79e53942008-11-07 14:24:08 -080015400
Chris Wilsondf0e9242010-09-09 16:20:55 +010015401void intel_connector_attach_encoder(struct intel_connector *connector,
15402 struct intel_encoder *encoder)
15403{
15404 connector->encoder = encoder;
15405 drm_mode_connector_attach_encoder(&connector->base,
15406 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015407}
Dave Airlie28d52042009-09-21 14:33:58 +100015408
15409/*
15410 * set vga decode state - true == enable VGA decode
15411 */
15412int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15413{
15414 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015415 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015416 u16 gmch_ctrl;
15417
Chris Wilson75fa0412014-02-07 18:37:02 -020015418 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15419 DRM_ERROR("failed to read control word\n");
15420 return -EIO;
15421 }
15422
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015423 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15424 return 0;
15425
Dave Airlie28d52042009-09-21 14:33:58 +100015426 if (state)
15427 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15428 else
15429 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015430
15431 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15432 DRM_ERROR("failed to write control word\n");
15433 return -EIO;
15434 }
15435
Dave Airlie28d52042009-09-21 14:33:58 +100015436 return 0;
15437}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015438
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015439struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015440
15441 u32 power_well_driver;
15442
Chris Wilson63b66e52013-08-08 15:12:06 +020015443 int num_transcoders;
15444
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015445 struct intel_cursor_error_state {
15446 u32 control;
15447 u32 position;
15448 u32 base;
15449 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015450 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015451
15452 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015453 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015454 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015455 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015456 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015457
15458 struct intel_plane_error_state {
15459 u32 control;
15460 u32 stride;
15461 u32 size;
15462 u32 pos;
15463 u32 addr;
15464 u32 surface;
15465 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015466 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015467
15468 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015469 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015470 enum transcoder cpu_transcoder;
15471
15472 u32 conf;
15473
15474 u32 htotal;
15475 u32 hblank;
15476 u32 hsync;
15477 u32 vtotal;
15478 u32 vblank;
15479 u32 vsync;
15480 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015481};
15482
15483struct intel_display_error_state *
15484intel_display_capture_error_state(struct drm_device *dev)
15485{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015486 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015487 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015488 int transcoders[] = {
15489 TRANSCODER_A,
15490 TRANSCODER_B,
15491 TRANSCODER_C,
15492 TRANSCODER_EDP,
15493 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015494 int i;
15495
Chris Wilson63b66e52013-08-08 15:12:06 +020015496 if (INTEL_INFO(dev)->num_pipes == 0)
15497 return NULL;
15498
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015499 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015500 if (error == NULL)
15501 return NULL;
15502
Imre Deak190be112013-11-25 17:15:31 +020015503 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015504 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15505
Damien Lespiau055e3932014-08-18 13:49:10 +010015506 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015507 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015508 __intel_display_power_is_enabled(dev_priv,
15509 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015510 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015511 continue;
15512
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015513 error->cursor[i].control = I915_READ(CURCNTR(i));
15514 error->cursor[i].position = I915_READ(CURPOS(i));
15515 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015516
15517 error->plane[i].control = I915_READ(DSPCNTR(i));
15518 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015519 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015520 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015521 error->plane[i].pos = I915_READ(DSPPOS(i));
15522 }
Paulo Zanonica291362013-03-06 20:03:14 -030015523 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15524 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015525 if (INTEL_INFO(dev)->gen >= 4) {
15526 error->plane[i].surface = I915_READ(DSPSURF(i));
15527 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15528 }
15529
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015530 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030015531
Sonika Jindal3abfce72014-07-21 15:23:43 +053015532 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030015533 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015534 }
15535
15536 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15537 if (HAS_DDI(dev_priv->dev))
15538 error->num_transcoders++; /* Account for eDP. */
15539
15540 for (i = 0; i < error->num_transcoders; i++) {
15541 enum transcoder cpu_transcoder = transcoders[i];
15542
Imre Deakddf9c532013-11-27 22:02:02 +020015543 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015544 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015545 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015546 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015547 continue;
15548
Chris Wilson63b66e52013-08-08 15:12:06 +020015549 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15550
15551 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15552 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15553 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15554 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15555 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15556 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15557 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015558 }
15559
15560 return error;
15561}
15562
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015563#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15564
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015565void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015566intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015567 struct drm_device *dev,
15568 struct intel_display_error_state *error)
15569{
Damien Lespiau055e3932014-08-18 13:49:10 +010015570 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015571 int i;
15572
Chris Wilson63b66e52013-08-08 15:12:06 +020015573 if (!error)
15574 return;
15575
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015576 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015577 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015578 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015579 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015580 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015581 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015582 err_printf(m, " Power: %s\n",
15583 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015584 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030015585 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015586
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015587 err_printf(m, "Plane [%d]:\n", i);
15588 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15589 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015590 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015591 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15592 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015593 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015594 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015595 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015596 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015597 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15598 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015599 }
15600
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015601 err_printf(m, "Cursor [%d]:\n", i);
15602 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15603 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15604 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015605 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015606
15607 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015608 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015609 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015610 err_printf(m, " Power: %s\n",
15611 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015612 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15613 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15614 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15615 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15616 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15617 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15618 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15619 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015620}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015621
15622void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15623{
15624 struct intel_crtc *crtc;
15625
15626 for_each_intel_crtc(dev, crtc) {
15627 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015628
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015629 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015630
15631 work = crtc->unpin_work;
15632
15633 if (work && work->event &&
15634 work->event->base.file_priv == file) {
15635 kfree(work->event);
15636 work->event = NULL;
15637 }
15638
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015639 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015640 }
15641}