Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2013, ARM Limited. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <assert.h> |
| 34 | #include <arch_helpers.h> |
| 35 | #include <console.h> |
| 36 | #include <platform.h> |
| 37 | #include <bl_common.h> |
| 38 | #include <bl31.h> |
| 39 | #include <bakery_lock.h> |
| 40 | #include <cci400.h> |
| 41 | #include <gic.h> |
| 42 | #include <fvp_pwrc.h> |
| 43 | /* Only included for error codes */ |
| 44 | #include <psci.h> |
| 45 | |
| 46 | /******************************************************************************* |
| 47 | * FVP handler called when an affinity instance is about to be turned on. The |
| 48 | * level and mpidr determine the affinity instance. |
| 49 | ******************************************************************************/ |
| 50 | int fvp_affinst_on(unsigned long mpidr, |
| 51 | unsigned long sec_entrypoint, |
| 52 | unsigned long ns_entrypoint, |
| 53 | unsigned int afflvl, |
| 54 | unsigned int state) |
| 55 | { |
| 56 | int rc = PSCI_E_SUCCESS; |
| 57 | unsigned long linear_id; |
| 58 | mailbox *fvp_mboxes; |
| 59 | unsigned int psysr; |
| 60 | |
| 61 | if (ns_entrypoint < DRAM_BASE) { |
| 62 | rc = PSCI_E_INVALID_PARAMS; |
| 63 | goto exit; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * It's possible to turn on only affinity level 0 i.e. a cpu |
| 68 | * on the FVP. Ignore any other affinity level. |
| 69 | */ |
| 70 | if (afflvl != MPIDR_AFFLVL0) |
| 71 | goto exit; |
| 72 | |
| 73 | /* |
| 74 | * Ensure that we do not cancel an inflight power off request |
| 75 | * for the target cpu. That would leave it in a zombie wfi. |
| 76 | * Wait for it to power off, program the jump address for the |
| 77 | * target cpu and then program the power controller to turn |
| 78 | * that cpu on |
| 79 | */ |
| 80 | do { |
| 81 | psysr = fvp_pwrc_read_psysr(mpidr); |
| 82 | } while (psysr & PSYSR_AFF_L0); |
| 83 | |
| 84 | linear_id = platform_get_core_pos(mpidr); |
| 85 | fvp_mboxes = (mailbox *) (TZDRAM_BASE + MBOX_OFF); |
| 86 | fvp_mboxes[linear_id].value = sec_entrypoint; |
| 87 | flush_dcache_range((unsigned long) &fvp_mboxes[linear_id], |
| 88 | sizeof(unsigned long)); |
| 89 | |
| 90 | fvp_pwrc_write_pponr(mpidr); |
| 91 | |
| 92 | exit: |
| 93 | return rc; |
| 94 | } |
| 95 | |
| 96 | /******************************************************************************* |
| 97 | * FVP handler called when an affinity instance is about to be turned off. The |
| 98 | * level and mpidr determine the affinity instance. The 'state' arg. allows the |
| 99 | * platform to decide whether the cluster is being turned off and take apt |
| 100 | * actions. |
| 101 | * |
| 102 | * CAUTION: This function is called with coherent stacks so that caches can be |
| 103 | * turned off, flushed and coherency disabled. There is no guarantee that caches |
| 104 | * will remain turned on across calls to this function as each affinity level is |
| 105 | * dealt with. So do not write & read global variables across calls. It will be |
| 106 | * wise to do flush a write to the global to prevent unpredictable results. |
| 107 | ******************************************************************************/ |
| 108 | int fvp_affinst_off(unsigned long mpidr, |
| 109 | unsigned int afflvl, |
| 110 | unsigned int state) |
| 111 | { |
| 112 | int rc = PSCI_E_SUCCESS; |
| 113 | unsigned int gicc_base, ectlr; |
| 114 | unsigned long cpu_setup; |
| 115 | |
| 116 | switch (afflvl) { |
| 117 | case MPIDR_AFFLVL1: |
| 118 | if (state == PSCI_STATE_OFF) { |
| 119 | /* |
| 120 | * Disable coherency if this cluster is to be |
| 121 | * turned off |
| 122 | */ |
| 123 | cci_disable_coherency(mpidr); |
| 124 | |
| 125 | /* |
| 126 | * Program the power controller to turn the |
| 127 | * cluster off |
| 128 | */ |
| 129 | fvp_pwrc_write_pcoffr(mpidr); |
| 130 | |
| 131 | } |
| 132 | break; |
| 133 | |
| 134 | case MPIDR_AFFLVL0: |
| 135 | if (state == PSCI_STATE_OFF) { |
| 136 | |
| 137 | /* |
| 138 | * Take this cpu out of intra-cluster coherency if |
| 139 | * the FVP flavour supports the SMP bit. |
| 140 | */ |
| 141 | cpu_setup = platform_get_cfgvar(CONFIG_CPU_SETUP); |
| 142 | if (cpu_setup) { |
| 143 | ectlr = read_cpuectlr(); |
| 144 | ectlr &= ~CPUECTLR_SMP_BIT; |
| 145 | write_cpuectlr(ectlr); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Prevent interrupts from spuriously waking up |
| 150 | * this cpu |
| 151 | */ |
| 152 | gicc_base = platform_get_cfgvar(CONFIG_GICC_ADDR); |
| 153 | gic_cpuif_deactivate(gicc_base); |
| 154 | |
| 155 | /* |
| 156 | * Program the power controller to power this |
| 157 | * cpu off |
| 158 | */ |
| 159 | fvp_pwrc_write_ppoffr(mpidr); |
| 160 | } |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | assert(0); |
| 165 | } |
| 166 | |
| 167 | return rc; |
| 168 | } |
| 169 | |
| 170 | /******************************************************************************* |
| 171 | * FVP handler called when an affinity instance is about to be suspended. The |
| 172 | * level and mpidr determine the affinity instance. The 'state' arg. allows the |
| 173 | * platform to decide whether the cluster is being turned off and take apt |
| 174 | * actions. |
| 175 | * |
| 176 | * CAUTION: This function is called with coherent stacks so that caches can be |
| 177 | * turned off, flushed and coherency disabled. There is no guarantee that caches |
| 178 | * will remain turned on across calls to this function as each affinity level is |
| 179 | * dealt with. So do not write & read global variables across calls. It will be |
| 180 | * wise to do flush a write to the global to prevent unpredictable results. |
| 181 | ******************************************************************************/ |
| 182 | int fvp_affinst_suspend(unsigned long mpidr, |
| 183 | unsigned long sec_entrypoint, |
| 184 | unsigned long ns_entrypoint, |
| 185 | unsigned int afflvl, |
| 186 | unsigned int state) |
| 187 | { |
| 188 | int rc = PSCI_E_SUCCESS; |
| 189 | unsigned int gicc_base, ectlr; |
| 190 | unsigned long cpu_setup, linear_id; |
| 191 | mailbox *fvp_mboxes; |
| 192 | |
| 193 | /* Cannot allow NS world to execute trusted firmware code */ |
| 194 | if (ns_entrypoint < DRAM_BASE) { |
| 195 | rc = PSCI_E_INVALID_PARAMS; |
| 196 | goto exit; |
| 197 | } |
| 198 | |
| 199 | switch (afflvl) { |
| 200 | case MPIDR_AFFLVL1: |
| 201 | if (state == PSCI_STATE_OFF) { |
| 202 | /* |
| 203 | * Disable coherency if this cluster is to be |
| 204 | * turned off |
| 205 | */ |
| 206 | cci_disable_coherency(mpidr); |
| 207 | |
| 208 | /* |
| 209 | * Program the power controller to turn the |
| 210 | * cluster off |
| 211 | */ |
| 212 | fvp_pwrc_write_pcoffr(mpidr); |
| 213 | |
| 214 | } |
| 215 | break; |
| 216 | |
| 217 | case MPIDR_AFFLVL0: |
| 218 | if (state == PSCI_STATE_OFF) { |
| 219 | /* |
| 220 | * Take this cpu out of intra-cluster coherency if |
| 221 | * the FVP flavour supports the SMP bit. |
| 222 | */ |
| 223 | cpu_setup = platform_get_cfgvar(CONFIG_CPU_SETUP); |
| 224 | if (cpu_setup) { |
| 225 | ectlr = read_cpuectlr(); |
| 226 | ectlr &= ~CPUECTLR_SMP_BIT; |
| 227 | write_cpuectlr(ectlr); |
| 228 | } |
| 229 | |
| 230 | /* Program the jump address for the target cpu */ |
| 231 | linear_id = platform_get_core_pos(mpidr); |
| 232 | fvp_mboxes = (mailbox *) (TZDRAM_BASE + MBOX_OFF); |
| 233 | fvp_mboxes[linear_id].value = sec_entrypoint; |
| 234 | flush_dcache_range((unsigned long) &fvp_mboxes[linear_id], |
| 235 | sizeof(unsigned long)); |
| 236 | |
| 237 | /* |
| 238 | * Prevent interrupts from spuriously waking up |
| 239 | * this cpu |
| 240 | */ |
| 241 | gicc_base = platform_get_cfgvar(CONFIG_GICC_ADDR); |
| 242 | gic_cpuif_deactivate(gicc_base); |
| 243 | |
| 244 | /* |
| 245 | * Program the power controller to power this |
| 246 | * cpu off and enable wakeup interrupts. |
| 247 | */ |
| 248 | fvp_pwrc_write_pwkupr(mpidr); |
| 249 | fvp_pwrc_write_ppoffr(mpidr); |
| 250 | } |
| 251 | break; |
| 252 | |
| 253 | default: |
| 254 | assert(0); |
| 255 | } |
| 256 | |
| 257 | exit: |
| 258 | return rc; |
| 259 | } |
| 260 | |
| 261 | /******************************************************************************* |
| 262 | * FVP handler called when an affinity instance has just been powered on after |
| 263 | * being turned off earlier. The level and mpidr determine the affinity |
| 264 | * instance. The 'state' arg. allows the platform to decide whether the cluster |
| 265 | * was turned off prior to wakeup and do what's necessary to setup it up |
| 266 | * correctly. |
| 267 | ******************************************************************************/ |
| 268 | int fvp_affinst_on_finish(unsigned long mpidr, |
| 269 | unsigned int afflvl, |
| 270 | unsigned int state) |
| 271 | { |
| 272 | int rc = PSCI_E_SUCCESS; |
| 273 | unsigned long linear_id, cpu_setup; |
| 274 | mailbox *fvp_mboxes; |
| 275 | unsigned int gicd_base, gicc_base, reg_val, ectlr; |
| 276 | |
| 277 | switch (afflvl) { |
| 278 | |
| 279 | case MPIDR_AFFLVL1: |
| 280 | /* Enable coherency if this cluster was off */ |
| 281 | if (state == PSCI_STATE_OFF) |
| 282 | cci_enable_coherency(mpidr); |
| 283 | break; |
| 284 | |
| 285 | case MPIDR_AFFLVL0: |
| 286 | /* |
| 287 | * Ignore the state passed for a cpu. It could only have |
| 288 | * been off if we are here. |
| 289 | */ |
| 290 | |
| 291 | /* |
| 292 | * Turn on intra-cluster coherency if the FVP flavour supports |
| 293 | * it. |
| 294 | */ |
| 295 | cpu_setup = platform_get_cfgvar(CONFIG_CPU_SETUP); |
| 296 | if (cpu_setup) { |
| 297 | ectlr = read_cpuectlr(); |
| 298 | ectlr |= CPUECTLR_SMP_BIT; |
| 299 | write_cpuectlr(ectlr); |
| 300 | } |
| 301 | |
| 302 | /* Zero the jump address in the mailbox for this cpu */ |
| 303 | fvp_mboxes = (mailbox *) (TZDRAM_BASE + MBOX_OFF); |
| 304 | linear_id = platform_get_core_pos(mpidr); |
| 305 | fvp_mboxes[linear_id].value = 0; |
| 306 | flush_dcache_range((unsigned long) &fvp_mboxes[linear_id], |
| 307 | sizeof(unsigned long)); |
| 308 | |
| 309 | gicd_base = platform_get_cfgvar(CONFIG_GICD_ADDR); |
| 310 | gicc_base = platform_get_cfgvar(CONFIG_GICC_ADDR); |
| 311 | |
| 312 | /* Enable the gic cpu interface */ |
| 313 | gic_cpuif_setup(gicc_base); |
| 314 | |
| 315 | /* TODO: This setup is needed only after a cold boot */ |
| 316 | gic_pcpu_distif_setup(gicd_base); |
| 317 | |
| 318 | /* Allow access to the System counter timer module */ |
| 319 | reg_val = (1 << CNTACR_RPCT_SHIFT) | (1 << CNTACR_RVCT_SHIFT); |
| 320 | reg_val |= (1 << CNTACR_RFRQ_SHIFT) | (1 << CNTACR_RVOFF_SHIFT); |
| 321 | reg_val |= (1 << CNTACR_RWVT_SHIFT) | (1 << CNTACR_RWPT_SHIFT); |
| 322 | mmio_write_32(SYS_TIMCTL_BASE + CNTACR_BASE(0), reg_val); |
| 323 | mmio_write_32(SYS_TIMCTL_BASE + CNTACR_BASE(1), reg_val); |
| 324 | |
| 325 | reg_val = (1 << CNTNSAR_NS_SHIFT(0)) | |
| 326 | (1 << CNTNSAR_NS_SHIFT(1)); |
| 327 | mmio_write_32(SYS_TIMCTL_BASE + CNTNSAR, reg_val); |
| 328 | |
| 329 | break; |
| 330 | |
| 331 | default: |
| 332 | assert(0); |
| 333 | } |
| 334 | |
| 335 | return rc; |
| 336 | } |
| 337 | |
| 338 | /******************************************************************************* |
| 339 | * FVP handler called when an affinity instance has just been powered on after |
| 340 | * having been suspended earlier. The level and mpidr determine the affinity |
| 341 | * instance. |
| 342 | * TODO: At the moment we reuse the on finisher and reinitialize the secure |
| 343 | * context. Need to implement a separate suspend finisher. |
| 344 | ******************************************************************************/ |
| 345 | int fvp_affinst_suspend_finish(unsigned long mpidr, |
| 346 | unsigned int afflvl, |
| 347 | unsigned int state) |
| 348 | { |
| 349 | return fvp_affinst_on_finish(mpidr, afflvl, state); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /******************************************************************************* |
| 354 | * Export the platform handlers to enable psci to invoke them |
| 355 | ******************************************************************************/ |
| 356 | static plat_pm_ops fvp_plat_pm_ops = { |
| 357 | 0, |
| 358 | fvp_affinst_on, |
| 359 | fvp_affinst_off, |
| 360 | fvp_affinst_suspend, |
| 361 | fvp_affinst_on_finish, |
| 362 | fvp_affinst_suspend_finish, |
| 363 | }; |
| 364 | |
| 365 | /******************************************************************************* |
| 366 | * Export the platform specific power ops & initialize the fvp power controller |
| 367 | ******************************************************************************/ |
| 368 | int platform_setup_pm(plat_pm_ops **plat_ops) |
| 369 | { |
| 370 | *plat_ops = &fvp_plat_pm_ops; |
| 371 | return 0; |
| 372 | } |