developer | 451d49d | 2022-11-16 21:52:21 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023, MediaTek Inc. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <common/debug.h> |
| 8 | #include <drivers/spm/mt_spm_resource_req.h> |
| 9 | #include <lib/pm/mtk_pm.h> |
| 10 | #include <lpm/mt_lp_api.h> |
| 11 | #include <lpm/mt_lp_rm.h> |
| 12 | #include <mt_spm.h> |
| 13 | #include <mt_spm_cond.h> |
| 14 | #include <mt_spm_conservation.h> |
| 15 | #include <mt_spm_constraint.h> |
| 16 | #include <mt_spm_idle.h> |
| 17 | #include <mt_spm_internal.h> |
| 18 | #include <mt_spm_notifier.h> |
| 19 | #include "mt_spm_rc_api.h" |
| 20 | #include "mt_spm_rc_internal.h" |
| 21 | #include <mt_spm_reg.h> |
| 22 | #include <mt_spm_suspend.h> |
| 23 | |
| 24 | #define CONSTRAINT_DRAM_ALLOW (MT_RM_CONSTRAINT_ALLOW_DRAM_S0 | \ |
| 25 | MT_RM_CONSTRAINT_ALLOW_DRAM_S1 | \ |
| 26 | MT_RM_CONSTRAINT_ALLOW_CPU_BUCK_OFF) |
| 27 | |
| 28 | #define CONSTRAINT_DRAM_PCM_FLAG (SPM_FLAG_DISABLE_INFRA_PDN | \ |
| 29 | SPM_FLAG_DISABLE_VCORE_DVS | \ |
| 30 | SPM_FLAG_DISABLE_VCORE_DFS | \ |
| 31 | SPM_FLAG_SRAM_SLEEP_CTRL | \ |
| 32 | SPM_FLAG_KEEP_CSYSPWRACK_HIGH | \ |
| 33 | SPM_FLAG_DISABLE_DRAMC_MCU_SRAM_SLEEP) |
| 34 | |
| 35 | #define CONSTRAINT_DRAM_PCM_FLAG1 (0) |
| 36 | |
| 37 | #define CONSTRAINT_DRAM_RESOURCE_REQ (MT_SPM_SYSPLL | MT_SPM_INFRA | MT_SPM_26M) |
| 38 | |
| 39 | static struct mt_spm_cond_tables cond_dram = { |
| 40 | .table_cg = { |
| 41 | 0xFF5DD002, /* MTCMOS1 */ |
| 42 | 0x0000003C, /* MTCMOS2 */ |
| 43 | 0x27AF8000, /* INFRA0 */ |
| 44 | 0x20010876, /* INFRA1 */ |
| 45 | 0x86000640, /* INFRA2 */ |
| 46 | 0x00000000, /* INFRA3 */ |
| 47 | 0x80000000, /* INFRA4 */ |
| 48 | 0x01002A00, /* PERI0 */ |
| 49 | 0x00080000, /* VPPSYS0_0 */ |
| 50 | 0x38803000, /* VPPSYS0_1 */ |
| 51 | 0x00081450, /* VPPSYS1_0 */ |
| 52 | 0x00003000, /* VPPSYS1_1 */ |
| 53 | 0x00000000, /* VDOSYS0_0 */ |
| 54 | 0x00000000, /* VDOSYS0_1 */ |
| 55 | 0x000001F8, /* VDOSYS1_0 */ |
| 56 | 0x000001E0, /* VDOSYS1_1 */ |
| 57 | 0x00FB0007, /* VDOSYS1_2 */ |
| 58 | }, |
| 59 | .table_pll = 0U, |
| 60 | }; |
| 61 | |
| 62 | static struct mt_spm_cond_tables cond_dram_res = { |
| 63 | .table_cg = { 0U }, |
| 64 | .table_pll = 0U, |
| 65 | }; |
| 66 | |
| 67 | static struct constraint_status status = { |
| 68 | .id = MT_RM_CONSTRAINT_ID_DRAM, |
| 69 | .is_valid = (MT_SPM_RC_VALID_SW | |
| 70 | MT_SPM_RC_VALID_COND_CHECK | |
| 71 | MT_SPM_RC_VALID_COND_LATCH | |
| 72 | MT_SPM_RC_VALID_XSOC_BBLPM | |
| 73 | MT_SPM_RC_VALID_TRACE_TIME), |
| 74 | .is_cond_block = 0U, |
| 75 | .enter_cnt = 0U, |
| 76 | .cond_res = &cond_dram_res, |
| 77 | .residency = 0ULL, |
| 78 | }; |
| 79 | |
| 80 | static unsigned short ext_status_dram; |
| 81 | |
| 82 | int spm_dram_conduct(int state_id, struct spm_lp_scen *spm_lp, unsigned int *resource_req) |
| 83 | { |
| 84 | unsigned int res_req = CONSTRAINT_DRAM_RESOURCE_REQ; |
| 85 | |
| 86 | if ((spm_lp == NULL) || (resource_req == NULL)) { |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | spm_lp->pwrctrl->pcm_flags = (uint32_t)CONSTRAINT_DRAM_PCM_FLAG; |
| 91 | spm_lp->pwrctrl->pcm_flags1 = (uint32_t)CONSTRAINT_DRAM_PCM_FLAG1; |
| 92 | |
| 93 | *resource_req |= res_req; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | bool spm_is_valid_rc_dram(unsigned int cpu, int state_id) |
| 98 | { |
| 99 | return (!(status.is_cond_block && (status.is_valid & MT_SPM_RC_VALID_COND_CHECK)) && |
| 100 | IS_MT_RM_RC_READY(status.is_valid) && |
| 101 | (IS_PLAT_SUSPEND_ID(state_id) || |
| 102 | (state_id == MT_PLAT_PWR_STATE_SYSTEM_MEM) || |
| 103 | (state_id == MT_PLAT_PWR_STATE_SYSTEM_PLL) || |
| 104 | (state_id == MT_PLAT_PWR_STATE_SYSTEM_BUS))); |
| 105 | } |
| 106 | |
| 107 | static int update_rc_condition(const void *val) |
| 108 | { |
| 109 | const struct mt_spm_cond_tables *tlb = (const struct mt_spm_cond_tables *)val; |
| 110 | const struct mt_spm_cond_tables *tlb_check = (const struct mt_spm_cond_tables *)&cond_dram; |
| 111 | |
| 112 | if (tlb == NULL) { |
| 113 | return MT_RM_STATUS_BAD; |
| 114 | } |
| 115 | |
| 116 | status.is_cond_block = mt_spm_cond_check(tlb, tlb_check, |
| 117 | (status.is_valid & MT_SPM_RC_VALID_COND_LATCH) ? |
| 118 | &cond_dram_res : NULL); |
| 119 | return MT_RM_STATUS_OK; |
| 120 | } |
| 121 | |
| 122 | static void update_rc_clkbuf_status(const void *val) |
| 123 | { |
| 124 | unsigned int is_flight = (val) ? !!(*((unsigned int *)val) == FLIGHT_MODE_ON) : 0; |
| 125 | |
| 126 | if (is_flight != 0U) { |
| 127 | spm_rc_constraint_valid_set(MT_RM_CONSTRAINT_ID_DRAM, |
| 128 | MT_RM_CONSTRAINT_ID_DRAM, |
| 129 | MT_SPM_RC_VALID_FLIGHTMODE, |
| 130 | (struct constraint_status * const)&status); |
| 131 | } else { |
| 132 | spm_rc_constraint_valid_clr(MT_RM_CONSTRAINT_ID_DRAM, |
| 133 | MT_RM_CONSTRAINT_ID_DRAM, |
| 134 | MT_SPM_RC_VALID_FLIGHTMODE, |
| 135 | (struct constraint_status * const)&status); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static void update_rc_ufs_status(const void *val) |
| 140 | { |
| 141 | unsigned int is_ufs_h8 = (val) ? !!(*((unsigned int *)val) == UFS_REF_CLK_OFF) : 0; |
| 142 | |
| 143 | if (is_ufs_h8 != 0U) { |
| 144 | spm_rc_constraint_valid_set(MT_RM_CONSTRAINT_ID_DRAM, |
| 145 | MT_RM_CONSTRAINT_ID_DRAM, |
| 146 | MT_SPM_RC_VALID_UFS_H8, |
| 147 | (struct constraint_status * const)&status); |
| 148 | } else { |
| 149 | spm_rc_constraint_valid_clr(MT_RM_CONSTRAINT_ID_DRAM, |
| 150 | MT_RM_CONSTRAINT_ID_DRAM, |
| 151 | MT_SPM_RC_VALID_UFS_H8, |
| 152 | (struct constraint_status * const)&status); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void update_rc_status(const void *val) |
| 157 | { |
| 158 | const struct rc_common_state *st; |
| 159 | |
| 160 | st = (const struct rc_common_state *)val; |
| 161 | |
| 162 | if (st == NULL) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if (st->type == CONSTRAINT_UPDATE_COND_CHECK) { |
| 167 | struct mt_spm_cond_tables * const tlb = &cond_dram; |
| 168 | |
| 169 | spm_rc_condition_modifier(st->id, st->act, st->value, |
| 170 | MT_RM_CONSTRAINT_ID_DRAM, tlb); |
| 171 | } else if ((st->type == CONSTRAINT_UPDATE_VALID) || |
| 172 | (st->type == CONSTRAINT_RESIDNECY)) { |
| 173 | spm_rc_constraint_status_set(st->id, st->type, st->act, |
| 174 | MT_RM_CONSTRAINT_ID_DRAM, |
| 175 | (struct constraint_status * const)st->value, |
| 176 | (struct constraint_status * const)&status); |
| 177 | } else { |
| 178 | INFO("[%s:%d] - Unknown type: 0x%x\n", __func__, __LINE__, st->type); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | int spm_update_rc_dram(int state_id, int type, const void *val) |
| 183 | { |
| 184 | int res = MT_RM_STATUS_OK; |
| 185 | |
| 186 | switch (type) { |
| 187 | case PLAT_RC_UPDATE_CONDITION: |
| 188 | res = update_rc_condition(val); |
| 189 | break; |
| 190 | case PLAT_RC_CLKBUF_STATUS: |
| 191 | update_rc_clkbuf_status(val); |
| 192 | break; |
| 193 | case PLAT_RC_UFS_STATUS: |
| 194 | update_rc_ufs_status(val); |
| 195 | break; |
| 196 | case PLAT_RC_STATUS: |
| 197 | update_rc_status(val); |
| 198 | break; |
| 199 | default: |
| 200 | INFO("[%s:%d] - Do nothing for type: %d\n", __func__, __LINE__, type); |
| 201 | break; |
| 202 | } |
| 203 | |
| 204 | return res; |
| 205 | } |
| 206 | |
| 207 | unsigned int spm_allow_rc_dram(int state_id) |
| 208 | { |
| 209 | return CONSTRAINT_DRAM_ALLOW; |
| 210 | } |
| 211 | |
| 212 | int spm_run_rc_dram(unsigned int cpu, int state_id) |
| 213 | { |
| 214 | unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; |
| 215 | unsigned int allows = CONSTRAINT_DRAM_ALLOW; |
| 216 | |
| 217 | ext_status_dram = status.is_valid; |
| 218 | |
| 219 | if (IS_MT_SPM_RC_BBLPM_MODE(ext_status_dram)) { |
| 220 | #ifdef MT_SPM_USING_SRCLKEN_RC |
| 221 | ext_op |= MT_SPM_EX_OP_SRCLKEN_RC_BBLPM; |
| 222 | #else |
| 223 | allows |= MT_RM_CONSTRAINT_ALLOW_BBLPM; |
| 224 | #endif |
| 225 | } |
| 226 | |
| 227 | #ifndef MTK_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT |
| 228 | mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_ENTER, allows | (IS_PLAT_SUSPEND_ID(state_id) ? |
| 229 | (MT_RM_CONSTRAINT_ALLOW_AP_SUSPEND) : (0U))); |
| 230 | #else |
| 231 | (void)allows; |
| 232 | #endif |
| 233 | |
| 234 | if (ext_status_dram & MT_SPM_RC_VALID_TRACE_TIME) { |
| 235 | ext_op |= MT_SPM_EX_OP_TRACE_TIMESTAMP_EN; |
| 236 | } |
| 237 | |
| 238 | if (IS_PLAT_SUSPEND_ID(state_id)) { |
| 239 | mt_spm_suspend_enter(state_id, |
| 240 | (MT_SPM_EX_OP_CLR_26M_RECORD | |
| 241 | MT_SPM_EX_OP_SET_WDT | |
| 242 | MT_SPM_EX_OP_SET_SUSPEND_MODE | |
| 243 | MT_SPM_EX_OP_HW_S1_DETECT), |
| 244 | CONSTRAINT_DRAM_RESOURCE_REQ); |
| 245 | } else { |
| 246 | mt_spm_idle_generic_enter(state_id, ext_op, spm_dram_conduct); |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | int spm_reset_rc_dram(unsigned int cpu, int state_id) |
| 253 | { |
| 254 | unsigned int ext_op = MT_SPM_EX_OP_HW_S1_DETECT; |
| 255 | unsigned int allows = CONSTRAINT_DRAM_ALLOW; |
| 256 | |
| 257 | if (IS_MT_SPM_RC_BBLPM_MODE(ext_status_dram)) { |
| 258 | #ifdef MT_SPM_USING_SRCLKEN_RC |
| 259 | ext_op |= MT_SPM_EX_OP_SRCLKEN_RC_BBLPM; |
| 260 | #else |
| 261 | allows |= MT_RM_CONSTRAINT_ALLOW_BBLPM; |
| 262 | #endif |
| 263 | } |
| 264 | |
| 265 | #ifndef MTK_PLAT_SPM_SSPM_NOTIFIER_UNSUPPORT |
| 266 | mt_spm_sspm_notify_u32(MT_SPM_NOTIFY_LP_LEAVE, allows); |
| 267 | #else |
| 268 | (void)allows; |
| 269 | #endif |
| 270 | |
| 271 | if (ext_status_dram & MT_SPM_RC_VALID_TRACE_TIME) { |
| 272 | ext_op |= MT_SPM_EX_OP_TRACE_TIMESTAMP_EN; |
| 273 | } |
| 274 | |
| 275 | if (IS_PLAT_SUSPEND_ID(state_id)) { |
| 276 | mt_spm_suspend_resume(state_id, |
| 277 | (MT_SPM_EX_OP_SET_WDT | MT_SPM_EX_OP_HW_S1_DETECT), |
| 278 | NULL); |
| 279 | } else { |
| 280 | struct wake_status *waken = NULL; |
| 281 | |
| 282 | if (spm_unlikely(status.is_valid & MT_SPM_RC_VALID_TRACE_EVENT)) { |
| 283 | ext_op |= MT_SPM_EX_OP_TRACE_LP; |
| 284 | } |
| 285 | mt_spm_idle_generic_resume(state_id, ext_op, &waken, NULL); |
| 286 | status.enter_cnt++; |
| 287 | |
| 288 | if (spm_unlikely(status.is_valid & MT_SPM_RC_VALID_RESIDNECY)) { |
| 289 | status.residency += (waken != NULL) ? waken->tr.comm.timer_out : 0; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | int spm_get_status_rc_dram(unsigned int type, void *priv) |
| 297 | { |
| 298 | int ret = MT_RM_STATUS_OK; |
| 299 | |
| 300 | if (type == PLAT_RC_STATUS) { |
| 301 | int res = 0; |
| 302 | struct rc_common_state *st = (struct rc_common_state *)priv; |
| 303 | |
| 304 | if (st == NULL) { |
| 305 | return MT_RM_STATUS_BAD; |
| 306 | } |
| 307 | |
| 308 | res = spm_rc_constraint_status_get(st->id, st->type, |
| 309 | st->act, MT_RM_CONSTRAINT_ID_DRAM, |
| 310 | (struct constraint_status * const)&status, |
| 311 | (struct constraint_status * const)st->value); |
| 312 | if ((res == 0) && (st->id != MT_RM_CONSTRAINT_ID_ALL)) { |
| 313 | ret = MT_RM_STATUS_STOP; |
| 314 | } |
| 315 | } |
| 316 | return ret; |
| 317 | } |