Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Copyright (C) 2015 ARM Limited
0005  */
0006 
0007 #define pr_fmt(fmt) "psci: " fmt
0008 
0009 #include <linux/acpi.h>
0010 #include <linux/arm-smccc.h>
0011 #include <linux/cpuidle.h>
0012 #include <linux/errno.h>
0013 #include <linux/linkage.h>
0014 #include <linux/of.h>
0015 #include <linux/pm.h>
0016 #include <linux/printk.h>
0017 #include <linux/psci.h>
0018 #include <linux/reboot.h>
0019 #include <linux/slab.h>
0020 #include <linux/suspend.h>
0021 
0022 #include <uapi/linux/psci.h>
0023 
0024 #include <asm/cpuidle.h>
0025 #include <asm/cputype.h>
0026 #include <asm/hypervisor.h>
0027 #include <asm/system_misc.h>
0028 #include <asm/smp_plat.h>
0029 #include <asm/suspend.h>
0030 
0031 /*
0032  * While a 64-bit OS can make calls with SMC32 calling conventions, for some
0033  * calls it is necessary to use SMC64 to pass or return 64-bit values.
0034  * For such calls PSCI_FN_NATIVE(version, name) will choose the appropriate
0035  * (native-width) function ID.
0036  */
0037 #ifdef CONFIG_64BIT
0038 #define PSCI_FN_NATIVE(version, name)   PSCI_##version##_FN64_##name
0039 #else
0040 #define PSCI_FN_NATIVE(version, name)   PSCI_##version##_FN_##name
0041 #endif
0042 
0043 /*
0044  * The CPU any Trusted OS is resident on. The trusted OS may reject CPU_OFF
0045  * calls to its resident CPU, so we must avoid issuing those. We never migrate
0046  * a Trusted OS even if it claims to be capable of migration -- doing so will
0047  * require cooperation with a Trusted OS driver.
0048  */
0049 static int resident_cpu = -1;
0050 struct psci_operations psci_ops;
0051 static enum arm_smccc_conduit psci_conduit = SMCCC_CONDUIT_NONE;
0052 
0053 bool psci_tos_resident_on(int cpu)
0054 {
0055     return cpu == resident_cpu;
0056 }
0057 
0058 typedef unsigned long (psci_fn)(unsigned long, unsigned long,
0059                 unsigned long, unsigned long);
0060 static psci_fn *invoke_psci_fn;
0061 
0062 static struct psci_0_1_function_ids psci_0_1_function_ids;
0063 
0064 struct psci_0_1_function_ids get_psci_0_1_function_ids(void)
0065 {
0066     return psci_0_1_function_ids;
0067 }
0068 
0069 #define PSCI_0_2_POWER_STATE_MASK       \
0070                 (PSCI_0_2_POWER_STATE_ID_MASK | \
0071                 PSCI_0_2_POWER_STATE_TYPE_MASK | \
0072                 PSCI_0_2_POWER_STATE_AFFL_MASK)
0073 
0074 #define PSCI_1_0_EXT_POWER_STATE_MASK       \
0075                 (PSCI_1_0_EXT_POWER_STATE_ID_MASK | \
0076                 PSCI_1_0_EXT_POWER_STATE_TYPE_MASK)
0077 
0078 static u32 psci_cpu_suspend_feature;
0079 static bool psci_system_reset2_supported;
0080 
0081 static inline bool psci_has_ext_power_state(void)
0082 {
0083     return psci_cpu_suspend_feature &
0084                 PSCI_1_0_FEATURES_CPU_SUSPEND_PF_MASK;
0085 }
0086 
0087 bool psci_has_osi_support(void)
0088 {
0089     return psci_cpu_suspend_feature & PSCI_1_0_OS_INITIATED;
0090 }
0091 
0092 static inline bool psci_power_state_loses_context(u32 state)
0093 {
0094     const u32 mask = psci_has_ext_power_state() ?
0095                     PSCI_1_0_EXT_POWER_STATE_TYPE_MASK :
0096                     PSCI_0_2_POWER_STATE_TYPE_MASK;
0097 
0098     return state & mask;
0099 }
0100 
0101 bool psci_power_state_is_valid(u32 state)
0102 {
0103     const u32 valid_mask = psci_has_ext_power_state() ?
0104                    PSCI_1_0_EXT_POWER_STATE_MASK :
0105                    PSCI_0_2_POWER_STATE_MASK;
0106 
0107     return !(state & ~valid_mask);
0108 }
0109 
0110 static unsigned long __invoke_psci_fn_hvc(unsigned long function_id,
0111             unsigned long arg0, unsigned long arg1,
0112             unsigned long arg2)
0113 {
0114     struct arm_smccc_res res;
0115 
0116     arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
0117     return res.a0;
0118 }
0119 
0120 static unsigned long __invoke_psci_fn_smc(unsigned long function_id,
0121             unsigned long arg0, unsigned long arg1,
0122             unsigned long arg2)
0123 {
0124     struct arm_smccc_res res;
0125 
0126     arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
0127     return res.a0;
0128 }
0129 
0130 static int psci_to_linux_errno(int errno)
0131 {
0132     switch (errno) {
0133     case PSCI_RET_SUCCESS:
0134         return 0;
0135     case PSCI_RET_NOT_SUPPORTED:
0136         return -EOPNOTSUPP;
0137     case PSCI_RET_INVALID_PARAMS:
0138     case PSCI_RET_INVALID_ADDRESS:
0139         return -EINVAL;
0140     case PSCI_RET_DENIED:
0141         return -EPERM;
0142     }
0143 
0144     return -EINVAL;
0145 }
0146 
0147 static u32 psci_0_1_get_version(void)
0148 {
0149     return PSCI_VERSION(0, 1);
0150 }
0151 
0152 static u32 psci_0_2_get_version(void)
0153 {
0154     return invoke_psci_fn(PSCI_0_2_FN_PSCI_VERSION, 0, 0, 0);
0155 }
0156 
0157 int psci_set_osi_mode(bool enable)
0158 {
0159     unsigned long suspend_mode;
0160     int err;
0161 
0162     suspend_mode = enable ? PSCI_1_0_SUSPEND_MODE_OSI :
0163             PSCI_1_0_SUSPEND_MODE_PC;
0164 
0165     err = invoke_psci_fn(PSCI_1_0_FN_SET_SUSPEND_MODE, suspend_mode, 0, 0);
0166     return psci_to_linux_errno(err);
0167 }
0168 
0169 static int __psci_cpu_suspend(u32 fn, u32 state, unsigned long entry_point)
0170 {
0171     int err;
0172 
0173     err = invoke_psci_fn(fn, state, entry_point, 0);
0174     return psci_to_linux_errno(err);
0175 }
0176 
0177 static int psci_0_1_cpu_suspend(u32 state, unsigned long entry_point)
0178 {
0179     return __psci_cpu_suspend(psci_0_1_function_ids.cpu_suspend,
0180                   state, entry_point);
0181 }
0182 
0183 static int psci_0_2_cpu_suspend(u32 state, unsigned long entry_point)
0184 {
0185     return __psci_cpu_suspend(PSCI_FN_NATIVE(0_2, CPU_SUSPEND),
0186                   state, entry_point);
0187 }
0188 
0189 static int __psci_cpu_off(u32 fn, u32 state)
0190 {
0191     int err;
0192 
0193     err = invoke_psci_fn(fn, state, 0, 0);
0194     return psci_to_linux_errno(err);
0195 }
0196 
0197 static int psci_0_1_cpu_off(u32 state)
0198 {
0199     return __psci_cpu_off(psci_0_1_function_ids.cpu_off, state);
0200 }
0201 
0202 static int psci_0_2_cpu_off(u32 state)
0203 {
0204     return __psci_cpu_off(PSCI_0_2_FN_CPU_OFF, state);
0205 }
0206 
0207 static int __psci_cpu_on(u32 fn, unsigned long cpuid, unsigned long entry_point)
0208 {
0209     int err;
0210 
0211     err = invoke_psci_fn(fn, cpuid, entry_point, 0);
0212     return psci_to_linux_errno(err);
0213 }
0214 
0215 static int psci_0_1_cpu_on(unsigned long cpuid, unsigned long entry_point)
0216 {
0217     return __psci_cpu_on(psci_0_1_function_ids.cpu_on, cpuid, entry_point);
0218 }
0219 
0220 static int psci_0_2_cpu_on(unsigned long cpuid, unsigned long entry_point)
0221 {
0222     return __psci_cpu_on(PSCI_FN_NATIVE(0_2, CPU_ON), cpuid, entry_point);
0223 }
0224 
0225 static int __psci_migrate(u32 fn, unsigned long cpuid)
0226 {
0227     int err;
0228 
0229     err = invoke_psci_fn(fn, cpuid, 0, 0);
0230     return psci_to_linux_errno(err);
0231 }
0232 
0233 static int psci_0_1_migrate(unsigned long cpuid)
0234 {
0235     return __psci_migrate(psci_0_1_function_ids.migrate, cpuid);
0236 }
0237 
0238 static int psci_0_2_migrate(unsigned long cpuid)
0239 {
0240     return __psci_migrate(PSCI_FN_NATIVE(0_2, MIGRATE), cpuid);
0241 }
0242 
0243 static int psci_affinity_info(unsigned long target_affinity,
0244         unsigned long lowest_affinity_level)
0245 {
0246     return invoke_psci_fn(PSCI_FN_NATIVE(0_2, AFFINITY_INFO),
0247                   target_affinity, lowest_affinity_level, 0);
0248 }
0249 
0250 static int psci_migrate_info_type(void)
0251 {
0252     return invoke_psci_fn(PSCI_0_2_FN_MIGRATE_INFO_TYPE, 0, 0, 0);
0253 }
0254 
0255 static unsigned long psci_migrate_info_up_cpu(void)
0256 {
0257     return invoke_psci_fn(PSCI_FN_NATIVE(0_2, MIGRATE_INFO_UP_CPU),
0258                   0, 0, 0);
0259 }
0260 
0261 static void set_conduit(enum arm_smccc_conduit conduit)
0262 {
0263     switch (conduit) {
0264     case SMCCC_CONDUIT_HVC:
0265         invoke_psci_fn = __invoke_psci_fn_hvc;
0266         break;
0267     case SMCCC_CONDUIT_SMC:
0268         invoke_psci_fn = __invoke_psci_fn_smc;
0269         break;
0270     default:
0271         WARN(1, "Unexpected PSCI conduit %d\n", conduit);
0272     }
0273 
0274     psci_conduit = conduit;
0275 }
0276 
0277 static int get_set_conduit_method(struct device_node *np)
0278 {
0279     const char *method;
0280 
0281     pr_info("probing for conduit method from DT.\n");
0282 
0283     if (of_property_read_string(np, "method", &method)) {
0284         pr_warn("missing \"method\" property\n");
0285         return -ENXIO;
0286     }
0287 
0288     if (!strcmp("hvc", method)) {
0289         set_conduit(SMCCC_CONDUIT_HVC);
0290     } else if (!strcmp("smc", method)) {
0291         set_conduit(SMCCC_CONDUIT_SMC);
0292     } else {
0293         pr_warn("invalid \"method\" property: %s\n", method);
0294         return -EINVAL;
0295     }
0296     return 0;
0297 }
0298 
0299 static int psci_sys_reset(struct notifier_block *nb, unsigned long action,
0300               void *data)
0301 {
0302     if ((reboot_mode == REBOOT_WARM || reboot_mode == REBOOT_SOFT) &&
0303         psci_system_reset2_supported) {
0304         /*
0305          * reset_type[31] = 0 (architectural)
0306          * reset_type[30:0] = 0 (SYSTEM_WARM_RESET)
0307          * cookie = 0 (ignored by the implementation)
0308          */
0309         invoke_psci_fn(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2), 0, 0, 0);
0310     } else {
0311         invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
0312     }
0313 
0314     return NOTIFY_DONE;
0315 }
0316 
0317 static struct notifier_block psci_sys_reset_nb = {
0318     .notifier_call = psci_sys_reset,
0319     .priority = 129,
0320 };
0321 
0322 static void psci_sys_poweroff(void)
0323 {
0324     invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
0325 }
0326 
0327 static int __init psci_features(u32 psci_func_id)
0328 {
0329     return invoke_psci_fn(PSCI_1_0_FN_PSCI_FEATURES,
0330                   psci_func_id, 0, 0);
0331 }
0332 
0333 #ifdef CONFIG_CPU_IDLE
0334 static int psci_suspend_finisher(unsigned long state)
0335 {
0336     u32 power_state = state;
0337     phys_addr_t pa_cpu_resume = __pa_symbol(function_nocfi(cpu_resume));
0338 
0339     return psci_ops.cpu_suspend(power_state, pa_cpu_resume);
0340 }
0341 
0342 int psci_cpu_suspend_enter(u32 state)
0343 {
0344     int ret;
0345 
0346     if (!psci_power_state_loses_context(state)) {
0347         struct arm_cpuidle_irq_context context;
0348 
0349         arm_cpuidle_save_irq_context(&context);
0350         ret = psci_ops.cpu_suspend(state, 0);
0351         arm_cpuidle_restore_irq_context(&context);
0352     } else {
0353         ret = cpu_suspend(state, psci_suspend_finisher);
0354     }
0355 
0356     return ret;
0357 }
0358 #endif
0359 
0360 static int psci_system_suspend(unsigned long unused)
0361 {
0362     phys_addr_t pa_cpu_resume = __pa_symbol(function_nocfi(cpu_resume));
0363 
0364     return invoke_psci_fn(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND),
0365                   pa_cpu_resume, 0, 0);
0366 }
0367 
0368 static int psci_system_suspend_enter(suspend_state_t state)
0369 {
0370     return cpu_suspend(0, psci_system_suspend);
0371 }
0372 
0373 static const struct platform_suspend_ops psci_suspend_ops = {
0374     .valid          = suspend_valid_only_mem,
0375     .enter          = psci_system_suspend_enter,
0376 };
0377 
0378 static void __init psci_init_system_reset2(void)
0379 {
0380     int ret;
0381 
0382     ret = psci_features(PSCI_FN_NATIVE(1_1, SYSTEM_RESET2));
0383 
0384     if (ret != PSCI_RET_NOT_SUPPORTED)
0385         psci_system_reset2_supported = true;
0386 }
0387 
0388 static void __init psci_init_system_suspend(void)
0389 {
0390     int ret;
0391 
0392     if (!IS_ENABLED(CONFIG_SUSPEND))
0393         return;
0394 
0395     ret = psci_features(PSCI_FN_NATIVE(1_0, SYSTEM_SUSPEND));
0396 
0397     if (ret != PSCI_RET_NOT_SUPPORTED)
0398         suspend_set_ops(&psci_suspend_ops);
0399 }
0400 
0401 static void __init psci_init_cpu_suspend(void)
0402 {
0403     int feature = psci_features(PSCI_FN_NATIVE(0_2, CPU_SUSPEND));
0404 
0405     if (feature != PSCI_RET_NOT_SUPPORTED)
0406         psci_cpu_suspend_feature = feature;
0407 }
0408 
0409 /*
0410  * Detect the presence of a resident Trusted OS which may cause CPU_OFF to
0411  * return DENIED (which would be fatal).
0412  */
0413 static void __init psci_init_migrate(void)
0414 {
0415     unsigned long cpuid;
0416     int type, cpu = -1;
0417 
0418     type = psci_ops.migrate_info_type();
0419 
0420     if (type == PSCI_0_2_TOS_MP) {
0421         pr_info("Trusted OS migration not required\n");
0422         return;
0423     }
0424 
0425     if (type == PSCI_RET_NOT_SUPPORTED) {
0426         pr_info("MIGRATE_INFO_TYPE not supported.\n");
0427         return;
0428     }
0429 
0430     if (type != PSCI_0_2_TOS_UP_MIGRATE &&
0431         type != PSCI_0_2_TOS_UP_NO_MIGRATE) {
0432         pr_err("MIGRATE_INFO_TYPE returned unknown type (%d)\n", type);
0433         return;
0434     }
0435 
0436     cpuid = psci_migrate_info_up_cpu();
0437     if (cpuid & ~MPIDR_HWID_BITMASK) {
0438         pr_warn("MIGRATE_INFO_UP_CPU reported invalid physical ID (0x%lx)\n",
0439             cpuid);
0440         return;
0441     }
0442 
0443     cpu = get_logical_index(cpuid);
0444     resident_cpu = cpu >= 0 ? cpu : -1;
0445 
0446     pr_info("Trusted OS resident on physical CPU 0x%lx\n", cpuid);
0447 }
0448 
0449 static void __init psci_init_smccc(void)
0450 {
0451     u32 ver = ARM_SMCCC_VERSION_1_0;
0452     int feature;
0453 
0454     feature = psci_features(ARM_SMCCC_VERSION_FUNC_ID);
0455 
0456     if (feature != PSCI_RET_NOT_SUPPORTED) {
0457         u32 ret;
0458         ret = invoke_psci_fn(ARM_SMCCC_VERSION_FUNC_ID, 0, 0, 0);
0459         if (ret >= ARM_SMCCC_VERSION_1_1) {
0460             arm_smccc_version_init(ret, psci_conduit);
0461             ver = ret;
0462         }
0463     }
0464 
0465     /*
0466      * Conveniently, the SMCCC and PSCI versions are encoded the
0467      * same way. No, this isn't accidental.
0468      */
0469     pr_info("SMC Calling Convention v%d.%d\n",
0470         PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver));
0471 
0472 }
0473 
0474 static void __init psci_0_2_set_functions(void)
0475 {
0476     pr_info("Using standard PSCI v0.2 function IDs\n");
0477 
0478     psci_ops = (struct psci_operations){
0479         .get_version = psci_0_2_get_version,
0480         .cpu_suspend = psci_0_2_cpu_suspend,
0481         .cpu_off = psci_0_2_cpu_off,
0482         .cpu_on = psci_0_2_cpu_on,
0483         .migrate = psci_0_2_migrate,
0484         .affinity_info = psci_affinity_info,
0485         .migrate_info_type = psci_migrate_info_type,
0486     };
0487 
0488     register_restart_handler(&psci_sys_reset_nb);
0489 
0490     pm_power_off = psci_sys_poweroff;
0491 }
0492 
0493 /*
0494  * Probe function for PSCI firmware versions >= 0.2
0495  */
0496 static int __init psci_probe(void)
0497 {
0498     u32 ver = psci_0_2_get_version();
0499 
0500     pr_info("PSCIv%d.%d detected in firmware.\n",
0501             PSCI_VERSION_MAJOR(ver),
0502             PSCI_VERSION_MINOR(ver));
0503 
0504     if (PSCI_VERSION_MAJOR(ver) == 0 && PSCI_VERSION_MINOR(ver) < 2) {
0505         pr_err("Conflicting PSCI version detected.\n");
0506         return -EINVAL;
0507     }
0508 
0509     psci_0_2_set_functions();
0510 
0511     psci_init_migrate();
0512 
0513     if (PSCI_VERSION_MAJOR(ver) >= 1) {
0514         psci_init_smccc();
0515         psci_init_cpu_suspend();
0516         psci_init_system_suspend();
0517         psci_init_system_reset2();
0518         kvm_init_hyp_services();
0519     }
0520 
0521     return 0;
0522 }
0523 
0524 typedef int (*psci_initcall_t)(const struct device_node *);
0525 
0526 /*
0527  * PSCI init function for PSCI versions >=0.2
0528  *
0529  * Probe based on PSCI PSCI_VERSION function
0530  */
0531 static int __init psci_0_2_init(struct device_node *np)
0532 {
0533     int err;
0534 
0535     err = get_set_conduit_method(np);
0536     if (err)
0537         return err;
0538 
0539     /*
0540      * Starting with v0.2, the PSCI specification introduced a call
0541      * (PSCI_VERSION) that allows probing the firmware version, so
0542      * that PSCI function IDs and version specific initialization
0543      * can be carried out according to the specific version reported
0544      * by firmware
0545      */
0546     return psci_probe();
0547 }
0548 
0549 /*
0550  * PSCI < v0.2 get PSCI Function IDs via DT.
0551  */
0552 static int __init psci_0_1_init(struct device_node *np)
0553 {
0554     u32 id;
0555     int err;
0556 
0557     err = get_set_conduit_method(np);
0558     if (err)
0559         return err;
0560 
0561     pr_info("Using PSCI v0.1 Function IDs from DT\n");
0562 
0563     psci_ops.get_version = psci_0_1_get_version;
0564 
0565     if (!of_property_read_u32(np, "cpu_suspend", &id)) {
0566         psci_0_1_function_ids.cpu_suspend = id;
0567         psci_ops.cpu_suspend = psci_0_1_cpu_suspend;
0568     }
0569 
0570     if (!of_property_read_u32(np, "cpu_off", &id)) {
0571         psci_0_1_function_ids.cpu_off = id;
0572         psci_ops.cpu_off = psci_0_1_cpu_off;
0573     }
0574 
0575     if (!of_property_read_u32(np, "cpu_on", &id)) {
0576         psci_0_1_function_ids.cpu_on = id;
0577         psci_ops.cpu_on = psci_0_1_cpu_on;
0578     }
0579 
0580     if (!of_property_read_u32(np, "migrate", &id)) {
0581         psci_0_1_function_ids.migrate = id;
0582         psci_ops.migrate = psci_0_1_migrate;
0583     }
0584 
0585     return 0;
0586 }
0587 
0588 static int __init psci_1_0_init(struct device_node *np)
0589 {
0590     int err;
0591 
0592     err = psci_0_2_init(np);
0593     if (err)
0594         return err;
0595 
0596     if (psci_has_osi_support()) {
0597         pr_info("OSI mode supported.\n");
0598 
0599         /* Default to PC mode. */
0600         psci_set_osi_mode(false);
0601     }
0602 
0603     return 0;
0604 }
0605 
0606 static const struct of_device_id psci_of_match[] __initconst = {
0607     { .compatible = "arm,psci", .data = psci_0_1_init},
0608     { .compatible = "arm,psci-0.2", .data = psci_0_2_init},
0609     { .compatible = "arm,psci-1.0", .data = psci_1_0_init},
0610     {},
0611 };
0612 
0613 int __init psci_dt_init(void)
0614 {
0615     struct device_node *np;
0616     const struct of_device_id *matched_np;
0617     psci_initcall_t init_fn;
0618     int ret;
0619 
0620     np = of_find_matching_node_and_match(NULL, psci_of_match, &matched_np);
0621 
0622     if (!np || !of_device_is_available(np))
0623         return -ENODEV;
0624 
0625     init_fn = (psci_initcall_t)matched_np->data;
0626     ret = init_fn(np);
0627 
0628     of_node_put(np);
0629     return ret;
0630 }
0631 
0632 #ifdef CONFIG_ACPI
0633 /*
0634  * We use PSCI 0.2+ when ACPI is deployed on ARM64 and it's
0635  * explicitly clarified in SBBR
0636  */
0637 int __init psci_acpi_init(void)
0638 {
0639     if (!acpi_psci_present()) {
0640         pr_info("is not implemented in ACPI.\n");
0641         return -EOPNOTSUPP;
0642     }
0643 
0644     pr_info("probing for conduit method from ACPI.\n");
0645 
0646     if (acpi_psci_use_hvc())
0647         set_conduit(SMCCC_CONDUIT_HVC);
0648     else
0649         set_conduit(SMCCC_CONDUIT_SMC);
0650 
0651     return psci_probe();
0652 }
0653 #endif