123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969 |
- /*
- * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 and
- * only version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- */
- /*
- * Copyright © 2014 Red Hat
- *
- * Permission to use, copy, modify, distribute, and sell this software and its
- * documentation for any purpose is hereby granted without fee, provided that
- * the above copyright notice appear in all copies and that both that copyright
- * notice and this permission notice appear in supporting documentation, and
- * that the name of the copyright holders not be used in advertising or
- * publicity pertaining to distribution of the software without specific,
- * written prior permission. The copyright holders make no representations
- * about the suitability of this software for any purpose. It is provided "as
- * is" without express or implied warranty.
- *
- * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
- * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
- * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
- * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
- * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
- * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
- * OF THIS SOFTWARE.
- */
- #include <linux/types.h>
- #include <linux/completion.h>
- #include <linux/delay.h>
- #include <drm/drm_fixed.h>
- #include <drm/drm_edid.h>
- #include <drm/drm_dp_mst_helper.h>
- #include "dp_mst_sim_helper.h"
- #include "dp_debug.h"
- #define DP_MST_DEBUG(fmt, ...) DP_DEBUG(fmt, ##__VA_ARGS__)
- #define DP_MST_INFO(fmt, ...) DP_DEBUG(fmt, ##__VA_ARGS__)
- #define DDC_SEGMENT_ADDR 0x30
- struct dp_mst_sim_context {
- void *host_dev;
- void (*host_hpd_irq)(void *host_dev);
- void (*host_req)(void *host_dev, const u8 *in, int in_size,
- u8 *out, int *out_size);
- struct dp_mst_sim_port *ports;
- u32 port_num;
- struct drm_dp_sideband_msg_rx down_req;
- struct drm_dp_sideband_msg_rx down_rep;
- struct mutex session_lock;
- struct completion session_comp;
- struct workqueue_struct *wq;
- u8 esi[16];
- u8 guid[16];
- u8 dpcd[1024];
- };
- struct dp_mst_sim_work {
- struct work_struct base;
- struct dp_mst_sim_context *ctx;
- unsigned int address;
- u8 buffer[256];
- size_t size;
- };
- #ifdef CONFIG_DYNAMIC_DEBUG
- static void dp_sideband_hex_dump(const char *name,
- u32 address, u8 *buffer, size_t size)
- {
- char prefix[64];
- int i, linelen, remaining = size;
- const int rowsize = 16;
- u8 linebuf[64];
- snprintf(prefix, sizeof(prefix), "%s(%d) %4xh(%2zu): ",
- name, current->pid, address, size);
- for (i = 0; i < size; i += rowsize) {
- linelen = min(remaining, rowsize);
- remaining -= rowsize;
- hex_dump_to_buffer(buffer + i, linelen, rowsize, 1,
- linebuf, sizeof(linebuf), false);
- DP_MST_DEBUG("%s%s\n", prefix, linebuf);
- }
- }
- #else
- static void dp_sideband_hex_dump(const char *name,
- u32 address, u8 *buffer, size_t size)
- {
- }
- #endif
- static u8 dp_mst_sim_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
- {
- u8 bitmask = 0x80;
- u8 bitshift = 7;
- u8 array_index = 0;
- int number_of_bits = num_nibbles * 4;
- u8 remainder = 0;
- while (number_of_bits != 0) {
- number_of_bits--;
- remainder <<= 1;
- remainder |= (data[array_index] & bitmask) >> bitshift;
- bitmask >>= 1;
- bitshift--;
- if (bitmask == 0) {
- bitmask = 0x80;
- bitshift = 7;
- array_index++;
- }
- if ((remainder & 0x10) == 0x10)
- remainder ^= 0x13;
- }
- number_of_bits = 4;
- while (number_of_bits != 0) {
- number_of_bits--;
- remainder <<= 1;
- if ((remainder & 0x10) != 0)
- remainder ^= 0x13;
- }
- return remainder;
- }
- static u8 dp_mst_sim_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
- {
- u8 bitmask = 0x80;
- u8 bitshift = 7;
- u8 array_index = 0;
- int number_of_bits = number_of_bytes * 8;
- u16 remainder = 0;
- while (number_of_bits != 0) {
- number_of_bits--;
- remainder <<= 1;
- remainder |= (data[array_index] & bitmask) >> bitshift;
- bitmask >>= 1;
- bitshift--;
- if (bitmask == 0) {
- bitmask = 0x80;
- bitshift = 7;
- array_index++;
- }
- if ((remainder & 0x100) == 0x100)
- remainder ^= 0xd5;
- }
- number_of_bits = 8;
- while (number_of_bits != 0) {
- number_of_bits--;
- remainder <<= 1;
- if ((remainder & 0x100) != 0)
- remainder ^= 0xd5;
- }
- return remainder & 0xff;
- }
- static bool dp_mst_sim_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
- u8 *buf, int buflen, u8 *hdrlen)
- {
- u8 crc4;
- u8 len;
- int i;
- u8 idx;
- if (buf[0] == 0)
- return false;
- len = 3;
- len += ((buf[0] & 0xf0) >> 4) / 2;
- if (len > buflen)
- return false;
- crc4 = dp_mst_sim_msg_header_crc4(buf, (len * 2) - 1);
- if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
- DP_MST_DEBUG("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
- return false;
- }
- hdr->lct = (buf[0] & 0xf0) >> 4;
- hdr->lcr = (buf[0] & 0xf);
- idx = 1;
- for (i = 0; i < (hdr->lct / 2); i++)
- hdr->rad[i] = buf[idx++];
- hdr->broadcast = (buf[idx] >> 7) & 0x1;
- hdr->path_msg = (buf[idx] >> 6) & 0x1;
- hdr->msg_len = buf[idx] & 0x3f;
- idx++;
- hdr->somt = (buf[idx] >> 7) & 0x1;
- hdr->eomt = (buf[idx] >> 6) & 0x1;
- hdr->seqno = (buf[idx] >> 4) & 0x1;
- idx++;
- *hdrlen = idx;
- return true;
- }
- static bool dp_mst_sim_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
- u8 *replybuf, u8 replybuflen, bool hdr)
- {
- int ret;
- u8 crc4;
- if (hdr) {
- u8 hdrlen;
- struct drm_dp_sideband_msg_hdr recv_hdr;
- ret = dp_mst_sim_decode_sideband_msg_hdr(&recv_hdr,
- replybuf, replybuflen, &hdrlen);
- if (ret == false)
- return false;
- /*
- * ignore out-of-order messages or messages that are part of a
- * failed transaction
- */
- if (!recv_hdr.somt && !msg->have_somt)
- return false;
- /* get length contained in this portion */
- msg->curchunk_len = recv_hdr.msg_len;
- msg->curchunk_hdrlen = hdrlen;
- /* we have already gotten an somt - don't bother parsing */
- if (recv_hdr.somt && msg->have_somt)
- return false;
- if (recv_hdr.somt) {
- memcpy(&msg->initial_hdr, &recv_hdr,
- sizeof(struct drm_dp_sideband_msg_hdr));
- msg->have_somt = true;
- }
- if (recv_hdr.eomt)
- msg->have_eomt = true;
- /* copy the bytes for the remainder of this header chunk */
- msg->curchunk_idx = min(msg->curchunk_len,
- (u8)(replybuflen - hdrlen));
- memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
- } else {
- memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
- msg->curchunk_idx += replybuflen;
- }
- if (msg->curchunk_idx >= msg->curchunk_len) {
- /* do CRC */
- crc4 = dp_mst_sim_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
- /* copy chunk into bigger msg */
- memcpy(&msg->msg[msg->curlen], msg->chunk,
- msg->curchunk_len - 1);
- msg->curlen += msg->curchunk_len - 1;
- }
- return true;
- }
- static void dp_mst_sim_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
- u8 *buf, int *len)
- {
- int idx = 0;
- int i;
- u8 crc4;
- buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
- for (i = 0; i < (hdr->lct / 2); i++)
- buf[idx++] = hdr->rad[i];
- buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
- (hdr->msg_len & 0x3f);
- buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
- crc4 = dp_mst_sim_msg_header_crc4(buf, (idx * 2) - 1);
- buf[idx - 1] |= (crc4 & 0xf);
- *len = idx;
- }
- static bool dp_get_one_sb_msg(struct drm_dp_sideband_msg_rx *msg,
- struct drm_dp_aux_msg *aux_msg)
- {
- int ret;
- if (!msg->have_somt) {
- ret = dp_mst_sim_sideband_msg_build(msg,
- aux_msg->buffer, aux_msg->size, true);
- if (!ret) {
- DP_ERR("sideband hdr build failed\n");
- return false;
- }
- } else {
- ret = dp_mst_sim_sideband_msg_build(msg,
- aux_msg->buffer, aux_msg->size, false);
- if (!ret) {
- DP_ERR("sideband msg build failed\n");
- return false;
- }
- }
- return true;
- }
- static int dp_sideband_build_nak_rep(
- struct dp_mst_sim_context *ctx)
- {
- struct drm_dp_sideband_msg_rx *msg = &ctx->down_req;
- u8 *buf = ctx->down_rep.msg;
- int idx = 0;
- buf[idx] = msg->msg[0] | 0x80;
- idx++;
- memcpy(&buf[idx], ctx->guid, 16);
- idx += 16;
- buf[idx] = 0x4;
- idx++;
- buf[idx] = 0;
- idx++;
- return idx;
- }
- static int dp_sideband_build_link_address_rep(
- struct dp_mst_sim_context *ctx)
- {
- struct dp_mst_sim_port *port;
- u8 *buf = ctx->down_rep.msg;
- int idx = 0;
- u32 i, tmp;
- buf[idx] = DP_LINK_ADDRESS;
- idx++;
- memcpy(&buf[idx], ctx->guid, 16);
- idx += 16;
- buf[idx] = ctx->port_num;
- idx++;
- for (i = 0; i < ctx->port_num; i++) {
- port = &ctx->ports[i];
- tmp = 0;
- if (port->input)
- tmp |= 0x80;
- tmp |= port->pdt << 4;
- tmp |= i & 0xF;
- buf[idx] = tmp;
- idx++;
- tmp = 0;
- if (port->mcs)
- tmp |= 0x80;
- if (port->ddps)
- tmp |= 0x40;
- if (port->input) {
- buf[idx] = tmp;
- idx++;
- continue;
- }
- if (port->ldps)
- tmp |= 0x20;
- buf[idx] = tmp;
- idx++;
- buf[idx] = port->dpcd_rev;
- idx++;
- memcpy(&buf[idx], port->peer_guid, 16);
- idx += 16;
- buf[idx] = (port->num_sdp_streams << 4) |
- (port->num_sdp_stream_sinks);
- idx++;
- }
- return idx;
- }
- static int dp_sideband_build_remote_i2c_read_rep(
- struct dp_mst_sim_context *ctx)
- {
- struct dp_mst_sim_port *port;
- struct drm_dp_remote_i2c_read i2c_read;
- u8 *buf;
- int idx;
- u32 i, start, len;
- buf = ctx->down_req.msg;
- idx = 1;
- i2c_read.num_transactions = buf[idx] & 0x3;
- i2c_read.port_number = buf[idx] >> 4;
- idx++;
- if (i2c_read.port_number >= ctx->port_num)
- goto err;
- for (i = 0; i < i2c_read.num_transactions; i++) {
- i2c_read.transactions[i].i2c_dev_id = buf[idx] & 0x7f;
- idx++;
- i2c_read.transactions[i].num_bytes = buf[idx];
- idx++;
- i2c_read.transactions[i].bytes = &buf[idx];
- idx += i2c_read.transactions[i].num_bytes;
- i2c_read.transactions[i].no_stop_bit = (buf[idx] >> 4) & 0x1;
- i2c_read.transactions[i].i2c_transaction_delay = buf[idx] & 0xf;
- idx++;
- }
- i2c_read.read_i2c_device_id = buf[idx];
- idx++;
- i2c_read.num_bytes_read = buf[idx];
- idx++;
- port = &ctx->ports[i2c_read.port_number];
- if (i2c_read.num_transactions == 1) {
- if (i2c_read.transactions[0].i2c_dev_id != DDC_ADDR ||
- i2c_read.transactions[0].num_bytes != 1) {
- DP_ERR("unsupported i2c address\n");
- goto err;
- }
- start = i2c_read.transactions[0].bytes[0];
- } else if (i2c_read.num_transactions == 2) {
- if (i2c_read.transactions[0].i2c_dev_id != DDC_SEGMENT_ADDR ||
- i2c_read.transactions[0].num_bytes != 1 ||
- i2c_read.transactions[1].i2c_dev_id != DDC_ADDR ||
- i2c_read.transactions[1].num_bytes != 1) {
- DP_ERR("unsupported i2c address\n");
- goto err;
- }
- start = i2c_read.transactions[0].bytes[0] * EDID_LENGTH * 2 +
- i2c_read.transactions[1].bytes[0];
- } else {
- DP_ERR("unsupported i2c transaction\n");
- goto err;
- }
- len = i2c_read.num_bytes_read;
- if (start + len > port->edid_size) {
- DP_ERR("edid data exceeds maximum\n");
- goto err;
- }
- buf = ctx->down_rep.msg;
- idx = 0;
- buf[idx] = DP_REMOTE_I2C_READ;
- idx++;
- buf[idx] = i2c_read.port_number;
- idx++;
- buf[idx] = len;
- idx++;
- memcpy(&buf[idx], &port->edid[start], len);
- idx += len;
- return idx;
- err:
- return dp_sideband_build_nak_rep(ctx);
- }
- static int dp_sideband_build_enum_path_resources_rep(
- struct dp_mst_sim_context *ctx)
- {
- struct dp_mst_sim_port *port;
- u8 port_num;
- u8 *buf;
- int idx;
- buf = ctx->down_req.msg;
- port_num = buf[1] >> 4;
- if (port_num >= ctx->port_num) {
- DP_ERR("invalid port num\n");
- goto err;
- }
- port = &ctx->ports[port_num];
- buf = ctx->down_rep.msg;
- idx = 0;
- buf[idx] = DP_ENUM_PATH_RESOURCES;
- idx++;
- buf[idx] = port_num << 4;
- idx++;
- buf[idx] = port->full_pbn >> 8;
- idx++;
- buf[idx] = port->full_pbn & 0xFF;
- idx++;
- buf[idx] = port->avail_pbn >> 8;
- idx++;
- buf[idx] = port->avail_pbn & 0xFF;
- idx++;
- return idx;
- err:
- return dp_sideband_build_nak_rep(ctx);
- }
- static int dp_sideband_build_allocate_payload_rep(
- struct dp_mst_sim_context *ctx)
- {
- struct drm_dp_allocate_payload allocate_payload;
- u8 *buf;
- int idx;
- u32 i;
- buf = ctx->down_req.msg;
- idx = 1;
- allocate_payload.port_number = buf[idx] >> 4;
- allocate_payload.number_sdp_streams = buf[idx] & 0xF;
- idx++;
- allocate_payload.vcpi = buf[idx];
- idx++;
- allocate_payload.pbn = (buf[idx] << 8) | buf[idx+1];
- idx += 2;
- for (i = 0; i < allocate_payload.number_sdp_streams / 2; i++) {
- allocate_payload.sdp_stream_sink[i * 2] = buf[idx] >> 4;
- allocate_payload.sdp_stream_sink[i * 2 + 1] = buf[idx] & 0xf;
- idx++;
- }
- if (allocate_payload.number_sdp_streams & 1) {
- i = allocate_payload.number_sdp_streams - 1;
- allocate_payload.sdp_stream_sink[i] = buf[idx] >> 4;
- idx++;
- }
- if (allocate_payload.port_number >= ctx->port_num) {
- DP_ERR("invalid port num\n");
- goto err;
- }
- buf = ctx->down_rep.msg;
- idx = 0;
- buf[idx] = DP_ALLOCATE_PAYLOAD;
- idx++;
- buf[idx] = allocate_payload.port_number;
- idx++;
- buf[idx] = allocate_payload.vcpi;
- idx++;
- buf[idx] = allocate_payload.pbn >> 8;
- idx++;
- buf[idx] = allocate_payload.pbn & 0xFF;
- idx++;
- return idx;
- err:
- return dp_sideband_build_nak_rep(ctx);
- }
- static int dp_sideband_build_power_updown_phy_rep(
- struct dp_mst_sim_context *ctx)
- {
- u8 port_num;
- u8 *buf;
- int idx;
- buf = ctx->down_req.msg;
- port_num = buf[1] >> 4;
- if (port_num >= ctx->port_num) {
- DP_ERR("invalid port num\n");
- goto err;
- }
- buf = ctx->down_rep.msg;
- idx = 0;
- buf[idx] = ctx->down_req.msg[0];
- idx++;
- buf[idx] = port_num;
- idx++;
- return idx;
- err:
- return dp_sideband_build_nak_rep(ctx);
- }
- static int dp_sideband_build_clear_payload_id_table_rep(
- struct dp_mst_sim_context *ctx)
- {
- u8 *buf = ctx->down_rep.msg;
- int idx = 0;
- buf[idx] = DP_CLEAR_PAYLOAD_ID_TABLE;
- idx++;
- return idx;
- }
- static inline int dp_sideband_update_esi(struct dp_mst_sim_context *ctx)
- {
- ctx->esi[0] = ctx->port_num;
- ctx->esi[1] = DP_DOWN_REP_MSG_RDY;
- ctx->esi[2] = 0;
- return 0;
- }
- static inline bool dp_sideband_pending_esi(
- struct dp_mst_sim_context *ctx)
- {
- return !!(ctx->esi[1] & DP_DOWN_REP_MSG_RDY);
- }
- static int dp_mst_sim_clear_esi(struct dp_mst_sim_context *ctx,
- struct drm_dp_aux_msg *msg)
- {
- size_t i;
- u8 old_esi = ctx->esi[1];
- u32 addr = msg->address - DP_SINK_COUNT_ESI;
- if (msg->size - addr >= 16) {
- msg->reply = DP_AUX_NATIVE_REPLY_NACK;
- return 0;
- }
- mutex_lock(&ctx->session_lock);
- for (i = 0; i < msg->size; i++)
- ctx->esi[addr + i] &= ~((u8 *)msg->buffer)[i];
- if ((old_esi & DP_DOWN_REP_MSG_RDY) &&
- !(ctx->esi[1] & DP_DOWN_REP_MSG_RDY)) {
- complete(&ctx->session_comp);
- }
- mutex_unlock(&ctx->session_lock);
- msg->reply = DP_AUX_NATIVE_REPLY_ACK;
- return 0;
- }
- static int dp_mst_sim_read_esi(struct dp_mst_sim_context *ctx,
- struct drm_dp_aux_msg *msg)
- {
- u32 addr = msg->address - DP_SINK_COUNT_ESI;
- if (msg->size - addr >= 16) {
- msg->reply = DP_AUX_NATIVE_REPLY_NACK;
- return 0;
- }
- memcpy(msg->buffer, &ctx->esi[addr], msg->size);
- msg->reply = DP_AUX_NATIVE_REPLY_ACK;
- return 0;
- }
- static int dp_mst_sim_down_req_internal(struct dp_mst_sim_context *ctx,
- struct drm_dp_aux_msg *aux_msg)
- {
- struct drm_dp_sideband_msg_rx *msg = &ctx->down_req;
- struct drm_dp_sideband_msg_hdr hdr;
- bool seqno;
- int ret, size, len, hdr_len;
- ret = dp_get_one_sb_msg(msg, aux_msg);
- if (!ret)
- return -EINVAL;
- if (!msg->have_eomt)
- return 0;
- seqno = msg->initial_hdr.seqno;
- switch (msg->msg[0]) {
- case DP_LINK_ADDRESS:
- size = dp_sideband_build_link_address_rep(ctx);
- break;
- case DP_REMOTE_I2C_READ:
- size = dp_sideband_build_remote_i2c_read_rep(ctx);
- break;
- case DP_ENUM_PATH_RESOURCES:
- size = dp_sideband_build_enum_path_resources_rep(ctx);
- break;
- case DP_ALLOCATE_PAYLOAD:
- size = dp_sideband_build_allocate_payload_rep(ctx);
- break;
- case DP_POWER_DOWN_PHY:
- case DP_POWER_UP_PHY:
- size = dp_sideband_build_power_updown_phy_rep(ctx);
- break;
- case DP_CLEAR_PAYLOAD_ID_TABLE:
- size = dp_sideband_build_clear_payload_id_table_rep(ctx);
- break;
- default:
- size = dp_sideband_build_nak_rep(ctx);
- break;
- }
- if (ctx->host_req)
- ctx->host_req(ctx->host_dev,
- ctx->down_req.msg, ctx->down_req.curlen,
- ctx->down_rep.msg, &size);
- memset(msg, 0, sizeof(*msg));
- msg = &ctx->down_rep;
- msg->curlen = 0;
- while (msg->curlen < size) {
- /* copy data */
- len = min(size - msg->curlen, 44);
- memcpy(&ctx->dpcd[3], &msg->msg[msg->curlen], len);
- msg->curlen += len;
- /* build header */
- memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
- hdr.broadcast = 0;
- hdr.path_msg = 0;
- hdr.lct = 1;
- hdr.lcr = 0;
- hdr.seqno = seqno;
- hdr.msg_len = len + 1;
- hdr.eomt = (msg->curlen == size);
- hdr.somt = (msg->curlen == len);
- dp_mst_sim_encode_sideband_msg_hdr(&hdr, ctx->dpcd, &hdr_len);
- /* build crc */
- ctx->dpcd[len + 3] = dp_mst_sim_msg_data_crc4(&ctx->dpcd[3], len);
- /* update esi */
- mutex_lock(&ctx->session_lock);
- dp_sideband_update_esi(ctx);
- mutex_unlock(&ctx->session_lock);
- /* notify host */
- ctx->host_hpd_irq(ctx->host_dev);
- /* wait until esi is cleared */
- mutex_lock(&ctx->session_lock);
- while (dp_sideband_pending_esi(ctx)) {
- mutex_unlock(&ctx->session_lock);
- wait_for_completion(&ctx->session_comp);
- mutex_lock(&ctx->session_lock);
- }
- mutex_unlock(&ctx->session_lock);
- }
- return 0;
- }
- static void dp_mst_sim_down_req_work(struct work_struct *work)
- {
- struct dp_mst_sim_work *sim_work =
- container_of(work, struct dp_mst_sim_work, base);
- struct drm_dp_aux_msg msg;
- msg.address = sim_work->address;
- msg.buffer = sim_work->buffer;
- msg.size = sim_work->size;
- dp_mst_sim_down_req_internal(sim_work->ctx, &msg);
- kfree(sim_work);
- }
- static int dp_mst_sim_down_req(struct dp_mst_sim_context *ctx,
- struct drm_dp_aux_msg *aux_msg)
- {
- struct dp_mst_sim_work *work;
- if (aux_msg->size >= 256) {
- aux_msg->reply = DP_AUX_NATIVE_REPLY_NACK;
- return 0;
- }
- dp_sideband_hex_dump("request",
- aux_msg->address, aux_msg->buffer, aux_msg->size);
- work = kzalloc(sizeof(*work), GFP_KERNEL);
- if (!work) {
- aux_msg->reply = DP_AUX_NATIVE_REPLY_NACK;
- return 0;
- }
- work->ctx = ctx;
- work->address = aux_msg->address;
- work->size = aux_msg->size;
- memcpy(work->buffer, aux_msg->buffer, aux_msg->size);
- INIT_WORK(&work->base, dp_mst_sim_down_req_work);
- queue_work(ctx->wq, &work->base);
- aux_msg->reply = DP_AUX_NATIVE_REPLY_ACK;
- return 0;
- }
- static int dp_mst_sim_down_rep(struct dp_mst_sim_context *ctx,
- struct drm_dp_aux_msg *msg)
- {
- u32 addr = msg->address - DP_SIDEBAND_MSG_DOWN_REP_BASE;
- memcpy(msg->buffer, &ctx->dpcd[addr], msg->size);
- msg->reply = DP_AUX_NATIVE_REPLY_ACK;
- dp_sideband_hex_dump("reply",
- addr, msg->buffer, msg->size);
- return 0;
- }
- int dp_mst_sim_transfer(void *mst_sim_context, struct drm_dp_aux_msg *msg)
- {
- if (msg->request == DP_AUX_NATIVE_WRITE) {
- if (msg->address >= DP_SIDEBAND_MSG_DOWN_REQ_BASE &&
- msg->address < DP_SIDEBAND_MSG_DOWN_REQ_BASE + 256)
- return dp_mst_sim_down_req(mst_sim_context, msg);
- if (msg->address >= DP_SINK_COUNT_ESI &&
- msg->address < DP_SINK_COUNT_ESI + 14)
- return dp_mst_sim_clear_esi(mst_sim_context, msg);
- } else if (msg->request == DP_AUX_NATIVE_READ) {
- if (msg->address >= DP_SIDEBAND_MSG_DOWN_REP_BASE &&
- msg->address < DP_SIDEBAND_MSG_DOWN_REP_BASE + 256)
- return dp_mst_sim_down_rep(mst_sim_context, msg);
- if (msg->address >= DP_SINK_COUNT_ESI &&
- msg->address < DP_SINK_COUNT_ESI + 14)
- return dp_mst_sim_read_esi(mst_sim_context, msg);
- }
- return -EINVAL;
- }
- int dp_mst_sim_update(void *mst_sim_context, u32 port_num,
- struct dp_mst_sim_port *ports)
- {
- struct dp_mst_sim_context *ctx = mst_sim_context;
- u8 *edid;
- int rc = 0;
- u32 i;
- if (port_num >= 15)
- return -EINVAL;
- mutex_lock(&ctx->session_lock);
- for (i = 0; i < ctx->port_num; i++)
- kfree(ctx->ports[i].edid);
- kfree(ctx->ports);
- ctx->port_num = 0;
- ctx->ports = kcalloc(port_num, sizeof(*ports), GFP_KERNEL);
- if (!ctx->ports) {
- rc = -ENOMEM;
- goto fail;
- }
- ctx->port_num = port_num;
- for (i = 0; i < port_num; i++) {
- ctx->ports[i] = ports[i];
- if (ports[i].edid_size) {
- if (!ports[i].edid) {
- rc = -EINVAL;
- goto fail;
- }
- edid = kzalloc(ports[i].edid_size,
- GFP_KERNEL);
- if (!edid) {
- rc = -ENOMEM;
- goto fail;
- }
- memcpy(edid, ports[i].edid, ports[i].edid_size);
- ctx->ports[i].edid = edid;
- }
- }
- fail:
- if (rc) {
- for (i = 0; i < ctx->port_num; i++)
- kfree(ctx->ports[i].edid);
- kfree(ctx->ports);
- }
- mutex_unlock(&ctx->session_lock);
- return rc;
- }
- int dp_mst_sim_create(const struct dp_mst_sim_cfg *cfg,
- void **mst_sim_context)
- {
- struct dp_mst_sim_context *ctx;
- ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
- if (!ctx)
- return -ENOMEM;
- ctx->host_dev = cfg->host_dev;
- ctx->host_hpd_irq = cfg->host_hpd_irq;
- ctx->host_req = cfg->host_req;
- memcpy(ctx->guid, cfg->guid, 16);
- mutex_init(&ctx->session_lock);
- init_completion(&ctx->session_comp);
- ctx->wq = create_singlethread_workqueue("dp_mst_sim");
- if (IS_ERR_OR_NULL(ctx->wq)) {
- DP_ERR("Error creating wq\n");
- kfree(ctx);
- return -EPERM;
- }
- *mst_sim_context = ctx;
- return 0;
- }
- int dp_mst_sim_destroy(void *mst_sim_context)
- {
- struct dp_mst_sim_context *ctx = mst_sim_context;
- u32 i;
- for (i = 0; i < ctx->port_num; i++)
- kfree(ctx->ports[i].edid);
- kfree(ctx->ports);
- destroy_workqueue(ctx->wq);
- return 0;
- }
|