Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 1 | #include <linux/notifier.h> |
| 2 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 3 | #include <xen/xen.h> |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 4 | #include <xen/xenbus.h> |
| 5 | |
Al Viro | bb89855 | 2008-08-17 21:05:42 -0400 | [diff] [blame] | 6 | #include <asm/xen/hypervisor.h> |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 7 | #include <asm/cpu.h> |
| 8 | |
| 9 | static void enable_hotplug_cpu(int cpu) |
| 10 | { |
| 11 | if (!cpu_present(cpu)) |
| 12 | arch_register_cpu(cpu); |
| 13 | |
Rusty Russell | d680eb8 | 2009-03-13 14:49:56 +1030 | [diff] [blame] | 14 | set_cpu_present(cpu, true); |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | static void disable_hotplug_cpu(int cpu) |
| 18 | { |
| 19 | if (cpu_present(cpu)) |
| 20 | arch_unregister_cpu(cpu); |
| 21 | |
Rusty Russell | d680eb8 | 2009-03-13 14:49:56 +1030 | [diff] [blame] | 22 | set_cpu_present(cpu, false); |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 23 | } |
| 24 | |
Ian Campbell | d745562 | 2009-04-02 13:24:28 +0100 | [diff] [blame] | 25 | static int vcpu_online(unsigned int cpu) |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 26 | { |
| 27 | int err; |
| 28 | char dir[32], state[32]; |
| 29 | |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 30 | sprintf(dir, "cpu/%u", cpu); |
| 31 | err = xenbus_scanf(XBT_NIL, dir, "availability", "%s", state); |
| 32 | if (err != 1) { |
Konrad Rzeszutek Wilk | 5b02aa1 | 2012-02-01 16:07:41 -0500 | [diff] [blame] | 33 | if (!xen_initial_domain()) |
| 34 | printk(KERN_ERR "XENBUS: Unable to read cpu state\n"); |
Ian Campbell | d745562 | 2009-04-02 13:24:28 +0100 | [diff] [blame] | 35 | return err; |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Ian Campbell | d745562 | 2009-04-02 13:24:28 +0100 | [diff] [blame] | 38 | if (strcmp(state, "online") == 0) |
| 39 | return 1; |
| 40 | else if (strcmp(state, "offline") == 0) |
| 41 | return 0; |
| 42 | |
| 43 | printk(KERN_ERR "XENBUS: unknown state(%s) on CPU%d\n", state, cpu); |
| 44 | return -EINVAL; |
| 45 | } |
| 46 | static void vcpu_hotplug(unsigned int cpu) |
| 47 | { |
| 48 | if (!cpu_possible(cpu)) |
| 49 | return; |
| 50 | |
| 51 | switch (vcpu_online(cpu)) { |
| 52 | case 1: |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 53 | enable_hotplug_cpu(cpu); |
Ian Campbell | d745562 | 2009-04-02 13:24:28 +0100 | [diff] [blame] | 54 | break; |
| 55 | case 0: |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 56 | (void)cpu_down(cpu); |
| 57 | disable_hotplug_cpu(cpu); |
Ian Campbell | d745562 | 2009-04-02 13:24:28 +0100 | [diff] [blame] | 58 | break; |
| 59 | default: |
| 60 | break; |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | static void handle_vcpu_hotplug_event(struct xenbus_watch *watch, |
| 65 | const char **vec, unsigned int len) |
| 66 | { |
| 67 | unsigned int cpu; |
| 68 | char *cpustr; |
| 69 | const char *node = vec[XS_WATCH_PATH]; |
| 70 | |
| 71 | cpustr = strstr(node, "cpu/"); |
| 72 | if (cpustr != NULL) { |
| 73 | sscanf(cpustr, "cpu/%u", &cpu); |
| 74 | vcpu_hotplug(cpu); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static int setup_cpu_watcher(struct notifier_block *notifier, |
| 79 | unsigned long event, void *data) |
| 80 | { |
Ian Campbell | d745562 | 2009-04-02 13:24:28 +0100 | [diff] [blame] | 81 | int cpu; |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 82 | static struct xenbus_watch cpu_watch = { |
| 83 | .node = "cpu", |
| 84 | .callback = handle_vcpu_hotplug_event}; |
| 85 | |
| 86 | (void)register_xenbus_watch(&cpu_watch); |
| 87 | |
Ian Campbell | d745562 | 2009-04-02 13:24:28 +0100 | [diff] [blame] | 88 | for_each_possible_cpu(cpu) { |
| 89 | if (vcpu_online(cpu) == 0) { |
| 90 | (void)cpu_down(cpu); |
Rusty Russell | d7d3756 | 2009-11-03 14:58:38 +1030 | [diff] [blame] | 91 | set_cpu_present(cpu, false); |
Ian Campbell | d745562 | 2009-04-02 13:24:28 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 95 | return NOTIFY_DONE; |
| 96 | } |
| 97 | |
| 98 | static int __init setup_vcpu_hotplug_event(void) |
| 99 | { |
| 100 | static struct notifier_block xsn_cpu = { |
| 101 | .notifier_call = setup_cpu_watcher }; |
| 102 | |
Alex Nixon | 5c51c63 | 2008-09-03 14:30:21 +0100 | [diff] [blame] | 103 | if (!xen_pv_domain()) |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 104 | return -ENODEV; |
| 105 | |
| 106 | register_xenstore_notifier(&xsn_cpu); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | arch_initcall(setup_vcpu_hotplug_event); |
| 112 | |