2
0

memory_hotplug.c 26 KB

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