]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg TerminalDxe: Execute key notify func at TPL_CALLBACK
authorStar Zeng <star.zeng@intel.com>
Mon, 19 Dec 2016 14:21:11 +0000 (22:21 +0800)
committerStar Zeng <star.zeng@intel.com>
Mon, 26 Dec 2016 10:16:59 +0000 (18:16 +0800)
Current implementation executes key notify function in TimerHandler
at TPL_NOTIFY. The code change is to make key notify function
executed at TPL_CALLBACK to reduce the time occupied at TPL_NOTIFY.

The code will signal KeyNotify process event if the key pressed
matches any key registered and insert the KeyData to the EFI Key
queue for notify, then the KeyNotify process handler will invoke
key notify functions at TPL_CALLBACK.

Cc: Ruiyu Ni <Ruiyu.ni@intel.com>
Cc: Michael Kinney <michael.d.kinney@intel.com>
Cc: Feng Tian <feng.tian@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Ruiyu Ni <Ruiyu.ni@intel.com>
MdeModulePkg/Universal/Console/TerminalDxe/Terminal.c
MdeModulePkg/Universal/Console/TerminalDxe/Terminal.h
MdeModulePkg/Universal/Console/TerminalDxe/TerminalConIn.c

index 3f445f02721fce00b7ce82e88904bb2f6bda7336..a209bf3cacf8e729432c9677af42f5030ee5de94 100644 (file)
@@ -2,7 +2,7 @@
   Produces Simple Text Input Protocol, Simple Text Input Extended Protocol and\r
   Simple Text Output Protocol upon Serial IO Protocol.\r
 \r
-Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\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
@@ -75,6 +75,7 @@ TERMINAL_DEV  mTerminalDevTemplate = {
   NULL, // RawFifo\r
   NULL, // UnicodeFiFo\r
   NULL, // EfiKeyFiFo\r
+  NULL, // EfiKeyFiFoForNotify\r
 \r
   NULL, // ControllerNameTable\r
   NULL, // TimerEvent\r
@@ -99,7 +100,8 @@ TERMINAL_DEV  mTerminalDevTemplate = {
   {   // NotifyList\r
     NULL,\r
     NULL,\r
-  }\r
+  },\r
+  NULL // KeyNotifyProcessEvent\r
 };\r
 \r
 TERMINAL_CONSOLE_MODE_DATA mTerminalConsoleModeData[] = {\r
@@ -791,6 +793,10 @@ TerminalDriverBindingStart (
     if (TerminalDevice->EfiKeyFiFo == NULL) {\r
       goto Error;\r
     }\r
+    TerminalDevice->EfiKeyFiFoForNotify = AllocateZeroPool (sizeof (EFI_KEY_FIFO));\r
+    if (TerminalDevice->EfiKeyFiFoForNotify == NULL) {\r
+      goto Error;\r
+    }\r
 \r
     //\r
     // Set the timeout value of serial buffer for\r
@@ -1000,6 +1006,15 @@ TerminalDriverBindingStart (
                     );\r
     ASSERT_EFI_ERROR (Status);\r
 \r
+    Status = gBS->CreateEvent (\r
+                    EVT_NOTIFY_SIGNAL,\r
+                    TPL_CALLBACK,\r
+                    KeyNotifyProcessHandler,\r
+                    TerminalDevice,\r
+                    &TerminalDevice->KeyNotifyProcessEvent\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
     Status = gBS->InstallProtocolInterface (\r
                     &TerminalDevice->Handle,\r
                     &gEfiDevicePathProtocolGuid,\r
@@ -1222,7 +1237,10 @@ Error:
       if (TerminalDevice->EfiKeyFiFo != NULL) {\r
         FreePool (TerminalDevice->EfiKeyFiFo);\r
       }\r
-\r
+      if (TerminalDevice->EfiKeyFiFoForNotify != NULL) {\r
+        FreePool (TerminalDevice->EfiKeyFiFoForNotify);\r
+      }\r
+  \r
       if (TerminalDevice->ControllerNameTable != NULL) {\r
         FreeUnicodeStringTable (TerminalDevice->ControllerNameTable);\r
       }\r
@@ -1400,6 +1418,7 @@ TerminalDriverBindingStop (
         gBS->CloseEvent (TerminalDevice->TwoSecondTimeOut);\r
         gBS->CloseEvent (TerminalDevice->SimpleInput.WaitForKey);\r
         gBS->CloseEvent (TerminalDevice->SimpleInputEx.WaitForKeyEx);\r
+        gBS->CloseEvent (TerminalDevice->KeyNotifyProcessEvent);\r
         TerminalFreeNotifyList (&TerminalDevice->NotifyList);\r
         FreePool (TerminalDevice->DevicePath);\r
         if (TerminalDevice->TerminalConsoleModeData != NULL) {\r
index 3ee396984e6a8a95374aa8e2190d561ddb637425..e16b89c264ce2a203d519c5c70fa5667e26227df 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Header file for Terminal driver.\r
 \r
-Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
 Copyright (C) 2016 Silicon Graphics, Inc. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
@@ -95,6 +95,7 @@ typedef struct {
   RAW_DATA_FIFO                       *RawFiFo;\r
   UNICODE_FIFO                        *UnicodeFiFo;\r
   EFI_KEY_FIFO                        *EfiKeyFiFo;\r
+  EFI_KEY_FIFO                        *EfiKeyFiFoForNotify;\r
   EFI_UNICODE_STRING_TABLE            *ControllerNameTable;\r
   EFI_EVENT                           TimerEvent;\r
   EFI_EVENT                           TwoSecondTimeOut;\r
@@ -113,6 +114,7 @@ typedef struct {
   BOOLEAN                             OutputEscChar;\r
   EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL   SimpleInputEx;\r
   LIST_ENTRY                          NotifyList;\r
+  EFI_EVENT                           KeyNotifyProcessEvent;\r
 } TERMINAL_DEV;\r
 \r
 #define INPUT_STATE_DEFAULT               0x00\r
@@ -941,6 +943,67 @@ IsRawFiFoFull (
   TERMINAL_DEV  *TerminalDevice\r
   );\r
 \r
+/**\r
+  Insert one pre-fetched key into the FIFO buffer.\r
+\r
+  @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.\r
+  @param  Input                 The key will be input.\r
+\r
+  @retval TRUE                  If insert successfully.\r
+  @retval FALSE                 If FIFO buffer is full before key insertion,\r
+                                and the key is lost.\r
+\r
+**/\r
+BOOLEAN\r
+EfiKeyFiFoForNotifyInsertOneKey (\r
+  EFI_KEY_FIFO                  *EfiKeyFiFo,\r
+  EFI_INPUT_KEY                 *Input\r
+  );\r
+\r
+/**\r
+  Remove one pre-fetched key out of the FIFO buffer.\r
+\r
+  @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.\r
+  @param  Output                The key will be removed.\r
+\r
+  @retval TRUE                  If insert successfully.\r
+  @retval FALSE                 If FIFO buffer is empty before remove operation.\r
+\r
+**/\r
+BOOLEAN\r
+EfiKeyFiFoForNotifyRemoveOneKey (\r
+  EFI_KEY_FIFO                  *EfiKeyFiFo,\r
+  EFI_INPUT_KEY                 *Output\r
+  );\r
+\r
+/**\r
+  Clarify whether FIFO buffer is empty.\r
+\r
+  @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.\r
+\r
+  @retval TRUE                  If FIFO buffer is empty.\r
+  @retval FALSE                 If FIFO buffer is not empty.\r
+\r
+**/\r
+BOOLEAN\r
+IsEfiKeyFiFoForNotifyEmpty (\r
+  IN EFI_KEY_FIFO               *EfiKeyFiFo\r
+  );\r
+\r
+/**\r
+  Clarify whether FIFO buffer is full.\r
+\r
+  @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.\r
+\r
+  @retval TRUE                  If FIFO buffer is full.\r
+  @retval FALSE                 If FIFO buffer is not full.\r
+\r
+**/\r
+BOOLEAN\r
+IsEfiKeyFiFoForNotifyFull (\r
+  EFI_KEY_FIFO                  *EfiKeyFiFo\r
+  );\r
+\r
 /**\r
   Insert one pre-fetched key into the FIFO buffer.\r
 \r
@@ -1360,4 +1423,19 @@ TerminalConInTimerHandler (
   IN EFI_EVENT            Event,\r
   IN VOID                 *Context\r
   );\r
+\r
+\r
+/**\r
+  Process key notify.\r
+\r
+  @param  Event                 Indicates the event that invoke this function.\r
+  @param  Context               Indicates the calling context.\r
+**/\r
+VOID\r
+EFIAPI\r
+KeyNotifyProcessHandler (\r
+  IN  EFI_EVENT                 Event,\r
+  IN  VOID                      *Context\r
+  );\r
+\r
 #endif\r
index 5c3ea86fe104f1c6aeb68483aba97721f76681b4..016241017a42693ec05ab57fa4d84953256cdd84 100644 (file)
@@ -2,7 +2,7 @@
   Implementation for EFI_SIMPLE_TEXT_INPUT_PROTOCOL protocol.\r
 \r
 (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>\r
-Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
 Copyright (C) 2016 Silicon Graphics, Inc. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
@@ -94,6 +94,7 @@ TerminalConInReset (
   TerminalDevice->RawFiFo->Head     = TerminalDevice->RawFiFo->Tail;\r
   TerminalDevice->UnicodeFiFo->Head = TerminalDevice->UnicodeFiFo->Tail;\r
   TerminalDevice->EfiKeyFiFo->Head  = TerminalDevice->EfiKeyFiFo->Tail;\r
+  TerminalDevice->EfiKeyFiFoForNotify->Head = TerminalDevice->EfiKeyFiFoForNotify->Tail;\r
 \r
   if (EFI_ERROR (Status)) {\r
     REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
@@ -598,6 +599,59 @@ TerminalConInTimerHandler (
   TranslateRawDataToEfiKey (TerminalDevice);\r
 }\r
 \r
+/**\r
+  Process key notify.\r
+\r
+  @param  Event                 Indicates the event that invoke this function.\r
+  @param  Context               Indicates the calling context.\r
+**/\r
+VOID\r
+EFIAPI\r
+KeyNotifyProcessHandler (\r
+  IN  EFI_EVENT                 Event,\r
+  IN  VOID                      *Context\r
+  )\r
+{\r
+  BOOLEAN                       HasKey;\r
+  TERMINAL_DEV                  *TerminalDevice;\r
+  EFI_INPUT_KEY                 Key;\r
+  EFI_KEY_DATA                  KeyData;\r
+  LIST_ENTRY                    *Link;\r
+  LIST_ENTRY                    *NotifyList;\r
+  TERMINAL_CONSOLE_IN_EX_NOTIFY *CurrentNotify;\r
+  EFI_TPL                       OldTpl;\r
+\r
+  TerminalDevice = (TERMINAL_DEV *) Context;\r
+\r
+  //\r
+  // Invoke notification functions.\r
+  //\r
+  NotifyList = &TerminalDevice->NotifyList;\r
+  while (TRUE) {\r
+    //\r
+    // Enter critical section\r
+    //  \r
+    OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+    HasKey = EfiKeyFiFoForNotifyRemoveOneKey (TerminalDevice->EfiKeyFiFoForNotify, &Key);\r
+    CopyMem (&KeyData.Key, &Key, sizeof (EFI_INPUT_KEY));\r
+    KeyData.KeyState.KeyShiftState  = 0;\r
+    KeyData.KeyState.KeyToggleState = 0;\r
+    //\r
+    // Leave critical section\r
+    //\r
+    gBS->RestoreTPL (OldTpl);\r
+    if (!HasKey) {\r
+      break;\r
+    }\r
+    for (Link = GetFirstNode (NotifyList); !IsNull (NotifyList, Link); Link = GetNextNode (NotifyList, Link)) {\r
+      CurrentNotify = CR (Link, TERMINAL_CONSOLE_IN_EX_NOTIFY, NotifyEntry, TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE);\r
+      if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {\r
+        CurrentNotify->KeyNotificationFn (&KeyData);\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
 /**\r
   Get one key out of serial buffer.\r
 \r
@@ -763,6 +817,126 @@ IsRawFiFoFull (
   return FALSE;\r
 }\r
 \r
+/**\r
+  Insert one pre-fetched key into the FIFO buffer.\r
+\r
+  @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.\r
+  @param  Input                 The key will be input.\r
+\r
+  @retval TRUE                  If insert successfully.\r
+  @retval FALSE                 If FIFO buffer is full before key insertion,\r
+                                and the key is lost.\r
+\r
+**/\r
+BOOLEAN\r
+EfiKeyFiFoForNotifyInsertOneKey (\r
+  EFI_KEY_FIFO                  *EfiKeyFiFo,\r
+  EFI_INPUT_KEY                 *Input\r
+  )\r
+{\r
+  UINT8                         Tail;\r
+\r
+  Tail = EfiKeyFiFo->Tail;\r
+\r
+  if (IsEfiKeyFiFoForNotifyFull (EfiKeyFiFo)) {\r
+    //\r
+    // FIFO is full\r
+    //\r
+    return FALSE;\r
+  }\r
+\r
+  CopyMem (&EfiKeyFiFo->Data[Tail], Input, sizeof (EFI_INPUT_KEY));\r
+\r
+  EfiKeyFiFo->Tail = (UINT8) ((Tail + 1) % (FIFO_MAX_NUMBER + 1));\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Remove one pre-fetched key out of the FIFO buffer.\r
+\r
+  @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.\r
+  @param  Output                The key will be removed.\r
+\r
+  @retval TRUE                  If remove successfully.\r
+  @retval FALSE                 If FIFO buffer is empty before remove operation.\r
+\r
+**/\r
+BOOLEAN\r
+EfiKeyFiFoForNotifyRemoveOneKey (\r
+  EFI_KEY_FIFO                  *EfiKeyFiFo,\r
+  EFI_INPUT_KEY                 *Output\r
+  )\r
+{\r
+  UINT8                         Head;\r
+\r
+  Head = EfiKeyFiFo->Head;\r
+  ASSERT (Head < FIFO_MAX_NUMBER + 1);\r
+\r
+  if (IsEfiKeyFiFoForNotifyEmpty (EfiKeyFiFo)) {\r
+    //\r
+    // FIFO is empty\r
+    //\r
+    Output->ScanCode    = SCAN_NULL;\r
+    Output->UnicodeChar = 0;\r
+    return FALSE;\r
+  }\r
+\r
+  CopyMem (Output, &EfiKeyFiFo->Data[Head], sizeof (EFI_INPUT_KEY));\r
+\r
+  EfiKeyFiFo->Head = (UINT8) ((Head + 1) % (FIFO_MAX_NUMBER + 1));\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Clarify whether FIFO buffer is empty.\r
+\r
+  @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.\r
+\r
+  @retval TRUE                  If FIFO buffer is empty.\r
+  @retval FALSE                 If FIFO buffer is not empty.\r
+\r
+**/\r
+BOOLEAN\r
+IsEfiKeyFiFoForNotifyEmpty (\r
+  EFI_KEY_FIFO                  *EfiKeyFiFo\r
+  )\r
+{\r
+  if (EfiKeyFiFo->Head == EfiKeyFiFo->Tail) {\r
+    return TRUE;\r
+  } else {\r
+    return FALSE;\r
+  }\r
+}\r
+\r
+/**\r
+  Clarify whether FIFO buffer is full.\r
+\r
+  @param  EfiKeyFiFo            Pointer to instance of EFI_KEY_FIFO.\r
+\r
+  @retval TRUE                  If FIFO buffer is full.\r
+  @retval FALSE                 If FIFO buffer is not full.\r
+\r
+**/\r
+BOOLEAN\r
+IsEfiKeyFiFoForNotifyFull (\r
+  EFI_KEY_FIFO                  *EfiKeyFiFo\r
+  )\r
+{\r
+  UINT8                         Tail;\r
+  UINT8                         Head;\r
+\r
+  Tail = EfiKeyFiFo->Tail;\r
+  Head = EfiKeyFiFo->Head;\r
+\r
+  if (((Tail + 1) % (FIFO_MAX_NUMBER + 1)) == Head) {\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
 /**\r
   Insert one pre-fetched key into the FIFO buffer.\r
 \r
@@ -793,7 +967,7 @@ EfiKeyFiFoInsertOneKey (
   KeyData.KeyState.KeyToggleState = 0;\r
 \r
   //\r
-  // Invoke notification functions if exist\r
+  // Signal KeyNotify process event if this key pressed matches any key registered.\r
   //\r
   NotifyList = &TerminalDevice->NotifyList;\r
   for (Link = GetFirstNode (NotifyList); !IsNull (NotifyList,Link); Link = GetNextNode (NotifyList,Link)) {\r
@@ -804,7 +978,13 @@ EfiKeyFiFoInsertOneKey (
                       TERMINAL_CONSOLE_IN_EX_NOTIFY_SIGNATURE\r
                       );\r
     if (IsKeyRegistered (&CurrentNotify->KeyData, &KeyData)) {\r
-      CurrentNotify->KeyNotificationFn (&KeyData);\r
+      //\r
+      // The key notification function needs to run at TPL_CALLBACK\r
+      // while current TPL is TPL_NOTIFY. It will be invoked in\r
+      // KeyNotifyProcessHandler() which runs at TPL_CALLBACK.\r
+      //\r
+      EfiKeyFiFoForNotifyInsertOneKey (TerminalDevice->EfiKeyFiFoForNotify, Key);\r
+      gBS->SignalEvent (TerminalDevice->KeyNotifyProcessEvent);\r
     }\r
   }\r
   if (IsEfiKeyFiFoFull (TerminalDevice)) {\r