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