blob: cc6513a176b0b07ea25990a8470f3d8ee907f507 [file] [log] [blame]
Joe Perches283c0972013-06-28 03:21:41 -07001#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
2
Alex Nixond68d82a2008-08-22 11:52:15 +01003#include <linux/notifier.h>
4
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -07005#include <xen/xen.h>
Alex Nixond68d82a2008-08-22 11:52:15 +01006#include <xen/xenbus.h>
7
Al Virobb898552008-08-17 21:05:42 -04008#include <asm/xen/hypervisor.h>
Alex Nixond68d82a2008-08-22 11:52:15 +01009#include <asm/cpu.h>
10
11static void enable_hotplug_cpu(int cpu)
12{
13 if (!cpu_present(cpu))
14 arch_register_cpu(cpu);
15
Rusty Russelld680eb82009-03-13 14:49:56 +103016 set_cpu_present(cpu, true);
Alex Nixond68d82a2008-08-22 11:52:15 +010017}
18
19static void disable_hotplug_cpu(int cpu)
20{
21 if (cpu_present(cpu))
22 arch_unregister_cpu(cpu);
23
Rusty Russelld680eb82009-03-13 14:49:56 +103024 set_cpu_present(cpu, false);
Alex Nixond68d82a2008-08-22 11:52:15 +010025}
26
Ian Campbelld7455622009-04-02 13:24:28 +010027static int vcpu_online(unsigned int cpu)
Alex Nixond68d82a2008-08-22 11:52:15 +010028{
29 int err;
Jan Beuliche5c702d2013-01-15 13:31:43 +000030 char dir[16], state[16];
Alex Nixond68d82a2008-08-22 11:52:15 +010031
Alex Nixond68d82a2008-08-22 11:52:15 +010032 sprintf(dir, "cpu/%u", cpu);
Jan Beuliche5c702d2013-01-15 13:31:43 +000033 err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
Alex Nixond68d82a2008-08-22 11:52:15 +010034 if (err != 1) {
Konrad Rzeszutek Wilk5b02aa12012-02-01 16:07:41 -050035 if (!xen_initial_domain())
Joe Perches283c0972013-06-28 03:21:41 -070036 pr_err("Unable to read cpu state\n");
Ian Campbelld7455622009-04-02 13:24:28 +010037 return err;
Alex Nixond68d82a2008-08-22 11:52:15 +010038 }
39
Ian Campbelld7455622009-04-02 13:24:28 +010040 if (strcmp(state, "online") == 0)
41 return 1;
42 else if (strcmp(state, "offline") == 0)
43 return 0;
44
Joe Perches283c0972013-06-28 03:21:41 -070045 pr_err("unknown state(%s) on CPU%d\n", state, cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010046 return -EINVAL;
47}
48static void vcpu_hotplug(unsigned int cpu)
49{
50 if (!cpu_possible(cpu))
51 return;
52
53 switch (vcpu_online(cpu)) {
54 case 1:
Alex Nixond68d82a2008-08-22 11:52:15 +010055 enable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010056 break;
57 case 0:
Alex Nixond68d82a2008-08-22 11:52:15 +010058 (void)cpu_down(cpu);
59 disable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010060 break;
61 default:
62 break;
Alex Nixond68d82a2008-08-22 11:52:15 +010063 }
64}
65
66static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
67 const char **vec, unsigned int len)
68{
69 unsigned int cpu;
70 char *cpustr;
71 const char *node = vec[XS_WATCH_PATH];
72
73 cpustr = strstr(node, "cpu/");
74 if (cpustr != NULL) {
75 sscanf(cpustr, "cpu/%u", &cpu);
76 vcpu_hotplug(cpu);
77 }
78}
79
80static int setup_cpu_watcher(struct notifier_block *notifier,
81 unsigned long event, void *data)
82{
Ian Campbelld7455622009-04-02 13:24:28 +010083 int cpu;
Alex Nixond68d82a2008-08-22 11:52:15 +010084 static struct xenbus_watch cpu_watch = {
85 .node = "cpu",
86 .callback = handle_vcpu_hotplug_event};
87
88 (void)register_xenbus_watch(&cpu_watch);
89
Ian Campbelld7455622009-04-02 13:24:28 +010090 for_each_possible_cpu(cpu) {
91 if (vcpu_online(cpu) == 0) {
92 (void)cpu_down(cpu);
Rusty Russelld7d37562009-11-03 14:58:38 +103093 set_cpu_present(cpu, false);
Ian Campbelld7455622009-04-02 13:24:28 +010094 }
95 }
96
Alex Nixond68d82a2008-08-22 11:52:15 +010097 return NOTIFY_DONE;
98}
99
100static int __init setup_vcpu_hotplug_event(void)
101{
102 static struct notifier_block xsn_cpu = {
103 .notifier_call = setup_cpu_watcher };
104
Alex Nixon5c51c632008-09-03 14:30:21 +0100105 if (!xen_pv_domain())
Alex Nixond68d82a2008-08-22 11:52:15 +0100106 return -ENODEV;
107
108 register_xenstore_notifier(&xsn_cpu);
109
110 return 0;
111}
112
113arch_initcall(setup_vcpu_hotplug_event);
114