]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/i915/intel_ddi.c
drm/i915: Mark manually wedged engines as guilty
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpu / drm / i915 / intel_ddi.c
CommitLineData
45244b87
ED
1/*
2 * Copyright © 2012 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 DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
28#include "i915_drv.h"
29#include "intel_drv.h"
30
10122051
JN
31struct ddi_buf_trans {
32 u32 trans1; /* balance leg enable, de-emph level */
33 u32 trans2; /* vref sel, vswing */
f8896f5d 34 u8 i_boost; /* SKL: I_boost; valid: 0x0, 0x1, 0x3, 0x7 */
10122051
JN
35};
36
97eeb872
VS
37static const u8 index_to_dp_signal_levels[] = {
38 [0] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0,
39 [1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1,
40 [2] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2,
41 [3] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3,
42 [4] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0,
43 [5] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1,
44 [6] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2,
45 [7] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0,
46 [8] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1,
47 [9] = DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0,
48};
49
45244b87
ED
50/* HDMI/DVI modes ignore everything but the last 2 items. So we share
51 * them for both DP and FDI transports, allowing those ports to
52 * automatically adapt to HDMI connections as well
53 */
10122051 54static const struct ddi_buf_trans hsw_ddi_translations_dp[] = {
f8896f5d
DW
55 { 0x00FFFFFF, 0x0006000E, 0x0 },
56 { 0x00D75FFF, 0x0005000A, 0x0 },
57 { 0x00C30FFF, 0x00040006, 0x0 },
58 { 0x80AAAFFF, 0x000B0000, 0x0 },
59 { 0x00FFFFFF, 0x0005000A, 0x0 },
60 { 0x00D75FFF, 0x000C0004, 0x0 },
61 { 0x80C30FFF, 0x000B0000, 0x0 },
62 { 0x00FFFFFF, 0x00040006, 0x0 },
63 { 0x80D75FFF, 0x000B0000, 0x0 },
45244b87
ED
64};
65
10122051 66static const struct ddi_buf_trans hsw_ddi_translations_fdi[] = {
f8896f5d
DW
67 { 0x00FFFFFF, 0x0007000E, 0x0 },
68 { 0x00D75FFF, 0x000F000A, 0x0 },
69 { 0x00C30FFF, 0x00060006, 0x0 },
70 { 0x00AAAFFF, 0x001E0000, 0x0 },
71 { 0x00FFFFFF, 0x000F000A, 0x0 },
72 { 0x00D75FFF, 0x00160004, 0x0 },
73 { 0x00C30FFF, 0x001E0000, 0x0 },
74 { 0x00FFFFFF, 0x00060006, 0x0 },
75 { 0x00D75FFF, 0x001E0000, 0x0 },
6acab15a
PZ
76};
77
10122051
JN
78static const struct ddi_buf_trans hsw_ddi_translations_hdmi[] = {
79 /* Idx NT mV d T mV d db */
f8896f5d
DW
80 { 0x00FFFFFF, 0x0006000E, 0x0 },/* 0: 400 400 0 */
81 { 0x00E79FFF, 0x000E000C, 0x0 },/* 1: 400 500 2 */
82 { 0x00D75FFF, 0x0005000A, 0x0 },/* 2: 400 600 3.5 */
83 { 0x00FFFFFF, 0x0005000A, 0x0 },/* 3: 600 600 0 */
84 { 0x00E79FFF, 0x001D0007, 0x0 },/* 4: 600 750 2 */
85 { 0x00D75FFF, 0x000C0004, 0x0 },/* 5: 600 900 3.5 */
86 { 0x00FFFFFF, 0x00040006, 0x0 },/* 6: 800 800 0 */
87 { 0x80E79FFF, 0x00030002, 0x0 },/* 7: 800 1000 2 */
88 { 0x00FFFFFF, 0x00140005, 0x0 },/* 8: 850 850 0 */
89 { 0x00FFFFFF, 0x000C0004, 0x0 },/* 9: 900 900 0 */
90 { 0x00FFFFFF, 0x001C0003, 0x0 },/* 10: 950 950 0 */
91 { 0x80FFFFFF, 0x00030002, 0x0 },/* 11: 1000 1000 0 */
45244b87
ED
92};
93
10122051 94static const struct ddi_buf_trans bdw_ddi_translations_edp[] = {
f8896f5d
DW
95 { 0x00FFFFFF, 0x00000012, 0x0 },
96 { 0x00EBAFFF, 0x00020011, 0x0 },
97 { 0x00C71FFF, 0x0006000F, 0x0 },
98 { 0x00AAAFFF, 0x000E000A, 0x0 },
99 { 0x00FFFFFF, 0x00020011, 0x0 },
100 { 0x00DB6FFF, 0x0005000F, 0x0 },
101 { 0x00BEEFFF, 0x000A000C, 0x0 },
102 { 0x00FFFFFF, 0x0005000F, 0x0 },
103 { 0x00DB6FFF, 0x000A000C, 0x0 },
300644c7
PZ
104};
105
10122051 106static const struct ddi_buf_trans bdw_ddi_translations_dp[] = {
f8896f5d
DW
107 { 0x00FFFFFF, 0x0007000E, 0x0 },
108 { 0x00D75FFF, 0x000E000A, 0x0 },
109 { 0x00BEFFFF, 0x00140006, 0x0 },
110 { 0x80B2CFFF, 0x001B0002, 0x0 },
111 { 0x00FFFFFF, 0x000E000A, 0x0 },
112 { 0x00DB6FFF, 0x00160005, 0x0 },
113 { 0x80C71FFF, 0x001A0002, 0x0 },
114 { 0x00F7DFFF, 0x00180004, 0x0 },
115 { 0x80D75FFF, 0x001B0002, 0x0 },
e58623cb
AR
116};
117
10122051 118static const struct ddi_buf_trans bdw_ddi_translations_fdi[] = {
f8896f5d
DW
119 { 0x00FFFFFF, 0x0001000E, 0x0 },
120 { 0x00D75FFF, 0x0004000A, 0x0 },
121 { 0x00C30FFF, 0x00070006, 0x0 },
122 { 0x00AAAFFF, 0x000C0000, 0x0 },
123 { 0x00FFFFFF, 0x0004000A, 0x0 },
124 { 0x00D75FFF, 0x00090004, 0x0 },
125 { 0x00C30FFF, 0x000C0000, 0x0 },
126 { 0x00FFFFFF, 0x00070006, 0x0 },
127 { 0x00D75FFF, 0x000C0000, 0x0 },
e58623cb
AR
128};
129
10122051
JN
130static const struct ddi_buf_trans bdw_ddi_translations_hdmi[] = {
131 /* Idx NT mV d T mV df db */
f8896f5d
DW
132 { 0x00FFFFFF, 0x0007000E, 0x0 },/* 0: 400 400 0 */
133 { 0x00D75FFF, 0x000E000A, 0x0 },/* 1: 400 600 3.5 */
134 { 0x00BEFFFF, 0x00140006, 0x0 },/* 2: 400 800 6 */
135 { 0x00FFFFFF, 0x0009000D, 0x0 },/* 3: 450 450 0 */
136 { 0x00FFFFFF, 0x000E000A, 0x0 },/* 4: 600 600 0 */
137 { 0x00D7FFFF, 0x00140006, 0x0 },/* 5: 600 800 2.5 */
138 { 0x80CB2FFF, 0x001B0002, 0x0 },/* 6: 600 1000 4.5 */
139 { 0x00FFFFFF, 0x00140006, 0x0 },/* 7: 800 800 0 */
140 { 0x80E79FFF, 0x001B0002, 0x0 },/* 8: 800 1000 2 */
141 { 0x80FFFFFF, 0x001B0002, 0x0 },/* 9: 1000 1000 0 */
a26aa8ba
DL
142};
143
5f8b2531 144/* Skylake H and S */
7f88e3af 145static const struct ddi_buf_trans skl_ddi_translations_dp[] = {
f8896f5d
DW
146 { 0x00002016, 0x000000A0, 0x0 },
147 { 0x00005012, 0x0000009B, 0x0 },
148 { 0x00007011, 0x00000088, 0x0 },
d7097cff 149 { 0x80009010, 0x000000C0, 0x1 },
f8896f5d
DW
150 { 0x00002016, 0x0000009B, 0x0 },
151 { 0x00005012, 0x00000088, 0x0 },
d7097cff 152 { 0x80007011, 0x000000C0, 0x1 },
f8896f5d 153 { 0x00002016, 0x000000DF, 0x0 },
d7097cff 154 { 0x80005012, 0x000000C0, 0x1 },
7f88e3af
DL
155};
156
f8896f5d
DW
157/* Skylake U */
158static const struct ddi_buf_trans skl_u_ddi_translations_dp[] = {
5f8b2531 159 { 0x0000201B, 0x000000A2, 0x0 },
f8896f5d 160 { 0x00005012, 0x00000088, 0x0 },
5ac90567 161 { 0x80007011, 0x000000CD, 0x1 },
d7097cff 162 { 0x80009010, 0x000000C0, 0x1 },
5f8b2531 163 { 0x0000201B, 0x0000009D, 0x0 },
d7097cff
RV
164 { 0x80005012, 0x000000C0, 0x1 },
165 { 0x80007011, 0x000000C0, 0x1 },
f8896f5d 166 { 0x00002016, 0x00000088, 0x0 },
d7097cff 167 { 0x80005012, 0x000000C0, 0x1 },
f8896f5d
DW
168};
169
5f8b2531
RV
170/* Skylake Y */
171static const struct ddi_buf_trans skl_y_ddi_translations_dp[] = {
f8896f5d
DW
172 { 0x00000018, 0x000000A2, 0x0 },
173 { 0x00005012, 0x00000088, 0x0 },
5ac90567 174 { 0x80007011, 0x000000CD, 0x3 },
d7097cff 175 { 0x80009010, 0x000000C0, 0x3 },
f8896f5d 176 { 0x00000018, 0x0000009D, 0x0 },
d7097cff
RV
177 { 0x80005012, 0x000000C0, 0x3 },
178 { 0x80007011, 0x000000C0, 0x3 },
f8896f5d 179 { 0x00000018, 0x00000088, 0x0 },
d7097cff 180 { 0x80005012, 0x000000C0, 0x3 },
f8896f5d
DW
181};
182
0fdd4918
RV
183/* Kabylake H and S */
184static const struct ddi_buf_trans kbl_ddi_translations_dp[] = {
185 { 0x00002016, 0x000000A0, 0x0 },
186 { 0x00005012, 0x0000009B, 0x0 },
187 { 0x00007011, 0x00000088, 0x0 },
188 { 0x80009010, 0x000000C0, 0x1 },
189 { 0x00002016, 0x0000009B, 0x0 },
190 { 0x00005012, 0x00000088, 0x0 },
191 { 0x80007011, 0x000000C0, 0x1 },
192 { 0x00002016, 0x00000097, 0x0 },
193 { 0x80005012, 0x000000C0, 0x1 },
194};
195
196/* Kabylake U */
197static const struct ddi_buf_trans kbl_u_ddi_translations_dp[] = {
198 { 0x0000201B, 0x000000A1, 0x0 },
199 { 0x00005012, 0x00000088, 0x0 },
200 { 0x80007011, 0x000000CD, 0x3 },
201 { 0x80009010, 0x000000C0, 0x3 },
202 { 0x0000201B, 0x0000009D, 0x0 },
203 { 0x80005012, 0x000000C0, 0x3 },
204 { 0x80007011, 0x000000C0, 0x3 },
205 { 0x00002016, 0x0000004F, 0x0 },
206 { 0x80005012, 0x000000C0, 0x3 },
207};
208
209/* Kabylake Y */
210static const struct ddi_buf_trans kbl_y_ddi_translations_dp[] = {
211 { 0x00001017, 0x000000A1, 0x0 },
212 { 0x00005012, 0x00000088, 0x0 },
213 { 0x80007011, 0x000000CD, 0x3 },
214 { 0x8000800F, 0x000000C0, 0x3 },
215 { 0x00001017, 0x0000009D, 0x0 },
216 { 0x80005012, 0x000000C0, 0x3 },
217 { 0x80007011, 0x000000C0, 0x3 },
218 { 0x00001017, 0x0000004C, 0x0 },
219 { 0x80005012, 0x000000C0, 0x3 },
220};
221
f8896f5d 222/*
0fdd4918 223 * Skylake/Kabylake H and S
f8896f5d
DW
224 * eDP 1.4 low vswing translation parameters
225 */
7ad14a29 226static const struct ddi_buf_trans skl_ddi_translations_edp[] = {
f8896f5d
DW
227 { 0x00000018, 0x000000A8, 0x0 },
228 { 0x00004013, 0x000000A9, 0x0 },
229 { 0x00007011, 0x000000A2, 0x0 },
230 { 0x00009010, 0x0000009C, 0x0 },
231 { 0x00000018, 0x000000A9, 0x0 },
232 { 0x00006013, 0x000000A2, 0x0 },
233 { 0x00007011, 0x000000A6, 0x0 },
234 { 0x00000018, 0x000000AB, 0x0 },
235 { 0x00007013, 0x0000009F, 0x0 },
236 { 0x00000018, 0x000000DF, 0x0 },
237};
238
239/*
0fdd4918 240 * Skylake/Kabylake U
f8896f5d
DW
241 * eDP 1.4 low vswing translation parameters
242 */
243static const struct ddi_buf_trans skl_u_ddi_translations_edp[] = {
244 { 0x00000018, 0x000000A8, 0x0 },
245 { 0x00004013, 0x000000A9, 0x0 },
246 { 0x00007011, 0x000000A2, 0x0 },
247 { 0x00009010, 0x0000009C, 0x0 },
248 { 0x00000018, 0x000000A9, 0x0 },
249 { 0x00006013, 0x000000A2, 0x0 },
250 { 0x00007011, 0x000000A6, 0x0 },
251 { 0x00002016, 0x000000AB, 0x0 },
252 { 0x00005013, 0x0000009F, 0x0 },
253 { 0x00000018, 0x000000DF, 0x0 },
7ad14a29
SJ
254};
255
f8896f5d 256/*
0fdd4918 257 * Skylake/Kabylake Y
f8896f5d
DW
258 * eDP 1.4 low vswing translation parameters
259 */
5f8b2531 260static const struct ddi_buf_trans skl_y_ddi_translations_edp[] = {
f8896f5d
DW
261 { 0x00000018, 0x000000A8, 0x0 },
262 { 0x00004013, 0x000000AB, 0x0 },
263 { 0x00007011, 0x000000A4, 0x0 },
264 { 0x00009010, 0x000000DF, 0x0 },
265 { 0x00000018, 0x000000AA, 0x0 },
266 { 0x00006013, 0x000000A4, 0x0 },
267 { 0x00007011, 0x0000009D, 0x0 },
268 { 0x00000018, 0x000000A0, 0x0 },
269 { 0x00006012, 0x000000DF, 0x0 },
270 { 0x00000018, 0x0000008A, 0x0 },
271};
7ad14a29 272
0fdd4918 273/* Skylake/Kabylake U, H and S */
7f88e3af 274static const struct ddi_buf_trans skl_ddi_translations_hdmi[] = {
f8896f5d
DW
275 { 0x00000018, 0x000000AC, 0x0 },
276 { 0x00005012, 0x0000009D, 0x0 },
277 { 0x00007011, 0x00000088, 0x0 },
278 { 0x00000018, 0x000000A1, 0x0 },
279 { 0x00000018, 0x00000098, 0x0 },
280 { 0x00004013, 0x00000088, 0x0 },
2e78416e 281 { 0x80006012, 0x000000CD, 0x1 },
f8896f5d 282 { 0x00000018, 0x000000DF, 0x0 },
2e78416e
RV
283 { 0x80003015, 0x000000CD, 0x1 }, /* Default */
284 { 0x80003015, 0x000000C0, 0x1 },
285 { 0x80000018, 0x000000C0, 0x1 },
f8896f5d
DW
286};
287
0fdd4918 288/* Skylake/Kabylake Y */
5f8b2531 289static const struct ddi_buf_trans skl_y_ddi_translations_hdmi[] = {
f8896f5d
DW
290 { 0x00000018, 0x000000A1, 0x0 },
291 { 0x00005012, 0x000000DF, 0x0 },
2e78416e 292 { 0x80007011, 0x000000CB, 0x3 },
f8896f5d
DW
293 { 0x00000018, 0x000000A4, 0x0 },
294 { 0x00000018, 0x0000009D, 0x0 },
295 { 0x00004013, 0x00000080, 0x0 },
2e78416e 296 { 0x80006013, 0x000000C0, 0x3 },
f8896f5d 297 { 0x00000018, 0x0000008A, 0x0 },
2e78416e
RV
298 { 0x80003015, 0x000000C0, 0x3 }, /* Default */
299 { 0x80003015, 0x000000C0, 0x3 },
300 { 0x80000018, 0x000000C0, 0x3 },
7f88e3af
DL
301};
302
96fb9f9b
VK
303struct bxt_ddi_buf_trans {
304 u32 margin; /* swing value */
305 u32 scale; /* scale value */
306 u32 enable; /* scale enable */
307 u32 deemphasis;
308 bool default_index; /* true if the entry represents default value */
309};
310
96fb9f9b
VK
311static const struct bxt_ddi_buf_trans bxt_ddi_translations_dp[] = {
312 /* Idx NT mV diff db */
fe4c63c8
ID
313 { 52, 0x9A, 0, 128, true }, /* 0: 400 0 */
314 { 78, 0x9A, 0, 85, false }, /* 1: 400 3.5 */
315 { 104, 0x9A, 0, 64, false }, /* 2: 400 6 */
316 { 154, 0x9A, 0, 43, false }, /* 3: 400 9.5 */
317 { 77, 0x9A, 0, 128, false }, /* 4: 600 0 */
318 { 116, 0x9A, 0, 85, false }, /* 5: 600 3.5 */
319 { 154, 0x9A, 0, 64, false }, /* 6: 600 6 */
320 { 102, 0x9A, 0, 128, false }, /* 7: 800 0 */
321 { 154, 0x9A, 0, 85, false }, /* 8: 800 3.5 */
f8896f5d 322 { 154, 0x9A, 1, 128, false }, /* 9: 1200 0 */
96fb9f9b
VK
323};
324
d9d7000d
SJ
325static const struct bxt_ddi_buf_trans bxt_ddi_translations_edp[] = {
326 /* Idx NT mV diff db */
327 { 26, 0, 0, 128, false }, /* 0: 200 0 */
328 { 38, 0, 0, 112, false }, /* 1: 200 1.5 */
329 { 48, 0, 0, 96, false }, /* 2: 200 4 */
330 { 54, 0, 0, 69, false }, /* 3: 200 6 */
331 { 32, 0, 0, 128, false }, /* 4: 250 0 */
332 { 48, 0, 0, 104, false }, /* 5: 250 1.5 */
333 { 54, 0, 0, 85, false }, /* 6: 250 4 */
334 { 43, 0, 0, 128, false }, /* 7: 300 0 */
335 { 54, 0, 0, 101, false }, /* 8: 300 1.5 */
336 { 48, 0, 0, 128, false }, /* 9: 300 0 */
337};
338
96fb9f9b
VK
339/* BSpec has 2 recommended values - entries 0 and 8.
340 * Using the entry with higher vswing.
341 */
342static const struct bxt_ddi_buf_trans bxt_ddi_translations_hdmi[] = {
343 /* Idx NT mV diff db */
fe4c63c8
ID
344 { 52, 0x9A, 0, 128, false }, /* 0: 400 0 */
345 { 52, 0x9A, 0, 85, false }, /* 1: 400 3.5 */
346 { 52, 0x9A, 0, 64, false }, /* 2: 400 6 */
347 { 42, 0x9A, 0, 43, false }, /* 3: 400 9.5 */
348 { 77, 0x9A, 0, 128, false }, /* 4: 600 0 */
349 { 77, 0x9A, 0, 85, false }, /* 5: 600 3.5 */
350 { 77, 0x9A, 0, 64, false }, /* 6: 600 6 */
351 { 102, 0x9A, 0, 128, false }, /* 7: 800 0 */
352 { 102, 0x9A, 0, 85, false }, /* 8: 800 3.5 */
96fb9f9b
VK
353 { 154, 0x9A, 1, 128, true }, /* 9: 1200 0 */
354};
355
5a5d24dc 356enum port intel_ddi_get_encoder_port(struct intel_encoder *encoder)
fc914639 357{
5a5d24dc 358 switch (encoder->type) {
8cd21b7f 359 case INTEL_OUTPUT_DP_MST:
5a5d24dc 360 return enc_to_mst(&encoder->base)->primary->port;
cca0502b 361 case INTEL_OUTPUT_DP:
8cd21b7f
JN
362 case INTEL_OUTPUT_EDP:
363 case INTEL_OUTPUT_HDMI:
364 case INTEL_OUTPUT_UNKNOWN:
5a5d24dc 365 return enc_to_dig_port(&encoder->base)->port;
8cd21b7f 366 case INTEL_OUTPUT_ANALOG:
5a5d24dc
VS
367 return PORT_E;
368 default:
369 MISSING_CASE(encoder->type);
370 return PORT_A;
fc914639
PZ
371 }
372}
373
a930acd9
VS
374static const struct ddi_buf_trans *
375bdw_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
376{
377 if (dev_priv->vbt.edp.low_vswing) {
378 *n_entries = ARRAY_SIZE(bdw_ddi_translations_edp);
379 return bdw_ddi_translations_edp;
380 } else {
381 *n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
382 return bdw_ddi_translations_dp;
383 }
384}
385
acee2998 386static const struct ddi_buf_trans *
78ab0bae 387skl_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
f8896f5d 388{
0fdd4918 389 if (IS_SKL_ULX(dev_priv)) {
5f8b2531 390 *n_entries = ARRAY_SIZE(skl_y_ddi_translations_dp);
acee2998 391 return skl_y_ddi_translations_dp;
0fdd4918 392 } else if (IS_SKL_ULT(dev_priv)) {
f8896f5d 393 *n_entries = ARRAY_SIZE(skl_u_ddi_translations_dp);
acee2998 394 return skl_u_ddi_translations_dp;
f8896f5d 395 } else {
f8896f5d 396 *n_entries = ARRAY_SIZE(skl_ddi_translations_dp);
acee2998 397 return skl_ddi_translations_dp;
f8896f5d 398 }
f8896f5d
DW
399}
400
0fdd4918
RV
401static const struct ddi_buf_trans *
402kbl_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
403{
404 if (IS_KBL_ULX(dev_priv)) {
405 *n_entries = ARRAY_SIZE(kbl_y_ddi_translations_dp);
406 return kbl_y_ddi_translations_dp;
407 } else if (IS_KBL_ULT(dev_priv)) {
408 *n_entries = ARRAY_SIZE(kbl_u_ddi_translations_dp);
409 return kbl_u_ddi_translations_dp;
410 } else {
411 *n_entries = ARRAY_SIZE(kbl_ddi_translations_dp);
412 return kbl_ddi_translations_dp;
413 }
414}
415
acee2998 416static const struct ddi_buf_trans *
78ab0bae 417skl_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
f8896f5d 418{
06411f08 419 if (dev_priv->vbt.edp.low_vswing) {
78ab0bae 420 if (IS_SKL_ULX(dev_priv) || IS_KBL_ULX(dev_priv)) {
5f8b2531 421 *n_entries = ARRAY_SIZE(skl_y_ddi_translations_edp);
acee2998 422 return skl_y_ddi_translations_edp;
78ab0bae 423 } else if (IS_SKL_ULT(dev_priv) || IS_KBL_ULT(dev_priv)) {
f8896f5d 424 *n_entries = ARRAY_SIZE(skl_u_ddi_translations_edp);
acee2998 425 return skl_u_ddi_translations_edp;
f8896f5d 426 } else {
f8896f5d 427 *n_entries = ARRAY_SIZE(skl_ddi_translations_edp);
acee2998 428 return skl_ddi_translations_edp;
f8896f5d
DW
429 }
430 }
cd1101cb 431
0fdd4918
RV
432 if (IS_KABYLAKE(dev_priv))
433 return kbl_get_buf_trans_dp(dev_priv, n_entries);
434 else
435 return skl_get_buf_trans_dp(dev_priv, n_entries);
f8896f5d
DW
436}
437
438static const struct ddi_buf_trans *
78ab0bae 439skl_get_buf_trans_hdmi(struct drm_i915_private *dev_priv, int *n_entries)
f8896f5d 440{
78ab0bae 441 if (IS_SKL_ULX(dev_priv) || IS_KBL_ULX(dev_priv)) {
5f8b2531 442 *n_entries = ARRAY_SIZE(skl_y_ddi_translations_hdmi);
acee2998 443 return skl_y_ddi_translations_hdmi;
f8896f5d 444 } else {
f8896f5d 445 *n_entries = ARRAY_SIZE(skl_ddi_translations_hdmi);
acee2998 446 return skl_ddi_translations_hdmi;
f8896f5d 447 }
f8896f5d
DW
448}
449
8d8bb85e
VS
450static int intel_ddi_hdmi_level(struct drm_i915_private *dev_priv, enum port port)
451{
452 int n_hdmi_entries;
453 int hdmi_level;
454 int hdmi_default_entry;
455
456 hdmi_level = dev_priv->vbt.ddi_port_info[port].hdmi_level_shift;
457
cc3f90f0 458 if (IS_GEN9_LP(dev_priv))
8d8bb85e
VS
459 return hdmi_level;
460
b976dc53 461 if (IS_GEN9_BC(dev_priv)) {
8d8bb85e
VS
462 skl_get_buf_trans_hdmi(dev_priv, &n_hdmi_entries);
463 hdmi_default_entry = 8;
464 } else if (IS_BROADWELL(dev_priv)) {
465 n_hdmi_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
466 hdmi_default_entry = 7;
467 } else if (IS_HASWELL(dev_priv)) {
468 n_hdmi_entries = ARRAY_SIZE(hsw_ddi_translations_hdmi);
469 hdmi_default_entry = 6;
470 } else {
471 WARN(1, "ddi translation table missing\n");
472 n_hdmi_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
473 hdmi_default_entry = 7;
474 }
475
476 /* Choose a good default if VBT is badly populated */
477 if (hdmi_level == HDMI_LEVEL_SHIFT_UNKNOWN ||
478 hdmi_level >= n_hdmi_entries)
479 hdmi_level = hdmi_default_entry;
480
481 return hdmi_level;
482}
483
7d1c42e6
VS
484static const struct ddi_buf_trans *
485intel_ddi_get_buf_trans_dp(struct drm_i915_private *dev_priv,
486 int *n_entries)
487{
488 if (IS_KABYLAKE(dev_priv)) {
489 return kbl_get_buf_trans_dp(dev_priv, n_entries);
490 } else if (IS_SKYLAKE(dev_priv)) {
491 return skl_get_buf_trans_dp(dev_priv, n_entries);
492 } else if (IS_BROADWELL(dev_priv)) {
493 *n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
494 return bdw_ddi_translations_dp;
495 } else if (IS_HASWELL(dev_priv)) {
496 *n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
497 return hsw_ddi_translations_dp;
498 }
499
500 *n_entries = 0;
501 return NULL;
502}
503
504static const struct ddi_buf_trans *
505intel_ddi_get_buf_trans_edp(struct drm_i915_private *dev_priv,
506 int *n_entries)
507{
508 if (IS_KABYLAKE(dev_priv) || IS_SKYLAKE(dev_priv)) {
509 return skl_get_buf_trans_edp(dev_priv, n_entries);
510 } else if (IS_BROADWELL(dev_priv)) {
511 return bdw_get_buf_trans_edp(dev_priv, n_entries);
512 } else if (IS_HASWELL(dev_priv)) {
513 *n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
514 return hsw_ddi_translations_dp;
515 }
516
517 *n_entries = 0;
518 return NULL;
519}
520
521static const struct ddi_buf_trans *
522intel_ddi_get_buf_trans_fdi(struct drm_i915_private *dev_priv,
523 int *n_entries)
524{
525 if (IS_BROADWELL(dev_priv)) {
526 *n_entries = ARRAY_SIZE(hsw_ddi_translations_fdi);
527 return hsw_ddi_translations_fdi;
528 } else if (IS_HASWELL(dev_priv)) {
529 *n_entries = ARRAY_SIZE(hsw_ddi_translations_fdi);
530 return hsw_ddi_translations_fdi;
531 }
532
533 *n_entries = 0;
534 return NULL;
535}
536
e58623cb
AR
537/*
538 * Starting with Haswell, DDI port buffers must be programmed with correct
32bdc400
VS
539 * values in advance. This function programs the correct values for
540 * DP/eDP/FDI use cases.
45244b87 541 */
32bdc400 542void intel_prepare_dp_ddi_buffers(struct intel_encoder *encoder)
45244b87 543{
6a7e4f99 544 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
75067dde 545 u32 iboost_bit = 0;
7d1c42e6 546 int i, n_entries;
32bdc400 547 enum port port = intel_ddi_get_encoder_port(encoder);
10122051 548 const struct ddi_buf_trans *ddi_translations;
e58623cb 549
cc3f90f0 550 if (IS_GEN9_LP(dev_priv))
96fb9f9b 551 return;
6a7e4f99 552
7d1c42e6
VS
553 switch (encoder->type) {
554 case INTEL_OUTPUT_EDP:
555 ddi_translations = intel_ddi_get_buf_trans_edp(dev_priv,
556 &n_entries);
557 break;
558 case INTEL_OUTPUT_DP:
559 ddi_translations = intel_ddi_get_buf_trans_dp(dev_priv,
560 &n_entries);
561 break;
562 case INTEL_OUTPUT_ANALOG:
563 ddi_translations = intel_ddi_get_buf_trans_fdi(dev_priv,
564 &n_entries);
565 break;
566 default:
567 MISSING_CASE(encoder->type);
568 return;
e58623cb
AR
569 }
570
b976dc53 571 if (IS_GEN9_BC(dev_priv)) {
0a91877c
RV
572 /* If we're boosting the current, set bit 31 of trans1 */
573 if (dev_priv->vbt.ddi_port_info[port].dp_boost_level)
574 iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
575
576 if (WARN_ON(encoder->type == INTEL_OUTPUT_EDP &&
577 port != PORT_A && port != PORT_E &&
7d1c42e6
VS
578 n_entries > 9))
579 n_entries = 9;
300644c7 580 }
45244b87 581
7d1c42e6 582 for (i = 0; i < n_entries; i++) {
9712e688
VS
583 I915_WRITE(DDI_BUF_TRANS_LO(port, i),
584 ddi_translations[i].trans1 | iboost_bit);
585 I915_WRITE(DDI_BUF_TRANS_HI(port, i),
586 ddi_translations[i].trans2);
45244b87 587 }
32bdc400
VS
588}
589
590/*
591 * Starting with Haswell, DDI port buffers must be programmed with correct
592 * values in advance. This function programs the correct values for
593 * HDMI/DVI use cases.
594 */
595static void intel_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder)
596{
597 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
598 u32 iboost_bit = 0;
599 int n_hdmi_entries, hdmi_level;
600 enum port port = intel_ddi_get_encoder_port(encoder);
601 const struct ddi_buf_trans *ddi_translations_hdmi;
ce4dd49e 602
cc3f90f0 603 if (IS_GEN9_LP(dev_priv))
ce3b7e9b
DL
604 return;
605
32bdc400
VS
606 hdmi_level = intel_ddi_hdmi_level(dev_priv, port);
607
b976dc53 608 if (IS_GEN9_BC(dev_priv)) {
32bdc400 609 ddi_translations_hdmi = skl_get_buf_trans_hdmi(dev_priv, &n_hdmi_entries);
1edaaa2f 610
32bdc400 611 /* If we're boosting the current, set bit 31 of trans1 */
1edaaa2f 612 if (dev_priv->vbt.ddi_port_info[port].hdmi_boost_level)
32bdc400
VS
613 iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
614 } else if (IS_BROADWELL(dev_priv)) {
615 ddi_translations_hdmi = bdw_ddi_translations_hdmi;
616 n_hdmi_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
617 } else if (IS_HASWELL(dev_priv)) {
618 ddi_translations_hdmi = hsw_ddi_translations_hdmi;
619 n_hdmi_entries = ARRAY_SIZE(hsw_ddi_translations_hdmi);
620 } else {
621 WARN(1, "ddi translation table missing\n");
622 ddi_translations_hdmi = bdw_ddi_translations_hdmi;
623 n_hdmi_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
624 }
625
6acab15a 626 /* Entry 9 is for HDMI: */
ed9c77d2 627 I915_WRITE(DDI_BUF_TRANS_LO(port, 9),
9712e688 628 ddi_translations_hdmi[hdmi_level].trans1 | iboost_bit);
ed9c77d2 629 I915_WRITE(DDI_BUF_TRANS_HI(port, 9),
9712e688 630 ddi_translations_hdmi[hdmi_level].trans2);
45244b87
ED
631}
632
248138b5
PZ
633static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
634 enum port port)
635{
f0f59a00 636 i915_reg_t reg = DDI_BUF_CTL(port);
248138b5
PZ
637 int i;
638
3449ca85 639 for (i = 0; i < 16; i++) {
248138b5
PZ
640 udelay(1);
641 if (I915_READ(reg) & DDI_BUF_IS_IDLE)
642 return;
643 }
644 DRM_ERROR("Timeout waiting for DDI BUF %c idle bit\n", port_name(port));
645}
c82e4d26 646
c856052a
ACO
647static uint32_t hsw_pll_to_ddi_pll_sel(struct intel_shared_dpll *pll)
648{
649 switch (pll->id) {
650 case DPLL_ID_WRPLL1:
651 return PORT_CLK_SEL_WRPLL1;
652 case DPLL_ID_WRPLL2:
653 return PORT_CLK_SEL_WRPLL2;
654 case DPLL_ID_SPLL:
655 return PORT_CLK_SEL_SPLL;
656 case DPLL_ID_LCPLL_810:
657 return PORT_CLK_SEL_LCPLL_810;
658 case DPLL_ID_LCPLL_1350:
659 return PORT_CLK_SEL_LCPLL_1350;
660 case DPLL_ID_LCPLL_2700:
661 return PORT_CLK_SEL_LCPLL_2700;
662 default:
663 MISSING_CASE(pll->id);
664 return PORT_CLK_SEL_NONE;
665 }
666}
667
c82e4d26
ED
668/* Starting with Haswell, different DDI ports can work in FDI mode for
669 * connection to the PCH-located connectors. For this, it is necessary to train
670 * both the DDI port and PCH receiver for the desired DDI buffer settings.
671 *
672 * The recommended port to work in FDI mode is DDI E, which we use here. Also,
673 * please note that when FDI mode is active on DDI E, it shares 2 lines with
674 * DDI A (which is used for eDP)
675 */
676
dc4a1094
ACO
677void hsw_fdi_link_train(struct intel_crtc *crtc,
678 const struct intel_crtc_state *crtc_state)
c82e4d26 679{
4cbe4b2b 680 struct drm_device *dev = crtc->base.dev;
fac5e23e 681 struct drm_i915_private *dev_priv = to_i915(dev);
6a7e4f99 682 struct intel_encoder *encoder;
c856052a 683 u32 temp, i, rx_ctl_val, ddi_pll_sel;
c82e4d26 684
4cbe4b2b 685 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
6a7e4f99 686 WARN_ON(encoder->type != INTEL_OUTPUT_ANALOG);
32bdc400 687 intel_prepare_dp_ddi_buffers(encoder);
6a7e4f99
VS
688 }
689
04945641
PZ
690 /* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
691 * mode set "sequence for CRT port" document:
692 * - TP1 to TP2 time with the default value
693 * - FDI delay to 90h
8693a824
DL
694 *
695 * WaFDIAutoLinkSetTimingOverrride:hsw
04945641 696 */
eede3b53 697 I915_WRITE(FDI_RX_MISC(PIPE_A), FDI_RX_PWRDN_LANE1_VAL(2) |
04945641
PZ
698 FDI_RX_PWRDN_LANE0_VAL(2) |
699 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
700
701 /* Enable the PCH Receiver FDI PLL */
3e68320e 702 rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
33d29b14 703 FDI_RX_PLL_ENABLE |
dc4a1094 704 FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
eede3b53
VS
705 I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
706 POSTING_READ(FDI_RX_CTL(PIPE_A));
04945641
PZ
707 udelay(220);
708
709 /* Switch from Rawclk to PCDclk */
710 rx_ctl_val |= FDI_PCDCLK;
eede3b53 711 I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
04945641
PZ
712
713 /* Configure Port Clock Select */
dc4a1094 714 ddi_pll_sel = hsw_pll_to_ddi_pll_sel(crtc_state->shared_dpll);
c856052a
ACO
715 I915_WRITE(PORT_CLK_SEL(PORT_E), ddi_pll_sel);
716 WARN_ON(ddi_pll_sel != PORT_CLK_SEL_SPLL);
04945641
PZ
717
718 /* Start the training iterating through available voltages and emphasis,
719 * testing each value twice. */
10122051 720 for (i = 0; i < ARRAY_SIZE(hsw_ddi_translations_fdi) * 2; i++) {
c82e4d26
ED
721 /* Configure DP_TP_CTL with auto-training */
722 I915_WRITE(DP_TP_CTL(PORT_E),
723 DP_TP_CTL_FDI_AUTOTRAIN |
724 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
725 DP_TP_CTL_LINK_TRAIN_PAT1 |
726 DP_TP_CTL_ENABLE);
727
876a8cdf
DL
728 /* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
729 * DDI E does not support port reversal, the functionality is
730 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
731 * port reversal bit */
c82e4d26 732 I915_WRITE(DDI_BUF_CTL(PORT_E),
04945641 733 DDI_BUF_CTL_ENABLE |
dc4a1094 734 ((crtc_state->fdi_lanes - 1) << 1) |
c5fe6a06 735 DDI_BUF_TRANS_SELECT(i / 2));
04945641 736 POSTING_READ(DDI_BUF_CTL(PORT_E));
c82e4d26
ED
737
738 udelay(600);
739
04945641 740 /* Program PCH FDI Receiver TU */
eede3b53 741 I915_WRITE(FDI_RX_TUSIZE1(PIPE_A), TU_SIZE(64));
04945641
PZ
742
743 /* Enable PCH FDI Receiver with auto-training */
744 rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
eede3b53
VS
745 I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
746 POSTING_READ(FDI_RX_CTL(PIPE_A));
04945641
PZ
747
748 /* Wait for FDI receiver lane calibration */
749 udelay(30);
750
751 /* Unset FDI_RX_MISC pwrdn lanes */
eede3b53 752 temp = I915_READ(FDI_RX_MISC(PIPE_A));
04945641 753 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
eede3b53
VS
754 I915_WRITE(FDI_RX_MISC(PIPE_A), temp);
755 POSTING_READ(FDI_RX_MISC(PIPE_A));
04945641
PZ
756
757 /* Wait for FDI auto training time */
758 udelay(5);
c82e4d26
ED
759
760 temp = I915_READ(DP_TP_STATUS(PORT_E));
761 if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
04945641 762 DRM_DEBUG_KMS("FDI link training done on step %d\n", i);
a308ccb3
VS
763 break;
764 }
c82e4d26 765
a308ccb3
VS
766 /*
767 * Leave things enabled even if we failed to train FDI.
768 * Results in less fireworks from the state checker.
769 */
770 if (i == ARRAY_SIZE(hsw_ddi_translations_fdi) * 2 - 1) {
771 DRM_ERROR("FDI link training failed!\n");
772 break;
c82e4d26 773 }
04945641 774
5b421c57
VS
775 rx_ctl_val &= ~FDI_RX_ENABLE;
776 I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
777 POSTING_READ(FDI_RX_CTL(PIPE_A));
778
248138b5
PZ
779 temp = I915_READ(DDI_BUF_CTL(PORT_E));
780 temp &= ~DDI_BUF_CTL_ENABLE;
781 I915_WRITE(DDI_BUF_CTL(PORT_E), temp);
782 POSTING_READ(DDI_BUF_CTL(PORT_E));
783
04945641 784 /* Disable DP_TP_CTL and FDI_RX_CTL and retry */
248138b5
PZ
785 temp = I915_READ(DP_TP_CTL(PORT_E));
786 temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
787 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
788 I915_WRITE(DP_TP_CTL(PORT_E), temp);
789 POSTING_READ(DP_TP_CTL(PORT_E));
790
791 intel_wait_ddi_buf_idle(dev_priv, PORT_E);
04945641 792
04945641 793 /* Reset FDI_RX_MISC pwrdn lanes */
eede3b53 794 temp = I915_READ(FDI_RX_MISC(PIPE_A));
04945641
PZ
795 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
796 temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
eede3b53
VS
797 I915_WRITE(FDI_RX_MISC(PIPE_A), temp);
798 POSTING_READ(FDI_RX_MISC(PIPE_A));
c82e4d26
ED
799 }
800
a308ccb3
VS
801 /* Enable normal pixel sending for FDI */
802 I915_WRITE(DP_TP_CTL(PORT_E),
803 DP_TP_CTL_FDI_AUTOTRAIN |
804 DP_TP_CTL_LINK_TRAIN_NORMAL |
805 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
806 DP_TP_CTL_ENABLE);
c82e4d26 807}
0e72a5b5 808
44905a27
DA
809void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder)
810{
811 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
812 struct intel_digital_port *intel_dig_port =
813 enc_to_dig_port(&encoder->base);
814
815 intel_dp->DP = intel_dig_port->saved_port_bits |
c5fe6a06 816 DDI_BUF_CTL_ENABLE | DDI_BUF_TRANS_SELECT(0);
901c2daf 817 intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
44905a27
DA
818}
819
8d9ddbcb 820static struct intel_encoder *
e9ce1a62 821intel_ddi_get_crtc_encoder(struct intel_crtc *crtc)
8d9ddbcb 822{
e9ce1a62 823 struct drm_device *dev = crtc->base.dev;
1524e93e 824 struct intel_encoder *encoder, *ret = NULL;
8d9ddbcb
PZ
825 int num_encoders = 0;
826
1524e93e
SS
827 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
828 ret = encoder;
8d9ddbcb
PZ
829 num_encoders++;
830 }
831
832 if (num_encoders != 1)
84f44ce7 833 WARN(1, "%d encoders on crtc for pipe %c\n", num_encoders,
e9ce1a62 834 pipe_name(crtc->pipe));
8d9ddbcb
PZ
835
836 BUG_ON(ret == NULL);
837 return ret;
838}
839
b77c7a90 840static struct intel_encoder *
3165c074 841intel_ddi_get_crtc_new_encoder(struct intel_crtc_state *crtc_state)
d0737e1d 842{
3165c074
ACO
843 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
844 struct intel_encoder *ret = NULL;
845 struct drm_atomic_state *state;
da3ced29
ACO
846 struct drm_connector *connector;
847 struct drm_connector_state *connector_state;
d0737e1d 848 int num_encoders = 0;
3165c074 849 int i;
d0737e1d 850
3165c074
ACO
851 state = crtc_state->base.state;
852
b77c7a90 853 for_each_new_connector_in_state(state, connector, connector_state, i) {
da3ced29 854 if (connector_state->crtc != crtc_state->base.crtc)
3165c074
ACO
855 continue;
856
da3ced29 857 ret = to_intel_encoder(connector_state->best_encoder);
3165c074 858 num_encoders++;
d0737e1d
ACO
859 }
860
861 WARN(num_encoders != 1, "%d encoders on crtc for pipe %c\n", num_encoders,
862 pipe_name(crtc->pipe));
863
864 BUG_ON(ret == NULL);
865 return ret;
866}
867
1c0b85c5 868#define LC_FREQ 2700
1c0b85c5 869
f0f59a00
VS
870static int hsw_ddi_calc_wrpll_link(struct drm_i915_private *dev_priv,
871 i915_reg_t reg)
11578553
JB
872{
873 int refclk = LC_FREQ;
874 int n, p, r;
875 u32 wrpll;
876
877 wrpll = I915_READ(reg);
114fe488
DV
878 switch (wrpll & WRPLL_PLL_REF_MASK) {
879 case WRPLL_PLL_SSC:
880 case WRPLL_PLL_NON_SSC:
11578553
JB
881 /*
882 * We could calculate spread here, but our checking
883 * code only cares about 5% accuracy, and spread is a max of
884 * 0.5% downspread.
885 */
886 refclk = 135;
887 break;
114fe488 888 case WRPLL_PLL_LCPLL:
11578553
JB
889 refclk = LC_FREQ;
890 break;
891 default:
892 WARN(1, "bad wrpll refclk\n");
893 return 0;
894 }
895
896 r = wrpll & WRPLL_DIVIDER_REF_MASK;
897 p = (wrpll & WRPLL_DIVIDER_POST_MASK) >> WRPLL_DIVIDER_POST_SHIFT;
898 n = (wrpll & WRPLL_DIVIDER_FB_MASK) >> WRPLL_DIVIDER_FB_SHIFT;
899
20f0ec16
JB
900 /* Convert to KHz, p & r have a fixed point portion */
901 return (refclk * n * 100) / (p * r);
11578553
JB
902}
903
540e732c
S
904static int skl_calc_wrpll_link(struct drm_i915_private *dev_priv,
905 uint32_t dpll)
906{
f0f59a00 907 i915_reg_t cfgcr1_reg, cfgcr2_reg;
540e732c
S
908 uint32_t cfgcr1_val, cfgcr2_val;
909 uint32_t p0, p1, p2, dco_freq;
910
923c1241
VS
911 cfgcr1_reg = DPLL_CFGCR1(dpll);
912 cfgcr2_reg = DPLL_CFGCR2(dpll);
540e732c
S
913
914 cfgcr1_val = I915_READ(cfgcr1_reg);
915 cfgcr2_val = I915_READ(cfgcr2_reg);
916
917 p0 = cfgcr2_val & DPLL_CFGCR2_PDIV_MASK;
918 p2 = cfgcr2_val & DPLL_CFGCR2_KDIV_MASK;
919
920 if (cfgcr2_val & DPLL_CFGCR2_QDIV_MODE(1))
921 p1 = (cfgcr2_val & DPLL_CFGCR2_QDIV_RATIO_MASK) >> 8;
922 else
923 p1 = 1;
924
925
926 switch (p0) {
927 case DPLL_CFGCR2_PDIV_1:
928 p0 = 1;
929 break;
930 case DPLL_CFGCR2_PDIV_2:
931 p0 = 2;
932 break;
933 case DPLL_CFGCR2_PDIV_3:
934 p0 = 3;
935 break;
936 case DPLL_CFGCR2_PDIV_7:
937 p0 = 7;
938 break;
939 }
940
941 switch (p2) {
942 case DPLL_CFGCR2_KDIV_5:
943 p2 = 5;
944 break;
945 case DPLL_CFGCR2_KDIV_2:
946 p2 = 2;
947 break;
948 case DPLL_CFGCR2_KDIV_3:
949 p2 = 3;
950 break;
951 case DPLL_CFGCR2_KDIV_1:
952 p2 = 1;
953 break;
954 }
955
956 dco_freq = (cfgcr1_val & DPLL_CFGCR1_DCO_INTEGER_MASK) * 24 * 1000;
957
958 dco_freq += (((cfgcr1_val & DPLL_CFGCR1_DCO_FRACTION_MASK) >> 9) * 24 *
959 1000) / 0x8000;
960
961 return dco_freq / (p0 * p1 * p2 * 5);
962}
963
398a017e
VS
964static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
965{
966 int dotclock;
967
968 if (pipe_config->has_pch_encoder)
969 dotclock = intel_dotclock_calculate(pipe_config->port_clock,
970 &pipe_config->fdi_m_n);
37a5650b 971 else if (intel_crtc_has_dp_encoder(pipe_config))
398a017e
VS
972 dotclock = intel_dotclock_calculate(pipe_config->port_clock,
973 &pipe_config->dp_m_n);
974 else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp == 36)
975 dotclock = pipe_config->port_clock * 2 / 3;
976 else
977 dotclock = pipe_config->port_clock;
978
979 if (pipe_config->pixel_multiplier)
980 dotclock /= pipe_config->pixel_multiplier;
981
982 pipe_config->base.adjusted_mode.crtc_clock = dotclock;
983}
540e732c
S
984
985static void skl_ddi_clock_get(struct intel_encoder *encoder,
5cec258b 986 struct intel_crtc_state *pipe_config)
540e732c 987{
fac5e23e 988 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
540e732c
S
989 int link_clock = 0;
990 uint32_t dpll_ctl1, dpll;
991
c856052a 992 dpll = intel_get_shared_dpll_id(dev_priv, pipe_config->shared_dpll);
540e732c
S
993
994 dpll_ctl1 = I915_READ(DPLL_CTRL1);
995
996 if (dpll_ctl1 & DPLL_CTRL1_HDMI_MODE(dpll)) {
997 link_clock = skl_calc_wrpll_link(dev_priv, dpll);
998 } else {
71cd8423
DL
999 link_clock = dpll_ctl1 & DPLL_CTRL1_LINK_RATE_MASK(dpll);
1000 link_clock >>= DPLL_CTRL1_LINK_RATE_SHIFT(dpll);
540e732c
S
1001
1002 switch (link_clock) {
71cd8423 1003 case DPLL_CTRL1_LINK_RATE_810:
540e732c
S
1004 link_clock = 81000;
1005 break;
71cd8423 1006 case DPLL_CTRL1_LINK_RATE_1080:
a8f3ef61
SJ
1007 link_clock = 108000;
1008 break;
71cd8423 1009 case DPLL_CTRL1_LINK_RATE_1350:
540e732c
S
1010 link_clock = 135000;
1011 break;
71cd8423 1012 case DPLL_CTRL1_LINK_RATE_1620:
a8f3ef61
SJ
1013 link_clock = 162000;
1014 break;
71cd8423 1015 case DPLL_CTRL1_LINK_RATE_2160:
a8f3ef61
SJ
1016 link_clock = 216000;
1017 break;
71cd8423 1018 case DPLL_CTRL1_LINK_RATE_2700:
540e732c
S
1019 link_clock = 270000;
1020 break;
1021 default:
1022 WARN(1, "Unsupported link rate\n");
1023 break;
1024 }
1025 link_clock *= 2;
1026 }
1027
1028 pipe_config->port_clock = link_clock;
1029
398a017e 1030 ddi_dotclock_get(pipe_config);
540e732c
S
1031}
1032
3d51278a 1033static void hsw_ddi_clock_get(struct intel_encoder *encoder,
5cec258b 1034 struct intel_crtc_state *pipe_config)
11578553 1035{
fac5e23e 1036 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
11578553
JB
1037 int link_clock = 0;
1038 u32 val, pll;
1039
c856052a 1040 val = hsw_pll_to_ddi_pll_sel(pipe_config->shared_dpll);
11578553
JB
1041 switch (val & PORT_CLK_SEL_MASK) {
1042 case PORT_CLK_SEL_LCPLL_810:
1043 link_clock = 81000;
1044 break;
1045 case PORT_CLK_SEL_LCPLL_1350:
1046 link_clock = 135000;
1047 break;
1048 case PORT_CLK_SEL_LCPLL_2700:
1049 link_clock = 270000;
1050 break;
1051 case PORT_CLK_SEL_WRPLL1:
01403de3 1052 link_clock = hsw_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL(0));
11578553
JB
1053 break;
1054 case PORT_CLK_SEL_WRPLL2:
01403de3 1055 link_clock = hsw_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL(1));
11578553
JB
1056 break;
1057 case PORT_CLK_SEL_SPLL:
1058 pll = I915_READ(SPLL_CTL) & SPLL_PLL_FREQ_MASK;
1059 if (pll == SPLL_PLL_FREQ_810MHz)
1060 link_clock = 81000;
1061 else if (pll == SPLL_PLL_FREQ_1350MHz)
1062 link_clock = 135000;
1063 else if (pll == SPLL_PLL_FREQ_2700MHz)
1064 link_clock = 270000;
1065 else {
1066 WARN(1, "bad spll freq\n");
1067 return;
1068 }
1069 break;
1070 default:
1071 WARN(1, "bad port clock sel\n");
1072 return;
1073 }
1074
1075 pipe_config->port_clock = link_clock * 2;
1076
398a017e 1077 ddi_dotclock_get(pipe_config);
11578553
JB
1078}
1079
977bb38d
S
1080static int bxt_calc_pll_link(struct drm_i915_private *dev_priv,
1081 enum intel_dpll_id dpll)
1082{
aa610dcb
ID
1083 struct intel_shared_dpll *pll;
1084 struct intel_dpll_hw_state *state;
9e2c8475 1085 struct dpll clock;
aa610dcb
ID
1086
1087 /* For DDI ports we always use a shared PLL. */
1088 if (WARN_ON(dpll == DPLL_ID_PRIVATE))
1089 return 0;
1090
1091 pll = &dev_priv->shared_dplls[dpll];
2c42e535 1092 state = &pll->state.hw_state;
aa610dcb
ID
1093
1094 clock.m1 = 2;
1095 clock.m2 = (state->pll0 & PORT_PLL_M2_MASK) << 22;
1096 if (state->pll3 & PORT_PLL_M2_FRAC_ENABLE)
1097 clock.m2 |= state->pll2 & PORT_PLL_M2_FRAC_MASK;
1098 clock.n = (state->pll1 & PORT_PLL_N_MASK) >> PORT_PLL_N_SHIFT;
1099 clock.p1 = (state->ebb0 & PORT_PLL_P1_MASK) >> PORT_PLL_P1_SHIFT;
1100 clock.p2 = (state->ebb0 & PORT_PLL_P2_MASK) >> PORT_PLL_P2_SHIFT;
1101
1102 return chv_calc_dpll_params(100000, &clock);
977bb38d
S
1103}
1104
1105static void bxt_ddi_clock_get(struct intel_encoder *encoder,
1106 struct intel_crtc_state *pipe_config)
1107{
fac5e23e 1108 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
977bb38d
S
1109 enum port port = intel_ddi_get_encoder_port(encoder);
1110 uint32_t dpll = port;
1111
398a017e 1112 pipe_config->port_clock = bxt_calc_pll_link(dev_priv, dpll);
977bb38d 1113
398a017e 1114 ddi_dotclock_get(pipe_config);
977bb38d
S
1115}
1116
3d51278a 1117void intel_ddi_clock_get(struct intel_encoder *encoder,
5cec258b 1118 struct intel_crtc_state *pipe_config)
3d51278a 1119{
0853723b 1120 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
22606a18 1121
0853723b 1122 if (INTEL_GEN(dev_priv) <= 8)
22606a18 1123 hsw_ddi_clock_get(encoder, pipe_config);
b976dc53 1124 else if (IS_GEN9_BC(dev_priv))
22606a18 1125 skl_ddi_clock_get(encoder, pipe_config);
cc3f90f0 1126 else if (IS_GEN9_LP(dev_priv))
977bb38d 1127 bxt_ddi_clock_get(encoder, pipe_config);
3d51278a
DV
1128}
1129
0220ab6e
DL
1130/*
1131 * Tries to find a *shared* PLL for the CRTC and store it in
1132 * intel_crtc->ddi_pll_sel.
1133 *
1134 * For private DPLLs, compute_config() should do the selection for us. This
1135 * function should be folded into compute_config() eventually.
1136 */
190f68c5
ACO
1137bool intel_ddi_pll_select(struct intel_crtc *intel_crtc,
1138 struct intel_crtc_state *crtc_state)
0220ab6e 1139{
1524e93e 1140 struct intel_encoder *encoder =
3165c074 1141 intel_ddi_get_crtc_new_encoder(crtc_state);
396a1200 1142 struct intel_shared_dpll *pll;
0220ab6e 1143
396a1200
PZ
1144 pll = intel_get_shared_dpll(intel_crtc, crtc_state, encoder);
1145 if (!pll)
1146 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
1147 pipe_name(intel_crtc->pipe));
1148
1149 return pll != NULL;
0220ab6e
DL
1150}
1151
3dc38eea 1152void intel_ddi_set_pipe_settings(const struct intel_crtc_state *crtc_state)
dae84799 1153{
3dc38eea 1154 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
e9ce1a62 1155 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1524e93e 1156 struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
3dc38eea 1157 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1524e93e 1158 int type = encoder->type;
dae84799
PZ
1159 uint32_t temp;
1160
cca0502b 1161 if (type == INTEL_OUTPUT_DP || type == INTEL_OUTPUT_EDP || type == INTEL_OUTPUT_DP_MST) {
4d1de975
JN
1162 WARN_ON(transcoder_is_dsi(cpu_transcoder));
1163
c9809791 1164 temp = TRANS_MSA_SYNC_CLK;
3dc38eea 1165 switch (crtc_state->pipe_bpp) {
dae84799 1166 case 18:
c9809791 1167 temp |= TRANS_MSA_6_BPC;
dae84799
PZ
1168 break;
1169 case 24:
c9809791 1170 temp |= TRANS_MSA_8_BPC;
dae84799
PZ
1171 break;
1172 case 30:
c9809791 1173 temp |= TRANS_MSA_10_BPC;
dae84799
PZ
1174 break;
1175 case 36:
c9809791 1176 temp |= TRANS_MSA_12_BPC;
dae84799
PZ
1177 break;
1178 default:
4e53c2e0 1179 BUG();
dae84799 1180 }
c9809791 1181 I915_WRITE(TRANS_MSA_MISC(cpu_transcoder), temp);
dae84799
PZ
1182 }
1183}
1184
3dc38eea
ACO
1185void intel_ddi_set_vc_payload_alloc(const struct intel_crtc_state *crtc_state,
1186 bool state)
0e32b39c 1187{
3dc38eea 1188 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
e9ce1a62 1189 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3dc38eea 1190 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
0e32b39c
DA
1191 uint32_t temp;
1192 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1193 if (state == true)
1194 temp |= TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1195 else
1196 temp &= ~TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1197 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
1198}
1199
3dc38eea 1200void intel_ddi_enable_transcoder_func(const struct intel_crtc_state *crtc_state)
8d9ddbcb 1201{
3dc38eea 1202 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1524e93e 1203 struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
e9ce1a62
ACO
1204 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1205 enum pipe pipe = crtc->pipe;
3dc38eea 1206 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1524e93e
SS
1207 enum port port = intel_ddi_get_encoder_port(encoder);
1208 int type = encoder->type;
8d9ddbcb
PZ
1209 uint32_t temp;
1210
ad80a810
PZ
1211 /* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
1212 temp = TRANS_DDI_FUNC_ENABLE;
174edf1f 1213 temp |= TRANS_DDI_SELECT_PORT(port);
dfcef252 1214
3dc38eea 1215 switch (crtc_state->pipe_bpp) {
dfcef252 1216 case 18:
ad80a810 1217 temp |= TRANS_DDI_BPC_6;
dfcef252
PZ
1218 break;
1219 case 24:
ad80a810 1220 temp |= TRANS_DDI_BPC_8;
dfcef252
PZ
1221 break;
1222 case 30:
ad80a810 1223 temp |= TRANS_DDI_BPC_10;
dfcef252
PZ
1224 break;
1225 case 36:
ad80a810 1226 temp |= TRANS_DDI_BPC_12;
dfcef252
PZ
1227 break;
1228 default:
4e53c2e0 1229 BUG();
dfcef252 1230 }
72662e10 1231
3dc38eea 1232 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
ad80a810 1233 temp |= TRANS_DDI_PVSYNC;
3dc38eea 1234 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
ad80a810 1235 temp |= TRANS_DDI_PHSYNC;
f63eb7c4 1236
e6f0bfc4
PZ
1237 if (cpu_transcoder == TRANSCODER_EDP) {
1238 switch (pipe) {
1239 case PIPE_A:
c7670b10
PZ
1240 /* On Haswell, can only use the always-on power well for
1241 * eDP when not using the panel fitter, and when not
1242 * using motion blur mitigation (which we don't
1243 * support). */
772c2a51 1244 if (IS_HASWELL(dev_priv) &&
3dc38eea
ACO
1245 (crtc_state->pch_pfit.enabled ||
1246 crtc_state->pch_pfit.force_thru))
d6dd9eb1
DV
1247 temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
1248 else
1249 temp |= TRANS_DDI_EDP_INPUT_A_ON;
e6f0bfc4
PZ
1250 break;
1251 case PIPE_B:
1252 temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
1253 break;
1254 case PIPE_C:
1255 temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
1256 break;
1257 default:
1258 BUG();
1259 break;
1260 }
1261 }
1262
7739c33b 1263 if (type == INTEL_OUTPUT_HDMI) {
3dc38eea 1264 if (crtc_state->has_hdmi_sink)
ad80a810 1265 temp |= TRANS_DDI_MODE_SELECT_HDMI;
8d9ddbcb 1266 else
ad80a810 1267 temp |= TRANS_DDI_MODE_SELECT_DVI;
7739c33b 1268 } else if (type == INTEL_OUTPUT_ANALOG) {
ad80a810 1269 temp |= TRANS_DDI_MODE_SELECT_FDI;
3dc38eea 1270 temp |= (crtc_state->fdi_lanes - 1) << 1;
cca0502b 1271 } else if (type == INTEL_OUTPUT_DP ||
7739c33b 1272 type == INTEL_OUTPUT_EDP) {
64ee2fd2 1273 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
3dc38eea 1274 temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
0e32b39c 1275 } else if (type == INTEL_OUTPUT_DP_MST) {
64ee2fd2 1276 temp |= TRANS_DDI_MODE_SELECT_DP_MST;
3dc38eea 1277 temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
8d9ddbcb 1278 } else {
84f44ce7 1279 WARN(1, "Invalid encoder type %d for pipe %c\n",
1524e93e 1280 encoder->type, pipe_name(pipe));
8d9ddbcb
PZ
1281 }
1282
ad80a810 1283 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
8d9ddbcb 1284}
72662e10 1285
ad80a810
PZ
1286void intel_ddi_disable_transcoder_func(struct drm_i915_private *dev_priv,
1287 enum transcoder cpu_transcoder)
8d9ddbcb 1288{
f0f59a00 1289 i915_reg_t reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
8d9ddbcb
PZ
1290 uint32_t val = I915_READ(reg);
1291
0e32b39c 1292 val &= ~(TRANS_DDI_FUNC_ENABLE | TRANS_DDI_PORT_MASK | TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
ad80a810 1293 val |= TRANS_DDI_PORT_NONE;
8d9ddbcb 1294 I915_WRITE(reg, val);
72662e10
ED
1295}
1296
bcbc889b
PZ
1297bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
1298{
1299 struct drm_device *dev = intel_connector->base.dev;
fac5e23e 1300 struct drm_i915_private *dev_priv = to_i915(dev);
1524e93e 1301 struct intel_encoder *encoder = intel_connector->encoder;
bcbc889b 1302 int type = intel_connector->base.connector_type;
1524e93e 1303 enum port port = intel_ddi_get_encoder_port(encoder);
bcbc889b
PZ
1304 enum pipe pipe = 0;
1305 enum transcoder cpu_transcoder;
1306 uint32_t tmp;
e27daab4 1307 bool ret;
bcbc889b 1308
79f255a0 1309 if (!intel_display_power_get_if_enabled(dev_priv,
1524e93e 1310 encoder->power_domain))
882244a3
PZ
1311 return false;
1312
1524e93e 1313 if (!encoder->get_hw_state(encoder, &pipe)) {
e27daab4
ID
1314 ret = false;
1315 goto out;
1316 }
bcbc889b
PZ
1317
1318 if (port == PORT_A)
1319 cpu_transcoder = TRANSCODER_EDP;
1320 else
1a240d4d 1321 cpu_transcoder = (enum transcoder) pipe;
bcbc889b
PZ
1322
1323 tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1324
1325 switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
1326 case TRANS_DDI_MODE_SELECT_HDMI:
1327 case TRANS_DDI_MODE_SELECT_DVI:
e27daab4
ID
1328 ret = type == DRM_MODE_CONNECTOR_HDMIA;
1329 break;
bcbc889b
PZ
1330
1331 case TRANS_DDI_MODE_SELECT_DP_SST:
e27daab4
ID
1332 ret = type == DRM_MODE_CONNECTOR_eDP ||
1333 type == DRM_MODE_CONNECTOR_DisplayPort;
1334 break;
1335
0e32b39c
DA
1336 case TRANS_DDI_MODE_SELECT_DP_MST:
1337 /* if the transcoder is in MST state then
1338 * connector isn't connected */
e27daab4
ID
1339 ret = false;
1340 break;
bcbc889b
PZ
1341
1342 case TRANS_DDI_MODE_SELECT_FDI:
e27daab4
ID
1343 ret = type == DRM_MODE_CONNECTOR_VGA;
1344 break;
bcbc889b
PZ
1345
1346 default:
e27daab4
ID
1347 ret = false;
1348 break;
bcbc889b 1349 }
e27daab4
ID
1350
1351out:
1524e93e 1352 intel_display_power_put(dev_priv, encoder->power_domain);
e27daab4
ID
1353
1354 return ret;
bcbc889b
PZ
1355}
1356
85234cdc
DV
1357bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
1358 enum pipe *pipe)
1359{
1360 struct drm_device *dev = encoder->base.dev;
fac5e23e 1361 struct drm_i915_private *dev_priv = to_i915(dev);
fe43d3f5 1362 enum port port = intel_ddi_get_encoder_port(encoder);
85234cdc
DV
1363 u32 tmp;
1364 int i;
e27daab4 1365 bool ret;
85234cdc 1366
79f255a0
ACO
1367 if (!intel_display_power_get_if_enabled(dev_priv,
1368 encoder->power_domain))
6d129bea
ID
1369 return false;
1370
e27daab4
ID
1371 ret = false;
1372
fe43d3f5 1373 tmp = I915_READ(DDI_BUF_CTL(port));
85234cdc
DV
1374
1375 if (!(tmp & DDI_BUF_CTL_ENABLE))
e27daab4 1376 goto out;
85234cdc 1377
ad80a810
PZ
1378 if (port == PORT_A) {
1379 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
85234cdc 1380
ad80a810
PZ
1381 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1382 case TRANS_DDI_EDP_INPUT_A_ON:
1383 case TRANS_DDI_EDP_INPUT_A_ONOFF:
1384 *pipe = PIPE_A;
1385 break;
1386 case TRANS_DDI_EDP_INPUT_B_ONOFF:
1387 *pipe = PIPE_B;
1388 break;
1389 case TRANS_DDI_EDP_INPUT_C_ONOFF:
1390 *pipe = PIPE_C;
1391 break;
1392 }
1393
e27daab4 1394 ret = true;
ad80a810 1395
e27daab4
ID
1396 goto out;
1397 }
0e32b39c 1398
e27daab4
ID
1399 for (i = TRANSCODER_A; i <= TRANSCODER_C; i++) {
1400 tmp = I915_READ(TRANS_DDI_FUNC_CTL(i));
1401
1402 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(port)) {
1403 if ((tmp & TRANS_DDI_MODE_SELECT_MASK) ==
1404 TRANS_DDI_MODE_SELECT_DP_MST)
1405 goto out;
1406
1407 *pipe = i;
1408 ret = true;
1409
1410 goto out;
85234cdc
DV
1411 }
1412 }
1413
84f44ce7 1414 DRM_DEBUG_KMS("No pipe for ddi port %c found\n", port_name(port));
85234cdc 1415
e27daab4 1416out:
cc3f90f0 1417 if (ret && IS_GEN9_LP(dev_priv)) {
e93da0a0
ID
1418 tmp = I915_READ(BXT_PHY_CTL(port));
1419 if ((tmp & (BXT_PHY_LANE_POWERDOWN_ACK |
1420 BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
1421 DRM_ERROR("Port %c enabled but PHY powered down? "
1422 "(PHY_CTL %08x)\n", port_name(port), tmp);
1423 }
1424
79f255a0 1425 intel_display_power_put(dev_priv, encoder->power_domain);
e27daab4
ID
1426
1427 return ret;
85234cdc
DV
1428}
1429
62b69566
ACO
1430static u64 intel_ddi_get_power_domains(struct intel_encoder *encoder)
1431{
1432 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
1433 enum pipe pipe;
1434
1435 if (intel_ddi_get_hw_state(encoder, &pipe))
1436 return BIT_ULL(dig_port->ddi_io_power_domain);
1437
1438 return 0;
1439}
1440
3dc38eea 1441void intel_ddi_enable_pipe_clock(const struct intel_crtc_state *crtc_state)
fc914639 1442{
3dc38eea 1443 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
e9ce1a62 1444 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1524e93e
SS
1445 struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
1446 enum port port = intel_ddi_get_encoder_port(encoder);
3dc38eea 1447 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
fc914639 1448
bb523fc0
PZ
1449 if (cpu_transcoder != TRANSCODER_EDP)
1450 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1451 TRANS_CLK_SEL_PORT(port));
fc914639
PZ
1452}
1453
3dc38eea 1454void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
fc914639 1455{
3dc38eea
ACO
1456 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1457 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
fc914639 1458
bb523fc0
PZ
1459 if (cpu_transcoder != TRANSCODER_EDP)
1460 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1461 TRANS_CLK_SEL_DISABLED);
fc914639
PZ
1462}
1463
a7d8dbc0
VS
1464static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
1465 enum port port, uint8_t iboost)
f8896f5d 1466{
a7d8dbc0
VS
1467 u32 tmp;
1468
1469 tmp = I915_READ(DISPIO_CR_TX_BMU_CR0);
1470 tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
1471 if (iboost)
1472 tmp |= iboost << BALANCE_LEG_SHIFT(port);
1473 else
1474 tmp |= BALANCE_LEG_DISABLE(port);
1475 I915_WRITE(DISPIO_CR_TX_BMU_CR0, tmp);
1476}
1477
1478static void skl_ddi_set_iboost(struct intel_encoder *encoder, u32 level)
1479{
1480 struct intel_digital_port *intel_dig_port = enc_to_dig_port(&encoder->base);
1481 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
1482 enum port port = intel_dig_port->port;
1483 int type = encoder->type;
f8896f5d
DW
1484 const struct ddi_buf_trans *ddi_translations;
1485 uint8_t iboost;
75067dde 1486 uint8_t dp_iboost, hdmi_iboost;
f8896f5d 1487 int n_entries;
f8896f5d 1488
75067dde
AK
1489 /* VBT may override standard boost values */
1490 dp_iboost = dev_priv->vbt.ddi_port_info[port].dp_boost_level;
1491 hdmi_iboost = dev_priv->vbt.ddi_port_info[port].hdmi_boost_level;
1492
cca0502b 1493 if (type == INTEL_OUTPUT_DP) {
75067dde
AK
1494 if (dp_iboost) {
1495 iboost = dp_iboost;
1496 } else {
0fdd4918
RV
1497 if (IS_KABYLAKE(dev_priv))
1498 ddi_translations = kbl_get_buf_trans_dp(dev_priv,
1499 &n_entries);
1500 else
1501 ddi_translations = skl_get_buf_trans_dp(dev_priv,
1502 &n_entries);
e4d4c05b 1503 iboost = ddi_translations[level].i_boost;
75067dde 1504 }
f8896f5d 1505 } else if (type == INTEL_OUTPUT_EDP) {
75067dde
AK
1506 if (dp_iboost) {
1507 iboost = dp_iboost;
1508 } else {
78ab0bae 1509 ddi_translations = skl_get_buf_trans_edp(dev_priv, &n_entries);
10afa0b6
VS
1510
1511 if (WARN_ON(port != PORT_A &&
1512 port != PORT_E && n_entries > 9))
1513 n_entries = 9;
1514
e4d4c05b 1515 iboost = ddi_translations[level].i_boost;
75067dde 1516 }
f8896f5d 1517 } else if (type == INTEL_OUTPUT_HDMI) {
75067dde
AK
1518 if (hdmi_iboost) {
1519 iboost = hdmi_iboost;
1520 } else {
78ab0bae 1521 ddi_translations = skl_get_buf_trans_hdmi(dev_priv, &n_entries);
e4d4c05b 1522 iboost = ddi_translations[level].i_boost;
75067dde 1523 }
f8896f5d
DW
1524 } else {
1525 return;
1526 }
1527
1528 /* Make sure that the requested I_boost is valid */
1529 if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
1530 DRM_ERROR("Invalid I_boost value %u\n", iboost);
1531 return;
1532 }
1533
a7d8dbc0 1534 _skl_ddi_set_iboost(dev_priv, port, iboost);
f8896f5d 1535
a7d8dbc0
VS
1536 if (port == PORT_A && intel_dig_port->max_lanes == 4)
1537 _skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
f8896f5d
DW
1538}
1539
78ab0bae
VS
1540static void bxt_ddi_vswing_sequence(struct drm_i915_private *dev_priv,
1541 u32 level, enum port port, int type)
96fb9f9b 1542{
96fb9f9b
VK
1543 const struct bxt_ddi_buf_trans *ddi_translations;
1544 u32 n_entries, i;
96fb9f9b 1545
06411f08 1546 if (type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.low_vswing) {
d9d7000d
SJ
1547 n_entries = ARRAY_SIZE(bxt_ddi_translations_edp);
1548 ddi_translations = bxt_ddi_translations_edp;
cca0502b 1549 } else if (type == INTEL_OUTPUT_DP
d9d7000d 1550 || type == INTEL_OUTPUT_EDP) {
96fb9f9b
VK
1551 n_entries = ARRAY_SIZE(bxt_ddi_translations_dp);
1552 ddi_translations = bxt_ddi_translations_dp;
1553 } else if (type == INTEL_OUTPUT_HDMI) {
1554 n_entries = ARRAY_SIZE(bxt_ddi_translations_hdmi);
1555 ddi_translations = bxt_ddi_translations_hdmi;
1556 } else {
1557 DRM_DEBUG_KMS("Vswing programming not done for encoder %d\n",
1558 type);
1559 return;
1560 }
1561
1562 /* Check if default value has to be used */
1563 if (level >= n_entries ||
1564 (type == INTEL_OUTPUT_HDMI && level == HDMI_LEVEL_SHIFT_UNKNOWN)) {
1565 for (i = 0; i < n_entries; i++) {
1566 if (ddi_translations[i].default_index) {
1567 level = i;
1568 break;
1569 }
1570 }
1571 }
1572
b6e08203
ACO
1573 bxt_ddi_phy_set_signal_level(dev_priv, port,
1574 ddi_translations[level].margin,
1575 ddi_translations[level].scale,
1576 ddi_translations[level].enable,
1577 ddi_translations[level].deemphasis);
96fb9f9b
VK
1578}
1579
ffe5111e
VS
1580u8 intel_ddi_dp_voltage_max(struct intel_encoder *encoder)
1581{
1582 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1583 int n_entries;
1584
1585 if (encoder->type == INTEL_OUTPUT_EDP)
1586 intel_ddi_get_buf_trans_edp(dev_priv, &n_entries);
1587 else
1588 intel_ddi_get_buf_trans_dp(dev_priv, &n_entries);
1589
1590 if (WARN_ON(n_entries < 1))
1591 n_entries = 1;
1592 if (WARN_ON(n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
1593 n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
1594
1595 return index_to_dp_signal_levels[n_entries - 1] &
1596 DP_TRAIN_VOLTAGE_SWING_MASK;
1597}
1598
f8896f5d
DW
1599static uint32_t translate_signal_level(int signal_levels)
1600{
97eeb872 1601 int i;
f8896f5d 1602
97eeb872
VS
1603 for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
1604 if (index_to_dp_signal_levels[i] == signal_levels)
1605 return i;
f8896f5d
DW
1606 }
1607
97eeb872
VS
1608 WARN(1, "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
1609 signal_levels);
1610
1611 return 0;
f8896f5d
DW
1612}
1613
1614uint32_t ddi_signal_levels(struct intel_dp *intel_dp)
1615{
1616 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
78ab0bae 1617 struct drm_i915_private *dev_priv = to_i915(dport->base.base.dev);
f8896f5d
DW
1618 struct intel_encoder *encoder = &dport->base;
1619 uint8_t train_set = intel_dp->train_set[0];
1620 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1621 DP_TRAIN_PRE_EMPHASIS_MASK);
1622 enum port port = dport->port;
1623 uint32_t level;
1624
1625 level = translate_signal_level(signal_levels);
1626
b976dc53 1627 if (IS_GEN9_BC(dev_priv))
a7d8dbc0 1628 skl_ddi_set_iboost(encoder, level);
cc3f90f0 1629 else if (IS_GEN9_LP(dev_priv))
78ab0bae 1630 bxt_ddi_vswing_sequence(dev_priv, level, port, encoder->type);
f8896f5d
DW
1631
1632 return DDI_BUF_TRANS_SELECT(level);
1633}
1634
e404ba8d 1635void intel_ddi_clk_select(struct intel_encoder *encoder,
c856052a 1636 struct intel_shared_dpll *pll)
6441ab5f 1637{
e404ba8d
VS
1638 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1639 enum port port = intel_ddi_get_encoder_port(encoder);
6441ab5f 1640
c856052a
ACO
1641 if (WARN_ON(!pll))
1642 return;
1643
b976dc53 1644 if (IS_GEN9_BC(dev_priv)) {
efa80add
S
1645 uint32_t val;
1646
5416d871 1647 /* DDI -> PLL mapping */
efa80add
S
1648 val = I915_READ(DPLL_CTRL2);
1649
1650 val &= ~(DPLL_CTRL2_DDI_CLK_OFF(port) |
1651 DPLL_CTRL2_DDI_CLK_SEL_MASK(port));
c856052a 1652 val |= (DPLL_CTRL2_DDI_CLK_SEL(pll->id, port) |
efa80add
S
1653 DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
1654
1655 I915_WRITE(DPLL_CTRL2, val);
5416d871 1656
e404ba8d 1657 } else if (INTEL_INFO(dev_priv)->gen < 9) {
c856052a 1658 I915_WRITE(PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
efa80add 1659 }
e404ba8d
VS
1660}
1661
ba88d153
MN
1662static void intel_ddi_pre_enable_dp(struct intel_encoder *encoder,
1663 int link_rate, uint32_t lane_count,
1664 struct intel_shared_dpll *pll,
1665 bool link_mst)
e404ba8d 1666{
ba88d153
MN
1667 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1668 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1669 enum port port = intel_ddi_get_encoder_port(encoder);
62b69566 1670 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
b2ccb822 1671
e081c846
ACO
1672 WARN_ON(link_mst && (port == PORT_A || port == PORT_E));
1673
ba88d153
MN
1674 intel_dp_set_link_params(intel_dp, link_rate, lane_count,
1675 link_mst);
1676 if (encoder->type == INTEL_OUTPUT_EDP)
e404ba8d 1677 intel_edp_panel_on(intel_dp);
32bdc400 1678
ba88d153 1679 intel_ddi_clk_select(encoder, pll);
62b69566
ACO
1680
1681 intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
1682
ba88d153
MN
1683 intel_prepare_dp_ddi_buffers(encoder);
1684 intel_ddi_init_dp_buf_reg(encoder);
1685 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
1686 intel_dp_start_link_train(intel_dp);
1687 if (port != PORT_A || INTEL_GEN(dev_priv) >= 9)
1688 intel_dp_stop_link_train(intel_dp);
1689}
901c2daf 1690
ba88d153
MN
1691static void intel_ddi_pre_enable_hdmi(struct intel_encoder *encoder,
1692 bool has_hdmi_sink,
ac240288
ML
1693 const struct intel_crtc_state *crtc_state,
1694 const struct drm_connector_state *conn_state,
ba88d153
MN
1695 struct intel_shared_dpll *pll)
1696{
1697 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(&encoder->base);
1698 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1699 struct drm_encoder *drm_encoder = &encoder->base;
1700 enum port port = intel_ddi_get_encoder_port(encoder);
1701 int level = intel_ddi_hdmi_level(dev_priv, port);
62b69566 1702 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
c19b0669 1703
ba88d153
MN
1704 intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
1705 intel_ddi_clk_select(encoder, pll);
62b69566
ACO
1706
1707 intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
1708
ba88d153 1709 intel_prepare_hdmi_ddi_buffers(encoder);
b976dc53 1710 if (IS_GEN9_BC(dev_priv))
ba88d153 1711 skl_ddi_set_iboost(encoder, level);
cc3f90f0 1712 else if (IS_GEN9_LP(dev_priv))
ba88d153
MN
1713 bxt_ddi_vswing_sequence(dev_priv, level, port,
1714 INTEL_OUTPUT_HDMI);
8d8bb85e 1715
ba88d153
MN
1716 intel_hdmi->set_infoframes(drm_encoder,
1717 has_hdmi_sink,
ac240288 1718 crtc_state, conn_state);
ba88d153 1719}
32bdc400 1720
1524e93e 1721static void intel_ddi_pre_enable(struct intel_encoder *encoder,
ba88d153
MN
1722 struct intel_crtc_state *pipe_config,
1723 struct drm_connector_state *conn_state)
1724{
1524e93e 1725 int type = encoder->type;
30cf6db8 1726
ba88d153 1727 if (type == INTEL_OUTPUT_DP || type == INTEL_OUTPUT_EDP) {
1524e93e 1728 intel_ddi_pre_enable_dp(encoder,
3dc38eea
ACO
1729 pipe_config->port_clock,
1730 pipe_config->lane_count,
1731 pipe_config->shared_dpll,
1732 intel_crtc_has_type(pipe_config,
ba88d153
MN
1733 INTEL_OUTPUT_DP_MST));
1734 }
1735 if (type == INTEL_OUTPUT_HDMI) {
1524e93e 1736 intel_ddi_pre_enable_hdmi(encoder,
ac240288
ML
1737 pipe_config->has_hdmi_sink,
1738 pipe_config, conn_state,
3dc38eea 1739 pipe_config->shared_dpll);
c19b0669 1740 }
6441ab5f
PZ
1741}
1742
fd6bbda9
ML
1743static void intel_ddi_post_disable(struct intel_encoder *intel_encoder,
1744 struct intel_crtc_state *old_crtc_state,
1745 struct drm_connector_state *old_conn_state)
6441ab5f
PZ
1746{
1747 struct drm_encoder *encoder = &intel_encoder->base;
66478475 1748 struct drm_i915_private *dev_priv = to_i915(encoder->dev);
6441ab5f 1749 enum port port = intel_ddi_get_encoder_port(intel_encoder);
62b69566 1750 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
82a4d9c0 1751 int type = intel_encoder->type;
2886e93f 1752 uint32_t val;
a836bdf9 1753 bool wait = false;
2886e93f 1754
fd6bbda9
ML
1755 /* old_crtc_state and old_conn_state are NULL when called from DP_MST */
1756
2886e93f
PZ
1757 val = I915_READ(DDI_BUF_CTL(port));
1758 if (val & DDI_BUF_CTL_ENABLE) {
1759 val &= ~DDI_BUF_CTL_ENABLE;
1760 I915_WRITE(DDI_BUF_CTL(port), val);
a836bdf9 1761 wait = true;
2886e93f 1762 }
6441ab5f 1763
a836bdf9
PZ
1764 val = I915_READ(DP_TP_CTL(port));
1765 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1766 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
1767 I915_WRITE(DP_TP_CTL(port), val);
1768
1769 if (wait)
1770 intel_wait_ddi_buf_idle(dev_priv, port);
1771
cca0502b 1772 if (type == INTEL_OUTPUT_DP || type == INTEL_OUTPUT_EDP) {
82a4d9c0 1773 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
76bb80ed 1774 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
24f3e092 1775 intel_edp_panel_vdd_on(intel_dp);
4be73780 1776 intel_edp_panel_off(intel_dp);
82a4d9c0
PZ
1777 }
1778
62b69566
ACO
1779 if (dig_port)
1780 intel_display_power_put(dev_priv, dig_port->ddi_io_power_domain);
1781
b976dc53 1782 if (IS_GEN9_BC(dev_priv))
efa80add
S
1783 I915_WRITE(DPLL_CTRL2, (I915_READ(DPLL_CTRL2) |
1784 DPLL_CTRL2_DDI_CLK_OFF(port)));
66478475 1785 else if (INTEL_GEN(dev_priv) < 9)
efa80add 1786 I915_WRITE(PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
b2ccb822
VS
1787
1788 if (type == INTEL_OUTPUT_HDMI) {
1789 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
1790
1791 intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
1792 }
6441ab5f
PZ
1793}
1794
1524e93e 1795void intel_ddi_fdi_post_disable(struct intel_encoder *encoder,
b7076546
ML
1796 struct intel_crtc_state *old_crtc_state,
1797 struct drm_connector_state *old_conn_state)
1798{
1524e93e 1799 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
b7076546
ML
1800 uint32_t val;
1801
1802 /*
1803 * Bspec lists this as both step 13 (before DDI_BUF_CTL disable)
1804 * and step 18 (after clearing PORT_CLK_SEL). Based on a BUN,
1805 * step 13 is the correct place for it. Step 18 is where it was
1806 * originally before the BUN.
1807 */
1808 val = I915_READ(FDI_RX_CTL(PIPE_A));
1809 val &= ~FDI_RX_ENABLE;
1810 I915_WRITE(FDI_RX_CTL(PIPE_A), val);
1811
1524e93e 1812 intel_ddi_post_disable(encoder, old_crtc_state, old_conn_state);
b7076546
ML
1813
1814 val = I915_READ(FDI_RX_MISC(PIPE_A));
1815 val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1816 val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
1817 I915_WRITE(FDI_RX_MISC(PIPE_A), val);
1818
1819 val = I915_READ(FDI_RX_CTL(PIPE_A));
1820 val &= ~FDI_PCDCLK;
1821 I915_WRITE(FDI_RX_CTL(PIPE_A), val);
1822
1823 val = I915_READ(FDI_RX_CTL(PIPE_A));
1824 val &= ~FDI_RX_PLL_ENABLE;
1825 I915_WRITE(FDI_RX_CTL(PIPE_A), val);
1826}
1827
fd6bbda9
ML
1828static void intel_enable_ddi(struct intel_encoder *intel_encoder,
1829 struct intel_crtc_state *pipe_config,
1830 struct drm_connector_state *conn_state)
72662e10 1831{
6547fef8 1832 struct drm_encoder *encoder = &intel_encoder->base;
66478475 1833 struct drm_i915_private *dev_priv = to_i915(encoder->dev);
6547fef8
PZ
1834 enum port port = intel_ddi_get_encoder_port(intel_encoder);
1835 int type = intel_encoder->type;
72662e10 1836
6547fef8 1837 if (type == INTEL_OUTPUT_HDMI) {
876a8cdf
DL
1838 struct intel_digital_port *intel_dig_port =
1839 enc_to_dig_port(encoder);
1840
6547fef8
PZ
1841 /* In HDMI/DVI mode, the port width, and swing/emphasis values
1842 * are ignored so nothing special needs to be done besides
1843 * enabling the port.
1844 */
876a8cdf 1845 I915_WRITE(DDI_BUF_CTL(port),
bcf53de4
SM
1846 intel_dig_port->saved_port_bits |
1847 DDI_BUF_CTL_ENABLE);
d6c50ff8
PZ
1848 } else if (type == INTEL_OUTPUT_EDP) {
1849 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1850
66478475 1851 if (port == PORT_A && INTEL_GEN(dev_priv) < 9)
3ab9c637
ID
1852 intel_dp_stop_link_train(intel_dp);
1853
4be73780 1854 intel_edp_backlight_on(intel_dp);
0bc12bcb 1855 intel_psr_enable(intel_dp);
85cb48a1 1856 intel_edp_drrs_enable(intel_dp, pipe_config);
6547fef8 1857 }
7b9f35a6 1858
37255d8d 1859 if (pipe_config->has_audio)
bbf35e9d 1860 intel_audio_codec_enable(intel_encoder, pipe_config, conn_state);
5ab432ef
DV
1861}
1862
fd6bbda9
ML
1863static void intel_disable_ddi(struct intel_encoder *intel_encoder,
1864 struct intel_crtc_state *old_crtc_state,
1865 struct drm_connector_state *old_conn_state)
5ab432ef 1866{
d6c50ff8
PZ
1867 struct drm_encoder *encoder = &intel_encoder->base;
1868 int type = intel_encoder->type;
1869
37255d8d 1870 if (old_crtc_state->has_audio)
69bfe1a9 1871 intel_audio_codec_disable(intel_encoder);
2831d842 1872
d6c50ff8
PZ
1873 if (type == INTEL_OUTPUT_EDP) {
1874 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1875
85cb48a1 1876 intel_edp_drrs_disable(intel_dp, old_crtc_state);
0bc12bcb 1877 intel_psr_disable(intel_dp);
4be73780 1878 intel_edp_backlight_off(intel_dp);
d6c50ff8 1879 }
72662e10 1880}
79f689aa 1881
fd6bbda9
ML
1882static void bxt_ddi_pre_pll_enable(struct intel_encoder *encoder,
1883 struct intel_crtc_state *pipe_config,
1884 struct drm_connector_state *conn_state)
95a7a2ae 1885{
3dc38eea 1886 uint8_t mask = pipe_config->lane_lat_optim_mask;
95a7a2ae 1887
47a6bc61 1888 bxt_ddi_phy_set_lane_optim_mask(encoder, mask);
95a7a2ae
ID
1889}
1890
ad64217b 1891void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp)
c19b0669 1892{
ad64217b
ACO
1893 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1894 struct drm_i915_private *dev_priv =
1895 to_i915(intel_dig_port->base.base.dev);
174edf1f 1896 enum port port = intel_dig_port->port;
c19b0669 1897 uint32_t val;
f3e227df 1898 bool wait = false;
c19b0669
PZ
1899
1900 if (I915_READ(DP_TP_CTL(port)) & DP_TP_CTL_ENABLE) {
1901 val = I915_READ(DDI_BUF_CTL(port));
1902 if (val & DDI_BUF_CTL_ENABLE) {
1903 val &= ~DDI_BUF_CTL_ENABLE;
1904 I915_WRITE(DDI_BUF_CTL(port), val);
1905 wait = true;
1906 }
1907
1908 val = I915_READ(DP_TP_CTL(port));
1909 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1910 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
1911 I915_WRITE(DP_TP_CTL(port), val);
1912 POSTING_READ(DP_TP_CTL(port));
1913
1914 if (wait)
1915 intel_wait_ddi_buf_idle(dev_priv, port);
1916 }
1917
0e32b39c 1918 val = DP_TP_CTL_ENABLE |
c19b0669 1919 DP_TP_CTL_LINK_TRAIN_PAT1 | DP_TP_CTL_SCRAMBLE_DISABLE;
64ee2fd2 1920 if (intel_dp->link_mst)
0e32b39c
DA
1921 val |= DP_TP_CTL_MODE_MST;
1922 else {
1923 val |= DP_TP_CTL_MODE_SST;
1924 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
1925 val |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
1926 }
c19b0669
PZ
1927 I915_WRITE(DP_TP_CTL(port), val);
1928 POSTING_READ(DP_TP_CTL(port));
1929
1930 intel_dp->DP |= DDI_BUF_CTL_ENABLE;
1931 I915_WRITE(DDI_BUF_CTL(port), intel_dp->DP);
1932 POSTING_READ(DDI_BUF_CTL(port));
1933
1934 udelay(600);
1935}
00c09d70 1936
9935f7fa
LY
1937bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
1938 struct intel_crtc *intel_crtc)
1939{
1940 u32 temp;
1941
1942 if (intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO)) {
1943 temp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
1944 if (temp & AUDIO_OUTPUT_ENABLE(intel_crtc->pipe))
1945 return true;
1946 }
1947 return false;
1948}
1949
6801c18c 1950void intel_ddi_get_config(struct intel_encoder *encoder,
5cec258b 1951 struct intel_crtc_state *pipe_config)
045ac3b5 1952{
fac5e23e 1953 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
045ac3b5 1954 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
0cb09a97 1955 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
bbd440fb 1956 struct intel_hdmi *intel_hdmi;
045ac3b5
JB
1957 u32 temp, flags = 0;
1958
4d1de975
JN
1959 /* XXX: DSI transcoder paranoia */
1960 if (WARN_ON(transcoder_is_dsi(cpu_transcoder)))
1961 return;
1962
045ac3b5
JB
1963 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1964 if (temp & TRANS_DDI_PHSYNC)
1965 flags |= DRM_MODE_FLAG_PHSYNC;
1966 else
1967 flags |= DRM_MODE_FLAG_NHSYNC;
1968 if (temp & TRANS_DDI_PVSYNC)
1969 flags |= DRM_MODE_FLAG_PVSYNC;
1970 else
1971 flags |= DRM_MODE_FLAG_NVSYNC;
1972
2d112de7 1973 pipe_config->base.adjusted_mode.flags |= flags;
42571aef
VS
1974
1975 switch (temp & TRANS_DDI_BPC_MASK) {
1976 case TRANS_DDI_BPC_6:
1977 pipe_config->pipe_bpp = 18;
1978 break;
1979 case TRANS_DDI_BPC_8:
1980 pipe_config->pipe_bpp = 24;
1981 break;
1982 case TRANS_DDI_BPC_10:
1983 pipe_config->pipe_bpp = 30;
1984 break;
1985 case TRANS_DDI_BPC_12:
1986 pipe_config->pipe_bpp = 36;
1987 break;
1988 default:
1989 break;
1990 }
eb14cb74
VS
1991
1992 switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
1993 case TRANS_DDI_MODE_SELECT_HDMI:
6897b4b5 1994 pipe_config->has_hdmi_sink = true;
bbd440fb
DV
1995 intel_hdmi = enc_to_intel_hdmi(&encoder->base);
1996
cda0aaaf 1997 if (intel_hdmi->infoframe_enabled(&encoder->base, pipe_config))
bbd440fb 1998 pipe_config->has_infoframe = true;
d4d6279a 1999 /* fall through */
eb14cb74 2000 case TRANS_DDI_MODE_SELECT_DVI:
d4d6279a
ACO
2001 pipe_config->lane_count = 4;
2002 break;
eb14cb74
VS
2003 case TRANS_DDI_MODE_SELECT_FDI:
2004 break;
2005 case TRANS_DDI_MODE_SELECT_DP_SST:
2006 case TRANS_DDI_MODE_SELECT_DP_MST:
90a6b7b0
VS
2007 pipe_config->lane_count =
2008 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
eb14cb74
VS
2009 intel_dp_get_m_n(intel_crtc, pipe_config);
2010 break;
2011 default:
2012 break;
2013 }
10214420 2014
9935f7fa
LY
2015 pipe_config->has_audio =
2016 intel_ddi_is_audio_enabled(dev_priv, intel_crtc);
9ed109a7 2017
6aa23e65
JN
2018 if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
2019 pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
10214420
DV
2020 /*
2021 * This is a big fat ugly hack.
2022 *
2023 * Some machines in UEFI boot mode provide us a VBT that has 18
2024 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
2025 * unknown we fail to light up. Yet the same BIOS boots up with
2026 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
2027 * max, not what it tells us to use.
2028 *
2029 * Note: This will still be broken if the eDP panel is not lit
2030 * up by the BIOS, and thus we can't get the mode at module
2031 * load.
2032 */
2033 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
6aa23e65
JN
2034 pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
2035 dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
10214420 2036 }
11578553 2037
22606a18 2038 intel_ddi_clock_get(encoder, pipe_config);
95a7a2ae 2039
cc3f90f0 2040 if (IS_GEN9_LP(dev_priv))
95a7a2ae
ID
2041 pipe_config->lane_lat_optim_mask =
2042 bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
045ac3b5
JB
2043}
2044
5bfe2ac0 2045static bool intel_ddi_compute_config(struct intel_encoder *encoder,
0a478c27
ML
2046 struct intel_crtc_state *pipe_config,
2047 struct drm_connector_state *conn_state)
00c09d70 2048{
fac5e23e 2049 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5bfe2ac0 2050 int type = encoder->type;
eccb140b 2051 int port = intel_ddi_get_encoder_port(encoder);
95a7a2ae 2052 int ret;
00c09d70 2053
5bfe2ac0 2054 WARN(type == INTEL_OUTPUT_UNKNOWN, "compute_config() on unknown output!\n");
00c09d70 2055
eccb140b
DV
2056 if (port == PORT_A)
2057 pipe_config->cpu_transcoder = TRANSCODER_EDP;
2058
00c09d70 2059 if (type == INTEL_OUTPUT_HDMI)
0a478c27 2060 ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
00c09d70 2061 else
0a478c27 2062 ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
95a7a2ae 2063
cc3f90f0 2064 if (IS_GEN9_LP(dev_priv) && ret)
95a7a2ae
ID
2065 pipe_config->lane_lat_optim_mask =
2066 bxt_ddi_phy_calc_lane_lat_optim_mask(encoder,
b284eeda 2067 pipe_config->lane_count);
95a7a2ae
ID
2068
2069 return ret;
2070
00c09d70
PZ
2071}
2072
2073static const struct drm_encoder_funcs intel_ddi_funcs = {
bf93ba67
ID
2074 .reset = intel_dp_encoder_reset,
2075 .destroy = intel_dp_encoder_destroy,
00c09d70
PZ
2076};
2077
4a28ae58
PZ
2078static struct intel_connector *
2079intel_ddi_init_dp_connector(struct intel_digital_port *intel_dig_port)
2080{
2081 struct intel_connector *connector;
2082 enum port port = intel_dig_port->port;
2083
9bdbd0b9 2084 connector = intel_connector_alloc();
4a28ae58
PZ
2085 if (!connector)
2086 return NULL;
2087
2088 intel_dig_port->dp.output_reg = DDI_BUF_CTL(port);
2089 if (!intel_dp_init_connector(intel_dig_port, connector)) {
2090 kfree(connector);
2091 return NULL;
2092 }
2093
2094 return connector;
2095}
2096
2097static struct intel_connector *
2098intel_ddi_init_hdmi_connector(struct intel_digital_port *intel_dig_port)
2099{
2100 struct intel_connector *connector;
2101 enum port port = intel_dig_port->port;
2102
9bdbd0b9 2103 connector = intel_connector_alloc();
4a28ae58
PZ
2104 if (!connector)
2105 return NULL;
2106
2107 intel_dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
2108 intel_hdmi_init_connector(intel_dig_port, connector);
2109
2110 return connector;
2111}
2112
c39055b0 2113void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
00c09d70
PZ
2114{
2115 struct intel_digital_port *intel_dig_port;
2116 struct intel_encoder *intel_encoder;
2117 struct drm_encoder *encoder;
ff662124 2118 bool init_hdmi, init_dp, init_lspcon = false;
10e7bec3
VS
2119 int max_lanes;
2120
2121 if (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES) {
2122 switch (port) {
2123 case PORT_A:
2124 max_lanes = 4;
2125 break;
2126 case PORT_E:
2127 max_lanes = 0;
2128 break;
2129 default:
2130 max_lanes = 4;
2131 break;
2132 }
2133 } else {
2134 switch (port) {
2135 case PORT_A:
2136 max_lanes = 2;
2137 break;
2138 case PORT_E:
2139 max_lanes = 2;
2140 break;
2141 default:
2142 max_lanes = 4;
2143 break;
2144 }
2145 }
311a2094
PZ
2146
2147 init_hdmi = (dev_priv->vbt.ddi_port_info[port].supports_dvi ||
2148 dev_priv->vbt.ddi_port_info[port].supports_hdmi);
2149 init_dp = dev_priv->vbt.ddi_port_info[port].supports_dp;
ff662124
SS
2150
2151 if (intel_bios_is_lspcon_present(dev_priv, port)) {
2152 /*
2153 * Lspcon device needs to be driven with DP connector
2154 * with special detection sequence. So make sure DP
2155 * is initialized before lspcon.
2156 */
2157 init_dp = true;
2158 init_lspcon = true;
2159 init_hdmi = false;
2160 DRM_DEBUG_KMS("VBT says port %c has lspcon\n", port_name(port));
2161 }
2162
311a2094 2163 if (!init_dp && !init_hdmi) {
500ea70d 2164 DRM_DEBUG_KMS("VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
311a2094 2165 port_name(port));
500ea70d 2166 return;
311a2094 2167 }
00c09d70 2168
b14c5679 2169 intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
00c09d70
PZ
2170 if (!intel_dig_port)
2171 return;
2172
00c09d70
PZ
2173 intel_encoder = &intel_dig_port->base;
2174 encoder = &intel_encoder->base;
2175
c39055b0 2176 drm_encoder_init(&dev_priv->drm, encoder, &intel_ddi_funcs,
580d8ed5 2177 DRM_MODE_ENCODER_TMDS, "DDI %c", port_name(port));
00c09d70 2178
5bfe2ac0 2179 intel_encoder->compute_config = intel_ddi_compute_config;
00c09d70 2180 intel_encoder->enable = intel_enable_ddi;
cc3f90f0 2181 if (IS_GEN9_LP(dev_priv))
95a7a2ae 2182 intel_encoder->pre_pll_enable = bxt_ddi_pre_pll_enable;
00c09d70
PZ
2183 intel_encoder->pre_enable = intel_ddi_pre_enable;
2184 intel_encoder->disable = intel_disable_ddi;
2185 intel_encoder->post_disable = intel_ddi_post_disable;
2186 intel_encoder->get_hw_state = intel_ddi_get_hw_state;
045ac3b5 2187 intel_encoder->get_config = intel_ddi_get_config;
bf93ba67 2188 intel_encoder->suspend = intel_dp_encoder_suspend;
62b69566 2189 intel_encoder->get_power_domains = intel_ddi_get_power_domains;
00c09d70
PZ
2190
2191 intel_dig_port->port = port;
bcf53de4
SM
2192 intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
2193 (DDI_BUF_PORT_REVERSAL |
2194 DDI_A_4_LANES);
00c09d70 2195
62b69566
ACO
2196 switch (port) {
2197 case PORT_A:
2198 intel_dig_port->ddi_io_power_domain =
2199 POWER_DOMAIN_PORT_DDI_A_IO;
2200 break;
2201 case PORT_B:
2202 intel_dig_port->ddi_io_power_domain =
2203 POWER_DOMAIN_PORT_DDI_B_IO;
2204 break;
2205 case PORT_C:
2206 intel_dig_port->ddi_io_power_domain =
2207 POWER_DOMAIN_PORT_DDI_C_IO;
2208 break;
2209 case PORT_D:
2210 intel_dig_port->ddi_io_power_domain =
2211 POWER_DOMAIN_PORT_DDI_D_IO;
2212 break;
2213 case PORT_E:
2214 intel_dig_port->ddi_io_power_domain =
2215 POWER_DOMAIN_PORT_DDI_E_IO;
2216 break;
2217 default:
2218 MISSING_CASE(port);
2219 }
2220
6c566dc9
MR
2221 /*
2222 * Bspec says that DDI_A_4_LANES is the only supported configuration
2223 * for Broxton. Yet some BIOS fail to set this bit on port A if eDP
2224 * wasn't lit up at boot. Force this bit on in our internal
2225 * configuration so that we use the proper lane count for our
2226 * calculations.
2227 */
cc3f90f0 2228 if (IS_GEN9_LP(dev_priv) && port == PORT_A) {
6c566dc9
MR
2229 if (!(intel_dig_port->saved_port_bits & DDI_A_4_LANES)) {
2230 DRM_DEBUG_KMS("BXT BIOS forgot to set DDI_A_4_LANES for port A; fixing\n");
2231 intel_dig_port->saved_port_bits |= DDI_A_4_LANES;
ed8d60f4 2232 max_lanes = 4;
6c566dc9
MR
2233 }
2234 }
2235
ed8d60f4
MR
2236 intel_dig_port->max_lanes = max_lanes;
2237
00c09d70 2238 intel_encoder->type = INTEL_OUTPUT_UNKNOWN;
79f255a0 2239 intel_encoder->power_domain = intel_port_to_power_domain(port);
03cdc1d4 2240 intel_encoder->port = port;
f68d697e 2241 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
bc079e8b 2242 intel_encoder->cloneable = 0;
00c09d70 2243
f68d697e
CW
2244 if (init_dp) {
2245 if (!intel_ddi_init_dp_connector(intel_dig_port))
2246 goto err;
13cf5504 2247
f68d697e 2248 intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
ca4c3890 2249 dev_priv->hotplug.irq_port[port] = intel_dig_port;
f68d697e 2250 }
21a8e6a4 2251
311a2094
PZ
2252 /* In theory we don't need the encoder->type check, but leave it just in
2253 * case we have some really bad VBTs... */
f68d697e
CW
2254 if (intel_encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
2255 if (!intel_ddi_init_hdmi_connector(intel_dig_port))
2256 goto err;
21a8e6a4 2257 }
f68d697e 2258
ff662124
SS
2259 if (init_lspcon) {
2260 if (lspcon_init(intel_dig_port))
2261 /* TODO: handle hdmi info frame part */
2262 DRM_DEBUG_KMS("LSPCON init success on port %c\n",
2263 port_name(port));
2264 else
2265 /*
2266 * LSPCON init faied, but DP init was success, so
2267 * lets try to drive as DP++ port.
2268 */
2269 DRM_ERROR("LSPCON init failed on port %c\n",
2270 port_name(port));
2271 }
2272
f68d697e
CW
2273 return;
2274
2275err:
2276 drm_encoder_cleanup(encoder);
2277 kfree(intel_dig_port);
00c09d70 2278}