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