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