Bug Summary

File:dev/pci/drm/drm_dp_mst_topology.c
Warning:line 2596, column 30
The left operand of '>>' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name drm_dp_mst_topology.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -D CONFIG_DRM_AMD_DC_DCN3_0 -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /usr/obj/sys/arch/amd64/compile/GENERIC.MP/scan-build/2022-01-12-131800-47421-1 -x c /usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c
1/*
2 * Copyright © 2014 Red Hat
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <linux/bitfield.h>
24#include <linux/delay.h>
25#include <linux/errno.h>
26#include <linux/i2c.h>
27#include <linux/init.h>
28#include <linux/kernel.h>
29#include <linux/random.h>
30#include <linux/sched.h>
31#include <linux/seq_file.h>
32#include <linux/iopoll.h>
33
34#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)0
35#include <linux/stacktrace.h>
36#include <linux/sort.h>
37#include <linux/timekeeping.h>
38#include <linux/math64.h>
39#endif
40
41#include <drm/drm_atomic.h>
42#include <drm/drm_atomic_helper.h>
43#include <drm/drm_dp_mst_helper.h>
44#include <drm/drm_drv.h>
45#include <drm/drm_print.h>
46#include <drm/drm_probe_helper.h>
47
48#include "drm_crtc_helper_internal.h"
49#include "drm_dp_mst_topology_internal.h"
50
51/**
52 * DOC: dp mst helper
53 *
54 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
55 * protocol. The helpers contain a topology manager and bandwidth manager.
56 * The helpers encapsulate the sending and received of sideband msgs.
57 */
58struct drm_dp_pending_up_req {
59 struct drm_dp_sideband_msg_hdr hdr;
60 struct drm_dp_sideband_msg_req_body msg;
61 struct list_head next;
62};
63
64static bool_Bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
65 char *buf);
66
67static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
68
69static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
70 int id,
71 struct drm_dp_payload *payload);
72
73static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
74 struct drm_dp_mst_port *port,
75 int offset, int size, u8 *bytes);
76static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
77 struct drm_dp_mst_port *port,
78 int offset, int size, u8 *bytes);
79
80static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
81 struct drm_dp_mst_branch *mstb);
82
83static void
84drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
85 struct drm_dp_mst_branch *mstb);
86
87static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
88 struct drm_dp_mst_branch *mstb,
89 struct drm_dp_mst_port *port);
90static bool_Bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
91 u8 *guid);
92
93static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port *port);
94static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port *port);
95static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
96
97static bool_Bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port,
98 struct drm_dp_mst_branch *branch);
99
100#define DBG_PREFIX"[dp_mst]" "[dp_mst]"
101
102#define DP_STR(x)[DRM_DP_SIDEBAND_TX_x] = "x" [DP_ ## x] = #x
103
104static const char *drm_dp_mst_req_type_str(u8 req_type)
105{
106 static const char * const req_type_str[] = {
107 DP_STR(GET_MSG_TRANSACTION_VERSION)[DRM_DP_SIDEBAND_TX_GET_MSG_TRANSACTION_VERSION] = "GET_MSG_TRANSACTION_VERSION",
108 DP_STR(LINK_ADDRESS)[DRM_DP_SIDEBAND_TX_LINK_ADDRESS] = "LINK_ADDRESS",
109 DP_STR(CONNECTION_STATUS_NOTIFY)[DRM_DP_SIDEBAND_TX_CONNECTION_STATUS_NOTIFY] = "CONNECTION_STATUS_NOTIFY",
110 DP_STR(ENUM_PATH_RESOURCES)[DRM_DP_SIDEBAND_TX_ENUM_PATH_RESOURCES] = "ENUM_PATH_RESOURCES",
111 DP_STR(ALLOCATE_PAYLOAD)[DRM_DP_SIDEBAND_TX_ALLOCATE_PAYLOAD] = "ALLOCATE_PAYLOAD",
112 DP_STR(QUERY_PAYLOAD)[DRM_DP_SIDEBAND_TX_QUERY_PAYLOAD] = "QUERY_PAYLOAD",
113 DP_STR(RESOURCE_STATUS_NOTIFY)[DRM_DP_SIDEBAND_TX_RESOURCE_STATUS_NOTIFY] = "RESOURCE_STATUS_NOTIFY",
114 DP_STR(CLEAR_PAYLOAD_ID_TABLE)[DRM_DP_SIDEBAND_TX_CLEAR_PAYLOAD_ID_TABLE] = "CLEAR_PAYLOAD_ID_TABLE",
115 DP_STR(REMOTE_DPCD_READ)[DRM_DP_SIDEBAND_TX_REMOTE_DPCD_READ] = "REMOTE_DPCD_READ",
116 DP_STR(REMOTE_DPCD_WRITE)[DRM_DP_SIDEBAND_TX_REMOTE_DPCD_WRITE] = "REMOTE_DPCD_WRITE",
117 DP_STR(REMOTE_I2C_READ)[DRM_DP_SIDEBAND_TX_REMOTE_I2C_READ] = "REMOTE_I2C_READ",
118 DP_STR(REMOTE_I2C_WRITE)[DRM_DP_SIDEBAND_TX_REMOTE_I2C_WRITE] = "REMOTE_I2C_WRITE",
119 DP_STR(POWER_UP_PHY)[DRM_DP_SIDEBAND_TX_POWER_UP_PHY] = "POWER_UP_PHY",
120 DP_STR(POWER_DOWN_PHY)[DRM_DP_SIDEBAND_TX_POWER_DOWN_PHY] = "POWER_DOWN_PHY",
121 DP_STR(SINK_EVENT_NOTIFY)[DRM_DP_SIDEBAND_TX_SINK_EVENT_NOTIFY] = "SINK_EVENT_NOTIFY",
122 DP_STR(QUERY_STREAM_ENC_STATUS)[DRM_DP_SIDEBAND_TX_QUERY_STREAM_ENC_STATUS] = "QUERY_STREAM_ENC_STATUS",
123 };
124
125 if (req_type >= ARRAY_SIZE(req_type_str)(sizeof((req_type_str)) / sizeof((req_type_str)[0])) ||
126 !req_type_str[req_type])
127 return "unknown";
128
129 return req_type_str[req_type];
130}
131
132#undef DP_STR
133#define DP_STR(x)[DRM_DP_SIDEBAND_TX_x] = "x" [DP_NAK_ ## x] = #x
134
135static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
136{
137 static const char * const nak_reason_str[] = {
138 DP_STR(WRITE_FAILURE)[DRM_DP_SIDEBAND_TX_WRITE_FAILURE] = "WRITE_FAILURE",
139 DP_STR(INVALID_READ)[DRM_DP_SIDEBAND_TX_INVALID_READ] = "INVALID_READ",
140 DP_STR(CRC_FAILURE)[DRM_DP_SIDEBAND_TX_CRC_FAILURE] = "CRC_FAILURE",
141 DP_STR(BAD_PARAM)[DRM_DP_SIDEBAND_TX_BAD_PARAM] = "BAD_PARAM",
142 DP_STR(DEFER)[DRM_DP_SIDEBAND_TX_DEFER] = "DEFER",
143 DP_STR(LINK_FAILURE)[DRM_DP_SIDEBAND_TX_LINK_FAILURE] = "LINK_FAILURE",
144 DP_STR(NO_RESOURCES)[DRM_DP_SIDEBAND_TX_NO_RESOURCES] = "NO_RESOURCES",
145 DP_STR(DPCD_FAIL)[DRM_DP_SIDEBAND_TX_DPCD_FAIL] = "DPCD_FAIL",
146 DP_STR(I2C_NAK)[DRM_DP_SIDEBAND_TX_I2C_NAK] = "I2C_NAK",
147 DP_STR(ALLOCATE_FAIL)[DRM_DP_SIDEBAND_TX_ALLOCATE_FAIL] = "ALLOCATE_FAIL",
148 };
149
150 if (nak_reason >= ARRAY_SIZE(nak_reason_str)(sizeof((nak_reason_str)) / sizeof((nak_reason_str)[0])) ||
151 !nak_reason_str[nak_reason])
152 return "unknown";
153
154 return nak_reason_str[nak_reason];
155}
156
157#undef DP_STR
158#define DP_STR(x)[DRM_DP_SIDEBAND_TX_x] = "x" [DRM_DP_SIDEBAND_TX_ ## x] = #x
159
160static const char *drm_dp_mst_sideband_tx_state_str(int state)
161{
162 static const char * const sideband_reason_str[] = {
163 DP_STR(QUEUED)[0] = "QUEUED",
164 DP_STR(START_SEND)[1] = "START_SEND",
165 DP_STR(SENT)[2] = "SENT",
166 DP_STR(RX)[3] = "RX",
167 DP_STR(TIMEOUT)[4] = "TIMEOUT",
168 };
169
170 if (state >= ARRAY_SIZE(sideband_reason_str)(sizeof((sideband_reason_str)) / sizeof((sideband_reason_str)
[0]))
||
171 !sideband_reason_str[state])
172 return "unknown";
173
174 return sideband_reason_str[state];
175}
176
177static int
178drm_dp_mst_rad_to_str(const u8 rad[8], u8 lct, char *out, size_t len)
179{
180 int i;
181 u8 unpacked_rad[16];
182
183 for (i = 0; i < lct; i++) {
184 if (i % 2)
185 unpacked_rad[i] = rad[i / 2] >> 4;
186 else
187 unpacked_rad[i] = rad[i / 2] & BIT_MASK(4)(1UL << ((4) % 64));
188 }
189
190 /* TODO: Eventually add something to printk so we can format the rad
191 * like this: 1.2.3
192 */
193 return snprintf(out, len, "%*phC", lct, unpacked_rad);
194}
195
196/* sideband msg handling */
197static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
198{
199 u8 bitmask = 0x80;
200 u8 bitshift = 7;
201 u8 array_index = 0;
202 int number_of_bits = num_nibbles * 4;
203 u8 remainder = 0;
204
205 while (number_of_bits != 0) {
206 number_of_bits--;
207 remainder <<= 1;
208 remainder |= (data[array_index] & bitmask) >> bitshift;
209 bitmask >>= 1;
210 bitshift--;
211 if (bitmask == 0) {
212 bitmask = 0x80;
213 bitshift = 7;
214 array_index++;
215 }
216 if ((remainder & 0x10) == 0x10)
217 remainder ^= 0x13;
218 }
219
220 number_of_bits = 4;
221 while (number_of_bits != 0) {
222 number_of_bits--;
223 remainder <<= 1;
224 if ((remainder & 0x10) != 0)
225 remainder ^= 0x13;
226 }
227
228 return remainder;
229}
230
231static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
232{
233 u8 bitmask = 0x80;
234 u8 bitshift = 7;
235 u8 array_index = 0;
236 int number_of_bits = number_of_bytes * 8;
237 u16 remainder = 0;
238
239 while (number_of_bits != 0) {
240 number_of_bits--;
241 remainder <<= 1;
242 remainder |= (data[array_index] & bitmask) >> bitshift;
243 bitmask >>= 1;
244 bitshift--;
245 if (bitmask == 0) {
246 bitmask = 0x80;
247 bitshift = 7;
248 array_index++;
249 }
250 if ((remainder & 0x100) == 0x100)
251 remainder ^= 0xd5;
252 }
253
254 number_of_bits = 8;
255 while (number_of_bits != 0) {
256 number_of_bits--;
257 remainder <<= 1;
258 if ((remainder & 0x100) != 0)
259 remainder ^= 0xd5;
260 }
261
262 return remainder & 0xff;
263}
264static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
265{
266 u8 size = 3;
267
268 size += (hdr->lct / 2);
269 return size;
270}
271
272static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
273 u8 *buf, int *len)
274{
275 int idx = 0;
276 int i;
277 u8 crc4;
278
279 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
280 for (i = 0; i < (hdr->lct / 2); i++)
281 buf[idx++] = hdr->rad[i];
282 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
283 (hdr->msg_len & 0x3f);
284 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
285
286 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
287 buf[idx - 1] |= (crc4 & 0xf);
288
289 *len = idx;
290}
291
292static bool_Bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
293 u8 *buf, int buflen, u8 *hdrlen)
294{
295 u8 crc4;
296 u8 len;
297 int i;
298 u8 idx;
299
300 if (buf[0] == 0)
15
Assuming the condition is false
16
Taking false branch
301 return false0;
302 len = 3;
303 len += ((buf[0] & 0xf0) >> 4) / 2;
304 if (len > buflen)
17
Assuming 'len' is <= 'buflen'
18
Taking false branch
305 return false0;
306 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
307
308 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
19
Assuming the condition is false
20
Taking false branch
309 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1])__drm_dbg(DRM_UT_KMS, "crc4 mismatch 0x%x 0x%x\n", crc4, buf[
len - 1])
;
310 return false0;
311 }
312
313 hdr->lct = (buf[0] & 0xf0) >> 4;
314 hdr->lcr = (buf[0] & 0xf);
315 idx = 1;
316 for (i = 0; i < (hdr->lct / 2); i++)
21
Assuming the condition is false
22
Loop condition is false. Execution continues on line 318
317 hdr->rad[i] = buf[idx++];
318 hdr->broadcast = (buf[idx] >> 7) & 0x1;
319 hdr->path_msg = (buf[idx] >> 6) & 0x1;
320 hdr->msg_len = buf[idx] & 0x3f;
321 idx++;
322 hdr->somt = (buf[idx] >> 7) & 0x1;
323 hdr->eomt = (buf[idx] >> 6) & 0x1;
324 hdr->seqno = (buf[idx] >> 4) & 0x1;
325 idx++;
326 *hdrlen = idx;
327 return true1;
328}
329
330void
331drm_dp_encode_sideband_req(const struct drm_dp_sideband_msg_req_body *req,
332 struct drm_dp_sideband_msg_tx *raw)
333{
334 int idx = 0;
335 int i;
336 u8 *buf = raw->msg;
337
338 buf[idx++] = req->req_type & 0x7f;
339
340 switch (req->req_type) {
341 case DP_ENUM_PATH_RESOURCES0x10:
342 case DP_POWER_DOWN_PHY0x25:
343 case DP_POWER_UP_PHY0x24:
344 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
345 idx++;
346 break;
347 case DP_ALLOCATE_PAYLOAD0x11:
348 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
349 (req->u.allocate_payload.number_sdp_streams & 0xf);
350 idx++;
351 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
352 idx++;
353 buf[idx] = (req->u.allocate_payload.pbn >> 8);
354 idx++;
355 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
356 idx++;
357 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
358 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
359 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
360 idx++;
361 }
362 if (req->u.allocate_payload.number_sdp_streams & 1) {
363 i = req->u.allocate_payload.number_sdp_streams - 1;
364 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
365 idx++;
366 }
367 break;
368 case DP_QUERY_PAYLOAD0x12:
369 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
370 idx++;
371 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
372 idx++;
373 break;
374 case DP_REMOTE_DPCD_READ0x20:
375 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
376 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
377 idx++;
378 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
379 idx++;
380 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
381 idx++;
382 buf[idx] = (req->u.dpcd_read.num_bytes);
383 idx++;
384 break;
385
386 case DP_REMOTE_DPCD_WRITE0x21:
387 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
388 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
389 idx++;
390 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
391 idx++;
392 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
393 idx++;
394 buf[idx] = (req->u.dpcd_write.num_bytes);
395 idx++;
396 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes)__builtin_memcpy((&buf[idx]), (req->u.dpcd_write.bytes
), (req->u.dpcd_write.num_bytes))
;
397 idx += req->u.dpcd_write.num_bytes;
398 break;
399 case DP_REMOTE_I2C_READ0x22:
400 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
401 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
402 idx++;
403 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
404 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
405 idx++;
406 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
407 idx++;
408 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes)__builtin_memcpy((&buf[idx]), (req->u.i2c_read.transactions
[i].bytes), (req->u.i2c_read.transactions[i].num_bytes))
;
409 idx += req->u.i2c_read.transactions[i].num_bytes;
410
411 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 4;
412 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
413 idx++;
414 }
415 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
416 idx++;
417 buf[idx] = (req->u.i2c_read.num_bytes_read);
418 idx++;
419 break;
420
421 case DP_REMOTE_I2C_WRITE0x23:
422 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
423 idx++;
424 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
425 idx++;
426 buf[idx] = (req->u.i2c_write.num_bytes);
427 idx++;
428 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes)__builtin_memcpy((&buf[idx]), (req->u.i2c_write.bytes)
, (req->u.i2c_write.num_bytes))
;
429 idx += req->u.i2c_write.num_bytes;
430 break;
431 case DP_QUERY_STREAM_ENC_STATUS0x38: {
432 const struct drm_dp_query_stream_enc_status *msg;
433
434 msg = &req->u.enc_status;
435 buf[idx] = msg->stream_id;
436 idx++;
437 memcpy(&buf[idx], msg->client_id, sizeof(msg->client_id))__builtin_memcpy((&buf[idx]), (msg->client_id), (sizeof
(msg->client_id)))
;
438 idx += sizeof(msg->client_id);
439 buf[idx] = 0;
440 buf[idx] |= FIELD_PREP(GENMASK(1, 0), msg->stream_event)(((typeof((((~0UL) >> (64 - (1) - 1)) & ((~0UL) <<
(0)))))(msg->stream_event) << (__builtin_ffsll((((~
0UL) >> (64 - (1) - 1)) & ((~0UL) << (0)))) -
1)) & ((((~0UL) >> (64 - (1) - 1)) & ((~0UL) <<
(0)))))
;
441 buf[idx] |= msg->valid_stream_event ? BIT(2)(1UL << (2)) : 0;
442 buf[idx] |= FIELD_PREP(GENMASK(4, 3), msg->stream_behavior)(((typeof((((~0UL) >> (64 - (4) - 1)) & ((~0UL) <<
(3)))))(msg->stream_behavior) << (__builtin_ffsll((
((~0UL) >> (64 - (4) - 1)) & ((~0UL) << (3)))
) - 1)) & ((((~0UL) >> (64 - (4) - 1)) & ((~0UL
) << (3)))))
;
443 buf[idx] |= msg->valid_stream_behavior ? BIT(5)(1UL << (5)) : 0;
444 idx++;
445 }
446 break;
447 }
448 raw->cur_len = idx;
449}
450EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_encode_sideband_req);
451
452/* Decode a sideband request we've encoded, mainly used for debugging */
453int
454drm_dp_decode_sideband_req(const struct drm_dp_sideband_msg_tx *raw,
455 struct drm_dp_sideband_msg_req_body *req)
456{
457 const u8 *buf = raw->msg;
458 int i, idx = 0;
459
460 req->req_type = buf[idx++] & 0x7f;
461 switch (req->req_type) {
462 case DP_ENUM_PATH_RESOURCES0x10:
463 case DP_POWER_DOWN_PHY0x25:
464 case DP_POWER_UP_PHY0x24:
465 req->u.port_num.port_number = (buf[idx] >> 4) & 0xf;
466 break;
467 case DP_ALLOCATE_PAYLOAD0x11:
468 {
469 struct drm_dp_allocate_payload *a =
470 &req->u.allocate_payload;
471
472 a->number_sdp_streams = buf[idx] & 0xf;
473 a->port_number = (buf[idx] >> 4) & 0xf;
474
475 WARN_ON(buf[++idx] & 0x80)({ int __ret = !!(buf[++idx] & 0x80); if (__ret) printf("WARNING %s failed at %s:%d\n"
, "buf[++idx] & 0x80", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 475); __builtin_expect(!!(__ret), 0); })
;
476 a->vcpi = buf[idx] & 0x7f;
477
478 a->pbn = buf[++idx] << 8;
479 a->pbn |= buf[++idx];
480
481 idx++;
482 for (i = 0; i < a->number_sdp_streams; i++) {
483 a->sdp_stream_sink[i] =
484 (buf[idx + (i / 2)] >> ((i % 2) ? 0 : 4)) & 0xf;
485 }
486 }
487 break;
488 case DP_QUERY_PAYLOAD0x12:
489 req->u.query_payload.port_number = (buf[idx] >> 4) & 0xf;
490 WARN_ON(buf[++idx] & 0x80)({ int __ret = !!(buf[++idx] & 0x80); if (__ret) printf("WARNING %s failed at %s:%d\n"
, "buf[++idx] & 0x80", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 490); __builtin_expect(!!(__ret), 0); })
;
491 req->u.query_payload.vcpi = buf[idx] & 0x7f;
492 break;
493 case DP_REMOTE_DPCD_READ0x20:
494 {
495 struct drm_dp_remote_dpcd_read *r = &req->u.dpcd_read;
496
497 r->port_number = (buf[idx] >> 4) & 0xf;
498
499 r->dpcd_address = (buf[idx] << 16) & 0xf0000;
500 r->dpcd_address |= (buf[++idx] << 8) & 0xff00;
501 r->dpcd_address |= buf[++idx] & 0xff;
502
503 r->num_bytes = buf[++idx];
504 }
505 break;
506 case DP_REMOTE_DPCD_WRITE0x21:
507 {
508 struct drm_dp_remote_dpcd_write *w =
509 &req->u.dpcd_write;
510
511 w->port_number = (buf[idx] >> 4) & 0xf;
512
513 w->dpcd_address = (buf[idx] << 16) & 0xf0000;
514 w->dpcd_address |= (buf[++idx] << 8) & 0xff00;
515 w->dpcd_address |= buf[++idx] & 0xff;
516
517 w->num_bytes = buf[++idx];
518
519 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
520 GFP_KERNEL(0x0001 | 0x0004));
521 if (!w->bytes)
522 return -ENOMEM12;
523 }
524 break;
525 case DP_REMOTE_I2C_READ0x22:
526 {
527 struct drm_dp_remote_i2c_read *r = &req->u.i2c_read;
528 struct drm_dp_remote_i2c_read_tx *tx;
529 bool_Bool failed = false0;
530
531 r->num_transactions = buf[idx] & 0x3;
532 r->port_number = (buf[idx] >> 4) & 0xf;
533 for (i = 0; i < r->num_transactions; i++) {
534 tx = &r->transactions[i];
535
536 tx->i2c_dev_id = buf[++idx] & 0x7f;
537 tx->num_bytes = buf[++idx];
538 tx->bytes = kmemdup(&buf[++idx],
539 tx->num_bytes,
540 GFP_KERNEL(0x0001 | 0x0004));
541 if (!tx->bytes) {
542 failed = true1;
543 break;
544 }
545 idx += tx->num_bytes;
546 tx->no_stop_bit = (buf[idx] >> 5) & 0x1;
547 tx->i2c_transaction_delay = buf[idx] & 0xf;
548 }
549
550 if (failed) {
551 for (i = 0; i < r->num_transactions; i++) {
552 tx = &r->transactions[i];
553 kfree(tx->bytes);
554 }
555 return -ENOMEM12;
556 }
557
558 r->read_i2c_device_id = buf[++idx] & 0x7f;
559 r->num_bytes_read = buf[++idx];
560 }
561 break;
562 case DP_REMOTE_I2C_WRITE0x23:
563 {
564 struct drm_dp_remote_i2c_write *w = &req->u.i2c_write;
565
566 w->port_number = (buf[idx] >> 4) & 0xf;
567 w->write_i2c_device_id = buf[++idx] & 0x7f;
568 w->num_bytes = buf[++idx];
569 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
570 GFP_KERNEL(0x0001 | 0x0004));
571 if (!w->bytes)
572 return -ENOMEM12;
573 }
574 break;
575 case DP_QUERY_STREAM_ENC_STATUS0x38:
576 req->u.enc_status.stream_id = buf[idx++];
577 for (i = 0; i < sizeof(req->u.enc_status.client_id); i++)
578 req->u.enc_status.client_id[i] = buf[idx++];
579
580 req->u.enc_status.stream_event = FIELD_GET(GENMASK(1, 0),((typeof((((~0UL) >> (64 - (1) - 1)) & ((~0UL) <<
(0)))))(((buf[idx]) & ((((~0UL) >> (64 - (1) - 1))
& ((~0UL) << (0))))) >> (__builtin_ffsll((((
~0UL) >> (64 - (1) - 1)) & ((~0UL) << (0)))) -
1)))
581 buf[idx])((typeof((((~0UL) >> (64 - (1) - 1)) & ((~0UL) <<
(0)))))(((buf[idx]) & ((((~0UL) >> (64 - (1) - 1))
& ((~0UL) << (0))))) >> (__builtin_ffsll((((
~0UL) >> (64 - (1) - 1)) & ((~0UL) << (0)))) -
1)))
;
582 req->u.enc_status.valid_stream_event = FIELD_GET(BIT(2),((typeof((1UL << (2))))(((buf[idx]) & ((1UL <<
(2)))) >> (__builtin_ffsll((1UL << (2))) - 1)))
583 buf[idx])((typeof((1UL << (2))))(((buf[idx]) & ((1UL <<
(2)))) >> (__builtin_ffsll((1UL << (2))) - 1)))
;
584 req->u.enc_status.stream_behavior = FIELD_GET(GENMASK(4, 3),((typeof((((~0UL) >> (64 - (4) - 1)) & ((~0UL) <<
(3)))))(((buf[idx]) & ((((~0UL) >> (64 - (4) - 1))
& ((~0UL) << (3))))) >> (__builtin_ffsll((((
~0UL) >> (64 - (4) - 1)) & ((~0UL) << (3)))) -
1)))
585 buf[idx])((typeof((((~0UL) >> (64 - (4) - 1)) & ((~0UL) <<
(3)))))(((buf[idx]) & ((((~0UL) >> (64 - (4) - 1))
& ((~0UL) << (3))))) >> (__builtin_ffsll((((
~0UL) >> (64 - (4) - 1)) & ((~0UL) << (3)))) -
1)))
;
586 req->u.enc_status.valid_stream_behavior = FIELD_GET(BIT(5),((typeof((1UL << (5))))(((buf[idx]) & ((1UL <<
(5)))) >> (__builtin_ffsll((1UL << (5))) - 1)))
587 buf[idx])((typeof((1UL << (5))))(((buf[idx]) & ((1UL <<
(5)))) >> (__builtin_ffsll((1UL << (5))) - 1)))
;
588 break;
589 }
590
591 return 0;
592}
593EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_decode_sideband_req);
594
595void
596drm_dp_dump_sideband_msg_req_body(const struct drm_dp_sideband_msg_req_body *req,
597 int indent, struct drm_printer *printer)
598{
599 int i;
600
601#define P(f, ...) drm_printf_indent(printer, indent, f, ##__VA_ARGS__)drm_printf((printer), "%.*s" f, (indent), "\t\t\t\t\tX", ##__VA_ARGS__
)
602 if (req->req_type == DP_LINK_ADDRESS0x01) {
603 /* No contents to print */
604 P("type=%s\n", drm_dp_mst_req_type_str(req->req_type));
605 return;
606 }
607
608 P("type=%s contents:\n", drm_dp_mst_req_type_str(req->req_type));
609 indent++;
610
611 switch (req->req_type) {
612 case DP_ENUM_PATH_RESOURCES0x10:
613 case DP_POWER_DOWN_PHY0x25:
614 case DP_POWER_UP_PHY0x24:
615 P("port=%d\n", req->u.port_num.port_number);
616 break;
617 case DP_ALLOCATE_PAYLOAD0x11:
618 P("port=%d vcpi=%d pbn=%d sdp_streams=%d %*ph\n",
619 req->u.allocate_payload.port_number,
620 req->u.allocate_payload.vcpi, req->u.allocate_payload.pbn,
621 req->u.allocate_payload.number_sdp_streams,
622 req->u.allocate_payload.number_sdp_streams,
623 req->u.allocate_payload.sdp_stream_sink);
624 break;
625 case DP_QUERY_PAYLOAD0x12:
626 P("port=%d vcpi=%d\n",
627 req->u.query_payload.port_number,
628 req->u.query_payload.vcpi);
629 break;
630 case DP_REMOTE_DPCD_READ0x20:
631 P("port=%d dpcd_addr=%05x len=%d\n",
632 req->u.dpcd_read.port_number, req->u.dpcd_read.dpcd_address,
633 req->u.dpcd_read.num_bytes);
634 break;
635 case DP_REMOTE_DPCD_WRITE0x21:
636 P("port=%d addr=%05x len=%d: %*ph\n",
637 req->u.dpcd_write.port_number,
638 req->u.dpcd_write.dpcd_address,
639 req->u.dpcd_write.num_bytes, req->u.dpcd_write.num_bytes,
640 req->u.dpcd_write.bytes);
641 break;
642 case DP_REMOTE_I2C_READ0x22:
643 P("port=%d num_tx=%d id=%d size=%d:\n",
644 req->u.i2c_read.port_number,
645 req->u.i2c_read.num_transactions,
646 req->u.i2c_read.read_i2c_device_id,
647 req->u.i2c_read.num_bytes_read);
648
649 indent++;
650 for (i = 0; i < req->u.i2c_read.num_transactions; i++) {
651 const struct drm_dp_remote_i2c_read_tx *rtx =
652 &req->u.i2c_read.transactions[i];
653
654 P("%d: id=%03d size=%03d no_stop_bit=%d tx_delay=%03d: %*ph\n",
655 i, rtx->i2c_dev_id, rtx->num_bytes,
656 rtx->no_stop_bit, rtx->i2c_transaction_delay,
657 rtx->num_bytes, rtx->bytes);
658 }
659 break;
660 case DP_REMOTE_I2C_WRITE0x23:
661 P("port=%d id=%d size=%d: %*ph\n",
662 req->u.i2c_write.port_number,
663 req->u.i2c_write.write_i2c_device_id,
664 req->u.i2c_write.num_bytes, req->u.i2c_write.num_bytes,
665 req->u.i2c_write.bytes);
666 break;
667 case DP_QUERY_STREAM_ENC_STATUS0x38:
668 P("stream_id=%u client_id=%*ph stream_event=%x "
669 "valid_event=%d stream_behavior=%x valid_behavior=%d",
670 req->u.enc_status.stream_id,
671 (int)ARRAY_SIZE(req->u.enc_status.client_id)(sizeof((req->u.enc_status.client_id)) / sizeof((req->u
.enc_status.client_id)[0]))
,
672 req->u.enc_status.client_id, req->u.enc_status.stream_event,
673 req->u.enc_status.valid_stream_event,
674 req->u.enc_status.stream_behavior,
675 req->u.enc_status.valid_stream_behavior);
676 break;
677 default:
678 P("???\n");
679 break;
680 }
681#undef P
682}
683EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_dump_sideband_msg_req_body);
684
685static inline void
686drm_dp_mst_dump_sideband_msg_tx(struct drm_printer *p,
687 const struct drm_dp_sideband_msg_tx *txmsg)
688{
689 struct drm_dp_sideband_msg_req_body req;
690 char buf[64];
691 int ret;
692 int i;
693
694 drm_dp_mst_rad_to_str(txmsg->dst->rad, txmsg->dst->lct, buf,
695 sizeof(buf));
696 drm_printf(p, "txmsg cur_offset=%x cur_len=%x seqno=%x state=%s path_msg=%d dst=%s\n",
697 txmsg->cur_offset, txmsg->cur_len, txmsg->seqno,
698 drm_dp_mst_sideband_tx_state_str(txmsg->state),
699 txmsg->path_msg, buf);
700
701 ret = drm_dp_decode_sideband_req(txmsg, &req);
702 if (ret) {
703 drm_printf(p, "<failed to decode sideband req: %d>\n", ret);
704 return;
705 }
706 drm_dp_dump_sideband_msg_req_body(&req, 1, p);
707
708 switch (req.req_type) {
709 case DP_REMOTE_DPCD_WRITE0x21:
710 kfree(req.u.dpcd_write.bytes);
711 break;
712 case DP_REMOTE_I2C_READ0x22:
713 for (i = 0; i < req.u.i2c_read.num_transactions; i++)
714 kfree(req.u.i2c_read.transactions[i].bytes);
715 break;
716 case DP_REMOTE_I2C_WRITE0x23:
717 kfree(req.u.i2c_write.bytes);
718 break;
719 }
720}
721
722static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
723{
724 u8 crc4;
725
726 crc4 = drm_dp_msg_data_crc4(msg, len);
727 msg[len] = crc4;
728}
729
730static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
731 struct drm_dp_sideband_msg_tx *raw)
732{
733 int idx = 0;
734 u8 *buf = raw->msg;
735
736 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
737
738 raw->cur_len = idx;
739}
740
741static int drm_dp_sideband_msg_set_header(struct drm_dp_sideband_msg_rx *msg,
742 struct drm_dp_sideband_msg_hdr *hdr,
743 u8 hdrlen)
744{
745 /*
746 * ignore out-of-order messages or messages that are part of a
747 * failed transaction
748 */
749 if (!hdr->somt && !msg->have_somt)
750 return false0;
751
752 /* get length contained in this portion */
753 msg->curchunk_idx = 0;
754 msg->curchunk_len = hdr->msg_len;
755 msg->curchunk_hdrlen = hdrlen;
756
757 /* we have already gotten an somt - don't bother parsing */
758 if (hdr->somt && msg->have_somt)
759 return false0;
760
761 if (hdr->somt) {
762 memcpy(&msg->initial_hdr, hdr,__builtin_memcpy((&msg->initial_hdr), (hdr), (sizeof(struct
drm_dp_sideband_msg_hdr)))
763 sizeof(struct drm_dp_sideband_msg_hdr))__builtin_memcpy((&msg->initial_hdr), (hdr), (sizeof(struct
drm_dp_sideband_msg_hdr)))
;
764 msg->have_somt = true1;
765 }
766 if (hdr->eomt)
767 msg->have_eomt = true1;
768
769 return true1;
770}
771
772/* this adds a chunk of msg to the builder to get the final msg */
773static bool_Bool drm_dp_sideband_append_payload(struct drm_dp_sideband_msg_rx *msg,
774 u8 *replybuf, u8 replybuflen)
775{
776 u8 crc4;
777
778 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen)__builtin_memcpy((&msg->chunk[msg->curchunk_idx]), (
replybuf), (replybuflen))
;
779 msg->curchunk_idx += replybuflen;
780
781 if (msg->curchunk_idx >= msg->curchunk_len) {
782 /* do CRC */
783 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
784 if (crc4 != msg->chunk[msg->curchunk_len - 1])
785 print_hex_dump(KERN_DEBUG"\0017", "wrong crc",
786 DUMP_PREFIX_NONE, 16, 1,
787 msg->chunk, msg->curchunk_len, false0);
788 /* copy chunk into bigger msg */
789 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1)__builtin_memcpy((&msg->msg[msg->curlen]), (msg->
chunk), (msg->curchunk_len - 1))
;
790 msg->curlen += msg->curchunk_len - 1;
791 }
792 return true1;
793}
794
795static bool_Bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
796 struct drm_dp_sideband_msg_reply_body *repmsg)
797{
798 int idx = 1;
799 int i;
800
801 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16)__builtin_memcpy((repmsg->u.link_addr.guid), (&raw->
msg[idx]), (16))
;
802 idx += 16;
803 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
804 idx++;
805 if (idx > raw->curlen)
806 goto fail_len;
807 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
808 if (raw->msg[idx] & 0x80)
809 repmsg->u.link_addr.ports[i].input_port = 1;
810
811 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
812 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
813
814 idx++;
815 if (idx > raw->curlen)
816 goto fail_len;
817 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
818 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
819 if (repmsg->u.link_addr.ports[i].input_port == 0)
820 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
821 idx++;
822 if (idx > raw->curlen)
823 goto fail_len;
824 if (repmsg->u.link_addr.ports[i].input_port == 0) {
825 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
826 idx++;
827 if (idx > raw->curlen)
828 goto fail_len;
829 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16)__builtin_memcpy((repmsg->u.link_addr.ports[i].peer_guid),
(&raw->msg[idx]), (16))
;
830 idx += 16;
831 if (idx > raw->curlen)
832 goto fail_len;
833 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
834 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
835 idx++;
836
837 }
838 if (idx > raw->curlen)
839 goto fail_len;
840 }
841
842 return true1;
843fail_len:
844 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "link address reply parse length fail %d %d\n"
, idx, raw->curlen)
;
845 return false0;
846}
847
848static bool_Bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
849 struct drm_dp_sideband_msg_reply_body *repmsg)
850{
851 int idx = 1;
852
853 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
854 idx++;
855 if (idx > raw->curlen)
856 goto fail_len;
857 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
858 idx++;
859 if (idx > raw->curlen)
860 goto fail_len;
861
862 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes)__builtin_memcpy((repmsg->u.remote_dpcd_read_ack.bytes), (
&raw->msg[idx]), (repmsg->u.remote_dpcd_read_ack.num_bytes
))
;
863 return true1;
864fail_len:
865 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "link address reply parse length fail %d %d\n"
, idx, raw->curlen)
;
866 return false0;
867}
868
869static bool_Bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
870 struct drm_dp_sideband_msg_reply_body *repmsg)
871{
872 int idx = 1;
873
874 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
875 idx++;
876 if (idx > raw->curlen)
877 goto fail_len;
878 return true1;
879fail_len:
880 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "parse length fail %d %d\n", idx, raw->
curlen)
;
881 return false0;
882}
883
884static bool_Bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
885 struct drm_dp_sideband_msg_reply_body *repmsg)
886{
887 int idx = 1;
888
889 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
890 idx++;
891 if (idx > raw->curlen)
892 goto fail_len;
893 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
894 idx++;
895 /* TODO check */
896 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes)__builtin_memcpy((repmsg->u.remote_i2c_read_ack.bytes), (&
raw->msg[idx]), (repmsg->u.remote_i2c_read_ack.num_bytes
))
;
897 return true1;
898fail_len:
899 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "remote i2c reply parse length fail %d %d\n"
, idx, raw->curlen)
;
900 return false0;
901}
902
903static bool_Bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
904 struct drm_dp_sideband_msg_reply_body *repmsg)
905{
906 int idx = 1;
907
908 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
909 repmsg->u.path_resources.fec_capable = raw->msg[idx] & 0x1;
910 idx++;
911 if (idx > raw->curlen)
912 goto fail_len;
913 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
914 idx += 2;
915 if (idx > raw->curlen)
916 goto fail_len;
917 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
918 idx += 2;
919 if (idx > raw->curlen)
920 goto fail_len;
921 return true1;
922fail_len:
923 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "enum resource parse length fail %d %d\n"
, idx, raw->curlen)
;
924 return false0;
925}
926
927static bool_Bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
928 struct drm_dp_sideband_msg_reply_body *repmsg)
929{
930 int idx = 1;
931
932 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
933 idx++;
934 if (idx > raw->curlen)
935 goto fail_len;
936 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
937 idx++;
938 if (idx > raw->curlen)
939 goto fail_len;
940 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
941 idx += 2;
942 if (idx > raw->curlen)
943 goto fail_len;
944 return true1;
945fail_len:
946 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "allocate payload parse length fail %d %d\n"
, idx, raw->curlen)
;
947 return false0;
948}
949
950static bool_Bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
951 struct drm_dp_sideband_msg_reply_body *repmsg)
952{
953 int idx = 1;
954
955 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
956 idx++;
957 if (idx > raw->curlen)
958 goto fail_len;
959 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
960 idx += 2;
961 if (idx > raw->curlen)
962 goto fail_len;
963 return true1;
964fail_len:
965 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "query payload parse length fail %d %d\n"
, idx, raw->curlen)
;
966 return false0;
967}
968
969static bool_Bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
970 struct drm_dp_sideband_msg_reply_body *repmsg)
971{
972 int idx = 1;
973
974 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
975 idx++;
976 if (idx > raw->curlen) {
977 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",__drm_dbg(DRM_UT_KMS, "power up/down phy parse length fail %d %d\n"
, idx, raw->curlen)
978 idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "power up/down phy parse length fail %d %d\n"
, idx, raw->curlen)
;
979 return false0;
980 }
981 return true1;
982}
983
984static bool_Bool
985drm_dp_sideband_parse_query_stream_enc_status(
986 struct drm_dp_sideband_msg_rx *raw,
987 struct drm_dp_sideband_msg_reply_body *repmsg)
988{
989 struct drm_dp_query_stream_enc_status_ack_reply *reply;
990
991 reply = &repmsg->u.enc_status;
992
993 reply->stream_id = raw->msg[3];
994
995 reply->reply_signed = raw->msg[2] & BIT(0)(1UL << (0));
996
997 /*
998 * NOTE: It's my impression from reading the spec that the below parsing
999 * is correct. However I noticed while testing with an HDCP 1.4 display
1000 * through an HDCP 2.2 hub that only bit 3 was set. In that case, I
1001 * would expect both bits to be set. So keep the parsing following the
1002 * spec, but beware reality might not match the spec (at least for some
1003 * configurations).
1004 */
1005 reply->hdcp_1x_device_present = raw->msg[2] & BIT(4)(1UL << (4));
1006 reply->hdcp_2x_device_present = raw->msg[2] & BIT(3)(1UL << (3));
1007
1008 reply->query_capable_device_present = raw->msg[2] & BIT(5)(1UL << (5));
1009 reply->legacy_device_present = raw->msg[2] & BIT(6)(1UL << (6));
1010 reply->unauthorizable_device_present = raw->msg[2] & BIT(7)(1UL << (7));
1011
1012 reply->auth_completed = !!(raw->msg[1] & BIT(3)(1UL << (3)));
1013 reply->encryption_enabled = !!(raw->msg[1] & BIT(4)(1UL << (4)));
1014 reply->repeater_present = !!(raw->msg[1] & BIT(5)(1UL << (5)));
1015 reply->state = (raw->msg[1] & GENMASK(7, 6)(((~0UL) >> (64 - (7) - 1)) & ((~0UL) << (6))
)
) >> 6;
1016
1017 return true1;
1018}
1019
1020static bool_Bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
1021 struct drm_dp_sideband_msg_reply_body *msg)
1022{
1023 memset(msg, 0, sizeof(*msg))__builtin_memset((msg), (0), (sizeof(*msg)));
1024 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
1025 msg->req_type = (raw->msg[0] & 0x7f);
1026
1027 if (msg->reply_type == DP_SIDEBAND_REPLY_NAK0x01) {
1028 memcpy(msg->u.nak.guid, &raw->msg[1], 16)__builtin_memcpy((msg->u.nak.guid), (&raw->msg[1]),
(16))
;
1029 msg->u.nak.reason = raw->msg[17];
1030 msg->u.nak.nak_data = raw->msg[18];
1031 return false0;
1032 }
1033
1034 switch (msg->req_type) {
1035 case DP_LINK_ADDRESS0x01:
1036 return drm_dp_sideband_parse_link_address(raw, msg);
1037 case DP_QUERY_PAYLOAD0x12:
1038 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
1039 case DP_REMOTE_DPCD_READ0x20:
1040 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
1041 case DP_REMOTE_DPCD_WRITE0x21:
1042 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
1043 case DP_REMOTE_I2C_READ0x22:
1044 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
1045 case DP_REMOTE_I2C_WRITE0x23:
1046 return true1; /* since there's nothing to parse */
1047 case DP_ENUM_PATH_RESOURCES0x10:
1048 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
1049 case DP_ALLOCATE_PAYLOAD0x11:
1050 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
1051 case DP_POWER_DOWN_PHY0x25:
1052 case DP_POWER_UP_PHY0x24:
1053 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
1054 case DP_CLEAR_PAYLOAD_ID_TABLE0x14:
1055 return true1; /* since there's nothing to parse */
1056 case DP_QUERY_STREAM_ENC_STATUS0x38:
1057 return drm_dp_sideband_parse_query_stream_enc_status(raw, msg);
1058 default:
1059 DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg->req_type,__drm_err("Got unknown reply 0x%02x (%s)\n", msg->req_type
, drm_dp_mst_req_type_str(msg->req_type))
1060 drm_dp_mst_req_type_str(msg->req_type))__drm_err("Got unknown reply 0x%02x (%s)\n", msg->req_type
, drm_dp_mst_req_type_str(msg->req_type))
;
1061 return false0;
1062 }
1063}
1064
1065static bool_Bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
1066 struct drm_dp_sideband_msg_req_body *msg)
1067{
1068 int idx = 1;
1069
1070 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
1071 idx++;
1072 if (idx > raw->curlen)
1073 goto fail_len;
1074
1075 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16)__builtin_memcpy((msg->u.conn_stat.guid), (&raw->msg
[idx]), (16))
;
1076 idx += 16;
1077 if (idx > raw->curlen)
1078 goto fail_len;
1079
1080 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
1081 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
1082 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
1083 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
1084 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
1085 idx++;
1086 return true1;
1087fail_len:
1088 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "connection status reply parse length fail %d %d\n"
, idx, raw->curlen)
;
1089 return false0;
1090}
1091
1092static bool_Bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
1093 struct drm_dp_sideband_msg_req_body *msg)
1094{
1095 int idx = 1;
1096
1097 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
1098 idx++;
1099 if (idx > raw->curlen)
1100 goto fail_len;
1101
1102 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16)__builtin_memcpy((msg->u.resource_stat.guid), (&raw->
msg[idx]), (16))
;
1103 idx += 16;
1104 if (idx > raw->curlen)
1105 goto fail_len;
1106
1107 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
1108 idx++;
1109 return true1;
1110fail_len:
1111 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen)__drm_dbg(DRM_UT_KMS, "resource status reply parse length fail %d %d\n"
, idx, raw->curlen)
;
1112 return false0;
1113}
1114
1115static bool_Bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
1116 struct drm_dp_sideband_msg_req_body *msg)
1117{
1118 memset(msg, 0, sizeof(*msg))__builtin_memset((msg), (0), (sizeof(*msg)));
1119 msg->req_type = (raw->msg[0] & 0x7f);
1120
1121 switch (msg->req_type) {
1122 case DP_CONNECTION_STATUS_NOTIFY0x02:
1123 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
1124 case DP_RESOURCE_STATUS_NOTIFY0x13:
1125 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
1126 default:
1127 DRM_ERROR("Got unknown request 0x%02x (%s)\n", msg->req_type,__drm_err("Got unknown request 0x%02x (%s)\n", msg->req_type
, drm_dp_mst_req_type_str(msg->req_type))
1128 drm_dp_mst_req_type_str(msg->req_type))__drm_err("Got unknown request 0x%02x (%s)\n", msg->req_type
, drm_dp_mst_req_type_str(msg->req_type))
;
1129 return false0;
1130 }
1131}
1132
1133static void build_dpcd_write(struct drm_dp_sideband_msg_tx *msg,
1134 u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
1135{
1136 struct drm_dp_sideband_msg_req_body req;
1137
1138 req.req_type = DP_REMOTE_DPCD_WRITE0x21;
1139 req.u.dpcd_write.port_number = port_num;
1140 req.u.dpcd_write.dpcd_address = offset;
1141 req.u.dpcd_write.num_bytes = num_bytes;
1142 req.u.dpcd_write.bytes = bytes;
1143 drm_dp_encode_sideband_req(&req, msg);
1144}
1145
1146static void build_link_address(struct drm_dp_sideband_msg_tx *msg)
1147{
1148 struct drm_dp_sideband_msg_req_body req;
1149
1150 req.req_type = DP_LINK_ADDRESS0x01;
1151 drm_dp_encode_sideband_req(&req, msg);
1152}
1153
1154static void build_clear_payload_id_table(struct drm_dp_sideband_msg_tx *msg)
1155{
1156 struct drm_dp_sideband_msg_req_body req;
1157
1158 req.req_type = DP_CLEAR_PAYLOAD_ID_TABLE0x14;
1159 drm_dp_encode_sideband_req(&req, msg);
1160 msg->path_msg = true1;
1161}
1162
1163static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg,
1164 int port_num)
1165{
1166 struct drm_dp_sideband_msg_req_body req;
1167
1168 req.req_type = DP_ENUM_PATH_RESOURCES0x10;
1169 req.u.port_num.port_number = port_num;
1170 drm_dp_encode_sideband_req(&req, msg);
1171 msg->path_msg = true1;
1172 return 0;
1173}
1174
1175static void build_allocate_payload(struct drm_dp_sideband_msg_tx *msg,
1176 int port_num,
1177 u8 vcpi, uint16_t pbn,
1178 u8 number_sdp_streams,
1179 u8 *sdp_stream_sink)
1180{
1181 struct drm_dp_sideband_msg_req_body req;
1182
1183 memset(&req, 0, sizeof(req))__builtin_memset((&req), (0), (sizeof(req)));
1184 req.req_type = DP_ALLOCATE_PAYLOAD0x11;
1185 req.u.allocate_payload.port_number = port_num;
1186 req.u.allocate_payload.vcpi = vcpi;
1187 req.u.allocate_payload.pbn = pbn;
1188 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
1189 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,__builtin_memcpy((req.u.allocate_payload.sdp_stream_sink), (sdp_stream_sink
), (number_sdp_streams))
1190 number_sdp_streams)__builtin_memcpy((req.u.allocate_payload.sdp_stream_sink), (sdp_stream_sink
), (number_sdp_streams))
;
1191 drm_dp_encode_sideband_req(&req, msg);
1192 msg->path_msg = true1;
1193}
1194
1195static void build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
1196 int port_num, bool_Bool power_up)
1197{
1198 struct drm_dp_sideband_msg_req_body req;
1199
1200 if (power_up)
1201 req.req_type = DP_POWER_UP_PHY0x24;
1202 else
1203 req.req_type = DP_POWER_DOWN_PHY0x25;
1204
1205 req.u.port_num.port_number = port_num;
1206 drm_dp_encode_sideband_req(&req, msg);
1207 msg->path_msg = true1;
1208}
1209
1210static int
1211build_query_stream_enc_status(struct drm_dp_sideband_msg_tx *msg, u8 stream_id,
1212 u8 *q_id)
1213{
1214 struct drm_dp_sideband_msg_req_body req;
1215
1216 req.req_type = DP_QUERY_STREAM_ENC_STATUS0x38;
1217 req.u.enc_status.stream_id = stream_id;
1218 memcpy(req.u.enc_status.client_id, q_id,__builtin_memcpy((req.u.enc_status.client_id), (q_id), (sizeof
(req.u.enc_status.client_id)))
1219 sizeof(req.u.enc_status.client_id))__builtin_memcpy((req.u.enc_status.client_id), (q_id), (sizeof
(req.u.enc_status.client_id)))
;
1220 req.u.enc_status.stream_event = 0;
1221 req.u.enc_status.valid_stream_event = false0;
1222 req.u.enc_status.stream_behavior = 0;
1223 req.u.enc_status.valid_stream_behavior = false0;
1224
1225 drm_dp_encode_sideband_req(&req, msg);
1226 return 0;
1227}
1228
1229static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
1230 struct drm_dp_vcpi *vcpi)
1231{
1232 int ret, vcpi_ret;
1233
1234 mutex_lock(&mgr->payload_lock)rw_enter_write(&mgr->payload_lock);
1235 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
1236 if (ret > mgr->max_payloads) {
1237 ret = -EINVAL22;
1238 DRM_DEBUG_KMS("out of payload ids %d\n", ret)__drm_dbg(DRM_UT_KMS, "out of payload ids %d\n", ret);
1239 goto out_unlock;
1240 }
1241
1242 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
1243 if (vcpi_ret > mgr->max_payloads) {
1244 ret = -EINVAL22;
1245 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret)__drm_dbg(DRM_UT_KMS, "out of vcpi ids %d\n", ret);
1246 goto out_unlock;
1247 }
1248
1249 set_bit(ret, &mgr->payload_mask);
1250 set_bit(vcpi_ret, &mgr->vcpi_mask);
1251 vcpi->vcpi = vcpi_ret + 1;
1252 mgr->proposed_vcpis[ret - 1] = vcpi;
1253out_unlock:
1254 mutex_unlock(&mgr->payload_lock)rw_exit_write(&mgr->payload_lock);
1255 return ret;
1256}
1257
1258static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
1259 int vcpi)
1260{
1261 int i;
1262
1263 if (vcpi == 0)
1264 return;
1265
1266 mutex_lock(&mgr->payload_lock)rw_enter_write(&mgr->payload_lock);
1267 DRM_DEBUG_KMS("putting payload %d\n", vcpi)__drm_dbg(DRM_UT_KMS, "putting payload %d\n", vcpi);
1268 clear_bit(vcpi - 1, &mgr->vcpi_mask);
1269
1270 for (i = 0; i < mgr->max_payloads; i++) {
1271 if (mgr->proposed_vcpis[i] &&
1272 mgr->proposed_vcpis[i]->vcpi == vcpi) {
1273 mgr->proposed_vcpis[i] = NULL((void *)0);
1274 clear_bit(i + 1, &mgr->payload_mask);
1275 }
1276 }
1277 mutex_unlock(&mgr->payload_lock)rw_exit_write(&mgr->payload_lock);
1278}
1279
1280static bool_Bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
1281 struct drm_dp_sideband_msg_tx *txmsg)
1282{
1283 unsigned int state;
1284
1285 /*
1286 * All updates to txmsg->state are protected by mgr->qlock, and the two
1287 * cases we check here are terminal states. For those the barriers
1288 * provided by the wake_up/wait_event pair are enough.
1289 */
1290 state = READ_ONCE(txmsg->state)({ typeof(txmsg->state) __tmp = *(volatile typeof(txmsg->
state) *)&(txmsg->state); membar_datadep_consumer(); __tmp
; })
;
1291 return (state == DRM_DP_SIDEBAND_TX_RX3 ||
1292 state == DRM_DP_SIDEBAND_TX_TIMEOUT4);
1293}
1294
1295static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
1296 struct drm_dp_sideband_msg_tx *txmsg)
1297{
1298 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1299 unsigned long wait_timeout = msecs_to_jiffies(4000)(((uint64_t)(4000)) * hz / 1000);
1300 unsigned long wait_expires = jiffies + wait_timeout;
1301 int ret;
1302
1303 for (;;) {
1304 /*
1305 * If the driver provides a way for this, change to
1306 * poll-waiting for the MST reply interrupt if we didn't receive
1307 * it for 50 msec. This would cater for cases where the HPD
1308 * pulse signal got lost somewhere, even though the sink raised
1309 * the corresponding MST interrupt correctly. One example is the
1310 * Club 3D CAC-1557 TypeC -> DP adapter which for some reason
1311 * filters out short pulses with a duration less than ~540 usec.
1312 *
1313 * The poll period is 50 msec to avoid missing an interrupt
1314 * after the sink has cleared it (after a 110msec timeout
1315 * since it raised the interrupt).
1316 */
1317 ret = wait_event_timeout(mgr->tx_waitq,({ long __ret = mgr->cbs->poll_hpd_irq ? (((uint64_t)(50
)) * hz / 1000) : wait_timeout; if (!(check_txmsg_state(mgr, txmsg
))) __ret = ({ long ret = mgr->cbs->poll_hpd_irq ? (((uint64_t
)(50)) * hz / 1000) : wait_timeout; do { int __error; unsigned
long deadline; ((!cold) ? (void)0 : __assert("diagnostic ", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 1321, "!cold")); mtx_enter(&sch_mtx); deadline = jiffies
+ ret; __error = msleep(&mgr->tx_waitq, &sch_mtx,
0, "drmweti", ret); ret = deadline - jiffies; if (__error ==
-1 || __error == 4) { ret = -4; mtx_leave(&sch_mtx); break
; } if ((mgr->cbs->poll_hpd_irq ? (((uint64_t)(50)) * hz
/ 1000) : wait_timeout) > 0 && (ret <= 0 || __error
== 35)) { mtx_leave(&sch_mtx); ret = ((check_txmsg_state
(mgr, txmsg))) ? 1 : 0; break; } mtx_leave(&sch_mtx); } while
(ret > 0 && !(check_txmsg_state(mgr, txmsg))); ret
; }); __ret; })
1318 check_txmsg_state(mgr, txmsg),({ long __ret = mgr->cbs->poll_hpd_irq ? (((uint64_t)(50
)) * hz / 1000) : wait_timeout; if (!(check_txmsg_state(mgr, txmsg
))) __ret = ({ long ret = mgr->cbs->poll_hpd_irq ? (((uint64_t
)(50)) * hz / 1000) : wait_timeout; do { int __error; unsigned
long deadline; ((!cold) ? (void)0 : __assert("diagnostic ", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 1321, "!cold")); mtx_enter(&sch_mtx); deadline = jiffies
+ ret; __error = msleep(&mgr->tx_waitq, &sch_mtx,
0, "drmweti", ret); ret = deadline - jiffies; if (__error ==
-1 || __error == 4) { ret = -4; mtx_leave(&sch_mtx); break
; } if ((mgr->cbs->poll_hpd_irq ? (((uint64_t)(50)) * hz
/ 1000) : wait_timeout) > 0 && (ret <= 0 || __error
== 35)) { mtx_leave(&sch_mtx); ret = ((check_txmsg_state
(mgr, txmsg))) ? 1 : 0; break; } mtx_leave(&sch_mtx); } while
(ret > 0 && !(check_txmsg_state(mgr, txmsg))); ret
; }); __ret; })
1319 mgr->cbs->poll_hpd_irq ?({ long __ret = mgr->cbs->poll_hpd_irq ? (((uint64_t)(50
)) * hz / 1000) : wait_timeout; if (!(check_txmsg_state(mgr, txmsg
))) __ret = ({ long ret = mgr->cbs->poll_hpd_irq ? (((uint64_t
)(50)) * hz / 1000) : wait_timeout; do { int __error; unsigned
long deadline; ((!cold) ? (void)0 : __assert("diagnostic ", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 1321, "!cold")); mtx_enter(&sch_mtx); deadline = jiffies
+ ret; __error = msleep(&mgr->tx_waitq, &sch_mtx,
0, "drmweti", ret); ret = deadline - jiffies; if (__error ==
-1 || __error == 4) { ret = -4; mtx_leave(&sch_mtx); break
; } if ((mgr->cbs->poll_hpd_irq ? (((uint64_t)(50)) * hz
/ 1000) : wait_timeout) > 0 && (ret <= 0 || __error
== 35)) { mtx_leave(&sch_mtx); ret = ((check_txmsg_state
(mgr, txmsg))) ? 1 : 0; break; } mtx_leave(&sch_mtx); } while
(ret > 0 && !(check_txmsg_state(mgr, txmsg))); ret
; }); __ret; })
1320 msecs_to_jiffies(50) :({ long __ret = mgr->cbs->poll_hpd_irq ? (((uint64_t)(50
)) * hz / 1000) : wait_timeout; if (!(check_txmsg_state(mgr, txmsg
))) __ret = ({ long ret = mgr->cbs->poll_hpd_irq ? (((uint64_t
)(50)) * hz / 1000) : wait_timeout; do { int __error; unsigned
long deadline; ((!cold) ? (void)0 : __assert("diagnostic ", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 1321, "!cold")); mtx_enter(&sch_mtx); deadline = jiffies
+ ret; __error = msleep(&mgr->tx_waitq, &sch_mtx,
0, "drmweti", ret); ret = deadline - jiffies; if (__error ==
-1 || __error == 4) { ret = -4; mtx_leave(&sch_mtx); break
; } if ((mgr->cbs->poll_hpd_irq ? (((uint64_t)(50)) * hz
/ 1000) : wait_timeout) > 0 && (ret <= 0 || __error
== 35)) { mtx_leave(&sch_mtx); ret = ((check_txmsg_state
(mgr, txmsg))) ? 1 : 0; break; } mtx_leave(&sch_mtx); } while
(ret > 0 && !(check_txmsg_state(mgr, txmsg))); ret
; }); __ret; })
1321 wait_timeout)({ long __ret = mgr->cbs->poll_hpd_irq ? (((uint64_t)(50
)) * hz / 1000) : wait_timeout; if (!(check_txmsg_state(mgr, txmsg
))) __ret = ({ long ret = mgr->cbs->poll_hpd_irq ? (((uint64_t
)(50)) * hz / 1000) : wait_timeout; do { int __error; unsigned
long deadline; ((!cold) ? (void)0 : __assert("diagnostic ", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 1321, "!cold")); mtx_enter(&sch_mtx); deadline = jiffies
+ ret; __error = msleep(&mgr->tx_waitq, &sch_mtx,
0, "drmweti", ret); ret = deadline - jiffies; if (__error ==
-1 || __error == 4) { ret = -4; mtx_leave(&sch_mtx); break
; } if ((mgr->cbs->poll_hpd_irq ? (((uint64_t)(50)) * hz
/ 1000) : wait_timeout) > 0 && (ret <= 0 || __error
== 35)) { mtx_leave(&sch_mtx); ret = ((check_txmsg_state
(mgr, txmsg))) ? 1 : 0; break; } mtx_leave(&sch_mtx); } while
(ret > 0 && !(check_txmsg_state(mgr, txmsg))); ret
; }); __ret; })
;
1322
1323 if (ret || !mgr->cbs->poll_hpd_irq ||
1324 time_after(jiffies, wait_expires)((long)(wait_expires) - (long)(jiffies) < 0))
1325 break;
1326
1327 mgr->cbs->poll_hpd_irq(mgr);
1328 }
1329
1330 mutex_lock(&mgr->qlock)rw_enter_write(&mgr->qlock);
1331 if (ret > 0) {
1332 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT4) {
1333 ret = -EIO5;
1334 goto out;
1335 }
1336 } else {
1337 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno)__drm_dbg(DRM_UT_KMS, "timedout msg send %p %d %d\n", txmsg, txmsg
->state, txmsg->seqno)
;
1338
1339 /* dump some state */
1340 ret = -EIO5;
1341
1342 /* remove from q */
1343 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED0 ||
1344 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND1 ||
1345 txmsg->state == DRM_DP_SIDEBAND_TX_SENT2)
1346 list_del(&txmsg->next);
1347 }
1348out:
1349 if (unlikely(ret == -EIO)__builtin_expect(!!(ret == -5), 0) && drm_debug_enabled(DRM_UT_DP)) {
1350 struct drm_printer p = drm_debug_printer(DBG_PREFIX"[dp_mst]");
1351
1352 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
1353 }
1354 mutex_unlock(&mgr->qlock)rw_exit_write(&mgr->qlock);
1355
1356 drm_dp_mst_kick_tx(mgr);
1357 return ret;
1358}
1359
1360static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
1361{
1362 struct drm_dp_mst_branch *mstb;
1363
1364 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL(0x0001 | 0x0004));
1365 if (!mstb)
1366 return NULL((void *)0);
1367
1368 mstb->lct = lct;
1369 if (lct > 1)
1370 memcpy(mstb->rad, rad, lct / 2)__builtin_memcpy((mstb->rad), (rad), (lct / 2));
1371 INIT_LIST_HEAD(&mstb->ports);
1372 kref_init(&mstb->topology_kref);
1373 kref_init(&mstb->malloc_kref);
1374 return mstb;
1375}
1376
1377static void drm_dp_free_mst_branch_device(struct kref *kref)
1378{
1379 struct drm_dp_mst_branch *mstb =
1380 container_of(kref, struct drm_dp_mst_branch, malloc_kref)({ const __typeof( ((struct drm_dp_mst_branch *)0)->malloc_kref
) *__mptr = (kref); (struct drm_dp_mst_branch *)( (char *)__mptr
- __builtin_offsetof(struct drm_dp_mst_branch, malloc_kref) )
;})
;
1381
1382 if (mstb->port_parent)
1383 drm_dp_mst_put_port_malloc(mstb->port_parent);
1384
1385 kfree(mstb);
1386}
1387
1388/**
1389 * DOC: Branch device and port refcounting
1390 *
1391 * Topology refcount overview
1392 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
1393 *
1394 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
1395 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
1396 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
1397 *
1398 * Topology refcounts are not exposed to drivers, and are handled internally
1399 * by the DP MST helpers. The helpers use them in order to prevent the
1400 * in-memory topology state from being changed in the middle of critical
1401 * operations like changing the internal state of payload allocations. This
1402 * means each branch and port will be considered to be connected to the rest
1403 * of the topology until its topology refcount reaches zero. Additionally,
1404 * for ports this means that their associated &struct drm_connector will stay
1405 * registered with userspace until the port's refcount reaches 0.
1406 *
1407 * Malloc refcount overview
1408 * ~~~~~~~~~~~~~~~~~~~~~~~~
1409 *
1410 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
1411 * drm_dp_mst_branch allocated even after all of its topology references have
1412 * been dropped, so that the driver or MST helpers can safely access each
1413 * branch's last known state before it was disconnected from the topology.
1414 * When the malloc refcount of a port or branch reaches 0, the memory
1415 * allocation containing the &struct drm_dp_mst_branch or &struct
1416 * drm_dp_mst_port respectively will be freed.
1417 *
1418 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
1419 * to drivers. As of writing this documentation, there are no drivers that
1420 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
1421 * helpers. Exposing this API to drivers in a race-free manner would take more
1422 * tweaking of the refcounting scheme, however patches are welcome provided
1423 * there is a legitimate driver usecase for this.
1424 *
1425 * Refcount relationships in a topology
1426 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1427 *
1428 * Let's take a look at why the relationship between topology and malloc
1429 * refcounts is designed the way it is.
1430 *
1431 * .. kernel-figure:: dp-mst/topology-figure-1.dot
1432 *
1433 * An example of topology and malloc refs in a DP MST topology with two
1434 * active payloads. Topology refcount increments are indicated by solid
1435 * lines, and malloc refcount increments are indicated by dashed lines.
1436 * Each starts from the branch which incremented the refcount, and ends at
1437 * the branch to which the refcount belongs to, i.e. the arrow points the
1438 * same way as the C pointers used to reference a structure.
1439 *
1440 * As you can see in the above figure, every branch increments the topology
1441 * refcount of its children, and increments the malloc refcount of its
1442 * parent. Additionally, every payload increments the malloc refcount of its
1443 * assigned port by 1.
1444 *
1445 * So, what would happen if MSTB #3 from the above figure was unplugged from
1446 * the system, but the driver hadn't yet removed payload #2 from port #3? The
1447 * topology would start to look like the figure below.
1448 *
1449 * .. kernel-figure:: dp-mst/topology-figure-2.dot
1450 *
1451 * Ports and branch devices which have been released from memory are
1452 * colored grey, and references which have been removed are colored red.
1453 *
1454 * Whenever a port or branch device's topology refcount reaches zero, it will
1455 * decrement the topology refcounts of all its children, the malloc refcount
1456 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
1457 * #4, this means they both have been disconnected from the topology and freed
1458 * from memory. But, because payload #2 is still holding a reference to port
1459 * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
1460 * is still accessible from memory. This also means port #3 has not yet
1461 * decremented the malloc refcount of MSTB #3, so its &struct
1462 * drm_dp_mst_branch will also stay allocated in memory until port #3's
1463 * malloc refcount reaches 0.
1464 *
1465 * This relationship is necessary because in order to release payload #2, we
1466 * need to be able to figure out the last relative of port #3 that's still
1467 * connected to the topology. In this case, we would travel up the topology as
1468 * shown below.
1469 *
1470 * .. kernel-figure:: dp-mst/topology-figure-3.dot
1471 *
1472 * And finally, remove payload #2 by communicating with port #2 through
1473 * sideband transactions.
1474 */
1475
1476/**
1477 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
1478 * device
1479 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
1480 *
1481 * Increments &drm_dp_mst_branch.malloc_kref. When
1482 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1483 * will be released and @mstb may no longer be used.
1484 *
1485 * See also: drm_dp_mst_put_mstb_malloc()
1486 */
1487static void
1488drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
1489{
1490 kref_get(&mstb->malloc_kref);
1491 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref))__drm_dbg(DRM_UT_CORE, "mstb %p (%d)\n", mstb, kref_read(&
mstb->malloc_kref))
;
1492}
1493
1494/**
1495 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
1496 * device
1497 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
1498 *
1499 * Decrements &drm_dp_mst_branch.malloc_kref. When
1500 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1501 * will be released and @mstb may no longer be used.
1502 *
1503 * See also: drm_dp_mst_get_mstb_malloc()
1504 */
1505static void
1506drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
1507{
1508 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1)__drm_dbg(DRM_UT_CORE, "mstb %p (%d)\n", mstb, kref_read(&
mstb->malloc_kref) - 1)
;
1509 kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
1510}
1511
1512static void drm_dp_free_mst_port(struct kref *kref)
1513{
1514 struct drm_dp_mst_port *port =
1515 container_of(kref, struct drm_dp_mst_port, malloc_kref)({ const __typeof( ((struct drm_dp_mst_port *)0)->malloc_kref
) *__mptr = (kref); (struct drm_dp_mst_port *)( (char *)__mptr
- __builtin_offsetof(struct drm_dp_mst_port, malloc_kref) );
})
;
1516
1517 drm_dp_mst_put_mstb_malloc(port->parent);
1518 kfree(port);
1519}
1520
1521/**
1522 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1523 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1524 *
1525 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1526 * reaches 0, the memory allocation for @port will be released and @port may
1527 * no longer be used.
1528 *
1529 * Because @port could potentially be freed at any time by the DP MST helpers
1530 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1531 * function, drivers that which to make use of &struct drm_dp_mst_port should
1532 * ensure that they grab at least one main malloc reference to their MST ports
1533 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1534 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1535 *
1536 * See also: drm_dp_mst_put_port_malloc()
1537 */
1538void
1539drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1540{
1541 kref_get(&port->malloc_kref);
1542 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref))__drm_dbg(DRM_UT_CORE, "port %p (%d)\n", port, kref_read(&
port->malloc_kref))
;
1543}
1544EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1545
1546/**
1547 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1548 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1549 *
1550 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1551 * reaches 0, the memory allocation for @port will be released and @port may
1552 * no longer be used.
1553 *
1554 * See also: drm_dp_mst_get_port_malloc()
1555 */
1556void
1557drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1558{
1559 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1)__drm_dbg(DRM_UT_CORE, "port %p (%d)\n", port, kref_read(&
port->malloc_kref) - 1)
;
1560 kref_put(&port->malloc_kref, drm_dp_free_mst_port);
1561}
1562EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1563
1564#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)0
1565
1566#define STACK_DEPTH 8
1567
1568static noinline__attribute__((__noinline__)) void
1569__topology_ref_save(struct drm_dp_mst_topology_mgr *mgr,
1570 struct drm_dp_mst_topology_ref_history *history,
1571 enum drm_dp_mst_topology_ref_type type)
1572{
1573 struct drm_dp_mst_topology_ref_entry *entry = NULL((void *)0);
1574 depot_stack_handle_t backtrace;
1575 ulong stack_entries[STACK_DEPTH];
1576 uint n;
1577 int i;
1578
1579 n = stack_trace_save(stack_entries, ARRAY_SIZE(stack_entries)(sizeof((stack_entries)) / sizeof((stack_entries)[0])), 1);
1580 backtrace = stack_depot_save(stack_entries, n, GFP_KERNEL(0x0001 | 0x0004));
1581 if (!backtrace)
1582 return;
1583
1584 /* Try to find an existing entry for this backtrace */
1585 for (i = 0; i < history->len; i++) {
1586 if (history->entries[i].backtrace == backtrace) {
1587 entry = &history->entries[i];
1588 break;
1589 }
1590 }
1591
1592 /* Otherwise add one */
1593 if (!entry) {
1594 struct drm_dp_mst_topology_ref_entry *new;
1595 int new_len = history->len + 1;
1596
1597 new = krealloc(history->entries, sizeof(*new) * new_len,
1598 GFP_KERNEL(0x0001 | 0x0004));
1599 if (!new)
1600 return;
1601
1602 entry = &new[history->len];
1603 history->len = new_len;
1604 history->entries = new;
1605
1606 entry->backtrace = backtrace;
1607 entry->type = type;
1608 entry->count = 0;
1609 }
1610 entry->count++;
1611 entry->ts_nsec = ktime_get_ns();
1612}
1613
1614static int
1615topology_ref_history_cmp(const void *a, const void *b)
1616{
1617 const struct drm_dp_mst_topology_ref_entry *entry_a = a, *entry_b = b;
1618
1619 if (entry_a->ts_nsec > entry_b->ts_nsec)
1620 return 1;
1621 else if (entry_a->ts_nsec < entry_b->ts_nsec)
1622 return -1;
1623 else
1624 return 0;
1625}
1626
1627static inline const char *
1628topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type)
1629{
1630 if (type == DRM_DP_MST_TOPOLOGY_REF_GET)
1631 return "get";
1632 else
1633 return "put";
1634}
1635
1636static void
1637__dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
1638 void *ptr, const char *type_str)
1639{
1640 struct drm_printer p = drm_debug_printer(DBG_PREFIX"[dp_mst]");
1641 char *buf = kzalloc(PAGE_SIZE(1 << 12), GFP_KERNEL(0x0001 | 0x0004));
1642 int i;
1643
1644 if (!buf)
1645 return;
1646
1647 if (!history->len)
1648 goto out;
1649
1650 /* First, sort the list so that it goes from oldest to newest
1651 * reference entry
1652 */
1653 sort(history->entries, history->len, sizeof(*history->entries),
1654 topology_ref_history_cmp, NULL((void *)0));
1655
1656 drm_printf(&p, "%s (%p) topology count reached 0, dumping history:\n",
1657 type_str, ptr);
1658
1659 for (i = 0; i < history->len; i++) {
1660 const struct drm_dp_mst_topology_ref_entry *entry =
1661 &history->entries[i];
1662 ulong *entries;
1663 uint nr_entries;
1664 u64 ts_nsec = entry->ts_nsec;
1665 u32 rem_nsec = do_div(ts_nsec, 1000000000)({ uint32_t __base = (1000000000); uint32_t __rem = ((uint64_t
)(ts_nsec)) % __base; (ts_nsec) = ((uint64_t)(ts_nsec)) / __base
; __rem; })
;
1666
1667 nr_entries = stack_depot_fetch(entry->backtrace, &entries);
1668 stack_trace_snprint(buf, PAGE_SIZE(1 << 12), entries, nr_entries, 4);
1669
1670 drm_printf(&p, " %d %ss (last at %5llu.%06u):\n%s",
1671 entry->count,
1672 topology_ref_type_to_str(entry->type),
1673 ts_nsec, rem_nsec / 1000, buf);
1674 }
1675
1676 /* Now free the history, since this is the only time we expose it */
1677 kfree(history->entries);
1678out:
1679 kfree(buf);
1680}
1681
1682static __always_inline__attribute__((__always_inline__)) void
1683drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)
1684{
1685 __dump_topology_ref_history(&mstb->topology_ref_history, mstb,
1686 "MSTB");
1687}
1688
1689static __always_inline__attribute__((__always_inline__)) void
1690drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port)
1691{
1692 __dump_topology_ref_history(&port->topology_ref_history, port,
1693 "Port");
1694}
1695
1696static __always_inline__attribute__((__always_inline__)) void
1697save_mstb_topology_ref(struct drm_dp_mst_branch *mstb,
1698 enum drm_dp_mst_topology_ref_type type)
1699{
1700 __topology_ref_save(mstb->mgr, &mstb->topology_ref_history, type);
1701}
1702
1703static __always_inline__attribute__((__always_inline__)) void
1704save_port_topology_ref(struct drm_dp_mst_port *port,
1705 enum drm_dp_mst_topology_ref_type type)
1706{
1707 __topology_ref_save(port->mgr, &port->topology_ref_history, type);
1708}
1709
1710static inline void
1711topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr)
1712{
1713 mutex_lock(&mgr->topology_ref_history_lock)rw_enter_write(&mgr->topology_ref_history_lock);
1714}
1715
1716static inline void
1717topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr)
1718{
1719 mutex_unlock(&mgr->topology_ref_history_lock)rw_exit_write(&mgr->topology_ref_history_lock);
1720}
1721#else
1722static inline void
1723topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr) {}
1724static inline void
1725topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {}
1726static inline void
1727drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb) {}
1728static inline void
1729drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port) {}
1730#define save_mstb_topology_ref(mstb, type)
1731#define save_port_topology_ref(port, type)
1732#endif
1733
1734static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1735{
1736 struct drm_dp_mst_branch *mstb =
1737 container_of(kref, struct drm_dp_mst_branch, topology_kref)({ const __typeof( ((struct drm_dp_mst_branch *)0)->topology_kref
) *__mptr = (kref); (struct drm_dp_mst_branch *)( (char *)__mptr
- __builtin_offsetof(struct drm_dp_mst_branch, topology_kref
) );})
;
1738 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1739
1740 drm_dp_mst_dump_mstb_topology_history(mstb);
1741
1742 INIT_LIST_HEAD(&mstb->destroy_next);
1743
1744 /*
1745 * This can get called under mgr->mutex, so we need to perform the
1746 * actual destruction of the mstb in another worker
1747 */
1748 mutex_lock(&mgr->delayed_destroy_lock)rw_enter_write(&mgr->delayed_destroy_lock);
1749 list_add(&mstb->destroy_next, &mgr->destroy_branch_device_list);
1750 mutex_unlock(&mgr->delayed_destroy_lock)rw_exit_write(&mgr->delayed_destroy_lock);
1751 queue_work(mgr->delayed_destroy_wq, &mgr->delayed_destroy_work);
1752}
1753
1754/**
1755 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1756 * branch device unless it's zero
1757 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1758 *
1759 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1760 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1761 * reached 0). Holding a topology reference implies that a malloc reference
1762 * will be held to @mstb as long as the user holds the topology reference.
1763 *
1764 * Care should be taken to ensure that the user has at least one malloc
1765 * reference to @mstb. If you already have a topology reference to @mstb, you
1766 * should use drm_dp_mst_topology_get_mstb() instead.
1767 *
1768 * See also:
1769 * drm_dp_mst_topology_get_mstb()
1770 * drm_dp_mst_topology_put_mstb()
1771 *
1772 * Returns:
1773 * * 1: A topology reference was grabbed successfully
1774 * * 0: @port is no longer in the topology, no reference was grabbed
1775 */
1776static int __must_check
1777drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
1778{
1779 int ret;
1780
1781 topology_ref_history_lock(mstb->mgr);
1782 ret = kref_get_unless_zero(&mstb->topology_kref);
1783 if (ret) {
1784 DRM_DEBUG("mstb %p (%d)\n",__drm_dbg(DRM_UT_CORE, "mstb %p (%d)\n", mstb, kref_read(&
mstb->topology_kref))
1785 mstb, kref_read(&mstb->topology_kref))__drm_dbg(DRM_UT_CORE, "mstb %p (%d)\n", mstb, kref_read(&
mstb->topology_kref))
;
1786 save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_GET);
1787 }
1788
1789 topology_ref_history_unlock(mstb->mgr);
1790
1791 return ret;
1792}
1793
1794/**
1795 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1796 * branch device
1797 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1798 *
1799 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1800 * not it's already reached 0. This is only valid to use in scenarios where
1801 * you are already guaranteed to have at least one active topology reference
1802 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1803 *
1804 * See also:
1805 * drm_dp_mst_topology_try_get_mstb()
1806 * drm_dp_mst_topology_put_mstb()
1807 */
1808static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1809{
1810 topology_ref_history_lock(mstb->mgr);
1811
1812 save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_GET);
1813 WARN_ON(kref_read(&mstb->topology_kref) == 0)({ int __ret = !!(kref_read(&mstb->topology_kref) == 0
); if (__ret) printf("WARNING %s failed at %s:%d\n", "kref_read(&mstb->topology_kref) == 0"
, "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c", 1813); __builtin_expect
(!!(__ret), 0); })
;
1814 kref_get(&mstb->topology_kref);
1815 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref))__drm_dbg(DRM_UT_CORE, "mstb %p (%d)\n", mstb, kref_read(&
mstb->topology_kref))
;
1816
1817 topology_ref_history_unlock(mstb->mgr);
1818}
1819
1820/**
1821 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1822 * device
1823 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1824 *
1825 * Releases a topology reference from @mstb by decrementing
1826 * &drm_dp_mst_branch.topology_kref.
1827 *
1828 * See also:
1829 * drm_dp_mst_topology_try_get_mstb()
1830 * drm_dp_mst_topology_get_mstb()
1831 */
1832static void
1833drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1834{
1835 topology_ref_history_lock(mstb->mgr);
1836
1837 DRM_DEBUG("mstb %p (%d)\n",__drm_dbg(DRM_UT_CORE, "mstb %p (%d)\n", mstb, kref_read(&
mstb->topology_kref) - 1)
1838 mstb, kref_read(&mstb->topology_kref) - 1)__drm_dbg(DRM_UT_CORE, "mstb %p (%d)\n", mstb, kref_read(&
mstb->topology_kref) - 1)
;
1839 save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_PUT);
1840
1841 topology_ref_history_unlock(mstb->mgr);
1842 kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
1843}
1844
1845static void drm_dp_destroy_port(struct kref *kref)
1846{
1847 struct drm_dp_mst_port *port =
1848 container_of(kref, struct drm_dp_mst_port, topology_kref)({ const __typeof( ((struct drm_dp_mst_port *)0)->topology_kref
) *__mptr = (kref); (struct drm_dp_mst_port *)( (char *)__mptr
- __builtin_offsetof(struct drm_dp_mst_port, topology_kref) )
;})
;
1849 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
1850
1851 drm_dp_mst_dump_port_topology_history(port);
1852
1853 /* There's nothing that needs locking to destroy an input port yet */
1854 if (port->input) {
1855 drm_dp_mst_put_port_malloc(port);
1856 return;
1857 }
1858
1859 kfree(port->cached_edid);
1860
1861 /*
1862 * we can't destroy the connector here, as we might be holding the
1863 * mode_config.mutex from an EDID retrieval
1864 */
1865 mutex_lock(&mgr->delayed_destroy_lock)rw_enter_write(&mgr->delayed_destroy_lock);
1866 list_add(&port->next, &mgr->destroy_port_list);
1867 mutex_unlock(&mgr->delayed_destroy_lock)rw_exit_write(&mgr->delayed_destroy_lock);
1868 queue_work(mgr->delayed_destroy_wq, &mgr->delayed_destroy_work);
1869}
1870
1871/**
1872 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1873 * port unless it's zero
1874 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1875 *
1876 * Attempts to grab a topology reference to @port, if it hasn't yet been
1877 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1878 * 0). Holding a topology reference implies that a malloc reference will be
1879 * held to @port as long as the user holds the topology reference.
1880 *
1881 * Care should be taken to ensure that the user has at least one malloc
1882 * reference to @port. If you already have a topology reference to @port, you
1883 * should use drm_dp_mst_topology_get_port() instead.
1884 *
1885 * See also:
1886 * drm_dp_mst_topology_get_port()
1887 * drm_dp_mst_topology_put_port()
1888 *
1889 * Returns:
1890 * * 1: A topology reference was grabbed successfully
1891 * * 0: @port is no longer in the topology, no reference was grabbed
1892 */
1893static int __must_check
1894drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1895{
1896 int ret;
1897
1898 topology_ref_history_lock(port->mgr);
1899 ret = kref_get_unless_zero(&port->topology_kref);
1900 if (ret) {
1901 DRM_DEBUG("port %p (%d)\n",__drm_dbg(DRM_UT_CORE, "port %p (%d)\n", port, kref_read(&
port->topology_kref))
1902 port, kref_read(&port->topology_kref))__drm_dbg(DRM_UT_CORE, "port %p (%d)\n", port, kref_read(&
port->topology_kref))
;
1903 save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_GET);
1904 }
1905
1906 topology_ref_history_unlock(port->mgr);
1907 return ret;
1908}
1909
1910/**
1911 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1912 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1913 *
1914 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1915 * not it's already reached 0. This is only valid to use in scenarios where
1916 * you are already guaranteed to have at least one active topology reference
1917 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1918 *
1919 * See also:
1920 * drm_dp_mst_topology_try_get_port()
1921 * drm_dp_mst_topology_put_port()
1922 */
1923static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1924{
1925 topology_ref_history_lock(port->mgr);
1926
1927 WARN_ON(kref_read(&port->topology_kref) == 0)({ int __ret = !!(kref_read(&port->topology_kref) == 0
); if (__ret) printf("WARNING %s failed at %s:%d\n", "kref_read(&port->topology_kref) == 0"
, "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c", 1927); __builtin_expect
(!!(__ret), 0); })
;
1928 kref_get(&port->topology_kref);
1929 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref))__drm_dbg(DRM_UT_CORE, "port %p (%d)\n", port, kref_read(&
port->topology_kref))
;
1930 save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_GET);
1931
1932 topology_ref_history_unlock(port->mgr);
1933}
1934
1935/**
1936 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1937 * @port: The &struct drm_dp_mst_port to release the topology reference from
1938 *
1939 * Releases a topology reference from @port by decrementing
1940 * &drm_dp_mst_port.topology_kref.
1941 *
1942 * See also:
1943 * drm_dp_mst_topology_try_get_port()
1944 * drm_dp_mst_topology_get_port()
1945 */
1946static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
1947{
1948 topology_ref_history_lock(port->mgr);
1949
1950 DRM_DEBUG("port %p (%d)\n",__drm_dbg(DRM_UT_CORE, "port %p (%d)\n", port, kref_read(&
port->topology_kref) - 1)
1951 port, kref_read(&port->topology_kref) - 1)__drm_dbg(DRM_UT_CORE, "port %p (%d)\n", port, kref_read(&
port->topology_kref) - 1)
;
1952 save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_PUT);
1953
1954 topology_ref_history_unlock(port->mgr);
1955 kref_put(&port->topology_kref, drm_dp_destroy_port);
1956}
1957
1958static struct drm_dp_mst_branch *
1959drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1960 struct drm_dp_mst_branch *to_find)
1961{
1962 struct drm_dp_mst_port *port;
1963 struct drm_dp_mst_branch *rmstb;
1964
1965 if (to_find == mstb)
1966 return mstb;
1967
1968 list_for_each_entry(port, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}); &port->next != (&mstb->ports); port = (
{ const __typeof( ((__typeof(*port) *)0)->next ) *__mptr =
(port->next.next); (__typeof(*port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*port), next) );}))
{
1969 if (port->mstb) {
1970 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1971 port->mstb, to_find);
1972 if (rmstb)
1973 return rmstb;
1974 }
1975 }
1976 return NULL((void *)0);
1977}
1978
1979static struct drm_dp_mst_branch *
1980drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1981 struct drm_dp_mst_branch *mstb)
1982{
1983 struct drm_dp_mst_branch *rmstb = NULL((void *)0);
1984
1985 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
1986 if (mgr->mst_primary) {
1987 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1988 mgr->mst_primary, mstb);
1989
1990 if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
1991 rmstb = NULL((void *)0);
1992 }
1993 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
1994 return rmstb;
1995}
1996
1997static struct drm_dp_mst_port *
1998drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1999 struct drm_dp_mst_port *to_find)
2000{
2001 struct drm_dp_mst_port *port, *mport;
2002
2003 list_for_each_entry(port, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}); &port->next != (&mstb->ports); port = (
{ const __typeof( ((__typeof(*port) *)0)->next ) *__mptr =
(port->next.next); (__typeof(*port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*port), next) );}))
{
2004 if (port == to_find)
2005 return port;
2006
2007 if (port->mstb) {
2008 mport = drm_dp_mst_topology_get_port_validated_locked(
2009 port->mstb, to_find);
2010 if (mport)
2011 return mport;
2012 }
2013 }
2014 return NULL((void *)0);
2015}
2016
2017static struct drm_dp_mst_port *
2018drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
2019 struct drm_dp_mst_port *port)
2020{
2021 struct drm_dp_mst_port *rport = NULL((void *)0);
2022
2023 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
2024 if (mgr->mst_primary) {
2025 rport = drm_dp_mst_topology_get_port_validated_locked(
2026 mgr->mst_primary, port);
2027
2028 if (rport && !drm_dp_mst_topology_try_get_port(rport))
2029 rport = NULL((void *)0);
2030 }
2031 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
2032 return rport;
2033}
2034
2035static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
2036{
2037 struct drm_dp_mst_port *port;
2038 int ret;
2039
2040 list_for_each_entry(port, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}); &port->next != (&mstb->ports); port = (
{ const __typeof( ((__typeof(*port) *)0)->next ) *__mptr =
(port->next.next); (__typeof(*port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*port), next) );}))
{
2041 if (port->port_num == port_num) {
2042 ret = drm_dp_mst_topology_try_get_port(port);
2043 return ret ? port : NULL((void *)0);
2044 }
2045 }
2046
2047 return NULL((void *)0);
2048}
2049
2050/*
2051 * calculate a new RAD for this MST branch device
2052 * if parent has an LCT of 2 then it has 1 nibble of RAD,
2053 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
2054 */
2055static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
2056 u8 *rad)
2057{
2058 int parent_lct = port->parent->lct;
2059 int shift = 4;
2060 int idx = (parent_lct - 1) / 2;
2061
2062 if (parent_lct > 1) {
2063 memcpy(rad, port->parent->rad, idx + 1)__builtin_memcpy((rad), (port->parent->rad), (idx + 1));
2064 shift = (parent_lct % 2) ? 4 : 0;
2065 } else
2066 rad[0] = 0;
2067
2068 rad[idx] |= port->port_num << shift;
2069 return parent_lct + 1;
2070}
2071
2072static bool_Bool drm_dp_mst_is_end_device(u8 pdt, bool_Bool mcs)
2073{
2074 switch (pdt) {
2075 case DP_PEER_DEVICE_DP_LEGACY_CONV0x4:
2076 case DP_PEER_DEVICE_SST_SINK0x3:
2077 return true1;
2078 case DP_PEER_DEVICE_MST_BRANCHING0x2:
2079 /* For sst branch device */
2080 if (!mcs)
2081 return true1;
2082
2083 return false0;
2084 }
2085 return true1;
2086}
2087
2088static int
2089drm_dp_port_set_pdt(struct drm_dp_mst_port *port, u8 new_pdt,
2090 bool_Bool new_mcs)
2091{
2092 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
2093 struct drm_dp_mst_branch *mstb;
2094 u8 rad[8], lct;
2095 int ret = 0;
2096
2097 if (port->pdt == new_pdt && port->mcs == new_mcs)
2098 return 0;
2099
2100 /* Teardown the old pdt, if there is one */
2101 if (port->pdt != DP_PEER_DEVICE_NONE0x0) {
2102 if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
2103 /*
2104 * If the new PDT would also have an i2c bus,
2105 * don't bother with reregistering it
2106 */
2107 if (new_pdt != DP_PEER_DEVICE_NONE0x0 &&
2108 drm_dp_mst_is_end_device(new_pdt, new_mcs)) {
2109 port->pdt = new_pdt;
2110 port->mcs = new_mcs;
2111 return 0;
2112 }
2113
2114 /* remove i2c over sideband */
2115 drm_dp_mst_unregister_i2c_bus(port);
2116 } else {
2117 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
2118 drm_dp_mst_topology_put_mstb(port->mstb);
2119 port->mstb = NULL((void *)0);
2120 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
2121 }
2122 }
2123
2124 port->pdt = new_pdt;
2125 port->mcs = new_mcs;
2126
2127 if (port->pdt != DP_PEER_DEVICE_NONE0x0) {
2128 if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
2129 /* add i2c over sideband */
2130 ret = drm_dp_mst_register_i2c_bus(port);
2131 } else {
2132 lct = drm_dp_calculate_rad(port, rad);
2133 mstb = drm_dp_add_mst_branch_device(lct, rad);
2134 if (!mstb) {
2135 ret = -ENOMEM12;
2136 DRM_ERROR("Failed to create MSTB for port %p",__drm_err("Failed to create MSTB for port %p", port)
2137 port)__drm_err("Failed to create MSTB for port %p", port);
2138 goto out;
2139 }
2140
2141 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
2142 port->mstb = mstb;
2143 mstb->mgr = port->mgr;
2144 mstb->port_parent = port;
2145
2146 /*
2147 * Make sure this port's memory allocation stays
2148 * around until its child MSTB releases it
2149 */
2150 drm_dp_mst_get_port_malloc(port);
2151 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
2152
2153 /* And make sure we send a link address for this */
2154 ret = 1;
2155 }
2156 }
2157
2158out:
2159 if (ret < 0)
2160 port->pdt = DP_PEER_DEVICE_NONE0x0;
2161 return ret;
2162}
2163
2164/**
2165 * drm_dp_mst_dpcd_read() - read a series of bytes from the DPCD via sideband
2166 * @aux: Fake sideband AUX CH
2167 * @offset: address of the (first) register to read
2168 * @buffer: buffer to store the register values
2169 * @size: number of bytes in @buffer
2170 *
2171 * Performs the same functionality for remote devices via
2172 * sideband messaging as drm_dp_dpcd_read() does for local
2173 * devices via actual AUX CH.
2174 *
2175 * Return: Number of bytes read, or negative error code on failure.
2176 */
2177ssize_t drm_dp_mst_dpcd_read(struct drm_dp_aux *aux,
2178 unsigned int offset, void *buffer, size_t size)
2179{
2180 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,({ const __typeof( ((struct drm_dp_mst_port *)0)->aux ) *__mptr
= (aux); (struct drm_dp_mst_port *)( (char *)__mptr - __builtin_offsetof
(struct drm_dp_mst_port, aux) );})
2181 aux)({ const __typeof( ((struct drm_dp_mst_port *)0)->aux ) *__mptr
= (aux); (struct drm_dp_mst_port *)( (char *)__mptr - __builtin_offsetof
(struct drm_dp_mst_port, aux) );})
;
2182
2183 return drm_dp_send_dpcd_read(port->mgr, port,
2184 offset, size, buffer);
2185}
2186
2187/**
2188 * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband
2189 * @aux: Fake sideband AUX CH
2190 * @offset: address of the (first) register to write
2191 * @buffer: buffer containing the values to write
2192 * @size: number of bytes in @buffer
2193 *
2194 * Performs the same functionality for remote devices via
2195 * sideband messaging as drm_dp_dpcd_write() does for local
2196 * devices via actual AUX CH.
2197 *
2198 * Return: number of bytes written on success, negative error code on failure.
2199 */
2200ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux,
2201 unsigned int offset, void *buffer, size_t size)
2202{
2203 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,({ const __typeof( ((struct drm_dp_mst_port *)0)->aux ) *__mptr
= (aux); (struct drm_dp_mst_port *)( (char *)__mptr - __builtin_offsetof
(struct drm_dp_mst_port, aux) );})
2204 aux)({ const __typeof( ((struct drm_dp_mst_port *)0)->aux ) *__mptr
= (aux); (struct drm_dp_mst_port *)( (char *)__mptr - __builtin_offsetof
(struct drm_dp_mst_port, aux) );})
;
2205
2206 return drm_dp_send_dpcd_write(port->mgr, port,
2207 offset, size, buffer);
2208}
2209
2210static int drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
2211{
2212 int ret = 0;
2213
2214 memcpy(mstb->guid, guid, 16)__builtin_memcpy((mstb->guid), (guid), (16));
2215
2216 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
2217 if (mstb->port_parent) {
2218 ret = drm_dp_send_dpcd_write(mstb->mgr,
2219 mstb->port_parent,
2220 DP_GUID0x030, 16, mstb->guid);
2221 } else {
2222 ret = drm_dp_dpcd_write(mstb->mgr->aux,
2223 DP_GUID0x030, mstb->guid, 16);
2224 }
2225 }
2226
2227 if (ret < 16 && ret > 0)
2228 return -EPROTO95;
2229
2230 return ret == 16 ? 0 : ret;
2231}
2232
2233static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
2234 int pnum,
2235 char *proppath,
2236 size_t proppath_size)
2237{
2238 int i;
2239 char temp[8];
2240
2241 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
2242 for (i = 0; i < (mstb->lct - 1); i++) {
2243 int shift = (i % 2) ? 0 : 4;
2244 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
2245
2246 snprintf(temp, sizeof(temp), "-%d", port_num);
2247 strlcat(proppath, temp, proppath_size);
2248 }
2249 snprintf(temp, sizeof(temp), "-%d", pnum);
2250 strlcat(proppath, temp, proppath_size);
2251}
2252
2253/**
2254 * drm_dp_mst_connector_late_register() - Late MST connector registration
2255 * @connector: The MST connector
2256 * @port: The MST port for this connector
2257 *
2258 * Helper to register the remote aux device for this MST port. Drivers should
2259 * call this from their mst connector's late_register hook to enable MST aux
2260 * devices.
2261 *
2262 * Return: 0 on success, negative error code on failure.
2263 */
2264int drm_dp_mst_connector_late_register(struct drm_connector *connector,
2265 struct drm_dp_mst_port *port)
2266{
2267#ifdef __linux__
2268 DRM_DEBUG_KMS("registering %s remote bus for %s\n",__drm_dbg(DRM_UT_KMS, "registering %s remote bus for %s\n", port
->aux.name, connector->kdev->kobj.name)
2269 port->aux.name, connector->kdev->kobj.name)__drm_dbg(DRM_UT_KMS, "registering %s remote bus for %s\n", port
->aux.name, connector->kdev->kobj.name)
;
2270#else
2271 DRM_DEBUG_KMS("registering %s remote bus\n",__drm_dbg(DRM_UT_KMS, "registering %s remote bus\n", port->
aux.name)
2272 port->aux.name)__drm_dbg(DRM_UT_KMS, "registering %s remote bus\n", port->
aux.name)
;
2273#endif
2274
2275 port->aux.dev = connector->kdev;
2276 return drm_dp_aux_register_devnode(&port->aux);
2277}
2278EXPORT_SYMBOL(drm_dp_mst_connector_late_register);
2279
2280/**
2281 * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration
2282 * @connector: The MST connector
2283 * @port: The MST port for this connector
2284 *
2285 * Helper to unregister the remote aux device for this MST port, registered by
2286 * drm_dp_mst_connector_late_register(). Drivers should call this from their mst
2287 * connector's early_unregister hook.
2288 */
2289void drm_dp_mst_connector_early_unregister(struct drm_connector *connector,
2290 struct drm_dp_mst_port *port)
2291{
2292#ifdef __linux__
2293 DRM_DEBUG_KMS("unregistering %s remote bus for %s\n",__drm_dbg(DRM_UT_KMS, "unregistering %s remote bus for %s\n",
port->aux.name, connector->kdev->kobj.name)
2294 port->aux.name, connector->kdev->kobj.name)__drm_dbg(DRM_UT_KMS, "unregistering %s remote bus for %s\n",
port->aux.name, connector->kdev->kobj.name)
;
2295#else
2296 DRM_DEBUG_KMS("unregistering %s remote bus\n",__drm_dbg(DRM_UT_KMS, "unregistering %s remote bus\n", port->
aux.name)
2297 port->aux.name)__drm_dbg(DRM_UT_KMS, "unregistering %s remote bus\n", port->
aux.name)
;
2298#endif
2299 drm_dp_aux_unregister_devnode(&port->aux);
2300}
2301EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister);
2302
2303static void
2304drm_dp_mst_port_add_connector(struct drm_dp_mst_branch *mstb,
2305 struct drm_dp_mst_port *port)
2306{
2307 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
2308 char proppath[255];
2309 int ret;
2310
2311 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
2312 port->connector = mgr->cbs->add_connector(mgr, port, proppath);
2313 if (!port->connector) {
2314 ret = -ENOMEM12;
2315 goto error;
2316 }
2317
2318 if (port->pdt != DP_PEER_DEVICE_NONE0x0 &&
2319 drm_dp_mst_is_end_device(port->pdt, port->mcs) &&
2320 port->port_num >= DP_MST_LOGICAL_PORT_08) {
2321 port->cached_edid = drm_get_edid(port->connector,
2322 &port->aux.ddc);
2323 drm_connector_set_tile_property(port->connector);
2324 }
2325
2326 drm_connector_register(port->connector);
2327 return;
2328
2329error:
2330 DRM_ERROR("Failed to create connector for port %p: %d\n", port, ret)__drm_err("Failed to create connector for port %p: %d\n", port
, ret)
;
2331}
2332
2333/*
2334 * Drop a topology reference, and unlink the port from the in-memory topology
2335 * layout
2336 */
2337static void
2338drm_dp_mst_topology_unlink_port(struct drm_dp_mst_topology_mgr *mgr,
2339 struct drm_dp_mst_port *port)
2340{
2341 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
2342 port->parent->num_ports--;
2343 list_del(&port->next);
2344 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
2345 drm_dp_mst_topology_put_port(port);
2346}
2347
2348static struct drm_dp_mst_port *
2349drm_dp_mst_add_port(struct drm_device *dev,
2350 struct drm_dp_mst_topology_mgr *mgr,
2351 struct drm_dp_mst_branch *mstb, u8 port_number)
2352{
2353 struct drm_dp_mst_port *port = kzalloc(sizeof(*port), GFP_KERNEL(0x0001 | 0x0004));
2354
2355 if (!port)
2356 return NULL((void *)0);
2357
2358 kref_init(&port->topology_kref);
2359 kref_init(&port->malloc_kref);
2360 port->parent = mstb;
2361 port->port_num = port_number;
2362 port->mgr = mgr;
2363 port->aux.name = "DPMST";
2364 port->aux.dev = dev->dev;
2365 port->aux.is_remote = true1;
2366
2367 /* initialize the MST downstream port's AUX crc work queue */
2368 drm_dp_remote_aux_init(&port->aux);
2369
2370 /*
2371 * Make sure the memory allocation for our parent branch stays
2372 * around until our own memory allocation is released
2373 */
2374 drm_dp_mst_get_mstb_malloc(mstb);
2375
2376 return port;
2377}
2378
2379static int
2380drm_dp_mst_handle_link_address_port(struct drm_dp_mst_branch *mstb,
2381 struct drm_device *dev,
2382 struct drm_dp_link_addr_reply_port *port_msg)
2383{
2384 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
2385 struct drm_dp_mst_port *port;
2386 int old_ddps = 0, ret;
2387 u8 new_pdt = DP_PEER_DEVICE_NONE0x0;
2388 bool_Bool new_mcs = 0;
2389 bool_Bool created = false0, send_link_addr = false0, changed = false0;
2390
2391 port = drm_dp_get_port(mstb, port_msg->port_number);
2392 if (!port) {
2393 port = drm_dp_mst_add_port(dev, mgr, mstb,
2394 port_msg->port_number);
2395 if (!port)
2396 return -ENOMEM12;
2397 created = true1;
2398 changed = true1;
2399 } else if (!port->input && port_msg->input_port && port->connector) {
2400 /* Since port->connector can't be changed here, we create a
2401 * new port if input_port changes from 0 to 1
2402 */
2403 drm_dp_mst_topology_unlink_port(mgr, port);
2404 drm_dp_mst_topology_put_port(port);
2405 port = drm_dp_mst_add_port(dev, mgr, mstb,
2406 port_msg->port_number);
2407 if (!port)
2408 return -ENOMEM12;
2409 changed = true1;
2410 created = true1;
2411 } else if (port->input && !port_msg->input_port) {
2412 changed = true1;
2413 } else if (port->connector) {
2414 /* We're updating a port that's exposed to userspace, so do it
2415 * under lock
2416 */
2417 drm_modeset_lock(&mgr->base.lock, NULL((void *)0));
2418
2419 old_ddps = port->ddps;
2420 changed = port->ddps != port_msg->ddps ||
2421 (port->ddps &&
2422 (port->ldps != port_msg->legacy_device_plug_status ||
2423 port->dpcd_rev != port_msg->dpcd_revision ||
2424 port->mcs != port_msg->mcs ||
2425 port->pdt != port_msg->peer_device_type ||
2426 port->num_sdp_stream_sinks !=
2427 port_msg->num_sdp_stream_sinks));
2428 }
2429
2430 port->input = port_msg->input_port;
2431 if (!port->input)
2432 new_pdt = port_msg->peer_device_type;
2433 new_mcs = port_msg->mcs;
2434 port->ddps = port_msg->ddps;
2435 port->ldps = port_msg->legacy_device_plug_status;
2436 port->dpcd_rev = port_msg->dpcd_revision;
2437 port->num_sdp_streams = port_msg->num_sdp_streams;
2438 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
2439
2440 /* manage mstb port lists with mgr lock - take a reference
2441 for this list */
2442 if (created) {
2443 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
2444 drm_dp_mst_topology_get_port(port);
2445 list_add(&port->next, &mstb->ports);
2446 mstb->num_ports++;
2447 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
2448 }
2449
2450 /*
2451 * Reprobe PBN caps on both hotplug, and when re-probing the link
2452 * for our parent mstb
2453 */
2454 if (old_ddps != port->ddps || !created) {
2455 if (port->ddps && !port->input) {
2456 ret = drm_dp_send_enum_path_resources(mgr, mstb,
2457 port);
2458 if (ret == 1)
2459 changed = true1;
2460 } else {
2461 port->full_pbn = 0;
2462 }
2463 }
2464
2465 ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
2466 if (ret == 1) {
2467 send_link_addr = true1;
2468 } else if (ret < 0) {
2469 DRM_ERROR("Failed to change PDT on port %p: %d\n",__drm_err("Failed to change PDT on port %p: %d\n", port, ret)
2470 port, ret)__drm_err("Failed to change PDT on port %p: %d\n", port, ret);
2471 goto fail;
2472 }
2473
2474 /*
2475 * If this port wasn't just created, then we're reprobing because
2476 * we're coming out of suspend. In this case, always resend the link
2477 * address if there's an MSTB on this port
2478 */
2479 if (!created && port->pdt == DP_PEER_DEVICE_MST_BRANCHING0x2 &&
2480 port->mcs)
2481 send_link_addr = true1;
2482
2483 if (port->connector)
2484 drm_modeset_unlock(&mgr->base.lock);
2485 else if (!port->input)
2486 drm_dp_mst_port_add_connector(mstb, port);
2487
2488 if (send_link_addr && port->mstb) {
2489 ret = drm_dp_send_link_address(mgr, port->mstb);
2490 if (ret == 1) /* MSTB below us changed */
2491 changed = true1;
2492 else if (ret < 0)
2493 goto fail_put;
2494 }
2495
2496 /* put reference to this port */
2497 drm_dp_mst_topology_put_port(port);
2498 return changed;
2499
2500fail:
2501 drm_dp_mst_topology_unlink_port(mgr, port);
2502 if (port->connector)
2503 drm_modeset_unlock(&mgr->base.lock);
2504fail_put:
2505 drm_dp_mst_topology_put_port(port);
2506 return ret;
2507}
2508
2509static void
2510drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch *mstb,
2511 struct drm_dp_connection_status_notify *conn_stat)
2512{
2513 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
2514 struct drm_dp_mst_port *port;
2515 int old_ddps, ret;
2516 u8 new_pdt;
2517 bool_Bool new_mcs;
2518 bool_Bool dowork = false0, create_connector = false0;
2519
2520 port = drm_dp_get_port(mstb, conn_stat->port_number);
2521 if (!port)
2522 return;
2523
2524 if (port->connector) {
2525 if (!port->input && conn_stat->input_port) {
2526 /*
2527 * We can't remove a connector from an already exposed
2528 * port, so just throw the port out and make sure we
2529 * reprobe the link address of it's parent MSTB
2530 */
2531 drm_dp_mst_topology_unlink_port(mgr, port);
2532 mstb->link_address_sent = false0;
2533 dowork = true1;
2534 goto out;
2535 }
2536
2537 /* Locking is only needed if the port's exposed to userspace */
2538 drm_modeset_lock(&mgr->base.lock, NULL((void *)0));
2539 } else if (port->input && !conn_stat->input_port) {
2540 create_connector = true1;
2541 /* Reprobe link address so we get num_sdp_streams */
2542 mstb->link_address_sent = false0;
2543 dowork = true1;
2544 }
2545
2546 old_ddps = port->ddps;
2547 port->input = conn_stat->input_port;
2548 port->ldps = conn_stat->legacy_device_plug_status;
2549 port->ddps = conn_stat->displayport_device_plug_status;
2550
2551 if (old_ddps != port->ddps) {
2552 if (port->ddps && !port->input)
2553 drm_dp_send_enum_path_resources(mgr, mstb, port);
2554 else
2555 port->full_pbn = 0;
2556 }
2557
2558 new_pdt = port->input ? DP_PEER_DEVICE_NONE0x0 : conn_stat->peer_device_type;
2559 new_mcs = conn_stat->message_capability_status;
2560 ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
2561 if (ret == 1) {
2562 dowork = true1;
2563 } else if (ret < 0) {
2564 DRM_ERROR("Failed to change PDT for port %p: %d\n",__drm_err("Failed to change PDT for port %p: %d\n", port, ret
)
2565 port, ret)__drm_err("Failed to change PDT for port %p: %d\n", port, ret
)
;
2566 dowork = false0;
2567 }
2568
2569 if (port->connector)
2570 drm_modeset_unlock(&mgr->base.lock);
2571 else if (create_connector)
2572 drm_dp_mst_port_add_connector(mstb, port);
2573
2574out:
2575 drm_dp_mst_topology_put_port(port);
2576 if (dowork)
2577 queue_work(system_long_wq, &mstb->mgr->work);
2578}
2579
2580static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
2581 u8 lct, u8 *rad)
2582{
2583 struct drm_dp_mst_branch *mstb;
2584 struct drm_dp_mst_port *port;
2585 int i, ret;
2586 /* find the port by iterating down */
2587
2588 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
2589 mstb = mgr->mst_primary;
2590
2591 if (!mstb)
27
Assuming 'mstb' is non-null
28
Taking false branch
2592 goto out;
2593
2594 for (i = 0; i < lct - 1; i++) {
29
The value 0 is assigned to 'i'
30
Assuming the condition is true
31
Loop condition is true. Entering loop body
2595 int shift = (i % 2) ? 0 : 4;
32
'?' condition is false
2596 int port_num = (rad[i / 2] >> shift) & 0xf;
33
The left operand of '>>' is a garbage value
2597
2598 list_for_each_entry(port, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}); &port->next != (&mstb->ports); port = (
{ const __typeof( ((__typeof(*port) *)0)->next ) *__mptr =
(port->next.next); (__typeof(*port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*port), next) );}))
{
2599 if (port->port_num == port_num) {
2600 mstb = port->mstb;
2601 if (!mstb) {
2602 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0])__drm_err("failed to lookup MSTB with lct %d, rad %02x\n", lct
, rad[0])
;
2603 goto out;
2604 }
2605
2606 break;
2607 }
2608 }
2609 }
2610 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2611 if (!ret)
2612 mstb = NULL((void *)0);
2613out:
2614 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
2615 return mstb;
2616}
2617
2618static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
2619 struct drm_dp_mst_branch *mstb,
2620 const uint8_t *guid)
2621{
2622 struct drm_dp_mst_branch *found_mstb;
2623 struct drm_dp_mst_port *port;
2624
2625 if (memcmp(mstb->guid, guid, 16)__builtin_memcmp((mstb->guid), (guid), (16)) == 0)
2626 return mstb;
2627
2628
2629 list_for_each_entry(port, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}); &port->next != (&mstb->ports); port = (
{ const __typeof( ((__typeof(*port) *)0)->next ) *__mptr =
(port->next.next); (__typeof(*port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*port), next) );}))
{
2630 if (!port->mstb)
2631 continue;
2632
2633 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
2634
2635 if (found_mstb)
2636 return found_mstb;
2637 }
2638
2639 return NULL((void *)0);
2640}
2641
2642static struct drm_dp_mst_branch *
2643drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
2644 const uint8_t *guid)
2645{
2646 struct drm_dp_mst_branch *mstb;
2647 int ret;
2648
2649 /* find the port by iterating down */
2650 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
2651
2652 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
2653 if (mstb) {
2654 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2655 if (!ret)
2656 mstb = NULL((void *)0);
2657 }
2658
2659 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
2660 return mstb;
2661}
2662
2663static int drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2664 struct drm_dp_mst_branch *mstb)
2665{
2666 struct drm_dp_mst_port *port;
2667 int ret;
2668 bool_Bool changed = false0;
2669
2670 if (!mstb->link_address_sent) {
2671 ret = drm_dp_send_link_address(mgr, mstb);
2672 if (ret == 1)
2673 changed = true1;
2674 else if (ret < 0)
2675 return ret;
2676 }
2677
2678 list_for_each_entry(port, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}); &port->next != (&mstb->ports); port = (
{ const __typeof( ((__typeof(*port) *)0)->next ) *__mptr =
(port->next.next); (__typeof(*port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*port), next) );}))
{
2679 struct drm_dp_mst_branch *mstb_child = NULL((void *)0);
2680
2681 if (port->input || !port->ddps)
2682 continue;
2683
2684 if (port->mstb)
2685 mstb_child = drm_dp_mst_topology_get_mstb_validated(
2686 mgr, port->mstb);
2687
2688 if (mstb_child) {
2689 ret = drm_dp_check_and_send_link_address(mgr,
2690 mstb_child);
2691 drm_dp_mst_topology_put_mstb(mstb_child);
2692 if (ret == 1)
2693 changed = true1;
2694 else if (ret < 0)
2695 return ret;
2696 }
2697 }
2698
2699 return changed;
2700}
2701
2702static void drm_dp_mst_link_probe_work(struct work_struct *work)
2703{
2704 struct drm_dp_mst_topology_mgr *mgr =
2705 container_of(work, struct drm_dp_mst_topology_mgr, work)({ const __typeof( ((struct drm_dp_mst_topology_mgr *)0)->
work ) *__mptr = (work); (struct drm_dp_mst_topology_mgr *)( (
char *)__mptr - __builtin_offsetof(struct drm_dp_mst_topology_mgr
, work) );})
;
2706 struct drm_device *dev = mgr->dev;
2707 struct drm_dp_mst_branch *mstb;
2708 int ret;
2709 bool_Bool clear_payload_id_table;
2710
2711 mutex_lock(&mgr->probe_lock)rw_enter_write(&mgr->probe_lock);
2712
2713 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
2714 clear_payload_id_table = !mgr->payload_id_table_cleared;
2715 mgr->payload_id_table_cleared = true1;
2716
2717 mstb = mgr->mst_primary;
2718 if (mstb) {
2719 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2720 if (!ret)
2721 mstb = NULL((void *)0);
2722 }
2723 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
2724 if (!mstb) {
2725 mutex_unlock(&mgr->probe_lock)rw_exit_write(&mgr->probe_lock);
2726 return;
2727 }
2728
2729 /*
2730 * Certain branch devices seem to incorrectly report an available_pbn
2731 * of 0 on downstream sinks, even after clearing the
2732 * DP_PAYLOAD_ALLOCATE_* registers in
2733 * drm_dp_mst_topology_mgr_set_mst(). Namely, the CableMatters USB-C
2734 * 2x DP hub. Sending a CLEAR_PAYLOAD_ID_TABLE message seems to make
2735 * things work again.
2736 */
2737 if (clear_payload_id_table) {
2738 DRM_DEBUG_KMS("Clearing payload ID table\n")__drm_dbg(DRM_UT_KMS, "Clearing payload ID table\n");
2739 drm_dp_send_clear_payload_id_table(mgr, mstb);
2740 }
2741
2742 ret = drm_dp_check_and_send_link_address(mgr, mstb);
2743 drm_dp_mst_topology_put_mstb(mstb);
2744
2745 mutex_unlock(&mgr->probe_lock)rw_exit_write(&mgr->probe_lock);
2746 if (ret)
2747 drm_kms_helper_hotplug_event(dev);
2748}
2749
2750static bool_Bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
2751 u8 *guid)
2752{
2753 u64 salt;
2754
2755 if (memchr_inv(guid, 0, 16))
2756 return true1;
2757
2758 salt = get_jiffies_64()jiffies;
2759
2760 memcpy(&guid[0], &salt, sizeof(u64))__builtin_memcpy((&guid[0]), (&salt), (sizeof(u64)));
2761 memcpy(&guid[8], &salt, sizeof(u64))__builtin_memcpy((&guid[8]), (&salt), (sizeof(u64)));
2762
2763 return false0;
2764}
2765
2766static void build_dpcd_read(struct drm_dp_sideband_msg_tx *msg,
2767 u8 port_num, u32 offset, u8 num_bytes)
2768{
2769 struct drm_dp_sideband_msg_req_body req;
2770
2771 req.req_type = DP_REMOTE_DPCD_READ0x20;
2772 req.u.dpcd_read.port_number = port_num;
2773 req.u.dpcd_read.dpcd_address = offset;
2774 req.u.dpcd_read.num_bytes = num_bytes;
2775 drm_dp_encode_sideband_req(&req, msg);
2776}
2777
2778static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
2779 bool_Bool up, u8 *msg, int len)
2780{
2781 int ret;
2782 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE0x1200 : DP_SIDEBAND_MSG_DOWN_REQ_BASE0x1000;
2783 int tosend, total, offset;
2784 int retries = 0;
2785
2786retry:
2787 total = len;
2788 offset = 0;
2789 do {
2790 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total)(((mgr->max_dpcd_transaction_bytes)<((((16)<(total))
?(16):(total))))?(mgr->max_dpcd_transaction_bytes):((((16)
<(total))?(16):(total))))
;
2791
2792 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
2793 &msg[offset],
2794 tosend);
2795 if (ret != tosend) {
2796 if (ret == -EIO5 && retries < 5) {
2797 retries++;
2798 goto retry;
2799 }
2800 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret)__drm_dbg(DRM_UT_KMS, "failed to dpcd write %d %d\n", tosend,
ret)
;
2801
2802 return -EIO5;
2803 }
2804 offset += tosend;
2805 total -= tosend;
2806 } while (total > 0);
2807 return 0;
2808}
2809
2810static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
2811 struct drm_dp_sideband_msg_tx *txmsg)
2812{
2813 struct drm_dp_mst_branch *mstb = txmsg->dst;
2814 u8 req_type;
2815
2816 req_type = txmsg->msg[0] & 0x7f;
2817 if (req_type == DP_CONNECTION_STATUS_NOTIFY0x02 ||
2818 req_type == DP_RESOURCE_STATUS_NOTIFY0x13 ||
2819 req_type == DP_CLEAR_PAYLOAD_ID_TABLE0x14)
2820 hdr->broadcast = 1;
2821 else
2822 hdr->broadcast = 0;
2823 hdr->path_msg = txmsg->path_msg;
2824 if (hdr->broadcast) {
2825 hdr->lct = 1;
2826 hdr->lcr = 6;
2827 } else {
2828 hdr->lct = mstb->lct;
2829 hdr->lcr = mstb->lct - 1;
2830 }
2831
2832 memcpy(hdr->rad, mstb->rad, hdr->lct / 2)__builtin_memcpy((hdr->rad), (mstb->rad), (hdr->lct /
2))
;
2833
2834 return 0;
2835}
2836/*
2837 * process a single block of the next message in the sideband queue
2838 */
2839static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
2840 struct drm_dp_sideband_msg_tx *txmsg,
2841 bool_Bool up)
2842{
2843 u8 chunk[48];
2844 struct drm_dp_sideband_msg_hdr hdr;
2845 int len, space, idx, tosend;
2846 int ret;
2847
2848 if (txmsg->state == DRM_DP_SIDEBAND_TX_SENT2)
2849 return 0;
2850
2851 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr))__builtin_memset((&hdr), (0), (sizeof(struct drm_dp_sideband_msg_hdr
)))
;
2852
2853 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED0)
2854 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND1;
2855
2856 /* make hdr from dst mst */
2857 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
2858 if (ret < 0)
2859 return ret;
2860
2861 /* amount left to send in this message */
2862 len = txmsg->cur_len - txmsg->cur_offset;
2863
2864 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
2865 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
2866
2867 tosend = min(len, space)(((len)<(space))?(len):(space));
2868 if (len == txmsg->cur_len)
2869 hdr.somt = 1;
2870 if (space >= len)
2871 hdr.eomt = 1;
2872
2873
2874 hdr.msg_len = tosend + 1;
2875 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
2876 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend)__builtin_memcpy((&chunk[idx]), (&txmsg->msg[txmsg
->cur_offset]), (tosend))
;
2877 /* add crc at end */
2878 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
2879 idx += tosend + 1;
2880
2881 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
2882 if (ret) {
2883 if (drm_debug_enabled(DRM_UT_DP)) {
2884 struct drm_printer p = drm_debug_printer(DBG_PREFIX"[dp_mst]");
2885
2886 drm_printf(&p, "sideband msg failed to send\n");
2887 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
2888 }
2889 return ret;
2890 }
2891
2892 txmsg->cur_offset += tosend;
2893 if (txmsg->cur_offset == txmsg->cur_len) {
2894 txmsg->state = DRM_DP_SIDEBAND_TX_SENT2;
2895 return 1;
2896 }
2897 return 0;
2898}
2899
2900static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
2901{
2902 struct drm_dp_sideband_msg_tx *txmsg;
2903 int ret;
2904
2905 WARN_ON(!mutex_is_locked(&mgr->qlock))({ int __ret = !!(!(rw_status(&mgr->qlock) != 0)); if (
__ret) printf("WARNING %s failed at %s:%d\n", "!(rw_status(&mgr->qlock) != 0)"
, "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c", 2905); __builtin_expect
(!!(__ret), 0); })
;
2906
2907 /* construct a chunk from the first msg in the tx_msg queue */
2908 if (list_empty(&mgr->tx_msg_downq))
2909 return;
2910
2911 txmsg = list_first_entry(&mgr->tx_msg_downq,({ const __typeof( ((struct drm_dp_sideband_msg_tx *)0)->next
) *__mptr = ((&mgr->tx_msg_downq)->next); (struct drm_dp_sideband_msg_tx
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_sideband_msg_tx
, next) );})
2912 struct drm_dp_sideband_msg_tx, next)({ const __typeof( ((struct drm_dp_sideband_msg_tx *)0)->next
) *__mptr = ((&mgr->tx_msg_downq)->next); (struct drm_dp_sideband_msg_tx
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_sideband_msg_tx
, next) );})
;
2913 ret = process_single_tx_qlock(mgr, txmsg, false0);
2914 if (ret < 0) {
2915 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret)__drm_dbg(DRM_UT_KMS, "failed to send msg in q %d\n", ret);
2916 list_del(&txmsg->next);
2917 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT4;
2918 wake_up_all(&mgr->tx_waitq)wake_up(&mgr->tx_waitq);
2919 }
2920}
2921
2922static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
2923 struct drm_dp_sideband_msg_tx *txmsg)
2924{
2925 mutex_lock(&mgr->qlock)rw_enter_write(&mgr->qlock);
2926 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
2927
2928 if (drm_debug_enabled(DRM_UT_DP)) {
2929 struct drm_printer p = drm_debug_printer(DBG_PREFIX"[dp_mst]");
2930
2931 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
2932 }
2933
2934 if (list_is_singular(&mgr->tx_msg_downq))
2935 process_single_down_tx_qlock(mgr);
2936 mutex_unlock(&mgr->qlock)rw_exit_write(&mgr->qlock);
2937}
2938
2939static void
2940drm_dp_dump_link_address(struct drm_dp_link_address_ack_reply *reply)
2941{
2942 struct drm_dp_link_addr_reply_port *port_reply;
2943 int i;
2944
2945 for (i = 0; i < reply->nports; i++) {
2946 port_reply = &reply->ports[i];
2947 DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n",__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2948 i,__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2949 port_reply->input_port,__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2950 port_reply->peer_device_type,__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2951 port_reply->port_number,__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2952 port_reply->dpcd_revision,__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2953 port_reply->mcs,__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2954 port_reply->ddps,__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2955 port_reply->legacy_device_plug_status,__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2956 port_reply->num_sdp_streams,__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
2957 port_reply->num_sdp_stream_sinks)__drm_dbg(DRM_UT_KMS, "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n"
, i, port_reply->input_port, port_reply->peer_device_type
, port_reply->port_number, port_reply->dpcd_revision, port_reply
->mcs, port_reply->ddps, port_reply->legacy_device_plug_status
, port_reply->num_sdp_streams, port_reply->num_sdp_stream_sinks
)
;
2958 }
2959}
2960
2961static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2962 struct drm_dp_mst_branch *mstb)
2963{
2964 struct drm_dp_sideband_msg_tx *txmsg;
2965 struct drm_dp_link_address_ack_reply *reply;
2966 struct drm_dp_mst_port *port, *tmp;
2967 int i, ret, port_mask = 0;
2968 bool_Bool changed = false0;
2969
2970 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
2971 if (!txmsg)
2972 return -ENOMEM12;
2973
2974 txmsg->dst = mstb;
2975 build_link_address(txmsg);
2976
2977 mstb->link_address_sent = true1;
2978 drm_dp_queue_down_tx(mgr, txmsg);
2979
2980 /* FIXME: Actually do some real error handling here */
2981 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2982 if (ret <= 0) {
2983 DRM_ERROR("Sending link address failed with %d\n", ret)__drm_err("Sending link address failed with %d\n", ret);
2984 goto out;
2985 }
2986 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01) {
2987 DRM_ERROR("link address NAK received\n")__drm_err("link address NAK received\n");
2988 ret = -EIO5;
2989 goto out;
2990 }
2991
2992 reply = &txmsg->reply.u.link_addr;
2993 DRM_DEBUG_KMS("link address reply: %d\n", reply->nports)__drm_dbg(DRM_UT_KMS, "link address reply: %d\n", reply->nports
)
;
2994 drm_dp_dump_link_address(reply);
2995
2996 ret = drm_dp_check_mstb_guid(mstb, reply->guid);
2997 if (ret) {
2998 char buf[64];
2999
3000 drm_dp_mst_rad_to_str(mstb->rad, mstb->lct, buf, sizeof(buf));
3001 DRM_ERROR("GUID check on %s failed: %d\n",__drm_err("GUID check on %s failed: %d\n", buf, ret)
3002 buf, ret)__drm_err("GUID check on %s failed: %d\n", buf, ret);
3003 goto out;
3004 }
3005
3006 for (i = 0; i < reply->nports; i++) {
3007 port_mask |= BIT(reply->ports[i].port_number)(1UL << (reply->ports[i].port_number));
3008 ret = drm_dp_mst_handle_link_address_port(mstb, mgr->dev,
3009 &reply->ports[i]);
3010 if (ret == 1)
3011 changed = true1;
3012 else if (ret < 0)
3013 goto out;
3014 }
3015
3016 /* Prune any ports that are currently a part of mstb in our in-memory
3017 * topology, but were not seen in this link address. Usually this
3018 * means that they were removed while the topology was out of sync,
3019 * e.g. during suspend/resume
3020 */
3021 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
3022 list_for_each_entry_safe(port, tmp, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}), tmp = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = (port->next.next); (__typeof(*port) *)( (char
*)__mptr - __builtin_offsetof(__typeof(*port), next) );}); &
port->next != (&mstb->ports); port = tmp, tmp = ({ const
__typeof( ((__typeof(*tmp) *)0)->next ) *__mptr = (tmp->
next.next); (__typeof(*tmp) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*tmp), next) );}))
{
3023 if (port_mask & BIT(port->port_num)(1UL << (port->port_num)))
3024 continue;
3025
3026 DRM_DEBUG_KMS("port %d was not in link address, removing\n",__drm_dbg(DRM_UT_KMS, "port %d was not in link address, removing\n"
, port->port_num)
3027 port->port_num)__drm_dbg(DRM_UT_KMS, "port %d was not in link address, removing\n"
, port->port_num)
;
3028 list_del(&port->next);
3029 drm_dp_mst_topology_put_port(port);
3030 changed = true1;
3031 }
3032 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
3033
3034out:
3035 if (ret <= 0)
3036 mstb->link_address_sent = false0;
3037 kfree(txmsg);
3038 return ret < 0 ? ret : changed;
3039}
3040
3041static void
3042drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
3043 struct drm_dp_mst_branch *mstb)
3044{
3045 struct drm_dp_sideband_msg_tx *txmsg;
3046 int ret;
3047
3048 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
3049 if (!txmsg)
3050 return;
3051
3052 txmsg->dst = mstb;
3053 build_clear_payload_id_table(txmsg);
3054
3055 drm_dp_queue_down_tx(mgr, txmsg);
3056
3057 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3058 if (ret > 0 && txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01)
3059 DRM_DEBUG_KMS("clear payload table id nak received\n")__drm_dbg(DRM_UT_KMS, "clear payload table id nak received\n"
)
;
3060
3061 kfree(txmsg);
3062}
3063
3064static int
3065drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
3066 struct drm_dp_mst_branch *mstb,
3067 struct drm_dp_mst_port *port)
3068{
3069 struct drm_dp_enum_path_resources_ack_reply *path_res;
3070 struct drm_dp_sideband_msg_tx *txmsg;
3071 int ret;
3072
3073 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
3074 if (!txmsg)
3075 return -ENOMEM12;
3076
3077 txmsg->dst = mstb;
3078 build_enum_path_resources(txmsg, port->port_num);
3079
3080 drm_dp_queue_down_tx(mgr, txmsg);
3081
3082 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3083 if (ret > 0) {
3084 ret = 0;
3085 path_res = &txmsg->reply.u.path_resources;
3086
3087 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01) {
3088 DRM_DEBUG_KMS("enum path resources nak received\n")__drm_dbg(DRM_UT_KMS, "enum path resources nak received\n");
3089 } else {
3090 if (port->port_num != path_res->port_number)
3091 DRM_ERROR("got incorrect port in response\n")__drm_err("got incorrect port in response\n");
3092
3093 DRM_DEBUG_KMS("enum path resources %d: %d %d\n",__drm_dbg(DRM_UT_KMS, "enum path resources %d: %d %d\n", path_res
->port_number, path_res->full_payload_bw_number, path_res
->avail_payload_bw_number)
3094 path_res->port_number,__drm_dbg(DRM_UT_KMS, "enum path resources %d: %d %d\n", path_res
->port_number, path_res->full_payload_bw_number, path_res
->avail_payload_bw_number)
3095 path_res->full_payload_bw_number,__drm_dbg(DRM_UT_KMS, "enum path resources %d: %d %d\n", path_res
->port_number, path_res->full_payload_bw_number, path_res
->avail_payload_bw_number)
3096 path_res->avail_payload_bw_number)__drm_dbg(DRM_UT_KMS, "enum path resources %d: %d %d\n", path_res
->port_number, path_res->full_payload_bw_number, path_res
->avail_payload_bw_number)
;
3097
3098 /*
3099 * If something changed, make sure we send a
3100 * hotplug
3101 */
3102 if (port->full_pbn != path_res->full_payload_bw_number ||
3103 port->fec_capable != path_res->fec_capable)
3104 ret = 1;
3105
3106 port->full_pbn = path_res->full_payload_bw_number;
3107 port->fec_capable = path_res->fec_capable;
3108 }
3109 }
3110
3111 kfree(txmsg);
3112 return ret;
3113}
3114
3115static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
3116{
3117 if (!mstb->port_parent)
3118 return NULL((void *)0);
3119
3120 if (mstb->port_parent->mstb != mstb)
3121 return mstb->port_parent;
3122
3123 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
3124}
3125
3126/*
3127 * Searches upwards in the topology starting from mstb to try to find the
3128 * closest available parent of mstb that's still connected to the rest of the
3129 * topology. This can be used in order to perform operations like releasing
3130 * payloads, where the branch device which owned the payload may no longer be
3131 * around and thus would require that the payload on the last living relative
3132 * be freed instead.
3133 */
3134static struct drm_dp_mst_branch *
3135drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
3136 struct drm_dp_mst_branch *mstb,
3137 int *port_num)
3138{
3139 struct drm_dp_mst_branch *rmstb = NULL((void *)0);
3140 struct drm_dp_mst_port *found_port;
3141
3142 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
3143 if (!mgr->mst_primary)
3144 goto out;
3145
3146 do {
3147 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
3148 if (!found_port)
3149 break;
3150
3151 if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
3152 rmstb = found_port->parent;
3153 *port_num = found_port->port_num;
3154 } else {
3155 /* Search again, starting from this parent */
3156 mstb = found_port->parent;
3157 }
3158 } while (!rmstb);
3159out:
3160 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
3161 return rmstb;
3162}
3163
3164static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
3165 struct drm_dp_mst_port *port,
3166 int id,
3167 int pbn)
3168{
3169 struct drm_dp_sideband_msg_tx *txmsg;
3170 struct drm_dp_mst_branch *mstb;
3171 int ret, port_num;
3172 u8 sinks[DRM_DP_MAX_SDP_STREAMS16];
3173 int i;
3174
3175 port_num = port->port_num;
3176 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
3177 if (!mstb) {
3178 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
3179 port->parent,
3180 &port_num);
3181
3182 if (!mstb)
3183 return -EINVAL22;
3184 }
3185
3186 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
3187 if (!txmsg) {
3188 ret = -ENOMEM12;
3189 goto fail_put;
3190 }
3191
3192 for (i = 0; i < port->num_sdp_streams; i++)
3193 sinks[i] = i;
3194
3195 txmsg->dst = mstb;
3196 build_allocate_payload(txmsg, port_num,
3197 id,
3198 pbn, port->num_sdp_streams, sinks);
3199
3200 drm_dp_queue_down_tx(mgr, txmsg);
3201
3202 /*
3203 * FIXME: there is a small chance that between getting the last
3204 * connected mstb and sending the payload message, the last connected
3205 * mstb could also be removed from the topology. In the future, this
3206 * needs to be fixed by restarting the
3207 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
3208 * timeout if the topology is still connected to the system.
3209 */
3210 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3211 if (ret > 0) {
3212 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01)
3213 ret = -EINVAL22;
3214 else
3215 ret = 0;
3216 }
3217 kfree(txmsg);
3218fail_put:
3219 drm_dp_mst_topology_put_mstb(mstb);
3220 return ret;
3221}
3222
3223int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
3224 struct drm_dp_mst_port *port, bool_Bool power_up)
3225{
3226 struct drm_dp_sideband_msg_tx *txmsg;
3227 int ret;
3228
3229 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3230 if (!port)
3231 return -EINVAL22;
3232
3233 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
3234 if (!txmsg) {
3235 drm_dp_mst_topology_put_port(port);
3236 return -ENOMEM12;
3237 }
3238
3239 txmsg->dst = port->parent;
3240 build_power_updown_phy(txmsg, port->port_num, power_up);
3241 drm_dp_queue_down_tx(mgr, txmsg);
3242
3243 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
3244 if (ret > 0) {
3245 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01)
3246 ret = -EINVAL22;
3247 else
3248 ret = 0;
3249 }
3250 kfree(txmsg);
3251 drm_dp_mst_topology_put_port(port);
3252
3253 return ret;
3254}
3255EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
3256
3257int drm_dp_send_query_stream_enc_status(struct drm_dp_mst_topology_mgr *mgr,
3258 struct drm_dp_mst_port *port,
3259 struct drm_dp_query_stream_enc_status_ack_reply *status)
3260{
3261 struct drm_dp_sideband_msg_tx *txmsg;
3262 u8 nonce[7];
3263 int len, ret;
3264
3265 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
3266 if (!txmsg)
3267 return -ENOMEM12;
3268
3269 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3270 if (!port) {
3271 ret = -EINVAL22;
3272 goto out_get_port;
3273 }
3274
3275 get_random_bytes(nonce, sizeof(nonce));
3276
3277 /*
3278 * "Source device targets the QUERY_STREAM_ENCRYPTION_STATUS message
3279 * transaction at the MST Branch device directly connected to the
3280 * Source"
3281 */
3282 txmsg->dst = mgr->mst_primary;
3283
3284 len = build_query_stream_enc_status(txmsg, port->vcpi.vcpi, nonce);
3285
3286 drm_dp_queue_down_tx(mgr, txmsg);
3287
3288 ret = drm_dp_mst_wait_tx_reply(mgr->mst_primary, txmsg);
3289 if (ret < 0) {
3290 goto out;
3291 } else if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01) {
3292 drm_dbg_kms(mgr->dev, "query encryption status nak received\n")drm_dev_dbg((mgr->dev)->dev, DRM_UT_KMS, "query encryption status nak received\n"
)
;
3293 ret = -ENXIO6;
3294 goto out;
3295 }
3296
3297 ret = 0;
3298 memcpy(status, &txmsg->reply.u.enc_status, sizeof(*status))__builtin_memcpy((status), (&txmsg->reply.u.enc_status
), (sizeof(*status)))
;
3299
3300out:
3301 drm_dp_mst_topology_put_port(port);
3302out_get_port:
3303 kfree(txmsg);
3304 return ret;
3305}
3306EXPORT_SYMBOL(drm_dp_send_query_stream_enc_status);
3307
3308static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
3309 int id,
3310 struct drm_dp_payload *payload)
3311{
3312 int ret;
3313
3314 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
3315 if (ret < 0) {
3316 payload->payload_state = 0;
3317 return ret;
3318 }
3319 payload->payload_state = DP_PAYLOAD_LOCAL1;
3320 return 0;
3321}
3322
3323static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
3324 struct drm_dp_mst_port *port,
3325 int id,
3326 struct drm_dp_payload *payload)
3327{
3328 int ret;
3329
3330 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
3331 if (ret < 0)
3332 return ret;
3333 payload->payload_state = DP_PAYLOAD_REMOTE2;
3334 return ret;
3335}
3336
3337static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
3338 struct drm_dp_mst_port *port,
3339 int id,
3340 struct drm_dp_payload *payload)
3341{
3342 DRM_DEBUG_KMS("\n")__drm_dbg(DRM_UT_KMS, "\n");
3343 /* it's okay for these to fail */
3344 if (port) {
3345 drm_dp_payload_send_msg(mgr, port, id, 0);
3346 }
3347
3348 drm_dp_dpcd_write_payload(mgr, id, payload);
3349 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL3;
3350 return 0;
3351}
3352
3353static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
3354 int id,
3355 struct drm_dp_payload *payload)
3356{
3357 payload->payload_state = 0;
3358 return 0;
3359}
3360
3361/**
3362 * drm_dp_update_payload_part1() - Execute payload update part 1
3363 * @mgr: manager to use.
3364 *
3365 * This iterates over all proposed virtual channels, and tries to
3366 * allocate space in the link for them. For 0->slots transitions,
3367 * this step just writes the VCPI to the MST device. For slots->0
3368 * transitions, this writes the updated VCPIs and removes the
3369 * remote VC payloads.
3370 *
3371 * after calling this the driver should generate ACT and payload
3372 * packets.
3373 */
3374int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
3375{
3376 struct drm_dp_payload req_payload;
3377 struct drm_dp_mst_port *port;
3378 int i, j;
3379 int cur_slots = 1;
3380 bool_Bool skip;
3381
3382 mutex_lock(&mgr->payload_lock)rw_enter_write(&mgr->payload_lock);
3383 for (i = 0; i < mgr->max_payloads; i++) {
3384 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
3385 struct drm_dp_payload *payload = &mgr->payloads[i];
3386 bool_Bool put_port = false0;
3387
3388 /* solve the current payloads - compare to the hw ones
3389 - update the hw view */
3390 req_payload.start_slot = cur_slots;
3391 if (vcpi) {
3392 port = container_of(vcpi, struct drm_dp_mst_port,({ const __typeof( ((struct drm_dp_mst_port *)0)->vcpi ) *
__mptr = (vcpi); (struct drm_dp_mst_port *)( (char *)__mptr -
__builtin_offsetof(struct drm_dp_mst_port, vcpi) );})
3393 vcpi)({ const __typeof( ((struct drm_dp_mst_port *)0)->vcpi ) *
__mptr = (vcpi); (struct drm_dp_mst_port *)( (char *)__mptr -
__builtin_offsetof(struct drm_dp_mst_port, vcpi) );})
;
3394
3395 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
3396 skip = !drm_dp_mst_port_downstream_of_branch(port, mgr->mst_primary);
3397 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
3398
3399 if (skip) {
3400 drm_dbg_kms(mgr->dev,drm_dev_dbg((mgr->dev)->dev, DRM_UT_KMS, "Virtual channel %d is not in current topology\n"
, i)
3401 "Virtual channel %d is not in current topology\n",drm_dev_dbg((mgr->dev)->dev, DRM_UT_KMS, "Virtual channel %d is not in current topology\n"
, i)
3402 i)drm_dev_dbg((mgr->dev)->dev, DRM_UT_KMS, "Virtual channel %d is not in current topology\n"
, i)
;
3403 continue;
3404 }
3405 /* Validated ports don't matter if we're releasing
3406 * VCPI
3407 */
3408 if (vcpi->num_slots) {
3409 port = drm_dp_mst_topology_get_port_validated(
3410 mgr, port);
3411 if (!port) {
3412 if (vcpi->num_slots == payload->num_slots) {
3413 cur_slots += vcpi->num_slots;
3414 payload->start_slot = req_payload.start_slot;
3415 continue;
3416 } else {
3417 drm_dbg_kms(mgr->dev,drm_dev_dbg((mgr->dev)->dev, DRM_UT_KMS, "Fail:set payload to invalid sink"
)
3418 "Fail:set payload to invalid sink")drm_dev_dbg((mgr->dev)->dev, DRM_UT_KMS, "Fail:set payload to invalid sink"
)
;
3419 mutex_unlock(&mgr->payload_lock)rw_exit_write(&mgr->payload_lock);
3420 return -EINVAL22;
3421 }
3422 }
3423 put_port = true1;
3424 }
3425
3426 req_payload.num_slots = vcpi->num_slots;
3427 req_payload.vcpi = vcpi->vcpi;
3428 } else {
3429 port = NULL((void *)0);
3430 req_payload.num_slots = 0;
3431 }
3432
3433 payload->start_slot = req_payload.start_slot;
3434 /* work out what is required to happen with this payload */
3435 if (payload->num_slots != req_payload.num_slots) {
3436
3437 /* need to push an update for this payload */
3438 if (req_payload.num_slots) {
3439 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
3440 &req_payload);
3441 payload->num_slots = req_payload.num_slots;
3442 payload->vcpi = req_payload.vcpi;
3443
3444 } else if (payload->num_slots) {
3445 payload->num_slots = 0;
3446 drm_dp_destroy_payload_step1(mgr, port,
3447 payload->vcpi,
3448 payload);
3449 req_payload.payload_state =
3450 payload->payload_state;
3451 payload->start_slot = 0;
3452 }
3453 payload->payload_state = req_payload.payload_state;
3454 }
3455 cur_slots += req_payload.num_slots;
3456
3457 if (put_port)
3458 drm_dp_mst_topology_put_port(port);
3459 }
3460
3461 for (i = 0; i < mgr->max_payloads; /* do nothing */) {
3462 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL3) {
3463 i++;
3464 continue;
3465 }
3466
3467 DRM_DEBUG_KMS("removing payload %d\n", i)__drm_dbg(DRM_UT_KMS, "removing payload %d\n", i);
3468 for (j = i; j < mgr->max_payloads - 1; j++) {
3469 mgr->payloads[j] = mgr->payloads[j + 1];
3470 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
3471
3472 if (mgr->proposed_vcpis[j] &&
3473 mgr->proposed_vcpis[j]->num_slots) {
3474 set_bit(j + 1, &mgr->payload_mask);
3475 } else {
3476 clear_bit(j + 1, &mgr->payload_mask);
3477 }
3478 }
3479
3480 memset(&mgr->payloads[mgr->max_payloads - 1], 0,__builtin_memset((&mgr->payloads[mgr->max_payloads -
1]), (0), (sizeof(struct drm_dp_payload)))
3481 sizeof(struct drm_dp_payload))__builtin_memset((&mgr->payloads[mgr->max_payloads -
1]), (0), (sizeof(struct drm_dp_payload)))
;
3482 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL((void *)0);
3483 clear_bit(mgr->max_payloads, &mgr->payload_mask);
3484 }
3485 mutex_unlock(&mgr->payload_lock)rw_exit_write(&mgr->payload_lock);
3486
3487 return 0;
3488}
3489EXPORT_SYMBOL(drm_dp_update_payload_part1);
3490
3491/**
3492 * drm_dp_update_payload_part2() - Execute payload update part 2
3493 * @mgr: manager to use.
3494 *
3495 * This iterates over all proposed virtual channels, and tries to
3496 * allocate space in the link for them. For 0->slots transitions,
3497 * this step writes the remote VC payload commands. For slots->0
3498 * this just resets some internal state.
3499 */
3500int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
3501{
3502 struct drm_dp_mst_port *port;
3503 int i;
3504 int ret = 0;
3505 bool_Bool skip;
3506
3507 mutex_lock(&mgr->payload_lock)rw_enter_write(&mgr->payload_lock);
3508 for (i = 0; i < mgr->max_payloads; i++) {
3509
3510 if (!mgr->proposed_vcpis[i])
3511 continue;
3512
3513 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi)({ const __typeof( ((struct drm_dp_mst_port *)0)->vcpi ) *
__mptr = (mgr->proposed_vcpis[i]); (struct drm_dp_mst_port
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_port
, vcpi) );})
;
3514
3515 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
3516 skip = !drm_dp_mst_port_downstream_of_branch(port, mgr->mst_primary);
3517 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
3518
3519 if (skip)
3520 continue;
3521
3522 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state)__drm_dbg(DRM_UT_KMS, "payload %d %d\n", i, mgr->payloads[
i].payload_state)
;
3523 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL1) {
3524 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
3525 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL3) {
3526 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
3527 }
3528 if (ret) {
3529 mutex_unlock(&mgr->payload_lock)rw_exit_write(&mgr->payload_lock);
3530 return ret;
3531 }
3532 }
3533 mutex_unlock(&mgr->payload_lock)rw_exit_write(&mgr->payload_lock);
3534 return 0;
3535}
3536EXPORT_SYMBOL(drm_dp_update_payload_part2);
3537
3538static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
3539 struct drm_dp_mst_port *port,
3540 int offset, int size, u8 *bytes)
3541{
3542 int ret = 0;
3543 struct drm_dp_sideband_msg_tx *txmsg;
3544 struct drm_dp_mst_branch *mstb;
3545
3546 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
3547 if (!mstb)
3548 return -EINVAL22;
3549
3550 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
3551 if (!txmsg) {
3552 ret = -ENOMEM12;
3553 goto fail_put;
3554 }
3555
3556 build_dpcd_read(txmsg, port->port_num, offset, size);
3557 txmsg->dst = port->parent;
3558
3559 drm_dp_queue_down_tx(mgr, txmsg);
3560
3561 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3562 if (ret < 0)
3563 goto fail_free;
3564
3565 /* DPCD read should never be NACKed */
3566 if (txmsg->reply.reply_type == 1) {
3567 DRM_ERROR("mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n",__drm_err("mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n"
, mstb, port->port_num, offset, size)
3568 mstb, port->port_num, offset, size)__drm_err("mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n"
, mstb, port->port_num, offset, size)
;
3569 ret = -EIO5;
3570 goto fail_free;
3571 }
3572
3573 if (txmsg->reply.u.remote_dpcd_read_ack.num_bytes != size) {
3574 ret = -EPROTO95;
3575 goto fail_free;
3576 }
3577
3578 ret = min_t(size_t, txmsg->reply.u.remote_dpcd_read_ack.num_bytes,({ size_t __min_a = (txmsg->reply.u.remote_dpcd_read_ack.num_bytes
); size_t __min_b = (size); __min_a < __min_b ? __min_a : __min_b
; })
3579 size)({ size_t __min_a = (txmsg->reply.u.remote_dpcd_read_ack.num_bytes
); size_t __min_b = (size); __min_a < __min_b ? __min_a : __min_b
; })
;
3580 memcpy(bytes, txmsg->reply.u.remote_dpcd_read_ack.bytes, ret)__builtin_memcpy((bytes), (txmsg->reply.u.remote_dpcd_read_ack
.bytes), (ret))
;
3581
3582fail_free:
3583 kfree(txmsg);
3584fail_put:
3585 drm_dp_mst_topology_put_mstb(mstb);
3586
3587 return ret;
3588}
3589
3590static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
3591 struct drm_dp_mst_port *port,
3592 int offset, int size, u8 *bytes)
3593{
3594 int ret;
3595 struct drm_dp_sideband_msg_tx *txmsg;
3596 struct drm_dp_mst_branch *mstb;
3597
3598 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
3599 if (!mstb)
3600 return -EINVAL22;
3601
3602 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
3603 if (!txmsg) {
3604 ret = -ENOMEM12;
3605 goto fail_put;
3606 }
3607
3608 build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
3609 txmsg->dst = mstb;
3610
3611 drm_dp_queue_down_tx(mgr, txmsg);
3612
3613 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3614 if (ret > 0) {
3615 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01)
3616 ret = -EIO5;
3617 else
3618 ret = size;
3619 }
3620
3621 kfree(txmsg);
3622fail_put:
3623 drm_dp_mst_topology_put_mstb(mstb);
3624 return ret;
3625}
3626
3627static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
3628{
3629 struct drm_dp_sideband_msg_reply_body reply;
3630
3631 reply.reply_type = DP_SIDEBAND_REPLY_ACK0x00;
3632 reply.req_type = req_type;
3633 drm_dp_encode_sideband_reply(&reply, msg);
3634 return 0;
3635}
3636
3637static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
3638 struct drm_dp_mst_branch *mstb,
3639 int req_type, bool_Bool broadcast)
3640{
3641 struct drm_dp_sideband_msg_tx *txmsg;
3642
3643 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
3644 if (!txmsg)
3645 return -ENOMEM12;
3646
3647 txmsg->dst = mstb;
3648 drm_dp_encode_up_ack_reply(txmsg, req_type);
3649
3650 mutex_lock(&mgr->qlock)rw_enter_write(&mgr->qlock);
3651 /* construct a chunk from the first msg in the tx_msg queue */
3652 process_single_tx_qlock(mgr, txmsg, true1);
3653 mutex_unlock(&mgr->qlock)rw_exit_write(&mgr->qlock);
3654
3655 kfree(txmsg);
3656 return 0;
3657}
3658
3659/**
3660 * drm_dp_get_vc_payload_bw - get the VC payload BW for an MST link
3661 * @link_rate: link rate in 10kbits/s units
3662 * @link_lane_count: lane count
3663 *
3664 * Calculate the total bandwidth of a MultiStream Transport link. The returned
3665 * value is in units of PBNs/(timeslots/1 MTP). This value can be used to
3666 * convert the number of PBNs required for a given stream to the number of
3667 * timeslots this stream requires in each MTP.
3668 */
3669int drm_dp_get_vc_payload_bw(int link_rate, int link_lane_count)
3670{
3671 if (link_rate == 0 || link_lane_count == 0)
3672 DRM_DEBUG_KMS("invalid link rate/lane count: (%d / %d)\n",__drm_dbg(DRM_UT_KMS, "invalid link rate/lane count: (%d / %d)\n"
, link_rate, link_lane_count)
3673 link_rate, link_lane_count)__drm_dbg(DRM_UT_KMS, "invalid link rate/lane count: (%d / %d)\n"
, link_rate, link_lane_count)
;
3674
3675 /* See DP v2.0 2.6.4.2, VCPayload_Bandwidth_for_OneTimeSlotPer_MTP_Allocation */
3676 return link_rate * link_lane_count / 54000;
3677}
3678EXPORT_SYMBOL(drm_dp_get_vc_payload_bw);
3679
3680/**
3681 * drm_dp_read_mst_cap() - check whether or not a sink supports MST
3682 * @aux: The DP AUX channel to use
3683 * @dpcd: A cached copy of the DPCD capabilities for this sink
3684 *
3685 * Returns: %True if the sink supports MST, %false otherwise
3686 */
3687bool_Bool drm_dp_read_mst_cap(struct drm_dp_aux *aux,
3688 const u8 dpcd[DP_RECEIVER_CAP_SIZE0xf])
3689{
3690 u8 mstm_cap;
3691
3692 if (dpcd[DP_DPCD_REV0x000] < DP_DPCD_REV_120x12)
3693 return false0;
3694
3695 if (drm_dp_dpcd_readb(aux, DP_MSTM_CAP0x021, &mstm_cap) != 1)
3696 return false0;
3697
3698 return mstm_cap & DP_MST_CAP(1 << 0);
3699}
3700EXPORT_SYMBOL(drm_dp_read_mst_cap);
3701
3702/**
3703 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
3704 * @mgr: manager to set state for
3705 * @mst_state: true to enable MST on this connector - false to disable.
3706 *
3707 * This is called by the driver when it detects an MST capable device plugged
3708 * into a DP MST capable port, or when a DP MST capable device is unplugged.
3709 */
3710int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool_Bool mst_state)
3711{
3712 int ret = 0;
3713 struct drm_dp_mst_branch *mstb = NULL((void *)0);
3714
3715 mutex_lock(&mgr->payload_lock)rw_enter_write(&mgr->payload_lock);
3716 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
3717 if (mst_state == mgr->mst_state)
3718 goto out_unlock;
3719
3720 mgr->mst_state = mst_state;
3721 /* set the device into MST mode */
3722 if (mst_state) {
3723 struct drm_dp_payload reset_pay;
3724
3725 WARN_ON(mgr->mst_primary)({ int __ret = !!(mgr->mst_primary); if (__ret) printf("WARNING %s failed at %s:%d\n"
, "mgr->mst_primary", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 3725); __builtin_expect(!!(__ret), 0); })
;
3726
3727 /* get dpcd info */
3728 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV0x000, mgr->dpcd, DP_RECEIVER_CAP_SIZE0xf);
3729 if (ret != DP_RECEIVER_CAP_SIZE0xf) {
3730 DRM_DEBUG_KMS("failed to read DPCD\n")__drm_dbg(DRM_UT_KMS, "failed to read DPCD\n");
3731 goto out_unlock;
3732 }
3733
3734 mgr->pbn_div = drm_dp_get_vc_payload_bw(drm_dp_bw_code_to_link_rate(mgr->dpcd[1]),
3735 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK0x1f);
3736 if (mgr->pbn_div == 0) {
3737 ret = -EINVAL22;
3738 goto out_unlock;
3739 }
3740
3741 /* add initial branch device at LCT 1 */
3742 mstb = drm_dp_add_mst_branch_device(1, NULL((void *)0));
3743 if (mstb == NULL((void *)0)) {
3744 ret = -ENOMEM12;
3745 goto out_unlock;
3746 }
3747 mstb->mgr = mgr;
3748
3749 /* give this the main reference */
3750 mgr->mst_primary = mstb;
3751 drm_dp_mst_topology_get_mstb(mgr->mst_primary);
3752
3753 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL0x111,
3754 DP_MST_EN(1 << 0) |
3755 DP_UP_REQ_EN(1 << 1) |
3756 DP_UPSTREAM_IS_SRC(1 << 2));
3757 if (ret < 0)
3758 goto out_unlock;
3759
3760 reset_pay.start_slot = 0;
3761 reset_pay.num_slots = 0x3f;
3762 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
3763
3764 queue_work(system_long_wq, &mgr->work);
3765
3766 ret = 0;
3767 } else {
3768 /* disable MST on the device */
3769 mstb = mgr->mst_primary;
3770 mgr->mst_primary = NULL((void *)0);
3771 /* this can fail if the device is gone */
3772 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL0x111, 0);
3773 ret = 0;
3774 memset(mgr->payloads, 0,__builtin_memset((mgr->payloads), (0), (mgr->max_payloads
* sizeof(mgr->payloads[0])))
3775 mgr->max_payloads * sizeof(mgr->payloads[0]))__builtin_memset((mgr->payloads), (0), (mgr->max_payloads
* sizeof(mgr->payloads[0])))
;
3776 memset(mgr->proposed_vcpis, 0,__builtin_memset((mgr->proposed_vcpis), (0), (mgr->max_payloads
* sizeof(mgr->proposed_vcpis[0])))
3777 mgr->max_payloads * sizeof(mgr->proposed_vcpis[0]))__builtin_memset((mgr->proposed_vcpis), (0), (mgr->max_payloads
* sizeof(mgr->proposed_vcpis[0])))
;
3778 mgr->payload_mask = 0;
3779 set_bit(0, &mgr->payload_mask);
3780 mgr->vcpi_mask = 0;
3781 mgr->payload_id_table_cleared = false0;
3782 }
3783
3784out_unlock:
3785 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
3786 mutex_unlock(&mgr->payload_lock)rw_exit_write(&mgr->payload_lock);
3787 if (mstb)
3788 drm_dp_mst_topology_put_mstb(mstb);
3789 return ret;
3790
3791}
3792EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
3793
3794static void
3795drm_dp_mst_topology_mgr_invalidate_mstb(struct drm_dp_mst_branch *mstb)
3796{
3797 struct drm_dp_mst_port *port;
3798
3799 /* The link address will need to be re-sent on resume */
3800 mstb->link_address_sent = false0;
3801
3802 list_for_each_entry(port, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}); &port->next != (&mstb->ports); port = (
{ const __typeof( ((__typeof(*port) *)0)->next ) *__mptr =
(port->next.next); (__typeof(*port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*port), next) );}))
3803 if (port->mstb)
3804 drm_dp_mst_topology_mgr_invalidate_mstb(port->mstb);
3805}
3806
3807/**
3808 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
3809 * @mgr: manager to suspend
3810 *
3811 * This function tells the MST device that we can't handle UP messages
3812 * anymore. This should stop it from sending any since we are suspended.
3813 */
3814void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
3815{
3816 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
3817 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL0x111,
3818 DP_MST_EN(1 << 0) | DP_UPSTREAM_IS_SRC(1 << 2));
3819 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
3820 flush_work(&mgr->up_req_work);
3821 flush_work(&mgr->work);
3822 flush_work(&mgr->delayed_destroy_work);
3823
3824 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
3825 if (mgr->mst_state && mgr->mst_primary)
3826 drm_dp_mst_topology_mgr_invalidate_mstb(mgr->mst_primary);
3827 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
3828}
3829EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
3830
3831/**
3832 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
3833 * @mgr: manager to resume
3834 * @sync: whether or not to perform topology reprobing synchronously
3835 *
3836 * This will fetch DPCD and see if the device is still there,
3837 * if it is, it will rewrite the MSTM control bits, and return.
3838 *
3839 * If the device fails this returns -1, and the driver should do
3840 * a full MST reprobe, in case we were undocked.
3841 *
3842 * During system resume (where it is assumed that the driver will be calling
3843 * drm_atomic_helper_resume()) this function should be called beforehand with
3844 * @sync set to true. In contexts like runtime resume where the driver is not
3845 * expected to be calling drm_atomic_helper_resume(), this function should be
3846 * called with @sync set to false in order to avoid deadlocking.
3847 *
3848 * Returns: -1 if the MST topology was removed while we were suspended, 0
3849 * otherwise.
3850 */
3851int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr,
3852 bool_Bool sync)
3853{
3854 int ret;
3855 u8 guid[16];
3856
3857 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
3858 if (!mgr->mst_primary)
3859 goto out_fail;
3860
3861 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV0x000, mgr->dpcd,
3862 DP_RECEIVER_CAP_SIZE0xf);
3863 if (ret != DP_RECEIVER_CAP_SIZE0xf) {
3864 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n")__drm_dbg(DRM_UT_KMS, "dpcd read failed - undocked during suspend?\n"
)
;
3865 goto out_fail;
3866 }
3867
3868 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL0x111,
3869 DP_MST_EN(1 << 0) |
3870 DP_UP_REQ_EN(1 << 1) |
3871 DP_UPSTREAM_IS_SRC(1 << 2));
3872 if (ret < 0) {
3873 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n")__drm_dbg(DRM_UT_KMS, "mst write failed - undocked during suspend?\n"
)
;
3874 goto out_fail;
3875 }
3876
3877 /* Some hubs forget their guids after they resume */
3878 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID0x030, guid, 16);
3879 if (ret != 16) {
3880 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n")__drm_dbg(DRM_UT_KMS, "dpcd read failed - undocked during suspend?\n"
)
;
3881 goto out_fail;
3882 }
3883
3884 ret = drm_dp_check_mstb_guid(mgr->mst_primary, guid);
3885 if (ret) {
3886 DRM_DEBUG_KMS("check mstb failed - undocked during suspend?\n")__drm_dbg(DRM_UT_KMS, "check mstb failed - undocked during suspend?\n"
)
;
3887 goto out_fail;
3888 }
3889
3890 /*
3891 * For the final step of resuming the topology, we need to bring the
3892 * state of our in-memory topology back into sync with reality. So,
3893 * restart the probing process as if we're probing a new hub
3894 */
3895 queue_work(system_long_wq, &mgr->work);
3896 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
3897
3898 if (sync) {
3899 DRM_DEBUG_KMS("Waiting for link probe work to finish re-syncing topology...\n")__drm_dbg(DRM_UT_KMS, "Waiting for link probe work to finish re-syncing topology...\n"
)
;
3900 flush_work(&mgr->work);
3901 }
3902
3903 return 0;
3904
3905out_fail:
3906 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
3907 return -1;
3908}
3909EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
3910
3911static bool_Bool
3912drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool_Bool up,
3913 struct drm_dp_mst_branch **mstb)
3914{
3915 int len;
3916 u8 replyblock[32];
3917 int replylen, curreply;
3918 int ret;
3919 u8 hdrlen;
3920 struct drm_dp_sideband_msg_hdr hdr;
3921 struct drm_dp_sideband_msg_rx *msg =
3922 up
6.1
'up' is false
? &mgr->up_req_recv : &mgr->down_rep_recv;
7
'?' condition is false
3923 int basereg = up
7.1
'up' is false
? DP_SIDEBAND_MSG_UP_REQ_BASE0x1600 :
8
'?' condition is false
3924 DP_SIDEBAND_MSG_DOWN_REP_BASE0x1400;
3925
3926 if (!up
8.1
'up' is false
)
9
Taking true branch
3927 *mstb = NULL((void *)0);
3928
3929 len = min(mgr->max_dpcd_transaction_bytes, 16)(((mgr->max_dpcd_transaction_bytes)<(16))?(mgr->max_dpcd_transaction_bytes
):(16))
;
10
Assuming field 'max_dpcd_transaction_bytes' is >= 16
11
'?' condition is false
3930 ret = drm_dp_dpcd_read(mgr->aux, basereg, replyblock, len);
3931 if (ret != len) {
12
Assuming 'ret' is equal to 'len'
13
Taking false branch
3932 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret)__drm_dbg(DRM_UT_KMS, "failed to read DPCD down rep %d %d\n",
len, ret)
;
3933 return false0;
3934 }
3935
3936 ret = drm_dp_decode_sideband_msg_hdr(&hdr, replyblock, len, &hdrlen);
14
Calling 'drm_dp_decode_sideband_msg_hdr'
23
Returning from 'drm_dp_decode_sideband_msg_hdr'
3937 if (ret
23.1
'ret' is not equal to false
== false0) {
24
Taking false branch
3938 print_hex_dump(KERN_DEBUG"\0017", "failed hdr", DUMP_PREFIX_NONE, 16,
3939 1, replyblock, len, false0);
3940 DRM_DEBUG_KMS("ERROR: failed header\n")__drm_dbg(DRM_UT_KMS, "ERROR: failed header\n");
3941 return false0;
3942 }
3943
3944 if (!up
24.1
'up' is false
) {
25
Taking true branch
3945 /* Caller is responsible for giving back this reference */
3946 *mstb = drm_dp_get_mst_branch_device(mgr, hdr.lct, hdr.rad);
26
Calling 'drm_dp_get_mst_branch_device'
3947 if (!*mstb) {
3948 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",__drm_dbg(DRM_UT_KMS, "Got MST reply from unknown device %d\n"
, hdr.lct)
3949 hdr.lct)__drm_dbg(DRM_UT_KMS, "Got MST reply from unknown device %d\n"
, hdr.lct)
;
3950 return false0;
3951 }
3952 }
3953
3954 if (!drm_dp_sideband_msg_set_header(msg, &hdr, hdrlen)) {
3955 DRM_DEBUG_KMS("sideband msg set header failed %d\n",__drm_dbg(DRM_UT_KMS, "sideband msg set header failed %d\n", replyblock
[0])
3956 replyblock[0])__drm_dbg(DRM_UT_KMS, "sideband msg set header failed %d\n", replyblock
[0])
;
3957 return false0;
3958 }
3959
3960 replylen = min(msg->curchunk_len, (u8)(len - hdrlen))(((msg->curchunk_len)<((u8)(len - hdrlen)))?(msg->curchunk_len
):((u8)(len - hdrlen)))
;
3961 ret = drm_dp_sideband_append_payload(msg, replyblock + hdrlen, replylen);
3962 if (!ret) {
3963 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0])__drm_dbg(DRM_UT_KMS, "sideband msg build failed %d\n", replyblock
[0])
;
3964 return false0;
3965 }
3966
3967 replylen = msg->curchunk_len + msg->curchunk_hdrlen - len;
3968 curreply = len;
3969 while (replylen > 0) {
3970 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16)(((replylen)<((((mgr->max_dpcd_transaction_bytes)<(16
))?(mgr->max_dpcd_transaction_bytes):(16))))?(replylen):((
((mgr->max_dpcd_transaction_bytes)<(16))?(mgr->max_dpcd_transaction_bytes
):(16))))
;
3971 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
3972 replyblock, len);
3973 if (ret != len) {
3974 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",__drm_dbg(DRM_UT_KMS, "failed to read a chunk (len %d, ret %d)\n"
, len, ret)
3975 len, ret)__drm_dbg(DRM_UT_KMS, "failed to read a chunk (len %d, ret %d)\n"
, len, ret)
;
3976 return false0;
3977 }
3978
3979 ret = drm_dp_sideband_append_payload(msg, replyblock, len);
3980 if (!ret) {
3981 DRM_DEBUG_KMS("failed to build sideband msg\n")__drm_dbg(DRM_UT_KMS, "failed to build sideband msg\n");
3982 return false0;
3983 }
3984
3985 curreply += len;
3986 replylen -= len;
3987 }
3988 return true1;
3989}
3990
3991static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
3992{
3993 struct drm_dp_sideband_msg_tx *txmsg;
3994 struct drm_dp_mst_branch *mstb = NULL((void *)0);
3995 struct drm_dp_sideband_msg_rx *msg = &mgr->down_rep_recv;
3996
3997 if (!drm_dp_get_one_sb_msg(mgr, false0, &mstb))
6
Calling 'drm_dp_get_one_sb_msg'
3998 goto out;
3999
4000 /* Multi-packet message transmission, don't clear the reply */
4001 if (!msg->have_eomt)
4002 goto out;
4003
4004 /* find the message */
4005 mutex_lock(&mgr->qlock)rw_enter_write(&mgr->qlock);
4006 txmsg = list_first_entry_or_null(&mgr->tx_msg_downq,(list_empty(&mgr->tx_msg_downq) ? ((void *)0) : ({ const
__typeof( ((struct drm_dp_sideband_msg_tx *)0)->next ) *__mptr
= ((&mgr->tx_msg_downq)->next); (struct drm_dp_sideband_msg_tx
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_sideband_msg_tx
, next) );}))
4007 struct drm_dp_sideband_msg_tx, next)(list_empty(&mgr->tx_msg_downq) ? ((void *)0) : ({ const
__typeof( ((struct drm_dp_sideband_msg_tx *)0)->next ) *__mptr
= ((&mgr->tx_msg_downq)->next); (struct drm_dp_sideband_msg_tx
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_sideband_msg_tx
, next) );}))
;
4008 mutex_unlock(&mgr->qlock)rw_exit_write(&mgr->qlock);
4009
4010 /* Were we actually expecting a response, and from this mstb? */
4011 if (!txmsg || txmsg->dst != mstb) {
4012 struct drm_dp_sideband_msg_hdr *hdr;
4013
4014 hdr = &msg->initial_hdr;
4015 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",__drm_dbg(DRM_UT_KMS, "Got MST reply with no msg %p %d %d %02x %02x\n"
, mstb, hdr->seqno, hdr->lct, hdr->rad[0], msg->msg
[0])
4016 mstb, hdr->seqno, hdr->lct, hdr->rad[0],__drm_dbg(DRM_UT_KMS, "Got MST reply with no msg %p %d %d %02x %02x\n"
, mstb, hdr->seqno, hdr->lct, hdr->rad[0], msg->msg
[0])
4017 msg->msg[0])__drm_dbg(DRM_UT_KMS, "Got MST reply with no msg %p %d %d %02x %02x\n"
, mstb, hdr->seqno, hdr->lct, hdr->rad[0], msg->msg
[0])
;
4018 goto out_clear_reply;
4019 }
4020
4021 drm_dp_sideband_parse_reply(msg, &txmsg->reply);
4022
4023 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01) {
4024 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",__drm_dbg(DRM_UT_KMS, "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n"
, txmsg->reply.req_type, drm_dp_mst_req_type_str(txmsg->
reply.req_type), txmsg->reply.u.nak.reason, drm_dp_mst_nak_reason_str
(txmsg->reply.u.nak.reason), txmsg->reply.u.nak.nak_data
)
4025 txmsg->reply.req_type,__drm_dbg(DRM_UT_KMS, "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n"
, txmsg->reply.req_type, drm_dp_mst_req_type_str(txmsg->
reply.req_type), txmsg->reply.u.nak.reason, drm_dp_mst_nak_reason_str
(txmsg->reply.u.nak.reason), txmsg->reply.u.nak.nak_data
)
4026 drm_dp_mst_req_type_str(txmsg->reply.req_type),__drm_dbg(DRM_UT_KMS, "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n"
, txmsg->reply.req_type, drm_dp_mst_req_type_str(txmsg->
reply.req_type), txmsg->reply.u.nak.reason, drm_dp_mst_nak_reason_str
(txmsg->reply.u.nak.reason), txmsg->reply.u.nak.nak_data
)
4027 txmsg->reply.u.nak.reason,__drm_dbg(DRM_UT_KMS, "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n"
, txmsg->reply.req_type, drm_dp_mst_req_type_str(txmsg->
reply.req_type), txmsg->reply.u.nak.reason, drm_dp_mst_nak_reason_str
(txmsg->reply.u.nak.reason), txmsg->reply.u.nak.nak_data
)
4028 drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),__drm_dbg(DRM_UT_KMS, "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n"
, txmsg->reply.req_type, drm_dp_mst_req_type_str(txmsg->
reply.req_type), txmsg->reply.u.nak.reason, drm_dp_mst_nak_reason_str
(txmsg->reply.u.nak.reason), txmsg->reply.u.nak.nak_data
)
4029 txmsg->reply.u.nak.nak_data)__drm_dbg(DRM_UT_KMS, "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n"
, txmsg->reply.req_type, drm_dp_mst_req_type_str(txmsg->
reply.req_type), txmsg->reply.u.nak.reason, drm_dp_mst_nak_reason_str
(txmsg->reply.u.nak.reason), txmsg->reply.u.nak.nak_data
)
;
4030 }
4031
4032 memset(msg, 0, sizeof(struct drm_dp_sideband_msg_rx))__builtin_memset((msg), (0), (sizeof(struct drm_dp_sideband_msg_rx
)))
;
4033 drm_dp_mst_topology_put_mstb(mstb);
4034
4035 mutex_lock(&mgr->qlock)rw_enter_write(&mgr->qlock);
4036 txmsg->state = DRM_DP_SIDEBAND_TX_RX3;
4037 list_del(&txmsg->next);
4038 mutex_unlock(&mgr->qlock)rw_exit_write(&mgr->qlock);
4039
4040 wake_up_all(&mgr->tx_waitq)wake_up(&mgr->tx_waitq);
4041
4042 return 0;
4043
4044out_clear_reply:
4045 memset(msg, 0, sizeof(struct drm_dp_sideband_msg_rx))__builtin_memset((msg), (0), (sizeof(struct drm_dp_sideband_msg_rx
)))
;
4046out:
4047 if (mstb)
4048 drm_dp_mst_topology_put_mstb(mstb);
4049
4050 return 0;
4051}
4052
4053static inline bool_Bool
4054drm_dp_mst_process_up_req(struct drm_dp_mst_topology_mgr *mgr,
4055 struct drm_dp_pending_up_req *up_req)
4056{
4057 struct drm_dp_mst_branch *mstb = NULL((void *)0);
4058 struct drm_dp_sideband_msg_req_body *msg = &up_req->msg;
4059 struct drm_dp_sideband_msg_hdr *hdr = &up_req->hdr;
4060 bool_Bool hotplug = false0;
4061
4062 if (hdr->broadcast) {
4063 const u8 *guid = NULL((void *)0);
4064
4065 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY0x02)
4066 guid = msg->u.conn_stat.guid;
4067 else if (msg->req_type == DP_RESOURCE_STATUS_NOTIFY0x13)
4068 guid = msg->u.resource_stat.guid;
4069
4070 if (guid)
4071 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, guid);
4072 } else {
4073 mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad);
4074 }
4075
4076 if (!mstb) {
4077 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",__drm_dbg(DRM_UT_KMS, "Got MST reply from unknown device %d\n"
, hdr->lct)
4078 hdr->lct)__drm_dbg(DRM_UT_KMS, "Got MST reply from unknown device %d\n"
, hdr->lct)
;
4079 return false0;
4080 }
4081
4082 /* TODO: Add missing handler for DP_RESOURCE_STATUS_NOTIFY events */
4083 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY0x02) {
4084 drm_dp_mst_handle_conn_stat(mstb, &msg->u.conn_stat);
4085 hotplug = true1;
4086 }
4087
4088 drm_dp_mst_topology_put_mstb(mstb);
4089 return hotplug;
4090}
4091
4092static void drm_dp_mst_up_req_work(struct work_struct *work)
4093{
4094 struct drm_dp_mst_topology_mgr *mgr =
4095 container_of(work, struct drm_dp_mst_topology_mgr,({ const __typeof( ((struct drm_dp_mst_topology_mgr *)0)->
up_req_work ) *__mptr = (work); (struct drm_dp_mst_topology_mgr
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_topology_mgr
, up_req_work) );})
4096 up_req_work)({ const __typeof( ((struct drm_dp_mst_topology_mgr *)0)->
up_req_work ) *__mptr = (work); (struct drm_dp_mst_topology_mgr
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_topology_mgr
, up_req_work) );})
;
4097 struct drm_dp_pending_up_req *up_req;
4098 bool_Bool send_hotplug = false0;
4099
4100 mutex_lock(&mgr->probe_lock)rw_enter_write(&mgr->probe_lock);
4101 while (true1) {
4102 mutex_lock(&mgr->up_req_lock)rw_enter_write(&mgr->up_req_lock);
4103 up_req = list_first_entry_or_null(&mgr->up_req_list,(list_empty(&mgr->up_req_list) ? ((void *)0) : ({ const
__typeof( ((struct drm_dp_pending_up_req *)0)->next ) *__mptr
= ((&mgr->up_req_list)->next); (struct drm_dp_pending_up_req
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_pending_up_req
, next) );}))
4104 struct drm_dp_pending_up_req,(list_empty(&mgr->up_req_list) ? ((void *)0) : ({ const
__typeof( ((struct drm_dp_pending_up_req *)0)->next ) *__mptr
= ((&mgr->up_req_list)->next); (struct drm_dp_pending_up_req
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_pending_up_req
, next) );}))
4105 next)(list_empty(&mgr->up_req_list) ? ((void *)0) : ({ const
__typeof( ((struct drm_dp_pending_up_req *)0)->next ) *__mptr
= ((&mgr->up_req_list)->next); (struct drm_dp_pending_up_req
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_pending_up_req
, next) );}))
;
4106 if (up_req)
4107 list_del(&up_req->next);
4108 mutex_unlock(&mgr->up_req_lock)rw_exit_write(&mgr->up_req_lock);
4109
4110 if (!up_req)
4111 break;
4112
4113 send_hotplug |= drm_dp_mst_process_up_req(mgr, up_req);
4114 kfree(up_req);
4115 }
4116 mutex_unlock(&mgr->probe_lock)rw_exit_write(&mgr->probe_lock);
4117
4118 if (send_hotplug)
4119 drm_kms_helper_hotplug_event(mgr->dev);
4120}
4121
4122static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
4123{
4124 struct drm_dp_pending_up_req *up_req;
4125
4126 if (!drm_dp_get_one_sb_msg(mgr, true1, NULL((void *)0)))
4127 goto out;
4128
4129 if (!mgr->up_req_recv.have_eomt)
4130 return 0;
4131
4132 up_req = kzalloc(sizeof(*up_req), GFP_KERNEL(0x0001 | 0x0004));
4133 if (!up_req) {
4134 DRM_ERROR("Not enough memory to process MST up req\n")__drm_err("Not enough memory to process MST up req\n");
4135 return -ENOMEM12;
4136 }
4137 INIT_LIST_HEAD(&up_req->next);
4138
4139 drm_dp_sideband_parse_req(&mgr->up_req_recv, &up_req->msg);
4140
4141 if (up_req->msg.req_type != DP_CONNECTION_STATUS_NOTIFY0x02 &&
4142 up_req->msg.req_type != DP_RESOURCE_STATUS_NOTIFY0x13) {
4143 DRM_DEBUG_KMS("Received unknown up req type, ignoring: %x\n",__drm_dbg(DRM_UT_KMS, "Received unknown up req type, ignoring: %x\n"
, up_req->msg.req_type)
4144 up_req->msg.req_type)__drm_dbg(DRM_UT_KMS, "Received unknown up req type, ignoring: %x\n"
, up_req->msg.req_type)
;
4145 kfree(up_req);
4146 goto out;
4147 }
4148
4149 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, up_req->msg.req_type,
4150 false0);
4151
4152 if (up_req->msg.req_type == DP_CONNECTION_STATUS_NOTIFY0x02) {
4153 const struct drm_dp_connection_status_notify *conn_stat =
4154 &up_req->msg.u.conn_stat;
4155
4156 DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n",__drm_dbg(DRM_UT_KMS, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n"
, conn_stat->port_number, conn_stat->legacy_device_plug_status
, conn_stat->displayport_device_plug_status, conn_stat->
message_capability_status, conn_stat->input_port, conn_stat
->peer_device_type)
4157 conn_stat->port_number,__drm_dbg(DRM_UT_KMS, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n"
, conn_stat->port_number, conn_stat->legacy_device_plug_status
, conn_stat->displayport_device_plug_status, conn_stat->
message_capability_status, conn_stat->input_port, conn_stat
->peer_device_type)
4158 conn_stat->legacy_device_plug_status,__drm_dbg(DRM_UT_KMS, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n"
, conn_stat->port_number, conn_stat->legacy_device_plug_status
, conn_stat->displayport_device_plug_status, conn_stat->
message_capability_status, conn_stat->input_port, conn_stat
->peer_device_type)
4159 conn_stat->displayport_device_plug_status,__drm_dbg(DRM_UT_KMS, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n"
, conn_stat->port_number, conn_stat->legacy_device_plug_status
, conn_stat->displayport_device_plug_status, conn_stat->
message_capability_status, conn_stat->input_port, conn_stat
->peer_device_type)
4160 conn_stat->message_capability_status,__drm_dbg(DRM_UT_KMS, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n"
, conn_stat->port_number, conn_stat->legacy_device_plug_status
, conn_stat->displayport_device_plug_status, conn_stat->
message_capability_status, conn_stat->input_port, conn_stat
->peer_device_type)
4161 conn_stat->input_port,__drm_dbg(DRM_UT_KMS, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n"
, conn_stat->port_number, conn_stat->legacy_device_plug_status
, conn_stat->displayport_device_plug_status, conn_stat->
message_capability_status, conn_stat->input_port, conn_stat
->peer_device_type)
4162 conn_stat->peer_device_type)__drm_dbg(DRM_UT_KMS, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n"
, conn_stat->port_number, conn_stat->legacy_device_plug_status
, conn_stat->displayport_device_plug_status, conn_stat->
message_capability_status, conn_stat->input_port, conn_stat
->peer_device_type)
;
4163 } else if (up_req->msg.req_type == DP_RESOURCE_STATUS_NOTIFY0x13) {
4164 const struct drm_dp_resource_status_notify *res_stat =
4165 &up_req->msg.u.resource_stat;
4166
4167 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n",__drm_dbg(DRM_UT_KMS, "Got RSN: pn: %d avail_pbn %d\n", res_stat
->port_number, res_stat->available_pbn)
4168 res_stat->port_number,__drm_dbg(DRM_UT_KMS, "Got RSN: pn: %d avail_pbn %d\n", res_stat
->port_number, res_stat->available_pbn)
4169 res_stat->available_pbn)__drm_dbg(DRM_UT_KMS, "Got RSN: pn: %d avail_pbn %d\n", res_stat
->port_number, res_stat->available_pbn)
;
4170 }
4171
4172 up_req->hdr = mgr->up_req_recv.initial_hdr;
4173 mutex_lock(&mgr->up_req_lock)rw_enter_write(&mgr->up_req_lock);
4174 list_add_tail(&up_req->next, &mgr->up_req_list);
4175 mutex_unlock(&mgr->up_req_lock)rw_exit_write(&mgr->up_req_lock);
4176 queue_work(system_long_wq, &mgr->up_req_work);
4177
4178out:
4179 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx))__builtin_memset((&mgr->up_req_recv), (0), (sizeof(struct
drm_dp_sideband_msg_rx)))
;
4180 return 0;
4181}
4182
4183/**
4184 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
4185 * @mgr: manager to notify irq for.
4186 * @esi: 4 bytes from SINK_COUNT_ESI
4187 * @handled: whether the hpd interrupt was consumed or not
4188 *
4189 * This should be called from the driver when it detects a short IRQ,
4190 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
4191 * topology manager will process the sideband messages received as a result
4192 * of this.
4193 */
4194int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool_Bool *handled)
4195{
4196 int ret = 0;
4197 int sc;
4198 *handled = false0;
4199 sc = esi[0] & 0x3f;
4200
4201 if (sc != mgr->sink_count) {
1
Assuming 'sc' is equal to field 'sink_count'
2
Taking false branch
4202 mgr->sink_count = sc;
4203 *handled = true1;
4204 }
4205
4206 if (esi[1] & DP_DOWN_REP_MSG_RDY(1 << 4)) {
3
Assuming the condition is true
4
Taking true branch
4207 ret = drm_dp_mst_handle_down_rep(mgr);
5
Calling 'drm_dp_mst_handle_down_rep'
4208 *handled = true1;
4209 }
4210
4211 if (esi[1] & DP_UP_REQ_MSG_RDY(1 << 5)) {
4212 ret |= drm_dp_mst_handle_up_req(mgr);
4213 *handled = true1;
4214 }
4215
4216 drm_dp_mst_kick_tx(mgr);
4217 return ret;
4218}
4219EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
4220
4221/**
4222 * drm_dp_mst_detect_port() - get connection status for an MST port
4223 * @connector: DRM connector for this port
4224 * @ctx: The acquisition context to use for grabbing locks
4225 * @mgr: manager for this port
4226 * @port: pointer to a port
4227 *
4228 * This returns the current connection state for a port.
4229 */
4230int
4231drm_dp_mst_detect_port(struct drm_connector *connector,
4232 struct drm_modeset_acquire_ctx *ctx,
4233 struct drm_dp_mst_topology_mgr *mgr,
4234 struct drm_dp_mst_port *port)
4235{
4236 int ret;
4237
4238 /* we need to search for the port in the mgr in case it's gone */
4239 port = drm_dp_mst_topology_get_port_validated(mgr, port);
4240 if (!port)
4241 return connector_status_disconnected;
4242
4243 ret = drm_modeset_lock(&mgr->base.lock, ctx);
4244 if (ret)
4245 goto out;
4246
4247 ret = connector_status_disconnected;
4248
4249 if (!port->ddps)
4250 goto out;
4251
4252 switch (port->pdt) {
4253 case DP_PEER_DEVICE_NONE0x0:
4254 break;
4255 case DP_PEER_DEVICE_MST_BRANCHING0x2:
4256 if (!port->mcs)
4257 ret = connector_status_connected;
4258 break;
4259
4260 case DP_PEER_DEVICE_SST_SINK0x3:
4261 ret = connector_status_connected;
4262 /* for logical ports - cache the EDID */
4263 if (port->port_num >= 8 && !port->cached_edid) {
4264 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
4265 }
4266 break;
4267 case DP_PEER_DEVICE_DP_LEGACY_CONV0x4:
4268 if (port->ldps)
4269 ret = connector_status_connected;
4270 break;
4271 }
4272out:
4273 drm_dp_mst_topology_put_port(port);
4274 return ret;
4275}
4276EXPORT_SYMBOL(drm_dp_mst_detect_port);
4277
4278/**
4279 * drm_dp_mst_get_edid() - get EDID for an MST port
4280 * @connector: toplevel connector to get EDID for
4281 * @mgr: manager for this port
4282 * @port: unverified pointer to a port.
4283 *
4284 * This returns an EDID for the port connected to a connector,
4285 * It validates the pointer still exists so the caller doesn't require a
4286 * reference.
4287 */
4288struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
4289{
4290 struct edid *edid = NULL((void *)0);
4291
4292 /* we need to search for the port in the mgr in case it's gone */
4293 port = drm_dp_mst_topology_get_port_validated(mgr, port);
4294 if (!port)
4295 return NULL((void *)0);
4296
4297 if (port->cached_edid)
4298 edid = drm_edid_duplicate(port->cached_edid);
4299 else {
4300 edid = drm_get_edid(connector, &port->aux.ddc);
4301 }
4302 port->has_audio = drm_detect_monitor_audio(edid);
4303 drm_dp_mst_topology_put_port(port);
4304 return edid;
4305}
4306EXPORT_SYMBOL(drm_dp_mst_get_edid);
4307
4308/**
4309 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
4310 * @mgr: manager to use
4311 * @pbn: payload bandwidth to convert into slots.
4312 *
4313 * Calculate the number of VCPI slots that will be required for the given PBN
4314 * value. This function is deprecated, and should not be used in atomic
4315 * drivers.
4316 *
4317 * RETURNS:
4318 * The total slots required for this port, or error.
4319 */
4320int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
4321 int pbn)
4322{
4323 int num_slots;
4324
4325 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div)(((pbn) + ((mgr->pbn_div) - 1)) / (mgr->pbn_div));
4326
4327 /* max. time slots - one slot for MTP header */
4328 if (num_slots > 63)
4329 return -ENOSPC28;
4330 return num_slots;
4331}
4332EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
4333
4334static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
4335 struct drm_dp_vcpi *vcpi, int pbn, int slots)
4336{
4337 int ret;
4338
4339 /* max. time slots - one slot for MTP header */
4340 if (slots > 63)
4341 return -ENOSPC28;
4342
4343 vcpi->pbn = pbn;
4344 vcpi->aligned_pbn = slots * mgr->pbn_div;
4345 vcpi->num_slots = slots;
4346
4347 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
4348 if (ret < 0)
4349 return ret;
4350 return 0;
4351}
4352
4353/**
4354 * drm_dp_atomic_find_vcpi_slots() - Find and add VCPI slots to the state
4355 * @state: global atomic state
4356 * @mgr: MST topology manager for the port
4357 * @port: port to find vcpi slots for
4358 * @pbn: bandwidth required for the mode in PBN
4359 * @pbn_div: divider for DSC mode that takes FEC into account
4360 *
4361 * Allocates VCPI slots to @port, replacing any previous VCPI allocations it
4362 * may have had. Any atomic drivers which support MST must call this function
4363 * in their &drm_encoder_helper_funcs.atomic_check() callback to change the
4364 * current VCPI allocation for the new state, but only when
4365 * &drm_crtc_state.mode_changed or &drm_crtc_state.connectors_changed is set
4366 * to ensure compatibility with userspace applications that still use the
4367 * legacy modesetting UAPI.
4368 *
4369 * Allocations set by this function are not checked against the bandwidth
4370 * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
4371 *
4372 * Additionally, it is OK to call this function multiple times on the same
4373 * @port as needed. It is not OK however, to call this function and
4374 * drm_dp_atomic_release_vcpi_slots() in the same atomic check phase.
4375 *
4376 * See also:
4377 * drm_dp_atomic_release_vcpi_slots()
4378 * drm_dp_mst_atomic_check()
4379 *
4380 * Returns:
4381 * Total slots in the atomic state assigned for this port, or a negative error
4382 * code if the port no longer exists
4383 */
4384int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
4385 struct drm_dp_mst_topology_mgr *mgr,
4386 struct drm_dp_mst_port *port, int pbn,
4387 int pbn_div)
4388{
4389 struct drm_dp_mst_topology_state *topology_state;
4390 struct drm_dp_vcpi_allocation *pos, *vcpi = NULL((void *)0);
4391 int prev_slots, prev_bw, req_slots;
4392
4393 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
4394 if (IS_ERR(topology_state))
4395 return PTR_ERR(topology_state);
4396
4397 /* Find the current allocation for this port, if any */
4398 list_for_each_entry(pos, &topology_state->vcpis, next)for (pos = ({ const __typeof( ((__typeof(*pos) *)0)->next )
*__mptr = ((&topology_state->vcpis)->next); (__typeof
(*pos) *)( (char *)__mptr - __builtin_offsetof(__typeof(*pos)
, next) );}); &pos->next != (&topology_state->vcpis
); pos = ({ const __typeof( ((__typeof(*pos) *)0)->next ) *
__mptr = (pos->next.next); (__typeof(*pos) *)( (char *)__mptr
- __builtin_offsetof(__typeof(*pos), next) );}))
{
4399 if (pos->port == port) {
4400 vcpi = pos;
4401 prev_slots = vcpi->vcpi;
4402 prev_bw = vcpi->pbn;
4403
4404 /*
4405 * This should never happen, unless the driver tries
4406 * releasing and allocating the same VCPI allocation,
4407 * which is an error
4408 */
4409 if (WARN_ON(!prev_slots)({ int __ret = !!(!prev_slots); if (__ret) printf("WARNING %s failed at %s:%d\n"
, "!prev_slots", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 4409); __builtin_expect(!!(__ret), 0); })
) {
4410 DRM_ERROR("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n",__drm_err("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n"
, port)
4411 port)__drm_err("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n"
, port)
;
4412 return -EINVAL22;
4413 }
4414
4415 break;
4416 }
4417 }
4418 if (!vcpi) {
4419 prev_slots = 0;
4420 prev_bw = 0;
4421 }
4422
4423 if (pbn_div <= 0)
4424 pbn_div = mgr->pbn_div;
4425
4426 req_slots = DIV_ROUND_UP(pbn, pbn_div)(((pbn) + ((pbn_div) - 1)) / (pbn_div));
4427
4428 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n",__drm_dbg(DRM_UT_ATOMIC, "[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n"
, port->connector->base.id, port->connector->name
, port, prev_slots, req_slots)
4429 port->connector->base.id, port->connector->name,__drm_dbg(DRM_UT_ATOMIC, "[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n"
, port->connector->base.id, port->connector->name
, port, prev_slots, req_slots)
4430 port, prev_slots, req_slots)__drm_dbg(DRM_UT_ATOMIC, "[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n"
, port->connector->base.id, port->connector->name
, port, prev_slots, req_slots)
;
4431 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n",__drm_dbg(DRM_UT_ATOMIC, "[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n"
, port->connector->base.id, port->connector->name
, port, prev_bw, pbn)
4432 port->connector->base.id, port->connector->name,__drm_dbg(DRM_UT_ATOMIC, "[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n"
, port->connector->base.id, port->connector->name
, port, prev_bw, pbn)
4433 port, prev_bw, pbn)__drm_dbg(DRM_UT_ATOMIC, "[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n"
, port->connector->base.id, port->connector->name
, port, prev_bw, pbn)
;
4434
4435 /* Add the new allocation to the state */
4436 if (!vcpi) {
4437 vcpi = kzalloc(sizeof(*vcpi), GFP_KERNEL(0x0001 | 0x0004));
4438 if (!vcpi)
4439 return -ENOMEM12;
4440
4441 drm_dp_mst_get_port_malloc(port);
4442 vcpi->port = port;
4443 list_add(&vcpi->next, &topology_state->vcpis);
4444 }
4445 vcpi->vcpi = req_slots;
4446 vcpi->pbn = pbn;
4447
4448 return req_slots;
4449}
4450EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
4451
4452/**
4453 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
4454 * @state: global atomic state
4455 * @mgr: MST topology manager for the port
4456 * @port: The port to release the VCPI slots from
4457 *
4458 * Releases any VCPI slots that have been allocated to a port in the atomic
4459 * state. Any atomic drivers which support MST must call this function in
4460 * their &drm_connector_helper_funcs.atomic_check() callback when the
4461 * connector will no longer have VCPI allocated (e.g. because its CRTC was
4462 * removed) when it had VCPI allocated in the previous atomic state.
4463 *
4464 * It is OK to call this even if @port has been removed from the system.
4465 * Additionally, it is OK to call this function multiple times on the same
4466 * @port as needed. It is not OK however, to call this function and
4467 * drm_dp_atomic_find_vcpi_slots() on the same @port in a single atomic check
4468 * phase.
4469 *
4470 * See also:
4471 * drm_dp_atomic_find_vcpi_slots()
4472 * drm_dp_mst_atomic_check()
4473 *
4474 * Returns:
4475 * 0 if all slots for this port were added back to
4476 * &drm_dp_mst_topology_state.avail_slots or negative error code
4477 */
4478int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
4479 struct drm_dp_mst_topology_mgr *mgr,
4480 struct drm_dp_mst_port *port)
4481{
4482 struct drm_dp_mst_topology_state *topology_state;
4483 struct drm_dp_vcpi_allocation *pos;
4484 bool_Bool found = false0;
4485
4486 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
4487 if (IS_ERR(topology_state))
4488 return PTR_ERR(topology_state);
4489
4490 list_for_each_entry(pos, &topology_state->vcpis, next)for (pos = ({ const __typeof( ((__typeof(*pos) *)0)->next )
*__mptr = ((&topology_state->vcpis)->next); (__typeof
(*pos) *)( (char *)__mptr - __builtin_offsetof(__typeof(*pos)
, next) );}); &pos->next != (&topology_state->vcpis
); pos = ({ const __typeof( ((__typeof(*pos) *)0)->next ) *
__mptr = (pos->next.next); (__typeof(*pos) *)( (char *)__mptr
- __builtin_offsetof(__typeof(*pos), next) );}))
{
4491 if (pos->port == port) {
4492 found = true1;
4493 break;
4494 }
4495 }
4496 if (WARN_ON(!found)({ int __ret = !!(!found); if (__ret) printf("WARNING %s failed at %s:%d\n"
, "!found", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c",
4496); __builtin_expect(!!(__ret), 0); })
) {
4497 DRM_ERROR("no VCPI for [MST PORT:%p] found in mst state %p\n",__drm_err("no VCPI for [MST PORT:%p] found in mst state %p\n"
, port, &topology_state->base)
4498 port, &topology_state->base)__drm_err("no VCPI for [MST PORT:%p] found in mst state %p\n"
, port, &topology_state->base)
;
4499 return -EINVAL22;
4500 }
4501
4502 DRM_DEBUG_ATOMIC("[MST PORT:%p] VCPI %d -> 0\n", port, pos->vcpi)__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] VCPI %d -> 0\n", port
, pos->vcpi)
;
4503 if (pos->vcpi) {
4504 drm_dp_mst_put_port_malloc(port);
4505 pos->vcpi = 0;
4506 pos->pbn = 0;
4507 }
4508
4509 return 0;
4510}
4511EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
4512
4513/**
4514 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
4515 * @mgr: manager for this port
4516 * @port: port to allocate a virtual channel for.
4517 * @pbn: payload bandwidth number to request
4518 * @slots: returned number of slots for this PBN.
4519 */
4520bool_Bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
4521 struct drm_dp_mst_port *port, int pbn, int slots)
4522{
4523 int ret;
4524
4525 if (slots < 0)
4526 return false0;
4527
4528 port = drm_dp_mst_topology_get_port_validated(mgr, port);
4529 if (!port)
4530 return false0;
4531
4532 if (port->vcpi.vcpi > 0) {
4533 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",__drm_dbg(DRM_UT_KMS, "payload: vcpi %d already allocated for pbn %d - requested pbn %d\n"
, port->vcpi.vcpi, port->vcpi.pbn, pbn)
4534 port->vcpi.vcpi, port->vcpi.pbn, pbn)__drm_dbg(DRM_UT_KMS, "payload: vcpi %d already allocated for pbn %d - requested pbn %d\n"
, port->vcpi.vcpi, port->vcpi.pbn, pbn)
;
4535 if (pbn == port->vcpi.pbn) {
4536 drm_dp_mst_topology_put_port(port);
4537 return true1;
4538 }
4539 }
4540
4541 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
4542 if (ret) {
4543 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",__drm_dbg(DRM_UT_KMS, "failed to init vcpi slots=%d max=63 ret=%d\n"
, (((pbn) + ((mgr->pbn_div) - 1)) / (mgr->pbn_div)), ret
)
4544 DIV_ROUND_UP(pbn, mgr->pbn_div), ret)__drm_dbg(DRM_UT_KMS, "failed to init vcpi slots=%d max=63 ret=%d\n"
, (((pbn) + ((mgr->pbn_div) - 1)) / (mgr->pbn_div)), ret
)
;
4545 drm_dp_mst_topology_put_port(port);
4546 goto out;
4547 }
4548 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",__drm_dbg(DRM_UT_KMS, "initing vcpi for pbn=%d slots=%d\n", pbn
, port->vcpi.num_slots)
4549 pbn, port->vcpi.num_slots)__drm_dbg(DRM_UT_KMS, "initing vcpi for pbn=%d slots=%d\n", pbn
, port->vcpi.num_slots)
;
4550
4551 /* Keep port allocated until its payload has been removed */
4552 drm_dp_mst_get_port_malloc(port);
4553 drm_dp_mst_topology_put_port(port);
4554 return true1;
4555out:
4556 return false0;
4557}
4558EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
4559
4560int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
4561{
4562 int slots = 0;
4563
4564 port = drm_dp_mst_topology_get_port_validated(mgr, port);
4565 if (!port)
4566 return slots;
4567
4568 slots = port->vcpi.num_slots;
4569 drm_dp_mst_topology_put_port(port);
4570 return slots;
4571}
4572EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
4573
4574/**
4575 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
4576 * @mgr: manager for this port
4577 * @port: unverified pointer to a port.
4578 *
4579 * This just resets the number of slots for the ports VCPI for later programming.
4580 */
4581void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
4582{
4583 /*
4584 * A port with VCPI will remain allocated until its VCPI is
4585 * released, no verified ref needed
4586 */
4587
4588 port->vcpi.num_slots = 0;
4589}
4590EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
4591
4592/**
4593 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
4594 * @mgr: manager for this port
4595 * @port: port to deallocate vcpi for
4596 *
4597 * This can be called unconditionally, regardless of whether
4598 * drm_dp_mst_allocate_vcpi() succeeded or not.
4599 */
4600void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
4601 struct drm_dp_mst_port *port)
4602{
4603 bool_Bool skip;
4604
4605 if (!port->vcpi.vcpi)
4606 return;
4607
4608 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
4609 skip = !drm_dp_mst_port_downstream_of_branch(port, mgr->mst_primary);
4610 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
4611
4612 if (skip)
4613 return;
4614
4615 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
4616 port->vcpi.num_slots = 0;
4617 port->vcpi.pbn = 0;
4618 port->vcpi.aligned_pbn = 0;
4619 port->vcpi.vcpi = 0;
4620 drm_dp_mst_put_port_malloc(port);
4621}
4622EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
4623
4624static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
4625 int id, struct drm_dp_payload *payload)
4626{
4627 u8 payload_alloc[3], status;
4628 int ret;
4629 int retries = 0;
4630
4631 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS0x2c0,
4632 DP_PAYLOAD_TABLE_UPDATED(1 << 0));
4633
4634 payload_alloc[0] = id;
4635 payload_alloc[1] = payload->start_slot;
4636 payload_alloc[2] = payload->num_slots;
4637
4638 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET0x1c0, payload_alloc, 3);
4639 if (ret != 3) {
4640 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret)__drm_dbg(DRM_UT_KMS, "failed to write payload allocation %d\n"
, ret)
;
4641 goto fail;
4642 }
4643
4644retry:
4645 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS0x2c0, &status);
4646 if (ret < 0) {
4647 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret)__drm_dbg(DRM_UT_KMS, "failed to read payload table status %d\n"
, ret)
;
4648 goto fail;
4649 }
4650
4651 if (!(status & DP_PAYLOAD_TABLE_UPDATED(1 << 0))) {
4652 retries++;
4653 if (retries < 20) {
4654 usleep_range(10000, 20000);
4655 goto retry;
4656 }
4657 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status)__drm_dbg(DRM_UT_KMS, "status not set after read payload table status %d\n"
, status)
;
4658 ret = -EINVAL22;
4659 goto fail;
4660 }
4661 ret = 0;
4662fail:
4663 return ret;
4664}
4665
4666static int do_get_act_status(struct drm_dp_aux *aux)
4667{
4668 int ret;
4669 u8 status;
4670
4671 ret = drm_dp_dpcd_readb(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS0x2c0, &status);
4672 if (ret < 0)
4673 return ret;
4674
4675 return status;
4676}
4677
4678/**
4679 * drm_dp_check_act_status() - Polls for ACT handled status.
4680 * @mgr: manager to use
4681 *
4682 * Tries waiting for the MST hub to finish updating it's payload table by
4683 * polling for the ACT handled bit for up to 3 seconds (yes-some hubs really
4684 * take that long).
4685 *
4686 * Returns:
4687 * 0 if the ACT was handled in time, negative error code on failure.
4688 */
4689int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
4690{
4691 /*
4692 * There doesn't seem to be any recommended retry count or timeout in
4693 * the MST specification. Since some hubs have been observed to take
4694 * over 1 second to update their payload allocations under certain
4695 * conditions, we use a rather large timeout value.
4696 */
4697 const int timeout_ms = 3000;
4698 int ret, status;
4699
4700 ret = readx_poll_timeout(do_get_act_status, mgr->aux, status,({ struct timeval __end, __now, __timeout_tv; int __timed_out
= 0; if (timeout_ms * 1000L) { microuptime(&__now); USEC_TO_TIMEVAL
(timeout_ms * 1000L, &__timeout_tv); do { (&__end)->
tv_sec = (&__now)->tv_sec + (&__timeout_tv)->tv_sec
; (&__end)->tv_usec = (&__now)->tv_usec + (&
__timeout_tv)->tv_usec; if ((&__end)->tv_usec >=
1000000) { (&__end)->tv_sec++; (&__end)->tv_usec
-= 1000000; } } while (0); } for (;;) { (status) = (do_get_act_status
)(mgr->aux); if (status & (1 << 1) || status <
0) break; if (timeout_ms * 1000L) { microuptime(&__now);
if ((((&__end)->tv_sec == (&__now)->tv_sec) ? (
(&__end)->tv_usec <= (&__now)->tv_usec) : ((
&__end)->tv_sec <= (&__now)->tv_sec))) { __timed_out
= 1; break; } } if (200) (*delay_func)((200) / 2); } (__timed_out
) ? -60 : 0; })
4701 status & DP_PAYLOAD_ACT_HANDLED || status < 0,({ struct timeval __end, __now, __timeout_tv; int __timed_out
= 0; if (timeout_ms * 1000L) { microuptime(&__now); USEC_TO_TIMEVAL
(timeout_ms * 1000L, &__timeout_tv); do { (&__end)->
tv_sec = (&__now)->tv_sec + (&__timeout_tv)->tv_sec
; (&__end)->tv_usec = (&__now)->tv_usec + (&
__timeout_tv)->tv_usec; if ((&__end)->tv_usec >=
1000000) { (&__end)->tv_sec++; (&__end)->tv_usec
-= 1000000; } } while (0); } for (;;) { (status) = (do_get_act_status
)(mgr->aux); if (status & (1 << 1) || status <
0) break; if (timeout_ms * 1000L) { microuptime(&__now);
if ((((&__end)->tv_sec == (&__now)->tv_sec) ? (
(&__end)->tv_usec <= (&__now)->tv_usec) : ((
&__end)->tv_sec <= (&__now)->tv_sec))) { __timed_out
= 1; break; } } if (200) (*delay_func)((200) / 2); } (__timed_out
) ? -60 : 0; })
4702 200, timeout_ms * USEC_PER_MSEC)({ struct timeval __end, __now, __timeout_tv; int __timed_out
= 0; if (timeout_ms * 1000L) { microuptime(&__now); USEC_TO_TIMEVAL
(timeout_ms * 1000L, &__timeout_tv); do { (&__end)->
tv_sec = (&__now)->tv_sec + (&__timeout_tv)->tv_sec
; (&__end)->tv_usec = (&__now)->tv_usec + (&
__timeout_tv)->tv_usec; if ((&__end)->tv_usec >=
1000000) { (&__end)->tv_sec++; (&__end)->tv_usec
-= 1000000; } } while (0); } for (;;) { (status) = (do_get_act_status
)(mgr->aux); if (status & (1 << 1) || status <
0) break; if (timeout_ms * 1000L) { microuptime(&__now);
if ((((&__end)->tv_sec == (&__now)->tv_sec) ? (
(&__end)->tv_usec <= (&__now)->tv_usec) : ((
&__end)->tv_sec <= (&__now)->tv_sec))) { __timed_out
= 1; break; } } if (200) (*delay_func)((200) / 2); } (__timed_out
) ? -60 : 0; })
;
4703 if (ret < 0 && status >= 0) {
4704 DRM_ERROR("Failed to get ACT after %dms, last status: %02x\n",__drm_err("Failed to get ACT after %dms, last status: %02x\n"
, timeout_ms, status)
4705 timeout_ms, status)__drm_err("Failed to get ACT after %dms, last status: %02x\n"
, timeout_ms, status)
;
4706 return -EINVAL22;
4707 } else if (status < 0) {
4708 /*
4709 * Failure here isn't unexpected - the hub may have
4710 * just been unplugged
4711 */
4712 DRM_DEBUG_KMS("Failed to read payload table status: %d\n",__drm_dbg(DRM_UT_KMS, "Failed to read payload table status: %d\n"
, status)
4713 status)__drm_dbg(DRM_UT_KMS, "Failed to read payload table status: %d\n"
, status)
;
4714 return status;
4715 }
4716
4717 return 0;
4718}
4719EXPORT_SYMBOL(drm_dp_check_act_status);
4720
4721/**
4722 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
4723 * @clock: dot clock for the mode
4724 * @bpp: bpp for the mode.
4725 * @dsc: DSC mode. If true, bpp has units of 1/16 of a bit per pixel
4726 *
4727 * This uses the formula in the spec to calculate the PBN value for a mode.
4728 */
4729int drm_dp_calc_pbn_mode(int clock, int bpp, bool_Bool dsc)
4730{
4731 /*
4732 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
4733 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
4734 * common multiplier to render an integer PBN for all link rate/lane
4735 * counts combinations
4736 * calculate
4737 * peak_kbps *= (1006/1000)
4738 * peak_kbps *= (64/54)
4739 * peak_kbps *= 8 convert to bytes
4740 *
4741 * If the bpp is in units of 1/16, further divide by 16. Put this
4742 * factor in the numerator rather than the denominator to avoid
4743 * integer overflow
4744 */
4745
4746 if (dsc)
4747 return DIV_ROUND_UP_ULL(mul_u32_u32(clock * (bpp / 16), 64 * 1006),(((mul_u32_u32(clock * (bpp / 16), 64 * 1006)) + ((8 * 54 * 1000
* 1000) - 1)) / (8 * 54 * 1000 * 1000))
4748 8 * 54 * 1000 * 1000)(((mul_u32_u32(clock * (bpp / 16), 64 * 1006)) + ((8 * 54 * 1000
* 1000) - 1)) / (8 * 54 * 1000 * 1000))
;
4749
4750 return DIV_ROUND_UP_ULL(mul_u32_u32(clock * bpp, 64 * 1006),(((mul_u32_u32(clock * bpp, 64 * 1006)) + ((8 * 54 * 1000 * 1000
) - 1)) / (8 * 54 * 1000 * 1000))
4751 8 * 54 * 1000 * 1000)(((mul_u32_u32(clock * bpp, 64 * 1006)) + ((8 * 54 * 1000 * 1000
) - 1)) / (8 * 54 * 1000 * 1000))
;
4752}
4753EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
4754
4755/* we want to kick the TX after we've ack the up/down IRQs. */
4756static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
4757{
4758 queue_work(system_long_wq, &mgr->tx_work);
4759}
4760
4761static void drm_dp_mst_dump_mstb(struct seq_file *m,
4762 struct drm_dp_mst_branch *mstb)
4763{
4764 struct drm_dp_mst_port *port;
4765 int tabs = mstb->lct;
4766 char prefix[10];
4767 int i;
4768
4769 for (i = 0; i < tabs; i++)
4770 prefix[i] = '\t';
4771 prefix[i] = '\0';
4772
4773 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
4774 list_for_each_entry(port, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}); &port->next != (&mstb->ports); port = (
{ const __typeof( ((__typeof(*port) *)0)->next ) *__mptr =
(port->next.next); (__typeof(*port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*port), next) );}))
{
4775 seq_printf(m, "%sport: %d: input: %d: pdt: %d, ddps: %d ldps: %d, sdp: %d/%d, %p, conn: %p\n", prefix, port->port_num, port->input, port->pdt, port->ddps, port->ldps, port->num_sdp_streams, port->num_sdp_stream_sinks, port, port->connector);
4776 if (port->mstb)
4777 drm_dp_mst_dump_mstb(m, port->mstb);
4778 }
4779}
4780
4781#define DP_PAYLOAD_TABLE_SIZE64 64
4782
4783static bool_Bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
4784 char *buf)
4785{
4786 int i;
4787
4788 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE64; i += 16) {
4789 if (drm_dp_dpcd_read(mgr->aux,
4790 DP_PAYLOAD_TABLE_UPDATE_STATUS0x2c0 + i,
4791 &buf[i], 16) != 16)
4792 return false0;
4793 }
4794 return true1;
4795}
4796
4797static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
4798 struct drm_dp_mst_port *port, char *name,
4799 int namelen)
4800{
4801 struct edid *mst_edid;
4802
4803 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
4804 drm_edid_get_monitor_name(mst_edid, name, namelen);
4805}
4806
4807/**
4808 * drm_dp_mst_dump_topology(): dump topology to seq file.
4809 * @m: seq_file to dump output to
4810 * @mgr: manager to dump current topology for.
4811 *
4812 * helper to dump MST topology to a seq file for debugfs.
4813 */
4814void drm_dp_mst_dump_topology(struct seq_file *m,
4815 struct drm_dp_mst_topology_mgr *mgr)
4816{
4817 int i;
4818 struct drm_dp_mst_port *port;
4819
4820 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
4821 if (mgr->mst_primary)
4822 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
4823
4824 /* dump VCPIs */
4825 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
4826
4827 mutex_lock(&mgr->payload_lock)rw_enter_write(&mgr->payload_lock);
4828 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
4829 mgr->max_payloads);
4830
4831 for (i = 0; i < mgr->max_payloads; i++) {
4832 if (mgr->proposed_vcpis[i]) {
4833 char name[14];
4834
4835 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi)({ const __typeof( ((struct drm_dp_mst_port *)0)->vcpi ) *
__mptr = (mgr->proposed_vcpis[i]); (struct drm_dp_mst_port
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_port
, vcpi) );})
;
4836 fetch_monitor_name(mgr, port, name, sizeof(name));
4837 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
4838 port->port_num, port->vcpi.vcpi,
4839 port->vcpi.num_slots,
4840 (*name != 0) ? name : "Unknown");
4841 } else
4842 seq_printf(m, "vcpi %d:unused\n", i);
4843 }
4844 for (i = 0; i < mgr->max_payloads; i++) {
4845 seq_printf(m, "payload %d: %d, %d, %d\n",
4846 i,
4847 mgr->payloads[i].payload_state,
4848 mgr->payloads[i].start_slot,
4849 mgr->payloads[i].num_slots);
4850
4851
4852 }
4853 mutex_unlock(&mgr->payload_lock)rw_exit_write(&mgr->payload_lock);
4854
4855 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
4856 if (mgr->mst_primary) {
4857 u8 buf[DP_PAYLOAD_TABLE_SIZE64];
4858 int ret;
4859
4860 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV0x000, buf, DP_RECEIVER_CAP_SIZE0xf);
4861 if (ret) {
4862 seq_printf(m, "dpcd read failed\n");
4863 goto out;
4864 }
4865 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE0xf, buf);
4866
4867 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP0x020, buf, 2);
4868 if (ret) {
4869 seq_printf(m, "faux/mst read failed\n");
4870 goto out;
4871 }
4872 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
4873
4874 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL0x111, buf, 1);
4875 if (ret) {
4876 seq_printf(m, "mst ctrl read failed\n");
4877 goto out;
4878 }
4879 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
4880
4881 /* dump the standard OUI branch header */
4882 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI0x500, buf, DP_BRANCH_OUI_HEADER_SIZE0xc);
4883 if (ret) {
4884 seq_printf(m, "branch oui read failed\n");
4885 goto out;
4886 }
4887 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
4888
4889 for (i = 0x3; i < 0x8 && buf[i]; i++)
4890 seq_printf(m, "%c", buf[i]);
4891 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
4892 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
4893 if (dump_dp_payload_table(mgr, buf))
4894 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE64, buf);
4895 }
4896
4897out:
4898 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
4899
4900}
4901EXPORT_SYMBOL(drm_dp_mst_dump_topology);
4902
4903static void drm_dp_tx_work(struct work_struct *work)
4904{
4905 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work)({ const __typeof( ((struct drm_dp_mst_topology_mgr *)0)->
tx_work ) *__mptr = (work); (struct drm_dp_mst_topology_mgr *
)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_topology_mgr
, tx_work) );})
;
4906
4907 mutex_lock(&mgr->qlock)rw_enter_write(&mgr->qlock);
4908 if (!list_empty(&mgr->tx_msg_downq))
4909 process_single_down_tx_qlock(mgr);
4910 mutex_unlock(&mgr->qlock)rw_exit_write(&mgr->qlock);
4911}
4912
4913static inline void
4914drm_dp_delayed_destroy_port(struct drm_dp_mst_port *port)
4915{
4916 drm_dp_port_set_pdt(port, DP_PEER_DEVICE_NONE0x0, port->mcs);
4917
4918 if (port->connector) {
4919 drm_connector_unregister(port->connector);
4920 drm_connector_put(port->connector);
4921 }
4922
4923 drm_dp_mst_put_port_malloc(port);
4924}
4925
4926static inline void
4927drm_dp_delayed_destroy_mstb(struct drm_dp_mst_branch *mstb)
4928{
4929 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
4930 struct drm_dp_mst_port *port, *port_tmp;
4931 struct drm_dp_sideband_msg_tx *txmsg, *txmsg_tmp;
4932 bool_Bool wake_tx = false0;
4933
4934 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
4935 list_for_each_entry_safe(port, port_tmp, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}), port_tmp = ({ const __typeof( ((__typeof(*port) *)0)->
next ) *__mptr = (port->next.next); (__typeof(*port) *)( (
char *)__mptr - __builtin_offsetof(__typeof(*port), next) );}
); &port->next != (&mstb->ports); port = port_tmp
, port_tmp = ({ const __typeof( ((__typeof(*port_tmp) *)0)->
next ) *__mptr = (port_tmp->next.next); (__typeof(*port_tmp
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port_tmp)
, next) );}))
{
4936 list_del(&port->next);
4937 drm_dp_mst_topology_put_port(port);
4938 }
4939 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
4940
4941 /* drop any tx slot msg */
4942 mutex_lock(&mstb->mgr->qlock)rw_enter_write(&mstb->mgr->qlock);
4943 list_for_each_entry_safe(txmsg, txmsg_tmp, &mgr->tx_msg_downq, next)for (txmsg = ({ const __typeof( ((__typeof(*txmsg) *)0)->next
) *__mptr = ((&mgr->tx_msg_downq)->next); (__typeof
(*txmsg) *)( (char *)__mptr - __builtin_offsetof(__typeof(*txmsg
), next) );}), txmsg_tmp = ({ const __typeof( ((__typeof(*txmsg
) *)0)->next ) *__mptr = (txmsg->next.next); (__typeof(
*txmsg) *)( (char *)__mptr - __builtin_offsetof(__typeof(*txmsg
), next) );}); &txmsg->next != (&mgr->tx_msg_downq
); txmsg = txmsg_tmp, txmsg_tmp = ({ const __typeof( ((__typeof
(*txmsg_tmp) *)0)->next ) *__mptr = (txmsg_tmp->next.next
); (__typeof(*txmsg_tmp) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*txmsg_tmp), next) );}))
{
4944 if (txmsg->dst != mstb)
4945 continue;
4946
4947 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT4;
4948 list_del(&txmsg->next);
4949 wake_tx = true1;
4950 }
4951 mutex_unlock(&mstb->mgr->qlock)rw_exit_write(&mstb->mgr->qlock);
4952
4953 if (wake_tx)
4954 wake_up_all(&mstb->mgr->tx_waitq)wake_up(&mstb->mgr->tx_waitq);
4955
4956 drm_dp_mst_put_mstb_malloc(mstb);
4957}
4958
4959static void drm_dp_delayed_destroy_work(struct work_struct *work)
4960{
4961 struct drm_dp_mst_topology_mgr *mgr =
4962 container_of(work, struct drm_dp_mst_topology_mgr,({ const __typeof( ((struct drm_dp_mst_topology_mgr *)0)->
delayed_destroy_work ) *__mptr = (work); (struct drm_dp_mst_topology_mgr
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_topology_mgr
, delayed_destroy_work) );})
4963 delayed_destroy_work)({ const __typeof( ((struct drm_dp_mst_topology_mgr *)0)->
delayed_destroy_work ) *__mptr = (work); (struct drm_dp_mst_topology_mgr
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_topology_mgr
, delayed_destroy_work) );})
;
4964 bool_Bool send_hotplug = false0, go_again;
4965
4966 /*
4967 * Not a regular list traverse as we have to drop the destroy
4968 * connector lock before destroying the mstb/port, to avoid AB->BA
4969 * ordering between this lock and the config mutex.
4970 */
4971 do {
4972 go_again = false0;
4973
4974 for (;;) {
4975 struct drm_dp_mst_branch *mstb;
4976
4977 mutex_lock(&mgr->delayed_destroy_lock)rw_enter_write(&mgr->delayed_destroy_lock);
4978 mstb = list_first_entry_or_null(&mgr->destroy_branch_device_list,(list_empty(&mgr->destroy_branch_device_list) ? ((void
*)0) : ({ const __typeof( ((struct drm_dp_mst_branch *)0)->
destroy_next ) *__mptr = ((&mgr->destroy_branch_device_list
)->next); (struct drm_dp_mst_branch *)( (char *)__mptr - __builtin_offsetof
(struct drm_dp_mst_branch, destroy_next) );}))
4979 struct drm_dp_mst_branch,(list_empty(&mgr->destroy_branch_device_list) ? ((void
*)0) : ({ const __typeof( ((struct drm_dp_mst_branch *)0)->
destroy_next ) *__mptr = ((&mgr->destroy_branch_device_list
)->next); (struct drm_dp_mst_branch *)( (char *)__mptr - __builtin_offsetof
(struct drm_dp_mst_branch, destroy_next) );}))
4980 destroy_next)(list_empty(&mgr->destroy_branch_device_list) ? ((void
*)0) : ({ const __typeof( ((struct drm_dp_mst_branch *)0)->
destroy_next ) *__mptr = ((&mgr->destroy_branch_device_list
)->next); (struct drm_dp_mst_branch *)( (char *)__mptr - __builtin_offsetof
(struct drm_dp_mst_branch, destroy_next) );}))
;
4981 if (mstb)
4982 list_del(&mstb->destroy_next);
4983 mutex_unlock(&mgr->delayed_destroy_lock)rw_exit_write(&mgr->delayed_destroy_lock);
4984
4985 if (!mstb)
4986 break;
4987
4988 drm_dp_delayed_destroy_mstb(mstb);
4989 go_again = true1;
4990 }
4991
4992 for (;;) {
4993 struct drm_dp_mst_port *port;
4994
4995 mutex_lock(&mgr->delayed_destroy_lock)rw_enter_write(&mgr->delayed_destroy_lock);
4996 port = list_first_entry_or_null(&mgr->destroy_port_list,(list_empty(&mgr->destroy_port_list) ? ((void *)0) : (
{ const __typeof( ((struct drm_dp_mst_port *)0)->next ) *__mptr
= ((&mgr->destroy_port_list)->next); (struct drm_dp_mst_port
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_port
, next) );}))
4997 struct drm_dp_mst_port,(list_empty(&mgr->destroy_port_list) ? ((void *)0) : (
{ const __typeof( ((struct drm_dp_mst_port *)0)->next ) *__mptr
= ((&mgr->destroy_port_list)->next); (struct drm_dp_mst_port
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_port
, next) );}))
4998 next)(list_empty(&mgr->destroy_port_list) ? ((void *)0) : (
{ const __typeof( ((struct drm_dp_mst_port *)0)->next ) *__mptr
= ((&mgr->destroy_port_list)->next); (struct drm_dp_mst_port
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_port
, next) );}))
;
4999 if (port)
5000 list_del(&port->next);
5001 mutex_unlock(&mgr->delayed_destroy_lock)rw_exit_write(&mgr->delayed_destroy_lock);
5002
5003 if (!port)
5004 break;
5005
5006 drm_dp_delayed_destroy_port(port);
5007 send_hotplug = true1;
5008 go_again = true1;
5009 }
5010 } while (go_again);
5011
5012 if (send_hotplug)
5013 drm_kms_helper_hotplug_event(mgr->dev);
5014}
5015
5016static struct drm_private_state *
5017drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
5018{
5019 struct drm_dp_mst_topology_state *state, *old_state =
5020 to_dp_mst_topology_state(obj->state)({ const __typeof( ((struct drm_dp_mst_topology_state *)0)->
base ) *__mptr = (obj->state); (struct drm_dp_mst_topology_state
*)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_topology_state
, base) );})
;
5021 struct drm_dp_vcpi_allocation *pos, *vcpi;
5022
5023 state = kmemdup(old_state, sizeof(*state), GFP_KERNEL(0x0001 | 0x0004));
5024 if (!state)
5025 return NULL((void *)0);
5026
5027 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
5028
5029 INIT_LIST_HEAD(&state->vcpis);
5030
5031 list_for_each_entry(pos, &old_state->vcpis, next)for (pos = ({ const __typeof( ((__typeof(*pos) *)0)->next )
*__mptr = ((&old_state->vcpis)->next); (__typeof(*
pos) *)( (char *)__mptr - __builtin_offsetof(__typeof(*pos), next
) );}); &pos->next != (&old_state->vcpis); pos =
({ const __typeof( ((__typeof(*pos) *)0)->next ) *__mptr =
(pos->next.next); (__typeof(*pos) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*pos), next) );}))
{
5032 /* Prune leftover freed VCPI allocations */
5033 if (!pos->vcpi)
5034 continue;
5035
5036 vcpi = kmemdup(pos, sizeof(*vcpi), GFP_KERNEL(0x0001 | 0x0004));
5037 if (!vcpi)
5038 goto fail;
5039
5040 drm_dp_mst_get_port_malloc(vcpi->port);
5041 list_add(&vcpi->next, &state->vcpis);
5042 }
5043
5044 return &state->base;
5045
5046fail:
5047 list_for_each_entry_safe(pos, vcpi, &state->vcpis, next)for (pos = ({ const __typeof( ((__typeof(*pos) *)0)->next )
*__mptr = ((&state->vcpis)->next); (__typeof(*pos)
*)( (char *)__mptr - __builtin_offsetof(__typeof(*pos), next
) );}), vcpi = ({ const __typeof( ((__typeof(*pos) *)0)->next
) *__mptr = (pos->next.next); (__typeof(*pos) *)( (char *
)__mptr - __builtin_offsetof(__typeof(*pos), next) );}); &
pos->next != (&state->vcpis); pos = vcpi, vcpi = ({
const __typeof( ((__typeof(*vcpi) *)0)->next ) *__mptr = (
vcpi->next.next); (__typeof(*vcpi) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*vcpi), next) );}))
{
5048 drm_dp_mst_put_port_malloc(pos->port);
5049 kfree(pos);
5050 }
5051 kfree(state);
5052
5053 return NULL((void *)0);
5054}
5055
5056static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
5057 struct drm_private_state *state)
5058{
5059 struct drm_dp_mst_topology_state *mst_state =
5060 to_dp_mst_topology_state(state)({ const __typeof( ((struct drm_dp_mst_topology_state *)0)->
base ) *__mptr = (state); (struct drm_dp_mst_topology_state *
)( (char *)__mptr - __builtin_offsetof(struct drm_dp_mst_topology_state
, base) );})
;
5061 struct drm_dp_vcpi_allocation *pos, *tmp;
5062
5063 list_for_each_entry_safe(pos, tmp, &mst_state->vcpis, next)for (pos = ({ const __typeof( ((__typeof(*pos) *)0)->next )
*__mptr = ((&mst_state->vcpis)->next); (__typeof(*
pos) *)( (char *)__mptr - __builtin_offsetof(__typeof(*pos), next
) );}), tmp = ({ const __typeof( ((__typeof(*pos) *)0)->next
) *__mptr = (pos->next.next); (__typeof(*pos) *)( (char *
)__mptr - __builtin_offsetof(__typeof(*pos), next) );}); &
pos->next != (&mst_state->vcpis); pos = tmp, tmp = (
{ const __typeof( ((__typeof(*tmp) *)0)->next ) *__mptr = (
tmp->next.next); (__typeof(*tmp) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*tmp), next) );}))
{
5064 /* We only keep references to ports with non-zero VCPIs */
5065 if (pos->vcpi)
5066 drm_dp_mst_put_port_malloc(pos->port);
5067 kfree(pos);
5068 }
5069
5070 kfree(mst_state);
5071}
5072
5073static bool_Bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port,
5074 struct drm_dp_mst_branch *branch)
5075{
5076 while (port->parent) {
5077 if (port->parent == branch)
5078 return true1;
5079
5080 if (port->parent->port_parent)
5081 port = port->parent->port_parent;
5082 else
5083 break;
5084 }
5085 return false0;
5086}
5087
5088static int
5089drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
5090 struct drm_dp_mst_topology_state *state);
5091
5092static int
5093drm_dp_mst_atomic_check_mstb_bw_limit(struct drm_dp_mst_branch *mstb,
5094 struct drm_dp_mst_topology_state *state)
5095{
5096 struct drm_dp_vcpi_allocation *vcpi;
5097 struct drm_dp_mst_port *port;
5098 int pbn_used = 0, ret;
5099 bool_Bool found = false0;
5100
5101 /* Check that we have at least one port in our state that's downstream
5102 * of this branch, otherwise we can skip this branch
5103 */
5104 list_for_each_entry(vcpi, &state->vcpis, next)for (vcpi = ({ const __typeof( ((__typeof(*vcpi) *)0)->next
) *__mptr = ((&state->vcpis)->next); (__typeof(*vcpi
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*vcpi), next
) );}); &vcpi->next != (&state->vcpis); vcpi = (
{ const __typeof( ((__typeof(*vcpi) *)0)->next ) *__mptr =
(vcpi->next.next); (__typeof(*vcpi) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*vcpi), next) );}))
{
5105 if (!vcpi->pbn ||
5106 !drm_dp_mst_port_downstream_of_branch(vcpi->port, mstb))
5107 continue;
5108
5109 found = true1;
5110 break;
5111 }
5112 if (!found)
5113 return 0;
5114
5115 if (mstb->port_parent)
5116 DRM_DEBUG_ATOMIC("[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n",__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n"
, mstb->port_parent->parent, mstb->port_parent, mstb
)
5117 mstb->port_parent->parent, mstb->port_parent,__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n"
, mstb->port_parent->parent, mstb->port_parent, mstb
)
5118 mstb)__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n"
, mstb->port_parent->parent, mstb->port_parent, mstb
)
;
5119 else
5120 DRM_DEBUG_ATOMIC("[MSTB:%p] Checking bandwidth limits\n",__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] Checking bandwidth limits\n"
, mstb)
5121 mstb)__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] Checking bandwidth limits\n"
, mstb)
;
5122
5123 list_for_each_entry(port, &mstb->ports, next)for (port = ({ const __typeof( ((__typeof(*port) *)0)->next
) *__mptr = ((&mstb->ports)->next); (__typeof(*port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*port), next
) );}); &port->next != (&mstb->ports); port = (
{ const __typeof( ((__typeof(*port) *)0)->next ) *__mptr =
(port->next.next); (__typeof(*port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*port), next) );}))
{
5124 ret = drm_dp_mst_atomic_check_port_bw_limit(port, state);
5125 if (ret < 0)
5126 return ret;
5127
5128 pbn_used += ret;
5129 }
5130
5131 return pbn_used;
5132}
5133
5134static int
5135drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
5136 struct drm_dp_mst_topology_state *state)
5137{
5138 struct drm_dp_vcpi_allocation *vcpi;
5139 int pbn_used = 0;
5140
5141 if (port->pdt == DP_PEER_DEVICE_NONE0x0)
5142 return 0;
5143
5144 if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
5145 bool_Bool found = false0;
5146
5147 list_for_each_entry(vcpi, &state->vcpis, next)for (vcpi = ({ const __typeof( ((__typeof(*vcpi) *)0)->next
) *__mptr = ((&state->vcpis)->next); (__typeof(*vcpi
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*vcpi), next
) );}); &vcpi->next != (&state->vcpis); vcpi = (
{ const __typeof( ((__typeof(*vcpi) *)0)->next ) *__mptr =
(vcpi->next.next); (__typeof(*vcpi) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*vcpi), next) );}))
{
5148 if (vcpi->port != port)
5149 continue;
5150 if (!vcpi->pbn)
5151 return 0;
5152
5153 found = true1;
5154 break;
5155 }
5156 if (!found)
5157 return 0;
5158
5159 /* This should never happen, as it means we tried to
5160 * set a mode before querying the full_pbn
5161 */
5162 if (WARN_ON(!port->full_pbn)({ int __ret = !!(!port->full_pbn); if (__ret) printf("WARNING %s failed at %s:%d\n"
, "!port->full_pbn", "/usr/src/sys/dev/pci/drm/drm_dp_mst_topology.c"
, 5162); __builtin_expect(!!(__ret), 0); })
)
5163 return -EINVAL22;
5164
5165 pbn_used = vcpi->pbn;
5166 } else {
5167 pbn_used = drm_dp_mst_atomic_check_mstb_bw_limit(port->mstb,
5168 state);
5169 if (pbn_used <= 0)
5170 return pbn_used;
5171 }
5172
5173 if (pbn_used > port->full_pbn) {
5174 DRM_DEBUG_ATOMIC("[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n",__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n"
, port->parent, port, pbn_used, port->full_pbn)
5175 port->parent, port, pbn_used,__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n"
, port->parent, port, pbn_used, port->full_pbn)
5176 port->full_pbn)__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n"
, port->parent, port, pbn_used, port->full_pbn)
;
5177 return -ENOSPC28;
5178 }
5179
5180 DRM_DEBUG_ATOMIC("[MSTB:%p] [MST PORT:%p] uses %d out of %d PBN\n",__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] [MST PORT:%p] uses %d out of %d PBN\n"
, port->parent, port, pbn_used, port->full_pbn)
5181 port->parent, port, pbn_used, port->full_pbn)__drm_dbg(DRM_UT_ATOMIC, "[MSTB:%p] [MST PORT:%p] uses %d out of %d PBN\n"
, port->parent, port, pbn_used, port->full_pbn)
;
5182
5183 return pbn_used;
5184}
5185
5186static inline int
5187drm_dp_mst_atomic_check_vcpi_alloc_limit(struct drm_dp_mst_topology_mgr *mgr,
5188 struct drm_dp_mst_topology_state *mst_state)
5189{
5190 struct drm_dp_vcpi_allocation *vcpi;
5191 int avail_slots = 63, payload_count = 0;
5192
5193 list_for_each_entry(vcpi, &mst_state->vcpis, next)for (vcpi = ({ const __typeof( ((__typeof(*vcpi) *)0)->next
) *__mptr = ((&mst_state->vcpis)->next); (__typeof
(*vcpi) *)( (char *)__mptr - __builtin_offsetof(__typeof(*vcpi
), next) );}); &vcpi->next != (&mst_state->vcpis
); vcpi = ({ const __typeof( ((__typeof(*vcpi) *)0)->next )
*__mptr = (vcpi->next.next); (__typeof(*vcpi) *)( (char *
)__mptr - __builtin_offsetof(__typeof(*vcpi), next) );}))
{
5194 /* Releasing VCPI is always OK-even if the port is gone */
5195 if (!vcpi->vcpi) {
5196 DRM_DEBUG_ATOMIC("[MST PORT:%p] releases all VCPI slots\n",__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] releases all VCPI slots\n"
, vcpi->port)
5197 vcpi->port)__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] releases all VCPI slots\n"
, vcpi->port)
;
5198 continue;
5199 }
5200
5201 DRM_DEBUG_ATOMIC("[MST PORT:%p] requires %d vcpi slots\n",__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] requires %d vcpi slots\n"
, vcpi->port, vcpi->vcpi)
5202 vcpi->port, vcpi->vcpi)__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] requires %d vcpi slots\n"
, vcpi->port, vcpi->vcpi)
;
5203
5204 avail_slots -= vcpi->vcpi;
5205 if (avail_slots < 0) {
5206 DRM_DEBUG_ATOMIC("[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n",__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n"
, vcpi->port, mst_state, avail_slots + vcpi->vcpi)
5207 vcpi->port, mst_state,__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n"
, vcpi->port, mst_state, avail_slots + vcpi->vcpi)
5208 avail_slots + vcpi->vcpi)__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n"
, vcpi->port, mst_state, avail_slots + vcpi->vcpi)
;
5209 return -ENOSPC28;
5210 }
5211
5212 if (++payload_count > mgr->max_payloads) {
5213 DRM_DEBUG_ATOMIC("[MST MGR:%p] state %p has too many payloads (max=%d)\n",__drm_dbg(DRM_UT_ATOMIC, "[MST MGR:%p] state %p has too many payloads (max=%d)\n"
, mgr, mst_state, mgr->max_payloads)
5214 mgr, mst_state, mgr->max_payloads)__drm_dbg(DRM_UT_ATOMIC, "[MST MGR:%p] state %p has too many payloads (max=%d)\n"
, mgr, mst_state, mgr->max_payloads)
;
5215 return -EINVAL22;
5216 }
5217 }
5218 DRM_DEBUG_ATOMIC("[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n",__drm_dbg(DRM_UT_ATOMIC, "[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n"
, mgr, mst_state, avail_slots, 63 - avail_slots)
5219 mgr, mst_state, avail_slots,__drm_dbg(DRM_UT_ATOMIC, "[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n"
, mgr, mst_state, avail_slots, 63 - avail_slots)
5220 63 - avail_slots)__drm_dbg(DRM_UT_ATOMIC, "[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n"
, mgr, mst_state, avail_slots, 63 - avail_slots)
;
5221
5222 return 0;
5223}
5224
5225/**
5226 * drm_dp_mst_add_affected_dsc_crtcs
5227 * @state: Pointer to the new struct drm_dp_mst_topology_state
5228 * @mgr: MST topology manager
5229 *
5230 * Whenever there is a change in mst topology
5231 * DSC configuration would have to be recalculated
5232 * therefore we need to trigger modeset on all affected
5233 * CRTCs in that topology
5234 *
5235 * See also:
5236 * drm_dp_mst_atomic_enable_dsc()
5237 */
5238int drm_dp_mst_add_affected_dsc_crtcs(struct drm_atomic_state *state, struct drm_dp_mst_topology_mgr *mgr)
5239{
5240 struct drm_dp_mst_topology_state *mst_state;
5241 struct drm_dp_vcpi_allocation *pos;
5242 struct drm_connector *connector;
5243 struct drm_connector_state *conn_state;
5244 struct drm_crtc *crtc;
5245 struct drm_crtc_state *crtc_state;
5246
5247 mst_state = drm_atomic_get_mst_topology_state(state, mgr);
5248
5249 if (IS_ERR(mst_state))
5250 return -EINVAL22;
5251
5252 list_for_each_entry(pos, &mst_state->vcpis, next)for (pos = ({ const __typeof( ((__typeof(*pos) *)0)->next )
*__mptr = ((&mst_state->vcpis)->next); (__typeof(*
pos) *)( (char *)__mptr - __builtin_offsetof(__typeof(*pos), next
) );}); &pos->next != (&mst_state->vcpis); pos =
({ const __typeof( ((__typeof(*pos) *)0)->next ) *__mptr =
(pos->next.next); (__typeof(*pos) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*pos), next) );}))
{
5253
5254 connector = pos->port->connector;
5255
5256 if (!connector)
5257 return -EINVAL22;
5258
5259 conn_state = drm_atomic_get_connector_state(state, connector);
5260
5261 if (IS_ERR(conn_state))
5262 return PTR_ERR(conn_state);
5263
5264 crtc = conn_state->crtc;
5265
5266 if (!crtc)
5267 continue;
5268
5269 if (!drm_dp_mst_dsc_aux_for_port(pos->port))
5270 continue;
5271
5272 crtc_state = drm_atomic_get_crtc_state(mst_state->base.state, crtc);
5273
5274 if (IS_ERR(crtc_state))
5275 return PTR_ERR(crtc_state);
5276
5277 DRM_DEBUG_ATOMIC("[MST MGR:%p] Setting mode_changed flag on CRTC %p\n",__drm_dbg(DRM_UT_ATOMIC, "[MST MGR:%p] Setting mode_changed flag on CRTC %p\n"
, mgr, crtc)
5278 mgr, crtc)__drm_dbg(DRM_UT_ATOMIC, "[MST MGR:%p] Setting mode_changed flag on CRTC %p\n"
, mgr, crtc)
;
5279
5280 crtc_state->mode_changed = true1;
5281 }
5282 return 0;
5283}
5284EXPORT_SYMBOL(drm_dp_mst_add_affected_dsc_crtcs);
5285
5286/**
5287 * drm_dp_mst_atomic_enable_dsc - Set DSC Enable Flag to On/Off
5288 * @state: Pointer to the new drm_atomic_state
5289 * @port: Pointer to the affected MST Port
5290 * @pbn: Newly recalculated bw required for link with DSC enabled
5291 * @pbn_div: Divider to calculate correct number of pbn per slot
5292 * @enable: Boolean flag to enable or disable DSC on the port
5293 *
5294 * This function enables DSC on the given Port
5295 * by recalculating its vcpi from pbn provided
5296 * and sets dsc_enable flag to keep track of which
5297 * ports have DSC enabled
5298 *
5299 */
5300int drm_dp_mst_atomic_enable_dsc(struct drm_atomic_state *state,
5301 struct drm_dp_mst_port *port,
5302 int pbn, int pbn_div,
5303 bool_Bool enable)
5304{
5305 struct drm_dp_mst_topology_state *mst_state;
5306 struct drm_dp_vcpi_allocation *pos;
5307 bool_Bool found = false0;
5308 int vcpi = 0;
5309
5310 mst_state = drm_atomic_get_mst_topology_state(state, port->mgr);
5311
5312 if (IS_ERR(mst_state))
5313 return PTR_ERR(mst_state);
5314
5315 list_for_each_entry(pos, &mst_state->vcpis, next)for (pos = ({ const __typeof( ((__typeof(*pos) *)0)->next )
*__mptr = ((&mst_state->vcpis)->next); (__typeof(*
pos) *)( (char *)__mptr - __builtin_offsetof(__typeof(*pos), next
) );}); &pos->next != (&mst_state->vcpis); pos =
({ const __typeof( ((__typeof(*pos) *)0)->next ) *__mptr =
(pos->next.next); (__typeof(*pos) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*pos), next) );}))
{
5316 if (pos->port == port) {
5317 found = true1;
5318 break;
5319 }
5320 }
5321
5322 if (!found) {
5323 DRM_DEBUG_ATOMIC("[MST PORT:%p] Couldn't find VCPI allocation in mst state %p\n",__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] Couldn't find VCPI allocation in mst state %p\n"
, port, mst_state)
5324 port, mst_state)__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] Couldn't find VCPI allocation in mst state %p\n"
, port, mst_state)
;
5325 return -EINVAL22;
5326 }
5327
5328 if (pos->dsc_enabled == enable) {
5329 DRM_DEBUG_ATOMIC("[MST PORT:%p] DSC flag is already set to %d, returning %d VCPI slots\n",__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] DSC flag is already set to %d, returning %d VCPI slots\n"
, port, enable, pos->vcpi)
5330 port, enable, pos->vcpi)__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] DSC flag is already set to %d, returning %d VCPI slots\n"
, port, enable, pos->vcpi)
;
5331 vcpi = pos->vcpi;
5332 }
5333
5334 if (enable) {
5335 vcpi = drm_dp_atomic_find_vcpi_slots(state, port->mgr, port, pbn, pbn_div);
5336 DRM_DEBUG_ATOMIC("[MST PORT:%p] Enabling DSC flag, reallocating %d VCPI slots on the port\n",__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] Enabling DSC flag, reallocating %d VCPI slots on the port\n"
, port, vcpi)
5337 port, vcpi)__drm_dbg(DRM_UT_ATOMIC, "[MST PORT:%p] Enabling DSC flag, reallocating %d VCPI slots on the port\n"
, port, vcpi)
;
5338 if (vcpi < 0)
5339 return -EINVAL22;
5340 }
5341
5342 pos->dsc_enabled = enable;
5343
5344 return vcpi;
5345}
5346EXPORT_SYMBOL(drm_dp_mst_atomic_enable_dsc);
5347/**
5348 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
5349 * atomic update is valid
5350 * @state: Pointer to the new &struct drm_dp_mst_topology_state
5351 *
5352 * Checks the given topology state for an atomic update to ensure that it's
5353 * valid. This includes checking whether there's enough bandwidth to support
5354 * the new VCPI allocations in the atomic update.
5355 *
5356 * Any atomic drivers supporting DP MST must make sure to call this after
5357 * checking the rest of their state in their
5358 * &drm_mode_config_funcs.atomic_check() callback.
5359 *
5360 * See also:
5361 * drm_dp_atomic_find_vcpi_slots()
5362 * drm_dp_atomic_release_vcpi_slots()
5363 *
5364 * Returns:
5365 *
5366 * 0 if the new state is valid, negative error code otherwise.
5367 */
5368int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
5369{
5370 struct drm_dp_mst_topology_mgr *mgr;
5371 struct drm_dp_mst_topology_state *mst_state;
5372 int i, ret = 0;
5373
5374 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i)for ((i) = 0; (i) < (state)->num_private_objs; (i)++) if
(!(__drm_dp_mst_state_iter_get((state), &(mgr), ((void *
)0), &(mst_state), (i)))) {} else
{
5375 if (!mgr->mst_state)
5376 continue;
5377
5378 ret = drm_dp_mst_atomic_check_vcpi_alloc_limit(mgr, mst_state);
5379 if (ret)
5380 break;
5381
5382 mutex_lock(&mgr->lock)rw_enter_write(&mgr->lock);
5383 ret = drm_dp_mst_atomic_check_mstb_bw_limit(mgr->mst_primary,
5384 mst_state);
5385 mutex_unlock(&mgr->lock)rw_exit_write(&mgr->lock);
5386 if (ret < 0)
5387 break;
5388 else
5389 ret = 0;
5390 }
5391
5392 return ret;
5393}
5394EXPORT_SYMBOL(drm_dp_mst_atomic_check);
5395
5396const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
5397 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
5398 .atomic_destroy_state = drm_dp_mst_destroy_state,
5399};
5400EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
5401
5402/**
5403 * drm_atomic_get_mst_topology_state: get MST topology state
5404 *
5405 * @state: global atomic state
5406 * @mgr: MST topology manager, also the private object in this case
5407 *
5408 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
5409 * state vtable so that the private object state returned is that of a MST
5410 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
5411 * to care of the locking, so warn if don't hold the connection_mutex.
5412 *
5413 * RETURNS:
5414 *
5415 * The MST topology state or error pointer.
5416 */
5417struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
5418 struct drm_dp_mst_topology_mgr *mgr)
5419{
5420 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base))({ const __typeof( ((struct drm_dp_mst_topology_state *)0)->
base ) *__mptr = (drm_atomic_get_private_obj_state(state, &
mgr->base)); (struct drm_dp_mst_topology_state *)( (char *
)__mptr - __builtin_offsetof(struct drm_dp_mst_topology_state
, base) );})
;
5421}
5422EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
5423
5424/**
5425 * drm_dp_mst_topology_mgr_init - initialise a topology manager
5426 * @mgr: manager struct to initialise
5427 * @dev: device providing this structure - for i2c addition.
5428 * @aux: DP helper aux channel to talk to this device
5429 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
5430 * @max_payloads: maximum number of payloads this GPU can source
5431 * @conn_base_id: the connector object ID the MST device is connected to.
5432 *
5433 * Return 0 for success, or negative error code on failure
5434 */
5435int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
5436 struct drm_device *dev, struct drm_dp_aux *aux,
5437 int max_dpcd_transaction_bytes,
5438 int max_payloads, int conn_base_id)
5439{
5440 struct drm_dp_mst_topology_state *mst_state;
5441
5442 rw_init(&mgr->lock, "mst")_rw_init_flags(&mgr->lock, "mst", 0, ((void *)0));
5443 rw_init(&mgr->qlock, "mstq")_rw_init_flags(&mgr->qlock, "mstq", 0, ((void *)0));
5444 rw_init(&mgr->payload_lock, "mstpl")_rw_init_flags(&mgr->payload_lock, "mstpl", 0, ((void *
)0))
;
5445 rw_init(&mgr->delayed_destroy_lock, "mstdc")_rw_init_flags(&mgr->delayed_destroy_lock, "mstdc", 0,
((void *)0))
;
5446 rw_init(&mgr->up_req_lock, "mstup")_rw_init_flags(&mgr->up_req_lock, "mstup", 0, ((void *
)0))
;
5447 rw_init(&mgr->probe_lock, "mstprb")_rw_init_flags(&mgr->probe_lock, "mstprb", 0, ((void *
)0))
;
5448#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)0
5449 rw_init(&mgr->topology_ref_history_lock, "mstref")_rw_init_flags(&mgr->topology_ref_history_lock, "mstref"
, 0, ((void *)0))
;
5450#endif
5451 INIT_LIST_HEAD(&mgr->tx_msg_downq);
5452 INIT_LIST_HEAD(&mgr->destroy_port_list);
5453 INIT_LIST_HEAD(&mgr->destroy_branch_device_list);
5454 INIT_LIST_HEAD(&mgr->up_req_list);
5455
5456 /*
5457 * delayed_destroy_work will be queued on a dedicated WQ, so that any
5458 * requeuing will be also flushed when deiniting the topology manager.
5459 */
5460 mgr->delayed_destroy_wq = alloc_ordered_workqueue("drm_dp_mst_wq", 0);
5461 if (mgr->delayed_destroy_wq == NULL((void *)0))
5462 return -ENOMEM12;
5463
5464 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
5465 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
5466 INIT_WORK(&mgr->delayed_destroy_work, drm_dp_delayed_destroy_work);
5467 INIT_WORK(&mgr->up_req_work, drm_dp_mst_up_req_work);
5468 init_waitqueue_head(&mgr->tx_waitq);
5469 mgr->dev = dev;
5470 mgr->aux = aux;
5471 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
5472 mgr->max_payloads = max_payloads;
5473 mgr->conn_base_id = conn_base_id;
5474 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
5475 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
5476 return -EINVAL22;
5477 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL(0x0001 | 0x0004));
5478 if (!mgr->payloads)
5479 return -ENOMEM12;
5480 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL(0x0001 | 0x0004));
5481 if (!mgr->proposed_vcpis)
5482 return -ENOMEM12;
5483 set_bit(0, &mgr->payload_mask);
5484
5485 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL(0x0001 | 0x0004));
5486 if (mst_state == NULL((void *)0))
5487 return -ENOMEM12;
5488
5489 mst_state->mgr = mgr;
5490 INIT_LIST_HEAD(&mst_state->vcpis);
5491
5492 drm_atomic_private_obj_init(dev, &mgr->base,
5493 &mst_state->base,
5494 &drm_dp_mst_topology_state_funcs);
5495
5496 return 0;
5497}
5498EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
5499
5500/**
5501 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
5502 * @mgr: manager to destroy
5503 */
5504void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
5505{
5506 drm_dp_mst_topology_mgr_set_mst(mgr, false0);
5507 flush_work(&mgr->work);
5508 /* The following will also drain any requeued work on the WQ. */
5509 if (mgr->delayed_destroy_wq) {
5510 destroy_workqueue(mgr->delayed_destroy_wq);
5511 mgr->delayed_destroy_wq = NULL((void *)0);
5512 }
5513 mutex_lock(&mgr->payload_lock)rw_enter_write(&mgr->payload_lock);
5514 kfree(mgr->payloads);
5515 mgr->payloads = NULL((void *)0);
5516 kfree(mgr->proposed_vcpis);
5517 mgr->proposed_vcpis = NULL((void *)0);
5518 mutex_unlock(&mgr->payload_lock)rw_exit_write(&mgr->payload_lock);
5519 mgr->dev = NULL((void *)0);
5520 mgr->aux = NULL((void *)0);
5521 drm_atomic_private_obj_fini(&mgr->base);
5522 mgr->funcs = NULL((void *)0);
5523
5524 mutex_destroy(&mgr->delayed_destroy_lock);
5525 mutex_destroy(&mgr->payload_lock);
5526 mutex_destroy(&mgr->qlock);
5527 mutex_destroy(&mgr->lock);
5528 mutex_destroy(&mgr->up_req_lock);
5529 mutex_destroy(&mgr->probe_lock);
5530#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)0
5531 mutex_destroy(&mgr->topology_ref_history_lock);
5532#endif
5533}
5534EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
5535
5536static bool_Bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
5537{
5538 int i;
5539
5540 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS4)
5541 return false0;
5542
5543 for (i = 0; i < num - 1; i++) {
5544 if (msgs[i].flags & I2C_M_RD0x0001 ||
5545 msgs[i].len > 0xff)
5546 return false0;
5547 }
5548
5549 return msgs[num - 1].flags & I2C_M_RD0x0001 &&
5550 msgs[num - 1].len <= 0xff;
5551}
5552
5553static bool_Bool remote_i2c_write_ok(const struct i2c_msg msgs[], int num)
5554{
5555 int i;
5556
5557 for (i = 0; i < num - 1; i++) {
5558 if (msgs[i].flags & I2C_M_RD0x0001 || !(msgs[i].flags & I2C_M_STOP0x0004) ||
5559 msgs[i].len > 0xff)
5560 return false0;
5561 }
5562
5563 return !(msgs[num - 1].flags & I2C_M_RD0x0001) && msgs[num - 1].len <= 0xff;
5564}
5565
5566static int drm_dp_mst_i2c_read(struct drm_dp_mst_branch *mstb,
5567 struct drm_dp_mst_port *port,
5568 struct i2c_msg *msgs, int num)
5569{
5570 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
5571 unsigned int i;
5572 struct drm_dp_sideband_msg_req_body msg;
5573 struct drm_dp_sideband_msg_tx *txmsg = NULL((void *)0);
5574 int ret;
5575
5576 memset(&msg, 0, sizeof(msg))__builtin_memset((&msg), (0), (sizeof(msg)));
5577 msg.req_type = DP_REMOTE_I2C_READ0x22;
5578 msg.u.i2c_read.num_transactions = num - 1;
5579 msg.u.i2c_read.port_number = port->port_num;
5580 for (i = 0; i < num - 1; i++) {
5581 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
5582 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
5583 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
5584 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP0x0004);
5585 }
5586 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
5587 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
5588
5589 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
5590 if (!txmsg) {
5591 ret = -ENOMEM12;
5592 goto out;
5593 }
5594
5595 txmsg->dst = mstb;
5596 drm_dp_encode_sideband_req(&msg, txmsg);
5597
5598 drm_dp_queue_down_tx(mgr, txmsg);
5599
5600 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
5601 if (ret > 0) {
5602
5603 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01) {
5604 ret = -EREMOTEIO5;
5605 goto out;
5606 }
5607 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
5608 ret = -EIO5;
5609 goto out;
5610 }
5611 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len)__builtin_memcpy((msgs[num - 1].buf), (txmsg->reply.u.remote_i2c_read_ack
.bytes), (msgs[num - 1].len))
;
5612 ret = num;
5613 }
5614out:
5615 kfree(txmsg);
5616 return ret;
5617}
5618
5619static int drm_dp_mst_i2c_write(struct drm_dp_mst_branch *mstb,
5620 struct drm_dp_mst_port *port,
5621 struct i2c_msg *msgs, int num)
5622{
5623 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
5624 unsigned int i;
5625 struct drm_dp_sideband_msg_req_body msg;
5626 struct drm_dp_sideband_msg_tx *txmsg = NULL((void *)0);
5627 int ret;
5628
5629 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL(0x0001 | 0x0004));
5630 if (!txmsg) {
5631 ret = -ENOMEM12;
5632 goto out;
5633 }
5634 for (i = 0; i < num; i++) {
5635 memset(&msg, 0, sizeof(msg))__builtin_memset((&msg), (0), (sizeof(msg)));
5636 msg.req_type = DP_REMOTE_I2C_WRITE0x23;
5637 msg.u.i2c_write.port_number = port->port_num;
5638 msg.u.i2c_write.write_i2c_device_id = msgs[i].addr;
5639 msg.u.i2c_write.num_bytes = msgs[i].len;
5640 msg.u.i2c_write.bytes = msgs[i].buf;
5641
5642 memset(txmsg, 0, sizeof(*txmsg))__builtin_memset((txmsg), (0), (sizeof(*txmsg)));
5643 txmsg->dst = mstb;
5644
5645 drm_dp_encode_sideband_req(&msg, txmsg);
5646 drm_dp_queue_down_tx(mgr, txmsg);
5647
5648 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
5649 if (ret > 0) {
5650 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK0x01) {
5651 ret = -EREMOTEIO5;
5652 goto out;
5653 }
5654 } else {
5655 goto out;
5656 }
5657 }
5658 ret = num;
5659out:
5660 kfree(txmsg);
5661 return ret;
5662}
5663
5664/* I2C device */
5665static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter,
5666 struct i2c_msg *msgs, int num)
5667{
5668 struct drm_dp_aux *aux = adapter->algo_data;
5669 struct drm_dp_mst_port *port =
5670 container_of(aux, struct drm_dp_mst_port, aux)({ const __typeof( ((struct drm_dp_mst_port *)0)->aux ) *__mptr
= (aux); (struct drm_dp_mst_port *)( (char *)__mptr - __builtin_offsetof
(struct drm_dp_mst_port, aux) );})
;
5671 struct drm_dp_mst_branch *mstb;
5672 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
5673 int ret;
5674
5675 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
5676 if (!mstb)
5677 return -EREMOTEIO5;
5678
5679 if (remote_i2c_read_ok(msgs, num)) {
5680 ret = drm_dp_mst_i2c_read(mstb, port, msgs, num);
5681 } else if (remote_i2c_write_ok(msgs, num)) {
5682 ret = drm_dp_mst_i2c_write(mstb, port, msgs, num);
5683 } else {
5684 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n")__drm_dbg(DRM_UT_KMS, "Unsupported I2C transaction for MST device\n"
)
;
5685 ret = -EIO5;
5686 }
5687
5688 drm_dp_mst_topology_put_mstb(mstb);
5689 return ret;
5690}
5691
5692static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
5693{
5694 return I2C_FUNC_I2C0 | I2C_FUNC_SMBUS_EMUL0 |
5695 I2C_FUNC_SMBUS_READ_BLOCK_DATA0 |
5696 I2C_FUNC_SMBUS_BLOCK_PROC_CALL0 |
5697 I2C_FUNC_10BIT_ADDR0;
5698}
5699
5700static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
5701 .functionality = drm_dp_mst_i2c_functionality,
5702 .master_xfer = drm_dp_mst_i2c_xfer,
5703};
5704
5705/**
5706 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
5707 * @port: The port to add the I2C bus on
5708 *
5709 * Returns 0 on success or a negative error code on failure.
5710 */
5711static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port *port)
5712{
5713 struct drm_dp_aux *aux = &port->aux;
5714#ifdef __linux__
5715 struct device *parent_dev = port->mgr->dev->dev;
5716#endif
5717
5718 aux->ddc.algo = &drm_dp_mst_i2c_algo;
5719 aux->ddc.algo_data = aux;
5720 aux->ddc.retries = 3;
5721
5722#ifdef __linux__
5723 aux->ddc.class = I2C_CLASS_DDC;
5724 aux->ddc.owner = THIS_MODULE((void *)0);
5725 /* FIXME: set the kdev of the port's connector as parent */
5726 aux->ddc.dev.parent = parent_dev;
5727 aux->ddc.dev.of_node = parent_dev->of_node;
5728#endif
5729
5730 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(parent_dev)"",
5731 sizeof(aux->ddc.name));
5732
5733 return i2c_add_adapter(&aux->ddc)0;
5734}
5735
5736/**
5737 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
5738 * @port: The port to remove the I2C bus from
5739 */
5740static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port *port)
5741{
5742 i2c_del_adapter(&port->aux.ddc);
5743}
5744
5745/**
5746 * drm_dp_mst_is_virtual_dpcd() - Is the given port a virtual DP Peer Device
5747 * @port: The port to check
5748 *
5749 * A single physical MST hub object can be represented in the topology
5750 * by multiple branches, with virtual ports between those branches.
5751 *
5752 * As of DP1.4, An MST hub with internal (virtual) ports must expose
5753 * certain DPCD registers over those ports. See sections 2.6.1.1.1
5754 * and 2.6.1.1.2 of Display Port specification v1.4 for details.
5755 *
5756 * May acquire mgr->lock
5757 *
5758 * Returns:
5759 * true if the port is a virtual DP peer device, false otherwise
5760 */
5761static bool_Bool drm_dp_mst_is_virtual_dpcd(struct drm_dp_mst_port *port)
5762{
5763 struct drm_dp_mst_port *downstream_port;
5764
5765 if (!port || port->dpcd_rev < DP_DPCD_REV_140x14)
5766 return false0;
5767
5768 /* Virtual DP Sink (Internal Display Panel) */
5769 if (port->port_num >= 8)
5770 return true1;
5771
5772 /* DP-to-HDMI Protocol Converter */
5773 if (port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV0x4 &&
5774 !port->mcs &&
5775 port->ldps)
5776 return true1;
5777
5778 /* DP-to-DP */
5779 mutex_lock(&port->mgr->lock)rw_enter_write(&port->mgr->lock);
5780 if (port->pdt == DP_PEER_DEVICE_MST_BRANCHING0x2 &&
5781 port->mstb &&
5782 port->mstb->num_ports == 2) {
5783 list_for_each_entry(downstream_port, &port->mstb->ports, next)for (downstream_port = ({ const __typeof( ((__typeof(*downstream_port
) *)0)->next ) *__mptr = ((&port->mstb->ports)->
next); (__typeof(*downstream_port) *)( (char *)__mptr - __builtin_offsetof
(__typeof(*downstream_port), next) );}); &downstream_port
->next != (&port->mstb->ports); downstream_port =
({ const __typeof( ((__typeof(*downstream_port) *)0)->next
) *__mptr = (downstream_port->next.next); (__typeof(*downstream_port
) *)( (char *)__mptr - __builtin_offsetof(__typeof(*downstream_port
), next) );}))
{
5784 if (downstream_port->pdt == DP_PEER_DEVICE_SST_SINK0x3 &&
5785 !downstream_port->input) {
5786 mutex_unlock(&port->mgr->lock)rw_exit_write(&port->mgr->lock);
5787 return true1;
5788 }
5789 }
5790 }
5791 mutex_unlock(&port->mgr->lock)rw_exit_write(&port->mgr->lock);
5792
5793 return false0;
5794}
5795
5796/**
5797 * drm_dp_mst_dsc_aux_for_port() - Find the correct aux for DSC
5798 * @port: The port to check. A leaf of the MST tree with an attached display.
5799 *
5800 * Depending on the situation, DSC may be enabled via the endpoint aux,
5801 * the immediately upstream aux, or the connector's physical aux.
5802 *
5803 * This is both the correct aux to read DSC_CAPABILITY and the
5804 * correct aux to write DSC_ENABLED.
5805 *
5806 * This operation can be expensive (up to four aux reads), so
5807 * the caller should cache the return.
5808 *
5809 * Returns:
5810 * NULL if DSC cannot be enabled on this port, otherwise the aux device
5811 */
5812struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port)
5813{
5814 struct drm_dp_mst_port *immediate_upstream_port;
5815 struct drm_dp_mst_port *fec_port;
5816 struct drm_dp_desc desc = {};
5817 u8 endpoint_fec;
5818 u8 endpoint_dsc;
5819
5820 if (!port)
5821 return NULL((void *)0);
5822
5823 if (port->parent->port_parent)
5824 immediate_upstream_port = port->parent->port_parent;
5825 else
5826 immediate_upstream_port = NULL((void *)0);
5827
5828 fec_port = immediate_upstream_port;
5829 while (fec_port) {
5830 /*
5831 * Each physical link (i.e. not a virtual port) between the
5832 * output and the primary device must support FEC
5833 */
5834 if (!drm_dp_mst_is_virtual_dpcd(fec_port) &&
5835 !fec_port->fec_capable)
5836 return NULL((void *)0);
5837
5838 fec_port = fec_port->parent->port_parent;
5839 }
5840
5841 /* DP-to-DP peer device */
5842 if (drm_dp_mst_is_virtual_dpcd(immediate_upstream_port)) {
5843 u8 upstream_dsc;
5844
5845 if (drm_dp_dpcd_read(&port->aux,
5846 DP_DSC_SUPPORT0x060, &endpoint_dsc, 1) != 1)
5847 return NULL((void *)0);
5848 if (drm_dp_dpcd_read(&port->aux,
5849 DP_FEC_CAPABILITY0x090, &endpoint_fec, 1) != 1)
5850 return NULL((void *)0);
5851 if (drm_dp_dpcd_read(&immediate_upstream_port->aux,
5852 DP_DSC_SUPPORT0x060, &upstream_dsc, 1) != 1)
5853 return NULL((void *)0);
5854
5855 /* Enpoint decompression with DP-to-DP peer device */
5856 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED(1 << 0)) &&
5857 (endpoint_fec & DP_FEC_CAPABLE(1 << 0)) &&
5858 (upstream_dsc & 0x2) /* DSC passthrough */)
5859 return &port->aux;
5860
5861 /* Virtual DPCD decompression with DP-to-DP peer device */
5862 return &immediate_upstream_port->aux;
5863 }
5864
5865 /* Virtual DPCD decompression with DP-to-HDMI or Virtual DP Sink */
5866 if (drm_dp_mst_is_virtual_dpcd(port))
5867 return &port->aux;
5868
5869 /*
5870 * Synaptics quirk
5871 * Applies to ports for which:
5872 * - Physical aux has Synaptics OUI
5873 * - DPv1.4 or higher
5874 * - Port is on primary branch device
5875 * - Not a VGA adapter (DP_DWN_STRM_PORT_TYPE_ANALOG)
5876 */
5877 if (drm_dp_read_desc(port->mgr->aux, &desc, true1))
5878 return NULL((void *)0);
5879
5880 if (drm_dp_has_quirk(&desc, 0,
5881 DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) &&
5882 port->mgr->dpcd[DP_DPCD_REV0x000] >= DP_DPCD_REV_140x14 &&
5883 port->parent == port->mgr->mst_primary) {
5884 u8 downstreamport;
5885
5886 if (drm_dp_dpcd_read(&port->aux, DP_DOWNSTREAMPORT_PRESENT0x005,
5887 &downstreamport, 1) < 0)
5888 return NULL((void *)0);
5889
5890 if ((downstreamport & DP_DWN_STRM_PORT_PRESENT(1 << 0)) &&
5891 ((downstreamport & DP_DWN_STRM_PORT_TYPE_MASK0x06)
5892 != DP_DWN_STRM_PORT_TYPE_ANALOG(1 << 1)))
5893 return port->mgr->aux;
5894 }
5895
5896 /*
5897 * The check below verifies if the MST sink
5898 * connected to the GPU is capable of DSC -
5899 * therefore the endpoint needs to be
5900 * both DSC and FEC capable.
5901 */
5902 if (drm_dp_dpcd_read(&port->aux,
5903 DP_DSC_SUPPORT0x060, &endpoint_dsc, 1) != 1)
5904 return NULL((void *)0);
5905 if (drm_dp_dpcd_read(&port->aux,
5906 DP_FEC_CAPABILITY0x090, &endpoint_fec, 1) != 1)
5907 return NULL((void *)0);
5908 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED(1 << 0)) &&
5909 (endpoint_fec & DP_FEC_CAPABLE(1 << 0)))
5910 return &port->aux;
5911
5912 return NULL((void *)0);
5913}
5914EXPORT_SYMBOL(drm_dp_mst_dsc_aux_for_port);