Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
283c0972 JP |
2 | #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt |
3 | ||
d68d82af AN |
4 | #include <linux/notifier.h> |
5 | ||
1ccbf534 | 6 | #include <xen/xen.h> |
d68d82af AN |
7 | #include <xen/xenbus.h> |
8 | ||
bb898558 | 9 | #include <asm/xen/hypervisor.h> |
d68d82af AN |
10 | #include <asm/cpu.h> |
11 | ||
12 | static void enable_hotplug_cpu(int cpu) | |
13 | { | |
14 | if (!cpu_present(cpu)) | |
a314e3eb | 15 | xen_arch_register_cpu(cpu); |
d68d82af | 16 | |
d680eb8b | 17 | set_cpu_present(cpu, true); |
d68d82af AN |
18 | } |
19 | ||
20 | static void disable_hotplug_cpu(int cpu) | |
21 | { | |
1c7a6213 SS |
22 | if (cpu_online(cpu)) { |
23 | lock_device_hotplug(); | |
24 | device_offline(get_cpu_device(cpu)); | |
25 | unlock_device_hotplug(); | |
26 | } | |
d68d82af | 27 | if (cpu_present(cpu)) |
a314e3eb | 28 | xen_arch_unregister_cpu(cpu); |
d68d82af | 29 | |
d680eb8b | 30 | set_cpu_present(cpu, false); |
d68d82af AN |
31 | } |
32 | ||
d745562c | 33 | static int vcpu_online(unsigned int cpu) |
d68d82af AN |
34 | { |
35 | int err; | |
e5c702d3 | 36 | char dir[16], state[16]; |
d68d82af | 37 | |
d68d82af | 38 | sprintf(dir, "cpu/%u", cpu); |
e5c702d3 | 39 | err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state); |
d68d82af | 40 | if (err != 1) { |
5b02aa1e | 41 | if (!xen_initial_domain()) |
283c0972 | 42 | pr_err("Unable to read cpu state\n"); |
d745562c | 43 | return err; |
d68d82af AN |
44 | } |
45 | ||
d745562c IC |
46 | if (strcmp(state, "online") == 0) |
47 | return 1; | |
48 | else if (strcmp(state, "offline") == 0) | |
49 | return 0; | |
50 | ||
283c0972 | 51 | pr_err("unknown state(%s) on CPU%d\n", state, cpu); |
d745562c IC |
52 | return -EINVAL; |
53 | } | |
54 | static void vcpu_hotplug(unsigned int cpu) | |
55 | { | |
56 | if (!cpu_possible(cpu)) | |
57 | return; | |
58 | ||
59 | switch (vcpu_online(cpu)) { | |
60 | case 1: | |
d68d82af | 61 | enable_hotplug_cpu(cpu); |
d745562c IC |
62 | break; |
63 | case 0: | |
d68d82af | 64 | disable_hotplug_cpu(cpu); |
d745562c IC |
65 | break; |
66 | default: | |
67 | break; | |
d68d82af AN |
68 | } |
69 | } | |
70 | ||
71 | static void handle_vcpu_hotplug_event(struct xenbus_watch *watch, | |
5584ea25 | 72 | const char *path, const char *token) |
d68d82af AN |
73 | { |
74 | unsigned int cpu; | |
75 | char *cpustr; | |
d68d82af | 76 | |
5584ea25 | 77 | cpustr = strstr(path, "cpu/"); |
d68d82af AN |
78 | if (cpustr != NULL) { |
79 | sscanf(cpustr, "cpu/%u", &cpu); | |
80 | vcpu_hotplug(cpu); | |
81 | } | |
82 | } | |
83 | ||
84 | static int setup_cpu_watcher(struct notifier_block *notifier, | |
85 | unsigned long event, void *data) | |
86 | { | |
d745562c | 87 | int cpu; |
d68d82af AN |
88 | static struct xenbus_watch cpu_watch = { |
89 | .node = "cpu", | |
90 | .callback = handle_vcpu_hotplug_event}; | |
91 | ||
92 | (void)register_xenbus_watch(&cpu_watch); | |
93 | ||
d745562c IC |
94 | for_each_possible_cpu(cpu) { |
95 | if (vcpu_online(cpu) == 0) { | |
96 | (void)cpu_down(cpu); | |
d7d3756c | 97 | set_cpu_present(cpu, false); |
d745562c IC |
98 | } |
99 | } | |
100 | ||
d68d82af AN |
101 | return NOTIFY_DONE; |
102 | } | |
103 | ||
104 | static int __init setup_vcpu_hotplug_event(void) | |
105 | { | |
106 | static struct notifier_block xsn_cpu = { | |
107 | .notifier_call = setup_cpu_watcher }; | |
108 | ||
a314e3eb | 109 | #ifdef CONFIG_X86 |
2a7197f0 | 110 | if (!xen_pv_domain() && !xen_pvh_domain()) |
a314e3eb SS |
111 | #else |
112 | if (!xen_domain()) | |
113 | #endif | |
d68d82af AN |
114 | return -ENODEV; |
115 | ||
116 | register_xenstore_notifier(&xsn_cpu); | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
121 | arch_initcall(setup_vcpu_hotplug_event); | |
122 |