1 #include "hw/acpi/memory_hotplug.h"
2 #include "hw/acpi/pc-hotplug.h"
3 #include "hw/mem/pc-dimm.h"
6 #include "qapi-event.h"
8 static ACPIOSTInfo
*acpi_memory_device_status(int slot
, MemStatus
*mdev
)
10 ACPIOSTInfo
*info
= g_new0(ACPIOSTInfo
, 1);
12 info
->slot_type
= ACPI_SLOT_TYPE_DIMM
;
13 info
->slot
= g_strdup_printf("%d", slot
);
14 info
->source
= mdev
->ost_event
;
15 info
->status
= mdev
->ost_status
;
17 DeviceState
*dev
= DEVICE(mdev
->dimm
);
19 info
->device
= g_strdup(dev
->id
);
20 info
->has_device
= true;
26 void acpi_memory_ospm_status(MemHotplugState
*mem_st
, ACPIOSTInfoList
***list
)
30 for (i
= 0; i
< mem_st
->dev_count
; i
++) {
31 ACPIOSTInfoList
*elem
= g_new0(ACPIOSTInfoList
, 1);
32 elem
->value
= acpi_memory_device_status(i
, &mem_st
->devs
[i
]);
39 static uint64_t acpi_memory_hotplug_read(void *opaque
, hwaddr addr
,
43 MemHotplugState
*mem_st
= opaque
;
47 if (mem_st
->selector
>= mem_st
->dev_count
) {
48 trace_mhp_acpi_invalid_slot_selected(mem_st
->selector
);
52 mdev
= &mem_st
->devs
[mem_st
->selector
];
53 o
= OBJECT(mdev
->dimm
);
55 case 0x0: /* Lo part of phys address where DIMM is mapped */
56 val
= o
? object_property_get_int(o
, PC_DIMM_ADDR_PROP
, NULL
) : 0;
57 trace_mhp_acpi_read_addr_lo(mem_st
->selector
, val
);
59 case 0x4: /* Hi part of phys address where DIMM is mapped */
60 val
= o
? object_property_get_int(o
, PC_DIMM_ADDR_PROP
, NULL
) >> 32 : 0;
61 trace_mhp_acpi_read_addr_hi(mem_st
->selector
, val
);
63 case 0x8: /* Lo part of DIMM size */
64 val
= o
? object_property_get_int(o
, PC_DIMM_SIZE_PROP
, NULL
) : 0;
65 trace_mhp_acpi_read_size_lo(mem_st
->selector
, val
);
67 case 0xc: /* Hi part of DIMM size */
68 val
= o
? object_property_get_int(o
, PC_DIMM_SIZE_PROP
, NULL
) >> 32 : 0;
69 trace_mhp_acpi_read_size_hi(mem_st
->selector
, val
);
71 case 0x10: /* node proximity for _PXM method */
72 val
= o
? object_property_get_int(o
, PC_DIMM_NODE_PROP
, NULL
) : 0;
73 trace_mhp_acpi_read_pxm(mem_st
->selector
, val
);
75 case 0x14: /* pack and return is_* fields */
76 val
|= mdev
->is_enabled
? 1 : 0;
77 val
|= mdev
->is_inserting
? 2 : 0;
78 trace_mhp_acpi_read_flags(mem_st
->selector
, val
);
87 static void acpi_memory_hotplug_write(void *opaque
, hwaddr addr
, uint64_t data
,
90 MemHotplugState
*mem_st
= opaque
;
94 if (!mem_st
->dev_count
) {
99 if (mem_st
->selector
>= mem_st
->dev_count
) {
100 trace_mhp_acpi_invalid_slot_selected(mem_st
->selector
);
106 case 0x0: /* DIMM slot selector */
107 mem_st
->selector
= data
;
108 trace_mhp_acpi_write_slot(mem_st
->selector
);
110 case 0x4: /* _OST event */
111 mdev
= &mem_st
->devs
[mem_st
->selector
];
113 /* TODO: handle device insert OST event */
114 } else if (data
== 3) {
115 /* TODO: handle device remove OST event */
117 mdev
->ost_event
= data
;
118 trace_mhp_acpi_write_ost_ev(mem_st
->selector
, mdev
->ost_event
);
120 case 0x8: /* _OST status */
121 mdev
= &mem_st
->devs
[mem_st
->selector
];
122 mdev
->ost_status
= data
;
123 trace_mhp_acpi_write_ost_status(mem_st
->selector
, mdev
->ost_status
);
124 /* TODO: implement memory removal on guest signal */
126 info
= acpi_memory_device_status(mem_st
->selector
, mdev
);
127 qapi_event_send_acpi_device_ost(info
, &error_abort
);
128 qapi_free_ACPIOSTInfo(info
);
131 mdev
= &mem_st
->devs
[mem_st
->selector
];
132 if (data
& 2) { /* clear insert event */
133 mdev
->is_inserting
= false;
134 trace_mhp_acpi_clear_insert_evt(mem_st
->selector
);
140 static const MemoryRegionOps acpi_memory_hotplug_ops
= {
141 .read
= acpi_memory_hotplug_read
,
142 .write
= acpi_memory_hotplug_write
,
143 .endianness
= DEVICE_LITTLE_ENDIAN
,
145 .min_access_size
= 1,
146 .max_access_size
= 4,
150 void acpi_memory_hotplug_init(MemoryRegion
*as
, Object
*owner
,
151 MemHotplugState
*state
)
153 MachineState
*machine
= MACHINE(qdev_get_machine());
155 state
->dev_count
= machine
->ram_slots
;
156 if (!state
->dev_count
) {
160 state
->devs
= g_malloc0(sizeof(*state
->devs
) * state
->dev_count
);
161 memory_region_init_io(&state
->io
, owner
, &acpi_memory_hotplug_ops
, state
,
162 "acpi-mem-hotplug", ACPI_MEMORY_HOTPLUG_IO_LEN
);
163 memory_region_add_subregion(as
, ACPI_MEMORY_HOTPLUG_BASE
, &state
->io
);
166 void acpi_memory_plug_cb(ACPIREGS
*ar
, qemu_irq irq
, MemHotplugState
*mem_st
,
167 DeviceState
*dev
, Error
**errp
)
170 Error
*local_err
= NULL
;
171 int slot
= object_property_get_int(OBJECT(dev
), "slot", &local_err
);
174 error_propagate(errp
, local_err
);
178 if (slot
>= mem_st
->dev_count
) {
179 char *dev_path
= object_get_canonical_path(OBJECT(dev
));
180 error_setg(errp
, "acpi_memory_plug_cb: "
181 "device [%s] returned invalid memory slot[%d]",
187 mdev
= &mem_st
->devs
[slot
];
189 mdev
->is_enabled
= true;
190 mdev
->is_inserting
= true;
193 ar
->gpe
.sts
[0] |= ACPI_MEMORY_HOTPLUG_STATUS
;
194 acpi_update_sci(ar
, irq
);
198 static const VMStateDescription vmstate_memhp_sts
= {
199 .name
= "memory hotplug device state",
201 .minimum_version_id
= 1,
202 .minimum_version_id_old
= 1,
203 .fields
= (VMStateField
[]) {
204 VMSTATE_BOOL(is_enabled
, MemStatus
),
205 VMSTATE_BOOL(is_inserting
, MemStatus
),
206 VMSTATE_UINT32(ost_event
, MemStatus
),
207 VMSTATE_UINT32(ost_status
, MemStatus
),
208 VMSTATE_END_OF_LIST()
212 const VMStateDescription vmstate_memory_hotplug
= {
213 .name
= "memory hotplug state",
215 .minimum_version_id
= 1,
216 .minimum_version_id_old
= 1,
217 .fields
= (VMStateField
[]) {
218 VMSTATE_UINT32(selector
, MemHotplugState
),
219 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs
, MemHotplugState
, dev_count
,
220 vmstate_memhp_sts
, MemStatus
),
221 VMSTATE_END_OF_LIST()