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