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