refactor(ti): refactor ti_sci_{setup,do}_xfer to allow zero size response

This allows us to use the common xfer setup path even for no-wait
messages. Then factor that out of each no-wait function.

Signed-off-by: Andrew Davis <afd@ti.com>
Change-Id: Ib17d3facd293f3fc91dda56b2906121b43250261
diff --git a/plat/ti/k3/common/drivers/ti_sci/ti_sci.c b/plat/ti/k3/common/drivers/ti_sci/ti_sci.c
index ade10a6..366189f 100644
--- a/plat/ti/k3/common/drivers/ti_sci/ti_sci.c
+++ b/plat/ti/k3/common/drivers/ti_sci/ti_sci.c
@@ -62,7 +62,6 @@
 	/* Ensure we have sane transfer sizes */
 	if (rx_message_size > TI_SCI_MAX_MESSAGE_SIZE ||
 	    tx_message_size > TI_SCI_MAX_MESSAGE_SIZE ||
-	    rx_message_size < sizeof(*hdr) ||
 	    tx_message_size < sizeof(*hdr))
 		return -ERANGE;
 
@@ -70,7 +69,11 @@
 	hdr->seq = ++message_sequence;
 	hdr->type = msg_type;
 	hdr->host = TI_SCI_HOST_ID;
-	hdr->flags = msg_flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED;
+	hdr->flags = msg_flags;
+	/* Request a response if rx_message_size is non-zero */
+	if (rx_message_size != 0U) {
+		hdr->flags |= TI_SCI_FLAG_REQ_ACK_ON_PROCESSED;
+	}
 
 	xfer->tx_message.buf = tx_buf;
 	xfer->tx_message.len = tx_message_size;
@@ -89,10 +92,9 @@
  *
  * Return: 0 if all goes well, else appropriate error message
  */
