xref: /qemu/hw/acpi/cpu_hotplug.c (revision 1be6b511a6a6f0d87b57725cf8d5caefcff3cc9a)
1 /*
2  * QEMU ACPI hotplug utilities
3  *
4  * Copyright (C) 2013 Red Hat Inc
5  *
6  * Authors:
7  *   Igor Mammedov <imammedo@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 #include "hw/hw.h"
13 #include "hw/acpi/cpu_hotplug.h"
14 
15 static uint64_t cpu_status_read(void *opaque, hwaddr addr, unsigned int size)
16 {
17     AcpiCpuHotplug *cpus = opaque;
18     uint64_t val = cpus->sts[addr];
19 
20     return val;
21 }
22 
23 static void cpu_status_write(void *opaque, hwaddr addr, uint64_t data,
24                              unsigned int size)
25 {
26     /* TODO: implement VCPU removal on guest signal that CPU can be removed */
27 }
28 
29 static const MemoryRegionOps AcpiCpuHotplug_ops = {
30     .read = cpu_status_read,
31     .write = cpu_status_write,
32     .endianness = DEVICE_LITTLE_ENDIAN,
33     .valid = {
34         .min_access_size = 1,
35         .max_access_size = 1,
36     },
37 };
38 
39 void acpi_cpu_plug_cb(ACPIREGS *ar, qemu_irq irq,
40                       AcpiCpuHotplug *g, DeviceState *dev, Error **errp)
41 {
42     CPUState *cpu = CPU(dev);
43     CPUClass *k = CPU_GET_CLASS(cpu);
44     int64_t cpu_id;
45 
46     cpu_id = k->get_arch_id(cpu);
47     if ((cpu_id / 8) >= ACPI_GPE_PROC_LEN) {
48         error_setg(errp, "acpi: invalid cpu id: %" PRIi64, cpu_id);
49         return;
50     }
51 
52     AcpiCpuHotplug_add(&ar->gpe, g, cpu);
53 
54     acpi_update_sci(ar, irq);
55 }
56 
57 void AcpiCpuHotplug_add(ACPIGPE *gpe, AcpiCpuHotplug *g, CPUState *cpu)
58 {
59     CPUClass *k = CPU_GET_CLASS(cpu);
60     int64_t cpu_id;
61 
62     *gpe->sts = *gpe->sts | ACPI_CPU_HOTPLUG_STATUS;
63     cpu_id = k->get_arch_id(CPU(cpu));
64     g_assert((cpu_id / 8) < ACPI_GPE_PROC_LEN);
65     g->sts[cpu_id / 8] |= (1 << (cpu_id % 8));
66 }
67 
68 void AcpiCpuHotplug_init(MemoryRegion *parent, Object *owner,
69                          AcpiCpuHotplug *gpe_cpu, uint16_t base)
70 {
71     CPUState *cpu;
72 
73     CPU_FOREACH(cpu) {
74         CPUClass *cc = CPU_GET_CLASS(cpu);
75         int64_t id = cc->get_arch_id(cpu);
76 
77         g_assert((id / 8) < ACPI_GPE_PROC_LEN);
78         gpe_cpu->sts[id / 8] |= (1 << (id % 8));
79     }
80     memory_region_init_io(&gpe_cpu->io, owner, &AcpiCpuHotplug_ops,
81                           gpe_cpu, "acpi-cpu-hotplug", ACPI_GPE_PROC_LEN);
82     memory_region_add_subregion(parent, base, &gpe_cpu->io);
83 }
84