ArmVirtPkg: reinstate timer unmask quirk for Xen
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Mon, 23 Apr 2018 14:55:18 +0000 (16:55 +0200)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Thu, 26 Apr 2018 06:31:40 +0000 (08:31 +0200)
Commit 411a373ed642 ("ArmPkg/TimerDxe: remove workaround for KVM timer
handling") removed the virtual timer handling quirk that cleared the
mask bit in the control register when enabling the timer, under the
assumption that only ancient KVM host implementations required it.
However, Julien reports that Xen also masks the timer interrupt in the
guest view of the timer control register, and therefore needs the same
quirk.

So let's reinstate it, but using a Xen specific implementation of the
timer support library, so that other virt platforms remain unchanged.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Leif Lindholm <leif.lindholm@linaro.org>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Tested-by: Julien Grall <julien.grall@arm.com>
Acked-by: Julien Grall <julien.grall@arm.com>
ArmVirtPkg/ArmVirtXen.dsc
ArmVirtPkg/Library/XenArmGenericTimerVirtCounterLib/XenArmGenericTimerVirtCounterLib.c [new file with mode: 0644]
ArmVirtPkg/Library/XenArmGenericTimerVirtCounterLib/XenArmGenericTimerVirtCounterLib.inf [new file with mode: 0644]

index 175b56d..8878912 100644 (file)
@@ -36,6 +36,7 @@
   RealTimeClockLib|ArmVirtPkg/Library/XenRealTimeClockLib/XenRealTimeClockLib.inf\r
   XenHypercallLib|OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf\r
 \r
+  ArmGenericTimerCounterLib|ArmVirtPkg/Library/XenArmGenericTimerVirtCounterLib/XenArmGenericTimerVirtCounterLib.inf\r
   ArmLib|ArmPkg/Library/ArmLib/ArmBaseLib.inf\r
   ArmMmuLib|ArmPkg/Library/ArmMmuLib/ArmMmuBaseLib.inf\r
 \r
diff --git a/ArmVirtPkg/Library/XenArmGenericTimerVirtCounterLib/XenArmGenericTimerVirtCounterLib.c b/ArmVirtPkg/Library/XenArmGenericTimerVirtCounterLib/XenArmGenericTimerVirtCounterLib.c
new file mode 100644 (file)
index 0000000..75c2816
--- /dev/null
@@ -0,0 +1,146 @@
+/** @file\r
+\r
+  Copyright (c) 2011 - 2014, ARM Ltd. All rights reserved.<BR>\r
+  Copyright (c) 2014 - 2018, Linaro Ltd. All rights reserved.<BR>\r
+\r
+  This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Library/ArmGenericTimerCounterLib.h>\r
+#include <Library/ArmLib.h>\r
+\r
+VOID\r
+EFIAPI\r
+ArmGenericTimerEnableTimer (\r
+  VOID\r
+  )\r
+{\r
+  UINTN TimerCtrlReg;\r
+\r
+  TimerCtrlReg = ArmReadCntvCtl ();\r
+  TimerCtrlReg |= ARM_ARCH_TIMER_ENABLE;\r
+  ArmWriteCntvCtl (TimerCtrlReg);\r
+}\r
+\r
+VOID\r
+EFIAPI\r
+ArmGenericTimerReenableTimer (\r
+  VOID\r
+  )\r
+{\r
+  UINTN TimerCtrlReg;\r
+\r
+  TimerCtrlReg = ArmReadCntvCtl ();\r
+  TimerCtrlReg |= ARM_ARCH_TIMER_ENABLE;\r
+\r
+  //\r
+  // When running under Xen, we need to unmask the interrupt on the timer side\r
+  // as Xen will mask it when servicing the interrupt at the hypervisor level\r
+  // and delivering the virtual timer interrupt to the guest. Otherwise, the\r
+  // interrupt will fire again, trapping into the hypervisor again, etc. etc.\r
+  //\r
+  TimerCtrlReg &= ~ARM_ARCH_TIMER_IMASK;\r
+  ArmWriteCntvCtl (TimerCtrlReg);\r
+}\r
+\r
+VOID\r
+EFIAPI\r
+ArmGenericTimerDisableTimer (\r
+  VOID\r
+  )\r
+{\r
+  UINTN TimerCtrlReg;\r
+\r
+  TimerCtrlReg = ArmReadCntvCtl ();\r
+  TimerCtrlReg &= ~ARM_ARCH_TIMER_ENABLE;\r
+  ArmWriteCntvCtl (TimerCtrlReg);\r
+}\r
+\r
+VOID\r
+EFIAPI\r
+ArmGenericTimerSetTimerFreq (\r
+  IN   UINTN  FreqInHz\r
+  )\r
+{\r
+  ArmWriteCntFrq (FreqInHz);\r
+}\r
+\r
+UINTN\r
+EFIAPI\r
+ArmGenericTimerGetTimerFreq (\r
+  VOID\r
+  )\r
+{\r
+  return ArmReadCntFrq ();\r
+}\r
+\r
+UINTN\r
+EFIAPI\r
+ArmGenericTimerGetTimerVal (\r
+  VOID\r
+  )\r
+{\r
+  return ArmReadCntvTval ();\r
+}\r
+\r
+\r
+VOID\r
+EFIAPI\r
+ArmGenericTimerSetTimerVal (\r
+  IN   UINTN   Value\r
+  )\r
+{\r
+  ArmWriteCntvTval (Value);\r
+}\r
+\r
+UINT64\r
+EFIAPI\r
+ArmGenericTimerGetSystemCount (\r
+  VOID\r
+  )\r
+{\r
+  return ArmReadCntvCt ();\r
+}\r
+\r
+UINTN\r
+EFIAPI\r
+ArmGenericTimerGetTimerCtrlReg (\r
+  VOID\r
+  )\r
+{\r
+  return ArmReadCntvCtl ();\r
+}\r
+\r
+VOID\r
+EFIAPI\r
+ArmGenericTimerSetTimerCtrlReg (\r
+  UINTN Value\r
+  )\r
+{\r
+  ArmWriteCntvCtl (Value);\r
+}\r
+\r
+UINT64\r
+EFIAPI\r
+ArmGenericTimerGetCompareVal (\r
+  VOID\r
+  )\r
+{\r
+  return ArmReadCntvCval ();\r
+}\r
+\r
+VOID\r
+EFIAPI\r
+ArmGenericTimerSetCompareVal (\r
+  IN   UINT64   Value\r
+  )\r
+{\r
+  ArmWriteCntvCval (Value);\r
+}\r
diff --git a/ArmVirtPkg/Library/XenArmGenericTimerVirtCounterLib/XenArmGenericTimerVirtCounterLib.inf b/ArmVirtPkg/Library/XenArmGenericTimerVirtCounterLib/XenArmGenericTimerVirtCounterLib.inf
new file mode 100644 (file)
index 0000000..bd6ac80
--- /dev/null
@@ -0,0 +1,33 @@
+#/** @file\r
+#  Implement ArmGenericTimerCounterLib for Xen using the virtual timer\r
+#\r
+#  Copyright (c) 2014 - 2018, Linaro Ltd. All rights reserved.<BR>\r
+#\r
+#  This program and the accompanying materials\r
+#  are licensed and made available under the terms and conditions of the BSD License\r
+#  which accompanies this distribution. The full text of the license may be found at\r
+#  http://opensource.org/licenses/bsd-license.php\r
+#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+#\r
+#**/\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x0001001A\r
+  BASE_NAME                      = XenArmGenericTimerVirtCounterLib\r
+  FILE_GUID                      = e3913319-96ac-4ac0-808b-8edb8776a51c\r
+  MODULE_TYPE                    = BASE\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = ArmGenericTimerCounterLib\r
+\r
+[Sources]\r
+  XenArmGenericTimerVirtCounterLib.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  ArmPkg/ArmPkg.dec\r
+\r
+[LibraryClasses]\r
+  ArmLib\r
+  BaseLib\r