1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
4 #include <linux/notifier.h>
7 #include <xen/xenbus.h>
9 #include <asm/xen/hypervisor.h>
12 static void enable_hotplug_cpu(int cpu)
14 if (!cpu_present(cpu))
15 xen_arch_register_cpu(cpu);
17 set_cpu_present(cpu, true);
20 static void disable_hotplug_cpu(int cpu)
22 if (cpu_online(cpu)) {
23 lock_device_hotplug();
24 device_offline(get_cpu_device(cpu));
25 unlock_device_hotplug();
28 xen_arch_unregister_cpu(cpu);
30 set_cpu_present(cpu, false);
33 static int vcpu_online(unsigned int cpu)
36 char dir[16], state[16];
38 sprintf(dir, "cpu/%u", cpu);
39 err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
41 if (!xen_initial_domain())
42 pr_err("Unable to read cpu state\n");
46 if (strcmp(state, "online") == 0)
48 else if (strcmp(state, "offline") == 0)
51 pr_err("unknown state(%s) on CPU%d\n", state, cpu);
54 static void vcpu_hotplug(unsigned int cpu)
56 if (!cpu_possible(cpu))
59 switch (vcpu_online(cpu)) {
61 enable_hotplug_cpu(cpu);
64 disable_hotplug_cpu(cpu);
71 static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
72 const char *path, const char *token)
77 cpustr = strstr(path, "cpu/");
79 sscanf(cpustr, "cpu/%u", &cpu);
84 static int setup_cpu_watcher(struct notifier_block *notifier,
85 unsigned long event, void *data)
88 static struct xenbus_watch cpu_watch = {
90 .callback = handle_vcpu_hotplug_event};
92 (void)register_xenbus_watch(&cpu_watch);
94 for_each_possible_cpu(cpu) {
95 if (vcpu_online(cpu) == 0) {
97 set_cpu_present(cpu, false);
104 static int __init setup_vcpu_hotplug_event(void)
106 static struct notifier_block xsn_cpu = {
107 .notifier_call = setup_cpu_watcher };
110 if (!xen_pv_domain() && !xen_pvh_domain())
116 register_xenstore_notifier(&xsn_cpu);
121 arch_initcall(setup_vcpu_hotplug_event);