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 "hw/acpi/acpi.h" 15 #include "hw/acpi/generic_event_device.h" 16 #include "hw/pci/pci.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 #include "system/runstate.h" 24 25 static const uint32_t ged_supported_events[] = { 26 ACPI_GED_MEM_HOTPLUG_EVT, 27 ACPI_GED_PWR_DOWN_EVT, 28 ACPI_GED_NVDIMM_HOTPLUG_EVT, 29 ACPI_GED_CPU_HOTPLUG_EVT, 30 }; 31 32 /* 33 * The ACPI Generic Event Device (GED) is a hardware-reduced specific 34 * device[ACPI v6.1 Section 5.6.9] that handles all platform events, 35 * including the hotplug ones. Platforms need to specify their own 36 * GED Event bitmap to describe what kind of events they want to support 37 * through GED. This routine uses a single interrupt for the GED device, 38 * relying on IO memory region to communicate the type of device 39 * affected by the interrupt. This way, we can support up to 32 events 40 * with a unique interrupt. 41 */ 42 void build_ged_aml(Aml *table, const char *name, HotplugHandler *hotplug_dev, 43 uint32_t ged_irq, AmlRegionSpace rs, hwaddr ged_base) 44 { 45 AcpiGedState *s = ACPI_GED(hotplug_dev); 46 Aml *crs = aml_resource_template(); 47 Aml *evt, *field; 48 Aml *dev = aml_device("%s", name); 49 Aml *evt_sel = aml_local(0); 50 Aml *esel = aml_name(AML_GED_EVT_SEL); 51 52 /* _CRS interrupt */ 53 aml_append(crs, aml_interrupt(AML_CONSUMER, AML_EDGE, AML_ACTIVE_HIGH, 54 AML_EXCLUSIVE, &ged_irq, 1)); 55 56 aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0013"))); 57 aml_append(dev, aml_name_decl("_UID", aml_string(GED_DEVICE))); 58 aml_append(dev, aml_name_decl("_CRS", crs)); 59 60 /* Append IO region */ 61 aml_append(dev, aml_operation_region(AML_GED_EVT_REG, rs, 62 aml_int(ged_base + ACPI_GED_EVT_SEL_OFFSET), 63 ACPI_GED_EVT_SEL_LEN)); 64 field = aml_field(AML_GED_EVT_REG, AML_DWORD_ACC, AML_NOLOCK, 65 AML_WRITE_AS_ZEROS); 66 aml_append(field, aml_named_field(AML_GED_EVT_SEL, 67 ACPI_GED_EVT_SEL_LEN * BITS_PER_BYTE)); 68 aml_append(dev, field); 69 70 /* 71 * For each GED event we: 72 * - Add a conditional block for each event, inside a loop. 73 * - Call a method for each supported GED event type. 74 * 75 * The resulting ASL code looks like: 76 * 77 * Local0 = ESEL 78 * If ((Local0 & One) == One) 79 * { 80 * MethodEvent0() 81 * } 82 * 83 * If ((Local0 & 0x2) == 0x2) 84 * { 85 * MethodEvent1() 86 * } 87 * ... 88 */ 89 evt = aml_method("_EVT", 1, AML_SERIALIZED); 90 { 91 Aml *if_ctx; 92 uint32_t i; 93 uint32_t ged_events = ctpop32(s->ged_event_bitmap); 94 95 /* Local0 = ESEL */ 96 aml_append(evt, aml_store(esel, evt_sel)); 97 98 for (i = 0; i < ARRAY_SIZE(ged_supported_events) && ged_events; i++) { 99 uint32_t event = s->ged_event_bitmap & ged_supported_events[i]; 100 101 if (!event) { 102 continue; 103 } 104 105 if_ctx = aml_if(aml_equal(aml_and(evt_sel, aml_int(event), NULL), 106 aml_int(event))); 107 switch (event) { 108 case ACPI_GED_MEM_HOTPLUG_EVT: 109 aml_append(if_ctx, aml_call0(MEMORY_DEVICES_CONTAINER "." 110 MEMORY_SLOT_SCAN_METHOD)); 111 break; 112 case ACPI_GED_CPU_HOTPLUG_EVT: 113 aml_append(if_ctx, aml_call0(AML_GED_EVT_CPU_SCAN_METHOD)); 114 break; 115 case ACPI_GED_PWR_DOWN_EVT: 116 aml_append(if_ctx, 117 aml_notify(aml_name(ACPI_POWER_BUTTON_DEVICE), 118 aml_int(0x80))); 119 break; 120 case ACPI_GED_NVDIMM_HOTPLUG_EVT: 121 aml_append(if_ctx, 122 aml_notify(aml_name("\\_SB.NVDR"), 123 aml_int(0x80))); 124 break; 125 default: 126 /* 127 * Please make sure all the events in ged_supported_events[] 128 * are handled above. 129 */ 130 g_assert_not_reached(); 131 } 132 133 aml_append(evt, if_ctx); 134 ged_events--; 135 } 136 137 if (ged_events) { 138 error_report("Unsupported events specified"); 139 abort(); 140 } 141 } 142 143 /* Append _EVT method */ 144 aml_append(dev, evt); 145 146 aml_append(table, dev); 147 } 148 149 void acpi_dsdt_add_power_button(Aml *scope) 150 { 151 Aml *dev = aml_device(ACPI_POWER_BUTTON_DEVICE); 152 aml_append(dev, aml_name_decl("_HID", aml_string("PNP0C0C"))); 153 aml_append(dev, aml_name_decl("_UID", aml_int(0))); 154 aml_append(scope, dev); 155 } 156 157 /* Memory read by the GED _EVT AML dynamic method */ 158 static uint64_t ged_evt_read(void *opaque, hwaddr addr, unsigned size) 159 { 160 uint64_t val = 0; 161 GEDState *ged_st = opaque; 162 163 switch (addr) { 164 case ACPI_GED_EVT_SEL_OFFSET: 165 /* Read the selector value and reset it */ 166 val = ged_st->sel; 167 ged_st->sel = 0; 168 break; 169 default: 170 break; 171 } 172 173 return val; 174 } 175 176 /* Nothing is expected to be written to the GED memory region */ 177 static void ged_evt_write(void *opaque, hwaddr addr, uint64_t data, 178 unsigned int size) 179 { 180 } 181 182 static const MemoryRegionOps ged_evt_ops = { 183 .read = ged_evt_read, 184 .write = ged_evt_write, 185 .endianness = DEVICE_LITTLE_ENDIAN, 186 .valid = { 187 .min_access_size = 4, 188 .max_access_size = 4, 189 }, 190 }; 191 192 static uint64_t ged_regs_read(void *opaque, hwaddr addr, unsigned size) 193 { 194 return 0; 195 } 196 197 static void ged_regs_write(void *opaque, hwaddr addr, uint64_t data, 198 unsigned int size) 199 { 200 bool slp_en; 201 int slp_typ; 202 203 switch (addr) { 204 case ACPI_GED_REG_SLEEP_CTL: 205 slp_typ = (data >> ACPI_GED_SLP_TYP_POS) & ACPI_GED_SLP_TYP_MASK; 206 slp_en = !!(data & ACPI_GED_SLP_EN); 207 if (slp_en && slp_typ == ACPI_GED_SLP_TYP_S5) { 208 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); 209 } 210 return; 211 case ACPI_GED_REG_SLEEP_STS: 212 return; 213 case ACPI_GED_REG_RESET: 214 if (data == ACPI_GED_RESET_VALUE) { 215 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 216 } 217 return; 218 } 219 } 220 221 static const MemoryRegionOps ged_regs_ops = { 222 .read = ged_regs_read, 223 .write = ged_regs_write, 224 .endianness = DEVICE_LITTLE_ENDIAN, 225 .valid = { 226 .min_access_size = 1, 227 .max_access_size = 1, 228 }, 229 }; 230 231 static void acpi_ged_device_plug_cb(HotplugHandler *hotplug_dev, 232 DeviceState *dev, Error **errp) 233 { 234 AcpiGedState *s = ACPI_GED(hotplug_dev); 235 236 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 237 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) { 238 nvdimm_acpi_plug_cb(hotplug_dev, dev); 239 } else { 240 acpi_memory_plug_cb(hotplug_dev, &s->memhp_state, dev, errp); 241 } 242 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 243 acpi_cpu_plug_cb(hotplug_dev, &s->cpuhp_state, dev, errp); 244 } else { 245 error_setg(errp, "virt: device plug request for unsupported device" 246 " type: %s", object_get_typename(OBJECT(dev))); 247 } 248 } 249 250 static void acpi_ged_unplug_request_cb(HotplugHandler *hotplug_dev, 251 DeviceState *dev, Error **errp) 252 { 253 AcpiGedState *s = ACPI_GED(hotplug_dev); 254 255 if ((object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) && 256 !(object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)))) { 257 acpi_memory_unplug_request_cb(hotplug_dev, &s->memhp_state, dev, errp); 258 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 259 acpi_cpu_unplug_request_cb(hotplug_dev, &s->cpuhp_state, dev, errp); 260 } else { 261 error_setg(errp, "acpi: device unplug request for unsupported device" 262 " type: %s", object_get_typename(OBJECT(dev))); 263 } 264 } 265 266 static void acpi_ged_unplug_cb(HotplugHandler *hotplug_dev, 267 DeviceState *dev, Error **errp) 268 { 269 AcpiGedState *s = ACPI_GED(hotplug_dev); 270 271 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) { 272 acpi_memory_unplug_cb(&s->memhp_state, dev, errp); 273 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) { 274 acpi_cpu_unplug_cb(&s->cpuhp_state, dev, errp); 275 } else { 276 error_setg(errp, "acpi: device unplug for unsupported device" 277 " type: %s", object_get_typename(OBJECT(dev))); 278 } 279 } 280 281 static void acpi_ged_ospm_status(AcpiDeviceIf *adev, ACPIOSTInfoList ***list) 282 { 283 AcpiGedState *s = ACPI_GED(adev); 284 285 acpi_memory_ospm_status(&s->memhp_state, list); 286 acpi_cpu_ospm_status(&s->cpuhp_state, list); 287 } 288 289 static void acpi_ged_send_event(AcpiDeviceIf *adev, AcpiEventStatusBits ev) 290 { 291 AcpiGedState *s = ACPI_GED(adev); 292 GEDState *ged_st = &s->ged_state; 293 uint32_t sel; 294 295 if (ev & ACPI_MEMORY_HOTPLUG_STATUS) { 296 sel = ACPI_GED_MEM_HOTPLUG_EVT; 297 } else if (ev & ACPI_POWER_DOWN_STATUS) { 298 sel = ACPI_GED_PWR_DOWN_EVT; 299 } else if (ev & ACPI_NVDIMM_HOTPLUG_STATUS) { 300 sel = ACPI_GED_NVDIMM_HOTPLUG_EVT; 301 } else if (ev & ACPI_CPU_HOTPLUG_STATUS) { 302 sel = ACPI_GED_CPU_HOTPLUG_EVT; 303 } else { 304 /* Unknown event. Return without generating interrupt. */ 305 warn_report("GED: Unsupported event %d. No irq injected", ev); 306 return; 307 } 308 309 /* 310 * Set the GED selector field to communicate the event type. 311 * This will be read by GED aml code to select the appropriate 312 * event method. 313 */ 314 ged_st->sel |= sel; 315 316 /* Trigger the event by sending an interrupt to the guest. */ 317 qemu_irq_pulse(s->irq); 318 } 319 320 static const Property acpi_ged_properties[] = { 321 DEFINE_PROP_UINT32("ged-event", AcpiGedState, ged_event_bitmap, 0), 322 DEFINE_PROP_BOOL(ACPI_PM_PROP_ACPI_PCIHP_BRIDGE, AcpiGedState, 323 pcihp_state.use_acpi_hotplug_bridge, 0), 324 DEFINE_PROP_LINK("bus", AcpiGedState, pcihp_state.root, 325 TYPE_PCI_BUS, PCIBus *), 326 }; 327 328 static const VMStateDescription vmstate_memhp_state = { 329 .name = "acpi-ged/memhp", 330 .version_id = 1, 331 .minimum_version_id = 1, 332 .fields = (const VMStateField[]) { 333 VMSTATE_MEMORY_HOTPLUG(memhp_state, AcpiGedState), 334 VMSTATE_END_OF_LIST() 335 } 336 }; 337 338 static bool cpuhp_needed(void *opaque) 339 { 340 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); 341 342 return mc->has_hotpluggable_cpus; 343 } 344 345 static const VMStateDescription vmstate_cpuhp_state = { 346 .name = "acpi-ged/cpuhp", 347 .version_id = 1, 348 .minimum_version_id = 1, 349 .needed = cpuhp_needed, 350 .fields = (VMStateField[]) { 351 VMSTATE_CPU_HOTPLUG(cpuhp_state, AcpiGedState), 352 VMSTATE_END_OF_LIST() 353 } 354 }; 355 356 static const VMStateDescription vmstate_ged_state = { 357 .name = "acpi-ged-state", 358 .version_id = 1, 359 .minimum_version_id = 1, 360 .fields = (const VMStateField[]) { 361 VMSTATE_UINT32(sel, GEDState), 362 VMSTATE_END_OF_LIST() 363 } 364 }; 365 366 static const VMStateDescription vmstate_ghes = { 367 .name = "acpi-ghes", 368 .version_id = 1, 369 .minimum_version_id = 1, 370 .fields = (const VMStateField[]) { 371 VMSTATE_UINT64(hw_error_le, AcpiGhesState), 372 VMSTATE_END_OF_LIST() 373 }, 374 }; 375 376 static bool ghes_needed(void *opaque) 377 { 378 AcpiGedState *s = opaque; 379 return s->ghes_state.hw_error_le; 380 } 381 382 static const VMStateDescription vmstate_ghes_state = { 383 .name = "acpi-ged/ghes", 384 .version_id = 1, 385 .minimum_version_id = 1, 386 .needed = ghes_needed, 387 .fields = (const VMStateField[]) { 388 VMSTATE_STRUCT(ghes_state, AcpiGedState, 1, 389 vmstate_ghes, AcpiGhesState), 390 VMSTATE_END_OF_LIST() 391 } 392 }; 393 394 static const VMStateDescription vmstate_acpi_ged = { 395 .name = "acpi-ged", 396 .version_id = 1, 397 .minimum_version_id = 1, 398 .fields = (const VMStateField[]) { 399 VMSTATE_STRUCT(ged_state, AcpiGedState, 1, vmstate_ged_state, GEDState), 400 VMSTATE_END_OF_LIST(), 401 }, 402 .subsections = (const VMStateDescription * const []) { 403 &vmstate_memhp_state, 404 &vmstate_cpuhp_state, 405 &vmstate_ghes_state, 406 NULL 407 } 408 }; 409 410 static void acpi_ged_realize(DeviceState *dev, Error **errp) 411 { 412 SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 413 AcpiGedState *s = ACPI_GED(dev); 414 uint32_t ged_events; 415 int i; 416 417 ged_events = ctpop32(s->ged_event_bitmap); 418 419 for (i = 0; i < ARRAY_SIZE(ged_supported_events) && ged_events; i++) { 420 uint32_t event = s->ged_event_bitmap & ged_supported_events[i]; 421 422 if (!event) { 423 continue; 424 } 425 426 switch (event) { 427 case ACPI_GED_CPU_HOTPLUG_EVT: 428 /* initialize CPU Hotplug related regions */ 429 memory_region_init(&s->container_cpuhp, OBJECT(dev), 430 "cpuhp container", 431 ACPI_CPU_HOTPLUG_REG_LEN); 432 sysbus_init_mmio(sbd, &s->container_cpuhp); 433 cpu_hotplug_hw_init(&s->container_cpuhp, OBJECT(dev), 434 &s->cpuhp_state, 0); 435 break; 436 } 437 ged_events--; 438 } 439 440 if (ged_events) { 441 error_report("Unsupported events specified"); 442 abort(); 443 } 444 } 445 446 static void acpi_ged_initfn(Object *obj) 447 { 448 DeviceState *dev = DEVICE(obj); 449 AcpiGedState *s = ACPI_GED(dev); 450 SysBusDevice *sbd = SYS_BUS_DEVICE(obj); 451 GEDState *ged_st = &s->ged_state; 452 453 memory_region_init_io(&ged_st->evt, obj, &ged_evt_ops, ged_st, 454 TYPE_ACPI_GED, ACPI_GED_EVT_SEL_LEN); 455 sysbus_init_mmio(sbd, &ged_st->evt); 456 457 sysbus_init_irq(sbd, &s->irq); 458 459 s->memhp_state.is_enabled = true; 460 /* 461 * GED handles memory hotplug event and acpi-mem-hotplug 462 * memory region gets initialized here. Create an exclusive 463 * container for memory hotplug IO and expose it as GED sysbus 464 * MMIO so that boards can map it separately. 465 */ 466 memory_region_init(&s->container_memhp, OBJECT(dev), "memhp container", 467 MEMORY_HOTPLUG_IO_LEN); 468 sysbus_init_mmio(sbd, &s->container_memhp); 469 acpi_memory_hotplug_init(&s->container_memhp, OBJECT(dev), 470 &s->memhp_state, 0); 471 472 memory_region_init_io(&ged_st->regs, obj, &ged_regs_ops, ged_st, 473 TYPE_ACPI_GED "-regs", ACPI_GED_REG_COUNT); 474 sysbus_init_mmio(sbd, &ged_st->regs); 475 } 476 477 static void acpi_ged_class_init(ObjectClass *class, const void *data) 478 { 479 DeviceClass *dc = DEVICE_CLASS(class); 480 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(class); 481 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_CLASS(class); 482 483 dc->desc = "ACPI Generic Event Device"; 484 device_class_set_props(dc, acpi_ged_properties); 485 dc->vmsd = &vmstate_acpi_ged; 486 dc->realize = acpi_ged_realize; 487 488 hc->plug = acpi_ged_device_plug_cb; 489 hc->unplug_request = acpi_ged_unplug_request_cb; 490 hc->unplug = acpi_ged_unplug_cb; 491 492 adevc->ospm_status = acpi_ged_ospm_status; 493 adevc->send_event = acpi_ged_send_event; 494 } 495 496 static const TypeInfo acpi_ged_info = { 497 .name = TYPE_ACPI_GED, 498 .parent = TYPE_SYS_BUS_DEVICE, 499 .instance_size = sizeof(AcpiGedState), 500 .instance_init = acpi_ged_initfn, 501 .class_init = acpi_ged_class_init, 502 .interfaces = (const InterfaceInfo[]) { 503 { TYPE_HOTPLUG_HANDLER }, 504 { TYPE_ACPI_DEVICE_IF }, 505 { } 506 } 507 }; 508 509 static void acpi_ged_register_types(void) 510 { 511 type_register_static(&acpi_ged_info); 512 } 513 514 type_init(acpi_ged_register_types) 515