2
0

memory_hotplug.c 26 KB

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