1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
22c2e6ecfSDeepthi Dharwar /*
32c2e6ecfSDeepthi Dharwar  *  cpuidle-powernv - idle state cpuidle driver.
42c2e6ecfSDeepthi Dharwar  *  Adapted from drivers/cpuidle/cpuidle-pseries
52c2e6ecfSDeepthi Dharwar  *
62c2e6ecfSDeepthi Dharwar  */
72c2e6ecfSDeepthi Dharwar 
82c2e6ecfSDeepthi Dharwar #include <linux/kernel.h>
92c2e6ecfSDeepthi Dharwar #include <linux/module.h>
102c2e6ecfSDeepthi Dharwar #include <linux/init.h>
112c2e6ecfSDeepthi Dharwar #include <linux/moduleparam.h>
122c2e6ecfSDeepthi Dharwar #include <linux/cpuidle.h>
132c2e6ecfSDeepthi Dharwar #include <linux/cpu.h>
142c2e6ecfSDeepthi Dharwar #include <linux/notifier.h>
150d948730SPreeti U Murthy #include <linux/clockchips.h>
160888839cSPreeti U Murthy #include <linux/of.h>
1792c83ff5SPreeti U Murthy #include <linux/slab.h>
182c2e6ecfSDeepthi Dharwar 
192c2e6ecfSDeepthi Dharwar #include <asm/machdep.h>
202c2e6ecfSDeepthi Dharwar #include <asm/firmware.h>
218eb8ac89SShreyas B. Prabhu #include <asm/opal.h>
22591ac0cbSNicolas Pitre #include <asm/runlatch.h>
2309206b60SGautham R. Shenoy #include <asm/cpuidle.h>
242c2e6ecfSDeepthi Dharwar 
259e9fc6f0SGautham R. Shenoy /*
269e9fc6f0SGautham R. Shenoy  * Expose only those Hardware idle states via the cpuidle framework
279e9fc6f0SGautham R. Shenoy  * that have latency value below POWERNV_THRESHOLD_LATENCY_NS.
289e9fc6f0SGautham R. Shenoy  */
293005c597SShreyas B. Prabhu #define POWERNV_THRESHOLD_LATENCY_NS 200000
303005c597SShreyas B. Prabhu 
31ed61390bSAndrew Donnellan static struct cpuidle_driver powernv_idle_driver = {
322c2e6ecfSDeepthi Dharwar 	.name             = "powernv_idle",
332c2e6ecfSDeepthi Dharwar 	.owner            = THIS_MODULE,
342c2e6ecfSDeepthi Dharwar };
352c2e6ecfSDeepthi Dharwar 
36624e46d0SNicholas Piggin static int max_idle_state __read_mostly;
37624e46d0SNicholas Piggin static struct cpuidle_state *cpuidle_state_table __read_mostly;
383005c597SShreyas B. Prabhu 
3909206b60SGautham R. Shenoy struct stop_psscr_table {
4009206b60SGautham R. Shenoy 	u64 val;
4109206b60SGautham R. Shenoy 	u64 mask;
4209206b60SGautham R. Shenoy };
4309206b60SGautham R. Shenoy 
44624e46d0SNicholas Piggin static struct stop_psscr_table stop_psscr_table[CPUIDLE_STATE_MAX] __read_mostly;
453005c597SShreyas B. Prabhu 
460a4ec6aaSGautham R. Shenoy static u64 default_snooze_timeout __read_mostly;
47624e46d0SNicholas Piggin static bool snooze_timeout_en __read_mostly;
482c2e6ecfSDeepthi Dharwar 
get_snooze_timeout(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)490a4ec6aaSGautham R. Shenoy static u64 get_snooze_timeout(struct cpuidle_device *dev,
500a4ec6aaSGautham R. Shenoy 			      struct cpuidle_driver *drv,
510a4ec6aaSGautham R. Shenoy 			      int index)
520a4ec6aaSGautham R. Shenoy {
530a4ec6aaSGautham R. Shenoy 	int i;
540a4ec6aaSGautham R. Shenoy 
550a4ec6aaSGautham R. Shenoy 	if (unlikely(!snooze_timeout_en))
560a4ec6aaSGautham R. Shenoy 		return default_snooze_timeout;
570a4ec6aaSGautham R. Shenoy 
580a4ec6aaSGautham R. Shenoy 	for (i = index + 1; i < drv->state_count; i++) {
5999e98d3fSRafael J. Wysocki 		if (dev->states_usage[i].disable)
600a4ec6aaSGautham R. Shenoy 			continue;
610a4ec6aaSGautham R. Shenoy 
6299e98d3fSRafael J. Wysocki 		return drv->states[i].target_residency * tb_ticks_per_usec;
630a4ec6aaSGautham R. Shenoy 	}
640a4ec6aaSGautham R. Shenoy 
650a4ec6aaSGautham R. Shenoy 	return default_snooze_timeout;
660a4ec6aaSGautham R. Shenoy }
670a4ec6aaSGautham R. Shenoy 
snooze_loop(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)682c2e6ecfSDeepthi Dharwar static int snooze_loop(struct cpuidle_device *dev,
692c2e6ecfSDeepthi Dharwar 			struct cpuidle_driver *drv,
702c2e6ecfSDeepthi Dharwar 			int index)
712c2e6ecfSDeepthi Dharwar {
7278eaa10fSShilpasri G Bhat 	u64 snooze_exit_time;
7378eaa10fSShilpasri G Bhat 
742c2e6ecfSDeepthi Dharwar 	set_thread_flag(TIF_POLLING_NRFLAG);
752c2e6ecfSDeepthi Dharwar 
763fc5ee92SNicholas Piggin 	local_irq_enable();
773fc5ee92SNicholas Piggin 
780a4ec6aaSGautham R. Shenoy 	snooze_exit_time = get_tb() + get_snooze_timeout(dev, drv, index);
79*5ddcc03aSAboorva Devarajan 	dev->poll_time_limit = false;
80591ac0cbSNicolas Pitre 	ppc64_runlatch_off();
812c2e6ecfSDeepthi Dharwar 	HMT_very_low();
8226eb48a9SAnton Blanchard 	while (!need_resched()) {
837ded4291SNicholas Piggin 		if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) {
847ded4291SNicholas Piggin 			/*
857ded4291SNicholas Piggin 			 * Task has not woken up but we are exiting the polling
867ded4291SNicholas Piggin 			 * loop anyway. Require a barrier after polling is
877ded4291SNicholas Piggin 			 * cleared to order subsequent test of need_resched().
887ded4291SNicholas Piggin 			 */
897ded4291SNicholas Piggin 			clear_thread_flag(TIF_POLLING_NRFLAG);
90*5ddcc03aSAboorva Devarajan 			dev->poll_time_limit = true;
917ded4291SNicholas Piggin 			smp_mb();
9278eaa10fSShilpasri G Bhat 			break;
932c2e6ecfSDeepthi Dharwar 		}
947ded4291SNicholas Piggin 	}
952c2e6ecfSDeepthi Dharwar 
962c2e6ecfSDeepthi Dharwar 	HMT_medium();
97591ac0cbSNicolas Pitre 	ppc64_runlatch_on();
982c2e6ecfSDeepthi Dharwar 	clear_thread_flag(TIF_POLLING_NRFLAG);
993fc5ee92SNicholas Piggin 
100f1343d04SNicholas Piggin 	local_irq_disable();
101f1343d04SNicholas Piggin 
1022c2e6ecfSDeepthi Dharwar 	return index;
1032c2e6ecfSDeepthi Dharwar }
1042c2e6ecfSDeepthi Dharwar 
nap_loop(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)1052c2e6ecfSDeepthi Dharwar static int nap_loop(struct cpuidle_device *dev,
1062c2e6ecfSDeepthi Dharwar 			struct cpuidle_driver *drv,
1072c2e6ecfSDeepthi Dharwar 			int index)
1082c2e6ecfSDeepthi Dharwar {
1092201f994SNicholas Piggin 	power7_idle_type(PNV_THREAD_NAP);
1102201f994SNicholas Piggin 
1112c2e6ecfSDeepthi Dharwar 	return index;
1122c2e6ecfSDeepthi Dharwar }
1132c2e6ecfSDeepthi Dharwar 
114cc5a2f7bSpreeti /* Register for fastsleep only in oneshot mode of broadcast */
115cc5a2f7bSpreeti #ifdef CONFIG_TICK_ONESHOT
fastsleep_loop(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)1160d948730SPreeti U Murthy static int fastsleep_loop(struct cpuidle_device *dev,
1170d948730SPreeti U Murthy 				struct cpuidle_driver *drv,
1180d948730SPreeti U Murthy 				int index)
1190d948730SPreeti U Murthy {
1200d948730SPreeti U Murthy 	unsigned long old_lpcr = mfspr(SPRN_LPCR);
1210d948730SPreeti U Murthy 	unsigned long new_lpcr;
1220d948730SPreeti U Murthy 
1230d948730SPreeti U Murthy 	if (unlikely(system_state < SYSTEM_RUNNING))
1240d948730SPreeti U Murthy 		return index;
1250d948730SPreeti U Murthy 
1260d948730SPreeti U Murthy 	new_lpcr = old_lpcr;
1279b6a68d9SMichael Neuling 	/* Do not exit powersave upon decrementer as we've setup the timer
1289b6a68d9SMichael Neuling 	 * offload.
1290d948730SPreeti U Murthy 	 */
1309b6a68d9SMichael Neuling 	new_lpcr &= ~LPCR_PECE1;
1310d948730SPreeti U Murthy 
1320d948730SPreeti U Murthy 	mtspr(SPRN_LPCR, new_lpcr);
1332201f994SNicholas Piggin 
1342201f994SNicholas Piggin 	power7_idle_type(PNV_THREAD_SLEEP);
1350d948730SPreeti U Murthy 
1360d948730SPreeti U Murthy 	mtspr(SPRN_LPCR, old_lpcr);
1370d948730SPreeti U Murthy 
1380d948730SPreeti U Murthy 	return index;
1390d948730SPreeti U Murthy }
140cc5a2f7bSpreeti #endif
1413005c597SShreyas B. Prabhu 
stop_loop(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)1423005c597SShreyas B. Prabhu static int stop_loop(struct cpuidle_device *dev,
1433005c597SShreyas B. Prabhu 		     struct cpuidle_driver *drv,
1443005c597SShreyas B. Prabhu 		     int index)
1453005c597SShreyas B. Prabhu {
146ffd2961bSNicholas Piggin 	arch300_idle_type(stop_psscr_table[index].val,
14709206b60SGautham R. Shenoy 			 stop_psscr_table[index].mask);
1483005c597SShreyas B. Prabhu 	return index;
1493005c597SShreyas B. Prabhu }
1503005c597SShreyas B. Prabhu 
1512c2e6ecfSDeepthi Dharwar /*
1522c2e6ecfSDeepthi Dharwar  * States for dedicated partition case.
1532c2e6ecfSDeepthi Dharwar  */
154169f3faeSShreyas B. Prabhu static struct cpuidle_state powernv_states[CPUIDLE_STATE_MAX] = {
1552c2e6ecfSDeepthi Dharwar 	{ /* Snooze */
1562c2e6ecfSDeepthi Dharwar 		.name = "snooze",
1572c2e6ecfSDeepthi Dharwar 		.desc = "snooze",
1582c2e6ecfSDeepthi Dharwar 		.exit_latency = 0,
1592c2e6ecfSDeepthi Dharwar 		.target_residency = 0,
160*5ddcc03aSAboorva Devarajan 		.enter = snooze_loop,
161*5ddcc03aSAboorva Devarajan 		.flags = CPUIDLE_FLAG_POLLING },
1622c2e6ecfSDeepthi Dharwar };
1632c2e6ecfSDeepthi Dharwar 
powernv_cpuidle_cpu_online(unsigned int cpu)16410fcca9dSSebastian Andrzej Siewior static int powernv_cpuidle_cpu_online(unsigned int cpu)
1652c2e6ecfSDeepthi Dharwar {
16610fcca9dSSebastian Andrzej Siewior 	struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
1672c2e6ecfSDeepthi Dharwar 
1682c2e6ecfSDeepthi Dharwar 	if (dev && cpuidle_get_driver()) {
1692c2e6ecfSDeepthi Dharwar 		cpuidle_pause_and_lock();
1702c2e6ecfSDeepthi Dharwar 		cpuidle_enable_device(dev);
1712c2e6ecfSDeepthi Dharwar 		cpuidle_resume_and_unlock();
17210fcca9dSSebastian Andrzej Siewior 	}
17310fcca9dSSebastian Andrzej Siewior 	return 0;
17410fcca9dSSebastian Andrzej Siewior }
1752c2e6ecfSDeepthi Dharwar 
powernv_cpuidle_cpu_dead(unsigned int cpu)17610fcca9dSSebastian Andrzej Siewior static int powernv_cpuidle_cpu_dead(unsigned int cpu)
17710fcca9dSSebastian Andrzej Siewior {
17810fcca9dSSebastian Andrzej Siewior 	struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
17910fcca9dSSebastian Andrzej Siewior 
18010fcca9dSSebastian Andrzej Siewior 	if (dev && cpuidle_get_driver()) {
1812c2e6ecfSDeepthi Dharwar 		cpuidle_pause_and_lock();
1822c2e6ecfSDeepthi Dharwar 		cpuidle_disable_device(dev);
1832c2e6ecfSDeepthi Dharwar 		cpuidle_resume_and_unlock();
1842c2e6ecfSDeepthi Dharwar 	}
18510fcca9dSSebastian Andrzej Siewior 	return 0;
1862c2e6ecfSDeepthi Dharwar }
1872c2e6ecfSDeepthi Dharwar 
1882c2e6ecfSDeepthi Dharwar /*
1892c2e6ecfSDeepthi Dharwar  * powernv_cpuidle_driver_init()
1902c2e6ecfSDeepthi Dharwar  */
powernv_cpuidle_driver_init(void)1912c2e6ecfSDeepthi Dharwar static int powernv_cpuidle_driver_init(void)
1922c2e6ecfSDeepthi Dharwar {
1932c2e6ecfSDeepthi Dharwar 	int idle_state;
1942c2e6ecfSDeepthi Dharwar 	struct cpuidle_driver *drv = &powernv_idle_driver;
1952c2e6ecfSDeepthi Dharwar 
1962c2e6ecfSDeepthi Dharwar 	drv->state_count = 0;
1972c2e6ecfSDeepthi Dharwar 
1982c2e6ecfSDeepthi Dharwar 	for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
1992c2e6ecfSDeepthi Dharwar 		/* Is the state not enabled? */
2002c2e6ecfSDeepthi Dharwar 		if (cpuidle_state_table[idle_state].enter == NULL)
2012c2e6ecfSDeepthi Dharwar 			continue;
2022c2e6ecfSDeepthi Dharwar 
2032c2e6ecfSDeepthi Dharwar 		drv->states[drv->state_count] =	/* structure copy */
2042c2e6ecfSDeepthi Dharwar 			cpuidle_state_table[idle_state];
2052c2e6ecfSDeepthi Dharwar 
2062c2e6ecfSDeepthi Dharwar 		drv->state_count += 1;
2072c2e6ecfSDeepthi Dharwar 	}
2082c2e6ecfSDeepthi Dharwar 
209293d264fSVaidyanathan Srinivasan 	/*
210293d264fSVaidyanathan Srinivasan 	 * On the PowerNV platform cpu_present may be less than cpu_possible in
211293d264fSVaidyanathan Srinivasan 	 * cases when firmware detects the CPU, but it is not available to the
212293d264fSVaidyanathan Srinivasan 	 * OS.  If CONFIG_HOTPLUG_CPU=n, then such CPUs are not hotplugable at
213293d264fSVaidyanathan Srinivasan 	 * run time and hence cpu_devices are not created for those CPUs by the
214293d264fSVaidyanathan Srinivasan 	 * generic topology_init().
215293d264fSVaidyanathan Srinivasan 	 *
216293d264fSVaidyanathan Srinivasan 	 * drv->cpumask defaults to cpu_possible_mask in
217293d264fSVaidyanathan Srinivasan 	 * __cpuidle_driver_init().  This breaks cpuidle on PowerNV where
218293d264fSVaidyanathan Srinivasan 	 * cpu_devices are not created for CPUs in cpu_possible_mask that
219293d264fSVaidyanathan Srinivasan 	 * cannot be hot-added later at run time.
220293d264fSVaidyanathan Srinivasan 	 *
221293d264fSVaidyanathan Srinivasan 	 * Trying cpuidle_register_device() on a CPU without a cpu_device is
222293d264fSVaidyanathan Srinivasan 	 * incorrect, so pass a correct CPU mask to the generic cpuidle driver.
223293d264fSVaidyanathan Srinivasan 	 */
224293d264fSVaidyanathan Srinivasan 
225293d264fSVaidyanathan Srinivasan 	drv->cpumask = (struct cpumask *)cpu_present_mask;
226293d264fSVaidyanathan Srinivasan 
2272c2e6ecfSDeepthi Dharwar 	return 0;
2282c2e6ecfSDeepthi Dharwar }
2292c2e6ecfSDeepthi Dharwar 
add_powernv_state(int index,const char * name,unsigned int flags,int (* idle_fn)(struct cpuidle_device *,struct cpuidle_driver *,int),unsigned int target_residency,unsigned int exit_latency,u64 psscr_val,u64 psscr_mask)2309e9fc6f0SGautham R. Shenoy static inline void add_powernv_state(int index, const char *name,
2319e9fc6f0SGautham R. Shenoy 				     unsigned int flags,
2329e9fc6f0SGautham R. Shenoy 				     int (*idle_fn)(struct cpuidle_device *,
2339e9fc6f0SGautham R. Shenoy 						    struct cpuidle_driver *,
2349e9fc6f0SGautham R. Shenoy 						    int),
2359e9fc6f0SGautham R. Shenoy 				     unsigned int target_residency,
2369e9fc6f0SGautham R. Shenoy 				     unsigned int exit_latency,
23709206b60SGautham R. Shenoy 				     u64 psscr_val, u64 psscr_mask)
2389e9fc6f0SGautham R. Shenoy {
239ccf28724SWolfram Sang 	strscpy(powernv_states[index].name, name, CPUIDLE_NAME_LEN);
240ccf28724SWolfram Sang 	strscpy(powernv_states[index].desc, name, CPUIDLE_NAME_LEN);
2419e9fc6f0SGautham R. Shenoy 	powernv_states[index].flags = flags;
2429e9fc6f0SGautham R. Shenoy 	powernv_states[index].target_residency = target_residency;
2439e9fc6f0SGautham R. Shenoy 	powernv_states[index].exit_latency = exit_latency;
2449e9fc6f0SGautham R. Shenoy 	powernv_states[index].enter = idle_fn;
2451961acadSAkshay Adiga 	/* For power8 and below psscr_* will be 0 */
24609206b60SGautham R. Shenoy 	stop_psscr_table[index].val = psscr_val;
24709206b60SGautham R. Shenoy 	stop_psscr_table[index].mask = psscr_mask;
2489e9fc6f0SGautham R. Shenoy }
2499e9fc6f0SGautham R. Shenoy 
250785a12afSGautham R. Shenoy extern u32 pnv_get_supported_cpuidle_states(void);
powernv_add_idle_states(void)2510888839cSPreeti U Murthy static int powernv_add_idle_states(void)
2520888839cSPreeti U Murthy {
2530888839cSPreeti U Murthy 	int nr_idle_states = 1; /* Snooze */
2541961acadSAkshay Adiga 	int dt_idle_states;
25509206b60SGautham R. Shenoy 	u32 has_stop_states = 0;
2561961acadSAkshay Adiga 	int i;
257785a12afSGautham R. Shenoy 	u32 supported_flags = pnv_get_supported_cpuidle_states();
258785a12afSGautham R. Shenoy 
2590888839cSPreeti U Murthy 
2600888839cSPreeti U Murthy 	/* Currently we have snooze statically defined */
2611961acadSAkshay Adiga 	if (nr_pnv_idle_states <= 0) {
2621961acadSAkshay Adiga 		pr_warn("cpuidle-powernv : Only Snooze is available\n");
26392c83ff5SPreeti U Murthy 		goto out;
2640888839cSPreeti U Murthy 	}
2650888839cSPreeti U Murthy 
2661961acadSAkshay Adiga 	/* TODO: Count only states which are eligible for cpuidle */
2671961acadSAkshay Adiga 	dt_idle_states = nr_pnv_idle_states;
268ecad4502SGautham R. Shenoy 
269957efcedSShreyas B. Prabhu 	/*
270957efcedSShreyas B. Prabhu 	 * Since snooze is used as first idle state, max idle states allowed is
271957efcedSShreyas B. Prabhu 	 * CPUIDLE_STATE_MAX -1
272957efcedSShreyas B. Prabhu 	 */
2731961acadSAkshay Adiga 	if (nr_pnv_idle_states > CPUIDLE_STATE_MAX - 1) {
274957efcedSShreyas B. Prabhu 		pr_warn("cpuidle-powernv: discovered idle states more than allowed");
275957efcedSShreyas B. Prabhu 		dt_idle_states = CPUIDLE_STATE_MAX - 1;
276957efcedSShreyas B. Prabhu 	}
277957efcedSShreyas B. Prabhu 
2783005c597SShreyas B. Prabhu 	/*
2793005c597SShreyas B. Prabhu 	 * If the idle states use stop instruction, probe for psscr values
28009206b60SGautham R. Shenoy 	 * and psscr mask which are necessary to specify required stop level.
2813005c597SShreyas B. Prabhu 	 */
2821961acadSAkshay Adiga 	has_stop_states = (pnv_idle_states[0].flags &
28309206b60SGautham R. Shenoy 			   (OPAL_PM_STOP_INST_FAST | OPAL_PM_STOP_INST_DEEP));
28492c83ff5SPreeti U Murthy 
2850888839cSPreeti U Murthy 	for (i = 0; i < dt_idle_states; i++) {
2869e9fc6f0SGautham R. Shenoy 		unsigned int exit_latency, target_residency;
287f9122ee4SGautham R. Shenoy 		bool stops_timebase = false;
2881961acadSAkshay Adiga 		struct pnv_idle_states_t *state = &pnv_idle_states[i];
289785a12afSGautham R. Shenoy 
290785a12afSGautham R. Shenoy 		/*
291785a12afSGautham R. Shenoy 		 * Skip the platform idle state whose flag isn't in
292785a12afSGautham R. Shenoy 		 * the supported_cpuidle_states flag mask.
293785a12afSGautham R. Shenoy 		 */
2941961acadSAkshay Adiga 		if ((state->flags & supported_flags) != state->flags)
295785a12afSGautham R. Shenoy 			continue;
2963005c597SShreyas B. Prabhu 		/*
2973005c597SShreyas B. Prabhu 		 * If an idle state has exit latency beyond
2983005c597SShreyas B. Prabhu 		 * POWERNV_THRESHOLD_LATENCY_NS then don't use it
2993005c597SShreyas B. Prabhu 		 * in cpu-idle.
3003005c597SShreyas B. Prabhu 		 */
3011961acadSAkshay Adiga 		if (state->latency_ns > POWERNV_THRESHOLD_LATENCY_NS)
3023005c597SShreyas B. Prabhu 			continue;
3039e9fc6f0SGautham R. Shenoy 		/*
3049e9fc6f0SGautham R. Shenoy 		 * Firmware passes residency and latency values in ns.
3059e9fc6f0SGautham R. Shenoy 		 * cpuidle expects it in us.
3069e9fc6f0SGautham R. Shenoy 		 */
3071961acadSAkshay Adiga 		exit_latency = DIV_ROUND_UP(state->latency_ns, 1000);
3081961acadSAkshay Adiga 		target_residency = DIV_ROUND_UP(state->residency_ns, 1000);
3090888839cSPreeti U Murthy 
3101961acadSAkshay Adiga 		if (has_stop_states && !(state->valid))
31109206b60SGautham R. Shenoy 				continue;
31209206b60SGautham R. Shenoy 
3131961acadSAkshay Adiga 		if (state->flags & OPAL_PM_TIMEBASE_STOP)
314f9122ee4SGautham R. Shenoy 			stops_timebase = true;
315f9122ee4SGautham R. Shenoy 
3161961acadSAkshay Adiga 		if (state->flags & OPAL_PM_NAP_ENABLED) {
3170888839cSPreeti U Murthy 			/* Add NAP state */
3189e9fc6f0SGautham R. Shenoy 			add_powernv_state(nr_idle_states, "Nap",
3199e9fc6f0SGautham R. Shenoy 					  CPUIDLE_FLAG_NONE, nap_loop,
32009206b60SGautham R. Shenoy 					  target_residency, exit_latency, 0, 0);
321f9122ee4SGautham R. Shenoy 		} else if (has_stop_states && !stops_timebase) {
3221961acadSAkshay Adiga 			add_powernv_state(nr_idle_states, state->name,
3239e9fc6f0SGautham R. Shenoy 					  CPUIDLE_FLAG_NONE, stop_loop,
3249e9fc6f0SGautham R. Shenoy 					  target_residency, exit_latency,
3251961acadSAkshay Adiga 					  state->psscr_val,
3261961acadSAkshay Adiga 					  state->psscr_mask);
327cc5a2f7bSpreeti 		}
328cc5a2f7bSpreeti 
329cc5a2f7bSpreeti 		/*
330cc5a2f7bSpreeti 		 * All cpuidle states with CPUIDLE_FLAG_TIMER_STOP set must come
331cc5a2f7bSpreeti 		 * within this config dependency check.
332cc5a2f7bSpreeti 		 */
333cc5a2f7bSpreeti #ifdef CONFIG_TICK_ONESHOT
3341961acadSAkshay Adiga 		else if (state->flags & OPAL_PM_SLEEP_ENABLED ||
3351961acadSAkshay Adiga 			 state->flags & OPAL_PM_SLEEP_ENABLED_ER1) {
3360888839cSPreeti U Murthy 			/* Add FASTSLEEP state */
3379e9fc6f0SGautham R. Shenoy 			add_powernv_state(nr_idle_states, "FastSleep",
3389e9fc6f0SGautham R. Shenoy 					  CPUIDLE_FLAG_TIMER_STOP,
3399e9fc6f0SGautham R. Shenoy 					  fastsleep_loop,
34009206b60SGautham R. Shenoy 					  target_residency, exit_latency, 0, 0);
341f9122ee4SGautham R. Shenoy 		} else if (has_stop_states && stops_timebase) {
3421961acadSAkshay Adiga 			add_powernv_state(nr_idle_states, state->name,
3439e9fc6f0SGautham R. Shenoy 					  CPUIDLE_FLAG_TIMER_STOP, stop_loop,
3449e9fc6f0SGautham R. Shenoy 					  target_residency, exit_latency,
3451961acadSAkshay Adiga 					  state->psscr_val,
3461961acadSAkshay Adiga 					  state->psscr_mask);
3470888839cSPreeti U Murthy 		}
348cc5a2f7bSpreeti #endif
349f9122ee4SGautham R. Shenoy 		else
350f9122ee4SGautham R. Shenoy 			continue;
35192c83ff5SPreeti U Murthy 		nr_idle_states++;
35292c83ff5SPreeti U Murthy 	}
35392c83ff5SPreeti U Murthy out:
3540888839cSPreeti U Murthy 	return nr_idle_states;
3550888839cSPreeti U Murthy }
3560888839cSPreeti U Murthy 
3572c2e6ecfSDeepthi Dharwar /*
3582c2e6ecfSDeepthi Dharwar  * powernv_idle_probe()
3592c2e6ecfSDeepthi Dharwar  * Choose state table for shared versus dedicated partition
3602c2e6ecfSDeepthi Dharwar  */
powernv_idle_probe(void)3612c2e6ecfSDeepthi Dharwar static int powernv_idle_probe(void)
3622c2e6ecfSDeepthi Dharwar {
3632c2e6ecfSDeepthi Dharwar 	if (cpuidle_disable != IDLE_NO_OVERRIDE)
3642c2e6ecfSDeepthi Dharwar 		return -ENODEV;
3652c2e6ecfSDeepthi Dharwar 
366e4d54f71SStewart Smith 	if (firmware_has_feature(FW_FEATURE_OPAL)) {
3672c2e6ecfSDeepthi Dharwar 		cpuidle_state_table = powernv_states;
3680888839cSPreeti U Murthy 		/* Device tree can indicate more idle states */
3690888839cSPreeti U Murthy 		max_idle_state = powernv_add_idle_states();
3700a4ec6aaSGautham R. Shenoy 		default_snooze_timeout = TICK_USEC * tb_ticks_per_usec;
3710a4ec6aaSGautham R. Shenoy 		if (max_idle_state > 1)
37278eaa10fSShilpasri G Bhat 			snooze_timeout_en = true;
3732c2e6ecfSDeepthi Dharwar  	} else
3742c2e6ecfSDeepthi Dharwar  		return -ENODEV;
3752c2e6ecfSDeepthi Dharwar 
3762c2e6ecfSDeepthi Dharwar 	return 0;
3772c2e6ecfSDeepthi Dharwar }
3782c2e6ecfSDeepthi Dharwar 
powernv_processor_idle_init(void)3792c2e6ecfSDeepthi Dharwar static int __init powernv_processor_idle_init(void)
3802c2e6ecfSDeepthi Dharwar {
3812c2e6ecfSDeepthi Dharwar 	int retval;
3822c2e6ecfSDeepthi Dharwar 
3832c2e6ecfSDeepthi Dharwar 	retval = powernv_idle_probe();
3842c2e6ecfSDeepthi Dharwar 	if (retval)
3852c2e6ecfSDeepthi Dharwar 		return retval;
3862c2e6ecfSDeepthi Dharwar 
3872c2e6ecfSDeepthi Dharwar 	powernv_cpuidle_driver_init();
3882c2e6ecfSDeepthi Dharwar 	retval = cpuidle_register(&powernv_idle_driver, NULL);
3892c2e6ecfSDeepthi Dharwar 	if (retval) {
3902c2e6ecfSDeepthi Dharwar 		printk(KERN_DEBUG "Registration of powernv driver failed.\n");
3912c2e6ecfSDeepthi Dharwar 		return retval;
3922c2e6ecfSDeepthi Dharwar 	}
3932c2e6ecfSDeepthi Dharwar 
39410fcca9dSSebastian Andrzej Siewior 	retval = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
39510fcca9dSSebastian Andrzej Siewior 					   "cpuidle/powernv:online",
39610fcca9dSSebastian Andrzej Siewior 					   powernv_cpuidle_cpu_online, NULL);
39710fcca9dSSebastian Andrzej Siewior 	WARN_ON(retval < 0);
39810fcca9dSSebastian Andrzej Siewior 	retval = cpuhp_setup_state_nocalls(CPUHP_CPUIDLE_DEAD,
39910fcca9dSSebastian Andrzej Siewior 					   "cpuidle/powernv:dead", NULL,
40010fcca9dSSebastian Andrzej Siewior 					   powernv_cpuidle_cpu_dead);
40110fcca9dSSebastian Andrzej Siewior 	WARN_ON(retval < 0);
4022c2e6ecfSDeepthi Dharwar 	printk(KERN_DEBUG "powernv_idle_driver registered\n");
4032c2e6ecfSDeepthi Dharwar 	return 0;
4042c2e6ecfSDeepthi Dharwar }
4052c2e6ecfSDeepthi Dharwar 
4062c2e6ecfSDeepthi Dharwar device_initcall(powernv_processor_idle_init);
407