blob: 5676aefdf2bca72bd8734ff2642c3720b480d43a [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))
Stefano Stabellinia314e3e2015-10-22 16:20:46 +000014 xen_arch_register_cpu(cpu);
Alex Nixond68d82a2008-08-22 11:52:15 +010015
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{
Stefano Stabellini1c7a6212015-10-22 16:21:46 +000021 if (cpu_online(cpu)) {
22 lock_device_hotplug();
23 device_offline(get_cpu_device(cpu));
24 unlock_device_hotplug();
25 }
Alex Nixond68d82a2008-08-22 11:52:15 +010026 if (cpu_present(cpu))
Stefano Stabellinia314e3e2015-10-22 16:20:46 +000027 xen_arch_unregister_cpu(cpu);
Alex Nixond68d82a2008-08-22 11:52:15 +010028
Rusty Russelld680eb82009-03-13 14:49:56 +103029 set_cpu_present(cpu, false);
Alex Nixond68d82a2008-08-22 11:52:15 +010030}
31
Ian Campbelld7455622009-04-02 13:24:28 +010032static int vcpu_online(unsigned int cpu)
Alex Nixond68d82a2008-08-22 11:52:15 +010033{
34 int err;
Jan Beuliche5c702d2013-01-15 13:31:43 +000035 char dir[16], state[16];
Alex Nixond68d82a2008-08-22 11:52:15 +010036
Alex Nixond68d82a2008-08-22 11:52:15 +010037 sprintf(dir, "cpu/%u", cpu);
Jan Beuliche5c702d2013-01-15 13:31:43 +000038 err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
Alex Nixond68d82a2008-08-22 11:52:15 +010039 if (err != 1) {
Konrad Rzeszutek Wilk5b02aa12012-02-01 16:07:41 -050040 if (!xen_initial_domain())
Joe Perches283c0972013-06-28 03:21:41 -070041 pr_err("Unable to read cpu state\n");
Ian Campbelld7455622009-04-02 13:24:28 +010042 return err;
Alex Nixond68d82a2008-08-22 11:52:15 +010043 }
44
Ian Campbelld7455622009-04-02 13:24:28 +010045 if (strcmp(state, "online") == 0)
46 return 1;
47 else if (strcmp(state, "offline") == 0)
48 return 0;
49
Joe Perches283c0972013-06-28 03:21:41 -070050 pr_err("unknown state(%s) on CPU%d\n", state, cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010051 return -EINVAL;
52}
53static void vcpu_hotplug(unsigned int cpu)
54{
55 if (!cpu_possible(cpu))
56 return;
57
58 switch (vcpu_online(cpu)) {
59 case 1:
Alex Nixond68d82a2008-08-22 11:52:15 +010060 enable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010061 break;
62 case 0:
Alex Nixond68d82a2008-08-22 11:52:15 +010063 disable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010064 break;
65 default:
66 break;
Alex Nixond68d82a2008-08-22 11:52:15 +010067 }
68}
69
70static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
71 const char **vec, unsigned int len)
72{
73 unsigned int cpu;
74 char *cpustr;
75 const char *node = vec[XS_WATCH_PATH];
76
77 cpustr = strstr(node, "cpu/");
78 if (cpustr != NULL) {
79 sscanf(cpustr, "cpu/%u", &cpu);
80 vcpu_hotplug(cpu);
81 }
82}
83
84static int setup_cpu_watcher(struct notifier_block *notifier,
85 unsigned long event, void *data)
86{
Ian Campbelld7455622009-04-02 13:24:28 +010087 int cpu;
Alex Nixond68d82a2008-08-22 11:52:15 +010088 static struct xenbus_watch cpu_watch = {
89 .node = "cpu",
90 .callback = handle_vcpu_hotplug_event};
91
92 (void)register_xenbus_watch(&cpu_watch);
93
Ian Campbelld7455622009-04-02 13:24:28 +010094 for_each_possible_cpu(cpu) {
95 if (vcpu_online(cpu) == 0) {
96 (void)cpu_down(cpu);
Rusty Russelld7d37562009-11-03 14:58:38 +103097 set_cpu_present(cpu, false);
Ian Campbelld7455622009-04-02 13:24:28 +010098 }
99 }
100
Alex Nixond68d82a2008-08-22 11:52:15 +0100101 return NOTIFY_DONE;
102}
103
104static int __init setup_vcpu_hotplug_event(void)
105{
106 static struct notifier_block xsn_cpu = {
107 .notifier_call = setup_cpu_watcher };
108
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000109#ifdef CONFIG_X86
Alex Nixon5c51c632008-09-03 14:30:21 +0100110 if (!xen_pv_domain())
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000111#else
112 if (!xen_domain())
113#endif
Alex Nixond68d82a2008-08-22 11:52:15 +0100114 return -ENODEV;
115
116 register_xenstore_notifier(&xsn_cpu);
117
118 return 0;
119}
120
121arch_initcall(setup_vcpu_hotplug_event);
122