-static inline int ti_sci_get_response(struct ti_sci_xfer *xfer,
+static inline int ti_sci_get_response(struct k3_sec_proxy_msg *msg,
 				      enum k3_sec_proxy_chan_id chan)
 {
-	struct k3_sec_proxy_msg *msg = &xfer->rx_message;
 	struct ti_sci_msg_hdr *hdr;
 	unsigned int retry = 5;
 	int ret;
@@ -140,7 +142,8 @@
  */
 static inline int ti_sci_do_xfer(struct ti_sci_xfer *xfer)
 {
-	struct k3_sec_proxy_msg *msg = &xfer->tx_message;
+	struct k3_sec_proxy_msg *tx_msg = &xfer->tx_message;
+	struct k3_sec_proxy_msg *rx_msg = &xfer->rx_message;
 	int ret;
 
 	/* Clear any spurious messages in receive queue */
@@ -151,17 +154,19 @@
 	}
 
 	/* Send the message */
-	ret = k3_sec_proxy_send(SP_HIGH_PRIORITY, msg);
+	ret = k3_sec_proxy_send(SP_HIGH_PRIORITY, tx_msg);
 	if (ret) {
 		ERROR("Message sending failed (%d)\n", ret);
 		return ret;
 	}
 
-	/* Get the response */
-	ret = ti_sci_get_response(xfer, SP_RESPONSE);
-	if (ret) {
-		ERROR("Failed to get response (%d)\n", ret);
-		return ret;
+	/* Get the response if requested */
+	if (rx_msg->len != 0U) {
+		ret = ti_sci_get_response(rx_msg, SP_RESPONSE);
+		if (ret != 0U) {
+			ERROR("Failed to get response (%d)\n", ret);
+			return ret;
+		}
 	}
 
 	return 0;
@@ -398,35 +403,27 @@
 int ti_sci_device_put_no_wait(uint32_t id)
 {
 	struct ti_sci_msg_req_set_device_state req;
-	struct ti_sci_msg_hdr *hdr;
-	struct k3_sec_proxy_msg tx_message;
+	struct ti_sci_xfer xfer;
 	int ret;
 
-	/* Ensure we have sane transfer size */
-	if (sizeof(req) > TI_SCI_MAX_MESSAGE_SIZE)
-		return -ERANGE;
-
-	hdr = (struct ti_sci_msg_hdr *)&req;
-	hdr->seq = ++message_sequence;
-	hdr->type = TI_SCI_MSG_SET_DEVICE_STATE;
-	hdr->host = TI_SCI_HOST_ID;
-	/* Setup with NORESPONSE flag to keep response queue clean */
-	hdr->flags = TI_SCI_FLAG_REQ_GENERIC_NORESPONSE;
+	ret = ti_sci_setup_one_xfer(TI_SCI_MSG_GET_DEVICE_STATE, 0,
+				    &req, sizeof(req),
+				    NULL, 0,
+				    &xfer);
+	if (ret != 0U) {
+		ERROR("Message alloc failed (%d)\n", ret);
+		return ret;
+	}
 
 	req.id = id;
 	req.state = MSG_DEVICE_SW_STATE_AUTO_OFF;
 
-	tx_message.buf = (uint8_t *)&req;
-	tx_message.len = sizeof(req);
-
-	 /* Send message */
-	ret = k3_sec_proxy_send(SP_HIGH_PRIORITY, &tx_message);
-	if (ret) {
-		ERROR("Message sending failed (%d)\n", ret);
+	ret = ti_sci_do_xfer(&xfer);
+	if (ret != 0U) {
+		ERROR("Transfer send failed (%d)\n", ret);
 		return ret;
 	}
 
-	/* Return without waiting for response */
 	return 0;
 }
 
@@ -1382,36 +1379,28 @@
 				      uint32_t control_flags_clear)
 {
 	struct ti_sci_msg_req_set_proc_boot_ctrl req;
-	struct ti_sci_msg_hdr *hdr;
-	struct k3_sec_proxy_msg tx_message;
+	struct ti_sci_xfer xfer;
 	int ret;
 
-	/* Ensure we have sane transfer size */
-	if (sizeof(req) > TI_SCI_MAX_MESSAGE_SIZE)
-		return -ERANGE;
-
-	hdr = (struct ti_sci_msg_hdr *)&req;
-	hdr->seq = ++message_sequence;
-	hdr->type = TISCI_MSG_SET_PROC_BOOT_CTRL;
-	hdr->host = TI_SCI_HOST_ID;
-	/* Setup with NORESPONSE flag to keep response queue clean */
-	hdr->flags = TI_SCI_FLAG_REQ_GENERIC_NORESPONSE;
+	ret = ti_sci_setup_one_xfer(TI_SCI_MSG_GET_DEVICE_STATE, 0,
+				    &req, sizeof(req),
+				    NULL, 0,
+				    &xfer);
+	if (ret != 0U) {
+		ERROR("Message alloc failed (%d)\n", ret);
+		return ret;
+	}
 
 	req.processor_id = proc_id;
 	req.control_flags_set = control_flags_set;
 	req.control_flags_clear = control_flags_clear;
 
-	tx_message.buf = (uint8_t *)&req;
-	tx_message.len = sizeof(req);
-
-	 /* Send message */
-	ret = k3_sec_proxy_send(SP_HIGH_PRIORITY, &tx_message);
-	if (ret) {
-		ERROR("Message sending failed (%d)\n", ret);
+	ret = ti_sci_do_xfer(&xfer);
+	if (ret != 0U) {
+		ERROR("Transfer send failed (%d)\n", ret);
 		return ret;
 	}
 
-	/* Return without waiting for response */
 	return 0;
 }
 
@@ -1624,20 +1613,17 @@
 					 uint32_t status_flags_1_clr_any_wait)
 {
 	struct ti_sci_msg_req_wait_proc_boot_status req;
-	struct ti_sci_msg_hdr *hdr;
-	struct k3_sec_proxy_msg tx_message;
+	struct ti_sci_xfer xfer;
 	int ret;
 
-	/* Ensure we have sane transfer size */
-	if (sizeof(req) > TI_SCI_MAX_MESSAGE_SIZE)
-		return -ERANGE;
-
-	hdr = (struct ti_sci_msg_hdr *)&req;
-	hdr->seq = ++message_sequence;
-	hdr->type = TISCI_MSG_WAIT_PROC_BOOT_STATUS;
-	hdr->host = TI_SCI_HOST_ID;
-	/* Setup with NORESPONSE flag to keep response queue clean */
-	hdr->flags = TI_SCI_FLAG_REQ_GENERIC_NORESPONSE;
+	ret = ti_sci_setup_one_xfer(TI_SCI_MSG_GET_DEVICE_STATE, 0,
+				    &req, sizeof(req),
+				    NULL, 0,
+				    &xfer);
+	if (ret != 0U) {
+		ERROR("Message alloc failed (%d)\n", ret);
+		return ret;
+	}
 
 	req.processor_id = proc_id;
 	req.num_wait_iterations = num_wait_iterations;
@@ -1649,17 +1635,12 @@
 	req.status_flags_1_clr_all_wait = status_flags_1_clr_all_wait;
 	req.status_flags_1_clr_any_wait = status_flags_1_clr_any_wait;
 
-	tx_message.buf = (uint8_t *)&req;
-	tx_message.len = sizeof(req);
-
-	 /* Send message */
-	ret = k3_sec_proxy_send(SP_HIGH_PRIORITY, &tx_message);
-	if (ret) {
-		ERROR("Message sending failed (%d)\n", ret);
+	ret = ti_sci_do_xfer(&xfer);
+	if (ret != 0U) {
+		ERROR("Transfer send failed (%d)\n", ret);
 		return ret;
 	}
 
-	/* Return without waiting for response */
 	return 0;
 }
 
@@ -1678,39 +1659,30 @@
 		       uint64_t core_resume_addr)
 {
 	struct ti_sci_msg_req_enter_sleep req;
-	struct ti_sci_msg_hdr *hdr;
-	struct k3_sec_proxy_msg tx_message;
+	struct ti_sci_xfer xfer;
 	int ret;
 
-	/* Ensure we have sane transfer size */
-	if (sizeof(req) > TI_SCI_MAX_MESSAGE_SIZE) {
-		return -ERANGE;
+	ret = ti_sci_setup_one_xfer(TI_SCI_MSG_GET_DEVICE_STATE, 0,
+				    &req, sizeof(req),
+				    NULL, 0,
+				    &xfer);
+	if (ret != 0U) {
+		ERROR("Message alloc failed (%d)\n", ret);
+		return ret;
 	}
 
-	hdr = (struct ti_sci_msg_hdr *)&req;
-	hdr->seq = ++message_sequence;
-	hdr->type = TI_SCI_MSG_ENTER_SLEEP;
-	hdr->host = TI_SCI_HOST_ID;
-	/* Setup with NORESPONSE flag to keep response queue clean */
-	hdr->flags = TI_SCI_FLAG_REQ_GENERIC_NORESPONSE;
-
 	req.processor_id = proc_id;
 	req.mode = mode;
 	req.core_resume_lo = core_resume_addr & TISCI_ADDR_LOW_MASK;
 	req.core_resume_hi = (core_resume_addr & TISCI_ADDR_HIGH_MASK) >>
 			     TISCI_ADDR_HIGH_SHIFT;
 
-	tx_message.buf = (uint8_t *)&req;
-	tx_message.len = sizeof(req);
-
-	/* Send message */
-	ret = k3_sec_proxy_send(SP_HIGH_PRIORITY, &tx_message);
-	if (ret != 0) {
-		ERROR("Message sending failed (%d)\n", ret);
+	ret = ti_sci_do_xfer(&xfer);
+	if (ret != 0U) {
+		ERROR("Transfer send failed (%d)\n", ret);
 		return ret;
 	}
 
-	/* Return without waiting for response */
 	return 0;
 }