blob: d926f4f77dd1ae9dc488fd169ea5237569d5f608 [file] [log] [blame]
Peter Maydellb6a0aa02016-01-26 18:17:03 +00001#include "qemu/osdep.h"
Igor Mammedov3ef77ac2014-06-02 15:25:16 +02002#include "hw/acpi/memory_hotplug.h"
Igor Mammedov3ef77ac2014-06-02 15:25:16 +02003#include "hw/mem/pc-dimm.h"
Zhu Guihuac06b2ff2015-04-27 16:47:21 +08004#include "hw/qdev-core.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +02005#include "migration/vmstate.h"
Igor Mammedovdfe292f2014-06-02 15:25:17 +02006#include "trace.h"
Markus Armbrustere688df62018-02-01 12:18:31 +01007#include "qapi/error.h"
Philippe Mathieu-Daudé27c91882020-09-13 21:53:47 +02008#include "qapi/qapi-events-acpi.h"
Philippe Mathieu-Daudéb495ec62020-09-13 21:53:46 +02009#include "qapi/qapi-events-machine.h"
Daniel Henrique Barboza46f2c282021-09-06 21:47:55 -030010#include "qapi/qapi-events-qdev.h"
Igor Mammedov3ef77ac2014-06-02 15:25:16 +020011
Igor Mammedovc9c08542016-12-06 00:32:27 +010012#define MEMORY_SLOTS_NUMBER "MDNR"
13#define MEMORY_HOTPLUG_IO_REGION "HPMR"
14#define MEMORY_SLOT_ADDR_LOW "MRBL"
15#define MEMORY_SLOT_ADDR_HIGH "MRBH"
16#define MEMORY_SLOT_SIZE_LOW "MRLL"
17#define MEMORY_SLOT_SIZE_HIGH "MRLH"
18#define MEMORY_SLOT_PROXIMITY "MPX"
19#define MEMORY_SLOT_ENABLED "MES"
20#define MEMORY_SLOT_INSERT_EVENT "MINS"
21#define MEMORY_SLOT_REMOVE_EVENT "MRMV"
22#define MEMORY_SLOT_EJECT "MEJ"
23#define MEMORY_SLOT_SLECTOR "MSEL"
24#define MEMORY_SLOT_OST_EVENT "MOEV"
25#define MEMORY_SLOT_OST_STATUS "MOSC"
26#define MEMORY_SLOT_LOCK "MLCK"
27#define MEMORY_SLOT_STATUS_METHOD "MRST"
28#define MEMORY_SLOT_CRS_METHOD "MCRS"
29#define MEMORY_SLOT_OST_METHOD "MOST"
30#define MEMORY_SLOT_PROXIMITY_METHOD "MPXM"
31#define MEMORY_SLOT_EJECT_METHOD "MEJ0"
32#define MEMORY_SLOT_NOTIFY_METHOD "MTFY"
Igor Mammedovc9c08542016-12-06 00:32:27 +010033#define MEMORY_HOTPLUG_DEVICE "MHPD"
34
Igor Mammedov43f50412014-06-16 19:12:27 +020035static ACPIOSTInfo *acpi_memory_device_status(int slot, MemStatus *mdev)
36{
37 ACPIOSTInfo *info = g_new0(ACPIOSTInfo, 1);
38
39 info->slot_type = ACPI_SLOT_TYPE_DIMM;
40 info->slot = g_strdup_printf("%d", slot);
41 info->source = mdev->ost_event;
42 info->status = mdev->ost_status;
43 if (mdev->dimm) {
44 DeviceState *dev = DEVICE(mdev->dimm);
45 if (dev->id) {
46 info->device = g_strdup(dev->id);
Igor Mammedov43f50412014-06-16 19:12:27 +020047 }
48 }
49 return info;
50}
51
52void acpi_memory_ospm_status(MemHotplugState *mem_st, ACPIOSTInfoList ***list)
53{
Eric Blakec3033fd2021-01-13 16:10:12 -060054 ACPIOSTInfoList ***tail = list;
Igor Mammedov43f50412014-06-16 19:12:27 +020055 int i;
56
57 for (i = 0; i < mem_st->dev_count; i++) {
Eric Blakec3033fd2021-01-13 16:10:12 -060058 QAPI_LIST_APPEND(*tail,
59 acpi_memory_device_status(i, &mem_st->devs[i]));
Igor Mammedov43f50412014-06-16 19:12:27 +020060 }
61}
62
Igor Mammedov3ef77ac2014-06-02 15:25:16 +020063static uint64_t acpi_memory_hotplug_read(void *opaque, hwaddr addr,
64 unsigned int size)
65{
66 uint32_t val = 0;
67 MemHotplugState *mem_st = opaque;
68 MemStatus *mdev;
69 Object *o;
70
71 if (mem_st->selector >= mem_st->dev_count) {
Igor Mammedovdfe292f2014-06-02 15:25:17 +020072 trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +020073 return 0;
74 }
75
76 mdev = &mem_st->devs[mem_st->selector];
77 o = OBJECT(mdev->dimm);
78 switch (addr) {
79 case 0x0: /* Lo part of phys address where DIMM is mapped */
Marc-André Lureau9ed442b2017-06-07 20:36:13 +040080 val = o ? object_property_get_uint(o, PC_DIMM_ADDR_PROP, NULL) : 0;
Igor Mammedovdfe292f2014-06-02 15:25:17 +020081 trace_mhp_acpi_read_addr_lo(mem_st->selector, val);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +020082 break;
83 case 0x4: /* Hi part of phys address where DIMM is mapped */
Marc-André Lureau9ed442b2017-06-07 20:36:13 +040084 val =
85 o ? object_property_get_uint(o, PC_DIMM_ADDR_PROP, NULL) >> 32 : 0;
Igor Mammedovdfe292f2014-06-02 15:25:17 +020086 trace_mhp_acpi_read_addr_hi(mem_st->selector, val);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +020087 break;
88 case 0x8: /* Lo part of DIMM size */
Marc-André Lureaub053ef62017-06-07 20:36:14 +040089 val = o ? object_property_get_uint(o, PC_DIMM_SIZE_PROP, NULL) : 0;
Igor Mammedovdfe292f2014-06-02 15:25:17 +020090 trace_mhp_acpi_read_size_lo(mem_st->selector, val);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +020091 break;
92 case 0xc: /* Hi part of DIMM size */
Marc-André Lureaub053ef62017-06-07 20:36:14 +040093 val =
94 o ? object_property_get_uint(o, PC_DIMM_SIZE_PROP, NULL) >> 32 : 0;
Igor Mammedovdfe292f2014-06-02 15:25:17 +020095 trace_mhp_acpi_read_size_hi(mem_st->selector, val);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +020096 break;
97 case 0x10: /* node proximity for _PXM method */
Marc-André Lureau9ed442b2017-06-07 20:36:13 +040098 val = o ? object_property_get_uint(o, PC_DIMM_NODE_PROP, NULL) : 0;
Igor Mammedovdfe292f2014-06-02 15:25:17 +020099 trace_mhp_acpi_read_pxm(mem_st->selector, val);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200100 break;
101 case 0x14: /* pack and return is_* fields */
102 val |= mdev->is_enabled ? 1 : 0;
103 val |= mdev->is_inserting ? 2 : 0;
Tang Chen64fec582015-04-27 16:47:17 +0800104 val |= mdev->is_removing ? 4 : 0;
Igor Mammedovdfe292f2014-06-02 15:25:17 +0200105 trace_mhp_acpi_read_flags(mem_st->selector, val);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200106 break;
107 default:
108 val = ~0;
109 break;
110 }
111 return val;
112}
113
114static void acpi_memory_hotplug_write(void *opaque, hwaddr addr, uint64_t data,
115 unsigned int size)
116{
117 MemHotplugState *mem_st = opaque;
118 MemStatus *mdev;
Igor Mammedov5f41fbb2014-06-23 15:26:57 +0200119 ACPIOSTInfo *info;
Zhu Guihuac06b2ff2015-04-27 16:47:21 +0800120 DeviceState *dev = NULL;
121 HotplugHandler *hotplug_ctrl = NULL;
Zhu Guihuabc09e062015-04-27 16:47:22 +0800122 Error *local_err = NULL;
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200123
124 if (!mem_st->dev_count) {
125 return;
126 }
127
128 if (addr) {
129 if (mem_st->selector >= mem_st->dev_count) {
Igor Mammedovdfe292f2014-06-02 15:25:17 +0200130 trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200131 return;
132 }
133 }
134
135 switch (addr) {
136 case 0x0: /* DIMM slot selector */
137 mem_st->selector = data;
Igor Mammedovdfe292f2014-06-02 15:25:17 +0200138 trace_mhp_acpi_write_slot(mem_st->selector);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200139 break;
140 case 0x4: /* _OST event */
141 mdev = &mem_st->devs[mem_st->selector];
142 if (data == 1) {
143 /* TODO: handle device insert OST event */
144 } else if (data == 3) {
145 /* TODO: handle device remove OST event */
146 }
147 mdev->ost_event = data;
Igor Mammedovdfe292f2014-06-02 15:25:17 +0200148 trace_mhp_acpi_write_ost_ev(mem_st->selector, mdev->ost_event);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200149 break;
150 case 0x8: /* _OST status */
151 mdev = &mem_st->devs[mem_st->selector];
152 mdev->ost_status = data;
Igor Mammedovdfe292f2014-06-02 15:25:17 +0200153 trace_mhp_acpi_write_ost_status(mem_st->selector, mdev->ost_status);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200154 /* TODO: implement memory removal on guest signal */
Igor Mammedov5f41fbb2014-06-23 15:26:57 +0200155
156 info = acpi_memory_device_status(mem_st->selector, mdev);
Peter Xu3ab72382018-08-15 21:37:37 +0800157 qapi_event_send_acpi_device_ost(info);
Igor Mammedov5f41fbb2014-06-23 15:26:57 +0200158 qapi_free_ACPIOSTInfo(info);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200159 break;
Zhu Guihuac06b2ff2015-04-27 16:47:21 +0800160 case 0x14: /* set is_* fields */
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200161 mdev = &mem_st->devs[mem_st->selector];
162 if (data & 2) { /* clear insert event */
163 mdev->is_inserting = false;
Igor Mammedovdfe292f2014-06-02 15:25:17 +0200164 trace_mhp_acpi_clear_insert_evt(mem_st->selector);
Zhu Guihuac06b2ff2015-04-27 16:47:21 +0800165 } else if (data & 4) {
166 mdev->is_removing = false;
167 trace_mhp_acpi_clear_remove_evt(mem_st->selector);
168 } else if (data & 8) {
169 if (!mdev->is_enabled) {
170 trace_mhp_acpi_ejecting_invalid_slot(mem_st->selector);
171 break;
172 }
173
174 dev = DEVICE(mdev->dimm);
175 hotplug_ctrl = qdev_get_hotplug_handler(dev);
176 /* call pc-dimm unplug cb */
Zhu Guihuabc09e062015-04-27 16:47:22 +0800177 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
178 if (local_err) {
179 trace_mhp_acpi_pc_dimm_delete_failed(mem_st->selector);
Daniel Henrique Barboza46f2c282021-09-06 21:47:55 -0300180
181 /*
182 * Send both MEM_UNPLUG_ERROR and DEVICE_UNPLUG_GUEST_ERROR
183 * while the deprecation of MEM_UNPLUG_ERROR is
184 * pending.
185 */
Daniel Henrique Barboza99b2c062021-09-06 21:47:49 -0300186 qapi_event_send_mem_unplug_error(dev->id ? : "",
Peter Xu3ab72382018-08-15 21:37:37 +0800187 error_get_pretty(local_err));
Markus Armbruster047f2ca2022-11-04 17:07:02 +0100188 qapi_event_send_device_unplug_guest_error(dev->id,
Daniel Henrique Barboza46f2c282021-09-06 21:47:55 -0300189 dev->canonical_path);
Stefano Dong (董兴水)903a41d2015-11-26 12:00:12 +0000190 error_free(local_err);
Zhu Guihuabc09e062015-04-27 16:47:22 +0800191 break;
192 }
David Hildenbrand07578b02019-02-28 13:28:47 +0100193 object_unparent(OBJECT(dev));
Zhu Guihuac06b2ff2015-04-27 16:47:21 +0800194 trace_mhp_acpi_pc_dimm_deleted(mem_st->selector);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200195 }
196 break;
Zhu Guihuac06b2ff2015-04-27 16:47:21 +0800197 default:
198 break;
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200199 }
200
201}
202static const MemoryRegionOps acpi_memory_hotplug_ops = {
203 .read = acpi_memory_hotplug_read,
204 .write = acpi_memory_hotplug_write,
205 .endianness = DEVICE_LITTLE_ENDIAN,
206 .valid = {
207 .min_access_size = 1,
208 .max_access_size = 4,
209 },
210};
211
212void acpi_memory_hotplug_init(MemoryRegion *as, Object *owner,
Shameer Kolothum091c4662019-09-18 14:06:23 +0100213 MemHotplugState *state, hwaddr io_base)
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200214{
215 MachineState *machine = MACHINE(qdev_get_machine());
216
217 state->dev_count = machine->ram_slots;
218 if (!state->dev_count) {
219 return;
220 }
221
222 state->devs = g_malloc0(sizeof(*state->devs) * state->dev_count);
223 memory_region_init_io(&state->io, owner, &acpi_memory_hotplug_ops, state,
Igor Mammedov80db0e72016-12-06 00:32:28 +0100224 "acpi-mem-hotplug", MEMORY_HOTPLUG_IO_LEN);
Shameer Kolothum091c4662019-09-18 14:06:23 +0100225 memory_region_add_subregion(as, io_base, &state->io);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200226}
227
Tang Chen4aae99b2015-04-27 16:47:16 +0800228/**
229 * acpi_memory_slot_status:
230 * @mem_st: memory hotplug state
231 * @dev: device
232 * @errp: set in case of an error
233 *
234 * Obtain a single memory slot status.
235 *
236 * This function will be called by memory unplug request cb and unplug cb.
237 */
238static MemStatus *
239acpi_memory_slot_status(MemHotplugState *mem_st,
240 DeviceState *dev, Error **errp)
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200241{
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200242 Error *local_err = NULL;
Tang Chen1d5157012015-02-26 09:16:43 +0800243 int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
244 &local_err);
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200245
246 if (local_err) {
247 error_propagate(errp, local_err);
Tang Chen4aae99b2015-04-27 16:47:16 +0800248 return NULL;
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200249 }
250
251 if (slot >= mem_st->dev_count) {
252 char *dev_path = object_get_canonical_path(OBJECT(dev));
Tang Chen4aae99b2015-04-27 16:47:16 +0800253 error_setg(errp, "acpi_memory_slot_status: "
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200254 "device [%s] returned invalid memory slot[%d]",
255 dev_path, slot);
256 g_free(dev_path);
Tang Chen4aae99b2015-04-27 16:47:16 +0800257 return NULL;
258 }
259
260 return &mem_st->devs[slot];
261}
262
Igor Mammedov0058c082016-05-31 12:01:17 +0200263void acpi_memory_plug_cb(HotplugHandler *hotplug_dev, MemHotplugState *mem_st,
Tang Chen4aae99b2015-04-27 16:47:16 +0800264 DeviceState *dev, Error **errp)
265{
266 MemStatus *mdev;
Xiao Guangrong75f27492016-11-07 19:13:38 +0800267 DeviceClass *dc = DEVICE_GET_CLASS(dev);
268
269 if (!dc->hotpluggable) {
270 return;
271 }
Tang Chen4aae99b2015-04-27 16:47:16 +0800272
273 mdev = acpi_memory_slot_status(mem_st, dev, errp);
274 if (!mdev) {
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200275 return;
276 }
277
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200278 mdev->dimm = dev;
Xiao Guangrong75f27492016-11-07 19:13:38 +0800279 mdev->is_enabled = true;
Igor Mammedov4828b102015-10-23 14:55:26 +0200280 if (dev->hotplugged) {
Xiao Guangrong75f27492016-11-07 19:13:38 +0800281 mdev->is_inserting = true;
282 acpi_send_event(DEVICE(hotplug_dev), ACPI_MEMORY_HOTPLUG_STATUS);
Igor Mammedov4828b102015-10-23 14:55:26 +0200283 }
Igor Mammedov3ef77ac2014-06-02 15:25:16 +0200284}
Igor Mammedovf816a622014-06-02 15:25:23 +0200285
Igor Mammedov0058c082016-05-31 12:01:17 +0200286void acpi_memory_unplug_request_cb(HotplugHandler *hotplug_dev,
Tang Chen64fec582015-04-27 16:47:17 +0800287 MemHotplugState *mem_st,
288 DeviceState *dev, Error **errp)
289{
290 MemStatus *mdev;
291
292 mdev = acpi_memory_slot_status(mem_st, dev, errp);
293 if (!mdev) {
294 return;
295 }
296
297 mdev->is_removing = true;
Igor Mammedov0058c082016-05-31 12:01:17 +0200298 acpi_send_event(DEVICE(hotplug_dev), ACPI_MEMORY_HOTPLUG_STATUS);
Tang Chen64fec582015-04-27 16:47:17 +0800299}
300
Tang Chenf7d3e292015-04-27 16:47:18 +0800301void acpi_memory_unplug_cb(MemHotplugState *mem_st,
302 DeviceState *dev, Error **errp)
303{
304 MemStatus *mdev;
305
306 mdev = acpi_memory_slot_status(mem_st, dev, errp);
307 if (!mdev) {
308 return;
309 }
310
311 mdev->is_enabled = false;
312 mdev->dimm = NULL;
313}
314
Igor Mammedovf816a622014-06-02 15:25:23 +0200315static const VMStateDescription vmstate_memhp_sts = {
316 .name = "memory hotplug device state",
317 .version_id = 1,
318 .minimum_version_id = 1,
Igor Mammedovf816a622014-06-02 15:25:23 +0200319 .fields = (VMStateField[]) {
320 VMSTATE_BOOL(is_enabled, MemStatus),
321 VMSTATE_BOOL(is_inserting, MemStatus),
322 VMSTATE_UINT32(ost_event, MemStatus),
323 VMSTATE_UINT32(ost_status, MemStatus),
324 VMSTATE_END_OF_LIST()
325 }
326};
327
328const VMStateDescription vmstate_memory_hotplug = {
329 .name = "memory hotplug state",
330 .version_id = 1,
331 .minimum_version_id = 1,
Igor Mammedovf816a622014-06-02 15:25:23 +0200332 .fields = (VMStateField[]) {
333 VMSTATE_UINT32(selector, MemHotplugState),
334 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs, MemHotplugState, dev_count,
335 vmstate_memhp_sts, MemStatus),
336 VMSTATE_END_OF_LIST()
337 }
338};
Igor Mammedova2088da2016-12-06 00:32:22 +0100339
Igor Mammedov8dfba502016-12-06 00:32:24 +0100340void build_memory_hotplug_aml(Aml *table, uint32_t nr_mem,
Igor Mammedovd1957da2016-12-06 00:32:26 +0100341 const char *res_root,
Shameer Kolothum091c4662019-09-18 14:06:23 +0100342 const char *event_handler_method,
343 AmlRegionSpace rs, hwaddr memhp_io_base)
Igor Mammedova2088da2016-12-06 00:32:22 +0100344{
Igor Mammedov8b35ab22016-12-06 00:32:25 +0100345 int i;
Igor Mammedova2088da2016-12-06 00:32:22 +0100346 Aml *ifctx;
347 Aml *method;
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100348 Aml *dev_container;
Igor Mammedova2088da2016-12-06 00:32:22 +0100349 Aml *mem_ctrl_dev;
Igor Mammedov80db0e72016-12-06 00:32:28 +0100350 char *mhp_res_path;
Igor Mammedova2088da2016-12-06 00:32:22 +0100351
Igor Mammedov80db0e72016-12-06 00:32:28 +0100352 mhp_res_path = g_strdup_printf("%s." MEMORY_HOTPLUG_DEVICE, res_root);
Igor Mammedovd1957da2016-12-06 00:32:26 +0100353 mem_ctrl_dev = aml_device("%s", mhp_res_path);
Igor Mammedova2088da2016-12-06 00:32:22 +0100354 {
Igor Mammedov8dfba502016-12-06 00:32:24 +0100355 Aml *crs;
Igor Mammedova2088da2016-12-06 00:32:22 +0100356
357 aml_append(mem_ctrl_dev, aml_name_decl("_HID", aml_string("PNP0A06")));
358 aml_append(mem_ctrl_dev,
359 aml_name_decl("_UID", aml_string("Memory hotplug resources")));
360
Igor Mammedov8dfba502016-12-06 00:32:24 +0100361 crs = aml_resource_template();
Shameer Kolothum091c4662019-09-18 14:06:23 +0100362 if (rs == AML_SYSTEM_IO) {
363 aml_append(crs,
364 aml_io(AML_DECODE16, memhp_io_base, memhp_io_base, 0,
365 MEMORY_HOTPLUG_IO_LEN)
366 );
367 } else {
368 aml_append(crs, aml_memory32_fixed(memhp_io_base,
369 MEMORY_HOTPLUG_IO_LEN, AML_READ_WRITE));
370 }
Igor Mammedov8dfba502016-12-06 00:32:24 +0100371 aml_append(mem_ctrl_dev, aml_name_decl("_CRS", crs));
372
373 aml_append(mem_ctrl_dev, aml_operation_region(
Shameer Kolothum091c4662019-09-18 14:06:23 +0100374 MEMORY_HOTPLUG_IO_REGION, rs,
Igor Mammedov80db0e72016-12-06 00:32:28 +0100375 aml_int(memhp_io_base), MEMORY_HOTPLUG_IO_LEN)
Igor Mammedov8dfba502016-12-06 00:32:24 +0100376 );
377
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100378 }
379 aml_append(table, mem_ctrl_dev);
380
381 dev_container = aml_device(MEMORY_DEVICES_CONTAINER);
382 {
383 Aml *field;
384 Aml *one = aml_int(1);
385 Aml *zero = aml_int(0);
386 Aml *ret_val = aml_local(0);
387 Aml *slot_arg0 = aml_arg(0);
388 Aml *slots_nr = aml_name(MEMORY_SLOTS_NUMBER);
389 Aml *ctrl_lock = aml_name(MEMORY_SLOT_LOCK);
390 Aml *slot_selector = aml_name(MEMORY_SLOT_SLECTOR);
391 char *mmio_path = g_strdup_printf("%s." MEMORY_HOTPLUG_IO_REGION,
392 mhp_res_path);
393
394 aml_append(dev_container, aml_name_decl("_HID", aml_string("PNP0A06")));
395 aml_append(dev_container,
396 aml_name_decl("_UID", aml_string("DIMM devices")));
397
398 assert(nr_mem <= ACPI_MAX_RAM_SLOTS);
399 aml_append(dev_container,
400 aml_name_decl(MEMORY_SLOTS_NUMBER, aml_int(nr_mem))
401 );
402
403 field = aml_field(mmio_path, AML_DWORD_ACC,
Igor Mammedov8dfba502016-12-06 00:32:24 +0100404 AML_NOLOCK, AML_PRESERVE);
405 aml_append(field, /* read only */
406 aml_named_field(MEMORY_SLOT_ADDR_LOW, 32));
407 aml_append(field, /* read only */
408 aml_named_field(MEMORY_SLOT_ADDR_HIGH, 32));
409 aml_append(field, /* read only */
410 aml_named_field(MEMORY_SLOT_SIZE_LOW, 32));
411 aml_append(field, /* read only */
412 aml_named_field(MEMORY_SLOT_SIZE_HIGH, 32));
413 aml_append(field, /* read only */
414 aml_named_field(MEMORY_SLOT_PROXIMITY, 32));
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100415 aml_append(dev_container, field);
Igor Mammedov8dfba502016-12-06 00:32:24 +0100416
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100417 field = aml_field(mmio_path, AML_BYTE_ACC,
Igor Mammedov8dfba502016-12-06 00:32:24 +0100418 AML_NOLOCK, AML_WRITE_AS_ZEROS);
419 aml_append(field, aml_reserved_field(160 /* bits, Offset(20) */));
420 aml_append(field, /* 1 if enabled, read only */
421 aml_named_field(MEMORY_SLOT_ENABLED, 1));
422 aml_append(field,
423 /*(read) 1 if has a insert event. (write) 1 to clear event */
424 aml_named_field(MEMORY_SLOT_INSERT_EVENT, 1));
425 aml_append(field,
426 /* (read) 1 if has a remove event. (write) 1 to clear event */
427 aml_named_field(MEMORY_SLOT_REMOVE_EVENT, 1));
428 aml_append(field,
429 /* initiates device eject, write only */
430 aml_named_field(MEMORY_SLOT_EJECT, 1));
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100431 aml_append(dev_container, field);
Igor Mammedov8dfba502016-12-06 00:32:24 +0100432
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100433 field = aml_field(mmio_path, AML_DWORD_ACC,
Igor Mammedov8dfba502016-12-06 00:32:24 +0100434 AML_NOLOCK, AML_PRESERVE);
435 aml_append(field, /* DIMM selector, write only */
436 aml_named_field(MEMORY_SLOT_SLECTOR, 32));
437 aml_append(field, /* _OST event code, write only */
438 aml_named_field(MEMORY_SLOT_OST_EVENT, 32));
439 aml_append(field, /* _OST status code, write only */
440 aml_named_field(MEMORY_SLOT_OST_STATUS, 32));
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100441 aml_append(dev_container, field);
442 g_free(mmio_path);
Igor Mammedov8dfba502016-12-06 00:32:24 +0100443
Igor Mammedova2088da2016-12-06 00:32:22 +0100444 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
445 ifctx = aml_if(aml_equal(slots_nr, zero));
446 {
447 aml_append(ifctx, aml_return(zero));
448 }
449 aml_append(method, ifctx);
450 /* present, functioning, decoding, not shown in UI */
451 aml_append(method, aml_return(aml_int(0xB)));
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100452 aml_append(dev_container, method);
Igor Mammedova2088da2016-12-06 00:32:22 +0100453
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100454 aml_append(dev_container, aml_mutex(MEMORY_SLOT_LOCK, 0));
Igor Mammedova2088da2016-12-06 00:32:22 +0100455
456 method = aml_method(MEMORY_SLOT_SCAN_METHOD, 0, AML_NOTSERIALIZED);
457 {
458 Aml *else_ctx;
459 Aml *while_ctx;
460 Aml *idx = aml_local(0);
461 Aml *eject_req = aml_int(3);
462 Aml *dev_chk = aml_int(1);
463
464 ifctx = aml_if(aml_equal(slots_nr, zero));
465 {
466 aml_append(ifctx, aml_return(zero));
467 }
468 aml_append(method, ifctx);
469
470 aml_append(method, aml_store(zero, idx));
471 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
472 /* build AML that:
473 * loops over all slots and Notifies DIMMs with
474 * Device Check or Eject Request notifications if
475 * slot has corresponding status bit set and clears
476 * slot status.
477 */
478 while_ctx = aml_while(aml_lless(idx, slots_nr));
479 {
480 Aml *ins_evt = aml_name(MEMORY_SLOT_INSERT_EVENT);
481 Aml *rm_evt = aml_name(MEMORY_SLOT_REMOVE_EVENT);
482
483 aml_append(while_ctx, aml_store(idx, slot_selector));
484 ifctx = aml_if(aml_equal(ins_evt, one));
485 {
486 aml_append(ifctx,
487 aml_call2(MEMORY_SLOT_NOTIFY_METHOD,
488 idx, dev_chk));
489 aml_append(ifctx, aml_store(one, ins_evt));
490 }
491 aml_append(while_ctx, ifctx);
492
493 else_ctx = aml_else();
494 ifctx = aml_if(aml_equal(rm_evt, one));
495 {
496 aml_append(ifctx,
497 aml_call2(MEMORY_SLOT_NOTIFY_METHOD,
498 idx, eject_req));
499 aml_append(ifctx, aml_store(one, rm_evt));
500 }
501 aml_append(else_ctx, ifctx);
502 aml_append(while_ctx, else_ctx);
503
504 aml_append(while_ctx, aml_add(idx, one, idx));
505 }
506 aml_append(method, while_ctx);
507 aml_append(method, aml_release(ctrl_lock));
508 aml_append(method, aml_return(one));
509 }
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100510 aml_append(dev_container, method);
Igor Mammedova2088da2016-12-06 00:32:22 +0100511
512 method = aml_method(MEMORY_SLOT_STATUS_METHOD, 1, AML_NOTSERIALIZED);
513 {
514 Aml *slot_enabled = aml_name(MEMORY_SLOT_ENABLED);
515
516 aml_append(method, aml_store(zero, ret_val));
517 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
518 aml_append(method,
519 aml_store(aml_to_integer(slot_arg0), slot_selector));
520
521 ifctx = aml_if(aml_equal(slot_enabled, one));
522 {
523 aml_append(ifctx, aml_store(aml_int(0xF), ret_val));
524 }
525 aml_append(method, ifctx);
526
527 aml_append(method, aml_release(ctrl_lock));
528 aml_append(method, aml_return(ret_val));
529 }
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100530 aml_append(dev_container, method);
Igor Mammedova2088da2016-12-06 00:32:22 +0100531
532 method = aml_method(MEMORY_SLOT_CRS_METHOD, 1, AML_SERIALIZED);
533 {
534 Aml *mr64 = aml_name("MR64");
535 Aml *mr32 = aml_name("MR32");
536 Aml *crs_tmpl = aml_resource_template();
537 Aml *minl = aml_name("MINL");
538 Aml *minh = aml_name("MINH");
539 Aml *maxl = aml_name("MAXL");
540 Aml *maxh = aml_name("MAXH");
541 Aml *lenl = aml_name("LENL");
542 Aml *lenh = aml_name("LENH");
543
544 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
545 aml_append(method, aml_store(aml_to_integer(slot_arg0),
546 slot_selector));
547
548 aml_append(crs_tmpl,
549 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
550 AML_CACHEABLE, AML_READ_WRITE,
551 0, 0x0, 0xFFFFFFFFFFFFFFFEULL, 0,
552 0xFFFFFFFFFFFFFFFFULL));
553 aml_append(method, aml_name_decl("MR64", crs_tmpl));
554 aml_append(method,
555 aml_create_dword_field(mr64, aml_int(14), "MINL"));
556 aml_append(method,
557 aml_create_dword_field(mr64, aml_int(18), "MINH"));
558 aml_append(method,
559 aml_create_dword_field(mr64, aml_int(38), "LENL"));
560 aml_append(method,
561 aml_create_dword_field(mr64, aml_int(42), "LENH"));
562 aml_append(method,
563 aml_create_dword_field(mr64, aml_int(22), "MAXL"));
564 aml_append(method,
565 aml_create_dword_field(mr64, aml_int(26), "MAXH"));
566
567 aml_append(method,
568 aml_store(aml_name(MEMORY_SLOT_ADDR_HIGH), minh));
569 aml_append(method,
570 aml_store(aml_name(MEMORY_SLOT_ADDR_LOW), minl));
571 aml_append(method,
572 aml_store(aml_name(MEMORY_SLOT_SIZE_HIGH), lenh));
573 aml_append(method,
574 aml_store(aml_name(MEMORY_SLOT_SIZE_LOW), lenl));
575
576 /* 64-bit math: MAX = MIN + LEN - 1 */
577 aml_append(method, aml_add(minl, lenl, maxl));
578 aml_append(method, aml_add(minh, lenh, maxh));
579 ifctx = aml_if(aml_lless(maxl, minl));
580 {
581 aml_append(ifctx, aml_add(maxh, one, maxh));
582 }
583 aml_append(method, ifctx);
584 ifctx = aml_if(aml_lless(maxl, one));
585 {
586 aml_append(ifctx, aml_subtract(maxh, one, maxh));
587 }
588 aml_append(method, ifctx);
589 aml_append(method, aml_subtract(maxl, one, maxl));
590
591 /* return 32-bit _CRS if addr/size is in low mem */
592 /* TODO: remove it since all hotplugged DIMMs are in high mem */
593 ifctx = aml_if(aml_equal(maxh, zero));
594 {
595 crs_tmpl = aml_resource_template();
596 aml_append(crs_tmpl,
597 aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED,
598 AML_MAX_FIXED, AML_CACHEABLE,
599 AML_READ_WRITE,
600 0, 0x0, 0xFFFFFFFE, 0,
601 0xFFFFFFFF));
602 aml_append(ifctx, aml_name_decl("MR32", crs_tmpl));
603 aml_append(ifctx,
604 aml_create_dword_field(mr32, aml_int(10), "MIN"));
605 aml_append(ifctx,
606 aml_create_dword_field(mr32, aml_int(14), "MAX"));
607 aml_append(ifctx,
608 aml_create_dword_field(mr32, aml_int(22), "LEN"));
609 aml_append(ifctx, aml_store(minl, aml_name("MIN")));
610 aml_append(ifctx, aml_store(maxl, aml_name("MAX")));
611 aml_append(ifctx, aml_store(lenl, aml_name("LEN")));
612
613 aml_append(ifctx, aml_release(ctrl_lock));
614 aml_append(ifctx, aml_return(mr32));
615 }
616 aml_append(method, ifctx);
617
618 aml_append(method, aml_release(ctrl_lock));
619 aml_append(method, aml_return(mr64));
620 }
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100621 aml_append(dev_container, method);
Igor Mammedova2088da2016-12-06 00:32:22 +0100622
623 method = aml_method(MEMORY_SLOT_PROXIMITY_METHOD, 1,
624 AML_NOTSERIALIZED);
625 {
626 Aml *proximity = aml_name(MEMORY_SLOT_PROXIMITY);
627
628 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
629 aml_append(method, aml_store(aml_to_integer(slot_arg0),
630 slot_selector));
631 aml_append(method, aml_store(proximity, ret_val));
632 aml_append(method, aml_release(ctrl_lock));
633 aml_append(method, aml_return(ret_val));
634 }
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100635 aml_append(dev_container, method);
Igor Mammedova2088da2016-12-06 00:32:22 +0100636
637 method = aml_method(MEMORY_SLOT_OST_METHOD, 4, AML_NOTSERIALIZED);
638 {
639 Aml *ost_evt = aml_name(MEMORY_SLOT_OST_EVENT);
640 Aml *ost_status = aml_name(MEMORY_SLOT_OST_STATUS);
641
642 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
643 aml_append(method, aml_store(aml_to_integer(slot_arg0),
644 slot_selector));
645 aml_append(method, aml_store(aml_arg(1), ost_evt));
646 aml_append(method, aml_store(aml_arg(2), ost_status));
647 aml_append(method, aml_release(ctrl_lock));
648 }
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100649 aml_append(dev_container, method);
Igor Mammedova2088da2016-12-06 00:32:22 +0100650
651 method = aml_method(MEMORY_SLOT_EJECT_METHOD, 2, AML_NOTSERIALIZED);
652 {
653 Aml *eject = aml_name(MEMORY_SLOT_EJECT);
654
655 aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
656 aml_append(method, aml_store(aml_to_integer(slot_arg0),
657 slot_selector));
658 aml_append(method, aml_store(one, eject));
659 aml_append(method, aml_release(ctrl_lock));
660 }
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100661 aml_append(dev_container, method);
662
663 /* build memory devices */
664 for (i = 0; i < nr_mem; i++) {
665 Aml *dev;
666 const char *s;
667
668 dev = aml_device("MP%02X", i);
669 aml_append(dev, aml_name_decl("_UID", aml_string("0x%02X", i)));
670 aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C80")));
671
672 method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
673 s = MEMORY_SLOT_CRS_METHOD;
674 aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
675 aml_append(dev, method);
676
677 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
678 s = MEMORY_SLOT_STATUS_METHOD;
679 aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
680 aml_append(dev, method);
681
682 method = aml_method("_PXM", 0, AML_NOTSERIALIZED);
683 s = MEMORY_SLOT_PROXIMITY_METHOD;
684 aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
685 aml_append(dev, method);
686
687 method = aml_method("_OST", 3, AML_NOTSERIALIZED);
688 s = MEMORY_SLOT_OST_METHOD;
Yang Zhonge6741322018-11-05 02:40:39 +0100689 aml_append(method,
690 aml_call4(s, aml_name("_UID"), aml_arg(0),
691 aml_arg(1), aml_arg(2)));
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100692 aml_append(dev, method);
693
694 method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
695 s = MEMORY_SLOT_EJECT_METHOD;
Yang Zhonge6741322018-11-05 02:40:39 +0100696 aml_append(method,
697 aml_call2(s, aml_name("_UID"), aml_arg(0)));
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100698 aml_append(dev, method);
699
700 aml_append(dev_container, dev);
701 }
702
703 /* build Method(MEMORY_SLOT_NOTIFY_METHOD, 2) {
704 * If (LEqual(Arg0, 0x00)) {Notify(MP00, Arg1)} ... }
705 */
706 method = aml_method(MEMORY_SLOT_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
707 for (i = 0; i < nr_mem; i++) {
708 ifctx = aml_if(aml_equal(aml_arg(0), aml_int(i)));
709 aml_append(ifctx,
710 aml_notify(aml_name("MP%.02X", i), aml_arg(1))
711 );
712 aml_append(method, ifctx);
713 }
714 aml_append(dev_container, method);
Igor Mammedova2088da2016-12-06 00:32:22 +0100715 }
Igor Mammedove1a58fc2016-12-06 00:32:29 +0100716 aml_append(table, dev_container);
Igor Mammedovd1957da2016-12-06 00:32:26 +0100717
Samuel Ortiz22338fe2019-09-18 14:06:24 +0100718 if (event_handler_method) {
719 method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
720 aml_append(method, aml_call0(MEMORY_DEVICES_CONTAINER "."
721 MEMORY_SLOT_SCAN_METHOD));
722 aml_append(table, method);
723 }
Igor Mammedovd1957da2016-12-06 00:32:26 +0100724
725 g_free(mhp_res_path);
Igor Mammedov75ff0f02016-12-06 00:32:23 +0100726}