Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[cascardo/linux.git] / drivers / cpuidle / cpuidle-powernv.c
1 /*
2  *  cpuidle-powernv - idle state cpuidle driver.
3  *  Adapted from drivers/cpuidle/cpuidle-pseries
4  *
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/moduleparam.h>
11 #include <linux/cpuidle.h>
12 #include <linux/cpu.h>
13 #include <linux/notifier.h>
14
15 #include <asm/machdep.h>
16 #include <asm/firmware.h>
17 #include <asm/runlatch.h>
18
19 struct cpuidle_driver powernv_idle_driver = {
20         .name             = "powernv_idle",
21         .owner            = THIS_MODULE,
22 };
23
24 static int max_idle_state;
25 static struct cpuidle_state *cpuidle_state_table;
26
27 static int snooze_loop(struct cpuidle_device *dev,
28                         struct cpuidle_driver *drv,
29                         int index)
30 {
31         local_irq_enable();
32         set_thread_flag(TIF_POLLING_NRFLAG);
33
34         ppc64_runlatch_off();
35         while (!need_resched()) {
36                 HMT_low();
37                 HMT_very_low();
38         }
39
40         HMT_medium();
41         ppc64_runlatch_on();
42         clear_thread_flag(TIF_POLLING_NRFLAG);
43         smp_mb();
44         return index;
45 }
46
47 static int nap_loop(struct cpuidle_device *dev,
48                         struct cpuidle_driver *drv,
49                         int index)
50 {
51         ppc64_runlatch_off();
52         power7_idle();
53         ppc64_runlatch_on();
54         return index;
55 }
56
57 /*
58  * States for dedicated partition case.
59  */
60 static struct cpuidle_state powernv_states[] = {
61         { /* Snooze */
62                 .name = "snooze",
63                 .desc = "snooze",
64                 .flags = CPUIDLE_FLAG_TIME_VALID,
65                 .exit_latency = 0,
66                 .target_residency = 0,
67                 .enter = &snooze_loop },
68         { /* NAP */
69                 .name = "NAP",
70                 .desc = "NAP",
71                 .flags = CPUIDLE_FLAG_TIME_VALID,
72                 .exit_latency = 10,
73                 .target_residency = 100,
74                 .enter = &nap_loop },
75 };
76
77 static int powernv_cpuidle_add_cpu_notifier(struct notifier_block *n,
78                         unsigned long action, void *hcpu)
79 {
80         int hotcpu = (unsigned long)hcpu;
81         struct cpuidle_device *dev =
82                                 per_cpu(cpuidle_devices, hotcpu);
83
84         if (dev && cpuidle_get_driver()) {
85                 switch (action) {
86                 case CPU_ONLINE:
87                 case CPU_ONLINE_FROZEN:
88                         cpuidle_pause_and_lock();
89                         cpuidle_enable_device(dev);
90                         cpuidle_resume_and_unlock();
91                         break;
92
93                 case CPU_DEAD:
94                 case CPU_DEAD_FROZEN:
95                         cpuidle_pause_and_lock();
96                         cpuidle_disable_device(dev);
97                         cpuidle_resume_and_unlock();
98                         break;
99
100                 default:
101                         return NOTIFY_DONE;
102                 }
103         }
104         return NOTIFY_OK;
105 }
106
107 static struct notifier_block setup_hotplug_notifier = {
108         .notifier_call = powernv_cpuidle_add_cpu_notifier,
109 };
110
111 /*
112  * powernv_cpuidle_driver_init()
113  */
114 static int powernv_cpuidle_driver_init(void)
115 {
116         int idle_state;
117         struct cpuidle_driver *drv = &powernv_idle_driver;
118
119         drv->state_count = 0;
120
121         for (idle_state = 0; idle_state < max_idle_state; ++idle_state) {
122                 /* Is the state not enabled? */
123                 if (cpuidle_state_table[idle_state].enter == NULL)
124                         continue;
125
126                 drv->states[drv->state_count] = /* structure copy */
127                         cpuidle_state_table[idle_state];
128
129                 drv->state_count += 1;
130         }
131
132         return 0;
133 }
134
135 /*
136  * powernv_idle_probe()
137  * Choose state table for shared versus dedicated partition
138  */
139 static int powernv_idle_probe(void)
140 {
141
142         if (cpuidle_disable != IDLE_NO_OVERRIDE)
143                 return -ENODEV;
144
145         if (firmware_has_feature(FW_FEATURE_OPALv3)) {
146                 cpuidle_state_table = powernv_states;
147                 max_idle_state = ARRAY_SIZE(powernv_states);
148         } else
149                 return -ENODEV;
150
151         return 0;
152 }
153
154 static int __init powernv_processor_idle_init(void)
155 {
156         int retval;
157
158         retval = powernv_idle_probe();
159         if (retval)
160                 return retval;
161
162         powernv_cpuidle_driver_init();
163         retval = cpuidle_register(&powernv_idle_driver, NULL);
164         if (retval) {
165                 printk(KERN_DEBUG "Registration of powernv driver failed.\n");
166                 return retval;
167         }
168
169         register_cpu_notifier(&setup_hotplug_notifier);
170         printk(KERN_DEBUG "powernv_idle_driver registered\n");
171         return 0;
172 }
173
174 device_initcall(powernv_processor_idle_init);