cpu_hotplug.c 2.2 KB
Newer Older
J
Joe Perches 已提交
1 2
#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt

A
Alex Nixon 已提交
3 4
#include <linux/notifier.h>

5
#include <xen/xen.h>
A
Alex Nixon 已提交
6 7
#include <xen/xenbus.h>

A
Al Viro 已提交
8
#include <asm/xen/hypervisor.h>
A
Alex Nixon 已提交
9 10 11 12 13
#include <asm/cpu.h>

static void enable_hotplug_cpu(int cpu)
{
	if (!cpu_present(cpu))
S
Stefano Stabellini 已提交
14
		xen_arch_register_cpu(cpu);
A
Alex Nixon 已提交
15

16
	set_cpu_present(cpu, true);
A
Alex Nixon 已提交
17 18 19 20
}

static void disable_hotplug_cpu(int cpu)
{
21 22 23 24 25
	if (cpu_online(cpu)) {
		lock_device_hotplug();
		device_offline(get_cpu_device(cpu));
		unlock_device_hotplug();
	}
A
Alex Nixon 已提交
26
	if (cpu_present(cpu))
S
Stefano Stabellini 已提交
27
		xen_arch_unregister_cpu(cpu);
A
Alex Nixon 已提交
28

29
	set_cpu_present(cpu, false);
A
Alex Nixon 已提交
30 31
}

32
static int vcpu_online(unsigned int cpu)
A
Alex Nixon 已提交
33 34
{
	int err;
35
	char dir[16], state[16];
A
Alex Nixon 已提交
36 37

	sprintf(dir, "cpu/%u", cpu);
38
	err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
A
Alex Nixon 已提交
39
	if (err != 1) {
40
		if (!xen_initial_domain())
J
Joe Perches 已提交
41
			pr_err("Unable to read cpu state\n");
42
		return err;
A
Alex Nixon 已提交
43 44
	}

45 46 47 48 49
	if (strcmp(state, "online") == 0)
		return 1;
	else if (strcmp(state, "offline") == 0)
		return 0;

J
Joe Perches 已提交
50
	pr_err("unknown state(%s) on CPU%d\n", state, cpu);
51 52 53 54 55 56 57 58 59
	return -EINVAL;
}
static void vcpu_hotplug(unsigned int cpu)
{
	if (!cpu_possible(cpu))
		return;

	switch (vcpu_online(cpu)) {
	case 1:
A
Alex Nixon 已提交
60
		enable_hotplug_cpu(cpu);
61 62
		break;
	case 0:
A
Alex Nixon 已提交
63
		disable_hotplug_cpu(cpu);
64 65 66
		break;
	default:
		break;
A
Alex Nixon 已提交
67 68 69 70
	}
}

static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
71
				      const char *path, const char *token)
A
Alex Nixon 已提交
72 73 74 75
{
	unsigned int cpu;
	char *cpustr;

76
	cpustr = strstr(path, "cpu/");
A
Alex Nixon 已提交
77 78 79 80 81 82 83 84 85
	if (cpustr != NULL) {
		sscanf(cpustr, "cpu/%u", &cpu);
		vcpu_hotplug(cpu);
	}
}

static int setup_cpu_watcher(struct notifier_block *notifier,
			      unsigned long event, void *data)
{
86
	int cpu;
A
Alex Nixon 已提交
87 88 89 90 91 92
	static struct xenbus_watch cpu_watch = {
		.node = "cpu",
		.callback = handle_vcpu_hotplug_event};

	(void)register_xenbus_watch(&cpu_watch);

93 94 95
	for_each_possible_cpu(cpu) {
		if (vcpu_online(cpu) == 0) {
			(void)cpu_down(cpu);
96
			set_cpu_present(cpu, false);
97 98 99
		}
	}

A
Alex Nixon 已提交
100 101 102 103 104 105 106 107
	return NOTIFY_DONE;
}

static int __init setup_vcpu_hotplug_event(void)
{
	static struct notifier_block xsn_cpu = {
		.notifier_call = setup_cpu_watcher };

S
Stefano Stabellini 已提交
108
#ifdef CONFIG_X86
B
Boris Ostrovsky 已提交
109
	if (!xen_pv_domain() && !xen_pvh_domain())
S
Stefano Stabellini 已提交
110 111 112
#else
	if (!xen_domain())
#endif
A
Alex Nixon 已提交
113 114 115 116 117 118 119 120 121
		return -ENODEV;

	register_xenstore_notifier(&xsn_cpu);

	return 0;
}

arch_initcall(setup_vcpu_hotplug_event);