2
0

memory_hotplug.c 26 KB

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