blob: c17ec502b0872b02c84615197733a8dbccce8a9f [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"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070042#include <drm/drm_plane_helper.h>
43#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080044#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080045
Matt Roper465c1202014-05-29 08:06:54 -070046/* Primary plane formats supported by all gen */
47#define COMMON_PRIMARY_FORMATS \
48 DRM_FORMAT_C8, \
49 DRM_FORMAT_RGB565, \
50 DRM_FORMAT_XRGB8888, \
51 DRM_FORMAT_ARGB8888
52
53/* Primary plane formats for gen <= 3 */
54static const uint32_t intel_primary_formats_gen2[] = {
55 COMMON_PRIMARY_FORMATS,
56 DRM_FORMAT_XRGB1555,
57 DRM_FORMAT_ARGB1555,
58};
59
60/* Primary plane formats for gen >= 4 */
61static const uint32_t intel_primary_formats_gen4[] = {
62 COMMON_PRIMARY_FORMATS, \
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_ABGR8888,
65 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_ARGB2101010,
67 DRM_FORMAT_XBGR2101010,
68 DRM_FORMAT_ABGR2101010,
69};
70
Matt Roper3d7d6512014-06-10 08:28:13 -070071/* Cursor formats */
72static const uint32_t intel_cursor_formats[] = {
73 DRM_FORMAT_ARGB8888,
74};
75
Chris Wilson6b383a72010-09-13 13:54:26 +010076static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080077
Jesse Barnesf1f644d2013-06-27 00:39:25 +030078static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
79 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030080static void ironlake_pch_clock_get(struct intel_crtc *crtc,
81 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030082
Damien Lespiaue7457a92013-08-08 22:28:59 +010083static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
84 int x, int y, struct drm_framebuffer *old_fb);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080085static int intel_framebuffer_init(struct drm_device *dev,
86 struct intel_framebuffer *ifb,
87 struct drm_mode_fb_cmd2 *mode_cmd,
88 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020089static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
90static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020091static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070092 struct intel_link_m_n *m_n,
93 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020094static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020095static void haswell_set_pipeconf(struct drm_crtc *crtc);
96static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +020097static void vlv_prepare_pll(struct intel_crtc *crtc,
98 const struct intel_crtc_config *pipe_config);
99static void chv_prepare_pll(struct intel_crtc *crtc,
100 const struct intel_crtc_config *pipe_config);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100101
Dave Airlie0e32b392014-05-02 14:02:48 +1000102static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
103{
104 if (!connector->mst_port)
105 return connector->encoder;
106 else
107 return &connector->mst_port->mst_encoders[pipe]->base;
108}
109
Jesse Barnes79e53942008-11-07 14:24:08 -0800110typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400111 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800112} intel_range_t;
113
114typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400115 int dot_limit;
116 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800117} intel_p2_t;
118
Ma Lingd4906092009-03-18 20:13:27 +0800119typedef struct intel_limit intel_limit_t;
120struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400121 intel_range_t dot, vco, n, m, m1, m2, p, p1;
122 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800123};
Jesse Barnes79e53942008-11-07 14:24:08 -0800124
Daniel Vetterd2acd212012-10-20 20:57:43 +0200125int
126intel_pch_rawclk(struct drm_device *dev)
127{
128 struct drm_i915_private *dev_priv = dev->dev_private;
129
130 WARN_ON(!HAS_PCH_SPLIT(dev));
131
132 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
133}
134
Chris Wilson021357a2010-09-07 20:54:59 +0100135static inline u32 /* units of 100MHz */
136intel_fdi_link_freq(struct drm_device *dev)
137{
Chris Wilson8b99e682010-10-13 09:59:17 +0100138 if (IS_GEN5(dev)) {
139 struct drm_i915_private *dev_priv = dev->dev_private;
140 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
141 } else
142 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100143}
144
Daniel Vetter5d536e22013-07-06 12:52:06 +0200145static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400146 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200147 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200148 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400149 .m = { .min = 96, .max = 140 },
150 .m1 = { .min = 18, .max = 26 },
151 .m2 = { .min = 6, .max = 16 },
152 .p = { .min = 4, .max = 128 },
153 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700154 .p2 = { .dot_limit = 165000,
155 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700156};
157
Daniel Vetter5d536e22013-07-06 12:52:06 +0200158static const intel_limit_t intel_limits_i8xx_dvo = {
159 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200160 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200161 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200162 .m = { .min = 96, .max = 140 },
163 .m1 = { .min = 18, .max = 26 },
164 .m2 = { .min = 6, .max = 16 },
165 .p = { .min = 4, .max = 128 },
166 .p1 = { .min = 2, .max = 33 },
167 .p2 = { .dot_limit = 165000,
168 .p2_slow = 4, .p2_fast = 4 },
169};
170
Keith Packarde4b36692009-06-05 19:22:17 -0700171static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400172 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200173 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200174 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400175 .m = { .min = 96, .max = 140 },
176 .m1 = { .min = 18, .max = 26 },
177 .m2 = { .min = 6, .max = 16 },
178 .p = { .min = 4, .max = 128 },
179 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700180 .p2 = { .dot_limit = 165000,
181 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700182};
Eric Anholt273e27c2011-03-30 13:01:10 -0700183
Keith Packarde4b36692009-06-05 19:22:17 -0700184static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400185 .dot = { .min = 20000, .max = 400000 },
186 .vco = { .min = 1400000, .max = 2800000 },
187 .n = { .min = 1, .max = 6 },
188 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100189 .m1 = { .min = 8, .max = 18 },
190 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400191 .p = { .min = 5, .max = 80 },
192 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700193 .p2 = { .dot_limit = 200000,
194 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700195};
196
197static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400198 .dot = { .min = 20000, .max = 400000 },
199 .vco = { .min = 1400000, .max = 2800000 },
200 .n = { .min = 1, .max = 6 },
201 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100202 .m1 = { .min = 8, .max = 18 },
203 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400204 .p = { .min = 7, .max = 98 },
205 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700206 .p2 = { .dot_limit = 112000,
207 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700208};
209
Eric Anholt273e27c2011-03-30 13:01:10 -0700210
Keith Packarde4b36692009-06-05 19:22:17 -0700211static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700212 .dot = { .min = 25000, .max = 270000 },
213 .vco = { .min = 1750000, .max = 3500000},
214 .n = { .min = 1, .max = 4 },
215 .m = { .min = 104, .max = 138 },
216 .m1 = { .min = 17, .max = 23 },
217 .m2 = { .min = 5, .max = 11 },
218 .p = { .min = 10, .max = 30 },
219 .p1 = { .min = 1, .max = 3},
220 .p2 = { .dot_limit = 270000,
221 .p2_slow = 10,
222 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800223 },
Keith Packarde4b36692009-06-05 19:22:17 -0700224};
225
226static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700227 .dot = { .min = 22000, .max = 400000 },
228 .vco = { .min = 1750000, .max = 3500000},
229 .n = { .min = 1, .max = 4 },
230 .m = { .min = 104, .max = 138 },
231 .m1 = { .min = 16, .max = 23 },
232 .m2 = { .min = 5, .max = 11 },
233 .p = { .min = 5, .max = 80 },
234 .p1 = { .min = 1, .max = 8},
235 .p2 = { .dot_limit = 165000,
236 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700237};
238
239static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700240 .dot = { .min = 20000, .max = 115000 },
241 .vco = { .min = 1750000, .max = 3500000 },
242 .n = { .min = 1, .max = 3 },
243 .m = { .min = 104, .max = 138 },
244 .m1 = { .min = 17, .max = 23 },
245 .m2 = { .min = 5, .max = 11 },
246 .p = { .min = 28, .max = 112 },
247 .p1 = { .min = 2, .max = 8 },
248 .p2 = { .dot_limit = 0,
249 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800250 },
Keith Packarde4b36692009-06-05 19:22:17 -0700251};
252
253static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700254 .dot = { .min = 80000, .max = 224000 },
255 .vco = { .min = 1750000, .max = 3500000 },
256 .n = { .min = 1, .max = 3 },
257 .m = { .min = 104, .max = 138 },
258 .m1 = { .min = 17, .max = 23 },
259 .m2 = { .min = 5, .max = 11 },
260 .p = { .min = 14, .max = 42 },
261 .p1 = { .min = 2, .max = 6 },
262 .p2 = { .dot_limit = 0,
263 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800264 },
Keith Packarde4b36692009-06-05 19:22:17 -0700265};
266
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500267static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400268 .dot = { .min = 20000, .max = 400000},
269 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700270 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400271 .n = { .min = 3, .max = 6 },
272 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400274 .m1 = { .min = 0, .max = 0 },
275 .m2 = { .min = 0, .max = 254 },
276 .p = { .min = 5, .max = 80 },
277 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700278 .p2 = { .dot_limit = 200000,
279 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700280};
281
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500282static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400283 .dot = { .min = 20000, .max = 400000 },
284 .vco = { .min = 1700000, .max = 3500000 },
285 .n = { .min = 3, .max = 6 },
286 .m = { .min = 2, .max = 256 },
287 .m1 = { .min = 0, .max = 0 },
288 .m2 = { .min = 0, .max = 254 },
289 .p = { .min = 7, .max = 112 },
290 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700291 .p2 = { .dot_limit = 112000,
292 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700293};
294
Eric Anholt273e27c2011-03-30 13:01:10 -0700295/* Ironlake / Sandybridge
296 *
297 * We calculate clock using (register_value + 2) for N/M1/M2, so here
298 * the range value for them is (actual_value - 2).
299 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800300static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700301 .dot = { .min = 25000, .max = 350000 },
302 .vco = { .min = 1760000, .max = 3510000 },
303 .n = { .min = 1, .max = 5 },
304 .m = { .min = 79, .max = 127 },
305 .m1 = { .min = 12, .max = 22 },
306 .m2 = { .min = 5, .max = 9 },
307 .p = { .min = 5, .max = 80 },
308 .p1 = { .min = 1, .max = 8 },
309 .p2 = { .dot_limit = 225000,
310 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700311};
312
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800313static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700314 .dot = { .min = 25000, .max = 350000 },
315 .vco = { .min = 1760000, .max = 3510000 },
316 .n = { .min = 1, .max = 3 },
317 .m = { .min = 79, .max = 118 },
318 .m1 = { .min = 12, .max = 22 },
319 .m2 = { .min = 5, .max = 9 },
320 .p = { .min = 28, .max = 112 },
321 .p1 = { .min = 2, .max = 8 },
322 .p2 = { .dot_limit = 225000,
323 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800324};
325
326static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700327 .dot = { .min = 25000, .max = 350000 },
328 .vco = { .min = 1760000, .max = 3510000 },
329 .n = { .min = 1, .max = 3 },
330 .m = { .min = 79, .max = 127 },
331 .m1 = { .min = 12, .max = 22 },
332 .m2 = { .min = 5, .max = 9 },
333 .p = { .min = 14, .max = 56 },
334 .p1 = { .min = 2, .max = 8 },
335 .p2 = { .dot_limit = 225000,
336 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800337};
338
Eric Anholt273e27c2011-03-30 13:01:10 -0700339/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800340static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700341 .dot = { .min = 25000, .max = 350000 },
342 .vco = { .min = 1760000, .max = 3510000 },
343 .n = { .min = 1, .max = 2 },
344 .m = { .min = 79, .max = 126 },
345 .m1 = { .min = 12, .max = 22 },
346 .m2 = { .min = 5, .max = 9 },
347 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400348 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700349 .p2 = { .dot_limit = 225000,
350 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800351};
352
353static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700354 .dot = { .min = 25000, .max = 350000 },
355 .vco = { .min = 1760000, .max = 3510000 },
356 .n = { .min = 1, .max = 3 },
357 .m = { .min = 79, .max = 126 },
358 .m1 = { .min = 12, .max = 22 },
359 .m2 = { .min = 5, .max = 9 },
360 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400361 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700362 .p2 = { .dot_limit = 225000,
363 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800364};
365
Ville Syrjälädc730512013-09-24 21:26:30 +0300366static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300367 /*
368 * These are the data rate limits (measured in fast clocks)
369 * since those are the strictest limits we have. The fast
370 * clock and actual rate limits are more relaxed, so checking
371 * them would make no difference.
372 */
373 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200374 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700375 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700376 .m1 = { .min = 2, .max = 3 },
377 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300378 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300379 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700380};
381
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300382static const intel_limit_t intel_limits_chv = {
383 /*
384 * These are the data rate limits (measured in fast clocks)
385 * since those are the strictest limits we have. The fast
386 * clock and actual rate limits are more relaxed, so checking
387 * them would make no difference.
388 */
389 .dot = { .min = 25000 * 5, .max = 540000 * 5},
390 .vco = { .min = 4860000, .max = 6700000 },
391 .n = { .min = 1, .max = 1 },
392 .m1 = { .min = 2, .max = 2 },
393 .m2 = { .min = 24 << 22, .max = 175 << 22 },
394 .p1 = { .min = 2, .max = 4 },
395 .p2 = { .p2_slow = 1, .p2_fast = 14 },
396};
397
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300398static void vlv_clock(int refclk, intel_clock_t *clock)
399{
400 clock->m = clock->m1 * clock->m2;
401 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200402 if (WARN_ON(clock->n == 0 || clock->p == 0))
403 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300404 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
405 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300406}
407
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300408/**
409 * Returns whether any output on the specified pipe is of the specified type
410 */
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200411bool intel_pipe_has_type(struct intel_crtc *crtc, int type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300412{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300413 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300414 struct intel_encoder *encoder;
415
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300416 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300417 if (encoder->type == type)
418 return true;
419
420 return false;
421}
422
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300423static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
Chris Wilson1b894b52010-12-14 20:04:54 +0000424 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800425{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300426 struct drm_device *dev = crtc->base.dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800427 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800428
429 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100430 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000431 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800432 limit = &intel_limits_ironlake_dual_lvds_100m;
433 else
434 limit = &intel_limits_ironlake_dual_lvds;
435 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000436 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800437 limit = &intel_limits_ironlake_single_lvds_100m;
438 else
439 limit = &intel_limits_ironlake_single_lvds;
440 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200441 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800442 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800443
444 return limit;
445}
446
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300447static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
Ma Ling044c7c42009-03-18 20:13:23 +0800448{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300449 struct drm_device *dev = crtc->base.dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800450 const intel_limit_t *limit;
451
452 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100453 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700454 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800455 else
Keith Packarde4b36692009-06-05 19:22:17 -0700456 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800457 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
458 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700459 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800460 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700461 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800462 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700463 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800464
465 return limit;
466}
467
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300468static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800469{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300470 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800471 const intel_limit_t *limit;
472
Eric Anholtbad720f2009-10-22 16:11:14 -0700473 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000474 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800475 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800476 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500477 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800478 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500479 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800480 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500481 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300482 } else if (IS_CHERRYVIEW(dev)) {
483 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700484 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300485 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100486 } else if (!IS_GEN2(dev)) {
487 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
488 limit = &intel_limits_i9xx_lvds;
489 else
490 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800491 } else {
492 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700493 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200494 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700495 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200496 else
497 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800498 }
499 return limit;
500}
501
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500502/* m1 is reserved as 0 in Pineview, n is a ring counter */
503static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800504{
Shaohua Li21778322009-02-23 15:19:16 +0800505 clock->m = clock->m2 + 2;
506 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200507 if (WARN_ON(clock->n == 0 || clock->p == 0))
508 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300509 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
510 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800511}
512
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200513static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
514{
515 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
516}
517
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200518static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800519{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200520 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800521 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200522 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
523 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300524 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
525 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800526}
527
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300528static void chv_clock(int refclk, intel_clock_t *clock)
529{
530 clock->m = clock->m1 * clock->m2;
531 clock->p = clock->p1 * clock->p2;
532 if (WARN_ON(clock->n == 0 || clock->p == 0))
533 return;
534 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
535 clock->n << 22);
536 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
537}
538
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800539#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800540/**
541 * Returns whether the given set of divisors are valid for a given refclk with
542 * the given connectors.
543 */
544
Chris Wilson1b894b52010-12-14 20:04:54 +0000545static bool intel_PLL_is_valid(struct drm_device *dev,
546 const intel_limit_t *limit,
547 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800548{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300549 if (clock->n < limit->n.min || limit->n.max < clock->n)
550 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800551 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400552 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800553 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400554 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800555 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400556 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300557
558 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
559 if (clock->m1 <= clock->m2)
560 INTELPllInvalid("m1 <= m2\n");
561
562 if (!IS_VALLEYVIEW(dev)) {
563 if (clock->p < limit->p.min || limit->p.max < clock->p)
564 INTELPllInvalid("p out of range\n");
565 if (clock->m < limit->m.min || limit->m.max < clock->m)
566 INTELPllInvalid("m out of range\n");
567 }
568
Jesse Barnes79e53942008-11-07 14:24:08 -0800569 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400570 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800571 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
572 * connector, etc., rather than just a single range.
573 */
574 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400575 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800576
577 return true;
578}
579
Ma Lingd4906092009-03-18 20:13:27 +0800580static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300581i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800582 int target, int refclk, intel_clock_t *match_clock,
583 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800584{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300585 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800586 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800587 int err = target;
588
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300589 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800590 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100591 * For LVDS just rely on its current settings for dual-channel.
592 * We haven't figured out how to reliably set up different
593 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800594 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100595 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800596 clock.p2 = limit->p2.p2_fast;
597 else
598 clock.p2 = limit->p2.p2_slow;
599 } else {
600 if (target < limit->p2.dot_limit)
601 clock.p2 = limit->p2.p2_slow;
602 else
603 clock.p2 = limit->p2.p2_fast;
604 }
605
Akshay Joshi0206e352011-08-16 15:34:10 -0400606 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800607
Zhao Yakui42158662009-11-20 11:24:18 +0800608 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
609 clock.m1++) {
610 for (clock.m2 = limit->m2.min;
611 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200612 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800613 break;
614 for (clock.n = limit->n.min;
615 clock.n <= limit->n.max; clock.n++) {
616 for (clock.p1 = limit->p1.min;
617 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800618 int this_err;
619
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200620 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000621 if (!intel_PLL_is_valid(dev, limit,
622 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800623 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800624 if (match_clock &&
625 clock.p != match_clock->p)
626 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800627
628 this_err = abs(clock.dot - target);
629 if (this_err < err) {
630 *best_clock = clock;
631 err = this_err;
632 }
633 }
634 }
635 }
636 }
637
638 return (err != target);
639}
640
Ma Lingd4906092009-03-18 20:13:27 +0800641static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300642pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200643 int target, int refclk, intel_clock_t *match_clock,
644 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200645{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300646 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200647 intel_clock_t clock;
648 int err = target;
649
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300650 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200651 /*
652 * For LVDS just rely on its current settings for dual-channel.
653 * We haven't figured out how to reliably set up different
654 * single/dual channel state, if we even can.
655 */
656 if (intel_is_dual_link_lvds(dev))
657 clock.p2 = limit->p2.p2_fast;
658 else
659 clock.p2 = limit->p2.p2_slow;
660 } else {
661 if (target < limit->p2.dot_limit)
662 clock.p2 = limit->p2.p2_slow;
663 else
664 clock.p2 = limit->p2.p2_fast;
665 }
666
667 memset(best_clock, 0, sizeof(*best_clock));
668
669 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
670 clock.m1++) {
671 for (clock.m2 = limit->m2.min;
672 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200673 for (clock.n = limit->n.min;
674 clock.n <= limit->n.max; clock.n++) {
675 for (clock.p1 = limit->p1.min;
676 clock.p1 <= limit->p1.max; clock.p1++) {
677 int this_err;
678
679 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800680 if (!intel_PLL_is_valid(dev, limit,
681 &clock))
682 continue;
683 if (match_clock &&
684 clock.p != match_clock->p)
685 continue;
686
687 this_err = abs(clock.dot - target);
688 if (this_err < err) {
689 *best_clock = clock;
690 err = this_err;
691 }
692 }
693 }
694 }
695 }
696
697 return (err != target);
698}
699
Ma Lingd4906092009-03-18 20:13:27 +0800700static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300701g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200702 int target, int refclk, intel_clock_t *match_clock,
703 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800704{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300705 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800706 intel_clock_t clock;
707 int max_n;
708 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400709 /* approximately equals target * 0.00585 */
710 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800711 found = false;
712
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300713 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100714 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800715 clock.p2 = limit->p2.p2_fast;
716 else
717 clock.p2 = limit->p2.p2_slow;
718 } else {
719 if (target < limit->p2.dot_limit)
720 clock.p2 = limit->p2.p2_slow;
721 else
722 clock.p2 = limit->p2.p2_fast;
723 }
724
725 memset(best_clock, 0, sizeof(*best_clock));
726 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200727 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800728 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200729 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800730 for (clock.m1 = limit->m1.max;
731 clock.m1 >= limit->m1.min; clock.m1--) {
732 for (clock.m2 = limit->m2.max;
733 clock.m2 >= limit->m2.min; clock.m2--) {
734 for (clock.p1 = limit->p1.max;
735 clock.p1 >= limit->p1.min; clock.p1--) {
736 int this_err;
737
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000739 if (!intel_PLL_is_valid(dev, limit,
740 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800741 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000742
743 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800744 if (this_err < err_most) {
745 *best_clock = clock;
746 err_most = this_err;
747 max_n = clock.n;
748 found = true;
749 }
750 }
751 }
752 }
753 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800754 return found;
755}
Ma Lingd4906092009-03-18 20:13:27 +0800756
Zhenyu Wang2c072452009-06-05 15:38:42 +0800757static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300758vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200759 int target, int refclk, intel_clock_t *match_clock,
760 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700761{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300762 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300763 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300764 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300765 /* min update 19.2 MHz */
766 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300767 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700768
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300769 target *= 5; /* fast clock */
770
771 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700772
773 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300774 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300775 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300776 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300777 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300778 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700779 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300780 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300781 unsigned int ppm, diff;
782
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300783 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
784 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300785
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300786 vlv_clock(refclk, &clock);
787
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300788 if (!intel_PLL_is_valid(dev, limit,
789 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300790 continue;
791
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300792 diff = abs(clock.dot - target);
793 ppm = div_u64(1000000ULL * diff, target);
794
795 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300796 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300797 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300798 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300799 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300800
Ville Syrjäläc6861222013-09-24 21:26:21 +0300801 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300802 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300803 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300804 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700805 }
806 }
807 }
808 }
809 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700810
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300811 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700812}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700813
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300814static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300815chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300816 int target, int refclk, intel_clock_t *match_clock,
817 intel_clock_t *best_clock)
818{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300819 struct drm_device *dev = crtc->base.dev;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300820 intel_clock_t clock;
821 uint64_t m2;
822 int found = false;
823
824 memset(best_clock, 0, sizeof(*best_clock));
825
826 /*
827 * Based on hardware doc, the n always set to 1, and m1 always
828 * set to 2. If requires to support 200Mhz refclk, we need to
829 * revisit this because n may not 1 anymore.
830 */
831 clock.n = 1, clock.m1 = 2;
832 target *= 5; /* fast clock */
833
834 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
835 for (clock.p2 = limit->p2.p2_fast;
836 clock.p2 >= limit->p2.p2_slow;
837 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
838
839 clock.p = clock.p1 * clock.p2;
840
841 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
842 clock.n) << 22, refclk * clock.m1);
843
844 if (m2 > INT_MAX/clock.m1)
845 continue;
846
847 clock.m2 = m2;
848
849 chv_clock(refclk, &clock);
850
851 if (!intel_PLL_is_valid(dev, limit, &clock))
852 continue;
853
854 /* based on hardware requirement, prefer bigger p
855 */
856 if (clock.p > best_clock->p) {
857 *best_clock = clock;
858 found = true;
859 }
860 }
861 }
862
863 return found;
864}
865
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300866bool intel_crtc_active(struct drm_crtc *crtc)
867{
868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
869
870 /* Be paranoid as we can arrive here with only partial
871 * state retrieved from the hardware during setup.
872 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100873 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300874 * as Haswell has gained clock readout/fastboot support.
875 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000876 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300877 * properly reconstruct framebuffers.
878 */
Matt Roperf4510a22014-04-01 15:22:40 -0700879 return intel_crtc->active && crtc->primary->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100880 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300881}
882
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200883enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
884 enum pipe pipe)
885{
886 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
888
Daniel Vetter3b117c82013-04-17 20:15:07 +0200889 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200890}
891
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300892static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
893{
894 struct drm_i915_private *dev_priv = dev->dev_private;
895 u32 reg = PIPEDSL(pipe);
896 u32 line1, line2;
897 u32 line_mask;
898
899 if (IS_GEN2(dev))
900 line_mask = DSL_LINEMASK_GEN2;
901 else
902 line_mask = DSL_LINEMASK_GEN3;
903
904 line1 = I915_READ(reg) & line_mask;
905 mdelay(5);
906 line2 = I915_READ(reg) & line_mask;
907
908 return line1 == line2;
909}
910
Keith Packardab7ad7f2010-10-03 00:33:06 -0700911/*
912 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300913 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700914 *
915 * After disabling a pipe, we can't wait for vblank in the usual way,
916 * spinning on the vblank interrupt status bit, since we won't actually
917 * see an interrupt when the pipe is disabled.
918 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700919 * On Gen4 and above:
920 * wait for the pipe register state bit to turn off
921 *
922 * Otherwise:
923 * wait for the display line value to settle (it usually
924 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100925 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700926 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300927static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700928{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300929 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700930 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300931 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
932 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700933
Keith Packardab7ad7f2010-10-03 00:33:06 -0700934 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200935 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700936
Keith Packardab7ad7f2010-10-03 00:33:06 -0700937 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100938 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
939 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200940 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700941 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700942 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300943 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200944 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700945 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800946}
947
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000948/*
949 * ibx_digital_port_connected - is the specified port connected?
950 * @dev_priv: i915 private structure
951 * @port: the port to test
952 *
953 * Returns true if @port is connected, false otherwise.
954 */
955bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
956 struct intel_digital_port *port)
957{
958 u32 bit;
959
Damien Lespiauc36346e2012-12-13 16:09:03 +0000960 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +0200961 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000962 case PORT_B:
963 bit = SDE_PORTB_HOTPLUG;
964 break;
965 case PORT_C:
966 bit = SDE_PORTC_HOTPLUG;
967 break;
968 case PORT_D:
969 bit = SDE_PORTD_HOTPLUG;
970 break;
971 default:
972 return true;
973 }
974 } else {
Robin Schroereba905b2014-05-18 02:24:50 +0200975 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000976 case PORT_B:
977 bit = SDE_PORTB_HOTPLUG_CPT;
978 break;
979 case PORT_C:
980 bit = SDE_PORTC_HOTPLUG_CPT;
981 break;
982 case PORT_D:
983 bit = SDE_PORTD_HOTPLUG_CPT;
984 break;
985 default:
986 return true;
987 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000988 }
989
990 return I915_READ(SDEISR) & bit;
991}
992
Jesse Barnesb24e7172011-01-04 15:09:30 -0800993static const char *state_string(bool enabled)
994{
995 return enabled ? "on" : "off";
996}
997
998/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200999void assert_pll(struct drm_i915_private *dev_priv,
1000 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001001{
1002 int reg;
1003 u32 val;
1004 bool cur_state;
1005
1006 reg = DPLL(pipe);
1007 val = I915_READ(reg);
1008 cur_state = !!(val & DPLL_VCO_ENABLE);
1009 WARN(cur_state != state,
1010 "PLL state assertion failure (expected %s, current %s)\n",
1011 state_string(state), state_string(cur_state));
1012}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001013
Jani Nikula23538ef2013-08-27 15:12:22 +03001014/* XXX: the dsi pll is shared between MIPI DSI ports */
1015static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1016{
1017 u32 val;
1018 bool cur_state;
1019
1020 mutex_lock(&dev_priv->dpio_lock);
1021 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1022 mutex_unlock(&dev_priv->dpio_lock);
1023
1024 cur_state = val & DSI_PLL_VCO_EN;
1025 WARN(cur_state != state,
1026 "DSI PLL state assertion failure (expected %s, current %s)\n",
1027 state_string(state), state_string(cur_state));
1028}
1029#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1030#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1031
Daniel Vetter55607e82013-06-16 21:42:39 +02001032struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001033intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001034{
Daniel Vettere2b78262013-06-07 23:10:03 +02001035 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1036
Daniel Vettera43f6e02013-06-07 23:10:32 +02001037 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001038 return NULL;
1039
Daniel Vettera43f6e02013-06-07 23:10:32 +02001040 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001041}
1042
Jesse Barnesb24e7172011-01-04 15:09:30 -08001043/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001044void assert_shared_dpll(struct drm_i915_private *dev_priv,
1045 struct intel_shared_dpll *pll,
1046 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001047{
Jesse Barnes040484a2011-01-03 12:14:26 -08001048 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001049 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001050
Chris Wilson92b27b02012-05-20 18:10:50 +01001051 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001052 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001053 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001054
Daniel Vetter53589012013-06-05 13:34:16 +02001055 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +01001056 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001057 "%s assertion failure (expected %s, current %s)\n",
1058 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001059}
Jesse Barnes040484a2011-01-03 12:14:26 -08001060
1061static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1062 enum pipe pipe, bool state)
1063{
1064 int reg;
1065 u32 val;
1066 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001067 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1068 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001069
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001070 if (HAS_DDI(dev_priv->dev)) {
1071 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001072 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001073 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001074 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001075 } else {
1076 reg = FDI_TX_CTL(pipe);
1077 val = I915_READ(reg);
1078 cur_state = !!(val & FDI_TX_ENABLE);
1079 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001080 WARN(cur_state != state,
1081 "FDI TX state assertion failure (expected %s, current %s)\n",
1082 state_string(state), state_string(cur_state));
1083}
1084#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1085#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1086
1087static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1088 enum pipe pipe, bool state)
1089{
1090 int reg;
1091 u32 val;
1092 bool cur_state;
1093
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001094 reg = FDI_RX_CTL(pipe);
1095 val = I915_READ(reg);
1096 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001097 WARN(cur_state != state,
1098 "FDI RX state assertion failure (expected %s, current %s)\n",
1099 state_string(state), state_string(cur_state));
1100}
1101#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1102#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1103
1104static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1105 enum pipe pipe)
1106{
1107 int reg;
1108 u32 val;
1109
1110 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001111 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001112 return;
1113
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001114 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001115 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001116 return;
1117
Jesse Barnes040484a2011-01-03 12:14:26 -08001118 reg = FDI_TX_CTL(pipe);
1119 val = I915_READ(reg);
1120 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1121}
1122
Daniel Vetter55607e82013-06-16 21:42:39 +02001123void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001125{
1126 int reg;
1127 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001128 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001129
1130 reg = FDI_RX_CTL(pipe);
1131 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001132 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1133 WARN(cur_state != state,
1134 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1135 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001136}
1137
Daniel Vetterb680c372014-09-19 18:27:27 +02001138void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1139 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001140{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001141 struct drm_device *dev = dev_priv->dev;
1142 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001143 u32 val;
1144 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001145 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001146
Jani Nikulabedd4db2014-08-22 15:04:13 +03001147 if (WARN_ON(HAS_DDI(dev)))
1148 return;
1149
1150 if (HAS_PCH_SPLIT(dev)) {
1151 u32 port_sel;
1152
Jesse Barnesea0760c2011-01-04 15:09:32 -08001153 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001154 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1155
1156 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1157 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1158 panel_pipe = PIPE_B;
1159 /* XXX: else fix for eDP */
1160 } else if (IS_VALLEYVIEW(dev)) {
1161 /* presumably write lock depends on pipe, not port select */
1162 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1163 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001164 } else {
1165 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001166 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1167 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001168 }
1169
1170 val = I915_READ(pp_reg);
1171 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001172 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001173 locked = false;
1174
Jesse Barnesea0760c2011-01-04 15:09:32 -08001175 WARN(panel_pipe == pipe && locked,
1176 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001177 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001178}
1179
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001180static void assert_cursor(struct drm_i915_private *dev_priv,
1181 enum pipe pipe, bool state)
1182{
1183 struct drm_device *dev = dev_priv->dev;
1184 bool cur_state;
1185
Paulo Zanonid9d82082014-02-27 16:30:56 -03001186 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001187 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001188 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001189 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001190
1191 WARN(cur_state != state,
1192 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1193 pipe_name(pipe), state_string(state), state_string(cur_state));
1194}
1195#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1196#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1197
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001198void assert_pipe(struct drm_i915_private *dev_priv,
1199 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001200{
1201 int reg;
1202 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001203 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001204 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1205 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001206
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001207 /* if we need the pipe quirk it must be always on */
1208 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1209 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001210 state = true;
1211
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001212 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001213 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001214 cur_state = false;
1215 } else {
1216 reg = PIPECONF(cpu_transcoder);
1217 val = I915_READ(reg);
1218 cur_state = !!(val & PIPECONF_ENABLE);
1219 }
1220
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001221 WARN(cur_state != state,
1222 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001223 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001224}
1225
Chris Wilson931872f2012-01-16 23:01:13 +00001226static void assert_plane(struct drm_i915_private *dev_priv,
1227 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001228{
1229 int reg;
1230 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001231 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001232
1233 reg = DSPCNTR(plane);
1234 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001235 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1236 WARN(cur_state != state,
1237 "plane %c assertion failure (expected %s, current %s)\n",
1238 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001239}
1240
Chris Wilson931872f2012-01-16 23:01:13 +00001241#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1242#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1243
Jesse Barnesb24e7172011-01-04 15:09:30 -08001244static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1245 enum pipe pipe)
1246{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001247 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001248 int reg, i;
1249 u32 val;
1250 int cur_pipe;
1251
Ville Syrjälä653e1022013-06-04 13:49:05 +03001252 /* Primary planes are fixed to pipes on gen4+ */
1253 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001254 reg = DSPCNTR(pipe);
1255 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001256 WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001257 "plane %c assertion failure, should be disabled but not\n",
1258 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001259 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001260 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001261
Jesse Barnesb24e7172011-01-04 15:09:30 -08001262 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001263 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001264 reg = DSPCNTR(i);
1265 val = I915_READ(reg);
1266 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1267 DISPPLANE_SEL_PIPE_SHIFT;
1268 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001269 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1270 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001271 }
1272}
1273
Jesse Barnes19332d72013-03-28 09:55:38 -07001274static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1275 enum pipe pipe)
1276{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001277 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001278 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001279 u32 val;
1280
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001281 if (INTEL_INFO(dev)->gen >= 9) {
1282 for_each_sprite(pipe, sprite) {
1283 val = I915_READ(PLANE_CTL(pipe, sprite));
1284 WARN(val & PLANE_CTL_ENABLE,
1285 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1286 sprite, pipe_name(pipe));
1287 }
1288 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001289 for_each_sprite(pipe, sprite) {
1290 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001291 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001292 WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001293 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001294 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001295 }
1296 } else if (INTEL_INFO(dev)->gen >= 7) {
1297 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001298 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001299 WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001300 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001301 plane_name(pipe), pipe_name(pipe));
1302 } else if (INTEL_INFO(dev)->gen >= 5) {
1303 reg = DVSCNTR(pipe);
1304 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001305 WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001306 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1307 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001308 }
1309}
1310
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001311static void assert_vblank_disabled(struct drm_crtc *crtc)
1312{
1313 if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1314 drm_crtc_vblank_put(crtc);
1315}
1316
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001317static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001318{
1319 u32 val;
1320 bool enabled;
1321
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001322 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001323
Jesse Barnes92f25842011-01-04 15:09:34 -08001324 val = I915_READ(PCH_DREF_CONTROL);
1325 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1326 DREF_SUPERSPREAD_SOURCE_MASK));
1327 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1328}
1329
Daniel Vetterab9412b2013-05-03 11:49:46 +02001330static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1331 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001332{
1333 int reg;
1334 u32 val;
1335 bool enabled;
1336
Daniel Vetterab9412b2013-05-03 11:49:46 +02001337 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001338 val = I915_READ(reg);
1339 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001340 WARN(enabled,
1341 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1342 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001343}
1344
Keith Packard4e634382011-08-06 10:39:45 -07001345static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1346 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001347{
1348 if ((val & DP_PORT_EN) == 0)
1349 return false;
1350
1351 if (HAS_PCH_CPT(dev_priv->dev)) {
1352 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1353 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1354 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1355 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001356 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1357 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1358 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001359 } else {
1360 if ((val & DP_PIPE_MASK) != (pipe << 30))
1361 return false;
1362 }
1363 return true;
1364}
1365
Keith Packard1519b992011-08-06 10:35:34 -07001366static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1367 enum pipe pipe, u32 val)
1368{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001369 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001370 return false;
1371
1372 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001373 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001374 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001375 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1376 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1377 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001378 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001379 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001380 return false;
1381 }
1382 return true;
1383}
1384
1385static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1386 enum pipe pipe, u32 val)
1387{
1388 if ((val & LVDS_PORT_EN) == 0)
1389 return false;
1390
1391 if (HAS_PCH_CPT(dev_priv->dev)) {
1392 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1393 return false;
1394 } else {
1395 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1396 return false;
1397 }
1398 return true;
1399}
1400
1401static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1402 enum pipe pipe, u32 val)
1403{
1404 if ((val & ADPA_DAC_ENABLE) == 0)
1405 return false;
1406 if (HAS_PCH_CPT(dev_priv->dev)) {
1407 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1408 return false;
1409 } else {
1410 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1411 return false;
1412 }
1413 return true;
1414}
1415
Jesse Barnes291906f2011-02-02 12:28:03 -08001416static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001417 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001418{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001419 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001420 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001421 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001422 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001423
Daniel Vetter75c5da22012-09-10 21:58:29 +02001424 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1425 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001426 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001427}
1428
1429static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1430 enum pipe pipe, int reg)
1431{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001432 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001433 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001434 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001435 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001436
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001437 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001438 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001439 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001440}
1441
1442static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1443 enum pipe pipe)
1444{
1445 int reg;
1446 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001447
Keith Packardf0575e92011-07-25 22:12:43 -07001448 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1449 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1450 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001451
1452 reg = PCH_ADPA;
1453 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001454 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001455 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001456 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001457
1458 reg = PCH_LVDS;
1459 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001460 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001461 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001462 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001463
Paulo Zanonie2debe92013-02-18 19:00:27 -03001464 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1465 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1466 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001467}
1468
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001469static void intel_init_dpio(struct drm_device *dev)
1470{
1471 struct drm_i915_private *dev_priv = dev->dev_private;
1472
1473 if (!IS_VALLEYVIEW(dev))
1474 return;
1475
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001476 /*
1477 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1478 * CHV x1 PHY (DP/HDMI D)
1479 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1480 */
1481 if (IS_CHERRYVIEW(dev)) {
1482 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1483 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1484 } else {
1485 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1486 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001487}
1488
Ville Syrjäläd288f652014-10-28 13:20:22 +02001489static void vlv_enable_pll(struct intel_crtc *crtc,
1490 const struct intel_crtc_config *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001491{
Daniel Vetter426115c2013-07-11 22:13:42 +02001492 struct drm_device *dev = crtc->base.dev;
1493 struct drm_i915_private *dev_priv = dev->dev_private;
1494 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001495 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001496
Daniel Vetter426115c2013-07-11 22:13:42 +02001497 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001498
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001499 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001500 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1501
1502 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001503 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001504 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001505
Daniel Vetter426115c2013-07-11 22:13:42 +02001506 I915_WRITE(reg, dpll);
1507 POSTING_READ(reg);
1508 udelay(150);
1509
1510 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1511 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1512
Ville Syrjäläd288f652014-10-28 13:20:22 +02001513 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001514 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001515
1516 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001517 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001518 POSTING_READ(reg);
1519 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001520 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001521 POSTING_READ(reg);
1522 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001523 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001524 POSTING_READ(reg);
1525 udelay(150); /* wait for warmup */
1526}
1527
Ville Syrjäläd288f652014-10-28 13:20:22 +02001528static void chv_enable_pll(struct intel_crtc *crtc,
1529 const struct intel_crtc_config *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001530{
1531 struct drm_device *dev = crtc->base.dev;
1532 struct drm_i915_private *dev_priv = dev->dev_private;
1533 int pipe = crtc->pipe;
1534 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001535 u32 tmp;
1536
1537 assert_pipe_disabled(dev_priv, crtc->pipe);
1538
1539 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1540
1541 mutex_lock(&dev_priv->dpio_lock);
1542
1543 /* Enable back the 10bit clock to display controller */
1544 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1545 tmp |= DPIO_DCLKP_EN;
1546 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1547
1548 /*
1549 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1550 */
1551 udelay(1);
1552
1553 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001554 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001555
1556 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001557 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001558 DRM_ERROR("PLL %d failed to lock\n", pipe);
1559
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001560 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001561 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001562 POSTING_READ(DPLL_MD(pipe));
1563
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001564 mutex_unlock(&dev_priv->dpio_lock);
1565}
1566
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001567static int intel_num_dvo_pipes(struct drm_device *dev)
1568{
1569 struct intel_crtc *crtc;
1570 int count = 0;
1571
1572 for_each_intel_crtc(dev, crtc)
1573 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001574 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001575
1576 return count;
1577}
1578
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001579static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001580{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001581 struct drm_device *dev = crtc->base.dev;
1582 struct drm_i915_private *dev_priv = dev->dev_private;
1583 int reg = DPLL(crtc->pipe);
1584 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001585
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001586 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001587
1588 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001589 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001590
1591 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001592 if (IS_MOBILE(dev) && !IS_I830(dev))
1593 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001594
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001595 /* Enable DVO 2x clock on both PLLs if necessary */
1596 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1597 /*
1598 * It appears to be important that we don't enable this
1599 * for the current pipe before otherwise configuring the
1600 * PLL. No idea how this should be handled if multiple
1601 * DVO outputs are enabled simultaneosly.
1602 */
1603 dpll |= DPLL_DVO_2X_MODE;
1604 I915_WRITE(DPLL(!crtc->pipe),
1605 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1606 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001607
1608 /* Wait for the clocks to stabilize. */
1609 POSTING_READ(reg);
1610 udelay(150);
1611
1612 if (INTEL_INFO(dev)->gen >= 4) {
1613 I915_WRITE(DPLL_MD(crtc->pipe),
1614 crtc->config.dpll_hw_state.dpll_md);
1615 } else {
1616 /* The pixel multiplier can only be updated once the
1617 * DPLL is enabled and the clocks are stable.
1618 *
1619 * So write it again.
1620 */
1621 I915_WRITE(reg, dpll);
1622 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001623
1624 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001625 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001628 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001629 POSTING_READ(reg);
1630 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001631 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001632 POSTING_READ(reg);
1633 udelay(150); /* wait for warmup */
1634}
1635
1636/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001637 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001638 * @dev_priv: i915 private structure
1639 * @pipe: pipe PLL to disable
1640 *
1641 * Disable the PLL for @pipe, making sure the pipe is off first.
1642 *
1643 * Note! This is for pre-ILK only.
1644 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001645static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001646{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001647 struct drm_device *dev = crtc->base.dev;
1648 struct drm_i915_private *dev_priv = dev->dev_private;
1649 enum pipe pipe = crtc->pipe;
1650
1651 /* Disable DVO 2x clock on both PLLs if necessary */
1652 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001653 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001654 intel_num_dvo_pipes(dev) == 1) {
1655 I915_WRITE(DPLL(PIPE_B),
1656 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1657 I915_WRITE(DPLL(PIPE_A),
1658 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1659 }
1660
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001661 /* Don't disable pipe or pipe PLLs if needed */
1662 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1663 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001664 return;
1665
1666 /* Make sure the pipe isn't still relying on us */
1667 assert_pipe_disabled(dev_priv, pipe);
1668
Daniel Vetter50b44a42013-06-05 13:34:33 +02001669 I915_WRITE(DPLL(pipe), 0);
1670 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001671}
1672
Jesse Barnesf6071162013-10-01 10:41:38 -07001673static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1674{
1675 u32 val = 0;
1676
1677 /* Make sure the pipe isn't still relying on us */
1678 assert_pipe_disabled(dev_priv, pipe);
1679
Imre Deake5cbfbf2014-01-09 17:08:16 +02001680 /*
1681 * Leave integrated clock source and reference clock enabled for pipe B.
1682 * The latter is needed for VGA hotplug / manual detection.
1683 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001684 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001685 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001686 I915_WRITE(DPLL(pipe), val);
1687 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001688
1689}
1690
1691static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1692{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001693 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001694 u32 val;
1695
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001696 /* Make sure the pipe isn't still relying on us */
1697 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001698
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001699 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001700 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001701 if (pipe != PIPE_A)
1702 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1703 I915_WRITE(DPLL(pipe), val);
1704 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001705
1706 mutex_lock(&dev_priv->dpio_lock);
1707
1708 /* Disable 10bit clock to display controller */
1709 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1710 val &= ~DPIO_DCLKP_EN;
1711 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1712
Ville Syrjälä61407f62014-05-27 16:32:55 +03001713 /* disable left/right clock distribution */
1714 if (pipe != PIPE_B) {
1715 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1716 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1717 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1718 } else {
1719 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1720 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1721 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1722 }
1723
Ville Syrjäläd7520482014-04-09 13:28:59 +03001724 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001725}
1726
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001727void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1728 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001729{
1730 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001731 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001732
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001733 switch (dport->port) {
1734 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001735 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001736 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001737 break;
1738 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001739 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001740 dpll_reg = DPLL(0);
1741 break;
1742 case PORT_D:
1743 port_mask = DPLL_PORTD_READY_MASK;
1744 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001745 break;
1746 default:
1747 BUG();
1748 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001749
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001750 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001751 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001752 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001753}
1754
Daniel Vetterb14b1052014-04-24 23:55:13 +02001755static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1756{
1757 struct drm_device *dev = crtc->base.dev;
1758 struct drm_i915_private *dev_priv = dev->dev_private;
1759 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1760
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001761 if (WARN_ON(pll == NULL))
1762 return;
1763
Daniel Vetterb14b1052014-04-24 23:55:13 +02001764 WARN_ON(!pll->refcount);
1765 if (pll->active == 0) {
1766 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1767 WARN_ON(pll->on);
1768 assert_shared_dpll_disabled(dev_priv, pll);
1769
1770 pll->mode_set(dev_priv, pll);
1771 }
1772}
1773
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001774/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001775 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001776 * @dev_priv: i915 private structure
1777 * @pipe: pipe PLL to enable
1778 *
1779 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1780 * drives the transcoder clock.
1781 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001782static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001783{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001784 struct drm_device *dev = crtc->base.dev;
1785 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001786 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001787
Daniel Vetter87a875b2013-06-05 13:34:19 +02001788 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001789 return;
1790
1791 if (WARN_ON(pll->refcount == 0))
1792 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001793
Damien Lespiau74dd6922014-07-29 18:06:17 +01001794 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001795 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001796 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001797
Daniel Vettercdbd2312013-06-05 13:34:03 +02001798 if (pll->active++) {
1799 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001800 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001801 return;
1802 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001803 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001804
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001805 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1806
Daniel Vetter46edb022013-06-05 13:34:12 +02001807 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001808 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001809 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001810}
1811
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001812static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001813{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001814 struct drm_device *dev = crtc->base.dev;
1815 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001816 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001817
Jesse Barnes92f25842011-01-04 15:09:34 -08001818 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001819 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001820 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001821 return;
1822
Chris Wilson48da64a2012-05-13 20:16:12 +01001823 if (WARN_ON(pll->refcount == 0))
1824 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001825
Daniel Vetter46edb022013-06-05 13:34:12 +02001826 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1827 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001828 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001829
Chris Wilson48da64a2012-05-13 20:16:12 +01001830 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001831 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001832 return;
1833 }
1834
Daniel Vettere9d69442013-06-05 13:34:15 +02001835 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001836 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001837 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001838 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001839
Daniel Vetter46edb022013-06-05 13:34:12 +02001840 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001841 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001842 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001843
1844 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001845}
1846
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001847static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1848 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001849{
Daniel Vetter23670b322012-11-01 09:15:30 +01001850 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001851 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001853 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001854
1855 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001856 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001857
1858 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001859 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001860 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001861
1862 /* FDI must be feeding us bits for PCH ports */
1863 assert_fdi_tx_enabled(dev_priv, pipe);
1864 assert_fdi_rx_enabled(dev_priv, pipe);
1865
Daniel Vetter23670b322012-11-01 09:15:30 +01001866 if (HAS_PCH_CPT(dev)) {
1867 /* Workaround: Set the timing override bit before enabling the
1868 * pch transcoder. */
1869 reg = TRANS_CHICKEN2(pipe);
1870 val = I915_READ(reg);
1871 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1872 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001873 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001874
Daniel Vetterab9412b2013-05-03 11:49:46 +02001875 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001876 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001877 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001878
1879 if (HAS_PCH_IBX(dev_priv->dev)) {
1880 /*
1881 * make the BPC in transcoder be consistent with
1882 * that in pipeconf reg.
1883 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001884 val &= ~PIPECONF_BPC_MASK;
1885 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001886 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001887
1888 val &= ~TRANS_INTERLACE_MASK;
1889 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001890 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001891 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001892 val |= TRANS_LEGACY_INTERLACED_ILK;
1893 else
1894 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001895 else
1896 val |= TRANS_PROGRESSIVE;
1897
Jesse Barnes040484a2011-01-03 12:14:26 -08001898 I915_WRITE(reg, val | TRANS_ENABLE);
1899 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001900 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001901}
1902
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001903static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001904 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001905{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001906 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001907
1908 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001909 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001910
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001912 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001913 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001914
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001915 /* Workaround: set timing override bit. */
1916 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001917 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001918 I915_WRITE(_TRANSA_CHICKEN2, val);
1919
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001920 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001921 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001922
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001923 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1924 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001925 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001926 else
1927 val |= TRANS_PROGRESSIVE;
1928
Daniel Vetterab9412b2013-05-03 11:49:46 +02001929 I915_WRITE(LPT_TRANSCONF, val);
1930 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001931 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001932}
1933
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001934static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1935 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001936{
Daniel Vetter23670b322012-11-01 09:15:30 +01001937 struct drm_device *dev = dev_priv->dev;
1938 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001939
1940 /* FDI relies on the transcoder */
1941 assert_fdi_tx_disabled(dev_priv, pipe);
1942 assert_fdi_rx_disabled(dev_priv, pipe);
1943
Jesse Barnes291906f2011-02-02 12:28:03 -08001944 /* Ports must be off as well */
1945 assert_pch_ports_disabled(dev_priv, pipe);
1946
Daniel Vetterab9412b2013-05-03 11:49:46 +02001947 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001948 val = I915_READ(reg);
1949 val &= ~TRANS_ENABLE;
1950 I915_WRITE(reg, val);
1951 /* wait for PCH transcoder off, transcoder state */
1952 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001953 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001954
1955 if (!HAS_PCH_IBX(dev)) {
1956 /* Workaround: Clear the timing override chicken bit again. */
1957 reg = TRANS_CHICKEN2(pipe);
1958 val = I915_READ(reg);
1959 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1960 I915_WRITE(reg, val);
1961 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001962}
1963
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001964static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001965{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001966 u32 val;
1967
Daniel Vetterab9412b2013-05-03 11:49:46 +02001968 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001969 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001970 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001971 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001972 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001973 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001974
1975 /* Workaround: clear timing override bit. */
1976 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001977 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001978 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001979}
1980
1981/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001982 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001983 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001984 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001985 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001986 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001987 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001988static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001989{
Paulo Zanoni03722642014-01-17 13:51:09 -02001990 struct drm_device *dev = crtc->base.dev;
1991 struct drm_i915_private *dev_priv = dev->dev_private;
1992 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001993 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1994 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001995 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001996 int reg;
1997 u32 val;
1998
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001999 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002000 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002001 assert_sprites_disabled(dev_priv, pipe);
2002
Paulo Zanoni681e5812012-12-06 11:12:38 -02002003 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002004 pch_transcoder = TRANSCODER_A;
2005 else
2006 pch_transcoder = pipe;
2007
Jesse Barnesb24e7172011-01-04 15:09:30 -08002008 /*
2009 * A pipe without a PLL won't actually be able to drive bits from
2010 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2011 * need the check.
2012 */
2013 if (!HAS_PCH_SPLIT(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002014 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002015 assert_dsi_pll_enabled(dev_priv);
2016 else
2017 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002018 else {
Paulo Zanoni30421c42014-01-17 13:51:10 -02002019 if (crtc->config.has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002020 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002021 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002022 assert_fdi_tx_pll_enabled(dev_priv,
2023 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002024 }
2025 /* FIXME: assert CPU port conditions for SNB+ */
2026 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002028 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002029 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002030 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002031 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2032 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002033 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002034 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002035
2036 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002037 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002038}
2039
2040/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002041 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002042 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002043 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002044 * Disable the pipe of @crtc, making sure that various hardware
2045 * specific requirements are met, if applicable, e.g. plane
2046 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047 *
2048 * Will wait until the pipe has shut down before returning.
2049 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002050static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002051{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002052 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2053 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2054 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002055 int reg;
2056 u32 val;
2057
2058 /*
2059 * Make sure planes won't keep trying to pump pixels to us,
2060 * or we might hang the display.
2061 */
2062 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002063 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002064 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002065
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002066 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002067 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002068 if ((val & PIPECONF_ENABLE) == 0)
2069 return;
2070
Ville Syrjälä67adc642014-08-15 01:21:57 +03002071 /*
2072 * Double wide has implications for planes
2073 * so best keep it disabled when not needed.
2074 */
2075 if (crtc->config.double_wide)
2076 val &= ~PIPECONF_DOUBLE_WIDE;
2077
2078 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002079 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2080 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002081 val &= ~PIPECONF_ENABLE;
2082
2083 I915_WRITE(reg, val);
2084 if ((val & PIPECONF_ENABLE) == 0)
2085 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002086}
2087
Keith Packardd74362c2011-07-28 14:47:14 -07002088/*
2089 * Plane regs are double buffered, going from enabled->disabled needs a
2090 * trigger in order to latch. The display address reg provides this.
2091 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002092void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2093 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002094{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002095 struct drm_device *dev = dev_priv->dev;
2096 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002097
2098 I915_WRITE(reg, I915_READ(reg));
2099 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002100}
2101
Jesse Barnesb24e7172011-01-04 15:09:30 -08002102/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002103 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002104 * @plane: plane to be enabled
2105 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002106 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002107 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002109static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2110 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002111{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002112 struct drm_device *dev = plane->dev;
2113 struct drm_i915_private *dev_priv = dev->dev_private;
2114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002115
2116 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002117 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002118
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002119 if (intel_crtc->primary_enabled)
2120 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002121
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002122 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002123
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002124 dev_priv->display.update_primary_plane(crtc, plane->fb,
2125 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002126
2127 /*
2128 * BDW signals flip done immediately if the plane
2129 * is disabled, even if the plane enable is already
2130 * armed to occur at the next vblank :(
2131 */
2132 if (IS_BROADWELL(dev))
2133 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002134}
2135
Jesse Barnesb24e7172011-01-04 15:09:30 -08002136/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002137 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002138 * @plane: plane to be disabled
2139 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002140 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002141 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002142 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002143static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2144 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002146 struct drm_device *dev = plane->dev;
2147 struct drm_i915_private *dev_priv = dev->dev_private;
2148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2149
2150 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002151
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002152 if (!intel_crtc->primary_enabled)
2153 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002154
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002155 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002156
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002157 dev_priv->display.update_primary_plane(crtc, plane->fb,
2158 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159}
2160
Chris Wilson693db182013-03-05 14:52:39 +00002161static bool need_vtd_wa(struct drm_device *dev)
2162{
2163#ifdef CONFIG_INTEL_IOMMU
2164 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2165 return true;
2166#endif
2167 return false;
2168}
2169
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002170static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2171{
2172 int tile_height;
2173
2174 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2175 return ALIGN(height, tile_height);
2176}
2177
Chris Wilson127bd2a2010-07-23 23:32:05 +01002178int
Chris Wilson48b956c2010-09-14 12:50:34 +01002179intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002180 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002181 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002182{
Chris Wilsonce453d82011-02-21 14:43:56 +00002183 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002184 u32 alignment;
2185 int ret;
2186
Matt Roperebcdd392014-07-09 16:22:11 -07002187 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2188
Chris Wilson05394f32010-11-08 19:18:58 +00002189 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002190 case I915_TILING_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002191 if (INTEL_INFO(dev)->gen >= 9)
2192 alignment = 256 * 1024;
2193 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002194 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002195 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002196 alignment = 4 * 1024;
2197 else
2198 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002199 break;
2200 case I915_TILING_X:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002201 if (INTEL_INFO(dev)->gen >= 9)
2202 alignment = 256 * 1024;
2203 else {
2204 /* pin() will align the object as required by fence */
2205 alignment = 0;
2206 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002207 break;
2208 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02002209 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002210 return -EINVAL;
2211 default:
2212 BUG();
2213 }
2214
Chris Wilson693db182013-03-05 14:52:39 +00002215 /* Note that the w/a also requires 64 PTE of padding following the
2216 * bo. We currently fill all unused PTE with the shadow page and so
2217 * we should always have valid PTE following the scanout preventing
2218 * the VT-d warning.
2219 */
2220 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2221 alignment = 256 * 1024;
2222
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002223 /*
2224 * Global gtt pte registers are special registers which actually forward
2225 * writes to a chunk of system memory. Which means that there is no risk
2226 * that the register values disappear as soon as we call
2227 * intel_runtime_pm_put(), so it is correct to wrap only the
2228 * pin/unpin/fence and not more.
2229 */
2230 intel_runtime_pm_get(dev_priv);
2231
Chris Wilsonce453d82011-02-21 14:43:56 +00002232 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002233 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002234 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002235 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002236
2237 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2238 * fence, whereas 965+ only requires a fence if using
2239 * framebuffer compression. For simplicity, we always install
2240 * a fence as the cost is not that onerous.
2241 */
Chris Wilson06d98132012-04-17 15:31:24 +01002242 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002243 if (ret)
2244 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002245
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002246 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002247
Chris Wilsonce453d82011-02-21 14:43:56 +00002248 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002249 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002250 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002251
2252err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002253 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002254err_interruptible:
2255 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002256 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002257 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002258}
2259
Chris Wilson1690e1e2011-12-14 13:57:08 +01002260void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2261{
Matt Roperebcdd392014-07-09 16:22:11 -07002262 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2263
Chris Wilson1690e1e2011-12-14 13:57:08 +01002264 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002265 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002266}
2267
Daniel Vetterc2c75132012-07-05 12:17:30 +02002268/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2269 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002270unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2271 unsigned int tiling_mode,
2272 unsigned int cpp,
2273 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002274{
Chris Wilsonbc752862013-02-21 20:04:31 +00002275 if (tiling_mode != I915_TILING_NONE) {
2276 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002277
Chris Wilsonbc752862013-02-21 20:04:31 +00002278 tile_rows = *y / 8;
2279 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002280
Chris Wilsonbc752862013-02-21 20:04:31 +00002281 tiles = *x / (512/cpp);
2282 *x %= 512/cpp;
2283
2284 return tile_rows * pitch * 8 + tiles * 4096;
2285 } else {
2286 unsigned int offset;
2287
2288 offset = *y * pitch + *x * cpp;
2289 *y = 0;
2290 *x = (offset & 4095) / cpp;
2291 return offset & -4096;
2292 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002293}
2294
Jesse Barnes46f297f2014-03-07 08:57:48 -08002295int intel_format_to_fourcc(int format)
2296{
2297 switch (format) {
2298 case DISPPLANE_8BPP:
2299 return DRM_FORMAT_C8;
2300 case DISPPLANE_BGRX555:
2301 return DRM_FORMAT_XRGB1555;
2302 case DISPPLANE_BGRX565:
2303 return DRM_FORMAT_RGB565;
2304 default:
2305 case DISPPLANE_BGRX888:
2306 return DRM_FORMAT_XRGB8888;
2307 case DISPPLANE_RGBX888:
2308 return DRM_FORMAT_XBGR8888;
2309 case DISPPLANE_BGRX101010:
2310 return DRM_FORMAT_XRGB2101010;
2311 case DISPPLANE_RGBX101010:
2312 return DRM_FORMAT_XBGR2101010;
2313 }
2314}
2315
Jesse Barnes484b41d2014-03-07 08:57:55 -08002316static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -08002317 struct intel_plane_config *plane_config)
2318{
2319 struct drm_device *dev = crtc->base.dev;
2320 struct drm_i915_gem_object *obj = NULL;
2321 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2322 u32 base = plane_config->base;
2323
Chris Wilsonff2652e2014-03-10 08:07:02 +00002324 if (plane_config->size == 0)
2325 return false;
2326
Jesse Barnes46f297f2014-03-07 08:57:48 -08002327 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2328 plane_config->size);
2329 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002330 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002331
2332 if (plane_config->tiled) {
2333 obj->tiling_mode = I915_TILING_X;
Dave Airlie66e514c2014-04-03 07:51:54 +10002334 obj->stride = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002335 }
2336
Dave Airlie66e514c2014-04-03 07:51:54 +10002337 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2338 mode_cmd.width = crtc->base.primary->fb->width;
2339 mode_cmd.height = crtc->base.primary->fb->height;
2340 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002341
2342 mutex_lock(&dev->struct_mutex);
2343
Dave Airlie66e514c2014-04-03 07:51:54 +10002344 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002345 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002346 DRM_DEBUG_KMS("intel fb init failed\n");
2347 goto out_unref_obj;
2348 }
2349
Daniel Vettera071fa02014-06-18 23:28:09 +02002350 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002351 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002352
2353 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2354 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002355
2356out_unref_obj:
2357 drm_gem_object_unreference(&obj->base);
2358 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002359 return false;
2360}
2361
2362static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2363 struct intel_plane_config *plane_config)
2364{
2365 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002366 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002367 struct drm_crtc *c;
2368 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002369 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002370
Dave Airlie66e514c2014-04-03 07:51:54 +10002371 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002372 return;
2373
2374 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2375 return;
2376
Dave Airlie66e514c2014-04-03 07:51:54 +10002377 kfree(intel_crtc->base.primary->fb);
2378 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002379
2380 /*
2381 * Failed to alloc the obj, check to see if we should share
2382 * an fb with another CRTC instead
2383 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002384 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002385 i = to_intel_crtc(c);
2386
2387 if (c == &intel_crtc->base)
2388 continue;
2389
Matt Roper2ff8fde2014-07-08 07:50:07 -07002390 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002391 continue;
2392
Matt Roper2ff8fde2014-07-08 07:50:07 -07002393 obj = intel_fb_obj(c->primary->fb);
2394 if (obj == NULL)
2395 continue;
2396
2397 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002398 if (obj->tiling_mode != I915_TILING_NONE)
2399 dev_priv->preserve_bios_swizzle = true;
2400
Dave Airlie66e514c2014-04-03 07:51:54 +10002401 drm_framebuffer_reference(c->primary->fb);
2402 intel_crtc->base.primary->fb = c->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002403 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002404 break;
2405 }
2406 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002407}
2408
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002409static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2410 struct drm_framebuffer *fb,
2411 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002412{
2413 struct drm_device *dev = crtc->dev;
2414 struct drm_i915_private *dev_priv = dev->dev_private;
2415 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002416 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002417 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002418 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002419 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002420 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302421 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002422
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002423 if (!intel_crtc->primary_enabled) {
2424 I915_WRITE(reg, 0);
2425 if (INTEL_INFO(dev)->gen >= 4)
2426 I915_WRITE(DSPSURF(plane), 0);
2427 else
2428 I915_WRITE(DSPADDR(plane), 0);
2429 POSTING_READ(reg);
2430 return;
2431 }
2432
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002433 obj = intel_fb_obj(fb);
2434 if (WARN_ON(obj == NULL))
2435 return;
2436
2437 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2438
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002439 dspcntr = DISPPLANE_GAMMA_ENABLE;
2440
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002441 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002442
2443 if (INTEL_INFO(dev)->gen < 4) {
2444 if (intel_crtc->pipe == PIPE_B)
2445 dspcntr |= DISPPLANE_SEL_PIPE_B;
2446
2447 /* pipesrc and dspsize control the size that is scaled from,
2448 * which should always be the user's requested size.
2449 */
2450 I915_WRITE(DSPSIZE(plane),
2451 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2452 (intel_crtc->config.pipe_src_w - 1));
2453 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002454 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2455 I915_WRITE(PRIMSIZE(plane),
2456 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2457 (intel_crtc->config.pipe_src_w - 1));
2458 I915_WRITE(PRIMPOS(plane), 0);
2459 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002460 }
2461
Ville Syrjälä57779d02012-10-31 17:50:14 +02002462 switch (fb->pixel_format) {
2463 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002464 dspcntr |= DISPPLANE_8BPP;
2465 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002466 case DRM_FORMAT_XRGB1555:
2467 case DRM_FORMAT_ARGB1555:
2468 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002469 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002470 case DRM_FORMAT_RGB565:
2471 dspcntr |= DISPPLANE_BGRX565;
2472 break;
2473 case DRM_FORMAT_XRGB8888:
2474 case DRM_FORMAT_ARGB8888:
2475 dspcntr |= DISPPLANE_BGRX888;
2476 break;
2477 case DRM_FORMAT_XBGR8888:
2478 case DRM_FORMAT_ABGR8888:
2479 dspcntr |= DISPPLANE_RGBX888;
2480 break;
2481 case DRM_FORMAT_XRGB2101010:
2482 case DRM_FORMAT_ARGB2101010:
2483 dspcntr |= DISPPLANE_BGRX101010;
2484 break;
2485 case DRM_FORMAT_XBGR2101010:
2486 case DRM_FORMAT_ABGR2101010:
2487 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002488 break;
2489 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002490 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002491 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002492
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002493 if (INTEL_INFO(dev)->gen >= 4 &&
2494 obj->tiling_mode != I915_TILING_NONE)
2495 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002496
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002497 if (IS_G4X(dev))
2498 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2499
Ville Syrjäläb98971272014-08-27 16:51:22 +03002500 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002501
Daniel Vetterc2c75132012-07-05 12:17:30 +02002502 if (INTEL_INFO(dev)->gen >= 4) {
2503 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002504 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002505 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002506 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002507 linear_offset -= intel_crtc->dspaddr_offset;
2508 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002509 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002510 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002511
Sonika Jindal48404c12014-08-22 14:06:04 +05302512 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2513 dspcntr |= DISPPLANE_ROTATE_180;
2514
2515 x += (intel_crtc->config.pipe_src_w - 1);
2516 y += (intel_crtc->config.pipe_src_h - 1);
2517
2518 /* Finding the last pixel of the last line of the display
2519 data and adding to linear_offset*/
2520 linear_offset +=
2521 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2522 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2523 }
2524
2525 I915_WRITE(reg, dspcntr);
2526
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002527 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2528 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2529 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002530 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002531 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002532 I915_WRITE(DSPSURF(plane),
2533 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002534 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002535 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002536 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002537 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002538 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002539}
2540
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002541static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2542 struct drm_framebuffer *fb,
2543 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002544{
2545 struct drm_device *dev = crtc->dev;
2546 struct drm_i915_private *dev_priv = dev->dev_private;
2547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002548 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002549 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002550 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002551 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002552 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302553 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002554
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002555 if (!intel_crtc->primary_enabled) {
2556 I915_WRITE(reg, 0);
2557 I915_WRITE(DSPSURF(plane), 0);
2558 POSTING_READ(reg);
2559 return;
2560 }
2561
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002562 obj = intel_fb_obj(fb);
2563 if (WARN_ON(obj == NULL))
2564 return;
2565
2566 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2567
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002568 dspcntr = DISPPLANE_GAMMA_ENABLE;
2569
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002570 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002571
2572 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2573 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2574
Ville Syrjälä57779d02012-10-31 17:50:14 +02002575 switch (fb->pixel_format) {
2576 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002577 dspcntr |= DISPPLANE_8BPP;
2578 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002579 case DRM_FORMAT_RGB565:
2580 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002581 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002582 case DRM_FORMAT_XRGB8888:
2583 case DRM_FORMAT_ARGB8888:
2584 dspcntr |= DISPPLANE_BGRX888;
2585 break;
2586 case DRM_FORMAT_XBGR8888:
2587 case DRM_FORMAT_ABGR8888:
2588 dspcntr |= DISPPLANE_RGBX888;
2589 break;
2590 case DRM_FORMAT_XRGB2101010:
2591 case DRM_FORMAT_ARGB2101010:
2592 dspcntr |= DISPPLANE_BGRX101010;
2593 break;
2594 case DRM_FORMAT_XBGR2101010:
2595 case DRM_FORMAT_ABGR2101010:
2596 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002597 break;
2598 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002599 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002600 }
2601
2602 if (obj->tiling_mode != I915_TILING_NONE)
2603 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002604
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002605 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002606 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002607
Ville Syrjäläb98971272014-08-27 16:51:22 +03002608 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002609 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002610 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002611 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002612 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002613 linear_offset -= intel_crtc->dspaddr_offset;
Sonika Jindal48404c12014-08-22 14:06:04 +05302614 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2615 dspcntr |= DISPPLANE_ROTATE_180;
2616
2617 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2618 x += (intel_crtc->config.pipe_src_w - 1);
2619 y += (intel_crtc->config.pipe_src_h - 1);
2620
2621 /* Finding the last pixel of the last line of the display
2622 data and adding to linear_offset*/
2623 linear_offset +=
2624 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2625 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2626 }
2627 }
2628
2629 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002630
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002631 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2632 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2633 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002634 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002635 I915_WRITE(DSPSURF(plane),
2636 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002637 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002638 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2639 } else {
2640 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2641 I915_WRITE(DSPLINOFF(plane), linear_offset);
2642 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002643 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002644}
2645
Damien Lespiau70d21f02013-07-03 21:06:04 +01002646static void skylake_update_primary_plane(struct drm_crtc *crtc,
2647 struct drm_framebuffer *fb,
2648 int x, int y)
2649{
2650 struct drm_device *dev = crtc->dev;
2651 struct drm_i915_private *dev_priv = dev->dev_private;
2652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2653 struct intel_framebuffer *intel_fb;
2654 struct drm_i915_gem_object *obj;
2655 int pipe = intel_crtc->pipe;
2656 u32 plane_ctl, stride;
2657
2658 if (!intel_crtc->primary_enabled) {
2659 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2660 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2661 POSTING_READ(PLANE_CTL(pipe, 0));
2662 return;
2663 }
2664
2665 plane_ctl = PLANE_CTL_ENABLE |
2666 PLANE_CTL_PIPE_GAMMA_ENABLE |
2667 PLANE_CTL_PIPE_CSC_ENABLE;
2668
2669 switch (fb->pixel_format) {
2670 case DRM_FORMAT_RGB565:
2671 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2672 break;
2673 case DRM_FORMAT_XRGB8888:
2674 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2675 break;
2676 case DRM_FORMAT_XBGR8888:
2677 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2678 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2679 break;
2680 case DRM_FORMAT_XRGB2101010:
2681 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2682 break;
2683 case DRM_FORMAT_XBGR2101010:
2684 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2685 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2686 break;
2687 default:
2688 BUG();
2689 }
2690
2691 intel_fb = to_intel_framebuffer(fb);
2692 obj = intel_fb->obj;
2693
2694 /*
2695 * The stride is either expressed as a multiple of 64 bytes chunks for
2696 * linear buffers or in number of tiles for tiled buffers.
2697 */
2698 switch (obj->tiling_mode) {
2699 case I915_TILING_NONE:
2700 stride = fb->pitches[0] >> 6;
2701 break;
2702 case I915_TILING_X:
2703 plane_ctl |= PLANE_CTL_TILED_X;
2704 stride = fb->pitches[0] >> 9;
2705 break;
2706 default:
2707 BUG();
2708 }
2709
2710 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal1447dde2014-10-04 10:53:31 +01002711 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180))
2712 plane_ctl |= PLANE_CTL_ROTATE_180;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002713
2714 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2715
2716 DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
2717 i915_gem_obj_ggtt_offset(obj),
2718 x, y, fb->width, fb->height,
2719 fb->pitches[0]);
2720
2721 I915_WRITE(PLANE_POS(pipe, 0), 0);
2722 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2723 I915_WRITE(PLANE_SIZE(pipe, 0),
2724 (intel_crtc->config.pipe_src_h - 1) << 16 |
2725 (intel_crtc->config.pipe_src_w - 1));
2726 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
2727 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2728
2729 POSTING_READ(PLANE_SURF(pipe, 0));
2730}
2731
Jesse Barnes17638cd2011-06-24 12:19:23 -07002732/* Assume fb object is pinned & idle & fenced and just update base pointers */
2733static int
2734intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2735 int x, int y, enum mode_set_atomic state)
2736{
2737 struct drm_device *dev = crtc->dev;
2738 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002739
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002740 if (dev_priv->display.disable_fbc)
2741 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07002742
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002743 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2744
2745 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002746}
2747
Ville Syrjälä96a02912013-02-18 19:08:49 +02002748void intel_display_handle_reset(struct drm_device *dev)
2749{
2750 struct drm_i915_private *dev_priv = dev->dev_private;
2751 struct drm_crtc *crtc;
2752
2753 /*
2754 * Flips in the rings have been nuked by the reset,
2755 * so complete all pending flips so that user space
2756 * will get its events and not get stuck.
2757 *
2758 * Also update the base address of all primary
2759 * planes to the the last fb to make sure we're
2760 * showing the correct fb after a reset.
2761 *
2762 * Need to make two loops over the crtcs so that we
2763 * don't try to grab a crtc mutex before the
2764 * pending_flip_queue really got woken up.
2765 */
2766
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002767 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2769 enum plane plane = intel_crtc->plane;
2770
2771 intel_prepare_page_flip(dev, plane);
2772 intel_finish_page_flip_plane(dev, plane);
2773 }
2774
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002775 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2777
Rob Clark51fd3712013-11-19 12:10:12 -05002778 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002779 /*
2780 * FIXME: Once we have proper support for primary planes (and
2781 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002782 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002783 */
Matt Roperf4510a22014-04-01 15:22:40 -07002784 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002785 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002786 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002787 crtc->x,
2788 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002789 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002790 }
2791}
2792
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002793static int
Chris Wilson14667a42012-04-03 17:58:35 +01002794intel_finish_fb(struct drm_framebuffer *old_fb)
2795{
Matt Roper2ff8fde2014-07-08 07:50:07 -07002796 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01002797 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2798 bool was_interruptible = dev_priv->mm.interruptible;
2799 int ret;
2800
Chris Wilson14667a42012-04-03 17:58:35 +01002801 /* Big Hammer, we also need to ensure that any pending
2802 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2803 * current scanout is retired before unpinning the old
2804 * framebuffer.
2805 *
2806 * This should only fail upon a hung GPU, in which case we
2807 * can safely continue.
2808 */
2809 dev_priv->mm.interruptible = false;
2810 ret = i915_gem_object_finish_gpu(obj);
2811 dev_priv->mm.interruptible = was_interruptible;
2812
2813 return ret;
2814}
2815
Chris Wilson7d5e3792014-03-04 13:15:08 +00002816static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2817{
2818 struct drm_device *dev = crtc->dev;
2819 struct drm_i915_private *dev_priv = dev->dev_private;
2820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002821 bool pending;
2822
2823 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2824 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2825 return false;
2826
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02002827 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002828 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02002829 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002830
2831 return pending;
2832}
2833
Gustavo Padovane30e8f72014-09-10 12:04:17 -03002834static void intel_update_pipe_size(struct intel_crtc *crtc)
2835{
2836 struct drm_device *dev = crtc->base.dev;
2837 struct drm_i915_private *dev_priv = dev->dev_private;
2838 const struct drm_display_mode *adjusted_mode;
2839
2840 if (!i915.fastboot)
2841 return;
2842
2843 /*
2844 * Update pipe size and adjust fitter if needed: the reason for this is
2845 * that in compute_mode_changes we check the native mode (not the pfit
2846 * mode) to see if we can flip rather than do a full mode set. In the
2847 * fastboot case, we'll flip, but if we don't update the pipesrc and
2848 * pfit state, we'll end up with a big fb scanned out into the wrong
2849 * sized surface.
2850 *
2851 * To fix this properly, we need to hoist the checks up into
2852 * compute_mode_changes (or above), check the actual pfit state and
2853 * whether the platform allows pfit disable with pipe active, and only
2854 * then update the pipesrc and pfit state, even on the flip path.
2855 */
2856
2857 adjusted_mode = &crtc->config.adjusted_mode;
2858
2859 I915_WRITE(PIPESRC(crtc->pipe),
2860 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2861 (adjusted_mode->crtc_vdisplay - 1));
2862 if (!crtc->config.pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002863 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2864 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03002865 I915_WRITE(PF_CTL(crtc->pipe), 0);
2866 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
2867 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
2868 }
2869 crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2870 crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
2871}
2872
Chris Wilson14667a42012-04-03 17:58:35 +01002873static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002874intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002875 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002876{
2877 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002878 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002879 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02002880 enum pipe pipe = intel_crtc->pipe;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002881 struct drm_framebuffer *old_fb = crtc->primary->fb;
2882 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2883 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002884 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002885
Chris Wilson7d5e3792014-03-04 13:15:08 +00002886 if (intel_crtc_has_pending_flip(crtc)) {
2887 DRM_ERROR("pipe is still busy with an old pageflip\n");
2888 return -EBUSY;
2889 }
2890
Jesse Barnes79e53942008-11-07 14:24:08 -08002891 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002892 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002893 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002894 return 0;
2895 }
2896
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002897 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002898 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2899 plane_name(intel_crtc->plane),
2900 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002901 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002902 }
2903
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002904 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02002905 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2906 if (ret == 0)
Matt Roper91565c852014-06-24 17:05:02 -07002907 i915_gem_track_fb(old_obj, obj,
Daniel Vettera071fa02014-06-18 23:28:09 +02002908 INTEL_FRONTBUFFER_PRIMARY(pipe));
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002909 mutex_unlock(&dev->struct_mutex);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002910 if (ret != 0) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002911 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002912 return ret;
2913 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002914
Gustavo Padovane30e8f72014-09-10 12:04:17 -03002915 intel_update_pipe_size(intel_crtc);
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002916
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002917 dev_priv->display.update_primary_plane(crtc, fb, x, y);
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002918
Daniel Vetterf99d7062014-06-19 16:01:59 +02002919 if (intel_crtc->active)
2920 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2921
Matt Roperf4510a22014-04-01 15:22:40 -07002922 crtc->primary->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002923 crtc->x = x;
2924 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002925
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002926 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002927 if (intel_crtc->active && old_fb != fb)
2928 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002929 mutex_lock(&dev->struct_mutex);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002930 intel_unpin_fb_obj(old_obj);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002931 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002932 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002933
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002934 mutex_lock(&dev->struct_mutex);
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002935 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002936 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002937
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002938 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002939}
2940
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002941static void intel_fdi_normal_train(struct drm_crtc *crtc)
2942{
2943 struct drm_device *dev = crtc->dev;
2944 struct drm_i915_private *dev_priv = dev->dev_private;
2945 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2946 int pipe = intel_crtc->pipe;
2947 u32 reg, temp;
2948
2949 /* enable normal train */
2950 reg = FDI_TX_CTL(pipe);
2951 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002952 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002953 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2954 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002955 } else {
2956 temp &= ~FDI_LINK_TRAIN_NONE;
2957 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002958 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002959 I915_WRITE(reg, temp);
2960
2961 reg = FDI_RX_CTL(pipe);
2962 temp = I915_READ(reg);
2963 if (HAS_PCH_CPT(dev)) {
2964 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2965 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2966 } else {
2967 temp &= ~FDI_LINK_TRAIN_NONE;
2968 temp |= FDI_LINK_TRAIN_NONE;
2969 }
2970 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2971
2972 /* wait one idle pattern time */
2973 POSTING_READ(reg);
2974 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002975
2976 /* IVB wants error correction enabled */
2977 if (IS_IVYBRIDGE(dev))
2978 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2979 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002980}
2981
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002982static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002983{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002984 return crtc->base.enabled && crtc->active &&
2985 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002986}
2987
Daniel Vetter01a415f2012-10-27 15:58:40 +02002988static void ivb_modeset_global_resources(struct drm_device *dev)
2989{
2990 struct drm_i915_private *dev_priv = dev->dev_private;
2991 struct intel_crtc *pipe_B_crtc =
2992 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2993 struct intel_crtc *pipe_C_crtc =
2994 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2995 uint32_t temp;
2996
Daniel Vetter1e833f42013-02-19 22:31:57 +01002997 /*
2998 * When everything is off disable fdi C so that we could enable fdi B
2999 * with all lanes. Note that we don't care about enabled pipes without
3000 * an enabled pch encoder.
3001 */
3002 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
3003 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02003004 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3005 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3006
3007 temp = I915_READ(SOUTH_CHICKEN1);
3008 temp &= ~FDI_BC_BIFURCATION_SELECT;
3009 DRM_DEBUG_KMS("disabling fdi C rx\n");
3010 I915_WRITE(SOUTH_CHICKEN1, temp);
3011 }
3012}
3013
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003014/* The FDI link training functions for ILK/Ibexpeak. */
3015static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3016{
3017 struct drm_device *dev = crtc->dev;
3018 struct drm_i915_private *dev_priv = dev->dev_private;
3019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3020 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003021 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003022
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003023 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003024 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003025
Adam Jacksone1a44742010-06-25 15:32:14 -04003026 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3027 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003028 reg = FDI_RX_IMR(pipe);
3029 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003030 temp &= ~FDI_RX_SYMBOL_LOCK;
3031 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003032 I915_WRITE(reg, temp);
3033 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003034 udelay(150);
3035
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003036 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003037 reg = FDI_TX_CTL(pipe);
3038 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003039 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3040 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003041 temp &= ~FDI_LINK_TRAIN_NONE;
3042 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003043 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003044
Chris Wilson5eddb702010-09-11 13:48:45 +01003045 reg = FDI_RX_CTL(pipe);
3046 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003047 temp &= ~FDI_LINK_TRAIN_NONE;
3048 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003049 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3050
3051 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003052 udelay(150);
3053
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003054 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003055 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3056 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3057 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003058
Chris Wilson5eddb702010-09-11 13:48:45 +01003059 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003060 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003061 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003062 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3063
3064 if ((temp & FDI_RX_BIT_LOCK)) {
3065 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003066 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003067 break;
3068 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003069 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003070 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003071 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003072
3073 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003074 reg = FDI_TX_CTL(pipe);
3075 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003076 temp &= ~FDI_LINK_TRAIN_NONE;
3077 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003078 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003079
Chris Wilson5eddb702010-09-11 13:48:45 +01003080 reg = FDI_RX_CTL(pipe);
3081 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003082 temp &= ~FDI_LINK_TRAIN_NONE;
3083 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003084 I915_WRITE(reg, temp);
3085
3086 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003087 udelay(150);
3088
Chris Wilson5eddb702010-09-11 13:48:45 +01003089 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003090 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003091 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003092 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3093
3094 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003095 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003096 DRM_DEBUG_KMS("FDI train 2 done.\n");
3097 break;
3098 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003099 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003100 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003101 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003102
3103 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003104
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003105}
3106
Akshay Joshi0206e352011-08-16 15:34:10 -04003107static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003108 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3109 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3110 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3111 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3112};
3113
3114/* The FDI link training functions for SNB/Cougarpoint. */
3115static void gen6_fdi_link_train(struct drm_crtc *crtc)
3116{
3117 struct drm_device *dev = crtc->dev;
3118 struct drm_i915_private *dev_priv = dev->dev_private;
3119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3120 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003121 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003122
Adam Jacksone1a44742010-06-25 15:32:14 -04003123 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3124 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003125 reg = FDI_RX_IMR(pipe);
3126 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003127 temp &= ~FDI_RX_SYMBOL_LOCK;
3128 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003129 I915_WRITE(reg, temp);
3130
3131 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003132 udelay(150);
3133
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003134 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003135 reg = FDI_TX_CTL(pipe);
3136 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003137 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3138 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003139 temp &= ~FDI_LINK_TRAIN_NONE;
3140 temp |= FDI_LINK_TRAIN_PATTERN_1;
3141 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3142 /* SNB-B */
3143 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003144 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003145
Daniel Vetterd74cf322012-10-26 10:58:13 +02003146 I915_WRITE(FDI_RX_MISC(pipe),
3147 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3148
Chris Wilson5eddb702010-09-11 13:48:45 +01003149 reg = FDI_RX_CTL(pipe);
3150 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003151 if (HAS_PCH_CPT(dev)) {
3152 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3153 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3154 } else {
3155 temp &= ~FDI_LINK_TRAIN_NONE;
3156 temp |= FDI_LINK_TRAIN_PATTERN_1;
3157 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003158 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3159
3160 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003161 udelay(150);
3162
Akshay Joshi0206e352011-08-16 15:34:10 -04003163 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003164 reg = FDI_TX_CTL(pipe);
3165 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003166 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3167 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003168 I915_WRITE(reg, temp);
3169
3170 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003171 udelay(500);
3172
Sean Paulfa37d392012-03-02 12:53:39 -05003173 for (retry = 0; retry < 5; retry++) {
3174 reg = FDI_RX_IIR(pipe);
3175 temp = I915_READ(reg);
3176 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3177 if (temp & FDI_RX_BIT_LOCK) {
3178 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3179 DRM_DEBUG_KMS("FDI train 1 done.\n");
3180 break;
3181 }
3182 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003183 }
Sean Paulfa37d392012-03-02 12:53:39 -05003184 if (retry < 5)
3185 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003186 }
3187 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003188 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003189
3190 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003191 reg = FDI_TX_CTL(pipe);
3192 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003193 temp &= ~FDI_LINK_TRAIN_NONE;
3194 temp |= FDI_LINK_TRAIN_PATTERN_2;
3195 if (IS_GEN6(dev)) {
3196 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3197 /* SNB-B */
3198 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3199 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003200 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003201
Chris Wilson5eddb702010-09-11 13:48:45 +01003202 reg = FDI_RX_CTL(pipe);
3203 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003204 if (HAS_PCH_CPT(dev)) {
3205 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3206 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3207 } else {
3208 temp &= ~FDI_LINK_TRAIN_NONE;
3209 temp |= FDI_LINK_TRAIN_PATTERN_2;
3210 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003211 I915_WRITE(reg, temp);
3212
3213 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003214 udelay(150);
3215
Akshay Joshi0206e352011-08-16 15:34:10 -04003216 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003217 reg = FDI_TX_CTL(pipe);
3218 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003219 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3220 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003221 I915_WRITE(reg, temp);
3222
3223 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003224 udelay(500);
3225
Sean Paulfa37d392012-03-02 12:53:39 -05003226 for (retry = 0; retry < 5; retry++) {
3227 reg = FDI_RX_IIR(pipe);
3228 temp = I915_READ(reg);
3229 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3230 if (temp & FDI_RX_SYMBOL_LOCK) {
3231 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3232 DRM_DEBUG_KMS("FDI train 2 done.\n");
3233 break;
3234 }
3235 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003236 }
Sean Paulfa37d392012-03-02 12:53:39 -05003237 if (retry < 5)
3238 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003239 }
3240 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003241 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003242
3243 DRM_DEBUG_KMS("FDI train done.\n");
3244}
3245
Jesse Barnes357555c2011-04-28 15:09:55 -07003246/* Manual link training for Ivy Bridge A0 parts */
3247static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3248{
3249 struct drm_device *dev = crtc->dev;
3250 struct drm_i915_private *dev_priv = dev->dev_private;
3251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3252 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003253 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003254
3255 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3256 for train result */
3257 reg = FDI_RX_IMR(pipe);
3258 temp = I915_READ(reg);
3259 temp &= ~FDI_RX_SYMBOL_LOCK;
3260 temp &= ~FDI_RX_BIT_LOCK;
3261 I915_WRITE(reg, temp);
3262
3263 POSTING_READ(reg);
3264 udelay(150);
3265
Daniel Vetter01a415f2012-10-27 15:58:40 +02003266 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3267 I915_READ(FDI_RX_IIR(pipe)));
3268
Jesse Barnes139ccd32013-08-19 11:04:55 -07003269 /* Try each vswing and preemphasis setting twice before moving on */
3270 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3271 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003272 reg = FDI_TX_CTL(pipe);
3273 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003274 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3275 temp &= ~FDI_TX_ENABLE;
3276 I915_WRITE(reg, temp);
3277
3278 reg = FDI_RX_CTL(pipe);
3279 temp = I915_READ(reg);
3280 temp &= ~FDI_LINK_TRAIN_AUTO;
3281 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3282 temp &= ~FDI_RX_ENABLE;
3283 I915_WRITE(reg, temp);
3284
3285 /* enable CPU FDI TX and PCH FDI RX */
3286 reg = FDI_TX_CTL(pipe);
3287 temp = I915_READ(reg);
3288 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3289 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3290 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003291 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003292 temp |= snb_b_fdi_train_param[j/2];
3293 temp |= FDI_COMPOSITE_SYNC;
3294 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3295
3296 I915_WRITE(FDI_RX_MISC(pipe),
3297 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3298
3299 reg = FDI_RX_CTL(pipe);
3300 temp = I915_READ(reg);
3301 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3302 temp |= FDI_COMPOSITE_SYNC;
3303 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3304
3305 POSTING_READ(reg);
3306 udelay(1); /* should be 0.5us */
3307
3308 for (i = 0; i < 4; i++) {
3309 reg = FDI_RX_IIR(pipe);
3310 temp = I915_READ(reg);
3311 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3312
3313 if (temp & FDI_RX_BIT_LOCK ||
3314 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3315 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3316 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3317 i);
3318 break;
3319 }
3320 udelay(1); /* should be 0.5us */
3321 }
3322 if (i == 4) {
3323 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3324 continue;
3325 }
3326
3327 /* Train 2 */
3328 reg = FDI_TX_CTL(pipe);
3329 temp = I915_READ(reg);
3330 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3331 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3332 I915_WRITE(reg, temp);
3333
3334 reg = FDI_RX_CTL(pipe);
3335 temp = I915_READ(reg);
3336 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3337 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003338 I915_WRITE(reg, temp);
3339
3340 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003341 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003342
Jesse Barnes139ccd32013-08-19 11:04:55 -07003343 for (i = 0; i < 4; i++) {
3344 reg = FDI_RX_IIR(pipe);
3345 temp = I915_READ(reg);
3346 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003347
Jesse Barnes139ccd32013-08-19 11:04:55 -07003348 if (temp & FDI_RX_SYMBOL_LOCK ||
3349 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3350 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3351 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3352 i);
3353 goto train_done;
3354 }
3355 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003356 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003357 if (i == 4)
3358 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003359 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003360
Jesse Barnes139ccd32013-08-19 11:04:55 -07003361train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003362 DRM_DEBUG_KMS("FDI train done.\n");
3363}
3364
Daniel Vetter88cefb62012-08-12 19:27:14 +02003365static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003366{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003367 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003368 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003369 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003370 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003371
Jesse Barnesc64e3112010-09-10 11:27:03 -07003372
Jesse Barnes0e23b992010-09-10 11:10:00 -07003373 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003374 reg = FDI_RX_CTL(pipe);
3375 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003376 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3377 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003378 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003379 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3380
3381 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003382 udelay(200);
3383
3384 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003385 temp = I915_READ(reg);
3386 I915_WRITE(reg, temp | FDI_PCDCLK);
3387
3388 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003389 udelay(200);
3390
Paulo Zanoni20749732012-11-23 15:30:38 -02003391 /* Enable CPU FDI TX PLL, always on for Ironlake */
3392 reg = FDI_TX_CTL(pipe);
3393 temp = I915_READ(reg);
3394 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3395 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003396
Paulo Zanoni20749732012-11-23 15:30:38 -02003397 POSTING_READ(reg);
3398 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003399 }
3400}
3401
Daniel Vetter88cefb62012-08-12 19:27:14 +02003402static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3403{
3404 struct drm_device *dev = intel_crtc->base.dev;
3405 struct drm_i915_private *dev_priv = dev->dev_private;
3406 int pipe = intel_crtc->pipe;
3407 u32 reg, temp;
3408
3409 /* Switch from PCDclk to Rawclk */
3410 reg = FDI_RX_CTL(pipe);
3411 temp = I915_READ(reg);
3412 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3413
3414 /* Disable CPU FDI TX PLL */
3415 reg = FDI_TX_CTL(pipe);
3416 temp = I915_READ(reg);
3417 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3418
3419 POSTING_READ(reg);
3420 udelay(100);
3421
3422 reg = FDI_RX_CTL(pipe);
3423 temp = I915_READ(reg);
3424 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3425
3426 /* Wait for the clocks to turn off. */
3427 POSTING_READ(reg);
3428 udelay(100);
3429}
3430
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003431static void ironlake_fdi_disable(struct drm_crtc *crtc)
3432{
3433 struct drm_device *dev = crtc->dev;
3434 struct drm_i915_private *dev_priv = dev->dev_private;
3435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3436 int pipe = intel_crtc->pipe;
3437 u32 reg, temp;
3438
3439 /* disable CPU FDI tx and PCH FDI rx */
3440 reg = FDI_TX_CTL(pipe);
3441 temp = I915_READ(reg);
3442 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3443 POSTING_READ(reg);
3444
3445 reg = FDI_RX_CTL(pipe);
3446 temp = I915_READ(reg);
3447 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003448 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003449 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3450
3451 POSTING_READ(reg);
3452 udelay(100);
3453
3454 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003455 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003456 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003457
3458 /* still set train pattern 1 */
3459 reg = FDI_TX_CTL(pipe);
3460 temp = I915_READ(reg);
3461 temp &= ~FDI_LINK_TRAIN_NONE;
3462 temp |= FDI_LINK_TRAIN_PATTERN_1;
3463 I915_WRITE(reg, temp);
3464
3465 reg = FDI_RX_CTL(pipe);
3466 temp = I915_READ(reg);
3467 if (HAS_PCH_CPT(dev)) {
3468 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3469 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3470 } else {
3471 temp &= ~FDI_LINK_TRAIN_NONE;
3472 temp |= FDI_LINK_TRAIN_PATTERN_1;
3473 }
3474 /* BPC in FDI rx is consistent with that in PIPECONF */
3475 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003476 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003477 I915_WRITE(reg, temp);
3478
3479 POSTING_READ(reg);
3480 udelay(100);
3481}
3482
Chris Wilson5dce5b932014-01-20 10:17:36 +00003483bool intel_has_pending_fb_unpin(struct drm_device *dev)
3484{
3485 struct intel_crtc *crtc;
3486
3487 /* Note that we don't need to be called with mode_config.lock here
3488 * as our list of CRTC objects is static for the lifetime of the
3489 * device and so cannot disappear as we iterate. Similarly, we can
3490 * happily treat the predicates as racy, atomic checks as userspace
3491 * cannot claim and pin a new fb without at least acquring the
3492 * struct_mutex and so serialising with us.
3493 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003494 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003495 if (atomic_read(&crtc->unpin_work_count) == 0)
3496 continue;
3497
3498 if (crtc->unpin_work)
3499 intel_wait_for_vblank(dev, crtc->pipe);
3500
3501 return true;
3502 }
3503
3504 return false;
3505}
3506
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003507static void page_flip_completed(struct intel_crtc *intel_crtc)
3508{
3509 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3510 struct intel_unpin_work *work = intel_crtc->unpin_work;
3511
3512 /* ensure that the unpin work is consistent wrt ->pending. */
3513 smp_rmb();
3514 intel_crtc->unpin_work = NULL;
3515
3516 if (work->event)
3517 drm_send_vblank_event(intel_crtc->base.dev,
3518 intel_crtc->pipe,
3519 work->event);
3520
3521 drm_crtc_vblank_put(&intel_crtc->base);
3522
3523 wake_up_all(&dev_priv->pending_flip_queue);
3524 queue_work(dev_priv->wq, &work->work);
3525
3526 trace_i915_flip_complete(intel_crtc->plane,
3527 work->pending_flip_obj);
3528}
3529
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003530void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003531{
Chris Wilson0f911282012-04-17 10:05:38 +01003532 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003533 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003534
Daniel Vetter2c10d572012-12-20 21:24:07 +01003535 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003536 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3537 !intel_crtc_has_pending_flip(crtc),
3538 60*HZ) == 0)) {
3539 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003540
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003541 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003542 if (intel_crtc->unpin_work) {
3543 WARN_ONCE(1, "Removing stuck page flip\n");
3544 page_flip_completed(intel_crtc);
3545 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003546 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003547 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003548
Chris Wilson975d5682014-08-20 13:13:34 +01003549 if (crtc->primary->fb) {
3550 mutex_lock(&dev->struct_mutex);
3551 intel_finish_fb(crtc->primary->fb);
3552 mutex_unlock(&dev->struct_mutex);
3553 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003554}
3555
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003556/* Program iCLKIP clock to the desired frequency */
3557static void lpt_program_iclkip(struct drm_crtc *crtc)
3558{
3559 struct drm_device *dev = crtc->dev;
3560 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003561 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003562 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3563 u32 temp;
3564
Daniel Vetter09153002012-12-12 14:06:44 +01003565 mutex_lock(&dev_priv->dpio_lock);
3566
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003567 /* It is necessary to ungate the pixclk gate prior to programming
3568 * the divisors, and gate it back when it is done.
3569 */
3570 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3571
3572 /* Disable SSCCTL */
3573 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003574 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3575 SBI_SSCCTL_DISABLE,
3576 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003577
3578 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003579 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003580 auxdiv = 1;
3581 divsel = 0x41;
3582 phaseinc = 0x20;
3583 } else {
3584 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003585 * but the adjusted_mode->crtc_clock in in KHz. To get the
3586 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003587 * convert the virtual clock precision to KHz here for higher
3588 * precision.
3589 */
3590 u32 iclk_virtual_root_freq = 172800 * 1000;
3591 u32 iclk_pi_range = 64;
3592 u32 desired_divisor, msb_divisor_value, pi_value;
3593
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003594 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003595 msb_divisor_value = desired_divisor / iclk_pi_range;
3596 pi_value = desired_divisor % iclk_pi_range;
3597
3598 auxdiv = 0;
3599 divsel = msb_divisor_value - 2;
3600 phaseinc = pi_value;
3601 }
3602
3603 /* This should not happen with any sane values */
3604 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3605 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3606 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3607 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3608
3609 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 +03003610 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003611 auxdiv,
3612 divsel,
3613 phasedir,
3614 phaseinc);
3615
3616 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003617 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003618 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3619 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3620 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3621 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3622 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3623 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003624 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003625
3626 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003627 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003628 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3629 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003630 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003631
3632 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003633 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003634 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003635 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003636
3637 /* Wait for initialization time */
3638 udelay(24);
3639
3640 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003641
3642 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003643}
3644
Daniel Vetter275f01b22013-05-03 11:49:47 +02003645static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3646 enum pipe pch_transcoder)
3647{
3648 struct drm_device *dev = crtc->base.dev;
3649 struct drm_i915_private *dev_priv = dev->dev_private;
3650 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3651
3652 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3653 I915_READ(HTOTAL(cpu_transcoder)));
3654 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3655 I915_READ(HBLANK(cpu_transcoder)));
3656 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3657 I915_READ(HSYNC(cpu_transcoder)));
3658
3659 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3660 I915_READ(VTOTAL(cpu_transcoder)));
3661 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3662 I915_READ(VBLANK(cpu_transcoder)));
3663 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3664 I915_READ(VSYNC(cpu_transcoder)));
3665 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3666 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3667}
3668
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003669static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3670{
3671 struct drm_i915_private *dev_priv = dev->dev_private;
3672 uint32_t temp;
3673
3674 temp = I915_READ(SOUTH_CHICKEN1);
3675 if (temp & FDI_BC_BIFURCATION_SELECT)
3676 return;
3677
3678 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3679 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3680
3681 temp |= FDI_BC_BIFURCATION_SELECT;
3682 DRM_DEBUG_KMS("enabling fdi C rx\n");
3683 I915_WRITE(SOUTH_CHICKEN1, temp);
3684 POSTING_READ(SOUTH_CHICKEN1);
3685}
3686
3687static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3688{
3689 struct drm_device *dev = intel_crtc->base.dev;
3690 struct drm_i915_private *dev_priv = dev->dev_private;
3691
3692 switch (intel_crtc->pipe) {
3693 case PIPE_A:
3694 break;
3695 case PIPE_B:
3696 if (intel_crtc->config.fdi_lanes > 2)
3697 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3698 else
3699 cpt_enable_fdi_bc_bifurcation(dev);
3700
3701 break;
3702 case PIPE_C:
3703 cpt_enable_fdi_bc_bifurcation(dev);
3704
3705 break;
3706 default:
3707 BUG();
3708 }
3709}
3710
Jesse Barnesf67a5592011-01-05 10:31:48 -08003711/*
3712 * Enable PCH resources required for PCH ports:
3713 * - PCH PLLs
3714 * - FDI training & RX/TX
3715 * - update transcoder timings
3716 * - DP transcoding bits
3717 * - transcoder
3718 */
3719static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003720{
3721 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003722 struct drm_i915_private *dev_priv = dev->dev_private;
3723 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3724 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003725 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003726
Daniel Vetterab9412b2013-05-03 11:49:46 +02003727 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003728
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003729 if (IS_IVYBRIDGE(dev))
3730 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3731
Daniel Vettercd986ab2012-10-26 10:58:12 +02003732 /* Write the TU size bits before fdi link training, so that error
3733 * detection works. */
3734 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3735 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3736
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003737 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003738 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003739
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003740 /* We need to program the right clock selection before writing the pixel
3741 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003742 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003743 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003744
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003745 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003746 temp |= TRANS_DPLL_ENABLE(pipe);
3747 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003748 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003749 temp |= sel;
3750 else
3751 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003752 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003753 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003754
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003755 /* XXX: pch pll's can be enabled any time before we enable the PCH
3756 * transcoder, and we actually should do this to not upset any PCH
3757 * transcoder that already use the clock when we share it.
3758 *
3759 * Note that enable_shared_dpll tries to do the right thing, but
3760 * get_shared_dpll unconditionally resets the pll - we need that to have
3761 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003762 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003763
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003764 /* set transcoder timing, panel must allow it */
3765 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003766 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003767
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003768 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003769
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003770 /* For PCH DP, enable TRANS_DP_CTL */
3771 if (HAS_PCH_CPT(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003772 (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3773 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003774 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003775 reg = TRANS_DP_CTL(pipe);
3776 temp = I915_READ(reg);
3777 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003778 TRANS_DP_SYNC_MASK |
3779 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003780 temp |= (TRANS_DP_OUTPUT_ENABLE |
3781 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003782 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003783
3784 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003785 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003786 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003787 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003788
3789 switch (intel_trans_dp_port_sel(crtc)) {
3790 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003791 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003792 break;
3793 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003794 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003795 break;
3796 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003797 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003798 break;
3799 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003800 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003801 }
3802
Chris Wilson5eddb702010-09-11 13:48:45 +01003803 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003804 }
3805
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003806 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003807}
3808
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003809static void lpt_pch_enable(struct drm_crtc *crtc)
3810{
3811 struct drm_device *dev = crtc->dev;
3812 struct drm_i915_private *dev_priv = dev->dev_private;
3813 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003814 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003815
Daniel Vetterab9412b2013-05-03 11:49:46 +02003816 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003817
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003818 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003819
Paulo Zanoni0540e482012-10-31 18:12:40 -02003820 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003821 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003822
Paulo Zanoni937bb612012-10-31 18:12:47 -02003823 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003824}
3825
Daniel Vetter716c2e52014-06-25 22:02:02 +03003826void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003827{
Daniel Vettere2b78262013-06-07 23:10:03 +02003828 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003829
3830 if (pll == NULL)
3831 return;
3832
3833 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003834 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003835 return;
3836 }
3837
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003838 if (--pll->refcount == 0) {
3839 WARN_ON(pll->on);
3840 WARN_ON(pll->active);
3841 }
3842
Daniel Vettera43f6e02013-06-07 23:10:32 +02003843 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003844}
3845
Daniel Vetter716c2e52014-06-25 22:02:02 +03003846struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003847{
Daniel Vettere2b78262013-06-07 23:10:03 +02003848 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3849 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3850 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003851
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003852 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003853 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3854 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003855 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003856 }
3857
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003858 if (HAS_PCH_IBX(dev_priv->dev)) {
3859 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003860 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003861 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003862
Daniel Vetter46edb022013-06-05 13:34:12 +02003863 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3864 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003865
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003866 WARN_ON(pll->refcount);
3867
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003868 goto found;
3869 }
3870
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003871 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3872 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003873
3874 /* Only want to check enabled timings first */
3875 if (pll->refcount == 0)
3876 continue;
3877
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003878 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3879 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003880 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003881 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003882 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003883
3884 goto found;
3885 }
3886 }
3887
3888 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003889 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3890 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003891 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003892 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3893 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003894 goto found;
3895 }
3896 }
3897
3898 return NULL;
3899
3900found:
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003901 if (pll->refcount == 0)
3902 pll->hw_state = crtc->config.dpll_hw_state;
3903
Daniel Vettera43f6e02013-06-07 23:10:32 +02003904 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003905 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3906 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003907
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003908 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003909
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003910 return pll;
3911}
3912
Daniel Vettera1520312013-05-03 11:49:50 +02003913static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003914{
3915 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003916 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003917 u32 temp;
3918
3919 temp = I915_READ(dslreg);
3920 udelay(500);
3921 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003922 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003923 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003924 }
3925}
3926
Jesse Barnesb074cec2013-04-25 12:55:02 -07003927static void ironlake_pfit_enable(struct intel_crtc *crtc)
3928{
3929 struct drm_device *dev = crtc->base.dev;
3930 struct drm_i915_private *dev_priv = dev->dev_private;
3931 int pipe = crtc->pipe;
3932
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003933 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003934 /* Force use of hard-coded filter coefficients
3935 * as some pre-programmed values are broken,
3936 * e.g. x201.
3937 */
3938 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3939 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3940 PF_PIPE_SEL_IVB(pipe));
3941 else
3942 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3943 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3944 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003945 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003946}
3947
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003948static void intel_enable_planes(struct drm_crtc *crtc)
3949{
3950 struct drm_device *dev = crtc->dev;
3951 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003952 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003953 struct intel_plane *intel_plane;
3954
Matt Roperaf2b6532014-04-01 15:22:32 -07003955 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3956 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003957 if (intel_plane->pipe == pipe)
3958 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003959 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003960}
3961
3962static void intel_disable_planes(struct drm_crtc *crtc)
3963{
3964 struct drm_device *dev = crtc->dev;
3965 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003966 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003967 struct intel_plane *intel_plane;
3968
Matt Roperaf2b6532014-04-01 15:22:32 -07003969 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3970 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003971 if (intel_plane->pipe == pipe)
3972 intel_plane_disable(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003973 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003974}
3975
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003976void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003977{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003978 struct drm_device *dev = crtc->base.dev;
3979 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03003980
3981 if (!crtc->config.ips_enabled)
3982 return;
3983
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003984 /* We can only enable IPS after we enable a plane and wait for a vblank */
3985 intel_wait_for_vblank(dev, crtc->pipe);
3986
Paulo Zanonid77e4532013-09-24 13:52:55 -03003987 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003988 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003989 mutex_lock(&dev_priv->rps.hw_lock);
3990 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3991 mutex_unlock(&dev_priv->rps.hw_lock);
3992 /* Quoting Art Runyan: "its not safe to expect any particular
3993 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003994 * mailbox." Moreover, the mailbox may return a bogus state,
3995 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003996 */
3997 } else {
3998 I915_WRITE(IPS_CTL, IPS_ENABLE);
3999 /* The bit only becomes 1 in the next vblank, so this wait here
4000 * is essentially intel_wait_for_vblank. If we don't have this
4001 * and don't wait for vblanks until the end of crtc_enable, then
4002 * the HW state readout code will complain that the expected
4003 * IPS_CTL value is not the one we read. */
4004 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4005 DRM_ERROR("Timed out waiting for IPS enable\n");
4006 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004007}
4008
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004009void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004010{
4011 struct drm_device *dev = crtc->base.dev;
4012 struct drm_i915_private *dev_priv = dev->dev_private;
4013
4014 if (!crtc->config.ips_enabled)
4015 return;
4016
4017 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004018 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004019 mutex_lock(&dev_priv->rps.hw_lock);
4020 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4021 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004022 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4023 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4024 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004025 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004026 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004027 POSTING_READ(IPS_CTL);
4028 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004029
4030 /* We need to wait for a vblank before we can disable the plane. */
4031 intel_wait_for_vblank(dev, crtc->pipe);
4032}
4033
4034/** Loads the palette/gamma unit for the CRTC with the prepared values */
4035static void intel_crtc_load_lut(struct drm_crtc *crtc)
4036{
4037 struct drm_device *dev = crtc->dev;
4038 struct drm_i915_private *dev_priv = dev->dev_private;
4039 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4040 enum pipe pipe = intel_crtc->pipe;
4041 int palreg = PALETTE(pipe);
4042 int i;
4043 bool reenable_ips = false;
4044
4045 /* The clocks have to be on to load the palette. */
4046 if (!crtc->enabled || !intel_crtc->active)
4047 return;
4048
4049 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004050 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004051 assert_dsi_pll_enabled(dev_priv);
4052 else
4053 assert_pll_enabled(dev_priv, pipe);
4054 }
4055
4056 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304057 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004058 palreg = LGC_PALETTE(pipe);
4059
4060 /* Workaround : Do not read or write the pipe palette/gamma data while
4061 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4062 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02004063 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004064 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4065 GAMMA_MODE_MODE_SPLIT)) {
4066 hsw_disable_ips(intel_crtc);
4067 reenable_ips = true;
4068 }
4069
4070 for (i = 0; i < 256; i++) {
4071 I915_WRITE(palreg + 4 * i,
4072 (intel_crtc->lut_r[i] << 16) |
4073 (intel_crtc->lut_g[i] << 8) |
4074 intel_crtc->lut_b[i]);
4075 }
4076
4077 if (reenable_ips)
4078 hsw_enable_ips(intel_crtc);
4079}
4080
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004081static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4082{
4083 if (!enable && intel_crtc->overlay) {
4084 struct drm_device *dev = intel_crtc->base.dev;
4085 struct drm_i915_private *dev_priv = dev->dev_private;
4086
4087 mutex_lock(&dev->struct_mutex);
4088 dev_priv->mm.interruptible = false;
4089 (void) intel_overlay_switch_off(intel_crtc->overlay);
4090 dev_priv->mm.interruptible = true;
4091 mutex_unlock(&dev->struct_mutex);
4092 }
4093
4094 /* Let userspace switch the overlay on again. In most cases userspace
4095 * has to recompute where to put it anyway.
4096 */
4097}
4098
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004099static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004100{
4101 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004102 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4103 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004104
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004105 intel_enable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004106 intel_enable_planes(crtc);
4107 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004108 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004109
4110 hsw_enable_ips(intel_crtc);
4111
4112 mutex_lock(&dev->struct_mutex);
4113 intel_update_fbc(dev);
4114 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004115
4116 /*
4117 * FIXME: Once we grow proper nuclear flip support out of this we need
4118 * to compute the mask of flip planes precisely. For the time being
4119 * consider this a flip from a NULL plane.
4120 */
4121 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004122}
4123
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004124static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004125{
4126 struct drm_device *dev = crtc->dev;
4127 struct drm_i915_private *dev_priv = dev->dev_private;
4128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4129 int pipe = intel_crtc->pipe;
4130 int plane = intel_crtc->plane;
4131
4132 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004133
4134 if (dev_priv->fbc.plane == plane)
4135 intel_disable_fbc(dev);
4136
4137 hsw_disable_ips(intel_crtc);
4138
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004139 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004140 intel_crtc_update_cursor(crtc, false);
4141 intel_disable_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004142 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004143
Daniel Vetterf99d7062014-06-19 16:01:59 +02004144 /*
4145 * FIXME: Once we grow proper nuclear flip support out of this we need
4146 * to compute the mask of flip planes precisely. For the time being
4147 * consider this a flip to a NULL plane.
4148 */
4149 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004150}
4151
Jesse Barnesf67a5592011-01-05 10:31:48 -08004152static void ironlake_crtc_enable(struct drm_crtc *crtc)
4153{
4154 struct drm_device *dev = crtc->dev;
4155 struct drm_i915_private *dev_priv = dev->dev_private;
4156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004157 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004158 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004159
Daniel Vetter08a48462012-07-02 11:43:47 +02004160 WARN_ON(!crtc->enabled);
4161
Jesse Barnesf67a5592011-01-05 10:31:48 -08004162 if (intel_crtc->active)
4163 return;
4164
Daniel Vetterb14b1052014-04-24 23:55:13 +02004165 if (intel_crtc->config.has_pch_encoder)
4166 intel_prepare_shared_dpll(intel_crtc);
4167
Daniel Vetter29407aa2014-04-24 23:55:08 +02004168 if (intel_crtc->config.has_dp_encoder)
4169 intel_dp_set_m_n(intel_crtc);
4170
4171 intel_set_pipe_timings(intel_crtc);
4172
4173 if (intel_crtc->config.has_pch_encoder) {
4174 intel_cpu_transcoder_set_m_n(intel_crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07004175 &intel_crtc->config.fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004176 }
4177
4178 ironlake_set_pipeconf(crtc);
4179
Jesse Barnesf67a5592011-01-05 10:31:48 -08004180 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004181
Daniel Vettera72e4c92014-09-30 10:56:47 +02004182 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4183 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004184
Daniel Vetterf6736a12013-06-05 13:34:30 +02004185 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004186 if (encoder->pre_enable)
4187 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004188
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004189 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004190 /* Note: FDI PLL enabling _must_ be done before we enable the
4191 * cpu pipes, hence this is separate from all the other fdi/pch
4192 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004193 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004194 } else {
4195 assert_fdi_tx_disabled(dev_priv, pipe);
4196 assert_fdi_rx_disabled(dev_priv, pipe);
4197 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004198
Jesse Barnesb074cec2013-04-25 12:55:02 -07004199 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004200
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004201 /*
4202 * On ILK+ LUT must be loaded before the pipe is running but with
4203 * clocks enabled
4204 */
4205 intel_crtc_load_lut(crtc);
4206
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004207 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004208 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004209
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004210 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004211 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004212
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004213 for_each_encoder_on_crtc(dev, crtc, encoder)
4214 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004215
4216 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004217 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004218
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004219 assert_vblank_disabled(crtc);
4220 drm_crtc_vblank_on(crtc);
4221
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004222 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004223}
4224
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004225/* IPS only exists on ULT machines and is tied to pipe A. */
4226static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4227{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004228 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004229}
4230
Paulo Zanonie4916942013-09-20 16:21:19 -03004231/*
4232 * This implements the workaround described in the "notes" section of the mode
4233 * set sequence documentation. When going from no pipes or single pipe to
4234 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4235 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4236 */
4237static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4238{
4239 struct drm_device *dev = crtc->base.dev;
4240 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4241
4242 /* We want to get the other_active_crtc only if there's only 1 other
4243 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004244 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004245 if (!crtc_it->active || crtc_it == crtc)
4246 continue;
4247
4248 if (other_active_crtc)
4249 return;
4250
4251 other_active_crtc = crtc_it;
4252 }
4253 if (!other_active_crtc)
4254 return;
4255
4256 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4257 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4258}
4259
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004260static void haswell_crtc_enable(struct drm_crtc *crtc)
4261{
4262 struct drm_device *dev = crtc->dev;
4263 struct drm_i915_private *dev_priv = dev->dev_private;
4264 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4265 struct intel_encoder *encoder;
4266 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004267
4268 WARN_ON(!crtc->enabled);
4269
4270 if (intel_crtc->active)
4271 return;
4272
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004273 if (intel_crtc_to_shared_dpll(intel_crtc))
4274 intel_enable_shared_dpll(intel_crtc);
4275
Daniel Vetter229fca92014-04-24 23:55:09 +02004276 if (intel_crtc->config.has_dp_encoder)
4277 intel_dp_set_m_n(intel_crtc);
4278
4279 intel_set_pipe_timings(intel_crtc);
4280
Clint Taylorebb69c92014-09-30 10:30:22 -07004281 if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
4282 I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
4283 intel_crtc->config.pixel_multiplier - 1);
4284 }
4285
Daniel Vetter229fca92014-04-24 23:55:09 +02004286 if (intel_crtc->config.has_pch_encoder) {
4287 intel_cpu_transcoder_set_m_n(intel_crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07004288 &intel_crtc->config.fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004289 }
4290
4291 haswell_set_pipeconf(crtc);
4292
4293 intel_set_pipe_csc(crtc);
4294
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004295 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004296
Daniel Vettera72e4c92014-09-30 10:56:47 +02004297 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004298 for_each_encoder_on_crtc(dev, crtc, encoder)
4299 if (encoder->pre_enable)
4300 encoder->pre_enable(encoder);
4301
Imre Deak4fe94672014-06-25 22:01:49 +03004302 if (intel_crtc->config.has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004303 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4304 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004305 dev_priv->display.fdi_link_train(crtc);
4306 }
4307
Paulo Zanoni1f544382012-10-24 11:32:00 -02004308 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004309
Jesse Barnesb074cec2013-04-25 12:55:02 -07004310 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004311
4312 /*
4313 * On ILK+ LUT must be loaded before the pipe is running but with
4314 * clocks enabled
4315 */
4316 intel_crtc_load_lut(crtc);
4317
Paulo Zanoni1f544382012-10-24 11:32:00 -02004318 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004319 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004320
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004321 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004322 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004323
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004324 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004325 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004326
Dave Airlie0e32b392014-05-02 14:02:48 +10004327 if (intel_crtc->config.dp_encoder_is_mst)
4328 intel_ddi_set_vc_payload_alloc(crtc, true);
4329
Jani Nikula8807e552013-08-30 19:40:32 +03004330 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004331 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004332 intel_opregion_notify_encoder(encoder, true);
4333 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004334
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004335 assert_vblank_disabled(crtc);
4336 drm_crtc_vblank_on(crtc);
4337
Paulo Zanonie4916942013-09-20 16:21:19 -03004338 /* If we change the relative order between pipe/planes enabling, we need
4339 * to change the workaround. */
4340 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004341 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004342}
4343
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004344static void ironlake_pfit_disable(struct intel_crtc *crtc)
4345{
4346 struct drm_device *dev = crtc->base.dev;
4347 struct drm_i915_private *dev_priv = dev->dev_private;
4348 int pipe = crtc->pipe;
4349
4350 /* To avoid upsetting the power well on haswell only disable the pfit if
4351 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01004352 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004353 I915_WRITE(PF_CTL(pipe), 0);
4354 I915_WRITE(PF_WIN_POS(pipe), 0);
4355 I915_WRITE(PF_WIN_SZ(pipe), 0);
4356 }
4357}
4358
Jesse Barnes6be4a602010-09-10 10:26:01 -07004359static void ironlake_crtc_disable(struct drm_crtc *crtc)
4360{
4361 struct drm_device *dev = crtc->dev;
4362 struct drm_i915_private *dev_priv = dev->dev_private;
4363 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004364 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004365 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004366 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004367
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004368 if (!intel_crtc->active)
4369 return;
4370
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004371 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004372
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004373 drm_crtc_vblank_off(crtc);
4374 assert_vblank_disabled(crtc);
4375
Daniel Vetterea9d7582012-07-10 10:42:52 +02004376 for_each_encoder_on_crtc(dev, crtc, encoder)
4377 encoder->disable(encoder);
4378
Daniel Vetterd925c592013-06-05 13:34:04 +02004379 if (intel_crtc->config.has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004380 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004381
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004382 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004383
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004384 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004385
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004386 for_each_encoder_on_crtc(dev, crtc, encoder)
4387 if (encoder->post_disable)
4388 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004389
Daniel Vetterd925c592013-06-05 13:34:04 +02004390 if (intel_crtc->config.has_pch_encoder) {
4391 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004392
Daniel Vetterd925c592013-06-05 13:34:04 +02004393 ironlake_disable_pch_transcoder(dev_priv, pipe);
Daniel Vettera72e4c92014-09-30 10:56:47 +02004394 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004395
Daniel Vetterd925c592013-06-05 13:34:04 +02004396 if (HAS_PCH_CPT(dev)) {
4397 /* disable TRANS_DP_CTL */
4398 reg = TRANS_DP_CTL(pipe);
4399 temp = I915_READ(reg);
4400 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4401 TRANS_DP_PORT_SEL_MASK);
4402 temp |= TRANS_DP_PORT_SEL_NONE;
4403 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004404
Daniel Vetterd925c592013-06-05 13:34:04 +02004405 /* disable DPLL_SEL */
4406 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004407 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004408 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004409 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004410
4411 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004412 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004413
4414 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004415 }
4416
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004417 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004418 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004419
4420 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004421 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004422 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004423}
4424
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004425static void haswell_crtc_disable(struct drm_crtc *crtc)
4426{
4427 struct drm_device *dev = crtc->dev;
4428 struct drm_i915_private *dev_priv = dev->dev_private;
4429 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4430 struct intel_encoder *encoder;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004431 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004432
4433 if (!intel_crtc->active)
4434 return;
4435
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004436 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004437
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004438 drm_crtc_vblank_off(crtc);
4439 assert_vblank_disabled(crtc);
4440
Jani Nikula8807e552013-08-30 19:40:32 +03004441 for_each_encoder_on_crtc(dev, crtc, encoder) {
4442 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004443 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004444 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004445
Paulo Zanoni86642812013-04-12 17:57:57 -03004446 if (intel_crtc->config.has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004447 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4448 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004449 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004450
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004451 if (intel_crtc->config.dp_encoder_is_mst)
4452 intel_ddi_set_vc_payload_alloc(crtc, false);
4453
Paulo Zanoniad80a812012-10-24 16:06:19 -02004454 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004455
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004456 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004457
Paulo Zanoni1f544382012-10-24 11:32:00 -02004458 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004459
Daniel Vetter88adfff2013-03-28 10:42:01 +01004460 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004461 lpt_disable_pch_transcoder(dev_priv);
Daniel Vettera72e4c92014-09-30 10:56:47 +02004462 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4463 true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004464 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004465 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004466
Imre Deak97b040a2014-06-25 22:01:50 +03004467 for_each_encoder_on_crtc(dev, crtc, encoder)
4468 if (encoder->post_disable)
4469 encoder->post_disable(encoder);
4470
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004471 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004472 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004473
4474 mutex_lock(&dev->struct_mutex);
4475 intel_update_fbc(dev);
4476 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004477
4478 if (intel_crtc_to_shared_dpll(intel_crtc))
4479 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004480}
4481
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004482static void ironlake_crtc_off(struct drm_crtc *crtc)
4483{
4484 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004485 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004486}
4487
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004488
Jesse Barnes2dd24552013-04-25 12:55:01 -07004489static void i9xx_pfit_enable(struct intel_crtc *crtc)
4490{
4491 struct drm_device *dev = crtc->base.dev;
4492 struct drm_i915_private *dev_priv = dev->dev_private;
4493 struct intel_crtc_config *pipe_config = &crtc->config;
4494
Daniel Vetter328d8e82013-05-08 10:36:31 +02004495 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004496 return;
4497
Daniel Vetterc0b03412013-05-28 12:05:54 +02004498 /*
4499 * The panel fitter should only be adjusted whilst the pipe is disabled,
4500 * according to register description and PRM.
4501 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004502 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4503 assert_pipe_disabled(dev_priv, crtc->pipe);
4504
Jesse Barnesb074cec2013-04-25 12:55:02 -07004505 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4506 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004507
4508 /* Border color in case we don't scale up to the full screen. Black by
4509 * default, change to something else for debugging. */
4510 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004511}
4512
Dave Airlied05410f2014-06-05 13:22:59 +10004513static enum intel_display_power_domain port_to_power_domain(enum port port)
4514{
4515 switch (port) {
4516 case PORT_A:
4517 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4518 case PORT_B:
4519 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4520 case PORT_C:
4521 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4522 case PORT_D:
4523 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4524 default:
4525 WARN_ON_ONCE(1);
4526 return POWER_DOMAIN_PORT_OTHER;
4527 }
4528}
4529
Imre Deak77d22dc2014-03-05 16:20:52 +02004530#define for_each_power_domain(domain, mask) \
4531 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4532 if ((1 << (domain)) & (mask))
4533
Imre Deak319be8a2014-03-04 19:22:57 +02004534enum intel_display_power_domain
4535intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004536{
Imre Deak319be8a2014-03-04 19:22:57 +02004537 struct drm_device *dev = intel_encoder->base.dev;
4538 struct intel_digital_port *intel_dig_port;
4539
4540 switch (intel_encoder->type) {
4541 case INTEL_OUTPUT_UNKNOWN:
4542 /* Only DDI platforms should ever use this output type */
4543 WARN_ON_ONCE(!HAS_DDI(dev));
4544 case INTEL_OUTPUT_DISPLAYPORT:
4545 case INTEL_OUTPUT_HDMI:
4546 case INTEL_OUTPUT_EDP:
4547 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004548 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004549 case INTEL_OUTPUT_DP_MST:
4550 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4551 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004552 case INTEL_OUTPUT_ANALOG:
4553 return POWER_DOMAIN_PORT_CRT;
4554 case INTEL_OUTPUT_DSI:
4555 return POWER_DOMAIN_PORT_DSI;
4556 default:
4557 return POWER_DOMAIN_PORT_OTHER;
4558 }
4559}
4560
4561static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4562{
4563 struct drm_device *dev = crtc->dev;
4564 struct intel_encoder *intel_encoder;
4565 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4566 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004567 unsigned long mask;
4568 enum transcoder transcoder;
4569
4570 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4571
4572 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4573 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Daniel Vetterfabf6e52014-05-29 14:10:22 +02004574 if (intel_crtc->config.pch_pfit.enabled ||
4575 intel_crtc->config.pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004576 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4577
Imre Deak319be8a2014-03-04 19:22:57 +02004578 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4579 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4580
Imre Deak77d22dc2014-03-05 16:20:52 +02004581 return mask;
4582}
4583
Imre Deak77d22dc2014-03-05 16:20:52 +02004584static void modeset_update_crtc_power_domains(struct drm_device *dev)
4585{
4586 struct drm_i915_private *dev_priv = dev->dev_private;
4587 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4588 struct intel_crtc *crtc;
4589
4590 /*
4591 * First get all needed power domains, then put all unneeded, to avoid
4592 * any unnecessary toggling of the power wells.
4593 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004594 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004595 enum intel_display_power_domain domain;
4596
4597 if (!crtc->base.enabled)
4598 continue;
4599
Imre Deak319be8a2014-03-04 19:22:57 +02004600 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004601
4602 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4603 intel_display_power_get(dev_priv, domain);
4604 }
4605
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004606 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004607 enum intel_display_power_domain domain;
4608
4609 for_each_power_domain(domain, crtc->enabled_power_domains)
4610 intel_display_power_put(dev_priv, domain);
4611
4612 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4613 }
4614
4615 intel_display_set_init_power(dev_priv, false);
4616}
4617
Ville Syrjälädfcab172014-06-13 13:37:47 +03004618/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004619static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004620{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004621 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004622
Jesse Barnes586f49d2013-11-04 16:06:59 -08004623 /* Obtain SKU information */
4624 mutex_lock(&dev_priv->dpio_lock);
4625 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4626 CCK_FUSE_HPLL_FREQ_MASK;
4627 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004628
Ville Syrjälädfcab172014-06-13 13:37:47 +03004629 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004630}
4631
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004632static void vlv_update_cdclk(struct drm_device *dev)
4633{
4634 struct drm_i915_private *dev_priv = dev->dev_private;
4635
4636 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03004637 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004638 dev_priv->vlv_cdclk_freq);
4639
4640 /*
4641 * Program the gmbus_freq based on the cdclk frequency.
4642 * BSpec erroneously claims we should aim for 4MHz, but
4643 * in fact 1MHz is the correct frequency.
4644 */
4645 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4646}
4647
Jesse Barnes30a970c2013-11-04 13:48:12 -08004648/* Adjust CDclk dividers to allow high res or save power if possible */
4649static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4650{
4651 struct drm_i915_private *dev_priv = dev->dev_private;
4652 u32 val, cmd;
4653
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03004654 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02004655
Ville Syrjälädfcab172014-06-13 13:37:47 +03004656 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08004657 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03004658 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004659 cmd = 1;
4660 else
4661 cmd = 0;
4662
4663 mutex_lock(&dev_priv->rps.hw_lock);
4664 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4665 val &= ~DSPFREQGUAR_MASK;
4666 val |= (cmd << DSPFREQGUAR_SHIFT);
4667 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4668 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4669 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4670 50)) {
4671 DRM_ERROR("timed out waiting for CDclk change\n");
4672 }
4673 mutex_unlock(&dev_priv->rps.hw_lock);
4674
Ville Syrjälädfcab172014-06-13 13:37:47 +03004675 if (cdclk == 400000) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08004676 u32 divider, vco;
4677
4678 vco = valleyview_get_vco(dev_priv);
Ville Syrjälädfcab172014-06-13 13:37:47 +03004679 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004680
4681 mutex_lock(&dev_priv->dpio_lock);
4682 /* adjust cdclk divider */
4683 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03004684 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004685 val |= divider;
4686 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03004687
4688 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4689 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4690 50))
4691 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08004692 mutex_unlock(&dev_priv->dpio_lock);
4693 }
4694
4695 mutex_lock(&dev_priv->dpio_lock);
4696 /* adjust self-refresh exit latency value */
4697 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4698 val &= ~0x7f;
4699
4700 /*
4701 * For high bandwidth configs, we set a higher latency in the bunit
4702 * so that the core display fetch happens in time to avoid underruns.
4703 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03004704 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004705 val |= 4500 / 250; /* 4.5 usec */
4706 else
4707 val |= 3000 / 250; /* 3.0 usec */
4708 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4709 mutex_unlock(&dev_priv->dpio_lock);
4710
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004711 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004712}
4713
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004714static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4715{
4716 struct drm_i915_private *dev_priv = dev->dev_private;
4717 u32 val, cmd;
4718
4719 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4720
4721 switch (cdclk) {
4722 case 400000:
4723 cmd = 3;
4724 break;
4725 case 333333:
4726 case 320000:
4727 cmd = 2;
4728 break;
4729 case 266667:
4730 cmd = 1;
4731 break;
4732 case 200000:
4733 cmd = 0;
4734 break;
4735 default:
4736 WARN_ON(1);
4737 return;
4738 }
4739
4740 mutex_lock(&dev_priv->rps.hw_lock);
4741 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4742 val &= ~DSPFREQGUAR_MASK_CHV;
4743 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4744 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4745 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4746 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4747 50)) {
4748 DRM_ERROR("timed out waiting for CDclk change\n");
4749 }
4750 mutex_unlock(&dev_priv->rps.hw_lock);
4751
4752 vlv_update_cdclk(dev);
4753}
4754
Jesse Barnes30a970c2013-11-04 13:48:12 -08004755static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4756 int max_pixclk)
4757{
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004758 int vco = valleyview_get_vco(dev_priv);
4759 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4760
Ville Syrjäläd49a3402014-06-28 02:03:58 +03004761 /* FIXME: Punit isn't quite ready yet */
4762 if (IS_CHERRYVIEW(dev_priv->dev))
4763 return 400000;
4764
Jesse Barnes30a970c2013-11-04 13:48:12 -08004765 /*
4766 * Really only a few cases to deal with, as only 4 CDclks are supported:
4767 * 200MHz
4768 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004769 * 320/333MHz (depends on HPLL freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004770 * 400MHz
4771 * So we check to see whether we're above 90% of the lower bin and
4772 * adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004773 *
4774 * We seem to get an unstable or solid color picture at 200MHz.
4775 * Not sure what's wrong. For now use 200MHz only when all pipes
4776 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08004777 */
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004778 if (max_pixclk > freq_320*9/10)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004779 return 400000;
4780 else if (max_pixclk > 266667*9/10)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004781 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004782 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004783 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004784 else
4785 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004786}
4787
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004788/* compute the max pixel clock for new configuration */
4789static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004790{
4791 struct drm_device *dev = dev_priv->dev;
4792 struct intel_crtc *intel_crtc;
4793 int max_pixclk = 0;
4794
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004795 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004796 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004797 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004798 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004799 }
4800
4801 return max_pixclk;
4802}
4803
4804static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004805 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004806{
4807 struct drm_i915_private *dev_priv = dev->dev_private;
4808 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004809 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004810
Imre Deakd60c4472014-03-27 17:45:10 +02004811 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4812 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004813 return;
4814
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004815 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004816 for_each_intel_crtc(dev, intel_crtc)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004817 if (intel_crtc->base.enabled)
4818 *prepare_pipes |= (1 << intel_crtc->pipe);
4819}
4820
4821static void valleyview_modeset_global_resources(struct drm_device *dev)
4822{
4823 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004824 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004825 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4826
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004827 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4828 if (IS_CHERRYVIEW(dev))
4829 cherryview_set_cdclk(dev, req_cdclk);
4830 else
4831 valleyview_set_cdclk(dev, req_cdclk);
4832 }
4833
Imre Deak77961eb2014-03-05 16:20:56 +02004834 modeset_update_crtc_power_domains(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004835}
4836
Jesse Barnes89b667f2013-04-18 14:51:36 -07004837static void valleyview_crtc_enable(struct drm_crtc *crtc)
4838{
4839 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02004840 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4842 struct intel_encoder *encoder;
4843 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03004844 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004845
4846 WARN_ON(!crtc->enabled);
4847
4848 if (intel_crtc->active)
4849 return;
4850
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004851 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05304852
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03004853 if (!is_dsi) {
4854 if (IS_CHERRYVIEW(dev))
Ville Syrjäläd288f652014-10-28 13:20:22 +02004855 chv_prepare_pll(intel_crtc, &intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03004856 else
Ville Syrjäläd288f652014-10-28 13:20:22 +02004857 vlv_prepare_pll(intel_crtc, &intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03004858 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02004859
4860 if (intel_crtc->config.has_dp_encoder)
4861 intel_dp_set_m_n(intel_crtc);
4862
4863 intel_set_pipe_timings(intel_crtc);
4864
Ville Syrjäläc14b0482014-10-16 20:52:34 +03004865 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
4866 struct drm_i915_private *dev_priv = dev->dev_private;
4867
4868 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
4869 I915_WRITE(CHV_CANVAS(pipe), 0);
4870 }
4871
Daniel Vetter5b18e572014-04-24 23:55:06 +02004872 i9xx_set_pipeconf(intel_crtc);
4873
Jesse Barnes89b667f2013-04-18 14:51:36 -07004874 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004875
Daniel Vettera72e4c92014-09-30 10:56:47 +02004876 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004877
Jesse Barnes89b667f2013-04-18 14:51:36 -07004878 for_each_encoder_on_crtc(dev, crtc, encoder)
4879 if (encoder->pre_pll_enable)
4880 encoder->pre_pll_enable(encoder);
4881
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004882 if (!is_dsi) {
4883 if (IS_CHERRYVIEW(dev))
Ville Syrjäläd288f652014-10-28 13:20:22 +02004884 chv_enable_pll(intel_crtc, &intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004885 else
Ville Syrjäläd288f652014-10-28 13:20:22 +02004886 vlv_enable_pll(intel_crtc, &intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004887 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07004888
4889 for_each_encoder_on_crtc(dev, crtc, encoder)
4890 if (encoder->pre_enable)
4891 encoder->pre_enable(encoder);
4892
Jesse Barnes2dd24552013-04-25 12:55:01 -07004893 i9xx_pfit_enable(intel_crtc);
4894
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004895 intel_crtc_load_lut(crtc);
4896
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004897 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004898 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004899
Jani Nikula50049452013-07-30 12:20:32 +03004900 for_each_encoder_on_crtc(dev, crtc, encoder)
4901 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004902
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004903 assert_vblank_disabled(crtc);
4904 drm_crtc_vblank_on(crtc);
4905
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004906 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004907
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004908 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02004909 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004910}
4911
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004912static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4913{
4914 struct drm_device *dev = crtc->base.dev;
4915 struct drm_i915_private *dev_priv = dev->dev_private;
4916
4917 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4918 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4919}
4920
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004921static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004922{
4923 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02004924 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08004925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004926 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004927 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004928
Daniel Vetter08a48462012-07-02 11:43:47 +02004929 WARN_ON(!crtc->enabled);
4930
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004931 if (intel_crtc->active)
4932 return;
4933
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004934 i9xx_set_pll_dividers(intel_crtc);
4935
Daniel Vetter5b18e572014-04-24 23:55:06 +02004936 if (intel_crtc->config.has_dp_encoder)
4937 intel_dp_set_m_n(intel_crtc);
4938
4939 intel_set_pipe_timings(intel_crtc);
4940
Daniel Vetter5b18e572014-04-24 23:55:06 +02004941 i9xx_set_pipeconf(intel_crtc);
4942
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004943 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004944
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004945 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02004946 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004947
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004948 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004949 if (encoder->pre_enable)
4950 encoder->pre_enable(encoder);
4951
Daniel Vetterf6736a12013-06-05 13:34:30 +02004952 i9xx_enable_pll(intel_crtc);
4953
Jesse Barnes2dd24552013-04-25 12:55:01 -07004954 i9xx_pfit_enable(intel_crtc);
4955
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004956 intel_crtc_load_lut(crtc);
4957
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004958 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004959 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004960
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004961 for_each_encoder_on_crtc(dev, crtc, encoder)
4962 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004963
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004964 assert_vblank_disabled(crtc);
4965 drm_crtc_vblank_on(crtc);
4966
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004967 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004968
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004969 /*
4970 * Gen2 reports pipe underruns whenever all planes are disabled.
4971 * So don't enable underrun reporting before at least some planes
4972 * are enabled.
4973 * FIXME: Need to fix the logic to work when we turn off all planes
4974 * but leave the pipe running.
4975 */
4976 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02004977 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004978
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004979 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02004980 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004981}
4982
Daniel Vetter87476d62013-04-11 16:29:06 +02004983static void i9xx_pfit_disable(struct intel_crtc *crtc)
4984{
4985 struct drm_device *dev = crtc->base.dev;
4986 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004987
4988 if (!crtc->config.gmch_pfit.control)
4989 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004990
4991 assert_pipe_disabled(dev_priv, crtc->pipe);
4992
Daniel Vetter328d8e82013-05-08 10:36:31 +02004993 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4994 I915_READ(PFIT_CONTROL));
4995 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004996}
4997
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004998static void i9xx_crtc_disable(struct drm_crtc *crtc)
4999{
5000 struct drm_device *dev = crtc->dev;
5001 struct drm_i915_private *dev_priv = dev->dev_private;
5002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005003 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005004 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005005
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005006 if (!intel_crtc->active)
5007 return;
5008
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005009 /*
5010 * Gen2 reports pipe underruns whenever all planes are disabled.
5011 * So diasble underrun reporting before all the planes get disabled.
5012 * FIXME: Need to fix the logic to work when we turn off all planes
5013 * but leave the pipe running.
5014 */
5015 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005016 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005017
Imre Deak564ed192014-06-13 14:54:21 +03005018 /*
5019 * Vblank time updates from the shadow to live plane control register
5020 * are blocked if the memory self-refresh mode is active at that
5021 * moment. So to make sure the plane gets truly disabled, disable
5022 * first the self-refresh mode. The self-refresh enable bit in turn
5023 * will be checked/applied by the HW only at the next frame start
5024 * event which is after the vblank start event, so we need to have a
5025 * wait-for-vblank between disabling the plane and the pipe.
5026 */
5027 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005028 intel_crtc_disable_planes(crtc);
5029
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005030 /*
5031 * On gen2 planes are double buffered but the pipe isn't, so we must
5032 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005033 * We also need to wait on all gmch platforms because of the
5034 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005035 */
Imre Deak564ed192014-06-13 14:54:21 +03005036 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005037
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005038 drm_crtc_vblank_off(crtc);
5039 assert_vblank_disabled(crtc);
5040
5041 for_each_encoder_on_crtc(dev, crtc, encoder)
5042 encoder->disable(encoder);
5043
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005044 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005045
Daniel Vetter87476d62013-04-11 16:29:06 +02005046 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005047
Jesse Barnes89b667f2013-04-18 14:51:36 -07005048 for_each_encoder_on_crtc(dev, crtc, encoder)
5049 if (encoder->post_disable)
5050 encoder->post_disable(encoder);
5051
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005052 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005053 if (IS_CHERRYVIEW(dev))
5054 chv_disable_pll(dev_priv, pipe);
5055 else if (IS_VALLEYVIEW(dev))
5056 vlv_disable_pll(dev_priv, pipe);
5057 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005058 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005059 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005060
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005061 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005062 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005063
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005064 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005065 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005066
Daniel Vetterefa96242014-04-24 23:55:02 +02005067 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01005068 intel_update_fbc(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005069 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005070}
5071
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005072static void i9xx_crtc_off(struct drm_crtc *crtc)
5073{
5074}
5075
Daniel Vetter976f8a22012-07-08 22:34:21 +02005076static void intel_crtc_update_sarea(struct drm_crtc *crtc,
5077 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005078{
5079 struct drm_device *dev = crtc->dev;
5080 struct drm_i915_master_private *master_priv;
5081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5082 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005083
5084 if (!dev->primary->master)
5085 return;
5086
5087 master_priv = dev->primary->master->driver_priv;
5088 if (!master_priv->sarea_priv)
5089 return;
5090
Jesse Barnes79e53942008-11-07 14:24:08 -08005091 switch (pipe) {
5092 case 0:
5093 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
5094 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
5095 break;
5096 case 1:
5097 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
5098 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
5099 break;
5100 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005101 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005102 break;
5103 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005104}
5105
Borun Fub04c5bd2014-07-12 10:02:27 +05305106/* Master function to enable/disable CRTC and corresponding power wells */
5107void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005108{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005109 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005110 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005111 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005112 enum intel_display_power_domain domain;
5113 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005114
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005115 if (enable) {
5116 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005117 domains = get_crtc_power_domains(crtc);
5118 for_each_power_domain(domain, domains)
5119 intel_display_power_get(dev_priv, domain);
5120 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005121
5122 dev_priv->display.crtc_enable(crtc);
5123 }
5124 } else {
5125 if (intel_crtc->active) {
5126 dev_priv->display.crtc_disable(crtc);
5127
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005128 domains = intel_crtc->enabled_power_domains;
5129 for_each_power_domain(domain, domains)
5130 intel_display_power_put(dev_priv, domain);
5131 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005132 }
5133 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305134}
5135
5136/**
5137 * Sets the power management mode of the pipe and plane.
5138 */
5139void intel_crtc_update_dpms(struct drm_crtc *crtc)
5140{
5141 struct drm_device *dev = crtc->dev;
5142 struct intel_encoder *intel_encoder;
5143 bool enable = false;
5144
5145 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5146 enable |= intel_encoder->connectors_active;
5147
5148 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005149
5150 intel_crtc_update_sarea(crtc, enable);
5151}
5152
Daniel Vetter976f8a22012-07-08 22:34:21 +02005153static void intel_crtc_disable(struct drm_crtc *crtc)
5154{
5155 struct drm_device *dev = crtc->dev;
5156 struct drm_connector *connector;
5157 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2ff8fde2014-07-08 07:50:07 -07005158 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
Daniel Vettera071fa02014-06-18 23:28:09 +02005159 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005160
5161 /* crtc should still be enabled when we disable it. */
5162 WARN_ON(!crtc->enabled);
5163
5164 dev_priv->display.crtc_disable(crtc);
5165 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005166 dev_priv->display.off(crtc);
5167
Matt Roperf4510a22014-04-01 15:22:40 -07005168 if (crtc->primary->fb) {
Chris Wilsoncdd59982010-09-08 16:30:16 +01005169 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02005170 intel_unpin_fb_obj(old_obj);
5171 i915_gem_track_fb(old_obj, NULL,
5172 INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilsoncdd59982010-09-08 16:30:16 +01005173 mutex_unlock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07005174 crtc->primary->fb = NULL;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005175 }
5176
5177 /* Update computed state. */
5178 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5179 if (!connector->encoder || !connector->encoder->crtc)
5180 continue;
5181
5182 if (connector->encoder->crtc != crtc)
5183 continue;
5184
5185 connector->dpms = DRM_MODE_DPMS_OFF;
5186 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005187 }
5188}
5189
Chris Wilsonea5b2132010-08-04 13:50:23 +01005190void intel_encoder_destroy(struct drm_encoder *encoder)
5191{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005192 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005193
Chris Wilsonea5b2132010-08-04 13:50:23 +01005194 drm_encoder_cleanup(encoder);
5195 kfree(intel_encoder);
5196}
5197
Damien Lespiau92373292013-08-08 22:28:57 +01005198/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005199 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5200 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005201static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005202{
5203 if (mode == DRM_MODE_DPMS_ON) {
5204 encoder->connectors_active = true;
5205
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005206 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005207 } else {
5208 encoder->connectors_active = false;
5209
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005210 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005211 }
5212}
5213
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005214/* Cross check the actual hw state with our own modeset state tracking (and it's
5215 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005216static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005217{
5218 if (connector->get_hw_state(connector)) {
5219 struct intel_encoder *encoder = connector->encoder;
5220 struct drm_crtc *crtc;
5221 bool encoder_enabled;
5222 enum pipe pipe;
5223
5224 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5225 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005226 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005227
Dave Airlie0e32b392014-05-02 14:02:48 +10005228 /* there is no real hw state for MST connectors */
5229 if (connector->mst_port)
5230 return;
5231
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005232 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5233 "wrong connector dpms state\n");
5234 WARN(connector->base.encoder != &encoder->base,
5235 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005236
Dave Airlie36cd7442014-05-02 13:44:18 +10005237 if (encoder) {
5238 WARN(!encoder->connectors_active,
5239 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005240
Dave Airlie36cd7442014-05-02 13:44:18 +10005241 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5242 WARN(!encoder_enabled, "encoder not enabled\n");
5243 if (WARN_ON(!encoder->base.crtc))
5244 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005245
Dave Airlie36cd7442014-05-02 13:44:18 +10005246 crtc = encoder->base.crtc;
5247
5248 WARN(!crtc->enabled, "crtc not enabled\n");
5249 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5250 WARN(pipe != to_intel_crtc(crtc)->pipe,
5251 "encoder active on the wrong pipe\n");
5252 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005253 }
5254}
5255
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005256/* Even simpler default implementation, if there's really no special case to
5257 * consider. */
5258void intel_connector_dpms(struct drm_connector *connector, int mode)
5259{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005260 /* All the simple cases only support two dpms states. */
5261 if (mode != DRM_MODE_DPMS_ON)
5262 mode = DRM_MODE_DPMS_OFF;
5263
5264 if (mode == connector->dpms)
5265 return;
5266
5267 connector->dpms = mode;
5268
5269 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01005270 if (connector->encoder)
5271 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005272
Daniel Vetterb9805142012-08-31 17:37:33 +02005273 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005274}
5275
Daniel Vetterf0947c32012-07-02 13:10:34 +02005276/* Simple connector->get_hw_state implementation for encoders that support only
5277 * one connector and no cloning and hence the encoder state determines the state
5278 * of the connector. */
5279bool intel_connector_get_hw_state(struct intel_connector *connector)
5280{
Daniel Vetter24929352012-07-02 20:28:59 +02005281 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005282 struct intel_encoder *encoder = connector->encoder;
5283
5284 return encoder->get_hw_state(encoder, &pipe);
5285}
5286
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005287static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5288 struct intel_crtc_config *pipe_config)
5289{
5290 struct drm_i915_private *dev_priv = dev->dev_private;
5291 struct intel_crtc *pipe_B_crtc =
5292 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5293
5294 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5295 pipe_name(pipe), pipe_config->fdi_lanes);
5296 if (pipe_config->fdi_lanes > 4) {
5297 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5298 pipe_name(pipe), pipe_config->fdi_lanes);
5299 return false;
5300 }
5301
Paulo Zanonibafb6552013-11-02 21:07:44 -07005302 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005303 if (pipe_config->fdi_lanes > 2) {
5304 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5305 pipe_config->fdi_lanes);
5306 return false;
5307 } else {
5308 return true;
5309 }
5310 }
5311
5312 if (INTEL_INFO(dev)->num_pipes == 2)
5313 return true;
5314
5315 /* Ivybridge 3 pipe is really complicated */
5316 switch (pipe) {
5317 case PIPE_A:
5318 return true;
5319 case PIPE_B:
5320 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5321 pipe_config->fdi_lanes > 2) {
5322 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5323 pipe_name(pipe), pipe_config->fdi_lanes);
5324 return false;
5325 }
5326 return true;
5327 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005328 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005329 pipe_B_crtc->config.fdi_lanes <= 2) {
5330 if (pipe_config->fdi_lanes > 2) {
5331 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5332 pipe_name(pipe), pipe_config->fdi_lanes);
5333 return false;
5334 }
5335 } else {
5336 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5337 return false;
5338 }
5339 return true;
5340 default:
5341 BUG();
5342 }
5343}
5344
Daniel Vettere29c22c2013-02-21 00:00:16 +01005345#define RETRY 1
5346static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5347 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005348{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005349 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005350 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005351 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005352 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005353
Daniel Vettere29c22c2013-02-21 00:00:16 +01005354retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005355 /* FDI is a binary signal running at ~2.7GHz, encoding
5356 * each output octet as 10 bits. The actual frequency
5357 * is stored as a divider into a 100MHz clock, and the
5358 * mode pixel clock is stored in units of 1KHz.
5359 * Hence the bw of each lane in terms of the mode signal
5360 * is:
5361 */
5362 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5363
Damien Lespiau241bfc32013-09-25 16:45:37 +01005364 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005365
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005366 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005367 pipe_config->pipe_bpp);
5368
5369 pipe_config->fdi_lanes = lane;
5370
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005371 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005372 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005373
Daniel Vettere29c22c2013-02-21 00:00:16 +01005374 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5375 intel_crtc->pipe, pipe_config);
5376 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5377 pipe_config->pipe_bpp -= 2*3;
5378 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5379 pipe_config->pipe_bpp);
5380 needs_recompute = true;
5381 pipe_config->bw_constrained = true;
5382
5383 goto retry;
5384 }
5385
5386 if (needs_recompute)
5387 return RETRY;
5388
5389 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005390}
5391
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005392static void hsw_compute_ips_config(struct intel_crtc *crtc,
5393 struct intel_crtc_config *pipe_config)
5394{
Jani Nikulad330a952014-01-21 11:24:25 +02005395 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005396 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005397 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005398}
5399
Daniel Vettera43f6e02013-06-07 23:10:32 +02005400static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01005401 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005402{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005403 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005404 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005405
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005406 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005407 if (INTEL_INFO(dev)->gen < 4) {
5408 struct drm_i915_private *dev_priv = dev->dev_private;
5409 int clock_limit =
5410 dev_priv->display.get_display_clock_speed(dev);
5411
5412 /*
5413 * Enable pixel doubling when the dot clock
5414 * is > 90% of the (display) core speed.
5415 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005416 * GDG double wide on either pipe,
5417 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005418 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005419 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005420 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005421 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005422 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005423 }
5424
Damien Lespiau241bfc32013-09-25 16:45:37 +01005425 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005426 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005427 }
Chris Wilson89749352010-09-12 18:25:19 +01005428
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005429 /*
5430 * Pipe horizontal size must be even in:
5431 * - DVO ganged mode
5432 * - LVDS dual channel mode
5433 * - Double wide pipe
5434 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005435 if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005436 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5437 pipe_config->pipe_src_w &= ~1;
5438
Damien Lespiau8693a822013-05-03 18:48:11 +01005439 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5440 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005441 */
5442 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5443 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005444 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005445
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005446 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005447 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005448 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005449 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5450 * for lvds. */
5451 pipe_config->pipe_bpp = 8*3;
5452 }
5453
Damien Lespiauf5adf942013-06-24 18:29:34 +01005454 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005455 hsw_compute_ips_config(crtc, pipe_config);
5456
Daniel Vetter12030432014-06-25 22:02:00 +03005457 /*
5458 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5459 * old clock survives for now.
5460 */
5461 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005462 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005463
Daniel Vetter877d48d2013-04-19 11:24:43 +02005464 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005465 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005466
Daniel Vettere29c22c2013-02-21 00:00:16 +01005467 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005468}
5469
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005470static int valleyview_get_display_clock_speed(struct drm_device *dev)
5471{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005472 struct drm_i915_private *dev_priv = dev->dev_private;
5473 int vco = valleyview_get_vco(dev_priv);
5474 u32 val;
5475 int divider;
5476
Ville Syrjäläd49a3402014-06-28 02:03:58 +03005477 /* FIXME: Punit isn't quite ready yet */
5478 if (IS_CHERRYVIEW(dev))
5479 return 400000;
5480
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005481 mutex_lock(&dev_priv->dpio_lock);
5482 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5483 mutex_unlock(&dev_priv->dpio_lock);
5484
5485 divider = val & DISPLAY_FREQUENCY_VALUES;
5486
Ville Syrjälä7d007f42014-06-13 13:37:53 +03005487 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5488 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5489 "cdclk change in progress\n");
5490
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005491 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005492}
5493
Jesse Barnese70236a2009-09-21 10:42:27 -07005494static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005495{
Jesse Barnese70236a2009-09-21 10:42:27 -07005496 return 400000;
5497}
Jesse Barnes79e53942008-11-07 14:24:08 -08005498
Jesse Barnese70236a2009-09-21 10:42:27 -07005499static int i915_get_display_clock_speed(struct drm_device *dev)
5500{
5501 return 333000;
5502}
Jesse Barnes79e53942008-11-07 14:24:08 -08005503
Jesse Barnese70236a2009-09-21 10:42:27 -07005504static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5505{
5506 return 200000;
5507}
Jesse Barnes79e53942008-11-07 14:24:08 -08005508
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005509static int pnv_get_display_clock_speed(struct drm_device *dev)
5510{
5511 u16 gcfgc = 0;
5512
5513 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5514
5515 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5516 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5517 return 267000;
5518 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5519 return 333000;
5520 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5521 return 444000;
5522 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5523 return 200000;
5524 default:
5525 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5526 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5527 return 133000;
5528 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5529 return 167000;
5530 }
5531}
5532
Jesse Barnese70236a2009-09-21 10:42:27 -07005533static int i915gm_get_display_clock_speed(struct drm_device *dev)
5534{
5535 u16 gcfgc = 0;
5536
5537 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5538
5539 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005540 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005541 else {
5542 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5543 case GC_DISPLAY_CLOCK_333_MHZ:
5544 return 333000;
5545 default:
5546 case GC_DISPLAY_CLOCK_190_200_MHZ:
5547 return 190000;
5548 }
5549 }
5550}
Jesse Barnes79e53942008-11-07 14:24:08 -08005551
Jesse Barnese70236a2009-09-21 10:42:27 -07005552static int i865_get_display_clock_speed(struct drm_device *dev)
5553{
5554 return 266000;
5555}
5556
5557static int i855_get_display_clock_speed(struct drm_device *dev)
5558{
5559 u16 hpllcc = 0;
5560 /* Assume that the hardware is in the high speed state. This
5561 * should be the default.
5562 */
5563 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5564 case GC_CLOCK_133_200:
5565 case GC_CLOCK_100_200:
5566 return 200000;
5567 case GC_CLOCK_166_250:
5568 return 250000;
5569 case GC_CLOCK_100_133:
5570 return 133000;
5571 }
5572
5573 /* Shouldn't happen */
5574 return 0;
5575}
5576
5577static int i830_get_display_clock_speed(struct drm_device *dev)
5578{
5579 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005580}
5581
Zhenyu Wang2c072452009-06-05 15:38:42 +08005582static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005583intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005584{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005585 while (*num > DATA_LINK_M_N_MASK ||
5586 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005587 *num >>= 1;
5588 *den >>= 1;
5589 }
5590}
5591
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005592static void compute_m_n(unsigned int m, unsigned int n,
5593 uint32_t *ret_m, uint32_t *ret_n)
5594{
5595 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5596 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5597 intel_reduce_m_n_ratio(ret_m, ret_n);
5598}
5599
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005600void
5601intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5602 int pixel_clock, int link_clock,
5603 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005604{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005605 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005606
5607 compute_m_n(bits_per_pixel * pixel_clock,
5608 link_clock * nlanes * 8,
5609 &m_n->gmch_m, &m_n->gmch_n);
5610
5611 compute_m_n(pixel_clock, link_clock,
5612 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005613}
5614
Chris Wilsona7615032011-01-12 17:04:08 +00005615static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5616{
Jani Nikulad330a952014-01-21 11:24:25 +02005617 if (i915.panel_use_ssc >= 0)
5618 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005619 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005620 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005621}
5622
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005623static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005624{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005625 struct drm_device *dev = crtc->base.dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005626 struct drm_i915_private *dev_priv = dev->dev_private;
5627 int refclk;
5628
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005629 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005630 refclk = 100000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005631 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005632 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005633 refclk = dev_priv->vbt.lvds_ssc_freq;
5634 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005635 } else if (!IS_GEN2(dev)) {
5636 refclk = 96000;
5637 } else {
5638 refclk = 48000;
5639 }
5640
5641 return refclk;
5642}
5643
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005644static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005645{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005646 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005647}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005648
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005649static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5650{
5651 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005652}
5653
Daniel Vetterf47709a2013-03-28 10:42:02 +01005654static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005655 intel_clock_t *reduced_clock)
5656{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005657 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005658 u32 fp, fp2 = 0;
5659
5660 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005661 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005662 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005663 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005664 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005665 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005666 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005667 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005668 }
5669
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005670 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005671
Daniel Vetterf47709a2013-03-28 10:42:02 +01005672 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005673 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005674 reduced_clock && i915.powersave) {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005675 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005676 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005677 } else {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005678 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005679 }
5680}
5681
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005682static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5683 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005684{
5685 u32 reg_val;
5686
5687 /*
5688 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5689 * and set it to a reasonable value instead.
5690 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005691 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005692 reg_val &= 0xffffff00;
5693 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005694 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005695
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005696 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005697 reg_val &= 0x8cffffff;
5698 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005699 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005700
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005701 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005702 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005703 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005704
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005705 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005706 reg_val &= 0x00ffffff;
5707 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005708 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005709}
5710
Daniel Vetterb5518422013-05-03 11:49:48 +02005711static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5712 struct intel_link_m_n *m_n)
5713{
5714 struct drm_device *dev = crtc->base.dev;
5715 struct drm_i915_private *dev_priv = dev->dev_private;
5716 int pipe = crtc->pipe;
5717
Daniel Vettere3b95f12013-05-03 11:49:49 +02005718 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5719 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5720 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5721 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005722}
5723
5724static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07005725 struct intel_link_m_n *m_n,
5726 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02005727{
5728 struct drm_device *dev = crtc->base.dev;
5729 struct drm_i915_private *dev_priv = dev->dev_private;
5730 int pipe = crtc->pipe;
5731 enum transcoder transcoder = crtc->config.cpu_transcoder;
5732
5733 if (INTEL_INFO(dev)->gen >= 5) {
5734 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5735 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5736 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5737 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07005738 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5739 * for gen < 8) and if DRRS is supported (to make sure the
5740 * registers are not unnecessarily accessed).
5741 */
5742 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5743 crtc->config.has_drrs) {
5744 I915_WRITE(PIPE_DATA_M2(transcoder),
5745 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5746 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5747 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5748 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5749 }
Daniel Vetterb5518422013-05-03 11:49:48 +02005750 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005751 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5752 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5753 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5754 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005755 }
5756}
5757
Vandana Kannanf769cd22014-08-05 07:51:22 -07005758void intel_dp_set_m_n(struct intel_crtc *crtc)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005759{
5760 if (crtc->config.has_pch_encoder)
5761 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5762 else
Vandana Kannanf769cd22014-08-05 07:51:22 -07005763 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5764 &crtc->config.dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005765}
5766
Ville Syrjäläd288f652014-10-28 13:20:22 +02005767static void vlv_update_pll(struct intel_crtc *crtc,
5768 struct intel_crtc_config *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005769{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005770 u32 dpll, dpll_md;
5771
5772 /*
5773 * Enable DPIO clock input. We should never disable the reference
5774 * clock for pipe B, since VGA hotplug / manual detection depends
5775 * on it.
5776 */
5777 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5778 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5779 /* We should never disable this, set it here for state tracking */
5780 if (crtc->pipe == PIPE_B)
5781 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5782 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02005783 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005784
Ville Syrjäläd288f652014-10-28 13:20:22 +02005785 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005786 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02005787 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005788}
5789
Ville Syrjäläd288f652014-10-28 13:20:22 +02005790static void vlv_prepare_pll(struct intel_crtc *crtc,
5791 const struct intel_crtc_config *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005792{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005793 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005794 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005795 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005796 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005797 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005798 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005799
Daniel Vetter09153002012-12-12 14:06:44 +01005800 mutex_lock(&dev_priv->dpio_lock);
5801
Ville Syrjäläd288f652014-10-28 13:20:22 +02005802 bestn = pipe_config->dpll.n;
5803 bestm1 = pipe_config->dpll.m1;
5804 bestm2 = pipe_config->dpll.m2;
5805 bestp1 = pipe_config->dpll.p1;
5806 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005807
Jesse Barnes89b667f2013-04-18 14:51:36 -07005808 /* See eDP HDMI DPIO driver vbios notes doc */
5809
5810 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005811 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005812 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005813
5814 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005815 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005816
5817 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005818 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005819 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005820 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005821
5822 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005823 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005824
5825 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005826 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5827 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5828 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005829 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005830
5831 /*
5832 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5833 * but we don't support that).
5834 * Note: don't use the DAC post divider as it seems unstable.
5835 */
5836 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005837 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005838
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005839 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005840 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005841
Jesse Barnes89b667f2013-04-18 14:51:36 -07005842 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02005843 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005844 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
5845 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005846 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03005847 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005848 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005849 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005850 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005851
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005852 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP) ||
5853 intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07005854 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005855 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005856 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005857 0x0df40000);
5858 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005859 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005860 0x0df70000);
5861 } else { /* HDMI or VGA */
5862 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005863 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005864 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005865 0x0df70000);
5866 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005867 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005868 0x0df40000);
5869 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005870
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005871 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005872 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005873 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
5874 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07005875 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005876 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005877
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005878 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01005879 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005880}
5881
Ville Syrjäläd288f652014-10-28 13:20:22 +02005882static void chv_update_pll(struct intel_crtc *crtc,
5883 struct intel_crtc_config *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005884{
Ville Syrjäläd288f652014-10-28 13:20:22 +02005885 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005886 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5887 DPLL_VCO_ENABLE;
5888 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02005889 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005890
Ville Syrjäläd288f652014-10-28 13:20:22 +02005891 pipe_config->dpll_hw_state.dpll_md =
5892 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005893}
5894
Ville Syrjäläd288f652014-10-28 13:20:22 +02005895static void chv_prepare_pll(struct intel_crtc *crtc,
5896 const struct intel_crtc_config *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005897{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005898 struct drm_device *dev = crtc->base.dev;
5899 struct drm_i915_private *dev_priv = dev->dev_private;
5900 int pipe = crtc->pipe;
5901 int dpll_reg = DPLL(crtc->pipe);
5902 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03005903 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005904 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5905 int refclk;
5906
Ville Syrjäläd288f652014-10-28 13:20:22 +02005907 bestn = pipe_config->dpll.n;
5908 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
5909 bestm1 = pipe_config->dpll.m1;
5910 bestm2 = pipe_config->dpll.m2 >> 22;
5911 bestp1 = pipe_config->dpll.p1;
5912 bestp2 = pipe_config->dpll.p2;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005913
5914 /*
5915 * Enable Refclk and SSC
5916 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005917 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02005918 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005919
5920 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005921
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005922 /* p1 and p2 divider */
5923 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5924 5 << DPIO_CHV_S1_DIV_SHIFT |
5925 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5926 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5927 1 << DPIO_CHV_K_DIV_SHIFT);
5928
5929 /* Feedback post-divider - m2 */
5930 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5931
5932 /* Feedback refclk divider - n and m1 */
5933 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5934 DPIO_CHV_M1_DIV_BY_2 |
5935 1 << DPIO_CHV_N_DIV_SHIFT);
5936
5937 /* M2 fraction division */
5938 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5939
5940 /* M2 fraction division enable */
5941 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5942 DPIO_CHV_FRAC_DIV_EN |
5943 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5944
5945 /* Loop filter */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005946 refclk = i9xx_get_refclk(crtc, 0);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005947 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5948 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5949 if (refclk == 100000)
5950 intcoeff = 11;
5951 else if (refclk == 38400)
5952 intcoeff = 10;
5953 else
5954 intcoeff = 9;
5955 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5956 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5957
5958 /* AFC Recal */
5959 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5960 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5961 DPIO_AFC_RECAL);
5962
5963 mutex_unlock(&dev_priv->dpio_lock);
5964}
5965
Ville Syrjäläd288f652014-10-28 13:20:22 +02005966/**
5967 * vlv_force_pll_on - forcibly enable just the PLL
5968 * @dev_priv: i915 private structure
5969 * @pipe: pipe PLL to enable
5970 * @dpll: PLL configuration
5971 *
5972 * Enable the PLL for @pipe using the supplied @dpll config. To be used
5973 * in cases where we need the PLL enabled even when @pipe is not going to
5974 * be enabled.
5975 */
5976void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
5977 const struct dpll *dpll)
5978{
5979 struct intel_crtc *crtc =
5980 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5981 struct intel_crtc_config pipe_config = {
5982 .pixel_multiplier = 1,
5983 .dpll = *dpll,
5984 };
5985
5986 if (IS_CHERRYVIEW(dev)) {
5987 chv_update_pll(crtc, &pipe_config);
5988 chv_prepare_pll(crtc, &pipe_config);
5989 chv_enable_pll(crtc, &pipe_config);
5990 } else {
5991 vlv_update_pll(crtc, &pipe_config);
5992 vlv_prepare_pll(crtc, &pipe_config);
5993 vlv_enable_pll(crtc, &pipe_config);
5994 }
5995}
5996
5997/**
5998 * vlv_force_pll_off - forcibly disable just the PLL
5999 * @dev_priv: i915 private structure
6000 * @pipe: pipe PLL to disable
6001 *
6002 * Disable the PLL for @pipe. To be used in cases where we need
6003 * the PLL enabled even when @pipe is not going to be enabled.
6004 */
6005void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
6006{
6007 if (IS_CHERRYVIEW(dev))
6008 chv_disable_pll(to_i915(dev), pipe);
6009 else
6010 vlv_disable_pll(to_i915(dev), pipe);
6011}
6012
Daniel Vetterf47709a2013-03-28 10:42:02 +01006013static void i9xx_update_pll(struct intel_crtc *crtc,
6014 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006015 int num_connectors)
6016{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006017 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006018 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006019 u32 dpll;
6020 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006021 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006022
Daniel Vetterf47709a2013-03-28 10:42:02 +01006023 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306024
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006025 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
6026 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006027
6028 dpll = DPLL_VGA_MODE_DIS;
6029
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006030 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006031 dpll |= DPLLB_MODE_LVDS;
6032 else
6033 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006034
Daniel Vetteref1b4602013-06-01 17:17:04 +02006035 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02006036 dpll |= (crtc->config.pixel_multiplier - 1)
6037 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006038 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006039
6040 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006041 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006042
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006043 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006044 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006045
6046 /* compute bitmask from p1 value */
6047 if (IS_PINEVIEW(dev))
6048 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6049 else {
6050 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6051 if (IS_G4X(dev) && reduced_clock)
6052 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6053 }
6054 switch (clock->p2) {
6055 case 5:
6056 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6057 break;
6058 case 7:
6059 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6060 break;
6061 case 10:
6062 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6063 break;
6064 case 14:
6065 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6066 break;
6067 }
6068 if (INTEL_INFO(dev)->gen >= 4)
6069 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6070
Daniel Vetter09ede542013-04-30 14:01:45 +02006071 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006072 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006073 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006074 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6075 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6076 else
6077 dpll |= PLL_REF_INPUT_DREFCLK;
6078
6079 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006080 crtc->config.dpll_hw_state.dpll = dpll;
6081
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006082 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02006083 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
6084 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006085 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006086 }
6087}
6088
Daniel Vetterf47709a2013-03-28 10:42:02 +01006089static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006090 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006091 int num_connectors)
6092{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006093 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006094 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006095 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006096 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006097
Daniel Vetterf47709a2013-03-28 10:42:02 +01006098 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306099
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006100 dpll = DPLL_VGA_MODE_DIS;
6101
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006102 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006103 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6104 } else {
6105 if (clock->p1 == 2)
6106 dpll |= PLL_P1_DIVIDE_BY_TWO;
6107 else
6108 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6109 if (clock->p2 == 4)
6110 dpll |= PLL_P2_DIVIDE_BY_4;
6111 }
6112
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006113 if (!IS_I830(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006114 dpll |= DPLL_DVO_2X_MODE;
6115
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006116 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006117 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6118 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6119 else
6120 dpll |= PLL_REF_INPUT_DREFCLK;
6121
6122 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006123 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006124}
6125
Daniel Vetter8a654f32013-06-01 17:16:22 +02006126static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006127{
6128 struct drm_device *dev = intel_crtc->base.dev;
6129 struct drm_i915_private *dev_priv = dev->dev_private;
6130 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006131 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02006132 struct drm_display_mode *adjusted_mode =
6133 &intel_crtc->config.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006134 uint32_t crtc_vtotal, crtc_vblank_end;
6135 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006136
6137 /* We need to be careful not to changed the adjusted mode, for otherwise
6138 * the hw state checker will get angry at the mismatch. */
6139 crtc_vtotal = adjusted_mode->crtc_vtotal;
6140 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006141
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006142 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006143 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006144 crtc_vtotal -= 1;
6145 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006146
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006147 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006148 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6149 else
6150 vsyncshift = adjusted_mode->crtc_hsync_start -
6151 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006152 if (vsyncshift < 0)
6153 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006154 }
6155
6156 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006157 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006158
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006159 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006160 (adjusted_mode->crtc_hdisplay - 1) |
6161 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006162 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006163 (adjusted_mode->crtc_hblank_start - 1) |
6164 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006165 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006166 (adjusted_mode->crtc_hsync_start - 1) |
6167 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6168
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006169 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006170 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006171 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006172 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006173 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006174 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006175 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006176 (adjusted_mode->crtc_vsync_start - 1) |
6177 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6178
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006179 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6180 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6181 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6182 * bits. */
6183 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6184 (pipe == PIPE_B || pipe == PIPE_C))
6185 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6186
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006187 /* pipesrc controls the size that is scaled from, which should
6188 * always be the user's requested size.
6189 */
6190 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006191 ((intel_crtc->config.pipe_src_w - 1) << 16) |
6192 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006193}
6194
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006195static void intel_get_pipe_timings(struct intel_crtc *crtc,
6196 struct intel_crtc_config *pipe_config)
6197{
6198 struct drm_device *dev = crtc->base.dev;
6199 struct drm_i915_private *dev_priv = dev->dev_private;
6200 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6201 uint32_t tmp;
6202
6203 tmp = I915_READ(HTOTAL(cpu_transcoder));
6204 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6205 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6206 tmp = I915_READ(HBLANK(cpu_transcoder));
6207 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6208 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6209 tmp = I915_READ(HSYNC(cpu_transcoder));
6210 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6211 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6212
6213 tmp = I915_READ(VTOTAL(cpu_transcoder));
6214 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6215 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6216 tmp = I915_READ(VBLANK(cpu_transcoder));
6217 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6218 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6219 tmp = I915_READ(VSYNC(cpu_transcoder));
6220 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6221 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6222
6223 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6224 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6225 pipe_config->adjusted_mode.crtc_vtotal += 1;
6226 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6227 }
6228
6229 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006230 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6231 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6232
6233 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6234 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006235}
6236
Daniel Vetterf6a83282014-02-11 15:28:57 -08006237void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6238 struct intel_crtc_config *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006239{
Daniel Vetterf6a83282014-02-11 15:28:57 -08006240 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6241 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6242 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6243 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006244
Daniel Vetterf6a83282014-02-11 15:28:57 -08006245 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6246 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6247 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6248 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006249
Daniel Vetterf6a83282014-02-11 15:28:57 -08006250 mode->flags = pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006251
Daniel Vetterf6a83282014-02-11 15:28:57 -08006252 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6253 mode->flags |= pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006254}
6255
Daniel Vetter84b046f2013-02-19 18:48:54 +01006256static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6257{
6258 struct drm_device *dev = intel_crtc->base.dev;
6259 struct drm_i915_private *dev_priv = dev->dev_private;
6260 uint32_t pipeconf;
6261
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006262 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006263
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006264 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6265 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6266 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006267
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006268 if (intel_crtc->config.double_wide)
6269 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006270
Daniel Vetterff9ce462013-04-24 14:57:17 +02006271 /* only g4x and later have fancy bpc/dither controls */
6272 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006273 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6274 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6275 pipeconf |= PIPECONF_DITHER_EN |
6276 PIPECONF_DITHER_TYPE_SP;
6277
6278 switch (intel_crtc->config.pipe_bpp) {
6279 case 18:
6280 pipeconf |= PIPECONF_6BPC;
6281 break;
6282 case 24:
6283 pipeconf |= PIPECONF_8BPC;
6284 break;
6285 case 30:
6286 pipeconf |= PIPECONF_10BPC;
6287 break;
6288 default:
6289 /* Case prevented by intel_choose_pipe_bpp_dither. */
6290 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006291 }
6292 }
6293
6294 if (HAS_PIPE_CXSR(dev)) {
6295 if (intel_crtc->lowfreq_avail) {
6296 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6297 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6298 } else {
6299 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006300 }
6301 }
6302
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006303 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6304 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006305 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006306 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6307 else
6308 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6309 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006310 pipeconf |= PIPECONF_PROGRESSIVE;
6311
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006312 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6313 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006314
Daniel Vetter84b046f2013-02-19 18:48:54 +01006315 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6316 POSTING_READ(PIPECONF(intel_crtc->pipe));
6317}
6318
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006319static int i9xx_crtc_mode_set(struct intel_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006320 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006321 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006322{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006323 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006324 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07006325 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006326 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006327 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006328 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006329 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006330 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006331
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006332 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01006333 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006334 case INTEL_OUTPUT_LVDS:
6335 is_lvds = true;
6336 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006337 case INTEL_OUTPUT_DSI:
6338 is_dsi = true;
6339 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006340 default:
6341 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006342 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006343
Eric Anholtc751ce42010-03-25 11:48:48 -07006344 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006345 }
6346
Jani Nikulaf2335332013-09-13 11:03:09 +03006347 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006348 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006349
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006350 if (!crtc->config.clock_set) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006351 refclk = i9xx_get_refclk(crtc, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03006352
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006353 /*
6354 * Returns a set of divisors for the desired target clock with
6355 * the given refclk, or FALSE. The returned values represent
6356 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6357 * 2) / p1 / p2.
6358 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006359 limit = intel_limit(crtc, refclk);
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006360 ok = dev_priv->display.find_dpll(limit, crtc,
6361 crtc->config.port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006362 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006363 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006364 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6365 return -EINVAL;
6366 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006367
Jani Nikulaf2335332013-09-13 11:03:09 +03006368 if (is_lvds && dev_priv->lvds_downclock_avail) {
6369 /*
6370 * Ensure we match the reduced clock's P to the target
6371 * clock. If the clocks don't match, we can't switch
6372 * the display clock by using the FP0/FP1. In such case
6373 * we will disable the LVDS downclock feature.
6374 */
6375 has_reduced_clock =
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006376 dev_priv->display.find_dpll(limit, crtc,
Jani Nikulaf2335332013-09-13 11:03:09 +03006377 dev_priv->lvds_downclock,
6378 refclk, &clock,
6379 &reduced_clock);
6380 }
6381 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006382 crtc->config.dpll.n = clock.n;
6383 crtc->config.dpll.m1 = clock.m1;
6384 crtc->config.dpll.m2 = clock.m2;
6385 crtc->config.dpll.p1 = clock.p1;
6386 crtc->config.dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006387 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006388
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006389 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006390 i8xx_update_pll(crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306391 has_reduced_clock ? &reduced_clock : NULL,
6392 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006393 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläd288f652014-10-28 13:20:22 +02006394 chv_update_pll(crtc, &crtc->config);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006395 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläd288f652014-10-28 13:20:22 +02006396 vlv_update_pll(crtc, &crtc->config);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006397 } else {
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006398 i9xx_update_pll(crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006399 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006400 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006401 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006402
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006403 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006404}
6405
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006406static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6407 struct intel_crtc_config *pipe_config)
6408{
6409 struct drm_device *dev = crtc->base.dev;
6410 struct drm_i915_private *dev_priv = dev->dev_private;
6411 uint32_t tmp;
6412
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006413 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6414 return;
6415
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006416 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006417 if (!(tmp & PFIT_ENABLE))
6418 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006419
Daniel Vetter06922822013-07-11 13:35:40 +02006420 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006421 if (INTEL_INFO(dev)->gen < 4) {
6422 if (crtc->pipe != PIPE_B)
6423 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006424 } else {
6425 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6426 return;
6427 }
6428
Daniel Vetter06922822013-07-11 13:35:40 +02006429 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006430 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6431 if (INTEL_INFO(dev)->gen < 5)
6432 pipe_config->gmch_pfit.lvds_border_bits =
6433 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6434}
6435
Jesse Barnesacbec812013-09-20 11:29:32 -07006436static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6437 struct intel_crtc_config *pipe_config)
6438{
6439 struct drm_device *dev = crtc->base.dev;
6440 struct drm_i915_private *dev_priv = dev->dev_private;
6441 int pipe = pipe_config->cpu_transcoder;
6442 intel_clock_t clock;
6443 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006444 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006445
Shobhit Kumarf573de52014-07-30 20:32:37 +05306446 /* In case of MIPI DPLL will not even be used */
6447 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6448 return;
6449
Jesse Barnesacbec812013-09-20 11:29:32 -07006450 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006451 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006452 mutex_unlock(&dev_priv->dpio_lock);
6453
6454 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6455 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6456 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6457 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6458 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6459
Ville Syrjäläf6466282013-10-14 14:50:31 +03006460 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006461
Ville Syrjäläf6466282013-10-14 14:50:31 +03006462 /* clock.dot is the fast clock */
6463 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006464}
6465
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006466static void i9xx_get_plane_config(struct intel_crtc *crtc,
6467 struct intel_plane_config *plane_config)
6468{
6469 struct drm_device *dev = crtc->base.dev;
6470 struct drm_i915_private *dev_priv = dev->dev_private;
6471 u32 val, base, offset;
6472 int pipe = crtc->pipe, plane = crtc->plane;
6473 int fourcc, pixel_format;
6474 int aligned_height;
6475
Dave Airlie66e514c2014-04-03 07:51:54 +10006476 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6477 if (!crtc->base.primary->fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006478 DRM_DEBUG_KMS("failed to alloc fb\n");
6479 return;
6480 }
6481
6482 val = I915_READ(DSPCNTR(plane));
6483
6484 if (INTEL_INFO(dev)->gen >= 4)
6485 if (val & DISPPLANE_TILED)
6486 plane_config->tiled = true;
6487
6488 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6489 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10006490 crtc->base.primary->fb->pixel_format = fourcc;
6491 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006492 drm_format_plane_cpp(fourcc, 0) * 8;
6493
6494 if (INTEL_INFO(dev)->gen >= 4) {
6495 if (plane_config->tiled)
6496 offset = I915_READ(DSPTILEOFF(plane));
6497 else
6498 offset = I915_READ(DSPLINOFF(plane));
6499 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6500 } else {
6501 base = I915_READ(DSPADDR(plane));
6502 }
6503 plane_config->base = base;
6504
6505 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006506 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6507 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006508
6509 val = I915_READ(DSPSTRIDE(pipe));
Rafael Barbalho026b96e2014-07-28 19:56:27 +01006510 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006511
Dave Airlie66e514c2014-04-03 07:51:54 +10006512 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006513 plane_config->tiled);
6514
Fabian Frederick1267a262014-07-01 20:39:41 +02006515 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6516 aligned_height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006517
6518 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
Dave Airlie66e514c2014-04-03 07:51:54 +10006519 pipe, plane, crtc->base.primary->fb->width,
6520 crtc->base.primary->fb->height,
6521 crtc->base.primary->fb->bits_per_pixel, base,
6522 crtc->base.primary->fb->pitches[0],
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006523 plane_config->size);
6524
6525}
6526
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006527static void chv_crtc_clock_get(struct intel_crtc *crtc,
6528 struct intel_crtc_config *pipe_config)
6529{
6530 struct drm_device *dev = crtc->base.dev;
6531 struct drm_i915_private *dev_priv = dev->dev_private;
6532 int pipe = pipe_config->cpu_transcoder;
6533 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6534 intel_clock_t clock;
6535 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6536 int refclk = 100000;
6537
6538 mutex_lock(&dev_priv->dpio_lock);
6539 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6540 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6541 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6542 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6543 mutex_unlock(&dev_priv->dpio_lock);
6544
6545 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6546 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6547 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6548 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6549 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6550
6551 chv_clock(refclk, &clock);
6552
6553 /* clock.dot is the fast clock */
6554 pipe_config->port_clock = clock.dot / 5;
6555}
6556
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006557static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6558 struct intel_crtc_config *pipe_config)
6559{
6560 struct drm_device *dev = crtc->base.dev;
6561 struct drm_i915_private *dev_priv = dev->dev_private;
6562 uint32_t tmp;
6563
Daniel Vetterf458ebb2014-09-30 10:56:39 +02006564 if (!intel_display_power_is_enabled(dev_priv,
6565 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02006566 return false;
6567
Daniel Vettere143a212013-07-04 12:01:15 +02006568 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006569 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006570
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006571 tmp = I915_READ(PIPECONF(crtc->pipe));
6572 if (!(tmp & PIPECONF_ENABLE))
6573 return false;
6574
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006575 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6576 switch (tmp & PIPECONF_BPC_MASK) {
6577 case PIPECONF_6BPC:
6578 pipe_config->pipe_bpp = 18;
6579 break;
6580 case PIPECONF_8BPC:
6581 pipe_config->pipe_bpp = 24;
6582 break;
6583 case PIPECONF_10BPC:
6584 pipe_config->pipe_bpp = 30;
6585 break;
6586 default:
6587 break;
6588 }
6589 }
6590
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006591 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6592 pipe_config->limited_color_range = true;
6593
Ville Syrjälä282740f2013-09-04 18:30:03 +03006594 if (INTEL_INFO(dev)->gen < 4)
6595 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6596
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006597 intel_get_pipe_timings(crtc, pipe_config);
6598
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006599 i9xx_get_pfit_config(crtc, pipe_config);
6600
Daniel Vetter6c49f242013-06-06 12:45:25 +02006601 if (INTEL_INFO(dev)->gen >= 4) {
6602 tmp = I915_READ(DPLL_MD(crtc->pipe));
6603 pipe_config->pixel_multiplier =
6604 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6605 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006606 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006607 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6608 tmp = I915_READ(DPLL(crtc->pipe));
6609 pipe_config->pixel_multiplier =
6610 ((tmp & SDVO_MULTIPLIER_MASK)
6611 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6612 } else {
6613 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6614 * port and will be fixed up in the encoder->get_config
6615 * function. */
6616 pipe_config->pixel_multiplier = 1;
6617 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006618 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6619 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006620 /*
6621 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6622 * on 830. Filter it out here so that we don't
6623 * report errors due to that.
6624 */
6625 if (IS_I830(dev))
6626 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6627
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006628 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6629 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006630 } else {
6631 /* Mask out read-only status bits. */
6632 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6633 DPLL_PORTC_READY_MASK |
6634 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006635 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006636
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006637 if (IS_CHERRYVIEW(dev))
6638 chv_crtc_clock_get(crtc, pipe_config);
6639 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006640 vlv_crtc_clock_get(crtc, pipe_config);
6641 else
6642 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006643
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006644 return true;
6645}
6646
Paulo Zanonidde86e22012-12-01 12:04:25 -02006647static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006648{
6649 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006650 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006651 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006652 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006653 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006654 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006655 bool has_ck505 = false;
6656 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006657
6658 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01006659 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07006660 switch (encoder->type) {
6661 case INTEL_OUTPUT_LVDS:
6662 has_panel = true;
6663 has_lvds = true;
6664 break;
6665 case INTEL_OUTPUT_EDP:
6666 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006667 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006668 has_cpu_edp = true;
6669 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006670 default:
6671 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006672 }
6673 }
6674
Keith Packard99eb6a02011-09-26 14:29:12 -07006675 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006676 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006677 can_ssc = has_ck505;
6678 } else {
6679 has_ck505 = false;
6680 can_ssc = true;
6681 }
6682
Imre Deak2de69052013-05-08 13:14:04 +03006683 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6684 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006685
6686 /* Ironlake: try to setup display ref clock before DPLL
6687 * enabling. This is only under driver's control after
6688 * PCH B stepping, previous chipset stepping should be
6689 * ignoring this setting.
6690 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006691 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006692
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006693 /* As we must carefully and slowly disable/enable each source in turn,
6694 * compute the final state we want first and check if we need to
6695 * make any changes at all.
6696 */
6697 final = val;
6698 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006699 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006700 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006701 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006702 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6703
6704 final &= ~DREF_SSC_SOURCE_MASK;
6705 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6706 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006707
Keith Packard199e5d72011-09-22 12:01:57 -07006708 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006709 final |= DREF_SSC_SOURCE_ENABLE;
6710
6711 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6712 final |= DREF_SSC1_ENABLE;
6713
6714 if (has_cpu_edp) {
6715 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6716 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6717 else
6718 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6719 } else
6720 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6721 } else {
6722 final |= DREF_SSC_SOURCE_DISABLE;
6723 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6724 }
6725
6726 if (final == val)
6727 return;
6728
6729 /* Always enable nonspread source */
6730 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6731
6732 if (has_ck505)
6733 val |= DREF_NONSPREAD_CK505_ENABLE;
6734 else
6735 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6736
6737 if (has_panel) {
6738 val &= ~DREF_SSC_SOURCE_MASK;
6739 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006740
Keith Packard199e5d72011-09-22 12:01:57 -07006741 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006742 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006743 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006744 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006745 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006746 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006747
6748 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006749 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006750 POSTING_READ(PCH_DREF_CONTROL);
6751 udelay(200);
6752
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006753 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006754
6755 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006756 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006757 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006758 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006759 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02006760 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006761 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006762 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006763 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006764
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006765 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006766 POSTING_READ(PCH_DREF_CONTROL);
6767 udelay(200);
6768 } else {
6769 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6770
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006771 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006772
6773 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006774 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006775
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006776 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006777 POSTING_READ(PCH_DREF_CONTROL);
6778 udelay(200);
6779
6780 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006781 val &= ~DREF_SSC_SOURCE_MASK;
6782 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006783
6784 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006785 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006786
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006787 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006788 POSTING_READ(PCH_DREF_CONTROL);
6789 udelay(200);
6790 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006791
6792 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006793}
6794
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006795static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006796{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006797 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006798
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006799 tmp = I915_READ(SOUTH_CHICKEN2);
6800 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6801 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006802
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006803 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6804 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6805 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006806
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006807 tmp = I915_READ(SOUTH_CHICKEN2);
6808 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6809 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006810
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006811 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6812 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6813 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006814}
6815
6816/* WaMPhyProgramming:hsw */
6817static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6818{
6819 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006820
6821 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6822 tmp &= ~(0xFF << 24);
6823 tmp |= (0x12 << 24);
6824 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6825
Paulo Zanonidde86e22012-12-01 12:04:25 -02006826 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6827 tmp |= (1 << 11);
6828 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6829
6830 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6831 tmp |= (1 << 11);
6832 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6833
Paulo Zanonidde86e22012-12-01 12:04:25 -02006834 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6835 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6836 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6837
6838 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6839 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6840 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6841
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006842 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6843 tmp &= ~(7 << 13);
6844 tmp |= (5 << 13);
6845 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006846
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006847 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6848 tmp &= ~(7 << 13);
6849 tmp |= (5 << 13);
6850 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006851
6852 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6853 tmp &= ~0xFF;
6854 tmp |= 0x1C;
6855 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6856
6857 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6858 tmp &= ~0xFF;
6859 tmp |= 0x1C;
6860 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6861
6862 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6863 tmp &= ~(0xFF << 16);
6864 tmp |= (0x1C << 16);
6865 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6866
6867 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6868 tmp &= ~(0xFF << 16);
6869 tmp |= (0x1C << 16);
6870 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6871
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006872 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6873 tmp |= (1 << 27);
6874 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006875
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006876 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6877 tmp |= (1 << 27);
6878 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006879
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006880 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6881 tmp &= ~(0xF << 28);
6882 tmp |= (4 << 28);
6883 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006884
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006885 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6886 tmp &= ~(0xF << 28);
6887 tmp |= (4 << 28);
6888 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006889}
6890
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006891/* Implements 3 different sequences from BSpec chapter "Display iCLK
6892 * Programming" based on the parameters passed:
6893 * - Sequence to enable CLKOUT_DP
6894 * - Sequence to enable CLKOUT_DP without spread
6895 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6896 */
6897static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6898 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006899{
6900 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006901 uint32_t reg, tmp;
6902
6903 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6904 with_spread = true;
6905 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6906 with_fdi, "LP PCH doesn't have FDI\n"))
6907 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006908
6909 mutex_lock(&dev_priv->dpio_lock);
6910
6911 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6912 tmp &= ~SBI_SSCCTL_DISABLE;
6913 tmp |= SBI_SSCCTL_PATHALT;
6914 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6915
6916 udelay(24);
6917
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006918 if (with_spread) {
6919 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6920 tmp &= ~SBI_SSCCTL_PATHALT;
6921 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006922
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006923 if (with_fdi) {
6924 lpt_reset_fdi_mphy(dev_priv);
6925 lpt_program_fdi_mphy(dev_priv);
6926 }
6927 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02006928
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006929 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6930 SBI_GEN0 : SBI_DBUFF0;
6931 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6932 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6933 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01006934
6935 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006936}
6937
Paulo Zanoni47701c32013-07-23 11:19:25 -03006938/* Sequence to disable CLKOUT_DP */
6939static void lpt_disable_clkout_dp(struct drm_device *dev)
6940{
6941 struct drm_i915_private *dev_priv = dev->dev_private;
6942 uint32_t reg, tmp;
6943
6944 mutex_lock(&dev_priv->dpio_lock);
6945
6946 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6947 SBI_GEN0 : SBI_DBUFF0;
6948 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6949 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6950 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6951
6952 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6953 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6954 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6955 tmp |= SBI_SSCCTL_PATHALT;
6956 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6957 udelay(32);
6958 }
6959 tmp |= SBI_SSCCTL_DISABLE;
6960 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6961 }
6962
6963 mutex_unlock(&dev_priv->dpio_lock);
6964}
6965
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006966static void lpt_init_pch_refclk(struct drm_device *dev)
6967{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006968 struct intel_encoder *encoder;
6969 bool has_vga = false;
6970
Damien Lespiaub2784e12014-08-05 11:29:37 +01006971 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006972 switch (encoder->type) {
6973 case INTEL_OUTPUT_ANALOG:
6974 has_vga = true;
6975 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006976 default:
6977 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006978 }
6979 }
6980
Paulo Zanoni47701c32013-07-23 11:19:25 -03006981 if (has_vga)
6982 lpt_enable_clkout_dp(dev, true, true);
6983 else
6984 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006985}
6986
Paulo Zanonidde86e22012-12-01 12:04:25 -02006987/*
6988 * Initialize reference clocks when the driver loads
6989 */
6990void intel_init_pch_refclk(struct drm_device *dev)
6991{
6992 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6993 ironlake_init_pch_refclk(dev);
6994 else if (HAS_PCH_LPT(dev))
6995 lpt_init_pch_refclk(dev);
6996}
6997
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006998static int ironlake_get_refclk(struct drm_crtc *crtc)
6999{
7000 struct drm_device *dev = crtc->dev;
7001 struct drm_i915_private *dev_priv = dev->dev_private;
7002 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007003 int num_connectors = 0;
7004 bool is_lvds = false;
7005
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02007006 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007007 switch (encoder->type) {
7008 case INTEL_OUTPUT_LVDS:
7009 is_lvds = true;
7010 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007011 default:
7012 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007013 }
7014 num_connectors++;
7015 }
7016
7017 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007018 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007019 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007020 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007021 }
7022
7023 return 120000;
7024}
7025
Daniel Vetter6ff93602013-04-19 11:24:36 +02007026static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007027{
7028 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7029 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7030 int pipe = intel_crtc->pipe;
7031 uint32_t val;
7032
Daniel Vetter78114072013-06-13 00:54:57 +02007033 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007034
Daniel Vetter965e0c42013-03-27 00:44:57 +01007035 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007036 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007037 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007038 break;
7039 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007040 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007041 break;
7042 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007043 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007044 break;
7045 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007046 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007047 break;
7048 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007049 /* Case prevented by intel_choose_pipe_bpp_dither. */
7050 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007051 }
7052
Daniel Vetterd8b32242013-04-25 17:54:44 +02007053 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007054 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7055
Daniel Vetter6ff93602013-04-19 11:24:36 +02007056 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007057 val |= PIPECONF_INTERLACED_ILK;
7058 else
7059 val |= PIPECONF_PROGRESSIVE;
7060
Daniel Vetter50f3b012013-03-27 00:44:56 +01007061 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007062 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007063
Paulo Zanonic8203562012-09-12 10:06:29 -03007064 I915_WRITE(PIPECONF(pipe), val);
7065 POSTING_READ(PIPECONF(pipe));
7066}
7067
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007068/*
7069 * Set up the pipe CSC unit.
7070 *
7071 * Currently only full range RGB to limited range RGB conversion
7072 * is supported, but eventually this should handle various
7073 * RGB<->YCbCr scenarios as well.
7074 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01007075static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007076{
7077 struct drm_device *dev = crtc->dev;
7078 struct drm_i915_private *dev_priv = dev->dev_private;
7079 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7080 int pipe = intel_crtc->pipe;
7081 uint16_t coeff = 0x7800; /* 1.0 */
7082
7083 /*
7084 * TODO: Check what kind of values actually come out of the pipe
7085 * with these coeff/postoff values and adjust to get the best
7086 * accuracy. Perhaps we even need to take the bpc value into
7087 * consideration.
7088 */
7089
Daniel Vetter50f3b012013-03-27 00:44:56 +01007090 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007091 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7092
7093 /*
7094 * GY/GU and RY/RU should be the other way around according
7095 * to BSpec, but reality doesn't agree. Just set them up in
7096 * a way that results in the correct picture.
7097 */
7098 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7099 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7100
7101 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7102 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7103
7104 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7105 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7106
7107 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7108 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7109 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7110
7111 if (INTEL_INFO(dev)->gen > 6) {
7112 uint16_t postoff = 0;
7113
Daniel Vetter50f3b012013-03-27 00:44:56 +01007114 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02007115 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007116
7117 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7118 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7119 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7120
7121 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7122 } else {
7123 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7124
Daniel Vetter50f3b012013-03-27 00:44:56 +01007125 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007126 mode |= CSC_BLACK_SCREEN_OFFSET;
7127
7128 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7129 }
7130}
7131
Daniel Vetter6ff93602013-04-19 11:24:36 +02007132static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007133{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007134 struct drm_device *dev = crtc->dev;
7135 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007137 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02007138 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007139 uint32_t val;
7140
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007141 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007142
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007143 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007144 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7145
Daniel Vetter6ff93602013-04-19 11:24:36 +02007146 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007147 val |= PIPECONF_INTERLACED_ILK;
7148 else
7149 val |= PIPECONF_PROGRESSIVE;
7150
Paulo Zanoni702e7a52012-10-23 18:29:59 -02007151 I915_WRITE(PIPECONF(cpu_transcoder), val);
7152 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007153
7154 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7155 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007156
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05307157 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007158 val = 0;
7159
7160 switch (intel_crtc->config.pipe_bpp) {
7161 case 18:
7162 val |= PIPEMISC_DITHER_6_BPC;
7163 break;
7164 case 24:
7165 val |= PIPEMISC_DITHER_8_BPC;
7166 break;
7167 case 30:
7168 val |= PIPEMISC_DITHER_10_BPC;
7169 break;
7170 case 36:
7171 val |= PIPEMISC_DITHER_12_BPC;
7172 break;
7173 default:
7174 /* Case prevented by pipe_config_set_bpp. */
7175 BUG();
7176 }
7177
7178 if (intel_crtc->config.dither)
7179 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7180
7181 I915_WRITE(PIPEMISC(pipe), val);
7182 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007183}
7184
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007185static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007186 intel_clock_t *clock,
7187 bool *has_reduced_clock,
7188 intel_clock_t *reduced_clock)
7189{
7190 struct drm_device *dev = crtc->dev;
7191 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007192 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007193 int refclk;
7194 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02007195 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007196
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007197 is_lvds = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007198
7199 refclk = ironlake_get_refclk(crtc);
7200
7201 /*
7202 * Returns a set of divisors for the desired target clock with the given
7203 * refclk, or FALSE. The returned values represent the clock equation:
7204 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7205 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007206 limit = intel_limit(intel_crtc, refclk);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007207 ret = dev_priv->display.find_dpll(limit, intel_crtc,
7208 intel_crtc->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007209 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007210 if (!ret)
7211 return false;
7212
7213 if (is_lvds && dev_priv->lvds_downclock_avail) {
7214 /*
7215 * Ensure we match the reduced clock's P to the target clock.
7216 * If the clocks don't match, we can't switch the display clock
7217 * by using the FP0/FP1. In such case we will disable the LVDS
7218 * downclock feature.
7219 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007220 *has_reduced_clock =
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007221 dev_priv->display.find_dpll(limit, intel_crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007222 dev_priv->lvds_downclock,
7223 refclk, clock,
7224 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007225 }
7226
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007227 return true;
7228}
7229
Paulo Zanonid4b19312012-11-29 11:29:32 -02007230int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7231{
7232 /*
7233 * Account for spread spectrum to avoid
7234 * oversubscribing the link. Max center spread
7235 * is 2.5%; use 5% for safety's sake.
7236 */
7237 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007238 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007239}
7240
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007241static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007242{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007243 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007244}
7245
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007246static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007247 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007248 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007249{
7250 struct drm_crtc *crtc = &intel_crtc->base;
7251 struct drm_device *dev = crtc->dev;
7252 struct drm_i915_private *dev_priv = dev->dev_private;
7253 struct intel_encoder *intel_encoder;
7254 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007255 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02007256 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007257
7258 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7259 switch (intel_encoder->type) {
7260 case INTEL_OUTPUT_LVDS:
7261 is_lvds = true;
7262 break;
7263 case INTEL_OUTPUT_SDVO:
7264 case INTEL_OUTPUT_HDMI:
7265 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007266 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007267 default:
7268 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007269 }
7270
7271 num_connectors++;
7272 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007273
Chris Wilsonc1858122010-12-03 21:35:48 +00007274 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007275 factor = 21;
7276 if (is_lvds) {
7277 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007278 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007279 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007280 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02007281 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007282 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007283
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007284 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007285 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007286
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007287 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7288 *fp2 |= FP_CB_TUNE;
7289
Chris Wilson5eddb702010-09-11 13:48:45 +01007290 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007291
Eric Anholta07d6782011-03-30 13:01:08 -07007292 if (is_lvds)
7293 dpll |= DPLLB_MODE_LVDS;
7294 else
7295 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007296
Daniel Vetteref1b4602013-06-01 17:17:04 +02007297 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7298 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007299
7300 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007301 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02007302 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007303 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007304
Eric Anholta07d6782011-03-30 13:01:08 -07007305 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007306 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007307 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007308 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007309
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007310 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007311 case 5:
7312 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7313 break;
7314 case 7:
7315 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7316 break;
7317 case 10:
7318 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7319 break;
7320 case 14:
7321 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7322 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007323 }
7324
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007325 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007326 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007327 else
7328 dpll |= PLL_REF_INPUT_DREFCLK;
7329
Daniel Vetter959e16d2013-06-05 13:34:21 +02007330 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007331}
7332
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007333static int ironlake_crtc_mode_set(struct intel_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08007334 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007335 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08007336{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007337 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007338 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007339 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007340 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007341 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007342 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007343
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007344 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08007345
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007346 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7347 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7348
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007349 ok = ironlake_compute_clocks(&crtc->base, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007350 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007351 if (!ok && !crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007352 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7353 return -EINVAL;
7354 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007355 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007356 if (!crtc->config.clock_set) {
7357 crtc->config.dpll.n = clock.n;
7358 crtc->config.dpll.m1 = clock.m1;
7359 crtc->config.dpll.m2 = clock.m2;
7360 crtc->config.dpll.p1 = clock.p1;
7361 crtc->config.dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007362 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007363
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007364 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007365 if (crtc->config.has_pch_encoder) {
7366 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007367 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007368 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007369
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007370 dpll = ironlake_compute_dpll(crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007371 &fp, &reduced_clock,
7372 has_reduced_clock ? &fp2 : NULL);
7373
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007374 crtc->config.dpll_hw_state.dpll = dpll;
7375 crtc->config.dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007376 if (has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007377 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007378 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007379 crtc->config.dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007380
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007381 pll = intel_get_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007382 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007383 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007384 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007385 return -EINVAL;
7386 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007387 } else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007388 intel_put_shared_dpll(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007389
Jani Nikulad330a952014-01-21 11:24:25 +02007390 if (is_lvds && has_reduced_clock && i915.powersave)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007391 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007392 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007393 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007394
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007395 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007396}
7397
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007398static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7399 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007400{
7401 struct drm_device *dev = crtc->base.dev;
7402 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007403 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007404
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007405 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7406 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7407 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7408 & ~TU_SIZE_MASK;
7409 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7410 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7411 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7412}
7413
7414static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7415 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007416 struct intel_link_m_n *m_n,
7417 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007418{
7419 struct drm_device *dev = crtc->base.dev;
7420 struct drm_i915_private *dev_priv = dev->dev_private;
7421 enum pipe pipe = crtc->pipe;
7422
7423 if (INTEL_INFO(dev)->gen >= 5) {
7424 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7425 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7426 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7427 & ~TU_SIZE_MASK;
7428 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7429 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7430 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007431 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7432 * gen < 8) and if DRRS is supported (to make sure the
7433 * registers are not unnecessarily read).
7434 */
7435 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7436 crtc->config.has_drrs) {
7437 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7438 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7439 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7440 & ~TU_SIZE_MASK;
7441 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7442 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7443 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7444 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007445 } else {
7446 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7447 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7448 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7449 & ~TU_SIZE_MASK;
7450 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7451 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7452 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7453 }
7454}
7455
7456void intel_dp_get_m_n(struct intel_crtc *crtc,
7457 struct intel_crtc_config *pipe_config)
7458{
7459 if (crtc->config.has_pch_encoder)
7460 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7461 else
7462 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007463 &pipe_config->dp_m_n,
7464 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007465}
7466
Daniel Vetter72419202013-04-04 13:28:53 +02007467static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7468 struct intel_crtc_config *pipe_config)
7469{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007470 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007471 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02007472}
7473
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007474static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7475 struct intel_crtc_config *pipe_config)
7476{
7477 struct drm_device *dev = crtc->base.dev;
7478 struct drm_i915_private *dev_priv = dev->dev_private;
7479 uint32_t tmp;
7480
7481 tmp = I915_READ(PF_CTL(crtc->pipe));
7482
7483 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007484 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007485 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7486 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007487
7488 /* We currently do not free assignements of panel fitters on
7489 * ivb/hsw (since we don't use the higher upscaling modes which
7490 * differentiates them) so just WARN about this case for now. */
7491 if (IS_GEN7(dev)) {
7492 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7493 PF_PIPE_SEL_IVB(crtc->pipe));
7494 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007495 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007496}
7497
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007498static void ironlake_get_plane_config(struct intel_crtc *crtc,
7499 struct intel_plane_config *plane_config)
7500{
7501 struct drm_device *dev = crtc->base.dev;
7502 struct drm_i915_private *dev_priv = dev->dev_private;
7503 u32 val, base, offset;
7504 int pipe = crtc->pipe, plane = crtc->plane;
7505 int fourcc, pixel_format;
7506 int aligned_height;
7507
Dave Airlie66e514c2014-04-03 07:51:54 +10007508 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7509 if (!crtc->base.primary->fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007510 DRM_DEBUG_KMS("failed to alloc fb\n");
7511 return;
7512 }
7513
7514 val = I915_READ(DSPCNTR(plane));
7515
7516 if (INTEL_INFO(dev)->gen >= 4)
7517 if (val & DISPPLANE_TILED)
7518 plane_config->tiled = true;
7519
7520 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7521 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10007522 crtc->base.primary->fb->pixel_format = fourcc;
7523 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007524 drm_format_plane_cpp(fourcc, 0) * 8;
7525
7526 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7527 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7528 offset = I915_READ(DSPOFFSET(plane));
7529 } else {
7530 if (plane_config->tiled)
7531 offset = I915_READ(DSPTILEOFF(plane));
7532 else
7533 offset = I915_READ(DSPLINOFF(plane));
7534 }
7535 plane_config->base = base;
7536
7537 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007538 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7539 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007540
7541 val = I915_READ(DSPSTRIDE(pipe));
Rafael Barbalho026b96e2014-07-28 19:56:27 +01007542 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007543
Dave Airlie66e514c2014-04-03 07:51:54 +10007544 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007545 plane_config->tiled);
7546
Fabian Frederick1267a262014-07-01 20:39:41 +02007547 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7548 aligned_height);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007549
7550 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
Dave Airlie66e514c2014-04-03 07:51:54 +10007551 pipe, plane, crtc->base.primary->fb->width,
7552 crtc->base.primary->fb->height,
7553 crtc->base.primary->fb->bits_per_pixel, base,
7554 crtc->base.primary->fb->pitches[0],
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007555 plane_config->size);
7556}
7557
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007558static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7559 struct intel_crtc_config *pipe_config)
7560{
7561 struct drm_device *dev = crtc->base.dev;
7562 struct drm_i915_private *dev_priv = dev->dev_private;
7563 uint32_t tmp;
7564
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007565 if (!intel_display_power_is_enabled(dev_priv,
7566 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03007567 return false;
7568
Daniel Vettere143a212013-07-04 12:01:15 +02007569 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007570 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007571
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007572 tmp = I915_READ(PIPECONF(crtc->pipe));
7573 if (!(tmp & PIPECONF_ENABLE))
7574 return false;
7575
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007576 switch (tmp & PIPECONF_BPC_MASK) {
7577 case PIPECONF_6BPC:
7578 pipe_config->pipe_bpp = 18;
7579 break;
7580 case PIPECONF_8BPC:
7581 pipe_config->pipe_bpp = 24;
7582 break;
7583 case PIPECONF_10BPC:
7584 pipe_config->pipe_bpp = 30;
7585 break;
7586 case PIPECONF_12BPC:
7587 pipe_config->pipe_bpp = 36;
7588 break;
7589 default:
7590 break;
7591 }
7592
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007593 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7594 pipe_config->limited_color_range = true;
7595
Daniel Vetterab9412b2013-05-03 11:49:46 +02007596 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007597 struct intel_shared_dpll *pll;
7598
Daniel Vetter88adfff2013-03-28 10:42:01 +01007599 pipe_config->has_pch_encoder = true;
7600
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007601 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7602 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7603 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007604
7605 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007606
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007607 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007608 pipe_config->shared_dpll =
7609 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007610 } else {
7611 tmp = I915_READ(PCH_DPLL_SEL);
7612 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7613 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7614 else
7615 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7616 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007617
7618 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7619
7620 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7621 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007622
7623 tmp = pipe_config->dpll_hw_state.dpll;
7624 pipe_config->pixel_multiplier =
7625 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7626 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007627
7628 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007629 } else {
7630 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007631 }
7632
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007633 intel_get_pipe_timings(crtc, pipe_config);
7634
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007635 ironlake_get_pfit_config(crtc, pipe_config);
7636
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007637 return true;
7638}
7639
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007640static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7641{
7642 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007643 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007644
Damien Lespiaud3fcc802014-05-13 23:32:22 +01007645 for_each_intel_crtc(dev, crtc)
Paulo Zanoni798183c2013-12-06 20:29:01 -02007646 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007647 pipe_name(crtc->pipe));
7648
7649 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
Daniel Vetter8cc3e162014-06-25 22:01:46 +03007650 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7651 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7652 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007653 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7654 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7655 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03007656 if (IS_HASWELL(dev))
7657 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7658 "CPU PWM2 enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007659 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7660 "PCH PWM1 enabled\n");
7661 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7662 "Utility pin enabled\n");
7663 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7664
Paulo Zanoni9926ada2014-04-01 19:39:47 -03007665 /*
7666 * In theory we can still leave IRQs enabled, as long as only the HPD
7667 * interrupts remain enabled. We used to check for that, but since it's
7668 * gen-specific and since we only disable LCPLL after we fully disable
7669 * the interrupts, the check below should be enough.
7670 */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07007671 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007672}
7673
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007674static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7675{
7676 struct drm_device *dev = dev_priv->dev;
7677
7678 if (IS_HASWELL(dev))
7679 return I915_READ(D_COMP_HSW);
7680 else
7681 return I915_READ(D_COMP_BDW);
7682}
7683
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007684static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7685{
7686 struct drm_device *dev = dev_priv->dev;
7687
7688 if (IS_HASWELL(dev)) {
7689 mutex_lock(&dev_priv->rps.hw_lock);
7690 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7691 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03007692 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007693 mutex_unlock(&dev_priv->rps.hw_lock);
7694 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007695 I915_WRITE(D_COMP_BDW, val);
7696 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007697 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007698}
7699
7700/*
7701 * This function implements pieces of two sequences from BSpec:
7702 * - Sequence for display software to disable LCPLL
7703 * - Sequence for display software to allow package C8+
7704 * The steps implemented here are just the steps that actually touch the LCPLL
7705 * register. Callers should take care of disabling all the display engine
7706 * functions, doing the mode unset, fixing interrupts, etc.
7707 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007708static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7709 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007710{
7711 uint32_t val;
7712
7713 assert_can_disable_lcpll(dev_priv);
7714
7715 val = I915_READ(LCPLL_CTL);
7716
7717 if (switch_to_fclk) {
7718 val |= LCPLL_CD_SOURCE_FCLK;
7719 I915_WRITE(LCPLL_CTL, val);
7720
7721 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7722 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7723 DRM_ERROR("Switching to FCLK failed\n");
7724
7725 val = I915_READ(LCPLL_CTL);
7726 }
7727
7728 val |= LCPLL_PLL_DISABLE;
7729 I915_WRITE(LCPLL_CTL, val);
7730 POSTING_READ(LCPLL_CTL);
7731
7732 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7733 DRM_ERROR("LCPLL still locked\n");
7734
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007735 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007736 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007737 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007738 ndelay(100);
7739
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007740 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7741 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007742 DRM_ERROR("D_COMP RCOMP still in progress\n");
7743
7744 if (allow_power_down) {
7745 val = I915_READ(LCPLL_CTL);
7746 val |= LCPLL_POWER_DOWN_ALLOW;
7747 I915_WRITE(LCPLL_CTL, val);
7748 POSTING_READ(LCPLL_CTL);
7749 }
7750}
7751
7752/*
7753 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7754 * source.
7755 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007756static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007757{
7758 uint32_t val;
7759
7760 val = I915_READ(LCPLL_CTL);
7761
7762 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7763 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7764 return;
7765
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007766 /*
7767 * Make sure we're not on PC8 state before disabling PC8, otherwise
7768 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7769 *
7770 * The other problem is that hsw_restore_lcpll() is called as part of
7771 * the runtime PM resume sequence, so we can't just call
7772 * gen6_gt_force_wake_get() because that function calls
7773 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7774 * while we are on the resume sequence. So to solve this problem we have
7775 * to call special forcewake code that doesn't touch runtime PM and
7776 * doesn't enable the forcewake delayed work.
7777 */
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007778 spin_lock_irq(&dev_priv->uncore.lock);
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007779 if (dev_priv->uncore.forcewake_count++ == 0)
7780 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007781 spin_unlock_irq(&dev_priv->uncore.lock);
Paulo Zanoni215733f2013-08-19 13:18:07 -03007782
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007783 if (val & LCPLL_POWER_DOWN_ALLOW) {
7784 val &= ~LCPLL_POWER_DOWN_ALLOW;
7785 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02007786 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007787 }
7788
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007789 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007790 val |= D_COMP_COMP_FORCE;
7791 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007792 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007793
7794 val = I915_READ(LCPLL_CTL);
7795 val &= ~LCPLL_PLL_DISABLE;
7796 I915_WRITE(LCPLL_CTL, val);
7797
7798 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7799 DRM_ERROR("LCPLL not locked yet\n");
7800
7801 if (val & LCPLL_CD_SOURCE_FCLK) {
7802 val = I915_READ(LCPLL_CTL);
7803 val &= ~LCPLL_CD_SOURCE_FCLK;
7804 I915_WRITE(LCPLL_CTL, val);
7805
7806 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7807 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7808 DRM_ERROR("Switching back to LCPLL failed\n");
7809 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03007810
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007811 /* See the big comment above. */
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007812 spin_lock_irq(&dev_priv->uncore.lock);
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007813 if (--dev_priv->uncore.forcewake_count == 0)
7814 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007815 spin_unlock_irq(&dev_priv->uncore.lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007816}
7817
Paulo Zanoni765dab672014-03-07 20:08:18 -03007818/*
7819 * Package states C8 and deeper are really deep PC states that can only be
7820 * reached when all the devices on the system allow it, so even if the graphics
7821 * device allows PC8+, it doesn't mean the system will actually get to these
7822 * states. Our driver only allows PC8+ when going into runtime PM.
7823 *
7824 * The requirements for PC8+ are that all the outputs are disabled, the power
7825 * well is disabled and most interrupts are disabled, and these are also
7826 * requirements for runtime PM. When these conditions are met, we manually do
7827 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7828 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7829 * hang the machine.
7830 *
7831 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7832 * the state of some registers, so when we come back from PC8+ we need to
7833 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7834 * need to take care of the registers kept by RC6. Notice that this happens even
7835 * if we don't put the device in PCI D3 state (which is what currently happens
7836 * because of the runtime PM support).
7837 *
7838 * For more, read "Display Sequences for Package C8" on the hardware
7839 * documentation.
7840 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007841void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007842{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007843 struct drm_device *dev = dev_priv->dev;
7844 uint32_t val;
7845
Paulo Zanonic67a4702013-08-19 13:18:09 -03007846 DRM_DEBUG_KMS("Enabling package C8+\n");
7847
Paulo Zanonic67a4702013-08-19 13:18:09 -03007848 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7849 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7850 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7851 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7852 }
7853
7854 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007855 hsw_disable_lcpll(dev_priv, true, true);
7856}
7857
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007858void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007859{
7860 struct drm_device *dev = dev_priv->dev;
7861 uint32_t val;
7862
Paulo Zanonic67a4702013-08-19 13:18:09 -03007863 DRM_DEBUG_KMS("Disabling package C8+\n");
7864
7865 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007866 lpt_init_pch_refclk(dev);
7867
7868 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7869 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7870 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7871 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7872 }
7873
7874 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007875}
7876
Paulo Zanoni9a952a02014-03-07 20:12:34 -03007877static void snb_modeset_global_resources(struct drm_device *dev)
7878{
7879 modeset_update_crtc_power_domains(dev);
7880}
7881
Imre Deak4f074122013-10-16 17:25:51 +03007882static void haswell_modeset_global_resources(struct drm_device *dev)
7883{
Paulo Zanonida723562013-12-19 11:54:51 -02007884 modeset_update_crtc_power_domains(dev);
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02007885}
7886
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007887static int haswell_crtc_mode_set(struct intel_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007888 int x, int y,
7889 struct drm_framebuffer *fb)
7890{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007891 if (!intel_ddi_pll_select(crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007892 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03007893
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007894 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02007895
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007896 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007897}
7898
Damien Lespiau7d2c8172014-07-29 18:06:18 +01007899static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7900 enum port port,
7901 struct intel_crtc_config *pipe_config)
7902{
7903 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7904
7905 switch (pipe_config->ddi_pll_sel) {
7906 case PORT_CLK_SEL_WRPLL1:
7907 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7908 break;
7909 case PORT_CLK_SEL_WRPLL2:
7910 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7911 break;
7912 }
7913}
7914
Daniel Vetter26804af2014-06-25 22:01:55 +03007915static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7916 struct intel_crtc_config *pipe_config)
7917{
7918 struct drm_device *dev = crtc->base.dev;
7919 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03007920 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03007921 enum port port;
7922 uint32_t tmp;
7923
7924 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7925
7926 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7927
Damien Lespiau7d2c8172014-07-29 18:06:18 +01007928 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03007929
Daniel Vetterd452c5b2014-07-04 11:27:39 -03007930 if (pipe_config->shared_dpll >= 0) {
7931 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7932
7933 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7934 &pipe_config->dpll_hw_state));
7935 }
7936
Daniel Vetter26804af2014-06-25 22:01:55 +03007937 /*
7938 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7939 * DDI E. So just check whether this pipe is wired to DDI E and whether
7940 * the PCH transcoder is on.
7941 */
Damien Lespiauca370452013-12-03 13:56:24 +00007942 if (INTEL_INFO(dev)->gen < 9 &&
7943 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03007944 pipe_config->has_pch_encoder = true;
7945
7946 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7947 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7948 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7949
7950 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7951 }
7952}
7953
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007954static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7955 struct intel_crtc_config *pipe_config)
7956{
7957 struct drm_device *dev = crtc->base.dev;
7958 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007959 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007960 uint32_t tmp;
7961
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007962 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02007963 POWER_DOMAIN_PIPE(crtc->pipe)))
7964 return false;
7965
Daniel Vettere143a212013-07-04 12:01:15 +02007966 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007967 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7968
Daniel Vettereccb1402013-05-22 00:50:22 +02007969 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7970 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7971 enum pipe trans_edp_pipe;
7972 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7973 default:
7974 WARN(1, "unknown pipe linked to edp transcoder\n");
7975 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7976 case TRANS_DDI_EDP_INPUT_A_ON:
7977 trans_edp_pipe = PIPE_A;
7978 break;
7979 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7980 trans_edp_pipe = PIPE_B;
7981 break;
7982 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7983 trans_edp_pipe = PIPE_C;
7984 break;
7985 }
7986
7987 if (trans_edp_pipe == crtc->pipe)
7988 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7989 }
7990
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007991 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02007992 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03007993 return false;
7994
Daniel Vettereccb1402013-05-22 00:50:22 +02007995 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007996 if (!(tmp & PIPECONF_ENABLE))
7997 return false;
7998
Daniel Vetter26804af2014-06-25 22:01:55 +03007999 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008000
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008001 intel_get_pipe_timings(crtc, pipe_config);
8002
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008003 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008004 if (intel_display_power_is_enabled(dev_priv, pfit_domain))
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008005 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01008006
Jesse Barnese59150d2014-01-07 13:30:45 -08008007 if (IS_HASWELL(dev))
8008 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
8009 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03008010
Clint Taylorebb69c92014-09-30 10:30:22 -07008011 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
8012 pipe_config->pixel_multiplier =
8013 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8014 } else {
8015 pipe_config->pixel_multiplier = 1;
8016 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008017
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008018 return true;
8019}
8020
Chris Wilson560b85b2010-08-07 11:01:38 +01008021static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8022{
8023 struct drm_device *dev = crtc->dev;
8024 struct drm_i915_private *dev_priv = dev->dev_private;
8025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008026 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008027
Ville Syrjälädc41c152014-08-13 11:57:05 +03008028 if (base) {
8029 unsigned int width = intel_crtc->cursor_width;
8030 unsigned int height = intel_crtc->cursor_height;
8031 unsigned int stride = roundup_pow_of_two(width) * 4;
8032
8033 switch (stride) {
8034 default:
8035 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8036 width, stride);
8037 stride = 256;
8038 /* fallthrough */
8039 case 256:
8040 case 512:
8041 case 1024:
8042 case 2048:
8043 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008044 }
8045
Ville Syrjälädc41c152014-08-13 11:57:05 +03008046 cntl |= CURSOR_ENABLE |
8047 CURSOR_GAMMA_ENABLE |
8048 CURSOR_FORMAT_ARGB |
8049 CURSOR_STRIDE(stride);
8050
8051 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008052 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008053
Ville Syrjälädc41c152014-08-13 11:57:05 +03008054 if (intel_crtc->cursor_cntl != 0 &&
8055 (intel_crtc->cursor_base != base ||
8056 intel_crtc->cursor_size != size ||
8057 intel_crtc->cursor_cntl != cntl)) {
8058 /* On these chipsets we can only modify the base/size/stride
8059 * whilst the cursor is disabled.
8060 */
8061 I915_WRITE(_CURACNTR, 0);
8062 POSTING_READ(_CURACNTR);
8063 intel_crtc->cursor_cntl = 0;
8064 }
8065
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008066 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03008067 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008068 intel_crtc->cursor_base = base;
8069 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03008070
8071 if (intel_crtc->cursor_size != size) {
8072 I915_WRITE(CURSIZE, size);
8073 intel_crtc->cursor_size = size;
8074 }
8075
Chris Wilson4b0e3332014-05-30 16:35:26 +03008076 if (intel_crtc->cursor_cntl != cntl) {
8077 I915_WRITE(_CURACNTR, cntl);
8078 POSTING_READ(_CURACNTR);
8079 intel_crtc->cursor_cntl = cntl;
8080 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008081}
8082
8083static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8084{
8085 struct drm_device *dev = crtc->dev;
8086 struct drm_i915_private *dev_priv = dev->dev_private;
8087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8088 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008089 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008090
Chris Wilson4b0e3332014-05-30 16:35:26 +03008091 cntl = 0;
8092 if (base) {
8093 cntl = MCURSOR_GAMMA_ENABLE;
8094 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308095 case 64:
8096 cntl |= CURSOR_MODE_64_ARGB_AX;
8097 break;
8098 case 128:
8099 cntl |= CURSOR_MODE_128_ARGB_AX;
8100 break;
8101 case 256:
8102 cntl |= CURSOR_MODE_256_ARGB_AX;
8103 break;
8104 default:
8105 WARN_ON(1);
8106 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008107 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008108 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03008109
8110 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8111 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01008112 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008113
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008114 if (to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180))
8115 cntl |= CURSOR_ROTATE_180;
8116
Chris Wilson4b0e3332014-05-30 16:35:26 +03008117 if (intel_crtc->cursor_cntl != cntl) {
8118 I915_WRITE(CURCNTR(pipe), cntl);
8119 POSTING_READ(CURCNTR(pipe));
8120 intel_crtc->cursor_cntl = cntl;
8121 }
8122
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008123 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008124 I915_WRITE(CURBASE(pipe), base);
8125 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008126
8127 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008128}
8129
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008130/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008131static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8132 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008133{
8134 struct drm_device *dev = crtc->dev;
8135 struct drm_i915_private *dev_priv = dev->dev_private;
8136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8137 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008138 int x = crtc->cursor_x;
8139 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008140 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008141
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008142 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008143 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008144
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008145 if (x >= intel_crtc->config.pipe_src_w)
8146 base = 0;
8147
8148 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008149 base = 0;
8150
8151 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008152 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008153 base = 0;
8154
8155 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8156 x = -x;
8157 }
8158 pos |= x << CURSOR_X_SHIFT;
8159
8160 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008161 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008162 base = 0;
8163
8164 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8165 y = -y;
8166 }
8167 pos |= y << CURSOR_Y_SHIFT;
8168
Chris Wilson4b0e3332014-05-30 16:35:26 +03008169 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008170 return;
8171
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008172 I915_WRITE(CURPOS(pipe), pos);
8173
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008174 /* ILK+ do this automagically */
8175 if (HAS_GMCH_DISPLAY(dev) &&
8176 to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180)) {
8177 base += (intel_crtc->cursor_height *
8178 intel_crtc->cursor_width - 1) * 4;
8179 }
8180
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008181 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008182 i845_update_cursor(crtc, base);
8183 else
8184 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008185}
8186
Ville Syrjälädc41c152014-08-13 11:57:05 +03008187static bool cursor_size_ok(struct drm_device *dev,
8188 uint32_t width, uint32_t height)
8189{
8190 if (width == 0 || height == 0)
8191 return false;
8192
8193 /*
8194 * 845g/865g are special in that they are only limited by
8195 * the width of their cursors, the height is arbitrary up to
8196 * the precision of the register. Everything else requires
8197 * square cursors, limited to a few power-of-two sizes.
8198 */
8199 if (IS_845G(dev) || IS_I865G(dev)) {
8200 if ((width & 63) != 0)
8201 return false;
8202
8203 if (width > (IS_845G(dev) ? 64 : 512))
8204 return false;
8205
8206 if (height > 1023)
8207 return false;
8208 } else {
8209 switch (width | height) {
8210 case 256:
8211 case 128:
8212 if (IS_GEN2(dev))
8213 return false;
8214 case 64:
8215 break;
8216 default:
8217 return false;
8218 }
8219 }
8220
8221 return true;
8222}
8223
Matt Ropere3287952014-06-10 08:28:12 -07008224static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8225 struct drm_i915_gem_object *obj,
8226 uint32_t width, uint32_t height)
Jesse Barnes79e53942008-11-07 14:24:08 -08008227{
8228 struct drm_device *dev = crtc->dev;
8229 struct drm_i915_private *dev_priv = dev->dev_private;
8230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02008231 enum pipe pipe = intel_crtc->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -03008232 unsigned old_width;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008233 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008234 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008235
Jesse Barnes79e53942008-11-07 14:24:08 -08008236 /* if we want to turn off the cursor ignore width and height */
Matt Ropere3287952014-06-10 08:28:12 -07008237 if (!obj) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008238 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008239 addr = 0;
Pierre Willenbrock50044172009-02-23 10:12:15 +10008240 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008241 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08008242 }
8243
Dave Airlie71acb5e2008-12-30 20:31:46 +10008244 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008245 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008246 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00008247 unsigned alignment;
8248
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008249 /*
8250 * Global gtt pte registers are special registers which actually
8251 * forward writes to a chunk of system memory. Which means that
8252 * there is no risk that the register values disappear as soon
8253 * as we call intel_runtime_pm_put(), so it is correct to wrap
8254 * only the pin/unpin/fence and not more.
8255 */
8256 intel_runtime_pm_get(dev_priv);
8257
Chris Wilson693db182013-03-05 14:52:39 +00008258 /* Note that the w/a also requires 2 PTE of padding following
8259 * the bo. We currently fill all unused PTE with the shadow
8260 * page and so we should always have valid PTE following the
8261 * cursor preventing the VT-d warning.
8262 */
8263 alignment = 0;
8264 if (need_vtd_wa(dev))
8265 alignment = 64*1024;
8266
8267 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01008268 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008269 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008270 intel_runtime_pm_put(dev_priv);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01008271 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008272 }
8273
Chris Wilsond9e86c02010-11-10 16:40:20 +00008274 ret = i915_gem_object_put_fence(obj);
8275 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008276 DRM_DEBUG_KMS("failed to release fence for cursor");
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008277 intel_runtime_pm_put(dev_priv);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008278 goto fail_unpin;
8279 }
8280
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008281 addr = i915_gem_obj_ggtt_offset(obj);
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008282
8283 intel_runtime_pm_put(dev_priv);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008284 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01008285 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson00731152014-05-21 12:42:56 +01008286 ret = i915_gem_object_attach_phys(obj, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008287 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008288 DRM_DEBUG_KMS("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008289 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10008290 }
Chris Wilson00731152014-05-21 12:42:56 +01008291 addr = obj->phys_handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008292 }
8293
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008294 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008295 if (intel_crtc->cursor_bo) {
Chris Wilson00731152014-05-21 12:42:56 +01008296 if (!INTEL_INFO(dev)->cursor_needs_physical)
Chris Wilsoncc98b412013-08-09 12:25:09 +01008297 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008298 }
Jesse Barnes80824002009-09-10 15:28:06 -07008299
Daniel Vettera071fa02014-06-18 23:28:09 +02008300 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8301 INTEL_FRONTBUFFER_CURSOR(pipe));
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008302 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008303
Chris Wilson64f962e2014-03-26 12:38:15 +00008304 old_width = intel_crtc->cursor_width;
8305
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008306 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00008307 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008308 intel_crtc->cursor_width = width;
8309 intel_crtc->cursor_height = height;
8310
Chris Wilson64f962e2014-03-26 12:38:15 +00008311 if (intel_crtc->active) {
8312 if (old_width != width)
8313 intel_update_watermarks(crtc);
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03008314 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008315
Gustavo Padovan3f20df92014-10-24 14:51:34 +01008316 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8317 }
Daniel Vetterf99d7062014-06-19 16:01:59 +02008318
Jesse Barnes79e53942008-11-07 14:24:08 -08008319 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008320fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01008321 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008322fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10008323 mutex_unlock(&dev->struct_mutex);
8324 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008325}
8326
Jesse Barnes79e53942008-11-07 14:24:08 -08008327static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008328 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008329{
James Simmons72034252010-08-03 01:33:19 +01008330 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008331 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008332
James Simmons72034252010-08-03 01:33:19 +01008333 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008334 intel_crtc->lut_r[i] = red[i] >> 8;
8335 intel_crtc->lut_g[i] = green[i] >> 8;
8336 intel_crtc->lut_b[i] = blue[i] >> 8;
8337 }
8338
8339 intel_crtc_load_lut(crtc);
8340}
8341
Jesse Barnes79e53942008-11-07 14:24:08 -08008342/* VESA 640x480x72Hz mode to set on the pipe */
8343static struct drm_display_mode load_detect_mode = {
8344 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8345 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8346};
8347
Daniel Vettera8bb6812014-02-10 18:00:39 +01008348struct drm_framebuffer *
8349__intel_framebuffer_create(struct drm_device *dev,
8350 struct drm_mode_fb_cmd2 *mode_cmd,
8351 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008352{
8353 struct intel_framebuffer *intel_fb;
8354 int ret;
8355
8356 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8357 if (!intel_fb) {
8358 drm_gem_object_unreference_unlocked(&obj->base);
8359 return ERR_PTR(-ENOMEM);
8360 }
8361
8362 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008363 if (ret)
8364 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008365
8366 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008367err:
8368 drm_gem_object_unreference_unlocked(&obj->base);
8369 kfree(intel_fb);
8370
8371 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008372}
8373
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008374static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008375intel_framebuffer_create(struct drm_device *dev,
8376 struct drm_mode_fb_cmd2 *mode_cmd,
8377 struct drm_i915_gem_object *obj)
8378{
8379 struct drm_framebuffer *fb;
8380 int ret;
8381
8382 ret = i915_mutex_lock_interruptible(dev);
8383 if (ret)
8384 return ERR_PTR(ret);
8385 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8386 mutex_unlock(&dev->struct_mutex);
8387
8388 return fb;
8389}
8390
Chris Wilsond2dff872011-04-19 08:36:26 +01008391static u32
8392intel_framebuffer_pitch_for_width(int width, int bpp)
8393{
8394 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8395 return ALIGN(pitch, 64);
8396}
8397
8398static u32
8399intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8400{
8401 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02008402 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01008403}
8404
8405static struct drm_framebuffer *
8406intel_framebuffer_create_for_mode(struct drm_device *dev,
8407 struct drm_display_mode *mode,
8408 int depth, int bpp)
8409{
8410 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008411 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008412
8413 obj = i915_gem_alloc_object(dev,
8414 intel_framebuffer_size_for_mode(mode, bpp));
8415 if (obj == NULL)
8416 return ERR_PTR(-ENOMEM);
8417
8418 mode_cmd.width = mode->hdisplay;
8419 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008420 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8421 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008422 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008423
8424 return intel_framebuffer_create(dev, &mode_cmd, obj);
8425}
8426
8427static struct drm_framebuffer *
8428mode_fits_in_fbdev(struct drm_device *dev,
8429 struct drm_display_mode *mode)
8430{
Daniel Vetter4520f532013-10-09 09:18:51 +02008431#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008432 struct drm_i915_private *dev_priv = dev->dev_private;
8433 struct drm_i915_gem_object *obj;
8434 struct drm_framebuffer *fb;
8435
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008436 if (!dev_priv->fbdev)
8437 return NULL;
8438
8439 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008440 return NULL;
8441
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008442 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008443 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008444
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008445 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008446 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8447 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008448 return NULL;
8449
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008450 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008451 return NULL;
8452
8453 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008454#else
8455 return NULL;
8456#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008457}
8458
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008459bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008460 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008461 struct intel_load_detect_pipe *old,
8462 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008463{
8464 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008465 struct intel_encoder *intel_encoder =
8466 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008467 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008468 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008469 struct drm_crtc *crtc = NULL;
8470 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008471 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008472 struct drm_mode_config *config = &dev->mode_config;
8473 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008474
Chris Wilsond2dff872011-04-19 08:36:26 +01008475 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008476 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008477 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008478
Rob Clark51fd3712013-11-19 12:10:12 -05008479retry:
8480 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8481 if (ret)
8482 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008483
Jesse Barnes79e53942008-11-07 14:24:08 -08008484 /*
8485 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008486 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008487 * - if the connector already has an assigned crtc, use it (but make
8488 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008489 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008490 * - try to find the first unused crtc that can drive this connector,
8491 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008492 */
8493
8494 /* See if we already have a CRTC for this connector */
8495 if (encoder->crtc) {
8496 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008497
Rob Clark51fd3712013-11-19 12:10:12 -05008498 ret = drm_modeset_lock(&crtc->mutex, ctx);
8499 if (ret)
8500 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008501
Daniel Vetter24218aa2012-08-12 19:27:11 +02008502 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008503 old->load_detect_temp = false;
8504
8505 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008506 if (connector->dpms != DRM_MODE_DPMS_ON)
8507 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008508
Chris Wilson71731882011-04-19 23:10:58 +01008509 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008510 }
8511
8512 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008513 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008514 i++;
8515 if (!(encoder->possible_crtcs & (1 << i)))
8516 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +03008517 if (possible_crtc->enabled)
8518 continue;
8519 /* This can occur when applying the pipe A quirk on resume. */
8520 if (to_intel_crtc(possible_crtc)->new_enabled)
8521 continue;
8522
8523 crtc = possible_crtc;
8524 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008525 }
8526
8527 /*
8528 * If we didn't find an unused CRTC, don't use any.
8529 */
8530 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008531 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008532 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008533 }
8534
Rob Clark51fd3712013-11-19 12:10:12 -05008535 ret = drm_modeset_lock(&crtc->mutex, ctx);
8536 if (ret)
8537 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008538 intel_encoder->new_crtc = to_intel_crtc(crtc);
8539 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008540
8541 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008542 intel_crtc->new_enabled = true;
8543 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008544 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008545 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008546 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008547
Chris Wilson64927112011-04-20 07:25:26 +01008548 if (!mode)
8549 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008550
Chris Wilsond2dff872011-04-19 08:36:26 +01008551 /* We need a framebuffer large enough to accommodate all accesses
8552 * that the plane may generate whilst we perform load detection.
8553 * We can not rely on the fbcon either being present (we get called
8554 * during its initialisation to detect all boot displays, or it may
8555 * not even exist) or that it is large enough to satisfy the
8556 * requested mode.
8557 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008558 fb = mode_fits_in_fbdev(dev, mode);
8559 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008560 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008561 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8562 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008563 } else
8564 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008565 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008566 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008567 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008568 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008569
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008570 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008571 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008572 if (old->release_fb)
8573 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008574 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008575 }
Chris Wilson71731882011-04-19 23:10:58 +01008576
Jesse Barnes79e53942008-11-07 14:24:08 -08008577 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008578 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008579 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008580
8581 fail:
8582 intel_crtc->new_enabled = crtc->enabled;
8583 if (intel_crtc->new_enabled)
8584 intel_crtc->new_config = &intel_crtc->config;
8585 else
8586 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008587fail_unlock:
8588 if (ret == -EDEADLK) {
8589 drm_modeset_backoff(ctx);
8590 goto retry;
8591 }
8592
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008593 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008594}
8595
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008596void intel_release_load_detect_pipe(struct drm_connector *connector,
Ville Syrjälä208bf9f2014-08-11 13:15:35 +03008597 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08008598{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008599 struct intel_encoder *intel_encoder =
8600 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008601 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008602 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008603 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008604
Chris Wilsond2dff872011-04-19 08:36:26 +01008605 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008606 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008607 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008608
Chris Wilson8261b192011-04-19 23:18:09 +01008609 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008610 to_intel_connector(connector)->new_encoder = NULL;
8611 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008612 intel_crtc->new_enabled = false;
8613 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008614 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008615
Daniel Vetter36206362012-12-10 20:42:17 +01008616 if (old->release_fb) {
8617 drm_framebuffer_unregister_private(old->release_fb);
8618 drm_framebuffer_unreference(old->release_fb);
8619 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008620
Chris Wilson0622a532011-04-21 09:32:11 +01008621 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008622 }
8623
Eric Anholtc751ce42010-03-25 11:48:48 -07008624 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008625 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8626 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008627}
8628
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008629static int i9xx_pll_refclk(struct drm_device *dev,
8630 const struct intel_crtc_config *pipe_config)
8631{
8632 struct drm_i915_private *dev_priv = dev->dev_private;
8633 u32 dpll = pipe_config->dpll_hw_state.dpll;
8634
8635 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008636 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008637 else if (HAS_PCH_SPLIT(dev))
8638 return 120000;
8639 else if (!IS_GEN2(dev))
8640 return 96000;
8641 else
8642 return 48000;
8643}
8644
Jesse Barnes79e53942008-11-07 14:24:08 -08008645/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008646static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8647 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008648{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008649 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008650 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008651 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008652 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008653 u32 fp;
8654 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008655 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008656
8657 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008658 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008659 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008660 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008661
8662 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008663 if (IS_PINEVIEW(dev)) {
8664 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8665 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008666 } else {
8667 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8668 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8669 }
8670
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008671 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008672 if (IS_PINEVIEW(dev))
8673 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8674 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008675 else
8676 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008677 DPLL_FPA01_P1_POST_DIV_SHIFT);
8678
8679 switch (dpll & DPLL_MODE_MASK) {
8680 case DPLLB_MODE_DAC_SERIAL:
8681 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8682 5 : 10;
8683 break;
8684 case DPLLB_MODE_LVDS:
8685 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8686 7 : 14;
8687 break;
8688 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008689 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008690 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008691 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008692 }
8693
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008694 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008695 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008696 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008697 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008698 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008699 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008700 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008701
8702 if (is_lvds) {
8703 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8704 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008705
8706 if (lvds & LVDS_CLKB_POWER_UP)
8707 clock.p2 = 7;
8708 else
8709 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008710 } else {
8711 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8712 clock.p1 = 2;
8713 else {
8714 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8715 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8716 }
8717 if (dpll & PLL_P2_DIVIDE_BY_4)
8718 clock.p2 = 4;
8719 else
8720 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008721 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008722
8723 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008724 }
8725
Ville Syrjälä18442d02013-09-13 16:00:08 +03008726 /*
8727 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008728 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008729 * encoder's get_config() function.
8730 */
8731 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008732}
8733
Ville Syrjälä6878da02013-09-13 15:59:11 +03008734int intel_dotclock_calculate(int link_freq,
8735 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008736{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008737 /*
8738 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008739 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008740 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008741 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008742 *
8743 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008744 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008745 */
8746
Ville Syrjälä6878da02013-09-13 15:59:11 +03008747 if (!m_n->link_n)
8748 return 0;
8749
8750 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8751}
8752
Ville Syrjälä18442d02013-09-13 16:00:08 +03008753static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8754 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008755{
8756 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008757
8758 /* read out port_clock from the DPLL */
8759 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008760
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008761 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008762 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008763 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008764 * agree once we know their relationship in the encoder's
8765 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008766 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008767 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008768 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8769 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008770}
8771
8772/** Returns the currently programmed mode of the given pipe. */
8773struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8774 struct drm_crtc *crtc)
8775{
Jesse Barnes548f2452011-02-17 10:40:53 -08008776 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008778 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008779 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008780 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008781 int htot = I915_READ(HTOTAL(cpu_transcoder));
8782 int hsync = I915_READ(HSYNC(cpu_transcoder));
8783 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8784 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008785 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008786
8787 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8788 if (!mode)
8789 return NULL;
8790
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008791 /*
8792 * Construct a pipe_config sufficient for getting the clock info
8793 * back out of crtc_clock_get.
8794 *
8795 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8796 * to use a real value here instead.
8797 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008798 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008799 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008800 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8801 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8802 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008803 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8804
Ville Syrjälä773ae032013-09-23 17:48:20 +03008805 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008806 mode->hdisplay = (htot & 0xffff) + 1;
8807 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8808 mode->hsync_start = (hsync & 0xffff) + 1;
8809 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8810 mode->vdisplay = (vtot & 0xffff) + 1;
8811 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8812 mode->vsync_start = (vsync & 0xffff) + 1;
8813 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8814
8815 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008816
8817 return mode;
8818}
8819
Jesse Barnes652c3932009-08-17 13:31:43 -07008820static void intel_decrease_pllclock(struct drm_crtc *crtc)
8821{
8822 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008823 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008825
Sonika Jindalbaff2962014-07-22 11:16:35 +05308826 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008827 return;
8828
8829 if (!dev_priv->lvds_downclock_avail)
8830 return;
8831
8832 /*
8833 * Since this is called by a timer, we should never get here in
8834 * the manual case.
8835 */
8836 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008837 int pipe = intel_crtc->pipe;
8838 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008839 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008840
Zhao Yakui44d98a62009-10-09 11:39:40 +08008841 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008842
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008843 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008844
Chris Wilson074b5e12012-05-02 12:07:06 +01008845 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008846 dpll |= DISPLAY_RATE_SELECT_FPA1;
8847 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008848 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008849 dpll = I915_READ(dpll_reg);
8850 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008851 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008852 }
8853
8854}
8855
Chris Wilsonf047e392012-07-21 12:31:41 +01008856void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008857{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008858 struct drm_i915_private *dev_priv = dev->dev_private;
8859
Chris Wilsonf62a0072014-02-21 17:55:39 +00008860 if (dev_priv->mm.busy)
8861 return;
8862
Paulo Zanoni43694d62014-03-07 20:08:08 -03008863 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008864 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00008865 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01008866}
8867
8868void intel_mark_idle(struct drm_device *dev)
8869{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008870 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008871 struct drm_crtc *crtc;
8872
Chris Wilsonf62a0072014-02-21 17:55:39 +00008873 if (!dev_priv->mm.busy)
8874 return;
8875
8876 dev_priv->mm.busy = false;
8877
Jani Nikulad330a952014-01-21 11:24:25 +02008878 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008879 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00008880
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008881 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008882 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00008883 continue;
8884
8885 intel_decrease_pllclock(crtc);
8886 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008887
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008888 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008889 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008890
8891out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03008892 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008893}
8894
Jesse Barnes79e53942008-11-07 14:24:08 -08008895static void intel_crtc_destroy(struct drm_crtc *crtc)
8896{
8897 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008898 struct drm_device *dev = crtc->dev;
8899 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02008900
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02008901 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008902 work = intel_crtc->unpin_work;
8903 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02008904 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008905
8906 if (work) {
8907 cancel_work_sync(&work->work);
8908 kfree(work);
8909 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008910
8911 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008912
Jesse Barnes79e53942008-11-07 14:24:08 -08008913 kfree(intel_crtc);
8914}
8915
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008916static void intel_unpin_work_fn(struct work_struct *__work)
8917{
8918 struct intel_unpin_work *work =
8919 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008920 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02008921 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008922
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008923 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008924 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008925 drm_gem_object_unreference(&work->pending_flip_obj->base);
8926 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008927
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008928 intel_update_fbc(dev);
8929 mutex_unlock(&dev->struct_mutex);
8930
Daniel Vetterf99d7062014-06-19 16:01:59 +02008931 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
8932
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008933 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8934 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8935
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008936 kfree(work);
8937}
8938
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008939static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008940 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008941{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8943 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008944 unsigned long flags;
8945
8946 /* Ignore early vblank irqs */
8947 if (intel_crtc == NULL)
8948 return;
8949
Daniel Vetterf3260382014-09-15 14:55:23 +02008950 /*
8951 * This is called both by irq handlers and the reset code (to complete
8952 * lost pageflips) so needs the full irqsave spinlocks.
8953 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008954 spin_lock_irqsave(&dev->event_lock, flags);
8955 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008956
8957 /* Ensure we don't miss a work->pending update ... */
8958 smp_rmb();
8959
8960 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008961 spin_unlock_irqrestore(&dev->event_lock, flags);
8962 return;
8963 }
8964
Chris Wilsond6bbafa2014-09-05 07:13:24 +01008965 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008966
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008967 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008968}
8969
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008970void intel_finish_page_flip(struct drm_device *dev, int pipe)
8971{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008972 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008973 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8974
Mario Kleiner49b14a52010-12-09 07:00:07 +01008975 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008976}
8977
8978void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8979{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008980 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008981 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8982
Mario Kleiner49b14a52010-12-09 07:00:07 +01008983 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008984}
8985
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03008986/* Is 'a' after or equal to 'b'? */
8987static bool g4x_flip_count_after_eq(u32 a, u32 b)
8988{
8989 return !((a - b) & 0x80000000);
8990}
8991
8992static bool page_flip_finished(struct intel_crtc *crtc)
8993{
8994 struct drm_device *dev = crtc->base.dev;
8995 struct drm_i915_private *dev_priv = dev->dev_private;
8996
8997 /*
8998 * The relevant registers doen't exist on pre-ctg.
8999 * As the flip done interrupt doesn't trigger for mmio
9000 * flips on gmch platforms, a flip count check isn't
9001 * really needed there. But since ctg has the registers,
9002 * include it in the check anyway.
9003 */
9004 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
9005 return true;
9006
9007 /*
9008 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
9009 * used the same base address. In that case the mmio flip might
9010 * have completed, but the CS hasn't even executed the flip yet.
9011 *
9012 * A flip count check isn't enough as the CS might have updated
9013 * the base address just after start of vblank, but before we
9014 * managed to process the interrupt. This means we'd complete the
9015 * CS flip too soon.
9016 *
9017 * Combining both checks should get us a good enough result. It may
9018 * still happen that the CS flip has been executed, but has not
9019 * yet actually completed. But in case the base address is the same
9020 * anyway, we don't really care.
9021 */
9022 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9023 crtc->unpin_work->gtt_offset &&
9024 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9025 crtc->unpin_work->flip_count);
9026}
9027
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009028void intel_prepare_page_flip(struct drm_device *dev, int plane)
9029{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009030 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009031 struct intel_crtc *intel_crtc =
9032 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9033 unsigned long flags;
9034
Daniel Vetterf3260382014-09-15 14:55:23 +02009035
9036 /*
9037 * This is called both by irq handlers and the reset code (to complete
9038 * lost pageflips) so needs the full irqsave spinlocks.
9039 *
9040 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009041 * generate a page-flip completion irq, i.e. every modeset
9042 * is also accompanied by a spurious intel_prepare_page_flip().
9043 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009044 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009045 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009046 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009047 spin_unlock_irqrestore(&dev->event_lock, flags);
9048}
9049
Robin Schroereba905b2014-05-18 02:24:50 +02009050static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009051{
9052 /* Ensure that the work item is consistent when activating it ... */
9053 smp_wmb();
9054 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9055 /* and that it is marked active as soon as the irq could fire. */
9056 smp_wmb();
9057}
9058
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009059static int intel_gen2_queue_flip(struct drm_device *dev,
9060 struct drm_crtc *crtc,
9061 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009062 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009063 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009064 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009065{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009067 u32 flip_mask;
9068 int ret;
9069
Daniel Vetter6d90c952012-04-26 23:28:05 +02009070 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009071 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009072 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009073
9074 /* Can't queue multiple flips, so wait for the previous
9075 * one to finish before executing the next.
9076 */
9077 if (intel_crtc->plane)
9078 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9079 else
9080 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009081 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9082 intel_ring_emit(ring, MI_NOOP);
9083 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9084 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9085 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009086 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009087 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009088
9089 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009090 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009091 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009092}
9093
9094static int intel_gen3_queue_flip(struct drm_device *dev,
9095 struct drm_crtc *crtc,
9096 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009097 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009098 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009099 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009100{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009102 u32 flip_mask;
9103 int ret;
9104
Daniel Vetter6d90c952012-04-26 23:28:05 +02009105 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009106 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009107 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009108
9109 if (intel_crtc->plane)
9110 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9111 else
9112 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009113 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9114 intel_ring_emit(ring, MI_NOOP);
9115 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9116 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9117 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009118 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009119 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009120
Chris Wilsone7d841c2012-12-03 11:36:30 +00009121 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009122 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009123 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009124}
9125
9126static int intel_gen4_queue_flip(struct drm_device *dev,
9127 struct drm_crtc *crtc,
9128 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009129 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009130 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009131 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009132{
9133 struct drm_i915_private *dev_priv = dev->dev_private;
9134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9135 uint32_t pf, pipesrc;
9136 int ret;
9137
Daniel Vetter6d90c952012-04-26 23:28:05 +02009138 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009139 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009140 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009141
9142 /* i965+ uses the linear or tiled offsets from the
9143 * Display Registers (which do not change across a page-flip)
9144 * so we need only reprogram the base address.
9145 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009146 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9147 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9148 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009149 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009150 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009151
9152 /* XXX Enabling the panel-fitter across page-flip is so far
9153 * untested on non-native modes, so ignore it for now.
9154 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9155 */
9156 pf = 0;
9157 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009158 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009159
9160 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009161 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009162 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009163}
9164
9165static int intel_gen6_queue_flip(struct drm_device *dev,
9166 struct drm_crtc *crtc,
9167 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009168 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009169 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009170 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009171{
9172 struct drm_i915_private *dev_priv = dev->dev_private;
9173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9174 uint32_t pf, pipesrc;
9175 int ret;
9176
Daniel Vetter6d90c952012-04-26 23:28:05 +02009177 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009178 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009179 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009180
Daniel Vetter6d90c952012-04-26 23:28:05 +02009181 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9182 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9183 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009184 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009185
Chris Wilson99d9acd2012-04-17 20:37:00 +01009186 /* Contrary to the suggestions in the documentation,
9187 * "Enable Panel Fitter" does not seem to be required when page
9188 * flipping with a non-native mode, and worse causes a normal
9189 * modeset to fail.
9190 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9191 */
9192 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009193 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009194 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009195
9196 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009197 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009198 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009199}
9200
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009201static int intel_gen7_queue_flip(struct drm_device *dev,
9202 struct drm_crtc *crtc,
9203 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009204 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009205 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009206 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009207{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009208 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009209 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009210 int len, ret;
9211
Robin Schroereba905b2014-05-18 02:24:50 +02009212 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009213 case PLANE_A:
9214 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9215 break;
9216 case PLANE_B:
9217 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9218 break;
9219 case PLANE_C:
9220 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9221 break;
9222 default:
9223 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009224 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009225 }
9226
Chris Wilsonffe74d72013-08-26 20:58:12 +01009227 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009228 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009229 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009230 /*
9231 * On Gen 8, SRM is now taking an extra dword to accommodate
9232 * 48bits addresses, and we need a NOOP for the batch size to
9233 * stay even.
9234 */
9235 if (IS_GEN8(dev))
9236 len += 2;
9237 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009238
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009239 /*
9240 * BSpec MI_DISPLAY_FLIP for IVB:
9241 * "The full packet must be contained within the same cache line."
9242 *
9243 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9244 * cacheline, if we ever start emitting more commands before
9245 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9246 * then do the cacheline alignment, and finally emit the
9247 * MI_DISPLAY_FLIP.
9248 */
9249 ret = intel_ring_cacheline_align(ring);
9250 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009251 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009252
Chris Wilsonffe74d72013-08-26 20:58:12 +01009253 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009254 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009255 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009256
Chris Wilsonffe74d72013-08-26 20:58:12 +01009257 /* Unmask the flip-done completion message. Note that the bspec says that
9258 * we should do this for both the BCS and RCS, and that we must not unmask
9259 * more than one flip event at any time (or ensure that one flip message
9260 * can be sent by waiting for flip-done prior to queueing new flips).
9261 * Experimentation says that BCS works despite DERRMR masking all
9262 * flip-done completion events and that unmasking all planes at once
9263 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9264 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9265 */
9266 if (ring->id == RCS) {
9267 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9268 intel_ring_emit(ring, DERRMR);
9269 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9270 DERRMR_PIPEB_PRI_FLIP_DONE |
9271 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009272 if (IS_GEN8(dev))
9273 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9274 MI_SRM_LRM_GLOBAL_GTT);
9275 else
9276 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9277 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009278 intel_ring_emit(ring, DERRMR);
9279 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009280 if (IS_GEN8(dev)) {
9281 intel_ring_emit(ring, 0);
9282 intel_ring_emit(ring, MI_NOOP);
9283 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009284 }
9285
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009286 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009287 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009288 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009289 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009290
9291 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009292 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009293 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009294}
9295
Sourab Gupta84c33a62014-06-02 16:47:17 +05309296static bool use_mmio_flip(struct intel_engine_cs *ring,
9297 struct drm_i915_gem_object *obj)
9298{
9299 /*
9300 * This is not being used for older platforms, because
9301 * non-availability of flip done interrupt forces us to use
9302 * CS flips. Older platforms derive flip done using some clever
9303 * tricks involving the flip_pending status bits and vblank irqs.
9304 * So using MMIO flips there would disrupt this mechanism.
9305 */
9306
Chris Wilson8e09bf82014-07-08 10:40:30 +01009307 if (ring == NULL)
9308 return true;
9309
Sourab Gupta84c33a62014-06-02 16:47:17 +05309310 if (INTEL_INFO(ring->dev)->gen < 5)
9311 return false;
9312
9313 if (i915.use_mmio_flip < 0)
9314 return false;
9315 else if (i915.use_mmio_flip > 0)
9316 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +01009317 else if (i915.enable_execlists)
9318 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309319 else
9320 return ring != obj->ring;
9321}
9322
9323static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9324{
9325 struct drm_device *dev = intel_crtc->base.dev;
9326 struct drm_i915_private *dev_priv = dev->dev_private;
9327 struct intel_framebuffer *intel_fb =
9328 to_intel_framebuffer(intel_crtc->base.primary->fb);
9329 struct drm_i915_gem_object *obj = intel_fb->obj;
9330 u32 dspcntr;
9331 u32 reg;
9332
9333 intel_mark_page_flip_active(intel_crtc);
9334
9335 reg = DSPCNTR(intel_crtc->plane);
9336 dspcntr = I915_READ(reg);
9337
Damien Lespiauc5d97472014-10-25 00:11:11 +01009338 if (obj->tiling_mode != I915_TILING_NONE)
9339 dspcntr |= DISPPLANE_TILED;
9340 else
9341 dspcntr &= ~DISPPLANE_TILED;
9342
Sourab Gupta84c33a62014-06-02 16:47:17 +05309343 I915_WRITE(reg, dspcntr);
9344
9345 I915_WRITE(DSPSURF(intel_crtc->plane),
9346 intel_crtc->unpin_work->gtt_offset);
9347 POSTING_READ(DSPSURF(intel_crtc->plane));
9348}
9349
9350static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9351{
9352 struct intel_engine_cs *ring;
9353 int ret;
9354
9355 lockdep_assert_held(&obj->base.dev->struct_mutex);
9356
9357 if (!obj->last_write_seqno)
9358 return 0;
9359
9360 ring = obj->ring;
9361
9362 if (i915_seqno_passed(ring->get_seqno(ring, true),
9363 obj->last_write_seqno))
9364 return 0;
9365
9366 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9367 if (ret)
9368 return ret;
9369
9370 if (WARN_ON(!ring->irq_get(ring)))
9371 return 0;
9372
9373 return 1;
9374}
9375
9376void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9377{
9378 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9379 struct intel_crtc *intel_crtc;
9380 unsigned long irq_flags;
9381 u32 seqno;
9382
9383 seqno = ring->get_seqno(ring, false);
9384
9385 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9386 for_each_intel_crtc(ring->dev, intel_crtc) {
9387 struct intel_mmio_flip *mmio_flip;
9388
9389 mmio_flip = &intel_crtc->mmio_flip;
9390 if (mmio_flip->seqno == 0)
9391 continue;
9392
9393 if (ring->id != mmio_flip->ring_id)
9394 continue;
9395
9396 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9397 intel_do_mmio_flip(intel_crtc);
9398 mmio_flip->seqno = 0;
9399 ring->irq_put(ring);
9400 }
9401 }
9402 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9403}
9404
9405static int intel_queue_mmio_flip(struct drm_device *dev,
9406 struct drm_crtc *crtc,
9407 struct drm_framebuffer *fb,
9408 struct drm_i915_gem_object *obj,
9409 struct intel_engine_cs *ring,
9410 uint32_t flags)
9411{
9412 struct drm_i915_private *dev_priv = dev->dev_private;
9413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309414 int ret;
9415
9416 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9417 return -EBUSY;
9418
9419 ret = intel_postpone_flip(obj);
9420 if (ret < 0)
9421 return ret;
9422 if (ret == 0) {
9423 intel_do_mmio_flip(intel_crtc);
9424 return 0;
9425 }
9426
Daniel Vetter24955f22014-09-15 14:55:32 +02009427 spin_lock_irq(&dev_priv->mmio_flip_lock);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309428 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9429 intel_crtc->mmio_flip.ring_id = obj->ring->id;
Daniel Vetter24955f22014-09-15 14:55:32 +02009430 spin_unlock_irq(&dev_priv->mmio_flip_lock);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309431
9432 /*
9433 * Double check to catch cases where irq fired before
9434 * mmio flip data was ready
9435 */
9436 intel_notify_mmio_flip(obj->ring);
9437 return 0;
9438}
9439
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009440static int intel_default_queue_flip(struct drm_device *dev,
9441 struct drm_crtc *crtc,
9442 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009443 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009444 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009445 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009446{
9447 return -ENODEV;
9448}
9449
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009450static bool __intel_pageflip_stall_check(struct drm_device *dev,
9451 struct drm_crtc *crtc)
9452{
9453 struct drm_i915_private *dev_priv = dev->dev_private;
9454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9455 struct intel_unpin_work *work = intel_crtc->unpin_work;
9456 u32 addr;
9457
9458 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9459 return true;
9460
9461 if (!work->enable_stall_check)
9462 return false;
9463
9464 if (work->flip_ready_vblank == 0) {
9465 if (work->flip_queued_ring &&
9466 !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
9467 work->flip_queued_seqno))
9468 return false;
9469
9470 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9471 }
9472
9473 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9474 return false;
9475
9476 /* Potential stall - if we see that the flip has happened,
9477 * assume a missed interrupt. */
9478 if (INTEL_INFO(dev)->gen >= 4)
9479 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9480 else
9481 addr = I915_READ(DSPADDR(intel_crtc->plane));
9482
9483 /* There is a potential issue here with a false positive after a flip
9484 * to the same address. We could address this by checking for a
9485 * non-incrementing frame counter.
9486 */
9487 return addr == work->gtt_offset;
9488}
9489
9490void intel_check_page_flip(struct drm_device *dev, int pipe)
9491{
9492 struct drm_i915_private *dev_priv = dev->dev_private;
9493 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9494 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterf3260382014-09-15 14:55:23 +02009495
9496 WARN_ON(!in_irq());
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009497
9498 if (crtc == NULL)
9499 return;
9500
Daniel Vetterf3260382014-09-15 14:55:23 +02009501 spin_lock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009502 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9503 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9504 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9505 page_flip_completed(intel_crtc);
9506 }
Daniel Vetterf3260382014-09-15 14:55:23 +02009507 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009508}
9509
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009510static int intel_crtc_page_flip(struct drm_crtc *crtc,
9511 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009512 struct drm_pending_vblank_event *event,
9513 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009514{
9515 struct drm_device *dev = crtc->dev;
9516 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009517 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009518 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009519 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02009520 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009521 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009522 struct intel_engine_cs *ring;
Chris Wilson52e68632010-08-08 10:15:59 +01009523 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009524
Matt Roper2ff8fde2014-07-08 07:50:07 -07009525 /*
9526 * drm_mode_page_flip_ioctl() should already catch this, but double
9527 * check to be safe. In the future we may enable pageflipping from
9528 * a disabled primary plane.
9529 */
9530 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9531 return -EBUSY;
9532
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009533 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009534 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009535 return -EINVAL;
9536
9537 /*
9538 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9539 * Note that pitch changes could also affect these register.
9540 */
9541 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009542 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9543 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009544 return -EINVAL;
9545
Chris Wilsonf900db42014-02-20 09:26:13 +00009546 if (i915_terminally_wedged(&dev_priv->gpu_error))
9547 goto out_hang;
9548
Daniel Vetterb14c5672013-09-19 12:18:32 +02009549 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009550 if (work == NULL)
9551 return -ENOMEM;
9552
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009553 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009554 work->crtc = crtc;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009555 work->old_fb_obj = intel_fb_obj(old_fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009556 INIT_WORK(&work->work, intel_unpin_work_fn);
9557
Daniel Vetter87b6b102014-05-15 15:33:46 +02009558 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009559 if (ret)
9560 goto free_work;
9561
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009562 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009563 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009564 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009565 /* Before declaring the flip queue wedged, check if
9566 * the hardware completed the operation behind our backs.
9567 */
9568 if (__intel_pageflip_stall_check(dev, crtc)) {
9569 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9570 page_flip_completed(intel_crtc);
9571 } else {
9572 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009573 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +01009574
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009575 drm_crtc_vblank_put(crtc);
9576 kfree(work);
9577 return -EBUSY;
9578 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009579 }
9580 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009581 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009582
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009583 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9584 flush_workqueue(dev_priv->wq);
9585
Chris Wilson79158102012-05-23 11:13:58 +01009586 ret = i915_mutex_lock_interruptible(dev);
9587 if (ret)
9588 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009589
Jesse Barnes75dfca82010-02-10 15:09:44 -08009590 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009591 drm_gem_object_reference(&work->old_fb_obj->base);
9592 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009593
Matt Roperf4510a22014-04-01 15:22:40 -07009594 crtc->primary->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01009595
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009596 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009597
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009598 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009599 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009600
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009601 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +02009602 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009603
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009604 if (IS_VALLEYVIEW(dev)) {
9605 ring = &dev_priv->ring[BCS];
Chris Wilson8e09bf82014-07-08 10:40:30 +01009606 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9607 /* vlv: DISPLAY_FLIP fails to change tiling */
9608 ring = NULL;
Chris Wilson2a92d5b2014-07-08 10:40:29 +01009609 } else if (IS_IVYBRIDGE(dev)) {
9610 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009611 } else if (INTEL_INFO(dev)->gen >= 7) {
9612 ring = obj->ring;
9613 if (ring == NULL || ring->id != RCS)
9614 ring = &dev_priv->ring[BCS];
9615 } else {
9616 ring = &dev_priv->ring[RCS];
9617 }
9618
9619 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009620 if (ret)
9621 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009622
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009623 work->gtt_offset =
9624 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9625
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009626 if (use_mmio_flip(ring, obj)) {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309627 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9628 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009629 if (ret)
9630 goto cleanup_unpin;
9631
9632 work->flip_queued_seqno = obj->last_write_seqno;
9633 work->flip_queued_ring = obj->ring;
9634 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309635 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009636 page_flip_flags);
9637 if (ret)
9638 goto cleanup_unpin;
9639
9640 work->flip_queued_seqno = intel_ring_get_seqno(ring);
9641 work->flip_queued_ring = ring;
9642 }
9643
9644 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9645 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009646
Daniel Vettera071fa02014-06-18 23:28:09 +02009647 i915_gem_track_fb(work->old_fb_obj, obj,
9648 INTEL_FRONTBUFFER_PRIMARY(pipe));
9649
Chris Wilson7782de32011-07-08 12:22:41 +01009650 intel_disable_fbc(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009651 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009652 mutex_unlock(&dev->struct_mutex);
9653
Jesse Barnese5510fa2010-07-01 16:48:37 -07009654 trace_i915_flip_request(intel_crtc->plane, obj);
9655
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009656 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01009657
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009658cleanup_unpin:
9659 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009660cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009661 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -07009662 crtc->primary->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00009663 drm_gem_object_unreference(&work->old_fb_obj->base);
9664 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01009665 mutex_unlock(&dev->struct_mutex);
9666
Chris Wilson79158102012-05-23 11:13:58 +01009667cleanup:
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009668 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +01009669 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009670 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +01009671
Daniel Vetter87b6b102014-05-15 15:33:46 +02009672 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009673free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01009674 kfree(work);
9675
Chris Wilsonf900db42014-02-20 09:26:13 +00009676 if (ret == -EIO) {
9677out_hang:
9678 intel_crtc_wait_for_pending_flips(crtc);
9679 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +01009680 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009681 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +02009682 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009683 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +01009684 }
Chris Wilsonf900db42014-02-20 09:26:13 +00009685 }
Chris Wilson96b099f2010-06-07 14:03:04 +01009686 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009687}
9688
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009689static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009690 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9691 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009692};
9693
Daniel Vetter9a935852012-07-05 22:34:27 +02009694/**
9695 * intel_modeset_update_staged_output_state
9696 *
9697 * Updates the staged output configuration state, e.g. after we've read out the
9698 * current hw state.
9699 */
9700static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9701{
Ville Syrjälä76688512014-01-10 11:28:06 +02009702 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009703 struct intel_encoder *encoder;
9704 struct intel_connector *connector;
9705
9706 list_for_each_entry(connector, &dev->mode_config.connector_list,
9707 base.head) {
9708 connector->new_encoder =
9709 to_intel_encoder(connector->base.encoder);
9710 }
9711
Damien Lespiaub2784e12014-08-05 11:29:37 +01009712 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009713 encoder->new_crtc =
9714 to_intel_crtc(encoder->base.crtc);
9715 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009716
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009717 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009718 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009719
9720 if (crtc->new_enabled)
9721 crtc->new_config = &crtc->config;
9722 else
9723 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02009724 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009725}
9726
9727/**
9728 * intel_modeset_commit_output_state
9729 *
9730 * This function copies the stage display pipe configuration to the real one.
9731 */
9732static void intel_modeset_commit_output_state(struct drm_device *dev)
9733{
Ville Syrjälä76688512014-01-10 11:28:06 +02009734 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009735 struct intel_encoder *encoder;
9736 struct intel_connector *connector;
9737
9738 list_for_each_entry(connector, &dev->mode_config.connector_list,
9739 base.head) {
9740 connector->base.encoder = &connector->new_encoder->base;
9741 }
9742
Damien Lespiaub2784e12014-08-05 11:29:37 +01009743 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009744 encoder->base.crtc = &encoder->new_crtc->base;
9745 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009746
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009747 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009748 crtc->base.enabled = crtc->new_enabled;
9749 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009750}
9751
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009752static void
Robin Schroereba905b2014-05-18 02:24:50 +02009753connected_sink_compute_bpp(struct intel_connector *connector,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009754 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009755{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009756 int bpp = pipe_config->pipe_bpp;
9757
9758 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9759 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03009760 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009761
9762 /* Don't use an invalid EDID bpc value */
9763 if (connector->base.display_info.bpc &&
9764 connector->base.display_info.bpc * 3 < bpp) {
9765 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9766 bpp, connector->base.display_info.bpc*3);
9767 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9768 }
9769
9770 /* Clamp bpp to 8 on screens without EDID 1.4 */
9771 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9772 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9773 bpp);
9774 pipe_config->pipe_bpp = 24;
9775 }
9776}
9777
9778static int
9779compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9780 struct drm_framebuffer *fb,
9781 struct intel_crtc_config *pipe_config)
9782{
9783 struct drm_device *dev = crtc->base.dev;
9784 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009785 int bpp;
9786
Daniel Vetterd42264b2013-03-28 16:38:08 +01009787 switch (fb->pixel_format) {
9788 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009789 bpp = 8*3; /* since we go through a colormap */
9790 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009791 case DRM_FORMAT_XRGB1555:
9792 case DRM_FORMAT_ARGB1555:
9793 /* checked in intel_framebuffer_init already */
9794 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9795 return -EINVAL;
9796 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009797 bpp = 6*3; /* min is 18bpp */
9798 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009799 case DRM_FORMAT_XBGR8888:
9800 case DRM_FORMAT_ABGR8888:
9801 /* checked in intel_framebuffer_init already */
9802 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9803 return -EINVAL;
9804 case DRM_FORMAT_XRGB8888:
9805 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009806 bpp = 8*3;
9807 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009808 case DRM_FORMAT_XRGB2101010:
9809 case DRM_FORMAT_ARGB2101010:
9810 case DRM_FORMAT_XBGR2101010:
9811 case DRM_FORMAT_ABGR2101010:
9812 /* checked in intel_framebuffer_init already */
9813 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01009814 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009815 bpp = 10*3;
9816 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01009817 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009818 default:
9819 DRM_DEBUG_KMS("unsupported depth\n");
9820 return -EINVAL;
9821 }
9822
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009823 pipe_config->pipe_bpp = bpp;
9824
9825 /* Clamp display bpp to EDID value */
9826 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009827 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02009828 if (!connector->new_encoder ||
9829 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009830 continue;
9831
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009832 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009833 }
9834
9835 return bpp;
9836}
9837
Daniel Vetter644db712013-09-19 14:53:58 +02009838static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9839{
9840 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9841 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01009842 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02009843 mode->crtc_hdisplay, mode->crtc_hsync_start,
9844 mode->crtc_hsync_end, mode->crtc_htotal,
9845 mode->crtc_vdisplay, mode->crtc_vsync_start,
9846 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9847}
9848
Daniel Vetterc0b03412013-05-28 12:05:54 +02009849static void intel_dump_pipe_config(struct intel_crtc *crtc,
9850 struct intel_crtc_config *pipe_config,
9851 const char *context)
9852{
9853 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9854 context, pipe_name(crtc->pipe));
9855
9856 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9857 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9858 pipe_config->pipe_bpp, pipe_config->dither);
9859 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9860 pipe_config->has_pch_encoder,
9861 pipe_config->fdi_lanes,
9862 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9863 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9864 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009865 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9866 pipe_config->has_dp_encoder,
9867 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9868 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9869 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009870
9871 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
9872 pipe_config->has_dp_encoder,
9873 pipe_config->dp_m2_n2.gmch_m,
9874 pipe_config->dp_m2_n2.gmch_n,
9875 pipe_config->dp_m2_n2.link_m,
9876 pipe_config->dp_m2_n2.link_n,
9877 pipe_config->dp_m2_n2.tu);
9878
Daniel Vetterc0b03412013-05-28 12:05:54 +02009879 DRM_DEBUG_KMS("requested mode:\n");
9880 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9881 DRM_DEBUG_KMS("adjusted mode:\n");
9882 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02009883 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009884 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009885 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9886 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009887 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9888 pipe_config->gmch_pfit.control,
9889 pipe_config->gmch_pfit.pgm_ratios,
9890 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009891 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02009892 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009893 pipe_config->pch_pfit.size,
9894 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009895 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03009896 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009897}
9898
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009899static bool encoders_cloneable(const struct intel_encoder *a,
9900 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009901{
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009902 /* masks could be asymmetric, so check both ways */
9903 return a == b || (a->cloneable & (1 << b->type) &&
9904 b->cloneable & (1 << a->type));
9905}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009906
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009907static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9908 struct intel_encoder *encoder)
9909{
9910 struct drm_device *dev = crtc->base.dev;
9911 struct intel_encoder *source_encoder;
9912
Damien Lespiaub2784e12014-08-05 11:29:37 +01009913 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009914 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009915 continue;
9916
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009917 if (!encoders_cloneable(encoder, source_encoder))
9918 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009919 }
9920
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009921 return true;
9922}
9923
9924static bool check_encoder_cloning(struct intel_crtc *crtc)
9925{
9926 struct drm_device *dev = crtc->base.dev;
9927 struct intel_encoder *encoder;
9928
Damien Lespiaub2784e12014-08-05 11:29:37 +01009929 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009930 if (encoder->new_crtc != crtc)
9931 continue;
9932
9933 if (!check_single_encoder_cloning(crtc, encoder))
9934 return false;
9935 }
9936
9937 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009938}
9939
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009940static struct intel_crtc_config *
9941intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009942 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009943 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02009944{
9945 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02009946 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009947 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01009948 int plane_bpp, ret = -EINVAL;
9949 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02009950
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009951 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009952 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9953 return ERR_PTR(-EINVAL);
9954 }
9955
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009956 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9957 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02009958 return ERR_PTR(-ENOMEM);
9959
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009960 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9961 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009962
Daniel Vettere143a212013-07-04 12:01:15 +02009963 pipe_config->cpu_transcoder =
9964 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009965 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009966
Imre Deak2960bc92013-07-30 13:36:32 +03009967 /*
9968 * Sanitize sync polarity flags based on requested ones. If neither
9969 * positive or negative polarity is requested, treat this as meaning
9970 * negative polarity.
9971 */
9972 if (!(pipe_config->adjusted_mode.flags &
9973 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9974 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9975
9976 if (!(pipe_config->adjusted_mode.flags &
9977 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9978 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9979
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009980 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9981 * plane pixel format and any sink constraints into account. Returns the
9982 * source plane bpp so that dithering can be selected on mismatches
9983 * after encoders and crtc also have had their say. */
9984 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9985 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009986 if (plane_bpp < 0)
9987 goto fail;
9988
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03009989 /*
9990 * Determine the real pipe dimensions. Note that stereo modes can
9991 * increase the actual pipe size due to the frame doubling and
9992 * insertion of additional space for blanks between the frame. This
9993 * is stored in the crtc timings. We use the requested mode to do this
9994 * computation to clearly distinguish it from the adjusted mode, which
9995 * can be changed by the connectors in the below retry loop.
9996 */
9997 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9998 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9999 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
10000
Daniel Vettere29c22c2013-02-21 00:00:16 +010010001encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020010002 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020010003 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020010004 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010005
Daniel Vetter135c81b2013-07-21 21:37:09 +020010006 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +010010007 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020010008
Daniel Vetter7758a112012-07-08 19:40:39 +020010009 /* Pass our mode to the connectors and the CRTC to give them a chance to
10010 * adjust it according to limitations or connector properties, and also
10011 * a chance to reject the mode entirely.
10012 */
Damien Lespiaub2784e12014-08-05 11:29:37 +010010013 for_each_intel_encoder(dev, encoder) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010014
10015 if (&encoder->new_crtc->base != crtc)
10016 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010017
Daniel Vetterefea6e82013-07-21 21:36:59 +020010018 if (!(encoder->compute_config(encoder, pipe_config))) {
10019 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010020 goto fail;
10021 }
10022 }
10023
Daniel Vetterff9a6752013-06-01 17:16:21 +020010024 /* Set default port clock if not overwritten by the encoder. Needs to be
10025 * done afterwards in case the encoder adjusts the mode. */
10026 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +010010027 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10028 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010029
Daniel Vettera43f6e02013-06-07 23:10:32 +020010030 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010031 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010032 DRM_DEBUG_KMS("CRTC fixup failed\n");
10033 goto fail;
10034 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010035
10036 if (ret == RETRY) {
10037 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10038 ret = -EINVAL;
10039 goto fail;
10040 }
10041
10042 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10043 retry = false;
10044 goto encoder_retry;
10045 }
10046
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010047 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10048 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10049 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10050
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010051 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010052fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010053 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010054 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010055}
10056
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010057/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10058 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10059static void
10060intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10061 unsigned *prepare_pipes, unsigned *disable_pipes)
10062{
10063 struct intel_crtc *intel_crtc;
10064 struct drm_device *dev = crtc->dev;
10065 struct intel_encoder *encoder;
10066 struct intel_connector *connector;
10067 struct drm_crtc *tmp_crtc;
10068
10069 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10070
10071 /* Check which crtcs have changed outputs connected to them, these need
10072 * to be part of the prepare_pipes mask. We don't (yet) support global
10073 * modeset across multiple crtcs, so modeset_pipes will only have one
10074 * bit set at most. */
10075 list_for_each_entry(connector, &dev->mode_config.connector_list,
10076 base.head) {
10077 if (connector->base.encoder == &connector->new_encoder->base)
10078 continue;
10079
10080 if (connector->base.encoder) {
10081 tmp_crtc = connector->base.encoder->crtc;
10082
10083 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10084 }
10085
10086 if (connector->new_encoder)
10087 *prepare_pipes |=
10088 1 << connector->new_encoder->new_crtc->pipe;
10089 }
10090
Damien Lespiaub2784e12014-08-05 11:29:37 +010010091 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010092 if (encoder->base.crtc == &encoder->new_crtc->base)
10093 continue;
10094
10095 if (encoder->base.crtc) {
10096 tmp_crtc = encoder->base.crtc;
10097
10098 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10099 }
10100
10101 if (encoder->new_crtc)
10102 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10103 }
10104
Ville Syrjälä76688512014-01-10 11:28:06 +020010105 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010106 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010107 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010108 continue;
10109
Ville Syrjälä76688512014-01-10 11:28:06 +020010110 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010111 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020010112 else
10113 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010114 }
10115
10116
10117 /* set_mode is also used to update properties on life display pipes. */
10118 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020010119 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010120 *prepare_pipes |= 1 << intel_crtc->pipe;
10121
Daniel Vetterb6c51642013-04-12 18:48:43 +020010122 /*
10123 * For simplicity do a full modeset on any pipe where the output routing
10124 * changed. We could be more clever, but that would require us to be
10125 * more careful with calling the relevant encoder->mode_set functions.
10126 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010127 if (*prepare_pipes)
10128 *modeset_pipes = *prepare_pipes;
10129
10130 /* ... and mask these out. */
10131 *modeset_pipes &= ~(*disable_pipes);
10132 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010133
10134 /*
10135 * HACK: We don't (yet) fully support global modesets. intel_set_config
10136 * obies this rule, but the modeset restore mode of
10137 * intel_modeset_setup_hw_state does not.
10138 */
10139 *modeset_pipes &= 1 << intel_crtc->pipe;
10140 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010141
10142 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10143 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010144}
10145
Daniel Vetterea9d7582012-07-10 10:42:52 +020010146static bool intel_crtc_in_use(struct drm_crtc *crtc)
10147{
10148 struct drm_encoder *encoder;
10149 struct drm_device *dev = crtc->dev;
10150
10151 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10152 if (encoder->crtc == crtc)
10153 return true;
10154
10155 return false;
10156}
10157
10158static void
10159intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10160{
10161 struct intel_encoder *intel_encoder;
10162 struct intel_crtc *intel_crtc;
10163 struct drm_connector *connector;
10164
Damien Lespiaub2784e12014-08-05 11:29:37 +010010165 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020010166 if (!intel_encoder->base.crtc)
10167 continue;
10168
10169 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10170
10171 if (prepare_pipes & (1 << intel_crtc->pipe))
10172 intel_encoder->connectors_active = false;
10173 }
10174
10175 intel_modeset_commit_output_state(dev);
10176
Ville Syrjälä76688512014-01-10 11:28:06 +020010177 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010178 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010179 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010180 WARN_ON(intel_crtc->new_config &&
10181 intel_crtc->new_config != &intel_crtc->config);
10182 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010183 }
10184
10185 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10186 if (!connector->encoder || !connector->encoder->crtc)
10187 continue;
10188
10189 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10190
10191 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020010192 struct drm_property *dpms_property =
10193 dev->mode_config.dpms_property;
10194
Daniel Vetterea9d7582012-07-10 10:42:52 +020010195 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050010196 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020010197 dpms_property,
10198 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010199
10200 intel_encoder = to_intel_encoder(connector->encoder);
10201 intel_encoder->connectors_active = true;
10202 }
10203 }
10204
10205}
10206
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010207static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010208{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010209 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010210
10211 if (clock1 == clock2)
10212 return true;
10213
10214 if (!clock1 || !clock2)
10215 return false;
10216
10217 diff = abs(clock1 - clock2);
10218
10219 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10220 return true;
10221
10222 return false;
10223}
10224
Daniel Vetter25c5b262012-07-08 22:08:04 +020010225#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10226 list_for_each_entry((intel_crtc), \
10227 &(dev)->mode_config.crtc_list, \
10228 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020010229 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020010230
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010231static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010232intel_pipe_config_compare(struct drm_device *dev,
10233 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010234 struct intel_crtc_config *pipe_config)
10235{
Daniel Vetter66e985c2013-06-05 13:34:20 +020010236#define PIPE_CONF_CHECK_X(name) \
10237 if (current_config->name != pipe_config->name) { \
10238 DRM_ERROR("mismatch in " #name " " \
10239 "(expected 0x%08x, found 0x%08x)\n", \
10240 current_config->name, \
10241 pipe_config->name); \
10242 return false; \
10243 }
10244
Daniel Vetter08a24032013-04-19 11:25:34 +020010245#define PIPE_CONF_CHECK_I(name) \
10246 if (current_config->name != pipe_config->name) { \
10247 DRM_ERROR("mismatch in " #name " " \
10248 "(expected %i, found %i)\n", \
10249 current_config->name, \
10250 pipe_config->name); \
10251 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010010252 }
10253
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010254/* This is required for BDW+ where there is only one set of registers for
10255 * switching between high and low RR.
10256 * This macro can be used whenever a comparison has to be made between one
10257 * hw state and multiple sw state variables.
10258 */
10259#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10260 if ((current_config->name != pipe_config->name) && \
10261 (current_config->alt_name != pipe_config->name)) { \
10262 DRM_ERROR("mismatch in " #name " " \
10263 "(expected %i or %i, found %i)\n", \
10264 current_config->name, \
10265 current_config->alt_name, \
10266 pipe_config->name); \
10267 return false; \
10268 }
10269
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010270#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10271 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070010272 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010273 "(expected %i, found %i)\n", \
10274 current_config->name & (mask), \
10275 pipe_config->name & (mask)); \
10276 return false; \
10277 }
10278
Ville Syrjälä5e550652013-09-06 23:29:07 +030010279#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10280 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10281 DRM_ERROR("mismatch in " #name " " \
10282 "(expected %i, found %i)\n", \
10283 current_config->name, \
10284 pipe_config->name); \
10285 return false; \
10286 }
10287
Daniel Vetterbb760062013-06-06 14:55:52 +020010288#define PIPE_CONF_QUIRK(quirk) \
10289 ((current_config->quirks | pipe_config->quirks) & (quirk))
10290
Daniel Vettereccb1402013-05-22 00:50:22 +020010291 PIPE_CONF_CHECK_I(cpu_transcoder);
10292
Daniel Vetter08a24032013-04-19 11:25:34 +020010293 PIPE_CONF_CHECK_I(has_pch_encoder);
10294 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020010295 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10296 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10297 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10298 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10299 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020010300
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010301 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010302
10303 if (INTEL_INFO(dev)->gen < 8) {
10304 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10305 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10306 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10307 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10308 PIPE_CONF_CHECK_I(dp_m_n.tu);
10309
10310 if (current_config->has_drrs) {
10311 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10312 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10313 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10314 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10315 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10316 }
10317 } else {
10318 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10319 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10320 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10321 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10322 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10323 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010324
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010325 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10326 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10327 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10328 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10329 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10330 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10331
10332 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10333 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10334 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10335 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10336 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10337 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10338
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010339 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020010340 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010341 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10342 IS_VALLEYVIEW(dev))
10343 PIPE_CONF_CHECK_I(limited_color_range);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010344
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010345 PIPE_CONF_CHECK_I(has_audio);
10346
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010347 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10348 DRM_MODE_FLAG_INTERLACE);
10349
Daniel Vetterbb760062013-06-06 14:55:52 +020010350 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10351 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10352 DRM_MODE_FLAG_PHSYNC);
10353 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10354 DRM_MODE_FLAG_NHSYNC);
10355 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10356 DRM_MODE_FLAG_PVSYNC);
10357 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10358 DRM_MODE_FLAG_NVSYNC);
10359 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010360
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010361 PIPE_CONF_CHECK_I(pipe_src_w);
10362 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010363
Daniel Vetter99535992014-04-13 12:00:33 +020010364 /*
10365 * FIXME: BIOS likes to set up a cloned config with lvds+external
10366 * screen. Since we don't yet re-compute the pipe config when moving
10367 * just the lvds port away to another pipe the sw tracking won't match.
10368 *
10369 * Proper atomic modesets with recomputed global state will fix this.
10370 * Until then just don't check gmch state for inherited modes.
10371 */
10372 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10373 PIPE_CONF_CHECK_I(gmch_pfit.control);
10374 /* pfit ratios are autocomputed by the hw on gen4+ */
10375 if (INTEL_INFO(dev)->gen < 4)
10376 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10377 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10378 }
10379
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010380 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10381 if (current_config->pch_pfit.enabled) {
10382 PIPE_CONF_CHECK_I(pch_pfit.pos);
10383 PIPE_CONF_CHECK_I(pch_pfit.size);
10384 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010385
Jesse Barnese59150d2014-01-07 13:30:45 -080010386 /* BDW+ don't expose a synchronous way to read the state */
10387 if (IS_HASWELL(dev))
10388 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010389
Ville Syrjälä282740f2013-09-04 18:30:03 +030010390 PIPE_CONF_CHECK_I(double_wide);
10391
Daniel Vetter26804af2014-06-25 22:01:55 +030010392 PIPE_CONF_CHECK_X(ddi_pll_sel);
10393
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010394 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010395 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010396 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010397 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10398 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010399 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010400
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010401 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10402 PIPE_CONF_CHECK_I(pipe_bpp);
10403
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010404 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10405 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010406
Daniel Vetter66e985c2013-06-05 13:34:20 +020010407#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010408#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010409#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010410#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010411#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010412#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010413
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010414 return true;
10415}
10416
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010417static void
10418check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010419{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010420 struct intel_connector *connector;
10421
10422 list_for_each_entry(connector, &dev->mode_config.connector_list,
10423 base.head) {
10424 /* This also checks the encoder/connector hw state with the
10425 * ->get_hw_state callbacks. */
10426 intel_connector_check_state(connector);
10427
10428 WARN(&connector->new_encoder->base != connector->base.encoder,
10429 "connector's staged encoder doesn't match current encoder\n");
10430 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010431}
10432
10433static void
10434check_encoder_state(struct drm_device *dev)
10435{
10436 struct intel_encoder *encoder;
10437 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010438
Damien Lespiaub2784e12014-08-05 11:29:37 +010010439 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010440 bool enabled = false;
10441 bool active = false;
10442 enum pipe pipe, tracked_pipe;
10443
10444 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10445 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030010446 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010447
10448 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10449 "encoder's stage crtc doesn't match current crtc\n");
10450 WARN(encoder->connectors_active && !encoder->base.crtc,
10451 "encoder's active_connectors set, but no crtc\n");
10452
10453 list_for_each_entry(connector, &dev->mode_config.connector_list,
10454 base.head) {
10455 if (connector->base.encoder != &encoder->base)
10456 continue;
10457 enabled = true;
10458 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10459 active = true;
10460 }
Dave Airlie0e32b392014-05-02 14:02:48 +100010461 /*
10462 * for MST connectors if we unplug the connector is gone
10463 * away but the encoder is still connected to a crtc
10464 * until a modeset happens in response to the hotplug.
10465 */
10466 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10467 continue;
10468
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010469 WARN(!!encoder->base.crtc != enabled,
10470 "encoder's enabled state mismatch "
10471 "(expected %i, found %i)\n",
10472 !!encoder->base.crtc, enabled);
10473 WARN(active && !encoder->base.crtc,
10474 "active encoder with no crtc\n");
10475
10476 WARN(encoder->connectors_active != active,
10477 "encoder's computed active state doesn't match tracked active state "
10478 "(expected %i, found %i)\n", active, encoder->connectors_active);
10479
10480 active = encoder->get_hw_state(encoder, &pipe);
10481 WARN(active != encoder->connectors_active,
10482 "encoder's hw state doesn't match sw tracking "
10483 "(expected %i, found %i)\n",
10484 encoder->connectors_active, active);
10485
10486 if (!encoder->base.crtc)
10487 continue;
10488
10489 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10490 WARN(active && pipe != tracked_pipe,
10491 "active encoder's pipe doesn't match"
10492 "(expected %i, found %i)\n",
10493 tracked_pipe, pipe);
10494
10495 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010496}
10497
10498static void
10499check_crtc_state(struct drm_device *dev)
10500{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010501 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010502 struct intel_crtc *crtc;
10503 struct intel_encoder *encoder;
10504 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010505
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010506 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010507 bool enabled = false;
10508 bool active = false;
10509
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010510 memset(&pipe_config, 0, sizeof(pipe_config));
10511
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010512 DRM_DEBUG_KMS("[CRTC:%d]\n",
10513 crtc->base.base.id);
10514
10515 WARN(crtc->active && !crtc->base.enabled,
10516 "active crtc, but not enabled in sw tracking\n");
10517
Damien Lespiaub2784e12014-08-05 11:29:37 +010010518 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010519 if (encoder->base.crtc != &crtc->base)
10520 continue;
10521 enabled = true;
10522 if (encoder->connectors_active)
10523 active = true;
10524 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010525
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010526 WARN(active != crtc->active,
10527 "crtc's computed active state doesn't match tracked active state "
10528 "(expected %i, found %i)\n", active, crtc->active);
10529 WARN(enabled != crtc->base.enabled,
10530 "crtc's computed enabled state doesn't match tracked enabled state "
10531 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10532
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010533 active = dev_priv->display.get_pipe_config(crtc,
10534 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010535
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030010536 /* hw state is inconsistent with the pipe quirk */
10537 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10538 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020010539 active = crtc->active;
10540
Damien Lespiaub2784e12014-08-05 11:29:37 +010010541 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010542 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010543 if (encoder->base.crtc != &crtc->base)
10544 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010545 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010546 encoder->get_config(encoder, &pipe_config);
10547 }
10548
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010549 WARN(crtc->active != active,
10550 "crtc active state doesn't match with hw state "
10551 "(expected %i, found %i)\n", crtc->active, active);
10552
Daniel Vetterc0b03412013-05-28 12:05:54 +020010553 if (active &&
10554 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10555 WARN(1, "pipe state doesn't match!\n");
10556 intel_dump_pipe_config(crtc, &pipe_config,
10557 "[hw state]");
10558 intel_dump_pipe_config(crtc, &crtc->config,
10559 "[sw state]");
10560 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010561 }
10562}
10563
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010564static void
10565check_shared_dpll_state(struct drm_device *dev)
10566{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010567 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010568 struct intel_crtc *crtc;
10569 struct intel_dpll_hw_state dpll_hw_state;
10570 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010571
10572 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10573 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10574 int enabled_crtcs = 0, active_crtcs = 0;
10575 bool active;
10576
10577 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10578
10579 DRM_DEBUG_KMS("%s\n", pll->name);
10580
10581 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10582
10583 WARN(pll->active > pll->refcount,
10584 "more active pll users than references: %i vs %i\n",
10585 pll->active, pll->refcount);
10586 WARN(pll->active && !pll->on,
10587 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +020010588 WARN(pll->on && !pll->active,
10589 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010590 WARN(pll->on != active,
10591 "pll on state mismatch (expected %i, found %i)\n",
10592 pll->on, active);
10593
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010594 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020010595 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10596 enabled_crtcs++;
10597 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10598 active_crtcs++;
10599 }
10600 WARN(pll->active != active_crtcs,
10601 "pll active crtcs mismatch (expected %i, found %i)\n",
10602 pll->active, active_crtcs);
10603 WARN(pll->refcount != enabled_crtcs,
10604 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10605 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010606
10607 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10608 sizeof(dpll_hw_state)),
10609 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010610 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010611}
10612
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010613void
10614intel_modeset_check_state(struct drm_device *dev)
10615{
10616 check_connector_state(dev);
10617 check_encoder_state(dev);
10618 check_crtc_state(dev);
10619 check_shared_dpll_state(dev);
10620}
10621
Ville Syrjälä18442d02013-09-13 16:00:08 +030010622void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10623 int dotclock)
10624{
10625 /*
10626 * FDI already provided one idea for the dotclock.
10627 * Yell if the encoder disagrees.
10628 */
Damien Lespiau241bfc32013-09-25 16:45:37 +010010629 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010630 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +010010631 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010632}
10633
Ville Syrjälä80715b22014-05-15 20:23:23 +030010634static void update_scanline_offset(struct intel_crtc *crtc)
10635{
10636 struct drm_device *dev = crtc->base.dev;
10637
10638 /*
10639 * The scanline counter increments at the leading edge of hsync.
10640 *
10641 * On most platforms it starts counting from vtotal-1 on the
10642 * first active line. That means the scanline counter value is
10643 * always one less than what we would expect. Ie. just after
10644 * start of vblank, which also occurs at start of hsync (on the
10645 * last active line), the scanline counter will read vblank_start-1.
10646 *
10647 * On gen2 the scanline counter starts counting from 1 instead
10648 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10649 * to keep the value positive), instead of adding one.
10650 *
10651 * On HSW+ the behaviour of the scanline counter depends on the output
10652 * type. For DP ports it behaves like most other platforms, but on HDMI
10653 * there's an extra 1 line difference. So we need to add two instead of
10654 * one to the value.
10655 */
10656 if (IS_GEN2(dev)) {
10657 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10658 int vtotal;
10659
10660 vtotal = mode->crtc_vtotal;
10661 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10662 vtotal /= 2;
10663
10664 crtc->scanline_offset = vtotal - 1;
10665 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030010666 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030010667 crtc->scanline_offset = 2;
10668 } else
10669 crtc->scanline_offset = 1;
10670}
10671
Daniel Vetterf30da182013-04-11 20:22:50 +020010672static int __intel_set_mode(struct drm_crtc *crtc,
10673 struct drm_display_mode *mode,
10674 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +020010675{
10676 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010677 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010678 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010679 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010680 struct intel_crtc *intel_crtc;
10681 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010682 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020010683
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010684 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010685 if (!saved_mode)
10686 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020010687
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010688 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +020010689 &prepare_pipes, &disable_pipes);
10690
Tim Gardner3ac18232012-12-07 07:54:26 -070010691 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010692
Daniel Vetter25c5b262012-07-08 22:08:04 +020010693 /* Hack: Because we don't (yet) support global modeset on multiple
10694 * crtcs, we don't keep track of the new mode for more than one crtc.
10695 * Hence simply check whether any bit is set in modeset_pipes in all the
10696 * pieces of code that are not yet converted to deal with mutliple crtcs
10697 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010698 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010699 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010700 if (IS_ERR(pipe_config)) {
10701 ret = PTR_ERR(pipe_config);
10702 pipe_config = NULL;
10703
Tim Gardner3ac18232012-12-07 07:54:26 -070010704 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010705 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010706 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10707 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010708 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +020010709 }
10710
Jesse Barnes30a970c2013-11-04 13:48:12 -080010711 /*
10712 * See if the config requires any additional preparation, e.g.
10713 * to adjust global state with pipes off. We need to do this
10714 * here so we can get the modeset_pipe updated config for the new
10715 * mode set on this crtc. For other crtcs we need to use the
10716 * adjusted_mode bits in the crtc directly.
10717 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020010718 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020010719 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080010720
Ville Syrjäläc164f832013-11-05 22:34:12 +020010721 /* may have added more to prepare_pipes than we should */
10722 prepare_pipes &= ~disable_pipes;
10723 }
10724
Daniel Vetter460da9162013-03-27 00:44:51 +010010725 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10726 intel_crtc_disable(&intel_crtc->base);
10727
Daniel Vetterea9d7582012-07-10 10:42:52 +020010728 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10729 if (intel_crtc->base.enabled)
10730 dev_priv->display.crtc_disable(&intel_crtc->base);
10731 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010732
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020010733 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10734 * to set it here already despite that we pass it down the callchain.
10735 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010736 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020010737 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010738 /* mode_set/enable/disable functions rely on a correct pipe
10739 * config. */
10740 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010741 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020010742
10743 /*
10744 * Calculate and store various constants which
10745 * are later needed by vblank and swap-completion
10746 * timestamping. They are derived from true hwmode.
10747 */
10748 drm_calc_timestamping_constants(crtc,
10749 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010750 }
Daniel Vetter7758a112012-07-08 19:40:39 +020010751
Daniel Vetterea9d7582012-07-10 10:42:52 +020010752 /* Only after disabling all output pipelines that will be changed can we
10753 * update the the output configuration. */
10754 intel_modeset_update_state(dev, prepare_pipes);
10755
Daniel Vetter47fab732012-10-26 10:58:18 +020010756 if (dev_priv->display.modeset_global_resources)
10757 dev_priv->display.modeset_global_resources(dev);
10758
Daniel Vettera6778b32012-07-02 09:56:42 +020010759 /* Set up the DPLL and any encoders state that needs to adjust or depend
10760 * on the DPLL.
10761 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010762 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070010763 struct drm_framebuffer *old_fb = crtc->primary->fb;
10764 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
10765 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Daniel Vetter4c107942014-04-24 23:55:05 +020010766
10767 mutex_lock(&dev->struct_mutex);
10768 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vettera071fa02014-06-18 23:28:09 +020010769 obj,
Daniel Vetter4c107942014-04-24 23:55:05 +020010770 NULL);
10771 if (ret != 0) {
10772 DRM_ERROR("pin & fence failed\n");
10773 mutex_unlock(&dev->struct_mutex);
10774 goto done;
10775 }
Matt Roper2ff8fde2014-07-08 07:50:07 -070010776 if (old_fb)
Daniel Vettera071fa02014-06-18 23:28:09 +020010777 intel_unpin_fb_obj(old_obj);
Daniel Vettera071fa02014-06-18 23:28:09 +020010778 i915_gem_track_fb(old_obj, obj,
10779 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Daniel Vetter4c107942014-04-24 23:55:05 +020010780 mutex_unlock(&dev->struct_mutex);
10781
10782 crtc->primary->fb = fb;
10783 crtc->x = x;
10784 crtc->y = y;
10785
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +030010786 ret = dev_priv->display.crtc_mode_set(intel_crtc, x, y, fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010787 if (ret)
10788 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +020010789 }
10790
10791 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030010792 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10793 update_scanline_offset(intel_crtc);
10794
Daniel Vetter25c5b262012-07-08 22:08:04 +020010795 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030010796 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010797
Daniel Vettera6778b32012-07-02 09:56:42 +020010798 /* FIXME: add subpixel order */
10799done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010800 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070010801 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010802
Tim Gardner3ac18232012-12-07 07:54:26 -070010803out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010804 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -070010805 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020010806 return ret;
10807}
10808
Damien Lespiaue7457a92013-08-08 22:28:59 +010010809static int intel_set_mode(struct drm_crtc *crtc,
10810 struct drm_display_mode *mode,
10811 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020010812{
10813 int ret;
10814
10815 ret = __intel_set_mode(crtc, mode, x, y, fb);
10816
10817 if (ret == 0)
10818 intel_modeset_check_state(crtc->dev);
10819
10820 return ret;
10821}
10822
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010823void intel_crtc_restore_mode(struct drm_crtc *crtc)
10824{
Matt Roperf4510a22014-04-01 15:22:40 -070010825 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010826}
10827
Daniel Vetter25c5b262012-07-08 22:08:04 +020010828#undef for_each_intel_crtc_masked
10829
Daniel Vetterd9e55602012-07-04 22:16:09 +020010830static void intel_set_config_free(struct intel_set_config *config)
10831{
10832 if (!config)
10833 return;
10834
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010835 kfree(config->save_connector_encoders);
10836 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020010837 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020010838 kfree(config);
10839}
10840
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010841static int intel_set_config_save_state(struct drm_device *dev,
10842 struct intel_set_config *config)
10843{
Ville Syrjälä76688512014-01-10 11:28:06 +020010844 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010845 struct drm_encoder *encoder;
10846 struct drm_connector *connector;
10847 int count;
10848
Ville Syrjälä76688512014-01-10 11:28:06 +020010849 config->save_crtc_enabled =
10850 kcalloc(dev->mode_config.num_crtc,
10851 sizeof(bool), GFP_KERNEL);
10852 if (!config->save_crtc_enabled)
10853 return -ENOMEM;
10854
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010855 config->save_encoder_crtcs =
10856 kcalloc(dev->mode_config.num_encoder,
10857 sizeof(struct drm_crtc *), GFP_KERNEL);
10858 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010859 return -ENOMEM;
10860
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010861 config->save_connector_encoders =
10862 kcalloc(dev->mode_config.num_connector,
10863 sizeof(struct drm_encoder *), GFP_KERNEL);
10864 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010865 return -ENOMEM;
10866
10867 /* Copy data. Note that driver private data is not affected.
10868 * Should anything bad happen only the expected state is
10869 * restored, not the drivers personal bookkeeping.
10870 */
10871 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010872 for_each_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010873 config->save_crtc_enabled[count++] = crtc->enabled;
10874 }
10875
10876 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010877 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010878 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010879 }
10880
10881 count = 0;
10882 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010883 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010884 }
10885
10886 return 0;
10887}
10888
10889static void intel_set_config_restore_state(struct drm_device *dev,
10890 struct intel_set_config *config)
10891{
Ville Syrjälä76688512014-01-10 11:28:06 +020010892 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010893 struct intel_encoder *encoder;
10894 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010895 int count;
10896
10897 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010898 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010899 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010900
10901 if (crtc->new_enabled)
10902 crtc->new_config = &crtc->config;
10903 else
10904 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010905 }
10906
10907 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010010908 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010909 encoder->new_crtc =
10910 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010911 }
10912
10913 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010914 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10915 connector->new_encoder =
10916 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010917 }
10918}
10919
Imre Deake3de42b2013-05-03 19:44:07 +020010920static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010010921is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020010922{
10923 int i;
10924
Chris Wilson2e57f472013-07-17 12:14:40 +010010925 if (set->num_connectors == 0)
10926 return false;
10927
10928 if (WARN_ON(set->connectors == NULL))
10929 return false;
10930
10931 for (i = 0; i < set->num_connectors; i++)
10932 if (set->connectors[i]->encoder &&
10933 set->connectors[i]->encoder->crtc == set->crtc &&
10934 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020010935 return true;
10936
10937 return false;
10938}
10939
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010940static void
10941intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10942 struct intel_set_config *config)
10943{
10944
10945 /* We should be able to check here if the fb has the same properties
10946 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010010947 if (is_crtc_connector_off(set)) {
10948 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070010949 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070010950 /*
10951 * If we have no fb, we can only flip as long as the crtc is
10952 * active, otherwise we need a full mode set. The crtc may
10953 * be active if we've only disabled the primary plane, or
10954 * in fastboot situations.
10955 */
Matt Roperf4510a22014-04-01 15:22:40 -070010956 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010957 struct intel_crtc *intel_crtc =
10958 to_intel_crtc(set->crtc);
10959
Matt Roper3b150f02014-05-29 08:06:53 -070010960 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010961 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10962 config->fb_changed = true;
10963 } else {
10964 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10965 config->mode_changed = true;
10966 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010967 } else if (set->fb == NULL) {
10968 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010010969 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070010970 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010971 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010972 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010973 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010974 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010975 }
10976
Daniel Vetter835c5872012-07-10 18:11:08 +020010977 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010978 config->fb_changed = true;
10979
10980 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10981 DRM_DEBUG_KMS("modes are different, full mode set\n");
10982 drm_mode_debug_printmodeline(&set->crtc->mode);
10983 drm_mode_debug_printmodeline(set->mode);
10984 config->mode_changed = true;
10985 }
Chris Wilsona1d95702013-08-13 18:48:47 +010010986
10987 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10988 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010989}
10990
Daniel Vetter2e431052012-07-04 22:42:15 +020010991static int
Daniel Vetter9a935852012-07-05 22:34:27 +020010992intel_modeset_stage_output_state(struct drm_device *dev,
10993 struct drm_mode_set *set,
10994 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020010995{
Daniel Vetter9a935852012-07-05 22:34:27 +020010996 struct intel_connector *connector;
10997 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020010998 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030010999 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020011000
Damien Lespiau9abdda72013-02-13 13:29:23 +000011001 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020011002 * of connectors. For paranoia, double-check this. */
11003 WARN_ON(!set->fb && (set->num_connectors != 0));
11004 WARN_ON(set->fb && (set->num_connectors == 0));
11005
Daniel Vetter9a935852012-07-05 22:34:27 +020011006 list_for_each_entry(connector, &dev->mode_config.connector_list,
11007 base.head) {
11008 /* Otherwise traverse passed in connector list and get encoders
11009 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020011010 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011011 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100011012 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020011013 break;
11014 }
11015 }
11016
Daniel Vetter9a935852012-07-05 22:34:27 +020011017 /* If we disable the crtc, disable all its connectors. Also, if
11018 * the connector is on the changing crtc but not on the new
11019 * connector list, disable it. */
11020 if ((!set->fb || ro == set->num_connectors) &&
11021 connector->base.encoder &&
11022 connector->base.encoder->crtc == set->crtc) {
11023 connector->new_encoder = NULL;
11024
11025 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11026 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011027 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020011028 }
11029
11030
11031 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011032 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011033 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011034 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011035 }
11036 /* connector->new_encoder is now updated for all connectors. */
11037
11038 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020011039 list_for_each_entry(connector, &dev->mode_config.connector_list,
11040 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011041 struct drm_crtc *new_crtc;
11042
Daniel Vetter9a935852012-07-05 22:34:27 +020011043 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020011044 continue;
11045
Daniel Vetter9a935852012-07-05 22:34:27 +020011046 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020011047
11048 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011049 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020011050 new_crtc = set->crtc;
11051 }
11052
11053 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010011054 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11055 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011056 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020011057 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011058 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020011059
11060 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11061 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011062 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020011063 new_crtc->base.id);
11064 }
11065
11066 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010011067 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011068 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011069 list_for_each_entry(connector,
11070 &dev->mode_config.connector_list,
11071 base.head) {
11072 if (connector->new_encoder == encoder) {
11073 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011074 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020011075 }
11076 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011077
11078 if (num_connectors == 0)
11079 encoder->new_crtc = NULL;
11080 else if (num_connectors > 1)
11081 return -EINVAL;
11082
Daniel Vetter9a935852012-07-05 22:34:27 +020011083 /* Only now check for crtc changes so we don't miss encoders
11084 * that will be disabled. */
11085 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011086 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011087 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011088 }
11089 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011090 /* Now we've also updated encoder->new_crtc for all encoders. */
Dave Airlie0e32b392014-05-02 14:02:48 +100011091 list_for_each_entry(connector, &dev->mode_config.connector_list,
11092 base.head) {
11093 if (connector->new_encoder)
11094 if (connector->new_encoder != connector->encoder)
11095 connector->encoder = connector->new_encoder;
11096 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011097 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011098 crtc->new_enabled = false;
11099
Damien Lespiaub2784e12014-08-05 11:29:37 +010011100 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011101 if (encoder->new_crtc == crtc) {
11102 crtc->new_enabled = true;
11103 break;
11104 }
11105 }
11106
11107 if (crtc->new_enabled != crtc->base.enabled) {
11108 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11109 crtc->new_enabled ? "en" : "dis");
11110 config->mode_changed = true;
11111 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011112
11113 if (crtc->new_enabled)
11114 crtc->new_config = &crtc->config;
11115 else
11116 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011117 }
11118
Daniel Vetter2e431052012-07-04 22:42:15 +020011119 return 0;
11120}
11121
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011122static void disable_crtc_nofb(struct intel_crtc *crtc)
11123{
11124 struct drm_device *dev = crtc->base.dev;
11125 struct intel_encoder *encoder;
11126 struct intel_connector *connector;
11127
11128 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11129 pipe_name(crtc->pipe));
11130
11131 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11132 if (connector->new_encoder &&
11133 connector->new_encoder->new_crtc == crtc)
11134 connector->new_encoder = NULL;
11135 }
11136
Damien Lespiaub2784e12014-08-05 11:29:37 +010011137 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011138 if (encoder->new_crtc == crtc)
11139 encoder->new_crtc = NULL;
11140 }
11141
11142 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011143 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011144}
11145
Daniel Vetter2e431052012-07-04 22:42:15 +020011146static int intel_crtc_set_config(struct drm_mode_set *set)
11147{
11148 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020011149 struct drm_mode_set save_set;
11150 struct intel_set_config *config;
11151 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020011152
Daniel Vetter8d3e3752012-07-05 16:09:09 +020011153 BUG_ON(!set);
11154 BUG_ON(!set->crtc);
11155 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020011156
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010011157 /* Enforce sane interface api - has been abused by the fb helper. */
11158 BUG_ON(!set->mode && set->fb);
11159 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020011160
Daniel Vetter2e431052012-07-04 22:42:15 +020011161 if (set->fb) {
11162 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11163 set->crtc->base.id, set->fb->base.id,
11164 (int)set->num_connectors, set->x, set->y);
11165 } else {
11166 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020011167 }
11168
11169 dev = set->crtc->dev;
11170
11171 ret = -ENOMEM;
11172 config = kzalloc(sizeof(*config), GFP_KERNEL);
11173 if (!config)
11174 goto out_config;
11175
11176 ret = intel_set_config_save_state(dev, config);
11177 if (ret)
11178 goto out_config;
11179
11180 save_set.crtc = set->crtc;
11181 save_set.mode = &set->crtc->mode;
11182 save_set.x = set->crtc->x;
11183 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070011184 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020011185
11186 /* Compute whether we need a full modeset, only an fb base update or no
11187 * change at all. In the future we might also check whether only the
11188 * mode changed, e.g. for LVDS where we only change the panel fitter in
11189 * such cases. */
11190 intel_set_config_compute_mode_changes(set, config);
11191
Daniel Vetter9a935852012-07-05 22:34:27 +020011192 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020011193 if (ret)
11194 goto fail;
11195
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011196 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011197 ret = intel_set_mode(set->crtc, set->mode,
11198 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011199 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070011200 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11201
Ville Syrjälä4878cae2013-02-18 19:08:48 +020011202 intel_crtc_wait_for_pending_flips(set->crtc);
11203
Daniel Vetter4f660f42012-07-02 09:47:37 +020011204 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020011205 set->x, set->y, set->fb);
Matt Roper3b150f02014-05-29 08:06:53 -070011206
11207 /*
11208 * We need to make sure the primary plane is re-enabled if it
11209 * has previously been turned off.
11210 */
11211 if (!intel_crtc->primary_enabled && ret == 0) {
11212 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030011213 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070011214 }
11215
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011216 /*
11217 * In the fastboot case this may be our only check of the
11218 * state after boot. It would be better to only do it on
11219 * the first update, but we don't have a nice way of doing that
11220 * (and really, set_config isn't used much for high freq page
11221 * flipping, so increasing its cost here shouldn't be a big
11222 * deal).
11223 */
Jani Nikulad330a952014-01-21 11:24:25 +020011224 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011225 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020011226 }
11227
Chris Wilson2d05eae2013-05-03 17:36:25 +010011228 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020011229 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11230 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020011231fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010011232 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011233
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011234 /*
11235 * HACK: if the pipe was on, but we didn't have a framebuffer,
11236 * force the pipe off to avoid oopsing in the modeset code
11237 * due to fb==NULL. This should only happen during boot since
11238 * we don't yet reconstruct the FB from the hardware state.
11239 */
11240 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11241 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11242
Chris Wilson2d05eae2013-05-03 17:36:25 +010011243 /* Try to restore the config */
11244 if (config->mode_changed &&
11245 intel_set_mode(save_set.crtc, save_set.mode,
11246 save_set.x, save_set.y, save_set.fb))
11247 DRM_ERROR("failed to restore config after modeset failure\n");
11248 }
Daniel Vetter50f56112012-07-02 09:35:43 +020011249
Daniel Vetterd9e55602012-07-04 22:16:09 +020011250out_config:
11251 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011252 return ret;
11253}
11254
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011255static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011256 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020011257 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011258 .destroy = intel_crtc_destroy,
11259 .page_flip = intel_crtc_page_flip,
11260};
11261
Daniel Vetter53589012013-06-05 13:34:16 +020011262static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11263 struct intel_shared_dpll *pll,
11264 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011265{
Daniel Vetter53589012013-06-05 13:34:16 +020011266 uint32_t val;
11267
Daniel Vetterf458ebb2014-09-30 10:56:39 +020011268 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030011269 return false;
11270
Daniel Vetter53589012013-06-05 13:34:16 +020011271 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020011272 hw_state->dpll = val;
11273 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11274 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020011275
11276 return val & DPLL_VCO_ENABLE;
11277}
11278
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011279static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11280 struct intel_shared_dpll *pll)
11281{
11282 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11283 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11284}
11285
Daniel Vettere7b903d2013-06-05 13:34:14 +020011286static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11287 struct intel_shared_dpll *pll)
11288{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011289 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020011290 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020011291
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011292 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11293
11294 /* Wait for the clocks to stabilize. */
11295 POSTING_READ(PCH_DPLL(pll->id));
11296 udelay(150);
11297
11298 /* The pixel multiplier can only be updated once the
11299 * DPLL is enabled and the clocks are stable.
11300 *
11301 * So write it again.
11302 */
11303 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11304 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011305 udelay(200);
11306}
11307
11308static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11309 struct intel_shared_dpll *pll)
11310{
11311 struct drm_device *dev = dev_priv->dev;
11312 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011313
11314 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011315 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020011316 if (intel_crtc_to_shared_dpll(crtc) == pll)
11317 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
11318 }
11319
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011320 I915_WRITE(PCH_DPLL(pll->id), 0);
11321 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011322 udelay(200);
11323}
11324
Daniel Vetter46edb022013-06-05 13:34:12 +020011325static char *ibx_pch_dpll_names[] = {
11326 "PCH DPLL A",
11327 "PCH DPLL B",
11328};
11329
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011330static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011331{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011332 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011333 int i;
11334
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011335 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011336
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011337 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011338 dev_priv->shared_dplls[i].id = i;
11339 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011340 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011341 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11342 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011343 dev_priv->shared_dplls[i].get_hw_state =
11344 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011345 }
11346}
11347
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011348static void intel_shared_dpll_init(struct drm_device *dev)
11349{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011350 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011351
Daniel Vetter9cd86932014-06-25 22:01:57 +030011352 if (HAS_DDI(dev))
11353 intel_ddi_pll_init(dev);
11354 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011355 ibx_pch_dpll_init(dev);
11356 else
11357 dev_priv->num_shared_dpll = 0;
11358
11359 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011360}
11361
Matt Roper465c1202014-05-29 08:06:54 -070011362static int
11363intel_primary_plane_disable(struct drm_plane *plane)
11364{
11365 struct drm_device *dev = plane->dev;
Matt Roper465c1202014-05-29 08:06:54 -070011366 struct intel_crtc *intel_crtc;
11367
11368 if (!plane->fb)
11369 return 0;
11370
11371 BUG_ON(!plane->crtc);
11372
11373 intel_crtc = to_intel_crtc(plane->crtc);
11374
11375 /*
11376 * Even though we checked plane->fb above, it's still possible that
11377 * the primary plane has been implicitly disabled because the crtc
11378 * coordinates given weren't visible, or because we detected
11379 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11380 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11381 * In either case, we need to unpin the FB and let the fb pointer get
11382 * updated, but otherwise we don't need to touch the hardware.
11383 */
11384 if (!intel_crtc->primary_enabled)
11385 goto disable_unpin;
11386
11387 intel_crtc_wait_for_pending_flips(plane->crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030011388 intel_disable_primary_hw_plane(plane, plane->crtc);
11389
Matt Roper465c1202014-05-29 08:06:54 -070011390disable_unpin:
Matt Roper4c345742014-07-09 16:22:10 -070011391 mutex_lock(&dev->struct_mutex);
Matt Roper2ff8fde2014-07-08 07:50:07 -070011392 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
Daniel Vettera071fa02014-06-18 23:28:09 +020011393 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Matt Roper2ff8fde2014-07-08 07:50:07 -070011394 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
Matt Roper4c345742014-07-09 16:22:10 -070011395 mutex_unlock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070011396 plane->fb = NULL;
11397
11398 return 0;
11399}
11400
11401static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011402intel_check_primary_plane(struct drm_plane *plane,
11403 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070011404{
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011405 struct drm_crtc *crtc = state->crtc;
11406 struct drm_framebuffer *fb = state->fb;
11407 struct drm_rect *dest = &state->dst;
11408 struct drm_rect *src = &state->src;
11409 const struct drm_rect *clip = &state->clip;
11410
Gustavo Padovan3ead8bb2014-10-24 19:00:18 +010011411 return drm_plane_helper_check_update(plane, crtc, fb,
11412 src, dest, clip,
11413 DRM_PLANE_HELPER_NO_SCALING,
11414 DRM_PLANE_HELPER_NO_SCALING,
11415 false, true, &state->visible);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011416}
11417
11418static int
Gustavo Padovan14af2932014-10-24 14:51:31 +010011419intel_prepare_primary_plane(struct drm_plane *plane,
11420 struct intel_plane_state *state)
11421{
11422 struct drm_crtc *crtc = state->crtc;
11423 struct drm_framebuffer *fb = state->fb;
11424 struct drm_device *dev = crtc->dev;
11425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11426 enum pipe pipe = intel_crtc->pipe;
11427 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11428 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
11429 int ret;
11430
11431 intel_crtc_wait_for_pending_flips(crtc);
11432
11433 if (intel_crtc_has_pending_flip(crtc)) {
11434 DRM_ERROR("pipe is still busy with an old pageflip\n");
11435 return -EBUSY;
11436 }
11437
11438 if (old_obj != obj) {
11439 mutex_lock(&dev->struct_mutex);
11440 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
11441 if (ret == 0)
11442 i915_gem_track_fb(old_obj, obj,
11443 INTEL_FRONTBUFFER_PRIMARY(pipe));
11444 mutex_unlock(&dev->struct_mutex);
11445 if (ret != 0) {
11446 DRM_DEBUG_KMS("pin & fence failed\n");
11447 return ret;
11448 }
11449 }
11450
11451 return 0;
11452}
11453
11454static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011455intel_commit_primary_plane(struct drm_plane *plane,
11456 struct intel_plane_state *state)
11457{
11458 struct drm_crtc *crtc = state->crtc;
11459 struct drm_framebuffer *fb = state->fb;
Matt Roper465c1202014-05-29 08:06:54 -070011460 struct drm_device *dev = crtc->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053011461 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper465c1202014-05-29 08:06:54 -070011462 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011463 enum pipe pipe = intel_crtc->pipe;
11464 struct drm_framebuffer *old_fb = plane->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011465 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11466 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Sonika Jindalce54d852014-08-21 11:44:39 +053011467 struct intel_plane *intel_plane = to_intel_plane(plane);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011468 struct drm_rect *src = &state->src;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011469
11470 crtc->primary->fb = fb;
11471 crtc->x = src->x1;
11472 crtc->y = src->y1;
11473
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011474 intel_plane->crtc_x = state->orig_dst.x1;
11475 intel_plane->crtc_y = state->orig_dst.y1;
11476 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
11477 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
11478 intel_plane->src_x = state->orig_src.x1;
11479 intel_plane->src_y = state->orig_src.y1;
11480 intel_plane->src_w = drm_rect_width(&state->orig_src);
11481 intel_plane->src_h = drm_rect_height(&state->orig_src);
Sonika Jindalce54d852014-08-21 11:44:39 +053011482 intel_plane->obj = obj;
Matt Roper465c1202014-05-29 08:06:54 -070011483
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011484 if (intel_crtc->active) {
11485 /*
11486 * FBC does not work on some platforms for rotated
11487 * planes, so disable it when rotation is not 0 and
11488 * update it when rotation is set back to 0.
11489 *
11490 * FIXME: This is redundant with the fbc update done in
11491 * the primary plane enable function except that that
11492 * one is done too late. We eventually need to unify
11493 * this.
11494 */
11495 if (intel_crtc->primary_enabled &&
11496 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11497 dev_priv->fbc.plane == intel_crtc->plane &&
11498 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11499 intel_disable_fbc(dev);
11500 }
11501
11502 if (state->visible) {
11503 bool was_enabled = intel_crtc->primary_enabled;
11504
11505 /* FIXME: kill this fastboot hack */
11506 intel_update_pipe_size(intel_crtc);
11507
11508 intel_crtc->primary_enabled = true;
11509
11510 dev_priv->display.update_primary_plane(crtc, plane->fb,
11511 crtc->x, crtc->y);
11512
11513 /*
11514 * BDW signals flip done immediately if the plane
11515 * is disabled, even if the plane enable is already
11516 * armed to occur at the next vblank :(
11517 */
11518 if (IS_BROADWELL(dev) && !was_enabled)
11519 intel_wait_for_vblank(dev, intel_crtc->pipe);
11520 } else {
11521 /*
11522 * If clipping results in a non-visible primary plane,
11523 * we'll disable the primary plane. Note that this is
11524 * a bit different than what happens if userspace
11525 * explicitly disables the plane by passing fb=0
11526 * because plane->fb still gets set and pinned.
11527 */
11528 intel_disable_primary_hw_plane(plane, crtc);
11529 }
11530
11531 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
11532
11533 mutex_lock(&dev->struct_mutex);
11534 intel_update_fbc(dev);
11535 mutex_unlock(&dev->struct_mutex);
11536 }
11537
11538 if (old_fb && old_fb != fb) {
11539 if (intel_crtc->active)
11540 intel_wait_for_vblank(dev, intel_crtc->pipe);
11541
11542 mutex_lock(&dev->struct_mutex);
11543 intel_unpin_fb_obj(old_obj);
11544 mutex_unlock(&dev->struct_mutex);
11545 }
Matt Roper465c1202014-05-29 08:06:54 -070011546}
11547
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011548static int
11549intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11550 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11551 unsigned int crtc_w, unsigned int crtc_h,
11552 uint32_t src_x, uint32_t src_y,
11553 uint32_t src_w, uint32_t src_h)
11554{
11555 struct intel_plane_state state;
11556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11557 int ret;
11558
11559 state.crtc = crtc;
11560 state.fb = fb;
11561
11562 /* sample coordinates in 16.16 fixed point */
11563 state.src.x1 = src_x;
11564 state.src.x2 = src_x + src_w;
11565 state.src.y1 = src_y;
11566 state.src.y2 = src_y + src_h;
11567
11568 /* integer pixels */
11569 state.dst.x1 = crtc_x;
11570 state.dst.x2 = crtc_x + crtc_w;
11571 state.dst.y1 = crtc_y;
11572 state.dst.y2 = crtc_y + crtc_h;
11573
11574 state.clip.x1 = 0;
11575 state.clip.y1 = 0;
11576 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11577 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11578
11579 state.orig_src = state.src;
11580 state.orig_dst = state.dst;
11581
11582 ret = intel_check_primary_plane(plane, &state);
11583 if (ret)
11584 return ret;
11585
Gustavo Padovan14af2932014-10-24 14:51:31 +010011586 ret = intel_prepare_primary_plane(plane, &state);
11587 if (ret)
11588 return ret;
11589
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011590 intel_commit_primary_plane(plane, &state);
11591
11592 return 0;
11593}
11594
Matt Roper3d7d6512014-06-10 08:28:13 -070011595/* Common destruction function for both primary and cursor planes */
11596static void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070011597{
11598 struct intel_plane *intel_plane = to_intel_plane(plane);
11599 drm_plane_cleanup(plane);
11600 kfree(intel_plane);
11601}
11602
11603static const struct drm_plane_funcs intel_primary_plane_funcs = {
11604 .update_plane = intel_primary_plane_setplane,
11605 .disable_plane = intel_primary_plane_disable,
Matt Roper3d7d6512014-06-10 08:28:13 -070011606 .destroy = intel_plane_destroy,
Sonika Jindal48404c12014-08-22 14:06:04 +053011607 .set_property = intel_plane_set_property
Matt Roper465c1202014-05-29 08:06:54 -070011608};
11609
11610static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11611 int pipe)
11612{
11613 struct intel_plane *primary;
11614 const uint32_t *intel_primary_formats;
11615 int num_formats;
11616
11617 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11618 if (primary == NULL)
11619 return NULL;
11620
11621 primary->can_scale = false;
11622 primary->max_downscale = 1;
11623 primary->pipe = pipe;
11624 primary->plane = pipe;
Sonika Jindal48404c12014-08-22 14:06:04 +053011625 primary->rotation = BIT(DRM_ROTATE_0);
Matt Roper465c1202014-05-29 08:06:54 -070011626 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11627 primary->plane = !pipe;
11628
11629 if (INTEL_INFO(dev)->gen <= 3) {
11630 intel_primary_formats = intel_primary_formats_gen2;
11631 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11632 } else {
11633 intel_primary_formats = intel_primary_formats_gen4;
11634 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11635 }
11636
11637 drm_universal_plane_init(dev, &primary->base, 0,
11638 &intel_primary_plane_funcs,
11639 intel_primary_formats, num_formats,
11640 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053011641
11642 if (INTEL_INFO(dev)->gen >= 4) {
11643 if (!dev->mode_config.rotation_property)
11644 dev->mode_config.rotation_property =
11645 drm_mode_create_rotation_property(dev,
11646 BIT(DRM_ROTATE_0) |
11647 BIT(DRM_ROTATE_180));
11648 if (dev->mode_config.rotation_property)
11649 drm_object_attach_property(&primary->base.base,
11650 dev->mode_config.rotation_property,
11651 primary->rotation);
11652 }
11653
Matt Roper465c1202014-05-29 08:06:54 -070011654 return &primary->base;
11655}
11656
Matt Roper3d7d6512014-06-10 08:28:13 -070011657static int
11658intel_cursor_plane_disable(struct drm_plane *plane)
11659{
11660 if (!plane->fb)
11661 return 0;
11662
11663 BUG_ON(!plane->crtc);
11664
11665 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11666}
11667
11668static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030011669intel_check_cursor_plane(struct drm_plane *plane,
11670 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070011671{
Gustavo Padovan852e7872014-09-05 17:22:31 -030011672 struct drm_crtc *crtc = state->crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011673 struct drm_device *dev = crtc->dev;
Gustavo Padovan852e7872014-09-05 17:22:31 -030011674 struct drm_framebuffer *fb = state->fb;
11675 struct drm_rect *dest = &state->dst;
11676 struct drm_rect *src = &state->src;
11677 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011678 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11679 int crtc_w, crtc_h;
11680 unsigned stride;
11681 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030011682
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011683 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030011684 src, dest, clip,
11685 DRM_PLANE_HELPER_NO_SCALING,
11686 DRM_PLANE_HELPER_NO_SCALING,
11687 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011688 if (ret)
11689 return ret;
11690
11691
11692 /* if we want to turn off the cursor ignore width and height */
11693 if (!obj)
11694 return 0;
11695
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011696 /* Check for which cursor types we support */
11697 crtc_w = drm_rect_width(&state->orig_dst);
11698 crtc_h = drm_rect_height(&state->orig_dst);
11699 if (!cursor_size_ok(dev, crtc_w, crtc_h)) {
11700 DRM_DEBUG("Cursor dimension not supported\n");
11701 return -EINVAL;
11702 }
11703
11704 stride = roundup_pow_of_two(crtc_w) * 4;
11705 if (obj->base.size < stride * crtc_h) {
11706 DRM_DEBUG_KMS("buffer is too small\n");
11707 return -ENOMEM;
11708 }
11709
Gustavo Padovane391ea82014-09-24 14:20:25 -030011710 if (fb == crtc->cursor->fb)
11711 return 0;
11712
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011713 /* we only need to pin inside GTT if cursor is non-phy */
11714 mutex_lock(&dev->struct_mutex);
11715 if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
11716 DRM_DEBUG_KMS("cursor cannot be tiled\n");
11717 ret = -EINVAL;
11718 }
11719 mutex_unlock(&dev->struct_mutex);
11720
11721 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030011722}
11723
11724static int
11725intel_commit_cursor_plane(struct drm_plane *plane,
11726 struct intel_plane_state *state)
11727{
11728 struct drm_crtc *crtc = state->crtc;
11729 struct drm_framebuffer *fb = state->fb;
Matt Roper3d7d6512014-06-10 08:28:13 -070011730 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070011731 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper3d7d6512014-06-10 08:28:13 -070011732 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11733 struct drm_i915_gem_object *obj = intel_fb->obj;
Gustavo Padovan852e7872014-09-05 17:22:31 -030011734 int crtc_w, crtc_h;
Matt Roper3d7d6512014-06-10 08:28:13 -070011735
Gustavo Padovan852e7872014-09-05 17:22:31 -030011736 crtc->cursor_x = state->orig_dst.x1;
11737 crtc->cursor_y = state->orig_dst.y1;
Sonika Jindala919db92014-10-23 07:41:33 -070011738
11739 intel_plane->crtc_x = state->orig_dst.x1;
11740 intel_plane->crtc_y = state->orig_dst.y1;
11741 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
11742 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
11743 intel_plane->src_x = state->orig_src.x1;
11744 intel_plane->src_y = state->orig_src.y1;
11745 intel_plane->src_w = drm_rect_width(&state->orig_src);
11746 intel_plane->src_h = drm_rect_height(&state->orig_src);
11747 intel_plane->obj = obj;
11748
Matt Roper3d7d6512014-06-10 08:28:13 -070011749 if (fb != crtc->cursor->fb) {
Gustavo Padovan852e7872014-09-05 17:22:31 -030011750 crtc_w = drm_rect_width(&state->orig_dst);
11751 crtc_h = drm_rect_height(&state->orig_dst);
Matt Roper3d7d6512014-06-10 08:28:13 -070011752 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
11753 } else {
Gustavo Padovan852e7872014-09-05 17:22:31 -030011754 intel_crtc_update_cursor(crtc, state->visible);
Daniel Vetter4ed91092014-08-08 20:27:01 +020011755
11756 intel_frontbuffer_flip(crtc->dev,
11757 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
11758
Matt Roper3d7d6512014-06-10 08:28:13 -070011759 return 0;
11760 }
11761}
Gustavo Padovan852e7872014-09-05 17:22:31 -030011762
11763static int
11764intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11765 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11766 unsigned int crtc_w, unsigned int crtc_h,
11767 uint32_t src_x, uint32_t src_y,
11768 uint32_t src_w, uint32_t src_h)
11769{
11770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11771 struct intel_plane_state state;
11772 int ret;
11773
11774 state.crtc = crtc;
11775 state.fb = fb;
11776
11777 /* sample coordinates in 16.16 fixed point */
11778 state.src.x1 = src_x;
11779 state.src.x2 = src_x + src_w;
11780 state.src.y1 = src_y;
11781 state.src.y2 = src_y + src_h;
11782
11783 /* integer pixels */
11784 state.dst.x1 = crtc_x;
11785 state.dst.x2 = crtc_x + crtc_w;
11786 state.dst.y1 = crtc_y;
11787 state.dst.y2 = crtc_y + crtc_h;
11788
11789 state.clip.x1 = 0;
11790 state.clip.y1 = 0;
11791 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11792 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11793
11794 state.orig_src = state.src;
11795 state.orig_dst = state.dst;
11796
11797 ret = intel_check_cursor_plane(plane, &state);
11798 if (ret)
11799 return ret;
11800
11801 return intel_commit_cursor_plane(plane, &state);
11802}
11803
Matt Roper3d7d6512014-06-10 08:28:13 -070011804static const struct drm_plane_funcs intel_cursor_plane_funcs = {
11805 .update_plane = intel_cursor_plane_update,
11806 .disable_plane = intel_cursor_plane_disable,
11807 .destroy = intel_plane_destroy,
Ville Syrjälä4398ad42014-10-23 07:41:34 -070011808 .set_property = intel_plane_set_property,
Matt Roper3d7d6512014-06-10 08:28:13 -070011809};
11810
11811static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
11812 int pipe)
11813{
11814 struct intel_plane *cursor;
11815
11816 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
11817 if (cursor == NULL)
11818 return NULL;
11819
11820 cursor->can_scale = false;
11821 cursor->max_downscale = 1;
11822 cursor->pipe = pipe;
11823 cursor->plane = pipe;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070011824 cursor->rotation = BIT(DRM_ROTATE_0);
Matt Roper3d7d6512014-06-10 08:28:13 -070011825
11826 drm_universal_plane_init(dev, &cursor->base, 0,
11827 &intel_cursor_plane_funcs,
11828 intel_cursor_formats,
11829 ARRAY_SIZE(intel_cursor_formats),
11830 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070011831
11832 if (INTEL_INFO(dev)->gen >= 4) {
11833 if (!dev->mode_config.rotation_property)
11834 dev->mode_config.rotation_property =
11835 drm_mode_create_rotation_property(dev,
11836 BIT(DRM_ROTATE_0) |
11837 BIT(DRM_ROTATE_180));
11838 if (dev->mode_config.rotation_property)
11839 drm_object_attach_property(&cursor->base.base,
11840 dev->mode_config.rotation_property,
11841 cursor->rotation);
11842 }
11843
Matt Roper3d7d6512014-06-10 08:28:13 -070011844 return &cursor->base;
11845}
11846
Hannes Ederb358d0a2008-12-18 21:18:47 +010011847static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080011848{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011849 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080011850 struct intel_crtc *intel_crtc;
Matt Roper3d7d6512014-06-10 08:28:13 -070011851 struct drm_plane *primary = NULL;
11852 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070011853 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011854
Daniel Vetter955382f2013-09-19 14:05:45 +020011855 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080011856 if (intel_crtc == NULL)
11857 return;
11858
Matt Roper465c1202014-05-29 08:06:54 -070011859 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070011860 if (!primary)
11861 goto fail;
11862
11863 cursor = intel_cursor_plane_create(dev, pipe);
11864 if (!cursor)
11865 goto fail;
11866
Matt Roper465c1202014-05-29 08:06:54 -070011867 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070011868 cursor, &intel_crtc_funcs);
11869 if (ret)
11870 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011871
11872 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080011873 for (i = 0; i < 256; i++) {
11874 intel_crtc->lut_r[i] = i;
11875 intel_crtc->lut_g[i] = i;
11876 intel_crtc->lut_b[i] = i;
11877 }
11878
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011879 /*
11880 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020011881 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011882 */
Jesse Barnes80824002009-09-10 15:28:06 -070011883 intel_crtc->pipe = pipe;
11884 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010011885 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080011886 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010011887 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070011888 }
11889
Chris Wilson4b0e3332014-05-30 16:35:26 +030011890 intel_crtc->cursor_base = ~0;
11891 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030011892 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030011893
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080011894 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11895 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11896 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11897 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11898
Jesse Barnes79e53942008-11-07 14:24:08 -080011899 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020011900
11901 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070011902 return;
11903
11904fail:
11905 if (primary)
11906 drm_plane_cleanup(primary);
11907 if (cursor)
11908 drm_plane_cleanup(cursor);
11909 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080011910}
11911
Jesse Barnes752aa882013-10-31 18:55:49 +020011912enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11913{
11914 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011915 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020011916
Rob Clark51fd3712013-11-19 12:10:12 -050011917 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020011918
11919 if (!encoder)
11920 return INVALID_PIPE;
11921
11922 return to_intel_crtc(encoder->crtc)->pipe;
11923}
11924
Carl Worth08d7b3d2009-04-29 14:43:54 -070011925int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000011926 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070011927{
Carl Worth08d7b3d2009-04-29 14:43:54 -070011928 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040011929 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020011930 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011931
Daniel Vetter1cff8f62012-04-24 09:55:08 +020011932 if (!drm_core_check_feature(dev, DRIVER_MODESET))
11933 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011934
Rob Clark7707e652014-07-17 23:30:04 -040011935 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070011936
Rob Clark7707e652014-07-17 23:30:04 -040011937 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070011938 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030011939 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011940 }
11941
Rob Clark7707e652014-07-17 23:30:04 -040011942 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020011943 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011944
Daniel Vetterc05422d2009-08-11 16:05:30 +020011945 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011946}
11947
Daniel Vetter66a92782012-07-12 20:08:18 +020011948static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080011949{
Daniel Vetter66a92782012-07-12 20:08:18 +020011950 struct drm_device *dev = encoder->base.dev;
11951 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011952 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011953 int entry = 0;
11954
Damien Lespiaub2784e12014-08-05 11:29:37 +010011955 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011956 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020011957 index_mask |= (1 << entry);
11958
Jesse Barnes79e53942008-11-07 14:24:08 -080011959 entry++;
11960 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010011961
Jesse Barnes79e53942008-11-07 14:24:08 -080011962 return index_mask;
11963}
11964
Chris Wilson4d302442010-12-14 19:21:29 +000011965static bool has_edp_a(struct drm_device *dev)
11966{
11967 struct drm_i915_private *dev_priv = dev->dev_private;
11968
11969 if (!IS_MOBILE(dev))
11970 return false;
11971
11972 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
11973 return false;
11974
Damien Lespiaue3589902014-02-07 19:12:50 +000011975 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000011976 return false;
11977
11978 return true;
11979}
11980
Damien Lespiauba0fbca2014-01-08 14:18:23 +000011981const char *intel_output_name(int output)
11982{
11983 static const char *names[] = {
11984 [INTEL_OUTPUT_UNUSED] = "Unused",
11985 [INTEL_OUTPUT_ANALOG] = "Analog",
11986 [INTEL_OUTPUT_DVO] = "DVO",
11987 [INTEL_OUTPUT_SDVO] = "SDVO",
11988 [INTEL_OUTPUT_LVDS] = "LVDS",
11989 [INTEL_OUTPUT_TVOUT] = "TV",
11990 [INTEL_OUTPUT_HDMI] = "HDMI",
11991 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
11992 [INTEL_OUTPUT_EDP] = "eDP",
11993 [INTEL_OUTPUT_DSI] = "DSI",
11994 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
11995 };
11996
11997 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
11998 return "Invalid";
11999
12000 return names[output];
12001}
12002
Jesse Barnes84b4e042014-06-25 08:24:29 -070012003static bool intel_crt_present(struct drm_device *dev)
12004{
12005 struct drm_i915_private *dev_priv = dev->dev_private;
12006
Damien Lespiau884497e2013-12-03 13:56:23 +000012007 if (INTEL_INFO(dev)->gen >= 9)
12008 return false;
12009
Damien Lespiaucf404ce2014-10-01 20:04:15 +010012010 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070012011 return false;
12012
12013 if (IS_CHERRYVIEW(dev))
12014 return false;
12015
12016 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
12017 return false;
12018
12019 return true;
12020}
12021
Jesse Barnes79e53942008-11-07 14:24:08 -080012022static void intel_setup_outputs(struct drm_device *dev)
12023{
Eric Anholt725e30a2009-01-22 13:01:02 -080012024 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010012025 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012026 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080012027
Daniel Vetterc9093352013-06-06 22:22:47 +020012028 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012029
Jesse Barnes84b4e042014-06-25 08:24:29 -070012030 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020012031 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012032
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012033 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030012034 int found;
12035
12036 /* Haswell uses DDI functions to detect digital outputs */
12037 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12038 /* DDI A only supports eDP */
12039 if (found)
12040 intel_ddi_init(dev, PORT_A);
12041
12042 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12043 * register */
12044 found = I915_READ(SFUSE_STRAP);
12045
12046 if (found & SFUSE_STRAP_DDIB_DETECTED)
12047 intel_ddi_init(dev, PORT_B);
12048 if (found & SFUSE_STRAP_DDIC_DETECTED)
12049 intel_ddi_init(dev, PORT_C);
12050 if (found & SFUSE_STRAP_DDID_DETECTED)
12051 intel_ddi_init(dev, PORT_D);
12052 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012053 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020012054 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020012055
12056 if (has_edp_a(dev))
12057 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012058
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012059 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080012060 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010012061 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012062 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012063 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012064 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012065 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012066 }
12067
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012068 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012069 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012070
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012071 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012072 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012073
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012074 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012075 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012076
Daniel Vetter270b3042012-10-27 15:52:05 +020012077 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012078 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070012079 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012080 /*
12081 * The DP_DETECTED bit is the latched state of the DDC
12082 * SDA pin at boot. However since eDP doesn't require DDC
12083 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12084 * eDP ports may have been muxed to an alternate function.
12085 * Thus we can't rely on the DP_DETECTED bit alone to detect
12086 * eDP ports. Consult the VBT as well as DP_DETECTED to
12087 * detect eDP ports.
12088 */
12089 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012090 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12091 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012092 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12093 intel_dp_is_edp(dev, PORT_B))
12094 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012095
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012096 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
Jesse Barnes6f6005a2013-08-09 09:34:35 -070012097 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12098 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012099 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12100 intel_dp_is_edp(dev, PORT_C))
12101 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053012102
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012103 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012104 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012105 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12106 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012107 /* eDP not supported on port D, so don't check VBT */
12108 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12109 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012110 }
12111
Jani Nikula3cfca972013-08-27 15:12:26 +030012112 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080012113 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012114 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080012115
Paulo Zanonie2debe92013-02-18 19:00:27 -030012116 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012117 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012118 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012119 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12120 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012121 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012122 }
Ma Ling27185ae2009-08-24 13:50:23 +080012123
Imre Deake7281ea2013-05-08 13:14:08 +030012124 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012125 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080012126 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012127
12128 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012129
Paulo Zanonie2debe92013-02-18 19:00:27 -030012130 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012131 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012132 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012133 }
Ma Ling27185ae2009-08-24 13:50:23 +080012134
Paulo Zanonie2debe92013-02-18 19:00:27 -030012135 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012136
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012137 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12138 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012139 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012140 }
Imre Deake7281ea2013-05-08 13:14:08 +030012141 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012142 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080012143 }
Ma Ling27185ae2009-08-24 13:50:23 +080012144
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012145 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030012146 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012147 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070012148 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012149 intel_dvo_init(dev);
12150
Zhenyu Wang103a1962009-11-27 11:44:36 +080012151 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012152 intel_tv_init(dev);
12153
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070012154 intel_edp_psr_init(dev);
12155
Damien Lespiaub2784e12014-08-05 11:29:37 +010012156 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010012157 encoder->base.possible_crtcs = encoder->crtc_mask;
12158 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020012159 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080012160 }
Chris Wilson47356eb2011-01-11 17:06:04 +000012161
Paulo Zanonidde86e22012-12-01 12:04:25 -020012162 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020012163
12164 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012165}
12166
12167static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12168{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012169 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080012170 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080012171
Daniel Vetteref2d6332014-02-10 18:00:38 +010012172 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012173 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010012174 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012175 drm_gem_object_unreference(&intel_fb->obj->base);
12176 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012177 kfree(intel_fb);
12178}
12179
12180static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000012181 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080012182 unsigned int *handle)
12183{
12184 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000012185 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012186
Chris Wilson05394f32010-11-08 19:18:58 +000012187 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080012188}
12189
12190static const struct drm_framebuffer_funcs intel_fb_funcs = {
12191 .destroy = intel_user_framebuffer_destroy,
12192 .create_handle = intel_user_framebuffer_create_handle,
12193};
12194
Daniel Vetterb5ea6422014-03-02 21:18:00 +010012195static int intel_framebuffer_init(struct drm_device *dev,
12196 struct intel_framebuffer *intel_fb,
12197 struct drm_mode_fb_cmd2 *mode_cmd,
12198 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080012199{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012200 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012201 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080012202 int ret;
12203
Daniel Vetterdd4916c2013-10-09 21:23:51 +020012204 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12205
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012206 if (obj->tiling_mode == I915_TILING_Y) {
12207 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010012208 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012209 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012210
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012211 if (mode_cmd->pitches[0] & 63) {
12212 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12213 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010012214 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012215 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012216
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012217 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12218 pitch_limit = 32*1024;
12219 } else if (INTEL_INFO(dev)->gen >= 4) {
12220 if (obj->tiling_mode)
12221 pitch_limit = 16*1024;
12222 else
12223 pitch_limit = 32*1024;
12224 } else if (INTEL_INFO(dev)->gen >= 3) {
12225 if (obj->tiling_mode)
12226 pitch_limit = 8*1024;
12227 else
12228 pitch_limit = 16*1024;
12229 } else
12230 /* XXX DSPC is limited to 4k tiled */
12231 pitch_limit = 8*1024;
12232
12233 if (mode_cmd->pitches[0] > pitch_limit) {
12234 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12235 obj->tiling_mode ? "tiled" : "linear",
12236 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012237 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012238 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012239
12240 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012241 mode_cmd->pitches[0] != obj->stride) {
12242 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12243 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012244 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012245 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012246
Ville Syrjälä57779d02012-10-31 17:50:14 +020012247 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012248 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020012249 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012250 case DRM_FORMAT_RGB565:
12251 case DRM_FORMAT_XRGB8888:
12252 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012253 break;
12254 case DRM_FORMAT_XRGB1555:
12255 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012256 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012257 DRM_DEBUG("unsupported pixel format: %s\n",
12258 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012259 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012260 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020012261 break;
12262 case DRM_FORMAT_XBGR8888:
12263 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012264 case DRM_FORMAT_XRGB2101010:
12265 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012266 case DRM_FORMAT_XBGR2101010:
12267 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012268 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012269 DRM_DEBUG("unsupported pixel format: %s\n",
12270 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012271 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012272 }
Jesse Barnesb5626742011-06-24 12:19:27 -070012273 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020012274 case DRM_FORMAT_YUYV:
12275 case DRM_FORMAT_UYVY:
12276 case DRM_FORMAT_YVYU:
12277 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012278 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012279 DRM_DEBUG("unsupported pixel format: %s\n",
12280 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012281 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012282 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012283 break;
12284 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012285 DRM_DEBUG("unsupported pixel format: %s\n",
12286 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010012287 return -EINVAL;
12288 }
12289
Ville Syrjälä90f9a332012-10-31 17:50:19 +020012290 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12291 if (mode_cmd->offsets[0] != 0)
12292 return -EINVAL;
12293
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012294 aligned_height = intel_align_height(dev, mode_cmd->height,
12295 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020012296 /* FIXME drm helper for size checks (especially planar formats)? */
12297 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12298 return -EINVAL;
12299
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012300 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12301 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020012302 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012303
Jesse Barnes79e53942008-11-07 14:24:08 -080012304 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12305 if (ret) {
12306 DRM_ERROR("framebuffer init failed %d\n", ret);
12307 return ret;
12308 }
12309
Jesse Barnes79e53942008-11-07 14:24:08 -080012310 return 0;
12311}
12312
Jesse Barnes79e53942008-11-07 14:24:08 -080012313static struct drm_framebuffer *
12314intel_user_framebuffer_create(struct drm_device *dev,
12315 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012316 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080012317{
Chris Wilson05394f32010-11-08 19:18:58 +000012318 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012319
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012320 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12321 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000012322 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010012323 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080012324
Chris Wilsond2dff872011-04-19 08:36:26 +010012325 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080012326}
12327
Daniel Vetter4520f532013-10-09 09:18:51 +020012328#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020012329static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020012330{
12331}
12332#endif
12333
Jesse Barnes79e53942008-11-07 14:24:08 -080012334static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080012335 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020012336 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080012337};
12338
Jesse Barnese70236a2009-09-21 10:42:27 -070012339/* Set up chip specific display functions */
12340static void intel_init_display(struct drm_device *dev)
12341{
12342 struct drm_i915_private *dev_priv = dev->dev_private;
12343
Daniel Vetteree9300b2013-06-03 22:40:22 +020012344 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12345 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030012346 else if (IS_CHERRYVIEW(dev))
12347 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020012348 else if (IS_VALLEYVIEW(dev))
12349 dev_priv->display.find_dpll = vlv_find_best_dpll;
12350 else if (IS_PINEVIEW(dev))
12351 dev_priv->display.find_dpll = pnv_find_best_dpll;
12352 else
12353 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12354
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012355 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012356 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080012357 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012358 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020012359 dev_priv->display.crtc_enable = haswell_crtc_enable;
12360 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030012361 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiau70d21f02013-07-03 21:06:04 +010012362 if (INTEL_INFO(dev)->gen >= 9)
12363 dev_priv->display.update_primary_plane =
12364 skylake_update_primary_plane;
12365 else
12366 dev_priv->display.update_primary_plane =
12367 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012368 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012369 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080012370 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070012371 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012372 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12373 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012374 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012375 dev_priv->display.update_primary_plane =
12376 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012377 } else if (IS_VALLEYVIEW(dev)) {
12378 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080012379 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012380 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12381 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12382 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12383 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012384 dev_priv->display.update_primary_plane =
12385 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012386 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012387 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080012388 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070012389 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012390 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12391 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012392 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012393 dev_priv->display.update_primary_plane =
12394 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012395 }
Jesse Barnese70236a2009-09-21 10:42:27 -070012396
Jesse Barnese70236a2009-09-21 10:42:27 -070012397 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070012398 if (IS_VALLEYVIEW(dev))
12399 dev_priv->display.get_display_clock_speed =
12400 valleyview_get_display_clock_speed;
12401 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070012402 dev_priv->display.get_display_clock_speed =
12403 i945_get_display_clock_speed;
12404 else if (IS_I915G(dev))
12405 dev_priv->display.get_display_clock_speed =
12406 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012407 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012408 dev_priv->display.get_display_clock_speed =
12409 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012410 else if (IS_PINEVIEW(dev))
12411 dev_priv->display.get_display_clock_speed =
12412 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070012413 else if (IS_I915GM(dev))
12414 dev_priv->display.get_display_clock_speed =
12415 i915gm_get_display_clock_speed;
12416 else if (IS_I865G(dev))
12417 dev_priv->display.get_display_clock_speed =
12418 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020012419 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012420 dev_priv->display.get_display_clock_speed =
12421 i855_get_display_clock_speed;
12422 else /* 852, 830 */
12423 dev_priv->display.get_display_clock_speed =
12424 i830_get_display_clock_speed;
12425
Jani Nikula7c10a2b2014-10-27 16:26:43 +020012426 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012427 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012428 } else if (IS_GEN6(dev)) {
12429 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012430 dev_priv->display.modeset_global_resources =
12431 snb_modeset_global_resources;
12432 } else if (IS_IVYBRIDGE(dev)) {
12433 /* FIXME: detect B0+ stepping and use auto training */
12434 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012435 dev_priv->display.modeset_global_resources =
12436 ivb_modeset_global_resources;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030012437 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012438 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012439 dev_priv->display.modeset_global_resources =
12440 haswell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012441 } else if (IS_VALLEYVIEW(dev)) {
12442 dev_priv->display.modeset_global_resources =
12443 valleyview_modeset_global_resources;
Satheeshakrishna M02c29252014-04-08 15:46:54 +053012444 } else if (INTEL_INFO(dev)->gen >= 9) {
Satheeshakrishna M02c29252014-04-08 15:46:54 +053012445 dev_priv->display.modeset_global_resources =
12446 haswell_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070012447 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012448
12449 /* Default just returns -ENODEV to indicate unsupported */
12450 dev_priv->display.queue_flip = intel_default_queue_flip;
12451
12452 switch (INTEL_INFO(dev)->gen) {
12453 case 2:
12454 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12455 break;
12456
12457 case 3:
12458 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12459 break;
12460
12461 case 4:
12462 case 5:
12463 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12464 break;
12465
12466 case 6:
12467 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12468 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012469 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070012470 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012471 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12472 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012473 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020012474
12475 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030012476
12477 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070012478}
12479
Jesse Barnesb690e962010-07-19 13:53:12 -070012480/*
12481 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12482 * resume, or other times. This quirk makes sure that's the case for
12483 * affected systems.
12484 */
Akshay Joshi0206e352011-08-16 15:34:10 -040012485static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070012486{
12487 struct drm_i915_private *dev_priv = dev->dev_private;
12488
12489 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012490 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012491}
12492
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012493static void quirk_pipeb_force(struct drm_device *dev)
12494{
12495 struct drm_i915_private *dev_priv = dev->dev_private;
12496
12497 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12498 DRM_INFO("applying pipe b force quirk\n");
12499}
12500
Keith Packard435793d2011-07-12 14:56:22 -070012501/*
12502 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12503 */
12504static void quirk_ssc_force_disable(struct drm_device *dev)
12505{
12506 struct drm_i915_private *dev_priv = dev->dev_private;
12507 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012508 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070012509}
12510
Carsten Emde4dca20e2012-03-15 15:56:26 +010012511/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010012512 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12513 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010012514 */
12515static void quirk_invert_brightness(struct drm_device *dev)
12516{
12517 struct drm_i915_private *dev_priv = dev->dev_private;
12518 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012519 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012520}
12521
Scot Doyle9c72cc62014-07-03 23:27:50 +000012522/* Some VBT's incorrectly indicate no backlight is present */
12523static void quirk_backlight_present(struct drm_device *dev)
12524{
12525 struct drm_i915_private *dev_priv = dev->dev_private;
12526 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12527 DRM_INFO("applying backlight present quirk\n");
12528}
12529
Jesse Barnesb690e962010-07-19 13:53:12 -070012530struct intel_quirk {
12531 int device;
12532 int subsystem_vendor;
12533 int subsystem_device;
12534 void (*hook)(struct drm_device *dev);
12535};
12536
Egbert Eich5f85f172012-10-14 15:46:38 +020012537/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12538struct intel_dmi_quirk {
12539 void (*hook)(struct drm_device *dev);
12540 const struct dmi_system_id (*dmi_id_list)[];
12541};
12542
12543static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12544{
12545 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12546 return 1;
12547}
12548
12549static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12550 {
12551 .dmi_id_list = &(const struct dmi_system_id[]) {
12552 {
12553 .callback = intel_dmi_reverse_brightness,
12554 .ident = "NCR Corporation",
12555 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12556 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12557 },
12558 },
12559 { } /* terminating entry */
12560 },
12561 .hook = quirk_invert_brightness,
12562 },
12563};
12564
Ben Widawskyc43b5632012-04-16 14:07:40 -070012565static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070012566 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040012567 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070012568
Jesse Barnesb690e962010-07-19 13:53:12 -070012569 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12570 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12571
Jesse Barnesb690e962010-07-19 13:53:12 -070012572 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12573 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12574
Ville Syrjälä5f080c02014-08-15 01:22:06 +030012575 /* 830 needs to leave pipe A & dpll A up */
12576 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12577
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012578 /* 830 needs to leave pipe B & dpll B up */
12579 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12580
Keith Packard435793d2011-07-12 14:56:22 -070012581 /* Lenovo U160 cannot use SSC on LVDS */
12582 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020012583
12584 /* Sony Vaio Y cannot use SSC on LVDS */
12585 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010012586
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010012587 /* Acer Aspire 5734Z must invert backlight brightness */
12588 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12589
12590 /* Acer/eMachines G725 */
12591 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12592
12593 /* Acer/eMachines e725 */
12594 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12595
12596 /* Acer/Packard Bell NCL20 */
12597 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12598
12599 /* Acer Aspire 4736Z */
12600 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020012601
12602 /* Acer Aspire 5336 */
12603 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000012604
12605 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12606 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000012607
Scot Doyledfb3d47b2014-08-21 16:08:02 +000012608 /* Acer C720 Chromebook (Core i3 4005U) */
12609 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
12610
Scot Doyled4967d82014-07-03 23:27:52 +000012611 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12612 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000012613
12614 /* HP Chromebook 14 (Celeron 2955U) */
12615 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070012616};
12617
12618static void intel_init_quirks(struct drm_device *dev)
12619{
12620 struct pci_dev *d = dev->pdev;
12621 int i;
12622
12623 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12624 struct intel_quirk *q = &intel_quirks[i];
12625
12626 if (d->device == q->device &&
12627 (d->subsystem_vendor == q->subsystem_vendor ||
12628 q->subsystem_vendor == PCI_ANY_ID) &&
12629 (d->subsystem_device == q->subsystem_device ||
12630 q->subsystem_device == PCI_ANY_ID))
12631 q->hook(dev);
12632 }
Egbert Eich5f85f172012-10-14 15:46:38 +020012633 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12634 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12635 intel_dmi_quirks[i].hook(dev);
12636 }
Jesse Barnesb690e962010-07-19 13:53:12 -070012637}
12638
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012639/* Disable the VGA plane that we never use */
12640static void i915_disable_vga(struct drm_device *dev)
12641{
12642 struct drm_i915_private *dev_priv = dev->dev_private;
12643 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012644 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012645
Ville Syrjälä2b37c612014-01-22 21:32:38 +020012646 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012647 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070012648 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012649 sr1 = inb(VGA_SR_DATA);
12650 outb(sr1 | 1<<5, VGA_SR_DATA);
12651 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12652 udelay(300);
12653
Ville Syrjälä69769f92014-08-15 01:22:08 +030012654 /*
12655 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12656 * from S3 without preserving (some of?) the other bits.
12657 */
12658 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012659 POSTING_READ(vga_reg);
12660}
12661
Daniel Vetterf8175862012-04-10 15:50:11 +020012662void intel_modeset_init_hw(struct drm_device *dev)
12663{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030012664 intel_prepare_ddi(dev);
12665
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030012666 if (IS_VALLEYVIEW(dev))
12667 vlv_update_cdclk(dev);
12668
Daniel Vetterf8175862012-04-10 15:50:11 +020012669 intel_init_clock_gating(dev);
12670
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012671 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020012672}
12673
Jesse Barnes79e53942008-11-07 14:24:08 -080012674void intel_modeset_init(struct drm_device *dev)
12675{
Jesse Barnes652c3932009-08-17 13:31:43 -070012676 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000012677 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012678 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080012679 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080012680
12681 drm_mode_config_init(dev);
12682
12683 dev->mode_config.min_width = 0;
12684 dev->mode_config.min_height = 0;
12685
Dave Airlie019d96c2011-09-29 16:20:42 +010012686 dev->mode_config.preferred_depth = 24;
12687 dev->mode_config.prefer_shadow = 1;
12688
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020012689 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080012690
Jesse Barnesb690e962010-07-19 13:53:12 -070012691 intel_init_quirks(dev);
12692
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030012693 intel_init_pm(dev);
12694
Ben Widawskye3c74752013-04-05 13:12:39 -070012695 if (INTEL_INFO(dev)->num_pipes == 0)
12696 return;
12697
Jesse Barnese70236a2009-09-21 10:42:27 -070012698 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020012699 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070012700
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012701 if (IS_GEN2(dev)) {
12702 dev->mode_config.max_width = 2048;
12703 dev->mode_config.max_height = 2048;
12704 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070012705 dev->mode_config.max_width = 4096;
12706 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080012707 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012708 dev->mode_config.max_width = 8192;
12709 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080012710 }
Damien Lespiau068be562014-03-28 14:17:49 +000012711
Ville Syrjälädc41c152014-08-13 11:57:05 +030012712 if (IS_845G(dev) || IS_I865G(dev)) {
12713 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12714 dev->mode_config.cursor_height = 1023;
12715 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000012716 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12717 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12718 } else {
12719 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12720 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12721 }
12722
Ben Widawsky5d4545a2013-01-17 12:45:15 -080012723 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080012724
Zhao Yakui28c97732009-10-09 11:39:41 +080012725 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012726 INTEL_INFO(dev)->num_pipes,
12727 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080012728
Damien Lespiau055e3932014-08-18 13:49:10 +010012729 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012730 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000012731 for_each_sprite(pipe, sprite) {
12732 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012733 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030012734 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000012735 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012736 }
Jesse Barnes79e53942008-11-07 14:24:08 -080012737 }
12738
Jesse Barnesf42bb702013-12-16 16:34:23 -080012739 intel_init_dpio(dev);
12740
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012741 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012742
Ville Syrjälä69769f92014-08-15 01:22:08 +030012743 /* save the BIOS value before clobbering it */
12744 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012745 /* Just disable it once at startup */
12746 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012747 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000012748
12749 /* Just in case the BIOS is doing something questionable. */
12750 intel_disable_fbc(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012751
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012752 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012753 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012754 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012755
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012756 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080012757 if (!crtc->active)
12758 continue;
12759
Jesse Barnes46f297f2014-03-07 08:57:48 -080012760 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080012761 * Note that reserving the BIOS fb up front prevents us
12762 * from stuffing other stolen allocations like the ring
12763 * on top. This prevents some ugliness at boot time, and
12764 * can even allow for smooth boot transitions if the BIOS
12765 * fb is large enough for the active pipe configuration.
12766 */
12767 if (dev_priv->display.get_plane_config) {
12768 dev_priv->display.get_plane_config(crtc,
12769 &crtc->plane_config);
12770 /*
12771 * If the fb is shared between multiple heads, we'll
12772 * just get the first one.
12773 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080012774 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012775 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080012776 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010012777}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080012778
Daniel Vetter7fad7982012-07-04 17:51:47 +020012779static void intel_enable_pipe_a(struct drm_device *dev)
12780{
12781 struct intel_connector *connector;
12782 struct drm_connector *crt = NULL;
12783 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030012784 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020012785
12786 /* We can't just switch on the pipe A, we need to set things up with a
12787 * proper mode and output configuration. As a gross hack, enable pipe A
12788 * by enabling the load detect pipe once. */
12789 list_for_each_entry(connector,
12790 &dev->mode_config.connector_list,
12791 base.head) {
12792 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12793 crt = &connector->base;
12794 break;
12795 }
12796 }
12797
12798 if (!crt)
12799 return;
12800
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030012801 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
12802 intel_release_load_detect_pipe(crt, &load_detect_temp);
Daniel Vetter7fad7982012-07-04 17:51:47 +020012803}
12804
Daniel Vetterfa555832012-10-10 23:14:00 +020012805static bool
12806intel_check_plane_mapping(struct intel_crtc *crtc)
12807{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012808 struct drm_device *dev = crtc->base.dev;
12809 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012810 u32 reg, val;
12811
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012812 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020012813 return true;
12814
12815 reg = DSPCNTR(!crtc->plane);
12816 val = I915_READ(reg);
12817
12818 if ((val & DISPLAY_PLANE_ENABLE) &&
12819 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12820 return false;
12821
12822 return true;
12823}
12824
Daniel Vetter24929352012-07-02 20:28:59 +020012825static void intel_sanitize_crtc(struct intel_crtc *crtc)
12826{
12827 struct drm_device *dev = crtc->base.dev;
12828 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012829 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020012830
Daniel Vetter24929352012-07-02 20:28:59 +020012831 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020012832 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020012833 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12834
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030012835 /* restore vblank interrupts to correct state */
Ville Syrjäläd297e102014-08-06 14:50:01 +030012836 if (crtc->active) {
12837 update_scanline_offset(crtc);
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030012838 drm_vblank_on(dev, crtc->pipe);
Ville Syrjäläd297e102014-08-06 14:50:01 +030012839 } else
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030012840 drm_vblank_off(dev, crtc->pipe);
12841
Daniel Vetter24929352012-07-02 20:28:59 +020012842 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020012843 * disable the crtc (and hence change the state) if it is wrong. Note
12844 * that gen4+ has a fixed plane -> pipe mapping. */
12845 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020012846 struct intel_connector *connector;
12847 bool plane;
12848
Daniel Vetter24929352012-07-02 20:28:59 +020012849 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12850 crtc->base.base.id);
12851
12852 /* Pipe has the wrong plane attached and the plane is active.
12853 * Temporarily change the plane mapping and disable everything
12854 * ... */
12855 plane = crtc->plane;
12856 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020012857 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020012858 dev_priv->display.crtc_disable(&crtc->base);
12859 crtc->plane = plane;
12860
12861 /* ... and break all links. */
12862 list_for_each_entry(connector, &dev->mode_config.connector_list,
12863 base.head) {
12864 if (connector->encoder->base.crtc != &crtc->base)
12865 continue;
12866
Egbert Eich7f1950f2014-04-25 10:56:22 +020012867 connector->base.dpms = DRM_MODE_DPMS_OFF;
12868 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012869 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012870 /* multiple connectors may have the same encoder:
12871 * handle them and break crtc link separately */
12872 list_for_each_entry(connector, &dev->mode_config.connector_list,
12873 base.head)
12874 if (connector->encoder->base.crtc == &crtc->base) {
12875 connector->encoder->base.crtc = NULL;
12876 connector->encoder->connectors_active = false;
12877 }
Daniel Vetter24929352012-07-02 20:28:59 +020012878
12879 WARN_ON(crtc->active);
12880 crtc->base.enabled = false;
12881 }
Daniel Vetter24929352012-07-02 20:28:59 +020012882
Daniel Vetter7fad7982012-07-04 17:51:47 +020012883 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
12884 crtc->pipe == PIPE_A && !crtc->active) {
12885 /* BIOS forgot to enable pipe A, this mostly happens after
12886 * resume. Force-enable the pipe to fix this, the update_dpms
12887 * call below we restore the pipe to the right state, but leave
12888 * the required bits on. */
12889 intel_enable_pipe_a(dev);
12890 }
12891
Daniel Vetter24929352012-07-02 20:28:59 +020012892 /* Adjust the state of the output pipe according to whether we
12893 * have active connectors/encoders. */
12894 intel_crtc_update_dpms(&crtc->base);
12895
12896 if (crtc->active != crtc->base.enabled) {
12897 struct intel_encoder *encoder;
12898
12899 /* This can happen either due to bugs in the get_hw_state
12900 * functions or because the pipe is force-enabled due to the
12901 * pipe A quirk. */
12902 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
12903 crtc->base.base.id,
12904 crtc->base.enabled ? "enabled" : "disabled",
12905 crtc->active ? "enabled" : "disabled");
12906
12907 crtc->base.enabled = crtc->active;
12908
12909 /* Because we only establish the connector -> encoder ->
12910 * crtc links if something is active, this means the
12911 * crtc is now deactivated. Break the links. connector
12912 * -> encoder links are only establish when things are
12913 * actually up, hence no need to break them. */
12914 WARN_ON(crtc->active);
12915
12916 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
12917 WARN_ON(encoder->connectors_active);
12918 encoder->base.crtc = NULL;
12919 }
12920 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012921
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030012922 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010012923 /*
12924 * We start out with underrun reporting disabled to avoid races.
12925 * For correct bookkeeping mark this on active crtcs.
12926 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012927 * Also on gmch platforms we dont have any hardware bits to
12928 * disable the underrun reporting. Which means we need to start
12929 * out with underrun reporting disabled also on inactive pipes,
12930 * since otherwise we'll complain about the garbage we read when
12931 * e.g. coming up after runtime pm.
12932 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010012933 * No protection against concurrent access is required - at
12934 * worst a fifo underrun happens which also sets this to false.
12935 */
12936 crtc->cpu_fifo_underrun_disabled = true;
12937 crtc->pch_fifo_underrun_disabled = true;
12938 }
Daniel Vetter24929352012-07-02 20:28:59 +020012939}
12940
12941static void intel_sanitize_encoder(struct intel_encoder *encoder)
12942{
12943 struct intel_connector *connector;
12944 struct drm_device *dev = encoder->base.dev;
12945
12946 /* We need to check both for a crtc link (meaning that the
12947 * encoder is active and trying to read from a pipe) and the
12948 * pipe itself being active. */
12949 bool has_active_crtc = encoder->base.crtc &&
12950 to_intel_crtc(encoder->base.crtc)->active;
12951
12952 if (encoder->connectors_active && !has_active_crtc) {
12953 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12954 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012955 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012956
12957 /* Connector is active, but has no active pipe. This is
12958 * fallout from our resume register restoring. Disable
12959 * the encoder manually again. */
12960 if (encoder->base.crtc) {
12961 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
12962 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012963 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012964 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030012965 if (encoder->post_disable)
12966 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020012967 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012968 encoder->base.crtc = NULL;
12969 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020012970
12971 /* Inconsistent output/port/pipe state happens presumably due to
12972 * a bug in one of the get_hw_state functions. Or someplace else
12973 * in our code, like the register restore mess on resume. Clamp
12974 * things to off as a safer default. */
12975 list_for_each_entry(connector,
12976 &dev->mode_config.connector_list,
12977 base.head) {
12978 if (connector->encoder != encoder)
12979 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020012980 connector->base.dpms = DRM_MODE_DPMS_OFF;
12981 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012982 }
12983 }
12984 /* Enabled encoders without active connectors will be fixed in
12985 * the crtc fixup. */
12986}
12987
Imre Deak04098752014-02-18 00:02:16 +020012988void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012989{
12990 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012991 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012992
Imre Deak04098752014-02-18 00:02:16 +020012993 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
12994 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
12995 i915_disable_vga(dev);
12996 }
12997}
12998
12999void i915_redisable_vga(struct drm_device *dev)
13000{
13001 struct drm_i915_private *dev_priv = dev->dev_private;
13002
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013003 /* This function can be called both from intel_modeset_setup_hw_state or
13004 * at a very early point in our resume sequence, where the power well
13005 * structures are not yet restored. Since this function is at a very
13006 * paranoid "someone might have enabled VGA while we were not looking"
13007 * level, just check if the power well is enabled instead of trying to
13008 * follow the "don't touch the power well if we don't need it" policy
13009 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013010 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030013011 return;
13012
Imre Deak04098752014-02-18 00:02:16 +020013013 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010013014}
13015
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013016static bool primary_get_hw_state(struct intel_crtc *crtc)
13017{
13018 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
13019
13020 if (!crtc->active)
13021 return false;
13022
13023 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
13024}
13025
Daniel Vetter30e984d2013-06-05 13:34:17 +020013026static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020013027{
13028 struct drm_i915_private *dev_priv = dev->dev_private;
13029 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020013030 struct intel_crtc *crtc;
13031 struct intel_encoder *encoder;
13032 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020013033 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020013034
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013035 for_each_intel_crtc(dev, crtc) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010013036 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020013037
Daniel Vetter99535992014-04-13 12:00:33 +020013038 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13039
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013040 crtc->active = dev_priv->display.get_pipe_config(crtc,
13041 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013042
13043 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013044 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020013045
13046 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13047 crtc->base.base.id,
13048 crtc->active ? "enabled" : "disabled");
13049 }
13050
Daniel Vetter53589012013-06-05 13:34:16 +020013051 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13052 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13053
13054 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13055 pll->active = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013056 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020013057 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13058 pll->active++;
13059 }
13060 pll->refcount = pll->active;
13061
Daniel Vetter35c95372013-07-17 06:55:04 +020013062 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13063 pll->name, pll->refcount, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013064
13065 if (pll->refcount)
13066 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020013067 }
13068
Damien Lespiaub2784e12014-08-05 11:29:37 +010013069 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013070 pipe = 0;
13071
13072 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013073 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13074 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010013075 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013076 } else {
13077 encoder->base.crtc = NULL;
13078 }
13079
13080 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013081 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020013082 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013083 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013084 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013085 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020013086 }
13087
13088 list_for_each_entry(connector, &dev->mode_config.connector_list,
13089 base.head) {
13090 if (connector->get_hw_state(connector)) {
13091 connector->base.dpms = DRM_MODE_DPMS_ON;
13092 connector->encoder->connectors_active = true;
13093 connector->base.encoder = &connector->encoder->base;
13094 } else {
13095 connector->base.dpms = DRM_MODE_DPMS_OFF;
13096 connector->base.encoder = NULL;
13097 }
13098 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13099 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013100 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013101 connector->base.encoder ? "enabled" : "disabled");
13102 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020013103}
13104
13105/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13106 * and i915 state tracking structures. */
13107void intel_modeset_setup_hw_state(struct drm_device *dev,
13108 bool force_restore)
13109{
13110 struct drm_i915_private *dev_priv = dev->dev_private;
13111 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013112 struct intel_crtc *crtc;
13113 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020013114 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013115
13116 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020013117
Jesse Barnesbabea612013-06-26 18:57:38 +030013118 /*
13119 * Now that we have the config, copy it to each CRTC struct
13120 * Note that this could go away if we move to using crtc_config
13121 * checking everywhere.
13122 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013123 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020013124 if (crtc->active && i915.fastboot) {
Daniel Vetterf6a83282014-02-11 15:28:57 -080013125 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030013126 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13127 crtc->base.base.id);
13128 drm_mode_debug_printmodeline(&crtc->base.mode);
13129 }
13130 }
13131
Daniel Vetter24929352012-07-02 20:28:59 +020013132 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010013133 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013134 intel_sanitize_encoder(encoder);
13135 }
13136
Damien Lespiau055e3932014-08-18 13:49:10 +010013137 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020013138 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13139 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020013140 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020013141 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013142
Daniel Vetter35c95372013-07-17 06:55:04 +020013143 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13144 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13145
13146 if (!pll->on || pll->active)
13147 continue;
13148
13149 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13150
13151 pll->disable(dev_priv, pll);
13152 pll->on = false;
13153 }
13154
Ville Syrjälä96f90c52013-12-05 15:51:38 +020013155 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030013156 ilk_wm_get_hw_state(dev);
13157
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013158 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030013159 i915_redisable_vga(dev);
13160
Daniel Vetterf30da182013-04-11 20:22:50 +020013161 /*
13162 * We need to use raw interfaces for restoring state to avoid
13163 * checking (bogus) intermediate states.
13164 */
Damien Lespiau055e3932014-08-18 13:49:10 +010013165 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070013166 struct drm_crtc *crtc =
13167 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020013168
13169 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
Matt Roperf4510a22014-04-01 15:22:40 -070013170 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013171 }
13172 } else {
13173 intel_modeset_update_staged_output_state(dev);
13174 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013175
13176 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010013177}
13178
13179void intel_modeset_gem_init(struct drm_device *dev)
13180{
Jesse Barnes484b41d2014-03-07 08:57:55 -080013181 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070013182 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013183
Imre Deakae484342014-03-31 15:10:44 +030013184 mutex_lock(&dev->struct_mutex);
13185 intel_init_gt_powersave(dev);
13186 mutex_unlock(&dev->struct_mutex);
13187
Chris Wilson1833b132012-05-09 11:56:28 +010013188 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020013189
13190 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013191
13192 /*
13193 * Make sure any fbs we allocated at startup are properly
13194 * pinned & fenced. When we do the allocation it's too early
13195 * for this.
13196 */
13197 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010013198 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070013199 obj = intel_fb_obj(c->primary->fb);
13200 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080013201 continue;
13202
Matt Roper2ff8fde2014-07-08 07:50:07 -070013203 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080013204 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13205 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100013206 drm_framebuffer_unreference(c->primary->fb);
13207 c->primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013208 }
13209 }
13210 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013211}
13212
Imre Deak4932e2c2014-02-11 17:12:48 +020013213void intel_connector_unregister(struct intel_connector *intel_connector)
13214{
13215 struct drm_connector *connector = &intel_connector->base;
13216
13217 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010013218 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020013219}
13220
Jesse Barnes79e53942008-11-07 14:24:08 -080013221void intel_modeset_cleanup(struct drm_device *dev)
13222{
Jesse Barnes652c3932009-08-17 13:31:43 -070013223 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030013224 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070013225
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013226 /*
13227 * Interrupts and polling as the first thing to avoid creating havoc.
13228 * Too much stuff here (turning of rps, connectors, ...) would
13229 * experience fancy races otherwise.
13230 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020013231 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070013232
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013233 /*
13234 * Due to the hpd irq storm handling the hotplug work can re-arm the
13235 * poll handlers. Hence disable polling after hpd handling is shut down.
13236 */
Keith Packardf87ea762010-10-03 19:36:26 -070013237 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013238
Jesse Barnes652c3932009-08-17 13:31:43 -070013239 mutex_lock(&dev->struct_mutex);
13240
Jesse Barnes723bfd72010-10-07 16:01:13 -070013241 intel_unregister_dsm_handler();
13242
Chris Wilson973d04f2011-07-08 12:22:37 +010013243 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013244
Daniel Vetter8090c6b2012-06-24 16:42:32 +020013245 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000013246
Daniel Vetter930ebb42012-06-29 23:32:16 +020013247 ironlake_teardown_rc6(dev);
13248
Kristian Høgsberg69341a52009-11-11 12:19:17 -050013249 mutex_unlock(&dev->struct_mutex);
13250
Chris Wilson1630fe72011-07-08 12:22:42 +010013251 /* flush any delayed tasks or pending work */
13252 flush_scheduled_work();
13253
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013254 /* destroy the backlight and sysfs files before encoders/connectors */
13255 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020013256 struct intel_connector *intel_connector;
13257
13258 intel_connector = to_intel_connector(connector);
13259 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013260 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030013261
Jesse Barnes79e53942008-11-07 14:24:08 -080013262 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010013263
13264 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030013265
13266 mutex_lock(&dev->struct_mutex);
13267 intel_cleanup_gt_powersave(dev);
13268 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013269}
13270
Dave Airlie28d52042009-09-21 14:33:58 +100013271/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080013272 * Return which encoder is currently attached for connector.
13273 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010013274struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080013275{
Chris Wilsondf0e9242010-09-09 16:20:55 +010013276 return &intel_attached_encoder(connector)->base;
13277}
Jesse Barnes79e53942008-11-07 14:24:08 -080013278
Chris Wilsondf0e9242010-09-09 16:20:55 +010013279void intel_connector_attach_encoder(struct intel_connector *connector,
13280 struct intel_encoder *encoder)
13281{
13282 connector->encoder = encoder;
13283 drm_mode_connector_attach_encoder(&connector->base,
13284 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080013285}
Dave Airlie28d52042009-09-21 14:33:58 +100013286
13287/*
13288 * set vga decode state - true == enable VGA decode
13289 */
13290int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13291{
13292 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000013293 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100013294 u16 gmch_ctrl;
13295
Chris Wilson75fa0412014-02-07 18:37:02 -020013296 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13297 DRM_ERROR("failed to read control word\n");
13298 return -EIO;
13299 }
13300
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020013301 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13302 return 0;
13303
Dave Airlie28d52042009-09-21 14:33:58 +100013304 if (state)
13305 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13306 else
13307 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020013308
13309 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13310 DRM_ERROR("failed to write control word\n");
13311 return -EIO;
13312 }
13313
Dave Airlie28d52042009-09-21 14:33:58 +100013314 return 0;
13315}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013316
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013317struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013318
13319 u32 power_well_driver;
13320
Chris Wilson63b66e52013-08-08 15:12:06 +020013321 int num_transcoders;
13322
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013323 struct intel_cursor_error_state {
13324 u32 control;
13325 u32 position;
13326 u32 base;
13327 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010013328 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013329
13330 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013331 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013332 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030013333 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010013334 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013335
13336 struct intel_plane_error_state {
13337 u32 control;
13338 u32 stride;
13339 u32 size;
13340 u32 pos;
13341 u32 addr;
13342 u32 surface;
13343 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010013344 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020013345
13346 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013347 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020013348 enum transcoder cpu_transcoder;
13349
13350 u32 conf;
13351
13352 u32 htotal;
13353 u32 hblank;
13354 u32 hsync;
13355 u32 vtotal;
13356 u32 vblank;
13357 u32 vsync;
13358 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013359};
13360
13361struct intel_display_error_state *
13362intel_display_capture_error_state(struct drm_device *dev)
13363{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013364 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013365 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020013366 int transcoders[] = {
13367 TRANSCODER_A,
13368 TRANSCODER_B,
13369 TRANSCODER_C,
13370 TRANSCODER_EDP,
13371 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013372 int i;
13373
Chris Wilson63b66e52013-08-08 15:12:06 +020013374 if (INTEL_INFO(dev)->num_pipes == 0)
13375 return NULL;
13376
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013377 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013378 if (error == NULL)
13379 return NULL;
13380
Imre Deak190be112013-11-25 17:15:31 +020013381 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013382 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13383
Damien Lespiau055e3932014-08-18 13:49:10 +010013384 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020013385 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013386 __intel_display_power_is_enabled(dev_priv,
13387 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020013388 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013389 continue;
13390
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030013391 error->cursor[i].control = I915_READ(CURCNTR(i));
13392 error->cursor[i].position = I915_READ(CURPOS(i));
13393 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013394
13395 error->plane[i].control = I915_READ(DSPCNTR(i));
13396 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013397 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030013398 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013399 error->plane[i].pos = I915_READ(DSPPOS(i));
13400 }
Paulo Zanonica291362013-03-06 20:03:14 -030013401 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13402 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013403 if (INTEL_INFO(dev)->gen >= 4) {
13404 error->plane[i].surface = I915_READ(DSPSURF(i));
13405 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13406 }
13407
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013408 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030013409
Sonika Jindal3abfce72014-07-21 15:23:43 +053013410 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030013411 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020013412 }
13413
13414 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13415 if (HAS_DDI(dev_priv->dev))
13416 error->num_transcoders++; /* Account for eDP. */
13417
13418 for (i = 0; i < error->num_transcoders; i++) {
13419 enum transcoder cpu_transcoder = transcoders[i];
13420
Imre Deakddf9c532013-11-27 22:02:02 +020013421 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013422 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020013423 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013424 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013425 continue;
13426
Chris Wilson63b66e52013-08-08 15:12:06 +020013427 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13428
13429 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13430 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13431 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13432 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13433 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13434 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13435 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013436 }
13437
13438 return error;
13439}
13440
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013441#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13442
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013443void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013444intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013445 struct drm_device *dev,
13446 struct intel_display_error_state *error)
13447{
Damien Lespiau055e3932014-08-18 13:49:10 +010013448 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013449 int i;
13450
Chris Wilson63b66e52013-08-08 15:12:06 +020013451 if (!error)
13452 return;
13453
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013454 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020013455 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013456 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013457 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010013458 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013459 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020013460 err_printf(m, " Power: %s\n",
13461 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013462 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030013463 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013464
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013465 err_printf(m, "Plane [%d]:\n", i);
13466 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13467 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013468 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013469 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13470 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013471 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030013472 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013473 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013474 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013475 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13476 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013477 }
13478
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013479 err_printf(m, "Cursor [%d]:\n", i);
13480 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13481 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13482 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013483 }
Chris Wilson63b66e52013-08-08 15:12:06 +020013484
13485 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010013486 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020013487 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013488 err_printf(m, " Power: %s\n",
13489 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020013490 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13491 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13492 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13493 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13494 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13495 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13496 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13497 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013498}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013499
13500void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13501{
13502 struct intel_crtc *crtc;
13503
13504 for_each_intel_crtc(dev, crtc) {
13505 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013506
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020013507 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013508
13509 work = crtc->unpin_work;
13510
13511 if (work && work->event &&
13512 work->event->base.file_priv == file) {
13513 kfree(work->event);
13514 work->event = NULL;
13515 }
13516
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020013517 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013518 }
13519}