xref: /openbmc/qemu/hw/acpi/memory_hotplug.c (revision 08da3180)
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"
5 #include "hw/boards.h"
6 #include "hw/qdev-core.h"
7 #include "trace.h"
8 #include "qapi-event.h"
9 
10 #define MEMORY_SLOTS_NUMBER          "MDNR"
11 #define MEMORY_HOTPLUG_IO_REGION     "HPMR"
12 #define MEMORY_SLOT_ADDR_LOW         "MRBL"
13 #define MEMORY_SLOT_ADDR_HIGH        "MRBH"
14 #define MEMORY_SLOT_SIZE_LOW         "MRLL"
15 #define MEMORY_SLOT_SIZE_HIGH        "MRLH"
16 #define MEMORY_SLOT_PROXIMITY        "MPX"
17 #define MEMORY_SLOT_ENABLED          "MES"
18 #define MEMORY_SLOT_INSERT_EVENT     "MINS"
19 #define MEMORY_SLOT_REMOVE_EVENT     "MRMV"
20 #define MEMORY_SLOT_EJECT            "MEJ"
21 #define MEMORY_SLOT_SLECTOR          "MSEL"
22 #define MEMORY_SLOT_OST_EVENT        "MOEV"
23 #define MEMORY_SLOT_OST_STATUS       "MOSC"
24 #define MEMORY_SLOT_LOCK             "MLCK"
25 #define MEMORY_SLOT_STATUS_METHOD    "MRST"
26 #define MEMORY_SLOT_CRS_METHOD       "MCRS"
27 #define MEMORY_SLOT_OST_METHOD       "MOST"
28 #define MEMORY_SLOT_PROXIMITY_METHOD "MPXM"
29 #define MEMORY_SLOT_EJECT_METHOD     "MEJ0"
30 #define MEMORY_SLOT_NOTIFY_METHOD    "MTFY"
31 #define MEMORY_SLOT_SCAN_METHOD      "MSCN"
32 #define MEMORY_HOTPLUG_DEVICE        "MHPD"
33 #define MEMORY_HOTPLUG_IO_LEN         24
34 #define MEMORY_DEVICES_CONTAINER     "\\_SB.MHPC"
35 
36 static uint16_t memhp_io_base;
37 
38 static ACPIOSTInfo *acpi_memory_device_status(int slot, MemStatus *mdev)
39 {
40     ACPIOSTInfo *info = g_new0(ACPIOSTInfo, 1);
41 
42     info->slot_type = ACPI_SLOT_TYPE_DIMM;
43     info->slot = g_strdup_printf("%d", slot);
44     info->source = mdev->ost_event;
45     info->status = mdev->ost_status;
46     if (mdev->dimm) {
47         DeviceState *dev = DEVICE(mdev->dimm);
48         if (dev->id) {
49             info->device = g_strdup(dev->id);
50             info->has_device = true;
51         }
52     }
53     return info;
54 }
55 
56 void acpi_memory_ospm_status(MemHotplugState *mem_st, ACPIOSTInfoList ***list)
57 {
58     int i;
59 
60     for (i = 0; i < mem_st->dev_count; i++) {
61         ACPIOSTInfoList *elem = g_new0(ACPIOSTInfoList, 1);
62         elem->value = acpi_memory_device_status(i, &mem_st->devs[i]);
63         elem->next = NULL;
64         **list = elem;
65         *list = &elem->next;
66     }
67 }
68 
69 static uint64_t acpi_memory_hotplug_read(void *opaque, hwaddr addr,
70                                          unsigned int size)
71 {
72     uint32_t val = 0;
73     MemHotplugState *mem_st = opaque;
74     MemStatus *mdev;
75     Object *o;
76 
77     if (mem_st->selector >= mem_st->dev_count) {
78         trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
79         return 0;
80     }
81 
82     mdev = &mem_st->devs[mem_st->selector];
83     o = OBJECT(mdev->dimm);
84     switch (addr) {
85     case 0x0: /* Lo part of phys address where DIMM is mapped */
86         val = o ? object_property_get_int(o, PC_DIMM_ADDR_PROP, NULL) : 0;
87         trace_mhp_acpi_read_addr_lo(mem_st->selector, val);
88         break;
89     case 0x4: /* Hi part of phys address where DIMM is mapped */
90         val = o ? object_property_get_int(o, PC_DIMM_ADDR_PROP, NULL) >> 32 : 0;
91         trace_mhp_acpi_read_addr_hi(mem_st->selector, val);
92         break;
93     case 0x8: /* Lo part of DIMM size */
94         val = o ? object_property_get_int(o, PC_DIMM_SIZE_PROP, NULL) : 0;
95         trace_mhp_acpi_read_size_lo(mem_st->selector, val);
96         break;
97     case 0xc: /* Hi part of DIMM size */
98         val = o ? object_property_get_int(o, PC_DIMM_SIZE_PROP, NULL) >> 32 : 0;
99         trace_mhp_acpi_read_size_hi(mem_st->selector, val);
100         break;
101     case 0x10: /* node proximity for _PXM method */
102         val = o ? object_property_get_int(o, PC_DIMM_NODE_PROP, NULL) : 0;
103         trace_mhp_acpi_read_pxm(mem_st->selector, val);
104         break;
105     case 0x14: /* pack and return is_* fields */
106         val |= mdev->is_enabled   ? 1 : 0;
107         val |= mdev->is_inserting ? 2 : 0;
108         val |= mdev->is_removing  ? 4 : 0;
109         trace_mhp_acpi_read_flags(mem_st->selector, val);
110         break;
111     default:
112         val = ~0;
113         break;
114     }
115     return val;
116 }
117 
118 static void acpi_memory_hotplug_write(void *opaque, hwaddr addr, uint64_t data,
119                                       unsigned int size)
120 {
121     MemHotplugState *mem_st = opaque;
122     MemStatus *mdev;
123     ACPIOSTInfo *info;
124     DeviceState *dev = NULL;
125     HotplugHandler *hotplug_ctrl = NULL;
126     Error *local_err = NULL;
127 
128     if (!mem_st->dev_count) {
129         return;
130     }
131 
132     if (addr) {
133         if (mem_st->selector >= mem_st->dev_count) {
134             trace_mhp_acpi_invalid_slot_selected(mem_st->selector);
135             return;
136         }
137     }
138 
139     switch (addr) {
140     case 0x0: /* DIMM slot selector */
141         mem_st->selector = data;
142         trace_mhp_acpi_write_slot(mem_st->selector);
143         break;
144     case 0x4: /* _OST event  */
145         mdev = &mem_st->devs[mem_st->selector];
146         if (data == 1) {
147             /* TODO: handle device insert OST event */
148         } else if (data == 3) {
149             /* TODO: handle device remove OST event */
150         }
151         mdev->ost_event = data;
152         trace_mhp_acpi_write_ost_ev(mem_st->selector, mdev->ost_event);
153         break;
154     case 0x8: /* _OST status */
155         mdev = &mem_st->devs[mem_st->selector];
156         mdev->ost_status = data;
157         trace_mhp_acpi_write_ost_status(mem_st->selector, mdev->ost_status);
158         /* TODO: implement memory removal on guest signal */
159 
160         info = acpi_memory_device_status(mem_st->selector, mdev);
161         qapi_event_send_acpi_device_ost(info, &error_abort);
162         qapi_free_ACPIOSTInfo(info);
163         break;
164     case 0x14: /* set is_* fields  */
165         mdev = &mem_st->devs[mem_st->selector];
166         if (data & 2) { /* clear insert event */
167             mdev->is_inserting  = false;
168             trace_mhp_acpi_clear_insert_evt(mem_st->selector);
169         } else if (data & 4) {
170             mdev->is_removing = false;
171             trace_mhp_acpi_clear_remove_evt(mem_st->selector);
172         } else if (data & 8) {
173             if (!mdev->is_enabled) {
174                 trace_mhp_acpi_ejecting_invalid_slot(mem_st->selector);
175                 break;
176             }
177 
178             dev = DEVICE(mdev->dimm);
179             hotplug_ctrl = qdev_get_hotplug_handler(dev);
180             /* call pc-dimm unplug cb */
181             hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
182             if (local_err) {
183                 trace_mhp_acpi_pc_dimm_delete_failed(mem_st->selector);
184                 qapi_event_send_mem_unplug_error(dev->id,
185                                                  error_get_pretty(local_err),
186                                                  &error_abort);
187                 error_free(local_err);
188                 break;
189             }
190             trace_mhp_acpi_pc_dimm_deleted(mem_st->selector);
191         }
192         break;
193     default:
194         break;
195     }
196 
197 }
198 static const MemoryRegionOps acpi_memory_hotplug_ops = {
199     .read = acpi_memory_hotplug_read,
200     .write = acpi_memory_hotplug_write,
201     .endianness = DEVICE_LITTLE_ENDIAN,
202     .valid = {
203         .min_access_size = 1,
204         .max_access_size = 4,
205     },
206 };
207 
208 void acpi_memory_hotplug_init(MemoryRegion *as, Object *owner,
209                               MemHotplugState *state, uint16_t io_base)
210 {
211     MachineState *machine = MACHINE(qdev_get_machine());
212 
213     state->dev_count = machine->ram_slots;
214     if (!state->dev_count) {
215         return;
216     }
217 
218     assert(!memhp_io_base);
219     memhp_io_base = io_base;
220     state->devs = g_malloc0(sizeof(*state->devs) * state->dev_count);
221     memory_region_init_io(&state->io, owner, &acpi_memory_hotplug_ops, state,
222                           "acpi-mem-hotplug", MEMORY_HOTPLUG_IO_LEN);
223     memory_region_add_subregion(as, memhp_io_base, &state->io);
224 }
225 
226 /**
227  * acpi_memory_slot_status:
228  * @mem_st: memory hotplug state
229  * @dev: device
230  * @errp: set in case of an error
231  *
232  * Obtain a single memory slot status.
233  *
234  * This function will be called by memory unplug request cb and unplug cb.
235  */
236 static MemStatus *
237 acpi_memory_slot_status(MemHotplugState *mem_st,
238                         DeviceState *dev, Error **errp)
239 {
240     Error *local_err = NULL;
241     int slot = object_property_get_int(OBJECT(dev), PC_DIMM_SLOT_PROP,
242                                        &local_err);
243 
244     if (local_err) {
245         error_propagate(errp, local_err);
246         return NULL;
247     }
248 
249     if (slot >= mem_st->dev_count) {
250         char *dev_path = object_get_canonical_path(OBJECT(dev));
251         error_setg(errp, "acpi_memory_slot_status: "
252                    "device [%s] returned invalid memory slot[%d]",
253                     dev_path, slot);
254         g_free(dev_path);
255         return NULL;
256     }
257 
258     return &mem_st->devs[slot];
259 }
260 
261 void acpi_memory_plug_cb(HotplugHandler *hotplug_dev, MemHotplugState *mem_st,
262                          DeviceState *dev, Error **errp)
263 {
264     MemStatus *mdev;
265     DeviceClass *dc = DEVICE_GET_CLASS(dev);
266 
267     if (!dc->hotpluggable) {
268         return;
269     }
270 
271     mdev = acpi_memory_slot_status(mem_st, dev, errp);
272     if (!mdev) {
273         return;
274     }
275 
276     mdev->dimm = dev;
277     mdev->is_enabled = true;
278     if (dev->hotplugged) {
279         mdev->is_inserting = true;
280         acpi_send_event(DEVICE(hotplug_dev), ACPI_MEMORY_HOTPLUG_STATUS);
281     }
282 }
283 
284 void acpi_memory_unplug_request_cb(HotplugHandler *hotplug_dev,
285                                    MemHotplugState *mem_st,
286                                    DeviceState *dev, Error **errp)
287 {
288     MemStatus *mdev;
289 
290     mdev = acpi_memory_slot_status(mem_st, dev, errp);
291     if (!mdev) {
292         return;
293     }
294 
295     mdev->is_removing = true;
296     acpi_send_event(DEVICE(hotplug_dev), ACPI_MEMORY_HOTPLUG_STATUS);
297 }
298 
299 void acpi_memory_unplug_cb(MemHotplugState *mem_st,
300                            DeviceState *dev, Error **errp)
301 {
302     MemStatus *mdev;
303 
304     mdev = acpi_memory_slot_status(mem_st, dev, errp);
305     if (!mdev) {
306         return;
307     }
308 
309     mdev->is_enabled = false;
310     mdev->dimm = NULL;
311 }
312 
313 static const VMStateDescription vmstate_memhp_sts = {
314     .name = "memory hotplug device state",
315     .version_id = 1,
316     .minimum_version_id = 1,
317     .minimum_version_id_old = 1,
318     .fields      = (VMStateField[]) {
319         VMSTATE_BOOL(is_enabled, MemStatus),
320         VMSTATE_BOOL(is_inserting, MemStatus),
321         VMSTATE_UINT32(ost_event, MemStatus),
322         VMSTATE_UINT32(ost_status, MemStatus),
323         VMSTATE_END_OF_LIST()
324     }
325 };
326 
327 const VMStateDescription vmstate_memory_hotplug = {
328     .name = "memory hotplug state",
329     .version_id = 1,
330     .minimum_version_id = 1,
331     .minimum_version_id_old = 1,
332     .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 };
339 
340 void build_memory_hotplug_aml(Aml *table, uint32_t nr_mem,
341                               const char *res_root,
342                               const char *event_handler_method)
343 {
344     int i;
345     Aml *ifctx;
346     Aml *method;
347     Aml *dev_container;
348     Aml *mem_ctrl_dev;
349     char *mhp_res_path;
350 
351     if (!memhp_io_base) {
352         return;
353     }
354 
355     mhp_res_path = g_strdup_printf("%s." MEMORY_HOTPLUG_DEVICE, res_root);
356     mem_ctrl_dev = aml_device("%s", mhp_res_path);
357     {
358         Aml *crs;
359 
360         aml_append(mem_ctrl_dev, aml_name_decl("_HID", aml_string("PNP0A06")));
361         aml_append(mem_ctrl_dev,
362             aml_name_decl("_UID", aml_string("Memory hotplug resources")));
363 
364         crs = aml_resource_template();
365         aml_append(crs,
366             aml_io(AML_DECODE16, memhp_io_base, memhp_io_base, 0,
367                    MEMORY_HOTPLUG_IO_LEN)
368         );
369         aml_append(mem_ctrl_dev, aml_name_decl("_CRS", crs));
370 
371         aml_append(mem_ctrl_dev, aml_operation_region(
372             MEMORY_HOTPLUG_IO_REGION, AML_SYSTEM_IO,
373             aml_int(memhp_io_base), MEMORY_HOTPLUG_IO_LEN)
374         );
375 
376     }
377     aml_append(table, mem_ctrl_dev);
378 
379     dev_container = aml_device(MEMORY_DEVICES_CONTAINER);
380     {
381         Aml *field;
382         Aml *one = aml_int(1);
383         Aml *zero = aml_int(0);
384         Aml *ret_val = aml_local(0);
385         Aml *slot_arg0 = aml_arg(0);
386         Aml *slots_nr = aml_name(MEMORY_SLOTS_NUMBER);
387         Aml *ctrl_lock = aml_name(MEMORY_SLOT_LOCK);
388         Aml *slot_selector = aml_name(MEMORY_SLOT_SLECTOR);
389         char *mmio_path = g_strdup_printf("%s." MEMORY_HOTPLUG_IO_REGION,
390                                           mhp_res_path);
391 
392         aml_append(dev_container, aml_name_decl("_HID", aml_string("PNP0A06")));
393         aml_append(dev_container,
394             aml_name_decl("_UID", aml_string("DIMM devices")));
395 
396         assert(nr_mem <= ACPI_MAX_RAM_SLOTS);
397         aml_append(dev_container,
398             aml_name_decl(MEMORY_SLOTS_NUMBER, aml_int(nr_mem))
399         );
400 
401         field = aml_field(mmio_path, AML_DWORD_ACC,
402                           AML_NOLOCK, AML_PRESERVE);
403         aml_append(field, /* read only */
404             aml_named_field(MEMORY_SLOT_ADDR_LOW, 32));
405         aml_append(field, /* read only */
406             aml_named_field(MEMORY_SLOT_ADDR_HIGH, 32));
407         aml_append(field, /* read only */
408             aml_named_field(MEMORY_SLOT_SIZE_LOW, 32));
409         aml_append(field, /* read only */
410             aml_named_field(MEMORY_SLOT_SIZE_HIGH, 32));
411         aml_append(field, /* read only */
412             aml_named_field(MEMORY_SLOT_PROXIMITY, 32));
413         aml_append(dev_container, field);
414 
415         field = aml_field(mmio_path, AML_BYTE_ACC,
416                           AML_NOLOCK, AML_WRITE_AS_ZEROS);
417         aml_append(field, aml_reserved_field(160 /* bits, Offset(20) */));
418         aml_append(field, /* 1 if enabled, read only */
419             aml_named_field(MEMORY_SLOT_ENABLED, 1));
420         aml_append(field,
421             /*(read) 1 if has a insert event. (write) 1 to clear event */
422             aml_named_field(MEMORY_SLOT_INSERT_EVENT, 1));
423         aml_append(field,
424             /* (read) 1 if has a remove event. (write) 1 to clear event */
425             aml_named_field(MEMORY_SLOT_REMOVE_EVENT, 1));
426         aml_append(field,
427             /* initiates device eject, write only */
428             aml_named_field(MEMORY_SLOT_EJECT, 1));
429         aml_append(dev_container, field);
430 
431         field = aml_field(mmio_path, AML_DWORD_ACC,
432                           AML_NOLOCK, AML_PRESERVE);
433         aml_append(field, /* DIMM selector, write only */
434             aml_named_field(MEMORY_SLOT_SLECTOR, 32));
435         aml_append(field, /* _OST event code, write only */
436             aml_named_field(MEMORY_SLOT_OST_EVENT, 32));
437         aml_append(field, /* _OST status code, write only */
438             aml_named_field(MEMORY_SLOT_OST_STATUS, 32));
439         aml_append(dev_container, field);
440         g_free(mmio_path);
441 
442         method = aml_method("_STA", 0, AML_NOTSERIALIZED);
443         ifctx = aml_if(aml_equal(slots_nr, zero));
444         {
445             aml_append(ifctx, aml_return(zero));
446         }
447         aml_append(method, ifctx);
448         /* present, functioning, decoding, not shown in UI */
449         aml_append(method, aml_return(aml_int(0xB)));
450         aml_append(dev_container, method);
451 
452         aml_append(dev_container, aml_mutex(MEMORY_SLOT_LOCK, 0));
453 
454         method = aml_method(MEMORY_SLOT_SCAN_METHOD, 0, AML_NOTSERIALIZED);
455         {
456             Aml *else_ctx;
457             Aml *while_ctx;
458             Aml *idx = aml_local(0);
459             Aml *eject_req = aml_int(3);
460             Aml *dev_chk = aml_int(1);
461 
462             ifctx = aml_if(aml_equal(slots_nr, zero));
463             {
464                 aml_append(ifctx, aml_return(zero));
465             }
466             aml_append(method, ifctx);
467 
468             aml_append(method, aml_store(zero, idx));
469             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
470             /* build AML that:
471              * loops over all slots and Notifies DIMMs with
472              * Device Check or Eject Request notifications if
473              * slot has corresponding status bit set and clears
474              * slot status.
475              */
476             while_ctx = aml_while(aml_lless(idx, slots_nr));
477             {
478                 Aml *ins_evt = aml_name(MEMORY_SLOT_INSERT_EVENT);
479                 Aml *rm_evt = aml_name(MEMORY_SLOT_REMOVE_EVENT);
480 
481                 aml_append(while_ctx, aml_store(idx, slot_selector));
482                 ifctx = aml_if(aml_equal(ins_evt, one));
483                 {
484                     aml_append(ifctx,
485                                aml_call2(MEMORY_SLOT_NOTIFY_METHOD,
486                                          idx, dev_chk));
487                     aml_append(ifctx, aml_store(one, ins_evt));
488                 }
489                 aml_append(while_ctx, ifctx);
490 
491                 else_ctx = aml_else();
492                 ifctx = aml_if(aml_equal(rm_evt, one));
493                 {
494                     aml_append(ifctx,
495                         aml_call2(MEMORY_SLOT_NOTIFY_METHOD,
496                                   idx, eject_req));
497                     aml_append(ifctx, aml_store(one, rm_evt));
498                 }
499                 aml_append(else_ctx, ifctx);
500                 aml_append(while_ctx, else_ctx);
501 
502                 aml_append(while_ctx, aml_add(idx, one, idx));
503             }
504             aml_append(method, while_ctx);
505             aml_append(method, aml_release(ctrl_lock));
506             aml_append(method, aml_return(one));
507         }
508         aml_append(dev_container, method);
509 
510         method = aml_method(MEMORY_SLOT_STATUS_METHOD, 1, AML_NOTSERIALIZED);
511         {
512             Aml *slot_enabled = aml_name(MEMORY_SLOT_ENABLED);
513 
514             aml_append(method, aml_store(zero, ret_val));
515             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
516             aml_append(method,
517                 aml_store(aml_to_integer(slot_arg0), slot_selector));
518 
519             ifctx = aml_if(aml_equal(slot_enabled, one));
520             {
521                 aml_append(ifctx, aml_store(aml_int(0xF), ret_val));
522             }
523             aml_append(method, ifctx);
524 
525             aml_append(method, aml_release(ctrl_lock));
526             aml_append(method, aml_return(ret_val));
527         }
528         aml_append(dev_container, method);
529 
530         method = aml_method(MEMORY_SLOT_CRS_METHOD, 1, AML_SERIALIZED);
531         {
532             Aml *mr64 = aml_name("MR64");
533             Aml *mr32 = aml_name("MR32");
534             Aml *crs_tmpl = aml_resource_template();
535             Aml *minl = aml_name("MINL");
536             Aml *minh = aml_name("MINH");
537             Aml *maxl =  aml_name("MAXL");
538             Aml *maxh =  aml_name("MAXH");
539             Aml *lenl = aml_name("LENL");
540             Aml *lenh = aml_name("LENH");
541 
542             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
543             aml_append(method, aml_store(aml_to_integer(slot_arg0),
544                                          slot_selector));
545 
546             aml_append(crs_tmpl,
547                 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
548                                  AML_CACHEABLE, AML_READ_WRITE,
549                                  0, 0x0, 0xFFFFFFFFFFFFFFFEULL, 0,
550                                  0xFFFFFFFFFFFFFFFFULL));
551             aml_append(method, aml_name_decl("MR64", crs_tmpl));
552             aml_append(method,
553                 aml_create_dword_field(mr64, aml_int(14), "MINL"));
554             aml_append(method,
555                 aml_create_dword_field(mr64, aml_int(18), "MINH"));
556             aml_append(method,
557                 aml_create_dword_field(mr64, aml_int(38), "LENL"));
558             aml_append(method,
559                 aml_create_dword_field(mr64, aml_int(42), "LENH"));
560             aml_append(method,
561                 aml_create_dword_field(mr64, aml_int(22), "MAXL"));
562             aml_append(method,
563                 aml_create_dword_field(mr64, aml_int(26), "MAXH"));
564 
565             aml_append(method,
566                 aml_store(aml_name(MEMORY_SLOT_ADDR_HIGH), minh));
567             aml_append(method,
568                 aml_store(aml_name(MEMORY_SLOT_ADDR_LOW), minl));
569             aml_append(method,
570                 aml_store(aml_name(MEMORY_SLOT_SIZE_HIGH), lenh));
571             aml_append(method,
572                 aml_store(aml_name(MEMORY_SLOT_SIZE_LOW), lenl));
573 
574             /* 64-bit math: MAX = MIN + LEN - 1 */
575             aml_append(method, aml_add(minl, lenl, maxl));
576             aml_append(method, aml_add(minh, lenh, maxh));
577             ifctx = aml_if(aml_lless(maxl, minl));
578             {
579                 aml_append(ifctx, aml_add(maxh, one, maxh));
580             }
581             aml_append(method, ifctx);
582             ifctx = aml_if(aml_lless(maxl, one));
583             {
584                 aml_append(ifctx, aml_subtract(maxh, one, maxh));
585             }
586             aml_append(method, ifctx);
587             aml_append(method, aml_subtract(maxl, one, maxl));
588 
589             /* return 32-bit _CRS if addr/size is in low mem */
590             /* TODO: remove it since all hotplugged DIMMs are in high mem */
591             ifctx = aml_if(aml_equal(maxh, zero));
592             {
593                 crs_tmpl = aml_resource_template();
594                 aml_append(crs_tmpl,
595                     aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED,
596                                      AML_MAX_FIXED, AML_CACHEABLE,
597                                      AML_READ_WRITE,
598                                      0, 0x0, 0xFFFFFFFE, 0,
599                                      0xFFFFFFFF));
600                 aml_append(ifctx, aml_name_decl("MR32", crs_tmpl));
601                 aml_append(ifctx,
602                     aml_create_dword_field(mr32, aml_int(10), "MIN"));
603                 aml_append(ifctx,
604                     aml_create_dword_field(mr32, aml_int(14), "MAX"));
605                 aml_append(ifctx,
606                     aml_create_dword_field(mr32, aml_int(22), "LEN"));
607                 aml_append(ifctx, aml_store(minl, aml_name("MIN")));
608                 aml_append(ifctx, aml_store(maxl, aml_name("MAX")));
609                 aml_append(ifctx, aml_store(lenl, aml_name("LEN")));
610 
611                 aml_append(ifctx, aml_release(ctrl_lock));
612                 aml_append(ifctx, aml_return(mr32));
613             }
614             aml_append(method, ifctx);
615 
616             aml_append(method, aml_release(ctrl_lock));
617             aml_append(method, aml_return(mr64));
618         }
619         aml_append(dev_container, method);
620 
621         method = aml_method(MEMORY_SLOT_PROXIMITY_METHOD, 1,
622                             AML_NOTSERIALIZED);
623         {
624             Aml *proximity = aml_name(MEMORY_SLOT_PROXIMITY);
625 
626             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
627             aml_append(method, aml_store(aml_to_integer(slot_arg0),
628                                          slot_selector));
629             aml_append(method, aml_store(proximity, ret_val));
630             aml_append(method, aml_release(ctrl_lock));
631             aml_append(method, aml_return(ret_val));
632         }
633         aml_append(dev_container, method);
634 
635         method = aml_method(MEMORY_SLOT_OST_METHOD, 4, AML_NOTSERIALIZED);
636         {
637             Aml *ost_evt = aml_name(MEMORY_SLOT_OST_EVENT);
638             Aml *ost_status = aml_name(MEMORY_SLOT_OST_STATUS);
639 
640             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
641             aml_append(method, aml_store(aml_to_integer(slot_arg0),
642                                          slot_selector));
643             aml_append(method, aml_store(aml_arg(1), ost_evt));
644             aml_append(method, aml_store(aml_arg(2), ost_status));
645             aml_append(method, aml_release(ctrl_lock));
646         }
647         aml_append(dev_container, method);
648 
649         method = aml_method(MEMORY_SLOT_EJECT_METHOD, 2, AML_NOTSERIALIZED);
650         {
651             Aml *eject = aml_name(MEMORY_SLOT_EJECT);
652 
653             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
654             aml_append(method, aml_store(aml_to_integer(slot_arg0),
655                                          slot_selector));
656             aml_append(method, aml_store(one, eject));
657             aml_append(method, aml_release(ctrl_lock));
658         }
659         aml_append(dev_container, method);
660 
661         /* build memory devices */
662         for (i = 0; i < nr_mem; i++) {
663             Aml *dev;
664             const char *s;
665 
666             dev = aml_device("MP%02X", i);
667             aml_append(dev, aml_name_decl("_UID", aml_string("0x%02X", i)));
668             aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C80")));
669 
670             method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
671             s = MEMORY_SLOT_CRS_METHOD;
672             aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
673             aml_append(dev, method);
674 
675             method = aml_method("_STA", 0, AML_NOTSERIALIZED);
676             s = MEMORY_SLOT_STATUS_METHOD;
677             aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
678             aml_append(dev, method);
679 
680             method = aml_method("_PXM", 0, AML_NOTSERIALIZED);
681             s = MEMORY_SLOT_PROXIMITY_METHOD;
682             aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
683             aml_append(dev, method);
684 
685             method = aml_method("_OST", 3, AML_NOTSERIALIZED);
686             s = MEMORY_SLOT_OST_METHOD;
687             aml_append(method, aml_return(aml_call4(
688                 s, aml_name("_UID"), aml_arg(0), aml_arg(1), aml_arg(2)
689             )));
690             aml_append(dev, method);
691 
692             method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
693             s = MEMORY_SLOT_EJECT_METHOD;
694             aml_append(method, aml_return(aml_call2(
695                        s, aml_name("_UID"), aml_arg(0))));
696             aml_append(dev, method);
697 
698             aml_append(dev_container, dev);
699         }
700 
701         /* build Method(MEMORY_SLOT_NOTIFY_METHOD, 2) {
702          *     If (LEqual(Arg0, 0x00)) {Notify(MP00, Arg1)} ... }
703          */
704         method = aml_method(MEMORY_SLOT_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
705         for (i = 0; i < nr_mem; i++) {
706             ifctx = aml_if(aml_equal(aml_arg(0), aml_int(i)));
707             aml_append(ifctx,
708                 aml_notify(aml_name("MP%.02X", i), aml_arg(1))
709             );
710             aml_append(method, ifctx);
711         }
712         aml_append(dev_container, method);
713     }
714     aml_append(table, dev_container);
715 
716     method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
717     aml_append(method,
718         aml_call0(MEMORY_DEVICES_CONTAINER "." MEMORY_SLOT_SCAN_METHOD));
719     aml_append(table, method);
720 
721     g_free(mhp_res_path);
722 }
723