blob: 4dcfced107f50e41c518405b28747064d62d78bc [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) {
Konrad Rzeszutek Wilk5b02aa12012-02-01 16:07:41 -050033 if (!xen_initial_domain())
34 printk(KERN_ERR "XENBUS: Unable to read cpu state\n");
Ian Campbelld7455622009-04-02 13:24:28 +010035 return err;
Alex Nixond68d82a2008-08-22 11:52:15 +010036 }
37
Ian Campbelld7455622009-04-02 13:24:28 +010038 if (strcmp(state, "online") == 0)
39 return 1;
40 else if (strcmp(state, "offline") == 0)
41 return 0;
42
43 printk(KERN_ERR "XENBUS: unknown state(%s) on CPU%d\n", state, cpu);
44 return -EINVAL;
45}
46static void vcpu_hotplug(unsigned int cpu)
47{
48 if (!cpu_possible(cpu))
49 return;
50
51 switch (vcpu_online(cpu)) {
52 case 1:
Alex Nixond68d82a2008-08-22 11:52:15 +010053 enable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010054 break;
55 case 0:
Alex Nixond68d82a2008-08-22 11:52:15 +010056 (void)cpu_down(cpu);
57 disable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010058 break;
59 default:
60 break;
Alex Nixond68d82a2008-08-22 11:52:15 +010061 }
62}
63
64static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
65 const char **vec, unsigned int len)
66{
67 unsigned int cpu;
68 char *cpustr;
69 const char *node = vec[XS_WATCH_PATH];
70
71 cpustr = strstr(node, "cpu/");
72 if (cpustr != NULL) {
73 sscanf(cpustr, "cpu/%u", &cpu);
74 vcpu_hotplug(cpu);
75 }
76}
77
78static int setup_cpu_watcher(struct notifier_block *notifier,
79 unsigned long event, void *data)
80{
Ian Campbelld7455622009-04-02 13:24:28 +010081 int cpu;
Alex Nixond68d82a2008-08-22 11:52:15 +010082 static struct xenbus_watch cpu_watch = {
83 .node = "cpu",
84 .callback = handle_vcpu_hotplug_event};
85
86 (void)register_xenbus_watch(&cpu_watch);
87
Ian Campbelld7455622009-04-02 13:24:28 +010088 for_each_possible_cpu(cpu) {
89 if (vcpu_online(cpu) == 0) {
90 (void)cpu_down(cpu);
Rusty Russelld7d37562009-11-03 14:58:38 +103091 set_cpu_present(cpu, false);
Ian Campbelld7455622009-04-02 13:24:28 +010092 }
93 }
94
Alex Nixond68d82a2008-08-22 11:52:15 +010095 return NOTIFY_DONE;
96}
97
98static int __init setup_vcpu_hotplug_event(void)
99{
100 static struct notifier_block xsn_cpu = {
101 .notifier_call = setup_cpu_watcher };
102
Alex Nixon5c51c632008-09-03 14:30:21 +0100103 if (!xen_pv_domain())
Alex Nixond68d82a2008-08-22 11:52:15 +0100104 return -ENODEV;
105
106 register_xenstore_notifier(&xsn_cpu);
107
108 return 0;
109}
110
111arch_initcall(setup_vcpu_hotplug_event);
112