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/kernel.h> 24#include <linux/delay.h> 25#include <linux/init.h> 26#include <linux/errno.h> 27#include <linux/sched.h> 28#include <linux/seq_file.h> 29#include <linux/i2c.h> 30#include <drm/drm_dp_mst_helper.h> 31#include <drm/drmP.h> 32 33#include <drm/drm_fixed.h> 34 35/** 36 * DOC: dp mst helper 37 * 38 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport 39 * protocol. The helpers contain a topology manager and bandwidth manager. 40 * The helpers encapsulate the sending and received of sideband msgs. 41 */ 42static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr, 43 char *buf); 44static int test_calc_pbn_mode(void); 45 46static void drm_dp_put_port(struct drm_dp_mst_port *port); 47 48static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr, 49 int id, 50 struct drm_dp_payload *payload); 51 52static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr, 53 struct drm_dp_mst_port *port, 54 int offset, int size, u8 *bytes); 55 56static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr, 57 struct drm_dp_mst_branch *mstb); 58static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr, 59 struct drm_dp_mst_branch *mstb, 60 struct drm_dp_mst_port *port); 61static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr, 62 u8 *guid); 63 64static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux); 65static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux); 66static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr); 67/* sideband msg handling */ 68static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles) 69{ 70 u8 bitmask = 0x80; 71 u8 bitshift = 7; 72 u8 array_index = 0; 73 int number_of_bits = num_nibbles * 4; 74 u8 remainder = 0; 75 76 while (number_of_bits != 0) { 77 number_of_bits--; 78 remainder <<= 1; 79 remainder |= (data[array_index] & bitmask) >> bitshift; 80 bitmask >>= 1; 81 bitshift--; 82 if (bitmask == 0) { 83 bitmask = 0x80; 84 bitshift = 7; 85 array_index++; 86 } 87 if ((remainder & 0x10) == 0x10) 88 remainder ^= 0x13; 89 } 90 91 number_of_bits = 4; 92 while (number_of_bits != 0) { 93 number_of_bits--; 94 remainder <<= 1; 95 if ((remainder & 0x10) != 0) 96 remainder ^= 0x13; 97 } 98 99 return remainder; 100} 101 102static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes) 103{ 104 u8 bitmask = 0x80; 105 u8 bitshift = 7; 106 u8 array_index = 0; 107 int number_of_bits = number_of_bytes * 8; 108 u16 remainder = 0; 109 110 while (number_of_bits != 0) { 111 number_of_bits--; 112 remainder <<= 1; 113 remainder |= (data[array_index] & bitmask) >> bitshift; 114 bitmask >>= 1; 115 bitshift--; 116 if (bitmask == 0) { 117 bitmask = 0x80; 118 bitshift = 7; 119 array_index++; 120 } 121 if ((remainder & 0x100) == 0x100) 122 remainder ^= 0xd5; 123 } 124 125 number_of_bits = 8; 126 while (number_of_bits != 0) { 127 number_of_bits--; 128 remainder <<= 1; 129 if ((remainder & 0x100) != 0) 130 remainder ^= 0xd5; 131 } 132 133 return remainder & 0xff; 134} 135static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr) 136{ 137 u8 size = 3; 138 size += (hdr->lct / 2); 139 return size; 140} 141 142static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr, 143 u8 *buf, int *len) 144{ 145 int idx = 0; 146 int i; 147 u8 crc4; 148 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf); 149 for (i = 0; i < (hdr->lct / 2); i++) 150 buf[idx++] = hdr->rad[i]; 151 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) | 152 (hdr->msg_len & 0x3f); 153 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4); 154 155 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1); 156 buf[idx - 1] |= (crc4 & 0xf); 157 158 *len = idx; 159} 160 161static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr, 162 u8 *buf, int buflen, u8 *hdrlen) 163{ 164 u8 crc4; 165 u8 len; 166 int i; 167 u8 idx; 168 if (buf[0] == 0) 169 return false; 170 len = 3; 171 len += ((buf[0] & 0xf0) >> 4) / 2; 172 if (len > buflen) 173 return false; 174 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1); 175 176 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) { 177 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]); 178 return false; 179 } 180 181 hdr->lct = (buf[0] & 0xf0) >> 4; 182 hdr->lcr = (buf[0] & 0xf); 183 idx = 1; 184 for (i = 0; i < (hdr->lct / 2); i++) 185 hdr->rad[i] = buf[idx++]; 186 hdr->broadcast = (buf[idx] >> 7) & 0x1; 187 hdr->path_msg = (buf[idx] >> 6) & 0x1; 188 hdr->msg_len = buf[idx] & 0x3f; 189 idx++; 190 hdr->somt = (buf[idx] >> 7) & 0x1; 191 hdr->eomt = (buf[idx] >> 6) & 0x1; 192 hdr->seqno = (buf[idx] >> 4) & 0x1; 193 idx++; 194 *hdrlen = idx; 195 return true; 196} 197 198static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req, 199 struct drm_dp_sideband_msg_tx *raw) 200{ 201 int idx = 0; 202 int i; 203 u8 *buf = raw->msg; 204 buf[idx++] = req->req_type & 0x7f; 205 206 switch (req->req_type) { 207 case DP_ENUM_PATH_RESOURCES: 208 buf[idx] = (req->u.port_num.port_number & 0xf) << 4; 209 idx++; 210 break; 211 case DP_ALLOCATE_PAYLOAD: 212 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 | 213 (req->u.allocate_payload.number_sdp_streams & 0xf); 214 idx++; 215 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f); 216 idx++; 217 buf[idx] = (req->u.allocate_payload.pbn >> 8); 218 idx++; 219 buf[idx] = (req->u.allocate_payload.pbn & 0xff); 220 idx++; 221 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) { 222 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) | 223 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf); 224 idx++; 225 } 226 if (req->u.allocate_payload.number_sdp_streams & 1) { 227 i = req->u.allocate_payload.number_sdp_streams - 1; 228 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4; 229 idx++; 230 } 231 break; 232 case DP_QUERY_PAYLOAD: 233 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4; 234 idx++; 235 buf[idx] = (req->u.query_payload.vcpi & 0x7f); 236 idx++; 237 break; 238 case DP_REMOTE_DPCD_READ: 239 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4; 240 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf; 241 idx++; 242 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8; 243 idx++; 244 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff); 245 idx++; 246 buf[idx] = (req->u.dpcd_read.num_bytes); 247 idx++; 248 break; 249 250 case DP_REMOTE_DPCD_WRITE: 251 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4; 252 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf; 253 idx++; 254 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8; 255 idx++; 256 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff); 257 idx++; 258 buf[idx] = (req->u.dpcd_write.num_bytes); 259 idx++; 260 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes); 261 idx += req->u.dpcd_write.num_bytes; 262 break; 263 case DP_REMOTE_I2C_READ: 264 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4; 265 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3); 266 idx++; 267 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) { 268 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f; 269 idx++; 270 buf[idx] = req->u.i2c_read.transactions[i].num_bytes; 271 idx++; 272 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes); 273 idx += req->u.i2c_read.transactions[i].num_bytes; 274 275 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5; 276 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf); 277 idx++; 278 } 279 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f; 280 idx++; 281 buf[idx] = (req->u.i2c_read.num_bytes_read); 282 idx++; 283 break; 284 285 case DP_REMOTE_I2C_WRITE: 286 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4; 287 idx++; 288 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f; 289 idx++; 290 buf[idx] = (req->u.i2c_write.num_bytes); 291 idx++; 292 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes); 293 idx += req->u.i2c_write.num_bytes; 294 break; 295 } 296 raw->cur_len = idx; 297} 298 299static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len) 300{ 301 u8 crc4; 302 crc4 = drm_dp_msg_data_crc4(msg, len); 303 msg[len] = crc4; 304} 305 306static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep, 307 struct drm_dp_sideband_msg_tx *raw) 308{ 309 int idx = 0; 310 u8 *buf = raw->msg; 311 312 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f); 313 314 raw->cur_len = idx; 315} 316 317/* this adds a chunk of msg to the builder to get the final msg */ 318static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg, 319 u8 *replybuf, u8 replybuflen, bool hdr) 320{ 321 int ret; 322 u8 crc4; 323 324 if (hdr) { 325 u8 hdrlen; 326 struct drm_dp_sideband_msg_hdr recv_hdr; 327 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen); 328 if (ret == false) { 329 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false); 330 return false; 331 } 332 333 /* get length contained in this portion */ 334 msg->curchunk_len = recv_hdr.msg_len; 335 msg->curchunk_hdrlen = hdrlen; 336 337 /* we have already gotten an somt - don't bother parsing */ 338 if (recv_hdr.somt && msg->have_somt) 339 return false; 340 341 if (recv_hdr.somt) { 342 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr)); 343 msg->have_somt = true; 344 } 345 if (recv_hdr.eomt) 346 msg->have_eomt = true; 347 348 /* copy the bytes for the remainder of this header chunk */ 349 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen)); 350 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx); 351 } else { 352 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen); 353 msg->curchunk_idx += replybuflen; 354 } 355 356 if (msg->curchunk_idx >= msg->curchunk_len) { 357 /* do CRC */ 358 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1); 359 /* copy chunk into bigger msg */ 360 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1); 361 msg->curlen += msg->curchunk_len - 1; 362 } 363 return true; 364} 365 366static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw, 367 struct drm_dp_sideband_msg_reply_body *repmsg) 368{ 369 int idx = 1; 370 int i; 371 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16); 372 idx += 16; 373 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf; 374 idx++; 375 if (idx > raw->curlen) 376 goto fail_len; 377 for (i = 0; i < repmsg->u.link_addr.nports; i++) { 378 if (raw->msg[idx] & 0x80) 379 repmsg->u.link_addr.ports[i].input_port = 1; 380 381 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7; 382 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf); 383 384 idx++; 385 if (idx > raw->curlen) 386 goto fail_len; 387 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1; 388 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1; 389 if (repmsg->u.link_addr.ports[i].input_port == 0) 390 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1; 391 idx++; 392 if (idx > raw->curlen) 393 goto fail_len; 394 if (repmsg->u.link_addr.ports[i].input_port == 0) { 395 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]); 396 idx++; 397 if (idx > raw->curlen) 398 goto fail_len; 399 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16); 400 idx += 16; 401 if (idx > raw->curlen) 402 goto fail_len; 403 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf; 404 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf); 405 idx++; 406 407 } 408 if (idx > raw->curlen) 409 goto fail_len; 410 } 411 412 return true; 413fail_len: 414 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen); 415 return false; 416} 417 418static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw, 419 struct drm_dp_sideband_msg_reply_body *repmsg) 420{ 421 int idx = 1; 422 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf; 423 idx++; 424 if (idx > raw->curlen) 425 goto fail_len; 426 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx]; 427 if (idx > raw->curlen) 428 goto fail_len; 429 430 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes); 431 return true; 432fail_len: 433 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen); 434 return false; 435} 436 437static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw, 438 struct drm_dp_sideband_msg_reply_body *repmsg) 439{ 440 int idx = 1; 441 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf; 442 idx++; 443 if (idx > raw->curlen) 444 goto fail_len; 445 return true; 446fail_len: 447 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen); 448 return false; 449} 450 451static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw, 452 struct drm_dp_sideband_msg_reply_body *repmsg) 453{ 454 int idx = 1; 455 456 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf); 457 idx++; 458 if (idx > raw->curlen) 459 goto fail_len; 460 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx]; 461 idx++; 462 /* TODO check */ 463 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes); 464 return true; 465fail_len: 466 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen); 467 return false; 468} 469 470static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw, 471 struct drm_dp_sideband_msg_reply_body *repmsg) 472{ 473 int idx = 1; 474 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf; 475 idx++; 476 if (idx > raw->curlen) 477 goto fail_len; 478 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]); 479 idx += 2; 480 if (idx > raw->curlen) 481 goto fail_len; 482 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]); 483 idx += 2; 484 if (idx > raw->curlen) 485 goto fail_len; 486 return true; 487fail_len: 488 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen); 489 return false; 490} 491 492static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw, 493 struct drm_dp_sideband_msg_reply_body *repmsg) 494{ 495 int idx = 1; 496 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf; 497 idx++; 498 if (idx > raw->curlen) 499 goto fail_len; 500 repmsg->u.allocate_payload.vcpi = raw->msg[idx]; 501 idx++; 502 if (idx > raw->curlen) 503 goto fail_len; 504 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]); 505 idx += 2; 506 if (idx > raw->curlen) 507 goto fail_len; 508 return true; 509fail_len: 510 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen); 511 return false; 512} 513 514static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw, 515 struct drm_dp_sideband_msg_reply_body *repmsg) 516{ 517 int idx = 1; 518 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf; 519 idx++; 520 if (idx > raw->curlen) 521 goto fail_len; 522 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]); 523 idx += 2; 524 if (idx > raw->curlen) 525 goto fail_len; 526 return true; 527fail_len: 528 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen); 529 return false; 530} 531 532static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw, 533 struct drm_dp_sideband_msg_reply_body *msg) 534{ 535 memset(msg, 0, sizeof(*msg)); 536 msg->reply_type = (raw->msg[0] & 0x80) >> 7; 537 msg->req_type = (raw->msg[0] & 0x7f); 538 539 if (msg->reply_type) { 540 memcpy(msg->u.nak.guid, &raw->msg[1], 16); 541 msg->u.nak.reason = raw->msg[17]; 542 msg->u.nak.nak_data = raw->msg[18]; 543 return false; 544 } 545 546 switch (msg->req_type) { 547 case DP_LINK_ADDRESS: 548 return drm_dp_sideband_parse_link_address(raw, msg); 549 case DP_QUERY_PAYLOAD: 550 return drm_dp_sideband_parse_query_payload_ack(raw, msg); 551 case DP_REMOTE_DPCD_READ: 552 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg); 553 case DP_REMOTE_DPCD_WRITE: 554 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg); 555 case DP_REMOTE_I2C_READ: 556 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg); 557 case DP_ENUM_PATH_RESOURCES: 558 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg); 559 case DP_ALLOCATE_PAYLOAD: 560 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg); 561 default: 562 DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type); 563 return false; 564 } 565} 566 567static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw, 568 struct drm_dp_sideband_msg_req_body *msg) 569{ 570 int idx = 1; 571 572 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4; 573 idx++; 574 if (idx > raw->curlen) 575 goto fail_len; 576 577 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16); 578 idx += 16; 579 if (idx > raw->curlen) 580 goto fail_len; 581 582 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1; 583 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1; 584 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1; 585 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1; 586 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7); 587 idx++; 588 return true; 589fail_len: 590 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen); 591 return false; 592} 593 594static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw, 595 struct drm_dp_sideband_msg_req_body *msg) 596{ 597 int idx = 1; 598 599 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4; 600 idx++; 601 if (idx > raw->curlen) 602 goto fail_len; 603 604 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16); 605 idx += 16; 606 if (idx > raw->curlen) 607 goto fail_len; 608 609 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]); 610 idx++; 611 return true; 612fail_len: 613 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen); 614 return false; 615} 616 617static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw, 618 struct drm_dp_sideband_msg_req_body *msg) 619{ 620 memset(msg, 0, sizeof(*msg)); 621 msg->req_type = (raw->msg[0] & 0x7f); 622 623 switch (msg->req_type) { 624 case DP_CONNECTION_STATUS_NOTIFY: 625 return drm_dp_sideband_parse_connection_status_notify(raw, msg); 626 case DP_RESOURCE_STATUS_NOTIFY: 627 return drm_dp_sideband_parse_resource_status_notify(raw, msg); 628 default: 629 DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type); 630 return false; 631 } 632} 633 634static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes) 635{ 636 struct drm_dp_sideband_msg_req_body req; 637 638 req.req_type = DP_REMOTE_DPCD_WRITE; 639 req.u.dpcd_write.port_number = port_num; 640 req.u.dpcd_write.dpcd_address = offset; 641 req.u.dpcd_write.num_bytes = num_bytes; 642 req.u.dpcd_write.bytes = bytes; 643 drm_dp_encode_sideband_req(&req, msg); 644 645 return 0; 646} 647 648static int build_link_address(struct drm_dp_sideband_msg_tx *msg) 649{ 650 struct drm_dp_sideband_msg_req_body req; 651 652 req.req_type = DP_LINK_ADDRESS; 653 drm_dp_encode_sideband_req(&req, msg); 654 return 0; 655} 656 657static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num) 658{ 659 struct drm_dp_sideband_msg_req_body req; 660 661 req.req_type = DP_ENUM_PATH_RESOURCES; 662 req.u.port_num.port_number = port_num; 663 drm_dp_encode_sideband_req(&req, msg); 664 msg->path_msg = true; 665 return 0; 666} 667 668static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num, 669 u8 vcpi, uint16_t pbn) 670{ 671 struct drm_dp_sideband_msg_req_body req; 672 memset(&req, 0, sizeof(req)); 673 req.req_type = DP_ALLOCATE_PAYLOAD; 674 req.u.allocate_payload.port_number = port_num; 675 req.u.allocate_payload.vcpi = vcpi; 676 req.u.allocate_payload.pbn = pbn; 677 drm_dp_encode_sideband_req(&req, msg); 678 msg->path_msg = true; 679 return 0; 680} 681 682static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr, 683 struct drm_dp_vcpi *vcpi) 684{ 685 int ret, vcpi_ret; 686 687 mutex_lock(&mgr->payload_lock); 688 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1); 689 if (ret > mgr->max_payloads) { 690 ret = -EINVAL; 691 DRM_DEBUG_KMS("out of payload ids %d\n", ret); 692 goto out_unlock; 693 } 694 695 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1); 696 if (vcpi_ret > mgr->max_payloads) { 697 ret = -EINVAL; 698 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret); 699 goto out_unlock; 700 } 701 702 set_bit(ret, &mgr->payload_mask); 703 set_bit(vcpi_ret, &mgr->vcpi_mask); 704 vcpi->vcpi = vcpi_ret + 1; 705 mgr->proposed_vcpis[ret - 1] = vcpi; 706out_unlock: 707 mutex_unlock(&mgr->payload_lock); 708 return ret; 709} 710 711static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr, 712 int vcpi) 713{ 714 int i; 715 if (vcpi == 0) 716 return; 717 718 mutex_lock(&mgr->payload_lock); 719 DRM_DEBUG_KMS("putting payload %d\n", vcpi); 720 clear_bit(vcpi - 1, &mgr->vcpi_mask); 721 722 for (i = 0; i < mgr->max_payloads; i++) { 723 if (mgr->proposed_vcpis[i]) 724 if (mgr->proposed_vcpis[i]->vcpi == vcpi) { 725 mgr->proposed_vcpis[i] = NULL; 726 clear_bit(i + 1, &mgr->payload_mask); 727 } 728 } 729 mutex_unlock(&mgr->payload_lock); 730} 731 732static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr, 733 struct drm_dp_sideband_msg_tx *txmsg) 734{ 735 bool ret; 736 mutex_lock(&mgr->qlock); 737 ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX || 738 txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT); 739 mutex_unlock(&mgr->qlock); 740 return ret; 741} 742 743static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb, 744 struct drm_dp_sideband_msg_tx *txmsg) 745{ 746 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr; 747 int ret; 748 749 ret = wait_event_timeout(mgr->tx_waitq, 750 check_txmsg_state(mgr, txmsg), 751 (4 * HZ)); 752 mutex_lock(&mstb->mgr->qlock); 753 if (ret > 0) { 754 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) { 755 ret = -EIO; 756 goto out; 757 } 758 } else { 759 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno); 760 761 /* dump some state */ 762 ret = -EIO; 763 764 /* remove from q */ 765 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED || 766 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) { 767 list_del(&txmsg->next); 768 } 769 770 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND || 771 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) { 772 mstb->tx_slots[txmsg->seqno] = NULL; 773 } 774 } 775out: 776 mutex_unlock(&mgr->qlock); 777 778 return ret; 779} 780 781static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad) 782{ 783 struct drm_dp_mst_branch *mstb; 784 785 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL); 786 if (!mstb) 787 return NULL; 788 789 mstb->lct = lct; 790 if (lct > 1) 791 memcpy(mstb->rad, rad, lct / 2); 792 INIT_LIST_HEAD(&mstb->ports); 793 kref_init(&mstb->kref); 794 return mstb; 795} 796 797static void drm_dp_destroy_mst_branch_device(struct kref *kref) 798{ 799 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref); 800 struct drm_dp_mst_port *port, *tmp; 801 bool wake_tx = false; 802 803 cancel_work_sync(&mstb->mgr->work); 804 805 /* 806 * destroy all ports - don't need lock 807 * as there are no more references to the mst branch 808 * device at this point. 809 */ 810 list_for_each_entry_safe(port, tmp, &mstb->ports, next) { 811 list_del(&port->next); 812 drm_dp_put_port(port); 813 } 814 815 /* drop any tx slots msg */ 816 mutex_lock(&mstb->mgr->qlock); 817 if (mstb->tx_slots[0]) { 818 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT; 819 mstb->tx_slots[0] = NULL; 820 wake_tx = true; 821 } 822 if (mstb->tx_slots[1]) { 823 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT; 824 mstb->tx_slots[1] = NULL; 825 wake_tx = true; 826 } 827 mutex_unlock(&mstb->mgr->qlock); 828 829 if (wake_tx) 830 wake_up(&mstb->mgr->tx_waitq); 831 kfree(mstb); 832} 833 834static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb) 835{ 836 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device); 837} 838 839 840static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt) 841{ 842 switch (old_pdt) { 843 case DP_PEER_DEVICE_DP_LEGACY_CONV: 844 case DP_PEER_DEVICE_SST_SINK: 845 /* remove i2c over sideband */ 846 drm_dp_mst_unregister_i2c_bus(&port->aux); 847 break; 848 case DP_PEER_DEVICE_MST_BRANCHING: 849 drm_dp_put_mst_branch_device(port->mstb); 850 port->mstb = NULL; 851 break; 852 } 853} 854 855static void drm_dp_destroy_port(struct kref *kref) 856{ 857 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref); 858 struct drm_dp_mst_topology_mgr *mgr = port->mgr; 859 if (!port->input) { 860 port->vcpi.num_slots = 0; 861 if (port->connector) 862 (*port->mgr->cbs->destroy_connector)(mgr, port->connector); 863 drm_dp_port_teardown_pdt(port, port->pdt); 864 865 if (!port->input && port->vcpi.vcpi > 0) 866 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi); 867 } 868 kfree(port); 869 870 (*mgr->cbs->hotplug)(mgr); 871} 872 873static void drm_dp_put_port(struct drm_dp_mst_port *port) 874{ 875 kref_put(&port->kref, drm_dp_destroy_port); 876} 877 878static struct drm_dp_mst_branch *drm_dp_mst_get_validated_mstb_ref_locked(struct drm_dp_mst_branch *mstb, struct drm_dp_mst_branch *to_find) 879{ 880 struct drm_dp_mst_port *port; 881 struct drm_dp_mst_branch *rmstb; 882 if (to_find == mstb) { 883 kref_get(&mstb->kref); 884 return mstb; 885 } 886 list_for_each_entry(port, &mstb->ports, next) { 887 if (port->mstb) { 888 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find); 889 if (rmstb) 890 return rmstb; 891 } 892 } 893 return NULL; 894} 895 896static struct drm_dp_mst_branch *drm_dp_get_validated_mstb_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb) 897{ 898 struct drm_dp_mst_branch *rmstb = NULL; 899 mutex_lock(&mgr->lock); 900 if (mgr->mst_primary) 901 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb); 902 mutex_unlock(&mgr->lock); 903 return rmstb; 904} 905 906static struct drm_dp_mst_port *drm_dp_mst_get_port_ref_locked(struct drm_dp_mst_branch *mstb, struct drm_dp_mst_port *to_find) 907{ 908 struct drm_dp_mst_port *port, *mport; 909 910 list_for_each_entry(port, &mstb->ports, next) { 911 if (port == to_find) { 912 kref_get(&port->kref); 913 return port; 914 } 915 if (port->mstb) { 916 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find); 917 if (mport) 918 return mport; 919 } 920 } 921 return NULL; 922} 923 924static struct drm_dp_mst_port *drm_dp_get_validated_port_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) 925{ 926 struct drm_dp_mst_port *rport = NULL; 927 mutex_lock(&mgr->lock); 928 if (mgr->mst_primary) 929 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port); 930 mutex_unlock(&mgr->lock); 931 return rport; 932} 933 934static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num) 935{ 936 struct drm_dp_mst_port *port; 937 938 list_for_each_entry(port, &mstb->ports, next) { 939 if (port->port_num == port_num) { 940 kref_get(&port->kref); 941 return port; 942 } 943 } 944 945 return NULL; 946} 947 948/* 949 * calculate a new RAD for this MST branch device 950 * if parent has an LCT of 2 then it has 1 nibble of RAD, 951 * if parent has an LCT of 3 then it has 2 nibbles of RAD, 952 */ 953static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port, 954 u8 *rad) 955{ 956 int lct = port->parent->lct; 957 int shift = 4; 958 int idx = lct / 2; 959 if (lct > 1) { 960 memcpy(rad, port->parent->rad, idx); 961 shift = (lct % 2) ? 4 : 0; 962 } else 963 rad[0] = 0; 964 965 rad[idx] |= port->port_num << shift; 966 return lct + 1; 967} 968 969/* 970 * return sends link address for new mstb 971 */ 972static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port) 973{ 974 int ret; 975 u8 rad[6], lct; 976 bool send_link = false; 977 switch (port->pdt) { 978 case DP_PEER_DEVICE_DP_LEGACY_CONV: 979 case DP_PEER_DEVICE_SST_SINK: 980 /* add i2c over sideband */ 981 ret = drm_dp_mst_register_i2c_bus(&port->aux); 982 break; 983 case DP_PEER_DEVICE_MST_BRANCHING: 984 lct = drm_dp_calculate_rad(port, rad); 985 986 port->mstb = drm_dp_add_mst_branch_device(lct, rad); 987 port->mstb->mgr = port->mgr; 988 port->mstb->port_parent = port; 989 990 send_link = true; 991 break; 992 } 993 return send_link; 994} 995 996static void drm_dp_check_port_guid(struct drm_dp_mst_branch *mstb, 997 struct drm_dp_mst_port *port) 998{ 999 int ret; 1000 if (port->dpcd_rev >= 0x12) { 1001 port->guid_valid = drm_dp_validate_guid(mstb->mgr, port->guid); 1002 if (!port->guid_valid) { 1003 ret = drm_dp_send_dpcd_write(mstb->mgr, 1004 port, 1005 DP_GUID, 1006 16, port->guid); 1007 port->guid_valid = true; 1008 } 1009 } 1010} 1011 1012static void build_mst_prop_path(struct drm_dp_mst_port *port, 1013 struct drm_dp_mst_branch *mstb, 1014 char *proppath) 1015{ 1016 int i; 1017 char temp[8]; 1018 snprintf(proppath, 255, "mst:%d", mstb->mgr->conn_base_id); 1019 for (i = 0; i < (mstb->lct - 1); i++) { 1020 int shift = (i % 2) ? 0 : 4; 1021 int port_num = mstb->rad[i / 2] >> shift; 1022 snprintf(temp, 8, "-%d", port_num); 1023 strncat(proppath, temp, 255); 1024 } 1025 snprintf(temp, 8, "-%d", port->port_num); 1026 strncat(proppath, temp, 255); 1027} 1028 1029static void drm_dp_add_port(struct drm_dp_mst_branch *mstb, 1030 struct device *dev, 1031 struct drm_dp_link_addr_reply_port *port_msg) 1032{ 1033 struct drm_dp_mst_port *port; 1034 bool ret; 1035 bool created = false; 1036 int old_pdt = 0; 1037 int old_ddps = 0; 1038 port = drm_dp_get_port(mstb, port_msg->port_number); 1039 if (!port) { 1040 port = kzalloc(sizeof(*port), GFP_KERNEL); 1041 if (!port) 1042 return; 1043 kref_init(&port->kref); 1044 port->parent = mstb; 1045 port->port_num = port_msg->port_number; 1046 port->mgr = mstb->mgr; 1047 port->aux.name = "DPMST"; 1048 port->aux.dev = dev; 1049 created = true; 1050 } else { 1051 old_pdt = port->pdt; 1052 old_ddps = port->ddps; 1053 } 1054 1055 port->pdt = port_msg->peer_device_type; 1056 port->input = port_msg->input_port; 1057 port->mcs = port_msg->mcs; 1058 port->ddps = port_msg->ddps; 1059 port->ldps = port_msg->legacy_device_plug_status; 1060 port->dpcd_rev = port_msg->dpcd_revision; 1061 port->num_sdp_streams = port_msg->num_sdp_streams; 1062 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks; 1063 memcpy(port->guid, port_msg->peer_guid, 16); 1064 1065 /* manage mstb port lists with mgr lock - take a reference 1066 for this list */ 1067 if (created) { 1068 mutex_lock(&mstb->mgr->lock); 1069 kref_get(&port->kref); 1070 list_add(&port->next, &mstb->ports); 1071 mutex_unlock(&mstb->mgr->lock); 1072 } 1073 1074 if (old_ddps != port->ddps) { 1075 if (port->ddps) { 1076 drm_dp_check_port_guid(mstb, port); 1077 if (!port->input) 1078 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port); 1079 } else { 1080 port->guid_valid = false; 1081 port->available_pbn = 0; 1082 } 1083 } 1084 1085 if (old_pdt != port->pdt && !port->input) { 1086 drm_dp_port_teardown_pdt(port, old_pdt); 1087 1088 ret = drm_dp_port_setup_pdt(port); 1089 if (ret == true) { 1090 drm_dp_send_link_address(mstb->mgr, port->mstb); 1091 port->mstb->link_address_sent = true; 1092 } 1093 } 1094 1095 if (created && !port->input) { 1096 char proppath[255]; 1097 build_mst_prop_path(port, mstb, proppath); 1098 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath); 1099 } 1100 1101 /* put reference to this port */ 1102 drm_dp_put_port(port); 1103} 1104 1105static void drm_dp_update_port(struct drm_dp_mst_branch *mstb, 1106 struct drm_dp_connection_status_notify *conn_stat) 1107{ 1108 struct drm_dp_mst_port *port; 1109 int old_pdt; 1110 int old_ddps; 1111 bool dowork = false; 1112 port = drm_dp_get_port(mstb, conn_stat->port_number); 1113 if (!port) 1114 return; 1115 1116 old_ddps = port->ddps; 1117 old_pdt = port->pdt; 1118 port->pdt = conn_stat->peer_device_type; 1119 port->mcs = conn_stat->message_capability_status; 1120 port->ldps = conn_stat->legacy_device_plug_status; 1121 port->ddps = conn_stat->displayport_device_plug_status; 1122 1123 if (old_ddps != port->ddps) { 1124 if (port->ddps) { 1125 drm_dp_check_port_guid(mstb, port); 1126 dowork = true; 1127 } else { 1128 port->guid_valid = false; 1129 port->available_pbn = 0; 1130 } 1131 } 1132 if (old_pdt != port->pdt && !port->input) { 1133 drm_dp_port_teardown_pdt(port, old_pdt); 1134 1135 if (drm_dp_port_setup_pdt(port)) 1136 dowork = true; 1137 } 1138 1139 drm_dp_put_port(port); 1140 if (dowork) 1141 queue_work(system_long_wq, &mstb->mgr->work); 1142 1143} 1144 1145static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr, 1146 u8 lct, u8 *rad) 1147{ 1148 struct drm_dp_mst_branch *mstb; 1149 struct drm_dp_mst_port *port; 1150 int i; 1151 /* find the port by iterating down */ 1152 mstb = mgr->mst_primary; 1153 1154 for (i = 0; i < lct - 1; i++) { 1155 int shift = (i % 2) ? 0 : 4; 1156 int port_num = rad[i / 2] >> shift; 1157 1158 list_for_each_entry(port, &mstb->ports, next) { 1159 if (port->port_num == port_num) { 1160 if (!port->mstb) { 1161 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]); 1162 return NULL; 1163 } 1164 1165 mstb = port->mstb; 1166 break; 1167 } 1168 } 1169 } 1170 kref_get(&mstb->kref); 1171 return mstb; 1172} 1173 1174static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr, 1175 struct drm_dp_mst_branch *mstb) 1176{ 1177 struct drm_dp_mst_port *port; 1178 1179 if (!mstb->link_address_sent) { 1180 drm_dp_send_link_address(mgr, mstb); 1181 mstb->link_address_sent = true; 1182 } 1183 list_for_each_entry(port, &mstb->ports, next) { 1184 if (port->input) 1185 continue; 1186 1187 if (!port->ddps) 1188 continue; 1189 1190 if (!port->available_pbn) 1191 drm_dp_send_enum_path_resources(mgr, mstb, port); 1192 1193 if (port->mstb) 1194 drm_dp_check_and_send_link_address(mgr, port->mstb); 1195 } 1196} 1197 1198static void drm_dp_mst_link_probe_work(struct work_struct *work) 1199{ 1200 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work); 1201 1202 drm_dp_check_and_send_link_address(mgr, mgr->mst_primary); 1203 1204} 1205 1206static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr, 1207 u8 *guid) 1208{ 1209 static u8 zero_guid[16]; 1210 1211 if (!memcmp(guid, zero_guid, 16)) { 1212 u64 salt = get_jiffies_64(); 1213 memcpy(&guid[0], &salt, sizeof(u64)); 1214 memcpy(&guid[8], &salt, sizeof(u64)); 1215 return false; 1216 } 1217 return true; 1218} 1219 1220#if 0 1221static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes) 1222{ 1223 struct drm_dp_sideband_msg_req_body req; 1224 1225 req.req_type = DP_REMOTE_DPCD_READ; 1226 req.u.dpcd_read.port_number = port_num; 1227 req.u.dpcd_read.dpcd_address = offset; 1228 req.u.dpcd_read.num_bytes = num_bytes; 1229 drm_dp_encode_sideband_req(&req, msg); 1230 1231 return 0; 1232} 1233#endif 1234 1235static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr, 1236 bool up, u8 *msg, int len) 1237{ 1238 int ret; 1239 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE; 1240 int tosend, total, offset; 1241 int retries = 0; 1242 1243retry: 1244 total = len; 1245 offset = 0; 1246 do { 1247 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total); 1248 1249 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset, 1250 &msg[offset], 1251 tosend); 1252 if (ret != tosend) { 1253 if (ret == -EIO && retries < 5) { 1254 retries++; 1255 goto retry; 1256 } 1257 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret); 1258 WARN(1, "fail\n"); 1259 1260 return -EIO; 1261 } 1262 offset += tosend; 1263 total -= tosend; 1264 } while (total > 0); 1265 return 0; 1266} 1267 1268static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr, 1269 struct drm_dp_sideband_msg_tx *txmsg) 1270{ 1271 struct drm_dp_mst_branch *mstb = txmsg->dst; 1272 1273 /* both msg slots are full */ 1274 if (txmsg->seqno == -1) { 1275 if (mstb->tx_slots[0] && mstb->tx_slots[1]) { 1276 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__); 1277 return -EAGAIN; 1278 } 1279 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) { 1280 txmsg->seqno = mstb->last_seqno; 1281 mstb->last_seqno ^= 1; 1282 } else if (mstb->tx_slots[0] == NULL) 1283 txmsg->seqno = 0; 1284 else 1285 txmsg->seqno = 1; 1286 mstb->tx_slots[txmsg->seqno] = txmsg; 1287 } 1288 hdr->broadcast = 0; 1289 hdr->path_msg = txmsg->path_msg; 1290 hdr->lct = mstb->lct; 1291 hdr->lcr = mstb->lct - 1; 1292 if (mstb->lct > 1) 1293 memcpy(hdr->rad, mstb->rad, mstb->lct / 2); 1294 hdr->seqno = txmsg->seqno; 1295 return 0; 1296} 1297/* 1298 * process a single block of the next message in the sideband queue 1299 */ 1300static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr, 1301 struct drm_dp_sideband_msg_tx *txmsg, 1302 bool up) 1303{ 1304 u8 chunk[48]; 1305 struct drm_dp_sideband_msg_hdr hdr; 1306 int len, space, idx, tosend; 1307 int ret; 1308 1309 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr)); 1310 1311 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) { 1312 txmsg->seqno = -1; 1313 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND; 1314 } 1315 1316 /* make hdr from dst mst - for replies use seqno 1317 otherwise assign one */ 1318 ret = set_hdr_from_dst_qlock(&hdr, txmsg); 1319 if (ret < 0) 1320 return ret; 1321 1322 /* amount left to send in this message */ 1323 len = txmsg->cur_len - txmsg->cur_offset; 1324 1325 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */ 1326 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr); 1327 1328 tosend = min(len, space); 1329 if (len == txmsg->cur_len) 1330 hdr.somt = 1; 1331 if (space >= len) 1332 hdr.eomt = 1; 1333 1334 1335 hdr.msg_len = tosend + 1; 1336 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx); 1337 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend); 1338 /* add crc at end */ 1339 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend); 1340 idx += tosend + 1; 1341 1342 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx); 1343 if (ret) { 1344 DRM_DEBUG_KMS("sideband msg failed to send\n"); 1345 return ret; 1346 } 1347 1348 txmsg->cur_offset += tosend; 1349 if (txmsg->cur_offset == txmsg->cur_len) { 1350 txmsg->state = DRM_DP_SIDEBAND_TX_SENT; 1351 return 1; 1352 } 1353 return 0; 1354} 1355 1356/* must be called holding qlock */ 1357static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr) 1358{ 1359 struct drm_dp_sideband_msg_tx *txmsg; 1360 int ret; 1361 1362 /* construct a chunk from the first msg in the tx_msg queue */ 1363 if (list_empty(&mgr->tx_msg_downq)) { 1364 mgr->tx_down_in_progress = false; 1365 return; 1366 } 1367 mgr->tx_down_in_progress = true; 1368 1369 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next); 1370 ret = process_single_tx_qlock(mgr, txmsg, false); 1371 if (ret == 1) { 1372 /* txmsg is sent it should be in the slots now */ 1373 list_del(&txmsg->next); 1374 } else if (ret) { 1375 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret); 1376 list_del(&txmsg->next); 1377 if (txmsg->seqno != -1) 1378 txmsg->dst->tx_slots[txmsg->seqno] = NULL; 1379 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT; 1380 wake_up(&mgr->tx_waitq); 1381 } 1382 if (list_empty(&mgr->tx_msg_downq)) { 1383 mgr->tx_down_in_progress = false; 1384 return; 1385 } 1386} 1387 1388/* called holding qlock */ 1389static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr) 1390{ 1391 struct drm_dp_sideband_msg_tx *txmsg; 1392 int ret; 1393 1394 /* construct a chunk from the first msg in the tx_msg queue */ 1395 if (list_empty(&mgr->tx_msg_upq)) { 1396 mgr->tx_up_in_progress = false; 1397 return; 1398 } 1399 1400 txmsg = list_first_entry(&mgr->tx_msg_upq, struct drm_dp_sideband_msg_tx, next); 1401 ret = process_single_tx_qlock(mgr, txmsg, true); 1402 if (ret == 1) { 1403 /* up txmsgs aren't put in slots - so free after we send it */ 1404 list_del(&txmsg->next); 1405 kfree(txmsg); 1406 } else if (ret) 1407 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret); 1408 mgr->tx_up_in_progress = true; 1409} 1410 1411static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr, 1412 struct drm_dp_sideband_msg_tx *txmsg) 1413{ 1414 mutex_lock(&mgr->qlock); 1415 list_add_tail(&txmsg->next, &mgr->tx_msg_downq); 1416 if (!mgr->tx_down_in_progress) 1417 process_single_down_tx_qlock(mgr); 1418 mutex_unlock(&mgr->qlock); 1419} 1420 1421static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr, 1422 struct drm_dp_mst_branch *mstb) 1423{ 1424 int len; 1425 struct drm_dp_sideband_msg_tx *txmsg; 1426 int ret; 1427 1428 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL); 1429 if (!txmsg) 1430 return -ENOMEM; 1431 1432 txmsg->dst = mstb; 1433 len = build_link_address(txmsg); 1434 1435 drm_dp_queue_down_tx(mgr, txmsg); 1436 1437 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 1438 if (ret > 0) { 1439 int i; 1440 1441 if (txmsg->reply.reply_type == 1) 1442 DRM_DEBUG_KMS("link address nak received\n"); 1443 else { 1444 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports); 1445 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) { 1446 DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n", i, 1447 txmsg->reply.u.link_addr.ports[i].input_port, 1448 txmsg->reply.u.link_addr.ports[i].peer_device_type, 1449 txmsg->reply.u.link_addr.ports[i].port_number, 1450 txmsg->reply.u.link_addr.ports[i].dpcd_revision, 1451 txmsg->reply.u.link_addr.ports[i].mcs, 1452 txmsg->reply.u.link_addr.ports[i].ddps, 1453 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status, 1454 txmsg->reply.u.link_addr.ports[i].num_sdp_streams, 1455 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks); 1456 } 1457 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) { 1458 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]); 1459 } 1460 (*mgr->cbs->hotplug)(mgr); 1461 } 1462 } else 1463 DRM_DEBUG_KMS("link address failed %d\n", ret); 1464 1465 kfree(txmsg); 1466 return 0; 1467} 1468 1469static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr, 1470 struct drm_dp_mst_branch *mstb, 1471 struct drm_dp_mst_port *port) 1472{ 1473 int len; 1474 struct drm_dp_sideband_msg_tx *txmsg; 1475 int ret; 1476 1477 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL); 1478 if (!txmsg) 1479 return -ENOMEM; 1480 1481 txmsg->dst = mstb; 1482 len = build_enum_path_resources(txmsg, port->port_num); 1483 1484 drm_dp_queue_down_tx(mgr, txmsg); 1485 1486 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 1487 if (ret > 0) { 1488 if (txmsg->reply.reply_type == 1) 1489 DRM_DEBUG_KMS("enum path resources nak received\n"); 1490 else { 1491 if (port->port_num != txmsg->reply.u.path_resources.port_number) 1492 DRM_ERROR("got incorrect port in response\n"); 1493 DRM_DEBUG_KMS("enum path resources %d: %d %d\n", txmsg->reply.u.path_resources.port_number, txmsg->reply.u.path_resources.full_payload_bw_number, 1494 txmsg->reply.u.path_resources.avail_payload_bw_number); 1495 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number; 1496 } 1497 } 1498 1499 kfree(txmsg); 1500 return 0; 1501} 1502 1503static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr, 1504 struct drm_dp_mst_port *port, 1505 int id, 1506 int pbn) 1507{ 1508 struct drm_dp_sideband_msg_tx *txmsg; 1509 struct drm_dp_mst_branch *mstb; 1510 int len, ret; 1511 1512 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent); 1513 if (!mstb) 1514 return -EINVAL; 1515 1516 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL); 1517 if (!txmsg) { 1518 ret = -ENOMEM; 1519 goto fail_put; 1520 } 1521 1522 txmsg->dst = mstb; 1523 len = build_allocate_payload(txmsg, port->port_num, 1524 id, 1525 pbn); 1526 1527 drm_dp_queue_down_tx(mgr, txmsg); 1528 1529 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 1530 if (ret > 0) { 1531 if (txmsg->reply.reply_type == 1) { 1532 ret = -EINVAL; 1533 } else 1534 ret = 0; 1535 } 1536 kfree(txmsg); 1537fail_put: 1538 drm_dp_put_mst_branch_device(mstb); 1539 return ret; 1540} 1541 1542static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr, 1543 int id, 1544 struct drm_dp_payload *payload) 1545{ 1546 int ret; 1547 1548 ret = drm_dp_dpcd_write_payload(mgr, id, payload); 1549 if (ret < 0) { 1550 payload->payload_state = 0; 1551 return ret; 1552 } 1553 payload->payload_state = DP_PAYLOAD_LOCAL; 1554 return 0; 1555} 1556 1557static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr, 1558 struct drm_dp_mst_port *port, 1559 int id, 1560 struct drm_dp_payload *payload) 1561{ 1562 int ret; 1563 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn); 1564 if (ret < 0) 1565 return ret; 1566 payload->payload_state = DP_PAYLOAD_REMOTE; 1567 return ret; 1568} 1569 1570static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr, 1571 struct drm_dp_mst_port *port, 1572 int id, 1573 struct drm_dp_payload *payload) 1574{ 1575 DRM_DEBUG_KMS("\n"); 1576 /* its okay for these to fail */ 1577 if (port) { 1578 drm_dp_payload_send_msg(mgr, port, id, 0); 1579 } 1580 1581 drm_dp_dpcd_write_payload(mgr, id, payload); 1582 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL; 1583 return 0; 1584} 1585 1586static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr, 1587 int id, 1588 struct drm_dp_payload *payload) 1589{ 1590 payload->payload_state = 0; 1591 return 0; 1592} 1593 1594/** 1595 * drm_dp_update_payload_part1() - Execute payload update part 1 1596 * @mgr: manager to use. 1597 * 1598 * This iterates over all proposed virtual channels, and tries to 1599 * allocate space in the link for them. For 0->slots transitions, 1600 * this step just writes the VCPI to the MST device. For slots->0 1601 * transitions, this writes the updated VCPIs and removes the 1602 * remote VC payloads. 1603 * 1604 * after calling this the driver should generate ACT and payload 1605 * packets. 1606 */ 1607int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr) 1608{ 1609 int i, j; 1610 int cur_slots = 1; 1611 struct drm_dp_payload req_payload; 1612 struct drm_dp_mst_port *port; 1613 1614 mutex_lock(&mgr->payload_lock); 1615 for (i = 0; i < mgr->max_payloads; i++) { 1616 /* solve the current payloads - compare to the hw ones 1617 - update the hw view */ 1618 req_payload.start_slot = cur_slots; 1619 if (mgr->proposed_vcpis[i]) { 1620 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi); 1621 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots; 1622 } else { 1623 port = NULL; 1624 req_payload.num_slots = 0; 1625 } 1626 1627 if (mgr->payloads[i].start_slot != req_payload.start_slot) { 1628 mgr->payloads[i].start_slot = req_payload.start_slot; 1629 } 1630 /* work out what is required to happen with this payload */ 1631 if (mgr->payloads[i].num_slots != req_payload.num_slots) { 1632 1633 /* need to push an update for this payload */ 1634 if (req_payload.num_slots) { 1635 drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload); 1636 mgr->payloads[i].num_slots = req_payload.num_slots; 1637 } else if (mgr->payloads[i].num_slots) { 1638 mgr->payloads[i].num_slots = 0; 1639 drm_dp_destroy_payload_step1(mgr, port, port->vcpi.vcpi, &mgr->payloads[i]); 1640 req_payload.payload_state = mgr->payloads[i].payload_state; 1641 mgr->payloads[i].start_slot = 0; 1642 } 1643 mgr->payloads[i].payload_state = req_payload.payload_state; 1644 } 1645 cur_slots += req_payload.num_slots; 1646 } 1647 1648 for (i = 0; i < mgr->max_payloads; i++) { 1649 if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) { 1650 DRM_DEBUG_KMS("removing payload %d\n", i); 1651 for (j = i; j < mgr->max_payloads - 1; j++) { 1652 memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload)); 1653 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1]; 1654 if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) { 1655 set_bit(j + 1, &mgr->payload_mask); 1656 } else { 1657 clear_bit(j + 1, &mgr->payload_mask); 1658 } 1659 } 1660 memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload)); 1661 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL; 1662 clear_bit(mgr->max_payloads, &mgr->payload_mask); 1663 1664 } 1665 } 1666 mutex_unlock(&mgr->payload_lock); 1667 1668 return 0; 1669} 1670EXPORT_SYMBOL(drm_dp_update_payload_part1); 1671 1672/** 1673 * drm_dp_update_payload_part2() - Execute payload update part 2 1674 * @mgr: manager to use. 1675 * 1676 * This iterates over all proposed virtual channels, and tries to 1677 * allocate space in the link for them. For 0->slots transitions, 1678 * this step writes the remote VC payload commands. For slots->0 1679 * this just resets some internal state. 1680 */ 1681int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr) 1682{ 1683 struct drm_dp_mst_port *port; 1684 int i; 1685 int ret = 0; 1686 mutex_lock(&mgr->payload_lock); 1687 for (i = 0; i < mgr->max_payloads; i++) { 1688 1689 if (!mgr->proposed_vcpis[i]) 1690 continue; 1691 1692 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi); 1693 1694 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state); 1695 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) { 1696 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]); 1697 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) { 1698 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]); 1699 } 1700 if (ret) { 1701 mutex_unlock(&mgr->payload_lock); 1702 return ret; 1703 } 1704 } 1705 mutex_unlock(&mgr->payload_lock); 1706 return 0; 1707} 1708EXPORT_SYMBOL(drm_dp_update_payload_part2); 1709 1710#if 0 /* unused as of yet */ 1711static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr, 1712 struct drm_dp_mst_port *port, 1713 int offset, int size) 1714{ 1715 int len; 1716 struct drm_dp_sideband_msg_tx *txmsg; 1717 1718 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL); 1719 if (!txmsg) 1720 return -ENOMEM; 1721 1722 len = build_dpcd_read(txmsg, port->port_num, 0, 8); 1723 txmsg->dst = port->parent; 1724 1725 drm_dp_queue_down_tx(mgr, txmsg); 1726 1727 return 0; 1728} 1729#endif 1730 1731static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr, 1732 struct drm_dp_mst_port *port, 1733 int offset, int size, u8 *bytes) 1734{ 1735 int len; 1736 int ret; 1737 struct drm_dp_sideband_msg_tx *txmsg; 1738 struct drm_dp_mst_branch *mstb; 1739 1740 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent); 1741 if (!mstb) 1742 return -EINVAL; 1743 1744 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL); 1745 if (!txmsg) { 1746 ret = -ENOMEM; 1747 goto fail_put; 1748 } 1749 1750 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes); 1751 txmsg->dst = mstb; 1752 1753 drm_dp_queue_down_tx(mgr, txmsg); 1754 1755 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 1756 if (ret > 0) { 1757 if (txmsg->reply.reply_type == 1) { 1758 ret = -EINVAL; 1759 } else 1760 ret = 0; 1761 } 1762 kfree(txmsg); 1763fail_put: 1764 drm_dp_put_mst_branch_device(mstb); 1765 return ret; 1766} 1767 1768static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type) 1769{ 1770 struct drm_dp_sideband_msg_reply_body reply; 1771 1772 reply.reply_type = 1; 1773 reply.req_type = req_type; 1774 drm_dp_encode_sideband_reply(&reply, msg); 1775 return 0; 1776} 1777 1778static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr, 1779 struct drm_dp_mst_branch *mstb, 1780 int req_type, int seqno, bool broadcast) 1781{ 1782 struct drm_dp_sideband_msg_tx *txmsg; 1783 1784 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL); 1785 if (!txmsg) 1786 return -ENOMEM; 1787 1788 txmsg->dst = mstb; 1789 txmsg->seqno = seqno; 1790 drm_dp_encode_up_ack_reply(txmsg, req_type); 1791 1792 mutex_lock(&mgr->qlock); 1793 list_add_tail(&txmsg->next, &mgr->tx_msg_upq); 1794 if (!mgr->tx_up_in_progress) { 1795 process_single_up_tx_qlock(mgr); 1796 } 1797 mutex_unlock(&mgr->qlock); 1798 return 0; 1799} 1800 1801static int drm_dp_get_vc_payload_bw(int dp_link_bw, int dp_link_count) 1802{ 1803 switch (dp_link_bw) { 1804 case DP_LINK_BW_1_62: 1805 return 3 * dp_link_count; 1806 case DP_LINK_BW_2_7: 1807 return 5 * dp_link_count; 1808 case DP_LINK_BW_5_4: 1809 return 10 * dp_link_count; 1810 } 1811 BUG(); 1812} 1813 1814/** 1815 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager 1816 * @mgr: manager to set state for 1817 * @mst_state: true to enable MST on this connector - false to disable. 1818 * 1819 * This is called by the driver when it detects an MST capable device plugged 1820 * into a DP MST capable port, or when a DP MST capable device is unplugged. 1821 */ 1822int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state) 1823{ 1824 int ret = 0; 1825 struct drm_dp_mst_branch *mstb = NULL; 1826 1827 mutex_lock(&mgr->lock); 1828 if (mst_state == mgr->mst_state) 1829 goto out_unlock; 1830 1831 mgr->mst_state = mst_state; 1832 /* set the device into MST mode */ 1833 if (mst_state) { 1834 WARN_ON(mgr->mst_primary); 1835 1836 /* get dpcd info */ 1837 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE); 1838 if (ret != DP_RECEIVER_CAP_SIZE) { 1839 DRM_DEBUG_KMS("failed to read DPCD\n"); 1840 goto out_unlock; 1841 } 1842 1843 mgr->pbn_div = drm_dp_get_vc_payload_bw(mgr->dpcd[1], mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK); 1844 mgr->total_pbn = 2560; 1845 mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div); 1846 mgr->avail_slots = mgr->total_slots; 1847 1848 /* add initial branch device at LCT 1 */ 1849 mstb = drm_dp_add_mst_branch_device(1, NULL); 1850 if (mstb == NULL) { 1851 ret = -ENOMEM; 1852 goto out_unlock; 1853 } 1854 mstb->mgr = mgr; 1855 1856 /* give this the main reference */ 1857 mgr->mst_primary = mstb; 1858 kref_get(&mgr->mst_primary->kref); 1859 1860 { 1861 struct drm_dp_payload reset_pay; 1862 reset_pay.start_slot = 0; 1863 reset_pay.num_slots = 0x3f; 1864 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay); 1865 } 1866 1867 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 1868 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC); 1869 if (ret < 0) { 1870 goto out_unlock; 1871 } 1872 1873 1874 /* sort out guid */ 1875 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, mgr->guid, 16); 1876 if (ret != 16) { 1877 DRM_DEBUG_KMS("failed to read DP GUID %d\n", ret); 1878 goto out_unlock; 1879 } 1880 1881 mgr->guid_valid = drm_dp_validate_guid(mgr, mgr->guid); 1882 if (!mgr->guid_valid) { 1883 ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, mgr->guid, 16); 1884 mgr->guid_valid = true; 1885 } 1886 1887 queue_work(system_long_wq, &mgr->work); 1888 1889 ret = 0; 1890 } else { 1891 /* disable MST on the device */ 1892 mstb = mgr->mst_primary; 1893 mgr->mst_primary = NULL; 1894 /* this can fail if the device is gone */ 1895 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0); 1896 ret = 0; 1897 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload)); 1898 mgr->payload_mask = 0; 1899 set_bit(0, &mgr->payload_mask); 1900 mgr->vcpi_mask = 0; 1901 } 1902 1903out_unlock: 1904 mutex_unlock(&mgr->lock); 1905 if (mstb) 1906 drm_dp_put_mst_branch_device(mstb); 1907 return ret; 1908 1909} 1910EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst); 1911 1912/** 1913 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager 1914 * @mgr: manager to suspend 1915 * 1916 * This function tells the MST device that we can't handle UP messages 1917 * anymore. This should stop it from sending any since we are suspended. 1918 */ 1919void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr) 1920{ 1921 mutex_lock(&mgr->lock); 1922 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 1923 DP_MST_EN | DP_UPSTREAM_IS_SRC); 1924 mutex_unlock(&mgr->lock); 1925} 1926EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend); 1927 1928/** 1929 * drm_dp_mst_topology_mgr_resume() - resume the MST manager 1930 * @mgr: manager to resume 1931 * 1932 * This will fetch DPCD and see if the device is still there, 1933 * if it is, it will rewrite the MSTM control bits, and return. 1934 * 1935 * if the device fails this returns -1, and the driver should do 1936 * a full MST reprobe, in case we were undocked. 1937 */ 1938int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr) 1939{ 1940 int ret = 0; 1941 1942 mutex_lock(&mgr->lock); 1943 1944 if (mgr->mst_primary) { 1945 int sret; 1946 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE); 1947 if (sret != DP_RECEIVER_CAP_SIZE) { 1948 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n"); 1949 ret = -1; 1950 goto out_unlock; 1951 } 1952 1953 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 1954 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC); 1955 if (ret < 0) { 1956 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n"); 1957 ret = -1; 1958 goto out_unlock; 1959 } 1960 ret = 0; 1961 } else 1962 ret = -1; 1963 1964out_unlock: 1965 mutex_unlock(&mgr->lock); 1966 return ret; 1967} 1968EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume); 1969 1970static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up) 1971{ 1972 int len; 1973 u8 replyblock[32]; 1974 int replylen, origlen, curreply; 1975 int ret; 1976 struct drm_dp_sideband_msg_rx *msg; 1977 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE; 1978 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv; 1979 1980 len = min(mgr->max_dpcd_transaction_bytes, 16); 1981 ret = drm_dp_dpcd_read(mgr->aux, basereg, 1982 replyblock, len); 1983 if (ret != len) { 1984 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret); 1985 return; 1986 } 1987 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true); 1988 if (!ret) { 1989 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]); 1990 return; 1991 } 1992 replylen = msg->curchunk_len + msg->curchunk_hdrlen; 1993 1994 origlen = replylen; 1995 replylen -= len; 1996 curreply = len; 1997 while (replylen > 0) { 1998 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16); 1999 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply, 2000 replyblock, len); 2001 if (ret != len) { 2002 DRM_DEBUG_KMS("failed to read a chunk\n"); 2003 } 2004 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false); 2005 if (ret == false) 2006 DRM_DEBUG_KMS("failed to build sideband msg\n"); 2007 curreply += len; 2008 replylen -= len; 2009 } 2010} 2011 2012static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr) 2013{ 2014 int ret = 0; 2015 2016 drm_dp_get_one_sb_msg(mgr, false); 2017 2018 if (mgr->down_rep_recv.have_eomt) { 2019 struct drm_dp_sideband_msg_tx *txmsg; 2020 struct drm_dp_mst_branch *mstb; 2021 int slot = -1; 2022 mstb = drm_dp_get_mst_branch_device(mgr, 2023 mgr->down_rep_recv.initial_hdr.lct, 2024 mgr->down_rep_recv.initial_hdr.rad); 2025 2026 if (!mstb) { 2027 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct); 2028 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx)); 2029 return 0; 2030 } 2031 2032 /* find the message */ 2033 slot = mgr->down_rep_recv.initial_hdr.seqno; 2034 mutex_lock(&mgr->qlock); 2035 txmsg = mstb->tx_slots[slot]; 2036 /* remove from slots */ 2037 mutex_unlock(&mgr->qlock); 2038 2039 if (!txmsg) { 2040 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n", 2041 mstb, 2042 mgr->down_rep_recv.initial_hdr.seqno, 2043 mgr->down_rep_recv.initial_hdr.lct, 2044 mgr->down_rep_recv.initial_hdr.rad[0], 2045 mgr->down_rep_recv.msg[0]); 2046 drm_dp_put_mst_branch_device(mstb); 2047 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx)); 2048 return 0; 2049 } 2050 2051 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply); 2052 if (txmsg->reply.reply_type == 1) { 2053 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x, reason 0x%02x, nak data 0x%02x\n", txmsg->reply.req_type, txmsg->reply.u.nak.reason, txmsg->reply.u.nak.nak_data); 2054 } 2055 2056 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx)); 2057 drm_dp_put_mst_branch_device(mstb); 2058 2059 mutex_lock(&mgr->qlock); 2060 txmsg->state = DRM_DP_SIDEBAND_TX_RX; 2061 mstb->tx_slots[slot] = NULL; 2062 mutex_unlock(&mgr->qlock); 2063 2064 wake_up(&mgr->tx_waitq); 2065 } 2066 return ret; 2067} 2068 2069static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr) 2070{ 2071 int ret = 0; 2072 drm_dp_get_one_sb_msg(mgr, true); 2073 2074 if (mgr->up_req_recv.have_eomt) { 2075 struct drm_dp_sideband_msg_req_body msg; 2076 struct drm_dp_mst_branch *mstb; 2077 bool seqno; 2078 mstb = drm_dp_get_mst_branch_device(mgr, 2079 mgr->up_req_recv.initial_hdr.lct, 2080 mgr->up_req_recv.initial_hdr.rad); 2081 if (!mstb) { 2082 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct); 2083 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx)); 2084 return 0; 2085 } 2086 2087 seqno = mgr->up_req_recv.initial_hdr.seqno; 2088 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg); 2089 2090 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) { 2091 drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false); 2092 drm_dp_update_port(mstb, &msg.u.conn_stat); 2093 DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n", msg.u.conn_stat.port_number, msg.u.conn_stat.legacy_device_plug_status, msg.u.conn_stat.displayport_device_plug_status, msg.u.conn_stat.message_capability_status, msg.u.conn_stat.input_port, msg.u.conn_stat.peer_device_type); 2094 (*mgr->cbs->hotplug)(mgr); 2095 2096 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) { 2097 drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false); 2098 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn); 2099 } 2100 2101 drm_dp_put_mst_branch_device(mstb); 2102 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx)); 2103 } 2104 return ret; 2105} 2106 2107/** 2108 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify 2109 * @mgr: manager to notify irq for. 2110 * @esi: 4 bytes from SINK_COUNT_ESI 2111 * @handled: whether the hpd interrupt was consumed or not 2112 * 2113 * This should be called from the driver when it detects a short IRQ, 2114 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The 2115 * topology manager will process the sideband messages received as a result 2116 * of this. 2117 */ 2118int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled) 2119{ 2120 int ret = 0; 2121 int sc; 2122 *handled = false; 2123 sc = esi[0] & 0x3f; 2124 2125 if (sc != mgr->sink_count) { 2126 mgr->sink_count = sc; 2127 *handled = true; 2128 } 2129 2130 if (esi[1] & DP_DOWN_REP_MSG_RDY) { 2131 ret = drm_dp_mst_handle_down_rep(mgr); 2132 *handled = true; 2133 } 2134 2135 if (esi[1] & DP_UP_REQ_MSG_RDY) { 2136 ret |= drm_dp_mst_handle_up_req(mgr); 2137 *handled = true; 2138 } 2139 2140 drm_dp_mst_kick_tx(mgr); 2141 return ret; 2142} 2143EXPORT_SYMBOL(drm_dp_mst_hpd_irq); 2144 2145/** 2146 * drm_dp_mst_detect_port() - get connection status for an MST port 2147 * @mgr: manager for this port 2148 * @port: unverified pointer to a port 2149 * 2150 * This returns the current connection state for a port. It validates the 2151 * port pointer still exists so the caller doesn't require a reference 2152 */ 2153enum drm_connector_status drm_dp_mst_detect_port(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) 2154{ 2155 enum drm_connector_status status = connector_status_disconnected; 2156 2157 /* we need to search for the port in the mgr in case its gone */ 2158 port = drm_dp_get_validated_port_ref(mgr, port); 2159 if (!port) 2160 return connector_status_disconnected; 2161 2162 if (!port->ddps) 2163 goto out; 2164 2165 switch (port->pdt) { 2166 case DP_PEER_DEVICE_NONE: 2167 case DP_PEER_DEVICE_MST_BRANCHING: 2168 break; 2169 2170 case DP_PEER_DEVICE_SST_SINK: 2171 status = connector_status_connected; 2172 break; 2173 case DP_PEER_DEVICE_DP_LEGACY_CONV: 2174 if (port->ldps) 2175 status = connector_status_connected; 2176 break; 2177 } 2178out: 2179 drm_dp_put_port(port); 2180 return status; 2181} 2182EXPORT_SYMBOL(drm_dp_mst_detect_port); 2183 2184/** 2185 * drm_dp_mst_get_edid() - get EDID for an MST port 2186 * @connector: toplevel connector to get EDID for 2187 * @mgr: manager for this port 2188 * @port: unverified pointer to a port. 2189 * 2190 * This returns an EDID for the port connected to a connector, 2191 * It validates the pointer still exists so the caller doesn't require a 2192 * reference. 2193 */ 2194struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) 2195{ 2196 struct edid *edid = NULL; 2197 2198 /* we need to search for the port in the mgr in case its gone */ 2199 port = drm_dp_get_validated_port_ref(mgr, port); 2200 if (!port) 2201 return NULL; 2202 2203 edid = drm_get_edid(connector, &port->aux.ddc); 2204 drm_dp_put_port(port); 2205 return edid; 2206} 2207EXPORT_SYMBOL(drm_dp_mst_get_edid); 2208 2209/** 2210 * drm_dp_find_vcpi_slots() - find slots for this PBN value 2211 * @mgr: manager to use 2212 * @pbn: payload bandwidth to convert into slots. 2213 */ 2214int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, 2215 int pbn) 2216{ 2217 int num_slots; 2218 2219 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div); 2220 2221 if (num_slots > mgr->avail_slots) 2222 return -ENOSPC; 2223 return num_slots; 2224} 2225EXPORT_SYMBOL(drm_dp_find_vcpi_slots); 2226 2227static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr, 2228 struct drm_dp_vcpi *vcpi, int pbn) 2229{ 2230 int num_slots; 2231 int ret; 2232 2233 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div); 2234 2235 if (num_slots > mgr->avail_slots) 2236 return -ENOSPC; 2237 2238 vcpi->pbn = pbn; 2239 vcpi->aligned_pbn = num_slots * mgr->pbn_div; 2240 vcpi->num_slots = num_slots; 2241 2242 ret = drm_dp_mst_assign_payload_id(mgr, vcpi); 2243 if (ret < 0) 2244 return ret; 2245 return 0; 2246} 2247 2248/** 2249 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel 2250 * @mgr: manager for this port 2251 * @port: port to allocate a virtual channel for. 2252 * @pbn: payload bandwidth number to request 2253 * @slots: returned number of slots for this PBN. 2254 */ 2255bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots) 2256{ 2257 int ret; 2258 2259 port = drm_dp_get_validated_port_ref(mgr, port); 2260 if (!port) 2261 return false; 2262 2263 if (port->vcpi.vcpi > 0) { 2264 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn); 2265 if (pbn == port->vcpi.pbn) { 2266 *slots = port->vcpi.num_slots; 2267 return true; 2268 } 2269 } 2270 2271 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn); 2272 if (ret) { 2273 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret); 2274 goto out; 2275 } 2276 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots); 2277 *slots = port->vcpi.num_slots; 2278 2279 drm_dp_put_port(port); 2280 return true; 2281out: 2282 return false; 2283} 2284EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi); 2285 2286/** 2287 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI 2288 * @mgr: manager for this port 2289 * @port: unverified pointer to a port. 2290 * 2291 * This just resets the number of slots for the ports VCPI for later programming. 2292 */ 2293void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) 2294{ 2295 port = drm_dp_get_validated_port_ref(mgr, port); 2296 if (!port) 2297 return; 2298 port->vcpi.num_slots = 0; 2299 drm_dp_put_port(port); 2300} 2301EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots); 2302 2303/** 2304 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI 2305 * @mgr: manager for this port 2306 * @port: unverified port to deallocate vcpi for 2307 */ 2308void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port) 2309{ 2310 port = drm_dp_get_validated_port_ref(mgr, port); 2311 if (!port) 2312 return; 2313 2314 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi); 2315 port->vcpi.num_slots = 0; 2316 port->vcpi.pbn = 0; 2317 port->vcpi.aligned_pbn = 0; 2318 port->vcpi.vcpi = 0; 2319 drm_dp_put_port(port); 2320} 2321EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi); 2322 2323static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr, 2324 int id, struct drm_dp_payload *payload) 2325{ 2326 u8 payload_alloc[3], status; 2327 int ret; 2328 int retries = 0; 2329 2330 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, 2331 DP_PAYLOAD_TABLE_UPDATED); 2332 2333 payload_alloc[0] = id; 2334 payload_alloc[1] = payload->start_slot; 2335 payload_alloc[2] = payload->num_slots; 2336 2337 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3); 2338 if (ret != 3) { 2339 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret); 2340 goto fail; 2341 } 2342 2343retry: 2344 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status); 2345 if (ret < 0) { 2346 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret); 2347 goto fail; 2348 } 2349 2350 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) { 2351 retries++; 2352 if (retries < 20) { 2353 usleep_range(10000, 20000); 2354 goto retry; 2355 } 2356 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status); 2357 ret = -EINVAL; 2358 goto fail; 2359 } 2360 ret = 0; 2361fail: 2362 return ret; 2363} 2364 2365 2366/** 2367 * drm_dp_check_act_status() - Check ACT handled status. 2368 * @mgr: manager to use 2369 * 2370 * Check the payload status bits in the DPCD for ACT handled completion. 2371 */ 2372int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr) 2373{ 2374 u8 status; 2375 int ret; 2376 int count = 0; 2377 2378 do { 2379 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status); 2380 2381 if (ret < 0) { 2382 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret); 2383 goto fail; 2384 } 2385 2386 if (status & DP_PAYLOAD_ACT_HANDLED) 2387 break; 2388 count++; 2389 udelay(100); 2390 2391 } while (count < 30); 2392 2393 if (!(status & DP_PAYLOAD_ACT_HANDLED)) { 2394 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count); 2395 ret = -EINVAL; 2396 goto fail; 2397 } 2398 return 0; 2399fail: 2400 return ret; 2401} 2402EXPORT_SYMBOL(drm_dp_check_act_status); 2403 2404/** 2405 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode. 2406 * @clock: dot clock for the mode 2407 * @bpp: bpp for the mode. 2408 * 2409 * This uses the formula in the spec to calculate the PBN value for a mode. 2410 */ 2411int drm_dp_calc_pbn_mode(int clock, int bpp) 2412{ 2413 fixed20_12 pix_bw; 2414 fixed20_12 fbpp; 2415 fixed20_12 result; 2416 fixed20_12 margin, tmp; 2417 u32 res; 2418 2419 pix_bw.full = dfixed_const(clock); 2420 fbpp.full = dfixed_const(bpp); 2421 tmp.full = dfixed_const(8); 2422 fbpp.full = dfixed_div(fbpp, tmp); 2423 2424 result.full = dfixed_mul(pix_bw, fbpp); 2425 margin.full = dfixed_const(54); 2426 tmp.full = dfixed_const(64); 2427 margin.full = dfixed_div(margin, tmp); 2428 result.full = dfixed_div(result, margin); 2429 2430 margin.full = dfixed_const(1006); 2431 tmp.full = dfixed_const(1000); 2432 margin.full = dfixed_div(margin, tmp); 2433 result.full = dfixed_mul(result, margin); 2434 2435 result.full = dfixed_div(result, tmp); 2436 result.full = dfixed_ceil(result); 2437 res = dfixed_trunc(result); 2438 return res; 2439} 2440EXPORT_SYMBOL(drm_dp_calc_pbn_mode); 2441 2442static int test_calc_pbn_mode(void) 2443{ 2444 int ret; 2445 ret = drm_dp_calc_pbn_mode(154000, 30); 2446 if (ret != 689) 2447 return -EINVAL; 2448 ret = drm_dp_calc_pbn_mode(234000, 30); 2449 if (ret != 1047) 2450 return -EINVAL; 2451 return 0; 2452} 2453 2454/* we want to kick the TX after we've ack the up/down IRQs. */ 2455static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr) 2456{ 2457 queue_work(system_long_wq, &mgr->tx_work); 2458} 2459 2460static void drm_dp_mst_dump_mstb(struct seq_file *m, 2461 struct drm_dp_mst_branch *mstb) 2462{ 2463 struct drm_dp_mst_port *port; 2464 int tabs = mstb->lct; 2465 char prefix[10]; 2466 int i; 2467 2468 for (i = 0; i < tabs; i++) 2469 prefix[i] = '\t'; 2470 prefix[i] = '\0'; 2471 2472 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports); 2473 list_for_each_entry(port, &mstb->ports, next) { 2474 seq_printf(m, "%sport: %d: ddps: %d ldps: %d, %p, conn: %p\n", prefix, port->port_num, port->ddps, port->ldps, port, port->connector); 2475 if (port->mstb) 2476 drm_dp_mst_dump_mstb(m, port->mstb); 2477 } 2478} 2479 2480static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr, 2481 char *buf) 2482{ 2483 int ret; 2484 int i; 2485 for (i = 0; i < 4; i++) { 2486 ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16); 2487 if (ret != 16) 2488 break; 2489 } 2490 if (i == 4) 2491 return true; 2492 return false; 2493} 2494 2495/** 2496 * drm_dp_mst_dump_topology(): dump topology to seq file. 2497 * @m: seq_file to dump output to 2498 * @mgr: manager to dump current topology for. 2499 * 2500 * helper to dump MST topology to a seq file for debugfs. 2501 */ 2502void drm_dp_mst_dump_topology(struct seq_file *m, 2503 struct drm_dp_mst_topology_mgr *mgr) 2504{ 2505 int i; 2506 struct drm_dp_mst_port *port; 2507 mutex_lock(&mgr->lock); 2508 if (mgr->mst_primary) 2509 drm_dp_mst_dump_mstb(m, mgr->mst_primary); 2510 2511 /* dump VCPIs */ 2512 mutex_unlock(&mgr->lock); 2513 2514 mutex_lock(&mgr->payload_lock); 2515 seq_printf(m, "vcpi: %lx %lx\n", mgr->payload_mask, mgr->vcpi_mask); 2516 2517 for (i = 0; i < mgr->max_payloads; i++) { 2518 if (mgr->proposed_vcpis[i]) { 2519 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi); 2520 seq_printf(m, "vcpi %d: %d %d %d\n", i, port->port_num, port->vcpi.vcpi, port->vcpi.num_slots); 2521 } else 2522 seq_printf(m, "vcpi %d:unsed\n", i); 2523 } 2524 for (i = 0; i < mgr->max_payloads; i++) { 2525 seq_printf(m, "payload %d: %d, %d, %d\n", 2526 i, 2527 mgr->payloads[i].payload_state, 2528 mgr->payloads[i].start_slot, 2529 mgr->payloads[i].num_slots); 2530 2531 2532 } 2533 mutex_unlock(&mgr->payload_lock); 2534 2535 mutex_lock(&mgr->lock); 2536 if (mgr->mst_primary) { 2537 u8 buf[64]; 2538 bool bret; 2539 int ret; 2540 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE); 2541 seq_printf(m, "dpcd: "); 2542 for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++) 2543 seq_printf(m, "%02x ", buf[i]); 2544 seq_printf(m, "\n"); 2545 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2); 2546 seq_printf(m, "faux/mst: "); 2547 for (i = 0; i < 2; i++) 2548 seq_printf(m, "%02x ", buf[i]); 2549 seq_printf(m, "\n"); 2550 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1); 2551 seq_printf(m, "mst ctrl: "); 2552 for (i = 0; i < 1; i++) 2553 seq_printf(m, "%02x ", buf[i]); 2554 seq_printf(m, "\n"); 2555 2556 bret = dump_dp_payload_table(mgr, buf); 2557 if (bret == true) { 2558 seq_printf(m, "payload table: "); 2559 for (i = 0; i < 63; i++) 2560 seq_printf(m, "%02x ", buf[i]); 2561 seq_printf(m, "\n"); 2562 } 2563 2564 } 2565 2566 mutex_unlock(&mgr->lock); 2567 2568} 2569EXPORT_SYMBOL(drm_dp_mst_dump_topology); 2570 2571static void drm_dp_tx_work(struct work_struct *work) 2572{ 2573 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work); 2574 2575 mutex_lock(&mgr->qlock); 2576 if (mgr->tx_down_in_progress) 2577 process_single_down_tx_qlock(mgr); 2578 mutex_unlock(&mgr->qlock); 2579} 2580 2581/** 2582 * drm_dp_mst_topology_mgr_init - initialise a topology manager 2583 * @mgr: manager struct to initialise 2584 * @dev: device providing this structure - for i2c addition. 2585 * @aux: DP helper aux channel to talk to this device 2586 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit 2587 * @max_payloads: maximum number of payloads this GPU can source 2588 * @conn_base_id: the connector object ID the MST device is connected to. 2589 * 2590 * Return 0 for success, or negative error code on failure 2591 */ 2592int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr, 2593 struct device *dev, struct drm_dp_aux *aux, 2594 int max_dpcd_transaction_bytes, 2595 int max_payloads, int conn_base_id) 2596{ 2597 mutex_init(&mgr->lock); 2598 mutex_init(&mgr->qlock); 2599 mutex_init(&mgr->payload_lock); 2600 INIT_LIST_HEAD(&mgr->tx_msg_upq); 2601 INIT_LIST_HEAD(&mgr->tx_msg_downq); 2602 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work); 2603 INIT_WORK(&mgr->tx_work, drm_dp_tx_work); 2604 init_waitqueue_head(&mgr->tx_waitq); 2605 mgr->dev = dev; 2606 mgr->aux = aux; 2607 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes; 2608 mgr->max_payloads = max_payloads; 2609 mgr->conn_base_id = conn_base_id; 2610 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL); 2611 if (!mgr->payloads) 2612 return -ENOMEM; 2613 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL); 2614 if (!mgr->proposed_vcpis) 2615 return -ENOMEM; 2616 set_bit(0, &mgr->payload_mask); 2617 test_calc_pbn_mode(); 2618 return 0; 2619} 2620EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init); 2621 2622/** 2623 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager. 2624 * @mgr: manager to destroy 2625 */ 2626void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr) 2627{ 2628 mutex_lock(&mgr->payload_lock); 2629 kfree(mgr->payloads); 2630 mgr->payloads = NULL; 2631 kfree(mgr->proposed_vcpis); 2632 mgr->proposed_vcpis = NULL; 2633 mutex_unlock(&mgr->payload_lock); 2634 mgr->dev = NULL; 2635 mgr->aux = NULL; 2636} 2637EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy); 2638 2639/* I2C device */ 2640static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, 2641 int num) 2642{ 2643 struct drm_dp_aux *aux = adapter->algo_data; 2644 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux); 2645 struct drm_dp_mst_branch *mstb; 2646 struct drm_dp_mst_topology_mgr *mgr = port->mgr; 2647 unsigned int i; 2648 bool reading = false; 2649 struct drm_dp_sideband_msg_req_body msg; 2650 struct drm_dp_sideband_msg_tx *txmsg = NULL; 2651 int ret; 2652 2653 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent); 2654 if (!mstb) 2655 return -EREMOTEIO; 2656 2657 /* construct i2c msg */ 2658 /* see if last msg is a read */ 2659 if (msgs[num - 1].flags & I2C_M_RD) 2660 reading = true; 2661 2662 if (!reading) { 2663 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n"); 2664 ret = -EIO; 2665 goto out; 2666 } 2667 2668 msg.req_type = DP_REMOTE_I2C_READ; 2669 msg.u.i2c_read.num_transactions = num - 1; 2670 msg.u.i2c_read.port_number = port->port_num; 2671 for (i = 0; i < num - 1; i++) { 2672 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr; 2673 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len; 2674 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf; 2675 } 2676 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr; 2677 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len; 2678 2679 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL); 2680 if (!txmsg) { 2681 ret = -ENOMEM; 2682 goto out; 2683 } 2684 2685 txmsg->dst = mstb; 2686 drm_dp_encode_sideband_req(&msg, txmsg); 2687 2688 drm_dp_queue_down_tx(mgr, txmsg); 2689 2690 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg); 2691 if (ret > 0) { 2692 2693 if (txmsg->reply.reply_type == 1) { /* got a NAK back */ 2694 ret = -EREMOTEIO; 2695 goto out; 2696 } 2697 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) { 2698 ret = -EIO; 2699 goto out; 2700 } 2701 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len); 2702 ret = num; 2703 } 2704out: 2705 kfree(txmsg); 2706 drm_dp_put_mst_branch_device(mstb); 2707 return ret; 2708} 2709 2710static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter) 2711{ 2712 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | 2713 I2C_FUNC_SMBUS_READ_BLOCK_DATA | 2714 I2C_FUNC_SMBUS_BLOCK_PROC_CALL | 2715 I2C_FUNC_10BIT_ADDR; 2716} 2717 2718static const struct i2c_algorithm drm_dp_mst_i2c_algo = { 2719 .functionality = drm_dp_mst_i2c_functionality, 2720 .master_xfer = drm_dp_mst_i2c_xfer, 2721}; 2722 2723/** 2724 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX 2725 * @aux: DisplayPort AUX channel 2726 * 2727 * Returns 0 on success or a negative error code on failure. 2728 */ 2729static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux) 2730{ 2731 aux->ddc.algo = &drm_dp_mst_i2c_algo; 2732 aux->ddc.algo_data = aux; 2733 aux->ddc.retries = 3; 2734 2735 aux->ddc.class = I2C_CLASS_DDC; 2736 aux->ddc.owner = THIS_MODULE; 2737 aux->ddc.dev.parent = aux->dev; 2738 aux->ddc.dev.of_node = aux->dev->of_node; 2739 2740 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev), 2741 sizeof(aux->ddc.name)); 2742 2743 return i2c_add_adapter(&aux->ddc); 2744} 2745 2746/** 2747 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter 2748 * @aux: DisplayPort AUX channel 2749 */ 2750static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux) 2751{ 2752 i2c_del_adapter(&aux->ddc); 2753} 2754