* OF THIS SOFTWARE.
  */
 
+#include <linux/bitfield.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
 #include <linux/i2c.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
+#include <linux/random.h>
 #include <linux/sched.h>
 #include <linux/seq_file.h>
 #include <linux/iopoll.h>
                memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
                idx += req->u.i2c_write.num_bytes;
                break;
+       case DP_QUERY_STREAM_ENC_STATUS: {
+               const struct drm_dp_query_stream_enc_status *msg;
+
+               msg = &req->u.enc_status;
+               buf[idx] = msg->stream_id;
+               idx++;
+               memcpy(&buf[idx], msg->client_id, sizeof(msg->client_id));
+               idx += sizeof(msg->client_id);
+               buf[idx] = 0;
+               buf[idx] |= FIELD_PREP(GENMASK(1, 0), msg->stream_event);
+               buf[idx] |= msg->valid_stream_event ? BIT(2) : 0;
+               buf[idx] |= FIELD_PREP(GENMASK(4, 3), msg->stream_behavior);
+               buf[idx] |= msg->valid_stream_behavior ? BIT(5) : 0;
+               idx++;
+               }
+               break;
        }
        raw->cur_len = idx;
 }
                                return -ENOMEM;
                }
                break;
+       case DP_QUERY_STREAM_ENC_STATUS:
+               req->u.enc_status.stream_id = buf[idx++];
+               for (i = 0; i < sizeof(req->u.enc_status.client_id); i++)
+                       req->u.enc_status.client_id[i] = buf[idx++];
+
+               req->u.enc_status.stream_event = FIELD_GET(GENMASK(1, 0),
+                                                          buf[idx]);
+               req->u.enc_status.valid_stream_event = FIELD_GET(BIT(2),
+                                                                buf[idx]);
+               req->u.enc_status.stream_behavior = FIELD_GET(GENMASK(4, 3),
+                                                             buf[idx]);
+               req->u.enc_status.valid_stream_behavior = FIELD_GET(BIT(5),
+                                                                   buf[idx]);
+               break;
        }
 
        return 0;
                  req->u.i2c_write.num_bytes, req->u.i2c_write.num_bytes,
                  req->u.i2c_write.bytes);
                break;
+       case DP_QUERY_STREAM_ENC_STATUS:
+               P("stream_id=%u client_id=%*ph stream_event=%x "
+                 "valid_event=%d stream_behavior=%x valid_behavior=%d",
+                 req->u.enc_status.stream_id,
+                 (int)ARRAY_SIZE(req->u.enc_status.client_id),
+                 req->u.enc_status.client_id, req->u.enc_status.stream_event,
+                 req->u.enc_status.valid_stream_event,
+                 req->u.enc_status.stream_behavior,
+                 req->u.enc_status.valid_stream_behavior);
+               break;
        default:
                P("???\n");
                break;
        return true;
 }
 
+static bool
+drm_dp_sideband_parse_query_stream_enc_status(
+                               struct drm_dp_sideband_msg_rx *raw,
+                               struct drm_dp_sideband_msg_reply_body *repmsg)
+{
+       struct drm_dp_query_stream_enc_status_ack_reply *reply;
+
+       reply = &repmsg->u.enc_status;
+
+       reply->stream_id = raw->msg[3];
+
+       reply->reply_signed = raw->msg[2] & BIT(0);
+
+       /*
+        * NOTE: It's my impression from reading the spec that the below parsing
+        * is correct. However I noticed while testing with an HDCP 1.4 display
+        * through an HDCP 2.2 hub that only bit 3 was set. In that case, I
+        * would expect both bits to be set. So keep the parsing following the
+        * spec, but beware reality might not match the spec (at least for some
+        * configurations).
+        */
+       reply->hdcp_1x_device_present = raw->msg[2] & BIT(4);
+       reply->hdcp_2x_device_present = raw->msg[2] & BIT(3);
+
+       reply->query_capable_device_present = raw->msg[2] & BIT(5);
+       reply->legacy_device_present = raw->msg[2] & BIT(6);
+       reply->unauthorizable_device_present = raw->msg[2] & BIT(7);
+
+       reply->auth_completed = !!(raw->msg[1] & BIT(3));
+       reply->encryption_enabled = !!(raw->msg[1] & BIT(4));
+       reply->repeater_present = !!(raw->msg[1] & BIT(5));
+       reply->state = (raw->msg[1] & GENMASK(7, 6)) >> 6;
+
+       return true;
+}
+
 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
                                        struct drm_dp_sideband_msg_reply_body *msg)
 {
                return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
        case DP_CLEAR_PAYLOAD_ID_TABLE:
                return true; /* since there's nothing to parse */
+       case DP_QUERY_STREAM_ENC_STATUS:
+               return drm_dp_sideband_parse_query_stream_enc_status(raw, msg);
        default:
                DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg->req_type,
                          drm_dp_mst_req_type_str(msg->req_type));
        msg->path_msg = true;
 }
 
