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