xref: /openbmc/linux/drivers/xen/cpu_hotplug.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2283c0972SJoe Perches #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
3283c0972SJoe Perches 
4d68d82afSAlex Nixon #include <linux/notifier.h>
5d68d82afSAlex Nixon 
61ccbf534SJeremy Fitzhardinge #include <xen/xen.h>
7d68d82afSAlex Nixon #include <xen/xenbus.h>
8d68d82afSAlex Nixon 
9bb898558SAl Viro #include <asm/xen/hypervisor.h>
10d68d82afSAlex Nixon #include <asm/cpu.h>
11d68d82afSAlex Nixon 
enable_hotplug_cpu(int cpu)12d68d82afSAlex Nixon static void enable_hotplug_cpu(int cpu)
13d68d82afSAlex Nixon {
14d68d82afSAlex Nixon 	if (!cpu_present(cpu))
15a314e3ebSStefano Stabellini 		xen_arch_register_cpu(cpu);
16d68d82afSAlex Nixon 
17d680eb8bSRusty Russell 	set_cpu_present(cpu, true);
18d68d82afSAlex Nixon }
19d68d82afSAlex Nixon 
disable_hotplug_cpu(int cpu)20d68d82afSAlex Nixon static void disable_hotplug_cpu(int cpu)
21d68d82afSAlex Nixon {
223366cdb6SOlaf Hering 	if (!cpu_is_hotpluggable(cpu))
233366cdb6SOlaf Hering 		return;
241c7a6213SStefano Stabellini 	lock_device_hotplug();
253366cdb6SOlaf Hering 	if (cpu_online(cpu))
261c7a6213SStefano Stabellini 		device_offline(get_cpu_device(cpu));
273366cdb6SOlaf Hering 	if (!cpu_online(cpu) && cpu_present(cpu)) {
28a314e3ebSStefano Stabellini 		xen_arch_unregister_cpu(cpu);
29d680eb8bSRusty Russell 		set_cpu_present(cpu, false);
30d68d82afSAlex Nixon 	}
313366cdb6SOlaf Hering 	unlock_device_hotplug();
323366cdb6SOlaf Hering }
33d68d82afSAlex Nixon 
vcpu_online(unsigned int cpu)34d745562cSIan Campbell static int vcpu_online(unsigned int cpu)
35d68d82afSAlex Nixon {
36d68d82afSAlex Nixon 	int err;
37e5c702d3SJan Beulich 	char dir[16], state[16];
38d68d82afSAlex Nixon 
39d68d82afSAlex Nixon 	sprintf(dir, "cpu/%u", cpu);
40e5c702d3SJan Beulich 	err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
41d68d82afSAlex Nixon 	if (err != 1) {
425b02aa1eSKonrad Rzeszutek Wilk 		if (!xen_initial_domain())
43283c0972SJoe Perches 			pr_err("Unable to read cpu state\n");
44d745562cSIan Campbell 		return err;
45d68d82afSAlex Nixon 	}
46d68d82afSAlex Nixon 
47d745562cSIan Campbell 	if (strcmp(state, "online") == 0)
48d745562cSIan Campbell 		return 1;
49d745562cSIan Campbell 	else if (strcmp(state, "offline") == 0)
50d745562cSIan Campbell 		return 0;
51d745562cSIan Campbell 
52283c0972SJoe Perches 	pr_err("unknown state(%s) on CPU%d\n", state, cpu);
53d745562cSIan Campbell 	return -EINVAL;
54d745562cSIan Campbell }
vcpu_hotplug(unsigned int cpu)55d745562cSIan Campbell static void vcpu_hotplug(unsigned int cpu)
56d745562cSIan Campbell {
5720167609SDan Carpenter 	if (cpu >= nr_cpu_ids || !cpu_possible(cpu))
58d745562cSIan Campbell 		return;
59d745562cSIan Campbell 
60d745562cSIan Campbell 	switch (vcpu_online(cpu)) {
61d745562cSIan Campbell 	case 1:
62d68d82afSAlex Nixon 		enable_hotplug_cpu(cpu);
63d745562cSIan Campbell 		break;
64d745562cSIan Campbell 	case 0:
65d68d82afSAlex Nixon 		disable_hotplug_cpu(cpu);
66d745562cSIan Campbell 		break;
67d745562cSIan Campbell 	default:
68d745562cSIan Campbell 		break;
69d68d82afSAlex Nixon 	}
70d68d82afSAlex Nixon }
71d68d82afSAlex Nixon 
handle_vcpu_hotplug_event(struct xenbus_watch * watch,const char * path,const char * token)72d68d82afSAlex Nixon static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
735584ea25SJuergen Gross 				      const char *path, const char *token)
74d68d82afSAlex Nixon {
75d68d82afSAlex Nixon 	unsigned int cpu;
76d68d82afSAlex Nixon 	char *cpustr;
77d68d82afSAlex Nixon 
785584ea25SJuergen Gross 	cpustr = strstr(path, "cpu/");
79d68d82afSAlex Nixon 	if (cpustr != NULL) {
80d68d82afSAlex Nixon 		sscanf(cpustr, "cpu/%u", &cpu);
81d68d82afSAlex Nixon 		vcpu_hotplug(cpu);
82d68d82afSAlex Nixon 	}
83d68d82afSAlex Nixon }
84d68d82afSAlex Nixon 
setup_cpu_watcher(struct notifier_block * notifier,unsigned long event,void * data)85d68d82afSAlex Nixon static int setup_cpu_watcher(struct notifier_block *notifier,
86d68d82afSAlex Nixon 			      unsigned long event, void *data)
87d68d82afSAlex Nixon {
88d745562cSIan Campbell 	int cpu;
89d68d82afSAlex Nixon 	static struct xenbus_watch cpu_watch = {
90d68d82afSAlex Nixon 		.node = "cpu",
91d68d82afSAlex Nixon 		.callback = handle_vcpu_hotplug_event};
92d68d82afSAlex Nixon 
93d68d82afSAlex Nixon 	(void)register_xenbus_watch(&cpu_watch);
94d68d82afSAlex Nixon 
95d745562cSIan Campbell 	for_each_possible_cpu(cpu) {
96*c54b071cSBoris Ostrovsky 		if (vcpu_online(cpu) == 0)
97*c54b071cSBoris Ostrovsky 			disable_hotplug_cpu(cpu);
98d745562cSIan Campbell 	}
99d745562cSIan Campbell 
100d68d82afSAlex Nixon 	return NOTIFY_DONE;
101d68d82afSAlex Nixon }
102d68d82afSAlex Nixon 
setup_vcpu_hotplug_event(void)103d68d82afSAlex Nixon static int __init setup_vcpu_hotplug_event(void)
104d68d82afSAlex Nixon {
105d68d82afSAlex Nixon 	static struct notifier_block xsn_cpu = {
106d68d82afSAlex Nixon 		.notifier_call = setup_cpu_watcher };
107d68d82afSAlex Nixon 
108a314e3ebSStefano Stabellini #ifdef CONFIG_X86
1092a7197f0SBoris Ostrovsky 	if (!xen_pv_domain() && !xen_pvh_domain())
110a314e3ebSStefano Stabellini #else
111a314e3ebSStefano Stabellini 	if (!xen_domain())
112a314e3ebSStefano Stabellini #endif
113d68d82afSAlex Nixon 		return -ENODEV;
114d68d82afSAlex Nixon 
115d68d82afSAlex Nixon 	register_xenstore_notifier(&xsn_cpu);
116d68d82afSAlex Nixon 
117d68d82afSAlex Nixon 	return 0;
118d68d82afSAlex Nixon }
119d68d82afSAlex Nixon 
120*c54b071cSBoris Ostrovsky late_initcall(setup_vcpu_hotplug_event);
121d68d82afSAlex Nixon 
122