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