blob: b96b11e2b571d17e202c72f569a2401804717911 [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{
Olaf Hering3366cdb2018-09-07 16:31:35 +020022 if (!cpu_is_hotpluggable(cpu))
23 return;
24 lock_device_hotplug();
25 if (cpu_online(cpu))
Stefano Stabellini1c7a6212015-10-22 16:21:46 +000026 device_offline(get_cpu_device(cpu));
Olaf Hering3366cdb2018-09-07 16:31:35 +020027 if (!cpu_online(cpu) && cpu_present(cpu)) {
Stefano Stabellinia314e3e2015-10-22 16:20:46 +000028 xen_arch_unregister_cpu(cpu);
Olaf Hering3366cdb2018-09-07 16:31:35 +020029 set_cpu_present(cpu, false);
30 }
31 unlock_device_hotplug();
Alex Nixond68d82a2008-08-22 11:52:15 +010032}
33
Ian Campbelld7455622009-04-02 13:24:28 +010034static int vcpu_online(unsigned int cpu)
Alex Nixond68d82a2008-08-22 11:52:15 +010035{
36 int err;
Jan Beuliche5c702d2013-01-15 13:31:43 +000037 char dir[16], state[16];
Alex Nixond68d82a2008-08-22 11:52:15 +010038
Alex Nixond68d82a2008-08-22 11:52:15 +010039 sprintf(dir, "cpu/%u", cpu);
Jan Beuliche5c702d2013-01-15 13:31:43 +000040 err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
Alex Nixond68d82a2008-08-22 11:52:15 +010041 if (err != 1) {
Konrad Rzeszutek Wilk5b02aa12012-02-01 16:07:41 -050042 if (!xen_initial_domain())
Joe Perches283c0972013-06-28 03:21:41 -070043 pr_err("Unable to read cpu state\n");
Ian Campbelld7455622009-04-02 13:24:28 +010044 return err;
Alex Nixond68d82a2008-08-22 11:52:15 +010045 }
46
Ian Campbelld7455622009-04-02 13:24:28 +010047 if (strcmp(state, "online") == 0)
48 return 1;
49 else if (strcmp(state, "offline") == 0)
50 return 0;
51
Joe Perches283c0972013-06-28 03:21:41 -070052 pr_err("unknown state(%s) on CPU%d\n", state, cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010053 return -EINVAL;
54}
55static void vcpu_hotplug(unsigned int cpu)
56{
Dan Carpenter20167602019-03-07 08:41:22 +030057 if (cpu >= nr_cpu_ids || !cpu_possible(cpu))
Ian Campbelld7455622009-04-02 13:24:28 +010058 return;
59
60 switch (vcpu_online(cpu)) {
61 case 1:
Alex Nixond68d82a2008-08-22 11:52:15 +010062 enable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010063 break;
64 case 0:
Alex Nixond68d82a2008-08-22 11:52:15 +010065 disable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010066 break;
67 default:
68 break;
Alex Nixond68d82a2008-08-22 11:52:15 +010069 }
70}
71
72static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
Juergen Gross5584ea22017-02-09 14:39:57 +010073 const char *path, const char *token)
Alex Nixond68d82a2008-08-22 11:52:15 +010074{
75 unsigned int cpu;
76 char *cpustr;
Alex Nixond68d82a2008-08-22 11:52:15 +010077
Juergen Gross5584ea22017-02-09 14:39:57 +010078 cpustr = strstr(path, "cpu/");
Alex Nixond68d82a2008-08-22 11:52:15 +010079 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) {
Boris Ostrovskyc54b0712020-05-08 18:28:43 -040096 if (vcpu_online(cpu) == 0)
97 disable_hotplug_cpu(cpu);
Ian Campbelld7455622009-04-02 13:24:28 +010098 }
99
Alex Nixond68d82a2008-08-22 11:52:15 +0100100 return NOTIFY_DONE;
101}
102
103static int __init setup_vcpu_hotplug_event(void)
104{
105 static struct notifier_block xsn_cpu = {
106 .notifier_call = setup_cpu_watcher };
107
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000108#ifdef CONFIG_X86
Boris Ostrovsky2a7197f2017-02-06 10:58:05 -0500109 if (!xen_pv_domain() && !xen_pvh_domain())
Stefano Stabellinia314e3e2015-10-22 16:20:46 +0000110#else
111 if (!xen_domain())
112#endif
Alex Nixond68d82a2008-08-22 11:52:15 +0100113 return -ENODEV;
114
115 register_xenstore_notifier(&xsn_cpu);
116
117 return 0;
118}
119
Boris Ostrovskyc54b0712020-05-08 18:28:43 -0400120late_initcall(setup_vcpu_hotplug_event);
Alex Nixond68d82a2008-08-22 11:52:15 +0100121