1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#define pr_fmt(fmt) "xen_cpu: " fmt
35
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>
41
42#include <xen/xen.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>
48
49
50
51
52
53
54
55
56struct pcpu {
57 struct list_head list;
58 struct device dev;
59 uint32_t cpu_id;
60 uint32_t flags;
61};
62
63static struct bus_type xen_pcpu_subsys = {
64 .name = "xen_cpu",
65 .dev_name = "xen_cpu",
66};
67
68static DEFINE_MUTEX(xen_pcpu_lock);
69
70static LIST_HEAD(xen_pcpus);
71
72static int xen_pcpu_down(uint32_t cpu_id)
73{
74 struct xen_platform_op op = {
75 .cmd = XENPF_cpu_offline,
76 .interface_version = XENPF_INTERFACE_VERSION,
77 .u.cpu_ol.cpuid = cpu_id,
78 };
79
80 return HYPERVISOR_dom0_op(&op);
81}
82
83static int xen_pcpu_up(uint32_t cpu_id)
84{
85 struct xen_platform_op op = {
86 .cmd = XENPF_cpu_online,
87 .interface_version = XENPF_INTERFACE_VERSION,
88 .u.cpu_ol.cpuid = cpu_id,
89 };
90
91 return HYPERVISOR_dom0_op(&op);
92}
93
94static ssize_t show_online(struct device *dev,
95 struct device_attribute *attr,
96 char *buf)
97{
98 struct pcpu *cpu = container_of(dev, struct pcpu, dev);
99
100 return sprintf(buf, "%u\n", !!(cpu->flags & XEN_PCPU_FLAGS_ONLINE));
101}
102
103static ssize_t __ref store_online(struct device *dev,
104 struct device_attribute *attr,
105 const char *buf, size_t count)
106{
107 struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
108 unsigned long long val;
109 ssize_t ret;
110
111 if (!capable(CAP_SYS_ADMIN))
112 return -EPERM;
113
114 if (kstrtoull(buf, 0, &val) < 0)
115 return -EINVAL;
116
117 switch (val) {
118 case 0:
119 ret = xen_pcpu_down(pcpu->cpu_id);
120 break;
121 case 1:
122 ret = xen_pcpu_up(pcpu->cpu_id);
123 break;
124 default:
125 ret = -EINVAL;
126 }
127
128 if (ret >= 0)
129 ret = count;
130 return ret;
131}
132static DEVICE_ATTR(online, S_IRUGO | S_IWUSR, show_online, store_online);
133
134static bool xen_pcpu_online(uint32_t flags)
135{
136 return !!(flags & XEN_PCPU_FLAGS_ONLINE);
137}
138
139static void pcpu_online_status(struct xenpf_pcpuinfo *info,
140 struct pcpu *pcpu)
141{
142 if (xen_pcpu_online(info->flags) &&
143 !xen_pcpu_online(pcpu->flags)) {
144
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
150 pcpu->flags &= ~XEN_PCPU_FLAGS_ONLINE;
151 kobject_uevent(&pcpu->dev.kobj, KOBJ_OFFLINE);
152 }
153}
154
155static struct pcpu *get_pcpu(uint32_t cpu_id)
156{
157 struct pcpu *pcpu;
158
159 list_for_each_entry(pcpu, &xen_pcpus, list) {
160 if (pcpu->cpu_id == cpu_id)
161 return pcpu;
162 }
163
164 return NULL;
165}
166
167static void pcpu_release(struct device *dev)
168{
169 struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
170
171 list_del(&pcpu->list);
172 kfree(pcpu);
173}
174
175static void unregister_and_remove_pcpu(struct pcpu *pcpu)
176{
177 struct device *dev;
178
179 if (!pcpu)
180 return;
181
182 dev = &pcpu->dev;
183 if (dev->id)
184 device_remove_file(dev, &dev_attr_online);
185
186
187 device_unregister(dev);
188}
189
190static int register_pcpu(struct pcpu *pcpu)
191{
192 struct device *dev;
193 int err = -EINVAL;
194
195 if (!pcpu)
196 return err;
197
198 dev = &pcpu->dev;
199 dev->bus = &xen_pcpu_subsys;
200 dev->id = pcpu->cpu_id;
201 dev->release = pcpu_release;
202
203 err = device_register(dev);
204 if (err) {
205 pcpu_release(dev);
206 return err;
207 }
208
209
210
211
212
213
214 if (dev->id) {
215 err = device_create_file(dev, &dev_attr_online);
216 if (err) {
217 device_unregister(dev);
218 return err;
219 }
220 }
221
222 return 0;
223}
224
225static struct pcpu *create_and_register_pcpu(struct xenpf_pcpuinfo *info)
226{
227 struct pcpu *pcpu;
228 int err;
229
230 if (info->flags & XEN_PCPU_FLAGS_INVALID)
231 return ERR_PTR(-ENODEV);
232
233 pcpu = kzalloc(sizeof(struct pcpu), GFP_KERNEL);
234 if (!pcpu)
235 return ERR_PTR(-ENOMEM);
236
237 INIT_LIST_HEAD(&pcpu->list);
238 pcpu->cpu_id = info->xen_cpuid;
239 pcpu->flags = info->flags;
240
241
242 list_add_tail(&pcpu->list, &xen_pcpus);
243
244 err = register_pcpu(pcpu);
245 if (err) {
246 pr_warn("Failed to register pcpu%u\n", info->xen_cpuid);
247 return ERR_PTR(-ENOENT);
248 }
249
250 return pcpu;
251}
252
253
254
255
256static int sync_pcpu(uint32_t cpu, uint32_t *max_cpu)
257{
258 int ret;
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,
265 };
266
267 ret = HYPERVISOR_dom0_op(&op);
268 if (ret)
269 return ret;
270
271 info = &op.u.pcpu_info;
272 if (max_cpu)
273 *max_cpu = info->max_present;
274
275 pcpu = get_pcpu(cpu);
276
277
278
279
280 if (info->flags & XEN_PCPU_FLAGS_INVALID) {
281 unregister_and_remove_pcpu(pcpu);
282 return 0;
283 }
284
285 if (!pcpu) {
286 pcpu = create_and_register_pcpu(info);
287 if (IS_ERR_OR_NULL(pcpu))
288 return -ENODEV;
289 } else
290 pcpu_online_status(info, pcpu);
291
292 return 0;
293}
294
295
296
297
298static int xen_sync_pcpus(void)
299{
300
301
302
303 uint32_t cpu = 0, max_cpu = 0;
304 int err = 0;
305 struct pcpu *pcpu, *tmp;
306
307 mutex_lock(&xen_pcpu_lock);
308
309 while (!err && (cpu <= max_cpu)) {
310 err = sync_pcpu(cpu, &max_cpu);
311 cpu++;
312 }
313
314 if (err)
315 list_for_each_entry_safe(pcpu, tmp, &xen_pcpus, list)
316 unregister_and_remove_pcpu(pcpu);
317
318 mutex_unlock(&xen_pcpu_lock);
319
320 return err;
321}
322
323static void xen_pcpu_work_fn(struct work_struct *work)
324{
325 xen_sync_pcpus();
326}
327static DECLARE_WORK(xen_pcpu_work, xen_pcpu_work_fn);
328
329static irqreturn_t xen_pcpu_interrupt(int irq, void *dev_id)
330{
331 schedule_work(&xen_pcpu_work);
332 return IRQ_HANDLED;
333}
334
335
336void xen_pcpu_hotplug_sync(void)
337{
338 schedule_work(&xen_pcpu_work);
339}
340EXPORT_SYMBOL_GPL(xen_pcpu_hotplug_sync);
341
342
343
344
345
346int xen_pcpu_id(uint32_t acpi_id)
347{
348 int cpu_id = 0, max_id = 0;
349 struct xen_platform_op op;
350
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)) {
355 cpu_id++;
356 continue;
357 }
358
359 if (acpi_id == op.u.pcpu_info.acpi_id)
360 return cpu_id;
361 if (op.u.pcpu_info.max_present > max_id)
362 max_id = op.u.pcpu_info.max_present;
363 cpu_id++;
364 }
365
366 return -ENODEV;
367}
368EXPORT_SYMBOL_GPL(xen_pcpu_id);
369
370static int __init xen_pcpu_init(void)
371{
372 int irq, ret;
373
374 if (!xen_initial_domain())
375 return -ENODEV;
376
377 irq = bind_virq_to_irqhandler(VIRQ_PCPU_STATE, 0,
378 xen_pcpu_interrupt, 0,
379 "xen-pcpu", NULL);
380 if (irq < 0) {
381 pr_warn("Failed to bind pcpu virq\n");
382 return irq;
383 }
384
385 ret = subsys_system_register(&xen_pcpu_subsys, NULL);
386 if (ret) {
387 pr_warn("Failed to register pcpu subsys\n");
388 goto err1;
389 }
390
391 ret = xen_sync_pcpus();
392 if (ret) {
393 pr_warn("Failed to sync pcpu info\n");
394 goto err2;
395 }
396
397 return 0;
398
399err2:
400 bus_unregister(&xen_pcpu_subsys);
401err1:
402 unbind_from_irqhandler(irq, NULL);
403 return ret;
404}
405arch_initcall(xen_pcpu_init);
406