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