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