1 /** @file
2 Runtime Lib function for QNC internal network access.
3 
4 Copyright (c) 2013-2015 Intel Corporation.
5 
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution.  The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10 
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 
17 #include <PiDxe.h>
18 
19 #include <Guid/EventGroup.h>
20 
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/UefiRuntimeLib.h>
25 #include <Library/QNCAccessLib.h>
26 
27 ///
28 /// Set Virtual Address Map Event
29 ///
30 EFI_EVENT                               mDxeRuntimeQncAccessLibVirtualNotifyEvent = NULL;
31 
32 ///
33 /// Module global that contains the base physical address of the PCI Express MMIO range.
34 ///
35 UINTN                                   mDxeRuntimeQncAccessLibPciExpressBaseAddress = 0;
36 
37 /**
38   Convert the physical PCI Express MMIO address to a virtual address.
39 
40   @param[in]    Event   The event that is being processed.
41   @param[in]    Context The Event Context.
42 **/
43 VOID
44 EFIAPI
DxeRuntimeQncAccessLibVirtualNotify(IN EFI_EVENT Event,IN VOID * Context)45 DxeRuntimeQncAccessLibVirtualNotify (
46   IN EFI_EVENT  Event,
47   IN VOID       *Context
48   )
49 {
50   EFI_STATUS                       Status;
51 
52   //
53   // Convert the physical PCI Express MMIO address to a virtual address.
54   //
55   Status = EfiConvertPointer (0, (VOID **) &mDxeRuntimeQncAccessLibPciExpressBaseAddress);
56 
57   ASSERT_EFI_ERROR (Status);
58 }
59 
60 /**
61   The constructor function to setup globals and goto virtual mode notify.
62 
63   @param  ImageHandle   The firmware allocated handle for the EFI image.
64   @param  SystemTable   A pointer to the EFI System Table.
65 
66   @retval EFI_SUCCESS   The constructor completed successfully.
67   @retval Other value   The constructor did not complete successfully.
68 
69 **/
70 EFI_STATUS
71 EFIAPI
DxeRuntimeQncAccessLibConstructor(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)72 DxeRuntimeQncAccessLibConstructor (
73   IN EFI_HANDLE        ImageHandle,
74   IN EFI_SYSTEM_TABLE  *SystemTable
75   )
76 {
77   EFI_STATUS  Status;
78 
79   //
80   // Cache the physical address of the PCI Express MMIO range into a module global variable
81   //
82   mDxeRuntimeQncAccessLibPciExpressBaseAddress = (UINTN) PcdGet64(PcdPciExpressBaseAddress);
83 
84   //
85   // Register SetVirtualAddressMap () notify function
86   //
87   Status = gBS->CreateEventEx (
88                   EVT_NOTIFY_SIGNAL,
89                   TPL_NOTIFY,
90                   DxeRuntimeQncAccessLibVirtualNotify,
91                   NULL,
92                   &gEfiEventVirtualAddressChangeGuid,
93                   &mDxeRuntimeQncAccessLibVirtualNotifyEvent
94                   );
95   ASSERT_EFI_ERROR (Status);
96 
97   return Status;
98 }
99 
100 /**
101   The destructor function frees any allocated buffers and closes the Set Virtual
102   Address Map event.
103 
104   @param  ImageHandle   The firmware allocated handle for the EFI image.
105   @param  SystemTable   A pointer to the EFI System Table.
106 
107   @retval EFI_SUCCESS   The destructor completed successfully.
108   @retval Other value   The destructor did not complete successfully.
109 
110 **/
111 EFI_STATUS
112 EFIAPI
DxeRuntimeQncAccessLibDestructor(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)113 DxeRuntimeQncAccessLibDestructor (
114   IN EFI_HANDLE        ImageHandle,
115   IN EFI_SYSTEM_TABLE  *SystemTable
116   )
117 {
118   EFI_STATUS  Status;
119 
120   //
121   // Close the Set Virtual Address Map event
122   //
123   Status = gBS->CloseEvent (mDxeRuntimeQncAccessLibVirtualNotifyEvent);
124   ASSERT_EFI_ERROR (Status);
125 
126   return Status;
127 }
128 
129 /**
130   Gets the base address of PCI Express for Quark North Cluster.
131 
132   @return The base address of PCI Express for Quark North Cluster.
133 
134 **/
135 UINTN
136 EFIAPI
QncGetPciExpressBaseAddress(VOID)137 QncGetPciExpressBaseAddress (
138   VOID
139   )
140 {
141   //
142   // If system goes to virtual mode then virtual notify callback will update
143   // mDxeRuntimeQncAccessLibPciExpressBaseAddress with virtual address of
144   // PCIe memory base.
145   //
146   return mDxeRuntimeQncAccessLibPciExpressBaseAddress;
147 }
148 
149