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