1 #include "qemu/osdep.h"
2 #include "hw/acpi/memory_hotplug.h"
3 #include "hw/acpi/pc-hotplug.h"
4 #include "hw/mem/pc-dimm.h"
6 #include "hw/qdev-core.h"
8 #include "qapi-event.h"
10 static ACPIOSTInfo
*acpi_memory_device_status(int slot
, MemStatus
*mdev
)
12 ACPIOSTInfo
*info
= g_new0(ACPIOSTInfo
, 1);
14 info
->slot_type
= ACPI_SLOT_TYPE_DIMM
;
15 info
->slot
= g_strdup_printf("%d", slot
);
16 info
->source
= mdev
->ost_event
;
17 info
->status
= mdev
->ost_status
;
19 DeviceState
*dev
= DEVICE(mdev
->dimm
);
21 info
->device
= g_strdup(dev
->id
);
22 info
->has_device
= true;
28 void acpi_memory_ospm_status(MemHotplugState
*mem_st
, ACPIOSTInfoList
***list
)
32 for (i
= 0; i
< mem_st
->dev_count
; i
++) {
33 ACPIOSTInfoList
*elem
= g_new0(ACPIOSTInfoList
, 1);
34 elem
->value
= acpi_memory_device_status(i
, &mem_st
->devs
[i
]);
41 static uint64_t acpi_memory_hotplug_read(void *opaque
, hwaddr addr
,
45 MemHotplugState
*mem_st
= opaque
;
49 if (mem_st
->selector
>= mem_st
->dev_count
) {
50 trace_mhp_acpi_invalid_slot_selected(mem_st
->selector
);
54 mdev
= &mem_st
->devs
[mem_st
->selector
];
55 o
= OBJECT(mdev
->dimm
);
57 case 0x0: /* Lo part of phys address where DIMM is mapped */
58 val
= o
? object_property_get_int(o
, PC_DIMM_ADDR_PROP
, NULL
) : 0;
59 trace_mhp_acpi_read_addr_lo(mem_st
->selector
, val
);
61 case 0x4: /* Hi part of phys address where DIMM is mapped */
62 val
= o
? object_property_get_int(o
, PC_DIMM_ADDR_PROP
, NULL
) >> 32 : 0;
63 trace_mhp_acpi_read_addr_hi(mem_st
->selector
, val
);
65 case 0x8: /* Lo part of DIMM size */
66 val
= o
? object_property_get_int(o
, PC_DIMM_SIZE_PROP
, NULL
) : 0;
67 trace_mhp_acpi_read_size_lo(mem_st
->selector
, val
);
69 case 0xc: /* Hi part of DIMM size */
70 val
= o
? object_property_get_int(o
, PC_DIMM_SIZE_PROP
, NULL
) >> 32 : 0;
71 trace_mhp_acpi_read_size_hi(mem_st
->selector
, val
);
73 case 0x10: /* node proximity for _PXM method */
74 val
= o
? object_property_get_int(o
, PC_DIMM_NODE_PROP
, NULL
) : 0;
75 trace_mhp_acpi_read_pxm(mem_st
->selector
, val
);
77 case 0x14: /* pack and return is_* fields */
78 val
|= mdev
->is_enabled
? 1 : 0;
79 val
|= mdev
->is_inserting
? 2 : 0;
80 val
|= mdev
->is_removing
? 4 : 0;
81 trace_mhp_acpi_read_flags(mem_st
->selector
, val
);
90 static void acpi_memory_hotplug_write(void *opaque
, hwaddr addr
, uint64_t data
,
93 MemHotplugState
*mem_st
= opaque
;
96 DeviceState
*dev
= NULL
;
97 HotplugHandler
*hotplug_ctrl
= NULL
;
98 Error
*local_err
= NULL
;
100 if (!mem_st
->dev_count
) {
105 if (mem_st
->selector
>= mem_st
->dev_count
) {
106 trace_mhp_acpi_invalid_slot_selected(mem_st
->selector
);
112 case 0x0: /* DIMM slot selector */
113 mem_st
->selector
= data
;
114 trace_mhp_acpi_write_slot(mem_st
->selector
);
116 case 0x4: /* _OST event */
117 mdev
= &mem_st
->devs
[mem_st
->selector
];
119 /* TODO: handle device insert OST event */
120 } else if (data
== 3) {
121 /* TODO: handle device remove OST event */
123 mdev
->ost_event
= data
;
124 trace_mhp_acpi_write_ost_ev(mem_st
->selector
, mdev
->ost_event
);
126 case 0x8: /* _OST status */
127 mdev
= &mem_st
->devs
[mem_st
->selector
];
128 mdev
->ost_status
= data
;
129 trace_mhp_acpi_write_ost_status(mem_st
->selector
, mdev
->ost_status
);
130 /* TODO: implement memory removal on guest signal */
132 info
= acpi_memory_device_status(mem_st
->selector
, mdev
);
133 qapi_event_send_acpi_device_ost(info
, &error_abort
);
134 qapi_free_ACPIOSTInfo(info
);
136 case 0x14: /* set is_* fields */
137 mdev
= &mem_st
->devs
[mem_st
->selector
];
138 if (data
& 2) { /* clear insert event */
139 mdev
->is_inserting
= false;
140 trace_mhp_acpi_clear_insert_evt(mem_st
->selector
);
141 } else if (data
& 4) {
142 mdev
->is_removing
= false;
143 trace_mhp_acpi_clear_remove_evt(mem_st
->selector
);
144 } else if (data
& 8) {
145 if (!mdev
->is_enabled
) {
146 trace_mhp_acpi_ejecting_invalid_slot(mem_st
->selector
);
150 dev
= DEVICE(mdev
->dimm
);
151 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
152 /* call pc-dimm unplug cb */
153 hotplug_handler_unplug(hotplug_ctrl
, dev
, &local_err
);
155 trace_mhp_acpi_pc_dimm_delete_failed(mem_st
->selector
);
156 qapi_event_send_mem_unplug_error(dev
->id
,
157 error_get_pretty(local_err
),
159 error_free(local_err
);
162 trace_mhp_acpi_pc_dimm_deleted(mem_st
->selector
);
170 static const MemoryRegionOps acpi_memory_hotplug_ops
= {
171 .read
= acpi_memory_hotplug_read
,
172 .write
= acpi_memory_hotplug_write
,
173 .endianness
= DEVICE_LITTLE_ENDIAN
,
175 .min_access_size
= 1,
176 .max_access_size
= 4,
180 void acpi_memory_hotplug_init(MemoryRegion
*as
, Object
*owner
,
181 MemHotplugState
*state
)
183 MachineState
*machine
= MACHINE(qdev_get_machine());
185 state
->dev_count
= machine
->ram_slots
;
186 if (!state
->dev_count
) {
190 state
->devs
= g_malloc0(sizeof(*state
->devs
) * state
->dev_count
);
191 memory_region_init_io(&state
->io
, owner
, &acpi_memory_hotplug_ops
, state
,
192 "acpi-mem-hotplug", ACPI_MEMORY_HOTPLUG_IO_LEN
);
193 memory_region_add_subregion(as
, ACPI_MEMORY_HOTPLUG_BASE
, &state
->io
);
197 * acpi_memory_slot_status:
198 * @mem_st: memory hotplug state
200 * @errp: set in case of an error
202 * Obtain a single memory slot status.
204 * This function will be called by memory unplug request cb and unplug cb.
207 acpi_memory_slot_status(MemHotplugState
*mem_st
,
208 DeviceState
*dev
, Error
**errp
)
210 Error
*local_err
= NULL
;
211 int slot
= object_property_get_int(OBJECT(dev
), PC_DIMM_SLOT_PROP
,
215 error_propagate(errp
, local_err
);
219 if (slot
>= mem_st
->dev_count
) {
220 char *dev_path
= object_get_canonical_path(OBJECT(dev
));
221 error_setg(errp
, "acpi_memory_slot_status: "
222 "device [%s] returned invalid memory slot[%d]",
228 return &mem_st
->devs
[slot
];
231 void acpi_memory_plug_cb(ACPIREGS
*ar
, qemu_irq irq
, MemHotplugState
*mem_st
,
232 DeviceState
*dev
, Error
**errp
)
235 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
237 if (!dc
->hotpluggable
) {
241 mdev
= acpi_memory_slot_status(mem_st
, dev
, errp
);
247 mdev
->is_enabled
= true;
248 if (dev
->hotplugged
) {
249 mdev
->is_inserting
= true;
252 acpi_send_gpe_event(ar
, irq
, ACPI_MEMORY_HOTPLUG_STATUS
);
256 void acpi_memory_unplug_request_cb(ACPIREGS
*ar
, qemu_irq irq
,
257 MemHotplugState
*mem_st
,
258 DeviceState
*dev
, Error
**errp
)
262 mdev
= acpi_memory_slot_status(mem_st
, dev
, errp
);
267 mdev
->is_removing
= true;
270 acpi_send_gpe_event(ar
, irq
, ACPI_MEMORY_HOTPLUG_STATUS
);
273 void acpi_memory_unplug_cb(MemHotplugState
*mem_st
,
274 DeviceState
*dev
, Error
**errp
)
278 mdev
= acpi_memory_slot_status(mem_st
, dev
, errp
);
283 mdev
->is_enabled
= false;
287 static const VMStateDescription vmstate_memhp_sts
= {
288 .name
= "memory hotplug device state",
290 .minimum_version_id
= 1,
291 .minimum_version_id_old
= 1,
292 .fields
= (VMStateField
[]) {
293 VMSTATE_BOOL(is_enabled
, MemStatus
),
294 VMSTATE_BOOL(is_inserting
, MemStatus
),
295 VMSTATE_UINT32(ost_event
, MemStatus
),
296 VMSTATE_UINT32(ost_status
, MemStatus
),
297 VMSTATE_END_OF_LIST()
301 const VMStateDescription vmstate_memory_hotplug
= {
302 .name
= "memory hotplug state",
304 .minimum_version_id
= 1,
305 .minimum_version_id_old
= 1,
306 .fields
= (VMStateField
[]) {
307 VMSTATE_UINT32(selector
, MemHotplugState
),
308 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs
, MemHotplugState
, dev_count
,
309 vmstate_memhp_sts
, MemStatus
),
310 VMSTATE_END_OF_LIST()