generic_event_device.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. *
  3. * Copyright (c) 2018 Intel Corporation
  4. * Copyright (c) 2019 Huawei Technologies R & D (UK) Ltd
  5. * Written by Samuel Ortiz, Shameer Kolothum
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2 or later, as published by the Free Software Foundation.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "qapi/error.h"
  13. #include "exec/address-spaces.h"
  14. #include "hw/acpi/acpi.h"
  15. #include "hw/acpi/generic_event_device.h"
  16. #include "hw/irq.h"
  17. #include "hw/mem/pc-dimm.h"
  18. #include "hw/mem/nvdimm.h"
  19. #include "hw/qdev-properties.h"
  20. #include "migration/vmstate.h"
  21. #include "qemu/error-report.h"
  22. static const uint32_t ged_supported_events[] = {
  23. ACPI_GED_MEM_HOTPLUG_EVT,
  24. ACPI_GED_PWR_DOWN_EVT,
  25. ACPI_GED_NVDIMM_HOTPLUG_EVT,
  26. };
  27. /*
  28. * The ACPI Generic Event Device (GED) is a hardware-reduced specific
  29. * device[ACPI v6.1 Section 5.6.9] that handles all platform events,
  30. * including the hotplug ones. Platforms need to specify their own
  31. * GED Event bitmap to describe what kind of events they want to support
  32. * through GED. This routine uses a single interrupt for the GED device,
  33. * relying on IO memory region to communicate the type of device
  34. * affected by the interrupt. This way, we can support up to 32 events
  35. * with a unique interrupt.
  36. */
  37. void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev,
  38. uint32_t ged_irq, AmlRegionSpace rs, hwaddr ged_base)
  39. {
  40. AcpiGedState *s = ACPI_GED(hotplug_dev);
  41. Aml *crs = aml_resource_template();
  42. Aml *evt, *field;
  43. Aml *dev = aml_device("%s", name);
  44. Aml *evt_sel = aml_local(0);
  45. Aml *esel = aml_name(AML_GED_EVT_SEL);
  46. /* _CRS interrupt */
  47. aml_append(crs, aml_interrupt(AML_CONSUMER, AML_EDGE, AML_ACTIVE_HIGH,
  48. AML_EXCLUSIVE, &ged_irq, 1));
  49. aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0013")));
  50. aml_append(dev, aml_name_decl("_UID", aml_string(GED_DEVICE)));
  51. aml_append(dev, aml_name_decl("_CRS", crs));
  52. /* Append IO region */
  53. aml_append(dev, aml_operation_region(AML_GED_EVT_REG, rs,
  54. aml_int(ged_base + ACPI_GED_EVT_SEL_OFFSET),
  55. ACPI_GED_EVT_SEL_LEN));
  56. field = aml_field(AML_GED_EVT_REG, AML_DWORD_ACC, AML_NOLOCK,
  57. AML_WRITE_AS_ZEROS);
  58. aml_append(field, aml_named_field(AML_GED_EVT_SEL,
  59. ACPI_GED_EVT_SEL_LEN * BITS_PER_BYTE));
  60. aml_append(dev, field);
  61. /*
  62. * For each GED event we:
  63. * - Add a conditional block for each event, inside a loop.
  64. * - Call a method for each supported GED event type.
  65. *
  66. * The resulting ASL code looks like:
  67. *
  68. * Local0 = ESEL
  69. * If ((Local0 & One) == One)
  70. * {
  71. * MethodEvent0()
  72. * }
  73. *
  74. * If ((Local0 & 0x2) == 0x2)
  75. * {
  76. * MethodEvent1()
  77. * }
  78. * ...
  79. */
  80. evt = aml_method("_EVT", 1, AML_SERIALIZED);
  81. {
  82. Aml *if_ctx;
  83. uint32_t i;
  84. uint32_t ged_events = ctpop32(s->ged_event_bitmap);
  85. /* Local0 = ESEL */
  86. aml_append(evt, aml_store(esel, evt_sel));
  87. for (i = 0; i < ARRAY_SIZE(ged_supported_events) && ged_events; i++) {
  88. uint32_t event = s->ged_event_bitmap & ged_supported_events[i];
  89. if (!event) {
  90. continue;
  91. }
  92. if_ctx = aml_if(aml_equal(aml_and(evt_sel, aml_int(event), NULL),
  93. aml_int(event)));
  94. switch (event) {
  95. case ACPI_GED_MEM_HOTPLUG_EVT:
  96. aml_append(if_ctx, aml_call0(MEMORY_DEVICES_CONTAINER "."
  97. MEMORY_SLOT_SCAN_METHOD));
  98. break;
  99. case ACPI_GED_PWR_DOWN_EVT:
  100. aml_append(if_ctx,
  101. aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE),
  102. aml_int(0x80)));
  103. break;
  104. case ACPI_GED_NVDIMM_HOTPLUG_EVT:
  105. aml_append(if_ctx,
  106. aml_notify(aml_name("\\_SB.NVDR"),
  107. aml_int(0x80)));
  108. break;
  109. default:
  110. /*
  111. * Please make sure all the events in ged_supported_events[]
  112. * are handled above.
  113. */
  114. g_assert_not_reached();
  115. }
  116. aml_append(evt, if_ctx);
  117. ged_events--;
  118. }
  119. if (ged_events) {
  120. error_report("Unsupported events specified");
  121. abort();
  122. }
  123. }
  124. /* Append _EVT method */
  125. aml_append(dev, evt);
  126. aml_append(table, dev);
  127. }
  128. /* Memory read by the GED _EVT AML dynamic method */
  129. static uint64_t ged_evt_read(void *opaque, hwaddr addr, unsigned size)
  130. {
  131. uint64_t val = 0;
  132. GEDState *ged_st = opaque;
  133. switch (addr) {
  134. case ACPI_GED_EVT_SEL_OFFSET:
  135. /* Read the selector value and reset it */
  136. val = ged_st->sel;
  137. ged_st->sel = 0;
  138. break;
  139. default:
  140. break;
  141. }
  142. return val;
  143. }
  144. /* Nothing is expected to be written to the GED memory region */
  145. static void ged_evt_write(void *opaque, hwaddr addr, uint64_t data,
  146. unsigned int size)
  147. {
  148. }
  149. static const MemoryRegionOps ged_evt_ops = {
  150. .read = ged_evt_read,
  151. .write = ged_evt_write,
  152. .endianness = DEVICE_LITTLE_ENDIAN,
  153. .valid = {
  154. .min_access_size = 4,
  155. .max_access_size = 4,
  156. },
  157. };
  158. static void acpi_ged_device_plug_cb(HotplugHandler *hotplug_dev,
  159. DeviceState *dev, Error **errp)
  160. {
  161. AcpiGedState *s = ACPI_GED(hotplug_dev);
  162. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  163. if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
  164. nvdimm_acpi_plug_cb(hotplug_dev, dev);
  165. } else {
  166. acpi_memory_plug_cb(hotplug_dev, &s->memhp_state, dev, errp);
  167. }
  168. } else {
  169. error_setg(errp, "virt: device plug request for unsupported device"
  170. " type: %s", object_get_typename(OBJECT(dev)));
  171. }
  172. }
  173. static void acpi_ged_unplug_request_cb(HotplugHandler *hotplug_dev,
  174. DeviceState *dev, Error **errp)
  175. {
  176. AcpiGedState *s = ACPI_GED(hotplug_dev);
  177. if ((object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) &&
  178. !(object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)))) {
  179. acpi_memory_unplug_request_cb(hotplug_dev, &s->memhp_state, dev, errp);
  180. } else {
  181. error_setg(errp, "acpi: device unplug request for unsupported device"
  182. " type: %s", object_get_typename(OBJECT(dev)));
  183. }
  184. }
  185. static void acpi_ged_unplug_cb(HotplugHandler *hotplug_dev,
  186. DeviceState *dev, Error **errp)
  187. {
  188. AcpiGedState *s = ACPI_GED(hotplug_dev);
  189. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  190. acpi_memory_unplug_cb(&s->memhp_state, dev, errp);
  191. } else {
  192. error_setg(errp, "acpi: device unplug for unsupported device"
  193. " type: %s", object_get_typename(OBJECT(dev)));
  194. }
  195. }
  196. static void acpi_ged_send_event(AcpiDeviceIf *adev, AcpiEventStatusBits ev)
  197. {
  198. AcpiGedState *s = ACPI_GED(adev);
  199. GEDState *ged_st = &s->ged_state;
  200. uint32_t sel;
  201. if (ev & ACPI_MEMORY_HOTPLUG_STATUS) {
  202. sel = ACPI_GED_MEM_HOTPLUG_EVT;
  203. } else if (ev & ACPI_POWER_DOWN_STATUS) {
  204. sel = ACPI_GED_PWR_DOWN_EVT;
  205. } else if (ev & ACPI_NVDIMM_HOTPLUG_STATUS) {
  206. sel = ACPI_GED_NVDIMM_HOTPLUG_EVT;
  207. } else {
  208. /* Unknown event. Return without generating interrupt. */
  209. warn_report("GED: Unsupported event %d. No irq injected", ev);
  210. return;
  211. }
  212. /*
  213. * Set the GED selector field to communicate the event type.
  214. * This will be read by GED aml code to select the appropriate
  215. * event method.
  216. */
  217. ged_st->sel |= sel;
  218. /* Trigger the event by sending an interrupt to the guest. */
  219. qemu_irq_pulse(s->irq);
  220. }
  221. static Property acpi_ged_properties[] = {
  222. DEFINE_PROP_UINT32("ged-event", AcpiGedState, ged_event_bitmap, 0),
  223. DEFINE_PROP_END_OF_LIST(),
  224. };
  225. static const VMStateDescription vmstate_memhp_state = {
  226. .name = "acpi-ged/memhp",
  227. .version_id = 1,
  228. .minimum_version_id = 1,
  229. .fields = (VMStateField[]) {
  230. VMSTATE_MEMORY_HOTPLUG(memhp_state, AcpiGedState),
  231. VMSTATE_END_OF_LIST()
  232. }
  233. };
  234. static const VMStateDescription vmstate_ged_state = {
  235. .name = "acpi-ged-state",
  236. .version_id = 1,
  237. .minimum_version_id = 1,
  238. .fields = (VMStateField[]) {
  239. VMSTATE_UINT32(sel, GEDState),
  240. VMSTATE_END_OF_LIST()
  241. }
  242. };
  243. static bool ghes_needed(void *opaque)
  244. {
  245. AcpiGedState *s = opaque;
  246. return s->ghes_state.ghes_addr_le;
  247. }
  248. static const VMStateDescription vmstate_ghes_state = {
  249. .name = "acpi-ged/ghes",
  250. .version_id = 1,
  251. .minimum_version_id = 1,
  252. .needed = ghes_needed,
  253. .fields = (VMStateField[]) {
  254. VMSTATE_STRUCT(ghes_state, AcpiGedState, 1,
  255. vmstate_ghes_state, AcpiGhesState),
  256. VMSTATE_END_OF_LIST()
  257. }
  258. };
  259. static const VMStateDescription vmstate_acpi_ged = {
  260. .name = "acpi-ged",
  261. .version_id = 1,
  262. .minimum_version_id = 1,
  263. .fields = (VMStateField[]) {
  264. VMSTATE_STRUCT(ged_state, AcpiGedState, 1, vmstate_ged_state, GEDState),
  265. VMSTATE_END_OF_LIST(),
  266. },
  267. .subsections = (const VMStateDescription * []) {
  268. &vmstate_memhp_state,
  269. &vmstate_ghes_state,
  270. NULL
  271. }
  272. };
  273. static void acpi_ged_initfn(Object *obj)
  274. {
  275. DeviceState *dev = DEVICE(obj);
  276. AcpiGedState *s = ACPI_GED(dev);
  277. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  278. GEDState *ged_st = &s->ged_state;
  279. memory_region_init_io(&ged_st->evt, obj, &ged_evt_ops, ged_st,
  280. TYPE_ACPI_GED, ACPI_GED_EVT_SEL_LEN);
  281. sysbus_init_mmio(sbd, &ged_st->evt);
  282. sysbus_init_irq(sbd, &s->irq);
  283. s->memhp_state.is_enabled = true;
  284. /*
  285. * GED handles memory hotplug event and acpi-mem-hotplug
  286. * memory region gets initialized here. Create an exclusive
  287. * container for memory hotplug IO and expose it as GED sysbus
  288. * MMIO so that boards can map it separately.
  289. */
  290. memory_region_init(&s->container_memhp, OBJECT(dev), "memhp container",
  291. MEMORY_HOTPLUG_IO_LEN);
  292. sysbus_init_mmio(sbd, &s->container_memhp);
  293. acpi_memory_hotplug_init(&s->container_memhp, OBJECT(dev),
  294. &s->memhp_state, 0);
  295. }
  296. static void acpi_ged_class_init(ObjectClass *class, void *data)
  297. {
  298. DeviceClass *dc = DEVICE_CLASS(class);
  299. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(class);
  300. AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_CLASS(class);
  301. dc->desc = "ACPI Generic Event Device";
  302. device_class_set_props(dc, acpi_ged_properties);
  303. dc->vmsd = &vmstate_acpi_ged;
  304. hc->plug = acpi_ged_device_plug_cb;
  305. hc->unplug_request = acpi_ged_unplug_request_cb;
  306. hc->unplug = acpi_ged_unplug_cb;
  307. adevc->send_event = acpi_ged_send_event;
  308. }
  309. static const TypeInfo acpi_ged_info = {
  310. .name = TYPE_ACPI_GED,
  311. .parent = TYPE_SYS_BUS_DEVICE,
  312. .instance_size = sizeof(AcpiGedState),
  313. .instance_init = acpi_ged_initfn,
  314. .class_init = acpi_ged_class_init,
  315. .interfaces = (InterfaceInfo[]) {
  316. { TYPE_HOTPLUG_HANDLER },
  317. { TYPE_ACPI_DEVICE_IF },
  318. { }
  319. }
  320. };
  321. static void acpi_ged_register_types(void)
  322. {
  323. type_register_static(&acpi_ged_info);
  324. }
  325. type_init(acpi_ged_register_types)