Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2016 ARM Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/cpu_pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <uapi/linux/sched/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/preempt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/topology.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <asm/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <uapi/linux/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define NUM_SUSPEND_CYCLE (10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static unsigned int nb_available_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static int tos_resident_cpu = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static atomic_t nb_active_threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static struct completion suspend_threads_started =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	COMPLETION_INITIALIZER(suspend_threads_started);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static struct completion suspend_threads_done =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	COMPLETION_INITIALIZER(suspend_threads_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * We assume that PSCI operations are used if they are available. This is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * necessarily true on arm64, since the decision is based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * "enable-method" property of each CPU in the DT, but given that there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * arch-specific way to check this, we assume that the DT is sensible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static int psci_ops_check(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int migrate_type = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (!(psci_ops.cpu_off && psci_ops.cpu_on && psci_ops.cpu_suspend)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		pr_warn("Missing PSCI operations, aborting tests\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (psci_ops.migrate_info_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		migrate_type = psci_ops.migrate_info_type();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (migrate_type == PSCI_0_2_TOS_UP_MIGRATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	    migrate_type == PSCI_0_2_TOS_UP_NO_MIGRATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		/* There is a UP Trusted OS, find on which core it resides. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		for_each_online_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			if (psci_tos_resident_on(cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				tos_resident_cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		if (tos_resident_cpu == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			pr_warn("UP Trusted OS resides on no online CPU\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * offlined_cpus is a temporary array but passing it as an argument avoids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * multiple allocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static unsigned int down_and_up_cpus(const struct cpumask *cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 				     struct cpumask *offlined_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	cpumask_clear(offlined_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/* Try to power down all CPUs in the mask. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	for_each_cpu(cpu, cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		int ret = remove_cpu(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		 * cpu_down() checks the number of online CPUs before the TOS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		 * resident CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (cpumask_weight(offlined_cpus) + 1 == nb_available_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			if (ret != -EBUSY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				pr_err("Unexpected return code %d while trying "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 				       "to power down last online CPU %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 				       ret, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 				++err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		} else if (cpu == tos_resident_cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			if (ret != -EPERM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				pr_err("Unexpected return code %d while trying "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 				       "to power down TOS resident CPU %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 				       ret, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				++err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		} else if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			pr_err("Error occurred (%d) while trying "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			       "to power down CPU %d\n", ret, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			++err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			cpumask_set_cpu(cpu, offlined_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	/* Try to power up all the CPUs that have been offlined. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	for_each_cpu(cpu, offlined_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		int ret = add_cpu(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			pr_err("Error occurred (%d) while trying "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			       "to power up CPU %d\n", ret, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			++err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			cpumask_clear_cpu(cpu, offlined_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 * Something went bad at some point and some CPUs could not be turned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 * back on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	WARN_ON(!cpumask_empty(offlined_cpus) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		num_online_cpus() != nb_available_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static void free_cpu_groups(int num, cpumask_var_t **pcpu_groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	cpumask_var_t *cpu_groups = *pcpu_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	for (i = 0; i < num; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		free_cpumask_var(cpu_groups[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	kfree(cpu_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int alloc_init_cpu_groups(cpumask_var_t **pcpu_groups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	int num_groups = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	cpumask_var_t tmp, *cpu_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (!alloc_cpumask_var(&tmp, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	cpu_groups = kcalloc(nb_available_cpus, sizeof(*cpu_groups),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			     GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (!cpu_groups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		free_cpumask_var(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	cpumask_copy(tmp, cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	while (!cpumask_empty(tmp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		const struct cpumask *cpu_group =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			topology_core_cpumask(cpumask_any(tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		if (!alloc_cpumask_var(&cpu_groups[num_groups], GFP_KERNEL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			free_cpumask_var(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			free_cpu_groups(num_groups, &cpu_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		cpumask_copy(cpu_groups[num_groups++], cpu_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		cpumask_andnot(tmp, tmp, cpu_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	free_cpumask_var(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	*pcpu_groups = cpu_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	return num_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int hotplug_tests(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int i, nb_cpu_group, err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	cpumask_var_t offlined_cpus, *cpu_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	char *page_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (!alloc_cpumask_var(&offlined_cpus, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	nb_cpu_group = alloc_init_cpu_groups(&cpu_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (nb_cpu_group < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		goto out_free_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	page_buf = (char *)__get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (!page_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		goto out_free_cpu_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 * Of course the last CPU cannot be powered down and cpu_down() should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 * refuse doing that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	pr_info("Trying to turn off and on again all CPUs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	err = down_and_up_cpus(cpu_online_mask, offlined_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * Take down CPUs by cpu group this time. When the last CPU is turned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * off, the cpu group itself should shut down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	for (i = 0; i < nb_cpu_group; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		ssize_t len = cpumap_print_to_pagebuf(true, page_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 						      cpu_groups[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		/* Remove trailing newline. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		page_buf[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		pr_info("Trying to turn off and on again group %d (CPUs %s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			i, page_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		err += down_and_up_cpus(cpu_groups[i], offlined_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	free_page((unsigned long)page_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) out_free_cpu_groups:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	free_cpu_groups(nb_cpu_group, &cpu_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) out_free_cpus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	free_cpumask_var(offlined_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static void dummy_callback(struct timer_list *unused) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int suspend_cpu(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		       struct cpuidle_driver *drv, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct cpuidle_state *state = &drv->states[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	bool broadcast = state->flags & CPUIDLE_FLAG_TIMER_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	arch_cpu_idle_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (broadcast) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		 * The local timer will be shut down, we need to enter tick
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		 * broadcast.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		ret = tick_broadcast_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			 * In the absence of hardware broadcast mechanism,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			 * this CPU might be used to broadcast wakeups, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			 * may be why entering tick broadcast has failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			 * There is little the kernel can do to work around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			 * that, so enter WFI instead (idle state 0).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			cpu_do_idle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			goto out_arch_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	ret = state->enter(dev, drv, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (broadcast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		tick_broadcast_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) out_arch_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	arch_cpu_idle_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int suspend_test_thread(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	int cpu = (long)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	int i, nb_suspend = 0, nb_shallow_sleep = 0, nb_err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct cpuidle_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct cpuidle_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	/* No need for an actual callback, we just want to wake up the CPU. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	struct timer_list wakeup_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	/* Wait for the main thread to give the start signal. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	wait_for_completion(&suspend_threads_started);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	/* Set maximum priority to preempt all other threads on this CPU. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	sched_set_fifo(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	dev = this_cpu_read(cpuidle_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	drv = cpuidle_get_cpu_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	pr_info("CPU %d entering suspend cycles, states 1 through %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		cpu, drv->state_count - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	timer_setup_on_stack(&wakeup_timer, dummy_callback, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	for (i = 0; i < NUM_SUSPEND_CYCLE; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		 * Test all possible states, except 0 (which is usually WFI and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		 * doesn't use PSCI).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		for (index = 1; index < drv->state_count; ++index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			struct cpuidle_state *state = &drv->states[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			 * Set the timer to wake this CPU up in some time (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			 * should be largely sufficient for entering suspend).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			 * If the local tick is disabled when entering suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			 * suspend_cpu() takes care of switching to a broadcast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			 * tick, so the timer will still wake us up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			mod_timer(&wakeup_timer, jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 				  usecs_to_jiffies(state->target_residency));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			/* IRQs must be disabled during suspend operations. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			ret = suspend_cpu(dev, drv, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			 * We have woken up. Re-enable IRQs to handle any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			 * pending interrupt, do not wait until the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			 * loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			if (ret == index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 				++nb_suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			} else if (ret >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				/* We did not enter the expected state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				++nb_shallow_sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 				pr_err("Failed to suspend CPU %d: error %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 				       "(requested state %d, cycle %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 				       cpu, ret, index, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 				++nb_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	 * Disable the timer to make sure that the timer will not trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	 * later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	del_timer(&wakeup_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	destroy_timer_on_stack(&wakeup_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (atomic_dec_return_relaxed(&nb_active_threads) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		complete(&suspend_threads_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		/* Needs to be set first to avoid missing a wakeup. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		if (kthread_should_park())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	pr_info("CPU %d suspend test results: success %d, shallow states %d, errors %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		cpu, nb_suspend, nb_shallow_sleep, nb_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	kthread_parkme();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	return nb_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static int suspend_tests(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	int i, cpu, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	struct task_struct **threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	int nb_threads = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	threads = kmalloc_array(nb_available_cpus, sizeof(*threads),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (!threads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	 * Stop cpuidle to prevent the idle tasks from entering a deep sleep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	 * mode, as it might interfere with the suspend threads on other CPUs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	 * This does not prevent the suspend threads from using cpuidle (only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	 * the idle tasks check this status). Take the idle lock so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	 * the cpuidle driver and device look-up can be carried out safely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	cpuidle_pause_and_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		struct task_struct *thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		/* Check that cpuidle is available on that CPU. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (!dev || !drv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			pr_warn("cpuidle not available on CPU %d, ignoring\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 				cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		thread = kthread_create_on_cpu(suspend_test_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 					       (void *)(long)cpu, cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 					       "psci_suspend_test");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		if (IS_ERR(thread))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			pr_err("Failed to create kthread on CPU %d\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			threads[nb_threads++] = thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (nb_threads < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	atomic_set(&nb_active_threads, nb_threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	 * Wake up the suspend threads. To avoid the main thread being preempted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	 * before all the threads have been unparked, the suspend threads will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	 * wait for the completion of suspend_threads_started.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	for (i = 0; i < nb_threads; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		wake_up_process(threads[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	complete_all(&suspend_threads_started);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	wait_for_completion(&suspend_threads_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	/* Stop and destroy all threads, get return status. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	for (i = 0; i < nb_threads; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		err += kthread_park(threads[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		err += kthread_stop(threads[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	cpuidle_resume_and_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	kfree(threads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static int __init psci_checker(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	 * Since we're in an initcall, we assume that all the CPUs that all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	 * CPUs that can be onlined have been onlined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	 * The tests assume that hotplug is enabled but nobody else is using it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	 * otherwise the results will be unpredictable. However, since there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	 * is no userspace yet in initcalls, that should be fine, as long as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	 * no torture test is running at the same time (see Kconfig).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	nb_available_cpus = num_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	/* Check PSCI operations are set up and working. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	ret = psci_ops_check();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	pr_info("PSCI checker started using %u CPUs\n", nb_available_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	pr_info("Starting hotplug tests\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	ret = hotplug_tests();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		pr_info("Hotplug tests passed OK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		pr_err("%d error(s) encountered in hotplug tests\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		pr_err("Out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	pr_info("Starting suspend tests (%d cycles per state)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		NUM_SUSPEND_CYCLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	ret = suspend_tests();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		pr_info("Suspend tests passed OK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	else if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		pr_err("%d error(s) encountered in suspend tests\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		case -ENOMEM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			pr_err("Out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		case -ENODEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			pr_warn("Could not start suspend tests on any CPU\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	pr_info("PSCI checker completed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	return ret < 0 ? ret : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) late_initcall(psci_checker);