1 /******************************************************************************
3 * Management physical cpu in dom0, get pcpu info and provide sys interface
5 * Copyright (c) 2012 Intel Corporation
6 * Author: Liu, Jinsong <jinsong.liu@intel.com>
7 * Author: Jiang, Yunhong <yunhong.jiang@intel.com>
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>
43 #include <xen/xenbus.h>
44 #include <xen/events.h>
45 #include <xen/interface/platform.h>
46 #include <asm/xen/hypervisor.h>
47 #include <asm/xen/hypercall.h>
51 * @cpu_id: Xen physical cpu logic number
52 * @flags: Xen physical cpu status flag
53 * - XEN_PCPU_FLAGS_ONLINE: cpu is online
54 * - XEN_PCPU_FLAGS_INVALID: cpu is not present
57 struct list_head list
;
63 static struct bus_type xen_pcpu_subsys
= {
65 .dev_name
= "xen_cpu",
68 static DEFINE_MUTEX(xen_pcpu_lock
);
70 static LIST_HEAD(xen_pcpus
);
72 static int xen_pcpu_down(uint32_t cpu_id
)
74 struct xen_platform_op op
= {
75 .cmd
= XENPF_cpu_offline
,
76 .interface_version
= XENPF_INTERFACE_VERSION
,
77 .u
.cpu_ol
.cpuid
= cpu_id
,
80 return HYPERVISOR_dom0_op(&op
);
83 static int xen_pcpu_up(uint32_t cpu_id
)
85 struct xen_platform_op op
= {
86 .cmd
= XENPF_cpu_online
,
87 .interface_version
= XENPF_INTERFACE_VERSION
,
88 .u
.cpu_ol
.cpuid
= cpu_id
,
91 return HYPERVISOR_dom0_op(&op
);
94 static ssize_t
show_online(struct device
*dev
,
95 struct device_attribute
*attr
,
98 struct pcpu
*cpu
= container_of(dev
, struct pcpu
, dev
);
100 return sprintf(buf
, "%u\n", !!(cpu
->flags
& XEN_PCPU_FLAGS_ONLINE
));
103 static ssize_t __ref
store_online(struct device
*dev
,
104 struct device_attribute
*attr
,
105 const char *buf
, size_t count
)
107 struct pcpu
*pcpu
= container_of(dev
, struct pcpu
, dev
);
108 unsigned long long val
;
111 if (!capable(CAP_SYS_ADMIN
))
114 if (kstrtoull(buf
, 0, &val
) < 0)
119 ret
= xen_pcpu_down(pcpu
->cpu_id
);
122 ret
= xen_pcpu_up(pcpu
->cpu_id
);
132 static DEVICE_ATTR(online
, S_IRUGO
| S_IWUSR
, show_online
, store_online
);
134 static bool xen_pcpu_online(uint32_t flags
)
136 return !!(flags
& XEN_PCPU_FLAGS_ONLINE
);
139 static void pcpu_online_status(struct xenpf_pcpuinfo
*info
,
142 if (xen_pcpu_online(info
->flags
) &&
143 !xen_pcpu_online(pcpu
->flags
)) {
144 /* the pcpu is onlined */
145 pcpu
->flags
|= XEN_PCPU_FLAGS_ONLINE
;
146 kobject_uevent(&pcpu
->dev
.kobj
, KOBJ_ONLINE
);
147 } else if (!xen_pcpu_online(info
->flags
) &&
148 xen_pcpu_online(pcpu
->flags
)) {
149 /* The pcpu is offlined */
150 pcpu
->flags
&= ~XEN_PCPU_FLAGS_ONLINE
;
151 kobject_uevent(&pcpu
->dev
.kobj
, KOBJ_OFFLINE
);
155 static struct pcpu
*get_pcpu(uint32_t cpu_id
)
159 list_for_each_entry(pcpu
, &xen_pcpus
, list
) {
160 if (pcpu
->cpu_id
== cpu_id
)
167 static void pcpu_release(struct device
*dev
)
169 struct pcpu
*pcpu
= container_of(dev
, struct pcpu
, dev
);
171 list_del(&pcpu
->list
);
175 static void unregister_and_remove_pcpu(struct pcpu
*pcpu
)
184 device_remove_file(dev
, &dev_attr_online
);
186 /* pcpu remove would be implicitly done */
187 device_unregister(dev
);
190 static int register_pcpu(struct pcpu
*pcpu
)
199 dev
->bus
= &xen_pcpu_subsys
;
200 dev
->id
= pcpu
->cpu_id
;
201 dev
->release
= pcpu_release
;
203 err
= device_register(dev
);
210 * Xen never offline cpu0 due to several restrictions
211 * and assumptions. This basically doesn't add a sys control
212 * to user, one cannot attempt to offline BSP.
215 err
= device_create_file(dev
, &dev_attr_online
);
217 device_unregister(dev
);
225 static struct pcpu
*create_and_register_pcpu(struct xenpf_pcpuinfo
*info
)
230 if (info
->flags
& XEN_PCPU_FLAGS_INVALID
)
231 return ERR_PTR(-ENODEV
);
233 pcpu
= kzalloc(sizeof(struct pcpu
), GFP_KERNEL
);
235 return ERR_PTR(-ENOMEM
);
237 INIT_LIST_HEAD(&pcpu
->list
);
238 pcpu
->cpu_id
= info
->xen_cpuid
;
239 pcpu
->flags
= info
->flags
;
241 /* Need hold on xen_pcpu_lock before pcpu list manipulations */
242 list_add_tail(&pcpu
->list
, &xen_pcpus
);
244 err
= register_pcpu(pcpu
);
246 pr_warn("Failed to register pcpu%u\n", info
->xen_cpuid
);
247 return ERR_PTR(-ENOENT
);
254 * Caller should hold the xen_pcpu_lock
256 static int sync_pcpu(uint32_t cpu
, uint32_t *max_cpu
)
259 struct pcpu
*pcpu
= NULL
;
260 struct xenpf_pcpuinfo
*info
;
261 struct xen_platform_op op
= {
262 .cmd
= XENPF_get_cpuinfo
,
263 .interface_version
= XENPF_INTERFACE_VERSION
,
264 .u
.pcpu_info
.xen_cpuid
= cpu
,
267 ret
= HYPERVISOR_dom0_op(&op
);
271 info
= &op
.u
.pcpu_info
;
273 *max_cpu
= info
->max_present
;
275 pcpu
= get_pcpu(cpu
);
278 * Only those at cpu present map has its sys interface.
280 if (info
->flags
& XEN_PCPU_FLAGS_INVALID
) {
281 unregister_and_remove_pcpu(pcpu
);
286 pcpu
= create_and_register_pcpu(info
);
287 if (IS_ERR_OR_NULL(pcpu
))
290 pcpu_online_status(info
, pcpu
);
296 * Sync dom0's pcpu information with xen hypervisor's
298 static int xen_sync_pcpus(void)
301 * Boot cpu always have cpu_id 0 in xen
303 uint32_t cpu
= 0, max_cpu
= 0;
305 struct pcpu
*pcpu
, *tmp
;
307 mutex_lock(&xen_pcpu_lock
);
309 while (!err
&& (cpu
<= max_cpu
)) {
310 err
= sync_pcpu(cpu
, &max_cpu
);
315 list_for_each_entry_safe(pcpu
, tmp
, &xen_pcpus
, list
)
316 unregister_and_remove_pcpu(pcpu
);
318 mutex_unlock(&xen_pcpu_lock
);
323 static void xen_pcpu_work_fn(struct work_struct
*work
)
327 static DECLARE_WORK(xen_pcpu_work
, xen_pcpu_work_fn
);
329 static irqreturn_t
xen_pcpu_interrupt(int irq
, void *dev_id
)
331 schedule_work(&xen_pcpu_work
);
335 /* Sync with Xen hypervisor after cpu hotadded */
336 void xen_pcpu_hotplug_sync(void)
338 schedule_work(&xen_pcpu_work
);
340 EXPORT_SYMBOL_GPL(xen_pcpu_hotplug_sync
);
343 * For hypervisor presented cpu, return logic cpu id;
344 * For hypervisor non-presented cpu, return -ENODEV.
346 int xen_pcpu_id(uint32_t acpi_id
)
348 int cpu_id
= 0, max_id
= 0;
349 struct xen_platform_op op
;
351 op
.cmd
= XENPF_get_cpuinfo
;
352 while (cpu_id
<= max_id
) {
353 op
.u
.pcpu_info
.xen_cpuid
= cpu_id
;
354 if (HYPERVISOR_dom0_op(&op
)) {
359 if (acpi_id
== op
.u
.pcpu_info
.acpi_id
)
361 if (op
.u
.pcpu_info
.max_present
> max_id
)
362 max_id
= op
.u
.pcpu_info
.max_present
;
368 EXPORT_SYMBOL_GPL(xen_pcpu_id
);
370 static int __init
xen_pcpu_init(void)
374 if (!xen_initial_domain())
377 irq
= bind_virq_to_irqhandler(VIRQ_PCPU_STATE
, 0,
378 xen_pcpu_interrupt
, 0,
381 pr_warn("Failed to bind pcpu virq\n");
385 ret
= subsys_system_register(&xen_pcpu_subsys
, NULL
);
387 pr_warn("Failed to register pcpu subsys\n");
391 ret
= xen_sync_pcpus();
393 pr_warn("Failed to sync pcpu info\n");
400 bus_unregister(&xen_pcpu_subsys
);
402 unbind_from_irqhandler(irq
, NULL
);
405 arch_initcall(xen_pcpu_init
);