memory_hotplug.c 26 KB

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