[Copybara Auto Merge] Merge branch zuma into android14-gs-pixel-5.15-udc-d1

edgetpu: move ctx activation to group activation
The problem is the context activation/deactivation calls could be called
when the device is powered off when a group is marked as non-detachable.
To fix this we always do context activation / deactivation on wakelock
acquirement / releasing instead of on domain attaching / detaching.
Test: Astress
Bug: 277256378
edgetpu: rio: LPM patch to fix FRC clock timing violation
Bug: 287661979
(cherry picked from commit 584af84078d0a02399a71f92040fac2e2ce2d88e)

Signed-off-by: Zuma copybara merger <zuma-automerger@google.com>
GitOrigin-RevId: b2c243b9351955c18780b887b3790e38b133e494
Change-Id: I1202f29697ab69543ddc3f83770919c57ab972e4
diff --git a/drivers/edgetpu/edgetpu-device-group.c b/drivers/edgetpu/edgetpu-device-group.c
index 3e94dcd..2b97d87 100644
--- a/drivers/edgetpu/edgetpu-device-group.c
+++ b/drivers/edgetpu/edgetpu-device-group.c
@@ -33,6 +33,7 @@
 #include "edgetpu-kci.h"
 #include "edgetpu-mapping.h"
 #include "edgetpu-mmu.h"
+#include "edgetpu-soc.h"
 #include "edgetpu-sw-watchdog.h"
 #include "edgetpu-wakelock.h"
 #include "edgetpu.h"
@@ -83,6 +84,7 @@
 		return 0;
 
 	mailbox_id = edgetpu_group_context_id_locked(group);
+	edgetpu_soc_activate_context(group->etdev, mailbox_id);
 	ret = edgetpu_mailbox_activate(group->etdev, mailbox_id, group->mbox_attr.client_priv,
 				       group->vcid, !group->activated);
 	if (ret) {
@@ -116,6 +118,11 @@
 	edgetpu_sw_wdt_dec_active_ref(group->etdev);
 	mailbox_id = edgetpu_group_context_id_locked(group);
 	edgetpu_mailbox_deactivate(group->etdev, mailbox_id);
+	/*
+	 * Deactivate the context to prevent speculative accesses from being issued to a disabled
+	 * context.
+	 */
+	edgetpu_soc_deactivate_context(group->etdev, mailbox_id);
 }
 
 /*
diff --git a/drivers/edgetpu/edgetpu-google-iommu.c b/drivers/edgetpu/edgetpu-google-iommu.c
index 4bf836f..a432783 100644
--- a/drivers/edgetpu/edgetpu-google-iommu.c
+++ b/drivers/edgetpu/edgetpu-google-iommu.c
@@ -19,7 +19,6 @@
 #include "edgetpu-internal.h"
 #include "edgetpu-mapping.h"
 #include "edgetpu-mmu.h"
-#include "edgetpu-soc.h"
 
 #if !defined(EDGETPU_NUM_PREALLOCATED_DOMAINS)
 #define EDGETPU_NUM_PREALLOCATED_DOMAINS 0
@@ -555,7 +554,6 @@
 		goto err_detach;
 	}
 
-	edgetpu_soc_activate_context(etdev, pasid);
 	etiommu->gdomains[pasid] = gdomain;
 	etdomain->pasid = pasid;
 	return 0;
@@ -575,11 +573,6 @@
 	if (pasid <= 0 || pasid >= EDGETPU_NCONTEXTS)
 		return;
 
-	/*
-	 * Deactivate the context before domain detaching to prevent speculative accesses from being
-	 * issued to a disabled context.
-	 */
-	edgetpu_soc_deactivate_context(etdev, pasid);
 	etiommu->gdomains[pasid] = NULL;
 	etdomain->pasid = IOMMU_PASID_INVALID;
 	iommu_aux_detach_device(etdomain->gdomain->domain, etdev->dev);
diff --git a/drivers/edgetpu/rio-pm.c b/drivers/edgetpu/rio-pm.c
index c50949a..bba5e18 100644
--- a/drivers/edgetpu/rio-pm.c
+++ b/drivers/edgetpu/rio-pm.c
@@ -83,6 +83,7 @@
 	EDGETPU_LPM_IMEM_OPS_SET(etdev, 200, 0x0bc95001);
 	EDGETPU_LPM_IMEM_OPS_SET(etdev, 201, 0x14171018);
 	EDGETPU_LPM_IMEM_OPS_SET(etdev, 202, 0x02001118);
+
 	/* psm_1_state_table_0_trans_1_next_state */
 	edgetpu_dev_write_32_sync(etdev, 0x1c2020, 0x00000000);
 	/* psm_1_state_table_0_trans_1_seq_addr */
@@ -93,6 +94,15 @@
 	edgetpu_dev_write_32_sync(etdev, 0x1c2034, 0x00000001);
 	/* trigger_csr_events_en_5_hi */
 	edgetpu_dev_write_32_sync(etdev, 0x1c012c, 0x00000003);
+
+        /*
+         * FRC clocking fix for b/287661979.
+         *
+         * Increases the delay between cluster clock enablement and logic
+         * retention/restore activation.
+         */
+	EDGETPU_LPM_IMEM_OPS_SET(etdev, 3, 0x21261101);
+	EDGETPU_LPM_IMEM_OPS_SET(etdev, 4, 0x11111005);
 }
 
 static int rio_lpm_up(struct edgetpu_dev *etdev)