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