+static int
+build_query_stream_enc_status(struct drm_dp_sideband_msg_tx *msg, u8 stream_id,
+                             u8 *q_id)
+{
+       struct drm_dp_sideband_msg_req_body req;
+
+       req.req_type = DP_QUERY_STREAM_ENC_STATUS;
+       req.u.enc_status.stream_id = stream_id;
+       memcpy(req.u.enc_status.client_id, q_id,
+              sizeof(req.u.enc_status.client_id));
+       req.u.enc_status.stream_event = 0;
+       req.u.enc_status.valid_stream_event = false;
+       req.u.enc_status.stream_behavior = 0;
+       req.u.enc_status.valid_stream_behavior = false;
+
+       drm_dp_encode_sideband_req(&req, msg);
+       return 0;
+}
+
 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
                                        struct drm_dp_vcpi *vcpi)
 {
 }
 EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
 
+int drm_dp_send_query_stream_enc_status(struct drm_dp_mst_topology_mgr *mgr,
+               struct drm_dp_mst_port *port,
+               struct drm_dp_query_stream_enc_status_ack_reply *status)
+{
+       struct drm_dp_sideband_msg_tx *txmsg;
+       u8 nonce[7];
+       int len, ret;
+
+       txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
+       if (!txmsg)
+               return -ENOMEM;
+
+       port = drm_dp_mst_topology_get_port_validated(mgr, port);
+       if (!port) {
+               ret = -EINVAL;
+               goto out_get_port;
+       }
+
+       get_random_bytes(nonce, sizeof(nonce));
+
+       /*
+        * "Source device targets the QUERY_STREAM_ENCRYPTION_STATUS message
+        *  transaction at the MST Branch device directly connected to the
+        *  Source"
+        */
+       txmsg->dst = mgr->mst_primary;
+
+       len = build_query_stream_enc_status(txmsg, port->vcpi.vcpi, nonce);
+
+       drm_dp_queue_down_tx(mgr, txmsg);
+
+       ret = drm_dp_mst_wait_tx_reply(mgr->mst_primary, txmsg);
+       if (ret < 0) {
+               goto out;
+       } else if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
+               drm_dbg_kms(mgr->dev, "query encryption status nak received\n");
+               ret = -ENXIO;
+               goto out;
+       }
+
+       ret = 0;
+       memcpy(status, &txmsg->reply.u.enc_status, sizeof(*status));
+
+out:
+       drm_dp_mst_topology_put_port(port);
+out_get_port:
+       kfree(txmsg);
+       return ret;
+}
+EXPORT_SYMBOL(drm_dp_send_query_stream_enc_status);
+
 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
                                       int id,
                                       struct drm_dp_payload *payload)
 
        u8 port_number;
 };
 
+struct drm_dp_query_stream_enc_status_ack_reply {
+       /* Bit[23:16]- Stream Id */
+       u8 stream_id;
+
+       /* Bit[15]- Signed */
+       bool reply_signed;
+
+       /* Bit[10:8]- Stream Output Sink Type */
+       bool unauthorizable_device_present;
+       bool legacy_device_present;
+       bool query_capable_device_present;
+
+       /* Bit[12:11]- Stream Output CP Type */
+       bool hdcp_1x_device_present;
+       bool hdcp_2x_device_present;
+
+       /* Bit[4]- Stream Authentication */
+       bool auth_completed;
+
+       /* Bit[3]- Stream Encryption */
+       bool encryption_enabled;
+
+       /* Bit[2]- Stream Repeater Function Present */
+       bool repeater_present;
+
+       /* Bit[1:0]- Stream State */
+       u8 state;
+};
 
 #define DRM_DP_MAX_SDP_STREAMS 16
 struct drm_dp_allocate_payload {
        u8 *bytes;
 };
 
+struct drm_dp_query_stream_enc_status {
+       u8 stream_id;
+       u8 client_id[7];        /* 56-bit nonce */
+       u8 stream_event;
+       bool valid_stream_event;
+       u8 stream_behavior;
+       u8 valid_stream_behavior;
+};
+
 /* this covers ENUM_RESOURCES, POWER_DOWN_PHY, POWER_UP_PHY */
 struct drm_dp_port_number_req {
        u8 port_number;
 
                struct drm_dp_remote_i2c_read i2c_read;
                struct drm_dp_remote_i2c_write i2c_write;
+
+               struct drm_dp_query_stream_enc_status enc_status;
        } u;
 };
 
                struct drm_dp_remote_i2c_read_ack_reply remote_i2c_read_ack;
                struct drm_dp_remote_i2c_read_nak_reply remote_i2c_read_nack;
                struct drm_dp_remote_i2c_write_ack_reply remote_i2c_write_ack;
+
+               struct drm_dp_query_stream_enc_status_ack_reply enc_status;
        } u;
 };
 
                                 struct drm_dp_mst_port *port);
 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
                                 struct drm_dp_mst_port *port, bool power_up);
+int drm_dp_send_query_stream_enc_status(struct drm_dp_mst_topology_mgr *mgr,
+               struct drm_dp_mst_port *port,
+               struct drm_dp_query_stream_enc_status_ack_reply *status);
 int __must_check drm_dp_mst_atomic_check(struct drm_atomic_state *state);
 
 void drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port);