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 (!mgr->mst_state || !mgr->mst_primary) { 3744 drm_dbg_kms(mgr->dev, "queue_probe skipped: topology torn down\n"); 3745 goto out_unlock; 3746 } 3747 3748 drm_dp_mst_topology_mgr_invalidate_mstb(mgr->mst_primary); 3749 drm_dp_mst_queue_probe_work(mgr); 3750 3751 out_unlock: 3752 mutex_unlock(&mgr->lock); 3753 } 3754 EXPORT_SYMBOL(drm_dp_mst_topology_queue_probe); 3755 3756 /** 3757 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager 3758 * @mgr: manager to suspend 3759 * 3760 * This function tells the MST device that we can't handle UP messages 3761 * anymore. This should stop it from sending any since we are suspended. 3762 */ 3763 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr) 3764 { 3765 mutex_lock(&mgr->lock); 3766 drm_dp_dpcd_write_byte(mgr->aux, DP_MSTM_CTRL, 3767 DP_MST_EN | DP_UPSTREAM_IS_SRC); 3768 mutex_unlock(&mgr->lock); 3769 flush_work(&mgr->up_req_work); 3770 flush_work(&mgr->work); 3771 flush_work(&mgr->delayed_destroy_work); 3772 3773 mutex_lock(&mgr->lock); 3774 if (mgr->mst_state && mgr->mst_primary) 3775 drm_dp_mst_topology_mgr_invalidate_mstb(mgr->mst_primary); 3776 mutex_unlock(&mgr->lock); 3777 } 3778 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend); 3779 3780 /** 3781 * drm_dp_mst_topology_mgr_resume() - resume the MST manager 3782 * @mgr: manager to resume 3783 * @sync: whether or not to perform topology reprobing synchronously 3784 * 3785 * This will fetch DPCD and see if the device is still there, 3786 * if it is, it will rewrite the MSTM control bits, and return. 3787 * 3788 * If the device fails this returns -1, and the driver should do 3789 * a full MST reprobe, in case we were undocked. 3790 * 3791 * During system resume (where it is assumed that the driver will be calling 3792 * drm_atomic_helper_resume()) this function should be called beforehand with 3793 * @sync set to true. In contexts like runtime resume where the driver is not 3794 * expected to be calling drm_atomic_helper_resume(), this function should be 3795 * called with @sync set to false in order to avoid deadlocking. 3796 * 3797 * Returns: -1 if the MST topology was removed while we were suspended, 0 3798 * otherwise. 3799 */ 3800 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr, 3801 bool sync) 3802 { 3803 u8 buf[UUID_SIZE]; 3804 guid_t guid; 3805 int ret; 3806 3807 mutex_lock(&mgr->lock); 3808 if (!mgr->mst_primary) 3809 goto out_fail; 3810 3811 if (drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd) < 0) { 3812 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n"); 3813 goto out_fail; 3814 } 3815 3816 ret = drm_dp_dpcd_write_byte(mgr->aux, DP_MSTM_CTRL, 3817 DP_MST_EN | 3818 DP_UP_REQ_EN | 3819 DP_UPSTREAM_IS_SRC); 3820 if (ret < 0) { 3821 drm_dbg_kms(mgr->dev, "mst write failed - undocked during suspend?\n"); 3822 goto out_fail; 3823 } 3824 3825 /* Some hubs forget their guids after they resume */ 3826 ret = drm_dp_dpcd_read_data(mgr->aux, DP_GUID, buf, sizeof(buf)); 3827 if (ret < 0) { 3828 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n"); 3829 goto out_fail; 3830 } 3831 3832 import_guid(&guid, buf); 3833 3834 ret = drm_dp_check_mstb_guid(mgr->mst_primary, &guid); 3835 if (ret) { 3836 drm_dbg_kms(mgr->dev, "check mstb failed - undocked during suspend?\n"); 3837 goto out_fail; 3838 } 3839 3840 /* 3841 * For the final step of resuming the topology, we need to bring the 3842 * state of our in-memory topology back into sync with reality. So, 3843 * restart the probing process as if we're probing a new hub 3844 */ 3845 drm_dp_mst_queue_probe_work(mgr); 3846 mutex_unlock(&mgr->lock); 3847 3848 if (sync) { 3849 drm_dbg_kms(mgr->dev, 3850 "Waiting for link probe work to finish re-syncing topology...\n"); 3851 flush_work(&mgr->work); 3852 } 3853 3854 return 0; 3855 3856 out_fail: 3857 mutex_unlock(&mgr->lock); 3858 return -1; 3859 } 3860 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume); 3861 3862 static void reset_msg_rx_state(struct drm_dp_sideband_msg_rx *msg) 3863 { 3864 memset(msg, 0, sizeof(*msg)); 3865 } 3866 3867 static bool 3868 drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up, 3869 struct drm_dp_mst_branch **mstb) 3870 { 3871 int len; 3872 u8 replyblock[32]; 3873 int replylen, curreply; 3874 int ret; 3875 u8 hdrlen; 3876 struct drm_dp_sideband_msg_hdr hdr; 3877 struct drm_dp_sideband_msg_rx *msg = 3878 up ? &mgr->up_req_recv : &mgr->down_rep_recv; 3879 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : 3880 DP_SIDEBAND_MSG_DOWN_REP_BASE; 3881 3882 if (!up) 3883 *mstb = NULL; 3884 3885 len = min(mgr->max_dpcd_transaction_bytes, 16); 3886 ret = drm_dp_dpcd_read_data(mgr->aux, basereg, replyblock, len); 3887 if (ret < 0) { 3888 drm_dbg_kms(mgr->dev, "failed to read DPCD down rep %d %d\n", len, ret); 3889 return false; 3890 } 3891 3892 ret = drm_dp_decode_sideband_msg_hdr(mgr, &hdr, replyblock, len, &hdrlen); 3893 if (ret == false) { 3894 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 3895 1, replyblock, len, false); 3896 drm_dbg_kms(mgr->dev, "ERROR: failed header\n"); 3897 return false; 3898 } 3899 3900 if (!up) { 3901 /* Caller is responsible for giving back this reference */ 3902 *mstb = drm_dp_get_mst_branch_device(mgr, hdr.lct, hdr.rad); 3903 if (!*mstb) { 3904 drm_dbg_kms(mgr->dev, "Got MST reply from unknown device %d\n", hdr.lct); 3905 return false; 3906 } 3907 } 3908 3909 if (!drm_dp_sideband_msg_set_header(msg, &hdr, hdrlen)) { 3910 drm_dbg_kms(mgr->dev, "sideband msg set header failed %d\n", replyblock[0]); 3911 return false; 3912 } 3913 3914 replylen = min(msg->curchunk_len, (u8)(len - hdrlen)); 3915 ret = drm_dp_sideband_append_payload(msg, replyblock + hdrlen, replylen); 3916 if (!ret) { 3917 drm_dbg_kms(mgr->dev, "sideband msg build failed %d\n", replyblock[0]); 3918 return false; 3919 } 3920 3921 replylen = msg->curchunk_len + msg->curchunk_hdrlen - len; 3922 curreply = len; 3923 while (replylen > 0) { 3924 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16); 3925 ret = drm_dp_dpcd_read_data(mgr->aux, basereg + curreply, 3926 replyblock, len); 3927 if (ret < 0) { 3928 drm_dbg_kms(mgr->dev, "failed to read a chunk (len %d, ret %d)\n", 3929 len, ret); 3930 return false; 3931 } 3932 3933 ret = drm_dp_sideband_append_payload(msg, replyblock, len); 3934 if (!ret) { 3935 drm_dbg_kms(mgr->dev, "failed to build sideband msg\n"); 3936 return false; 3937 } 3938 3939 curreply += len; 3940 replylen -= len; 3941 } 3942 return true; 3943 } 3944 3945 static int get_msg_request_type(u8 data) 3946 { 3947 return data & 0x7f; 3948 } 3949 3950 static bool verify_rx_request_type(struct drm_dp_mst_topology_mgr *mgr, 3951 const struct drm_dp_sideband_msg_tx *txmsg, 3952 const struct drm_dp_sideband_msg_rx *rxmsg) 3953 { 3954 const struct drm_dp_sideband_msg_hdr *hdr = &rxmsg->initial_hdr; 3955 const struct drm_dp_mst_branch *mstb = txmsg->dst; 3956 int tx_req_type = get_msg_request_type(txmsg->msg[0]); 3957 int rx_req_type = get_msg_request_type(rxmsg->msg[0]); 3958 char rad_str[64]; 3959 3960 if (tx_req_type == rx_req_type) 3961 return true; 3962 3963 drm_dp_mst_rad_to_str(mstb->rad, mstb->lct, rad_str, sizeof(rad_str)); 3964 drm_dbg_kms(mgr->dev, 3965 "Got unexpected MST reply, mstb: %p seqno: %d lct: %d rad: %s rx_req_type: %s (%02x) != tx_req_type: %s (%02x)\n", 3966 mstb, hdr->seqno, mstb->lct, rad_str, 3967 drm_dp_mst_req_type_str(rx_req_type), rx_req_type, 3968 drm_dp_mst_req_type_str(tx_req_type), tx_req_type); 3969 3970 return false; 3971 } 3972 3973 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr) 3974 { 3975 struct drm_dp_sideband_msg_tx *txmsg; 3976 struct drm_dp_mst_branch *mstb = NULL; 3977 struct drm_dp_sideband_msg_rx *msg = &mgr->down_rep_recv; 3978 3979 if (!drm_dp_get_one_sb_msg(mgr, false, &mstb)) 3980 goto out_clear_reply; 3981 3982 /* Multi-packet message transmission, don't clear the reply */ 3983 if (!msg->have_eomt) 3984 goto out; 3985 3986 /* find the message */ 3987 mutex_lock(&mgr->qlock); 3988 3989 txmsg = list_first_entry_or_null(&mgr->tx_msg_downq, 3990 struct drm_dp_sideband_msg_tx, next); 3991 3992 /* Were we actually expecting a response, and from this mstb? */ 3993 if (!txmsg || txmsg->dst != mstb) { 3994 struct drm_dp_sideband_msg_hdr *hdr; 3995 3996 hdr = &msg->initial_hdr; 3997 drm_dbg_kms(mgr->dev, "Got MST reply with no msg %p %d %d %02x %02x\n", 3998 mstb, hdr->seqno, hdr->lct, hdr->rad[0], msg->msg[0]); 3999 4000 mutex_unlock(&mgr->qlock); 4001 4002 goto out_clear_reply; 4003 } 4004 4005 if (!verify_rx_request_type(mgr, txmsg, msg)) { 4006 mutex_unlock(&mgr->qlock); 4007 4008 goto out_clear_reply; 4009 } 4010 4011 drm_dp_sideband_parse_reply(mgr, msg, &txmsg->reply); 4012 4013 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) { 4014 drm_dbg_kms(mgr->dev, 4015 "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n", 4016 txmsg->reply.req_type, 4017 drm_dp_mst_req_type_str(txmsg->reply.req_type), 4018 txmsg->reply.u.nak.reason, 4019 drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason), 4020 txmsg->reply.u.nak.nak_data); 4021 } 4022 4023 txmsg->state = DRM_DP_SIDEBAND_TX_RX; 4024 list_del(&txmsg->next); 4025 4026 mutex_unlock(&mgr->qlock); 4027 4028 wake_up_all(&mgr->tx_waitq); 4029 4030 out_clear_reply: 4031 reset_msg_rx_state(msg); 4032 out: 4033 if (mstb) 4034 drm_dp_mst_topology_put_mstb(mstb); 4035 4036 return 0; 4037 } 4038 4039 static bool primary_mstb_probing_is_done(struct drm_dp_mst_topology_mgr *mgr) 4040 { 4041 bool probing_done = false; 4042 4043 mutex_lock(&mgr->lock); 4044 4045 if (mgr->mst_primary && drm_dp_mst_topology_try_get_mstb(mgr->mst_primary)) { 4046 probing_done = mgr->mst_primary->link_address_sent; 4047 drm_dp_mst_topology_put_mstb(mgr->mst_primary); 4048 } 4049 4050 mutex_unlock(&mgr->lock); 4051 4052 return probing_done; 4053 } 4054 4055 static inline bool 4056 drm_dp_mst_process_up_req(struct drm_dp_mst_topology_mgr *mgr, 4057 struct drm_dp_pending_up_req *up_req) 4058 { 4059 struct drm_dp_mst_branch *mstb = NULL; 4060 struct drm_dp_sideband_msg_req_body *msg = &up_req->msg; 4061 struct drm_dp_sideband_msg_hdr *hdr = &up_req->hdr; 4062 bool hotplug = false, dowork = false; 4063 4064 if (hdr->broadcast) { 4065 const guid_t *guid = NULL; 4066 4067 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY) 4068 guid = &msg->u.conn_stat.guid; 4069 else if (msg->req_type == DP_RESOURCE_STATUS_NOTIFY) 4070 guid = &msg->u.resource_stat.guid; 4071 4072 if (guid) 4073 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, guid); 4074 } else { 4075 mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad); 4076 } 4077 4078 if (!mstb) { 4079 drm_dbg_kms(mgr->dev, "Got MST reply from unknown device %d\n", hdr->lct); 4080 return false; 4081 } 4082 4083 /* TODO: Add missing handler for DP_RESOURCE_STATUS_NOTIFY events */ 4084 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY) { 4085 if (!primary_mstb_probing_is_done(mgr)) { 4086 drm_dbg_kms(mgr->dev, "Got CSN before finish topology probing. Skip it.\n"); 4087 } else { 4088 dowork = drm_dp_mst_handle_conn_stat(mstb, &msg->u.conn_stat); 4089 hotplug = true; 4090 } 4091 } 4092 4093 drm_dp_mst_topology_put_mstb(mstb); 4094 4095 if (dowork) 4096 queue_work(system_long_wq, &mgr->work); 4097 return hotplug; 4098 } 4099 4100 static void drm_dp_mst_up_req_work(struct work_struct *work) 4101 { 4102 struct drm_dp_mst_topology_mgr *mgr = 4103 container_of(work, struct drm_dp_mst_topology_mgr, 4104 up_req_work); 4105 struct drm_dp_pending_up_req *up_req; 4106 bool send_hotplug = false; 4107 4108 mutex_lock(&mgr->probe_lock); 4109 while (true) { 4110 mutex_lock(&mgr->up_req_lock); 4111 up_req = list_first_entry_or_null(&mgr->up_req_list, 4112 struct drm_dp_pending_up_req, 4113 next); 4114 if (up_req) 4115 list_del(&up_req->next); 4116 mutex_unlock(&mgr->up_req_lock); 4117 4118 if (!up_req) 4119 break; 4120 4121 send_hotplug |= drm_dp_mst_process_up_req(mgr, up_req); 4122 kfree(up_req); 4123 } 4124 mutex_unlock(&mgr->probe_lock); 4125 4126 if (send_hotplug) 4127 drm_kms_helper_hotplug_event(mgr->dev); 4128 } 4129 4130 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr) 4131 { 4132 struct drm_dp_pending_up_req *up_req; 4133 struct drm_dp_mst_branch *mst_primary; 4134 int ret = 0; 4135 4136 if (!drm_dp_get_one_sb_msg(mgr, true, NULL)) 4137 goto out_clear_reply; 4138 4139 if (!mgr->up_req_recv.have_eomt) 4140 return 0; 4141 4142 up_req = kzalloc_obj(*up_req); 4143 if (!up_req) { 4144 ret = -ENOMEM; 4145 goto out_clear_reply; 4146 } 4147 4148 INIT_LIST_HEAD(&up_req->next); 4149 4150 drm_dp_sideband_parse_req(mgr, &mgr->up_req_recv, &up_req->msg); 4151 4152 if (up_req->msg.req_type != DP_CONNECTION_STATUS_NOTIFY && 4153 up_req->msg.req_type != DP_RESOURCE_STATUS_NOTIFY) { 4154 drm_dbg_kms(mgr->dev, "Received unknown up req type, ignoring: %x\n", 4155 up_req->msg.req_type); 4156 kfree(up_req); 4157 goto out_clear_reply; 4158 } 4159 4160 mutex_lock(&mgr->lock); 4161 mst_primary = mgr->mst_primary; 4162 if (!mst_primary || !drm_dp_mst_topology_try_get_mstb(mst_primary)) { 4163 mutex_unlock(&mgr->lock); 4164 kfree(up_req); 4165 goto out_clear_reply; 4166 } 4167 mutex_unlock(&mgr->lock); 4168 4169 drm_dp_send_up_ack_reply(mgr, mst_primary, up_req->msg.req_type, 4170 false); 4171 4172 drm_dp_mst_topology_put_mstb(mst_primary); 4173 4174 if (up_req->msg.req_type == DP_CONNECTION_STATUS_NOTIFY) { 4175 const struct drm_dp_connection_status_notify *conn_stat = 4176 &up_req->msg.u.conn_stat; 4177 4178 drm_dbg_kms(mgr->dev, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n", 4179 conn_stat->port_number, 4180 conn_stat->legacy_device_plug_status, 4181 conn_stat->displayport_device_plug_status, 4182 conn_stat->message_capability_status, 4183 conn_stat->input_port, 4184 conn_stat->peer_device_type); 4185 } else if (up_req->msg.req_type == DP_RESOURCE_STATUS_NOTIFY) { 4186 const struct drm_dp_resource_status_notify *res_stat = 4187 &up_req->msg.u.resource_stat; 4188 4189 drm_dbg_kms(mgr->dev, "Got RSN: pn: %d avail_pbn %d\n", 4190 res_stat->port_number, 4191 res_stat->available_pbn); 4192 } 4193 4194 up_req->hdr = mgr->up_req_recv.initial_hdr; 4195 mutex_lock(&mgr->up_req_lock); 4196 list_add_tail(&up_req->next, &mgr->up_req_list); 4197 mutex_unlock(&mgr->up_req_lock); 4198 queue_work(system_long_wq, &mgr->up_req_work); 4199 out_clear_reply: 4200 reset_msg_rx_state(&mgr->up_req_recv); 4201 return ret; 4202 } 4203 4204 static void update_msg_rx_state(struct drm_dp_mst_topology_mgr *mgr) 4205 { 4206 mutex_lock(&mgr->lock); 4207 if (mgr->reset_rx_state) { 4208 mgr->reset_rx_state = false; 4209 reset_msg_rx_state(&mgr->down_rep_recv); 4210 reset_msg_rx_state(&mgr->up_req_recv); 4211 } 4212 mutex_unlock(&mgr->lock); 4213 } 4214 4215 /** 4216 * drm_dp_mst_hpd_irq_handle_event() - MST hotplug IRQ handle MST event 4217 * @mgr: manager to notify irq for. 4218 * @esi: 4 bytes from SINK_COUNT_ESI 4219 * @ack: 4 bytes used to ack events starting from SINK_COUNT_ESI 4220 * @handled: whether the hpd interrupt was consumed or not 4221 * 4222 * This should be called from the driver when it detects a HPD IRQ, 4223 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The 4224 * topology manager will process the sideband messages received 4225 * as indicated in the DEVICE_SERVICE_IRQ_VECTOR_ESI0 and set the 4226 * corresponding flags that Driver has to ack the DP receiver later. 4227 * 4228 * Note that driver shall also call 4229 * drm_dp_mst_hpd_irq_send_new_request() if the 'handled' is set 4230 * after calling this function, to try to kick off a new request in 4231 * the queue if the previous message transaction is completed. 4232 * 4233 * See also: 4234 * drm_dp_mst_hpd_irq_send_new_request() 4235 */ 4236 int drm_dp_mst_hpd_irq_handle_event(struct drm_dp_mst_topology_mgr *mgr, const u8 *esi, 4237 u8 *ack, bool *handled) 4238 { 4239 int ret = 0; 4240 int sc; 4241 *handled = false; 4242 sc = DP_GET_SINK_COUNT(esi[0]); 4243 4244 if (sc != mgr->sink_count) { 4245 mgr->sink_count = sc; 4246 *handled = true; 4247 } 4248 4249 update_msg_rx_state(mgr); 4250 4251 if (esi[1] & DP_DOWN_REP_MSG_RDY) { 4252 ret = drm_dp_mst_handle_down_rep(mgr); 4253 *handled = true; 4254 ack[1] |= DP_DOWN_REP_MSG_RDY; 4255 } 4256 4257 if (esi[1] & DP_UP_REQ_MSG_RDY) { 4258 ret |= drm_dp_mst_handle_up_req(mgr); 4259 *handled = true; 4260 ack[1] |= DP_UP_REQ_MSG_RDY; 4261 } 4262 4263 return ret; 4264 } 4265 EXPORT_SYMBOL(drm_dp_mst_hpd_irq_handle_event); 4266 4267 /** 4268 * drm_dp_mst_hpd_irq_send_new_request() - MST hotplug IRQ kick off new request 4269 * @mgr: manager to notify irq for. 4270 * 4271 * This should be called from the driver when mst irq event is handled 4272 * and acked. Note that new down request should only be sent when 4273 * previous message transaction is completed. Source is not supposed to generate 4274 * interleaved message transactions. 4275 */ 4276 void drm_dp_mst_hpd_irq_send_new_request(struct drm_dp_mst_topology_mgr *mgr) 4277 { 4278 struct drm_dp_sideband_msg_tx *txmsg; 4279 bool kick = true; 4280 4281 mutex_lock(&mgr->qlock); 4282 txmsg = list_first_entry_or_null(&mgr->tx_msg_downq, 4283 struct drm_dp_sideband_msg_tx, next); 4284 /* If last transaction is not completed yet*/ 4285 if (!txmsg || 4286 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND || 4287 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) 4288 kick = false; 4289 mutex_unlock(&mgr->qlock); 4290 4291 if (kick) 4292 drm_dp_mst_kick_tx(mgr); 4293 } 4294 EXPORT_SYMBOL(drm_dp_mst_hpd_irq_send_new_request); 4295 /** 4296 * drm_dp_mst_detect_port() - get connection status for an MST port 4297 * @connector: DRM connector for this port 4298 * @ctx: The acquisition context to use for grabbing locks 4299 * @mgr: manager for this port 4300 * @port: pointer to a port 4301 * 4302 * This returns the current connection state for a port. 4303 */ 4304 int 4305 drm_dp_mst_detect_port(struct drm_connector *connector, 4306 struct drm_modeset_acquire_ctx *ctx, 4307 struct drm_dp_mst_topology_mgr *mgr, 4308 struct drm_dp_mst_port *port) 4309 { 4310 int ret; 4311 4312 /* we need to search for the port in the mgr in case it's gone */ 4313 port = drm_dp_mst_topology_get_port_validated(mgr, port); 4314 if (!port) 4315 return connector_status_disconnected; 4316 4317 ret = drm_modeset_lock(&mgr->base.lock, ctx); 4318 if (ret) 4319 goto out; 4320 4321 ret = connector_status_disconnected; 4322 4323 if (!port->ddps) 4324 goto out; 4325 4326 switch (port->pdt) { 4327 case DP_PEER_DEVICE_NONE: 4328 break; 4329 case DP_PEER_DEVICE_MST_BRANCHING: 4330 if (!port->mcs) 4331 ret = connector_status_connected; 4332 break; 4333 4334 case DP_PEER_DEVICE_SST_SINK: 4335 ret = connector_status_connected; 4336 /* for logical ports - cache the EDID */ 4337 if (drm_dp_mst_port_is_logical(port) && !port->cached_edid) 4338 port->cached_edid = drm_edid_read_ddc(connector, &port->aux.ddc); 4339 break; 4340 case DP_PEER_DEVICE_DP_LEGACY_CONV: 4341 if (port->ldps) 4342 ret = connector_status_connected; 4343 break; 4344 } 4345 out: 4346 drm_dp_mst_topology_put_port(port); 4347 return ret; 4348 } 4349 EXPORT_SYMBOL(drm_dp_mst_detect_port); 4350 4351 /** 4352 * drm_dp_mst_edid_read() - get EDID for an MST port 4353 * @connector: toplevel connector to get EDID for 4354 * @mgr: manager for this port 4355 * @port: unverified pointer to a port. 4356 * 4357 * This returns an EDID for the port connected to a connector, 4358 * It validates the pointer still exists so the caller doesn't require a 4359 * reference. 4360 */ 4361 const struct drm_edid *drm_dp_mst_edid_read(struct drm_connector *connector, 4362 struct drm_dp_mst_topology_mgr *mgr, 4363 struct drm_dp_mst_port *port) 4364 { 4365 const struct drm_edid *drm_edid; 4366 4367 /* we need to search for the port in the mgr in case it's gone */ 4368 port = drm_dp_mst_topology_get_port_validated(mgr, port); 4369 if (!port) 4370 return NULL; 4371 4372 if (port->cached_edid) 4373 drm_edid = drm_edid_dup(port->cached_edid); 4374 else 4375 drm_edid = drm_edid_read_ddc(connector, &port->aux.ddc); 4376 4377 drm_dp_mst_topology_put_port(port); 4378 4379 return drm_edid; 4380 } 4381 EXPORT_SYMBOL(drm_dp_mst_edid_read); 4382 4383 /** 4384 * drm_dp_mst_get_edid() - get EDID for an MST port 4385 * @connector: toplevel connector to get EDID for 4386 * @mgr: manager for this port 4387 * @port: unverified pointer to a port. 4388 * 4389 * This function is deprecated; please use drm_dp_mst_edid_read() instead. 4390 * 4391 * This returns an EDID for the port connected to a connector, 4392 * It validates the pointer still exists so the caller doesn't require a 4393 * reference. 4394 */ 4395 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, 4396 struct drm_dp_mst_topology_mgr *mgr, 4397 struct drm_dp_mst_port *port) 4398 { 4399 const struct drm_edid *drm_edid; 4400 struct edid *edid; 4401 4402 drm_edid = drm_dp_mst_edid_read(connector, mgr, port); 4403 4404 edid = drm_edid_duplicate(drm_edid_raw(drm_edid)); 4405 4406 drm_edid_free(drm_edid); 4407 4408 return edid; 4409 } 4410 EXPORT_SYMBOL(drm_dp_mst_get_edid); 4411 4412 /** 4413 * drm_dp_atomic_find_time_slots() - Find and add time slots to the state 4414 * @state: global atomic state 4415 * @mgr: MST topology manager for the port 4416 * @port: port to find time slots for 4417 * @pbn: bandwidth required for the mode in PBN 4418 * 4419 * Allocates time slots to @port, replacing any previous time slot allocations it may 4420 * have had. Any atomic drivers which support MST must call this function in 4421 * their &drm_encoder_helper_funcs.atomic_check() callback unconditionally to 4422 * change the current time slot allocation for the new state, and ensure the MST 4423 * atomic state is added whenever the state of payloads in the topology changes. 4424 * 4425 * Allocations set by this function are not checked against the bandwidth 4426 * restraints of @mgr until the driver calls drm_dp_mst_atomic_check(). 4427 * 4428 * Additionally, it is OK to call this function multiple times on the same 4429 * @port as needed. It is not OK however, to call this function and 4430 * drm_dp_atomic_release_time_slots() in the same atomic check phase. 4431 * 4432 * See also: 4433 * drm_dp_atomic_release_time_slots() 4434 * drm_dp_mst_atomic_check() 4435 * 4436 * Returns: 4437 * Total slots in the atomic state assigned for this port, or a negative error 4438 * code if the port no longer exists 4439 */ 4440 int drm_dp_atomic_find_time_slots(struct drm_atomic_commit *state, 4441 struct drm_dp_mst_topology_mgr *mgr, 4442 struct drm_dp_mst_port *port, int pbn) 4443 { 4444 struct drm_dp_mst_topology_state *topology_state; 4445 struct drm_dp_mst_atomic_payload *payload = NULL; 4446 struct drm_connector_state *conn_state; 4447 int prev_slots = 0, prev_bw = 0, req_slots; 4448 4449 topology_state = drm_atomic_get_mst_topology_state(state, mgr); 4450 if (IS_ERR(topology_state)) 4451 return PTR_ERR(topology_state); 4452 4453 conn_state = drm_atomic_get_new_connector_state(state, port->connector); 4454 topology_state->pending_crtc_mask |= drm_crtc_mask(conn_state->crtc); 4455 4456 /* Find the current allocation for this port, if any */ 4457 payload = drm_atomic_get_mst_payload_state(topology_state, port); 4458 if (payload) { 4459 prev_slots = payload->time_slots; 4460 prev_bw = payload->pbn; 4461 4462 /* 4463 * This should never happen, unless the driver tries 4464 * releasing and allocating the same timeslot allocation, 4465 * which is an error 4466 */ 4467 if (drm_WARN_ON(mgr->dev, payload->delete)) { 4468 drm_err(mgr->dev, 4469 "cannot allocate and release time slots on [MST PORT:%p] in the same state\n", 4470 port); 4471 return -EINVAL; 4472 } 4473 } 4474 4475 req_slots = DIV_ROUND_UP(dfixed_const(pbn), topology_state->pbn_div.full); 4476 4477 drm_dbg_atomic(mgr->dev, "[CONNECTOR:%d:%s] [MST PORT:%p] TU %d -> %d\n", 4478 port->connector->base.id, port->connector->name, 4479 port, prev_slots, req_slots); 4480 drm_dbg_atomic(mgr->dev, "[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n", 4481 port->connector->base.id, port->connector->name, 4482 port, prev_bw, pbn); 4483 4484 /* Add the new allocation to the state, note the VCPI isn't assigned until the end */ 4485 if (!payload) { 4486 payload = kzalloc_obj(*payload); 4487 if (!payload) 4488 return -ENOMEM; 4489 4490 drm_dp_mst_get_port_malloc(port); 4491 payload->port = port; 4492 payload->vc_start_slot = -1; 4493 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_NONE; 4494 list_add(&payload->next, &topology_state->payloads); 4495 } 4496 payload->time_slots = req_slots; 4497 payload->pbn = pbn; 4498 4499 return req_slots; 4500 } 4501 EXPORT_SYMBOL(drm_dp_atomic_find_time_slots); 4502 4503 /** 4504 * drm_dp_atomic_release_time_slots() - Release allocated time slots 4505 * @state: global atomic state 4506 * @mgr: MST topology manager for the port 4507 * @port: The port to release the time slots from 4508 * 4509 * Releases any time slots that have been allocated to a port in the atomic 4510 * state. Any atomic drivers which support MST must call this function 4511 * unconditionally in their &drm_connector_helper_funcs.atomic_check() callback. 4512 * This helper will check whether time slots would be released by the new state and 4513 * respond accordingly, along with ensuring the MST state is always added to the 4514 * atomic state whenever a new state would modify the state of payloads on the 4515 * topology. 4516 * 4517 * It is OK to call this even if @port has been removed from the system. 4518 * Additionally, it is OK to call this function multiple times on the same 4519 * @port as needed. It is not OK however, to call this function and 4520 * drm_dp_atomic_find_time_slots() on the same @port in a single atomic check 4521 * phase. 4522 * 4523 * See also: 4524 * drm_dp_atomic_find_time_slots() 4525 * drm_dp_mst_atomic_check() 4526 * 4527 * Returns: 4528 * 0 on success, negative error code otherwise 4529 */ 4530 int drm_dp_atomic_release_time_slots(struct drm_atomic_commit *state, 4531 struct drm_dp_mst_topology_mgr *mgr, 4532 struct drm_dp_mst_port *port) 4533 { 4534 struct drm_dp_mst_topology_state *topology_state; 4535 struct drm_dp_mst_atomic_payload *payload; 4536 struct drm_connector_state *old_conn_state, *new_conn_state; 4537 bool update_payload = true; 4538 4539 old_conn_state = drm_atomic_get_old_connector_state(state, port->connector); 4540 if (!old_conn_state->crtc) 4541 return 0; 4542 4543 /* If the CRTC isn't disabled by this state, don't release it's payload */ 4544 new_conn_state = drm_atomic_get_new_connector_state(state, port->connector); 4545 if (new_conn_state->crtc) { 4546 struct drm_crtc_state *crtc_state = 4547 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc); 4548 4549 /* No modeset means no payload changes, so it's safe to not pull in the MST state */ 4550 if (!crtc_state || !drm_atomic_crtc_needs_modeset(crtc_state)) 4551 return 0; 4552 4553 if (!crtc_state->mode_changed && !crtc_state->connectors_changed) 4554 update_payload = false; 4555 } 4556 4557 topology_state = drm_atomic_get_mst_topology_state(state, mgr); 4558 if (IS_ERR(topology_state)) 4559 return PTR_ERR(topology_state); 4560 4561 topology_state->pending_crtc_mask |= drm_crtc_mask(old_conn_state->crtc); 4562 if (!update_payload) 4563 return 0; 4564 4565 payload = drm_atomic_get_mst_payload_state(topology_state, port); 4566 if (WARN_ON(!payload)) { 4567 drm_err(mgr->dev, "No payload for [MST PORT:%p] found in mst state %p\n", 4568 port, &topology_state->base); 4569 return -EINVAL; 4570 } 4571 4572 if (new_conn_state->crtc) 4573 return 0; 4574 4575 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] TU %d -> 0\n", port, payload->time_slots); 4576 if (!payload->delete) { 4577 payload->pbn = 0; 4578 payload->delete = true; 4579 if (payload->vcpi > 0) 4580 topology_state->payload_mask &= ~BIT(payload->vcpi - 1); 4581 } 4582 4583 return 0; 4584 } 4585 EXPORT_SYMBOL(drm_dp_atomic_release_time_slots); 4586 4587 /** 4588 * drm_dp_mst_atomic_setup_commit() - setup_commit hook for MST helpers 4589 * @state: global atomic state 4590 * 4591 * This function saves all of the &drm_crtc_commit structs in an atomic state that touch any CRTCs 4592 * currently assigned to an MST topology. Drivers must call this hook from their 4593 * &drm_mode_config_helper_funcs.atomic_commit_setup hook. 4594 * 4595 * Returns: 4596 * 0 if all CRTC commits were retrieved successfully, negative error code otherwise 4597 */ 4598 int drm_dp_mst_atomic_setup_commit(struct drm_atomic_commit *state) 4599 { 4600 struct drm_dp_mst_topology_mgr *mgr; 4601 struct drm_dp_mst_topology_state *mst_state; 4602 struct drm_crtc *crtc; 4603 struct drm_crtc_state *crtc_state; 4604 int i, j, commit_idx, num_commit_deps; 4605 4606 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) { 4607 if (!mst_state->pending_crtc_mask) 4608 continue; 4609 4610 num_commit_deps = hweight32(mst_state->pending_crtc_mask); 4611 mst_state->commit_deps = kmalloc_objs(*mst_state->commit_deps, 4612 num_commit_deps); 4613 if (!mst_state->commit_deps) 4614 return -ENOMEM; 4615 mst_state->num_commit_deps = num_commit_deps; 4616 4617 commit_idx = 0; 4618 for_each_new_crtc_in_state(state, crtc, crtc_state, j) { 4619 if (mst_state->pending_crtc_mask & drm_crtc_mask(crtc)) { 4620 mst_state->commit_deps[commit_idx++] = 4621 drm_crtc_commit_get(crtc_state->commit); 4622 } 4623 } 4624 } 4625 4626 return 0; 4627 } 4628 EXPORT_SYMBOL(drm_dp_mst_atomic_setup_commit); 4629 4630 /** 4631 * drm_dp_mst_atomic_wait_for_dependencies() - Wait for all pending commits on MST topologies, 4632 * prepare new MST state for commit 4633 * @state: global atomic state 4634 * 4635 * Goes through any MST topologies in this atomic state, and waits for any pending commits which 4636 * touched CRTCs that were/are on an MST topology to be programmed to hardware and flipped to before 4637 * returning. This is to prevent multiple non-blocking commits affecting an MST topology from racing 4638 * with eachother by forcing them to be executed sequentially in situations where the only resources 4639 * the modeset objects in these commits share are an MST topology. 4640 * 4641 * This function also prepares the new MST state for commit by performing some state preparation 4642 * which can't be done until this point, such as reading back the final VC start slots (which are 4643 * determined at commit-time) from the previous state. 4644 * 4645 * All MST drivers must call this function after calling drm_atomic_helper_wait_for_dependencies(), 4646 * or whatever their equivalent of that is. 4647 */ 4648 void drm_dp_mst_atomic_wait_for_dependencies(struct drm_atomic_commit *state) 4649 { 4650 struct drm_dp_mst_topology_state *old_mst_state, *new_mst_state; 4651 struct drm_dp_mst_topology_mgr *mgr; 4652 struct drm_dp_mst_atomic_payload *old_payload, *new_payload; 4653 int i, j, ret; 4654 4655 for_each_oldnew_mst_mgr_in_state(state, mgr, old_mst_state, new_mst_state, i) { 4656 for (j = 0; j < old_mst_state->num_commit_deps; j++) { 4657 ret = drm_crtc_commit_wait(old_mst_state->commit_deps[j]); 4658 if (ret < 0) 4659 drm_err(state->dev, "Failed to wait for %s: %d\n", 4660 old_mst_state->commit_deps[j]->crtc->name, ret); 4661 } 4662 4663 /* Now that previous state is committed, it's safe to copy over the start slot 4664 * and allocation status assignments 4665 */ 4666 list_for_each_entry(old_payload, &old_mst_state->payloads, next) { 4667 if (old_payload->delete) 4668 continue; 4669 4670 new_payload = drm_atomic_get_mst_payload_state(new_mst_state, 4671 old_payload->port); 4672 new_payload->vc_start_slot = old_payload->vc_start_slot; 4673 new_payload->payload_allocation_status = 4674 old_payload->payload_allocation_status; 4675 } 4676 } 4677 } 4678 EXPORT_SYMBOL(drm_dp_mst_atomic_wait_for_dependencies); 4679 4680 /** 4681 * drm_dp_mst_root_conn_atomic_check() - Serialize CRTC commits on MST-capable connectors operating 4682 * in SST mode 4683 * @new_conn_state: The new connector state of the &drm_connector 4684 * @mgr: The MST topology manager for the &drm_connector 4685 * 4686 * Since MST uses fake &drm_encoder structs, the generic atomic modesetting code isn't able to 4687 * serialize non-blocking commits happening on the real DP connector of an MST topology switching 4688 * into/away from MST mode - as the CRTC on the real DP connector and the CRTCs on the connector's 4689 * MST topology will never share the same &drm_encoder. 4690 * 4691 * This function takes care of this serialization issue, by checking a root MST connector's atomic 4692 * state to determine if it is about to have a modeset - and then pulling in the MST topology state 4693 * if so, along with adding any relevant CRTCs to &drm_dp_mst_topology_state.pending_crtc_mask. 4694 * 4695 * Drivers implementing MST must call this function from the 4696 * &drm_connector_helper_funcs.atomic_check hook of any physical DP &drm_connector capable of 4697 * driving MST sinks. 4698 * 4699 * Returns: 4700 * 0 on success, negative error code otherwise 4701 */ 4702 int drm_dp_mst_root_conn_atomic_check(struct drm_connector_state *new_conn_state, 4703 struct drm_dp_mst_topology_mgr *mgr) 4704 { 4705 struct drm_atomic_commit *state = new_conn_state->state; 4706 struct drm_connector_state *old_conn_state = 4707 drm_atomic_get_old_connector_state(state, new_conn_state->connector); 4708 struct drm_crtc_state *crtc_state; 4709 struct drm_dp_mst_topology_state *mst_state = NULL; 4710 4711 if (new_conn_state->crtc) { 4712 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc); 4713 if (crtc_state && drm_atomic_crtc_needs_modeset(crtc_state)) { 4714 mst_state = drm_atomic_get_mst_topology_state(state, mgr); 4715 if (IS_ERR(mst_state)) 4716 return PTR_ERR(mst_state); 4717 4718 mst_state->pending_crtc_mask |= drm_crtc_mask(new_conn_state->crtc); 4719 } 4720 } 4721 4722 if (old_conn_state->crtc) { 4723 crtc_state = drm_atomic_get_new_crtc_state(state, old_conn_state->crtc); 4724 if (crtc_state && drm_atomic_crtc_needs_modeset(crtc_state)) { 4725 if (!mst_state) { 4726 mst_state = drm_atomic_get_mst_topology_state(state, mgr); 4727 if (IS_ERR(mst_state)) 4728 return PTR_ERR(mst_state); 4729 } 4730 4731 mst_state->pending_crtc_mask |= drm_crtc_mask(old_conn_state->crtc); 4732 } 4733 } 4734 4735 return 0; 4736 } 4737 EXPORT_SYMBOL(drm_dp_mst_root_conn_atomic_check); 4738 4739 /** 4740 * drm_dp_mst_update_slots() - updates the slot info depending on the DP ecoding format 4741 * @mst_state: mst_state to update 4742 * @link_encoding_cap: the ecoding format on the link 4743 */ 4744 void drm_dp_mst_update_slots(struct drm_dp_mst_topology_state *mst_state, uint8_t link_encoding_cap) 4745 { 4746 if (link_encoding_cap == DP_CAP_ANSI_128B132B) { 4747 mst_state->total_avail_slots = 64; 4748 mst_state->start_slot = 0; 4749 } else { 4750 mst_state->total_avail_slots = 63; 4751 mst_state->start_slot = 1; 4752 } 4753 4754 DRM_DEBUG_KMS("%s encoding format on mst_state 0x%p\n", 4755 (link_encoding_cap == DP_CAP_ANSI_128B132B) ? "128b/132b":"8b/10b", 4756 mst_state); 4757 } 4758 EXPORT_SYMBOL(drm_dp_mst_update_slots); 4759 4760 /** 4761 * drm_dp_check_act_status() - Polls for ACT handled status. 4762 * @mgr: manager to use 4763 * 4764 * Tries waiting for the MST hub to finish updating it's payload table by 4765 * polling for the ACT handled bit for up to 3 seconds (yes-some hubs really 4766 * take that long). 4767 * 4768 * Returns: 4769 * 0 if the ACT was handled in time, negative error code on failure. 4770 */ 4771 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr) 4772 { 4773 /* 4774 * There doesn't seem to be any recommended retry count or timeout in 4775 * the MST specification. Since some hubs have been observed to take 4776 * over 1 second to update their payload allocations under certain 4777 * conditions, we use a rather large timeout value of 3 seconds. 4778 */ 4779 return drm_dp_dpcd_poll_act_handled(mgr->aux, 3000); 4780 } 4781 EXPORT_SYMBOL(drm_dp_check_act_status); 4782 4783 /** 4784 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode. 4785 * @clock: dot clock 4786 * @bpp: bpp as .4 binary fixed point 4787 * 4788 * This uses the formula in the spec to calculate the PBN value for a mode. 4789 */ 4790 int drm_dp_calc_pbn_mode(int clock, int bpp) 4791 { 4792 /* 4793 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on 4794 * common multiplier to render an integer PBN for all link rate/lane 4795 * counts combinations 4796 * calculate 4797 * peak_kbps = clock * bpp / 16 4798 * peak_kbps *= SSC overhead / 1000000 4799 * peak_kbps /= 8 convert to Kbytes 4800 * peak_kBps *= (64/54) / 1000 convert to PBN 4801 */ 4802 /* 4803 * TODO: Use the actual link and mode parameters to calculate 4804 * the overhead. For now it's assumed that these are 4805 * 4 link lanes, 4096 hactive pixels, which don't add any 4806 * significant data padding overhead and that there is no DSC 4807 * or FEC overhead. 4808 */ 4809 int overhead = drm_dp_bw_overhead(4, 4096, 0, bpp, 4810 DRM_DP_BW_OVERHEAD_MST | 4811 DRM_DP_BW_OVERHEAD_SSC_REF_CLK); 4812 4813 return DIV64_U64_ROUND_UP(mul_u32_u32(clock * bpp, 64 * overhead >> 4), 4814 1000000ULL * 8 * 54 * 1000); 4815 } 4816 EXPORT_SYMBOL(drm_dp_calc_pbn_mode); 4817 4818 /* we want to kick the TX after we've ack the up/down IRQs. */ 4819 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr) 4820 { 4821 queue_work(system_long_wq, &mgr->tx_work); 4822 } 4823 4824 /* 4825 * Helper function for parsing DP device types into convenient strings 4826 * for use with dp_mst_topology 4827 */ 4828 static const char *pdt_to_string(u8 pdt) 4829 { 4830 switch (pdt) { 4831 case DP_PEER_DEVICE_NONE: 4832 return "NONE"; 4833 case DP_PEER_DEVICE_SOURCE_OR_SST: 4834 return "SOURCE OR SST"; 4835 case DP_PEER_DEVICE_MST_BRANCHING: 4836 return "MST BRANCHING"; 4837 case DP_PEER_DEVICE_SST_SINK: 4838 return "SST SINK"; 4839 case DP_PEER_DEVICE_DP_LEGACY_CONV: 4840 return "DP LEGACY CONV"; 4841 default: 4842 return "ERR"; 4843 } 4844 } 4845 4846 static void drm_dp_mst_dump_mstb(struct seq_file *m, 4847 struct drm_dp_mst_branch *mstb) 4848 { 4849 struct drm_dp_mst_port *port; 4850 int tabs = mstb->lct; 4851 char prefix[10]; 4852 int i; 4853 4854 for (i = 0; i < tabs; i++) 4855 prefix[i] = '\t'; 4856 prefix[i] = '\0'; 4857 4858 seq_printf(m, "%smstb - [%p]: num_ports: %d\n", prefix, mstb, mstb->num_ports); 4859 list_for_each_entry(port, &mstb->ports, next) { 4860 seq_printf(m, "%sport %d - [%p] (%s - %s): ddps: %d, ldps: %d, sdp: %d/%d, fec: %s, conn: %p\n", 4861 prefix, 4862 port->port_num, 4863 port, 4864 port->input ? "input" : "output", 4865 pdt_to_string(port->pdt), 4866 port->ddps, 4867 port->ldps, 4868 port->num_sdp_streams, 4869 port->num_sdp_stream_sinks, 4870 port->fec_capable ? "true" : "false", 4871 port->connector); 4872 if (port->mstb) 4873 drm_dp_mst_dump_mstb(m, port->mstb); 4874 } 4875 } 4876 4877 #define DP_PAYLOAD_TABLE_SIZE 64 4878 4879 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr, 4880 char *buf) 4881 { 4882 int i; 4883 4884 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) { 4885 if (drm_dp_dpcd_read_data(mgr->aux, 4886 DP_PAYLOAD_TABLE_UPDATE_STATUS + i, 4887 &buf[i], 16) < 0) 4888 return false; 4889 } 4890 return true; 4891 } 4892 4893 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr, 4894 struct drm_dp_mst_port *port, char *name, 4895 int namelen) 4896 { 4897 struct edid *mst_edid; 4898 4899 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port); 4900 drm_edid_get_monitor_name(mst_edid, name, namelen); 4901 kfree(mst_edid); 4902 } 4903 4904 /** 4905 * drm_dp_mst_dump_topology(): dump topology to seq file. 4906 * @m: seq_file to dump output to 4907 * @mgr: manager to dump current topology for. 4908 * 4909 * helper to dump MST topology to a seq file for debugfs. 4910 */ 4911 void drm_dp_mst_dump_topology(struct seq_file *m, 4912 struct drm_dp_mst_topology_mgr *mgr) 4913 { 4914 struct drm_dp_mst_topology_state *state; 4915 struct drm_dp_mst_atomic_payload *payload; 4916 int i, ret; 4917 4918 static const char *const status[] = { 4919 "None", 4920 "Local", 4921 "DFP", 4922 "Remote", 4923 }; 4924 4925 mutex_lock(&mgr->lock); 4926 if (mgr->mst_primary) 4927 drm_dp_mst_dump_mstb(m, mgr->mst_primary); 4928 4929 /* dump VCPIs */ 4930 mutex_unlock(&mgr->lock); 4931 4932 ret = drm_modeset_lock_single_interruptible(&mgr->base.lock); 4933 if (ret < 0) 4934 return; 4935 4936 state = to_drm_dp_mst_topology_state(mgr->base.state); 4937 seq_printf(m, "\n*** Atomic state info ***\n"); 4938 seq_printf(m, "payload_mask: %x, max_payloads: %d, start_slot: %u, pbn_div: %d\n", 4939 state->payload_mask, mgr->max_payloads, state->start_slot, 4940 dfixed_trunc(state->pbn_div)); 4941 4942 seq_printf(m, "\n| idx | port | vcpi | slots | pbn | dsc | status | sink name |\n"); 4943 for (i = 0; i < mgr->max_payloads; i++) { 4944 list_for_each_entry(payload, &state->payloads, next) { 4945 char name[14]; 4946 4947 if (payload->vcpi != i || payload->delete) 4948 continue; 4949 4950 fetch_monitor_name(mgr, payload->port, name, sizeof(name)); 4951 seq_printf(m, " %5d %6d %6d %02d - %02d %5d %5s %8s %19s\n", 4952 i, 4953 payload->port->port_num, 4954 payload->vcpi, 4955 payload->vc_start_slot, 4956 payload->vc_start_slot + payload->time_slots - 1, 4957 payload->pbn, 4958 payload->dsc_enabled ? "Y" : "N", 4959 status[payload->payload_allocation_status], 4960 (*name != 0) ? name : "Unknown"); 4961 } 4962 } 4963 4964 seq_printf(m, "\n*** DPCD Info ***\n"); 4965 mutex_lock(&mgr->lock); 4966 if (mgr->mst_primary) { 4967 u8 buf[DP_PAYLOAD_TABLE_SIZE]; 4968 int ret; 4969 4970 if (drm_dp_read_dpcd_caps(mgr->aux, buf) < 0) { 4971 seq_printf(m, "dpcd read failed\n"); 4972 goto out; 4973 } 4974 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf); 4975 4976 ret = drm_dp_dpcd_read_data(mgr->aux, DP_FAUX_CAP, buf, 2); 4977 if (ret < 0) { 4978 seq_printf(m, "faux/mst read failed\n"); 4979 goto out; 4980 } 4981 seq_printf(m, "faux/mst: %*ph\n", 2, buf); 4982 4983 ret = drm_dp_dpcd_read_data(mgr->aux, DP_MSTM_CTRL, buf, 1); 4984 if (ret < 0) { 4985 seq_printf(m, "mst ctrl read failed\n"); 4986 goto out; 4987 } 4988 seq_printf(m, "mst ctrl: %*ph\n", 1, buf); 4989 4990 /* dump the standard OUI branch header */ 4991 ret = drm_dp_dpcd_read_data(mgr->aux, DP_BRANCH_OUI, buf, 4992 DP_BRANCH_OUI_HEADER_SIZE); 4993 if (ret < 0) { 4994 seq_printf(m, "branch oui read failed\n"); 4995 goto out; 4996 } 4997 seq_printf(m, "branch oui: %*phN devid: ", 3, buf); 4998 4999 for (i = 0x3; i < 0x8 && buf[i]; i++) 5000 seq_putc(m, buf[i]); 5001 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n", 5002 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]); 5003 if (dump_dp_payload_table(mgr, buf)) 5004 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf); 5005 } 5006 5007 out: 5008 mutex_unlock(&mgr->lock); 5009 drm_modeset_unlock(&mgr->base.lock); 5010 } 5011 EXPORT_SYMBOL(drm_dp_mst_dump_topology); 5012 5013 static void drm_dp_tx_work(struct work_struct *work) 5014 { 5015 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work); 5016 5017 mutex_lock(&mgr->qlock); 5018 if (!list_empty(&mgr->tx_msg_downq)) 5019 process_single_down_tx_qlock(mgr); 5020 mutex_unlock(&mgr->qlock); 5021 } 5022 5023 static inline void 5024 drm_dp_delayed_destroy_port(struct drm_dp_mst_port *port) 5025 { 5026 drm_dp_port_set_pdt(port, DP_PEER_DEVICE_NONE, port->mcs); 5027 5028 if (port->connector) { 5029 drm_connector_unregister(port->connector); 5030 drm_connector_put(port->connector); 5031 } 5032 5033 drm_dp_mst_put_port_malloc(port); 5034 } 5035 5036 static inline void 5037 drm_dp_delayed_destroy_mstb(struct drm_dp_mst_branch *mstb) 5038 { 5039 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr; 5040 struct drm_dp_mst_port *port, *port_tmp; 5041 struct drm_dp_sideband_msg_tx *txmsg, *txmsg_tmp; 5042 bool wake_tx = false; 5043 5044 mutex_lock(&mgr->lock); 5045 list_for_each_entry_safe(port, port_tmp, &mstb->ports, next) { 5046 list_del(&port->next); 5047 drm_dp_mst_topology_put_port(port); 5048 } 5049 mutex_unlock(&mgr->lock); 5050 5051 /* drop any tx slot msg */ 5052 mutex_lock(&mstb->mgr->qlock); 5053 list_for_each_entry_safe(txmsg, txmsg_tmp, &mgr->tx_msg_downq, next) { 5054 if (txmsg->dst != mstb) 5055 continue; 5056 5057 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT; 5058 list_del(&txmsg->next); 5059 wake_tx = true; 5060 } 5061 mutex_unlock(&mstb->mgr->qlock); 5062 5063 if (wake_tx) 5064 wake_up_all(&mstb->mgr->tx_waitq); 5065 5066 drm_dp_mst_put_mstb_malloc(mstb); 5067 } 5068 5069 static void drm_dp_delayed_destroy_work(struct work_struct *work) 5070 { 5071 struct drm_dp_mst_topology_mgr *mgr = 5072 container_of(work, struct drm_dp_mst_topology_mgr, 5073 delayed_destroy_work); 5074 bool send_hotplug = false, go_again; 5075 5076 /* 5077 * Not a regular list traverse as we have to drop the destroy 5078 * connector lock before destroying the mstb/port, to avoid AB->BA 5079 * ordering between this lock and the config mutex. 5080 */ 5081 do { 5082 go_again = false; 5083 5084 for (;;) { 5085 struct drm_dp_mst_branch *mstb; 5086 5087 mutex_lock(&mgr->delayed_destroy_lock); 5088 mstb = list_first_entry_or_null(&mgr->destroy_branch_device_list, 5089 struct drm_dp_mst_branch, 5090 destroy_next); 5091 if (mstb) 5092 list_del(&mstb->destroy_next); 5093 mutex_unlock(&mgr->delayed_destroy_lock); 5094 5095 if (!mstb) 5096 break; 5097 5098 drm_dp_delayed_destroy_mstb(mstb); 5099 go_again = true; 5100 } 5101 5102 for (;;) { 5103 struct drm_dp_mst_port *port; 5104 5105 mutex_lock(&mgr->delayed_destroy_lock); 5106 port = list_first_entry_or_null(&mgr->destroy_port_list, 5107 struct drm_dp_mst_port, 5108 next); 5109 if (port) 5110 list_del(&port->next); 5111 mutex_unlock(&mgr->delayed_destroy_lock); 5112 5113 if (!port) 5114 break; 5115 5116 drm_dp_delayed_destroy_port(port); 5117 send_hotplug = true; 5118 go_again = true; 5119 } 5120 } while (go_again); 5121 5122 if (send_hotplug) 5123 drm_kms_helper_hotplug_event(mgr->dev); 5124 } 5125 5126 static struct drm_private_state * 5127 drm_dp_mst_duplicate_state(struct drm_private_obj *obj) 5128 { 5129 struct drm_dp_mst_topology_state *state, *old_state = 5130 to_dp_mst_topology_state(obj->state); 5131 struct drm_dp_mst_atomic_payload *pos, *payload; 5132 5133 state = kmemdup(old_state, sizeof(*state), GFP_KERNEL); 5134 if (!state) 5135 return NULL; 5136 5137 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 5138 5139 INIT_LIST_HEAD(&state->payloads); 5140 state->commit_deps = NULL; 5141 state->num_commit_deps = 0; 5142 state->pending_crtc_mask = 0; 5143 5144 list_for_each_entry(pos, &old_state->payloads, next) { 5145 /* Prune leftover freed timeslot allocations */ 5146 if (pos->delete) 5147 continue; 5148 5149 payload = kmemdup(pos, sizeof(*payload), GFP_KERNEL); 5150 if (!payload) 5151 goto fail; 5152 5153 drm_dp_mst_get_port_malloc(payload->port); 5154 list_add(&payload->next, &state->payloads); 5155 } 5156 5157 return &state->base; 5158 5159 fail: 5160 list_for_each_entry_safe(pos, payload, &state->payloads, next) { 5161 drm_dp_mst_put_port_malloc(pos->port); 5162 kfree(pos); 5163 } 5164 kfree(state); 5165 5166 return NULL; 5167 } 5168 5169 static void drm_dp_mst_destroy_state(struct drm_private_obj *obj, 5170 struct drm_private_state *state) 5171 { 5172 struct drm_dp_mst_topology_state *mst_state = 5173 to_dp_mst_topology_state(state); 5174 struct drm_dp_mst_atomic_payload *pos, *tmp; 5175 int i; 5176 5177 list_for_each_entry_safe(pos, tmp, &mst_state->payloads, next) { 5178 /* We only keep references to ports with active payloads */ 5179 if (!pos->delete) 5180 drm_dp_mst_put_port_malloc(pos->port); 5181 kfree(pos); 5182 } 5183 5184 for (i = 0; i < mst_state->num_commit_deps; i++) 5185 drm_crtc_commit_put(mst_state->commit_deps[i]); 5186 5187 kfree(mst_state->commit_deps); 5188 kfree(mst_state); 5189 } 5190 5191 static struct drm_private_state * 5192 drm_dp_mst_atomic_create_state(struct drm_private_obj *obj) 5193 { 5194 struct drm_dp_mst_topology_mgr *mgr = 5195 to_dp_mst_topology_mgr(obj); 5196 struct drm_dp_mst_topology_state *mst_state; 5197 5198 mst_state = kzalloc_obj(*mst_state); 5199 if (!mst_state) 5200 return ERR_PTR(-ENOMEM); 5201 5202 __drm_atomic_helper_private_obj_create_state(obj, &mst_state->base); 5203 5204 mst_state->total_avail_slots = 63; 5205 mst_state->start_slot = 1; 5206 5207 mst_state->mgr = mgr; 5208 INIT_LIST_HEAD(&mst_state->payloads); 5209 5210 return &mst_state->base; 5211 } 5212 5213 static bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port, 5214 struct drm_dp_mst_branch *branch) 5215 { 5216 while (port->parent) { 5217 if (port->parent == branch) 5218 return true; 5219 5220 if (port->parent->port_parent) 5221 port = port->parent->port_parent; 5222 else 5223 break; 5224 } 5225 return false; 5226 } 5227 5228 static bool 5229 drm_dp_mst_port_downstream_of_parent_locked(struct drm_dp_mst_topology_mgr *mgr, 5230 struct drm_dp_mst_port *port, 5231 struct drm_dp_mst_port *parent) 5232 { 5233 if (!mgr->mst_primary) 5234 return false; 5235 5236 port = drm_dp_mst_topology_get_port_validated_locked(mgr->mst_primary, 5237 port); 5238 if (!port) 5239 return false; 5240 5241 if (!parent) 5242 return true; 5243 5244 parent = drm_dp_mst_topology_get_port_validated_locked(mgr->mst_primary, 5245 parent); 5246 if (!parent) 5247 return false; 5248 5249 if (!parent->mstb) 5250 return false; 5251 5252 return drm_dp_mst_port_downstream_of_branch(port, parent->mstb); 5253 } 5254 5255 /** 5256 * drm_dp_mst_port_downstream_of_parent - check if a port is downstream of a parent port 5257 * @mgr: MST topology manager 5258 * @port: the port being looked up 5259 * @parent: the parent port 5260 * 5261 * The function returns %true if @port is downstream of @parent. If @parent is 5262 * %NULL - denoting the root port - the function returns %true if @port is in 5263 * @mgr's topology. 5264 */ 5265 bool 5266 drm_dp_mst_port_downstream_of_parent(struct drm_dp_mst_topology_mgr *mgr, 5267 struct drm_dp_mst_port *port, 5268 struct drm_dp_mst_port *parent) 5269 { 5270 bool ret; 5271 5272 mutex_lock(&mgr->lock); 5273 ret = drm_dp_mst_port_downstream_of_parent_locked(mgr, port, parent); 5274 mutex_unlock(&mgr->lock); 5275 5276 return ret; 5277 } 5278 EXPORT_SYMBOL(drm_dp_mst_port_downstream_of_parent); 5279 5280 static int 5281 drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port, 5282 struct drm_dp_mst_topology_state *state, 5283 struct drm_dp_mst_port **failing_port); 5284 5285 static int 5286 drm_dp_mst_atomic_check_mstb_bw_limit(struct drm_dp_mst_branch *mstb, 5287 struct drm_dp_mst_topology_state *state, 5288 struct drm_dp_mst_port **failing_port) 5289 { 5290 struct drm_dp_mst_atomic_payload *payload; 5291 struct drm_dp_mst_port *port; 5292 int pbn_used = 0, ret; 5293 bool found = false; 5294 5295 /* Check that we have at least one port in our state that's downstream 5296 * of this branch, otherwise we can skip this branch 5297 */ 5298 list_for_each_entry(payload, &state->payloads, next) { 5299 if (!payload->pbn || 5300 !drm_dp_mst_port_downstream_of_branch(payload->port, mstb)) 5301 continue; 5302 5303 found = true; 5304 break; 5305 } 5306 if (!found) 5307 return 0; 5308 5309 if (mstb->port_parent) 5310 drm_dbg_atomic(mstb->mgr->dev, 5311 "[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n", 5312 mstb->port_parent->parent, mstb->port_parent, mstb); 5313 else 5314 drm_dbg_atomic(mstb->mgr->dev, "[MSTB:%p] Checking bandwidth limits\n", mstb); 5315 5316 list_for_each_entry(port, &mstb->ports, next) { 5317 ret = drm_dp_mst_atomic_check_port_bw_limit(port, state, failing_port); 5318 if (ret < 0) 5319 return ret; 5320 5321 pbn_used += ret; 5322 } 5323 5324 return pbn_used; 5325 } 5326 5327 static int 5328 drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port, 5329 struct drm_dp_mst_topology_state *state, 5330 struct drm_dp_mst_port **failing_port) 5331 { 5332 struct drm_dp_mst_atomic_payload *payload; 5333 int pbn_used = 0; 5334 5335 if (port->pdt == DP_PEER_DEVICE_NONE) 5336 return 0; 5337 5338 if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) { 5339 payload = drm_atomic_get_mst_payload_state(state, port); 5340 if (!payload) 5341 return 0; 5342 5343 /* 5344 * This could happen if the sink deasserted its HPD line, but 5345 * the branch device still reports it as attached (PDT != NONE). 5346 */ 5347 if (!port->full_pbn) { 5348 drm_dbg_atomic(port->mgr->dev, 5349 "[MSTB:%p] [MST PORT:%p] no BW available for the port\n", 5350 port->parent, port); 5351 *failing_port = port; 5352 return -EINVAL; 5353 } 5354 5355 pbn_used = payload->pbn; 5356 } else { 5357 pbn_used = drm_dp_mst_atomic_check_mstb_bw_limit(port->mstb, 5358 state, 5359 failing_port); 5360 if (pbn_used <= 0) 5361 return pbn_used; 5362 } 5363 5364 if (pbn_used > port->full_pbn) { 5365 drm_dbg_atomic(port->mgr->dev, 5366 "[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n", 5367 port->parent, port, pbn_used, port->full_pbn); 5368 *failing_port = port; 5369 return -ENOSPC; 5370 } 5371 5372 drm_dbg_atomic(port->mgr->dev, "[MSTB:%p] [MST PORT:%p] uses %d out of %d PBN\n", 5373 port->parent, port, pbn_used, port->full_pbn); 5374 5375 return pbn_used; 5376 } 5377 5378 static inline int 5379 drm_dp_mst_atomic_check_payload_alloc_limits(struct drm_dp_mst_topology_mgr *mgr, 5380 struct drm_dp_mst_topology_state *mst_state) 5381 { 5382 struct drm_dp_mst_atomic_payload *payload; 5383 int avail_slots = mst_state->total_avail_slots, payload_count = 0; 5384 5385 list_for_each_entry(payload, &mst_state->payloads, next) { 5386 /* Releasing payloads is always OK-even if the port is gone */ 5387 if (payload->delete) { 5388 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] releases all time slots\n", 5389 payload->port); 5390 continue; 5391 } 5392 5393 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] requires %d time slots\n", 5394 payload->port, payload->time_slots); 5395 5396 avail_slots -= payload->time_slots; 5397 if (avail_slots < 0) { 5398 drm_dbg_atomic(mgr->dev, 5399 "[MST PORT:%p] not enough time slots in mst state %p (avail=%d)\n", 5400 payload->port, mst_state, avail_slots + payload->time_slots); 5401 return -ENOSPC; 5402 } 5403 5404 if (++payload_count > mgr->max_payloads) { 5405 drm_dbg_atomic(mgr->dev, 5406 "[MST MGR:%p] state %p has too many payloads (max=%d)\n", 5407 mgr, mst_state, mgr->max_payloads); 5408 return -EINVAL; 5409 } 5410 5411 /* Assign a VCPI */ 5412 if (!payload->vcpi) { 5413 payload->vcpi = ffz(mst_state->payload_mask) + 1; 5414 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] assigned VCPI #%d\n", 5415 payload->port, payload->vcpi); 5416 mst_state->payload_mask |= BIT(payload->vcpi - 1); 5417 } 5418 } 5419 5420 if (!payload_count) 5421 mst_state->pbn_div.full = dfixed_const(0); 5422 5423 drm_dbg_atomic(mgr->dev, "[MST MGR:%p] mst state %p TU pbn_div=%d avail=%d used=%d\n", 5424 mgr, mst_state, dfixed_trunc(mst_state->pbn_div), avail_slots, 5425 mst_state->total_avail_slots - avail_slots); 5426 5427 return 0; 5428 } 5429 5430 /** 5431 * drm_dp_mst_add_affected_dsc_crtcs 5432 * @state: Pointer to the new struct drm_dp_mst_topology_state 5433 * @mgr: MST topology manager 5434 * 5435 * Whenever there is a change in mst topology 5436 * DSC configuration would have to be recalculated 5437 * therefore we need to trigger modeset on all affected 5438 * CRTCs in that topology 5439 * 5440 * See also: 5441 * drm_dp_mst_atomic_enable_dsc() 5442 */ 5443 int drm_dp_mst_add_affected_dsc_crtcs(struct drm_atomic_commit *state, struct drm_dp_mst_topology_mgr *mgr) 5444 { 5445 struct drm_dp_mst_topology_state *mst_state; 5446 struct drm_dp_mst_atomic_payload *pos; 5447 struct drm_connector *connector; 5448 struct drm_connector_state *conn_state; 5449 struct drm_crtc *crtc; 5450 struct drm_crtc_state *crtc_state; 5451 5452 mst_state = drm_atomic_get_mst_topology_state(state, mgr); 5453 5454 if (IS_ERR(mst_state)) 5455 return PTR_ERR(mst_state); 5456 5457 list_for_each_entry(pos, &mst_state->payloads, next) { 5458 5459 connector = pos->port->connector; 5460 5461 if (!connector) 5462 return -EINVAL; 5463 5464 conn_state = drm_atomic_get_connector_state(state, connector); 5465 5466 if (IS_ERR(conn_state)) 5467 return PTR_ERR(conn_state); 5468 5469 crtc = conn_state->crtc; 5470 5471 if (!crtc) 5472 continue; 5473 5474 if (!drm_dp_mst_dsc_aux_for_port(pos->port)) 5475 continue; 5476 5477 crtc_state = drm_atomic_get_crtc_state(mst_state->base.state, crtc); 5478 5479 if (IS_ERR(crtc_state)) 5480 return PTR_ERR(crtc_state); 5481 5482 drm_dbg_atomic(mgr->dev, "[MST MGR:%p] Setting mode_changed flag on CRTC %p\n", 5483 mgr, crtc); 5484 5485 crtc_state->mode_changed = true; 5486 } 5487 return 0; 5488 } 5489 EXPORT_SYMBOL(drm_dp_mst_add_affected_dsc_crtcs); 5490 5491 /** 5492 * drm_dp_mst_atomic_enable_dsc - Set DSC Enable Flag to On/Off 5493 * @state: Pointer to the new drm_atomic_commit 5494 * @port: Pointer to the affected MST Port 5495 * @pbn: Newly recalculated bw required for link with DSC enabled 5496 * @enable: Boolean flag to enable or disable DSC on the port 5497 * 5498 * This function enables DSC on the given Port 5499 * by recalculating its vcpi from pbn provided 5500 * and sets dsc_enable flag to keep track of which 5501 * ports have DSC enabled 5502 * 5503 */ 5504 int drm_dp_mst_atomic_enable_dsc(struct drm_atomic_commit *state, 5505 struct drm_dp_mst_port *port, 5506 int pbn, bool enable) 5507 { 5508 struct drm_dp_mst_topology_state *mst_state; 5509 struct drm_dp_mst_atomic_payload *payload; 5510 int time_slots = 0; 5511 5512 mst_state = drm_atomic_get_mst_topology_state(state, port->mgr); 5513 if (IS_ERR(mst_state)) 5514 return PTR_ERR(mst_state); 5515 5516 payload = drm_atomic_get_mst_payload_state(mst_state, port); 5517 if (!payload) { 5518 drm_dbg_atomic(state->dev, 5519 "[MST PORT:%p] Couldn't find payload in mst state %p\n", 5520 port, mst_state); 5521 return -EINVAL; 5522 } 5523 5524 if (payload->dsc_enabled == enable) { 5525 drm_dbg_atomic(state->dev, 5526 "[MST PORT:%p] DSC flag is already set to %d, returning %d time slots\n", 5527 port, enable, payload->time_slots); 5528 time_slots = payload->time_slots; 5529 } 5530 5531 if (enable) { 5532 time_slots = drm_dp_atomic_find_time_slots(state, port->mgr, port, pbn); 5533 drm_dbg_atomic(state->dev, 5534 "[MST PORT:%p] Enabling DSC flag, reallocating %d time slots on the port\n", 5535 port, time_slots); 5536 if (time_slots < 0) 5537 return -EINVAL; 5538 } 5539 5540 payload->dsc_enabled = enable; 5541 5542 return time_slots; 5543 } 5544 EXPORT_SYMBOL(drm_dp_mst_atomic_enable_dsc); 5545 5546 /** 5547 * drm_dp_mst_atomic_check_mgr - Check the atomic state of an MST topology manager 5548 * @state: The global atomic state 5549 * @mgr: Manager to check 5550 * @mst_state: The MST atomic state for @mgr 5551 * @failing_port: Returns the port with a BW limitation 5552 * 5553 * Checks the given MST manager's topology state for an atomic update to ensure 5554 * that it's valid. This includes checking whether there's enough bandwidth to 5555 * support the new timeslot allocations in the atomic update. 5556 * 5557 * Any atomic drivers supporting DP MST must make sure to call this or 5558 * the drm_dp_mst_atomic_check() function after checking the rest of their state 5559 * in their &drm_mode_config_funcs.atomic_check() callback. 5560 * 5561 * See also: 5562 * drm_dp_mst_atomic_check() 5563 * drm_dp_atomic_find_time_slots() 5564 * drm_dp_atomic_release_time_slots() 5565 * 5566 * Returns: 5567 * - 0 if the new state is valid 5568 * - %-ENOSPC, if the new state is invalid, because of BW limitation 5569 * @failing_port is set to: 5570 * 5571 * - The non-root port where a BW limit check failed 5572 * with all the ports downstream of @failing_port passing 5573 * the BW limit check. 5574 * The returned port pointer is valid until at least 5575 * one payload downstream of it exists. 5576 * - %NULL if the BW limit check failed at the root port 5577 * with all the ports downstream of the root port passing 5578 * the BW limit check. 5579 * 5580 * - %-EINVAL, if the new state is invalid, because the root port has 5581 * too many payloads. 5582 */ 5583 int drm_dp_mst_atomic_check_mgr(struct drm_atomic_commit *state, 5584 struct drm_dp_mst_topology_mgr *mgr, 5585 struct drm_dp_mst_topology_state *mst_state, 5586 struct drm_dp_mst_port **failing_port) 5587 { 5588 int ret; 5589 5590 *failing_port = NULL; 5591 5592 if (!mgr->mst_state) 5593 return 0; 5594 5595 mutex_lock(&mgr->lock); 5596 ret = drm_dp_mst_atomic_check_mstb_bw_limit(mgr->mst_primary, 5597 mst_state, 5598 failing_port); 5599 mutex_unlock(&mgr->lock); 5600 5601 if (ret < 0) 5602 return ret; 5603 5604 return drm_dp_mst_atomic_check_payload_alloc_limits(mgr, mst_state); 5605 } 5606 EXPORT_SYMBOL(drm_dp_mst_atomic_check_mgr); 5607 5608 /** 5609 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an 5610 * atomic update is valid 5611 * @state: Pointer to the new &struct drm_dp_mst_topology_state 5612 * 5613 * Checks the given topology state for an atomic update to ensure that it's 5614 * valid, calling drm_dp_mst_atomic_check_mgr() for all MST manager in the 5615 * atomic state. This includes checking whether there's enough bandwidth to 5616 * support the new timeslot allocations in the atomic update. 5617 * 5618 * Any atomic drivers supporting DP MST must make sure to call this after 5619 * checking the rest of their state in their 5620 * &drm_mode_config_funcs.atomic_check() callback. 5621 * 5622 * See also: 5623 * drm_dp_mst_atomic_check_mgr() 5624 * drm_dp_atomic_find_time_slots() 5625 * drm_dp_atomic_release_time_slots() 5626 * 5627 * Returns: 5628 * 0 if the new state is valid, negative error code otherwise. 5629 */ 5630 int drm_dp_mst_atomic_check(struct drm_atomic_commit *state) 5631 { 5632 struct drm_dp_mst_topology_mgr *mgr; 5633 struct drm_dp_mst_topology_state *mst_state; 5634 int i, ret = 0; 5635 5636 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) { 5637 struct drm_dp_mst_port *tmp_port; 5638 5639 ret = drm_dp_mst_atomic_check_mgr(state, mgr, mst_state, &tmp_port); 5640 if (ret) 5641 break; 5642 } 5643 5644 return ret; 5645 } 5646 EXPORT_SYMBOL(drm_dp_mst_atomic_check); 5647 5648 const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = { 5649 .atomic_create_state = drm_dp_mst_atomic_create_state, 5650 .atomic_duplicate_state = drm_dp_mst_duplicate_state, 5651 .atomic_destroy_state = drm_dp_mst_destroy_state, 5652 }; 5653 EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs); 5654 5655 /** 5656 * drm_atomic_get_mst_topology_state: get MST topology state 5657 * @state: global atomic state 5658 * @mgr: MST topology manager, also the private object in this case 5659 * 5660 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic 5661 * state vtable so that the private object state returned is that of a MST 5662 * topology object. 5663 * 5664 * RETURNS: 5665 * The MST topology state or error pointer. 5666 */ 5667 struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_commit *state, 5668 struct drm_dp_mst_topology_mgr *mgr) 5669 { 5670 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base)); 5671 } 5672 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state); 5673 5674 /** 5675 * drm_atomic_get_old_mst_topology_state: get old MST topology state in atomic state, if any 5676 * @state: global atomic state 5677 * @mgr: MST topology manager, also the private object in this case 5678 * 5679 * This function wraps drm_atomic_get_old_private_obj_state() passing in the MST atomic 5680 * state vtable so that the private object state returned is that of a MST 5681 * topology object. 5682 * 5683 * Returns: 5684 * The old MST topology state, or NULL if there's no topology state for this MST mgr 5685 * in the global atomic state 5686 */ 5687 struct drm_dp_mst_topology_state * 5688 drm_atomic_get_old_mst_topology_state(struct drm_atomic_commit *state, 5689 struct drm_dp_mst_topology_mgr *mgr) 5690 { 5691 struct drm_private_state *old_priv_state = 5692 drm_atomic_get_old_private_obj_state(state, &mgr->base); 5693 5694 return old_priv_state ? to_dp_mst_topology_state(old_priv_state) : NULL; 5695 } 5696 EXPORT_SYMBOL(drm_atomic_get_old_mst_topology_state); 5697 5698 /** 5699 * drm_atomic_get_new_mst_topology_state: get new MST topology state in atomic state, if any 5700 * @state: global atomic state 5701 * @mgr: MST topology manager, also the private object in this case 5702 * 5703 * This function wraps drm_atomic_get_new_private_obj_state() passing in the MST atomic 5704 * state vtable so that the private object state returned is that of a MST 5705 * topology object. 5706 * 5707 * Returns: 5708 * The new MST topology state, or NULL if there's no topology state for this MST mgr 5709 * in the global atomic state 5710 */ 5711 struct drm_dp_mst_topology_state * 5712 drm_atomic_get_new_mst_topology_state(struct drm_atomic_commit *state, 5713 struct drm_dp_mst_topology_mgr *mgr) 5714 { 5715 struct drm_private_state *new_priv_state = 5716 drm_atomic_get_new_private_obj_state(state, &mgr->base); 5717 5718 return new_priv_state ? to_dp_mst_topology_state(new_priv_state) : NULL; 5719 } 5720 EXPORT_SYMBOL(drm_atomic_get_new_mst_topology_state); 5721 5722 /** 5723 * drm_dp_mst_topology_mgr_init - initialise a topology manager 5724 * @mgr: manager struct to initialise 5725 * @dev: device providing this structure - for i2c addition. 5726 * @aux: DP helper aux channel to talk to this device 5727 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit 5728 * @max_payloads: maximum number of payloads this GPU can source 5729 * @conn_base_id: the connector object ID the MST device is connected to. 5730 * 5731 * Return 0 for success, or negative error code on failure 5732 */ 5733 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr, 5734 struct drm_device *dev, struct drm_dp_aux *aux, 5735 int max_dpcd_transaction_bytes, int max_payloads, 5736 int conn_base_id) 5737 { 5738 mutex_init(&mgr->lock); 5739 mutex_init(&mgr->qlock); 5740 mutex_init(&mgr->delayed_destroy_lock); 5741 mutex_init(&mgr->up_req_lock); 5742 mutex_init(&mgr->probe_lock); 5743 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) 5744 mutex_init(&mgr->topology_ref_history_lock); 5745 stack_depot_init(); 5746 #endif 5747 INIT_LIST_HEAD(&mgr->tx_msg_downq); 5748 INIT_LIST_HEAD(&mgr->destroy_port_list); 5749 INIT_LIST_HEAD(&mgr->destroy_branch_device_list); 5750 INIT_LIST_HEAD(&mgr->up_req_list); 5751 5752 /* 5753 * delayed_destroy_work will be queued on a dedicated WQ, so that any 5754 * requeuing will be also flushed when deiniting the topology manager. 5755 */ 5756 mgr->delayed_destroy_wq = alloc_ordered_workqueue("drm_dp_mst_wq", 0); 5757 if (mgr->delayed_destroy_wq == NULL) 5758 return -ENOMEM; 5759 5760 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work); 5761 INIT_WORK(&mgr->tx_work, drm_dp_tx_work); 5762 INIT_WORK(&mgr->delayed_destroy_work, drm_dp_delayed_destroy_work); 5763 INIT_WORK(&mgr->up_req_work, drm_dp_mst_up_req_work); 5764 init_waitqueue_head(&mgr->tx_waitq); 5765 mgr->dev = dev; 5766 mgr->aux = aux; 5767 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes; 5768 mgr->max_payloads = max_payloads; 5769 mgr->conn_base_id = conn_base_id; 5770 5771 drm_atomic_private_obj_init(dev, &mgr->base, 5772 &drm_dp_mst_topology_state_funcs); 5773 5774 return 0; 5775 } 5776 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init); 5777 5778 /** 5779 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager. 5780 * @mgr: manager to destroy 5781 */ 5782 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr) 5783 { 5784 drm_dp_mst_topology_mgr_set_mst(mgr, false); 5785 flush_work(&mgr->work); 5786 /* The following will also drain any requeued work on the WQ. */ 5787 if (mgr->delayed_destroy_wq) { 5788 destroy_workqueue(mgr->delayed_destroy_wq); 5789 mgr->delayed_destroy_wq = NULL; 5790 } 5791 mgr->dev = NULL; 5792 mgr->aux = NULL; 5793 drm_atomic_private_obj_fini(&mgr->base); 5794 mgr->funcs = NULL; 5795 5796 mutex_destroy(&mgr->delayed_destroy_lock); 5797 mutex_destroy(&mgr->qlock); 5798 mutex_destroy(&mgr->lock); 5799 mutex_destroy(&mgr->up_req_lock); 5800 mutex_destroy(&mgr->probe_lock); 5801 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) 5802 mutex_destroy(&mgr->topology_ref_history_lock); 5803 #endif 5804 } 5805 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy); 5806 5807 static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num) 5808 { 5809 int i; 5810 5811 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS) 5812 return false; 5813 5814 for (i = 0; i < num - 1; i++) { 5815 if (msgs[i].flags & I2C_M_RD || 5816 msgs[i].len > 0xff) 5817 return false; 5818 } 5819 5820 return msgs[num - 1].flags & I2C_M_RD && 5821 msgs[num - 1].len <= 0xff; 5822 } 5823 5824 static bool remote_i2c_write_ok(const struct i2c_msg msgs[], int num) 5825 { 5826 int i; 5827 5828 for (i = 0; i < num - 1; i++) { 5829 if (msgs[i].flags & I2C_M_RD || !(msgs[i].flags & I2C_M_STOP) || 5830 msgs[i].len > 0xff) 5831 return false; 5832 } 5833 5834 return !(msgs[num - 1].flags & I2C_M_RD) && msgs[num - 1].len <= 0xff; 5835 } 5836 5837 static int drm_dp_mst_i2c_read(struct drm_dp_mst_branch *mstb, 5838 struct drm_dp_mst_port *port, 5839 struct i2c_msg *msgs, int num) 5840 { 5841 struct drm_dp_mst_topology_mgr *mgr = port->mgr; 5842 unsigned int i; 5843 struct drm_dp_sideband_msg_req_body msg; 5844 struct drm_dp_sideband_msg_tx *txmsg = NULL; 5845 int ret; 5846 5847 memset(&msg, 0, sizeof(msg)); 5848 msg.req_type = DP_REMOTE_I2C_READ; 5849 msg.u.i2c_read.num_transactions = num - 1; 5850 msg.u.i2c_read.port_number = port->port_num; 5851 for (i = 0; i < num - 1; i++) { 5852 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr; 5853 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len; 5854 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf; 5855 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP); 5856 } 5857 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr; 5858 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len; 5859 5860 txmsg = kzalloc_obj(*txmsg); 5861 if (!txmsg) { 5862 ret = -ENOMEM; 5863 goto out; 5864 } 5865 5866 txmsg->dst = mstb; 5867 drm_dp_encode_sideband_req(&msg, txmsg); 5868 5869 drm_dp_queue_down_tx(mgr, txmsg); 5870 5871 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 5872 if (ret > 0) { 5873 5874 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) { 5875 ret = -EREMOTEIO; 5876 goto out; 5877 } 5878 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) { 5879 ret = -EIO; 5880 goto out; 5881 } 5882 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len); 5883 ret = num; 5884 } 5885 out: 5886 kfree(txmsg); 5887 return ret; 5888 } 5889 5890 static int drm_dp_mst_i2c_write(struct drm_dp_mst_branch *mstb, 5891 struct drm_dp_mst_port *port, 5892 struct i2c_msg *msgs, int num) 5893 { 5894 struct drm_dp_mst_topology_mgr *mgr = port->mgr; 5895 unsigned int i; 5896 struct drm_dp_sideband_msg_req_body msg; 5897 struct drm_dp_sideband_msg_tx *txmsg = NULL; 5898 int ret; 5899 5900 txmsg = kzalloc_obj(*txmsg); 5901 if (!txmsg) { 5902 ret = -ENOMEM; 5903 goto out; 5904 } 5905 for (i = 0; i < num; i++) { 5906 memset(&msg, 0, sizeof(msg)); 5907 msg.req_type = DP_REMOTE_I2C_WRITE; 5908 msg.u.i2c_write.port_number = port->port_num; 5909 msg.u.i2c_write.write_i2c_device_id = msgs[i].addr; 5910 msg.u.i2c_write.num_bytes = msgs[i].len; 5911 msg.u.i2c_write.bytes = msgs[i].buf; 5912 5913 memset(txmsg, 0, sizeof(*txmsg)); 5914 txmsg->dst = mstb; 5915 5916 drm_dp_encode_sideband_req(&msg, txmsg); 5917 drm_dp_queue_down_tx(mgr, txmsg); 5918 5919 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 5920 if (ret > 0) { 5921 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) { 5922 ret = -EREMOTEIO; 5923 goto out; 5924 } 5925 } else { 5926 goto out; 5927 } 5928 } 5929 ret = num; 5930 out: 5931 kfree(txmsg); 5932 return ret; 5933 } 5934 5935 /* I2C device */ 5936 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, 5937 struct i2c_msg *msgs, int num) 5938 { 5939 struct drm_dp_aux *aux = adapter->algo_data; 5940 struct drm_dp_mst_port *port = 5941 container_of(aux, struct drm_dp_mst_port, aux); 5942 struct drm_dp_mst_branch *mstb; 5943 struct drm_dp_mst_topology_mgr *mgr = port->mgr; 5944 int ret; 5945 5946 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent); 5947 if (!mstb) 5948 return -EREMOTEIO; 5949 5950 if (remote_i2c_read_ok(msgs, num)) { 5951 ret = drm_dp_mst_i2c_read(mstb, port, msgs, num); 5952 } else if (remote_i2c_write_ok(msgs, num)) { 5953 ret = drm_dp_mst_i2c_write(mstb, port, msgs, num); 5954 } else { 5955 drm_dbg_kms(mgr->dev, "Unsupported I2C transaction for MST device\n"); 5956 ret = -EIO; 5957 } 5958 5959 drm_dp_mst_topology_put_mstb(mstb); 5960 return ret; 5961 } 5962 5963 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter) 5964 { 5965 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | 5966 I2C_FUNC_SMBUS_READ_BLOCK_DATA | 5967 I2C_FUNC_SMBUS_BLOCK_PROC_CALL | 5968 I2C_FUNC_10BIT_ADDR; 5969 } 5970 5971 static const struct i2c_algorithm drm_dp_mst_i2c_algo = { 5972 .functionality = drm_dp_mst_i2c_functionality, 5973 .master_xfer = drm_dp_mst_i2c_xfer, 5974 }; 5975 5976 /** 5977 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX 5978 * @port: The port to add the I2C bus on 5979 * 5980 * Returns 0 on success or a negative error code on failure. 5981 */ 5982 static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port *port) 5983 { 5984 struct drm_dp_aux *aux = &port->aux; 5985 struct device *parent_dev = port->mgr->dev->dev; 5986 5987 aux->ddc.algo = &drm_dp_mst_i2c_algo; 5988 aux->ddc.algo_data = aux; 5989 aux->ddc.retries = 3; 5990 5991 aux->ddc.owner = THIS_MODULE; 5992 /* FIXME: set the kdev of the port's connector as parent */ 5993 aux->ddc.dev.parent = parent_dev; 5994 aux->ddc.dev.of_node = parent_dev->of_node; 5995 5996 strscpy(aux->ddc.name, aux->name ? aux->name : dev_name(parent_dev), 5997 sizeof(aux->ddc.name)); 5998 5999 return i2c_add_adapter(&aux->ddc); 6000 } 6001 6002 /** 6003 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter 6004 * @port: The port to remove the I2C bus from 6005 */ 6006 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port *port) 6007 { 6008 i2c_del_adapter(&port->aux.ddc); 6009 } 6010 6011 /** 6012 * drm_dp_mst_is_virtual_dpcd() - Is the given port a virtual DP Peer Device 6013 * @port: The port to check 6014 * 6015 * A single physical MST hub object can be represented in the topology 6016 * by multiple branches, with virtual ports between those branches. 6017 * 6018 * As of DP1.4, An MST hub with internal (virtual) ports must expose 6019 * certain DPCD registers over those ports. See sections 2.6.1.1.1 6020 * and 2.6.1.1.2 of Display Port specification v1.4 for details. 6021 * 6022 * May acquire mgr->lock 6023 * 6024 * Returns: 6025 * true if the port is a virtual DP peer device, false otherwise 6026 */ 6027 static bool drm_dp_mst_is_virtual_dpcd(struct drm_dp_mst_port *port) 6028 { 6029 struct drm_dp_mst_port *downstream_port; 6030 6031 if (!port || port->dpcd_rev < DP_DPCD_REV_14) 6032 return false; 6033 6034 /* Virtual DP Sink (Internal Display Panel) */ 6035 if (drm_dp_mst_port_is_logical(port)) 6036 return true; 6037 6038 /* DP-to-HDMI Protocol Converter */ 6039 if (port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV && 6040 !port->mcs && 6041 port->ldps) 6042 return true; 6043 6044 /* DP-to-DP */ 6045 mutex_lock(&port->mgr->lock); 6046 if (port->pdt == DP_PEER_DEVICE_MST_BRANCHING && 6047 port->mstb && 6048 port->mstb->num_ports == 2) { 6049 list_for_each_entry(downstream_port, &port->mstb->ports, next) { 6050 if (downstream_port->pdt == DP_PEER_DEVICE_SST_SINK && 6051 !downstream_port->input) { 6052 mutex_unlock(&port->mgr->lock); 6053 return true; 6054 } 6055 } 6056 } 6057 mutex_unlock(&port->mgr->lock); 6058 6059 return false; 6060 } 6061 6062 /** 6063 * drm_dp_mst_aux_for_parent() - Get the AUX device for an MST port's parent 6064 * @port: MST port whose parent's AUX device is returned 6065 * 6066 * Return the AUX device for @port's parent or NULL if port's parent is the 6067 * root port. 6068 */ 6069 struct drm_dp_aux *drm_dp_mst_aux_for_parent(struct drm_dp_mst_port *port) 6070 { 6071 if (!port->parent || !port->parent->port_parent) 6072 return NULL; 6073 6074 return &port->parent->port_parent->aux; 6075 } 6076 EXPORT_SYMBOL(drm_dp_mst_aux_for_parent); 6077 6078 /** 6079 * drm_dp_mst_dsc_aux_for_port() - Find the correct aux for DSC 6080 * @port: The port to check. A leaf of the MST tree with an attached display. 6081 * 6082 * Depending on the situation, DSC may be enabled via the endpoint aux, 6083 * the immediately upstream aux, or the connector's physical aux. 6084 * 6085 * This is both the correct aux to read DSC_CAPABILITY and the 6086 * correct aux to write DSC_ENABLED. 6087 * 6088 * This operation can be expensive (up to four aux reads), so 6089 * the caller should cache the return. 6090 * 6091 * Returns: 6092 * NULL if DSC cannot be enabled on this port, otherwise the aux device 6093 */ 6094 struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port) 6095 { 6096 struct drm_dp_mst_port *immediate_upstream_port; 6097 struct drm_dp_aux *immediate_upstream_aux; 6098 struct drm_dp_mst_port *fec_port; 6099 struct drm_dp_desc desc = {}; 6100 u8 upstream_dsc; 6101 u8 endpoint_fec; 6102 u8 endpoint_dsc; 6103 6104 if (!port) 6105 return NULL; 6106 6107 if (port->parent->port_parent) 6108 immediate_upstream_port = port->parent->port_parent; 6109 else 6110 immediate_upstream_port = NULL; 6111 6112 fec_port = immediate_upstream_port; 6113 while (fec_port) { 6114 /* 6115 * Each physical link (i.e. not a virtual port) between the 6116 * output and the primary device must support FEC 6117 */ 6118 if (!drm_dp_mst_is_virtual_dpcd(fec_port) && 6119 !fec_port->fec_capable) 6120 return NULL; 6121 6122 fec_port = fec_port->parent->port_parent; 6123 } 6124 6125 /* DP-to-DP peer device */ 6126 if (drm_dp_mst_is_virtual_dpcd(immediate_upstream_port)) { 6127 if (drm_dp_dpcd_read_data(&port->aux, 6128 DP_DSC_SUPPORT, &endpoint_dsc, 1) < 0) 6129 return NULL; 6130 if (drm_dp_dpcd_read_data(&port->aux, 6131 DP_FEC_CAPABILITY, &endpoint_fec, 1) < 0) 6132 return NULL; 6133 if (drm_dp_dpcd_read_data(&immediate_upstream_port->aux, 6134 DP_DSC_SUPPORT, &upstream_dsc, 1) < 0) 6135 return NULL; 6136 6137 /* Enpoint decompression with DP-to-DP peer device */ 6138 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) && 6139 (endpoint_fec & DP_FEC_CAPABLE) && 6140 (upstream_dsc & DP_DSC_PASSTHROUGH_IS_SUPPORTED)) { 6141 port->passthrough_aux = &immediate_upstream_port->aux; 6142 return &port->aux; 6143 } 6144 6145 /* Virtual DPCD decompression with DP-to-DP peer device */ 6146 return &immediate_upstream_port->aux; 6147 } 6148 6149 /* Virtual DPCD decompression with DP-to-HDMI or Virtual DP Sink */ 6150 if (drm_dp_mst_is_virtual_dpcd(port)) 6151 return &port->aux; 6152 6153 /* 6154 * Synaptics quirk 6155 * Applies to ports for which: 6156 * - Physical aux has Synaptics OUI 6157 * - DPv1.4 or higher 6158 * - Port is on primary branch device 6159 * - Not a VGA adapter (DP_DWN_STRM_PORT_TYPE_ANALOG) 6160 */ 6161 if (immediate_upstream_port) 6162 immediate_upstream_aux = &immediate_upstream_port->aux; 6163 else 6164 immediate_upstream_aux = port->mgr->aux; 6165 6166 if (drm_dp_read_desc(immediate_upstream_aux, &desc, true)) 6167 return NULL; 6168 6169 if (drm_dp_has_quirk(&desc, DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD)) { 6170 u8 dpcd_ext[DP_RECEIVER_CAP_SIZE]; 6171 6172 if (drm_dp_dpcd_read_data(immediate_upstream_aux, 6173 DP_DSC_SUPPORT, &upstream_dsc, 1) < 0) 6174 return NULL; 6175 6176 if (!(upstream_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED)) 6177 return NULL; 6178 6179 if (drm_dp_read_dpcd_caps(immediate_upstream_aux, dpcd_ext) < 0) 6180 return NULL; 6181 6182 if (dpcd_ext[DP_DPCD_REV] >= DP_DPCD_REV_14 && 6183 ((dpcd_ext[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT) && 6184 ((dpcd_ext[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) 6185 != DP_DWN_STRM_PORT_TYPE_ANALOG))) 6186 return immediate_upstream_aux; 6187 } 6188 6189 /* 6190 * The check below verifies if the MST sink 6191 * connected to the GPU is capable of DSC - 6192 * therefore the endpoint needs to be 6193 * both DSC and FEC capable. 6194 */ 6195 if (drm_dp_dpcd_read_data(&port->aux, 6196 DP_DSC_SUPPORT, &endpoint_dsc, 1) < 0) 6197 return NULL; 6198 if (drm_dp_dpcd_read_data(&port->aux, 6199 DP_FEC_CAPABILITY, &endpoint_fec, 1) < 0) 6200 return NULL; 6201 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) && 6202 (endpoint_fec & DP_FEC_CAPABLE)) 6203 return &port->aux; 6204 6205 return NULL; 6206 } 6207 EXPORT_SYMBOL(drm_dp_mst_dsc_aux_for_port); 6208