xref: /openbmc/linux/drivers/xen/cpu_hotplug.c (revision 283c0972d53769ee44750cad4c27e3f5fa26ec1f)
1*283c0972SJoe Perches #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
2*283c0972SJoe Perches 
3d68d82afSAlex Nixon #include <linux/notifier.h>
4d68d82afSAlex Nixon 
51ccbf534SJeremy Fitzhardinge #include <xen/xen.h>
6d68d82afSAlex Nixon #include <xen/xenbus.h>
7d68d82afSAlex Nixon 
8bb898558SAl Viro #include <asm/xen/hypervisor.h>
9d68d82afSAlex Nixon #include <asm/cpu.h>
10d68d82afSAlex Nixon 
11d68d82afSAlex Nixon static void enable_hotplug_cpu(int cpu)
12d68d82afSAlex Nixon {
13d68d82afSAlex Nixon 	if (!cpu_present(cpu))
14d68d82afSAlex Nixon 		arch_register_cpu(cpu);
15d68d82afSAlex Nixon 
16d680eb8bSRusty Russell 	set_cpu_present(cpu, true);
17d68d82afSAlex Nixon }
18d68d82afSAlex Nixon 
19d68d82afSAlex Nixon static void disable_hotplug_cpu(int cpu)
20d68d82afSAlex Nixon {
21d68d82afSAlex Nixon 	if (cpu_present(cpu))
22d68d82afSAlex Nixon 		arch_unregister_cpu(cpu);
23d68d82afSAlex Nixon 
24d680eb8bSRusty Russell 	set_cpu_present(cpu, false);
25d68d82afSAlex Nixon }
26d68d82afSAlex Nixon 
27d745562cSIan Campbell static int vcpu_online(unsigned int cpu)
28d68d82afSAlex Nixon {
29d68d82afSAlex Nixon 	int err;
30e5c702d3SJan Beulich 	char dir[16], state[16];
31d68d82afSAlex Nixon 
32d68d82afSAlex Nixon 	sprintf(dir, "cpu/%u", cpu);
33e5c702d3SJan Beulich 	err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
34d68d82afSAlex Nixon 	if (err != 1) {
355b02aa1eSKonrad Rzeszutek Wilk 		if (!xen_initial_domain())
36*283c0972SJoe Perches 			pr_err("Unable to read cpu state\n");
37d745562cSIan Campbell 		return err;
38d68d82afSAlex Nixon 	}
39d68d82afSAlex Nixon 
40d745562cSIan Campbell 	if (strcmp(state, "online") == 0)
41d745562cSIan Campbell 		return 1;
42d745562cSIan Campbell 	else if (strcmp(state, "offline") == 0)
43d745562cSIan Campbell 		return 0;
44d745562cSIan Campbell 
45*283c0972SJoe Perches 	pr_err("unknown state(%s) on CPU%d\n", state, cpu);
46d745562cSIan Campbell 	return -EINVAL;
47d745562cSIan Campbell }
48d745562cSIan Campbell static void vcpu_hotplug(unsigned int cpu)
49d745562cSIan Campbell {
50d745562cSIan Campbell 	if (!cpu_possible(cpu))
51d745562cSIan Campbell 		return;
52d745562cSIan Campbell 
53d745562cSIan Campbell 	switch (vcpu_online(cpu)) {
54d745562cSIan Campbell 	case 1:
55d68d82afSAlex Nixon 		enable_hotplug_cpu(cpu);
56d745562cSIan Campbell 		break;
57d745562cSIan Campbell 	case 0:
58d68d82afSAlex Nixon 		(void)cpu_down(cpu);
59d68d82afSAlex Nixon 		disable_hotplug_cpu(cpu);
60d745562cSIan Campbell 		break;
61d745562cSIan Campbell 	default:
62d745562cSIan Campbell 		break;
63d68d82afSAlex Nixon 	}
64d68d82afSAlex Nixon }
65d68d82afSAlex Nixon 
66d68d82afSAlex Nixon static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
67d68d82afSAlex Nixon 					const char **vec, unsigned int len)
68d68d82afSAlex Nixon {
69d68d82afSAlex Nixon 	unsigned int cpu;
70d68d82afSAlex Nixon 	char *cpustr;
71d68d82afSAlex Nixon 	const char *node = vec[XS_WATCH_PATH];
72d68d82afSAlex Nixon 
73d68d82afSAlex Nixon 	cpustr = strstr(node, "cpu/");
74d68d82afSAlex Nixon 	if (cpustr != NULL) {
75d68d82afSAlex Nixon 		sscanf(cpustr, "cpu/%u", &cpu);
76d68d82afSAlex Nixon 		vcpu_hotplug(cpu);
77d68d82afSAlex Nixon 	}
78d68d82afSAlex Nixon }
79d68d82afSAlex Nixon 
80d68d82afSAlex Nixon static int setup_cpu_watcher(struct notifier_block *notifier,
81d68d82afSAlex Nixon 			      unsigned long event, void *data)
82d68d82afSAlex Nixon {
83d745562cSIan Campbell 	int cpu;
84d68d82afSAlex Nixon 	static struct xenbus_watch cpu_watch = {
85d68d82afSAlex Nixon 		.node = "cpu",
86d68d82afSAlex Nixon 		.callback = handle_vcpu_hotplug_event};
87d68d82afSAlex Nixon 
88d68d82afSAlex Nixon 	(void)register_xenbus_watch(&cpu_watch);
89d68d82afSAlex Nixon 
90d745562cSIan Campbell 	for_each_possible_cpu(cpu) {
91d745562cSIan Campbell 		if (vcpu_online(cpu) == 0) {
92d745562cSIan Campbell 			(void)cpu_down(cpu);
93d7d3756cSRusty Russell 			set_cpu_present(cpu, false);
94d745562cSIan Campbell 		}
95d745562cSIan Campbell 	}
96d745562cSIan Campbell 
97d68d82afSAlex Nixon 	return NOTIFY_DONE;
98d68d82afSAlex Nixon }
99d68d82afSAlex Nixon 
100d68d82afSAlex Nixon static int __init setup_vcpu_hotplug_event(void)
101d68d82afSAlex Nixon {
102d68d82afSAlex Nixon 	static struct notifier_block xsn_cpu = {
103d68d82afSAlex Nixon 		.notifier_call = setup_cpu_watcher };
104d68d82afSAlex Nixon 
1055c51c637SAlex Nixon 	if (!xen_pv_domain())
106d68d82afSAlex Nixon 		return -ENODEV;
107d68d82afSAlex Nixon 
108d68d82afSAlex Nixon 	register_xenstore_notifier(&xsn_cpu);
109d68d82afSAlex Nixon 
110d68d82afSAlex Nixon 	return 0;
111d68d82afSAlex Nixon }
112d68d82afSAlex Nixon 
113d68d82afSAlex Nixon arch_initcall(setup_vcpu_hotplug_event);
114d68d82afSAlex Nixon 
115