summaryrefslogtreecommitdiff
path: root/OvmfPkg/Library/ResetSystemLib/BaseResetShutdownXen.c
blob: 50c9fe9c505aa9894b4950922b658f51c7694670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/** @file
  Base Reset System Library Shutdown API implementation for OVMF.

  Copyright (C) 2020, Red Hat, Inc.
  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
  Copyright (c) 2022, Citrix Systems, Inc.

  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Base.h>                   // BIT13

#include <IndustryStandard/Xen/sched.h>
#include <Library/BaseLib.h>        // CpuDeadLoop()
#include <Library/DebugLib.h>       // ASSERT()
#include <Library/IoLib.h>          // IoOr16()
#include <Library/PciLib.h>         // PciRead16()
#include <Library/ResetSystemLib.h> // ResetShutdown()
#include <Library/XenHypercallLib.h>
#include <OvmfPlatforms.h>          // OVMF_HOSTBRIDGE_DID

/**
  Calling this function causes the system to enter a power state equivalent
  to the ACPI G2/S5 or G3 states.

  System shutdown should not return, if it returns, it means the system does
  not support shut down reset.
**/
VOID
EFIAPI
ResetShutdown (
  VOID
  )
{
  UINT16  AcpiPmBaseAddress;
  UINT16  HostBridgeDevId;

  AcpiPmBaseAddress = 0;
  HostBridgeDevId   = PciRead16 (OVMF_HOSTBRIDGE_DID);
  switch (HostBridgeDevId) {
    case INTEL_82441_DEVICE_ID:
      AcpiPmBaseAddress = PIIX4_PMBA_VALUE;
      break;
    case INTEL_Q35_MCH_DEVICE_ID:
      AcpiPmBaseAddress = ICH9_PMBASE_VALUE;
      break;
    default:
    {
      //
      // Fallback to using hypercall.
      // Necessary for PVH guest, but should work for HVM guest.
      //
      INTN                ReturnCode;
      XEN_SCHED_SHUTDOWN  ShutdownOp = {
        .Reason = XEN_SHED_SHUTDOWN_POWEROFF,
      };
      ReturnCode = XenHypercallSchedOp (XEN_SCHEDOP_SHUTDOWN, &ShutdownOp);
      ASSERT (ReturnCode == 0);
      CpuDeadLoop ();
    }
  }

  IoBitFieldWrite16 (AcpiPmBaseAddress + 4, 10, 13, 0);
  IoOr16 (AcpiPmBaseAddress + 4, BIT13);
  CpuDeadLoop ();
}