1 /******************************************************************************
3 * Management physical cpu in dom0, get pcpu info and provide sys interface
5 * Copyright (c) 2012 Intel Corporation
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation; or, when distributed
12 * separately from the Linux kernel or incorporated into other
13 * software packages, subject to the following license:
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this source file (the "Software"), to deal in the Software without
17 * restriction, including without limitation the rights to use, copy, modify,
18 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19 * and to permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies or substantial portions of the Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 #define pr_fmt(fmt) "xen_cpu: " fmt
36 #include <linux/interrupt.h>
37 #include <linux/spinlock.h>
38 #include <linux/cpu.h>
39 #include <linux/stat.h>
40 #include <linux/capability.h>
44 #include <xen/xenbus.h>
45 #include <xen/events.h>
46 #include <xen/interface/platform.h>
47 #include <asm/xen/hypervisor.h>
48 #include <asm/xen/hypercall.h>
52 * @cpu_id: Xen physical cpu logic number
53 * @flags: Xen physical cpu status flag
54 * - XEN_PCPU_FLAGS_ONLINE: cpu is online
55 * - XEN_PCPU_FLAGS_INVALID: cpu is not present
58 struct list_head list;
65 static struct bus_type xen_pcpu_subsys = {
67 .dev_name = "xen_cpu",
70 static DEFINE_MUTEX(xen_pcpu_lock);
72 static LIST_HEAD(xen_pcpus);
74 static int xen_pcpu_down(uint32_t cpu_id)
76 struct xen_platform_op op = {
77 .cmd = XENPF_cpu_offline,
78 .interface_version = XENPF_INTERFACE_VERSION,
79 .u.cpu_ol.cpuid = cpu_id,
82 return HYPERVISOR_platform_op(&op);
85 static int xen_pcpu_up(uint32_t cpu_id)
87 struct xen_platform_op op = {
88 .cmd = XENPF_cpu_online,
89 .interface_version = XENPF_INTERFACE_VERSION,
90 .u.cpu_ol.cpuid = cpu_id,
93 return HYPERVISOR_platform_op(&op);
96 static ssize_t online_show(struct device *dev,
97 struct device_attribute *attr,
100 struct pcpu *cpu = container_of(dev, struct pcpu, dev);
102 return sprintf(buf, "%u\n", !!(cpu->flags & XEN_PCPU_FLAGS_ONLINE));
105 static ssize_t __ref online_store(struct device *dev,
106 struct device_attribute *attr,
107 const char *buf, size_t count)
109 struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
110 unsigned long long val;
113 if (!capable(CAP_SYS_ADMIN))
116 if (kstrtoull(buf, 0, &val) < 0)
121 ret = xen_pcpu_down(pcpu->cpu_id);
124 ret = xen_pcpu_up(pcpu->cpu_id);
134 static DEVICE_ATTR_RW(online);
136 static struct attribute *pcpu_dev_attrs[] = {
137 &dev_attr_online.attr,
141 static umode_t pcpu_dev_is_visible(struct kobject *kobj,
142 struct attribute *attr, int idx)
144 struct device *dev = kobj_to_dev(kobj);
146 * Xen never offline cpu0 due to several restrictions
147 * and assumptions. This basically doesn't add a sys control
148 * to user, one cannot attempt to offline BSP.
150 return dev->id ? attr->mode : 0;
153 static const struct attribute_group pcpu_dev_group = {
154 .attrs = pcpu_dev_attrs,
155 .is_visible = pcpu_dev_is_visible,
158 static const struct attribute_group *pcpu_dev_groups[] = {
163 static bool xen_pcpu_online(uint32_t flags)
165 return !!(flags & XEN_PCPU_FLAGS_ONLINE);
168 static void pcpu_online_status(struct xenpf_pcpuinfo *info,
171 if (xen_pcpu_online(info->flags) &&
172 !xen_pcpu_online(pcpu->flags)) {
173 /* the pcpu is onlined */
174 pcpu->flags |= XEN_PCPU_FLAGS_ONLINE;
175 kobject_uevent(&pcpu->dev.kobj, KOBJ_ONLINE);
176 } else if (!xen_pcpu_online(info->flags) &&
177 xen_pcpu_online(pcpu->flags)) {
178 /* The pcpu is offlined */
179 pcpu->flags &= ~XEN_PCPU_FLAGS_ONLINE;
180 kobject_uevent(&pcpu->dev.kobj, KOBJ_OFFLINE);
184 static struct pcpu *get_pcpu(uint32_t cpu_id)
188 list_for_each_entry(pcpu, &xen_pcpus, list) {
189 if (pcpu->cpu_id == cpu_id)
196 static void pcpu_release(struct device *dev)
198 struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
200 list_del(&pcpu->list);
204 static void unregister_and_remove_pcpu(struct pcpu *pcpu)
212 /* pcpu remove would be implicitly done */
213 device_unregister(dev);
216 static int register_pcpu(struct pcpu *pcpu)
225 dev->bus = &xen_pcpu_subsys;
226 dev->id = pcpu->cpu_id;
227 dev->release = pcpu_release;
228 dev->groups = pcpu_dev_groups;
230 err = device_register(dev);
239 static struct pcpu *create_and_register_pcpu(struct xenpf_pcpuinfo *info)
244 if (info->flags & XEN_PCPU_FLAGS_INVALID)
245 return ERR_PTR(-ENODEV);
247 pcpu = kzalloc(sizeof(struct pcpu), GFP_KERNEL);
249 return ERR_PTR(-ENOMEM);
251 INIT_LIST_HEAD(&pcpu->list);
252 pcpu->cpu_id = info->xen_cpuid;
253 pcpu->acpi_id = info->acpi_id;
254 pcpu->flags = info->flags;
256 /* Need hold on xen_pcpu_lock before pcpu list manipulations */
257 list_add_tail(&pcpu->list, &xen_pcpus);
259 err = register_pcpu(pcpu);
261 pr_warn("Failed to register pcpu%u\n", info->xen_cpuid);
262 return ERR_PTR(-ENOENT);
269 * Caller should hold the xen_pcpu_lock
271 static int sync_pcpu(uint32_t cpu, uint32_t *max_cpu)
274 struct pcpu *pcpu = NULL;
275 struct xenpf_pcpuinfo *info;
276 struct xen_platform_op op = {
277 .cmd = XENPF_get_cpuinfo,
278 .interface_version = XENPF_INTERFACE_VERSION,
279 .u.pcpu_info.xen_cpuid = cpu,
282 ret = HYPERVISOR_platform_op(&op);
286 info = &op.u.pcpu_info;
288 *max_cpu = info->max_present;
290 pcpu = get_pcpu(cpu);
293 * Only those at cpu present map has its sys interface.
295 if (info->flags & XEN_PCPU_FLAGS_INVALID) {
296 unregister_and_remove_pcpu(pcpu);
301 pcpu = create_and_register_pcpu(info);
302 if (IS_ERR_OR_NULL(pcpu))
305 pcpu_online_status(info, pcpu);
311 * Sync dom0's pcpu information with xen hypervisor's
313 static int xen_sync_pcpus(void)
316 * Boot cpu always have cpu_id 0 in xen
318 uint32_t cpu = 0, max_cpu = 0;
320 struct pcpu *pcpu, *tmp;
322 mutex_lock(&xen_pcpu_lock);
324 while (!err && (cpu <= max_cpu)) {
325 err = sync_pcpu(cpu, &max_cpu);
330 list_for_each_entry_safe(pcpu, tmp, &xen_pcpus, list)
331 unregister_and_remove_pcpu(pcpu);
333 mutex_unlock(&xen_pcpu_lock);
338 static void xen_pcpu_work_fn(struct work_struct *work)
342 static DECLARE_WORK(xen_pcpu_work, xen_pcpu_work_fn);
344 static irqreturn_t xen_pcpu_interrupt(int irq, void *dev_id)
346 schedule_work(&xen_pcpu_work);
350 static int __init xen_pcpu_init(void)
354 if (!xen_initial_domain())
357 irq = bind_virq_to_irqhandler(VIRQ_PCPU_STATE, 0,
358 xen_pcpu_interrupt, 0,
361 pr_warn("Failed to bind pcpu virq\n");
365 ret = subsys_system_register(&xen_pcpu_subsys, NULL);
367 pr_warn("Failed to register pcpu subsys\n");
371 ret = xen_sync_pcpus();
373 pr_warn("Failed to sync pcpu info\n");
380 bus_unregister(&xen_pcpu_subsys);
382 unbind_from_irqhandler(irq, NULL);
385 arch_initcall(xen_pcpu_init);
388 bool __init xen_processor_present(uint32_t acpi_id)
390 const struct pcpu *pcpu;
393 mutex_lock(&xen_pcpu_lock);
394 list_for_each_entry(pcpu, &xen_pcpus, list)
395 if (pcpu->acpi_id == acpi_id) {
396 online = pcpu->flags & XEN_PCPU_FLAGS_ONLINE;
399 mutex_unlock(&xen_pcpu_lock);