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 if (!port->ddps) 2165 return -EIO; 2166 2167 return drm_dp_send_dpcd_read(port->mgr, port, 2168 offset, size, buffer); 2169 } 2170 2171 /** 2172 * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband 2173 * @aux: Fake sideband AUX CH 2174 * @offset: address of the (first) register to write 2175 * @buffer: buffer containing the values to write 2176 * @size: number of bytes in @buffer 2177 * 2178 * Performs the same functionality for remote devices via 2179 * sideband messaging as drm_dp_dpcd_write() does for local 2180 * devices via actual AUX CH. 2181 * 2182 * Return: number of bytes written on success, negative error code on failure. 2183 */ 2184 ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux, 2185 unsigned int offset, void *buffer, size_t size) 2186 { 2187 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, 2188 aux); 2189 2190 if (!port->ddps) 2191 return -EIO; 2192 2193 return drm_dp_send_dpcd_write(port->mgr, port, 2194 offset, size, buffer); 2195 } 2196 2197 static int drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, guid_t *guid) 2198 { 2199 int ret = 0; 2200 2201 guid_copy(&mstb->guid, guid); 2202 2203 if (!drm_dp_validate_guid(mstb->mgr, &mstb->guid)) { 2204 struct drm_dp_aux *aux; 2205 u8 buf[UUID_SIZE]; 2206 2207 export_guid(buf, &mstb->guid); 2208 2209 if (mstb->port_parent) 2210 aux = &mstb->port_parent->aux; 2211 else 2212 aux = mstb->mgr->aux; 2213 2214 ret = drm_dp_dpcd_write_data(aux, DP_GUID, buf, sizeof(buf)); 2215 } 2216 2217 return ret; 2218 } 2219 2220 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb, 2221 int pnum, 2222 char *proppath, 2223 size_t proppath_size) 2224 { 2225 int i; 2226 char temp[8]; 2227 2228 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id); 2229 for (i = 0; i < (mstb->lct - 1); i++) { 2230 int shift = (i % 2) ? 0 : 4; 2231 int port_num = (mstb->rad[i / 2] >> shift) & 0xf; 2232 2233 snprintf(temp, sizeof(temp), "-%d", port_num); 2234 strlcat(proppath, temp, proppath_size); 2235 } 2236 snprintf(temp, sizeof(temp), "-%d", pnum); 2237 strlcat(proppath, temp, proppath_size); 2238 } 2239 2240 /** 2241 * drm_dp_mst_connector_late_register() - Late MST connector registration 2242 * @connector: The MST connector 2243 * @port: The MST port for this connector 2244 * 2245 * Helper to register the remote aux device for this MST port. Drivers should 2246 * call this from their mst connector's late_register hook to enable MST aux 2247 * devices. 2248 * 2249 * Return: 0 on success, negative error code on failure. 2250 */ 2251 int drm_dp_mst_connector_late_register(struct drm_connector *connector, 2252 struct drm_dp_mst_port *port) 2253 { 2254 drm_dbg_kms(port->mgr->dev, "registering %s remote bus for %s\n", 2255 port->aux.name, connector->kdev->kobj.name); 2256 2257 port->aux.dev = connector->kdev; 2258 return drm_dp_aux_register_devnode(&port->aux); 2259 } 2260 EXPORT_SYMBOL(drm_dp_mst_connector_late_register); 2261 2262 /** 2263 * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration 2264 * @connector: The MST connector 2265 * @port: The MST port for this connector 2266 * 2267 * Helper to unregister the remote aux device for this MST port, registered by 2268 * drm_dp_mst_connector_late_register(). Drivers should call this from their mst 2269 * connector's early_unregister hook. 2270 */ 2271 void drm_dp_mst_connector_early_unregister(struct drm_connector *connector, 2272 struct drm_dp_mst_port *port) 2273 { 2274 drm_dbg_kms(port->mgr->dev, "unregistering %s remote bus for %s\n", 2275 port->aux.name, connector->kdev->kobj.name); 2276 drm_dp_aux_unregister_devnode(&port->aux); 2277 } 2278 EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister); 2279 2280 static void 2281 drm_dp_mst_port_add_connector(struct drm_dp_mst_branch *mstb, 2282 struct drm_dp_mst_port *port) 2283 { 2284 struct drm_dp_mst_topology_mgr *mgr = port->mgr; 2285 char proppath[255]; 2286 int ret; 2287 2288 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath)); 2289 port->connector = mgr->cbs->add_connector(mgr, port, proppath); 2290 if (!port->connector) { 2291 ret = -ENOMEM; 2292 goto error; 2293 } 2294 2295 if (port->pdt != DP_PEER_DEVICE_NONE && 2296 drm_dp_mst_is_end_device(port->pdt, port->mcs) && 2297 drm_dp_mst_port_is_logical(port)) 2298 port->cached_edid = drm_edid_read_ddc(port->connector, 2299 &port->aux.ddc); 2300 2301 drm_connector_dynamic_register(port->connector); 2302 return; 2303 2304 error: 2305 drm_err(mgr->dev, "Failed to create connector for port %p: %d\n", port, ret); 2306 } 2307 2308 /* 2309 * Drop a topology reference, and unlink the port from the in-memory topology 2310 * layout 2311 */ 2312 static void 2313 drm_dp_mst_topology_unlink_port(struct drm_dp_mst_topology_mgr *mgr, 2314 struct drm_dp_mst_port *port) 2315 { 2316 mutex_lock(&mgr->lock); 2317 port->parent->num_ports--; 2318 list_del(&port->next); 2319 mutex_unlock(&mgr->lock); 2320 drm_dp_mst_topology_put_port(port); 2321 } 2322 2323 static struct drm_dp_mst_port * 2324 drm_dp_mst_add_port(struct drm_device *dev, 2325 struct drm_dp_mst_topology_mgr *mgr, 2326 struct drm_dp_mst_branch *mstb, u8 port_number) 2327 { 2328 struct drm_dp_mst_port *port = kzalloc_obj(*port); 2329 2330 if (!port) 2331 return NULL; 2332 2333 kref_init(&port->topology_kref); 2334 kref_init(&port->malloc_kref); 2335 port->parent = mstb; 2336 port->port_num = port_number; 2337 port->mgr = mgr; 2338 port->aux.name = "DPMST"; 2339 port->aux.dev = dev->dev; 2340 port->aux.is_remote = true; 2341 2342 /* initialize the MST downstream port's AUX crc work queue */ 2343 port->aux.drm_dev = dev; 2344 drm_dp_remote_aux_init(&port->aux); 2345 2346 /* 2347 * Make sure the memory allocation for our parent branch stays 2348 * around until our own memory allocation is released 2349 */ 2350 drm_dp_mst_get_mstb_malloc(mstb); 2351 2352 return port; 2353 } 2354 2355 static int 2356 drm_dp_mst_handle_link_address_port(struct drm_dp_mst_branch *mstb, 2357 struct drm_device *dev, 2358 struct drm_dp_link_addr_reply_port *port_msg) 2359 { 2360 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr; 2361 struct drm_dp_mst_port *port; 2362 int ret; 2363 u8 new_pdt = DP_PEER_DEVICE_NONE; 2364 bool new_mcs = 0; 2365 bool created = false, send_link_addr = false, changed = false; 2366 2367 port = drm_dp_get_port(mstb, port_msg->port_number); 2368 if (!port) { 2369 port = drm_dp_mst_add_port(dev, mgr, mstb, 2370 port_msg->port_number); 2371 if (!port) 2372 return -ENOMEM; 2373 created = true; 2374 changed = true; 2375 } else if (!port->input && port_msg->input_port && port->connector) { 2376 /* Since port->connector can't be changed here, we create a 2377 * new port if input_port changes from 0 to 1 2378 */ 2379 drm_dp_mst_topology_unlink_port(mgr, port); 2380 drm_dp_mst_topology_put_port(port); 2381 port = drm_dp_mst_add_port(dev, mgr, mstb, 2382 port_msg->port_number); 2383 if (!port) 2384 return -ENOMEM; 2385 changed = true; 2386 created = true; 2387 } else if (port->input && !port_msg->input_port) { 2388 changed = true; 2389 } else if (port->connector) { 2390 /* We're updating a port that's exposed to userspace, so do it 2391 * under lock 2392 */ 2393 drm_modeset_lock(&mgr->base.lock, NULL); 2394 2395 changed = port->ddps != port_msg->ddps || 2396 (port->ddps && 2397 (port->ldps != port_msg->legacy_device_plug_status || 2398 port->dpcd_rev != port_msg->dpcd_revision || 2399 port->mcs != port_msg->mcs || 2400 port->pdt != port_msg->peer_device_type || 2401 port->num_sdp_stream_sinks != 2402 port_msg->num_sdp_stream_sinks)); 2403 } 2404 2405 port->input = port_msg->input_port; 2406 if (!port->input) 2407 new_pdt = port_msg->peer_device_type; 2408 new_mcs = port_msg->mcs; 2409 port->ddps = port_msg->ddps; 2410 port->ldps = port_msg->legacy_device_plug_status; 2411 port->dpcd_rev = port_msg->dpcd_revision; 2412 port->num_sdp_streams = port_msg->num_sdp_streams; 2413 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks; 2414 2415 /* manage mstb port lists with mgr lock - take a reference 2416 for this list */ 2417 if (created) { 2418 mutex_lock(&mgr->lock); 2419 drm_dp_mst_topology_get_port(port); 2420 list_add(&port->next, &mstb->ports); 2421 mstb->num_ports++; 2422 mutex_unlock(&mgr->lock); 2423 } 2424 2425 /* 2426 * Reprobe PBN caps on both hotplug, and when re-probing the link 2427 * for our parent mstb 2428 */ 2429 if (port->ddps && !port->input) { 2430 ret = drm_dp_send_enum_path_resources(mgr, mstb, 2431 port); 2432 if (ret == 1) 2433 changed = true; 2434 } else { 2435 port->full_pbn = 0; 2436 } 2437 2438 ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs); 2439 if (ret == 1) { 2440 send_link_addr = true; 2441 } else if (ret < 0) { 2442 drm_err(dev, "Failed to change PDT on port %p: %d\n", port, ret); 2443 goto fail; 2444 } 2445 2446 /* 2447 * If this port wasn't just created, then we're reprobing because 2448 * we're coming out of suspend. In this case, always resend the link 2449 * address if there's an MSTB on this port 2450 */ 2451 if (!created && port->pdt == DP_PEER_DEVICE_MST_BRANCHING && 2452 port->mcs) 2453 send_link_addr = true; 2454 2455 if (port->connector) 2456 drm_modeset_unlock(&mgr->base.lock); 2457 else if (!port->input) 2458 drm_dp_mst_port_add_connector(mstb, port); 2459 2460 if (send_link_addr && port->mstb) { 2461 ret = drm_dp_send_link_address(mgr, port->mstb); 2462 if (ret == 1) /* MSTB below us changed */ 2463 changed = true; 2464 else if (ret < 0) 2465 goto fail_put; 2466 } 2467 2468 /* put reference to this port */ 2469 drm_dp_mst_topology_put_port(port); 2470 return changed; 2471 2472 fail: 2473 drm_dp_mst_topology_unlink_port(mgr, port); 2474 if (port->connector) 2475 drm_modeset_unlock(&mgr->base.lock); 2476 fail_put: 2477 drm_dp_mst_topology_put_port(port); 2478 return ret; 2479 } 2480 2481 static int 2482 drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch *mstb, 2483 struct drm_dp_connection_status_notify *conn_stat) 2484 { 2485 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr; 2486 struct drm_dp_mst_port *port; 2487 int old_ddps, ret; 2488 u8 new_pdt; 2489 bool new_mcs; 2490 bool dowork = false, create_connector = false; 2491 2492 port = drm_dp_get_port(mstb, conn_stat->port_number); 2493 if (!port) 2494 return 0; 2495 2496 if (port->connector) { 2497 if (!port->input && conn_stat->input_port) { 2498 /* 2499 * We can't remove a connector from an already exposed 2500 * port, so just throw the port out and make sure we 2501 * reprobe the link address of it's parent MSTB 2502 */ 2503 drm_dp_mst_topology_unlink_port(mgr, port); 2504 mstb->link_address_sent = false; 2505 dowork = true; 2506 goto out; 2507 } 2508 2509 /* Locking is only needed if the port's exposed to userspace */ 2510 drm_modeset_lock(&mgr->base.lock, NULL); 2511 } else if (port->input && !conn_stat->input_port) { 2512 create_connector = true; 2513 /* Reprobe link address so we get num_sdp_streams */ 2514 mstb->link_address_sent = false; 2515 dowork = true; 2516 } 2517 2518 old_ddps = port->ddps; 2519 port->input = conn_stat->input_port; 2520 port->ldps = conn_stat->legacy_device_plug_status; 2521 port->ddps = conn_stat->displayport_device_plug_status; 2522 2523 if (old_ddps != port->ddps) { 2524 if (port->ddps && !port->input) 2525 drm_dp_send_enum_path_resources(mgr, mstb, port); 2526 else 2527 port->full_pbn = 0; 2528 } 2529 2530 new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat->peer_device_type; 2531 new_mcs = conn_stat->message_capability_status; 2532 ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs); 2533 if (ret == 1) { 2534 dowork = true; 2535 } else if (ret < 0) { 2536 drm_err(mgr->dev, "Failed to change PDT for port %p: %d\n", port, ret); 2537 dowork = false; 2538 } 2539 2540 if (port->connector) 2541 drm_modeset_unlock(&mgr->base.lock); 2542 else if (create_connector) 2543 drm_dp_mst_port_add_connector(mstb, port); 2544 2545 out: 2546 drm_dp_mst_topology_put_port(port); 2547 return dowork; 2548 } 2549 2550 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr, 2551 u8 lct, u8 *rad) 2552 { 2553 struct drm_dp_mst_branch *mstb; 2554 struct drm_dp_mst_port *port; 2555 int i, ret; 2556 /* find the port by iterating down */ 2557 2558 mutex_lock(&mgr->lock); 2559 mstb = mgr->mst_primary; 2560 2561 if (!mstb) 2562 goto out; 2563 2564 for (i = 1; i < lct; i++) { 2565 int port_num = drm_dp_mst_get_ufp_num_at_lct_from_rad(i + 1, rad); 2566 2567 list_for_each_entry(port, &mstb->ports, next) { 2568 if (port->port_num == port_num) { 2569 mstb = port->mstb; 2570 if (!mstb) { 2571 drm_err(mgr->dev, 2572 "failed to lookup MSTB with lct %d, rad %02x\n", 2573 lct, rad[0]); 2574 goto out; 2575 } 2576 2577 break; 2578 } 2579 } 2580 } 2581 ret = drm_dp_mst_topology_try_get_mstb(mstb); 2582 if (!ret) 2583 mstb = NULL; 2584 out: 2585 mutex_unlock(&mgr->lock); 2586 return mstb; 2587 } 2588 2589 static struct drm_dp_mst_branch * 2590 get_mst_branch_device_by_guid_helper(struct drm_dp_mst_branch *mstb, 2591 const guid_t *guid) 2592 { 2593 struct drm_dp_mst_branch *found_mstb; 2594 struct drm_dp_mst_port *port; 2595 2596 if (!mstb) 2597 return NULL; 2598 2599 if (guid_equal(&mstb->guid, guid)) 2600 return mstb; 2601 2602 list_for_each_entry(port, &mstb->ports, next) { 2603 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid); 2604 2605 if (found_mstb) 2606 return found_mstb; 2607 } 2608 2609 return NULL; 2610 } 2611 2612 static struct drm_dp_mst_branch * 2613 drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr, 2614 const guid_t *guid) 2615 { 2616 struct drm_dp_mst_branch *mstb; 2617 int ret; 2618 2619 /* find the port by iterating down */ 2620 mutex_lock(&mgr->lock); 2621 2622 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid); 2623 if (mstb) { 2624 ret = drm_dp_mst_topology_try_get_mstb(mstb); 2625 if (!ret) 2626 mstb = NULL; 2627 } 2628 2629 mutex_unlock(&mgr->lock); 2630 return mstb; 2631 } 2632 2633 static int drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr, 2634 struct drm_dp_mst_branch *mstb) 2635 { 2636 struct drm_dp_mst_port *port; 2637 int ret; 2638 bool changed = false; 2639 2640 if (!mstb->link_address_sent) { 2641 ret = drm_dp_send_link_address(mgr, mstb); 2642 if (ret == 1) 2643 changed = true; 2644 else if (ret < 0) 2645 return ret; 2646 } 2647 2648 list_for_each_entry(port, &mstb->ports, next) { 2649 if (port->input || !port->ddps || !port->mstb) 2650 continue; 2651 2652 ret = drm_dp_check_and_send_link_address(mgr, port->mstb); 2653 if (ret == 1) 2654 changed = true; 2655 else if (ret < 0) 2656 return ret; 2657 } 2658 2659 return changed; 2660 } 2661 2662 static void drm_dp_mst_link_probe_work(struct work_struct *work) 2663 { 2664 struct drm_dp_mst_topology_mgr *mgr = 2665 container_of(work, struct drm_dp_mst_topology_mgr, work); 2666 struct drm_device *dev = mgr->dev; 2667 struct drm_dp_mst_branch *mstb; 2668 int ret; 2669 bool clear_payload_id_table; 2670 2671 mutex_lock(&mgr->probe_lock); 2672 2673 mutex_lock(&mgr->lock); 2674 clear_payload_id_table = !mgr->payload_id_table_cleared; 2675 mgr->payload_id_table_cleared = true; 2676 2677 mstb = mgr->mst_primary; 2678 if (mstb) { 2679 ret = drm_dp_mst_topology_try_get_mstb(mstb); 2680 if (!ret) 2681 mstb = NULL; 2682 } 2683 mutex_unlock(&mgr->lock); 2684 if (!mstb) { 2685 mutex_unlock(&mgr->probe_lock); 2686 return; 2687 } 2688 2689 /* 2690 * Certain branch devices seem to incorrectly report an available_pbn 2691 * of 0 on downstream sinks, even after clearing the 2692 * DP_PAYLOAD_ALLOCATE_* registers in 2693 * drm_dp_mst_topology_mgr_set_mst(). Namely, the CableMatters USB-C 2694 * 2x DP hub. Sending a CLEAR_PAYLOAD_ID_TABLE message seems to make 2695 * things work again. 2696 */ 2697 if (clear_payload_id_table) { 2698 drm_dbg_kms(dev, "Clearing payload ID table\n"); 2699 drm_dp_send_clear_payload_id_table(mgr, mstb); 2700 } 2701 2702 ret = drm_dp_check_and_send_link_address(mgr, mstb); 2703 drm_dp_mst_topology_put_mstb(mstb); 2704 2705 mutex_unlock(&mgr->probe_lock); 2706 if (ret > 0) 2707 drm_kms_helper_hotplug_event(dev); 2708 } 2709 2710 static void drm_dp_mst_queue_probe_work(struct drm_dp_mst_topology_mgr *mgr) 2711 { 2712 queue_work(system_long_wq, &mgr->work); 2713 } 2714 2715 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr, 2716 guid_t *guid) 2717 { 2718 if (!guid_is_null(guid)) 2719 return true; 2720 2721 guid_gen(guid); 2722 2723 return false; 2724 } 2725 2726 static void build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, 2727 u8 port_num, u32 offset, u8 num_bytes) 2728 { 2729 struct drm_dp_sideband_msg_req_body req; 2730 2731 req.req_type = DP_REMOTE_DPCD_READ; 2732 req.u.dpcd_read.port_number = port_num; 2733 req.u.dpcd_read.dpcd_address = offset; 2734 req.u.dpcd_read.num_bytes = num_bytes; 2735 drm_dp_encode_sideband_req(&req, msg); 2736 } 2737 2738 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr, 2739 bool up, u8 *msg, int len) 2740 { 2741 int ret; 2742 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE; 2743 int tosend, total, offset; 2744 int retries = 0; 2745 2746 retry: 2747 total = len; 2748 offset = 0; 2749 do { 2750 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total); 2751 2752 ret = drm_dp_dpcd_write_data(mgr->aux, regbase + offset, 2753 &msg[offset], 2754 tosend); 2755 if (ret == -EIO && retries < 5) { 2756 retries++; 2757 goto retry; 2758 } else if (ret < 0) { 2759 drm_dbg_kms(mgr->dev, "failed to dpcd write %d %d\n", tosend, ret); 2760 2761 return -EIO; 2762 } 2763 offset += tosend; 2764 total -= tosend; 2765 } while (total > 0); 2766 return 0; 2767 } 2768 2769 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr, 2770 struct drm_dp_sideband_msg_tx *txmsg) 2771 { 2772 struct drm_dp_mst_branch *mstb = txmsg->dst; 2773 u8 req_type; 2774 2775 req_type = txmsg->msg[0] & 0x7f; 2776 if (req_type == DP_CONNECTION_STATUS_NOTIFY || 2777 req_type == DP_RESOURCE_STATUS_NOTIFY || 2778 req_type == DP_CLEAR_PAYLOAD_ID_TABLE) 2779 hdr->broadcast = 1; 2780 else 2781 hdr->broadcast = 0; 2782 hdr->path_msg = txmsg->path_msg; 2783 if (hdr->broadcast) { 2784 hdr->lct = 1; 2785 hdr->lcr = 6; 2786 } else { 2787 hdr->lct = mstb->lct; 2788 hdr->lcr = mstb->lct - 1; 2789 } 2790 2791 memcpy(hdr->rad, mstb->rad, hdr->lct / 2); 2792 2793 return 0; 2794 } 2795 /* 2796 * process a single block of the next message in the sideband queue 2797 */ 2798 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr, 2799 struct drm_dp_sideband_msg_tx *txmsg, 2800 bool up) 2801 { 2802 u8 chunk[48]; 2803 struct drm_dp_sideband_msg_hdr hdr; 2804 int len, space, idx, tosend; 2805 int ret; 2806 2807 if (txmsg->state == DRM_DP_SIDEBAND_TX_SENT) 2808 return 0; 2809 2810 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr)); 2811 2812 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) 2813 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND; 2814 2815 /* make hdr from dst mst */ 2816 ret = set_hdr_from_dst_qlock(&hdr, txmsg); 2817 if (ret < 0) 2818 return ret; 2819 2820 /* amount left to send in this message */ 2821 len = txmsg->cur_len - txmsg->cur_offset; 2822 2823 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */ 2824 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr); 2825 2826 tosend = min(len, space); 2827 if (len == txmsg->cur_len) 2828 hdr.somt = 1; 2829 if (space >= len) 2830 hdr.eomt = 1; 2831 2832 2833 hdr.msg_len = tosend + 1; 2834 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx); 2835 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend); 2836 /* add crc at end */ 2837 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend); 2838 idx += tosend + 1; 2839 2840 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx); 2841 if (ret) { 2842 if (drm_debug_enabled(DRM_UT_DP)) { 2843 struct drm_printer p = drm_dbg_printer(mgr->dev, 2844 DRM_UT_DP, 2845 DBG_PREFIX); 2846 2847 drm_printf(&p, "sideband msg failed to send\n"); 2848 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg); 2849 } 2850 return ret; 2851 } 2852 2853 txmsg->cur_offset += tosend; 2854 if (txmsg->cur_offset == txmsg->cur_len) { 2855 txmsg->state = DRM_DP_SIDEBAND_TX_SENT; 2856 return 1; 2857 } 2858 return 0; 2859 } 2860 2861 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr) 2862 { 2863 struct drm_dp_sideband_msg_tx *txmsg; 2864 int ret; 2865 2866 WARN_ON(!mutex_is_locked(&mgr->qlock)); 2867 2868 /* construct a chunk from the first msg in the tx_msg queue */ 2869 if (list_empty(&mgr->tx_msg_downq)) 2870 return; 2871 2872 txmsg = list_first_entry(&mgr->tx_msg_downq, 2873 struct drm_dp_sideband_msg_tx, next); 2874 ret = process_single_tx_qlock(mgr, txmsg, false); 2875 if (ret < 0) { 2876 drm_dbg_kms(mgr->dev, "failed to send msg in q %d\n", ret); 2877 list_del(&txmsg->next); 2878 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT; 2879 wake_up_all(&mgr->tx_waitq); 2880 } 2881 } 2882 2883 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr, 2884 struct drm_dp_sideband_msg_tx *txmsg) 2885 { 2886 mutex_lock(&mgr->qlock); 2887 list_add_tail(&txmsg->next, &mgr->tx_msg_downq); 2888 2889 if (drm_debug_enabled(DRM_UT_DP)) { 2890 struct drm_printer p = drm_dbg_printer(mgr->dev, DRM_UT_DP, 2891 DBG_PREFIX); 2892 2893 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg); 2894 } 2895 2896 if (list_is_singular(&mgr->tx_msg_downq)) 2897 process_single_down_tx_qlock(mgr); 2898 mutex_unlock(&mgr->qlock); 2899 } 2900 2901 static void 2902 drm_dp_dump_link_address(const struct drm_dp_mst_topology_mgr *mgr, 2903 struct drm_dp_link_address_ack_reply *reply) 2904 { 2905 struct drm_dp_link_addr_reply_port *port_reply; 2906 int i; 2907 2908 for (i = 0; i < reply->nports; i++) { 2909 port_reply = &reply->ports[i]; 2910 drm_dbg_kms(mgr->dev, 2911 "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n", 2912 i, 2913 port_reply->input_port, 2914 port_reply->peer_device_type, 2915 port_reply->port_number, 2916 port_reply->dpcd_revision, 2917 port_reply->mcs, 2918 port_reply->ddps, 2919 port_reply->legacy_device_plug_status, 2920 port_reply->num_sdp_streams, 2921 port_reply->num_sdp_stream_sinks); 2922 } 2923 } 2924 2925 static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr, 2926 struct drm_dp_mst_branch *mstb) 2927 { 2928 struct drm_dp_sideband_msg_tx *txmsg; 2929 struct drm_dp_link_address_ack_reply *reply; 2930 struct drm_dp_mst_port *port, *tmp; 2931 int i, ret, port_mask = 0; 2932 bool changed = false; 2933 2934 txmsg = kzalloc_obj(*txmsg); 2935 if (!txmsg) 2936 return -ENOMEM; 2937 2938 txmsg->dst = mstb; 2939 build_link_address(txmsg); 2940 2941 mstb->link_address_sent = true; 2942 drm_dp_queue_down_tx(mgr, txmsg); 2943 2944 /* FIXME: Actually do some real error handling here */ 2945 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 2946 if (ret < 0) { 2947 drm_err(mgr->dev, "Sending link address failed with %d\n", ret); 2948 goto out; 2949 } 2950 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) { 2951 drm_err(mgr->dev, "link address NAK received\n"); 2952 ret = -EIO; 2953 goto out; 2954 } 2955 2956 reply = &txmsg->reply.u.link_addr; 2957 drm_dbg_kms(mgr->dev, "link address reply: %d\n", reply->nports); 2958 drm_dp_dump_link_address(mgr, reply); 2959 2960 ret = drm_dp_check_mstb_guid(mstb, &reply->guid); 2961 if (ret) { 2962 char buf[64]; 2963 2964 drm_dp_mst_rad_to_str(mstb->rad, mstb->lct, buf, sizeof(buf)); 2965 drm_err(mgr->dev, "GUID check on %s failed: %d\n", buf, ret); 2966 goto out; 2967 } 2968 2969 for (i = 0; i < reply->nports; i++) { 2970 port_mask |= BIT(reply->ports[i].port_number); 2971 ret = drm_dp_mst_handle_link_address_port(mstb, mgr->dev, 2972 &reply->ports[i]); 2973 if (ret == 1) 2974 changed = true; 2975 else if (ret < 0) 2976 goto out; 2977 } 2978 2979 /* Prune any ports that are currently a part of mstb in our in-memory 2980 * topology, but were not seen in this link address. Usually this 2981 * means that they were removed while the topology was out of sync, 2982 * e.g. during suspend/resume 2983 */ 2984 mutex_lock(&mgr->lock); 2985 list_for_each_entry_safe(port, tmp, &mstb->ports, next) { 2986 if (port_mask & BIT(port->port_num)) 2987 continue; 2988 2989 drm_dbg_kms(mgr->dev, "port %d was not in link address, removing\n", 2990 port->port_num); 2991 list_del(&port->next); 2992 drm_dp_mst_topology_put_port(port); 2993 changed = true; 2994 } 2995 mutex_unlock(&mgr->lock); 2996 2997 out: 2998 if (ret < 0) 2999 mstb->link_address_sent = false; 3000 kfree(txmsg); 3001 return ret < 0 ? ret : changed; 3002 } 3003 3004 static void 3005 drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr, 3006 struct drm_dp_mst_branch *mstb) 3007 { 3008 struct drm_dp_sideband_msg_tx *txmsg; 3009 int ret; 3010 3011 txmsg = kzalloc_obj(*txmsg); 3012 if (!txmsg) 3013 return; 3014 3015 txmsg->dst = mstb; 3016 build_clear_payload_id_table(txmsg); 3017 3018 drm_dp_queue_down_tx(mgr, txmsg); 3019 3020 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 3021 if (ret > 0 && txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) 3022 drm_dbg_kms(mgr->dev, "clear payload table id nak received\n"); 3023 3024 kfree(txmsg); 3025 } 3026 3027 static int 3028 drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr, 3029 struct drm_dp_mst_branch *mstb, 3030 struct drm_dp_mst_port *port) 3031 { 3032 struct drm_dp_enum_path_resources_ack_reply *path_res; 3033 struct drm_dp_sideband_msg_tx *txmsg; 3034 int ret; 3035 3036 txmsg = kzalloc_obj(*txmsg); 3037 if (!txmsg) 3038 return -ENOMEM; 3039 3040 txmsg->dst = mstb; 3041 build_enum_path_resources(txmsg, port->port_num); 3042 3043 drm_dp_queue_down_tx(mgr, txmsg); 3044 3045 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 3046 if (ret > 0) { 3047 ret = 0; 3048 path_res = &txmsg->reply.u.path_resources; 3049 3050 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) { 3051 drm_dbg_kms(mgr->dev, "enum path resources nak received\n"); 3052 } else { 3053 if (port->port_num != path_res->port_number) 3054 DRM_ERROR("got incorrect port in response\n"); 3055 3056 drm_dbg_kms(mgr->dev, "enum path resources %d: %d %d\n", 3057 path_res->port_number, 3058 path_res->full_payload_bw_number, 3059 path_res->avail_payload_bw_number); 3060 3061 /* 3062 * If something changed, make sure we send a 3063 * hotplug 3064 */ 3065 if (port->full_pbn != path_res->full_payload_bw_number || 3066 port->fec_capable != path_res->fec_capable) 3067 ret = 1; 3068 3069 port->full_pbn = path_res->full_payload_bw_number; 3070 port->fec_capable = path_res->fec_capable; 3071 } 3072 } 3073 3074 kfree(txmsg); 3075 return ret; 3076 } 3077 3078 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb) 3079 { 3080 if (!mstb->port_parent) 3081 return NULL; 3082 3083 if (mstb->port_parent->mstb != mstb) 3084 return mstb->port_parent; 3085 3086 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent); 3087 } 3088 3089 /* 3090 * Searches upwards in the topology starting from mstb to try to find the 3091 * closest available parent of mstb that's still connected to the rest of the 3092 * topology. This can be used in order to perform operations like releasing 3093 * payloads, where the branch device which owned the payload may no longer be 3094 * around and thus would require that the payload on the last living relative 3095 * be freed instead. 3096 */ 3097 static struct drm_dp_mst_branch * 3098 drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr, 3099 struct drm_dp_mst_branch *mstb, 3100 int *port_num) 3101 { 3102 struct drm_dp_mst_branch *rmstb = NULL; 3103 struct drm_dp_mst_port *found_port; 3104 3105 mutex_lock(&mgr->lock); 3106 if (!mgr->mst_primary) 3107 goto out; 3108 3109 do { 3110 found_port = drm_dp_get_last_connected_port_to_mstb(mstb); 3111 if (!found_port) 3112 break; 3113 3114 if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) { 3115 rmstb = found_port->parent; 3116 *port_num = found_port->port_num; 3117 } else { 3118 /* Search again, starting from this parent */ 3119 mstb = found_port->parent; 3120 } 3121 } while (!rmstb); 3122 out: 3123 mutex_unlock(&mgr->lock); 3124 return rmstb; 3125 } 3126 3127 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr, 3128 struct drm_dp_mst_port *port, 3129 int id, 3130 int pbn) 3131 { 3132 struct drm_dp_sideband_msg_tx *txmsg; 3133 struct drm_dp_mst_branch *mstb; 3134 int ret, port_num; 3135 u8 sinks[DRM_DP_MAX_SDP_STREAMS]; 3136 int i; 3137 3138 port_num = port->port_num; 3139 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent); 3140 if (!mstb) { 3141 mstb = drm_dp_get_last_connected_port_and_mstb(mgr, 3142 port->parent, 3143 &port_num); 3144 3145 if (!mstb) 3146 return -EINVAL; 3147 } 3148 3149 txmsg = kzalloc_obj(*txmsg); 3150 if (!txmsg) { 3151 ret = -ENOMEM; 3152 goto fail_put; 3153 } 3154 3155 for (i = 0; i < port->num_sdp_streams; i++) 3156 sinks[i] = i; 3157 3158 txmsg->dst = mstb; 3159 build_allocate_payload(txmsg, port_num, 3160 id, 3161 pbn, port->num_sdp_streams, sinks); 3162 3163 drm_dp_queue_down_tx(mgr, txmsg); 3164 3165 /* 3166 * FIXME: there is a small chance that between getting the last 3167 * connected mstb and sending the payload message, the last connected 3168 * mstb could also be removed from the topology. In the future, this 3169 * needs to be fixed by restarting the 3170 * drm_dp_get_last_connected_port_and_mstb() search in the event of a 3171 * timeout if the topology is still connected to the system. 3172 */ 3173 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 3174 if (ret > 0) { 3175 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) 3176 ret = -EINVAL; 3177 else 3178 ret = 0; 3179 } 3180 kfree(txmsg); 3181 fail_put: 3182 drm_dp_mst_topology_put_mstb(mstb); 3183 return ret; 3184 } 3185 3186 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr, 3187 struct drm_dp_mst_port *port, bool power_up) 3188 { 3189 struct drm_dp_sideband_msg_tx *txmsg; 3190 int ret; 3191 3192 port = drm_dp_mst_topology_get_port_validated(mgr, port); 3193 if (!port) 3194 return -EINVAL; 3195 3196 txmsg = kzalloc_obj(*txmsg); 3197 if (!txmsg) { 3198 drm_dp_mst_topology_put_port(port); 3199 return -ENOMEM; 3200 } 3201 3202 txmsg->dst = port->parent; 3203 build_power_updown_phy(txmsg, port->port_num, power_up); 3204 drm_dp_queue_down_tx(mgr, txmsg); 3205 3206 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg); 3207 if (ret > 0) { 3208 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) 3209 ret = -EINVAL; 3210 else 3211 ret = 0; 3212 } 3213 kfree(txmsg); 3214 drm_dp_mst_topology_put_port(port); 3215 3216 return ret; 3217 } 3218 EXPORT_SYMBOL(drm_dp_send_power_updown_phy); 3219 3220 int drm_dp_send_query_stream_enc_status(struct drm_dp_mst_topology_mgr *mgr, 3221 struct drm_dp_mst_port *port, 3222 struct drm_dp_query_stream_enc_status_ack_reply *status) 3223 { 3224 struct drm_dp_mst_topology_state *state; 3225 struct drm_dp_mst_atomic_payload *payload; 3226 struct drm_dp_sideband_msg_tx *txmsg; 3227 u8 nonce[7]; 3228 int ret; 3229 3230 txmsg = kzalloc_obj(*txmsg); 3231 if (!txmsg) 3232 return -ENOMEM; 3233 3234 port = drm_dp_mst_topology_get_port_validated(mgr, port); 3235 if (!port) { 3236 ret = -EINVAL; 3237 goto out_get_port; 3238 } 3239 3240 get_random_bytes(nonce, sizeof(nonce)); 3241 3242 drm_modeset_lock(&mgr->base.lock, NULL); 3243 state = to_drm_dp_mst_topology_state(mgr->base.state); 3244 payload = drm_atomic_get_mst_payload_state(state, port); 3245 3246 /* 3247 * "Source device targets the QUERY_STREAM_ENCRYPTION_STATUS message 3248 * transaction at the MST Branch device directly connected to the 3249 * Source" 3250 */ 3251 txmsg->dst = mgr->mst_primary; 3252 3253 build_query_stream_enc_status(txmsg, payload->vcpi, nonce); 3254 3255 drm_dp_queue_down_tx(mgr, txmsg); 3256 3257 ret = drm_dp_mst_wait_tx_reply(mgr->mst_primary, txmsg); 3258 if (ret < 0) { 3259 goto out; 3260 } else if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) { 3261 drm_dbg_kms(mgr->dev, "query encryption status nak received\n"); 3262 ret = -ENXIO; 3263 goto out; 3264 } 3265 3266 ret = 0; 3267 memcpy(status, &txmsg->reply.u.enc_status, sizeof(*status)); 3268 3269 out: 3270 drm_modeset_unlock(&mgr->base.lock); 3271 drm_dp_mst_topology_put_port(port); 3272 out_get_port: 3273 kfree(txmsg); 3274 return ret; 3275 } 3276 EXPORT_SYMBOL(drm_dp_send_query_stream_enc_status); 3277 3278 static int drm_dp_create_payload_at_dfp(struct drm_dp_mst_topology_mgr *mgr, 3279 struct drm_dp_mst_atomic_payload *payload) 3280 { 3281 return drm_dp_dpcd_write_payload(mgr->aux, payload->vcpi, payload->vc_start_slot, 3282 payload->time_slots); 3283 } 3284 3285 static int drm_dp_create_payload_to_remote(struct drm_dp_mst_topology_mgr *mgr, 3286 struct drm_dp_mst_atomic_payload *payload) 3287 { 3288 int ret; 3289 struct drm_dp_mst_port *port = drm_dp_mst_topology_get_port_validated(mgr, payload->port); 3290 3291 if (!port) 3292 return -EIO; 3293 3294 ret = drm_dp_payload_send_msg(mgr, port, payload->vcpi, payload->pbn); 3295 drm_dp_mst_topology_put_port(port); 3296 return ret; 3297 } 3298 3299 static void drm_dp_destroy_payload_at_remote_and_dfp(struct drm_dp_mst_topology_mgr *mgr, 3300 struct drm_dp_mst_topology_state *mst_state, 3301 struct drm_dp_mst_atomic_payload *payload) 3302 { 3303 drm_dbg_kms(mgr->dev, "\n"); 3304 3305 /* it's okay for these to fail */ 3306 if (payload->payload_allocation_status == DRM_DP_MST_PAYLOAD_ALLOCATION_REMOTE) { 3307 drm_dp_payload_send_msg(mgr, payload->port, payload->vcpi, 0); 3308 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_DFP; 3309 } 3310 3311 if (payload->payload_allocation_status == DRM_DP_MST_PAYLOAD_ALLOCATION_DFP) 3312 drm_dp_dpcd_write_payload(mgr->aux, payload->vcpi, payload->vc_start_slot, 0); 3313 } 3314 3315 /** 3316 * drm_dp_add_payload_part1() - Execute payload update part 1 3317 * @mgr: Manager to use. 3318 * @mst_state: The MST atomic state 3319 * @payload: The payload to write 3320 * 3321 * Determines the starting time slot for the given payload, and programs the VCPI for this payload 3322 * into the DPCD of DPRX. After calling this, the driver should generate ACT and payload packets. 3323 * 3324 * Returns: 0 on success, error code on failure. 3325 */ 3326 int drm_dp_add_payload_part1(struct drm_dp_mst_topology_mgr *mgr, 3327 struct drm_dp_mst_topology_state *mst_state, 3328 struct drm_dp_mst_atomic_payload *payload) 3329 { 3330 struct drm_dp_mst_port *port; 3331 int ret; 3332 3333 /* Update mst mgr info */ 3334 if (mgr->payload_count == 0) 3335 mgr->next_start_slot = mst_state->start_slot; 3336 3337 payload->vc_start_slot = mgr->next_start_slot; 3338 3339 mgr->payload_count++; 3340 mgr->next_start_slot += payload->time_slots; 3341 3342 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_LOCAL; 3343 3344 /* Allocate payload to immediate downstream facing port */ 3345 port = drm_dp_mst_topology_get_port_validated(mgr, payload->port); 3346 if (!port) { 3347 drm_dbg_kms(mgr->dev, 3348 "VCPI %d for port %p not in topology, not creating a payload to remote\n", 3349 payload->vcpi, payload->port); 3350 return -EIO; 3351 } 3352 3353 ret = drm_dp_create_payload_at_dfp(mgr, payload); 3354 if (ret < 0) { 3355 drm_dbg_kms(mgr->dev, "Failed to create MST payload for port %p: %d\n", 3356 payload->port, ret); 3357 goto put_port; 3358 } 3359 3360 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_DFP; 3361 3362 put_port: 3363 drm_dp_mst_topology_put_port(port); 3364 3365 return ret; 3366 } 3367 EXPORT_SYMBOL(drm_dp_add_payload_part1); 3368 3369 /** 3370 * drm_dp_remove_payload_part1() - Remove an MST payload along the virtual channel 3371 * @mgr: Manager to use. 3372 * @mst_state: The MST atomic state 3373 * @payload: The payload to remove 3374 * 3375 * Removes a payload along the virtual channel if it was successfully allocated. 3376 * After calling this, the driver should set HW to generate ACT and then switch to new 3377 * payload allocation state. 3378 */ 3379 void drm_dp_remove_payload_part1(struct drm_dp_mst_topology_mgr *mgr, 3380 struct drm_dp_mst_topology_state *mst_state, 3381 struct drm_dp_mst_atomic_payload *payload) 3382 { 3383 /* Remove remote payload allocation */ 3384 bool send_remove = false; 3385 3386 mutex_lock(&mgr->lock); 3387 send_remove = drm_dp_mst_port_downstream_of_branch(payload->port, mgr->mst_primary); 3388 mutex_unlock(&mgr->lock); 3389 3390 if (send_remove) 3391 drm_dp_destroy_payload_at_remote_and_dfp(mgr, mst_state, payload); 3392 else 3393 drm_dbg_kms(mgr->dev, "Payload for VCPI %d not in topology, not sending remove\n", 3394 payload->vcpi); 3395 3396 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_LOCAL; 3397 } 3398 EXPORT_SYMBOL(drm_dp_remove_payload_part1); 3399 3400 /** 3401 * drm_dp_remove_payload_part2() - Remove an MST payload locally 3402 * @mgr: Manager to use. 3403 * @mst_state: The MST atomic state 3404 * @old_payload: The payload with its old state 3405 * @new_payload: The payload with its latest state 3406 * 3407 * Updates the starting time slots of all other payloads which would have been shifted towards 3408 * the start of the payload ID table as a result of removing a payload. Driver should call this 3409 * function whenever it removes a payload in its HW. It's independent to the result of payload 3410 * allocation/deallocation at branch devices along the virtual channel. 3411 */ 3412 void drm_dp_remove_payload_part2(struct drm_dp_mst_topology_mgr *mgr, 3413 struct drm_dp_mst_topology_state *mst_state, 3414 const struct drm_dp_mst_atomic_payload *old_payload, 3415 struct drm_dp_mst_atomic_payload *new_payload) 3416 { 3417 struct drm_dp_mst_atomic_payload *pos; 3418 3419 /* Remove local payload allocation */ 3420 list_for_each_entry(pos, &mst_state->payloads, next) { 3421 if (pos != new_payload && pos->vc_start_slot > new_payload->vc_start_slot) 3422 pos->vc_start_slot -= old_payload->time_slots; 3423 } 3424 new_payload->vc_start_slot = -1; 3425 3426 mgr->payload_count--; 3427 mgr->next_start_slot -= old_payload->time_slots; 3428 3429 if (new_payload->delete) 3430 drm_dp_mst_put_port_malloc(new_payload->port); 3431 3432 new_payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_NONE; 3433 } 3434 EXPORT_SYMBOL(drm_dp_remove_payload_part2); 3435 /** 3436 * drm_dp_add_payload_part2() - Execute payload update part 2 3437 * @mgr: Manager to use. 3438 * @payload: The payload to update 3439 * 3440 * If @payload was successfully assigned a starting time slot by drm_dp_add_payload_part1(), this 3441 * function will send the sideband messages to finish allocating this payload. 3442 * 3443 * Returns: 0 on success, negative error code on failure. 3444 */ 3445 int drm_dp_add_payload_part2(struct drm_dp_mst_topology_mgr *mgr, 3446 struct drm_dp_mst_atomic_payload *payload) 3447 { 3448 int ret = 0; 3449 3450 /* Skip failed payloads */ 3451 if (payload->payload_allocation_status != DRM_DP_MST_PAYLOAD_ALLOCATION_DFP) { 3452 drm_dbg_kms(mgr->dev, "Part 1 of payload creation for %s failed, skipping part 2\n", 3453 payload->port->connector->name); 3454 return -EIO; 3455 } 3456 3457 /* Allocate payload to remote end */ 3458 ret = drm_dp_create_payload_to_remote(mgr, payload); 3459 if (ret < 0) 3460 drm_err(mgr->dev, "Step 2 of creating MST payload for %p failed: %d\n", 3461 payload->port, ret); 3462 else 3463 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_REMOTE; 3464 3465 return ret; 3466 } 3467 EXPORT_SYMBOL(drm_dp_add_payload_part2); 3468 3469 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr, 3470 struct drm_dp_mst_port *port, 3471 int offset, int size, u8 *bytes) 3472 { 3473 int ret = 0; 3474 struct drm_dp_sideband_msg_tx *txmsg; 3475 struct drm_dp_mst_branch *mstb; 3476 3477 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent); 3478 if (!mstb) 3479 return -EINVAL; 3480 3481 txmsg = kzalloc_obj(*txmsg); 3482 if (!txmsg) { 3483 ret = -ENOMEM; 3484 goto fail_put; 3485 } 3486 3487 build_dpcd_read(txmsg, port->port_num, offset, size); 3488 txmsg->dst = port->parent; 3489 3490 drm_dp_queue_down_tx(mgr, txmsg); 3491 3492 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 3493 if (ret < 0) 3494 goto fail_free; 3495 3496 if (txmsg->reply.reply_type == 1) { 3497 drm_dbg_kms(mgr->dev, "mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n", 3498 mstb, port->port_num, offset, size); 3499 ret = -EIO; 3500 goto fail_free; 3501 } 3502 3503 if (txmsg->reply.u.remote_dpcd_read_ack.num_bytes != size) { 3504 ret = -EPROTO; 3505 goto fail_free; 3506 } 3507 3508 ret = min_t(size_t, txmsg->reply.u.remote_dpcd_read_ack.num_bytes, 3509 size); 3510 memcpy(bytes, txmsg->reply.u.remote_dpcd_read_ack.bytes, ret); 3511 3512 fail_free: 3513 kfree(txmsg); 3514 fail_put: 3515 drm_dp_mst_topology_put_mstb(mstb); 3516 3517 return ret; 3518 } 3519 3520 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr, 3521 struct drm_dp_mst_port *port, 3522 int offset, int size, u8 *bytes) 3523 { 3524 int ret; 3525 struct drm_dp_sideband_msg_tx *txmsg; 3526 struct drm_dp_mst_branch *mstb; 3527 3528 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent); 3529 if (!mstb) 3530 return -EINVAL; 3531 3532 txmsg = kzalloc_obj(*txmsg); 3533 if (!txmsg) { 3534 ret = -ENOMEM; 3535 goto fail_put; 3536 } 3537 3538 build_dpcd_write(txmsg, port->port_num, offset, size, bytes); 3539 txmsg->dst = mstb; 3540 3541 drm_dp_queue_down_tx(mgr, txmsg); 3542 3543 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 3544 if (ret > 0) { 3545 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) 3546 ret = -EIO; 3547 else 3548 ret = size; 3549 } 3550 3551 kfree(txmsg); 3552 fail_put: 3553 drm_dp_mst_topology_put_mstb(mstb); 3554 return ret; 3555 } 3556 3557 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type) 3558 { 3559 struct drm_dp_sideband_msg_reply_body reply; 3560 3561 reply.reply_type = DP_SIDEBAND_REPLY_ACK; 3562 reply.req_type = req_type; 3563 drm_dp_encode_sideband_reply(&reply, msg); 3564 return 0; 3565 } 3566 3567 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr, 3568 struct drm_dp_mst_branch *mstb, 3569 int req_type, bool broadcast) 3570 { 3571 struct drm_dp_sideband_msg_tx *txmsg; 3572 3573 txmsg = kzalloc_obj(*txmsg); 3574 if (!txmsg) 3575 return -ENOMEM; 3576 3577 txmsg->dst = mstb; 3578 drm_dp_encode_up_ack_reply(txmsg, req_type); 3579 3580 mutex_lock(&mgr->qlock); 3581 /* construct a chunk from the first msg in the tx_msg queue */ 3582 process_single_tx_qlock(mgr, txmsg, true); 3583 mutex_unlock(&mgr->qlock); 3584 3585 kfree(txmsg); 3586 return 0; 3587 } 3588 3589 /** 3590 * drm_dp_get_vc_payload_bw - get the VC payload BW for an MTP link 3591 * @link_rate: link rate in 10kbits/s units 3592 * @link_lane_count: lane count 3593 * 3594 * Calculate the total bandwidth of a MultiStream Transport link. The returned 3595 * value is in units of PBNs/(timeslots/1 MTP). This value can be used to 3596 * convert the number of PBNs required for a given stream to the number of 3597 * timeslots this stream requires in each MTP. 3598 * 3599 * Returns the BW / timeslot value in 20.12 fixed point format. 3600 */ 3601 fixed20_12 drm_dp_get_vc_payload_bw(int link_rate, int link_lane_count) 3602 { 3603 int ch_coding_efficiency = 3604 drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(link_rate)); 3605 fixed20_12 ret; 3606 3607 /* See DP v2.0 2.6.4.2, 2.7.6.3 VCPayload_Bandwidth_for_OneTimeSlotPer_MTP_Allocation */ 3608 ret.full = DIV_ROUND_DOWN_ULL(mul_u32_u32(link_rate * link_lane_count, 3609 ch_coding_efficiency), 3610 (1000000ULL * 8 * 5400) >> 12); 3611 3612 return ret; 3613 } 3614 EXPORT_SYMBOL(drm_dp_get_vc_payload_bw); 3615 3616 /** 3617 * drm_dp_read_mst_cap() - Read the sink's MST mode capability 3618 * @aux: The DP AUX channel to use 3619 * @dpcd: A cached copy of the DPCD capabilities for this sink 3620 * 3621 * Returns: enum drm_dp_mst_mode to indicate MST mode capability 3622 */ 3623 enum drm_dp_mst_mode drm_dp_read_mst_cap(struct drm_dp_aux *aux, 3624 const u8 dpcd[DP_RECEIVER_CAP_SIZE]) 3625 { 3626 u8 mstm_cap; 3627 3628 if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_12) 3629 return DRM_DP_SST; 3630 3631 if (drm_dp_dpcd_read_byte(aux, DP_MSTM_CAP, &mstm_cap) < 0) 3632 return DRM_DP_SST; 3633 3634 if (mstm_cap & DP_MST_CAP) 3635 return DRM_DP_MST; 3636 3637 if (mstm_cap & DP_SINGLE_STREAM_SIDEBAND_MSG) 3638 return DRM_DP_SST_SIDEBAND_MSG; 3639 3640 return DRM_DP_SST; 3641 } 3642 EXPORT_SYMBOL(drm_dp_read_mst_cap); 3643 3644 /** 3645 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager 3646 * @mgr: manager to set state for 3647 * @mst_state: true to enable MST on this connector - false to disable. 3648 * 3649 * This is called by the driver when it detects an MST capable device plugged 3650 * into a DP MST capable port, or when a DP MST capable device is unplugged. 3651 */ 3652 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state) 3653 { 3654 int ret = 0; 3655 struct drm_dp_mst_branch *mstb = NULL; 3656 3657 mutex_lock(&mgr->lock); 3658 if (mst_state == mgr->mst_state) 3659 goto out_unlock; 3660 3661 mgr->mst_state = mst_state; 3662 /* set the device into MST mode */ 3663 if (mst_state) { 3664 WARN_ON(mgr->mst_primary); 3665 3666 /* get dpcd info */ 3667 ret = drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd); 3668 if (ret < 0) { 3669 drm_dbg_kms(mgr->dev, "%s: failed to read DPCD, ret %d\n", 3670 mgr->aux->name, ret); 3671 goto out_unlock; 3672 } 3673 3674 /* add initial branch device at LCT 1 */ 3675 mstb = drm_dp_add_mst_branch_device(1, NULL); 3676 if (mstb == NULL) { 3677 ret = -ENOMEM; 3678 goto out_unlock; 3679 } 3680 mstb->mgr = mgr; 3681 3682 /* give this the main reference */ 3683 mgr->mst_primary = mstb; 3684 drm_dp_mst_topology_get_mstb(mgr->mst_primary); 3685 3686 ret = drm_dp_dpcd_write_byte(mgr->aux, DP_MSTM_CTRL, 3687 DP_MST_EN | 3688 DP_UP_REQ_EN | 3689 DP_UPSTREAM_IS_SRC); 3690 if (ret < 0) 3691 goto out_unlock; 3692 3693 /* Write reset payload */ 3694 drm_dp_dpcd_clear_payload(mgr->aux); 3695 3696 drm_dp_mst_queue_probe_work(mgr); 3697 3698 ret = 0; 3699 } else { 3700 /* disable MST on the device */ 3701 mstb = mgr->mst_primary; 3702 mgr->mst_primary = NULL; 3703 /* this can fail if the device is gone */ 3704 drm_dp_dpcd_write_byte(mgr->aux, DP_MSTM_CTRL, 0); 3705 ret = 0; 3706 mgr->payload_id_table_cleared = false; 3707 3708 mgr->reset_rx_state = true; 3709 } 3710 3711 out_unlock: 3712 mutex_unlock(&mgr->lock); 3713 if (mstb) 3714 drm_dp_mst_topology_put_mstb(mstb); 3715 return ret; 3716 3717 } 3718 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst); 3719 3720 static void 3721 drm_dp_mst_topology_mgr_invalidate_mstb(struct drm_dp_mst_branch *mstb) 3722 { 3723 struct drm_dp_mst_port *port; 3724 3725 /* The link address will need to be re-sent on resume */ 3726 mstb->link_address_sent = false; 3727 3728 list_for_each_entry(port, &mstb->ports, next) 3729 if (port->mstb) 3730 drm_dp_mst_topology_mgr_invalidate_mstb(port->mstb); 3731 } 3732 3733 /** 3734 * drm_dp_mst_topology_queue_probe - Queue a topology probe 3735 * @mgr: manager to probe 3736 * 3737 * Queue a work to probe the MST topology. Driver's should call this only to 3738 * sync the topology's HW->SW state after the MST link's parameters have 3739 * changed in a way the state could've become out-of-sync. This is the case 3740 * for instance when the link rate between the source and first downstream 3741 * branch device has switched between UHBR and non-UHBR rates. Except of those 3742 * cases - for instance when a sink gets plugged/unplugged to a port - the SW 3743 * state will get updated automatically via MST UP message notifications. 3744 */ 3745 void drm_dp_mst_topology_queue_probe(struct drm_dp_mst_topology_mgr *mgr) 3746 { 3747 mutex_lock(&mgr->lock); 3748 3749 if (!mgr->mst_state || !mgr->mst_primary) { 3750 drm_dbg_kms(mgr->dev, "queue_probe skipped: topology torn down\n"); 3751 goto out_unlock; 3752 } 3753 3754 drm_dp_mst_topology_mgr_invalidate_mstb(mgr->mst_primary); 3755 drm_dp_mst_queue_probe_work(mgr); 3756 3757 out_unlock: 3758 mutex_unlock(&mgr->lock); 3759 } 3760 EXPORT_SYMBOL(drm_dp_mst_topology_queue_probe); 3761 3762 /** 3763 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager 3764 * @mgr: manager to suspend 3765 * 3766 * This function tells the MST device that we can't handle UP messages 3767 * anymore. This should stop it from sending any since we are suspended. 3768 */ 3769 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr) 3770 { 3771 mutex_lock(&mgr->lock); 3772 drm_dp_dpcd_write_byte(mgr->aux, DP_MSTM_CTRL, 3773 DP_MST_EN | DP_UPSTREAM_IS_SRC); 3774 mutex_unlock(&mgr->lock); 3775 flush_work(&mgr->up_req_work); 3776 flush_work(&mgr->work); 3777 flush_work(&mgr->delayed_destroy_work); 3778 3779 mutex_lock(&mgr->lock); 3780 if (mgr->mst_state && mgr->mst_primary) 3781 drm_dp_mst_topology_mgr_invalidate_mstb(mgr->mst_primary); 3782 mutex_unlock(&mgr->lock); 3783 } 3784 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend); 3785 3786 /** 3787 * drm_dp_mst_topology_mgr_resume() - resume the MST manager 3788 * @mgr: manager to resume 3789 * @sync: whether or not to perform topology reprobing synchronously 3790 * 3791 * This will fetch DPCD and see if the device is still there, 3792 * if it is, it will rewrite the MSTM control bits, and return. 3793 * 3794 * If the device fails this returns -1, and the driver should do 3795 * a full MST reprobe, in case we were undocked. 3796 * 3797 * During system resume (where it is assumed that the driver will be calling 3798 * drm_atomic_helper_resume()) this function should be called beforehand with 3799 * @sync set to true. In contexts like runtime resume where the driver is not 3800 * expected to be calling drm_atomic_helper_resume(), this function should be 3801 * called with @sync set to false in order to avoid deadlocking. 3802 * 3803 * Returns: -1 if the MST topology was removed while we were suspended, 0 3804 * otherwise. 3805 */ 3806 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr, 3807 bool sync) 3808 { 3809 u8 buf[UUID_SIZE]; 3810 guid_t guid; 3811 int ret; 3812 3813 mutex_lock(&mgr->lock); 3814 if (!mgr->mst_primary) 3815 goto out_fail; 3816 3817 if (drm_dp_read_dpcd_caps(mgr->aux, mgr->dpcd) < 0) { 3818 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n"); 3819 goto out_fail; 3820 } 3821 3822 ret = drm_dp_dpcd_write_byte(mgr->aux, DP_MSTM_CTRL, 3823 DP_MST_EN | 3824 DP_UP_REQ_EN | 3825 DP_UPSTREAM_IS_SRC); 3826 if (ret < 0) { 3827 drm_dbg_kms(mgr->dev, "mst write failed - undocked during suspend?\n"); 3828 goto out_fail; 3829 } 3830 3831 /* Some hubs forget their guids after they resume */ 3832 ret = drm_dp_dpcd_read_data(mgr->aux, DP_GUID, buf, sizeof(buf)); 3833 if (ret < 0) { 3834 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n"); 3835 goto out_fail; 3836 } 3837 3838 import_guid(&guid, buf); 3839 3840 ret = drm_dp_check_mstb_guid(mgr->mst_primary, &guid); 3841 if (ret) { 3842 drm_dbg_kms(mgr->dev, "check mstb failed - undocked during suspend?\n"); 3843 goto out_fail; 3844 } 3845 3846 /* 3847 * For the final step of resuming the topology, we need to bring the 3848 * state of our in-memory topology back into sync with reality. So, 3849 * restart the probing process as if we're probing a new hub 3850 */ 3851 drm_dp_mst_queue_probe_work(mgr); 3852 mutex_unlock(&mgr->lock); 3853 3854 if (sync) { 3855 drm_dbg_kms(mgr->dev, 3856 "Waiting for link probe work to finish re-syncing topology...\n"); 3857 flush_work(&mgr->work); 3858 } 3859 3860 return 0; 3861 3862 out_fail: 3863 mutex_unlock(&mgr->lock); 3864 return -1; 3865 } 3866 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume); 3867 3868 static void reset_msg_rx_state(struct drm_dp_sideband_msg_rx *msg) 3869 { 3870 memset(msg, 0, sizeof(*msg)); 3871 } 3872 3873 static bool 3874 drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up, 3875 struct drm_dp_mst_branch **mstb) 3876 { 3877 int len; 3878 u8 replyblock[32]; 3879 int replylen, curreply; 3880 int ret; 3881 u8 hdrlen; 3882 struct drm_dp_sideband_msg_hdr hdr; 3883 struct drm_dp_sideband_msg_rx *msg = 3884 up ? &mgr->up_req_recv : &mgr->down_rep_recv; 3885 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : 3886 DP_SIDEBAND_MSG_DOWN_REP_BASE; 3887 3888 if (!up) 3889 *mstb = NULL; 3890 3891 len = min(mgr->max_dpcd_transaction_bytes, 16); 3892 ret = drm_dp_dpcd_read_data(mgr->aux, basereg, replyblock, len); 3893 if (ret < 0) { 3894 drm_dbg_kms(mgr->dev, "failed to read DPCD down rep %d %d\n", len, ret); 3895 return false; 3896 } 3897 3898 ret = drm_dp_decode_sideband_msg_hdr(mgr, &hdr, replyblock, len, &hdrlen); 3899 if (ret == false) { 3900 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 3901 1, replyblock, len, false); 3902 drm_dbg_kms(mgr->dev, "ERROR: failed header\n"); 3903 return false; 3904 } 3905 3906 if (!up) { 3907 /* Caller is responsible for giving back this reference */ 3908 *mstb = drm_dp_get_mst_branch_device(mgr, hdr.lct, hdr.rad); 3909 if (!*mstb) { 3910 drm_dbg_kms(mgr->dev, "Got MST reply from unknown device %d\n", hdr.lct); 3911 return false; 3912 } 3913 } 3914 3915 if (!drm_dp_sideband_msg_set_header(msg, &hdr, hdrlen)) { 3916 drm_dbg_kms(mgr->dev, "sideband msg set header failed %d\n", replyblock[0]); 3917 return false; 3918 } 3919 3920 replylen = min(msg->curchunk_len, (u8)(len - hdrlen)); 3921 ret = drm_dp_sideband_append_payload(msg, replyblock + hdrlen, replylen); 3922 if (!ret) { 3923 drm_dbg_kms(mgr->dev, "sideband msg build failed %d\n", replyblock[0]); 3924 return false; 3925 } 3926 3927 replylen = msg->curchunk_len + msg->curchunk_hdrlen - len; 3928 curreply = len; 3929 while (replylen > 0) { 3930 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16); 3931 ret = drm_dp_dpcd_read_data(mgr->aux, basereg + curreply, 3932 replyblock, len); 3933 if (ret < 0) { 3934 drm_dbg_kms(mgr->dev, "failed to read a chunk (len %d, ret %d)\n", 3935 len, ret); 3936 return false; 3937 } 3938 3939 ret = drm_dp_sideband_append_payload(msg, replyblock, len); 3940 if (!ret) { 3941 drm_dbg_kms(mgr->dev, "failed to build sideband msg\n"); 3942 return false; 3943 } 3944 3945 curreply += len; 3946 replylen -= len; 3947 } 3948 return true; 3949 } 3950 3951 static int get_msg_request_type(u8 data) 3952 { 3953 return data & 0x7f; 3954 } 3955 3956 static bool verify_rx_request_type(struct drm_dp_mst_topology_mgr *mgr, 3957 const struct drm_dp_sideband_msg_tx *txmsg, 3958 const struct drm_dp_sideband_msg_rx *rxmsg) 3959 { 3960 const struct drm_dp_sideband_msg_hdr *hdr = &rxmsg->initial_hdr; 3961 const struct drm_dp_mst_branch *mstb = txmsg->dst; 3962 int tx_req_type = get_msg_request_type(txmsg->msg[0]); 3963 int rx_req_type = get_msg_request_type(rxmsg->msg[0]); 3964 char rad_str[64]; 3965 3966 if (tx_req_type == rx_req_type) 3967 return true; 3968 3969 drm_dp_mst_rad_to_str(mstb->rad, mstb->lct, rad_str, sizeof(rad_str)); 3970 drm_dbg_kms(mgr->dev, 3971 "Got unexpected MST reply, mstb: %p seqno: %d lct: %d rad: %s rx_req_type: %s (%02x) != tx_req_type: %s (%02x)\n", 3972 mstb, hdr->seqno, mstb->lct, rad_str, 3973 drm_dp_mst_req_type_str(rx_req_type), rx_req_type, 3974 drm_dp_mst_req_type_str(tx_req_type), tx_req_type); 3975 3976 return false; 3977 } 3978 3979 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr) 3980 { 3981 struct drm_dp_sideband_msg_tx *txmsg; 3982 struct drm_dp_mst_branch *mstb = NULL; 3983 struct drm_dp_sideband_msg_rx *msg = &mgr->down_rep_recv; 3984 3985 if (!drm_dp_get_one_sb_msg(mgr, false, &mstb)) 3986 goto out_clear_reply; 3987 3988 /* Multi-packet message transmission, don't clear the reply */ 3989 if (!msg->have_eomt) 3990 goto out; 3991 3992 /* find the message */ 3993 mutex_lock(&mgr->qlock); 3994 3995 txmsg = list_first_entry_or_null(&mgr->tx_msg_downq, 3996 struct drm_dp_sideband_msg_tx, next); 3997 3998 /* Were we actually expecting a response, and from this mstb? */ 3999 if (!txmsg || txmsg->dst != mstb) { 4000 struct drm_dp_sideband_msg_hdr *hdr; 4001 4002 hdr = &msg->initial_hdr; 4003 drm_dbg_kms(mgr->dev, "Got MST reply with no msg %p %d %d %02x %02x\n", 4004 mstb, hdr->seqno, hdr->lct, hdr->rad[0], msg->msg[0]); 4005 4006 mutex_unlock(&mgr->qlock); 4007 4008 goto out_clear_reply; 4009 } 4010 4011 if (!verify_rx_request_type(mgr, txmsg, msg)) { 4012 mutex_unlock(&mgr->qlock); 4013 4014 goto out_clear_reply; 4015 } 4016 4017 drm_dp_sideband_parse_reply(mgr, msg, &txmsg->reply); 4018 4019 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) { 4020 drm_dbg_kms(mgr->dev, 4021 "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n", 4022 txmsg->reply.req_type, 4023 drm_dp_mst_req_type_str(txmsg->reply.req_type), 4024 txmsg->reply.u.nak.reason, 4025 drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason), 4026 txmsg->reply.u.nak.nak_data); 4027 } 4028 4029 txmsg->state = DRM_DP_SIDEBAND_TX_RX; 4030 list_del(&txmsg->next); 4031 4032 mutex_unlock(&mgr->qlock); 4033 4034 wake_up_all(&mgr->tx_waitq); 4035 4036 out_clear_reply: 4037 reset_msg_rx_state(msg); 4038 out: 4039 if (mstb) 4040 drm_dp_mst_topology_put_mstb(mstb); 4041 4042 return 0; 4043 } 4044 4045 static bool primary_mstb_probing_is_done(struct drm_dp_mst_topology_mgr *mgr) 4046 { 4047 bool probing_done = false; 4048 4049 mutex_lock(&mgr->lock); 4050 4051 if (mgr->mst_primary && drm_dp_mst_topology_try_get_mstb(mgr->mst_primary)) { 4052 probing_done = mgr->mst_primary->link_address_sent; 4053 drm_dp_mst_topology_put_mstb(mgr->mst_primary); 4054 } 4055 4056 mutex_unlock(&mgr->lock); 4057 4058 return probing_done; 4059 } 4060 4061 static inline bool 4062 drm_dp_mst_process_up_req(struct drm_dp_mst_topology_mgr *mgr, 4063 struct drm_dp_pending_up_req *up_req) 4064 { 4065 struct drm_dp_mst_branch *mstb = NULL; 4066 struct drm_dp_sideband_msg_req_body *msg = &up_req->msg; 4067 struct drm_dp_sideband_msg_hdr *hdr = &up_req->hdr; 4068 bool hotplug = false, dowork = false; 4069 4070 if (hdr->broadcast) { 4071 const guid_t *guid = NULL; 4072 4073 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY) 4074 guid = &msg->u.conn_stat.guid; 4075 else if (msg->req_type == DP_RESOURCE_STATUS_NOTIFY) 4076 guid = &msg->u.resource_stat.guid; 4077 4078 if (guid) 4079 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, guid); 4080 } else { 4081 mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad); 4082 } 4083 4084 if (!mstb) { 4085 drm_dbg_kms(mgr->dev, "Got MST reply from unknown device %d\n", hdr->lct); 4086 return false; 4087 } 4088 4089 /* TODO: Add missing handler for DP_RESOURCE_STATUS_NOTIFY events */ 4090 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY) { 4091 if (!primary_mstb_probing_is_done(mgr)) { 4092 drm_dbg_kms(mgr->dev, "Got CSN before finish topology probing. Skip it.\n"); 4093 } else { 4094 dowork = drm_dp_mst_handle_conn_stat(mstb, &msg->u.conn_stat); 4095 hotplug = true; 4096 } 4097 } 4098 4099 drm_dp_mst_topology_put_mstb(mstb); 4100 4101 if (dowork) 4102 queue_work(system_long_wq, &mgr->work); 4103 return hotplug; 4104 } 4105 4106 static void drm_dp_mst_up_req_work(struct work_struct *work) 4107 { 4108 struct drm_dp_mst_topology_mgr *mgr = 4109 container_of(work, struct drm_dp_mst_topology_mgr, 4110 up_req_work); 4111 struct drm_dp_pending_up_req *up_req; 4112 bool send_hotplug = false; 4113 4114 mutex_lock(&mgr->probe_lock); 4115 while (true) { 4116 mutex_lock(&mgr->up_req_lock); 4117 up_req = list_first_entry_or_null(&mgr->up_req_list, 4118 struct drm_dp_pending_up_req, 4119 next); 4120 if (up_req) 4121 list_del(&up_req->next); 4122 mutex_unlock(&mgr->up_req_lock); 4123 4124 if (!up_req) 4125 break; 4126 4127 send_hotplug |= drm_dp_mst_process_up_req(mgr, up_req); 4128 kfree(up_req); 4129 } 4130 mutex_unlock(&mgr->probe_lock); 4131 4132 if (send_hotplug) 4133 drm_kms_helper_hotplug_event(mgr->dev); 4134 } 4135 4136 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr) 4137 { 4138 struct drm_dp_pending_up_req *up_req; 4139 struct drm_dp_mst_branch *mst_primary; 4140 int ret = 0; 4141 4142 if (!drm_dp_get_one_sb_msg(mgr, true, NULL)) 4143 goto out_clear_reply; 4144 4145 if (!mgr->up_req_recv.have_eomt) 4146 return 0; 4147 4148 up_req = kzalloc_obj(*up_req); 4149 if (!up_req) { 4150 ret = -ENOMEM; 4151 goto out_clear_reply; 4152 } 4153 4154 INIT_LIST_HEAD(&up_req->next); 4155 4156 drm_dp_sideband_parse_req(mgr, &mgr->up_req_recv, &up_req->msg); 4157 4158 if (up_req->msg.req_type != DP_CONNECTION_STATUS_NOTIFY && 4159 up_req->msg.req_type != DP_RESOURCE_STATUS_NOTIFY) { 4160 drm_dbg_kms(mgr->dev, "Received unknown up req type, ignoring: %x\n", 4161 up_req->msg.req_type); 4162 kfree(up_req); 4163 goto out_clear_reply; 4164 } 4165 4166 mutex_lock(&mgr->lock); 4167 mst_primary = mgr->mst_primary; 4168 if (!mst_primary || !drm_dp_mst_topology_try_get_mstb(mst_primary)) { 4169 mutex_unlock(&mgr->lock); 4170 kfree(up_req); 4171 goto out_clear_reply; 4172 } 4173 mutex_unlock(&mgr->lock); 4174 4175 drm_dp_send_up_ack_reply(mgr, mst_primary, up_req->msg.req_type, 4176 false); 4177 4178 drm_dp_mst_topology_put_mstb(mst_primary); 4179 4180 if (up_req->msg.req_type == DP_CONNECTION_STATUS_NOTIFY) { 4181 const struct drm_dp_connection_status_notify *conn_stat = 4182 &up_req->msg.u.conn_stat; 4183 4184 drm_dbg_kms(mgr->dev, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n", 4185 conn_stat->port_number, 4186 conn_stat->legacy_device_plug_status, 4187 conn_stat->displayport_device_plug_status, 4188 conn_stat->message_capability_status, 4189 conn_stat->input_port, 4190 conn_stat->peer_device_type); 4191 } else if (up_req->msg.req_type == DP_RESOURCE_STATUS_NOTIFY) { 4192 const struct drm_dp_resource_status_notify *res_stat = 4193 &up_req->msg.u.resource_stat; 4194 4195 drm_dbg_kms(mgr->dev, "Got RSN: pn: %d avail_pbn %d\n", 4196 res_stat->port_number, 4197 res_stat->available_pbn); 4198 } 4199 4200 up_req->hdr = mgr->up_req_recv.initial_hdr; 4201 mutex_lock(&mgr->up_req_lock); 4202 list_add_tail(&up_req->next, &mgr->up_req_list); 4203 mutex_unlock(&mgr->up_req_lock); 4204 queue_work(system_long_wq, &mgr->up_req_work); 4205 out_clear_reply: 4206 reset_msg_rx_state(&mgr->up_req_recv); 4207 return ret; 4208 } 4209 4210 static void update_msg_rx_state(struct drm_dp_mst_topology_mgr *mgr) 4211 { 4212 mutex_lock(&mgr->lock); 4213 if (mgr->reset_rx_state) { 4214 mgr->reset_rx_state = false; 4215 reset_msg_rx_state(&mgr->down_rep_recv); 4216 reset_msg_rx_state(&mgr->up_req_recv); 4217 } 4218 mutex_unlock(&mgr->lock); 4219 } 4220 4221 /** 4222 * drm_dp_mst_hpd_irq_handle_event() - MST hotplug IRQ handle MST event 4223 * @mgr: manager to notify irq for. 4224 * @esi: 4 bytes from SINK_COUNT_ESI 4225 * @ack: 4 bytes used to ack events starting from SINK_COUNT_ESI 4226 * @handled: whether the hpd interrupt was consumed or not 4227 * 4228 * This should be called from the driver when it detects a HPD IRQ, 4229 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The 4230 * topology manager will process the sideband messages received 4231 * as indicated in the DEVICE_SERVICE_IRQ_VECTOR_ESI0 and set the 4232 * corresponding flags that Driver has to ack the DP receiver later. 4233 * 4234 * Note that driver shall also call 4235 * drm_dp_mst_hpd_irq_send_new_request() if the 'handled' is set 4236 * after calling this function, to try to kick off a new request in 4237 * the queue if the previous message transaction is completed. 4238 * 4239 * See also: 4240 * drm_dp_mst_hpd_irq_send_new_request() 4241 */ 4242 int drm_dp_mst_hpd_irq_handle_event(struct drm_dp_mst_topology_mgr *mgr, const u8 *esi, 4243 u8 *ack, bool *handled) 4244 { 4245 int ret = 0; 4246 int sc; 4247 *handled = false; 4248 sc = DP_GET_SINK_COUNT(esi[0]); 4249 4250 if (sc != mgr->sink_count) { 4251 mgr->sink_count = sc; 4252 *handled = true; 4253 } 4254 4255 update_msg_rx_state(mgr); 4256 4257 if (esi[1] & DP_DOWN_REP_MSG_RDY) { 4258 ret = drm_dp_mst_handle_down_rep(mgr); 4259 *handled = true; 4260 ack[1] |= DP_DOWN_REP_MSG_RDY; 4261 } 4262 4263 if (esi[1] & DP_UP_REQ_MSG_RDY) { 4264 ret |= drm_dp_mst_handle_up_req(mgr); 4265 *handled = true; 4266 ack[1] |= DP_UP_REQ_MSG_RDY; 4267 } 4268 4269 return ret; 4270 } 4271 EXPORT_SYMBOL(drm_dp_mst_hpd_irq_handle_event); 4272 4273 /** 4274 * drm_dp_mst_hpd_irq_send_new_request() - MST hotplug IRQ kick off new request 4275 * @mgr: manager to notify irq for. 4276 * 4277 * This should be called from the driver when mst irq event is handled 4278 * and acked. Note that new down request should only be sent when 4279 * previous message transaction is completed. Source is not supposed to generate 4280 * interleaved message transactions. 4281 */ 4282 void drm_dp_mst_hpd_irq_send_new_request(struct drm_dp_mst_topology_mgr *mgr) 4283 { 4284 struct drm_dp_sideband_msg_tx *txmsg; 4285 bool kick = true; 4286 4287 mutex_lock(&mgr->qlock); 4288 txmsg = list_first_entry_or_null(&mgr->tx_msg_downq, 4289 struct drm_dp_sideband_msg_tx, next); 4290 /* If last transaction is not completed yet*/ 4291 if (!txmsg || 4292 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND || 4293 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) 4294 kick = false; 4295 mutex_unlock(&mgr->qlock); 4296 4297 if (kick) 4298 drm_dp_mst_kick_tx(mgr); 4299 } 4300 EXPORT_SYMBOL(drm_dp_mst_hpd_irq_send_new_request); 4301 /** 4302 * drm_dp_mst_detect_port() - get connection status for an MST port 4303 * @connector: DRM connector for this port 4304 * @ctx: The acquisition context to use for grabbing locks 4305 * @mgr: manager for this port 4306 * @port: pointer to a port 4307 * 4308 * This returns the current connection state for a port. 4309 */ 4310 int 4311 drm_dp_mst_detect_port(struct drm_connector *connector, 4312 struct drm_modeset_acquire_ctx *ctx, 4313 struct drm_dp_mst_topology_mgr *mgr, 4314 struct drm_dp_mst_port *port) 4315 { 4316 int ret; 4317 4318 /* we need to search for the port in the mgr in case it's gone */ 4319 port = drm_dp_mst_topology_get_port_validated(mgr, port); 4320 if (!port) 4321 return connector_status_disconnected; 4322 4323 ret = drm_modeset_lock(&mgr->base.lock, ctx); 4324 if (ret) 4325 goto out; 4326 4327 ret = connector_status_disconnected; 4328 4329 if (!port->ddps) 4330 goto out; 4331 4332 switch (port->pdt) { 4333 case DP_PEER_DEVICE_NONE: 4334 break; 4335 case DP_PEER_DEVICE_MST_BRANCHING: 4336 if (!port->mcs) 4337 ret = connector_status_connected; 4338 break; 4339 4340 case DP_PEER_DEVICE_SST_SINK: 4341 ret = connector_status_connected; 4342 /* for logical ports - cache the EDID */ 4343 if (drm_dp_mst_port_is_logical(port) && !port->cached_edid) 4344 port->cached_edid = drm_edid_read_ddc(connector, &port->aux.ddc); 4345 break; 4346 case DP_PEER_DEVICE_DP_LEGACY_CONV: 4347 if (port->ldps) 4348 ret = connector_status_connected; 4349 break; 4350 } 4351 out: 4352 drm_dp_mst_topology_put_port(port); 4353 return ret; 4354 } 4355 EXPORT_SYMBOL(drm_dp_mst_detect_port); 4356 4357 /** 4358 * drm_dp_mst_edid_read() - get EDID for an MST port 4359 * @connector: toplevel connector to get EDID for 4360 * @mgr: manager for this port 4361 * @port: unverified pointer to a port. 4362 * 4363 * This returns an EDID for the port connected to a connector, 4364 * It validates the pointer still exists so the caller doesn't require a 4365 * reference. 4366 */ 4367 const struct drm_edid *drm_dp_mst_edid_read(struct drm_connector *connector, 4368 struct drm_dp_mst_topology_mgr *mgr, 4369 struct drm_dp_mst_port *port) 4370 { 4371 const struct drm_edid *drm_edid; 4372 4373 /* we need to search for the port in the mgr in case it's gone */ 4374 port = drm_dp_mst_topology_get_port_validated(mgr, port); 4375 if (!port) 4376 return NULL; 4377 4378 if (port->cached_edid) 4379 drm_edid = drm_edid_dup(port->cached_edid); 4380 else 4381 drm_edid = drm_edid_read_ddc(connector, &port->aux.ddc); 4382 4383 drm_dp_mst_topology_put_port(port); 4384 4385 return drm_edid; 4386 } 4387 EXPORT_SYMBOL(drm_dp_mst_edid_read); 4388 4389 /** 4390 * drm_dp_mst_get_edid() - get EDID for an MST port 4391 * @connector: toplevel connector to get EDID for 4392 * @mgr: manager for this port 4393 * @port: unverified pointer to a port. 4394 * 4395 * This function is deprecated; please use drm_dp_mst_edid_read() instead. 4396 * 4397 * This returns an EDID for the port connected to a connector, 4398 * It validates the pointer still exists so the caller doesn't require a 4399 * reference. 4400 */ 4401 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, 4402 struct drm_dp_mst_topology_mgr *mgr, 4403 struct drm_dp_mst_port *port) 4404 { 4405 const struct drm_edid *drm_edid; 4406 struct edid *edid; 4407 4408 drm_edid = drm_dp_mst_edid_read(connector, mgr, port); 4409 4410 edid = drm_edid_duplicate(drm_edid_raw(drm_edid)); 4411 4412 drm_edid_free(drm_edid); 4413 4414 return edid; 4415 } 4416 EXPORT_SYMBOL(drm_dp_mst_get_edid); 4417 4418 /** 4419 * drm_dp_atomic_find_time_slots() - Find and add time slots to the state 4420 * @state: global atomic state 4421 * @mgr: MST topology manager for the port 4422 * @port: port to find time slots for 4423 * @pbn: bandwidth required for the mode in PBN 4424 * 4425 * Allocates time slots to @port, replacing any previous time slot allocations it may 4426 * have had. Any atomic drivers which support MST must call this function in 4427 * their &drm_encoder_helper_funcs.atomic_check() callback unconditionally to 4428 * change the current time slot allocation for the new state, and ensure the MST 4429 * atomic state is added whenever the state of payloads in the topology changes. 4430 * 4431 * Allocations set by this function are not checked against the bandwidth 4432 * restraints of @mgr until the driver calls drm_dp_mst_atomic_check(). 4433 * 4434 * Additionally, it is OK to call this function multiple times on the same 4435 * @port as needed. It is not OK however, to call this function and 4436 * drm_dp_atomic_release_time_slots() in the same atomic check phase. 4437 * 4438 * See also: 4439 * drm_dp_atomic_release_time_slots() 4440 * drm_dp_mst_atomic_check() 4441 * 4442 * Returns: 4443 * Total slots in the atomic state assigned for this port, or a negative error 4444 * code if the port no longer exists 4445 */ 4446 int drm_dp_atomic_find_time_slots(struct drm_atomic_commit *state, 4447 struct drm_dp_mst_topology_mgr *mgr, 4448 struct drm_dp_mst_port *port, int pbn) 4449 { 4450 struct drm_dp_mst_topology_state *topology_state; 4451 struct drm_dp_mst_atomic_payload *payload = NULL; 4452 struct drm_connector_state *conn_state; 4453 int prev_slots = 0, prev_bw = 0, req_slots; 4454 4455 topology_state = drm_atomic_get_mst_topology_state(state, mgr); 4456 if (IS_ERR(topology_state)) 4457 return PTR_ERR(topology_state); 4458 4459 conn_state = drm_atomic_get_new_connector_state(state, port->connector); 4460 topology_state->pending_crtc_mask |= drm_crtc_mask(conn_state->crtc); 4461 4462 /* Find the current allocation for this port, if any */ 4463 payload = drm_atomic_get_mst_payload_state(topology_state, port); 4464 if (payload) { 4465 prev_slots = payload->time_slots; 4466 prev_bw = payload->pbn; 4467 4468 /* 4469 * This should never happen, unless the driver tries 4470 * releasing and allocating the same timeslot allocation, 4471 * which is an error 4472 */ 4473 if (drm_WARN_ON(mgr->dev, payload->delete)) { 4474 drm_err(mgr->dev, 4475 "cannot allocate and release time slots on [MST PORT:%p] in the same state\n", 4476 port); 4477 return -EINVAL; 4478 } 4479 } 4480 4481 req_slots = DIV_ROUND_UP(dfixed_const(pbn), topology_state->pbn_div.full); 4482 4483 drm_dbg_atomic(mgr->dev, "[CONNECTOR:%d:%s] [MST PORT:%p] TU %d -> %d\n", 4484 port->connector->base.id, port->connector->name, 4485 port, prev_slots, req_slots); 4486 drm_dbg_atomic(mgr->dev, "[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n", 4487 port->connector->base.id, port->connector->name, 4488 port, prev_bw, pbn); 4489 4490 /* Add the new allocation to the state, note the VCPI isn't assigned until the end */ 4491 if (!payload) { 4492 payload = kzalloc_obj(*payload); 4493 if (!payload) 4494 return -ENOMEM; 4495 4496 drm_dp_mst_get_port_malloc(port); 4497 payload->port = port; 4498 payload->vc_start_slot = -1; 4499 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_NONE; 4500 list_add(&payload->next, &topology_state->payloads); 4501 } 4502 payload->time_slots = req_slots; 4503 payload->pbn = pbn; 4504 4505 return req_slots; 4506 } 4507 EXPORT_SYMBOL(drm_dp_atomic_find_time_slots); 4508 4509 /** 4510 * drm_dp_atomic_release_time_slots() - Release allocated time slots 4511 * @state: global atomic state 4512 * @mgr: MST topology manager for the port 4513 * @port: The port to release the time slots from 4514 * 4515 * Releases any time slots that have been allocated to a port in the atomic 4516 * state. Any atomic drivers which support MST must call this function 4517 * unconditionally in their &drm_connector_helper_funcs.atomic_check() callback. 4518 * This helper will check whether time slots would be released by the new state and 4519 * respond accordingly, along with ensuring the MST state is always added to the 4520 * atomic state whenever a new state would modify the state of payloads on the 4521 * topology. 4522 * 4523 * It is OK to call this even if @port has been removed from the system. 4524 * Additionally, it is OK to call this function multiple times on the same 4525 * @port as needed. It is not OK however, to call this function and 4526 * drm_dp_atomic_find_time_slots() on the same @port in a single atomic check 4527 * phase. 4528 * 4529 * See also: 4530 * drm_dp_atomic_find_time_slots() 4531 * drm_dp_mst_atomic_check() 4532 * 4533 * Returns: 4534 * 0 on success, negative error code otherwise 4535 */ 4536 int drm_dp_atomic_release_time_slots(struct drm_atomic_commit *state, 4537 struct drm_dp_mst_topology_mgr *mgr, 4538 struct drm_dp_mst_port *port) 4539 { 4540 struct drm_dp_mst_topology_state *topology_state; 4541 struct drm_dp_mst_atomic_payload *payload; 4542 struct drm_connector_state *old_conn_state, *new_conn_state; 4543 bool update_payload = true; 4544 4545 old_conn_state = drm_atomic_get_old_connector_state(state, port->connector); 4546 if (!old_conn_state->crtc) 4547 return 0; 4548 4549 /* If the CRTC isn't disabled by this state, don't release it's payload */ 4550 new_conn_state = drm_atomic_get_new_connector_state(state, port->connector); 4551 if (new_conn_state->crtc) { 4552 struct drm_crtc_state *crtc_state = 4553 drm_atomic_get_new_crtc_state(state, new_conn_state->crtc); 4554 4555 /* No modeset means no payload changes, so it's safe to not pull in the MST state */ 4556 if (!crtc_state || !drm_atomic_crtc_needs_modeset(crtc_state)) 4557 return 0; 4558 4559 if (!crtc_state->mode_changed && !crtc_state->connectors_changed) 4560 update_payload = false; 4561 } 4562 4563 topology_state = drm_atomic_get_mst_topology_state(state, mgr); 4564 if (IS_ERR(topology_state)) 4565 return PTR_ERR(topology_state); 4566 4567 topology_state->pending_crtc_mask |= drm_crtc_mask(old_conn_state->crtc); 4568 if (!update_payload) 4569 return 0; 4570 4571 payload = drm_atomic_get_mst_payload_state(topology_state, port); 4572 if (WARN_ON(!payload)) { 4573 drm_err(mgr->dev, "No payload for [MST PORT:%p] found in mst state %p\n", 4574 port, &topology_state->base); 4575 return -EINVAL; 4576 } 4577 4578 if (new_conn_state->crtc) 4579 return 0; 4580 4581 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] TU %d -> 0\n", port, payload->time_slots); 4582 if (!payload->delete) { 4583 payload->pbn = 0; 4584 payload->delete = true; 4585 if (payload->vcpi > 0) 4586 topology_state->payload_mask &= ~BIT(payload->vcpi - 1); 4587 } 4588 4589 return 0; 4590 } 4591 EXPORT_SYMBOL(drm_dp_atomic_release_time_slots); 4592 4593 /** 4594 * drm_dp_mst_atomic_setup_commit() - setup_commit hook for MST helpers 4595 * @state: global atomic state 4596 * 4597 * This function saves all of the &drm_crtc_commit structs in an atomic state that touch any CRTCs 4598 * currently assigned to an MST topology. Drivers must call this hook from their 4599 * &drm_mode_config_helper_funcs.atomic_commit_setup hook. 4600 * 4601 * Returns: 4602 * 0 if all CRTC commits were retrieved successfully, negative error code otherwise 4603 */ 4604 int drm_dp_mst_atomic_setup_commit(struct drm_atomic_commit *state) 4605 { 4606 struct drm_dp_mst_topology_mgr *mgr; 4607 struct drm_dp_mst_topology_state *mst_state; 4608 struct drm_crtc *crtc; 4609 struct drm_crtc_state *crtc_state; 4610 int i, j, commit_idx, num_commit_deps; 4611 4612 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) { 4613 if (!mst_state->pending_crtc_mask) 4614 continue; 4615 4616 num_commit_deps = hweight32(mst_state->pending_crtc_mask); 4617 mst_state->commit_deps = kmalloc_objs(*mst_state->commit_deps, 4618 num_commit_deps); 4619 if (!mst_state->commit_deps) 4620 return -ENOMEM; 4621 mst_state->num_commit_deps = num_commit_deps; 4622 4623 commit_idx = 0; 4624 for_each_new_crtc_in_state(state, crtc, crtc_state, j) { 4625 if (mst_state->pending_crtc_mask & drm_crtc_mask(crtc)) { 4626 mst_state->commit_deps[commit_idx++] = 4627 drm_crtc_commit_get(crtc_state->commit); 4628 } 4629 } 4630 } 4631 4632 return 0; 4633 } 4634 EXPORT_SYMBOL(drm_dp_mst_atomic_setup_commit); 4635 4636 /** 4637 * drm_dp_mst_atomic_wait_for_dependencies() - Wait for all pending commits on MST topologies, 4638 * prepare new MST state for commit 4639 * @state: global atomic state 4640 * 4641 * Goes through any MST topologies in this atomic state, and waits for any pending commits which 4642 * touched CRTCs that were/are on an MST topology to be programmed to hardware and flipped to before 4643 * returning. This is to prevent multiple non-blocking commits affecting an MST topology from racing 4644 * with eachother by forcing them to be executed sequentially in situations where the only resources 4645 * the modeset objects in these commits share are an MST topology. 4646 * 4647 * This function also prepares the new MST state for commit by performing some state preparation 4648 * which can't be done until this point, such as reading back the final VC start slots (which are 4649 * determined at commit-time) from the previous state. 4650 * 4651 * All MST drivers must call this function after calling drm_atomic_helper_wait_for_dependencies(), 4652 * or whatever their equivalent of that is. 4653 */ 4654 void drm_dp_mst_atomic_wait_for_dependencies(struct drm_atomic_commit *state) 4655 { 4656 struct drm_dp_mst_topology_state *old_mst_state, *new_mst_state; 4657 struct drm_dp_mst_topology_mgr *mgr; 4658 struct drm_dp_mst_atomic_payload *old_payload, *new_payload; 4659 int i, j, ret; 4660 4661 for_each_oldnew_mst_mgr_in_state(state, mgr, old_mst_state, new_mst_state, i) { 4662 for (j = 0; j < old_mst_state->num_commit_deps; j++) { 4663 ret = drm_crtc_commit_wait(old_mst_state->commit_deps[j]); 4664 if (ret < 0) 4665 drm_err(state->dev, "Failed to wait for %s: %d\n", 4666 old_mst_state->commit_deps[j]->crtc->name, ret); 4667 } 4668 4669 /* Now that previous state is committed, it's safe to copy over the start slot 4670 * and allocation status assignments 4671 */ 4672 list_for_each_entry(old_payload, &old_mst_state->payloads, next) { 4673 if (old_payload->delete) 4674 continue; 4675 4676 new_payload = drm_atomic_get_mst_payload_state(new_mst_state, 4677 old_payload->port); 4678 new_payload->vc_start_slot = old_payload->vc_start_slot; 4679 new_payload->payload_allocation_status = 4680 old_payload->payload_allocation_status; 4681 } 4682 } 4683 } 4684 EXPORT_SYMBOL(drm_dp_mst_atomic_wait_for_dependencies); 4685 4686 /** 4687 * drm_dp_mst_root_conn_atomic_check() - Serialize CRTC commits on MST-capable connectors operating 4688 * in SST mode 4689 * @new_conn_state: The new connector state of the &drm_connector 4690 * @mgr: The MST topology manager for the &drm_connector 4691 * 4692 * Since MST uses fake &drm_encoder structs, the generic atomic modesetting code isn't able to 4693 * serialize non-blocking commits happening on the real DP connector of an MST topology switching 4694 * into/away from MST mode - as the CRTC on the real DP connector and the CRTCs on the connector's 4695 * MST topology will never share the same &drm_encoder. 4696 * 4697 * This function takes care of this serialization issue, by checking a root MST connector's atomic 4698 * state to determine if it is about to have a modeset - and then pulling in the MST topology state 4699 * if so, along with adding any relevant CRTCs to &drm_dp_mst_topology_state.pending_crtc_mask. 4700 * 4701 * Drivers implementing MST must call this function from the 4702 * &drm_connector_helper_funcs.atomic_check hook of any physical DP &drm_connector capable of 4703 * driving MST sinks. 4704 * 4705 * Returns: 4706 * 0 on success, negative error code otherwise 4707 */ 4708 int drm_dp_mst_root_conn_atomic_check(struct drm_connector_state *new_conn_state, 4709 struct drm_dp_mst_topology_mgr *mgr) 4710 { 4711 struct drm_atomic_commit *state = new_conn_state->state; 4712 struct drm_connector_state *old_conn_state = 4713 drm_atomic_get_old_connector_state(state, new_conn_state->connector); 4714 struct drm_crtc_state *crtc_state; 4715 struct drm_dp_mst_topology_state *mst_state = NULL; 4716 4717 if (new_conn_state->crtc) { 4718 crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc); 4719 if (crtc_state && drm_atomic_crtc_needs_modeset(crtc_state)) { 4720 mst_state = drm_atomic_get_mst_topology_state(state, mgr); 4721 if (IS_ERR(mst_state)) 4722 return PTR_ERR(mst_state); 4723 4724 mst_state->pending_crtc_mask |= drm_crtc_mask(new_conn_state->crtc); 4725 } 4726 } 4727 4728 if (old_conn_state->crtc) { 4729 crtc_state = drm_atomic_get_new_crtc_state(state, old_conn_state->crtc); 4730 if (crtc_state && drm_atomic_crtc_needs_modeset(crtc_state)) { 4731 if (!mst_state) { 4732 mst_state = drm_atomic_get_mst_topology_state(state, mgr); 4733 if (IS_ERR(mst_state)) 4734 return PTR_ERR(mst_state); 4735 } 4736 4737 mst_state->pending_crtc_mask |= drm_crtc_mask(old_conn_state->crtc); 4738 } 4739 } 4740 4741 return 0; 4742 } 4743 EXPORT_SYMBOL(drm_dp_mst_root_conn_atomic_check); 4744 4745 /** 4746 * drm_dp_mst_update_slots() - updates the slot info depending on the DP ecoding format 4747 * @mst_state: mst_state to update 4748 * @link_encoding_cap: the ecoding format on the link 4749 */ 4750 void drm_dp_mst_update_slots(struct drm_dp_mst_topology_state *mst_state, uint8_t link_encoding_cap) 4751 { 4752 if (link_encoding_cap == DP_CAP_ANSI_128B132B) { 4753 mst_state->total_avail_slots = 64; 4754 mst_state->start_slot = 0; 4755 } else { 4756 mst_state->total_avail_slots = 63; 4757 mst_state->start_slot = 1; 4758 } 4759 4760 DRM_DEBUG_KMS("%s encoding format on mst_state 0x%p\n", 4761 (link_encoding_cap == DP_CAP_ANSI_128B132B) ? "128b/132b":"8b/10b", 4762 mst_state); 4763 } 4764 EXPORT_SYMBOL(drm_dp_mst_update_slots); 4765 4766 /** 4767 * drm_dp_check_act_status() - Polls for ACT handled status. 4768 * @mgr: manager to use 4769 * 4770 * Tries waiting for the MST hub to finish updating it's payload table by 4771 * polling for the ACT handled bit for up to 3 seconds (yes-some hubs really 4772 * take that long). 4773 * 4774 * Returns: 4775 * 0 if the ACT was handled in time, negative error code on failure. 4776 */ 4777 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr) 4778 { 4779 /* 4780 * There doesn't seem to be any recommended retry count or timeout in 4781 * the MST specification. Since some hubs have been observed to take 4782 * over 1 second to update their payload allocations under certain 4783 * conditions, we use a rather large timeout value of 3 seconds. 4784 */ 4785 return drm_dp_dpcd_poll_act_handled(mgr->aux, 3000); 4786 } 4787 EXPORT_SYMBOL(drm_dp_check_act_status); 4788 4789 /** 4790 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode. 4791 * @clock: dot clock 4792 * @bpp: bpp as .4 binary fixed point 4793 * 4794 * This uses the formula in the spec to calculate the PBN value for a mode. 4795 */ 4796 int drm_dp_calc_pbn_mode(int clock, int bpp) 4797 { 4798 /* 4799 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on 4800 * common multiplier to render an integer PBN for all link rate/lane 4801 * counts combinations 4802 * calculate 4803 * peak_kbps = clock * bpp / 16 4804 * peak_kbps *= SSC overhead / 1000000 4805 * peak_kbps /= 8 convert to Kbytes 4806 * peak_kBps *= (64/54) / 1000 convert to PBN 4807 */ 4808 /* 4809 * TODO: Use the actual link and mode parameters to calculate 4810 * the overhead. For now it's assumed that these are 4811 * 4 link lanes, 4096 hactive pixels, which don't add any 4812 * significant data padding overhead and that there is no DSC 4813 * or FEC overhead. 4814 */ 4815 int overhead = drm_dp_bw_overhead(4, 4096, 0, bpp, 4816 DRM_DP_BW_OVERHEAD_MST | 4817 DRM_DP_BW_OVERHEAD_SSC_REF_CLK); 4818 4819 return DIV64_U64_ROUND_UP(mul_u32_u32(clock * bpp, 64 * overhead >> 4), 4820 1000000ULL * 8 * 54 * 1000); 4821 } 4822 EXPORT_SYMBOL(drm_dp_calc_pbn_mode); 4823 4824 /* we want to kick the TX after we've ack the up/down IRQs. */ 4825 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr) 4826 { 4827 queue_work(system_long_wq, &mgr->tx_work); 4828 } 4829 4830 /* 4831 * Helper function for parsing DP device types into convenient strings 4832 * for use with dp_mst_topology 4833 */ 4834 static const char *pdt_to_string(u8 pdt) 4835 { 4836 switch (pdt) { 4837 case DP_PEER_DEVICE_NONE: 4838 return "NONE"; 4839 case DP_PEER_DEVICE_SOURCE_OR_SST: 4840 return "SOURCE OR SST"; 4841 case DP_PEER_DEVICE_MST_BRANCHING: 4842 return "MST BRANCHING"; 4843 case DP_PEER_DEVICE_SST_SINK: 4844 return "SST SINK"; 4845 case DP_PEER_DEVICE_DP_LEGACY_CONV: 4846 return "DP LEGACY CONV"; 4847 default: 4848 return "ERR"; 4849 } 4850 } 4851 4852 static void drm_dp_mst_dump_mstb(struct seq_file *m, 4853 struct drm_dp_mst_branch *mstb) 4854 { 4855 struct drm_dp_mst_port *port; 4856 int tabs = mstb->lct; 4857 char prefix[10]; 4858 int i; 4859 4860 for (i = 0; i < tabs; i++) 4861 prefix[i] = '\t'; 4862 prefix[i] = '\0'; 4863 4864 seq_printf(m, "%smstb - [%p]: num_ports: %d\n", prefix, mstb, mstb->num_ports); 4865 list_for_each_entry(port, &mstb->ports, next) { 4866 seq_printf(m, "%sport %d - [%p] (%s - %s): ddps: %d, ldps: %d, sdp: %d/%d, fec: %s, conn: %p\n", 4867 prefix, 4868 port->port_num, 4869 port, 4870 port->input ? "input" : "output", 4871 pdt_to_string(port->pdt), 4872 port->ddps, 4873 port->ldps, 4874 port->num_sdp_streams, 4875 port->num_sdp_stream_sinks, 4876 port->fec_capable ? "true" : "false", 4877 port->connector); 4878 if (port->mstb) 4879 drm_dp_mst_dump_mstb(m, port->mstb); 4880 } 4881 } 4882 4883 #define DP_PAYLOAD_TABLE_SIZE 64 4884 4885 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr, 4886 char *buf) 4887 { 4888 int i; 4889 4890 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) { 4891 if (drm_dp_dpcd_read_data(mgr->aux, 4892 DP_PAYLOAD_TABLE_UPDATE_STATUS + i, 4893 &buf[i], 16) < 0) 4894 return false; 4895 } 4896 return true; 4897 } 4898 4899 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr, 4900 struct drm_dp_mst_port *port, char *name, 4901 int namelen) 4902 { 4903 struct edid *mst_edid; 4904 4905 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port); 4906 drm_edid_get_monitor_name(mst_edid, name, namelen); 4907 kfree(mst_edid); 4908 } 4909 4910 /** 4911 * drm_dp_mst_dump_topology(): dump topology to seq file. 4912 * @m: seq_file to dump output to 4913 * @mgr: manager to dump current topology for. 4914 * 4915 * helper to dump MST topology to a seq file for debugfs. 4916 */ 4917 void drm_dp_mst_dump_topology(struct seq_file *m, 4918 struct drm_dp_mst_topology_mgr *mgr) 4919 { 4920 struct drm_dp_mst_topology_state *state; 4921 struct drm_dp_mst_atomic_payload *payload; 4922 int i, ret; 4923 4924 static const char *const status[] = { 4925 "None", 4926 "Local", 4927 "DFP", 4928 "Remote", 4929 }; 4930 4931 mutex_lock(&mgr->lock); 4932 if (mgr->mst_primary) 4933 drm_dp_mst_dump_mstb(m, mgr->mst_primary); 4934 4935 /* dump VCPIs */ 4936 mutex_unlock(&mgr->lock); 4937 4938 ret = drm_modeset_lock_single_interruptible(&mgr->base.lock); 4939 if (ret < 0) 4940 return; 4941 4942 state = to_drm_dp_mst_topology_state(mgr->base.state); 4943 seq_printf(m, "\n*** Atomic state info ***\n"); 4944 seq_printf(m, "payload_mask: %x, max_payloads: %d, start_slot: %u, pbn_div: %d\n", 4945 state->payload_mask, mgr->max_payloads, state->start_slot, 4946 dfixed_trunc(state->pbn_div)); 4947 4948 seq_printf(m, "\n| idx | port | vcpi | slots | pbn | dsc | status | sink name |\n"); 4949 for (i = 0; i < mgr->max_payloads; i++) { 4950 list_for_each_entry(payload, &state->payloads, next) { 4951 char name[14]; 4952 4953 if (payload->vcpi != i || payload->delete) 4954 continue; 4955 4956 fetch_monitor_name(mgr, payload->port, name, sizeof(name)); 4957 seq_printf(m, " %5d %6d %6d %02d - %02d %5d %5s %8s %19s\n", 4958 i, 4959 payload->port->port_num, 4960 payload->vcpi, 4961 payload->vc_start_slot, 4962 payload->vc_start_slot + payload->time_slots - 1, 4963 payload->pbn, 4964 payload->dsc_enabled ? "Y" : "N", 4965 status[payload->payload_allocation_status], 4966 (*name != 0) ? name : "Unknown"); 4967 } 4968 } 4969 4970 seq_printf(m, "\n*** DPCD Info ***\n"); 4971 mutex_lock(&mgr->lock); 4972 if (mgr->mst_primary) { 4973 u8 buf[DP_PAYLOAD_TABLE_SIZE]; 4974 int ret; 4975 4976 if (drm_dp_read_dpcd_caps(mgr->aux, buf) < 0) { 4977 seq_printf(m, "dpcd read failed\n"); 4978 goto out; 4979 } 4980 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf); 4981 4982 ret = drm_dp_dpcd_read_data(mgr->aux, DP_FAUX_CAP, buf, 2); 4983 if (ret < 0) { 4984 seq_printf(m, "faux/mst read failed\n"); 4985 goto out; 4986 } 4987 seq_printf(m, "faux/mst: %*ph\n", 2, buf); 4988 4989 ret = drm_dp_dpcd_read_data(mgr->aux, DP_MSTM_CTRL, buf, 1); 4990 if (ret < 0) { 4991 seq_printf(m, "mst ctrl read failed\n"); 4992 goto out; 4993 } 4994 seq_printf(m, "mst ctrl: %*ph\n", 1, buf); 4995 4996 /* dump the standard OUI branch header */ 4997 ret = drm_dp_dpcd_read_data(mgr->aux, DP_BRANCH_OUI, buf, 4998 DP_BRANCH_OUI_HEADER_SIZE); 4999 if (ret < 0) { 5000 seq_printf(m, "branch oui read failed\n"); 5001 goto out; 5002 } 5003 seq_printf(m, "branch oui: %*phN devid: ", 3, buf); 5004 5005 for (i = 0x3; i < 0x8 && buf[i]; i++) 5006 seq_putc(m, buf[i]); 5007 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n", 5008 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]); 5009 if (dump_dp_payload_table(mgr, buf)) 5010 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf); 5011 } 5012 5013 out: 5014 mutex_unlock(&mgr->lock); 5015 drm_modeset_unlock(&mgr->base.lock); 5016 } 5017 EXPORT_SYMBOL(drm_dp_mst_dump_topology); 5018 5019 static void drm_dp_tx_work(struct work_struct *work) 5020 { 5021 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work); 5022 5023 mutex_lock(&mgr->qlock); 5024 if (!list_empty(&mgr->tx_msg_downq)) 5025 process_single_down_tx_qlock(mgr); 5026 mutex_unlock(&mgr->qlock); 5027 } 5028 5029 static inline void 5030 drm_dp_delayed_destroy_port(struct drm_dp_mst_port *port) 5031 { 5032 drm_dp_port_set_pdt(port, DP_PEER_DEVICE_NONE, port->mcs); 5033 5034 if (port->connector) { 5035 drm_connector_unregister(port->connector); 5036 drm_connector_put(port->connector); 5037 } 5038 5039 drm_dp_mst_put_port_malloc(port); 5040 } 5041 5042 static inline void 5043 drm_dp_delayed_destroy_mstb(struct drm_dp_mst_branch *mstb) 5044 { 5045 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr; 5046 struct drm_dp_mst_port *port, *port_tmp; 5047 struct drm_dp_sideband_msg_tx *txmsg, *txmsg_tmp; 5048 bool wake_tx = false; 5049 5050 mutex_lock(&mgr->lock); 5051 list_for_each_entry_safe(port, port_tmp, &mstb->ports, next) { 5052 list_del(&port->next); 5053 drm_dp_mst_topology_put_port(port); 5054 } 5055 mutex_unlock(&mgr->lock); 5056 5057 /* drop any tx slot msg */ 5058 mutex_lock(&mstb->mgr->qlock); 5059 list_for_each_entry_safe(txmsg, txmsg_tmp, &mgr->tx_msg_downq, next) { 5060 if (txmsg->dst != mstb) 5061 continue; 5062 5063 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT; 5064 list_del(&txmsg->next); 5065 wake_tx = true; 5066 } 5067 mutex_unlock(&mstb->mgr->qlock); 5068 5069 if (wake_tx) 5070 wake_up_all(&mstb->mgr->tx_waitq); 5071 5072 drm_dp_mst_put_mstb_malloc(mstb); 5073 } 5074 5075 static void drm_dp_delayed_destroy_work(struct work_struct *work) 5076 { 5077 struct drm_dp_mst_topology_mgr *mgr = 5078 container_of(work, struct drm_dp_mst_topology_mgr, 5079 delayed_destroy_work); 5080 bool send_hotplug = false, go_again; 5081 5082 /* 5083 * Not a regular list traverse as we have to drop the destroy 5084 * connector lock before destroying the mstb/port, to avoid AB->BA 5085 * ordering between this lock and the config mutex. 5086 */ 5087 do { 5088 go_again = false; 5089 5090 for (;;) { 5091 struct drm_dp_mst_branch *mstb; 5092 5093 mutex_lock(&mgr->delayed_destroy_lock); 5094 mstb = list_first_entry_or_null(&mgr->destroy_branch_device_list, 5095 struct drm_dp_mst_branch, 5096 destroy_next); 5097 if (mstb) 5098 list_del(&mstb->destroy_next); 5099 mutex_unlock(&mgr->delayed_destroy_lock); 5100 5101 if (!mstb) 5102 break; 5103 5104 drm_dp_delayed_destroy_mstb(mstb); 5105 go_again = true; 5106 } 5107 5108 for (;;) { 5109 struct drm_dp_mst_port *port; 5110 5111 mutex_lock(&mgr->delayed_destroy_lock); 5112 port = list_first_entry_or_null(&mgr->destroy_port_list, 5113 struct drm_dp_mst_port, 5114 next); 5115 if (port) 5116 list_del(&port->next); 5117 mutex_unlock(&mgr->delayed_destroy_lock); 5118 5119 if (!port) 5120 break; 5121 5122 drm_dp_delayed_destroy_port(port); 5123 send_hotplug = true; 5124 go_again = true; 5125 } 5126 } while (go_again); 5127 5128 if (send_hotplug) 5129 drm_kms_helper_hotplug_event(mgr->dev); 5130 } 5131 5132 static struct drm_private_state * 5133 drm_dp_mst_duplicate_state(struct drm_private_obj *obj) 5134 { 5135 struct drm_dp_mst_topology_state *state, *old_state = 5136 to_dp_mst_topology_state(obj->state); 5137 struct drm_dp_mst_atomic_payload *pos, *payload; 5138 5139 state = kmemdup(old_state, sizeof(*state), GFP_KERNEL); 5140 if (!state) 5141 return NULL; 5142 5143 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 5144 5145 INIT_LIST_HEAD(&state->payloads); 5146 state->commit_deps = NULL; 5147 state->num_commit_deps = 0; 5148 state->pending_crtc_mask = 0; 5149 5150 list_for_each_entry(pos, &old_state->payloads, next) { 5151 /* Prune leftover freed timeslot allocations */ 5152 if (pos->delete) 5153 continue; 5154 5155 payload = kmemdup(pos, sizeof(*payload), GFP_KERNEL); 5156 if (!payload) 5157 goto fail; 5158 5159 drm_dp_mst_get_port_malloc(payload->port); 5160 list_add(&payload->next, &state->payloads); 5161 } 5162 5163 return &state->base; 5164 5165 fail: 5166 list_for_each_entry_safe(pos, payload, &state->payloads, next) { 5167 drm_dp_mst_put_port_malloc(pos->port); 5168 kfree(pos); 5169 } 5170 kfree(state); 5171 5172 return NULL; 5173 } 5174 5175 static void drm_dp_mst_destroy_state(struct drm_private_obj *obj, 5176 struct drm_private_state *state) 5177 { 5178 struct drm_dp_mst_topology_state *mst_state = 5179 to_dp_mst_topology_state(state); 5180 struct drm_dp_mst_atomic_payload *pos, *tmp; 5181 int i; 5182 5183 list_for_each_entry_safe(pos, tmp, &mst_state->payloads, next) { 5184 /* We only keep references to ports with active payloads */ 5185 if (!pos->delete) 5186 drm_dp_mst_put_port_malloc(pos->port); 5187 kfree(pos); 5188 } 5189 5190 for (i = 0; i < mst_state->num_commit_deps; i++) 5191 drm_crtc_commit_put(mst_state->commit_deps[i]); 5192 5193 kfree(mst_state->commit_deps); 5194 kfree(mst_state); 5195 } 5196 5197 static struct drm_private_state * 5198 drm_dp_mst_atomic_create_state(struct drm_private_obj *obj) 5199 { 5200 struct drm_dp_mst_topology_mgr *mgr = 5201 to_dp_mst_topology_mgr(obj); 5202 struct drm_dp_mst_topology_state *mst_state; 5203 5204 mst_state = kzalloc_obj(*mst_state); 5205 if (!mst_state) 5206 return ERR_PTR(-ENOMEM); 5207 5208 __drm_atomic_helper_private_obj_create_state(obj, &mst_state->base); 5209 5210 mst_state->total_avail_slots = 63; 5211 mst_state->start_slot = 1; 5212 5213 mst_state->mgr = mgr; 5214 INIT_LIST_HEAD(&mst_state->payloads); 5215 5216 return &mst_state->base; 5217 } 5218 5219 static bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port, 5220 struct drm_dp_mst_branch *branch) 5221 { 5222 while (port->parent) { 5223 if (port->parent == branch) 5224 return true; 5225 5226 if (port->parent->port_parent) 5227 port = port->parent->port_parent; 5228 else 5229 break; 5230 } 5231 return false; 5232 } 5233 5234 static bool 5235 drm_dp_mst_port_downstream_of_parent_locked(struct drm_dp_mst_topology_mgr *mgr, 5236 struct drm_dp_mst_port *port, 5237 struct drm_dp_mst_port *parent) 5238 { 5239 if (!mgr->mst_primary) 5240 return false; 5241 5242 port = drm_dp_mst_topology_get_port_validated_locked(mgr->mst_primary, 5243 port); 5244 if (!port) 5245 return false; 5246 5247 if (!parent) 5248 return true; 5249 5250 parent = drm_dp_mst_topology_get_port_validated_locked(mgr->mst_primary, 5251 parent); 5252 if (!parent) 5253 return false; 5254 5255 if (!parent->mstb) 5256 return false; 5257 5258 return drm_dp_mst_port_downstream_of_branch(port, parent->mstb); 5259 } 5260 5261 /** 5262 * drm_dp_mst_port_downstream_of_parent - check if a port is downstream of a parent port 5263 * @mgr: MST topology manager 5264 * @port: the port being looked up 5265 * @parent: the parent port 5266 * 5267 * The function returns %true if @port is downstream of @parent. If @parent is 5268 * %NULL - denoting the root port - the function returns %true if @port is in 5269 * @mgr's topology. 5270 */ 5271 bool 5272 drm_dp_mst_port_downstream_of_parent(struct drm_dp_mst_topology_mgr *mgr, 5273 struct drm_dp_mst_port *port, 5274 struct drm_dp_mst_port *parent) 5275 { 5276 bool ret; 5277 5278 mutex_lock(&mgr->lock); 5279 ret = drm_dp_mst_port_downstream_of_parent_locked(mgr, port, parent); 5280 mutex_unlock(&mgr->lock); 5281 5282 return ret; 5283 } 5284 EXPORT_SYMBOL(drm_dp_mst_port_downstream_of_parent); 5285 5286 static int 5287 drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port, 5288 struct drm_dp_mst_topology_state *state, 5289 struct drm_dp_mst_port **failing_port); 5290 5291 static int 5292 drm_dp_mst_atomic_check_mstb_bw_limit(struct drm_dp_mst_branch *mstb, 5293 struct drm_dp_mst_topology_state *state, 5294 struct drm_dp_mst_port **failing_port) 5295 { 5296 struct drm_dp_mst_atomic_payload *payload; 5297 struct drm_dp_mst_port *port; 5298 int pbn_used = 0, ret; 5299 bool found = false; 5300 5301 /* Check that we have at least one port in our state that's downstream 5302 * of this branch, otherwise we can skip this branch 5303 */ 5304 list_for_each_entry(payload, &state->payloads, next) { 5305 if (!payload->pbn || 5306 !drm_dp_mst_port_downstream_of_branch(payload->port, mstb)) 5307 continue; 5308 5309 found = true; 5310 break; 5311 } 5312 if (!found) 5313 return 0; 5314 5315 if (mstb->port_parent) 5316 drm_dbg_atomic(mstb->mgr->dev, 5317 "[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n", 5318 mstb->port_parent->parent, mstb->port_parent, mstb); 5319 else 5320 drm_dbg_atomic(mstb->mgr->dev, "[MSTB:%p] Checking bandwidth limits\n", mstb); 5321 5322 list_for_each_entry(port, &mstb->ports, next) { 5323 ret = drm_dp_mst_atomic_check_port_bw_limit(port, state, failing_port); 5324 if (ret < 0) 5325 return ret; 5326 5327 pbn_used += ret; 5328 } 5329 5330 return pbn_used; 5331 } 5332 5333 static int 5334 drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port, 5335 struct drm_dp_mst_topology_state *state, 5336 struct drm_dp_mst_port **failing_port) 5337 { 5338 struct drm_dp_mst_atomic_payload *payload; 5339 int pbn_used = 0; 5340 5341 if (port->pdt == DP_PEER_DEVICE_NONE) 5342 return 0; 5343 5344 if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) { 5345 payload = drm_atomic_get_mst_payload_state(state, port); 5346 if (!payload) 5347 return 0; 5348 5349 /* 5350 * This could happen if the sink deasserted its HPD line, but 5351 * the branch device still reports it as attached (PDT != NONE). 5352 */ 5353 if (!port->full_pbn) { 5354 drm_dbg_atomic(port->mgr->dev, 5355 "[MSTB:%p] [MST PORT:%p] no BW available for the port\n", 5356 port->parent, port); 5357 *failing_port = port; 5358 return -EINVAL; 5359 } 5360 5361 pbn_used = payload->pbn; 5362 } else { 5363 pbn_used = drm_dp_mst_atomic_check_mstb_bw_limit(port->mstb, 5364 state, 5365 failing_port); 5366 if (pbn_used <= 0) 5367 return pbn_used; 5368 } 5369 5370 if (pbn_used > port->full_pbn) { 5371 drm_dbg_atomic(port->mgr->dev, 5372 "[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n", 5373 port->parent, port, pbn_used, port->full_pbn); 5374 *failing_port = port; 5375 return -ENOSPC; 5376 } 5377 5378 drm_dbg_atomic(port->mgr->dev, "[MSTB:%p] [MST PORT:%p] uses %d out of %d PBN\n", 5379 port->parent, port, pbn_used, port->full_pbn); 5380 5381 return pbn_used; 5382 } 5383 5384 static inline int 5385 drm_dp_mst_atomic_check_payload_alloc_limits(struct drm_dp_mst_topology_mgr *mgr, 5386 struct drm_dp_mst_topology_state *mst_state) 5387 { 5388 struct drm_dp_mst_atomic_payload *payload; 5389 int avail_slots = mst_state->total_avail_slots, payload_count = 0; 5390 5391 list_for_each_entry(payload, &mst_state->payloads, next) { 5392 /* Releasing payloads is always OK-even if the port is gone */ 5393 if (payload->delete) { 5394 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] releases all time slots\n", 5395 payload->port); 5396 continue; 5397 } 5398 5399 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] requires %d time slots\n", 5400 payload->port, payload->time_slots); 5401 5402 avail_slots -= payload->time_slots; 5403 if (avail_slots < 0) { 5404 drm_dbg_atomic(mgr->dev, 5405 "[MST PORT:%p] not enough time slots in mst state %p (avail=%d)\n", 5406 payload->port, mst_state, avail_slots + payload->time_slots); 5407 return -ENOSPC; 5408 } 5409 5410 if (++payload_count > mgr->max_payloads) { 5411 drm_dbg_atomic(mgr->dev, 5412 "[MST MGR:%p] state %p has too many payloads (max=%d)\n", 5413 mgr, mst_state, mgr->max_payloads); 5414 return -EINVAL; 5415 } 5416 5417 /* Assign a VCPI */ 5418 if (!payload->vcpi) { 5419 payload->vcpi = ffz(mst_state->payload_mask) + 1; 5420 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] assigned VCPI #%d\n", 5421 payload->port, payload->vcpi); 5422 mst_state->payload_mask |= BIT(payload->vcpi - 1); 5423 } 5424 } 5425 5426 if (!payload_count) 5427 mst_state->pbn_div.full = dfixed_const(0); 5428 5429 drm_dbg_atomic(mgr->dev, "[MST MGR:%p] mst state %p TU pbn_div=%d avail=%d used=%d\n", 5430 mgr, mst_state, dfixed_trunc(mst_state->pbn_div), avail_slots, 5431 mst_state->total_avail_slots - avail_slots); 5432 5433 return 0; 5434 } 5435 5436 /** 5437 * drm_dp_mst_add_affected_dsc_crtcs 5438 * @state: Pointer to the new struct drm_dp_mst_topology_state 5439 * @mgr: MST topology manager 5440 * 5441 * Whenever there is a change in mst topology 5442 * DSC configuration would have to be recalculated 5443 * therefore we need to trigger modeset on all affected 5444 * CRTCs in that topology 5445 * 5446 * See also: 5447 * drm_dp_mst_atomic_enable_dsc() 5448 */ 5449 int drm_dp_mst_add_affected_dsc_crtcs(struct drm_atomic_commit *state, struct drm_dp_mst_topology_mgr *mgr) 5450 { 5451 struct drm_dp_mst_topology_state *mst_state; 5452 struct drm_dp_mst_atomic_payload *pos; 5453 struct drm_connector *connector; 5454 struct drm_connector_state *conn_state; 5455 struct drm_crtc *crtc; 5456 struct drm_crtc_state *crtc_state; 5457 5458 mst_state = drm_atomic_get_mst_topology_state(state, mgr); 5459 5460 if (IS_ERR(mst_state)) 5461 return PTR_ERR(mst_state); 5462 5463 list_for_each_entry(pos, &mst_state->payloads, next) { 5464 5465 connector = pos->port->connector; 5466 5467 if (!connector) 5468 return -EINVAL; 5469 5470 conn_state = drm_atomic_get_connector_state(state, connector); 5471 5472 if (IS_ERR(conn_state)) 5473 return PTR_ERR(conn_state); 5474 5475 crtc = conn_state->crtc; 5476 5477 if (!crtc) 5478 continue; 5479 5480 if (!drm_dp_mst_dsc_aux_for_port(pos->port)) 5481 continue; 5482 5483 crtc_state = drm_atomic_get_crtc_state(mst_state->base.state, crtc); 5484 5485 if (IS_ERR(crtc_state)) 5486 return PTR_ERR(crtc_state); 5487 5488 drm_dbg_atomic(mgr->dev, "[MST MGR:%p] Setting mode_changed flag on CRTC %p\n", 5489 mgr, crtc); 5490 5491 crtc_state->mode_changed = true; 5492 } 5493 return 0; 5494 } 5495 EXPORT_SYMBOL(drm_dp_mst_add_affected_dsc_crtcs); 5496 5497 /** 5498 * drm_dp_mst_atomic_enable_dsc - Set DSC Enable Flag to On/Off 5499 * @state: Pointer to the new drm_atomic_commit 5500 * @port: Pointer to the affected MST Port 5501 * @pbn: Newly recalculated bw required for link with DSC enabled 5502 * @enable: Boolean flag to enable or disable DSC on the port 5503 * 5504 * This function enables DSC on the given Port 5505 * by recalculating its vcpi from pbn provided 5506 * and sets dsc_enable flag to keep track of which 5507 * ports have DSC enabled 5508 * 5509 */ 5510 int drm_dp_mst_atomic_enable_dsc(struct drm_atomic_commit *state, 5511 struct drm_dp_mst_port *port, 5512 int pbn, bool enable) 5513 { 5514 struct drm_dp_mst_topology_state *mst_state; 5515 struct drm_dp_mst_atomic_payload *payload; 5516 int time_slots = 0; 5517 5518 mst_state = drm_atomic_get_mst_topology_state(state, port->mgr); 5519 if (IS_ERR(mst_state)) 5520 return PTR_ERR(mst_state); 5521 5522 payload = drm_atomic_get_mst_payload_state(mst_state, port); 5523 if (!payload) { 5524 drm_dbg_atomic(state->dev, 5525 "[MST PORT:%p] Couldn't find payload in mst state %p\n", 5526 port, mst_state); 5527 return -EINVAL; 5528 } 5529 5530 if (payload->dsc_enabled == enable) { 5531 drm_dbg_atomic(state->dev, 5532 "[MST PORT:%p] DSC flag is already set to %d, returning %d time slots\n", 5533 port, enable, payload->time_slots); 5534 time_slots = payload->time_slots; 5535 } 5536 5537 if (enable) { 5538 time_slots = drm_dp_atomic_find_time_slots(state, port->mgr, port, pbn); 5539 drm_dbg_atomic(state->dev, 5540 "[MST PORT:%p] Enabling DSC flag, reallocating %d time slots on the port\n", 5541 port, time_slots); 5542 if (time_slots < 0) 5543 return -EINVAL; 5544 } 5545 5546 payload->dsc_enabled = enable; 5547 5548 return time_slots; 5549 } 5550 EXPORT_SYMBOL(drm_dp_mst_atomic_enable_dsc); 5551 5552 /** 5553 * drm_dp_mst_atomic_check_mgr - Check the atomic state of an MST topology manager 5554 * @state: The global atomic state 5555 * @mgr: Manager to check 5556 * @mst_state: The MST atomic state for @mgr 5557 * @failing_port: Returns the port with a BW limitation 5558 * 5559 * Checks the given MST manager's topology state for an atomic update to ensure 5560 * that it's valid. This includes checking whether there's enough bandwidth to 5561 * support the new timeslot allocations in the atomic update. 5562 * 5563 * Any atomic drivers supporting DP MST must make sure to call this or 5564 * the drm_dp_mst_atomic_check() function after checking the rest of their state 5565 * in their &drm_mode_config_funcs.atomic_check() callback. 5566 * 5567 * See also: 5568 * drm_dp_mst_atomic_check() 5569 * drm_dp_atomic_find_time_slots() 5570 * drm_dp_atomic_release_time_slots() 5571 * 5572 * Returns: 5573 * - 0 if the new state is valid 5574 * - %-ENOSPC, if the new state is invalid, because of BW limitation 5575 * @failing_port is set to: 5576 * 5577 * - The non-root port where a BW limit check failed 5578 * with all the ports downstream of @failing_port passing 5579 * the BW limit check. 5580 * The returned port pointer is valid until at least 5581 * one payload downstream of it exists. 5582 * - %NULL if the BW limit check failed at the root port 5583 * with all the ports downstream of the root port passing 5584 * the BW limit check. 5585 * 5586 * - %-EINVAL, if the new state is invalid, because the root port has 5587 * too many payloads. 5588 */ 5589 int drm_dp_mst_atomic_check_mgr(struct drm_atomic_commit *state, 5590 struct drm_dp_mst_topology_mgr *mgr, 5591 struct drm_dp_mst_topology_state *mst_state, 5592 struct drm_dp_mst_port **failing_port) 5593 { 5594 int ret; 5595 5596 *failing_port = NULL; 5597 5598 if (!mgr->mst_state) 5599 return 0; 5600 5601 mutex_lock(&mgr->lock); 5602 ret = drm_dp_mst_atomic_check_mstb_bw_limit(mgr->mst_primary, 5603 mst_state, 5604 failing_port); 5605 mutex_unlock(&mgr->lock); 5606 5607 if (ret < 0) 5608 return ret; 5609 5610 return drm_dp_mst_atomic_check_payload_alloc_limits(mgr, mst_state); 5611 } 5612 EXPORT_SYMBOL(drm_dp_mst_atomic_check_mgr); 5613 5614 /** 5615 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an 5616 * atomic update is valid 5617 * @state: Pointer to the new &struct drm_dp_mst_topology_state 5618 * 5619 * Checks the given topology state for an atomic update to ensure that it's 5620 * valid, calling drm_dp_mst_atomic_check_mgr() for all MST manager in the 5621 * atomic state. This includes checking whether there's enough bandwidth to 5622 * support the new timeslot allocations in the atomic update. 5623 * 5624 * Any atomic drivers supporting DP MST must make sure to call this after 5625 * checking the rest of their state in their 5626 * &drm_mode_config_funcs.atomic_check() callback. 5627 * 5628 * See also: 5629 * drm_dp_mst_atomic_check_mgr() 5630 * drm_dp_atomic_find_time_slots() 5631 * drm_dp_atomic_release_time_slots() 5632 * 5633 * Returns: 5634 * 0 if the new state is valid, negative error code otherwise. 5635 */ 5636 int drm_dp_mst_atomic_check(struct drm_atomic_commit *state) 5637 { 5638 struct drm_dp_mst_topology_mgr *mgr; 5639 struct drm_dp_mst_topology_state *mst_state; 5640 int i, ret = 0; 5641 5642 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) { 5643 struct drm_dp_mst_port *tmp_port; 5644 5645 ret = drm_dp_mst_atomic_check_mgr(state, mgr, mst_state, &tmp_port); 5646 if (ret) 5647 break; 5648 } 5649 5650 return ret; 5651 } 5652 EXPORT_SYMBOL(drm_dp_mst_atomic_check); 5653 5654 const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = { 5655 .atomic_create_state = drm_dp_mst_atomic_create_state, 5656 .atomic_duplicate_state = drm_dp_mst_duplicate_state, 5657 .atomic_destroy_state = drm_dp_mst_destroy_state, 5658 }; 5659 EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs); 5660 5661 /** 5662 * drm_atomic_get_mst_topology_state: get MST topology state 5663 * @state: global atomic state 5664 * @mgr: MST topology manager, also the private object in this case 5665 * 5666 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic 5667 * state vtable so that the private object state returned is that of a MST 5668 * topology object. 5669 * 5670 * RETURNS: 5671 * The MST topology state or error pointer. 5672 */ 5673 struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_commit *state, 5674 struct drm_dp_mst_topology_mgr *mgr) 5675 { 5676 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base)); 5677 } 5678 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state); 5679 5680 /** 5681 * drm_atomic_get_old_mst_topology_state: get old MST topology state in atomic state, if any 5682 * @state: global atomic state 5683 * @mgr: MST topology manager, also the private object in this case 5684 * 5685 * This function wraps drm_atomic_get_old_private_obj_state() passing in the MST atomic 5686 * state vtable so that the private object state returned is that of a MST 5687 * topology object. 5688 * 5689 * Returns: 5690 * The old MST topology state, or NULL if there's no topology state for this MST mgr 5691 * in the global atomic state 5692 */ 5693 struct drm_dp_mst_topology_state * 5694 drm_atomic_get_old_mst_topology_state(struct drm_atomic_commit *state, 5695 struct drm_dp_mst_topology_mgr *mgr) 5696 { 5697 struct drm_private_state *old_priv_state = 5698 drm_atomic_get_old_private_obj_state(state, &mgr->base); 5699 5700 return old_priv_state ? to_dp_mst_topology_state(old_priv_state) : NULL; 5701 } 5702 EXPORT_SYMBOL(drm_atomic_get_old_mst_topology_state); 5703 5704 /** 5705 * drm_atomic_get_new_mst_topology_state: get new MST topology state in atomic state, if any 5706 * @state: global atomic state 5707 * @mgr: MST topology manager, also the private object in this case 5708 * 5709 * This function wraps drm_atomic_get_new_private_obj_state() passing in the MST atomic 5710 * state vtable so that the private object state returned is that of a MST 5711 * topology object. 5712 * 5713 * Returns: 5714 * The new MST topology state, or NULL if there's no topology state for this MST mgr 5715 * in the global atomic state 5716 */ 5717 struct drm_dp_mst_topology_state * 5718 drm_atomic_get_new_mst_topology_state(struct drm_atomic_commit *state, 5719 struct drm_dp_mst_topology_mgr *mgr) 5720 { 5721 struct drm_private_state *new_priv_state = 5722 drm_atomic_get_new_private_obj_state(state, &mgr->base); 5723 5724 return new_priv_state ? to_dp_mst_topology_state(new_priv_state) : NULL; 5725 } 5726 EXPORT_SYMBOL(drm_atomic_get_new_mst_topology_state); 5727 5728 /** 5729 * drm_dp_mst_topology_mgr_init - initialise a topology manager 5730 * @mgr: manager struct to initialise 5731 * @dev: device providing this structure - for i2c addition. 5732 * @aux: DP helper aux channel to talk to this device 5733 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit 5734 * @max_payloads: maximum number of payloads this GPU can source 5735 * @conn_base_id: the connector object ID the MST device is connected to. 5736 * 5737 * Return 0 for success, or negative error code on failure 5738 */ 5739 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr, 5740 struct drm_device *dev, struct drm_dp_aux *aux, 5741 int max_dpcd_transaction_bytes, int max_payloads, 5742 int conn_base_id) 5743 { 5744 mutex_init(&mgr->lock); 5745 mutex_init(&mgr->qlock); 5746 mutex_init(&mgr->delayed_destroy_lock); 5747 mutex_init(&mgr->up_req_lock); 5748 mutex_init(&mgr->probe_lock); 5749 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) 5750 mutex_init(&mgr->topology_ref_history_lock); 5751 stack_depot_init(); 5752 #endif 5753 INIT_LIST_HEAD(&mgr->tx_msg_downq); 5754 INIT_LIST_HEAD(&mgr->destroy_port_list); 5755 INIT_LIST_HEAD(&mgr->destroy_branch_device_list); 5756 INIT_LIST_HEAD(&mgr->up_req_list); 5757 5758 /* 5759 * delayed_destroy_work will be queued on a dedicated WQ, so that any 5760 * requeuing will be also flushed when deiniting the topology manager. 5761 */ 5762 mgr->delayed_destroy_wq = alloc_ordered_workqueue("drm_dp_mst_wq", 0); 5763 if (mgr->delayed_destroy_wq == NULL) 5764 return -ENOMEM; 5765 5766 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work); 5767 INIT_WORK(&mgr->tx_work, drm_dp_tx_work); 5768 INIT_WORK(&mgr->delayed_destroy_work, drm_dp_delayed_destroy_work); 5769 INIT_WORK(&mgr->up_req_work, drm_dp_mst_up_req_work); 5770 init_waitqueue_head(&mgr->tx_waitq); 5771 mgr->dev = dev; 5772 mgr->aux = aux; 5773 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes; 5774 mgr->max_payloads = max_payloads; 5775 mgr->conn_base_id = conn_base_id; 5776 5777 drm_atomic_private_obj_init(dev, &mgr->base, 5778 &drm_dp_mst_topology_state_funcs); 5779 5780 return 0; 5781 } 5782 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init); 5783 5784 /** 5785 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager. 5786 * @mgr: manager to destroy 5787 */ 5788 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr) 5789 { 5790 drm_dp_mst_topology_mgr_set_mst(mgr, false); 5791 flush_work(&mgr->work); 5792 /* The following will also drain any requeued work on the WQ. */ 5793 if (mgr->delayed_destroy_wq) { 5794 destroy_workqueue(mgr->delayed_destroy_wq); 5795 mgr->delayed_destroy_wq = NULL; 5796 } 5797 mgr->dev = NULL; 5798 mgr->aux = NULL; 5799 drm_atomic_private_obj_fini(&mgr->base); 5800 mgr->funcs = NULL; 5801 5802 mutex_destroy(&mgr->delayed_destroy_lock); 5803 mutex_destroy(&mgr->qlock); 5804 mutex_destroy(&mgr->lock); 5805 mutex_destroy(&mgr->up_req_lock); 5806 mutex_destroy(&mgr->probe_lock); 5807 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) 5808 mutex_destroy(&mgr->topology_ref_history_lock); 5809 #endif 5810 } 5811 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy); 5812 5813 static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num) 5814 { 5815 int i; 5816 5817 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS) 5818 return false; 5819 5820 for (i = 0; i < num - 1; i++) { 5821 if (msgs[i].flags & I2C_M_RD || 5822 msgs[i].len > 0xff) 5823 return false; 5824 } 5825 5826 return msgs[num - 1].flags & I2C_M_RD && 5827 msgs[num - 1].len <= 0xff; 5828 } 5829 5830 static bool remote_i2c_write_ok(const struct i2c_msg msgs[], int num) 5831 { 5832 int i; 5833 5834 for (i = 0; i < num - 1; i++) { 5835 if (msgs[i].flags & I2C_M_RD || !(msgs[i].flags & I2C_M_STOP) || 5836 msgs[i].len > 0xff) 5837 return false; 5838 } 5839 5840 return !(msgs[num - 1].flags & I2C_M_RD) && msgs[num - 1].len <= 0xff; 5841 } 5842 5843 static int drm_dp_mst_i2c_read(struct drm_dp_mst_branch *mstb, 5844 struct drm_dp_mst_port *port, 5845 struct i2c_msg *msgs, int num) 5846 { 5847 struct drm_dp_mst_topology_mgr *mgr = port->mgr; 5848 unsigned int i; 5849 struct drm_dp_sideband_msg_req_body msg; 5850 struct drm_dp_sideband_msg_tx *txmsg = NULL; 5851 int ret; 5852 5853 memset(&msg, 0, sizeof(msg)); 5854 msg.req_type = DP_REMOTE_I2C_READ; 5855 msg.u.i2c_read.num_transactions = num - 1; 5856 msg.u.i2c_read.port_number = port->port_num; 5857 for (i = 0; i < num - 1; i++) { 5858 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr; 5859 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len; 5860 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf; 5861 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP); 5862 } 5863 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr; 5864 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len; 5865 5866 txmsg = kzalloc_obj(*txmsg); 5867 if (!txmsg) { 5868 ret = -ENOMEM; 5869 goto out; 5870 } 5871 5872 txmsg->dst = mstb; 5873 drm_dp_encode_sideband_req(&msg, txmsg); 5874 5875 drm_dp_queue_down_tx(mgr, txmsg); 5876 5877 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 5878 if (ret > 0) { 5879 5880 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) { 5881 ret = -EREMOTEIO; 5882 goto out; 5883 } 5884 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) { 5885 ret = -EIO; 5886 goto out; 5887 } 5888 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len); 5889 ret = num; 5890 } 5891 out: 5892 kfree(txmsg); 5893 return ret; 5894 } 5895 5896 static int drm_dp_mst_i2c_write(struct drm_dp_mst_branch *mstb, 5897 struct drm_dp_mst_port *port, 5898 struct i2c_msg *msgs, int num) 5899 { 5900 struct drm_dp_mst_topology_mgr *mgr = port->mgr; 5901 unsigned int i; 5902 struct drm_dp_sideband_msg_req_body msg; 5903 struct drm_dp_sideband_msg_tx *txmsg = NULL; 5904 int ret; 5905 5906 txmsg = kzalloc_obj(*txmsg); 5907 if (!txmsg) { 5908 ret = -ENOMEM; 5909 goto out; 5910 } 5911 for (i = 0; i < num; i++) { 5912 memset(&msg, 0, sizeof(msg)); 5913 msg.req_type = DP_REMOTE_I2C_WRITE; 5914 msg.u.i2c_write.port_number = port->port_num; 5915 msg.u.i2c_write.write_i2c_device_id = msgs[i].addr; 5916 msg.u.i2c_write.num_bytes = msgs[i].len; 5917 msg.u.i2c_write.bytes = msgs[i].buf; 5918 5919 memset(txmsg, 0, sizeof(*txmsg)); 5920 txmsg->dst = mstb; 5921 5922 drm_dp_encode_sideband_req(&msg, txmsg); 5923 drm_dp_queue_down_tx(mgr, txmsg); 5924 5925 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 5926 if (ret > 0) { 5927 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) { 5928 ret = -EREMOTEIO; 5929 goto out; 5930 } 5931 } else { 5932 goto out; 5933 } 5934 } 5935 ret = num; 5936 out: 5937 kfree(txmsg); 5938 return ret; 5939 } 5940 5941 /* I2C device */ 5942 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, 5943 struct i2c_msg *msgs, int num) 5944 { 5945 struct drm_dp_aux *aux = adapter->algo_data; 5946 struct drm_dp_mst_port *port = 5947 container_of(aux, struct drm_dp_mst_port, aux); 5948 struct drm_dp_mst_branch *mstb; 5949 struct drm_dp_mst_topology_mgr *mgr = port->mgr; 5950 int ret; 5951 5952 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent); 5953 if (!mstb) 5954 return -EREMOTEIO; 5955 5956 if (remote_i2c_read_ok(msgs, num)) { 5957 ret = drm_dp_mst_i2c_read(mstb, port, msgs, num); 5958 } else if (remote_i2c_write_ok(msgs, num)) { 5959 ret = drm_dp_mst_i2c_write(mstb, port, msgs, num); 5960 } else { 5961 drm_dbg_kms(mgr->dev, "Unsupported I2C transaction for MST device\n"); 5962 ret = -EIO; 5963 } 5964 5965 drm_dp_mst_topology_put_mstb(mstb); 5966 return ret; 5967 } 5968 5969 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter) 5970 { 5971 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | 5972 I2C_FUNC_SMBUS_READ_BLOCK_DATA | 5973 I2C_FUNC_SMBUS_BLOCK_PROC_CALL | 5974 I2C_FUNC_10BIT_ADDR; 5975 } 5976 5977 static const struct i2c_algorithm drm_dp_mst_i2c_algo = { 5978 .functionality = drm_dp_mst_i2c_functionality, 5979 .master_xfer = drm_dp_mst_i2c_xfer, 5980 }; 5981 5982 /** 5983 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX 5984 * @port: The port to add the I2C bus on 5985 * 5986 * Returns 0 on success or a negative error code on failure. 5987 */ 5988 static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port *port) 5989 { 5990 struct drm_dp_aux *aux = &port->aux; 5991 struct device *parent_dev = port->mgr->dev->dev; 5992 5993 aux->ddc.algo = &drm_dp_mst_i2c_algo; 5994 aux->ddc.algo_data = aux; 5995 aux->ddc.retries = 3; 5996 5997 aux->ddc.owner = THIS_MODULE; 5998 /* FIXME: set the kdev of the port's connector as parent */ 5999 aux->ddc.dev.parent = parent_dev; 6000 aux->ddc.dev.of_node = parent_dev->of_node; 6001 6002 strscpy(aux->ddc.name, aux->name ? aux->name : dev_name(parent_dev), 6003 sizeof(aux->ddc.name)); 6004 6005 return i2c_add_adapter(&aux->ddc); 6006 } 6007 6008 /** 6009 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter 6010 * @port: The port to remove the I2C bus from 6011 */ 6012 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port *port) 6013 { 6014 i2c_del_adapter(&port->aux.ddc); 6015 } 6016 6017 /** 6018 * drm_dp_mst_is_virtual_dpcd() - Is the given port a virtual DP Peer Device 6019 * @port: The port to check 6020 * 6021 * A single physical MST hub object can be represented in the topology 6022 * by multiple branches, with virtual ports between those branches. 6023 * 6024 * As of DP1.4, An MST hub with internal (virtual) ports must expose 6025 * certain DPCD registers over those ports. See sections 2.6.1.1.1 6026 * and 2.6.1.1.2 of Display Port specification v1.4 for details. 6027 * 6028 * May acquire mgr->lock 6029 * 6030 * Returns: 6031 * true if the port is a virtual DP peer device, false otherwise 6032 */ 6033 static bool drm_dp_mst_is_virtual_dpcd(struct drm_dp_mst_port *port) 6034 { 6035 struct drm_dp_mst_port *downstream_port; 6036 6037 if (!port || port->dpcd_rev < DP_DPCD_REV_14) 6038 return false; 6039 6040 /* Virtual DP Sink (Internal Display Panel) */ 6041 if (drm_dp_mst_port_is_logical(port)) 6042 return true; 6043 6044 /* DP-to-HDMI Protocol Converter */ 6045 if (port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV && 6046 !port->mcs && 6047 port->ldps) 6048 return true; 6049 6050 /* DP-to-DP */ 6051 mutex_lock(&port->mgr->lock); 6052 if (port->pdt == DP_PEER_DEVICE_MST_BRANCHING && 6053 port->mstb && 6054 port->mstb->num_ports == 2) { 6055 list_for_each_entry(downstream_port, &port->mstb->ports, next) { 6056 if (downstream_port->pdt == DP_PEER_DEVICE_SST_SINK && 6057 !downstream_port->input) { 6058 mutex_unlock(&port->mgr->lock); 6059 return true; 6060 } 6061 } 6062 } 6063 mutex_unlock(&port->mgr->lock); 6064 6065 return false; 6066 } 6067 6068 /** 6069 * drm_dp_mst_aux_for_parent() - Get the AUX device for an MST port's parent 6070 * @port: MST port whose parent's AUX device is returned 6071 * 6072 * Return the AUX device for @port's parent or NULL if port's parent is the 6073 * root port. 6074 */ 6075 struct drm_dp_aux *drm_dp_mst_aux_for_parent(struct drm_dp_mst_port *port) 6076 { 6077 if (!port->parent || !port->parent->port_parent) 6078 return NULL; 6079 6080 return &port->parent->port_parent->aux; 6081 } 6082 EXPORT_SYMBOL(drm_dp_mst_aux_for_parent); 6083 6084 /** 6085 * drm_dp_mst_dsc_aux_for_port() - Find the correct aux for DSC 6086 * @port: The port to check. A leaf of the MST tree with an attached display. 6087 * 6088 * Depending on the situation, DSC may be enabled via the endpoint aux, 6089 * the immediately upstream aux, or the connector's physical aux. 6090 * 6091 * This is both the correct aux to read DSC_CAPABILITY and the 6092 * correct aux to write DSC_ENABLED. 6093 * 6094 * This operation can be expensive (up to four aux reads), so 6095 * the caller should cache the return. 6096 * 6097 * Returns: 6098 * NULL if DSC cannot be enabled on this port, otherwise the aux device 6099 */ 6100 struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port) 6101 { 6102 struct drm_dp_mst_port *immediate_upstream_port; 6103 struct drm_dp_aux *immediate_upstream_aux; 6104 struct drm_dp_mst_port *fec_port; 6105 struct drm_dp_desc desc = {}; 6106 u8 upstream_dsc; 6107 u8 endpoint_fec; 6108 u8 endpoint_dsc; 6109 6110 if (!port) 6111 return NULL; 6112 6113 if (port->parent->port_parent) 6114 immediate_upstream_port = port->parent->port_parent; 6115 else 6116 immediate_upstream_port = NULL; 6117 6118 fec_port = immediate_upstream_port; 6119 while (fec_port) { 6120 /* 6121 * Each physical link (i.e. not a virtual port) between the 6122 * output and the primary device must support FEC 6123 */ 6124 if (!drm_dp_mst_is_virtual_dpcd(fec_port) && 6125 !fec_port->fec_capable) 6126 return NULL; 6127 6128 fec_port = fec_port->parent->port_parent; 6129 } 6130 6131 /* DP-to-DP peer device */ 6132 if (drm_dp_mst_is_virtual_dpcd(immediate_upstream_port)) { 6133 if (drm_dp_dpcd_read_data(&port->aux, 6134 DP_DSC_SUPPORT, &endpoint_dsc, 1) < 0) 6135 return NULL; 6136 if (drm_dp_dpcd_read_data(&port->aux, 6137 DP_FEC_CAPABILITY, &endpoint_fec, 1) < 0) 6138 return NULL; 6139 if (drm_dp_dpcd_read_data(&immediate_upstream_port->aux, 6140 DP_DSC_SUPPORT, &upstream_dsc, 1) < 0) 6141 return NULL; 6142 6143 /* Enpoint decompression with DP-to-DP peer device */ 6144 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) && 6145 (endpoint_fec & DP_FEC_CAPABLE) && 6146 (upstream_dsc & DP_DSC_PASSTHROUGH_IS_SUPPORTED)) { 6147 port->passthrough_aux = &immediate_upstream_port->aux; 6148 return &port->aux; 6149 } 6150 6151 /* Virtual DPCD decompression with DP-to-DP peer device */ 6152 return &immediate_upstream_port->aux; 6153 } 6154 6155 /* Virtual DPCD decompression with DP-to-HDMI or Virtual DP Sink */ 6156 if (drm_dp_mst_is_virtual_dpcd(port)) 6157 return &port->aux; 6158 6159 /* 6160 * Synaptics quirk 6161 * Applies to ports for which: 6162 * - Physical aux has Synaptics OUI 6163 * - DPv1.4 or higher 6164 * - Port is on primary branch device 6165 * - Not a VGA adapter (DP_DWN_STRM_PORT_TYPE_ANALOG) 6166 */ 6167 if (immediate_upstream_port) 6168 immediate_upstream_aux = &immediate_upstream_port->aux; 6169 else 6170 immediate_upstream_aux = port->mgr->aux; 6171 6172 if (drm_dp_read_desc(immediate_upstream_aux, &desc, true)) 6173 return NULL; 6174 6175 if (drm_dp_has_quirk(&desc, DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD)) { 6176 u8 dpcd_ext[DP_RECEIVER_CAP_SIZE]; 6177 6178 if (drm_dp_dpcd_read_data(immediate_upstream_aux, 6179 DP_DSC_SUPPORT, &upstream_dsc, 1) < 0) 6180 return NULL; 6181 6182 if (!(upstream_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED)) 6183 return NULL; 6184 6185 if (drm_dp_read_dpcd_caps(immediate_upstream_aux, dpcd_ext) < 0) 6186 return NULL; 6187 6188 if (dpcd_ext[DP_DPCD_REV] >= DP_DPCD_REV_14 && 6189 ((dpcd_ext[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT) && 6190 ((dpcd_ext[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) 6191 != DP_DWN_STRM_PORT_TYPE_ANALOG))) 6192 return immediate_upstream_aux; 6193 } 6194 6195 /* 6196 * The check below verifies if the MST sink 6197 * connected to the GPU is capable of DSC - 6198 * therefore the endpoint needs to be 6199 * both DSC and FEC capable. 6200 */ 6201 if (drm_dp_dpcd_read_data(&port->aux, 6202 DP_DSC_SUPPORT, &endpoint_dsc, 1) < 0) 6203 return NULL; 6204 if (drm_dp_dpcd_read_data(&port->aux, 6205 DP_FEC_CAPABILITY, &endpoint_fec, 1) < 0) 6206 return NULL; 6207 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) && 6208 (endpoint_fec & DP_FEC_CAPABLE)) 6209 return &port->aux; 6210 6211 return NULL; 6212 } 6213 EXPORT_SYMBOL(drm_dp_mst_dsc_aux_for_port); 6214