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