Blame view

drivers/xen/cpu_hotplug.c 2.02 KB
d68d82afd   Alex Nixon   xen: implement CP...
1
  #include <linux/notifier.h>
1ccbf5344   Jeremy Fitzhardinge   xen: move Xen-tes...
2
  #include <xen/xen.h>
d68d82afd   Alex Nixon   xen: implement CP...
3
  #include <xen/xenbus.h>
bb8985586   Al Viro   x86, um: ... and ...
4
  #include <asm/xen/hypervisor.h>
d68d82afd   Alex Nixon   xen: implement CP...
5
6
7
8
9
10
  #include <asm/cpu.h>
  
  static void enable_hotplug_cpu(int cpu)
  {
  	if (!cpu_present(cpu))
  		arch_register_cpu(cpu);
d680eb8bc   Rusty Russell   cpumask: make Xen...
11
  	set_cpu_present(cpu, true);
d68d82afd   Alex Nixon   xen: implement CP...
12
13
14
15
16
17
  }
  
  static void disable_hotplug_cpu(int cpu)
  {
  	if (cpu_present(cpu))
  		arch_unregister_cpu(cpu);
d680eb8bc   Rusty Russell   cpumask: make Xen...
18
  	set_cpu_present(cpu, false);
d68d82afd   Alex Nixon   xen: implement CP...
19
  }
d745562cc   Ian Campbell   xen: honour VCPU ...
20
  static int vcpu_online(unsigned int cpu)
d68d82afd   Alex Nixon   xen: implement CP...
21
22
23
  {
  	int err;
  	char dir[32], state[32];
d68d82afd   Alex Nixon   xen: implement CP...
24
25
26
27
28
  	sprintf(dir, "cpu/%u", cpu);
  	err = xenbus_scanf(XBT_NIL, dir, "availability", "%s", state);
  	if (err != 1) {
  		printk(KERN_ERR "XENBUS: Unable to read cpu state
  ");
d745562cc   Ian Campbell   xen: honour VCPU ...
29
  		return err;
d68d82afd   Alex Nixon   xen: implement CP...
30
  	}
d745562cc   Ian Campbell   xen: honour VCPU ...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  	if (strcmp(state, "online") == 0)
  		return 1;
  	else if (strcmp(state, "offline") == 0)
  		return 0;
  
  	printk(KERN_ERR "XENBUS: unknown state(%s) on CPU%d
  ", state, cpu);
  	return -EINVAL;
  }
  static void vcpu_hotplug(unsigned int cpu)
  {
  	if (!cpu_possible(cpu))
  		return;
  
  	switch (vcpu_online(cpu)) {
  	case 1:
d68d82afd   Alex Nixon   xen: implement CP...
47
  		enable_hotplug_cpu(cpu);
d745562cc   Ian Campbell   xen: honour VCPU ...
48
49
  		break;
  	case 0:
d68d82afd   Alex Nixon   xen: implement CP...
50
51
  		(void)cpu_down(cpu);
  		disable_hotplug_cpu(cpu);
d745562cc   Ian Campbell   xen: honour VCPU ...
52
53
54
  		break;
  	default:
  		break;
d68d82afd   Alex Nixon   xen: implement CP...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  	}
  }
  
  static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
  					const char **vec, unsigned int len)
  {
  	unsigned int cpu;
  	char *cpustr;
  	const char *node = vec[XS_WATCH_PATH];
  
  	cpustr = strstr(node, "cpu/");
  	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)
  {
d745562cc   Ian Campbell   xen: honour VCPU ...
75
  	int cpu;
d68d82afd   Alex Nixon   xen: implement CP...
76
77
78
79
80
  	static struct xenbus_watch cpu_watch = {
  		.node = "cpu",
  		.callback = handle_vcpu_hotplug_event};
  
  	(void)register_xenbus_watch(&cpu_watch);
d745562cc   Ian Campbell   xen: honour VCPU ...
81
82
83
  	for_each_possible_cpu(cpu) {
  		if (vcpu_online(cpu) == 0) {
  			(void)cpu_down(cpu);
d7d3756c5   Rusty Russell   cpumask: Use mode...
84
  			set_cpu_present(cpu, false);
d745562cc   Ian Campbell   xen: honour VCPU ...
85
86
  		}
  	}
d68d82afd   Alex Nixon   xen: implement CP...
87
88
89
90
91
92
93
  	return NOTIFY_DONE;
  }
  
  static int __init setup_vcpu_hotplug_event(void)
  {
  	static struct notifier_block xsn_cpu = {
  		.notifier_call = setup_cpu_watcher };
5c51c637e   Alex Nixon   Xen: fix cpu_hotp...
94
  	if (!xen_pv_domain())
d68d82afd   Alex Nixon   xen: implement CP...
95
96
97
98
99
100
101
102
  		return -ENODEV;
  
  	register_xenstore_notifier(&xsn_cpu);
  
  	return 0;
  }
  
  arch_initcall(setup_vcpu_hotplug_event);