Soby Mathew | 200fffd | 2016-10-21 11:34:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 200fffd | 2016-10-21 11:34:59 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
| 8 | #include <assert.h> |
| 9 | #include <css_pm.h> |
| 10 | #include <debug.h> |
dp-arm | 199fb4a | 2017-04-11 11:48:49 +0100 | [diff] [blame] | 11 | #include <plat_arm.h> |
Soby Mathew | 200fffd | 2016-10-21 11:34:59 +0100 | [diff] [blame] | 12 | #include "../scpi/css_scpi.h" |
| 13 | #include "css_scp.h" |
| 14 | |
| 15 | /* |
| 16 | * This file implements the SCP power management functions using SCPI protocol. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Helper function to inform power down state to SCP. |
| 21 | */ |
| 22 | void css_scp_suspend(const psci_power_state_t *target_state) |
| 23 | { |
| 24 | uint32_t cluster_state = scpi_power_on; |
| 25 | uint32_t system_state = scpi_power_on; |
| 26 | |
| 27 | /* Check if power down at system power domain level is requested */ |
| 28 | if (CSS_SYSTEM_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) |
| 29 | system_state = scpi_power_retention; |
| 30 | |
| 31 | /* Cluster is to be turned off, so disable coherency */ |
| 32 | if (CSS_CLUSTER_PWR_STATE(target_state) == ARM_LOCAL_STATE_OFF) |
| 33 | cluster_state = scpi_power_off; |
| 34 | |
| 35 | /* |
| 36 | * Ask the SCP to power down the appropriate components depending upon |
| 37 | * their state. |
| 38 | */ |
| 39 | scpi_set_css_power_state(read_mpidr_el1(), |
| 40 | scpi_power_off, |
| 41 | cluster_state, |
| 42 | system_state); |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * Helper function to turn off a CPU power domain and its parent power domains |
| 47 | * if applicable. Since SCPI doesn't differentiate between OFF and suspend, we |
| 48 | * call the suspend helper here. |
| 49 | */ |
| 50 | void css_scp_off(const psci_power_state_t *target_state) |
| 51 | { |
| 52 | css_scp_suspend(target_state); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Helper function to turn ON a CPU power domain and its parent power domains |
| 57 | * if applicable. |
| 58 | */ |
| 59 | void css_scp_on(u_register_t mpidr) |
| 60 | { |
| 61 | /* |
| 62 | * SCP takes care of powering up parent power domains so we |
| 63 | * only need to care about level 0 |
| 64 | */ |
| 65 | scpi_set_css_power_state(mpidr, scpi_power_on, scpi_power_on, |
| 66 | scpi_power_on); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Helper function to get the power state of a power domain node as reported |
| 71 | * by the SCP. |
| 72 | */ |
| 73 | int css_scp_get_power_state(u_register_t mpidr, unsigned int power_level) |
| 74 | { |
| 75 | int rc, element; |
| 76 | unsigned int cpu_state, cluster_state; |
| 77 | |
| 78 | /* |
| 79 | * The format of 'power_level' is implementation-defined, but 0 must |
| 80 | * mean a CPU. We also allow 1 to denote the cluster |
| 81 | */ |
| 82 | if (power_level != ARM_PWR_LVL0 && power_level != ARM_PWR_LVL1) |
| 83 | return PSCI_E_INVALID_PARAMS; |
| 84 | |
| 85 | /* Query SCP */ |
| 86 | rc = scpi_get_css_power_state(mpidr, &cpu_state, &cluster_state); |
| 87 | if (rc != 0) |
| 88 | return PSCI_E_INVALID_PARAMS; |
| 89 | |
| 90 | /* Map power states of CPU and cluster to expected PSCI return codes */ |
| 91 | if (power_level == ARM_PWR_LVL0) { |
| 92 | /* |
| 93 | * The CPU state returned by SCP is an 8-bit bit mask |
| 94 | * corresponding to each CPU in the cluster |
| 95 | */ |
| 96 | element = mpidr & MPIDR_AFFLVL_MASK; |
| 97 | return CSS_CPU_PWR_STATE(cpu_state, element) == |
| 98 | CSS_CPU_PWR_STATE_ON ? HW_ON : HW_OFF; |
| 99 | } else { |
| 100 | assert(cluster_state == CSS_CLUSTER_PWR_STATE_ON || |
| 101 | cluster_state == CSS_CLUSTER_PWR_STATE_OFF); |
| 102 | return cluster_state == CSS_CLUSTER_PWR_STATE_ON ? HW_ON : |
| 103 | HW_OFF; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Helper function to shutdown the system via SCPI. |
| 109 | */ |
| 110 | void __dead2 css_scp_sys_shutdown(void) |
| 111 | { |
| 112 | uint32_t response; |
| 113 | |
dp-arm | 199fb4a | 2017-04-11 11:48:49 +0100 | [diff] [blame] | 114 | /* |
| 115 | * Disable GIC CPU interface to prevent pending interrupt |
| 116 | * from waking up the AP from WFI. |
| 117 | */ |
| 118 | plat_arm_gic_cpuif_disable(); |
| 119 | |
Soby Mathew | 200fffd | 2016-10-21 11:34:59 +0100 | [diff] [blame] | 120 | /* Send the power down request to the SCP */ |
| 121 | response = scpi_sys_power_state(scpi_system_shutdown); |
| 122 | |
| 123 | if (response != SCP_OK) { |
| 124 | ERROR("CSS System Off: SCP error %u.\n", response); |
| 125 | panic(); |
| 126 | } |
| 127 | wfi(); |
| 128 | ERROR("CSS System Off: operation not handled.\n"); |
| 129 | panic(); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Helper function to reset the system via SCPI. |
| 134 | */ |
| 135 | void __dead2 css_scp_sys_reboot(void) |
| 136 | { |
| 137 | uint32_t response; |
| 138 | |
dp-arm | 199fb4a | 2017-04-11 11:48:49 +0100 | [diff] [blame] | 139 | /* |
| 140 | * Disable GIC CPU interface to prevent pending interrupt |
| 141 | * from waking up the AP from WFI. |
| 142 | */ |
| 143 | plat_arm_gic_cpuif_disable(); |
| 144 | |
Soby Mathew | 200fffd | 2016-10-21 11:34:59 +0100 | [diff] [blame] | 145 | /* Send the system reset request to the SCP */ |
| 146 | response = scpi_sys_power_state(scpi_system_reboot); |
| 147 | |
| 148 | if (response != SCP_OK) { |
| 149 | ERROR("CSS System Reset: SCP error %u.\n", response); |
| 150 | panic(); |
| 151 | } |
| 152 | wfi(); |
| 153 | ERROR("CSS System Reset: operation not handled.\n"); |
| 154 | panic(); |
| 155 | } |