blob: 14e2d995e95800ca88a064ac5ba09caec2b48973 [file] [log] [blame]
Alex Nixond68d82a2008-08-22 11:52:15 +01001#include <linux/notifier.h>
2
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -07003#include <xen/xen.h>
Alex Nixond68d82a2008-08-22 11:52:15 +01004#include <xen/xenbus.h>
5
Al Virobb898552008-08-17 21:05:42 -04006#include <asm/xen/hypervisor.h>
Alex Nixond68d82a2008-08-22 11:52:15 +01007#include <asm/cpu.h>
8
9static void enable_hotplug_cpu(int cpu)
10{
11 if (!cpu_present(cpu))
12 arch_register_cpu(cpu);
13
Rusty Russelld680eb82009-03-13 14:49:56 +103014 set_cpu_present(cpu, true);
Alex Nixond68d82a2008-08-22 11:52:15 +010015}
16
17static void disable_hotplug_cpu(int cpu)
18{
19 if (cpu_present(cpu))
20 arch_unregister_cpu(cpu);
21
Rusty Russelld680eb82009-03-13 14:49:56 +103022 set_cpu_present(cpu, false);
Alex Nixond68d82a2008-08-22 11:52:15 +010023}
24
Ian Campbelld7455622009-04-02 13:24:28 +010025static int vcpu_online(unsigned int cpu)
Alex Nixond68d82a2008-08-22 11:52:15 +010026{
27 int err;
28 char dir[32], state[32];
29
Alex Nixond68d82a2008-08-22 11:52:15 +010030 sprintf(dir, "cpu/%u", cpu);
31 err = xenbus_scanf(XBT_NIL, dir, "availability", "%s", state);
32 if (err != 1) {
33 printk(KERN_ERR "XENBUS: Unable to read cpu state\n");
Ian Campbelld7455622009-04-02 13:24:28 +010034 return err;
Alex Nixond68d82a2008-08-22 11:52:15 +010035 }
36
Ian Campbelld7455622009-04-02 13:24:28 +010037 if (strcmp(state, "online") == 0)
38 return 1;
39 else if (strcmp(state, "offline") == 0)
40 return 0;
41
42 printk(KERN_ERR "XENBUS: unknown state(%s) on CPU%d\n", state, cpu);
43 return -EINVAL;
44}
45static void vcpu_hotplug(unsigned int cpu)
46{
47 if (!cpu_possible(cpu))
48 return;
49
50 switch (vcpu_online(cpu)) {
51 case 1:
Alex Nixond68d82a2008-08-22 11:52:15 +010052 enable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010053 break;
54 case 0:
Alex Nixond68d82a2008-08-22 11:52:15 +010055 (void)cpu_down(cpu);
56 disable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010057 break;
58 default:
59 break;
Alex Nixond68d82a2008-08-22 11:52:15 +010060 }
61}
62
63static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
64 const char **vec, unsigned int len)
65{
66 unsigned int cpu;
67 char *cpustr;
68 const char *node = vec[XS_WATCH_PATH];
69
70 cpustr = strstr(node, "cpu/");
71 if (cpustr != NULL) {
72 sscanf(cpustr, "cpu/%u", &cpu);
73 vcpu_hotplug(cpu);
74 }
75}
76
77static int setup_cpu_watcher(struct notifier_block *notifier,
78 unsigned long event, void *data)
79{
Ian Campbelld7455622009-04-02 13:24:28 +010080 int cpu;
Alex Nixond68d82a2008-08-22 11:52:15 +010081 static struct xenbus_watch cpu_watch = {
82 .node = "cpu",
83 .callback = handle_vcpu_hotplug_event};
84
85 (void)register_xenbus_watch(&cpu_watch);
86
Ian Campbelld7455622009-04-02 13:24:28 +010087 for_each_possible_cpu(cpu) {
88 if (vcpu_online(cpu) == 0) {
89 (void)cpu_down(cpu);
Rusty Russelld7d37562009-11-03 14:58:38 +103090 set_cpu_present(cpu, false);
Ian Campbelld7455622009-04-02 13:24:28 +010091 }
92 }
93
Alex Nixond68d82a2008-08-22 11:52:15 +010094 return NOTIFY_DONE;
95}
96
97static int __init setup_vcpu_hotplug_event(void)
98{
99 static struct notifier_block xsn_cpu = {
100 .notifier_call = setup_cpu_watcher };
101
Alex Nixon5c51c632008-09-03 14:30:21 +0100102 if (!xen_pv_domain())
Alex Nixond68d82a2008-08-22 11:52:15 +0100103 return -ENODEV;
104
105 register_xenstore_notifier(&xsn_cpu);
106
107 return 0;
108}
109
110arch_initcall(setup_vcpu_hotplug_event);
111