xref: /openbmc/qemu/hw/acpi/memory_hotplug.c (revision b2580720)
1 #include "qemu/osdep.h"
2 #include "hw/acpi/memory_hotplug.h"
3 #include "hw/mem/pc-dimm.h"
4 #include "hw/boards.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 #include "qapi/qapi-events-qdev.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         }
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_device_unplug_guest_error(dev->id,
182                                                           dev->canonical_path);
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     .fields = (const VMStateField[]) {
313         VMSTATE_BOOL(is_enabled, MemStatus),
314         VMSTATE_BOOL(is_inserting, MemStatus),
315         VMSTATE_UINT32(ost_event, MemStatus),
316         VMSTATE_UINT32(ost_status, MemStatus),
317         VMSTATE_END_OF_LIST()
318     }
319 };
320 
321 const VMStateDescription vmstate_memory_hotplug = {
322     .name = "memory hotplug state",
323     .version_id = 1,
324     .minimum_version_id = 1,
325     .fields = (const VMStateField[]) {
326         VMSTATE_UINT32(selector, MemHotplugState),
327         VMSTATE_STRUCT_VARRAY_POINTER_UINT32(devs, MemHotplugState, dev_count,
328                                              vmstate_memhp_sts, MemStatus),
329         VMSTATE_END_OF_LIST()
330     }
331 };
332 
333 void build_memory_hotplug_aml(Aml *table, uint32_t nr_mem,
334                               const char *res_root,
335                               const char *event_handler_method,
336                               AmlRegionSpace rs, hwaddr memhp_io_base)
337 {
338     int i;
339     Aml *ifctx;
340     Aml *method;
341     Aml *dev_container;
342     Aml *mem_ctrl_dev;
343     char *mhp_res_path;
344 
345     mhp_res_path = g_strdup_printf("%s." MEMORY_HOTPLUG_DEVICE, res_root);
346     mem_ctrl_dev = aml_device("%s", mhp_res_path);
347     {
348         Aml *crs;
349 
350         aml_append(mem_ctrl_dev, aml_name_decl("_HID", aml_string("PNP0A06")));
351         aml_append(mem_ctrl_dev,
352             aml_name_decl("_UID", aml_string("Memory hotplug resources")));
353 
354         crs = aml_resource_template();
355         if (rs == AML_SYSTEM_IO) {
356             aml_append(crs,
357                 aml_io(AML_DECODE16, memhp_io_base, memhp_io_base, 0,
358                        MEMORY_HOTPLUG_IO_LEN)
359             );
360         } else {
361             aml_append(crs, aml_memory32_fixed(memhp_io_base,
362                             MEMORY_HOTPLUG_IO_LEN, AML_READ_WRITE));
363         }
364         aml_append(mem_ctrl_dev, aml_name_decl("_CRS", crs));
365 
366         aml_append(mem_ctrl_dev, aml_operation_region(
367             MEMORY_HOTPLUG_IO_REGION, rs,
368             aml_int(memhp_io_base), MEMORY_HOTPLUG_IO_LEN)
369         );
370 
371     }
372     aml_append(table, mem_ctrl_dev);
373 
374     dev_container = aml_device(MEMORY_DEVICES_CONTAINER);
375     {
376         Aml *field;
377         Aml *one = aml_int(1);
378         Aml *zero = aml_int(0);
379         Aml *ret_val = aml_local(0);
380         Aml *slot_arg0 = aml_arg(0);
381         Aml *slots_nr = aml_name(MEMORY_SLOTS_NUMBER);
382         Aml *ctrl_lock = aml_name(MEMORY_SLOT_LOCK);
383         Aml *slot_selector = aml_name(MEMORY_SLOT_SLECTOR);
384         char *mmio_path = g_strdup_printf("%s." MEMORY_HOTPLUG_IO_REGION,
385                                           mhp_res_path);
386 
387         aml_append(dev_container, aml_name_decl("_HID", aml_string("PNP0A06")));
388         aml_append(dev_container,
389             aml_name_decl("_UID", aml_string("DIMM devices")));
390 
391         assert(nr_mem <= ACPI_MAX_RAM_SLOTS);
392         aml_append(dev_container,
393             aml_name_decl(MEMORY_SLOTS_NUMBER, aml_int(nr_mem))
394         );
395 
396         field = aml_field(mmio_path, AML_DWORD_ACC,
397                           AML_NOLOCK, AML_PRESERVE);
398         aml_append(field, /* read only */
399             aml_named_field(MEMORY_SLOT_ADDR_LOW, 32));
400         aml_append(field, /* read only */
401             aml_named_field(MEMORY_SLOT_ADDR_HIGH, 32));
402         aml_append(field, /* read only */
403             aml_named_field(MEMORY_SLOT_SIZE_LOW, 32));
404         aml_append(field, /* read only */
405             aml_named_field(MEMORY_SLOT_SIZE_HIGH, 32));
406         aml_append(field, /* read only */
407             aml_named_field(MEMORY_SLOT_PROXIMITY, 32));
408         aml_append(dev_container, field);
409 
410         field = aml_field(mmio_path, AML_BYTE_ACC,
411                           AML_NOLOCK, AML_WRITE_AS_ZEROS);
412         aml_append(field, aml_reserved_field(160 /* bits, Offset(20) */));
413         aml_append(field, /* 1 if enabled, read only */
414             aml_named_field(MEMORY_SLOT_ENABLED, 1));
415         aml_append(field,
416             /*(read) 1 if has a insert event. (write) 1 to clear event */
417             aml_named_field(MEMORY_SLOT_INSERT_EVENT, 1));
418         aml_append(field,
419             /* (read) 1 if has a remove event. (write) 1 to clear event */
420             aml_named_field(MEMORY_SLOT_REMOVE_EVENT, 1));
421         aml_append(field,
422             /* initiates device eject, write only */
423             aml_named_field(MEMORY_SLOT_EJECT, 1));
424         aml_append(dev_container, field);
425 
426         field = aml_field(mmio_path, AML_DWORD_ACC,
427                           AML_NOLOCK, AML_PRESERVE);
428         aml_append(field, /* DIMM selector, write only */
429             aml_named_field(MEMORY_SLOT_SLECTOR, 32));
430         aml_append(field, /* _OST event code, write only */
431             aml_named_field(MEMORY_SLOT_OST_EVENT, 32));
432         aml_append(field, /* _OST status code, write only */
433             aml_named_field(MEMORY_SLOT_OST_STATUS, 32));
434         aml_append(dev_container, field);
435         g_free(mmio_path);
436 
437         method = aml_method("_STA", 0, AML_NOTSERIALIZED);
438         ifctx = aml_if(aml_equal(slots_nr, zero));
439         {
440             aml_append(ifctx, aml_return(zero));
441         }
442         aml_append(method, ifctx);
443         /* present, functioning, decoding, not shown in UI */
444         aml_append(method, aml_return(aml_int(0xB)));
445         aml_append(dev_container, method);
446 
447         aml_append(dev_container, aml_mutex(MEMORY_SLOT_LOCK, 0));
448 
449         method = aml_method(MEMORY_SLOT_SCAN_METHOD, 0, AML_NOTSERIALIZED);
450         {
451             Aml *else_ctx;
452             Aml *while_ctx;
453             Aml *idx = aml_local(0);
454             Aml *eject_req = aml_int(3);
455             Aml *dev_chk = aml_int(1);
456 
457             ifctx = aml_if(aml_equal(slots_nr, zero));
458             {
459                 aml_append(ifctx, aml_return(zero));
460             }
461             aml_append(method, ifctx);
462 
463             aml_append(method, aml_store(zero, idx));
464             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
465             /* build AML that:
466              * loops over all slots and Notifies DIMMs with
467              * Device Check or Eject Request notifications if
468              * slot has corresponding status bit set and clears
469              * slot status.
470              */
471             while_ctx = aml_while(aml_lless(idx, slots_nr));
472             {
473                 Aml *ins_evt = aml_name(MEMORY_SLOT_INSERT_EVENT);
474                 Aml *rm_evt = aml_name(MEMORY_SLOT_REMOVE_EVENT);
475 
476                 aml_append(while_ctx, aml_store(idx, slot_selector));
477                 ifctx = aml_if(aml_equal(ins_evt, one));
478                 {
479                     aml_append(ifctx,
480                                aml_call2(MEMORY_SLOT_NOTIFY_METHOD,
481                                          idx, dev_chk));
482                     aml_append(ifctx, aml_store(one, ins_evt));
483                 }
484                 aml_append(while_ctx, ifctx);
485 
486                 else_ctx = aml_else();
487                 ifctx = aml_if(aml_equal(rm_evt, one));
488                 {
489                     aml_append(ifctx,
490                         aml_call2(MEMORY_SLOT_NOTIFY_METHOD,
491                                   idx, eject_req));
492                     aml_append(ifctx, aml_store(one, rm_evt));
493                 }
494                 aml_append(else_ctx, ifctx);
495                 aml_append(while_ctx, else_ctx);
496 
497                 aml_append(while_ctx, aml_add(idx, one, idx));
498             }
499             aml_append(method, while_ctx);
500             aml_append(method, aml_release(ctrl_lock));
501             aml_append(method, aml_return(one));
502         }
503         aml_append(dev_container, method);
504 
505         method = aml_method(MEMORY_SLOT_STATUS_METHOD, 1, AML_NOTSERIALIZED);
506         {
507             Aml *slot_enabled = aml_name(MEMORY_SLOT_ENABLED);
508 
509             aml_append(method, aml_store(zero, ret_val));
510             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
511             aml_append(method,
512                 aml_store(aml_to_integer(slot_arg0), slot_selector));
513 
514             ifctx = aml_if(aml_equal(slot_enabled, one));
515             {
516                 aml_append(ifctx, aml_store(aml_int(0xF), ret_val));
517             }
518             aml_append(method, ifctx);
519 
520             aml_append(method, aml_release(ctrl_lock));
521             aml_append(method, aml_return(ret_val));
522         }
523         aml_append(dev_container, method);
524 
525         method = aml_method(MEMORY_SLOT_CRS_METHOD, 1, AML_SERIALIZED);
526         {
527             Aml *mr64 = aml_name("MR64");
528             Aml *mr32 = aml_name("MR32");
529             Aml *crs_tmpl = aml_resource_template();
530             Aml *minl = aml_name("MINL");
531             Aml *minh = aml_name("MINH");
532             Aml *maxl =  aml_name("MAXL");
533             Aml *maxh =  aml_name("MAXH");
534             Aml *lenl = aml_name("LENL");
535             Aml *lenh = aml_name("LENH");
536 
537             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
538             aml_append(method, aml_store(aml_to_integer(slot_arg0),
539                                          slot_selector));
540 
541             aml_append(crs_tmpl,
542                 aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
543                                  AML_CACHEABLE, AML_READ_WRITE,
544                                  0, 0x0, 0xFFFFFFFFFFFFFFFEULL, 0,
545                                  0xFFFFFFFFFFFFFFFFULL));
546             aml_append(method, aml_name_decl("MR64", crs_tmpl));
547             aml_append(method,
548                 aml_create_dword_field(mr64, aml_int(14), "MINL"));
549             aml_append(method,
550                 aml_create_dword_field(mr64, aml_int(18), "MINH"));
551             aml_append(method,
552                 aml_create_dword_field(mr64, aml_int(38), "LENL"));
553             aml_append(method,
554                 aml_create_dword_field(mr64, aml_int(42), "LENH"));
555             aml_append(method,
556                 aml_create_dword_field(mr64, aml_int(22), "MAXL"));
557             aml_append(method,
558                 aml_create_dword_field(mr64, aml_int(26), "MAXH"));
559 
560             aml_append(method,
561                 aml_store(aml_name(MEMORY_SLOT_ADDR_HIGH), minh));
562             aml_append(method,
563                 aml_store(aml_name(MEMORY_SLOT_ADDR_LOW), minl));
564             aml_append(method,
565                 aml_store(aml_name(MEMORY_SLOT_SIZE_HIGH), lenh));
566             aml_append(method,
567                 aml_store(aml_name(MEMORY_SLOT_SIZE_LOW), lenl));
568 
569             /* 64-bit math: MAX = MIN + LEN - 1 */
570             aml_append(method, aml_add(minl, lenl, maxl));
571             aml_append(method, aml_add(minh, lenh, maxh));
572             ifctx = aml_if(aml_lless(maxl, minl));
573             {
574                 aml_append(ifctx, aml_add(maxh, one, maxh));
575             }
576             aml_append(method, ifctx);
577             ifctx = aml_if(aml_lless(maxl, one));
578             {
579                 aml_append(ifctx, aml_subtract(maxh, one, maxh));
580             }
581             aml_append(method, ifctx);
582             aml_append(method, aml_subtract(maxl, one, maxl));
583 
584             /* return 32-bit _CRS if addr/size is in low mem */
585             /* TODO: remove it since all hotplugged DIMMs are in high mem */
586             ifctx = aml_if(aml_equal(maxh, zero));
587             {
588                 crs_tmpl = aml_resource_template();
589                 aml_append(crs_tmpl,
590                     aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED,
591                                      AML_MAX_FIXED, AML_CACHEABLE,
592                                      AML_READ_WRITE,
593                                      0, 0x0, 0xFFFFFFFE, 0,
594                                      0xFFFFFFFF));
595                 aml_append(ifctx, aml_name_decl("MR32", crs_tmpl));
596                 aml_append(ifctx,
597                     aml_create_dword_field(mr32, aml_int(10), "MIN"));
598                 aml_append(ifctx,
599                     aml_create_dword_field(mr32, aml_int(14), "MAX"));
600                 aml_append(ifctx,
601                     aml_create_dword_field(mr32, aml_int(22), "LEN"));
602                 aml_append(ifctx, aml_store(minl, aml_name("MIN")));
603                 aml_append(ifctx, aml_store(maxl, aml_name("MAX")));
604                 aml_append(ifctx, aml_store(lenl, aml_name("LEN")));
605 
606                 aml_append(ifctx, aml_release(ctrl_lock));
607                 aml_append(ifctx, aml_return(mr32));
608             }
609             aml_append(method, ifctx);
610 
611             aml_append(method, aml_release(ctrl_lock));
612             aml_append(method, aml_return(mr64));
613         }
614         aml_append(dev_container, method);
615 
616         method = aml_method(MEMORY_SLOT_PROXIMITY_METHOD, 1,
617                             AML_NOTSERIALIZED);
618         {
619             Aml *proximity = aml_name(MEMORY_SLOT_PROXIMITY);
620 
621             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
622             aml_append(method, aml_store(aml_to_integer(slot_arg0),
623                                          slot_selector));
624             aml_append(method, aml_store(proximity, ret_val));
625             aml_append(method, aml_release(ctrl_lock));
626             aml_append(method, aml_return(ret_val));
627         }
628         aml_append(dev_container, method);
629 
630         method = aml_method(MEMORY_SLOT_OST_METHOD, 4, AML_NOTSERIALIZED);
631         {
632             Aml *ost_evt = aml_name(MEMORY_SLOT_OST_EVENT);
633             Aml *ost_status = aml_name(MEMORY_SLOT_OST_STATUS);
634 
635             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
636             aml_append(method, aml_store(aml_to_integer(slot_arg0),
637                                          slot_selector));
638             aml_append(method, aml_store(aml_arg(1), ost_evt));
639             aml_append(method, aml_store(aml_arg(2), ost_status));
640             aml_append(method, aml_release(ctrl_lock));
641         }
642         aml_append(dev_container, method);
643 
644         method = aml_method(MEMORY_SLOT_EJECT_METHOD, 2, AML_NOTSERIALIZED);
645         {
646             Aml *eject = aml_name(MEMORY_SLOT_EJECT);
647 
648             aml_append(method, aml_acquire(ctrl_lock, 0xFFFF));
649             aml_append(method, aml_store(aml_to_integer(slot_arg0),
650                                          slot_selector));
651             aml_append(method, aml_store(one, eject));
652             aml_append(method, aml_release(ctrl_lock));
653         }
654         aml_append(dev_container, method);
655 
656         /* build memory devices */
657         for (i = 0; i < nr_mem; i++) {
658             Aml *dev;
659             const char *s;
660 
661             dev = aml_device("MP%02X", i);
662             aml_append(dev, aml_name_decl("_UID", aml_string("0x%02X", i)));
663             aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C80")));
664 
665             method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
666             s = MEMORY_SLOT_CRS_METHOD;
667             aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
668             aml_append(dev, method);
669 
670             method = aml_method("_STA", 0, AML_NOTSERIALIZED);
671             s = MEMORY_SLOT_STATUS_METHOD;
672             aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
673             aml_append(dev, method);
674 
675             method = aml_method("_PXM", 0, AML_NOTSERIALIZED);
676             s = MEMORY_SLOT_PROXIMITY_METHOD;
677             aml_append(method, aml_return(aml_call1(s, aml_name("_UID"))));
678             aml_append(dev, method);
679 
680             method = aml_method("_OST", 3, AML_NOTSERIALIZED);
681             s = MEMORY_SLOT_OST_METHOD;
682             aml_append(method,
683                        aml_call4(s, aml_name("_UID"), aml_arg(0),
684                                  aml_arg(1), aml_arg(2)));
685             aml_append(dev, method);
686 
687             method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
688             s = MEMORY_SLOT_EJECT_METHOD;
689             aml_append(method,
690                        aml_call2(s, aml_name("_UID"), aml_arg(0)));
691             aml_append(dev, method);
692 
693             aml_append(dev_container, dev);
694         }
695 
696         /* build Method(MEMORY_SLOT_NOTIFY_METHOD, 2) {
697          *     If (LEqual(Arg0, 0x00)) {Notify(MP00, Arg1)} ... }
698          */
699         method = aml_method(MEMORY_SLOT_NOTIFY_METHOD, 2, AML_NOTSERIALIZED);
700         for (i = 0; i < nr_mem; i++) {
701             ifctx = aml_if(aml_equal(aml_arg(0), aml_int(i)));
702             aml_append(ifctx,
703                 aml_notify(aml_name("MP%.02X", i), aml_arg(1))
704             );
705             aml_append(method, ifctx);
706         }
707         aml_append(dev_container, method);
708     }
709     aml_append(table, dev_container);
710 
711     if (event_handler_method) {
712         method = aml_method(event_handler_method, 0, AML_NOTSERIALIZED);
713         aml_append(method, aml_call0(MEMORY_DEVICES_CONTAINER "."
714                                      MEMORY_SLOT_SCAN_METHOD));
715         aml_append(table, method);
716     }
717 
718     g_free(mhp_res_path);
719 }
720