psci: fix affinity level upgrade issue

The psci implementation does not track target affinity level requests
specified during cpu_suspend calls correctly as per the following
example.

1. cpu0.cluster0 calls cpu_suspend with the target affinity level as 0
2. Only the cpu0.cluster0 is powered down while cluster0 remains
   powered up
3. cpu1.cluster0 calls cpu_off to power itself down to highest
   possible affinity level
4. cluster0 will be powered off even though cpu0.cluster0 does not
   allow cluster shutdown

This patch introduces reference counts at affinity levels > 0 to track
the number of cpus which want an affinity instance at level X to
remain powered up. This instance can be turned off only if its
reference count is 0. Cpus still undergo the normal state transitions
(ON, OFF, ON_PENDING, SUSPEND) but the higher levels can only be
either ON or OFF depending upon their reference count.

The above issue is thus fixed as follows:

1. cluster0's reference count is incremented by two when cpu0 and cpu1
   are initially powered on.

2. cpu0.cluster0 calls cpu_suspend with the target affinity level as
   0. This does not affect the cluster0 reference count.

3. Only the cpu0.cluster0 is powered down while cluster0 remains
   powered up as it has a non-zero reference count.

4. cpu1.cluster0 call cpu_off to power itself down to highest possible
   affinity level. This decrements the cluster0 reference count.

5. cluster0 is still not powered off since its reference count will at
   least be 1 due to the restriction placed by cpu0.

Change-Id: I433dfe82b946f5f6985b1602c2de87800504f7a9
diff --git a/common/psci/psci_afflvl_on.c b/common/psci/psci_afflvl_on.c
index c1a2e99..83d47d5 100644
--- a/common/psci/psci_afflvl_on.c
+++ b/common/psci/psci_afflvl_on.c
@@ -46,12 +46,12 @@
  * This function checks whether a cpu which has been requested to be turned on
  * is OFF to begin with.
  ******************************************************************************/
-static int cpu_on_validate_state(unsigned int state)
+static int cpu_on_validate_state(aff_map_node *node)
 {
 	unsigned int psci_state;
 
 	/* Get the raw psci state */
-	psci_state = psci_get_state(state);
+	psci_state = psci_get_state(node);
 
 	if (psci_state == PSCI_STATE_ON || psci_state == PSCI_STATE_SUSPEND)
 		return PSCI_E_ALREADY_ON;
@@ -84,7 +84,7 @@
 	 * Generic management: Ensure that the cpu is off to be
 	 * turned on
 	 */
-	rc = cpu_on_validate_state(cpu_node->state);
+	rc = cpu_on_validate_state(cpu_node);
 	if (rc != PSCI_E_SUCCESS)
 		return rc;
 
@@ -101,6 +101,9 @@
 	/* Set the secure world (EL3) re-entry point after BL1 */
 	psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
 
+	/* State management: Set this cpu's state as ON PENDING */
+	psci_set_state(cpu_node, PSCI_STATE_ON_PENDING);
+
 	/*
 	 * Plat. management: Give the platform the current state
 	 * of the target cpu to allow it to perform the necessary
@@ -109,7 +112,7 @@
 	if (psci_plat_pm_ops->affinst_on) {
 
 		/* Get the current physical state of this cpu */
-		plat_state = psci_get_aff_phys_state(cpu_node);
+		plat_state = psci_get_phys_state(cpu_node);
 		rc = psci_plat_pm_ops->affinst_on(target_cpu,
 						  psci_entrypoint,
 						  ns_entrypoint,
@@ -141,13 +144,15 @@
 	 * management required
 	 */
 
+	/* State management: Is not required while turning a cluster on */
+
 	/*
 	 * Plat. management: Give the platform the current state
 	 * of the target cpu to allow it to perform the necessary
 	 * steps to power on.
 	 */
 	if (psci_plat_pm_ops->affinst_on) {
-		plat_state = psci_get_aff_phys_state(cluster_node);
+		plat_state = psci_get_phys_state(cluster_node);
 		psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
 		rc = psci_plat_pm_ops->affinst_on(target_cpu,
 						  psci_entrypoint,
@@ -181,13 +186,15 @@
 	 * required
 	 */
 
+	/* State management: Is not required while turning a system on */
+
 	/*
 	 * Plat. management: Give the platform the current state
 	 * of the target cpu to allow it to perform the necessary
 	 * steps to power on.
 	 */
 	if (psci_plat_pm_ops->affinst_on) {
-		plat_state = psci_get_aff_phys_state(system_node);
+		plat_state = psci_get_phys_state(system_node);
 		psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
 		rc = psci_plat_pm_ops->affinst_on(target_cpu,
 						  psci_entrypoint,
@@ -299,19 +306,7 @@
 				   target_cpu,
 				   entrypoint,
 				   context_id);
-	if (rc != PSCI_E_SUCCESS)
-		goto exit;
-
-	/*
-	 * State management: Update the state of each affinity instance
-	 * between the start and end affinity levels
-	 */
-	psci_change_state(target_cpu_nodes,
-			  start_afflvl,
-			  end_afflvl,
-			  PSCI_STATE_ON_PENDING);
 
-exit:
 	/*
 	 * This loop releases the lock corresponding to each affinity level
 	 * in the reverse order to which they were acquired.
@@ -336,7 +331,7 @@
 	assert(cpu_node->level == MPIDR_AFFLVL0);
 
 	/* Ensure we have been explicitly woken up by another cpu */
-	state = psci_get_state(cpu_node->state);
+	state = psci_get_state(cpu_node);
 	assert(state == PSCI_STATE_ON_PENDING);
 
 	/*
@@ -348,7 +343,7 @@
 	if (psci_plat_pm_ops->affinst_on_finish) {
 
 		/* Get the physical state of this cpu */
-		plat_state = psci_get_phys_state(state);
+		plat_state = get_phys_state(state);
 		rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
 							 cpu_node->level,
 							 plat_state);
@@ -377,6 +372,9 @@
 	index = cpu_node->data;
 	psci_get_ns_entry_info(index);
 
+	/* State management: mark this cpu as on */
+	psci_set_state(cpu_node, PSCI_STATE_ON);
+
 	/* Clean caches before re-entering normal world */
 	dcsw_op_louis(DCCSW);
 
@@ -401,13 +399,16 @@
 	if (psci_plat_pm_ops->affinst_on_finish) {
 
 		/* Get the physical state of this cluster */
-		plat_state = psci_get_aff_phys_state(cluster_node);
+		plat_state = psci_get_phys_state(cluster_node);
 		rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
 							 cluster_node->level,
 							 plat_state);
 		assert(rc == PSCI_E_SUCCESS);
 	}
 
+	/* State management: Increment the cluster reference count */
+	psci_set_state(cluster_node, PSCI_STATE_ON);
+
 	return rc;
 }
 
@@ -436,13 +437,16 @@
 	if (psci_plat_pm_ops->affinst_on_finish) {
 
 		/* Get the physical state of the system */
-		plat_state = psci_get_aff_phys_state(system_node);
+		plat_state = psci_get_phys_state(system_node);
 		rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
 							 system_node->level,
 							 plat_state);
 		assert(rc == PSCI_E_SUCCESS);
 	}
 
+	/* State management: Increment the system reference count */
+	psci_set_state(system_node, PSCI_STATE_ON);
+
 	return rc;
 }