generic_event_device.c 9.3 KB

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