blob: 17054d695411788da16eb4fd4f5ea08d1c9d4ad2 [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{
Olaf Heringa571f892018-09-07 16:31:35 +020021 if (!cpu_is_hotpluggable(cpu))
22 return;
23 lock_device_hotplug();
24 if (cpu_online(cpu))
Stefano Stabellini1c7a6212015-10-22 16:21:46 +000025 device_offline(get_cpu_device(cpu));
Olaf Heringa571f892018-09-07 16:31:35 +020026 if (!cpu_online(cpu) && cpu_present(cpu)) {
Stefano Stabellinia314e3e2015-10-22 16:20:46 +000027 xen_arch_unregister_cpu(cpu);
Olaf Heringa571f892018-09-07 16:31:35 +020028 set_cpu_present(cpu, false);
29 }
30 unlock_device_hotplug();
Alex Nixond68d82a2008-08-22 11:52:15 +010031}
32
Ian Campbelld7455622009-04-02 13:24:28 +010033static int vcpu_online(unsigned int cpu)
Alex Nixond68d82a2008-08-22 11:52:15 +010034{
35 int err;
Jan Beuliche5c702d2013-01-15 13:31:43 +000036 char dir[16], state[16];
Alex Nixond68d82a2008-08-22 11:52:15 +010037
Alex Nixond68d82a2008-08-22 11:52:15 +010038 sprintf(dir, "cpu/%u", cpu);
Jan Beuliche5c702d2013-01-15 13:31:43 +000039 err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
Alex Nixond68d82a2008-08-22 11:52:15 +010040 if (err != 1) {
Konrad Rzeszutek Wilk5b02aa12012-02-01 16:07:41 -050041 if (!xen_initial_domain())
Joe Perches283c0972013-06-28 03:21:41 -070042 pr_err("Unable to read cpu state\n");
Ian Campbelld7455622009-04-02 13:24:28 +010043 return err;
Alex Nixond68d82a2008-08-22 11:52:15 +010044 }
45
Ian Campbelld7455622009-04-02 13:24:28 +010046 if (strcmp(state, "online") == 0)
47 return 1;
48 else if (strcmp(state, "offline") == 0)
49 return 0;
50
Joe Perches283c0972013-06-28 03:21:41 -070051 pr_err("unknown state(%s) on CPU%d\n", state, cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010052 return -EINVAL;
53}
54static void vcpu_hotplug(unsigned int cpu)
55{
Dan Carpenter93501e72019-03-07 08:41:22 +030056 if (cpu >= nr_cpu_ids || !cpu_possible(cpu))
Ian Campbelld7455622009-04-02 13:24:28 +010057 return;
58
59 switch (vcpu_online(cpu)) {
60 case 1:
Alex Nixond68d82a2008-08-22 11:52:15 +010061 enable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010062 break;
63 case 0:
Alex Nixond68d82a2008-08-22 11:52:15 +010064 disable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010065 break;
66 default:
67 break;
Alex Nixond68d82a2008-08-22 11:52:15 +010068 }
69}
70
71static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
72 const char **vec, unsigned int len)
73{
74 unsigned int cpu;
75 char *cpustr;
76 const char *node = vec[XS_WATCH_PATH];
77
78 cpustr = strstr(node, "cpu/");
79 if (cpustr != NULL) {
80 sscanf(cpustr, "cpu/%u", &cpu);
81 vcpu_hotplug(cpu);
82 }
83}
84
85static int setup_cpu_watcher(struct notifier_block *notifier,
86 unsigned long event, void *data)
87{
Ian Campbelld7455622009-04-02 13:24:28 +010088 int cpu;
Alex Nixond68d82a2008-08-22 11:52:15 +010089 static struct xenbus_watch cpu_watch = {
90 .node = "cpu",
91 .callback = handle_vcpu_hotplug_event};
92
93 (void)register_xenbus_watch(&cpu_watch);
94
Ian Campbelld7455622009-04-02 13:24:28 +010095 for_each_possible_cpu(cpu) {
96 if (vcpu_online(cpu) == 0) {
97 (void)cpu_down(cpu);
Rusty Russelld7d37562009-11-03 14:58:38 +103098 set_cpu_present(cpu, false);
Ian Campbelld7455622009-04-02 13:24:28 +010099 }
100 }
101
Alex Nixond68d82a2008-08-22 11:52:15 +0100102 return NOTIFY_DONE;
103}
104
105static int __init setup_vcpu_hotplug_event(void)
106{
107 static struct notifier_block xsn_cpu = {
108 .notifier_call = setup_cpu_watcher };
109
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000110#ifdef CONFIG_X86
Alex Nixon5c51c632008-09-03 14:30:21 +0100111 if (!xen_pv_domain())
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000112#else
113 if (!xen_domain())
114#endif
Alex Nixond68d82a2008-08-22 11:52:15 +0100115 return -ENODEV;
116
117 register_xenstore_notifier(&xsn_cpu);
118
119 return 0;
120}
121
122arch_initcall(setup_vcpu_hotplug_event);
123