Home | History | Annotate | Line # | Download | only in drm
      1  1.12  riastrad /*	$NetBSD: drm_dp_mst_topology.c,v 1.12 2022/08/22 18:30:50 riastradh Exp $	*/
      2   1.1  riastrad 
      3   1.1  riastrad /*
      4   1.1  riastrad  * Copyright  2014 Red Hat
      5   1.1  riastrad  *
      6   1.1  riastrad  * Permission to use, copy, modify, distribute, and sell this software and its
      7   1.1  riastrad  * documentation for any purpose is hereby granted without fee, provided that
      8   1.1  riastrad  * the above copyright notice appear in all copies and that both that copyright
      9   1.1  riastrad  * notice and this permission notice appear in supporting documentation, and
     10   1.1  riastrad  * that the name of the copyright holders not be used in advertising or
     11   1.1  riastrad  * publicity pertaining to distribution of the software without specific,
     12   1.1  riastrad  * written prior permission.  The copyright holders make no representations
     13   1.1  riastrad  * about the suitability of this software for any purpose.  It is provided "as
     14   1.1  riastrad  * is" without express or implied warranty.
     15   1.1  riastrad  *
     16   1.1  riastrad  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     17   1.1  riastrad  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
     18   1.1  riastrad  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
     19   1.1  riastrad  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
     20   1.1  riastrad  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
     21   1.1  riastrad  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
     22   1.1  riastrad  * OF THIS SOFTWARE.
     23   1.1  riastrad  */
     24   1.1  riastrad 
     25   1.1  riastrad #include <sys/cdefs.h>
     26  1.12  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_dp_mst_topology.c,v 1.12 2022/08/22 18:30:50 riastradh Exp $");
     27   1.1  riastrad 
     28   1.1  riastrad #include <linux/delay.h>
     29   1.6  riastrad #include <linux/errno.h>
     30   1.6  riastrad #include <linux/i2c.h>
     31   1.1  riastrad #include <linux/init.h>
     32   1.6  riastrad #include <linux/kernel.h>
     33   1.1  riastrad #include <linux/sched.h>
     34   1.1  riastrad #include <linux/seq_file.h>
     35   1.6  riastrad 
     36   1.6  riastrad #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
     37   1.6  riastrad #include <linux/stacktrace.h>
     38   1.6  riastrad #include <linux/sort.h>
     39   1.6  riastrad #include <linux/timekeeping.h>
     40   1.6  riastrad #include <linux/math64.h>
     41   1.6  riastrad #endif
     42   1.6  riastrad 
     43   1.6  riastrad #include <drm/drm_atomic.h>
     44   1.6  riastrad #include <drm/drm_atomic_helper.h>
     45   1.1  riastrad #include <drm/drm_dp_mst_helper.h>
     46   1.6  riastrad #include <drm/drm_drv.h>
     47   1.6  riastrad #include <drm/drm_print.h>
     48   1.6  riastrad #include <drm/drm_probe_helper.h>
     49   1.1  riastrad 
     50   1.6  riastrad #include "drm_crtc_helper_internal.h"
     51   1.6  riastrad #include "drm_dp_mst_topology_internal.h"
     52   1.1  riastrad 
     53   1.4  riastrad #include <linux/nbsd-namespace.h>
     54   1.4  riastrad 
     55   1.1  riastrad /**
     56   1.1  riastrad  * DOC: dp mst helper
     57   1.1  riastrad  *
     58   1.1  riastrad  * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
     59   1.1  riastrad  * protocol. The helpers contain a topology manager and bandwidth manager.
     60   1.1  riastrad  * The helpers encapsulate the sending and received of sideband msgs.
     61   1.1  riastrad  */
     62   1.6  riastrad struct drm_dp_pending_up_req {
     63   1.6  riastrad 	struct drm_dp_sideband_msg_hdr hdr;
     64   1.6  riastrad 	struct drm_dp_sideband_msg_req_body msg;
     65   1.6  riastrad 	struct list_head next;
     66   1.6  riastrad };
     67   1.6  riastrad 
     68   1.3  riastrad #if IS_ENABLED(CONFIG_DEBUG_FS)
     69   1.1  riastrad static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
     70   1.1  riastrad 				  char *buf);
     71   1.3  riastrad #endif
     72   1.1  riastrad 
     73   1.6  riastrad static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
     74   1.1  riastrad 
     75   1.1  riastrad static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
     76   1.1  riastrad 				     int id,
     77   1.1  riastrad 				     struct drm_dp_payload *payload);
     78   1.1  riastrad 
     79   1.6  riastrad static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
     80   1.6  riastrad 				 struct drm_dp_mst_port *port,
     81   1.6  riastrad 				 int offset, int size, u8 *bytes);
     82   1.1  riastrad static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
     83   1.1  riastrad 				  struct drm_dp_mst_port *port,
     84   1.1  riastrad 				  int offset, int size, u8 *bytes);
     85   1.1  riastrad 
     86   1.6  riastrad static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
     87   1.6  riastrad 				    struct drm_dp_mst_branch *mstb);
     88   1.6  riastrad 
     89   1.6  riastrad static void
     90   1.6  riastrad drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
     91   1.6  riastrad 				   struct drm_dp_mst_branch *mstb);
     92   1.6  riastrad 
     93   1.1  riastrad static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
     94   1.1  riastrad 					   struct drm_dp_mst_branch *mstb,
     95   1.1  riastrad 					   struct drm_dp_mst_port *port);
     96   1.1  riastrad static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
     97   1.1  riastrad 				 u8 *guid);
     98   1.1  riastrad 
     99   1.1  riastrad static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
    100   1.1  riastrad static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
    101   1.1  riastrad static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
    102   1.6  riastrad 
    103   1.6  riastrad #define DBG_PREFIX "[dp_mst]"
    104   1.6  riastrad 
    105   1.6  riastrad #define DP_STR(x) [DP_ ## x] = #x
    106   1.6  riastrad 
    107   1.6  riastrad static const char *drm_dp_mst_req_type_str(u8 req_type)
    108   1.6  riastrad {
    109   1.6  riastrad 	static const char * const req_type_str[] = {
    110   1.6  riastrad 		DP_STR(GET_MSG_TRANSACTION_VERSION),
    111   1.6  riastrad 		DP_STR(LINK_ADDRESS),
    112   1.6  riastrad 		DP_STR(CONNECTION_STATUS_NOTIFY),
    113   1.6  riastrad 		DP_STR(ENUM_PATH_RESOURCES),
    114   1.6  riastrad 		DP_STR(ALLOCATE_PAYLOAD),
    115   1.6  riastrad 		DP_STR(QUERY_PAYLOAD),
    116   1.6  riastrad 		DP_STR(RESOURCE_STATUS_NOTIFY),
    117   1.6  riastrad 		DP_STR(CLEAR_PAYLOAD_ID_TABLE),
    118   1.6  riastrad 		DP_STR(REMOTE_DPCD_READ),
    119   1.6  riastrad 		DP_STR(REMOTE_DPCD_WRITE),
    120   1.6  riastrad 		DP_STR(REMOTE_I2C_READ),
    121   1.6  riastrad 		DP_STR(REMOTE_I2C_WRITE),
    122   1.6  riastrad 		DP_STR(POWER_UP_PHY),
    123   1.6  riastrad 		DP_STR(POWER_DOWN_PHY),
    124   1.6  riastrad 		DP_STR(SINK_EVENT_NOTIFY),
    125   1.6  riastrad 		DP_STR(QUERY_STREAM_ENC_STATUS),
    126   1.6  riastrad 	};
    127   1.6  riastrad 
    128   1.6  riastrad 	if (req_type >= ARRAY_SIZE(req_type_str) ||
    129   1.6  riastrad 	    !req_type_str[req_type])
    130   1.6  riastrad 		return "unknown";
    131   1.6  riastrad 
    132   1.6  riastrad 	return req_type_str[req_type];
    133   1.6  riastrad }
    134   1.6  riastrad 
    135   1.6  riastrad #undef DP_STR
    136   1.6  riastrad #define DP_STR(x) [DP_NAK_ ## x] = #x
    137   1.6  riastrad 
    138   1.6  riastrad static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
    139   1.6  riastrad {
    140   1.6  riastrad 	static const char * const nak_reason_str[] = {
    141   1.6  riastrad 		DP_STR(WRITE_FAILURE),
    142   1.6  riastrad 		DP_STR(INVALID_READ),
    143   1.6  riastrad 		DP_STR(CRC_FAILURE),
    144   1.6  riastrad 		DP_STR(BAD_PARAM),
    145   1.6  riastrad 		DP_STR(DEFER),
    146   1.6  riastrad 		DP_STR(LINK_FAILURE),
    147   1.6  riastrad 		DP_STR(NO_RESOURCES),
    148   1.6  riastrad 		DP_STR(DPCD_FAIL),
    149   1.6  riastrad 		DP_STR(I2C_NAK),
    150   1.6  riastrad 		DP_STR(ALLOCATE_FAIL),
    151   1.6  riastrad 	};
    152   1.6  riastrad 
    153   1.6  riastrad 	if (nak_reason >= ARRAY_SIZE(nak_reason_str) ||
    154   1.6  riastrad 	    !nak_reason_str[nak_reason])
    155   1.6  riastrad 		return "unknown";
    156   1.6  riastrad 
    157   1.6  riastrad 	return nak_reason_str[nak_reason];
    158   1.6  riastrad }
    159   1.6  riastrad 
    160   1.6  riastrad #undef DP_STR
    161   1.6  riastrad #define DP_STR(x) [DRM_DP_SIDEBAND_TX_ ## x] = #x
    162   1.6  riastrad 
    163   1.6  riastrad static const char *drm_dp_mst_sideband_tx_state_str(int state)
    164   1.6  riastrad {
    165   1.6  riastrad 	static const char * const sideband_reason_str[] = {
    166   1.6  riastrad 		DP_STR(QUEUED),
    167   1.6  riastrad 		DP_STR(START_SEND),
    168   1.6  riastrad 		DP_STR(SENT),
    169   1.6  riastrad 		DP_STR(RX),
    170   1.6  riastrad 		DP_STR(TIMEOUT),
    171   1.6  riastrad 	};
    172   1.6  riastrad 
    173   1.6  riastrad 	if (state >= ARRAY_SIZE(sideband_reason_str) ||
    174   1.6  riastrad 	    !sideband_reason_str[state])
    175   1.6  riastrad 		return "unknown";
    176   1.6  riastrad 
    177   1.6  riastrad 	return sideband_reason_str[state];
    178   1.6  riastrad }
    179   1.6  riastrad 
    180   1.6  riastrad static int
    181   1.6  riastrad drm_dp_mst_rad_to_str(const u8 rad[8], u8 lct, char *out, size_t len)
    182   1.6  riastrad {
    183   1.6  riastrad 	int i;
    184   1.6  riastrad 	u8 unpacked_rad[16];
    185   1.6  riastrad 
    186   1.6  riastrad 	for (i = 0; i < lct; i++) {
    187   1.6  riastrad 		if (i % 2)
    188   1.6  riastrad 			unpacked_rad[i] = rad[i / 2] >> 4;
    189   1.6  riastrad 		else
    190   1.6  riastrad 			unpacked_rad[i] = rad[i / 2] & BIT_MASK(4);
    191   1.6  riastrad 	}
    192   1.6  riastrad 
    193   1.6  riastrad 	/* TODO: Eventually add something to printk so we can format the rad
    194   1.6  riastrad 	 * like this: 1.2.3
    195   1.6  riastrad 	 */
    196   1.6  riastrad 	return snprintf(out, len, "%*phC", lct, unpacked_rad);
    197   1.6  riastrad }
    198   1.6  riastrad 
    199   1.1  riastrad /* sideband msg handling */
    200   1.1  riastrad static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
    201   1.1  riastrad {
    202   1.1  riastrad 	u8 bitmask = 0x80;
    203   1.1  riastrad 	u8 bitshift = 7;
    204   1.1  riastrad 	u8 array_index = 0;
    205   1.1  riastrad 	int number_of_bits = num_nibbles * 4;
    206   1.1  riastrad 	u8 remainder = 0;
    207   1.1  riastrad 
    208   1.1  riastrad 	while (number_of_bits != 0) {
    209   1.1  riastrad 		number_of_bits--;
    210   1.1  riastrad 		remainder <<= 1;
    211   1.1  riastrad 		remainder |= (data[array_index] & bitmask) >> bitshift;
    212   1.1  riastrad 		bitmask >>= 1;
    213   1.1  riastrad 		bitshift--;
    214   1.1  riastrad 		if (bitmask == 0) {
    215   1.1  riastrad 			bitmask = 0x80;
    216   1.1  riastrad 			bitshift = 7;
    217   1.1  riastrad 			array_index++;
    218   1.1  riastrad 		}
    219   1.1  riastrad 		if ((remainder & 0x10) == 0x10)
    220   1.1  riastrad 			remainder ^= 0x13;
    221   1.1  riastrad 	}
    222   1.1  riastrad 
    223   1.1  riastrad 	number_of_bits = 4;
    224   1.1  riastrad 	while (number_of_bits != 0) {
    225   1.1  riastrad 		number_of_bits--;
    226   1.1  riastrad 		remainder <<= 1;
    227   1.1  riastrad 		if ((remainder & 0x10) != 0)
    228   1.1  riastrad 			remainder ^= 0x13;
    229   1.1  riastrad 	}
    230   1.1  riastrad 
    231   1.1  riastrad 	return remainder;
    232   1.1  riastrad }
    233   1.1  riastrad 
    234   1.1  riastrad static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
    235   1.1  riastrad {
    236   1.1  riastrad 	u8 bitmask = 0x80;
    237   1.1  riastrad 	u8 bitshift = 7;
    238   1.1  riastrad 	u8 array_index = 0;
    239   1.1  riastrad 	int number_of_bits = number_of_bytes * 8;
    240   1.1  riastrad 	u16 remainder = 0;
    241   1.1  riastrad 
    242   1.1  riastrad 	while (number_of_bits != 0) {
    243   1.1  riastrad 		number_of_bits--;
    244   1.1  riastrad 		remainder <<= 1;
    245   1.1  riastrad 		remainder |= (data[array_index] & bitmask) >> bitshift;
    246   1.1  riastrad 		bitmask >>= 1;
    247   1.1  riastrad 		bitshift--;
    248   1.1  riastrad 		if (bitmask == 0) {
    249   1.1  riastrad 			bitmask = 0x80;
    250   1.1  riastrad 			bitshift = 7;
    251   1.1  riastrad 			array_index++;
    252   1.1  riastrad 		}
    253   1.1  riastrad 		if ((remainder & 0x100) == 0x100)
    254   1.1  riastrad 			remainder ^= 0xd5;
    255   1.1  riastrad 	}
    256   1.1  riastrad 
    257   1.1  riastrad 	number_of_bits = 8;
    258   1.1  riastrad 	while (number_of_bits != 0) {
    259   1.1  riastrad 		number_of_bits--;
    260   1.1  riastrad 		remainder <<= 1;
    261   1.1  riastrad 		if ((remainder & 0x100) != 0)
    262   1.1  riastrad 			remainder ^= 0xd5;
    263   1.1  riastrad 	}
    264   1.1  riastrad 
    265   1.1  riastrad 	return remainder & 0xff;
    266   1.1  riastrad }
    267   1.1  riastrad static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
    268   1.1  riastrad {
    269   1.1  riastrad 	u8 size = 3;
    270   1.1  riastrad 	size += (hdr->lct / 2);
    271   1.1  riastrad 	return size;
    272   1.1  riastrad }
    273   1.1  riastrad 
    274   1.1  riastrad static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
    275   1.1  riastrad 					   u8 *buf, int *len)
    276   1.1  riastrad {
    277   1.1  riastrad 	int idx = 0;
    278   1.1  riastrad 	int i;
    279   1.1  riastrad 	u8 crc4;
    280   1.1  riastrad 	buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
    281   1.1  riastrad 	for (i = 0; i < (hdr->lct / 2); i++)
    282   1.1  riastrad 		buf[idx++] = hdr->rad[i];
    283   1.1  riastrad 	buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
    284   1.1  riastrad 		(hdr->msg_len & 0x3f);
    285   1.1  riastrad 	buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
    286   1.1  riastrad 
    287   1.1  riastrad 	crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
    288   1.1  riastrad 	buf[idx - 1] |= (crc4 & 0xf);
    289   1.1  riastrad 
    290   1.1  riastrad 	*len = idx;
    291   1.1  riastrad }
    292   1.1  riastrad 
    293   1.1  riastrad static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
    294   1.1  riastrad 					   u8 *buf, int buflen, u8 *hdrlen)
    295   1.1  riastrad {
    296   1.1  riastrad 	u8 crc4;
    297   1.1  riastrad 	u8 len;
    298   1.1  riastrad 	int i;
    299   1.1  riastrad 	u8 idx;
    300   1.1  riastrad 	if (buf[0] == 0)
    301   1.1  riastrad 		return false;
    302   1.1  riastrad 	len = 3;
    303   1.1  riastrad 	len += ((buf[0] & 0xf0) >> 4) / 2;
    304   1.1  riastrad 	if (len > buflen)
    305   1.1  riastrad 		return false;
    306   1.1  riastrad 	crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
    307   1.1  riastrad 
    308   1.1  riastrad 	if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
    309   1.1  riastrad 		DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
    310   1.1  riastrad 		return false;
    311   1.1  riastrad 	}
    312   1.1  riastrad 
    313   1.1  riastrad 	hdr->lct = (buf[0] & 0xf0) >> 4;
    314   1.1  riastrad 	hdr->lcr = (buf[0] & 0xf);
    315   1.1  riastrad 	idx = 1;
    316   1.1  riastrad 	for (i = 0; i < (hdr->lct / 2); i++)
    317   1.1  riastrad 		hdr->rad[i] = buf[idx++];
    318   1.1  riastrad 	hdr->broadcast = (buf[idx] >> 7) & 0x1;
    319   1.1  riastrad 	hdr->path_msg = (buf[idx] >> 6) & 0x1;
    320   1.1  riastrad 	hdr->msg_len = buf[idx] & 0x3f;
    321   1.1  riastrad 	idx++;
    322   1.1  riastrad 	hdr->somt = (buf[idx] >> 7) & 0x1;
    323   1.1  riastrad 	hdr->eomt = (buf[idx] >> 6) & 0x1;
    324   1.1  riastrad 	hdr->seqno = (buf[idx] >> 4) & 0x1;
    325   1.1  riastrad 	idx++;
    326   1.1  riastrad 	*hdrlen = idx;
    327   1.1  riastrad 	return true;
    328   1.1  riastrad }
    329   1.1  riastrad 
    330   1.8  riastrad static void
    331   1.6  riastrad drm_dp_encode_sideband_req(const struct drm_dp_sideband_msg_req_body *req,
    332   1.6  riastrad 			   struct drm_dp_sideband_msg_tx *raw)
    333   1.1  riastrad {
    334   1.1  riastrad 	int idx = 0;
    335   1.1  riastrad 	int i;
    336   1.1  riastrad 	u8 *buf = raw->msg;
    337   1.1  riastrad 	buf[idx++] = req->req_type & 0x7f;
    338   1.1  riastrad 
    339   1.1  riastrad 	switch (req->req_type) {
    340   1.1  riastrad 	case DP_ENUM_PATH_RESOURCES:
    341   1.6  riastrad 	case DP_POWER_DOWN_PHY:
    342   1.6  riastrad 	case DP_POWER_UP_PHY:
    343   1.1  riastrad 		buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
    344   1.1  riastrad 		idx++;
    345   1.1  riastrad 		break;
    346   1.1  riastrad 	case DP_ALLOCATE_PAYLOAD:
    347   1.1  riastrad 		buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
    348   1.1  riastrad 			(req->u.allocate_payload.number_sdp_streams & 0xf);
    349   1.1  riastrad 		idx++;
    350   1.1  riastrad 		buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
    351   1.1  riastrad 		idx++;
    352   1.1  riastrad 		buf[idx] = (req->u.allocate_payload.pbn >> 8);
    353   1.1  riastrad 		idx++;
    354   1.1  riastrad 		buf[idx] = (req->u.allocate_payload.pbn & 0xff);
    355   1.1  riastrad 		idx++;
    356   1.1  riastrad 		for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
    357   1.1  riastrad 			buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
    358   1.1  riastrad 				(req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
    359   1.1  riastrad 			idx++;
    360   1.1  riastrad 		}
    361   1.1  riastrad 		if (req->u.allocate_payload.number_sdp_streams & 1) {
    362   1.1  riastrad 			i = req->u.allocate_payload.number_sdp_streams - 1;
    363   1.1  riastrad 			buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
    364   1.1  riastrad 			idx++;
    365   1.1  riastrad 		}
    366   1.1  riastrad 		break;
    367   1.1  riastrad 	case DP_QUERY_PAYLOAD:
    368   1.1  riastrad 		buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
    369   1.1  riastrad 		idx++;
    370   1.1  riastrad 		buf[idx] = (req->u.query_payload.vcpi & 0x7f);
    371   1.1  riastrad 		idx++;
    372   1.1  riastrad 		break;
    373   1.1  riastrad 	case DP_REMOTE_DPCD_READ:
    374   1.1  riastrad 		buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
    375   1.1  riastrad 		buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
    376   1.1  riastrad 		idx++;
    377   1.1  riastrad 		buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
    378   1.1  riastrad 		idx++;
    379   1.1  riastrad 		buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
    380   1.1  riastrad 		idx++;
    381   1.1  riastrad 		buf[idx] = (req->u.dpcd_read.num_bytes);
    382   1.1  riastrad 		idx++;
    383   1.1  riastrad 		break;
    384   1.1  riastrad 
    385   1.1  riastrad 	case DP_REMOTE_DPCD_WRITE:
    386   1.1  riastrad 		buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
    387   1.1  riastrad 		buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
    388   1.1  riastrad 		idx++;
    389   1.1  riastrad 		buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
    390   1.1  riastrad 		idx++;
    391   1.1  riastrad 		buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
    392   1.1  riastrad 		idx++;
    393   1.1  riastrad 		buf[idx] = (req->u.dpcd_write.num_bytes);
    394   1.1  riastrad 		idx++;
    395   1.1  riastrad 		memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
    396   1.1  riastrad 		idx += req->u.dpcd_write.num_bytes;
    397   1.1  riastrad 		break;
    398   1.1  riastrad 	case DP_REMOTE_I2C_READ:
    399   1.1  riastrad 		buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
    400   1.1  riastrad 		buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
    401   1.1  riastrad 		idx++;
    402   1.1  riastrad 		for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
    403   1.1  riastrad 			buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
    404   1.1  riastrad 			idx++;
    405   1.1  riastrad 			buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
    406   1.1  riastrad 			idx++;
    407   1.1  riastrad 			memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
    408   1.1  riastrad 			idx += req->u.i2c_read.transactions[i].num_bytes;
    409   1.1  riastrad 
    410   1.6  riastrad 			buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 4;
    411   1.1  riastrad 			buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
    412   1.1  riastrad 			idx++;
    413   1.1  riastrad 		}
    414   1.1  riastrad 		buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
    415   1.1  riastrad 		idx++;
    416   1.1  riastrad 		buf[idx] = (req->u.i2c_read.num_bytes_read);
    417   1.1  riastrad 		idx++;
    418   1.1  riastrad 		break;
    419   1.1  riastrad 
    420   1.1  riastrad 	case DP_REMOTE_I2C_WRITE:
    421   1.1  riastrad 		buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
    422   1.1  riastrad 		idx++;
    423   1.1  riastrad 		buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
    424   1.1  riastrad 		idx++;
    425   1.1  riastrad 		buf[idx] = (req->u.i2c_write.num_bytes);
    426   1.1  riastrad 		idx++;
    427   1.1  riastrad 		memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
    428   1.1  riastrad 		idx += req->u.i2c_write.num_bytes;
    429   1.1  riastrad 		break;
    430   1.1  riastrad 	}
    431   1.1  riastrad 	raw->cur_len = idx;
    432   1.1  riastrad }
    433   1.6  riastrad EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_encode_sideband_req);
    434   1.6  riastrad 
    435   1.6  riastrad /* Decode a sideband request we've encoded, mainly used for debugging */
    436   1.8  riastrad static int
    437   1.6  riastrad drm_dp_decode_sideband_req(const struct drm_dp_sideband_msg_tx *raw,
    438   1.6  riastrad 			   struct drm_dp_sideband_msg_req_body *req)
    439   1.6  riastrad {
    440   1.6  riastrad 	const u8 *buf = raw->msg;
    441   1.6  riastrad 	int i, idx = 0;
    442   1.6  riastrad 
    443   1.6  riastrad 	req->req_type = buf[idx++] & 0x7f;
    444   1.6  riastrad 	switch (req->req_type) {
    445   1.6  riastrad 	case DP_ENUM_PATH_RESOURCES:
    446   1.6  riastrad 	case DP_POWER_DOWN_PHY:
    447   1.6  riastrad 	case DP_POWER_UP_PHY:
    448   1.6  riastrad 		req->u.port_num.port_number = (buf[idx] >> 4) & 0xf;
    449   1.6  riastrad 		break;
    450   1.6  riastrad 	case DP_ALLOCATE_PAYLOAD:
    451   1.6  riastrad 		{
    452   1.6  riastrad 			struct drm_dp_allocate_payload *a =
    453   1.6  riastrad 				&req->u.allocate_payload;
    454   1.6  riastrad 
    455   1.6  riastrad 			a->number_sdp_streams = buf[idx] & 0xf;
    456   1.6  riastrad 			a->port_number = (buf[idx] >> 4) & 0xf;
    457   1.6  riastrad 
    458   1.6  riastrad 			WARN_ON(buf[++idx] & 0x80);
    459   1.6  riastrad 			a->vcpi = buf[idx] & 0x7f;
    460   1.6  riastrad 
    461   1.6  riastrad 			a->pbn = buf[++idx] << 8;
    462   1.6  riastrad 			a->pbn |= buf[++idx];
    463   1.6  riastrad 
    464   1.6  riastrad 			idx++;
    465   1.6  riastrad 			for (i = 0; i < a->number_sdp_streams; i++) {
    466   1.6  riastrad 				a->sdp_stream_sink[i] =
    467   1.6  riastrad 					(buf[idx + (i / 2)] >> ((i % 2) ? 0 : 4)) & 0xf;
    468   1.6  riastrad 			}
    469   1.6  riastrad 		}
    470   1.6  riastrad 		break;
    471   1.6  riastrad 	case DP_QUERY_PAYLOAD:
    472   1.6  riastrad 		req->u.query_payload.port_number = (buf[idx] >> 4) & 0xf;
    473   1.6  riastrad 		WARN_ON(buf[++idx] & 0x80);
    474   1.6  riastrad 		req->u.query_payload.vcpi = buf[idx] & 0x7f;
    475   1.6  riastrad 		break;
    476   1.6  riastrad 	case DP_REMOTE_DPCD_READ:
    477   1.6  riastrad 		{
    478   1.6  riastrad 			struct drm_dp_remote_dpcd_read *r = &req->u.dpcd_read;
    479   1.6  riastrad 
    480   1.6  riastrad 			r->port_number = (buf[idx] >> 4) & 0xf;
    481   1.6  riastrad 
    482   1.6  riastrad 			r->dpcd_address = (buf[idx] << 16) & 0xf0000;
    483   1.6  riastrad 			r->dpcd_address |= (buf[++idx] << 8) & 0xff00;
    484   1.6  riastrad 			r->dpcd_address |= buf[++idx] & 0xff;
    485   1.6  riastrad 
    486   1.6  riastrad 			r->num_bytes = buf[++idx];
    487   1.6  riastrad 		}
    488   1.6  riastrad 		break;
    489   1.6  riastrad 	case DP_REMOTE_DPCD_WRITE:
    490   1.6  riastrad 		{
    491   1.6  riastrad 			struct drm_dp_remote_dpcd_write *w =
    492   1.6  riastrad 				&req->u.dpcd_write;
    493   1.6  riastrad 
    494   1.6  riastrad 			w->port_number = (buf[idx] >> 4) & 0xf;
    495   1.6  riastrad 
    496   1.6  riastrad 			w->dpcd_address = (buf[idx] << 16) & 0xf0000;
    497   1.6  riastrad 			w->dpcd_address |= (buf[++idx] << 8) & 0xff00;
    498   1.6  riastrad 			w->dpcd_address |= buf[++idx] & 0xff;
    499   1.6  riastrad 
    500   1.6  riastrad 			w->num_bytes = buf[++idx];
    501   1.6  riastrad 
    502   1.6  riastrad 			w->bytes = kmemdup(&buf[++idx], w->num_bytes,
    503   1.6  riastrad 					   GFP_KERNEL);
    504   1.6  riastrad 			if (!w->bytes)
    505   1.6  riastrad 				return -ENOMEM;
    506   1.6  riastrad 		}
    507   1.6  riastrad 		break;
    508   1.6  riastrad 	case DP_REMOTE_I2C_READ:
    509   1.6  riastrad 		{
    510   1.6  riastrad 			struct drm_dp_remote_i2c_read *r = &req->u.i2c_read;
    511   1.6  riastrad 			struct drm_dp_remote_i2c_read_tx *tx;
    512   1.6  riastrad 			bool failed = false;
    513   1.6  riastrad 
    514   1.6  riastrad 			r->num_transactions = buf[idx] & 0x3;
    515   1.6  riastrad 			r->port_number = (buf[idx] >> 4) & 0xf;
    516   1.6  riastrad 			for (i = 0; i < r->num_transactions; i++) {
    517   1.6  riastrad 				tx = &r->transactions[i];
    518   1.6  riastrad 
    519   1.6  riastrad 				tx->i2c_dev_id = buf[++idx] & 0x7f;
    520   1.6  riastrad 				tx->num_bytes = buf[++idx];
    521   1.6  riastrad 				tx->bytes = kmemdup(&buf[++idx],
    522   1.6  riastrad 						    tx->num_bytes,
    523   1.6  riastrad 						    GFP_KERNEL);
    524   1.6  riastrad 				if (!tx->bytes) {
    525   1.6  riastrad 					failed = true;
    526   1.6  riastrad 					break;
    527   1.6  riastrad 				}
    528   1.6  riastrad 				idx += tx->num_bytes;
    529   1.6  riastrad 				tx->no_stop_bit = (buf[idx] >> 5) & 0x1;
    530   1.6  riastrad 				tx->i2c_transaction_delay = buf[idx] & 0xf;
    531   1.6  riastrad 			}
    532   1.6  riastrad 
    533   1.6  riastrad 			if (failed) {
    534   1.6  riastrad 				for (i = 0; i < r->num_transactions; i++) {
    535   1.6  riastrad 					tx = &r->transactions[i];
    536   1.6  riastrad 					kfree(tx->bytes);
    537   1.6  riastrad 				}
    538   1.6  riastrad 				return -ENOMEM;
    539   1.6  riastrad 			}
    540   1.6  riastrad 
    541   1.6  riastrad 			r->read_i2c_device_id = buf[++idx] & 0x7f;
    542   1.6  riastrad 			r->num_bytes_read = buf[++idx];
    543   1.6  riastrad 		}
    544   1.6  riastrad 		break;
    545   1.6  riastrad 	case DP_REMOTE_I2C_WRITE:
    546   1.6  riastrad 		{
    547   1.6  riastrad 			struct drm_dp_remote_i2c_write *w = &req->u.i2c_write;
    548   1.6  riastrad 
    549   1.6  riastrad 			w->port_number = (buf[idx] >> 4) & 0xf;
    550   1.6  riastrad 			w->write_i2c_device_id = buf[++idx] & 0x7f;
    551   1.6  riastrad 			w->num_bytes = buf[++idx];
    552   1.6  riastrad 			w->bytes = kmemdup(&buf[++idx], w->num_bytes,
    553   1.6  riastrad 					   GFP_KERNEL);
    554   1.6  riastrad 			if (!w->bytes)
    555   1.6  riastrad 				return -ENOMEM;
    556   1.6  riastrad 		}
    557   1.6  riastrad 		break;
    558   1.6  riastrad 	}
    559   1.6  riastrad 
    560   1.6  riastrad 	return 0;
    561   1.6  riastrad }
    562   1.6  riastrad EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_decode_sideband_req);
    563   1.6  riastrad 
    564   1.8  riastrad static void
    565   1.6  riastrad drm_dp_dump_sideband_msg_req_body(const struct drm_dp_sideband_msg_req_body *req,
    566   1.6  riastrad 				  int indent, struct drm_printer *printer)
    567   1.6  riastrad {
    568   1.6  riastrad 	int i;
    569   1.6  riastrad 
    570   1.6  riastrad #define P(f, ...) drm_printf_indent(printer, indent, f, ##__VA_ARGS__)
    571   1.6  riastrad 	if (req->req_type == DP_LINK_ADDRESS) {
    572   1.6  riastrad 		/* No contents to print */
    573   1.6  riastrad 		P("type=%s\n", drm_dp_mst_req_type_str(req->req_type));
    574   1.6  riastrad 		return;
    575   1.6  riastrad 	}
    576   1.6  riastrad 
    577   1.6  riastrad 	P("type=%s contents:\n", drm_dp_mst_req_type_str(req->req_type));
    578   1.6  riastrad 	indent++;
    579   1.6  riastrad 
    580   1.6  riastrad 	switch (req->req_type) {
    581   1.6  riastrad 	case DP_ENUM_PATH_RESOURCES:
    582   1.6  riastrad 	case DP_POWER_DOWN_PHY:
    583   1.6  riastrad 	case DP_POWER_UP_PHY:
    584   1.6  riastrad 		P("port=%d\n", req->u.port_num.port_number);
    585   1.6  riastrad 		break;
    586   1.6  riastrad 	case DP_ALLOCATE_PAYLOAD:
    587   1.6  riastrad 		P("port=%d vcpi=%d pbn=%d sdp_streams=%d %*ph\n",
    588   1.6  riastrad 		  req->u.allocate_payload.port_number,
    589   1.6  riastrad 		  req->u.allocate_payload.vcpi, req->u.allocate_payload.pbn,
    590   1.6  riastrad 		  req->u.allocate_payload.number_sdp_streams,
    591   1.6  riastrad 		  req->u.allocate_payload.number_sdp_streams,
    592   1.6  riastrad 		  req->u.allocate_payload.sdp_stream_sink);
    593   1.6  riastrad 		break;
    594   1.6  riastrad 	case DP_QUERY_PAYLOAD:
    595   1.6  riastrad 		P("port=%d vcpi=%d\n",
    596   1.6  riastrad 		  req->u.query_payload.port_number,
    597   1.6  riastrad 		  req->u.query_payload.vcpi);
    598   1.6  riastrad 		break;
    599   1.6  riastrad 	case DP_REMOTE_DPCD_READ:
    600   1.6  riastrad 		P("port=%d dpcd_addr=%05x len=%d\n",
    601   1.6  riastrad 		  req->u.dpcd_read.port_number, req->u.dpcd_read.dpcd_address,
    602   1.6  riastrad 		  req->u.dpcd_read.num_bytes);
    603   1.6  riastrad 		break;
    604   1.6  riastrad 	case DP_REMOTE_DPCD_WRITE:
    605   1.6  riastrad 		P("port=%d addr=%05x len=%d: %*ph\n",
    606   1.6  riastrad 		  req->u.dpcd_write.port_number,
    607   1.6  riastrad 		  req->u.dpcd_write.dpcd_address,
    608   1.6  riastrad 		  req->u.dpcd_write.num_bytes, req->u.dpcd_write.num_bytes,
    609   1.6  riastrad 		  req->u.dpcd_write.bytes);
    610   1.6  riastrad 		break;
    611   1.6  riastrad 	case DP_REMOTE_I2C_READ:
    612   1.6  riastrad 		P("port=%d num_tx=%d id=%d size=%d:\n",
    613   1.6  riastrad 		  req->u.i2c_read.port_number,
    614   1.6  riastrad 		  req->u.i2c_read.num_transactions,
    615   1.6  riastrad 		  req->u.i2c_read.read_i2c_device_id,
    616   1.6  riastrad 		  req->u.i2c_read.num_bytes_read);
    617   1.6  riastrad 
    618   1.6  riastrad 		indent++;
    619   1.6  riastrad 		for (i = 0; i < req->u.i2c_read.num_transactions; i++) {
    620   1.6  riastrad 			const struct drm_dp_remote_i2c_read_tx *rtx =
    621   1.6  riastrad 				&req->u.i2c_read.transactions[i];
    622   1.6  riastrad 
    623   1.6  riastrad 			P("%d: id=%03d size=%03d no_stop_bit=%d tx_delay=%03d: %*ph\n",
    624   1.6  riastrad 			  i, rtx->i2c_dev_id, rtx->num_bytes,
    625   1.6  riastrad 			  rtx->no_stop_bit, rtx->i2c_transaction_delay,
    626   1.6  riastrad 			  rtx->num_bytes, rtx->bytes);
    627   1.6  riastrad 		}
    628   1.6  riastrad 		break;
    629   1.6  riastrad 	case DP_REMOTE_I2C_WRITE:
    630   1.6  riastrad 		P("port=%d id=%d size=%d: %*ph\n",
    631   1.6  riastrad 		  req->u.i2c_write.port_number,
    632   1.6  riastrad 		  req->u.i2c_write.write_i2c_device_id,
    633   1.6  riastrad 		  req->u.i2c_write.num_bytes, req->u.i2c_write.num_bytes,
    634   1.6  riastrad 		  req->u.i2c_write.bytes);
    635   1.6  riastrad 		break;
    636   1.6  riastrad 	default:
    637   1.6  riastrad 		P("???\n");
    638   1.6  riastrad 		break;
    639   1.6  riastrad 	}
    640   1.6  riastrad #undef P
    641   1.6  riastrad }
    642   1.6  riastrad EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_dump_sideband_msg_req_body);
    643   1.6  riastrad 
    644   1.6  riastrad static inline void
    645   1.6  riastrad drm_dp_mst_dump_sideband_msg_tx(struct drm_printer *p,
    646   1.6  riastrad 				const struct drm_dp_sideband_msg_tx *txmsg)
    647   1.6  riastrad {
    648   1.6  riastrad 	struct drm_dp_sideband_msg_req_body req;
    649   1.6  riastrad 	char buf[64];
    650   1.6  riastrad 	int ret;
    651   1.6  riastrad 	int i;
    652   1.6  riastrad 
    653   1.6  riastrad 	drm_dp_mst_rad_to_str(txmsg->dst->rad, txmsg->dst->lct, buf,
    654   1.6  riastrad 			      sizeof(buf));
    655   1.6  riastrad 	drm_printf(p, "txmsg cur_offset=%x cur_len=%x seqno=%x state=%s path_msg=%d dst=%s\n",
    656   1.6  riastrad 		   txmsg->cur_offset, txmsg->cur_len, txmsg->seqno,
    657   1.6  riastrad 		   drm_dp_mst_sideband_tx_state_str(txmsg->state),
    658   1.6  riastrad 		   txmsg->path_msg, buf);
    659   1.6  riastrad 
    660   1.6  riastrad 	ret = drm_dp_decode_sideband_req(txmsg, &req);
    661   1.6  riastrad 	if (ret) {
    662   1.6  riastrad 		drm_printf(p, "<failed to decode sideband req: %d>\n", ret);
    663   1.6  riastrad 		return;
    664   1.6  riastrad 	}
    665   1.6  riastrad 	drm_dp_dump_sideband_msg_req_body(&req, 1, p);
    666   1.6  riastrad 
    667   1.6  riastrad 	switch (req.req_type) {
    668   1.6  riastrad 	case DP_REMOTE_DPCD_WRITE:
    669   1.6  riastrad 		kfree(req.u.dpcd_write.bytes);
    670   1.6  riastrad 		break;
    671   1.6  riastrad 	case DP_REMOTE_I2C_READ:
    672   1.6  riastrad 		for (i = 0; i < req.u.i2c_read.num_transactions; i++)
    673   1.6  riastrad 			kfree(req.u.i2c_read.transactions[i].bytes);
    674   1.6  riastrad 		break;
    675   1.6  riastrad 	case DP_REMOTE_I2C_WRITE:
    676   1.6  riastrad 		kfree(req.u.i2c_write.bytes);
    677   1.6  riastrad 		break;
    678   1.6  riastrad 	}
    679   1.6  riastrad }
    680   1.1  riastrad 
    681   1.1  riastrad static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
    682   1.1  riastrad {
    683   1.1  riastrad 	u8 crc4;
    684   1.1  riastrad 	crc4 = drm_dp_msg_data_crc4(msg, len);
    685   1.1  riastrad 	msg[len] = crc4;
    686   1.1  riastrad }
    687   1.1  riastrad 
    688   1.1  riastrad static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
    689   1.1  riastrad 					 struct drm_dp_sideband_msg_tx *raw)
    690   1.1  riastrad {
    691   1.1  riastrad 	int idx = 0;
    692   1.1  riastrad 	u8 *buf = raw->msg;
    693   1.1  riastrad 
    694   1.1  riastrad 	buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
    695   1.1  riastrad 
    696   1.1  riastrad 	raw->cur_len = idx;
    697   1.1  riastrad }
    698   1.1  riastrad 
    699   1.1  riastrad /* this adds a chunk of msg to the builder to get the final msg */
    700   1.1  riastrad static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
    701   1.1  riastrad 				      u8 *replybuf, u8 replybuflen, bool hdr)
    702   1.1  riastrad {
    703   1.1  riastrad 	int ret;
    704   1.3  riastrad 	u8 crc4 __unused;	/* XXX Mistake?  */
    705   1.1  riastrad 
    706   1.1  riastrad 	if (hdr) {
    707   1.1  riastrad 		u8 hdrlen;
    708   1.1  riastrad 		struct drm_dp_sideband_msg_hdr recv_hdr;
    709   1.1  riastrad 		ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
    710   1.1  riastrad 		if (ret == false) {
    711   1.1  riastrad 			print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
    712   1.1  riastrad 			return false;
    713   1.1  riastrad 		}
    714   1.1  riastrad 
    715   1.1  riastrad 		/*
    716   1.1  riastrad 		 * ignore out-of-order messages or messages that are part of a
    717   1.1  riastrad 		 * failed transaction
    718   1.1  riastrad 		 */
    719   1.1  riastrad 		if (!recv_hdr.somt && !msg->have_somt)
    720   1.1  riastrad 			return false;
    721   1.1  riastrad 
    722   1.1  riastrad 		/* get length contained in this portion */
    723   1.1  riastrad 		msg->curchunk_len = recv_hdr.msg_len;
    724   1.1  riastrad 		msg->curchunk_hdrlen = hdrlen;
    725   1.1  riastrad 
    726   1.1  riastrad 		/* we have already gotten an somt - don't bother parsing */
    727   1.1  riastrad 		if (recv_hdr.somt && msg->have_somt)
    728   1.1  riastrad 			return false;
    729   1.1  riastrad 
    730   1.1  riastrad 		if (recv_hdr.somt) {
    731   1.1  riastrad 			memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
    732   1.1  riastrad 			msg->have_somt = true;
    733   1.1  riastrad 		}
    734   1.1  riastrad 		if (recv_hdr.eomt)
    735   1.1  riastrad 			msg->have_eomt = true;
    736   1.1  riastrad 
    737   1.1  riastrad 		/* copy the bytes for the remainder of this header chunk */
    738   1.1  riastrad 		msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
    739   1.1  riastrad 		memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
    740   1.1  riastrad 	} else {
    741   1.1  riastrad 		memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
    742   1.1  riastrad 		msg->curchunk_idx += replybuflen;
    743   1.1  riastrad 	}
    744   1.1  riastrad 
    745   1.1  riastrad 	if (msg->curchunk_idx >= msg->curchunk_len) {
    746   1.1  riastrad 		/* do CRC */
    747   1.1  riastrad 		crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
    748   1.1  riastrad 		/* copy chunk into bigger msg */
    749   1.1  riastrad 		memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
    750   1.1  riastrad 		msg->curlen += msg->curchunk_len - 1;
    751   1.1  riastrad 	}
    752   1.1  riastrad 	return true;
    753   1.1  riastrad }
    754   1.1  riastrad 
    755   1.1  riastrad static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
    756   1.1  riastrad 					       struct drm_dp_sideband_msg_reply_body *repmsg)
    757   1.1  riastrad {
    758   1.1  riastrad 	int idx = 1;
    759   1.1  riastrad 	int i;
    760   1.1  riastrad 	memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
    761   1.1  riastrad 	idx += 16;
    762   1.1  riastrad 	repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
    763   1.1  riastrad 	idx++;
    764   1.1  riastrad 	if (idx > raw->curlen)
    765   1.1  riastrad 		goto fail_len;
    766   1.1  riastrad 	for (i = 0; i < repmsg->u.link_addr.nports; i++) {
    767   1.1  riastrad 		if (raw->msg[idx] & 0x80)
    768   1.1  riastrad 			repmsg->u.link_addr.ports[i].input_port = 1;
    769   1.1  riastrad 
    770   1.1  riastrad 		repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
    771   1.1  riastrad 		repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
    772   1.1  riastrad 
    773   1.1  riastrad 		idx++;
    774   1.1  riastrad 		if (idx > raw->curlen)
    775   1.1  riastrad 			goto fail_len;
    776   1.1  riastrad 		repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
    777   1.1  riastrad 		repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
    778   1.1  riastrad 		if (repmsg->u.link_addr.ports[i].input_port == 0)
    779   1.1  riastrad 			repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
    780   1.1  riastrad 		idx++;
    781   1.1  riastrad 		if (idx > raw->curlen)
    782   1.1  riastrad 			goto fail_len;
    783   1.1  riastrad 		if (repmsg->u.link_addr.ports[i].input_port == 0) {
    784   1.1  riastrad 			repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
    785   1.1  riastrad 			idx++;
    786   1.1  riastrad 			if (idx > raw->curlen)
    787   1.1  riastrad 				goto fail_len;
    788   1.1  riastrad 			memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
    789   1.1  riastrad 			idx += 16;
    790   1.1  riastrad 			if (idx > raw->curlen)
    791   1.1  riastrad 				goto fail_len;
    792   1.1  riastrad 			repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
    793   1.1  riastrad 			repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
    794   1.1  riastrad 			idx++;
    795   1.1  riastrad 
    796   1.1  riastrad 		}
    797   1.1  riastrad 		if (idx > raw->curlen)
    798   1.1  riastrad 			goto fail_len;
    799   1.1  riastrad 	}
    800   1.1  riastrad 
    801   1.1  riastrad 	return true;
    802   1.1  riastrad fail_len:
    803   1.1  riastrad 	DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
    804   1.1  riastrad 	return false;
    805   1.1  riastrad }
    806   1.1  riastrad 
    807   1.1  riastrad static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
    808   1.1  riastrad 						   struct drm_dp_sideband_msg_reply_body *repmsg)
    809   1.1  riastrad {
    810   1.1  riastrad 	int idx = 1;
    811   1.1  riastrad 	repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
    812   1.1  riastrad 	idx++;
    813   1.1  riastrad 	if (idx > raw->curlen)
    814   1.1  riastrad 		goto fail_len;
    815   1.1  riastrad 	repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
    816   1.6  riastrad 	idx++;
    817   1.1  riastrad 	if (idx > raw->curlen)
    818   1.1  riastrad 		goto fail_len;
    819   1.1  riastrad 
    820   1.1  riastrad 	memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
    821   1.1  riastrad 	return true;
    822   1.1  riastrad fail_len:
    823   1.1  riastrad 	DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
    824   1.1  riastrad 	return false;
    825   1.1  riastrad }
    826   1.1  riastrad 
    827   1.1  riastrad static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
    828   1.1  riastrad 						      struct drm_dp_sideband_msg_reply_body *repmsg)
    829   1.1  riastrad {
    830   1.1  riastrad 	int idx = 1;
    831   1.1  riastrad 	repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
    832   1.1  riastrad 	idx++;
    833   1.1  riastrad 	if (idx > raw->curlen)
    834   1.1  riastrad 		goto fail_len;
    835   1.1  riastrad 	return true;
    836   1.1  riastrad fail_len:
    837   1.1  riastrad 	DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
    838   1.1  riastrad 	return false;
    839   1.1  riastrad }
    840   1.1  riastrad 
    841   1.1  riastrad static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
    842   1.1  riastrad 						      struct drm_dp_sideband_msg_reply_body *repmsg)
    843   1.1  riastrad {
    844   1.1  riastrad 	int idx = 1;
    845   1.1  riastrad 
    846   1.1  riastrad 	repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
    847   1.1  riastrad 	idx++;
    848   1.1  riastrad 	if (idx > raw->curlen)
    849   1.1  riastrad 		goto fail_len;
    850   1.1  riastrad 	repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
    851   1.1  riastrad 	idx++;
    852   1.1  riastrad 	/* TODO check */
    853   1.1  riastrad 	memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
    854   1.1  riastrad 	return true;
    855   1.1  riastrad fail_len:
    856   1.1  riastrad 	DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
    857   1.1  riastrad 	return false;
    858   1.1  riastrad }
    859   1.1  riastrad 
    860   1.1  riastrad static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
    861   1.1  riastrad 							  struct drm_dp_sideband_msg_reply_body *repmsg)
    862   1.1  riastrad {
    863   1.1  riastrad 	int idx = 1;
    864   1.1  riastrad 	repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
    865   1.6  riastrad 	repmsg->u.path_resources.fec_capable = raw->msg[idx] & 0x1;
    866   1.1  riastrad 	idx++;
    867   1.1  riastrad 	if (idx > raw->curlen)
    868   1.1  riastrad 		goto fail_len;
    869   1.1  riastrad 	repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
    870   1.1  riastrad 	idx += 2;
    871   1.1  riastrad 	if (idx > raw->curlen)
    872   1.1  riastrad 		goto fail_len;
    873   1.1  riastrad 	repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
    874   1.1  riastrad 	idx += 2;
    875   1.1  riastrad 	if (idx > raw->curlen)
    876   1.1  riastrad 		goto fail_len;
    877   1.1  riastrad 	return true;
    878   1.1  riastrad fail_len:
    879   1.1  riastrad 	DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
    880   1.1  riastrad 	return false;
    881   1.1  riastrad }
    882   1.1  riastrad 
    883   1.1  riastrad static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
    884   1.1  riastrad 							  struct drm_dp_sideband_msg_reply_body *repmsg)
    885   1.1  riastrad {
    886   1.1  riastrad 	int idx = 1;
    887   1.1  riastrad 	repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
    888   1.1  riastrad 	idx++;
    889   1.1  riastrad 	if (idx > raw->curlen)
    890   1.1  riastrad 		goto fail_len;
    891   1.1  riastrad 	repmsg->u.allocate_payload.vcpi = raw->msg[idx];
    892   1.1  riastrad 	idx++;
    893   1.1  riastrad 	if (idx > raw->curlen)
    894   1.1  riastrad 		goto fail_len;
    895   1.1  riastrad 	repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
    896   1.1  riastrad 	idx += 2;
    897   1.1  riastrad 	if (idx > raw->curlen)
    898   1.1  riastrad 		goto fail_len;
    899   1.1  riastrad 	return true;
    900   1.1  riastrad fail_len:
    901   1.1  riastrad 	DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
    902   1.1  riastrad 	return false;
    903   1.1  riastrad }
    904   1.1  riastrad 
    905   1.1  riastrad static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
    906   1.1  riastrad 						    struct drm_dp_sideband_msg_reply_body *repmsg)
    907   1.1  riastrad {
    908   1.1  riastrad 	int idx = 1;
    909   1.1  riastrad 	repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
    910   1.1  riastrad 	idx++;
    911   1.1  riastrad 	if (idx > raw->curlen)
    912   1.1  riastrad 		goto fail_len;
    913   1.1  riastrad 	repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
    914   1.1  riastrad 	idx += 2;
    915   1.1  riastrad 	if (idx > raw->curlen)
    916   1.1  riastrad 		goto fail_len;
    917   1.1  riastrad 	return true;
    918   1.1  riastrad fail_len:
    919   1.1  riastrad 	DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
    920   1.1  riastrad 	return false;
    921   1.1  riastrad }
    922   1.1  riastrad 
    923   1.6  riastrad static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
    924   1.6  riastrad 						       struct drm_dp_sideband_msg_reply_body *repmsg)
    925   1.6  riastrad {
    926   1.6  riastrad 	int idx = 1;
    927   1.6  riastrad 
    928   1.6  riastrad 	repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
    929   1.6  riastrad 	idx++;
    930   1.6  riastrad 	if (idx > raw->curlen) {
    931   1.6  riastrad 		DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
    932   1.6  riastrad 			      idx, raw->curlen);
    933   1.6  riastrad 		return false;
    934   1.6  riastrad 	}
    935   1.6  riastrad 	return true;
    936   1.6  riastrad }
    937   1.6  riastrad 
    938   1.1  riastrad static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
    939   1.1  riastrad 					struct drm_dp_sideband_msg_reply_body *msg)
    940   1.1  riastrad {
    941   1.1  riastrad 	memset(msg, 0, sizeof(*msg));
    942   1.1  riastrad 	msg->reply_type = (raw->msg[0] & 0x80) >> 7;
    943   1.1  riastrad 	msg->req_type = (raw->msg[0] & 0x7f);
    944   1.1  riastrad 
    945   1.6  riastrad 	if (msg->reply_type == DP_SIDEBAND_REPLY_NAK) {
    946   1.1  riastrad 		memcpy(msg->u.nak.guid, &raw->msg[1], 16);
    947   1.1  riastrad 		msg->u.nak.reason = raw->msg[17];
    948   1.1  riastrad 		msg->u.nak.nak_data = raw->msg[18];
    949   1.1  riastrad 		return false;
    950   1.1  riastrad 	}
    951   1.1  riastrad 
    952   1.1  riastrad 	switch (msg->req_type) {
    953   1.1  riastrad 	case DP_LINK_ADDRESS:
    954   1.1  riastrad 		return drm_dp_sideband_parse_link_address(raw, msg);
    955   1.1  riastrad 	case DP_QUERY_PAYLOAD:
    956   1.1  riastrad 		return drm_dp_sideband_parse_query_payload_ack(raw, msg);
    957   1.1  riastrad 	case DP_REMOTE_DPCD_READ:
    958   1.1  riastrad 		return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
    959   1.1  riastrad 	case DP_REMOTE_DPCD_WRITE:
    960   1.1  riastrad 		return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
    961   1.1  riastrad 	case DP_REMOTE_I2C_READ:
    962   1.1  riastrad 		return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
    963   1.1  riastrad 	case DP_ENUM_PATH_RESOURCES:
    964   1.1  riastrad 		return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
    965   1.1  riastrad 	case DP_ALLOCATE_PAYLOAD:
    966   1.1  riastrad 		return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
    967   1.6  riastrad 	case DP_POWER_DOWN_PHY:
    968   1.6  riastrad 	case DP_POWER_UP_PHY:
    969   1.6  riastrad 		return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
    970   1.6  riastrad 	case DP_CLEAR_PAYLOAD_ID_TABLE:
    971   1.6  riastrad 		return true; /* since there's nothing to parse */
    972   1.1  riastrad 	default:
    973   1.6  riastrad 		DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg->req_type,
    974   1.6  riastrad 			  drm_dp_mst_req_type_str(msg->req_type));
    975   1.1  riastrad 		return false;
    976   1.1  riastrad 	}
    977   1.1  riastrad }
    978   1.1  riastrad 
    979   1.1  riastrad static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
    980   1.1  riastrad 							   struct drm_dp_sideband_msg_req_body *msg)
    981   1.1  riastrad {
    982   1.1  riastrad 	int idx = 1;
    983   1.1  riastrad 
    984   1.1  riastrad 	msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
    985   1.1  riastrad 	idx++;
    986   1.1  riastrad 	if (idx > raw->curlen)
    987   1.1  riastrad 		goto fail_len;
    988   1.1  riastrad 
    989   1.1  riastrad 	memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
    990   1.1  riastrad 	idx += 16;
    991   1.1  riastrad 	if (idx > raw->curlen)
    992   1.1  riastrad 		goto fail_len;
    993   1.1  riastrad 
    994   1.1  riastrad 	msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
    995   1.1  riastrad 	msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
    996   1.1  riastrad 	msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
    997   1.1  riastrad 	msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
    998   1.1  riastrad 	msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
    999   1.1  riastrad 	idx++;
   1000   1.1  riastrad 	return true;
   1001   1.1  riastrad fail_len:
   1002   1.1  riastrad 	DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
   1003   1.1  riastrad 	return false;
   1004   1.1  riastrad }
   1005   1.1  riastrad 
   1006   1.1  riastrad static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
   1007   1.1  riastrad 							   struct drm_dp_sideband_msg_req_body *msg)
   1008   1.1  riastrad {
   1009   1.1  riastrad 	int idx = 1;
   1010   1.1  riastrad 
   1011   1.1  riastrad 	msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
   1012   1.1  riastrad 	idx++;
   1013   1.1  riastrad 	if (idx > raw->curlen)
   1014   1.1  riastrad 		goto fail_len;
   1015   1.1  riastrad 
   1016   1.1  riastrad 	memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
   1017   1.1  riastrad 	idx += 16;
   1018   1.1  riastrad 	if (idx > raw->curlen)
   1019   1.1  riastrad 		goto fail_len;
   1020   1.1  riastrad 
   1021   1.1  riastrad 	msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
   1022   1.1  riastrad 	idx++;
   1023   1.1  riastrad 	return true;
   1024   1.1  riastrad fail_len:
   1025   1.1  riastrad 	DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
   1026   1.1  riastrad 	return false;
   1027   1.1  riastrad }
   1028   1.1  riastrad 
   1029   1.1  riastrad static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
   1030   1.1  riastrad 				      struct drm_dp_sideband_msg_req_body *msg)
   1031   1.1  riastrad {
   1032   1.1  riastrad 	memset(msg, 0, sizeof(*msg));
   1033   1.1  riastrad 	msg->req_type = (raw->msg[0] & 0x7f);
   1034   1.1  riastrad 
   1035   1.1  riastrad 	switch (msg->req_type) {
   1036   1.1  riastrad 	case DP_CONNECTION_STATUS_NOTIFY:
   1037   1.1  riastrad 		return drm_dp_sideband_parse_connection_status_notify(raw, msg);
   1038   1.1  riastrad 	case DP_RESOURCE_STATUS_NOTIFY:
   1039   1.1  riastrad 		return drm_dp_sideband_parse_resource_status_notify(raw, msg);
   1040   1.1  riastrad 	default:
   1041   1.6  riastrad 		DRM_ERROR("Got unknown request 0x%02x (%s)\n", msg->req_type,
   1042   1.6  riastrad 			  drm_dp_mst_req_type_str(msg->req_type));
   1043   1.1  riastrad 		return false;
   1044   1.1  riastrad 	}
   1045   1.1  riastrad }
   1046   1.1  riastrad 
   1047   1.1  riastrad static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
   1048   1.1  riastrad {
   1049   1.1  riastrad 	struct drm_dp_sideband_msg_req_body req;
   1050   1.1  riastrad 
   1051   1.1  riastrad 	req.req_type = DP_REMOTE_DPCD_WRITE;
   1052   1.1  riastrad 	req.u.dpcd_write.port_number = port_num;
   1053   1.1  riastrad 	req.u.dpcd_write.dpcd_address = offset;
   1054   1.1  riastrad 	req.u.dpcd_write.num_bytes = num_bytes;
   1055   1.1  riastrad 	req.u.dpcd_write.bytes = bytes;
   1056   1.1  riastrad 	drm_dp_encode_sideband_req(&req, msg);
   1057   1.1  riastrad 
   1058   1.1  riastrad 	return 0;
   1059   1.1  riastrad }
   1060   1.1  riastrad 
   1061   1.1  riastrad static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
   1062   1.1  riastrad {
   1063   1.1  riastrad 	struct drm_dp_sideband_msg_req_body req;
   1064   1.1  riastrad 
   1065   1.1  riastrad 	req.req_type = DP_LINK_ADDRESS;
   1066   1.1  riastrad 	drm_dp_encode_sideband_req(&req, msg);
   1067   1.1  riastrad 	return 0;
   1068   1.1  riastrad }
   1069   1.1  riastrad 
   1070   1.6  riastrad static int build_clear_payload_id_table(struct drm_dp_sideband_msg_tx *msg)
   1071   1.6  riastrad {
   1072   1.6  riastrad 	struct drm_dp_sideband_msg_req_body req;
   1073   1.6  riastrad 
   1074   1.6  riastrad 	req.req_type = DP_CLEAR_PAYLOAD_ID_TABLE;
   1075   1.6  riastrad 	drm_dp_encode_sideband_req(&req, msg);
   1076   1.6  riastrad 	return 0;
   1077   1.6  riastrad }
   1078   1.6  riastrad 
   1079   1.1  riastrad static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
   1080   1.1  riastrad {
   1081   1.1  riastrad 	struct drm_dp_sideband_msg_req_body req;
   1082   1.1  riastrad 
   1083   1.1  riastrad 	req.req_type = DP_ENUM_PATH_RESOURCES;
   1084   1.1  riastrad 	req.u.port_num.port_number = port_num;
   1085   1.1  riastrad 	drm_dp_encode_sideband_req(&req, msg);
   1086   1.1  riastrad 	msg->path_msg = true;
   1087   1.1  riastrad 	return 0;
   1088   1.1  riastrad }
   1089   1.1  riastrad 
   1090   1.1  riastrad static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
   1091   1.6  riastrad 				  u8 vcpi, uint16_t pbn,
   1092   1.6  riastrad 				  u8 number_sdp_streams,
   1093   1.6  riastrad 				  u8 *sdp_stream_sink)
   1094   1.1  riastrad {
   1095   1.1  riastrad 	struct drm_dp_sideband_msg_req_body req;
   1096   1.1  riastrad 	memset(&req, 0, sizeof(req));
   1097   1.1  riastrad 	req.req_type = DP_ALLOCATE_PAYLOAD;
   1098   1.1  riastrad 	req.u.allocate_payload.port_number = port_num;
   1099   1.1  riastrad 	req.u.allocate_payload.vcpi = vcpi;
   1100   1.1  riastrad 	req.u.allocate_payload.pbn = pbn;
   1101   1.6  riastrad 	req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
   1102   1.6  riastrad 	memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
   1103   1.6  riastrad 		   number_sdp_streams);
   1104   1.6  riastrad 	drm_dp_encode_sideband_req(&req, msg);
   1105   1.6  riastrad 	msg->path_msg = true;
   1106   1.6  riastrad 	return 0;
   1107   1.6  riastrad }
   1108   1.6  riastrad 
   1109   1.6  riastrad static int build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
   1110   1.6  riastrad 				  int port_num, bool power_up)
   1111   1.6  riastrad {
   1112   1.6  riastrad 	struct drm_dp_sideband_msg_req_body req;
   1113   1.6  riastrad 
   1114   1.6  riastrad 	if (power_up)
   1115   1.6  riastrad 		req.req_type = DP_POWER_UP_PHY;
   1116   1.6  riastrad 	else
   1117   1.6  riastrad 		req.req_type = DP_POWER_DOWN_PHY;
   1118   1.6  riastrad 
   1119   1.6  riastrad 	req.u.port_num.port_number = port_num;
   1120   1.1  riastrad 	drm_dp_encode_sideband_req(&req, msg);
   1121   1.1  riastrad 	msg->path_msg = true;
   1122   1.1  riastrad 	return 0;
   1123   1.1  riastrad }
   1124   1.1  riastrad 
   1125   1.1  riastrad static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
   1126   1.1  riastrad 					struct drm_dp_vcpi *vcpi)
   1127   1.1  riastrad {
   1128   1.1  riastrad 	int ret, vcpi_ret;
   1129   1.1  riastrad 
   1130   1.1  riastrad 	mutex_lock(&mgr->payload_lock);
   1131   1.1  riastrad 	ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
   1132   1.1  riastrad 	if (ret > mgr->max_payloads) {
   1133   1.1  riastrad 		ret = -EINVAL;
   1134   1.1  riastrad 		DRM_DEBUG_KMS("out of payload ids %d\n", ret);
   1135   1.1  riastrad 		goto out_unlock;
   1136   1.1  riastrad 	}
   1137   1.1  riastrad 
   1138   1.1  riastrad 	vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
   1139   1.1  riastrad 	if (vcpi_ret > mgr->max_payloads) {
   1140   1.1  riastrad 		ret = -EINVAL;
   1141   1.1  riastrad 		DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
   1142   1.1  riastrad 		goto out_unlock;
   1143   1.1  riastrad 	}
   1144   1.1  riastrad 
   1145   1.1  riastrad 	set_bit(ret, &mgr->payload_mask);
   1146   1.1  riastrad 	set_bit(vcpi_ret, &mgr->vcpi_mask);
   1147   1.1  riastrad 	vcpi->vcpi = vcpi_ret + 1;
   1148   1.1  riastrad 	mgr->proposed_vcpis[ret - 1] = vcpi;
   1149   1.1  riastrad out_unlock:
   1150   1.1  riastrad 	mutex_unlock(&mgr->payload_lock);
   1151   1.1  riastrad 	return ret;
   1152   1.1  riastrad }
   1153   1.1  riastrad 
   1154   1.1  riastrad static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
   1155   1.1  riastrad 				      int vcpi)
   1156   1.1  riastrad {
   1157   1.1  riastrad 	int i;
   1158   1.1  riastrad 	if (vcpi == 0)
   1159   1.1  riastrad 		return;
   1160   1.1  riastrad 
   1161   1.1  riastrad 	mutex_lock(&mgr->payload_lock);
   1162   1.1  riastrad 	DRM_DEBUG_KMS("putting payload %d\n", vcpi);
   1163   1.1  riastrad 	clear_bit(vcpi - 1, &mgr->vcpi_mask);
   1164   1.1  riastrad 
   1165   1.1  riastrad 	for (i = 0; i < mgr->max_payloads; i++) {
   1166   1.6  riastrad 		if (mgr->proposed_vcpis[i] &&
   1167   1.6  riastrad 		    mgr->proposed_vcpis[i]->vcpi == vcpi) {
   1168   1.6  riastrad 			mgr->proposed_vcpis[i] = NULL;
   1169   1.6  riastrad 			clear_bit(i + 1, &mgr->payload_mask);
   1170   1.6  riastrad 		}
   1171   1.1  riastrad 	}
   1172   1.1  riastrad 	mutex_unlock(&mgr->payload_lock);
   1173   1.1  riastrad }
   1174   1.1  riastrad 
   1175   1.1  riastrad static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
   1176   1.1  riastrad 			      struct drm_dp_sideband_msg_tx *txmsg)
   1177   1.1  riastrad {
   1178   1.6  riastrad 	unsigned int state;
   1179   1.1  riastrad 
   1180   1.1  riastrad 	/*
   1181   1.1  riastrad 	 * All updates to txmsg->state are protected by mgr->qlock, and the two
   1182   1.1  riastrad 	 * cases we check here are terminal states. For those the barriers
   1183   1.1  riastrad 	 * provided by the wake_up/wait_event pair are enough.
   1184   1.1  riastrad 	 */
   1185   1.6  riastrad 	state = READ_ONCE(txmsg->state);
   1186   1.6  riastrad 	return (state == DRM_DP_SIDEBAND_TX_RX ||
   1187   1.6  riastrad 		state == DRM_DP_SIDEBAND_TX_TIMEOUT);
   1188   1.1  riastrad }
   1189   1.1  riastrad 
   1190   1.1  riastrad static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
   1191   1.1  riastrad 				    struct drm_dp_sideband_msg_tx *txmsg)
   1192   1.1  riastrad {
   1193   1.1  riastrad 	struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
   1194   1.1  riastrad 	int ret;
   1195   1.1  riastrad 
   1196   1.3  riastrad 	mutex_lock(&mstb->mgr->qlock);
   1197   1.3  riastrad 	DRM_TIMED_WAIT_UNTIL(ret, &mgr->tx_waitq, &mstb->mgr->qlock, 4*HZ,
   1198   1.3  riastrad 	    check_txmsg_state(mgr, txmsg));
   1199   1.1  riastrad 	if (ret > 0) {
   1200   1.1  riastrad 		if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
   1201   1.1  riastrad 			ret = -EIO;
   1202   1.1  riastrad 			goto out;
   1203   1.1  riastrad 		}
   1204   1.1  riastrad 	} else {
   1205   1.1  riastrad 		DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
   1206   1.1  riastrad 
   1207   1.1  riastrad 		/* dump some state */
   1208   1.1  riastrad 		ret = -EIO;
   1209   1.1  riastrad 
   1210   1.1  riastrad 		/* remove from q */
   1211   1.1  riastrad 		if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
   1212   1.1  riastrad 		    txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
   1213   1.1  riastrad 			list_del(&txmsg->next);
   1214   1.1  riastrad 		}
   1215   1.1  riastrad 
   1216   1.1  riastrad 		if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
   1217   1.1  riastrad 		    txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
   1218   1.1  riastrad 			mstb->tx_slots[txmsg->seqno] = NULL;
   1219   1.1  riastrad 		}
   1220   1.6  riastrad 		mgr->is_waiting_for_dwn_reply = false;
   1221   1.6  riastrad 
   1222   1.1  riastrad 	}
   1223   1.1  riastrad out:
   1224   1.6  riastrad 	if (unlikely(ret == -EIO) && drm_debug_enabled(DRM_UT_DP)) {
   1225   1.6  riastrad 		struct drm_printer p = drm_debug_printer(DBG_PREFIX);
   1226   1.6  riastrad 
   1227   1.6  riastrad 		drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
   1228   1.6  riastrad 	}
   1229   1.1  riastrad 	mutex_unlock(&mgr->qlock);
   1230   1.1  riastrad 
   1231   1.6  riastrad 	drm_dp_mst_kick_tx(mgr);
   1232   1.1  riastrad 	return ret;
   1233   1.1  riastrad }
   1234   1.1  riastrad 
   1235   1.1  riastrad static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
   1236   1.1  riastrad {
   1237   1.1  riastrad 	struct drm_dp_mst_branch *mstb;
   1238   1.1  riastrad 
   1239   1.1  riastrad 	mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
   1240   1.1  riastrad 	if (!mstb)
   1241   1.1  riastrad 		return NULL;
   1242   1.1  riastrad 
   1243   1.1  riastrad 	mstb->lct = lct;
   1244   1.1  riastrad 	if (lct > 1)
   1245   1.1  riastrad 		memcpy(mstb->rad, rad, lct / 2);
   1246   1.1  riastrad 	INIT_LIST_HEAD(&mstb->ports);
   1247   1.6  riastrad 	kref_init(&mstb->topology_kref);
   1248   1.6  riastrad 	kref_init(&mstb->malloc_kref);
   1249   1.1  riastrad 	return mstb;
   1250   1.1  riastrad }
   1251   1.1  riastrad 
   1252   1.1  riastrad static void drm_dp_free_mst_branch_device(struct kref *kref)
   1253   1.1  riastrad {
   1254   1.6  riastrad 	struct drm_dp_mst_branch *mstb =
   1255   1.6  riastrad 		container_of(kref, struct drm_dp_mst_branch, malloc_kref);
   1256   1.6  riastrad 
   1257   1.6  riastrad 	if (mstb->port_parent)
   1258   1.6  riastrad 		drm_dp_mst_put_port_malloc(mstb->port_parent);
   1259   1.6  riastrad 
   1260   1.1  riastrad 	kfree(mstb);
   1261   1.1  riastrad }
   1262   1.1  riastrad 
   1263   1.6  riastrad /**
   1264   1.6  riastrad  * DOC: Branch device and port refcounting
   1265   1.6  riastrad  *
   1266   1.6  riastrad  * Topology refcount overview
   1267   1.6  riastrad  * ~~~~~~~~~~~~~~~~~~~~~~~~~~
   1268   1.6  riastrad  *
   1269   1.6  riastrad  * The refcounting schemes for &struct drm_dp_mst_branch and &struct
   1270   1.6  riastrad  * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
   1271   1.6  riastrad  * two different kinds of refcounts: topology refcounts, and malloc refcounts.
   1272   1.6  riastrad  *
   1273   1.6  riastrad  * Topology refcounts are not exposed to drivers, and are handled internally
   1274   1.6  riastrad  * by the DP MST helpers. The helpers use them in order to prevent the
   1275   1.6  riastrad  * in-memory topology state from being changed in the middle of critical
   1276   1.6  riastrad  * operations like changing the internal state of payload allocations. This
   1277   1.6  riastrad  * means each branch and port will be considered to be connected to the rest
   1278   1.6  riastrad  * of the topology until its topology refcount reaches zero. Additionally,
   1279   1.6  riastrad  * for ports this means that their associated &struct drm_connector will stay
   1280   1.6  riastrad  * registered with userspace until the port's refcount reaches 0.
   1281   1.6  riastrad  *
   1282   1.6  riastrad  * Malloc refcount overview
   1283   1.6  riastrad  * ~~~~~~~~~~~~~~~~~~~~~~~~
   1284   1.6  riastrad  *
   1285   1.6  riastrad  * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
   1286   1.6  riastrad  * drm_dp_mst_branch allocated even after all of its topology references have
   1287   1.6  riastrad  * been dropped, so that the driver or MST helpers can safely access each
   1288   1.6  riastrad  * branch's last known state before it was disconnected from the topology.
   1289   1.6  riastrad  * When the malloc refcount of a port or branch reaches 0, the memory
   1290   1.6  riastrad  * allocation containing the &struct drm_dp_mst_branch or &struct
   1291   1.6  riastrad  * drm_dp_mst_port respectively will be freed.
   1292   1.6  riastrad  *
   1293   1.6  riastrad  * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
   1294   1.6  riastrad  * to drivers. As of writing this documentation, there are no drivers that
   1295   1.6  riastrad  * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
   1296   1.6  riastrad  * helpers. Exposing this API to drivers in a race-free manner would take more
   1297   1.6  riastrad  * tweaking of the refcounting scheme, however patches are welcome provided
   1298   1.6  riastrad  * there is a legitimate driver usecase for this.
   1299   1.6  riastrad  *
   1300   1.6  riastrad  * Refcount relationships in a topology
   1301   1.6  riastrad  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   1302   1.6  riastrad  *
   1303   1.6  riastrad  * Let's take a look at why the relationship between topology and malloc
   1304   1.6  riastrad  * refcounts is designed the way it is.
   1305   1.6  riastrad  *
   1306   1.6  riastrad  * .. kernel-figure:: dp-mst/topology-figure-1.dot
   1307   1.6  riastrad  *
   1308   1.6  riastrad  *    An example of topology and malloc refs in a DP MST topology with two
   1309   1.6  riastrad  *    active payloads. Topology refcount increments are indicated by solid
   1310   1.6  riastrad  *    lines, and malloc refcount increments are indicated by dashed lines.
   1311   1.6  riastrad  *    Each starts from the branch which incremented the refcount, and ends at
   1312   1.6  riastrad  *    the branch to which the refcount belongs to, i.e. the arrow points the
   1313   1.6  riastrad  *    same way as the C pointers used to reference a structure.
   1314   1.6  riastrad  *
   1315   1.6  riastrad  * As you can see in the above figure, every branch increments the topology
   1316   1.6  riastrad  * refcount of its children, and increments the malloc refcount of its
   1317   1.6  riastrad  * parent. Additionally, every payload increments the malloc refcount of its
   1318   1.6  riastrad  * assigned port by 1.
   1319   1.6  riastrad  *
   1320   1.6  riastrad  * So, what would happen if MSTB #3 from the above figure was unplugged from
   1321   1.6  riastrad  * the system, but the driver hadn't yet removed payload #2 from port #3? The
   1322   1.6  riastrad  * topology would start to look like the figure below.
   1323   1.6  riastrad  *
   1324   1.6  riastrad  * .. kernel-figure:: dp-mst/topology-figure-2.dot
   1325   1.6  riastrad  *
   1326   1.6  riastrad  *    Ports and branch devices which have been released from memory are
   1327   1.6  riastrad  *    colored grey, and references which have been removed are colored red.
   1328   1.6  riastrad  *
   1329   1.6  riastrad  * Whenever a port or branch device's topology refcount reaches zero, it will
   1330   1.6  riastrad  * decrement the topology refcounts of all its children, the malloc refcount
   1331   1.6  riastrad  * of its parent, and finally its own malloc refcount. For MSTB #4 and port
   1332   1.6  riastrad  * #4, this means they both have been disconnected from the topology and freed
   1333   1.6  riastrad  * from memory. But, because payload #2 is still holding a reference to port
   1334   1.6  riastrad  * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
   1335   1.6  riastrad  * is still accessible from memory. This also means port #3 has not yet
   1336   1.6  riastrad  * decremented the malloc refcount of MSTB #3, so its &struct
   1337   1.6  riastrad  * drm_dp_mst_branch will also stay allocated in memory until port #3's
   1338   1.6  riastrad  * malloc refcount reaches 0.
   1339   1.6  riastrad  *
   1340   1.6  riastrad  * This relationship is necessary because in order to release payload #2, we
   1341   1.6  riastrad  * need to be able to figure out the last relative of port #3 that's still
   1342   1.6  riastrad  * connected to the topology. In this case, we would travel up the topology as
   1343   1.6  riastrad  * shown below.
   1344   1.6  riastrad  *
   1345   1.6  riastrad  * .. kernel-figure:: dp-mst/topology-figure-3.dot
   1346   1.6  riastrad  *
   1347   1.6  riastrad  * And finally, remove payload #2 by communicating with port #2 through
   1348   1.6  riastrad  * sideband transactions.
   1349   1.6  riastrad  */
   1350   1.6  riastrad 
   1351   1.6  riastrad /**
   1352   1.6  riastrad  * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
   1353   1.6  riastrad  * device
   1354   1.6  riastrad  * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
   1355   1.6  riastrad  *
   1356   1.6  riastrad  * Increments &drm_dp_mst_branch.malloc_kref. When
   1357   1.6  riastrad  * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
   1358   1.6  riastrad  * will be released and @mstb may no longer be used.
   1359   1.6  riastrad  *
   1360   1.6  riastrad  * See also: drm_dp_mst_put_mstb_malloc()
   1361   1.6  riastrad  */
   1362   1.6  riastrad static void
   1363   1.6  riastrad drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
   1364   1.1  riastrad {
   1365   1.6  riastrad 	kref_get(&mstb->malloc_kref);
   1366   1.6  riastrad 	DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
   1367   1.6  riastrad }
   1368   1.1  riastrad 
   1369   1.6  riastrad /**
   1370   1.6  riastrad  * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
   1371   1.6  riastrad  * device
   1372   1.6  riastrad  * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
   1373   1.6  riastrad  *
   1374   1.6  riastrad  * Decrements &drm_dp_mst_branch.malloc_kref. When
   1375   1.6  riastrad  * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
   1376   1.6  riastrad  * will be released and @mstb may no longer be used.
   1377   1.6  riastrad  *
   1378   1.6  riastrad  * See also: drm_dp_mst_get_mstb_malloc()
   1379   1.6  riastrad  */
   1380   1.6  riastrad static void
   1381   1.6  riastrad drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
   1382   1.6  riastrad {
   1383   1.6  riastrad 	DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
   1384   1.6  riastrad 	kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
   1385   1.6  riastrad }
   1386   1.1  riastrad 
   1387   1.6  riastrad static void drm_dp_free_mst_port(struct kref *kref)
   1388   1.6  riastrad {
   1389   1.6  riastrad 	struct drm_dp_mst_port *port =
   1390   1.6  riastrad 		container_of(kref, struct drm_dp_mst_port, malloc_kref);
   1391   1.1  riastrad 
   1392   1.6  riastrad 	drm_dp_mst_put_mstb_malloc(port->parent);
   1393   1.6  riastrad 	kfree(port);
   1394   1.6  riastrad }
   1395   1.1  riastrad 
   1396   1.6  riastrad /**
   1397   1.6  riastrad  * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
   1398   1.6  riastrad  * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
   1399   1.6  riastrad  *
   1400   1.6  riastrad  * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
   1401   1.6  riastrad  * reaches 0, the memory allocation for @port will be released and @port may
   1402   1.6  riastrad  * no longer be used.
   1403   1.6  riastrad  *
   1404   1.6  riastrad  * Because @port could potentially be freed at any time by the DP MST helpers
   1405   1.6  riastrad  * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
   1406   1.6  riastrad  * function, drivers that which to make use of &struct drm_dp_mst_port should
   1407   1.6  riastrad  * ensure that they grab at least one main malloc reference to their MST ports
   1408   1.6  riastrad  * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
   1409   1.6  riastrad  * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
   1410   1.6  riastrad  *
   1411   1.6  riastrad  * See also: drm_dp_mst_put_port_malloc()
   1412   1.6  riastrad  */
   1413   1.6  riastrad void
   1414   1.6  riastrad drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
   1415   1.6  riastrad {
   1416   1.6  riastrad 	kref_get(&port->malloc_kref);
   1417   1.6  riastrad 	DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref));
   1418   1.1  riastrad }
   1419   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
   1420   1.1  riastrad 
   1421   1.6  riastrad /**
   1422   1.6  riastrad  * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
   1423   1.6  riastrad  * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
   1424   1.6  riastrad  *
   1425   1.6  riastrad  * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
   1426   1.6  riastrad  * reaches 0, the memory allocation for @port will be released and @port may
   1427   1.6  riastrad  * no longer be used.
   1428   1.6  riastrad  *
   1429   1.6  riastrad  * See also: drm_dp_mst_get_port_malloc()
   1430   1.6  riastrad  */
   1431   1.6  riastrad void
   1432   1.6  riastrad drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
   1433   1.1  riastrad {
   1434   1.6  riastrad 	DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
   1435   1.6  riastrad 	kref_put(&port->malloc_kref, drm_dp_free_mst_port);
   1436   1.1  riastrad }
   1437   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
   1438   1.1  riastrad 
   1439   1.6  riastrad #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
   1440   1.1  riastrad 
   1441   1.6  riastrad #define STACK_DEPTH 8
   1442   1.6  riastrad 
   1443   1.6  riastrad static noinline void
   1444   1.6  riastrad __topology_ref_save(struct drm_dp_mst_topology_mgr *mgr,
   1445   1.6  riastrad 		    struct drm_dp_mst_topology_ref_history *history,
   1446   1.6  riastrad 		    enum drm_dp_mst_topology_ref_type type)
   1447   1.1  riastrad {
   1448   1.6  riastrad 	struct drm_dp_mst_topology_ref_entry *entry = NULL;
   1449   1.6  riastrad 	depot_stack_handle_t backtrace;
   1450   1.6  riastrad 	ulong stack_entries[STACK_DEPTH];
   1451   1.6  riastrad 	uint n;
   1452   1.6  riastrad 	int i;
   1453   1.6  riastrad 
   1454   1.6  riastrad 	n = stack_trace_save(stack_entries, ARRAY_SIZE(stack_entries), 1);
   1455   1.6  riastrad 	backtrace = stack_depot_save(stack_entries, n, GFP_KERNEL);
   1456   1.6  riastrad 	if (!backtrace)
   1457   1.6  riastrad 		return;
   1458   1.1  riastrad 
   1459   1.6  riastrad 	/* Try to find an existing entry for this backtrace */
   1460   1.6  riastrad 	for (i = 0; i < history->len; i++) {
   1461   1.6  riastrad 		if (history->entries[i].backtrace == backtrace) {
   1462   1.6  riastrad 			entry = &history->entries[i];
   1463   1.6  riastrad 			break;
   1464   1.6  riastrad 		}
   1465   1.6  riastrad 	}
   1466   1.6  riastrad 
   1467   1.6  riastrad 	/* Otherwise add one */
   1468   1.6  riastrad 	if (!entry) {
   1469   1.6  riastrad 		struct drm_dp_mst_topology_ref_entry *new;
   1470   1.6  riastrad 		int new_len = history->len + 1;
   1471   1.6  riastrad 
   1472   1.6  riastrad 		new = krealloc(history->entries, sizeof(*new) * new_len,
   1473   1.6  riastrad 			       GFP_KERNEL);
   1474   1.6  riastrad 		if (!new)
   1475   1.6  riastrad 			return;
   1476   1.6  riastrad 
   1477   1.6  riastrad 		entry = &new[history->len];
   1478   1.6  riastrad 		history->len = new_len;
   1479   1.6  riastrad 		history->entries = new;
   1480   1.6  riastrad 
   1481   1.6  riastrad 		entry->backtrace = backtrace;
   1482   1.6  riastrad 		entry->type = type;
   1483   1.6  riastrad 		entry->count = 0;
   1484   1.6  riastrad 	}
   1485   1.6  riastrad 	entry->count++;
   1486   1.6  riastrad 	entry->ts_nsec = ktime_get_ns();
   1487   1.6  riastrad }
   1488   1.6  riastrad 
   1489   1.6  riastrad static int
   1490   1.6  riastrad topology_ref_history_cmp(const void *a, const void *b)
   1491   1.6  riastrad {
   1492   1.6  riastrad 	const struct drm_dp_mst_topology_ref_entry *entry_a = a, *entry_b = b;
   1493   1.6  riastrad 
   1494   1.6  riastrad 	if (entry_a->ts_nsec > entry_b->ts_nsec)
   1495   1.6  riastrad 		return 1;
   1496   1.6  riastrad 	else if (entry_a->ts_nsec < entry_b->ts_nsec)
   1497   1.6  riastrad 		return -1;
   1498   1.6  riastrad 	else
   1499   1.6  riastrad 		return 0;
   1500   1.6  riastrad }
   1501   1.6  riastrad 
   1502   1.6  riastrad static inline const char *
   1503   1.6  riastrad topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type)
   1504   1.6  riastrad {
   1505   1.6  riastrad 	if (type == DRM_DP_MST_TOPOLOGY_REF_GET)
   1506   1.6  riastrad 		return "get";
   1507   1.6  riastrad 	else
   1508   1.6  riastrad 		return "put";
   1509   1.6  riastrad }
   1510   1.6  riastrad 
   1511   1.6  riastrad static void
   1512   1.6  riastrad __dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
   1513   1.6  riastrad 			    void *ptr, const char *type_str)
   1514   1.6  riastrad {
   1515   1.6  riastrad 	struct drm_printer p = drm_debug_printer(DBG_PREFIX);
   1516   1.6  riastrad 	char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
   1517   1.6  riastrad 	int i;
   1518   1.6  riastrad 
   1519   1.6  riastrad 	if (!buf)
   1520   1.6  riastrad 		return;
   1521   1.6  riastrad 
   1522   1.6  riastrad 	if (!history->len)
   1523   1.6  riastrad 		goto out;
   1524   1.6  riastrad 
   1525   1.6  riastrad 	/* First, sort the list so that it goes from oldest to newest
   1526   1.6  riastrad 	 * reference entry
   1527   1.6  riastrad 	 */
   1528   1.6  riastrad 	sort(history->entries, history->len, sizeof(*history->entries),
   1529   1.6  riastrad 	     topology_ref_history_cmp, NULL);
   1530   1.6  riastrad 
   1531   1.6  riastrad 	drm_printf(&p, "%s (%p) topology count reached 0, dumping history:\n",
   1532   1.6  riastrad 		   type_str, ptr);
   1533   1.6  riastrad 
   1534   1.6  riastrad 	for (i = 0; i < history->len; i++) {
   1535   1.6  riastrad 		const struct drm_dp_mst_topology_ref_entry *entry =
   1536   1.6  riastrad 			&history->entries[i];
   1537   1.6  riastrad 		ulong *entries;
   1538   1.6  riastrad 		uint nr_entries;
   1539   1.6  riastrad 		u64 ts_nsec = entry->ts_nsec;
   1540   1.6  riastrad 		u32 rem_nsec = do_div(ts_nsec, 1000000000);
   1541   1.6  riastrad 
   1542   1.6  riastrad 		nr_entries = stack_depot_fetch(entry->backtrace, &entries);
   1543   1.6  riastrad 		stack_trace_snprint(buf, PAGE_SIZE, entries, nr_entries, 4);
   1544   1.6  riastrad 
   1545   1.6  riastrad 		drm_printf(&p, "  %d %ss (last at %5llu.%06u):\n%s",
   1546   1.6  riastrad 			   entry->count,
   1547   1.6  riastrad 			   topology_ref_type_to_str(entry->type),
   1548   1.6  riastrad 			   ts_nsec, rem_nsec / 1000, buf);
   1549   1.6  riastrad 	}
   1550   1.6  riastrad 
   1551   1.6  riastrad 	/* Now free the history, since this is the only time we expose it */
   1552   1.6  riastrad 	kfree(history->entries);
   1553   1.6  riastrad out:
   1554   1.6  riastrad 	kfree(buf);
   1555   1.6  riastrad }
   1556   1.6  riastrad 
   1557   1.6  riastrad static __always_inline void
   1558   1.6  riastrad drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)
   1559   1.6  riastrad {
   1560   1.6  riastrad 	__dump_topology_ref_history(&mstb->topology_ref_history, mstb,
   1561   1.6  riastrad 				    "MSTB");
   1562   1.6  riastrad }
   1563   1.6  riastrad 
   1564   1.6  riastrad static __always_inline void
   1565   1.6  riastrad drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port)
   1566   1.6  riastrad {
   1567   1.6  riastrad 	__dump_topology_ref_history(&port->topology_ref_history, port,
   1568   1.6  riastrad 				    "Port");
   1569   1.6  riastrad }
   1570   1.6  riastrad 
   1571   1.6  riastrad static __always_inline void
   1572   1.6  riastrad save_mstb_topology_ref(struct drm_dp_mst_branch *mstb,
   1573   1.6  riastrad 		       enum drm_dp_mst_topology_ref_type type)
   1574   1.6  riastrad {
   1575   1.6  riastrad 	__topology_ref_save(mstb->mgr, &mstb->topology_ref_history, type);
   1576   1.6  riastrad }
   1577   1.6  riastrad 
   1578   1.6  riastrad static __always_inline void
   1579   1.6  riastrad save_port_topology_ref(struct drm_dp_mst_port *port,
   1580   1.6  riastrad 		       enum drm_dp_mst_topology_ref_type type)
   1581   1.6  riastrad {
   1582   1.6  riastrad 	__topology_ref_save(port->mgr, &port->topology_ref_history, type);
   1583   1.6  riastrad }
   1584   1.6  riastrad 
   1585   1.6  riastrad static inline void
   1586   1.6  riastrad topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr)
   1587   1.6  riastrad {
   1588   1.6  riastrad 	mutex_lock(&mgr->topology_ref_history_lock);
   1589   1.6  riastrad }
   1590   1.6  riastrad 
   1591   1.6  riastrad static inline void
   1592   1.6  riastrad topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr)
   1593   1.6  riastrad {
   1594   1.6  riastrad 	mutex_unlock(&mgr->topology_ref_history_lock);
   1595   1.6  riastrad }
   1596   1.6  riastrad #else
   1597   1.6  riastrad static inline void
   1598   1.6  riastrad topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr) {}
   1599   1.6  riastrad static inline void
   1600   1.6  riastrad topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {}
   1601   1.6  riastrad static inline void
   1602   1.6  riastrad drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb) {}
   1603   1.6  riastrad static inline void
   1604   1.6  riastrad drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port) {}
   1605   1.6  riastrad #define save_mstb_topology_ref(mstb, type)
   1606   1.6  riastrad #define save_port_topology_ref(port, type)
   1607   1.6  riastrad #endif
   1608   1.6  riastrad 
   1609   1.6  riastrad static void drm_dp_destroy_mst_branch_device(struct kref *kref)
   1610   1.6  riastrad {
   1611   1.6  riastrad 	struct drm_dp_mst_branch *mstb =
   1612   1.6  riastrad 		container_of(kref, struct drm_dp_mst_branch, topology_kref);
   1613   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
   1614   1.6  riastrad 
   1615   1.6  riastrad 	drm_dp_mst_dump_mstb_topology_history(mstb);
   1616   1.6  riastrad 
   1617   1.6  riastrad 	INIT_LIST_HEAD(&mstb->destroy_next);
   1618   1.6  riastrad 
   1619   1.6  riastrad 	/*
   1620   1.6  riastrad 	 * This can get called under mgr->mutex, so we need to perform the
   1621   1.6  riastrad 	 * actual destruction of the mstb in another worker
   1622   1.6  riastrad 	 */
   1623   1.6  riastrad 	mutex_lock(&mgr->delayed_destroy_lock);
   1624   1.6  riastrad 	list_add(&mstb->destroy_next, &mgr->destroy_branch_device_list);
   1625   1.6  riastrad 	mutex_unlock(&mgr->delayed_destroy_lock);
   1626   1.6  riastrad 	schedule_work(&mgr->delayed_destroy_work);
   1627   1.6  riastrad }
   1628   1.6  riastrad 
   1629   1.6  riastrad /**
   1630   1.6  riastrad  * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
   1631   1.6  riastrad  * branch device unless it's zero
   1632   1.6  riastrad  * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
   1633   1.6  riastrad  *
   1634   1.6  riastrad  * Attempts to grab a topology reference to @mstb, if it hasn't yet been
   1635   1.6  riastrad  * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
   1636   1.6  riastrad  * reached 0). Holding a topology reference implies that a malloc reference
   1637   1.6  riastrad  * will be held to @mstb as long as the user holds the topology reference.
   1638   1.6  riastrad  *
   1639   1.6  riastrad  * Care should be taken to ensure that the user has at least one malloc
   1640   1.6  riastrad  * reference to @mstb. If you already have a topology reference to @mstb, you
   1641   1.6  riastrad  * should use drm_dp_mst_topology_get_mstb() instead.
   1642   1.6  riastrad  *
   1643   1.6  riastrad  * See also:
   1644   1.6  riastrad  * drm_dp_mst_topology_get_mstb()
   1645   1.6  riastrad  * drm_dp_mst_topology_put_mstb()
   1646   1.6  riastrad  *
   1647   1.6  riastrad  * Returns:
   1648   1.6  riastrad  * * 1: A topology reference was grabbed successfully
   1649   1.6  riastrad  * * 0: @port is no longer in the topology, no reference was grabbed
   1650   1.6  riastrad  */
   1651   1.6  riastrad static int __must_check
   1652   1.6  riastrad drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
   1653   1.6  riastrad {
   1654   1.6  riastrad 	int ret;
   1655   1.6  riastrad 
   1656   1.6  riastrad 	topology_ref_history_lock(mstb->mgr);
   1657   1.6  riastrad 	ret = kref_get_unless_zero(&mstb->topology_kref);
   1658   1.6  riastrad 	if (ret) {
   1659   1.6  riastrad 		DRM_DEBUG("mstb %p (%d)\n",
   1660   1.6  riastrad 			  mstb, kref_read(&mstb->topology_kref));
   1661   1.6  riastrad 		save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_GET);
   1662   1.1  riastrad 	}
   1663   1.6  riastrad 
   1664   1.6  riastrad 	topology_ref_history_unlock(mstb->mgr);
   1665   1.6  riastrad 
   1666   1.6  riastrad 	return ret;
   1667   1.6  riastrad }
   1668   1.6  riastrad 
   1669   1.6  riastrad /**
   1670   1.6  riastrad  * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
   1671   1.6  riastrad  * branch device
   1672   1.6  riastrad  * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
   1673   1.6  riastrad  *
   1674   1.6  riastrad  * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
   1675   1.6  riastrad  * not it's already reached 0. This is only valid to use in scenarios where
   1676   1.6  riastrad  * you are already guaranteed to have at least one active topology reference
   1677   1.6  riastrad  * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
   1678   1.6  riastrad  *
   1679   1.6  riastrad  * See also:
   1680   1.6  riastrad  * drm_dp_mst_topology_try_get_mstb()
   1681   1.6  riastrad  * drm_dp_mst_topology_put_mstb()
   1682   1.6  riastrad  */
   1683   1.6  riastrad static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
   1684   1.6  riastrad {
   1685   1.6  riastrad 	topology_ref_history_lock(mstb->mgr);
   1686   1.6  riastrad 
   1687   1.6  riastrad 	save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_GET);
   1688   1.6  riastrad 	WARN_ON(kref_read(&mstb->topology_kref) == 0);
   1689   1.6  riastrad 	kref_get(&mstb->topology_kref);
   1690   1.6  riastrad 	DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
   1691   1.6  riastrad 
   1692   1.6  riastrad 	topology_ref_history_unlock(mstb->mgr);
   1693   1.6  riastrad }
   1694   1.6  riastrad 
   1695   1.6  riastrad /**
   1696   1.6  riastrad  * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
   1697   1.6  riastrad  * device
   1698   1.6  riastrad  * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
   1699   1.6  riastrad  *
   1700   1.6  riastrad  * Releases a topology reference from @mstb by decrementing
   1701   1.6  riastrad  * &drm_dp_mst_branch.topology_kref.
   1702   1.6  riastrad  *
   1703   1.6  riastrad  * See also:
   1704   1.6  riastrad  * drm_dp_mst_topology_try_get_mstb()
   1705   1.6  riastrad  * drm_dp_mst_topology_get_mstb()
   1706   1.6  riastrad  */
   1707   1.6  riastrad static void
   1708   1.6  riastrad drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
   1709   1.6  riastrad {
   1710   1.6  riastrad 	topology_ref_history_lock(mstb->mgr);
   1711   1.6  riastrad 
   1712   1.6  riastrad 	DRM_DEBUG("mstb %p (%d)\n",
   1713   1.6  riastrad 		  mstb, kref_read(&mstb->topology_kref) - 1);
   1714   1.6  riastrad 	save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_PUT);
   1715   1.6  riastrad 
   1716   1.6  riastrad 	topology_ref_history_unlock(mstb->mgr);
   1717   1.6  riastrad 	kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
   1718   1.1  riastrad }
   1719   1.1  riastrad 
   1720   1.1  riastrad static void drm_dp_destroy_port(struct kref *kref)
   1721   1.1  riastrad {
   1722   1.6  riastrad 	struct drm_dp_mst_port *port =
   1723   1.6  riastrad 		container_of(kref, struct drm_dp_mst_port, topology_kref);
   1724   1.1  riastrad 	struct drm_dp_mst_topology_mgr *mgr = port->mgr;
   1725   1.1  riastrad 
   1726   1.6  riastrad 	drm_dp_mst_dump_port_topology_history(port);
   1727   1.1  riastrad 
   1728   1.6  riastrad 	/* There's nothing that needs locking to destroy an input port yet */
   1729   1.6  riastrad 	if (port->input) {
   1730   1.6  riastrad 		drm_dp_mst_put_port_malloc(port);
   1731   1.6  riastrad 		return;
   1732   1.6  riastrad 	}
   1733   1.6  riastrad 
   1734   1.6  riastrad 	kfree(port->cached_edid);
   1735   1.1  riastrad 
   1736   1.6  riastrad 	/*
   1737   1.6  riastrad 	 * we can't destroy the connector here, as we might be holding the
   1738   1.6  riastrad 	 * mode_config.mutex from an EDID retrieval
   1739   1.6  riastrad 	 */
   1740   1.6  riastrad 	mutex_lock(&mgr->delayed_destroy_lock);
   1741   1.6  riastrad 	list_add(&port->next, &mgr->destroy_port_list);
   1742   1.6  riastrad 	mutex_unlock(&mgr->delayed_destroy_lock);
   1743   1.6  riastrad 	schedule_work(&mgr->delayed_destroy_work);
   1744   1.6  riastrad }
   1745   1.6  riastrad 
   1746   1.6  riastrad /**
   1747   1.6  riastrad  * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
   1748   1.6  riastrad  * port unless it's zero
   1749   1.6  riastrad  * @port: &struct drm_dp_mst_port to increment the topology refcount of
   1750   1.6  riastrad  *
   1751   1.6  riastrad  * Attempts to grab a topology reference to @port, if it hasn't yet been
   1752   1.6  riastrad  * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
   1753   1.6  riastrad  * 0). Holding a topology reference implies that a malloc reference will be
   1754   1.6  riastrad  * held to @port as long as the user holds the topology reference.
   1755   1.6  riastrad  *
   1756   1.6  riastrad  * Care should be taken to ensure that the user has at least one malloc
   1757   1.6  riastrad  * reference to @port. If you already have a topology reference to @port, you
   1758   1.6  riastrad  * should use drm_dp_mst_topology_get_port() instead.
   1759   1.6  riastrad  *
   1760   1.6  riastrad  * See also:
   1761   1.6  riastrad  * drm_dp_mst_topology_get_port()
   1762   1.6  riastrad  * drm_dp_mst_topology_put_port()
   1763   1.6  riastrad  *
   1764   1.6  riastrad  * Returns:
   1765   1.6  riastrad  * * 1: A topology reference was grabbed successfully
   1766   1.6  riastrad  * * 0: @port is no longer in the topology, no reference was grabbed
   1767   1.6  riastrad  */
   1768   1.6  riastrad static int __must_check
   1769   1.6  riastrad drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
   1770   1.6  riastrad {
   1771   1.6  riastrad 	int ret;
   1772   1.6  riastrad 
   1773   1.6  riastrad 	topology_ref_history_lock(port->mgr);
   1774   1.6  riastrad 	ret = kref_get_unless_zero(&port->topology_kref);
   1775   1.6  riastrad 	if (ret) {
   1776   1.6  riastrad 		DRM_DEBUG("port %p (%d)\n",
   1777   1.6  riastrad 			  port, kref_read(&port->topology_kref));
   1778   1.6  riastrad 		save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_GET);
   1779   1.1  riastrad 	}
   1780   1.6  riastrad 
   1781   1.6  riastrad 	topology_ref_history_unlock(port->mgr);
   1782   1.6  riastrad 	return ret;
   1783   1.6  riastrad }
   1784   1.6  riastrad 
   1785   1.6  riastrad /**
   1786   1.6  riastrad  * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
   1787   1.6  riastrad  * @port: The &struct drm_dp_mst_port to increment the topology refcount of
   1788   1.6  riastrad  *
   1789   1.6  riastrad  * Increments &drm_dp_mst_port.topology_refcount without checking whether or
   1790   1.6  riastrad  * not it's already reached 0. This is only valid to use in scenarios where
   1791   1.6  riastrad  * you are already guaranteed to have at least one active topology reference
   1792   1.6  riastrad  * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
   1793   1.6  riastrad  *
   1794   1.6  riastrad  * See also:
   1795   1.6  riastrad  * drm_dp_mst_topology_try_get_port()
   1796   1.6  riastrad  * drm_dp_mst_topology_put_port()
   1797   1.6  riastrad  */
   1798   1.6  riastrad static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
   1799   1.6  riastrad {
   1800   1.6  riastrad 	topology_ref_history_lock(port->mgr);
   1801   1.6  riastrad 
   1802   1.6  riastrad 	WARN_ON(kref_read(&port->topology_kref) == 0);
   1803   1.6  riastrad 	kref_get(&port->topology_kref);
   1804   1.6  riastrad 	DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref));
   1805   1.6  riastrad 	save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_GET);
   1806   1.6  riastrad 
   1807   1.6  riastrad 	topology_ref_history_unlock(port->mgr);
   1808   1.1  riastrad }
   1809   1.1  riastrad 
   1810   1.6  riastrad /**
   1811   1.6  riastrad  * drm_dp_mst_topology_put_port() - release a topology reference to a port
   1812   1.6  riastrad  * @port: The &struct drm_dp_mst_port to release the topology reference from
   1813   1.6  riastrad  *
   1814   1.6  riastrad  * Releases a topology reference from @port by decrementing
   1815   1.6  riastrad  * &drm_dp_mst_port.topology_kref.
   1816   1.6  riastrad  *
   1817   1.6  riastrad  * See also:
   1818   1.6  riastrad  * drm_dp_mst_topology_try_get_port()
   1819   1.6  riastrad  * drm_dp_mst_topology_get_port()
   1820   1.6  riastrad  */
   1821   1.6  riastrad static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
   1822   1.1  riastrad {
   1823   1.6  riastrad 	topology_ref_history_lock(port->mgr);
   1824   1.6  riastrad 
   1825   1.6  riastrad 	DRM_DEBUG("port %p (%d)\n",
   1826   1.6  riastrad 		  port, kref_read(&port->topology_kref) - 1);
   1827   1.6  riastrad 	save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_PUT);
   1828   1.6  riastrad 
   1829   1.6  riastrad 	topology_ref_history_unlock(port->mgr);
   1830   1.6  riastrad 	kref_put(&port->topology_kref, drm_dp_destroy_port);
   1831   1.1  riastrad }
   1832   1.1  riastrad 
   1833   1.6  riastrad static struct drm_dp_mst_branch *
   1834   1.6  riastrad drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
   1835   1.6  riastrad 					      struct drm_dp_mst_branch *to_find)
   1836   1.1  riastrad {
   1837   1.1  riastrad 	struct drm_dp_mst_port *port;
   1838   1.1  riastrad 	struct drm_dp_mst_branch *rmstb;
   1839   1.6  riastrad 
   1840   1.6  riastrad 	if (to_find == mstb)
   1841   1.1  riastrad 		return mstb;
   1842   1.6  riastrad 
   1843   1.1  riastrad 	list_for_each_entry(port, &mstb->ports, next) {
   1844   1.1  riastrad 		if (port->mstb) {
   1845   1.6  riastrad 			rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
   1846   1.6  riastrad 			    port->mstb, to_find);
   1847   1.1  riastrad 			if (rmstb)
   1848   1.1  riastrad 				return rmstb;
   1849   1.1  riastrad 		}
   1850   1.1  riastrad 	}
   1851   1.1  riastrad 	return NULL;
   1852   1.1  riastrad }
   1853   1.1  riastrad 
   1854   1.6  riastrad static struct drm_dp_mst_branch *
   1855   1.6  riastrad drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
   1856   1.6  riastrad 				       struct drm_dp_mst_branch *mstb)
   1857   1.1  riastrad {
   1858   1.1  riastrad 	struct drm_dp_mst_branch *rmstb = NULL;
   1859   1.6  riastrad 
   1860   1.1  riastrad 	mutex_lock(&mgr->lock);
   1861   1.6  riastrad 	if (mgr->mst_primary) {
   1862   1.6  riastrad 		rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
   1863   1.6  riastrad 		    mgr->mst_primary, mstb);
   1864   1.6  riastrad 
   1865   1.6  riastrad 		if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
   1866   1.6  riastrad 			rmstb = NULL;
   1867   1.6  riastrad 	}
   1868   1.1  riastrad 	mutex_unlock(&mgr->lock);
   1869   1.1  riastrad 	return rmstb;
   1870   1.1  riastrad }
   1871   1.1  riastrad 
   1872   1.6  riastrad static struct drm_dp_mst_port *
   1873   1.6  riastrad drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
   1874   1.6  riastrad 					      struct drm_dp_mst_port *to_find)
   1875   1.1  riastrad {
   1876   1.1  riastrad 	struct drm_dp_mst_port *port, *mport;
   1877   1.1  riastrad 
   1878   1.1  riastrad 	list_for_each_entry(port, &mstb->ports, next) {
   1879   1.6  riastrad 		if (port == to_find)
   1880   1.1  riastrad 			return port;
   1881   1.6  riastrad 
   1882   1.1  riastrad 		if (port->mstb) {
   1883   1.6  riastrad 			mport = drm_dp_mst_topology_get_port_validated_locked(
   1884   1.6  riastrad 			    port->mstb, to_find);
   1885   1.1  riastrad 			if (mport)
   1886   1.1  riastrad 				return mport;
   1887   1.1  riastrad 		}
   1888   1.1  riastrad 	}
   1889   1.1  riastrad 	return NULL;
   1890   1.1  riastrad }
   1891   1.1  riastrad 
   1892   1.6  riastrad static struct drm_dp_mst_port *
   1893   1.6  riastrad drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
   1894   1.6  riastrad 				       struct drm_dp_mst_port *port)
   1895   1.1  riastrad {
   1896   1.1  riastrad 	struct drm_dp_mst_port *rport = NULL;
   1897   1.6  riastrad 
   1898   1.1  riastrad 	mutex_lock(&mgr->lock);
   1899   1.6  riastrad 	if (mgr->mst_primary) {
   1900   1.6  riastrad 		rport = drm_dp_mst_topology_get_port_validated_locked(
   1901   1.6  riastrad 		    mgr->mst_primary, port);
   1902   1.6  riastrad 
   1903   1.6  riastrad 		if (rport && !drm_dp_mst_topology_try_get_port(rport))
   1904   1.6  riastrad 			rport = NULL;
   1905   1.6  riastrad 	}
   1906   1.1  riastrad 	mutex_unlock(&mgr->lock);
   1907   1.1  riastrad 	return rport;
   1908   1.1  riastrad }
   1909   1.1  riastrad 
   1910   1.1  riastrad static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
   1911   1.1  riastrad {
   1912   1.1  riastrad 	struct drm_dp_mst_port *port;
   1913   1.6  riastrad 	int ret;
   1914   1.1  riastrad 
   1915   1.1  riastrad 	list_for_each_entry(port, &mstb->ports, next) {
   1916   1.1  riastrad 		if (port->port_num == port_num) {
   1917   1.6  riastrad 			ret = drm_dp_mst_topology_try_get_port(port);
   1918   1.6  riastrad 			return ret ? port : NULL;
   1919   1.1  riastrad 		}
   1920   1.1  riastrad 	}
   1921   1.1  riastrad 
   1922   1.1  riastrad 	return NULL;
   1923   1.1  riastrad }
   1924   1.1  riastrad 
   1925   1.1  riastrad /*
   1926   1.1  riastrad  * calculate a new RAD for this MST branch device
   1927   1.1  riastrad  * if parent has an LCT of 2 then it has 1 nibble of RAD,
   1928   1.1  riastrad  * if parent has an LCT of 3 then it has 2 nibbles of RAD,
   1929   1.1  riastrad  */
   1930   1.1  riastrad static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
   1931   1.1  riastrad 				 u8 *rad)
   1932   1.1  riastrad {
   1933   1.1  riastrad 	int parent_lct = port->parent->lct;
   1934   1.1  riastrad 	int shift = 4;
   1935   1.1  riastrad 	int idx = (parent_lct - 1) / 2;
   1936   1.1  riastrad 	if (parent_lct > 1) {
   1937   1.1  riastrad 		memcpy(rad, port->parent->rad, idx + 1);
   1938   1.1  riastrad 		shift = (parent_lct % 2) ? 4 : 0;
   1939   1.1  riastrad 	} else
   1940   1.1  riastrad 		rad[0] = 0;
   1941   1.1  riastrad 
   1942   1.1  riastrad 	rad[idx] |= port->port_num << shift;
   1943   1.1  riastrad 	return parent_lct + 1;
   1944   1.1  riastrad }
   1945   1.1  riastrad 
   1946   1.6  riastrad static bool drm_dp_mst_is_dp_mst_end_device(u8 pdt, bool mcs)
   1947   1.1  riastrad {
   1948   1.6  riastrad 	switch (pdt) {
   1949   1.1  riastrad 	case DP_PEER_DEVICE_DP_LEGACY_CONV:
   1950   1.1  riastrad 	case DP_PEER_DEVICE_SST_SINK:
   1951   1.6  riastrad 		return true;
   1952   1.1  riastrad 	case DP_PEER_DEVICE_MST_BRANCHING:
   1953   1.6  riastrad 		/* For sst branch device */
   1954   1.6  riastrad 		if (!mcs)
   1955   1.6  riastrad 			return true;
   1956   1.1  riastrad 
   1957   1.6  riastrad 		return false;
   1958   1.6  riastrad 	}
   1959   1.6  riastrad 	return true;
   1960   1.6  riastrad }
   1961   1.6  riastrad 
   1962   1.6  riastrad static int
   1963   1.6  riastrad drm_dp_port_set_pdt(struct drm_dp_mst_port *port, u8 new_pdt,
   1964   1.6  riastrad 		    bool new_mcs)
   1965   1.6  riastrad {
   1966   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr = port->mgr;
   1967   1.6  riastrad 	struct drm_dp_mst_branch *mstb;
   1968   1.6  riastrad 	u8 rad[8], lct;
   1969   1.6  riastrad 	int ret = 0;
   1970   1.1  riastrad 
   1971   1.6  riastrad 	if (port->pdt == new_pdt && port->mcs == new_mcs)
   1972   1.6  riastrad 		return 0;
   1973   1.6  riastrad 
   1974   1.6  riastrad 	/* Teardown the old pdt, if there is one */
   1975   1.6  riastrad 	if (port->pdt != DP_PEER_DEVICE_NONE) {
   1976   1.6  riastrad 		if (drm_dp_mst_is_dp_mst_end_device(port->pdt, port->mcs)) {
   1977   1.6  riastrad 			/*
   1978   1.6  riastrad 			 * If the new PDT would also have an i2c bus,
   1979   1.6  riastrad 			 * don't bother with reregistering it
   1980   1.6  riastrad 			 */
   1981   1.6  riastrad 			if (new_pdt != DP_PEER_DEVICE_NONE &&
   1982   1.6  riastrad 			    drm_dp_mst_is_dp_mst_end_device(new_pdt, new_mcs)) {
   1983   1.6  riastrad 				port->pdt = new_pdt;
   1984   1.6  riastrad 				port->mcs = new_mcs;
   1985   1.6  riastrad 				return 0;
   1986   1.6  riastrad 			}
   1987   1.6  riastrad 
   1988   1.6  riastrad 			/* remove i2c over sideband */
   1989   1.6  riastrad 			drm_dp_mst_unregister_i2c_bus(&port->aux);
   1990   1.6  riastrad 		} else {
   1991   1.6  riastrad 			mutex_lock(&mgr->lock);
   1992   1.6  riastrad 			drm_dp_mst_topology_put_mstb(port->mstb);
   1993   1.6  riastrad 			port->mstb = NULL;
   1994   1.6  riastrad 			mutex_unlock(&mgr->lock);
   1995   1.6  riastrad 		}
   1996   1.6  riastrad 	}
   1997   1.6  riastrad 
   1998   1.6  riastrad 	port->pdt = new_pdt;
   1999   1.6  riastrad 	port->mcs = new_mcs;
   2000   1.6  riastrad 
   2001   1.6  riastrad 	if (port->pdt != DP_PEER_DEVICE_NONE) {
   2002   1.6  riastrad 		if (drm_dp_mst_is_dp_mst_end_device(port->pdt, port->mcs)) {
   2003   1.6  riastrad 			/* add i2c over sideband */
   2004   1.6  riastrad 			ret = drm_dp_mst_register_i2c_bus(&port->aux);
   2005   1.6  riastrad 		} else {
   2006   1.6  riastrad 			lct = drm_dp_calculate_rad(port, rad);
   2007   1.6  riastrad 			mstb = drm_dp_add_mst_branch_device(lct, rad);
   2008   1.6  riastrad 			if (!mstb) {
   2009   1.6  riastrad 				ret = -ENOMEM;
   2010   1.6  riastrad 				DRM_ERROR("Failed to create MSTB for port %p",
   2011   1.6  riastrad 					  port);
   2012   1.6  riastrad 				goto out;
   2013   1.6  riastrad 			}
   2014   1.6  riastrad 
   2015   1.6  riastrad 			mutex_lock(&mgr->lock);
   2016   1.6  riastrad 			port->mstb = mstb;
   2017   1.6  riastrad 			mstb->mgr = port->mgr;
   2018   1.6  riastrad 			mstb->port_parent = port;
   2019   1.6  riastrad 
   2020   1.6  riastrad 			/*
   2021   1.6  riastrad 			 * Make sure this port's memory allocation stays
   2022   1.6  riastrad 			 * around until its child MSTB releases it
   2023   1.6  riastrad 			 */
   2024   1.6  riastrad 			drm_dp_mst_get_port_malloc(port);
   2025   1.6  riastrad 			mutex_unlock(&mgr->lock);
   2026   1.6  riastrad 
   2027   1.6  riastrad 			/* And make sure we send a link address for this */
   2028   1.6  riastrad 			ret = 1;
   2029   1.6  riastrad 		}
   2030   1.1  riastrad 	}
   2031   1.6  riastrad 
   2032   1.6  riastrad out:
   2033   1.6  riastrad 	if (ret < 0)
   2034   1.6  riastrad 		port->pdt = DP_PEER_DEVICE_NONE;
   2035   1.6  riastrad 	return ret;
   2036   1.6  riastrad }
   2037   1.6  riastrad 
   2038   1.6  riastrad /**
   2039   1.6  riastrad  * drm_dp_mst_dpcd_read() - read a series of bytes from the DPCD via sideband
   2040   1.6  riastrad  * @aux: Fake sideband AUX CH
   2041   1.6  riastrad  * @offset: address of the (first) register to read
   2042   1.6  riastrad  * @buffer: buffer to store the register values
   2043   1.6  riastrad  * @size: number of bytes in @buffer
   2044   1.6  riastrad  *
   2045   1.6  riastrad  * Performs the same functionality for remote devices via
   2046   1.6  riastrad  * sideband messaging as drm_dp_dpcd_read() does for local
   2047   1.6  riastrad  * devices via actual AUX CH.
   2048   1.6  riastrad  *
   2049   1.6  riastrad  * Return: Number of bytes read, or negative error code on failure.
   2050   1.6  riastrad  */
   2051   1.6  riastrad ssize_t drm_dp_mst_dpcd_read(struct drm_dp_aux *aux,
   2052   1.6  riastrad 			     unsigned int offset, void *buffer, size_t size)
   2053   1.6  riastrad {
   2054   1.6  riastrad 	struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
   2055   1.6  riastrad 						    aux);
   2056   1.6  riastrad 
   2057   1.6  riastrad 	return drm_dp_send_dpcd_read(port->mgr, port,
   2058   1.6  riastrad 				     offset, size, buffer);
   2059   1.6  riastrad }
   2060   1.6  riastrad 
   2061   1.6  riastrad /**
   2062   1.6  riastrad  * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband
   2063   1.6  riastrad  * @aux: Fake sideband AUX CH
   2064   1.6  riastrad  * @offset: address of the (first) register to write
   2065   1.6  riastrad  * @buffer: buffer containing the values to write
   2066   1.6  riastrad  * @size: number of bytes in @buffer
   2067   1.6  riastrad  *
   2068   1.6  riastrad  * Performs the same functionality for remote devices via
   2069   1.6  riastrad  * sideband messaging as drm_dp_dpcd_write() does for local
   2070   1.6  riastrad  * devices via actual AUX CH.
   2071   1.6  riastrad  *
   2072   1.6  riastrad  * Return: 0 on success, negative error code on failure.
   2073   1.6  riastrad  */
   2074   1.6  riastrad ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux,
   2075   1.6  riastrad 			      unsigned int offset, void *buffer, size_t size)
   2076   1.6  riastrad {
   2077   1.6  riastrad 	struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
   2078   1.6  riastrad 						    aux);
   2079   1.6  riastrad 
   2080   1.6  riastrad 	return drm_dp_send_dpcd_write(port->mgr, port,
   2081   1.6  riastrad 				      offset, size, buffer);
   2082   1.1  riastrad }
   2083   1.1  riastrad 
   2084   1.1  riastrad static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
   2085   1.1  riastrad {
   2086   1.3  riastrad 	int ret __unused;
   2087   1.1  riastrad 
   2088   1.1  riastrad 	memcpy(mstb->guid, guid, 16);
   2089   1.1  riastrad 
   2090   1.1  riastrad 	if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
   2091   1.1  riastrad 		if (mstb->port_parent) {
   2092   1.1  riastrad 			ret = drm_dp_send_dpcd_write(
   2093   1.1  riastrad 					mstb->mgr,
   2094   1.1  riastrad 					mstb->port_parent,
   2095   1.1  riastrad 					DP_GUID,
   2096   1.1  riastrad 					16,
   2097   1.1  riastrad 					mstb->guid);
   2098   1.1  riastrad 		} else {
   2099   1.1  riastrad 
   2100   1.1  riastrad 			ret = drm_dp_dpcd_write(
   2101   1.1  riastrad 					mstb->mgr->aux,
   2102   1.1  riastrad 					DP_GUID,
   2103   1.1  riastrad 					mstb->guid,
   2104   1.1  riastrad 					16);
   2105   1.1  riastrad 		}
   2106   1.1  riastrad 	}
   2107   1.1  riastrad }
   2108   1.1  riastrad 
   2109   1.1  riastrad static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
   2110   1.1  riastrad 				int pnum,
   2111   1.1  riastrad 				char *proppath,
   2112   1.1  riastrad 				size_t proppath_size)
   2113   1.1  riastrad {
   2114   1.1  riastrad 	int i;
   2115   1.1  riastrad 	char temp[8];
   2116   1.1  riastrad 	snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
   2117   1.1  riastrad 	for (i = 0; i < (mstb->lct - 1); i++) {
   2118   1.1  riastrad 		int shift = (i % 2) ? 0 : 4;
   2119   1.1  riastrad 		int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
   2120   1.1  riastrad 		snprintf(temp, sizeof(temp), "-%d", port_num);
   2121   1.1  riastrad 		strlcat(proppath, temp, proppath_size);
   2122   1.1  riastrad 	}
   2123   1.1  riastrad 	snprintf(temp, sizeof(temp), "-%d", pnum);
   2124   1.1  riastrad 	strlcat(proppath, temp, proppath_size);
   2125   1.1  riastrad }
   2126   1.1  riastrad 
   2127   1.6  riastrad /**
   2128   1.6  riastrad  * drm_dp_mst_connector_late_register() - Late MST connector registration
   2129   1.6  riastrad  * @connector: The MST connector
   2130   1.6  riastrad  * @port: The MST port for this connector
   2131   1.6  riastrad  *
   2132   1.6  riastrad  * Helper to register the remote aux device for this MST port. Drivers should
   2133   1.6  riastrad  * call this from their mst connector's late_register hook to enable MST aux
   2134   1.6  riastrad  * devices.
   2135   1.6  riastrad  *
   2136   1.6  riastrad  * Return: 0 on success, negative error code on failure.
   2137   1.6  riastrad  */
   2138   1.6  riastrad int drm_dp_mst_connector_late_register(struct drm_connector *connector,
   2139   1.6  riastrad 				       struct drm_dp_mst_port *port)
   2140   1.6  riastrad {
   2141   1.6  riastrad 	DRM_DEBUG_KMS("registering %s remote bus for %s\n",
   2142   1.9  riastrad 		      port->aux.name, device_xname(connector->dev->dev));
   2143   1.6  riastrad 
   2144   1.6  riastrad 	port->aux.dev = connector->kdev;
   2145   1.6  riastrad 	return drm_dp_aux_register_devnode(&port->aux);
   2146   1.6  riastrad }
   2147   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_connector_late_register);
   2148   1.6  riastrad 
   2149   1.6  riastrad /**
   2150   1.6  riastrad  * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration
   2151   1.6  riastrad  * @connector: The MST connector
   2152   1.6  riastrad  * @port: The MST port for this connector
   2153   1.6  riastrad  *
   2154   1.6  riastrad  * Helper to unregister the remote aux device for this MST port, registered by
   2155   1.6  riastrad  * drm_dp_mst_connector_late_register(). Drivers should call this from their mst
   2156   1.6  riastrad  * connector's early_unregister hook.
   2157   1.6  riastrad  */
   2158   1.6  riastrad void drm_dp_mst_connector_early_unregister(struct drm_connector *connector,
   2159   1.6  riastrad 					   struct drm_dp_mst_port *port)
   2160   1.6  riastrad {
   2161   1.6  riastrad 	DRM_DEBUG_KMS("unregistering %s remote bus for %s\n",
   2162   1.9  riastrad 		      port->aux.name, device_xname(connector->dev->dev));
   2163   1.6  riastrad 	drm_dp_aux_unregister_devnode(&port->aux);
   2164   1.6  riastrad }
   2165   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister);
   2166   1.6  riastrad 
   2167   1.6  riastrad static void
   2168   1.6  riastrad drm_dp_mst_port_add_connector(struct drm_dp_mst_branch *mstb,
   2169   1.6  riastrad 			      struct drm_dp_mst_port *port)
   2170   1.6  riastrad {
   2171   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr = port->mgr;
   2172   1.6  riastrad 	char proppath[255];
   2173   1.6  riastrad 	int ret;
   2174   1.6  riastrad 
   2175   1.6  riastrad 	build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
   2176   1.6  riastrad 	port->connector = mgr->cbs->add_connector(mgr, port, proppath);
   2177   1.6  riastrad 	if (!port->connector) {
   2178   1.6  riastrad 		ret = -ENOMEM;
   2179   1.6  riastrad 		goto error;
   2180   1.6  riastrad 	}
   2181   1.6  riastrad 
   2182   1.6  riastrad 	if (port->pdt != DP_PEER_DEVICE_NONE &&
   2183   1.6  riastrad 	    drm_dp_mst_is_dp_mst_end_device(port->pdt, port->mcs)) {
   2184   1.6  riastrad 		port->cached_edid = drm_get_edid(port->connector,
   2185   1.6  riastrad 						 &port->aux.ddc);
   2186   1.6  riastrad 		drm_connector_set_tile_property(port->connector);
   2187   1.6  riastrad 	}
   2188   1.6  riastrad 
   2189   1.6  riastrad 	mgr->cbs->register_connector(port->connector);
   2190   1.6  riastrad 	return;
   2191   1.6  riastrad 
   2192   1.6  riastrad error:
   2193   1.6  riastrad 	DRM_ERROR("Failed to create connector for port %p: %d\n", port, ret);
   2194   1.6  riastrad }
   2195   1.6  riastrad 
   2196   1.6  riastrad /*
   2197   1.6  riastrad  * Drop a topology reference, and unlink the port from the in-memory topology
   2198   1.6  riastrad  * layout
   2199   1.6  riastrad  */
   2200   1.6  riastrad static void
   2201   1.6  riastrad drm_dp_mst_topology_unlink_port(struct drm_dp_mst_topology_mgr *mgr,
   2202   1.6  riastrad 				struct drm_dp_mst_port *port)
   2203   1.1  riastrad {
   2204   1.6  riastrad 	mutex_lock(&mgr->lock);
   2205   1.6  riastrad 	port->parent->num_ports--;
   2206   1.6  riastrad 	list_del(&port->next);
   2207   1.6  riastrad 	mutex_unlock(&mgr->lock);
   2208   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   2209   1.6  riastrad }
   2210   1.6  riastrad 
   2211   1.6  riastrad static struct drm_dp_mst_port *
   2212   1.6  riastrad drm_dp_mst_add_port(struct drm_device *dev,
   2213   1.6  riastrad 		    struct drm_dp_mst_topology_mgr *mgr,
   2214   1.6  riastrad 		    struct drm_dp_mst_branch *mstb, u8 port_number)
   2215   1.6  riastrad {
   2216   1.6  riastrad 	struct drm_dp_mst_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
   2217   1.6  riastrad 
   2218   1.6  riastrad 	if (!port)
   2219   1.6  riastrad 		return NULL;
   2220   1.6  riastrad 
   2221   1.6  riastrad 	kref_init(&port->topology_kref);
   2222   1.6  riastrad 	kref_init(&port->malloc_kref);
   2223   1.6  riastrad 	port->parent = mstb;
   2224   1.6  riastrad 	port->port_num = port_number;
   2225   1.6  riastrad 	port->mgr = mgr;
   2226   1.6  riastrad 	port->aux.name = "DPMST";
   2227   1.6  riastrad 	port->aux.dev = dev->dev;
   2228   1.6  riastrad 	port->aux.is_remote = true;
   2229   1.6  riastrad 
   2230   1.6  riastrad 	/* initialize the MST downstream port's AUX crc work queue */
   2231   1.6  riastrad 	drm_dp_remote_aux_init(&port->aux);
   2232   1.6  riastrad 
   2233   1.6  riastrad 	/*
   2234   1.6  riastrad 	 * Make sure the memory allocation for our parent branch stays
   2235   1.6  riastrad 	 * around until our own memory allocation is released
   2236   1.6  riastrad 	 */
   2237   1.6  riastrad 	drm_dp_mst_get_mstb_malloc(mstb);
   2238   1.6  riastrad 
   2239   1.6  riastrad 	return port;
   2240   1.6  riastrad }
   2241   1.6  riastrad 
   2242   1.6  riastrad static int
   2243   1.6  riastrad drm_dp_mst_handle_link_address_port(struct drm_dp_mst_branch *mstb,
   2244   1.6  riastrad 				    struct drm_device *dev,
   2245   1.6  riastrad 				    struct drm_dp_link_addr_reply_port *port_msg)
   2246   1.6  riastrad {
   2247   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
   2248   1.1  riastrad 	struct drm_dp_mst_port *port;
   2249   1.6  riastrad 	int old_ddps = 0, ret;
   2250   1.6  riastrad 	u8 new_pdt = DP_PEER_DEVICE_NONE;
   2251   1.6  riastrad 	bool new_mcs = 0;
   2252   1.6  riastrad 	bool created = false, send_link_addr = false, changed = false;
   2253   1.6  riastrad 
   2254   1.1  riastrad 	port = drm_dp_get_port(mstb, port_msg->port_number);
   2255   1.1  riastrad 	if (!port) {
   2256   1.6  riastrad 		port = drm_dp_mst_add_port(dev, mgr, mstb,
   2257   1.6  riastrad 					   port_msg->port_number);
   2258   1.1  riastrad 		if (!port)
   2259   1.6  riastrad 			return -ENOMEM;
   2260   1.6  riastrad 		created = true;
   2261   1.6  riastrad 		changed = true;
   2262   1.6  riastrad 	} else if (!port->input && port_msg->input_port && port->connector) {
   2263   1.6  riastrad 		/* Since port->connector can't be changed here, we create a
   2264   1.6  riastrad 		 * new port if input_port changes from 0 to 1
   2265   1.6  riastrad 		 */
   2266   1.6  riastrad 		drm_dp_mst_topology_unlink_port(mgr, port);
   2267   1.6  riastrad 		drm_dp_mst_topology_put_port(port);
   2268   1.6  riastrad 		port = drm_dp_mst_add_port(dev, mgr, mstb,
   2269   1.6  riastrad 					   port_msg->port_number);
   2270   1.6  riastrad 		if (!port)
   2271   1.6  riastrad 			return -ENOMEM;
   2272   1.6  riastrad 		changed = true;
   2273   1.1  riastrad 		created = true;
   2274   1.6  riastrad 	} else if (port->input && !port_msg->input_port) {
   2275   1.6  riastrad 		changed = true;
   2276   1.6  riastrad 	} else if (port->connector) {
   2277   1.6  riastrad 		/* We're updating a port that's exposed to userspace, so do it
   2278   1.6  riastrad 		 * under lock
   2279   1.6  riastrad 		 */
   2280   1.6  riastrad 		drm_modeset_lock(&mgr->base.lock, NULL);
   2281   1.6  riastrad 
   2282   1.1  riastrad 		old_ddps = port->ddps;
   2283   1.6  riastrad 		changed = port->ddps != port_msg->ddps ||
   2284   1.6  riastrad 			(port->ddps &&
   2285   1.6  riastrad 			 (port->ldps != port_msg->legacy_device_plug_status ||
   2286   1.6  riastrad 			  port->dpcd_rev != port_msg->dpcd_revision ||
   2287   1.6  riastrad 			  port->mcs != port_msg->mcs ||
   2288   1.6  riastrad 			  port->pdt != port_msg->peer_device_type ||
   2289   1.6  riastrad 			  port->num_sdp_stream_sinks !=
   2290   1.6  riastrad 			  port_msg->num_sdp_stream_sinks));
   2291   1.1  riastrad 	}
   2292   1.1  riastrad 
   2293   1.1  riastrad 	port->input = port_msg->input_port;
   2294   1.6  riastrad 	if (!port->input)
   2295   1.6  riastrad 		new_pdt = port_msg->peer_device_type;
   2296   1.6  riastrad 	new_mcs = port_msg->mcs;
   2297   1.1  riastrad 	port->ddps = port_msg->ddps;
   2298   1.1  riastrad 	port->ldps = port_msg->legacy_device_plug_status;
   2299   1.1  riastrad 	port->dpcd_rev = port_msg->dpcd_revision;
   2300   1.1  riastrad 	port->num_sdp_streams = port_msg->num_sdp_streams;
   2301   1.1  riastrad 	port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
   2302   1.1  riastrad 
   2303   1.1  riastrad 	/* manage mstb port lists with mgr lock - take a reference
   2304   1.1  riastrad 	   for this list */
   2305   1.1  riastrad 	if (created) {
   2306   1.6  riastrad 		mutex_lock(&mgr->lock);
   2307   1.6  riastrad 		drm_dp_mst_topology_get_port(port);
   2308   1.1  riastrad 		list_add(&port->next, &mstb->ports);
   2309   1.6  riastrad 		mstb->num_ports++;
   2310   1.6  riastrad 		mutex_unlock(&mgr->lock);
   2311   1.1  riastrad 	}
   2312   1.1  riastrad 
   2313   1.1  riastrad 	if (old_ddps != port->ddps) {
   2314   1.1  riastrad 		if (port->ddps) {
   2315   1.6  riastrad 			if (!port->input) {
   2316   1.6  riastrad 				drm_dp_send_enum_path_resources(mgr, mstb,
   2317   1.6  riastrad 								port);
   2318   1.6  riastrad 			}
   2319   1.1  riastrad 		} else {
   2320   1.1  riastrad 			port->available_pbn = 0;
   2321   1.6  riastrad 		}
   2322   1.1  riastrad 	}
   2323   1.1  riastrad 
   2324   1.6  riastrad 	ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
   2325   1.6  riastrad 	if (ret == 1) {
   2326   1.6  riastrad 		send_link_addr = true;
   2327   1.6  riastrad 	} else if (ret < 0) {
   2328   1.6  riastrad 		DRM_ERROR("Failed to change PDT on port %p: %d\n",
   2329   1.6  riastrad 			  port, ret);
   2330   1.6  riastrad 		goto fail;
   2331   1.6  riastrad 	}
   2332   1.1  riastrad 
   2333   1.6  riastrad 	/*
   2334   1.6  riastrad 	 * If this port wasn't just created, then we're reprobing because
   2335   1.6  riastrad 	 * we're coming out of suspend. In this case, always resend the link
   2336   1.6  riastrad 	 * address if there's an MSTB on this port
   2337   1.6  riastrad 	 */
   2338   1.6  riastrad 	if (!created && port->pdt == DP_PEER_DEVICE_MST_BRANCHING &&
   2339   1.6  riastrad 	    port->mcs)
   2340   1.6  riastrad 		send_link_addr = true;
   2341   1.6  riastrad 
   2342   1.6  riastrad 	if (port->connector)
   2343   1.6  riastrad 		drm_modeset_unlock(&mgr->base.lock);
   2344   1.6  riastrad 	else if (!port->input)
   2345   1.6  riastrad 		drm_dp_mst_port_add_connector(mstb, port);
   2346   1.6  riastrad 
   2347   1.6  riastrad 	if (send_link_addr && port->mstb) {
   2348   1.6  riastrad 		ret = drm_dp_send_link_address(mgr, port->mstb);
   2349   1.6  riastrad 		if (ret == 1) /* MSTB below us changed */
   2350   1.6  riastrad 			changed = true;
   2351   1.6  riastrad 		else if (ret < 0)
   2352   1.6  riastrad 			goto fail_put;
   2353   1.1  riastrad 	}
   2354   1.1  riastrad 
   2355   1.1  riastrad 	/* put reference to this port */
   2356   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   2357   1.6  riastrad 	return changed;
   2358   1.6  riastrad 
   2359   1.6  riastrad fail:
   2360   1.6  riastrad 	drm_dp_mst_topology_unlink_port(mgr, port);
   2361   1.6  riastrad 	if (port->connector)
   2362   1.6  riastrad 		drm_modeset_unlock(&mgr->base.lock);
   2363   1.6  riastrad fail_put:
   2364   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   2365   1.6  riastrad 	return ret;
   2366   1.1  riastrad }
   2367   1.1  riastrad 
   2368   1.6  riastrad static void
   2369   1.6  riastrad drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch *mstb,
   2370   1.6  riastrad 			    struct drm_dp_connection_status_notify *conn_stat)
   2371   1.1  riastrad {
   2372   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
   2373   1.1  riastrad 	struct drm_dp_mst_port *port;
   2374   1.6  riastrad 	int old_ddps, old_input, ret, i;
   2375   1.6  riastrad 	u8 new_pdt;
   2376   1.6  riastrad 	bool new_mcs;
   2377   1.6  riastrad 	bool dowork = false, create_connector = false;
   2378   1.6  riastrad 
   2379   1.1  riastrad 	port = drm_dp_get_port(mstb, conn_stat->port_number);
   2380   1.1  riastrad 	if (!port)
   2381   1.1  riastrad 		return;
   2382   1.1  riastrad 
   2383   1.6  riastrad 	if (port->connector) {
   2384   1.6  riastrad 		if (!port->input && conn_stat->input_port) {
   2385   1.6  riastrad 			/*
   2386   1.6  riastrad 			 * We can't remove a connector from an already exposed
   2387   1.6  riastrad 			 * port, so just throw the port out and make sure we
   2388   1.6  riastrad 			 * reprobe the link address of it's parent MSTB
   2389   1.6  riastrad 			 */
   2390   1.6  riastrad 			drm_dp_mst_topology_unlink_port(mgr, port);
   2391   1.6  riastrad 			mstb->link_address_sent = false;
   2392   1.6  riastrad 			dowork = true;
   2393   1.6  riastrad 			goto out;
   2394   1.6  riastrad 		}
   2395   1.6  riastrad 
   2396   1.6  riastrad 		/* Locking is only needed if the port's exposed to userspace */
   2397   1.6  riastrad 		drm_modeset_lock(&mgr->base.lock, NULL);
   2398   1.6  riastrad 	} else if (port->input && !conn_stat->input_port) {
   2399   1.6  riastrad 		create_connector = true;
   2400   1.6  riastrad 		/* Reprobe link address so we get num_sdp_streams */
   2401   1.6  riastrad 		mstb->link_address_sent = false;
   2402   1.6  riastrad 		dowork = true;
   2403   1.6  riastrad 	}
   2404   1.6  riastrad 
   2405   1.1  riastrad 	old_ddps = port->ddps;
   2406   1.6  riastrad 	old_input = port->input;
   2407   1.6  riastrad 	port->input = conn_stat->input_port;
   2408   1.1  riastrad 	port->ldps = conn_stat->legacy_device_plug_status;
   2409   1.1  riastrad 	port->ddps = conn_stat->displayport_device_plug_status;
   2410   1.1  riastrad 
   2411   1.1  riastrad 	if (old_ddps != port->ddps) {
   2412   1.1  riastrad 		if (port->ddps) {
   2413   1.1  riastrad 			dowork = true;
   2414   1.1  riastrad 		} else {
   2415   1.1  riastrad 			port->available_pbn = 0;
   2416   1.1  riastrad 		}
   2417   1.1  riastrad 	}
   2418   1.1  riastrad 
   2419   1.6  riastrad 	new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat->peer_device_type;
   2420   1.6  riastrad 	new_mcs = conn_stat->message_capability_status;
   2421   1.6  riastrad 	ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
   2422   1.6  riastrad 	if (ret == 1) {
   2423   1.6  riastrad 		dowork = true;
   2424   1.6  riastrad 	} else if (ret < 0) {
   2425   1.6  riastrad 		DRM_ERROR("Failed to change PDT for port %p: %d\n",
   2426   1.6  riastrad 			  port, ret);
   2427   1.6  riastrad 		dowork = false;
   2428   1.6  riastrad 	}
   2429   1.6  riastrad 
   2430   1.6  riastrad 	if (!old_input && old_ddps != port->ddps && !port->ddps) {
   2431   1.6  riastrad 		for (i = 0; i < mgr->max_payloads; i++) {
   2432   1.6  riastrad 			struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
   2433   1.6  riastrad 			struct drm_dp_mst_port *port_validated;
   2434   1.6  riastrad 
   2435   1.6  riastrad 			if (!vcpi)
   2436   1.6  riastrad 				continue;
   2437   1.6  riastrad 
   2438   1.6  riastrad 			port_validated =
   2439   1.6  riastrad 				container_of(vcpi, struct drm_dp_mst_port, vcpi);
   2440   1.6  riastrad 			port_validated =
   2441   1.6  riastrad 				drm_dp_mst_topology_get_port_validated(mgr, port_validated);
   2442   1.6  riastrad 			if (!port_validated) {
   2443   1.6  riastrad 				mutex_lock(&mgr->payload_lock);
   2444   1.6  riastrad 				vcpi->num_slots = 0;
   2445   1.6  riastrad 				mutex_unlock(&mgr->payload_lock);
   2446   1.6  riastrad 			} else {
   2447   1.6  riastrad 				drm_dp_mst_topology_put_port(port_validated);
   2448   1.6  riastrad 			}
   2449   1.6  riastrad 		}
   2450   1.1  riastrad 	}
   2451   1.1  riastrad 
   2452   1.6  riastrad 	if (port->connector)
   2453   1.6  riastrad 		drm_modeset_unlock(&mgr->base.lock);
   2454   1.6  riastrad 	else if (create_connector)
   2455   1.6  riastrad 		drm_dp_mst_port_add_connector(mstb, port);
   2456   1.6  riastrad 
   2457   1.6  riastrad out:
   2458   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   2459   1.1  riastrad 	if (dowork)
   2460   1.1  riastrad 		queue_work(system_long_wq, &mstb->mgr->work);
   2461   1.1  riastrad }
   2462   1.1  riastrad 
   2463   1.1  riastrad static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
   2464   1.1  riastrad 							       u8 lct, u8 *rad)
   2465   1.1  riastrad {
   2466   1.1  riastrad 	struct drm_dp_mst_branch *mstb;
   2467   1.1  riastrad 	struct drm_dp_mst_port *port;
   2468   1.6  riastrad 	int i, ret;
   2469   1.1  riastrad 	/* find the port by iterating down */
   2470   1.1  riastrad 
   2471   1.1  riastrad 	mutex_lock(&mgr->lock);
   2472   1.1  riastrad 	mstb = mgr->mst_primary;
   2473   1.1  riastrad 
   2474   1.6  riastrad 	if (!mstb)
   2475   1.6  riastrad 		goto out;
   2476   1.6  riastrad 
   2477   1.1  riastrad 	for (i = 0; i < lct - 1; i++) {
   2478   1.1  riastrad 		int shift = (i % 2) ? 0 : 4;
   2479   1.1  riastrad 		int port_num = (rad[i / 2] >> shift) & 0xf;
   2480   1.1  riastrad 
   2481   1.1  riastrad 		list_for_each_entry(port, &mstb->ports, next) {
   2482   1.1  riastrad 			if (port->port_num == port_num) {
   2483   1.1  riastrad 				mstb = port->mstb;
   2484   1.1  riastrad 				if (!mstb) {
   2485   1.1  riastrad 					DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
   2486   1.1  riastrad 					goto out;
   2487   1.1  riastrad 				}
   2488   1.1  riastrad 
   2489   1.1  riastrad 				break;
   2490   1.1  riastrad 			}
   2491   1.1  riastrad 		}
   2492   1.1  riastrad 	}
   2493   1.6  riastrad 	ret = drm_dp_mst_topology_try_get_mstb(mstb);
   2494   1.6  riastrad 	if (!ret)
   2495   1.6  riastrad 		mstb = NULL;
   2496   1.1  riastrad out:
   2497   1.1  riastrad 	mutex_unlock(&mgr->lock);
   2498   1.1  riastrad 	return mstb;
   2499   1.1  riastrad }
   2500   1.1  riastrad 
   2501   1.1  riastrad static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
   2502   1.1  riastrad 	struct drm_dp_mst_branch *mstb,
   2503   1.6  riastrad 	const uint8_t *guid)
   2504   1.1  riastrad {
   2505   1.1  riastrad 	struct drm_dp_mst_branch *found_mstb;
   2506   1.1  riastrad 	struct drm_dp_mst_port *port;
   2507   1.1  riastrad 
   2508   1.1  riastrad 	if (memcmp(mstb->guid, guid, 16) == 0)
   2509   1.1  riastrad 		return mstb;
   2510   1.1  riastrad 
   2511   1.1  riastrad 
   2512   1.1  riastrad 	list_for_each_entry(port, &mstb->ports, next) {
   2513   1.1  riastrad 		if (!port->mstb)
   2514   1.1  riastrad 			continue;
   2515   1.1  riastrad 
   2516   1.1  riastrad 		found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
   2517   1.1  riastrad 
   2518   1.1  riastrad 		if (found_mstb)
   2519   1.1  riastrad 			return found_mstb;
   2520   1.1  riastrad 	}
   2521   1.1  riastrad 
   2522   1.1  riastrad 	return NULL;
   2523   1.1  riastrad }
   2524   1.1  riastrad 
   2525   1.6  riastrad static struct drm_dp_mst_branch *
   2526   1.6  riastrad drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
   2527   1.6  riastrad 				     const uint8_t *guid)
   2528   1.1  riastrad {
   2529   1.1  riastrad 	struct drm_dp_mst_branch *mstb;
   2530   1.6  riastrad 	int ret;
   2531   1.1  riastrad 
   2532   1.1  riastrad 	/* find the port by iterating down */
   2533   1.1  riastrad 	mutex_lock(&mgr->lock);
   2534   1.1  riastrad 
   2535   1.1  riastrad 	mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
   2536   1.6  riastrad 	if (mstb) {
   2537   1.6  riastrad 		ret = drm_dp_mst_topology_try_get_mstb(mstb);
   2538   1.6  riastrad 		if (!ret)
   2539   1.6  riastrad 			mstb = NULL;
   2540   1.6  riastrad 	}
   2541   1.1  riastrad 
   2542   1.1  riastrad 	mutex_unlock(&mgr->lock);
   2543   1.1  riastrad 	return mstb;
   2544   1.1  riastrad }
   2545   1.1  riastrad 
   2546   1.6  riastrad static int drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
   2547   1.1  riastrad 					       struct drm_dp_mst_branch *mstb)
   2548   1.1  riastrad {
   2549   1.1  riastrad 	struct drm_dp_mst_port *port;
   2550   1.6  riastrad 	int ret;
   2551   1.6  riastrad 	bool changed = false;
   2552   1.6  riastrad 
   2553   1.6  riastrad 	if (!mstb->link_address_sent) {
   2554   1.6  riastrad 		ret = drm_dp_send_link_address(mgr, mstb);
   2555   1.6  riastrad 		if (ret == 1)
   2556   1.6  riastrad 			changed = true;
   2557   1.6  riastrad 		else if (ret < 0)
   2558   1.6  riastrad 			return ret;
   2559   1.6  riastrad 	}
   2560   1.1  riastrad 
   2561   1.1  riastrad 	list_for_each_entry(port, &mstb->ports, next) {
   2562   1.6  riastrad 		struct drm_dp_mst_branch *mstb_child = NULL;
   2563   1.1  riastrad 
   2564   1.6  riastrad 		if (port->input || !port->ddps)
   2565   1.1  riastrad 			continue;
   2566   1.1  riastrad 
   2567   1.6  riastrad 		if (!port->available_pbn) {
   2568   1.6  riastrad 			drm_modeset_lock(&mgr->base.lock, NULL);
   2569   1.1  riastrad 			drm_dp_send_enum_path_resources(mgr, mstb, port);
   2570   1.6  riastrad 			drm_modeset_unlock(&mgr->base.lock);
   2571   1.6  riastrad 			changed = true;
   2572   1.6  riastrad 		}
   2573   1.1  riastrad 
   2574   1.6  riastrad 		if (port->mstb)
   2575   1.6  riastrad 			mstb_child = drm_dp_mst_topology_get_mstb_validated(
   2576   1.6  riastrad 			    mgr, port->mstb);
   2577   1.6  riastrad 
   2578   1.6  riastrad 		if (mstb_child) {
   2579   1.6  riastrad 			ret = drm_dp_check_and_send_link_address(mgr,
   2580   1.6  riastrad 								 mstb_child);
   2581   1.6  riastrad 			drm_dp_mst_topology_put_mstb(mstb_child);
   2582   1.6  riastrad 			if (ret == 1)
   2583   1.6  riastrad 				changed = true;
   2584   1.6  riastrad 			else if (ret < 0)
   2585   1.6  riastrad 				return ret;
   2586   1.1  riastrad 		}
   2587   1.1  riastrad 	}
   2588   1.6  riastrad 
   2589   1.6  riastrad 	return changed;
   2590   1.1  riastrad }
   2591   1.1  riastrad 
   2592   1.1  riastrad static void drm_dp_mst_link_probe_work(struct work_struct *work)
   2593   1.1  riastrad {
   2594   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr =
   2595   1.6  riastrad 		container_of(work, struct drm_dp_mst_topology_mgr, work);
   2596   1.6  riastrad 	struct drm_device *dev = mgr->dev;
   2597   1.1  riastrad 	struct drm_dp_mst_branch *mstb;
   2598   1.6  riastrad 	int ret;
   2599   1.6  riastrad 	bool clear_payload_id_table;
   2600   1.6  riastrad 
   2601   1.6  riastrad 	mutex_lock(&mgr->probe_lock);
   2602   1.1  riastrad 
   2603   1.1  riastrad 	mutex_lock(&mgr->lock);
   2604   1.6  riastrad 	clear_payload_id_table = !mgr->payload_id_table_cleared;
   2605   1.6  riastrad 	mgr->payload_id_table_cleared = true;
   2606   1.6  riastrad 
   2607   1.1  riastrad 	mstb = mgr->mst_primary;
   2608   1.1  riastrad 	if (mstb) {
   2609   1.6  riastrad 		ret = drm_dp_mst_topology_try_get_mstb(mstb);
   2610   1.6  riastrad 		if (!ret)
   2611   1.6  riastrad 			mstb = NULL;
   2612   1.1  riastrad 	}
   2613   1.1  riastrad 	mutex_unlock(&mgr->lock);
   2614   1.6  riastrad 	if (!mstb) {
   2615   1.6  riastrad 		mutex_unlock(&mgr->probe_lock);
   2616   1.6  riastrad 		return;
   2617   1.6  riastrad 	}
   2618   1.6  riastrad 
   2619   1.6  riastrad 	/*
   2620   1.6  riastrad 	 * Certain branch devices seem to incorrectly report an available_pbn
   2621   1.6  riastrad 	 * of 0 on downstream sinks, even after clearing the
   2622   1.6  riastrad 	 * DP_PAYLOAD_ALLOCATE_* registers in
   2623   1.6  riastrad 	 * drm_dp_mst_topology_mgr_set_mst(). Namely, the CableMatters USB-C
   2624   1.6  riastrad 	 * 2x DP hub. Sending a CLEAR_PAYLOAD_ID_TABLE message seems to make
   2625   1.6  riastrad 	 * things work again.
   2626   1.6  riastrad 	 */
   2627   1.6  riastrad 	if (clear_payload_id_table) {
   2628   1.6  riastrad 		DRM_DEBUG_KMS("Clearing payload ID table\n");
   2629   1.6  riastrad 		drm_dp_send_clear_payload_id_table(mgr, mstb);
   2630   1.1  riastrad 	}
   2631   1.6  riastrad 
   2632   1.6  riastrad 	ret = drm_dp_check_and_send_link_address(mgr, mstb);
   2633   1.6  riastrad 	drm_dp_mst_topology_put_mstb(mstb);
   2634   1.6  riastrad 
   2635   1.6  riastrad 	mutex_unlock(&mgr->probe_lock);
   2636   1.6  riastrad 	if (ret)
   2637   1.6  riastrad 		drm_kms_helper_hotplug_event(dev);
   2638   1.1  riastrad }
   2639   1.1  riastrad 
   2640   1.1  riastrad static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
   2641   1.1  riastrad 				 u8 *guid)
   2642   1.1  riastrad {
   2643   1.6  riastrad 	u64 salt;
   2644   1.6  riastrad 
   2645   1.6  riastrad 	if (memchr_inv(guid, 0, 16))
   2646   1.6  riastrad 		return true;
   2647   1.1  riastrad 
   2648   1.6  riastrad 	salt = get_jiffies_64();
   2649   1.6  riastrad 
   2650   1.6  riastrad 	memcpy(&guid[0], &salt, sizeof(u64));
   2651   1.6  riastrad 	memcpy(&guid[8], &salt, sizeof(u64));
   2652   1.6  riastrad 
   2653   1.6  riastrad 	return false;
   2654   1.1  riastrad }
   2655   1.1  riastrad 
   2656   1.1  riastrad static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
   2657   1.1  riastrad {
   2658   1.1  riastrad 	struct drm_dp_sideband_msg_req_body req;
   2659   1.1  riastrad 
   2660   1.1  riastrad 	req.req_type = DP_REMOTE_DPCD_READ;
   2661   1.1  riastrad 	req.u.dpcd_read.port_number = port_num;
   2662   1.1  riastrad 	req.u.dpcd_read.dpcd_address = offset;
   2663   1.1  riastrad 	req.u.dpcd_read.num_bytes = num_bytes;
   2664   1.1  riastrad 	drm_dp_encode_sideband_req(&req, msg);
   2665   1.1  riastrad 
   2666   1.1  riastrad 	return 0;
   2667   1.1  riastrad }
   2668   1.1  riastrad 
   2669   1.1  riastrad static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
   2670   1.1  riastrad 				    bool up, u8 *msg, int len)
   2671   1.1  riastrad {
   2672   1.1  riastrad 	int ret;
   2673   1.1  riastrad 	int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
   2674   1.1  riastrad 	int tosend, total, offset;
   2675   1.1  riastrad 	int retries = 0;
   2676   1.1  riastrad 
   2677   1.1  riastrad retry:
   2678   1.1  riastrad 	total = len;
   2679   1.1  riastrad 	offset = 0;
   2680   1.1  riastrad 	do {
   2681   1.1  riastrad 		tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
   2682   1.1  riastrad 
   2683   1.1  riastrad 		ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
   2684   1.1  riastrad 					&msg[offset],
   2685   1.1  riastrad 					tosend);
   2686   1.1  riastrad 		if (ret != tosend) {
   2687   1.1  riastrad 			if (ret == -EIO && retries < 5) {
   2688   1.1  riastrad 				retries++;
   2689   1.1  riastrad 				goto retry;
   2690   1.1  riastrad 			}
   2691   1.1  riastrad 			DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
   2692   1.1  riastrad 
   2693   1.1  riastrad 			return -EIO;
   2694   1.1  riastrad 		}
   2695   1.1  riastrad 		offset += tosend;
   2696   1.1  riastrad 		total -= tosend;
   2697   1.1  riastrad 	} while (total > 0);
   2698   1.1  riastrad 	return 0;
   2699   1.1  riastrad }
   2700   1.1  riastrad 
   2701   1.1  riastrad static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
   2702   1.1  riastrad 				  struct drm_dp_sideband_msg_tx *txmsg)
   2703   1.1  riastrad {
   2704   1.1  riastrad 	struct drm_dp_mst_branch *mstb = txmsg->dst;
   2705   1.1  riastrad 	u8 req_type;
   2706   1.1  riastrad 
   2707   1.1  riastrad 	/* both msg slots are full */
   2708   1.1  riastrad 	if (txmsg->seqno == -1) {
   2709   1.1  riastrad 		if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
   2710   1.1  riastrad 			DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
   2711   1.1  riastrad 			return -EAGAIN;
   2712   1.1  riastrad 		}
   2713   1.1  riastrad 		if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
   2714   1.1  riastrad 			txmsg->seqno = mstb->last_seqno;
   2715   1.1  riastrad 			mstb->last_seqno ^= 1;
   2716   1.1  riastrad 		} else if (mstb->tx_slots[0] == NULL)
   2717   1.1  riastrad 			txmsg->seqno = 0;
   2718   1.1  riastrad 		else
   2719   1.1  riastrad 			txmsg->seqno = 1;
   2720   1.1  riastrad 		mstb->tx_slots[txmsg->seqno] = txmsg;
   2721   1.1  riastrad 	}
   2722   1.1  riastrad 
   2723   1.1  riastrad 	req_type = txmsg->msg[0] & 0x7f;
   2724   1.1  riastrad 	if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
   2725   1.1  riastrad 		req_type == DP_RESOURCE_STATUS_NOTIFY)
   2726   1.1  riastrad 		hdr->broadcast = 1;
   2727   1.1  riastrad 	else
   2728   1.1  riastrad 		hdr->broadcast = 0;
   2729   1.1  riastrad 	hdr->path_msg = txmsg->path_msg;
   2730   1.1  riastrad 	hdr->lct = mstb->lct;
   2731   1.1  riastrad 	hdr->lcr = mstb->lct - 1;
   2732   1.1  riastrad 	if (mstb->lct > 1)
   2733   1.1  riastrad 		memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
   2734   1.1  riastrad 	hdr->seqno = txmsg->seqno;
   2735   1.1  riastrad 	return 0;
   2736   1.1  riastrad }
   2737   1.1  riastrad /*
   2738   1.1  riastrad  * process a single block of the next message in the sideband queue
   2739   1.1  riastrad  */
   2740   1.1  riastrad static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
   2741   1.1  riastrad 				   struct drm_dp_sideband_msg_tx *txmsg,
   2742   1.1  riastrad 				   bool up)
   2743   1.1  riastrad {
   2744   1.1  riastrad 	u8 chunk[48];
   2745   1.1  riastrad 	struct drm_dp_sideband_msg_hdr hdr;
   2746   1.1  riastrad 	int len, space, idx, tosend;
   2747   1.1  riastrad 	int ret;
   2748   1.1  riastrad 
   2749   1.1  riastrad 	memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
   2750   1.1  riastrad 
   2751   1.1  riastrad 	if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
   2752   1.1  riastrad 		txmsg->seqno = -1;
   2753   1.1  riastrad 		txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
   2754   1.1  riastrad 	}
   2755   1.1  riastrad 
   2756   1.1  riastrad 	/* make hdr from dst mst - for replies use seqno
   2757   1.1  riastrad 	   otherwise assign one */
   2758   1.1  riastrad 	ret = set_hdr_from_dst_qlock(&hdr, txmsg);
   2759   1.1  riastrad 	if (ret < 0)
   2760   1.1  riastrad 		return ret;
   2761   1.1  riastrad 
   2762   1.1  riastrad 	/* amount left to send in this message */
   2763   1.1  riastrad 	len = txmsg->cur_len - txmsg->cur_offset;
   2764   1.1  riastrad 
   2765   1.1  riastrad 	/* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
   2766   1.1  riastrad 	space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
   2767   1.1  riastrad 
   2768   1.1  riastrad 	tosend = min(len, space);
   2769   1.1  riastrad 	if (len == txmsg->cur_len)
   2770   1.1  riastrad 		hdr.somt = 1;
   2771   1.1  riastrad 	if (space >= len)
   2772   1.1  riastrad 		hdr.eomt = 1;
   2773   1.1  riastrad 
   2774   1.1  riastrad 
   2775   1.1  riastrad 	hdr.msg_len = tosend + 1;
   2776   1.1  riastrad 	drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
   2777   1.1  riastrad 	memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
   2778   1.1  riastrad 	/* add crc at end */
   2779   1.1  riastrad 	drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
   2780   1.1  riastrad 	idx += tosend + 1;
   2781   1.1  riastrad 
   2782   1.1  riastrad 	ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
   2783   1.6  riastrad 	if (unlikely(ret) && drm_debug_enabled(DRM_UT_DP)) {
   2784   1.6  riastrad 		struct drm_printer p = drm_debug_printer(DBG_PREFIX);
   2785   1.6  riastrad 
   2786   1.6  riastrad 		drm_printf(&p, "sideband msg failed to send\n");
   2787   1.6  riastrad 		drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
   2788   1.1  riastrad 		return ret;
   2789   1.1  riastrad 	}
   2790   1.1  riastrad 
   2791   1.1  riastrad 	txmsg->cur_offset += tosend;
   2792   1.1  riastrad 	if (txmsg->cur_offset == txmsg->cur_len) {
   2793   1.1  riastrad 		txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
   2794   1.1  riastrad 		return 1;
   2795   1.1  riastrad 	}
   2796   1.1  riastrad 	return 0;
   2797   1.1  riastrad }
   2798   1.1  riastrad 
   2799   1.1  riastrad static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
   2800   1.1  riastrad {
   2801   1.1  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   2802   1.1  riastrad 	int ret;
   2803   1.1  riastrad 
   2804   1.1  riastrad 	WARN_ON(!mutex_is_locked(&mgr->qlock));
   2805   1.1  riastrad 
   2806   1.1  riastrad 	/* construct a chunk from the first msg in the tx_msg queue */
   2807   1.6  riastrad 	if (list_empty(&mgr->tx_msg_downq))
   2808   1.1  riastrad 		return;
   2809   1.1  riastrad 
   2810   1.1  riastrad 	txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
   2811   1.1  riastrad 	ret = process_single_tx_qlock(mgr, txmsg, false);
   2812   1.1  riastrad 	if (ret == 1) {
   2813   1.1  riastrad 		/* txmsg is sent it should be in the slots now */
   2814   1.6  riastrad 		mgr->is_waiting_for_dwn_reply = true;
   2815   1.1  riastrad 		list_del(&txmsg->next);
   2816   1.1  riastrad 	} else if (ret) {
   2817   1.1  riastrad 		DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
   2818   1.6  riastrad 		mgr->is_waiting_for_dwn_reply = false;
   2819   1.1  riastrad 		list_del(&txmsg->next);
   2820   1.1  riastrad 		if (txmsg->seqno != -1)
   2821   1.1  riastrad 			txmsg->dst->tx_slots[txmsg->seqno] = NULL;
   2822   1.1  riastrad 		txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
   2823   1.6  riastrad 		DRM_WAKEUP_ALL(&mgr->tx_waitq, &mgr->qlock);
   2824   1.1  riastrad 	}
   2825   1.1  riastrad }
   2826   1.1  riastrad 
   2827   1.1  riastrad /* called holding qlock */
   2828   1.1  riastrad static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
   2829   1.1  riastrad 				       struct drm_dp_sideband_msg_tx *txmsg)
   2830   1.1  riastrad {
   2831   1.1  riastrad 	int ret;
   2832   1.1  riastrad 
   2833   1.1  riastrad 	/* construct a chunk from the first msg in the tx_msg queue */
   2834   1.1  riastrad 	ret = process_single_tx_qlock(mgr, txmsg, true);
   2835   1.1  riastrad 
   2836   1.6  riastrad 	if (ret != 1)
   2837   1.6  riastrad 		DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
   2838   1.6  riastrad 
   2839   1.6  riastrad 	if (txmsg->seqno != -1) {
   2840   1.6  riastrad 		WARN_ON((unsigned int)txmsg->seqno >
   2841   1.6  riastrad 			ARRAY_SIZE(txmsg->dst->tx_slots));
   2842   1.6  riastrad 		txmsg->dst->tx_slots[txmsg->seqno] = NULL;
   2843   1.6  riastrad 	}
   2844   1.6  riastrad }
   2845   1.6  riastrad 
   2846   1.6  riastrad static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
   2847   1.6  riastrad 				 struct drm_dp_sideband_msg_tx *txmsg)
   2848   1.6  riastrad {
   2849   1.6  riastrad 	mutex_lock(&mgr->qlock);
   2850   1.6  riastrad 	list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
   2851   1.6  riastrad 
   2852   1.6  riastrad 	if (drm_debug_enabled(DRM_UT_DP)) {
   2853   1.6  riastrad 		struct drm_printer p = drm_debug_printer(DBG_PREFIX);
   2854   1.6  riastrad 
   2855   1.6  riastrad 		drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
   2856   1.6  riastrad 	}
   2857   1.6  riastrad 
   2858   1.6  riastrad 	if (list_is_singular(&mgr->tx_msg_downq) &&
   2859   1.6  riastrad 	    !mgr->is_waiting_for_dwn_reply)
   2860   1.6  riastrad 		process_single_down_tx_qlock(mgr);
   2861   1.6  riastrad 	mutex_unlock(&mgr->qlock);
   2862   1.6  riastrad }
   2863   1.6  riastrad 
   2864   1.6  riastrad static void
   2865   1.6  riastrad drm_dp_dump_link_address(struct drm_dp_link_address_ack_reply *reply)
   2866   1.6  riastrad {
   2867   1.6  riastrad 	struct drm_dp_link_addr_reply_port *port_reply;
   2868   1.6  riastrad 	int i;
   2869   1.6  riastrad 
   2870   1.6  riastrad 	for (i = 0; i < reply->nports; i++) {
   2871   1.6  riastrad 		port_reply = &reply->ports[i];
   2872   1.6  riastrad 		DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n",
   2873   1.6  riastrad 			      i,
   2874   1.6  riastrad 			      port_reply->input_port,
   2875   1.6  riastrad 			      port_reply->peer_device_type,
   2876   1.6  riastrad 			      port_reply->port_number,
   2877   1.6  riastrad 			      port_reply->dpcd_revision,
   2878   1.6  riastrad 			      port_reply->mcs,
   2879   1.6  riastrad 			      port_reply->ddps,
   2880   1.6  riastrad 			      port_reply->legacy_device_plug_status,
   2881   1.6  riastrad 			      port_reply->num_sdp_streams,
   2882   1.6  riastrad 			      port_reply->num_sdp_stream_sinks);
   2883   1.6  riastrad 	}
   2884   1.6  riastrad }
   2885   1.6  riastrad 
   2886   1.6  riastrad static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
   2887   1.6  riastrad 				     struct drm_dp_mst_branch *mstb)
   2888   1.6  riastrad {
   2889   1.6  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   2890   1.6  riastrad 	struct drm_dp_link_address_ack_reply *reply;
   2891   1.6  riastrad 	struct drm_dp_mst_port *port, *tmp;
   2892   1.6  riastrad 	int i, len __unused, ret, port_mask = 0;
   2893   1.6  riastrad 	bool changed = false;
   2894   1.6  riastrad 
   2895   1.6  riastrad 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
   2896   1.6  riastrad 	if (!txmsg)
   2897   1.6  riastrad 		return -ENOMEM;
   2898   1.6  riastrad 
   2899   1.6  riastrad 	txmsg->dst = mstb;
   2900   1.6  riastrad 	len = build_link_address(txmsg);
   2901   1.6  riastrad 
   2902   1.6  riastrad 	mstb->link_address_sent = true;
   2903   1.6  riastrad 	drm_dp_queue_down_tx(mgr, txmsg);
   2904   1.6  riastrad 
   2905   1.6  riastrad 	/* FIXME: Actually do some real error handling here */
   2906   1.6  riastrad 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
   2907   1.6  riastrad 	if (ret <= 0) {
   2908   1.6  riastrad 		DRM_ERROR("Sending link address failed with %d\n", ret);
   2909   1.6  riastrad 		goto out;
   2910   1.6  riastrad 	}
   2911   1.6  riastrad 	if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
   2912   1.6  riastrad 		DRM_ERROR("link address NAK received\n");
   2913   1.6  riastrad 		ret = -EIO;
   2914   1.6  riastrad 		goto out;
   2915   1.6  riastrad 	}
   2916   1.6  riastrad 
   2917   1.6  riastrad 	reply = &txmsg->reply.u.link_addr;
   2918   1.6  riastrad 	DRM_DEBUG_KMS("link address reply: %d\n", reply->nports);
   2919   1.6  riastrad 	drm_dp_dump_link_address(reply);
   2920   1.6  riastrad 
   2921   1.6  riastrad 	drm_dp_check_mstb_guid(mstb, reply->guid);
   2922   1.6  riastrad 
   2923   1.6  riastrad 	for (i = 0; i < reply->nports; i++) {
   2924   1.6  riastrad 		port_mask |= BIT(reply->ports[i].port_number);
   2925   1.6  riastrad 		ret = drm_dp_mst_handle_link_address_port(mstb, mgr->dev,
   2926   1.6  riastrad 							  &reply->ports[i]);
   2927   1.6  riastrad 		if (ret == 1)
   2928   1.6  riastrad 			changed = true;
   2929   1.6  riastrad 		else if (ret < 0)
   2930   1.6  riastrad 			goto out;
   2931   1.6  riastrad 	}
   2932   1.6  riastrad 
   2933   1.6  riastrad 	/* Prune any ports that are currently a part of mstb in our in-memory
   2934   1.6  riastrad 	 * topology, but were not seen in this link address. Usually this
   2935   1.6  riastrad 	 * means that they were removed while the topology was out of sync,
   2936   1.6  riastrad 	 * e.g. during suspend/resume
   2937   1.6  riastrad 	 */
   2938   1.6  riastrad 	mutex_lock(&mgr->lock);
   2939   1.6  riastrad 	list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
   2940   1.6  riastrad 		if (port_mask & BIT(port->port_num))
   2941   1.6  riastrad 			continue;
   2942   1.6  riastrad 
   2943   1.6  riastrad 		DRM_DEBUG_KMS("port %d was not in link address, removing\n",
   2944   1.6  riastrad 			      port->port_num);
   2945   1.6  riastrad 		list_del(&port->next);
   2946   1.6  riastrad 		drm_dp_mst_topology_put_port(port);
   2947   1.6  riastrad 		changed = true;
   2948   1.6  riastrad 	}
   2949   1.6  riastrad 	mutex_unlock(&mgr->lock);
   2950   1.1  riastrad 
   2951   1.6  riastrad out:
   2952   1.6  riastrad 	if (ret <= 0)
   2953   1.6  riastrad 		mstb->link_address_sent = false;
   2954   1.6  riastrad 	kfree(txmsg);
   2955   1.6  riastrad 	return ret < 0 ? ret : changed;
   2956   1.1  riastrad }
   2957   1.1  riastrad 
   2958   1.6  riastrad void drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
   2959   1.6  riastrad 					struct drm_dp_mst_branch *mstb)
   2960   1.1  riastrad {
   2961   1.1  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   2962  1.10  riastrad 	int len __unused, ret;
   2963   1.1  riastrad 
   2964   1.1  riastrad 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
   2965   1.1  riastrad 	if (!txmsg)
   2966   1.1  riastrad 		return;
   2967   1.1  riastrad 
   2968   1.1  riastrad 	txmsg->dst = mstb;
   2969   1.6  riastrad 	len = build_clear_payload_id_table(txmsg);
   2970   1.1  riastrad 
   2971   1.1  riastrad 	drm_dp_queue_down_tx(mgr, txmsg);
   2972   1.1  riastrad 
   2973   1.1  riastrad 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
   2974   1.6  riastrad 	if (ret > 0 && txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
   2975   1.6  riastrad 		DRM_DEBUG_KMS("clear payload table id nak received\n");
   2976   1.1  riastrad 
   2977   1.1  riastrad 	kfree(txmsg);
   2978   1.1  riastrad }
   2979   1.1  riastrad 
   2980   1.6  riastrad static int
   2981   1.6  riastrad drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
   2982   1.6  riastrad 				struct drm_dp_mst_branch *mstb,
   2983   1.6  riastrad 				struct drm_dp_mst_port *port)
   2984   1.1  riastrad {
   2985   1.6  riastrad 	struct drm_dp_enum_path_resources_ack_reply *path_res;
   2986   1.6  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   2987   1.3  riastrad 	int len __unused;
   2988   1.1  riastrad 	int ret;
   2989   1.1  riastrad 
   2990   1.1  riastrad 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
   2991   1.1  riastrad 	if (!txmsg)
   2992   1.1  riastrad 		return -ENOMEM;
   2993   1.1  riastrad 
   2994   1.1  riastrad 	txmsg->dst = mstb;
   2995   1.1  riastrad 	len = build_enum_path_resources(txmsg, port->port_num);
   2996   1.1  riastrad 
   2997   1.1  riastrad 	drm_dp_queue_down_tx(mgr, txmsg);
   2998   1.1  riastrad 
   2999   1.1  riastrad 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
   3000   1.1  riastrad 	if (ret > 0) {
   3001   1.6  riastrad 		path_res = &txmsg->reply.u.path_resources;
   3002   1.6  riastrad 
   3003   1.6  riastrad 		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
   3004   1.1  riastrad 			DRM_DEBUG_KMS("enum path resources nak received\n");
   3005   1.6  riastrad 		} else {
   3006   1.6  riastrad 			if (port->port_num != path_res->port_number)
   3007   1.1  riastrad 				DRM_ERROR("got incorrect port in response\n");
   3008   1.6  riastrad 
   3009   1.6  riastrad 			DRM_DEBUG_KMS("enum path resources %d: %d %d\n",
   3010   1.6  riastrad 				      path_res->port_number,
   3011   1.6  riastrad 				      path_res->full_payload_bw_number,
   3012   1.6  riastrad 				      path_res->avail_payload_bw_number);
   3013   1.6  riastrad 			port->available_pbn =
   3014   1.6  riastrad 				path_res->avail_payload_bw_number;
   3015   1.6  riastrad 			port->fec_capable = path_res->fec_capable;
   3016   1.1  riastrad 		}
   3017   1.1  riastrad 	}
   3018   1.1  riastrad 
   3019   1.1  riastrad 	kfree(txmsg);
   3020   1.1  riastrad 	return 0;
   3021   1.1  riastrad }
   3022   1.1  riastrad 
   3023   1.1  riastrad static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
   3024   1.1  riastrad {
   3025   1.1  riastrad 	if (!mstb->port_parent)
   3026   1.1  riastrad 		return NULL;
   3027   1.1  riastrad 
   3028   1.1  riastrad 	if (mstb->port_parent->mstb != mstb)
   3029   1.1  riastrad 		return mstb->port_parent;
   3030   1.1  riastrad 
   3031   1.1  riastrad 	return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
   3032   1.1  riastrad }
   3033   1.1  riastrad 
   3034   1.6  riastrad /*
   3035   1.6  riastrad  * Searches upwards in the topology starting from mstb to try to find the
   3036   1.6  riastrad  * closest available parent of mstb that's still connected to the rest of the
   3037   1.6  riastrad  * topology. This can be used in order to perform operations like releasing
   3038   1.6  riastrad  * payloads, where the branch device which owned the payload may no longer be
   3039   1.6  riastrad  * around and thus would require that the payload on the last living relative
   3040   1.6  riastrad  * be freed instead.
   3041   1.6  riastrad  */
   3042   1.6  riastrad static struct drm_dp_mst_branch *
   3043   1.6  riastrad drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
   3044   1.6  riastrad 					struct drm_dp_mst_branch *mstb,
   3045   1.6  riastrad 					int *port_num)
   3046   1.1  riastrad {
   3047   1.1  riastrad 	struct drm_dp_mst_branch *rmstb = NULL;
   3048   1.1  riastrad 	struct drm_dp_mst_port *found_port;
   3049   1.6  riastrad 
   3050   1.1  riastrad 	mutex_lock(&mgr->lock);
   3051   1.6  riastrad 	if (!mgr->mst_primary)
   3052   1.6  riastrad 		goto out;
   3053   1.6  riastrad 
   3054   1.6  riastrad 	do {
   3055   1.1  riastrad 		found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
   3056   1.6  riastrad 		if (!found_port)
   3057   1.6  riastrad 			break;
   3058   1.1  riastrad 
   3059   1.6  riastrad 		if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
   3060   1.1  riastrad 			rmstb = found_port->parent;
   3061   1.1  riastrad 			*port_num = found_port->port_num;
   3062   1.6  riastrad 		} else {
   3063   1.6  riastrad 			/* Search again, starting from this parent */
   3064   1.6  riastrad 			mstb = found_port->parent;
   3065   1.1  riastrad 		}
   3066   1.6  riastrad 	} while (!rmstb);
   3067   1.6  riastrad out:
   3068   1.1  riastrad 	mutex_unlock(&mgr->lock);
   3069   1.1  riastrad 	return rmstb;
   3070   1.1  riastrad }
   3071   1.1  riastrad 
   3072   1.1  riastrad static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
   3073   1.1  riastrad 				   struct drm_dp_mst_port *port,
   3074   1.1  riastrad 				   int id,
   3075   1.1  riastrad 				   int pbn)
   3076   1.1  riastrad {
   3077   1.1  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   3078   1.1  riastrad 	struct drm_dp_mst_branch *mstb;
   3079   1.3  riastrad 	int len __unused, ret, port_num;
   3080   1.6  riastrad 	u8 sinks[DRM_DP_MAX_SDP_STREAMS];
   3081   1.6  riastrad 	int i;
   3082   1.1  riastrad 
   3083   1.1  riastrad 	port_num = port->port_num;
   3084   1.6  riastrad 	mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
   3085   1.1  riastrad 	if (!mstb) {
   3086   1.6  riastrad 		mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
   3087   1.6  riastrad 							       port->parent,
   3088   1.6  riastrad 							       &port_num);
   3089   1.1  riastrad 
   3090   1.6  riastrad 		if (!mstb)
   3091   1.1  riastrad 			return -EINVAL;
   3092   1.1  riastrad 	}
   3093   1.1  riastrad 
   3094   1.1  riastrad 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
   3095   1.1  riastrad 	if (!txmsg) {
   3096   1.1  riastrad 		ret = -ENOMEM;
   3097   1.1  riastrad 		goto fail_put;
   3098   1.1  riastrad 	}
   3099   1.1  riastrad 
   3100   1.6  riastrad 	for (i = 0; i < port->num_sdp_streams; i++)
   3101   1.6  riastrad 		sinks[i] = i;
   3102   1.6  riastrad 
   3103   1.1  riastrad 	txmsg->dst = mstb;
   3104   1.1  riastrad 	len = build_allocate_payload(txmsg, port_num,
   3105   1.1  riastrad 				     id,
   3106   1.6  riastrad 				     pbn, port->num_sdp_streams, sinks);
   3107   1.1  riastrad 
   3108   1.1  riastrad 	drm_dp_queue_down_tx(mgr, txmsg);
   3109   1.1  riastrad 
   3110   1.6  riastrad 	/*
   3111   1.6  riastrad 	 * FIXME: there is a small chance that between getting the last
   3112   1.6  riastrad 	 * connected mstb and sending the payload message, the last connected
   3113   1.6  riastrad 	 * mstb could also be removed from the topology. In the future, this
   3114   1.6  riastrad 	 * needs to be fixed by restarting the
   3115   1.6  riastrad 	 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
   3116   1.6  riastrad 	 * timeout if the topology is still connected to the system.
   3117   1.6  riastrad 	 */
   3118   1.1  riastrad 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
   3119   1.1  riastrad 	if (ret > 0) {
   3120   1.6  riastrad 		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
   3121   1.1  riastrad 			ret = -EINVAL;
   3122   1.6  riastrad 		else
   3123   1.1  riastrad 			ret = 0;
   3124   1.1  riastrad 	}
   3125   1.1  riastrad 	kfree(txmsg);
   3126   1.1  riastrad fail_put:
   3127   1.6  riastrad 	drm_dp_mst_topology_put_mstb(mstb);
   3128   1.6  riastrad 	return ret;
   3129   1.6  riastrad }
   3130   1.6  riastrad 
   3131   1.6  riastrad int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
   3132   1.6  riastrad 				 struct drm_dp_mst_port *port, bool power_up)
   3133   1.6  riastrad {
   3134   1.6  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   3135   1.7  riastrad 	int len __unused, ret;
   3136   1.6  riastrad 
   3137   1.6  riastrad 	port = drm_dp_mst_topology_get_port_validated(mgr, port);
   3138   1.6  riastrad 	if (!port)
   3139   1.6  riastrad 		return -EINVAL;
   3140   1.6  riastrad 
   3141   1.6  riastrad 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
   3142   1.6  riastrad 	if (!txmsg) {
   3143   1.6  riastrad 		drm_dp_mst_topology_put_port(port);
   3144   1.6  riastrad 		return -ENOMEM;
   3145   1.6  riastrad 	}
   3146   1.6  riastrad 
   3147   1.6  riastrad 	txmsg->dst = port->parent;
   3148   1.6  riastrad 	len = build_power_updown_phy(txmsg, port->port_num, power_up);
   3149   1.6  riastrad 	drm_dp_queue_down_tx(mgr, txmsg);
   3150   1.6  riastrad 
   3151   1.6  riastrad 	ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
   3152   1.6  riastrad 	if (ret > 0) {
   3153   1.6  riastrad 		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
   3154   1.6  riastrad 			ret = -EINVAL;
   3155   1.6  riastrad 		else
   3156   1.6  riastrad 			ret = 0;
   3157   1.6  riastrad 	}
   3158   1.6  riastrad 	kfree(txmsg);
   3159   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   3160   1.6  riastrad 
   3161   1.1  riastrad 	return ret;
   3162   1.1  riastrad }
   3163   1.6  riastrad EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
   3164   1.1  riastrad 
   3165   1.1  riastrad static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
   3166   1.1  riastrad 				       int id,
   3167   1.1  riastrad 				       struct drm_dp_payload *payload)
   3168   1.1  riastrad {
   3169   1.1  riastrad 	int ret;
   3170   1.1  riastrad 
   3171   1.1  riastrad 	ret = drm_dp_dpcd_write_payload(mgr, id, payload);
   3172   1.1  riastrad 	if (ret < 0) {
   3173   1.1  riastrad 		payload->payload_state = 0;
   3174   1.1  riastrad 		return ret;
   3175   1.1  riastrad 	}
   3176   1.1  riastrad 	payload->payload_state = DP_PAYLOAD_LOCAL;
   3177   1.1  riastrad 	return 0;
   3178   1.1  riastrad }
   3179   1.1  riastrad 
   3180   1.1  riastrad static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
   3181   1.1  riastrad 				       struct drm_dp_mst_port *port,
   3182   1.1  riastrad 				       int id,
   3183   1.1  riastrad 				       struct drm_dp_payload *payload)
   3184   1.1  riastrad {
   3185   1.1  riastrad 	int ret;
   3186   1.1  riastrad 	ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
   3187   1.1  riastrad 	if (ret < 0)
   3188   1.1  riastrad 		return ret;
   3189   1.1  riastrad 	payload->payload_state = DP_PAYLOAD_REMOTE;
   3190   1.1  riastrad 	return ret;
   3191   1.1  riastrad }
   3192   1.1  riastrad 
   3193   1.1  riastrad static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
   3194   1.1  riastrad 					struct drm_dp_mst_port *port,
   3195   1.1  riastrad 					int id,
   3196   1.1  riastrad 					struct drm_dp_payload *payload)
   3197   1.1  riastrad {
   3198   1.1  riastrad 	DRM_DEBUG_KMS("\n");
   3199   1.6  riastrad 	/* it's okay for these to fail */
   3200   1.1  riastrad 	if (port) {
   3201   1.1  riastrad 		drm_dp_payload_send_msg(mgr, port, id, 0);
   3202   1.1  riastrad 	}
   3203   1.1  riastrad 
   3204   1.1  riastrad 	drm_dp_dpcd_write_payload(mgr, id, payload);
   3205   1.1  riastrad 	payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
   3206   1.1  riastrad 	return 0;
   3207   1.1  riastrad }
   3208   1.1  riastrad 
   3209   1.1  riastrad static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
   3210   1.1  riastrad 					int id,
   3211   1.1  riastrad 					struct drm_dp_payload *payload)
   3212   1.1  riastrad {
   3213   1.1  riastrad 	payload->payload_state = 0;
   3214   1.1  riastrad 	return 0;
   3215   1.1  riastrad }
   3216   1.1  riastrad 
   3217   1.1  riastrad /**
   3218   1.1  riastrad  * drm_dp_update_payload_part1() - Execute payload update part 1
   3219   1.1  riastrad  * @mgr: manager to use.
   3220   1.1  riastrad  *
   3221   1.1  riastrad  * This iterates over all proposed virtual channels, and tries to
   3222   1.1  riastrad  * allocate space in the link for them. For 0->slots transitions,
   3223   1.1  riastrad  * this step just writes the VCPI to the MST device. For slots->0
   3224   1.1  riastrad  * transitions, this writes the updated VCPIs and removes the
   3225   1.1  riastrad  * remote VC payloads.
   3226   1.1  riastrad  *
   3227   1.1  riastrad  * after calling this the driver should generate ACT and payload
   3228   1.1  riastrad  * packets.
   3229   1.1  riastrad  */
   3230   1.1  riastrad int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
   3231   1.1  riastrad {
   3232   1.6  riastrad 	struct drm_dp_payload req_payload;
   3233   1.6  riastrad 	struct drm_dp_mst_port *port;
   3234   1.1  riastrad 	int i, j;
   3235   1.1  riastrad 	int cur_slots = 1;
   3236   1.1  riastrad 
   3237   1.1  riastrad 	mutex_lock(&mgr->payload_lock);
   3238   1.1  riastrad 	for (i = 0; i < mgr->max_payloads; i++) {
   3239   1.6  riastrad 		struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
   3240   1.6  riastrad 		struct drm_dp_payload *payload = &mgr->payloads[i];
   3241   1.6  riastrad 		bool put_port = false;
   3242   1.6  riastrad 
   3243   1.1  riastrad 		/* solve the current payloads - compare to the hw ones
   3244   1.1  riastrad 		   - update the hw view */
   3245   1.1  riastrad 		req_payload.start_slot = cur_slots;
   3246   1.6  riastrad 		if (vcpi) {
   3247   1.6  riastrad 			port = container_of(vcpi, struct drm_dp_mst_port,
   3248   1.6  riastrad 					    vcpi);
   3249   1.6  riastrad 
   3250   1.6  riastrad 			/* Validated ports don't matter if we're releasing
   3251   1.6  riastrad 			 * VCPI
   3252   1.6  riastrad 			 */
   3253   1.6  riastrad 			if (vcpi->num_slots) {
   3254   1.6  riastrad 				port = drm_dp_mst_topology_get_port_validated(
   3255   1.6  riastrad 				    mgr, port);
   3256   1.6  riastrad 				if (!port) {
   3257   1.6  riastrad 					mutex_unlock(&mgr->payload_lock);
   3258   1.6  riastrad 					return -EINVAL;
   3259   1.6  riastrad 				}
   3260   1.6  riastrad 				put_port = true;
   3261   1.1  riastrad 			}
   3262   1.6  riastrad 
   3263   1.6  riastrad 			req_payload.num_slots = vcpi->num_slots;
   3264   1.6  riastrad 			req_payload.vcpi = vcpi->vcpi;
   3265   1.1  riastrad 		} else {
   3266   1.1  riastrad 			port = NULL;
   3267   1.1  riastrad 			req_payload.num_slots = 0;
   3268   1.1  riastrad 		}
   3269   1.1  riastrad 
   3270   1.6  riastrad 		payload->start_slot = req_payload.start_slot;
   3271   1.1  riastrad 		/* work out what is required to happen with this payload */
   3272   1.6  riastrad 		if (payload->num_slots != req_payload.num_slots) {
   3273   1.1  riastrad 
   3274   1.1  riastrad 			/* need to push an update for this payload */
   3275   1.1  riastrad 			if (req_payload.num_slots) {
   3276   1.6  riastrad 				drm_dp_create_payload_step1(mgr, vcpi->vcpi,
   3277   1.6  riastrad 							    &req_payload);
   3278   1.6  riastrad 				payload->num_slots = req_payload.num_slots;
   3279   1.6  riastrad 				payload->vcpi = req_payload.vcpi;
   3280   1.6  riastrad 
   3281   1.6  riastrad 			} else if (payload->num_slots) {
   3282   1.6  riastrad 				payload->num_slots = 0;
   3283   1.6  riastrad 				drm_dp_destroy_payload_step1(mgr, port,
   3284   1.6  riastrad 							     payload->vcpi,
   3285   1.6  riastrad 							     payload);
   3286   1.6  riastrad 				req_payload.payload_state =
   3287   1.6  riastrad 					payload->payload_state;
   3288   1.6  riastrad 				payload->start_slot = 0;
   3289   1.1  riastrad 			}
   3290   1.6  riastrad 			payload->payload_state = req_payload.payload_state;
   3291   1.1  riastrad 		}
   3292   1.1  riastrad 		cur_slots += req_payload.num_slots;
   3293   1.1  riastrad 
   3294   1.6  riastrad 		if (put_port)
   3295   1.6  riastrad 			drm_dp_mst_topology_put_port(port);
   3296   1.1  riastrad 	}
   3297   1.1  riastrad 
   3298   1.6  riastrad 	for (i = 0; i < mgr->max_payloads; /* do nothing */) {
   3299   1.6  riastrad 		if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL) {
   3300   1.6  riastrad 			i++;
   3301   1.6  riastrad 			continue;
   3302   1.6  riastrad 		}
   3303   1.6  riastrad 
   3304   1.6  riastrad 		DRM_DEBUG_KMS("removing payload %d\n", i);
   3305   1.6  riastrad 		for (j = i; j < mgr->max_payloads - 1; j++) {
   3306   1.6  riastrad 			mgr->payloads[j] = mgr->payloads[j + 1];
   3307   1.6  riastrad 			mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
   3308   1.6  riastrad 
   3309   1.6  riastrad 			if (mgr->proposed_vcpis[j] &&
   3310   1.6  riastrad 			    mgr->proposed_vcpis[j]->num_slots) {
   3311   1.6  riastrad 				set_bit(j + 1, &mgr->payload_mask);
   3312   1.6  riastrad 			} else {
   3313   1.6  riastrad 				clear_bit(j + 1, &mgr->payload_mask);
   3314   1.1  riastrad 			}
   3315   1.6  riastrad 		}
   3316   1.1  riastrad 
   3317   1.6  riastrad 		memset(&mgr->payloads[mgr->max_payloads - 1], 0,
   3318   1.6  riastrad 		       sizeof(struct drm_dp_payload));
   3319   1.6  riastrad 		mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
   3320   1.6  riastrad 		clear_bit(mgr->max_payloads, &mgr->payload_mask);
   3321   1.1  riastrad 	}
   3322   1.1  riastrad 	mutex_unlock(&mgr->payload_lock);
   3323   1.1  riastrad 
   3324   1.1  riastrad 	return 0;
   3325   1.1  riastrad }
   3326   1.1  riastrad EXPORT_SYMBOL(drm_dp_update_payload_part1);
   3327   1.1  riastrad 
   3328   1.1  riastrad /**
   3329   1.1  riastrad  * drm_dp_update_payload_part2() - Execute payload update part 2
   3330   1.1  riastrad  * @mgr: manager to use.
   3331   1.1  riastrad  *
   3332   1.1  riastrad  * This iterates over all proposed virtual channels, and tries to
   3333   1.1  riastrad  * allocate space in the link for them. For 0->slots transitions,
   3334   1.1  riastrad  * this step writes the remote VC payload commands. For slots->0
   3335   1.1  riastrad  * this just resets some internal state.
   3336   1.1  riastrad  */
   3337   1.1  riastrad int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
   3338   1.1  riastrad {
   3339   1.1  riastrad 	struct drm_dp_mst_port *port;
   3340   1.1  riastrad 	int i;
   3341   1.1  riastrad 	int ret = 0;
   3342   1.1  riastrad 	mutex_lock(&mgr->payload_lock);
   3343   1.1  riastrad 	for (i = 0; i < mgr->max_payloads; i++) {
   3344   1.1  riastrad 
   3345   1.1  riastrad 		if (!mgr->proposed_vcpis[i])
   3346   1.1  riastrad 			continue;
   3347   1.1  riastrad 
   3348   1.1  riastrad 		port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
   3349   1.1  riastrad 
   3350   1.1  riastrad 		DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
   3351   1.1  riastrad 		if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
   3352   1.1  riastrad 			ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
   3353   1.1  riastrad 		} else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
   3354   1.1  riastrad 			ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
   3355   1.1  riastrad 		}
   3356   1.1  riastrad 		if (ret) {
   3357   1.1  riastrad 			mutex_unlock(&mgr->payload_lock);
   3358   1.1  riastrad 			return ret;
   3359   1.1  riastrad 		}
   3360   1.1  riastrad 	}
   3361   1.1  riastrad 	mutex_unlock(&mgr->payload_lock);
   3362   1.1  riastrad 	return 0;
   3363   1.1  riastrad }
   3364   1.1  riastrad EXPORT_SYMBOL(drm_dp_update_payload_part2);
   3365   1.1  riastrad 
   3366   1.1  riastrad static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
   3367   1.1  riastrad 				 struct drm_dp_mst_port *port,
   3368   1.6  riastrad 				 int offset, int size, u8 *bytes)
   3369   1.1  riastrad {
   3370  1.10  riastrad 	int len __unused;
   3371   1.6  riastrad 	int ret = 0;
   3372   1.1  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   3373   1.6  riastrad 	struct drm_dp_mst_branch *mstb;
   3374   1.6  riastrad 
   3375   1.6  riastrad 	mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
   3376   1.6  riastrad 	if (!mstb)
   3377   1.6  riastrad 		return -EINVAL;
   3378   1.1  riastrad 
   3379   1.1  riastrad 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
   3380   1.6  riastrad 	if (!txmsg) {
   3381   1.6  riastrad 		ret = -ENOMEM;
   3382   1.6  riastrad 		goto fail_put;
   3383   1.6  riastrad 	}
   3384   1.1  riastrad 
   3385   1.6  riastrad 	len = build_dpcd_read(txmsg, port->port_num, offset, size);
   3386   1.1  riastrad 	txmsg->dst = port->parent;
   3387   1.1  riastrad 
   3388   1.1  riastrad 	drm_dp_queue_down_tx(mgr, txmsg);
   3389   1.1  riastrad 
   3390   1.6  riastrad 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
   3391   1.6  riastrad 	if (ret < 0)
   3392   1.6  riastrad 		goto fail_free;
   3393   1.6  riastrad 
   3394   1.6  riastrad 	/* DPCD read should never be NACKed */
   3395   1.6  riastrad 	if (txmsg->reply.reply_type == 1) {
   3396   1.6  riastrad 		DRM_ERROR("mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n",
   3397   1.6  riastrad 			  mstb, port->port_num, offset, size);
   3398   1.6  riastrad 		ret = -EIO;
   3399   1.6  riastrad 		goto fail_free;
   3400   1.6  riastrad 	}
   3401   1.6  riastrad 
   3402   1.6  riastrad 	if (txmsg->reply.u.remote_dpcd_read_ack.num_bytes != size) {
   3403   1.6  riastrad 		ret = -EPROTO;
   3404   1.6  riastrad 		goto fail_free;
   3405   1.6  riastrad 	}
   3406   1.6  riastrad 
   3407   1.6  riastrad 	ret = min_t(size_t, txmsg->reply.u.remote_dpcd_read_ack.num_bytes,
   3408   1.6  riastrad 		    size);
   3409   1.6  riastrad 	memcpy(bytes, txmsg->reply.u.remote_dpcd_read_ack.bytes, ret);
   3410   1.6  riastrad 
   3411   1.6  riastrad fail_free:
   3412   1.6  riastrad 	kfree(txmsg);
   3413   1.6  riastrad fail_put:
   3414   1.6  riastrad 	drm_dp_mst_topology_put_mstb(mstb);
   3415   1.6  riastrad 
   3416   1.6  riastrad 	return ret;
   3417   1.1  riastrad }
   3418   1.1  riastrad 
   3419   1.1  riastrad static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
   3420   1.1  riastrad 				  struct drm_dp_mst_port *port,
   3421   1.1  riastrad 				  int offset, int size, u8 *bytes)
   3422   1.1  riastrad {
   3423   1.3  riastrad 	int len __unused;
   3424   1.1  riastrad 	int ret;
   3425   1.1  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   3426   1.1  riastrad 	struct drm_dp_mst_branch *mstb;
   3427   1.1  riastrad 
   3428   1.6  riastrad 	mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
   3429   1.1  riastrad 	if (!mstb)
   3430   1.1  riastrad 		return -EINVAL;
   3431   1.1  riastrad 
   3432   1.1  riastrad 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
   3433   1.1  riastrad 	if (!txmsg) {
   3434   1.1  riastrad 		ret = -ENOMEM;
   3435   1.1  riastrad 		goto fail_put;
   3436   1.1  riastrad 	}
   3437   1.1  riastrad 
   3438   1.1  riastrad 	len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
   3439   1.1  riastrad 	txmsg->dst = mstb;
   3440   1.1  riastrad 
   3441   1.1  riastrad 	drm_dp_queue_down_tx(mgr, txmsg);
   3442   1.1  riastrad 
   3443   1.1  riastrad 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
   3444   1.1  riastrad 	if (ret > 0) {
   3445   1.6  riastrad 		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
   3446   1.6  riastrad 			ret = -EIO;
   3447   1.6  riastrad 		else
   3448   1.1  riastrad 			ret = 0;
   3449   1.1  riastrad 	}
   3450   1.1  riastrad 	kfree(txmsg);
   3451   1.1  riastrad fail_put:
   3452   1.6  riastrad 	drm_dp_mst_topology_put_mstb(mstb);
   3453   1.1  riastrad 	return ret;
   3454   1.1  riastrad }
   3455   1.1  riastrad 
   3456   1.1  riastrad static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
   3457   1.1  riastrad {
   3458   1.1  riastrad 	struct drm_dp_sideband_msg_reply_body reply;
   3459   1.1  riastrad 
   3460   1.6  riastrad 	reply.reply_type = DP_SIDEBAND_REPLY_ACK;
   3461   1.1  riastrad 	reply.req_type = req_type;
   3462   1.1  riastrad 	drm_dp_encode_sideband_reply(&reply, msg);
   3463   1.1  riastrad 	return 0;
   3464   1.1  riastrad }
   3465   1.1  riastrad 
   3466   1.1  riastrad static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
   3467   1.1  riastrad 				    struct drm_dp_mst_branch *mstb,
   3468   1.1  riastrad 				    int req_type, int seqno, bool broadcast)
   3469   1.1  riastrad {
   3470   1.1  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   3471   1.1  riastrad 
   3472   1.1  riastrad 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
   3473   1.1  riastrad 	if (!txmsg)
   3474   1.1  riastrad 		return -ENOMEM;
   3475   1.1  riastrad 
   3476   1.1  riastrad 	txmsg->dst = mstb;
   3477   1.1  riastrad 	txmsg->seqno = seqno;
   3478   1.1  riastrad 	drm_dp_encode_up_ack_reply(txmsg, req_type);
   3479   1.1  riastrad 
   3480   1.1  riastrad 	mutex_lock(&mgr->qlock);
   3481   1.1  riastrad 
   3482   1.1  riastrad 	process_single_up_tx_qlock(mgr, txmsg);
   3483   1.1  riastrad 
   3484   1.1  riastrad 	mutex_unlock(&mgr->qlock);
   3485   1.1  riastrad 
   3486   1.1  riastrad 	kfree(txmsg);
   3487   1.1  riastrad 	return 0;
   3488   1.1  riastrad }
   3489   1.1  riastrad 
   3490   1.6  riastrad static int drm_dp_get_vc_payload_bw(u8 dp_link_bw, u8  dp_link_count)
   3491   1.1  riastrad {
   3492   1.6  riastrad 	if (dp_link_bw == 0 || dp_link_count == 0)
   3493   1.1  riastrad 		DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
   3494   1.1  riastrad 			      dp_link_bw, dp_link_count);
   3495   1.1  riastrad 
   3496   1.6  riastrad 	return dp_link_bw * dp_link_count / 2;
   3497   1.1  riastrad }
   3498   1.1  riastrad 
   3499   1.1  riastrad /**
   3500   1.1  riastrad  * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
   3501   1.1  riastrad  * @mgr: manager to set state for
   3502   1.1  riastrad  * @mst_state: true to enable MST on this connector - false to disable.
   3503   1.1  riastrad  *
   3504   1.1  riastrad  * This is called by the driver when it detects an MST capable device plugged
   3505   1.1  riastrad  * into a DP MST capable port, or when a DP MST capable device is unplugged.
   3506   1.1  riastrad  */
   3507   1.1  riastrad int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
   3508   1.1  riastrad {
   3509   1.1  riastrad 	int ret = 0;
   3510   1.6  riastrad 	int i = 0;
   3511   1.1  riastrad 	struct drm_dp_mst_branch *mstb = NULL;
   3512   1.1  riastrad 
   3513   1.1  riastrad 	mutex_lock(&mgr->lock);
   3514   1.1  riastrad 	if (mst_state == mgr->mst_state)
   3515   1.1  riastrad 		goto out_unlock;
   3516   1.1  riastrad 
   3517   1.1  riastrad 	mgr->mst_state = mst_state;
   3518   1.1  riastrad 	/* set the device into MST mode */
   3519   1.1  riastrad 	if (mst_state) {
   3520   1.1  riastrad 		WARN_ON(mgr->mst_primary);
   3521   1.1  riastrad 
   3522   1.1  riastrad 		/* get dpcd info */
   3523   1.1  riastrad 		ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
   3524   1.1  riastrad 		if (ret != DP_RECEIVER_CAP_SIZE) {
   3525   1.1  riastrad 			DRM_DEBUG_KMS("failed to read DPCD\n");
   3526   1.1  riastrad 			goto out_unlock;
   3527   1.1  riastrad 		}
   3528   1.1  riastrad 
   3529   1.6  riastrad 		mgr->pbn_div = drm_dp_get_vc_payload_bw(mgr->dpcd[1],
   3530   1.6  riastrad 							mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK);
   3531   1.6  riastrad 		if (mgr->pbn_div == 0) {
   3532   1.1  riastrad 			ret = -EINVAL;
   3533   1.1  riastrad 			goto out_unlock;
   3534   1.1  riastrad 		}
   3535   1.1  riastrad 
   3536   1.1  riastrad 		/* add initial branch device at LCT 1 */
   3537   1.1  riastrad 		mstb = drm_dp_add_mst_branch_device(1, NULL);
   3538   1.1  riastrad 		if (mstb == NULL) {
   3539   1.1  riastrad 			ret = -ENOMEM;
   3540   1.1  riastrad 			goto out_unlock;
   3541   1.1  riastrad 		}
   3542   1.1  riastrad 		mstb->mgr = mgr;
   3543   1.1  riastrad 
   3544   1.1  riastrad 		/* give this the main reference */
   3545   1.1  riastrad 		mgr->mst_primary = mstb;
   3546   1.6  riastrad 		drm_dp_mst_topology_get_mstb(mgr->mst_primary);
   3547   1.1  riastrad 
   3548   1.1  riastrad 		ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
   3549   1.1  riastrad 							 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
   3550   1.1  riastrad 		if (ret < 0) {
   3551   1.1  riastrad 			goto out_unlock;
   3552   1.1  riastrad 		}
   3553   1.1  riastrad 
   3554   1.1  riastrad 		{
   3555   1.1  riastrad 			struct drm_dp_payload reset_pay;
   3556   1.1  riastrad 			reset_pay.start_slot = 0;
   3557   1.1  riastrad 			reset_pay.num_slots = 0x3f;
   3558   1.1  riastrad 			drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
   3559   1.1  riastrad 		}
   3560   1.1  riastrad 
   3561   1.1  riastrad 		queue_work(system_long_wq, &mgr->work);
   3562   1.1  riastrad 
   3563   1.1  riastrad 		ret = 0;
   3564   1.1  riastrad 	} else {
   3565   1.1  riastrad 		/* disable MST on the device */
   3566   1.1  riastrad 		mstb = mgr->mst_primary;
   3567   1.1  riastrad 		mgr->mst_primary = NULL;
   3568   1.1  riastrad 		/* this can fail if the device is gone */
   3569   1.1  riastrad 		drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
   3570   1.1  riastrad 		ret = 0;
   3571   1.6  riastrad 		mutex_lock(&mgr->payload_lock);
   3572   1.1  riastrad 		memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
   3573   1.1  riastrad 		mgr->payload_mask = 0;
   3574   1.1  riastrad 		set_bit(0, &mgr->payload_mask);
   3575   1.6  riastrad 		for (i = 0; i < mgr->max_payloads; i++) {
   3576   1.6  riastrad 			struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
   3577   1.6  riastrad 
   3578   1.6  riastrad 			if (vcpi) {
   3579   1.6  riastrad 				vcpi->vcpi = 0;
   3580   1.6  riastrad 				vcpi->num_slots = 0;
   3581   1.6  riastrad 			}
   3582   1.6  riastrad 			mgr->proposed_vcpis[i] = NULL;
   3583   1.6  riastrad 		}
   3584   1.1  riastrad 		mgr->vcpi_mask = 0;
   3585   1.6  riastrad 		mutex_unlock(&mgr->payload_lock);
   3586   1.6  riastrad 
   3587   1.6  riastrad 		mgr->payload_id_table_cleared = false;
   3588   1.1  riastrad 	}
   3589   1.1  riastrad 
   3590   1.1  riastrad out_unlock:
   3591   1.1  riastrad 	mutex_unlock(&mgr->lock);
   3592   1.1  riastrad 	if (mstb)
   3593   1.6  riastrad 		drm_dp_mst_topology_put_mstb(mstb);
   3594   1.1  riastrad 	return ret;
   3595   1.1  riastrad 
   3596   1.1  riastrad }
   3597   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
   3598   1.1  riastrad 
   3599   1.6  riastrad static void
   3600   1.6  riastrad drm_dp_mst_topology_mgr_invalidate_mstb(struct drm_dp_mst_branch *mstb)
   3601   1.6  riastrad {
   3602   1.6  riastrad 	struct drm_dp_mst_port *port;
   3603   1.6  riastrad 
   3604   1.6  riastrad 	/* The link address will need to be re-sent on resume */
   3605   1.6  riastrad 	mstb->link_address_sent = false;
   3606   1.6  riastrad 
   3607   1.6  riastrad 	list_for_each_entry(port, &mstb->ports, next) {
   3608   1.6  riastrad 		/* The PBN for each port will also need to be re-probed */
   3609   1.6  riastrad 		port->available_pbn = 0;
   3610   1.6  riastrad 
   3611   1.6  riastrad 		if (port->mstb)
   3612   1.6  riastrad 			drm_dp_mst_topology_mgr_invalidate_mstb(port->mstb);
   3613   1.6  riastrad 	}
   3614   1.6  riastrad }
   3615   1.6  riastrad 
   3616   1.1  riastrad /**
   3617   1.1  riastrad  * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
   3618   1.1  riastrad  * @mgr: manager to suspend
   3619   1.1  riastrad  *
   3620   1.1  riastrad  * This function tells the MST device that we can't handle UP messages
   3621   1.1  riastrad  * anymore. This should stop it from sending any since we are suspended.
   3622   1.1  riastrad  */
   3623   1.1  riastrad void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
   3624   1.1  riastrad {
   3625   1.1  riastrad 	mutex_lock(&mgr->lock);
   3626   1.1  riastrad 	drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
   3627   1.1  riastrad 			   DP_MST_EN | DP_UPSTREAM_IS_SRC);
   3628   1.1  riastrad 	mutex_unlock(&mgr->lock);
   3629   1.6  riastrad 	flush_work(&mgr->up_req_work);
   3630   1.1  riastrad 	flush_work(&mgr->work);
   3631   1.6  riastrad 	flush_work(&mgr->delayed_destroy_work);
   3632   1.6  riastrad 
   3633   1.6  riastrad 	mutex_lock(&mgr->lock);
   3634   1.6  riastrad 	if (mgr->mst_state && mgr->mst_primary)
   3635   1.6  riastrad 		drm_dp_mst_topology_mgr_invalidate_mstb(mgr->mst_primary);
   3636   1.6  riastrad 	mutex_unlock(&mgr->lock);
   3637   1.1  riastrad }
   3638   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
   3639   1.1  riastrad 
   3640   1.1  riastrad /**
   3641   1.1  riastrad  * drm_dp_mst_topology_mgr_resume() - resume the MST manager
   3642   1.1  riastrad  * @mgr: manager to resume
   3643   1.6  riastrad  * @sync: whether or not to perform topology reprobing synchronously
   3644   1.1  riastrad  *
   3645   1.1  riastrad  * This will fetch DPCD and see if the device is still there,
   3646   1.1  riastrad  * if it is, it will rewrite the MSTM control bits, and return.
   3647   1.1  riastrad  *
   3648   1.6  riastrad  * If the device fails this returns -1, and the driver should do
   3649   1.1  riastrad  * a full MST reprobe, in case we were undocked.
   3650   1.6  riastrad  *
   3651   1.6  riastrad  * During system resume (where it is assumed that the driver will be calling
   3652   1.6  riastrad  * drm_atomic_helper_resume()) this function should be called beforehand with
   3653   1.6  riastrad  * @sync set to true. In contexts like runtime resume where the driver is not
   3654   1.6  riastrad  * expected to be calling drm_atomic_helper_resume(), this function should be
   3655   1.6  riastrad  * called with @sync set to false in order to avoid deadlocking.
   3656   1.6  riastrad  *
   3657   1.6  riastrad  * Returns: -1 if the MST topology was removed while we were suspended, 0
   3658   1.6  riastrad  * otherwise.
   3659   1.1  riastrad  */
   3660   1.6  riastrad int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr,
   3661   1.6  riastrad 				   bool sync)
   3662   1.1  riastrad {
   3663   1.6  riastrad 	int ret;
   3664   1.6  riastrad 	u8 guid[16];
   3665   1.1  riastrad 
   3666   1.1  riastrad 	mutex_lock(&mgr->lock);
   3667   1.6  riastrad 	if (!mgr->mst_primary)
   3668   1.6  riastrad 		goto out_fail;
   3669   1.6  riastrad 
   3670   1.6  riastrad 	ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd,
   3671   1.6  riastrad 			       DP_RECEIVER_CAP_SIZE);
   3672   1.6  riastrad 	if (ret != DP_RECEIVER_CAP_SIZE) {
   3673   1.6  riastrad 		DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
   3674   1.6  riastrad 		goto out_fail;
   3675   1.6  riastrad 	}
   3676   1.1  riastrad 
   3677   1.6  riastrad 	ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
   3678   1.6  riastrad 				 DP_MST_EN |
   3679   1.6  riastrad 				 DP_UP_REQ_EN |
   3680   1.6  riastrad 				 DP_UPSTREAM_IS_SRC);
   3681   1.6  riastrad 	if (ret < 0) {
   3682   1.6  riastrad 		DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
   3683   1.6  riastrad 		goto out_fail;
   3684   1.6  riastrad 	}
   3685   1.1  riastrad 
   3686   1.6  riastrad 	/* Some hubs forget their guids after they resume */
   3687   1.6  riastrad 	ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
   3688   1.6  riastrad 	if (ret != 16) {
   3689   1.6  riastrad 		DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
   3690   1.6  riastrad 		goto out_fail;
   3691   1.6  riastrad 	}
   3692   1.6  riastrad 	drm_dp_check_mstb_guid(mgr->mst_primary, guid);
   3693   1.1  riastrad 
   3694   1.6  riastrad 	/*
   3695   1.6  riastrad 	 * For the final step of resuming the topology, we need to bring the
   3696   1.6  riastrad 	 * state of our in-memory topology back into sync with reality. So,
   3697   1.6  riastrad 	 * restart the probing process as if we're probing a new hub
   3698   1.6  riastrad 	 */
   3699   1.6  riastrad 	queue_work(system_long_wq, &mgr->work);
   3700   1.6  riastrad 	mutex_unlock(&mgr->lock);
   3701   1.1  riastrad 
   3702   1.6  riastrad 	if (sync) {
   3703   1.6  riastrad 		DRM_DEBUG_KMS("Waiting for link probe work to finish re-syncing topology...\n");
   3704   1.6  riastrad 		flush_work(&mgr->work);
   3705   1.6  riastrad 	}
   3706   1.1  riastrad 
   3707   1.6  riastrad 	return 0;
   3708   1.1  riastrad 
   3709   1.6  riastrad out_fail:
   3710   1.1  riastrad 	mutex_unlock(&mgr->lock);
   3711   1.6  riastrad 	return -1;
   3712   1.1  riastrad }
   3713   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
   3714   1.1  riastrad 
   3715   1.1  riastrad static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
   3716   1.1  riastrad {
   3717   1.1  riastrad 	int len;
   3718   1.1  riastrad 	u8 replyblock[32];
   3719   1.3  riastrad 	int replylen, origlen __unused, curreply;
   3720   1.1  riastrad 	int ret;
   3721   1.1  riastrad 	struct drm_dp_sideband_msg_rx *msg;
   3722   1.1  riastrad 	int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
   3723   1.1  riastrad 	msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
   3724   1.1  riastrad 
   3725   1.1  riastrad 	len = min(mgr->max_dpcd_transaction_bytes, 16);
   3726   1.1  riastrad 	ret = drm_dp_dpcd_read(mgr->aux, basereg,
   3727   1.1  riastrad 			       replyblock, len);
   3728   1.1  riastrad 	if (ret != len) {
   3729   1.1  riastrad 		DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
   3730   1.1  riastrad 		return false;
   3731   1.1  riastrad 	}
   3732   1.1  riastrad 	ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
   3733   1.1  riastrad 	if (!ret) {
   3734   1.1  riastrad 		DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
   3735   1.1  riastrad 		return false;
   3736   1.1  riastrad 	}
   3737   1.1  riastrad 	replylen = msg->curchunk_len + msg->curchunk_hdrlen;
   3738   1.1  riastrad 
   3739   1.1  riastrad 	origlen = replylen;
   3740   1.1  riastrad 	replylen -= len;
   3741   1.1  riastrad 	curreply = len;
   3742   1.1  riastrad 	while (replylen > 0) {
   3743   1.1  riastrad 		len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
   3744   1.1  riastrad 		ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
   3745   1.1  riastrad 				    replyblock, len);
   3746   1.1  riastrad 		if (ret != len) {
   3747   1.1  riastrad 			DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
   3748   1.1  riastrad 				      len, ret);
   3749   1.1  riastrad 			return false;
   3750   1.1  riastrad 		}
   3751   1.1  riastrad 
   3752   1.1  riastrad 		ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
   3753   1.1  riastrad 		if (!ret) {
   3754   1.1  riastrad 			DRM_DEBUG_KMS("failed to build sideband msg\n");
   3755   1.1  riastrad 			return false;
   3756   1.1  riastrad 		}
   3757   1.1  riastrad 
   3758   1.1  riastrad 		curreply += len;
   3759   1.1  riastrad 		replylen -= len;
   3760   1.1  riastrad 	}
   3761   1.1  riastrad 	return true;
   3762   1.1  riastrad }
   3763   1.1  riastrad 
   3764   1.1  riastrad static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
   3765   1.1  riastrad {
   3766   1.6  riastrad 	struct drm_dp_sideband_msg_tx *txmsg;
   3767   1.6  riastrad 	struct drm_dp_mst_branch *mstb;
   3768   1.6  riastrad 	struct drm_dp_sideband_msg_hdr *hdr = &mgr->down_rep_recv.initial_hdr;
   3769   1.6  riastrad 	int slot = -1;
   3770   1.6  riastrad 
   3771   1.6  riastrad 	if (!drm_dp_get_one_sb_msg(mgr, false))
   3772   1.6  riastrad 		goto clear_down_rep_recv;
   3773   1.1  riastrad 
   3774   1.6  riastrad 	if (!mgr->down_rep_recv.have_eomt)
   3775   1.1  riastrad 		return 0;
   3776   1.6  riastrad 
   3777   1.6  riastrad 	mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad);
   3778   1.6  riastrad 	if (!mstb) {
   3779   1.6  riastrad 		DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
   3780   1.6  riastrad 			      hdr->lct);
   3781   1.6  riastrad 		goto clear_down_rep_recv;
   3782   1.1  riastrad 	}
   3783   1.1  riastrad 
   3784   1.6  riastrad 	/* find the message */
   3785   1.6  riastrad 	slot = hdr->seqno;
   3786   1.6  riastrad 	mutex_lock(&mgr->qlock);
   3787   1.6  riastrad 	txmsg = mstb->tx_slots[slot];
   3788   1.6  riastrad 	/* remove from slots */
   3789   1.6  riastrad 	mutex_unlock(&mgr->qlock);
   3790   1.6  riastrad 
   3791   1.6  riastrad 	if (!txmsg) {
   3792   1.6  riastrad 		DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
   3793   1.6  riastrad 			      mstb, hdr->seqno, hdr->lct, hdr->rad[0],
   3794   1.6  riastrad 			      mgr->down_rep_recv.msg[0]);
   3795   1.6  riastrad 		goto no_msg;
   3796   1.6  riastrad 	}
   3797   1.6  riastrad 
   3798   1.6  riastrad 	drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
   3799   1.6  riastrad 
   3800   1.6  riastrad 	if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
   3801   1.6  riastrad 		DRM_DEBUG_KMS("Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
   3802   1.6  riastrad 			      txmsg->reply.req_type,
   3803   1.6  riastrad 			      drm_dp_mst_req_type_str(txmsg->reply.req_type),
   3804   1.6  riastrad 			      txmsg->reply.u.nak.reason,
   3805   1.6  riastrad 			      drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),
   3806   1.6  riastrad 			      txmsg->reply.u.nak.nak_data);
   3807   1.6  riastrad 
   3808   1.6  riastrad 	memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
   3809   1.6  riastrad 	drm_dp_mst_topology_put_mstb(mstb);
   3810   1.6  riastrad 
   3811   1.6  riastrad 	mutex_lock(&mgr->qlock);
   3812   1.6  riastrad 	txmsg->state = DRM_DP_SIDEBAND_TX_RX;
   3813   1.6  riastrad 	mstb->tx_slots[slot] = NULL;
   3814   1.6  riastrad 	mgr->is_waiting_for_dwn_reply = false;
   3815  1.12  riastrad 	DRM_WAKEUP_ALL(&mgr->tx_waitq, &mgr->qlock);
   3816   1.6  riastrad 	mutex_unlock(&mgr->qlock);
   3817   1.6  riastrad 
   3818   1.6  riastrad 	return 0;
   3819   1.6  riastrad 
   3820   1.6  riastrad no_msg:
   3821   1.6  riastrad 	drm_dp_mst_topology_put_mstb(mstb);
   3822   1.6  riastrad clear_down_rep_recv:
   3823   1.6  riastrad 	mutex_lock(&mgr->qlock);
   3824   1.6  riastrad 	mgr->is_waiting_for_dwn_reply = false;
   3825   1.6  riastrad 	mutex_unlock(&mgr->qlock);
   3826   1.6  riastrad 	memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
   3827   1.6  riastrad 
   3828   1.6  riastrad 	return 0;
   3829   1.6  riastrad }
   3830   1.6  riastrad 
   3831   1.6  riastrad static inline bool
   3832   1.6  riastrad drm_dp_mst_process_up_req(struct drm_dp_mst_topology_mgr *mgr,
   3833   1.6  riastrad 			  struct drm_dp_pending_up_req *up_req)
   3834   1.6  riastrad {
   3835   1.6  riastrad 	struct drm_dp_mst_branch *mstb = NULL;
   3836   1.6  riastrad 	struct drm_dp_sideband_msg_req_body *msg = &up_req->msg;
   3837   1.6  riastrad 	struct drm_dp_sideband_msg_hdr *hdr = &up_req->hdr;
   3838   1.6  riastrad 	bool hotplug = false;
   3839   1.6  riastrad 
   3840   1.6  riastrad 	if (hdr->broadcast) {
   3841   1.6  riastrad 		const u8 *guid = NULL;
   3842   1.6  riastrad 
   3843   1.6  riastrad 		if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY)
   3844   1.6  riastrad 			guid = msg->u.conn_stat.guid;
   3845   1.6  riastrad 		else if (msg->req_type == DP_RESOURCE_STATUS_NOTIFY)
   3846   1.6  riastrad 			guid = msg->u.resource_stat.guid;
   3847   1.6  riastrad 
   3848   1.6  riastrad 		if (guid)
   3849   1.6  riastrad 			mstb = drm_dp_get_mst_branch_device_by_guid(mgr, guid);
   3850   1.6  riastrad 	} else {
   3851   1.6  riastrad 		mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad);
   3852   1.6  riastrad 	}
   3853   1.6  riastrad 
   3854   1.6  riastrad 	if (!mstb) {
   3855   1.6  riastrad 		DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
   3856   1.6  riastrad 			      hdr->lct);
   3857   1.6  riastrad 		return false;
   3858   1.6  riastrad 	}
   3859   1.1  riastrad 
   3860   1.6  riastrad 	/* TODO: Add missing handler for DP_RESOURCE_STATUS_NOTIFY events */
   3861   1.6  riastrad 	if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY) {
   3862   1.6  riastrad 		drm_dp_mst_handle_conn_stat(mstb, &msg->u.conn_stat);
   3863   1.6  riastrad 		hotplug = true;
   3864   1.1  riastrad 	}
   3865   1.6  riastrad 
   3866   1.6  riastrad 	drm_dp_mst_topology_put_mstb(mstb);
   3867   1.6  riastrad 	return hotplug;
   3868   1.1  riastrad }
   3869   1.1  riastrad 
   3870   1.6  riastrad static void drm_dp_mst_up_req_work(struct work_struct *work)
   3871   1.1  riastrad {
   3872   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr =
   3873   1.6  riastrad 		container_of(work, struct drm_dp_mst_topology_mgr,
   3874   1.6  riastrad 			     up_req_work);
   3875   1.6  riastrad 	struct drm_dp_pending_up_req *up_req;
   3876   1.6  riastrad 	bool send_hotplug = false;
   3877   1.1  riastrad 
   3878   1.6  riastrad 	mutex_lock(&mgr->probe_lock);
   3879   1.6  riastrad 	while (true) {
   3880   1.6  riastrad 		mutex_lock(&mgr->up_req_lock);
   3881   1.6  riastrad 		up_req = list_first_entry_or_null(&mgr->up_req_list,
   3882   1.6  riastrad 						  struct drm_dp_pending_up_req,
   3883   1.6  riastrad 						  next);
   3884   1.6  riastrad 		if (up_req)
   3885   1.6  riastrad 			list_del(&up_req->next);
   3886   1.6  riastrad 		mutex_unlock(&mgr->up_req_lock);
   3887   1.1  riastrad 
   3888   1.6  riastrad 		if (!up_req)
   3889   1.6  riastrad 			break;
   3890   1.1  riastrad 
   3891   1.6  riastrad 		send_hotplug |= drm_dp_mst_process_up_req(mgr, up_req);
   3892   1.6  riastrad 		kfree(up_req);
   3893   1.6  riastrad 	}
   3894   1.6  riastrad 	mutex_unlock(&mgr->probe_lock);
   3895   1.1  riastrad 
   3896   1.6  riastrad 	if (send_hotplug)
   3897   1.6  riastrad 		drm_kms_helper_hotplug_event(mgr->dev);
   3898   1.6  riastrad }
   3899   1.1  riastrad 
   3900   1.6  riastrad static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
   3901   1.6  riastrad {
   3902   1.6  riastrad 	struct drm_dp_sideband_msg_hdr *hdr = &mgr->up_req_recv.initial_hdr;
   3903   1.6  riastrad 	struct drm_dp_pending_up_req *up_req;
   3904   1.6  riastrad 	bool seqno;
   3905   1.1  riastrad 
   3906   1.6  riastrad 	if (!drm_dp_get_one_sb_msg(mgr, true))
   3907   1.6  riastrad 		goto out;
   3908   1.1  riastrad 
   3909   1.6  riastrad 	if (!mgr->up_req_recv.have_eomt)
   3910   1.6  riastrad 		return 0;
   3911   1.1  riastrad 
   3912   1.6  riastrad 	up_req = kzalloc(sizeof(*up_req), GFP_KERNEL);
   3913   1.6  riastrad 	if (!up_req) {
   3914   1.6  riastrad 		DRM_ERROR("Not enough memory to process MST up req\n");
   3915   1.6  riastrad 		return -ENOMEM;
   3916   1.6  riastrad 	}
   3917   1.6  riastrad 	INIT_LIST_HEAD(&up_req->next);
   3918   1.1  riastrad 
   3919   1.6  riastrad 	seqno = hdr->seqno;
   3920   1.6  riastrad 	drm_dp_sideband_parse_req(&mgr->up_req_recv, &up_req->msg);
   3921   1.1  riastrad 
   3922   1.6  riastrad 	if (up_req->msg.req_type != DP_CONNECTION_STATUS_NOTIFY &&
   3923   1.6  riastrad 	    up_req->msg.req_type != DP_RESOURCE_STATUS_NOTIFY) {
   3924   1.6  riastrad 		DRM_DEBUG_KMS("Received unknown up req type, ignoring: %x\n",
   3925   1.6  riastrad 			      up_req->msg.req_type);
   3926   1.6  riastrad 		kfree(up_req);
   3927   1.6  riastrad 		goto out;
   3928   1.6  riastrad 	}
   3929   1.1  riastrad 
   3930   1.6  riastrad 	drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, up_req->msg.req_type,
   3931   1.6  riastrad 				 seqno, false);
   3932   1.1  riastrad 
   3933   1.6  riastrad 	if (up_req->msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
   3934   1.6  riastrad 		const struct drm_dp_connection_status_notify *conn_stat =
   3935   1.6  riastrad 			&up_req->msg.u.conn_stat;
   3936   1.6  riastrad 
   3937   1.6  riastrad 		DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n",
   3938   1.6  riastrad 			      conn_stat->port_number,
   3939   1.6  riastrad 			      conn_stat->legacy_device_plug_status,
   3940   1.6  riastrad 			      conn_stat->displayport_device_plug_status,
   3941   1.6  riastrad 			      conn_stat->message_capability_status,
   3942   1.6  riastrad 			      conn_stat->input_port,
   3943   1.6  riastrad 			      conn_stat->peer_device_type);
   3944   1.6  riastrad 	} else if (up_req->msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
   3945   1.6  riastrad 		const struct drm_dp_resource_status_notify *res_stat =
   3946   1.6  riastrad 			&up_req->msg.u.resource_stat;
   3947   1.6  riastrad 
   3948   1.6  riastrad 		DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n",
   3949   1.6  riastrad 			      res_stat->port_number,
   3950   1.6  riastrad 			      res_stat->available_pbn);
   3951   1.6  riastrad 	}
   3952   1.6  riastrad 
   3953   1.6  riastrad 	up_req->hdr = *hdr;
   3954   1.6  riastrad 	mutex_lock(&mgr->up_req_lock);
   3955   1.6  riastrad 	list_add_tail(&up_req->next, &mgr->up_req_list);
   3956   1.6  riastrad 	mutex_unlock(&mgr->up_req_lock);
   3957   1.6  riastrad 	queue_work(system_long_wq, &mgr->up_req_work);
   3958   1.1  riastrad 
   3959   1.6  riastrad out:
   3960   1.6  riastrad 	memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
   3961   1.6  riastrad 	return 0;
   3962   1.1  riastrad }
   3963   1.1  riastrad 
   3964   1.1  riastrad /**
   3965   1.1  riastrad  * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
   3966   1.1  riastrad  * @mgr: manager to notify irq for.
   3967   1.1  riastrad  * @esi: 4 bytes from SINK_COUNT_ESI
   3968   1.1  riastrad  * @handled: whether the hpd interrupt was consumed or not
   3969   1.1  riastrad  *
   3970   1.1  riastrad  * This should be called from the driver when it detects a short IRQ,
   3971   1.1  riastrad  * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
   3972   1.1  riastrad  * topology manager will process the sideband messages received as a result
   3973   1.1  riastrad  * of this.
   3974   1.1  riastrad  */
   3975   1.1  riastrad int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
   3976   1.1  riastrad {
   3977   1.1  riastrad 	int ret = 0;
   3978   1.1  riastrad 	int sc;
   3979   1.1  riastrad 	*handled = false;
   3980   1.1  riastrad 	sc = esi[0] & 0x3f;
   3981   1.1  riastrad 
   3982   1.1  riastrad 	if (sc != mgr->sink_count) {
   3983   1.1  riastrad 		mgr->sink_count = sc;
   3984   1.1  riastrad 		*handled = true;
   3985   1.1  riastrad 	}
   3986   1.1  riastrad 
   3987   1.1  riastrad 	if (esi[1] & DP_DOWN_REP_MSG_RDY) {
   3988   1.1  riastrad 		ret = drm_dp_mst_handle_down_rep(mgr);
   3989   1.1  riastrad 		*handled = true;
   3990   1.1  riastrad 	}
   3991   1.1  riastrad 
   3992   1.1  riastrad 	if (esi[1] & DP_UP_REQ_MSG_RDY) {
   3993   1.1  riastrad 		ret |= drm_dp_mst_handle_up_req(mgr);
   3994   1.1  riastrad 		*handled = true;
   3995   1.1  riastrad 	}
   3996   1.1  riastrad 
   3997   1.1  riastrad 	drm_dp_mst_kick_tx(mgr);
   3998   1.1  riastrad 	return ret;
   3999   1.1  riastrad }
   4000   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
   4001   1.1  riastrad 
   4002   1.1  riastrad /**
   4003   1.1  riastrad  * drm_dp_mst_detect_port() - get connection status for an MST port
   4004   1.6  riastrad  * @connector: DRM connector for this port
   4005   1.6  riastrad  * @ctx: The acquisition context to use for grabbing locks
   4006   1.1  riastrad  * @mgr: manager for this port
   4007   1.6  riastrad  * @port: pointer to a port
   4008   1.1  riastrad  *
   4009   1.6  riastrad  * This returns the current connection state for a port.
   4010   1.1  riastrad  */
   4011   1.6  riastrad int
   4012   1.6  riastrad drm_dp_mst_detect_port(struct drm_connector *connector,
   4013   1.6  riastrad 		       struct drm_modeset_acquire_ctx *ctx,
   4014   1.6  riastrad 		       struct drm_dp_mst_topology_mgr *mgr,
   4015   1.6  riastrad 		       struct drm_dp_mst_port *port)
   4016   1.1  riastrad {
   4017   1.6  riastrad 	int ret;
   4018   1.1  riastrad 
   4019   1.6  riastrad 	/* we need to search for the port in the mgr in case it's gone */
   4020   1.6  riastrad 	port = drm_dp_mst_topology_get_port_validated(mgr, port);
   4021   1.1  riastrad 	if (!port)
   4022   1.1  riastrad 		return connector_status_disconnected;
   4023   1.1  riastrad 
   4024   1.6  riastrad 	ret = drm_modeset_lock(&mgr->base.lock, ctx);
   4025   1.6  riastrad 	if (ret)
   4026   1.6  riastrad 		goto out;
   4027   1.6  riastrad 
   4028   1.6  riastrad 	ret = connector_status_disconnected;
   4029   1.6  riastrad 
   4030   1.1  riastrad 	if (!port->ddps)
   4031   1.1  riastrad 		goto out;
   4032   1.1  riastrad 
   4033   1.1  riastrad 	switch (port->pdt) {
   4034   1.1  riastrad 	case DP_PEER_DEVICE_NONE:
   4035   1.1  riastrad 	case DP_PEER_DEVICE_MST_BRANCHING:
   4036   1.6  riastrad 		if (!port->mcs)
   4037   1.6  riastrad 			ret = connector_status_connected;
   4038   1.1  riastrad 		break;
   4039   1.1  riastrad 
   4040   1.1  riastrad 	case DP_PEER_DEVICE_SST_SINK:
   4041   1.6  riastrad 		ret = connector_status_connected;
   4042   1.1  riastrad 		/* for logical ports - cache the EDID */
   4043   1.1  riastrad 		if (port->port_num >= 8 && !port->cached_edid) {
   4044   1.1  riastrad 			port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
   4045   1.1  riastrad 		}
   4046   1.1  riastrad 		break;
   4047   1.1  riastrad 	case DP_PEER_DEVICE_DP_LEGACY_CONV:
   4048   1.1  riastrad 		if (port->ldps)
   4049   1.6  riastrad 			ret = connector_status_connected;
   4050   1.1  riastrad 		break;
   4051   1.1  riastrad 	}
   4052   1.1  riastrad out:
   4053   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   4054   1.6  riastrad 	return ret;
   4055   1.1  riastrad }
   4056   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_detect_port);
   4057   1.1  riastrad 
   4058   1.1  riastrad /**
   4059   1.6  riastrad  * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
   4060   1.6  riastrad  * @mgr: manager for this port
   4061   1.6  riastrad  * @port: unverified pointer to a port.
   4062   1.6  riastrad  *
   4063   1.6  riastrad  * This returns whether the port supports audio or not.
   4064   1.6  riastrad  */
   4065   1.6  riastrad bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
   4066   1.6  riastrad 					struct drm_dp_mst_port *port)
   4067   1.6  riastrad {
   4068   1.6  riastrad 	bool ret = false;
   4069   1.6  riastrad 
   4070   1.6  riastrad 	port = drm_dp_mst_topology_get_port_validated(mgr, port);
   4071   1.6  riastrad 	if (!port)
   4072   1.6  riastrad 		return ret;
   4073   1.6  riastrad 	ret = port->has_audio;
   4074   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   4075   1.6  riastrad 	return ret;
   4076   1.6  riastrad }
   4077   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
   4078   1.6  riastrad 
   4079   1.6  riastrad /**
   4080   1.1  riastrad  * drm_dp_mst_get_edid() - get EDID for an MST port
   4081   1.1  riastrad  * @connector: toplevel connector to get EDID for
   4082   1.1  riastrad  * @mgr: manager for this port
   4083   1.1  riastrad  * @port: unverified pointer to a port.
   4084   1.1  riastrad  *
   4085   1.1  riastrad  * This returns an EDID for the port connected to a connector,
   4086   1.1  riastrad  * It validates the pointer still exists so the caller doesn't require a
   4087   1.1  riastrad  * reference.
   4088   1.1  riastrad  */
   4089   1.1  riastrad struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
   4090   1.1  riastrad {
   4091   1.1  riastrad 	struct edid *edid = NULL;
   4092   1.1  riastrad 
   4093   1.6  riastrad 	/* we need to search for the port in the mgr in case it's gone */
   4094   1.6  riastrad 	port = drm_dp_mst_topology_get_port_validated(mgr, port);
   4095   1.1  riastrad 	if (!port)
   4096   1.1  riastrad 		return NULL;
   4097   1.1  riastrad 
   4098   1.1  riastrad 	if (port->cached_edid)
   4099   1.1  riastrad 		edid = drm_edid_duplicate(port->cached_edid);
   4100   1.1  riastrad 	else {
   4101   1.1  riastrad 		edid = drm_get_edid(connector, &port->aux.ddc);
   4102   1.1  riastrad 	}
   4103   1.6  riastrad 	port->has_audio = drm_detect_monitor_audio(edid);
   4104   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   4105   1.1  riastrad 	return edid;
   4106   1.1  riastrad }
   4107   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_get_edid);
   4108   1.1  riastrad 
   4109   1.1  riastrad /**
   4110   1.6  riastrad  * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
   4111   1.1  riastrad  * @mgr: manager to use
   4112   1.1  riastrad  * @pbn: payload bandwidth to convert into slots.
   4113   1.6  riastrad  *
   4114   1.6  riastrad  * Calculate the number of VCPI slots that will be required for the given PBN
   4115   1.6  riastrad  * value. This function is deprecated, and should not be used in atomic
   4116   1.6  riastrad  * drivers.
   4117   1.6  riastrad  *
   4118   1.6  riastrad  * RETURNS:
   4119   1.6  riastrad  * The total slots required for this port, or error.
   4120   1.1  riastrad  */
   4121   1.1  riastrad int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
   4122   1.1  riastrad 			   int pbn)
   4123   1.1  riastrad {
   4124   1.1  riastrad 	int num_slots;
   4125   1.1  riastrad 
   4126   1.1  riastrad 	num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
   4127   1.1  riastrad 
   4128   1.6  riastrad 	/* max. time slots - one slot for MTP header */
   4129   1.6  riastrad 	if (num_slots > 63)
   4130   1.1  riastrad 		return -ENOSPC;
   4131   1.1  riastrad 	return num_slots;
   4132   1.1  riastrad }
   4133   1.1  riastrad EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
   4134   1.1  riastrad 
   4135   1.1  riastrad static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
   4136   1.6  riastrad 			    struct drm_dp_vcpi *vcpi, int pbn, int slots)
   4137   1.1  riastrad {
   4138   1.1  riastrad 	int ret;
   4139   1.1  riastrad 
   4140   1.6  riastrad 	/* max. time slots - one slot for MTP header */
   4141   1.6  riastrad 	if (slots > 63)
   4142   1.1  riastrad 		return -ENOSPC;
   4143   1.1  riastrad 
   4144   1.1  riastrad 	vcpi->pbn = pbn;
   4145   1.6  riastrad 	vcpi->aligned_pbn = slots * mgr->pbn_div;
   4146   1.6  riastrad 	vcpi->num_slots = slots;
   4147   1.1  riastrad 
   4148   1.1  riastrad 	ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
   4149   1.1  riastrad 	if (ret < 0)
   4150   1.1  riastrad 		return ret;
   4151   1.1  riastrad 	return 0;
   4152   1.1  riastrad }
   4153   1.1  riastrad 
   4154   1.1  riastrad /**
   4155   1.6  riastrad  * drm_dp_atomic_find_vcpi_slots() - Find and add VCPI slots to the state
   4156   1.6  riastrad  * @state: global atomic state
   4157   1.6  riastrad  * @mgr: MST topology manager for the port
   4158   1.6  riastrad  * @port: port to find vcpi slots for
   4159   1.6  riastrad  * @pbn: bandwidth required for the mode in PBN
   4160   1.6  riastrad  * @pbn_div: divider for DSC mode that takes FEC into account
   4161   1.6  riastrad  *
   4162   1.6  riastrad  * Allocates VCPI slots to @port, replacing any previous VCPI allocations it
   4163   1.6  riastrad  * may have had. Any atomic drivers which support MST must call this function
   4164   1.6  riastrad  * in their &drm_encoder_helper_funcs.atomic_check() callback to change the
   4165   1.6  riastrad  * current VCPI allocation for the new state, but only when
   4166   1.6  riastrad  * &drm_crtc_state.mode_changed or &drm_crtc_state.connectors_changed is set
   4167   1.6  riastrad  * to ensure compatibility with userspace applications that still use the
   4168   1.6  riastrad  * legacy modesetting UAPI.
   4169   1.6  riastrad  *
   4170   1.6  riastrad  * Allocations set by this function are not checked against the bandwidth
   4171   1.6  riastrad  * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
   4172   1.6  riastrad  *
   4173   1.6  riastrad  * Additionally, it is OK to call this function multiple times on the same
   4174   1.6  riastrad  * @port as needed. It is not OK however, to call this function and
   4175   1.6  riastrad  * drm_dp_atomic_release_vcpi_slots() in the same atomic check phase.
   4176   1.6  riastrad  *
   4177   1.6  riastrad  * See also:
   4178   1.6  riastrad  * drm_dp_atomic_release_vcpi_slots()
   4179   1.6  riastrad  * drm_dp_mst_atomic_check()
   4180   1.6  riastrad  *
   4181   1.6  riastrad  * Returns:
   4182   1.6  riastrad  * Total slots in the atomic state assigned for this port, or a negative error
   4183   1.6  riastrad  * code if the port no longer exists
   4184   1.6  riastrad  */
   4185   1.6  riastrad int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
   4186   1.6  riastrad 				  struct drm_dp_mst_topology_mgr *mgr,
   4187   1.6  riastrad 				  struct drm_dp_mst_port *port, int pbn,
   4188   1.6  riastrad 				  int pbn_div)
   4189   1.6  riastrad {
   4190   1.6  riastrad 	struct drm_dp_mst_topology_state *topology_state;
   4191   1.6  riastrad 	struct drm_dp_vcpi_allocation *pos, *vcpi = NULL;
   4192   1.6  riastrad 	int prev_slots, prev_bw, req_slots;
   4193   1.6  riastrad 
   4194   1.6  riastrad 	topology_state = drm_atomic_get_mst_topology_state(state, mgr);
   4195   1.6  riastrad 	if (IS_ERR(topology_state))
   4196   1.6  riastrad 		return PTR_ERR(topology_state);
   4197   1.6  riastrad 
   4198   1.6  riastrad 	/* Find the current allocation for this port, if any */
   4199   1.6  riastrad 	list_for_each_entry(pos, &topology_state->vcpis, next) {
   4200   1.6  riastrad 		if (pos->port == port) {
   4201   1.6  riastrad 			vcpi = pos;
   4202   1.6  riastrad 			prev_slots = vcpi->vcpi;
   4203   1.6  riastrad 			prev_bw = vcpi->pbn;
   4204   1.6  riastrad 
   4205   1.6  riastrad 			/*
   4206   1.6  riastrad 			 * This should never happen, unless the driver tries
   4207   1.6  riastrad 			 * releasing and allocating the same VCPI allocation,
   4208   1.6  riastrad 			 * which is an error
   4209   1.6  riastrad 			 */
   4210   1.6  riastrad 			if (WARN_ON(!prev_slots)) {
   4211   1.6  riastrad 				DRM_ERROR("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n",
   4212   1.6  riastrad 					  port);
   4213   1.6  riastrad 				return -EINVAL;
   4214   1.6  riastrad 			}
   4215   1.6  riastrad 
   4216   1.6  riastrad 			break;
   4217   1.6  riastrad 		}
   4218   1.6  riastrad 	}
   4219   1.6  riastrad 	if (!vcpi) {
   4220   1.6  riastrad 		prev_slots = 0;
   4221   1.6  riastrad 		prev_bw = 0;
   4222   1.6  riastrad 	}
   4223   1.6  riastrad 
   4224   1.6  riastrad 	if (pbn_div <= 0)
   4225   1.6  riastrad 		pbn_div = mgr->pbn_div;
   4226   1.6  riastrad 
   4227   1.6  riastrad 	req_slots = DIV_ROUND_UP(pbn, pbn_div);
   4228   1.6  riastrad 
   4229   1.6  riastrad 	DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n",
   4230   1.6  riastrad 			 port->connector->base.id, port->connector->name,
   4231   1.6  riastrad 			 port, prev_slots, req_slots);
   4232   1.6  riastrad 	DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n",
   4233   1.6  riastrad 			 port->connector->base.id, port->connector->name,
   4234   1.6  riastrad 			 port, prev_bw, pbn);
   4235   1.6  riastrad 
   4236   1.6  riastrad 	/* Add the new allocation to the state */
   4237   1.6  riastrad 	if (!vcpi) {
   4238   1.6  riastrad 		vcpi = kzalloc(sizeof(*vcpi), GFP_KERNEL);
   4239   1.6  riastrad 		if (!vcpi)
   4240   1.6  riastrad 			return -ENOMEM;
   4241   1.6  riastrad 
   4242   1.6  riastrad 		drm_dp_mst_get_port_malloc(port);
   4243   1.6  riastrad 		vcpi->port = port;
   4244   1.6  riastrad 		list_add(&vcpi->next, &topology_state->vcpis);
   4245   1.6  riastrad 	}
   4246   1.6  riastrad 	vcpi->vcpi = req_slots;
   4247   1.6  riastrad 	vcpi->pbn = pbn;
   4248   1.6  riastrad 
   4249   1.6  riastrad 	return req_slots;
   4250   1.6  riastrad }
   4251   1.6  riastrad EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
   4252   1.6  riastrad 
   4253   1.6  riastrad /**
   4254   1.6  riastrad  * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
   4255   1.6  riastrad  * @state: global atomic state
   4256   1.6  riastrad  * @mgr: MST topology manager for the port
   4257   1.6  riastrad  * @port: The port to release the VCPI slots from
   4258   1.6  riastrad  *
   4259   1.6  riastrad  * Releases any VCPI slots that have been allocated to a port in the atomic
   4260   1.6  riastrad  * state. Any atomic drivers which support MST must call this function in
   4261   1.6  riastrad  * their &drm_connector_helper_funcs.atomic_check() callback when the
   4262   1.6  riastrad  * connector will no longer have VCPI allocated (e.g. because its CRTC was
   4263   1.6  riastrad  * removed) when it had VCPI allocated in the previous atomic state.
   4264   1.6  riastrad  *
   4265   1.6  riastrad  * It is OK to call this even if @port has been removed from the system.
   4266   1.6  riastrad  * Additionally, it is OK to call this function multiple times on the same
   4267   1.6  riastrad  * @port as needed. It is not OK however, to call this function and
   4268   1.6  riastrad  * drm_dp_atomic_find_vcpi_slots() on the same @port in a single atomic check
   4269   1.6  riastrad  * phase.
   4270   1.6  riastrad  *
   4271   1.6  riastrad  * See also:
   4272   1.6  riastrad  * drm_dp_atomic_find_vcpi_slots()
   4273   1.6  riastrad  * drm_dp_mst_atomic_check()
   4274   1.6  riastrad  *
   4275   1.6  riastrad  * Returns:
   4276   1.6  riastrad  * 0 if all slots for this port were added back to
   4277   1.6  riastrad  * &drm_dp_mst_topology_state.avail_slots or negative error code
   4278   1.6  riastrad  */
   4279   1.6  riastrad int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
   4280   1.6  riastrad 				     struct drm_dp_mst_topology_mgr *mgr,
   4281   1.6  riastrad 				     struct drm_dp_mst_port *port)
   4282   1.6  riastrad {
   4283   1.6  riastrad 	struct drm_dp_mst_topology_state *topology_state;
   4284   1.6  riastrad 	struct drm_dp_vcpi_allocation *pos;
   4285   1.6  riastrad 	bool found = false;
   4286   1.6  riastrad 
   4287   1.6  riastrad 	topology_state = drm_atomic_get_mst_topology_state(state, mgr);
   4288   1.6  riastrad 	if (IS_ERR(topology_state))
   4289   1.6  riastrad 		return PTR_ERR(topology_state);
   4290   1.6  riastrad 
   4291   1.6  riastrad 	list_for_each_entry(pos, &topology_state->vcpis, next) {
   4292   1.6  riastrad 		if (pos->port == port) {
   4293   1.6  riastrad 			found = true;
   4294   1.6  riastrad 			break;
   4295   1.6  riastrad 		}
   4296   1.6  riastrad 	}
   4297   1.6  riastrad 	if (WARN_ON(!found)) {
   4298   1.6  riastrad 		DRM_ERROR("no VCPI for [MST PORT:%p] found in mst state %p\n",
   4299   1.6  riastrad 			  port, &topology_state->base);
   4300   1.6  riastrad 		return -EINVAL;
   4301   1.6  riastrad 	}
   4302   1.6  riastrad 
   4303   1.6  riastrad 	DRM_DEBUG_ATOMIC("[MST PORT:%p] VCPI %d -> 0\n", port, pos->vcpi);
   4304   1.6  riastrad 	if (pos->vcpi) {
   4305   1.6  riastrad 		drm_dp_mst_put_port_malloc(port);
   4306   1.6  riastrad 		pos->vcpi = 0;
   4307   1.6  riastrad 	}
   4308   1.6  riastrad 
   4309   1.6  riastrad 	return 0;
   4310   1.6  riastrad }
   4311   1.6  riastrad EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
   4312   1.6  riastrad 
   4313   1.6  riastrad /**
   4314   1.1  riastrad  * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
   4315   1.1  riastrad  * @mgr: manager for this port
   4316   1.1  riastrad  * @port: port to allocate a virtual channel for.
   4317   1.1  riastrad  * @pbn: payload bandwidth number to request
   4318   1.1  riastrad  * @slots: returned number of slots for this PBN.
   4319   1.1  riastrad  */
   4320   1.6  riastrad bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
   4321   1.6  riastrad 			      struct drm_dp_mst_port *port, int pbn, int slots)
   4322   1.1  riastrad {
   4323   1.1  riastrad 	int ret;
   4324   1.1  riastrad 
   4325   1.6  riastrad 	port = drm_dp_mst_topology_get_port_validated(mgr, port);
   4326   1.1  riastrad 	if (!port)
   4327   1.1  riastrad 		return false;
   4328   1.1  riastrad 
   4329   1.6  riastrad 	if (slots < 0)
   4330   1.6  riastrad 		return false;
   4331   1.6  riastrad 
   4332   1.1  riastrad 	if (port->vcpi.vcpi > 0) {
   4333   1.6  riastrad 		DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
   4334   1.6  riastrad 			      port->vcpi.vcpi, port->vcpi.pbn, pbn);
   4335   1.1  riastrad 		if (pbn == port->vcpi.pbn) {
   4336   1.6  riastrad 			drm_dp_mst_topology_put_port(port);
   4337   1.1  riastrad 			return true;
   4338   1.1  riastrad 		}
   4339   1.1  riastrad 	}
   4340   1.1  riastrad 
   4341   1.6  riastrad 	ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
   4342   1.1  riastrad 	if (ret) {
   4343   1.6  riastrad 		DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
   4344   1.6  riastrad 			      DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
   4345   1.1  riastrad 		goto out;
   4346   1.1  riastrad 	}
   4347   1.6  riastrad 	DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
   4348   1.6  riastrad 		      pbn, port->vcpi.num_slots);
   4349   1.1  riastrad 
   4350   1.6  riastrad 	/* Keep port allocated until its payload has been removed */
   4351   1.6  riastrad 	drm_dp_mst_get_port_malloc(port);
   4352   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   4353   1.1  riastrad 	return true;
   4354   1.1  riastrad out:
   4355   1.1  riastrad 	return false;
   4356   1.1  riastrad }
   4357   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
   4358   1.1  riastrad 
   4359   1.1  riastrad int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
   4360   1.1  riastrad {
   4361   1.1  riastrad 	int slots = 0;
   4362   1.6  riastrad 	port = drm_dp_mst_topology_get_port_validated(mgr, port);
   4363   1.1  riastrad 	if (!port)
   4364   1.1  riastrad 		return slots;
   4365   1.1  riastrad 
   4366   1.1  riastrad 	slots = port->vcpi.num_slots;
   4367   1.6  riastrad 	drm_dp_mst_topology_put_port(port);
   4368   1.1  riastrad 	return slots;
   4369   1.1  riastrad }
   4370   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
   4371   1.1  riastrad 
   4372   1.1  riastrad /**
   4373   1.1  riastrad  * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
   4374   1.1  riastrad  * @mgr: manager for this port
   4375   1.1  riastrad  * @port: unverified pointer to a port.
   4376   1.1  riastrad  *
   4377   1.1  riastrad  * This just resets the number of slots for the ports VCPI for later programming.
   4378   1.1  riastrad  */
   4379   1.1  riastrad void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
   4380   1.1  riastrad {
   4381   1.6  riastrad 	/*
   4382   1.6  riastrad 	 * A port with VCPI will remain allocated until its VCPI is
   4383   1.6  riastrad 	 * released, no verified ref needed
   4384   1.6  riastrad 	 */
   4385   1.6  riastrad 
   4386   1.1  riastrad 	port->vcpi.num_slots = 0;
   4387   1.1  riastrad }
   4388   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
   4389   1.1  riastrad 
   4390   1.1  riastrad /**
   4391   1.1  riastrad  * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
   4392   1.1  riastrad  * @mgr: manager for this port
   4393   1.6  riastrad  * @port: port to deallocate vcpi for
   4394   1.6  riastrad  *
   4395   1.6  riastrad  * This can be called unconditionally, regardless of whether
   4396   1.6  riastrad  * drm_dp_mst_allocate_vcpi() succeeded or not.
   4397   1.1  riastrad  */
   4398   1.6  riastrad void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
   4399   1.6  riastrad 				struct drm_dp_mst_port *port)
   4400   1.1  riastrad {
   4401   1.6  riastrad 	if (!port->vcpi.vcpi)
   4402   1.1  riastrad 		return;
   4403   1.1  riastrad 
   4404   1.1  riastrad 	drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
   4405   1.1  riastrad 	port->vcpi.num_slots = 0;
   4406   1.1  riastrad 	port->vcpi.pbn = 0;
   4407   1.1  riastrad 	port->vcpi.aligned_pbn = 0;
   4408   1.1  riastrad 	port->vcpi.vcpi = 0;
   4409   1.6  riastrad 	drm_dp_mst_put_port_malloc(port);
   4410   1.1  riastrad }
   4411   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
   4412   1.1  riastrad 
   4413   1.1  riastrad static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
   4414   1.1  riastrad 				     int id, struct drm_dp_payload *payload)
   4415   1.1  riastrad {
   4416   1.1  riastrad 	u8 payload_alloc[3], status;
   4417   1.1  riastrad 	int ret;
   4418   1.1  riastrad 	int retries = 0;
   4419   1.1  riastrad 
   4420   1.1  riastrad 	drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
   4421   1.1  riastrad 			   DP_PAYLOAD_TABLE_UPDATED);
   4422   1.1  riastrad 
   4423   1.1  riastrad 	payload_alloc[0] = id;
   4424   1.1  riastrad 	payload_alloc[1] = payload->start_slot;
   4425   1.1  riastrad 	payload_alloc[2] = payload->num_slots;
   4426   1.1  riastrad 
   4427   1.1  riastrad 	ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
   4428   1.1  riastrad 	if (ret != 3) {
   4429   1.1  riastrad 		DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
   4430   1.1  riastrad 		goto fail;
   4431   1.1  riastrad 	}
   4432   1.1  riastrad 
   4433   1.1  riastrad retry:
   4434   1.1  riastrad 	ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
   4435   1.1  riastrad 	if (ret < 0) {
   4436   1.1  riastrad 		DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
   4437   1.1  riastrad 		goto fail;
   4438   1.1  riastrad 	}
   4439   1.1  riastrad 
   4440   1.1  riastrad 	if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
   4441   1.1  riastrad 		retries++;
   4442   1.1  riastrad 		if (retries < 20) {
   4443   1.1  riastrad 			usleep_range(10000, 20000);
   4444   1.1  riastrad 			goto retry;
   4445   1.1  riastrad 		}
   4446   1.1  riastrad 		DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
   4447   1.1  riastrad 		ret = -EINVAL;
   4448   1.1  riastrad 		goto fail;
   4449   1.1  riastrad 	}
   4450   1.1  riastrad 	ret = 0;
   4451   1.1  riastrad fail:
   4452   1.1  riastrad 	return ret;
   4453   1.1  riastrad }
   4454   1.1  riastrad 
   4455   1.1  riastrad 
   4456   1.1  riastrad /**
   4457   1.1  riastrad  * drm_dp_check_act_status() - Check ACT handled status.
   4458   1.1  riastrad  * @mgr: manager to use
   4459   1.1  riastrad  *
   4460   1.1  riastrad  * Check the payload status bits in the DPCD for ACT handled completion.
   4461   1.1  riastrad  */
   4462   1.1  riastrad int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
   4463   1.1  riastrad {
   4464   1.1  riastrad 	u8 status;
   4465   1.1  riastrad 	int ret;
   4466   1.1  riastrad 	int count = 0;
   4467   1.1  riastrad 
   4468   1.1  riastrad 	do {
   4469   1.1  riastrad 		ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
   4470   1.1  riastrad 
   4471   1.1  riastrad 		if (ret < 0) {
   4472   1.1  riastrad 			DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
   4473   1.1  riastrad 			goto fail;
   4474   1.1  riastrad 		}
   4475   1.1  riastrad 
   4476   1.1  riastrad 		if (status & DP_PAYLOAD_ACT_HANDLED)
   4477   1.1  riastrad 			break;
   4478   1.1  riastrad 		count++;
   4479   1.1  riastrad 		udelay(100);
   4480   1.1  riastrad 
   4481   1.1  riastrad 	} while (count < 30);
   4482   1.1  riastrad 
   4483   1.1  riastrad 	if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
   4484   1.1  riastrad 		DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
   4485   1.1  riastrad 		ret = -EINVAL;
   4486   1.1  riastrad 		goto fail;
   4487   1.1  riastrad 	}
   4488   1.1  riastrad 	return 0;
   4489   1.1  riastrad fail:
   4490   1.1  riastrad 	return ret;
   4491   1.1  riastrad }
   4492   1.1  riastrad EXPORT_SYMBOL(drm_dp_check_act_status);
   4493   1.1  riastrad 
   4494   1.1  riastrad /**
   4495   1.1  riastrad  * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
   4496   1.1  riastrad  * @clock: dot clock for the mode
   4497   1.1  riastrad  * @bpp: bpp for the mode.
   4498   1.6  riastrad  * @dsc: DSC mode. If true, bpp has units of 1/16 of a bit per pixel
   4499   1.1  riastrad  *
   4500   1.1  riastrad  * This uses the formula in the spec to calculate the PBN value for a mode.
   4501   1.1  riastrad  */
   4502   1.6  riastrad int drm_dp_calc_pbn_mode(int clock, int bpp, bool dsc)
   4503   1.6  riastrad {
   4504   1.6  riastrad 	/*
   4505   1.6  riastrad 	 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
   4506   1.6  riastrad 	 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
   4507   1.6  riastrad 	 * common multiplier to render an integer PBN for all link rate/lane
   4508   1.6  riastrad 	 * counts combinations
   4509   1.6  riastrad 	 * calculate
   4510   1.6  riastrad 	 * peak_kbps *= (1006/1000)
   4511   1.6  riastrad 	 * peak_kbps *= (64/54)
   4512   1.6  riastrad 	 * peak_kbps *= 8    convert to bytes
   4513   1.6  riastrad 	 *
   4514   1.6  riastrad 	 * If the bpp is in units of 1/16, further divide by 16. Put this
   4515   1.6  riastrad 	 * factor in the numerator rather than the denominator to avoid
   4516   1.6  riastrad 	 * integer overflow
   4517   1.6  riastrad 	 */
   4518   1.6  riastrad 
   4519   1.6  riastrad 	if (dsc)
   4520   1.6  riastrad 		return DIV_ROUND_UP_ULL(mul_u32_u32(clock * (bpp / 16), 64 * 1006),
   4521   1.6  riastrad 					8 * 54 * 1000 * 1000);
   4522   1.6  riastrad 
   4523   1.6  riastrad 	return DIV_ROUND_UP_ULL(mul_u32_u32(clock * bpp, 64 * 1006),
   4524   1.6  riastrad 				8 * 54 * 1000 * 1000);
   4525   1.6  riastrad }
   4526   1.6  riastrad EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
   4527   1.6  riastrad 
   4528   1.6  riastrad /* we want to kick the TX after we've ack the up/down IRQs. */
   4529   1.6  riastrad static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
   4530   1.6  riastrad {
   4531   1.6  riastrad 	queue_work(system_long_wq, &mgr->tx_work);
   4532   1.6  riastrad }
   4533   1.6  riastrad 
   4534   1.6  riastrad #if IS_ENABLED(CONFIG_DEBUG_FS)
   4535   1.6  riastrad static void drm_dp_mst_dump_mstb(struct seq_file *m,
   4536   1.6  riastrad 				 struct drm_dp_mst_branch *mstb)
   4537   1.6  riastrad {
   4538   1.6  riastrad 	struct drm_dp_mst_port *port;
   4539   1.6  riastrad 	int tabs = mstb->lct;
   4540   1.6  riastrad 	char prefix[10];
   4541   1.6  riastrad 	int i;
   4542   1.6  riastrad 
   4543   1.6  riastrad 	for (i = 0; i < tabs; i++)
   4544   1.6  riastrad 		prefix[i] = '\t';
   4545   1.6  riastrad 	prefix[i] = '\0';
   4546   1.6  riastrad 
   4547   1.6  riastrad 	seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
   4548   1.6  riastrad 	list_for_each_entry(port, &mstb->ports, next) {
   4549   1.6  riastrad 		seq_printf(m, "%sport: %d: input: %d: pdt: %d, ddps: %d ldps: %d, sdp: %d/%d, %p, conn: %p\n", prefix, port->port_num, port->input, port->pdt, port->ddps, port->ldps, port->num_sdp_streams, port->num_sdp_stream_sinks, port, port->connector);
   4550   1.6  riastrad 		if (port->mstb)
   4551   1.6  riastrad 			drm_dp_mst_dump_mstb(m, port->mstb);
   4552   1.6  riastrad 	}
   4553   1.6  riastrad }
   4554   1.6  riastrad 
   4555   1.6  riastrad #define DP_PAYLOAD_TABLE_SIZE		64
   4556   1.6  riastrad 
   4557   1.6  riastrad static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
   4558   1.6  riastrad 				  char *buf)
   4559   1.6  riastrad {
   4560   1.6  riastrad 	int i;
   4561   1.6  riastrad 
   4562   1.6  riastrad 	for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
   4563   1.6  riastrad 		if (drm_dp_dpcd_read(mgr->aux,
   4564   1.6  riastrad 				     DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
   4565   1.6  riastrad 				     &buf[i], 16) != 16)
   4566   1.6  riastrad 			return false;
   4567   1.6  riastrad 	}
   4568   1.6  riastrad 	return true;
   4569   1.6  riastrad }
   4570   1.6  riastrad 
   4571   1.6  riastrad static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
   4572   1.6  riastrad 			       struct drm_dp_mst_port *port, char *name,
   4573   1.6  riastrad 			       int namelen)
   4574   1.6  riastrad {
   4575   1.6  riastrad 	struct edid *mst_edid;
   4576   1.6  riastrad 
   4577   1.6  riastrad 	mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
   4578   1.6  riastrad 	drm_edid_get_monitor_name(mst_edid, name, namelen);
   4579   1.6  riastrad }
   4580   1.6  riastrad 
   4581   1.6  riastrad /**
   4582   1.6  riastrad  * drm_dp_mst_dump_topology(): dump topology to seq file.
   4583   1.6  riastrad  * @m: seq_file to dump output to
   4584   1.6  riastrad  * @mgr: manager to dump current topology for.
   4585   1.6  riastrad  *
   4586   1.6  riastrad  * helper to dump MST topology to a seq file for debugfs.
   4587   1.6  riastrad  */
   4588   1.6  riastrad void drm_dp_mst_dump_topology(struct seq_file *m,
   4589   1.6  riastrad 			      struct drm_dp_mst_topology_mgr *mgr)
   4590   1.6  riastrad {
   4591   1.6  riastrad 	int i;
   4592   1.6  riastrad 	struct drm_dp_mst_port *port;
   4593   1.6  riastrad 
   4594   1.6  riastrad 	mutex_lock(&mgr->lock);
   4595   1.6  riastrad 	if (mgr->mst_primary)
   4596   1.6  riastrad 		drm_dp_mst_dump_mstb(m, mgr->mst_primary);
   4597   1.6  riastrad 
   4598   1.6  riastrad 	/* dump VCPIs */
   4599   1.6  riastrad 	mutex_unlock(&mgr->lock);
   4600   1.6  riastrad 
   4601   1.6  riastrad 	mutex_lock(&mgr->payload_lock);
   4602   1.6  riastrad 	seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
   4603   1.6  riastrad 		mgr->max_payloads);
   4604   1.6  riastrad 
   4605   1.6  riastrad 	for (i = 0; i < mgr->max_payloads; i++) {
   4606   1.6  riastrad 		if (mgr->proposed_vcpis[i]) {
   4607   1.6  riastrad 			char name[14];
   4608   1.6  riastrad 
   4609   1.6  riastrad 			port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
   4610   1.6  riastrad 			fetch_monitor_name(mgr, port, name, sizeof(name));
   4611   1.6  riastrad 			seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
   4612   1.6  riastrad 				   port->port_num, port->vcpi.vcpi,
   4613   1.6  riastrad 				   port->vcpi.num_slots,
   4614   1.6  riastrad 				   (*name != 0) ? name :  "Unknown");
   4615   1.6  riastrad 		} else
   4616   1.6  riastrad 			seq_printf(m, "vcpi %d:unused\n", i);
   4617   1.6  riastrad 	}
   4618   1.6  riastrad 	for (i = 0; i < mgr->max_payloads; i++) {
   4619   1.6  riastrad 		seq_printf(m, "payload %d: %d, %d, %d\n",
   4620   1.6  riastrad 			   i,
   4621   1.6  riastrad 			   mgr->payloads[i].payload_state,
   4622   1.6  riastrad 			   mgr->payloads[i].start_slot,
   4623   1.6  riastrad 			   mgr->payloads[i].num_slots);
   4624   1.6  riastrad 
   4625   1.6  riastrad 
   4626   1.6  riastrad 	}
   4627   1.6  riastrad 	mutex_unlock(&mgr->payload_lock);
   4628   1.6  riastrad 
   4629   1.6  riastrad 	mutex_lock(&mgr->lock);
   4630   1.6  riastrad 	if (mgr->mst_primary) {
   4631   1.6  riastrad 		u8 buf[DP_PAYLOAD_TABLE_SIZE];
   4632   1.6  riastrad 		int ret;
   4633   1.6  riastrad 
   4634   1.6  riastrad 		ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
   4635   1.6  riastrad 		seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
   4636   1.6  riastrad 		ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
   4637   1.6  riastrad 		seq_printf(m, "faux/mst: %*ph\n", 2, buf);
   4638   1.6  riastrad 		ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
   4639   1.6  riastrad 		seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
   4640   1.6  riastrad 
   4641   1.6  riastrad 		/* dump the standard OUI branch header */
   4642   1.6  riastrad 		ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
   4643   1.6  riastrad 		seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
   4644   1.6  riastrad 		for (i = 0x3; i < 0x8 && buf[i]; i++)
   4645   1.6  riastrad 			seq_printf(m, "%c", buf[i]);
   4646   1.6  riastrad 		seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
   4647   1.6  riastrad 			   buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
   4648   1.6  riastrad 		if (dump_dp_payload_table(mgr, buf))
   4649   1.6  riastrad 			seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
   4650   1.6  riastrad 	}
   4651   1.6  riastrad 
   4652   1.6  riastrad 	mutex_unlock(&mgr->lock);
   4653   1.6  riastrad 
   4654   1.6  riastrad }
   4655   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_dump_topology);
   4656   1.6  riastrad #endif	/* IS_ENABLED(CONFIG_DEBUG_FS) */
   4657   1.6  riastrad 
   4658   1.6  riastrad static void drm_dp_tx_work(struct work_struct *work)
   4659   1.6  riastrad {
   4660   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
   4661   1.6  riastrad 
   4662   1.6  riastrad 	mutex_lock(&mgr->qlock);
   4663   1.6  riastrad 	if (!list_empty(&mgr->tx_msg_downq) && !mgr->is_waiting_for_dwn_reply)
   4664   1.6  riastrad 		process_single_down_tx_qlock(mgr);
   4665   1.6  riastrad 	mutex_unlock(&mgr->qlock);
   4666   1.6  riastrad }
   4667   1.6  riastrad 
   4668   1.6  riastrad static inline void
   4669   1.6  riastrad drm_dp_delayed_destroy_port(struct drm_dp_mst_port *port)
   4670   1.6  riastrad {
   4671   1.6  riastrad 	if (port->connector)
   4672   1.6  riastrad 		port->mgr->cbs->destroy_connector(port->mgr, port->connector);
   4673   1.6  riastrad 
   4674   1.6  riastrad 	drm_dp_port_set_pdt(port, DP_PEER_DEVICE_NONE, port->mcs);
   4675   1.6  riastrad 	drm_dp_mst_put_port_malloc(port);
   4676   1.6  riastrad }
   4677   1.6  riastrad 
   4678   1.6  riastrad static inline void
   4679   1.6  riastrad drm_dp_delayed_destroy_mstb(struct drm_dp_mst_branch *mstb)
   4680   1.6  riastrad {
   4681   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
   4682   1.6  riastrad 	struct drm_dp_mst_port *port, *tmp;
   4683   1.6  riastrad 	bool wake_tx = false;
   4684   1.6  riastrad 
   4685   1.6  riastrad 	mutex_lock(&mgr->lock);
   4686   1.6  riastrad 	list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
   4687   1.6  riastrad 		list_del(&port->next);
   4688   1.6  riastrad 		drm_dp_mst_topology_put_port(port);
   4689   1.6  riastrad 	}
   4690   1.6  riastrad 	mutex_unlock(&mgr->lock);
   4691   1.6  riastrad 
   4692   1.6  riastrad 	/* drop any tx slots msg */
   4693   1.6  riastrad 	mutex_lock(&mstb->mgr->qlock);
   4694   1.6  riastrad 	if (mstb->tx_slots[0]) {
   4695   1.6  riastrad 		mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
   4696   1.6  riastrad 		mstb->tx_slots[0] = NULL;
   4697   1.6  riastrad 		wake_tx = true;
   4698   1.6  riastrad 	}
   4699   1.6  riastrad 	if (mstb->tx_slots[1]) {
   4700   1.6  riastrad 		mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
   4701   1.6  riastrad 		mstb->tx_slots[1] = NULL;
   4702   1.6  riastrad 		wake_tx = true;
   4703   1.6  riastrad 	}
   4704   1.6  riastrad 	if (wake_tx)
   4705  1.11  riastrad 		DRM_WAKEUP_ALL(&mstb->mgr->tx_waitq, &mstb->mgr->qlock);
   4706  1.12  riastrad 	mutex_unlock(&mstb->mgr->qlock);
   4707   1.6  riastrad 
   4708   1.6  riastrad 	drm_dp_mst_put_mstb_malloc(mstb);
   4709   1.6  riastrad }
   4710   1.6  riastrad 
   4711   1.6  riastrad static void drm_dp_delayed_destroy_work(struct work_struct *work)
   4712   1.6  riastrad {
   4713   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr =
   4714   1.6  riastrad 		container_of(work, struct drm_dp_mst_topology_mgr,
   4715   1.6  riastrad 			     delayed_destroy_work);
   4716   1.6  riastrad 	bool send_hotplug = false, go_again;
   4717   1.6  riastrad 
   4718   1.6  riastrad 	/*
   4719   1.6  riastrad 	 * Not a regular list traverse as we have to drop the destroy
   4720   1.6  riastrad 	 * connector lock before destroying the mstb/port, to avoid AB->BA
   4721   1.6  riastrad 	 * ordering between this lock and the config mutex.
   4722   1.6  riastrad 	 */
   4723   1.6  riastrad 	do {
   4724   1.6  riastrad 		go_again = false;
   4725   1.6  riastrad 
   4726   1.6  riastrad 		for (;;) {
   4727   1.6  riastrad 			struct drm_dp_mst_branch *mstb;
   4728   1.6  riastrad 
   4729   1.6  riastrad 			mutex_lock(&mgr->delayed_destroy_lock);
   4730   1.6  riastrad 			mstb = list_first_entry_or_null(&mgr->destroy_branch_device_list,
   4731   1.6  riastrad 							struct drm_dp_mst_branch,
   4732   1.6  riastrad 							destroy_next);
   4733   1.6  riastrad 			if (mstb)
   4734   1.6  riastrad 				list_del(&mstb->destroy_next);
   4735   1.6  riastrad 			mutex_unlock(&mgr->delayed_destroy_lock);
   4736   1.6  riastrad 
   4737   1.6  riastrad 			if (!mstb)
   4738   1.6  riastrad 				break;
   4739   1.6  riastrad 
   4740   1.6  riastrad 			drm_dp_delayed_destroy_mstb(mstb);
   4741   1.6  riastrad 			go_again = true;
   4742   1.6  riastrad 		}
   4743   1.6  riastrad 
   4744   1.6  riastrad 		for (;;) {
   4745   1.6  riastrad 			struct drm_dp_mst_port *port;
   4746   1.6  riastrad 
   4747   1.6  riastrad 			mutex_lock(&mgr->delayed_destroy_lock);
   4748   1.6  riastrad 			port = list_first_entry_or_null(&mgr->destroy_port_list,
   4749   1.6  riastrad 							struct drm_dp_mst_port,
   4750   1.6  riastrad 							next);
   4751   1.6  riastrad 			if (port)
   4752   1.6  riastrad 				list_del(&port->next);
   4753   1.6  riastrad 			mutex_unlock(&mgr->delayed_destroy_lock);
   4754   1.6  riastrad 
   4755   1.6  riastrad 			if (!port)
   4756   1.6  riastrad 				break;
   4757   1.6  riastrad 
   4758   1.6  riastrad 			drm_dp_delayed_destroy_port(port);
   4759   1.6  riastrad 			send_hotplug = true;
   4760   1.6  riastrad 			go_again = true;
   4761   1.6  riastrad 		}
   4762   1.6  riastrad 	} while (go_again);
   4763   1.6  riastrad 
   4764   1.6  riastrad 	if (send_hotplug)
   4765   1.6  riastrad 		drm_kms_helper_hotplug_event(mgr->dev);
   4766   1.6  riastrad }
   4767   1.6  riastrad 
   4768   1.6  riastrad static struct drm_private_state *
   4769   1.6  riastrad drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
   4770   1.6  riastrad {
   4771   1.6  riastrad 	struct drm_dp_mst_topology_state *state, *old_state =
   4772   1.6  riastrad 		to_dp_mst_topology_state(obj->state);
   4773   1.6  riastrad 	struct drm_dp_vcpi_allocation *pos, *vcpi;
   4774   1.6  riastrad 
   4775   1.6  riastrad 	state = kmemdup(old_state, sizeof(*state), GFP_KERNEL);
   4776   1.6  riastrad 	if (!state)
   4777   1.6  riastrad 		return NULL;
   4778   1.6  riastrad 
   4779   1.6  riastrad 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
   4780   1.6  riastrad 
   4781   1.6  riastrad 	INIT_LIST_HEAD(&state->vcpis);
   4782   1.6  riastrad 
   4783   1.6  riastrad 	list_for_each_entry(pos, &old_state->vcpis, next) {
   4784   1.6  riastrad 		/* Prune leftover freed VCPI allocations */
   4785   1.6  riastrad 		if (!pos->vcpi)
   4786   1.6  riastrad 			continue;
   4787   1.1  riastrad 
   4788   1.6  riastrad 		vcpi = kmemdup(pos, sizeof(*vcpi), GFP_KERNEL);
   4789   1.6  riastrad 		if (!vcpi)
   4790   1.6  riastrad 			goto fail;
   4791   1.1  riastrad 
   4792   1.6  riastrad 		drm_dp_mst_get_port_malloc(vcpi->port);
   4793   1.6  riastrad 		list_add(&vcpi->next, &state->vcpis);
   4794   1.6  riastrad 	}
   4795   1.1  riastrad 
   4796   1.6  riastrad 	return &state->base;
   4797   1.1  riastrad 
   4798   1.6  riastrad fail:
   4799   1.6  riastrad 	list_for_each_entry_safe(pos, vcpi, &state->vcpis, next) {
   4800   1.6  riastrad 		drm_dp_mst_put_port_malloc(pos->port);
   4801   1.6  riastrad 		kfree(pos);
   4802   1.6  riastrad 	}
   4803   1.6  riastrad 	kfree(state);
   4804   1.1  riastrad 
   4805   1.6  riastrad 	return NULL;
   4806   1.1  riastrad }
   4807   1.1  riastrad 
   4808   1.6  riastrad static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
   4809   1.6  riastrad 				     struct drm_private_state *state)
   4810   1.1  riastrad {
   4811   1.6  riastrad 	struct drm_dp_mst_topology_state *mst_state =
   4812   1.6  riastrad 		to_dp_mst_topology_state(state);
   4813   1.6  riastrad 	struct drm_dp_vcpi_allocation *pos, *tmp;
   4814   1.6  riastrad 
   4815   1.6  riastrad 	list_for_each_entry_safe(pos, tmp, &mst_state->vcpis, next) {
   4816   1.6  riastrad 		/* We only keep references to ports with non-zero VCPIs */
   4817   1.6  riastrad 		if (pos->vcpi)
   4818   1.6  riastrad 			drm_dp_mst_put_port_malloc(pos->port);
   4819   1.6  riastrad 		kfree(pos);
   4820   1.1  riastrad 	}
   4821   1.6  riastrad 
   4822   1.6  riastrad 	kfree(mst_state);
   4823   1.1  riastrad }
   4824   1.1  riastrad 
   4825   1.6  riastrad static bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port,
   4826   1.6  riastrad 						 struct drm_dp_mst_branch *branch)
   4827   1.1  riastrad {
   4828   1.6  riastrad 	while (port->parent) {
   4829   1.6  riastrad 		if (port->parent == branch)
   4830   1.6  riastrad 			return true;
   4831   1.6  riastrad 
   4832   1.6  riastrad 		if (port->parent->port_parent)
   4833   1.6  riastrad 			port = port->parent->port_parent;
   4834   1.6  riastrad 		else
   4835   1.6  riastrad 			break;
   4836   1.6  riastrad 	}
   4837   1.6  riastrad 	return false;
   4838   1.1  riastrad }
   4839   1.1  riastrad 
   4840   1.6  riastrad static inline
   4841   1.6  riastrad int drm_dp_mst_atomic_check_bw_limit(struct drm_dp_mst_branch *branch,
   4842   1.6  riastrad 				     struct drm_dp_mst_topology_state *mst_state)
   4843   1.1  riastrad {
   4844   1.1  riastrad 	struct drm_dp_mst_port *port;
   4845   1.6  riastrad 	struct drm_dp_vcpi_allocation *vcpi;
   4846   1.6  riastrad 	int pbn_limit = 0, pbn_used = 0;
   4847   1.6  riastrad 
   4848   1.6  riastrad 	list_for_each_entry(port, &branch->ports, next) {
   4849   1.6  riastrad 		if (port->mstb)
   4850   1.6  riastrad 			if (drm_dp_mst_atomic_check_bw_limit(port->mstb, mst_state))
   4851   1.6  riastrad 				return -ENOSPC;
   4852   1.6  riastrad 
   4853   1.6  riastrad 		if (port->available_pbn > 0)
   4854   1.6  riastrad 			pbn_limit = port->available_pbn;
   4855   1.6  riastrad 	}
   4856   1.6  riastrad 	DRM_DEBUG_ATOMIC("[MST BRANCH:%p] branch has %d PBN available\n",
   4857   1.6  riastrad 			 branch, pbn_limit);
   4858   1.6  riastrad 
   4859   1.6  riastrad 	list_for_each_entry(vcpi, &mst_state->vcpis, next) {
   4860   1.6  riastrad 		if (!vcpi->pbn)
   4861   1.6  riastrad 			continue;
   4862   1.1  riastrad 
   4863   1.6  riastrad 		if (drm_dp_mst_port_downstream_of_branch(vcpi->port, branch))
   4864   1.6  riastrad 			pbn_used += vcpi->pbn;
   4865   1.6  riastrad 	}
   4866   1.6  riastrad 	DRM_DEBUG_ATOMIC("[MST BRANCH:%p] branch used %d PBN\n",
   4867   1.6  riastrad 			 branch, pbn_used);
   4868   1.1  riastrad 
   4869   1.6  riastrad 	if (pbn_used > pbn_limit) {
   4870   1.6  riastrad 		DRM_DEBUG_ATOMIC("[MST BRANCH:%p] No available bandwidth\n",
   4871   1.6  riastrad 				 branch);
   4872   1.6  riastrad 		return -ENOSPC;
   4873   1.1  riastrad 	}
   4874   1.6  riastrad 	return 0;
   4875   1.1  riastrad }
   4876   1.1  riastrad 
   4877   1.6  riastrad static inline int
   4878   1.6  riastrad drm_dp_mst_atomic_check_vcpi_alloc_limit(struct drm_dp_mst_topology_mgr *mgr,
   4879   1.6  riastrad 					 struct drm_dp_mst_topology_state *mst_state)
   4880   1.6  riastrad {
   4881   1.6  riastrad 	struct drm_dp_vcpi_allocation *vcpi;
   4882   1.6  riastrad 	int avail_slots = 63, payload_count = 0;
   4883   1.6  riastrad 
   4884   1.6  riastrad 	list_for_each_entry(vcpi, &mst_state->vcpis, next) {
   4885   1.6  riastrad 		/* Releasing VCPI is always OK-even if the port is gone */
   4886   1.6  riastrad 		if (!vcpi->vcpi) {
   4887   1.6  riastrad 			DRM_DEBUG_ATOMIC("[MST PORT:%p] releases all VCPI slots\n",
   4888   1.6  riastrad 					 vcpi->port);
   4889   1.6  riastrad 			continue;
   4890   1.6  riastrad 		}
   4891   1.6  riastrad 
   4892   1.6  riastrad 		DRM_DEBUG_ATOMIC("[MST PORT:%p] requires %d vcpi slots\n",
   4893   1.6  riastrad 				 vcpi->port, vcpi->vcpi);
   4894   1.6  riastrad 
   4895   1.6  riastrad 		avail_slots -= vcpi->vcpi;
   4896   1.6  riastrad 		if (avail_slots < 0) {
   4897   1.6  riastrad 			DRM_DEBUG_ATOMIC("[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n",
   4898   1.6  riastrad 					 vcpi->port, mst_state,
   4899   1.6  riastrad 					 avail_slots + vcpi->vcpi);
   4900   1.6  riastrad 			return -ENOSPC;
   4901   1.6  riastrad 		}
   4902   1.6  riastrad 
   4903   1.6  riastrad 		if (++payload_count > mgr->max_payloads) {
   4904   1.6  riastrad 			DRM_DEBUG_ATOMIC("[MST MGR:%p] state %p has too many payloads (max=%d)\n",
   4905   1.6  riastrad 					 mgr, mst_state, mgr->max_payloads);
   4906   1.6  riastrad 			return -EINVAL;
   4907   1.6  riastrad 		}
   4908   1.1  riastrad 	}
   4909   1.6  riastrad 	DRM_DEBUG_ATOMIC("[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n",
   4910   1.6  riastrad 			 mgr, mst_state, avail_slots,
   4911   1.6  riastrad 			 63 - avail_slots);
   4912   1.6  riastrad 
   4913   1.6  riastrad 	return 0;
   4914   1.1  riastrad }
   4915   1.1  riastrad 
   4916   1.1  riastrad /**
   4917   1.6  riastrad  * drm_dp_mst_add_affected_dsc_crtcs
   4918   1.6  riastrad  * @state: Pointer to the new struct drm_dp_mst_topology_state
   4919   1.6  riastrad  * @mgr: MST topology manager
   4920   1.6  riastrad  *
   4921   1.6  riastrad  * Whenever there is a change in mst topology
   4922   1.6  riastrad  * DSC configuration would have to be recalculated
   4923   1.6  riastrad  * therefore we need to trigger modeset on all affected
   4924   1.6  riastrad  * CRTCs in that topology
   4925   1.1  riastrad  *
   4926   1.6  riastrad  * See also:
   4927   1.6  riastrad  * drm_dp_mst_atomic_enable_dsc()
   4928   1.1  riastrad  */
   4929   1.6  riastrad int drm_dp_mst_add_affected_dsc_crtcs(struct drm_atomic_state *state, struct drm_dp_mst_topology_mgr *mgr)
   4930   1.1  riastrad {
   4931   1.6  riastrad 	struct drm_dp_mst_topology_state *mst_state;
   4932   1.6  riastrad 	struct drm_dp_vcpi_allocation *pos;
   4933   1.6  riastrad 	struct drm_connector *connector;
   4934   1.6  riastrad 	struct drm_connector_state *conn_state;
   4935   1.6  riastrad 	struct drm_crtc *crtc;
   4936   1.6  riastrad 	struct drm_crtc_state *crtc_state;
   4937   1.6  riastrad 
   4938   1.6  riastrad 	mst_state = drm_atomic_get_mst_topology_state(state, mgr);
   4939   1.1  riastrad 
   4940   1.6  riastrad 	if (IS_ERR(mst_state))
   4941   1.6  riastrad 		return -EINVAL;
   4942   1.1  riastrad 
   4943   1.6  riastrad 	list_for_each_entry(pos, &mst_state->vcpis, next) {
   4944   1.1  riastrad 
   4945   1.6  riastrad 		connector = pos->port->connector;
   4946   1.1  riastrad 
   4947   1.6  riastrad 		if (!connector)
   4948   1.6  riastrad 			return -EINVAL;
   4949   1.1  riastrad 
   4950   1.6  riastrad 		conn_state = drm_atomic_get_connector_state(state, connector);
   4951   1.1  riastrad 
   4952   1.6  riastrad 		if (IS_ERR(conn_state))
   4953   1.6  riastrad 			return PTR_ERR(conn_state);
   4954   1.1  riastrad 
   4955   1.6  riastrad 		crtc = conn_state->crtc;
   4956   1.1  riastrad 
   4957   1.6  riastrad 		if (WARN_ON(!crtc))
   4958   1.6  riastrad 			return -EINVAL;
   4959   1.1  riastrad 
   4960   1.6  riastrad 		if (!drm_dp_mst_dsc_aux_for_port(pos->port))
   4961   1.6  riastrad 			continue;
   4962   1.1  riastrad 
   4963   1.6  riastrad 		crtc_state = drm_atomic_get_crtc_state(mst_state->base.state, crtc);
   4964   1.1  riastrad 
   4965   1.6  riastrad 		if (IS_ERR(crtc_state))
   4966   1.6  riastrad 			return PTR_ERR(crtc_state);
   4967   1.1  riastrad 
   4968   1.6  riastrad 		DRM_DEBUG_ATOMIC("[MST MGR:%p] Setting mode_changed flag on CRTC %p\n",
   4969   1.6  riastrad 				 mgr, crtc);
   4970   1.1  riastrad 
   4971   1.6  riastrad 		crtc_state->mode_changed = true;
   4972   1.6  riastrad 	}
   4973   1.6  riastrad 	return 0;
   4974   1.1  riastrad }
   4975   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_add_affected_dsc_crtcs);
   4976   1.1  riastrad 
   4977   1.6  riastrad /**
   4978   1.6  riastrad  * drm_dp_mst_atomic_enable_dsc - Set DSC Enable Flag to On/Off
   4979   1.6  riastrad  * @state: Pointer to the new drm_atomic_state
   4980   1.6  riastrad  * @port: Pointer to the affected MST Port
   4981   1.6  riastrad  * @pbn: Newly recalculated bw required for link with DSC enabled
   4982   1.6  riastrad  * @pbn_div: Divider to calculate correct number of pbn per slot
   4983   1.6  riastrad  * @enable: Boolean flag to enable or disable DSC on the port
   4984   1.6  riastrad  *
   4985   1.6  riastrad  * This function enables DSC on the given Port
   4986   1.6  riastrad  * by recalculating its vcpi from pbn provided
   4987   1.6  riastrad  * and sets dsc_enable flag to keep track of which
   4988   1.6  riastrad  * ports have DSC enabled
   4989   1.6  riastrad  *
   4990   1.6  riastrad  */
   4991   1.6  riastrad int drm_dp_mst_atomic_enable_dsc(struct drm_atomic_state *state,
   4992   1.6  riastrad 				 struct drm_dp_mst_port *port,
   4993   1.6  riastrad 				 int pbn, int pbn_div,
   4994   1.6  riastrad 				 bool enable)
   4995   1.1  riastrad {
   4996   1.6  riastrad 	struct drm_dp_mst_topology_state *mst_state;
   4997   1.6  riastrad 	struct drm_dp_vcpi_allocation *pos;
   4998   1.6  riastrad 	bool found = false;
   4999   1.6  riastrad 	int vcpi = 0;
   5000   1.6  riastrad 
   5001   1.6  riastrad 	mst_state = drm_atomic_get_mst_topology_state(state, port->mgr);
   5002   1.6  riastrad 
   5003   1.6  riastrad 	if (IS_ERR(mst_state))
   5004   1.6  riastrad 		return PTR_ERR(mst_state);
   5005   1.6  riastrad 
   5006   1.6  riastrad 	list_for_each_entry(pos, &mst_state->vcpis, next) {
   5007   1.6  riastrad 		if (pos->port == port) {
   5008   1.6  riastrad 			found = true;
   5009   1.1  riastrad 			break;
   5010   1.1  riastrad 		}
   5011   1.6  riastrad 	}
   5012   1.6  riastrad 
   5013   1.6  riastrad 	if (!found) {
   5014   1.6  riastrad 		DRM_DEBUG_ATOMIC("[MST PORT:%p] Couldn't find VCPI allocation in mst state %p\n",
   5015   1.6  riastrad 				 port, mst_state);
   5016   1.6  riastrad 		return -EINVAL;
   5017   1.6  riastrad 	}
   5018   1.6  riastrad 
   5019   1.6  riastrad 	if (pos->dsc_enabled == enable) {
   5020   1.6  riastrad 		DRM_DEBUG_ATOMIC("[MST PORT:%p] DSC flag is already set to %d, returning %d VCPI slots\n",
   5021   1.6  riastrad 				 port, enable, pos->vcpi);
   5022   1.6  riastrad 		vcpi = pos->vcpi;
   5023   1.6  riastrad 	}
   5024   1.1  riastrad 
   5025   1.6  riastrad 	if (enable) {
   5026   1.6  riastrad 		vcpi = drm_dp_atomic_find_vcpi_slots(state, port->mgr, port, pbn, pbn_div);
   5027   1.6  riastrad 		DRM_DEBUG_ATOMIC("[MST PORT:%p] Enabling DSC flag, reallocating %d VCPI slots on the port\n",
   5028   1.6  riastrad 				 port, vcpi);
   5029   1.6  riastrad 		if (vcpi < 0)
   5030   1.6  riastrad 			return -EINVAL;
   5031   1.6  riastrad 	}
   5032   1.1  riastrad 
   5033   1.6  riastrad 	pos->dsc_enabled = enable;
   5034   1.1  riastrad 
   5035   1.6  riastrad 	return vcpi;
   5036   1.6  riastrad }
   5037   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_atomic_enable_dsc);
   5038   1.6  riastrad /**
   5039   1.6  riastrad  * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
   5040   1.6  riastrad  * atomic update is valid
   5041   1.6  riastrad  * @state: Pointer to the new &struct drm_dp_mst_topology_state
   5042   1.6  riastrad  *
   5043   1.6  riastrad  * Checks the given topology state for an atomic update to ensure that it's
   5044   1.6  riastrad  * valid. This includes checking whether there's enough bandwidth to support
   5045   1.6  riastrad  * the new VCPI allocations in the atomic update.
   5046   1.6  riastrad  *
   5047   1.6  riastrad  * Any atomic drivers supporting DP MST must make sure to call this after
   5048   1.6  riastrad  * checking the rest of their state in their
   5049   1.6  riastrad  * &drm_mode_config_funcs.atomic_check() callback.
   5050   1.6  riastrad  *
   5051   1.6  riastrad  * See also:
   5052   1.6  riastrad  * drm_dp_atomic_find_vcpi_slots()
   5053   1.6  riastrad  * drm_dp_atomic_release_vcpi_slots()
   5054   1.6  riastrad  *
   5055   1.6  riastrad  * Returns:
   5056   1.6  riastrad  *
   5057   1.6  riastrad  * 0 if the new state is valid, negative error code otherwise.
   5058   1.6  riastrad  */
   5059   1.6  riastrad int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
   5060   1.6  riastrad {
   5061   1.6  riastrad 	struct drm_dp_mst_topology_mgr *mgr;
   5062   1.6  riastrad 	struct drm_dp_mst_topology_state *mst_state;
   5063   1.6  riastrad 	int i, ret = 0;
   5064   1.1  riastrad 
   5065   1.6  riastrad 	for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
   5066   1.6  riastrad 		if (!mgr->mst_state)
   5067   1.6  riastrad 			continue;
   5068   1.1  riastrad 
   5069   1.6  riastrad 		ret = drm_dp_mst_atomic_check_vcpi_alloc_limit(mgr, mst_state);
   5070   1.6  riastrad 		if (ret)
   5071   1.6  riastrad 			break;
   5072   1.6  riastrad 		ret = drm_dp_mst_atomic_check_bw_limit(mgr->mst_primary, mst_state);
   5073   1.6  riastrad 		if (ret)
   5074   1.6  riastrad 			break;
   5075   1.1  riastrad 	}
   5076   1.6  riastrad 
   5077   1.6  riastrad 	return ret;
   5078   1.6  riastrad }
   5079   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_atomic_check);
   5080   1.6  riastrad 
   5081   1.6  riastrad const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
   5082   1.6  riastrad 	.atomic_duplicate_state = drm_dp_mst_duplicate_state,
   5083   1.6  riastrad 	.atomic_destroy_state = drm_dp_mst_destroy_state,
   5084   1.6  riastrad };
   5085   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
   5086   1.6  riastrad 
   5087   1.6  riastrad /**
   5088   1.6  riastrad  * drm_atomic_get_mst_topology_state: get MST topology state
   5089   1.6  riastrad  *
   5090   1.6  riastrad  * @state: global atomic state
   5091   1.6  riastrad  * @mgr: MST topology manager, also the private object in this case
   5092   1.6  riastrad  *
   5093   1.6  riastrad  * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
   5094   1.6  riastrad  * state vtable so that the private object state returned is that of a MST
   5095   1.6  riastrad  * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
   5096   1.6  riastrad  * to care of the locking, so warn if don't hold the connection_mutex.
   5097   1.6  riastrad  *
   5098   1.6  riastrad  * RETURNS:
   5099   1.6  riastrad  *
   5100   1.6  riastrad  * The MST topology state or error pointer.
   5101   1.6  riastrad  */
   5102   1.6  riastrad struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
   5103   1.6  riastrad 								    struct drm_dp_mst_topology_mgr *mgr)
   5104   1.6  riastrad {
   5105   1.6  riastrad 	return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
   5106   1.1  riastrad }
   5107   1.6  riastrad EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
   5108   1.1  riastrad 
   5109   1.1  riastrad /**
   5110   1.1  riastrad  * drm_dp_mst_topology_mgr_init - initialise a topology manager
   5111   1.1  riastrad  * @mgr: manager struct to initialise
   5112   1.1  riastrad  * @dev: device providing this structure - for i2c addition.
   5113   1.1  riastrad  * @aux: DP helper aux channel to talk to this device
   5114   1.1  riastrad  * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
   5115   1.1  riastrad  * @max_payloads: maximum number of payloads this GPU can source
   5116   1.1  riastrad  * @conn_base_id: the connector object ID the MST device is connected to.
   5117   1.1  riastrad  *
   5118   1.1  riastrad  * Return 0 for success, or negative error code on failure
   5119   1.1  riastrad  */
   5120   1.1  riastrad int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
   5121   1.6  riastrad 				 struct drm_device *dev, struct drm_dp_aux *aux,
   5122   1.1  riastrad 				 int max_dpcd_transaction_bytes,
   5123   1.1  riastrad 				 int max_payloads, int conn_base_id)
   5124   1.1  riastrad {
   5125   1.6  riastrad 	struct drm_dp_mst_topology_state *mst_state;
   5126   1.6  riastrad 
   5127   1.1  riastrad 	mutex_init(&mgr->lock);
   5128   1.1  riastrad 	mutex_init(&mgr->qlock);
   5129   1.1  riastrad 	mutex_init(&mgr->payload_lock);
   5130   1.6  riastrad 	mutex_init(&mgr->delayed_destroy_lock);
   5131   1.6  riastrad 	mutex_init(&mgr->up_req_lock);
   5132   1.6  riastrad 	mutex_init(&mgr->probe_lock);
   5133   1.6  riastrad #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
   5134   1.6  riastrad 	mutex_init(&mgr->topology_ref_history_lock);
   5135   1.6  riastrad #endif
   5136   1.1  riastrad 	INIT_LIST_HEAD(&mgr->tx_msg_downq);
   5137   1.6  riastrad 	INIT_LIST_HEAD(&mgr->destroy_port_list);
   5138   1.6  riastrad 	INIT_LIST_HEAD(&mgr->destroy_branch_device_list);
   5139   1.6  riastrad 	INIT_LIST_HEAD(&mgr->up_req_list);
   5140   1.1  riastrad 	INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
   5141   1.1  riastrad 	INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
   5142   1.6  riastrad 	INIT_WORK(&mgr->delayed_destroy_work, drm_dp_delayed_destroy_work);
   5143   1.6  riastrad 	INIT_WORK(&mgr->up_req_work, drm_dp_mst_up_req_work);
   5144   1.3  riastrad 	DRM_INIT_WAITQUEUE(&mgr->tx_waitq, "dpmstwait");
   5145   1.1  riastrad 	mgr->dev = dev;
   5146   1.1  riastrad 	mgr->aux = aux;
   5147   1.1  riastrad 	mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
   5148   1.1  riastrad 	mgr->max_payloads = max_payloads;
   5149   1.1  riastrad 	mgr->conn_base_id = conn_base_id;
   5150   1.6  riastrad 	if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
   5151   1.6  riastrad 	    max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
   5152   1.6  riastrad 		return -EINVAL;
   5153   1.1  riastrad 	mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
   5154   1.1  riastrad 	if (!mgr->payloads)
   5155   1.1  riastrad 		return -ENOMEM;
   5156   1.1  riastrad 	mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
   5157   1.1  riastrad 	if (!mgr->proposed_vcpis)
   5158   1.1  riastrad 		return -ENOMEM;
   5159   1.1  riastrad 	set_bit(0, &mgr->payload_mask);
   5160   1.6  riastrad 
   5161   1.6  riastrad 	mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
   5162   1.6  riastrad 	if (mst_state == NULL)
   5163   1.6  riastrad 		return -ENOMEM;
   5164   1.6  riastrad 
   5165   1.6  riastrad 	mst_state->mgr = mgr;
   5166   1.6  riastrad 	INIT_LIST_HEAD(&mst_state->vcpis);
   5167   1.6  riastrad 
   5168   1.6  riastrad 	drm_atomic_private_obj_init(dev, &mgr->base,
   5169   1.6  riastrad 				    &mst_state->base,
   5170   1.6  riastrad 				    &drm_dp_mst_topology_state_funcs);
   5171   1.6  riastrad 
   5172   1.1  riastrad 	return 0;
   5173   1.1  riastrad }
   5174   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
   5175   1.1  riastrad 
   5176   1.1  riastrad /**
   5177   1.1  riastrad  * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
   5178   1.1  riastrad  * @mgr: manager to destroy
   5179   1.1  riastrad  */
   5180   1.1  riastrad void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
   5181   1.1  riastrad {
   5182   1.6  riastrad 	drm_dp_mst_topology_mgr_set_mst(mgr, false);
   5183   1.1  riastrad 	flush_work(&mgr->work);
   5184   1.6  riastrad 	cancel_work_sync(&mgr->delayed_destroy_work);
   5185   1.1  riastrad 	mutex_lock(&mgr->payload_lock);
   5186   1.1  riastrad 	kfree(mgr->payloads);
   5187   1.1  riastrad 	mgr->payloads = NULL;
   5188   1.1  riastrad 	kfree(mgr->proposed_vcpis);
   5189   1.1  riastrad 	mgr->proposed_vcpis = NULL;
   5190   1.1  riastrad 	mutex_unlock(&mgr->payload_lock);
   5191   1.1  riastrad 	mgr->dev = NULL;
   5192   1.1  riastrad 	mgr->aux = NULL;
   5193   1.6  riastrad 	drm_atomic_private_obj_fini(&mgr->base);
   5194   1.6  riastrad 	mgr->funcs = NULL;
   5195   1.6  riastrad 
   5196   1.3  riastrad 	DRM_DESTROY_WAITQUEUE(&mgr->tx_waitq);
   5197   1.6  riastrad 	mutex_destroy(&mgr->delayed_destroy_lock);
   5198   1.3  riastrad 	mutex_destroy(&mgr->payload_lock);
   5199   1.3  riastrad 	mutex_destroy(&mgr->qlock);
   5200   1.3  riastrad 	mutex_destroy(&mgr->lock);
   5201   1.6  riastrad 	mutex_destroy(&mgr->up_req_lock);
   5202   1.6  riastrad 	mutex_destroy(&mgr->probe_lock);
   5203   1.6  riastrad #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
   5204   1.6  riastrad 	mutex_destroy(&mgr->topology_ref_history_lock);
   5205   1.6  riastrad #endif
   5206   1.1  riastrad }
   5207   1.1  riastrad EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
   5208   1.1  riastrad 
   5209   1.6  riastrad static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
   5210   1.6  riastrad {
   5211   1.6  riastrad 	int i;
   5212   1.6  riastrad 
   5213   1.6  riastrad 	if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
   5214   1.6  riastrad 		return false;
   5215   1.6  riastrad 
   5216   1.6  riastrad 	for (i = 0; i < num - 1; i++) {
   5217   1.6  riastrad 		if (msgs[i].flags & I2C_M_RD ||
   5218   1.6  riastrad 		    msgs[i].len > 0xff)
   5219   1.6  riastrad 			return false;
   5220   1.6  riastrad 	}
   5221   1.6  riastrad 
   5222   1.6  riastrad 	return msgs[num - 1].flags & I2C_M_RD &&
   5223   1.6  riastrad 		msgs[num - 1].len <= 0xff;
   5224   1.6  riastrad }
   5225   1.6  riastrad 
   5226   1.1  riastrad /* I2C device */
   5227   1.1  riastrad static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
   5228   1.1  riastrad 			       int num)
   5229   1.1  riastrad {
   5230   1.1  riastrad 	struct drm_dp_aux *aux = adapter->algo_data;
   5231   1.1  riastrad 	struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
   5232   1.1  riastrad 	struct drm_dp_mst_branch *mstb;
   5233   1.1  riastrad 	struct drm_dp_mst_topology_mgr *mgr = port->mgr;
   5234   1.1  riastrad 	unsigned int i;
   5235   1.1  riastrad 	struct drm_dp_sideband_msg_req_body msg;
   5236   1.1  riastrad 	struct drm_dp_sideband_msg_tx *txmsg = NULL;
   5237   1.1  riastrad 	int ret;
   5238   1.1  riastrad 
   5239   1.6  riastrad 	mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
   5240   1.1  riastrad 	if (!mstb)
   5241   1.1  riastrad 		return -EREMOTEIO;
   5242   1.1  riastrad 
   5243   1.6  riastrad 	if (!remote_i2c_read_ok(msgs, num)) {
   5244   1.1  riastrad 		DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
   5245   1.1  riastrad 		ret = -EIO;
   5246   1.1  riastrad 		goto out;
   5247   1.1  riastrad 	}
   5248   1.1  riastrad 
   5249   1.1  riastrad 	memset(&msg, 0, sizeof(msg));
   5250   1.1  riastrad 	msg.req_type = DP_REMOTE_I2C_READ;
   5251   1.1  riastrad 	msg.u.i2c_read.num_transactions = num - 1;
   5252   1.1  riastrad 	msg.u.i2c_read.port_number = port->port_num;
   5253   1.1  riastrad 	for (i = 0; i < num - 1; i++) {
   5254   1.1  riastrad 		msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
   5255   1.1  riastrad 		msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
   5256   1.1  riastrad 		msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
   5257   1.6  riastrad 		msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
   5258   1.1  riastrad 	}
   5259   1.1  riastrad 	msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
   5260   1.1  riastrad 	msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
   5261   1.1  riastrad 
   5262   1.1  riastrad 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
   5263   1.1  riastrad 	if (!txmsg) {
   5264   1.1  riastrad 		ret = -ENOMEM;
   5265   1.1  riastrad 		goto out;
   5266   1.1  riastrad 	}
   5267   1.1  riastrad 
   5268   1.1  riastrad 	txmsg->dst = mstb;
   5269   1.1  riastrad 	drm_dp_encode_sideband_req(&msg, txmsg);
   5270   1.1  riastrad 
   5271   1.1  riastrad 	drm_dp_queue_down_tx(mgr, txmsg);
   5272   1.1  riastrad 
   5273   1.1  riastrad 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
   5274   1.1  riastrad 	if (ret > 0) {
   5275   1.1  riastrad 
   5276   1.6  riastrad 		if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
   5277   1.1  riastrad 			ret = -EREMOTEIO;
   5278   1.1  riastrad 			goto out;
   5279   1.1  riastrad 		}
   5280   1.1  riastrad 		if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
   5281   1.1  riastrad 			ret = -EIO;
   5282   1.1  riastrad 			goto out;
   5283   1.1  riastrad 		}
   5284   1.1  riastrad 		memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
   5285   1.1  riastrad 		ret = num;
   5286   1.1  riastrad 	}
   5287   1.1  riastrad out:
   5288   1.1  riastrad 	kfree(txmsg);
   5289   1.6  riastrad 	drm_dp_mst_topology_put_mstb(mstb);
   5290   1.1  riastrad 	return ret;
   5291   1.1  riastrad }
   5292   1.1  riastrad 
   5293   1.1  riastrad static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
   5294   1.1  riastrad {
   5295   1.1  riastrad 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
   5296   1.1  riastrad 	       I2C_FUNC_SMBUS_READ_BLOCK_DATA |
   5297   1.1  riastrad 	       I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
   5298   1.1  riastrad 	       I2C_FUNC_10BIT_ADDR;
   5299   1.1  riastrad }
   5300   1.1  riastrad 
   5301   1.1  riastrad static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
   5302   1.1  riastrad 	.functionality = drm_dp_mst_i2c_functionality,
   5303   1.1  riastrad 	.master_xfer = drm_dp_mst_i2c_xfer,
   5304   1.1  riastrad };
   5305   1.1  riastrad 
   5306   1.1  riastrad /**
   5307   1.1  riastrad  * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
   5308   1.1  riastrad  * @aux: DisplayPort AUX channel
   5309   1.1  riastrad  *
   5310   1.1  riastrad  * Returns 0 on success or a negative error code on failure.
   5311   1.1  riastrad  */
   5312   1.1  riastrad static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
   5313   1.1  riastrad {
   5314   1.1  riastrad 	aux->ddc.algo = &drm_dp_mst_i2c_algo;
   5315   1.1  riastrad 	aux->ddc.algo_data = aux;
   5316   1.1  riastrad 	aux->ddc.retries = 3;
   5317   1.1  riastrad 
   5318   1.1  riastrad 	aux->ddc.class = I2C_CLASS_DDC;
   5319   1.1  riastrad 	aux->ddc.owner = THIS_MODULE;
   5320   1.1  riastrad 	aux->ddc.dev.parent = aux->dev;
   5321   1.3  riastrad #ifndef __NetBSD__		/* XXX of? */
   5322   1.1  riastrad 	aux->ddc.dev.of_node = aux->dev->of_node;
   5323   1.3  riastrad #endif
   5324   1.1  riastrad 
   5325   1.1  riastrad 	strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
   5326   1.1  riastrad 		sizeof(aux->ddc.name));
   5327   1.1  riastrad 
   5328   1.1  riastrad 	return i2c_add_adapter(&aux->ddc);
   5329   1.1  riastrad }
   5330   1.1  riastrad 
   5331   1.1  riastrad /**
   5332   1.1  riastrad  * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
   5333   1.1  riastrad  * @aux: DisplayPort AUX channel
   5334   1.1  riastrad  */
   5335   1.1  riastrad static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
   5336   1.1  riastrad {
   5337   1.1  riastrad 	i2c_del_adapter(&aux->ddc);
   5338   1.1  riastrad }
   5339   1.6  riastrad 
   5340   1.6  riastrad /**
   5341   1.6  riastrad  * drm_dp_mst_is_virtual_dpcd() - Is the given port a virtual DP Peer Device
   5342   1.6  riastrad  * @port: The port to check
   5343   1.6  riastrad  *
   5344   1.6  riastrad  * A single physical MST hub object can be represented in the topology
   5345   1.6  riastrad  * by multiple branches, with virtual ports between those branches.
   5346   1.6  riastrad  *
   5347   1.6  riastrad  * As of DP1.4, An MST hub with internal (virtual) ports must expose
   5348   1.6  riastrad  * certain DPCD registers over those ports. See sections 2.6.1.1.1
   5349   1.6  riastrad  * and 2.6.1.1.2 of Display Port specification v1.4 for details.
   5350   1.6  riastrad  *
   5351   1.6  riastrad  * May acquire mgr->lock
   5352   1.6  riastrad  *
   5353   1.6  riastrad  * Returns:
   5354   1.6  riastrad  * true if the port is a virtual DP peer device, false otherwise
   5355   1.6  riastrad  */
   5356   1.6  riastrad static bool drm_dp_mst_is_virtual_dpcd(struct drm_dp_mst_port *port)
   5357   1.6  riastrad {
   5358   1.6  riastrad 	struct drm_dp_mst_port *downstream_port;
   5359   1.6  riastrad 
   5360   1.6  riastrad 	if (!port || port->dpcd_rev < DP_DPCD_REV_14)
   5361   1.6  riastrad 		return false;
   5362   1.6  riastrad 
   5363   1.6  riastrad 	/* Virtual DP Sink (Internal Display Panel) */
   5364   1.6  riastrad 	if (port->port_num >= 8)
   5365   1.6  riastrad 		return true;
   5366   1.6  riastrad 
   5367   1.6  riastrad 	/* DP-to-HDMI Protocol Converter */
   5368   1.6  riastrad 	if (port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV &&
   5369   1.6  riastrad 	    !port->mcs &&
   5370   1.6  riastrad 	    port->ldps)
   5371   1.6  riastrad 		return true;
   5372   1.6  riastrad 
   5373   1.6  riastrad 	/* DP-to-DP */
   5374   1.6  riastrad 	mutex_lock(&port->mgr->lock);
   5375   1.6  riastrad 	if (port->pdt == DP_PEER_DEVICE_MST_BRANCHING &&
   5376   1.6  riastrad 	    port->mstb &&
   5377   1.6  riastrad 	    port->mstb->num_ports == 2) {
   5378   1.6  riastrad 		list_for_each_entry(downstream_port, &port->mstb->ports, next) {
   5379   1.6  riastrad 			if (downstream_port->pdt == DP_PEER_DEVICE_SST_SINK &&
   5380   1.6  riastrad 			    !downstream_port->input) {
   5381   1.6  riastrad 				mutex_unlock(&port->mgr->lock);
   5382   1.6  riastrad 				return true;
   5383   1.6  riastrad 			}
   5384   1.6  riastrad 		}
   5385   1.6  riastrad 	}
   5386   1.6  riastrad 	mutex_unlock(&port->mgr->lock);
   5387   1.6  riastrad 
   5388   1.6  riastrad 	return false;
   5389   1.6  riastrad }
   5390   1.6  riastrad 
   5391   1.6  riastrad /**
   5392   1.6  riastrad  * drm_dp_mst_dsc_aux_for_port() - Find the correct aux for DSC
   5393   1.6  riastrad  * @port: The port to check. A leaf of the MST tree with an attached display.
   5394   1.6  riastrad  *
   5395   1.6  riastrad  * Depending on the situation, DSC may be enabled via the endpoint aux,
   5396   1.6  riastrad  * the immediately upstream aux, or the connector's physical aux.
   5397   1.6  riastrad  *
   5398   1.6  riastrad  * This is both the correct aux to read DSC_CAPABILITY and the
   5399   1.6  riastrad  * correct aux to write DSC_ENABLED.
   5400   1.6  riastrad  *
   5401   1.6  riastrad  * This operation can be expensive (up to four aux reads), so
   5402   1.6  riastrad  * the caller should cache the return.
   5403   1.6  riastrad  *
   5404   1.6  riastrad  * Returns:
   5405   1.6  riastrad  * NULL if DSC cannot be enabled on this port, otherwise the aux device
   5406   1.6  riastrad  */
   5407   1.6  riastrad struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port)
   5408   1.6  riastrad {
   5409   1.6  riastrad 	struct drm_dp_mst_port *immediate_upstream_port;
   5410   1.6  riastrad 	struct drm_dp_mst_port *fec_port;
   5411   1.6  riastrad 	struct drm_dp_desc desc = { 0 };
   5412   1.6  riastrad 	u8 endpoint_fec;
   5413   1.6  riastrad 	u8 endpoint_dsc;
   5414   1.6  riastrad 
   5415   1.6  riastrad 	if (!port)
   5416   1.6  riastrad 		return NULL;
   5417   1.6  riastrad 
   5418   1.6  riastrad 	if (port->parent->port_parent)
   5419   1.6  riastrad 		immediate_upstream_port = port->parent->port_parent;
   5420   1.6  riastrad 	else
   5421   1.6  riastrad 		immediate_upstream_port = NULL;
   5422   1.6  riastrad 
   5423   1.6  riastrad 	fec_port = immediate_upstream_port;
   5424   1.6  riastrad 	while (fec_port) {
   5425   1.6  riastrad 		/*
   5426   1.6  riastrad 		 * Each physical link (i.e. not a virtual port) between the
   5427   1.6  riastrad 		 * output and the primary device must support FEC
   5428   1.6  riastrad 		 */
   5429   1.6  riastrad 		if (!drm_dp_mst_is_virtual_dpcd(fec_port) &&
   5430   1.6  riastrad 		    !fec_port->fec_capable)
   5431   1.6  riastrad 			return NULL;
   5432   1.6  riastrad 
   5433   1.6  riastrad 		fec_port = fec_port->parent->port_parent;
   5434   1.6  riastrad 	}
   5435   1.6  riastrad 
   5436   1.6  riastrad 	/* DP-to-DP peer device */
   5437   1.6  riastrad 	if (drm_dp_mst_is_virtual_dpcd(immediate_upstream_port)) {
   5438   1.6  riastrad 		u8 upstream_dsc;
   5439   1.6  riastrad 
   5440   1.6  riastrad 		if (drm_dp_dpcd_read(&port->aux,
   5441   1.6  riastrad 				     DP_DSC_SUPPORT, &endpoint_dsc, 1) != 1)
   5442   1.6  riastrad 			return NULL;
   5443   1.6  riastrad 		if (drm_dp_dpcd_read(&port->aux,
   5444   1.6  riastrad 				     DP_FEC_CAPABILITY, &endpoint_fec, 1) != 1)
   5445   1.6  riastrad 			return NULL;
   5446   1.6  riastrad 		if (drm_dp_dpcd_read(&immediate_upstream_port->aux,
   5447   1.6  riastrad 				     DP_DSC_SUPPORT, &upstream_dsc, 1) != 1)
   5448   1.6  riastrad 			return NULL;
   5449   1.6  riastrad 
   5450   1.6  riastrad 		/* Enpoint decompression with DP-to-DP peer device */
   5451   1.6  riastrad 		if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) &&
   5452   1.6  riastrad 		    (endpoint_fec & DP_FEC_CAPABLE) &&
   5453   1.6  riastrad 		    (upstream_dsc & 0x2) /* DSC passthrough */)
   5454   1.6  riastrad 			return &port->aux;
   5455   1.6  riastrad 
   5456   1.6  riastrad 		/* Virtual DPCD decompression with DP-to-DP peer device */
   5457   1.6  riastrad 		return &immediate_upstream_port->aux;
   5458   1.6  riastrad 	}
   5459   1.6  riastrad 
   5460   1.6  riastrad 	/* Virtual DPCD decompression with DP-to-HDMI or Virtual DP Sink */
   5461   1.6  riastrad 	if (drm_dp_mst_is_virtual_dpcd(port))
   5462   1.6  riastrad 		return &port->aux;
   5463   1.6  riastrad 
   5464   1.6  riastrad 	/*
   5465   1.6  riastrad 	 * Synaptics quirk
   5466   1.6  riastrad 	 * Applies to ports for which:
   5467   1.6  riastrad 	 * - Physical aux has Synaptics OUI
   5468   1.6  riastrad 	 * - DPv1.4 or higher
   5469   1.6  riastrad 	 * - Port is on primary branch device
   5470   1.6  riastrad 	 * - Not a VGA adapter (DP_DWN_STRM_PORT_TYPE_ANALOG)
   5471   1.6  riastrad 	 */
   5472   1.6  riastrad 	if (drm_dp_read_desc(port->mgr->aux, &desc, true))
   5473   1.6  riastrad 		return NULL;
   5474   1.6  riastrad 
   5475   1.6  riastrad 	if (drm_dp_has_quirk(&desc, DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) &&
   5476   1.6  riastrad 	    port->mgr->dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14 &&
   5477   1.6  riastrad 	    port->parent == port->mgr->mst_primary) {
   5478   1.6  riastrad 		u8 downstreamport;
   5479   1.6  riastrad 
   5480   1.6  riastrad 		if (drm_dp_dpcd_read(&port->aux, DP_DOWNSTREAMPORT_PRESENT,
   5481   1.6  riastrad 				     &downstreamport, 1) < 0)
   5482   1.6  riastrad 			return NULL;
   5483   1.6  riastrad 
   5484   1.6  riastrad 		if ((downstreamport & DP_DWN_STRM_PORT_PRESENT) &&
   5485   1.6  riastrad 		   ((downstreamport & DP_DWN_STRM_PORT_TYPE_MASK)
   5486   1.6  riastrad 		     != DP_DWN_STRM_PORT_TYPE_ANALOG))
   5487   1.6  riastrad 			return port->mgr->aux;
   5488   1.6  riastrad 	}
   5489   1.6  riastrad 
   5490   1.6  riastrad 	/*
   5491   1.6  riastrad 	 * The check below verifies if the MST sink
   5492   1.6  riastrad 	 * connected to the GPU is capable of DSC -
   5493   1.6  riastrad 	 * therefore the endpoint needs to be
   5494   1.6  riastrad 	 * both DSC and FEC capable.
   5495   1.6  riastrad 	 */
   5496   1.6  riastrad 	if (drm_dp_dpcd_read(&port->aux,
   5497   1.6  riastrad 	   DP_DSC_SUPPORT, &endpoint_dsc, 1) != 1)
   5498   1.6  riastrad 		return NULL;
   5499   1.6  riastrad 	if (drm_dp_dpcd_read(&port->aux,
   5500   1.6  riastrad 	   DP_FEC_CAPABILITY, &endpoint_fec, 1) != 1)
   5501   1.6  riastrad 		return NULL;
   5502   1.6  riastrad 	if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) &&
   5503   1.6  riastrad 	   (endpoint_fec & DP_FEC_CAPABLE))
   5504   1.6  riastrad 		return &port->aux;
   5505   1.6  riastrad 
   5506   1.6  riastrad 	return NULL;
   5507   1.6  riastrad }
   5508   1.6  riastrad EXPORT_SYMBOL(drm_dp_mst_dsc_aux_for_port);
   5509