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