dsp: fix set_adm_pp_params command timeout

Because Waves effect use send_adm_apr() in rtac.c to set adm pp params
when starting or seeking music, but there is sync issue with
set_adm_pp_params() in q6adm.c.

Patch is provided from case#04514971. It adds new api adm_apr_send_pkt()
which is used to send adm set pp params command in q6adm.c and rtac.c,
and it also add mutex this_adm.adm_apr_lock to protect it to make sure
sending next adm pp params command after previous response is received.

Original problem:
q6adm send pp param(thread1) -> rtac send adm pp param(thread2) ->
adm_callback send response to rtac but this one should send to q6adm ->
adm_callback send response to q6adm but this one should send to rtac

After applying the patch:
q6adm send pp param(thread1) -> rtac send adm pp param(thread2 blocked) ->
adm_callback to q6adm -> rtac send adm pp param -> adm_callback to rtac

Bug: 147327901
Test: verified seeking with visualizer enabled
Change-Id: I6c1a46b5b9bde8a45680b517239ee70cc929def8
Signed-off-by: Robert Lee <lerobert@google.com>
diff --git a/dsp/q6adm.c b/dsp/q6adm.c
index db0ebea..013c37a 100644
--- a/dsp/q6adm.c
+++ b/dsp/q6adm.c
@@ -100,6 +100,7 @@
 	struct param_outband outband_memmap;
 	struct source_tracking_data sourceTrackingData;
 
+	struct mutex adm_apr_lock;
 	int set_custom_topology;
 	int ec_ref_rx;
 	int num_ec_ref_rx_chans;
@@ -901,6 +902,47 @@
 EXPORT_SYMBOL(adm_set_custom_chmix_cfg);
 
 /*
+ * adm_apr_send_pkt : returns 0 on success, negative otherwise.
+ */
+int adm_apr_send_pkt(void *data, int port_idx, int copp_idx)
+{
+	int ret = 0;
+	atomic_t *copp_stat = NULL;
+	wait_queue_head_t *wait = NULL;
+
+	mutex_lock(&this_adm.adm_apr_lock);
+
+	wait = &this_adm.copp.wait[port_idx][copp_idx];
+	copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
+	atomic_set(copp_stat, -1);
+
+	ret = apr_send_pkt(this_adm.apr, data);
+	if (ret > 0) {
+		ret = wait_event_timeout(*wait,
+				atomic_read(copp_stat) >= 0,
+				msecs_to_jiffies(TIMEOUT_MS));
+		if (atomic_read(copp_stat) > 0) {
+			pr_err("%s: DSP returned error[%s]\n", __func__,
+				adsp_err_get_err_str(atomic_read(copp_stat)));
+			ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
+		} else  if (!ret) {
+			pr_err_ratelimited("%s: request timedout\n",
+						__func__);
+			ret = -ETIMEDOUT;
+		} else {
+			ret = 0;
+		}
+	} else if (ret == 0) {
+		pr_err("%s: packet not transmitted\n", __func__);
+		/* apr_send_pkt can return 0 when nothing is transmitted */
+		ret = -EINVAL;
+	}
+
+	mutex_unlock(&this_adm.adm_apr_lock);
+	return ret;
+}
+
+/*
  * With pre-packed data, only the opcode differes from V5 and V6.
  * Use q6common_pack_pp_params to pack the data correctly.
  */
@@ -911,7 +953,6 @@
 	struct adm_cmd_set_pp_params *adm_set_params = NULL;
 	int size = 0;
 	int port_idx = 0;
-	atomic_t *copp_stat = NULL;
 	int ret = 0;
 
 	port_id = afe_convert_virtual_to_portid(port_id);
@@ -924,6 +965,12 @@
 		return -EINVAL;
 	}
 
+	if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
+		pr_err("%s:: port[0x%x] coppid[0x%x] is not active, ERROR\n",
+			__func__, port_id, copp_idx);
+		return -EINVAL;
+	}
+
 	/* Only add params_size in inband case */
 	size = sizeof(struct adm_cmd_set_pp_params);
 	if (param_data != NULL)
@@ -970,34 +1017,8 @@
 		goto done;
 	}
 
-	copp_stat = &this_adm.copp.stat[port_idx][copp_idx];
-	atomic_set(copp_stat, -1);
-	ret = apr_send_pkt(this_adm.apr, (uint32_t *) adm_set_params);
-	if (ret < 0) {
-		pr_err("%s: Set params APR send failed port = 0x%x ret %d\n",
-		       __func__, port_id, ret);
-		goto done;
-	}
-	ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
-				 atomic_read(copp_stat) >= 0,
-				 msecs_to_jiffies(TIMEOUT_MS));
-	if (!ret) {
-		pr_err("%s: Set params timed out port = 0x%x\n", __func__,
-		       port_id);
-		ret = -ETIMEDOUT;
-
-		pr_err("trigger ADSP SSR b/136031875");
-		subsystem_restart("adsp");
-		goto done;
-	}
-	if (atomic_read(copp_stat) > 0) {
-		pr_err("%s: DSP returned error[%s]\n", __func__,
-		       adsp_err_get_err_str(atomic_read(copp_stat)));
-		ret = adsp_err_get_lnx_err_code(atomic_read(copp_stat));
-		goto done;
-	}
-
-	ret = 0;
+	ret = adm_apr_send_pkt((uint32_t *) adm_set_params,
+			port_idx, copp_idx);
 done:
 	kfree(adm_set_params);
 	return ret;
