blob: d4265c8ebb22a24b1ab8794ab79b592793d14908 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Joe Perches283c0972013-06-28 03:21:41 -07002#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
3
Alex Nixond68d82a2008-08-22 11:52:15 +01004#include <linux/notifier.h>
5
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -07006#include <xen/xen.h>
Alex Nixond68d82a2008-08-22 11:52:15 +01007#include <xen/xenbus.h>
8
Al Virobb898552008-08-17 21:05:42 -04009#include <asm/xen/hypervisor.h>
Alex Nixond68d82a2008-08-22 11:52:15 +010010#include <asm/cpu.h>
11
12static void enable_hotplug_cpu(int cpu)
13{
14 if (!cpu_present(cpu))
Stefano Stabellinia314e3e2015-10-22 16:20:46 +000015 xen_arch_register_cpu(cpu);
Alex Nixond68d82a2008-08-22 11:52:15 +010016
Rusty Russelld680eb82009-03-13 14:49:56 +103017 set_cpu_present(cpu, true);
Alex Nixond68d82a2008-08-22 11:52:15 +010018}
19
20static void disable_hotplug_cpu(int cpu)
21{
Stefano Stabellini1c7a6212015-10-22 16:21:46 +000022 if (cpu_online(cpu)) {
23 lock_device_hotplug();
24 device_offline(get_cpu_device(cpu));
25 unlock_device_hotplug();
26 }
Alex Nixond68d82a2008-08-22 11:52:15 +010027 if (cpu_present(cpu))
Stefano Stabellinia314e3e2015-10-22 16:20:46 +000028 xen_arch_unregister_cpu(cpu);
Alex Nixond68d82a2008-08-22 11:52:15 +010029
Rusty Russelld680eb82009-03-13 14:49:56 +103030 set_cpu_present(cpu, false);
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{
56 if (!cpu_possible(cpu))
57 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,
Juergen Gross5584ea22017-02-09 14:39:57 +010072 const char *path, const char *token)
Alex Nixond68d82a2008-08-22 11:52:15 +010073{
74 unsigned int cpu;
75 char *cpustr;
Alex Nixond68d82a2008-08-22 11:52:15 +010076
Juergen Gross5584ea22017-02-09 14:39:57 +010077 cpustr = strstr(path, "cpu/");
Alex Nixond68d82a2008-08-22 11:52:15 +010078 if (cpustr != NULL) {
79 sscanf(cpustr, "cpu/%u", &cpu);
80 vcpu_hotplug(cpu);
81 }
82}
83
84static int setup_cpu_watcher(struct notifier_block *notifier,
85 unsigned long event, void *data)
86{
Ian Campbelld7455622009-04-02 13:24:28 +010087 int cpu;
Alex Nixond68d82a2008-08-22 11:52:15 +010088 static struct xenbus_watch cpu_watch = {
89 .node = "cpu",
90 .callback = handle_vcpu_hotplug_event};
91
92 (void)register_xenbus_watch(&cpu_watch);
93
Ian Campbelld7455622009-04-02 13:24:28 +010094 for_each_possible_cpu(cpu) {
95 if (vcpu_online(cpu) == 0) {
96 (void)cpu_down(cpu);
Rusty Russelld7d37562009-11-03 14:58:38 +103097 set_cpu_present(cpu, false);
Ian Campbelld7455622009-04-02 13:24:28 +010098 }
99 }
100
Alex Nixond68d82a2008-08-22 11:52:15 +0100101 return NOTIFY_DONE;
102}
103
104static int __init setup_vcpu_hotplug_event(void)
105{
106 static struct notifier_block xsn_cpu = {
107 .notifier_call = setup_cpu_watcher };
108
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000109#ifdef CONFIG_X86
Boris Ostrovsky2a7197f2017-02-06 10:58:05 -0500110 if (!xen_pv_domain() && !xen_pvh_domain())
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000111#else
112 if (!xen_domain())
113#endif
Alex Nixond68d82a2008-08-22 11:52:15 +0100114 return -ENODEV;
115
116 register_xenstore_notifier(&xsn_cpu);
117
118 return 0;
119}
120
121arch_initcall(setup_vcpu_hotplug_event);
122