1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2013, ARM Ltd 4 * Copyright (c) 2014, Allwinner Technology Co., Ltd. 5 * Copyright (c) 2018, Linaro Limited 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright notice, 15 * this list of conditions and the following disclaimer in the documentation 16 * and/or other materials provided with the distribution. 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 <console.h> 32 #include <io.h> 33 #include <stdint.h> 34 #include <kernel/generic_boot.h> 35 #include <kernel/misc.h> 36 #include <kernel/panic.h> 37 #include <kernel/pm_stubs.h> 38 #include <kernel/delay.h> 39 #include <mm/core_mmu.h> 40 #include <mm/core_memprot.h> 41 #include <mm/tee_pager.h> 42 #include <platform_config.h> 43 #include <sm/tee_mon.h> 44 #include <sm/optee_smc.h> 45 #include <sm/psci.h> 46 #include <arm32.h> 47 48 #define REG_CPUCFG_RES0 (0x0000) 49 #define REG_CPUCFG_CPU_RST(cpu) (0x0040 + (cpu) * (0x0040)) 50 #define REG_CPUCFG_GEN_CTRL (0x0184) 51 #define REG_CPUCFG_PRIV0 (0x01a4) 52 #define REG_CPUCFG_DBG_CTRL1 (0x01e4) 53 #define REG_PRCM_CPU_PWROFF (0x0100) 54 #define REG_PRCM_CPU_PWR_CLAMP(cpu) (0x0140 + (cpu) * (0x0004)) 55 56 int psci_features(uint32_t psci_fid) 57 { 58 switch (psci_fid) { 59 #ifdef CFG_BOOT_SECONDARY_REQUEST 60 case PSCI_CPU_ON: 61 return 0; 62 #endif 63 64 default: 65 return PSCI_RET_NOT_SUPPORTED; 66 } 67 } 68 69 #ifdef CFG_BOOT_SECONDARY_REQUEST 70 int psci_cpu_on(uint32_t core_idx, uint32_t entry, 71 uint32_t context_id) 72 { 73 vaddr_t base = (vaddr_t)phys_to_virt(SUNXI_PRCM_BASE, MEM_AREA_IO_SEC); 74 vaddr_t cpucfg = (vaddr_t)phys_to_virt(SUNXI_CPUCFG_BASE, 75 MEM_AREA_IO_SEC); 76 uint32_t tmpff; 77 uint32_t val; 78 79 assert(base); 80 assert(cpucfg); 81 82 if ((core_idx == 0) || (core_idx >= CFG_TEE_CORE_NB_CORE)) 83 return PSCI_RET_INVALID_PARAMETERS; 84 85 /* set secondary cores' NS entry addresses */ 86 generic_boot_set_core_ns_entry(core_idx, entry, context_id); 87 88 val = virt_to_phys((void *)TEE_TEXT_VA_START); 89 90 /* set entry address */ 91 DMSG("set entry address for CPU %d", core_idx); 92 io_write32(cpucfg + REG_CPUCFG_PRIV0, val); 93 94 /* assert reset on target CPU */ 95 DMSG("assert reset on target CPU %d", core_idx); 96 io_write32(cpucfg + REG_CPUCFG_CPU_RST(core_idx), 0); 97 98 /* invalidate L1 cache */ 99 DMSG("invalidate L1 cache for CPU %d", core_idx); 100 io_clrbits32(cpucfg + REG_CPUCFG_GEN_CTRL, BIT32(core_idx)); 101 102 /* lock CPU (Disable external debug access) */ 103 DMSG("lock CPU %d", core_idx); 104 io_clrbits32(cpucfg + REG_CPUCFG_DBG_CTRL1, BIT32(core_idx)); 105 106 /* release clamp */ 107 DMSG("release clamp for CPU %d", core_idx); 108 tmpff = 0x1ff; 109 do { 110 tmpff >>= 1; 111 io_write32(base + REG_PRCM_CPU_PWR_CLAMP(core_idx), tmpff); 112 } while (tmpff); 113 mdelay(10); 114 115 /* clear power gating */ 116 DMSG("clear power gating for CPU %d", core_idx); 117 io_clrbits32(base + REG_PRCM_CPU_PWROFF, BIT32(core_idx)); 118 udelay(1000); 119 120 /* de-assert reset on target CPU */ 121 DMSG("de-assert reset on target CPU %d", core_idx); 122 io_write32(cpucfg + REG_CPUCFG_CPU_RST(core_idx), 0x03); 123 124 /* unlock CPU (enable external debug access) */ 125 DMSG("unlock CPU %d", core_idx); 126 io_setbits32(cpucfg + REG_CPUCFG_DBG_CTRL1, BIT32(core_idx)); 127 128 return PSCI_RET_SUCCESS; 129 } 130 131 int psci_cpu_off(void) 132 { 133 uint32_t core_id; 134 vaddr_t base = (vaddr_t)phys_to_virt(SUNXI_PRCM_BASE, MEM_AREA_IO_SEC); 135 vaddr_t cpucfg = (vaddr_t)phys_to_virt(SUNXI_CPUCFG_BASE, 136 MEM_AREA_IO_SEC); 137 138 core_id = get_core_pos(); 139 140 DMSG("core_id: %" PRIu32, core_id); 141 142 #ifdef CFG_PSCI_ARM32 143 psci_armv7_cpu_off(); 144 #endif /* CFG_PSCI_ARM32 */ 145 146 assert(base); 147 assert(cpucfg); 148 149 /* set power gating */ 150 DMSG("set power gating for cpu %d", core_id); 151 io_setbits32(base + REG_PRCM_CPU_PWROFF, BIT32(core_id)); 152 153 /* Activate power clamp */ 154 DMSG("Activate power clamp for cpu %d", core_id); 155 io_write32(base + REG_PRCM_CPU_PWR_CLAMP(core_id), 0xff); 156 157 while (true) 158 wfi(); 159 160 return PSCI_RET_INTERNAL_FAILURE; 161 } 162 #endif 163