@@ -1632,9 +1653,16 @@
 				if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
 					this_adm.sourceTrackingData.
 						apr_cmd_status = payload[1];
-				else if (rtac_make_adm_callback(payload,
-							data->payload_size))
-					break;
+				else {
+					rtac_make_adm_callback(payload,
+						data->payload_size);
+				}
+				atomic_set(&this_adm.copp.stat[port_idx]
+						[copp_idx], payload[1]);
+				wake_up(
+				&this_adm.copp.wait[port_idx][copp_idx]);
+				break;
+
 				/*
 				 * if soft volume is called and already
 				 * interrupted break out of the sequence here
@@ -1777,8 +1805,14 @@
 				this_adm.sourceTrackingData.apr_cmd_status =
 					payload[0];
 			else if (rtac_make_adm_callback(payload,
-							data->payload_size))
+						data->payload_size)) {
+				atomic_set(
+					&this_adm.copp.stat[port_idx][copp_idx],
+					payload[0]);
+				wake_up(
+				&this_adm.copp.wait[port_idx][copp_idx]);
 				break;
+			}
 
 			idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
 			if (payload[0] == 0 && data->payload_size > 0) {
@@ -5132,6 +5166,7 @@
 	this_adm.ffecns_port_id = -1;
 	init_waitqueue_head(&this_adm.matrix_map_wait);
 	init_waitqueue_head(&this_adm.adm_wait);
+	mutex_init(&this_adm.adm_apr_lock);
 
 	for (i = 0; i < AFE_MAX_PORTS; i++) {
 		for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
@@ -5156,6 +5191,7 @@
 
 void adm_exit(void)
 {
+	mutex_destroy(&this_adm.adm_apr_lock);
 	if (this_adm.apr)
 		adm_reset_data();
 	adm_delete_cal_data();
diff --git a/dsp/rtac.c b/dsp/rtac.c
index 5a3ce34..5c48cdd 100644
--- a/dsp/rtac.c
+++ b/dsp/rtac.c
@@ -736,7 +736,6 @@
 		atomic_set(&rtac_common.apr_err_code, payload[1]);
 
 	atomic_set(&rtac_adm_apr_data.cmd_state, 0);
-	wake_up(&rtac_adm_apr_data.cmd_wait);
 	return true;
 }
 
@@ -888,32 +887,17 @@
 	pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%pK\n",
 		__func__, opcode,
 		&rtac_cal[ADM_RTAC_CAL].cal_data.paddr);
+	mutex_unlock(&rtac_adm_apr_mutex);
 
-	result = apr_send_pkt(rtac_adm_apr_data.apr_handle,
-					(uint32_t *)rtac_adm_buffer);
+	result = adm_apr_send_pkt((uint32_t *)rtac_adm_buffer,
+			port_idx, copp_idx);
+
+	mutex_lock(&rtac_adm_apr_mutex);
+
 	if (result < 0) {
 		pr_err("%s: Set params failed copp = %d\n", __func__, copp_id);
 		goto err;
 	}
-	/* Wait for the callback */
-	result = wait_event_timeout(rtac_adm_apr_data.cmd_wait,
-		(atomic_read(&rtac_adm_apr_data.cmd_state) == 0),
-		msecs_to_jiffies(TIMEOUT_MS));
-	if (!result) {
-		pr_err("%s: Set params timed out copp = %d\n", __func__,
-			copp_id);
-		goto err;
-	}
-	if (atomic_read(&rtac_common.apr_err_code)) {
-		pr_err("%s: DSP returned error code = [%s], opcode = 0x%x\n",
-			__func__, adsp_err_get_err_str(atomic_read(
-			&rtac_common.apr_err_code)),
-			opcode);
-		result = adsp_err_get_lnx_err_code(
-					atomic_read(
-					&rtac_common.apr_err_code));
-		goto err;
-	}
 
 	if (opcode == ADM_CMD_GET_PP_PARAMS_V5) {
 		bytes_returned = ((u32 *)rtac_cal[ADM_RTAC_CAL].cal_data.
diff --git a/include/dsp/q6adm-v2.h b/include/dsp/q6adm-v2.h
index ecaa926..8e758d6 100644
--- a/include/dsp/q6adm-v2.h
+++ b/include/dsp/q6adm-v2.h
@@ -215,4 +215,5 @@
 void msm_dts_srs_acquire_lock(void);
 void msm_dts_srs_release_lock(void);
 void adm_set_native_mode(int mode);
+int adm_apr_send_pkt(void *data, int port_idx, int copp_idx);
 #endif /* __Q6_ADM_V2_H__ */