blob: 0003912a8111959a101803f52ed8be80f4ae2f30 [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,
Juergen Gross5584ea22017-02-09 14:39:57 +010071 const char *path, const char *token)
Alex Nixond68d82a2008-08-22 11:52:15 +010072{
73 unsigned int cpu;
74 char *cpustr;
Alex Nixond68d82a2008-08-22 11:52:15 +010075
Juergen Gross5584ea22017-02-09 14:39:57 +010076 cpustr = strstr(path, "cpu/");
Alex Nixond68d82a2008-08-22 11:52:15 +010077 if (cpustr != NULL) {
78 sscanf(cpustr, "cpu/%u", &cpu);
79 vcpu_hotplug(cpu);
80 }
81}
82
83static int setup_cpu_watcher(struct notifier_block *notifier,
84 unsigned long event, void *data)
85{
Ian Campbelld7455622009-04-02 13:24:28 +010086 int cpu;
Alex Nixond68d82a2008-08-22 11:52:15 +010087 static struct xenbus_watch cpu_watch = {
88 .node = "cpu",
89 .callback = handle_vcpu_hotplug_event};
90
91 (void)register_xenbus_watch(&cpu_watch);
92
Ian Campbelld7455622009-04-02 13:24:28 +010093 for_each_possible_cpu(cpu) {
94 if (vcpu_online(cpu) == 0) {
95 (void)cpu_down(cpu);
Rusty Russelld7d37562009-11-03 14:58:38 +103096 set_cpu_present(cpu, false);
Ian Campbelld7455622009-04-02 13:24:28 +010097 }
98 }
99
Alex Nixond68d82a2008-08-22 11:52:15 +0100100 return NOTIFY_DONE;
101}
102
103static int __init setup_vcpu_hotplug_event(void)
104{
105 static struct notifier_block xsn_cpu = {
106 .notifier_call = setup_cpu_watcher };
107
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000108#ifdef CONFIG_X86
Boris Ostrovsky2a7197f2017-02-06 10:58:05 -0500109 if (!xen_pv_domain() && !xen_pvh_domain())
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000110#else
111 if (!xen_domain())
112#endif
Alex Nixond68d82a2008-08-22 11:52:15 +0100113 return -ENODEV;
114
115 register_xenstore_notifier(&xsn_cpu);
116
117 return 0;
118}
119
120arch_initcall(setup_vcpu_hotplug_event);
121