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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  cpuidle-powernv - idle state cpuidle driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *  Adapted from drivers/cpuidle/cpuidle-pseries
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/moduleparam.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.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/clockchips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/machdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/opal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/runlatch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <asm/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * Expose only those Hardware idle states via the cpuidle framework
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define POWERNV_THRESHOLD_LATENCY_NS 200000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static struct cpuidle_driver powernv_idle_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	.name             = "powernv_idle",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	.owner            = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static int max_idle_state __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static struct cpuidle_state *cpuidle_state_table __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) struct stop_psscr_table {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	u64 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX] __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static u64 default_snooze_timeout __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static bool snooze_timeout_en __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static u64 get_snooze_timeout(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			      struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			      int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (unlikely(!snooze_timeout_en))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return default_snooze_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	for (i = index + 1; i < drv->state_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (dev->states_usage[i].disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return drv->states[i].target_residency * tb_ticks_per_usec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	return default_snooze_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static int snooze_loop(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	u64 snooze_exit_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	set_thread_flag(TIF_POLLING_NRFLAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	snooze_exit_time = get_tb() + get_snooze_timeout(dev, drv, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	ppc64_runlatch_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	HMT_very_low();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	while (!need_resched()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			 * Task has not woken up but we are exiting the polling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			 * loop anyway. Require a barrier after polling is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			 * cleared to order subsequent test of need_resched().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			clear_thread_flag(TIF_POLLING_NRFLAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			smp_mb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	HMT_medium();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	ppc64_runlatch_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	clear_thread_flag(TIF_POLLING_NRFLAG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int nap_loop(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	power7_idle_type(PNV_THREAD_NAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* Register for fastsleep only in oneshot mode of broadcast */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #ifdef CONFIG_TICK_ONESHOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static int fastsleep_loop(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 				struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	unsigned long old_lpcr = mfspr(SPRN_LPCR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	unsigned long new_lpcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (unlikely(system_state < SYSTEM_RUNNING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	new_lpcr = old_lpcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	/* Do not exit powersave upon decrementer as we've setup the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	 * offload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	new_lpcr &= ~LPCR_PECE1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	mtspr(SPRN_LPCR, new_lpcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	power7_idle_type(PNV_THREAD_SLEEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	mtspr(SPRN_LPCR, old_lpcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static int stop_loop(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		     struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		     int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	arch300_idle_type(stop_psscr_table[index].val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			 stop_psscr_table[index].mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^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)  * States for dedicated partition case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	{ /* Snooze */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		.name = "snooze",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		.desc = "snooze",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		.exit_latency = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		.target_residency = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		.enter = snooze_loop },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int powernv_cpuidle_cpu_online(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	if (dev && cpuidle_get_driver()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		cpuidle_pause_and_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		cpuidle_enable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		cpuidle_resume_and_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static int powernv_cpuidle_cpu_dead(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (dev && cpuidle_get_driver()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		cpuidle_pause_and_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		cpuidle_disable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		cpuidle_resume_and_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^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)  * powernv_cpuidle_driver_init()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static int powernv_cpuidle_driver_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	int idle_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct cpuidle_driver *drv = &powernv_idle_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	drv->state_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		/* Is the state not enabled? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if (cpuidle_state_table[idle_state].enter == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		drv->states[drv->state_count] =	/* structure copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 			cpuidle_state_table[idle_state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		drv->state_count += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 * On the PowerNV platform cpu_present may be less than cpu_possible in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * cases when firmware detects the CPU, but it is not available to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * OS.  If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * run time and hence cpu_devices are not created for those CPUs by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * generic topology_init().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	 * drv->cpumask defaults to cpu_possible_mask in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	 * __cpuidle_driver_init().  This breaks cpuidle on PowerNV where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	 * cpu_devices are not created for CPUs in cpu_possible_mask that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	 * cannot be hot-added later at run time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	 * Trying cpuidle_register_device() on a CPU without a cpu_device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	drv->cpumask = (struct cpumask *)cpu_present_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static inline void add_powernv_state(int index, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 				     unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				     int (*idle_fn)(struct cpuidle_device *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 						    struct cpuidle_driver *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 						    int),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				     unsigned int target_residency,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 				     unsigned int exit_latency,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				     u64 psscr_val, u64 psscr_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	strlcpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	strlcpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	powernv_states[index].flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	powernv_states[index].target_residency = target_residency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	powernv_states[index].exit_latency = exit_latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	powernv_states[index].enter = idle_fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	/* For power8 and below psscr_* will be 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	stop_psscr_table[index].val = psscr_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	stop_psscr_table[index].mask = psscr_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) extern u32 pnv_get_supported_cpuidle_states(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static int powernv_add_idle_states(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	int nr_idle_states = 1; /* Snooze */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	int dt_idle_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	u32 has_stop_states = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	u32 supported_flags = pnv_get_supported_cpuidle_states();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	/* Currently we have snooze statically defined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (nr_pnv_idle_states <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		pr_warn("cpuidle-powernv : Only Snooze is available\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	/* TODO: Count only states which are eligible for cpuidle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	dt_idle_states = nr_pnv_idle_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	 * Since snooze is used as first idle state, max idle states allowed is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 * CPUIDLE_STATE_MAX -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (nr_pnv_idle_states > CPUIDLE_STATE_MAX - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		pr_warn("cpuidle-powernv: discovered idle states more than allowed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		dt_idle_states = CPUIDLE_STATE_MAX - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	 * If the idle states use stop instruction, probe for psscr values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	 * and psscr mask which are necessary to specify required stop level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	has_stop_states = (pnv_idle_states[0].flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			   (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	for (i = 0; i < dt_idle_states; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		unsigned int exit_latency, target_residency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		bool stops_timebase = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		struct pnv_idle_states_t *state = &pnv_idle_states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		 * Skip the platform idle state whose flag isn't in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		 * the supported_cpuidle_states flag mask.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		if ((state->flags & supported_flags) != state->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		 * If an idle state has exit latency beyond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		 * in cpu-idle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		if (state->latency_ns > POWERNV_THRESHOLD_LATENCY_NS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		 * Firmware passes residency and latency values in ns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		 * cpuidle expects it in us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		exit_latency = DIV_ROUND_UP(state->latency_ns, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		target_residency = DIV_ROUND_UP(state->residency_ns, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		if (has_stop_states && !(state->valid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		if (state->flags & OPAL_PM_TIMEBASE_STOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			stops_timebase = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		if (state->flags & OPAL_PM_NAP_ENABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			/* Add NAP state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			add_powernv_state(nr_idle_states, "Nap",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 					  CPUIDLE_FLAG_NONE, nap_loop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 					  target_residency, exit_latency, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		} else if (has_stop_states && !stops_timebase) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			add_powernv_state(nr_idle_states, state->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 					  CPUIDLE_FLAG_NONE, stop_loop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 					  target_residency, exit_latency,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 					  state->psscr_val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 					  state->psscr_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		 * within this config dependency check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) #ifdef CONFIG_TICK_ONESHOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		else if (state->flags & OPAL_PM_SLEEP_ENABLED ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			 state->flags & OPAL_PM_SLEEP_ENABLED_ER1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			/* Add FASTSLEEP state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			add_powernv_state(nr_idle_states, "FastSleep",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 					  CPUIDLE_FLAG_TIMER_STOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 					  fastsleep_loop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 					  target_residency, exit_latency, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		} else if (has_stop_states && stops_timebase) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			add_powernv_state(nr_idle_states, state->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 					  CPUIDLE_FLAG_TIMER_STOP, stop_loop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 					  target_residency, exit_latency,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 					  state->psscr_val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 					  state->psscr_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		nr_idle_states++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	return nr_idle_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)  * powernv_idle_probe()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)  * Choose state table for shared versus dedicated partition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static int powernv_idle_probe(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	if (cpuidle_disable != IDLE_NO_OVERRIDE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (firmware_has_feature(FW_FEATURE_OPAL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		cpuidle_state_table = powernv_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		/* Device tree can indicate more idle states */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		max_idle_state = powernv_add_idle_states();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		default_snooze_timeout = TICK_USEC * tb_ticks_per_usec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		if (max_idle_state > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			snooze_timeout_en = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static int __init powernv_processor_idle_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	retval = powernv_idle_probe();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	powernv_cpuidle_driver_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	retval = cpuidle_register(&powernv_idle_driver, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		printk(KERN_DEBUG "Registration of powernv driver failed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 					   "cpuidle/powernv:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 					   powernv_cpuidle_cpu_online, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	WARN_ON(retval < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 					   "cpuidle/powernv:dead", NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 					   powernv_cpuidle_cpu_dead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	WARN_ON(retval < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	printk(KERN_DEBUG "powernv_idle_driver registered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) device_initcall(powernv_processor_idle_init);