]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/XenConsoleSerialPortLib/XenConsoleSerialPortLib.c
OvmfPkg/QemuFwCfgLib: Prepare for SEV support
[mirror_edk2.git] / OvmfPkg / Library / XenConsoleSerialPortLib / XenConsoleSerialPortLib.c
CommitLineData
d401a487
AB
1/** @file\r
2 Xen console SerialPortLib instance\r
3\r
4 Copyright (c) 2015, Linaro Ltd. All rights reserved.<BR>\r
ece2806d 5 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
d401a487
AB
6\r
7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include <Base.h>\r
d401a487
AB
18\r
19#include <Library/BaseLib.h>\r
20#include <Library/SerialPortLib.h>\r
21#include <Library/XenHypercallLib.h>\r
22\r
23#include <IndustryStandard/Xen/io/console.h>\r
24#include <IndustryStandard/Xen/hvm/params.h>\r
25#include <IndustryStandard/Xen/event_channel.h>\r
26\r
c6d29726
AB
27//\r
28// We can't use DebugLib due to a constructor dependency cycle between DebugLib\r
29// and ourselves.\r
30//\r
31#define ASSERT(Expression) \\r
32 do { \\r
33 if (!(Expression)) { \\r
34 CpuDeadLoop (); \\r
35 } \\r
36 } while (FALSE)\r
37\r
d401a487
AB
38//\r
39// The code below expects these global variables to be mutable, even in the case\r
40// that we have been incorporated into SEC or PEIM phase modules (which is\r
41// allowed by our INF description). While this is a dangerous assumption to make\r
42// in general, it is actually fine for the Xen domU (guest) environment that\r
43// this module is intended for, as UEFI always executes from DRAM in that case.\r
44//\r
45STATIC evtchn_send_t mXenConsoleEventChain;\r
46STATIC struct xencons_interface *mXenConsoleInterface;\r
47\r
c6d29726
AB
48/**\r
49 Initialize the serial device hardware.\r
50\r
51 If no initialization is required, then return RETURN_SUCCESS.\r
52 If the serial device was successfully initialized, then return RETURN_SUCCESS.\r
53 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.\r
54\r
55 @retval RETURN_SUCCESS The serial device was initialized.\r
56 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.\r
57\r
58**/\r
d401a487
AB
59RETURN_STATUS\r
60EFIAPI\r
61SerialPortInitialize (\r
62 VOID\r
63 )\r
64{\r
48b3ff04 65 if (! XenHypercallIsAvailable ()) {\r
c6d29726 66 return RETURN_DEVICE_ERROR;\r
48b3ff04
LE
67 }\r
68\r
d401a487
AB
69 if (!mXenConsoleInterface) {\r
70 mXenConsoleEventChain.port = (UINT32)XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_EVTCHN);\r
71 mXenConsoleInterface = (struct xencons_interface *)(UINTN)\r
72 (XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_PFN) << EFI_PAGE_SHIFT);\r
73\r
74 //\r
75 // No point in ASSERT'ing here as we won't be seeing the output\r
76 //\r
77 }\r
78 return RETURN_SUCCESS;\r
79}\r
80\r
81/**\r
c6d29726
AB
82 Write data from buffer to serial device.\r
83\r
84 Writes NumberOfBytes data bytes from Buffer to the serial device.\r
85 The number of bytes actually written to the serial device is returned.\r
86 If the return value is less than NumberOfBytes, then the write operation failed.\r
87 If Buffer is NULL, then ASSERT().\r
88 If NumberOfBytes is zero, then return 0.\r
d401a487 89\r
c6d29726
AB
90 @param Buffer Pointer to the data buffer to be written.\r
91 @param NumberOfBytes Number of bytes to written to the serial device.\r
d401a487 92\r
c6d29726
AB
93 @retval 0 NumberOfBytes is 0.\r
94 @retval >0 The number of bytes written to the serial device.\r
95 If this value is less than NumberOfBytes, then the write operation failed.\r
d401a487
AB
96\r
97**/\r
98UINTN\r
99EFIAPI\r
100SerialPortWrite (\r
101 IN UINT8 *Buffer,\r
102 IN UINTN NumberOfBytes\r
103 )\r
104{\r
105 XENCONS_RING_IDX Consumer, Producer;\r
106 UINTN Sent;\r
107\r
c6d29726
AB
108 ASSERT (Buffer != NULL);\r
109\r
110 if (NumberOfBytes == 0) {\r
111 return 0;\r
112 }\r
113\r
d401a487
AB
114 if (!mXenConsoleInterface) {\r
115 return 0;\r
116 }\r
117\r
c6d29726
AB
118 Sent = 0;\r
119 do {\r
120 Consumer = mXenConsoleInterface->out_cons;\r
121 Producer = mXenConsoleInterface->out_prod;\r
d401a487 122\r
c6d29726 123 MemoryFence ();\r
d401a487 124\r
c6d29726
AB
125 while (Sent < NumberOfBytes && ((Producer - Consumer) < sizeof (mXenConsoleInterface->out)))\r
126 mXenConsoleInterface->out[MASK_XENCONS_IDX(Producer++, mXenConsoleInterface->out)] = Buffer[Sent++];\r
d401a487 127\r
c6d29726 128 MemoryFence ();\r
d401a487 129\r
c6d29726 130 mXenConsoleInterface->out_prod = Producer;\r
d401a487 131\r
d401a487 132 XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);\r
c6d29726
AB
133\r
134 } while (Sent < NumberOfBytes);\r
d401a487
AB
135\r
136 return Sent;\r
137}\r
138\r
139/**\r
c6d29726
AB
140 Read data from serial device and save the datas in buffer.\r
141\r
142 Reads NumberOfBytes data bytes from a serial device into the buffer\r
143 specified by Buffer. The number of bytes actually read is returned.\r
144 If Buffer is NULL, then ASSERT().\r
145 If NumberOfBytes is zero, then return 0.\r
d401a487 146\r
c6d29726
AB
147 @param Buffer Pointer to the data buffer to store the data read from the serial device.\r
148 @param NumberOfBytes Number of bytes which will be read.\r
d401a487 149\r
c6d29726
AB
150 @retval 0 Read data failed, no data is to be read.\r
151 @retval >0 Actual number of bytes read from serial device.\r
d401a487
AB
152\r
153**/\r
154UINTN\r
155EFIAPI\r
156SerialPortRead (\r
157 OUT UINT8 *Buffer,\r
158 IN UINTN NumberOfBytes\r
159)\r
160{\r
161 XENCONS_RING_IDX Consumer, Producer;\r
162 UINTN Received;\r
163\r
c6d29726
AB
164 ASSERT (Buffer != NULL);\r
165\r
166 if (NumberOfBytes == 0) {\r
167 return 0;\r
168 }\r
169\r
d401a487
AB
170 if (!mXenConsoleInterface) {\r
171 return 0;\r
172 }\r
173\r
174 Consumer = mXenConsoleInterface->in_cons;\r
175 Producer = mXenConsoleInterface->in_prod;\r
176\r
177 MemoryFence ();\r
178\r
179 Received = 0;\r
180 while (Received < NumberOfBytes && Consumer < Producer)\r
181 Buffer[Received++] = mXenConsoleInterface->in[MASK_XENCONS_IDX(Consumer++, mXenConsoleInterface->in)];\r
182\r
183 MemoryFence ();\r
184\r
185 mXenConsoleInterface->in_cons = Consumer;\r
186\r
187 XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);\r
188\r
189 return Received;\r
190}\r
191\r
192/**\r
c6d29726 193 Polls a serial device to see if there is any data waiting to be read.\r
d401a487 194\r
c6d29726
AB
195 @retval TRUE Data is waiting to be read from the serial device.\r
196 @retval FALSE There is no data waiting to be read from the serial device.\r
d401a487
AB
197\r
198**/\r
199BOOLEAN\r
200EFIAPI\r
201SerialPortPoll (\r
202 VOID\r
203 )\r
204{\r
205 return mXenConsoleInterface &&\r
206 mXenConsoleInterface->in_cons != mXenConsoleInterface->in_prod;\r
207}\r
ece2806d
SZ
208\r
209/**\r
210 Sets the control bits on a serial device.\r
211\r
212 @param Control Sets the bits of Control that are settable.\r
213\r
214 @retval RETURN_SUCCESS The new control bits were set on the serial device.\r
215 @retval RETURN_UNSUPPORTED The serial device does not support this operation.\r
216 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.\r
217\r
218**/\r
219RETURN_STATUS\r
220EFIAPI\r
221SerialPortSetControl (\r
222 IN UINT32 Control\r
223 )\r
224{\r
225 return RETURN_UNSUPPORTED;\r
226}\r
227\r
228/**\r
229 Retrieve the status of the control bits on a serial device.\r
230\r
231 @param Control A pointer to return the current control signals from the serial device.\r
232\r
233 @retval RETURN_SUCCESS The control bits were read from the serial device.\r
234 @retval RETURN_UNSUPPORTED The serial device does not support this operation.\r
235 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.\r
236\r
237**/\r
238RETURN_STATUS\r
239EFIAPI\r
240SerialPortGetControl (\r
241 OUT UINT32 *Control\r
242 )\r
243{\r
244 if (!mXenConsoleInterface) {\r
245 return RETURN_UNSUPPORTED;\r
246 }\r
247\r
248 *Control = 0;\r
249 if (!SerialPortPoll ()) {\r
250 *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY;\r
251 }\r
252 return RETURN_SUCCESS;\r
253}\r
254\r
255/**\r
256 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,\r
257 data bits, and stop bits on a serial device.\r
258\r
259 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the\r
260 device's default interface speed.\r
261 On output, the value actually set.\r
262 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the\r
263 serial interface. A ReceiveFifoDepth value of 0 will use\r
264 the device's default FIFO depth.\r
265 On output, the value actually set.\r
266 @param Timeout The requested time out for a single character in microseconds.\r
267 This timeout applies to both the transmit and receive side of the\r
268 interface. A Timeout value of 0 will use the device's default time\r
269 out value.\r
270 On output, the value actually set.\r
271 @param Parity The type of parity to use on this serial device. A Parity value of\r
272 DefaultParity will use the device's default parity value.\r
273 On output, the value actually set.\r
274 @param DataBits The number of data bits to use on the serial device. A DataBits\r
275 vaule of 0 will use the device's default data bit setting.\r
276 On output, the value actually set.\r
277 @param StopBits The number of stop bits to use on this serial device. A StopBits\r
278 value of DefaultStopBits will use the device's default number of\r
279 stop bits.\r
280 On output, the value actually set.\r
281\r
282 @retval RETURN_SUCCESS The new attributes were set on the serial device.\r
283 @retval RETURN_UNSUPPORTED The serial device does not support this operation.\r
284 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.\r
285 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.\r
286\r
287**/\r
288RETURN_STATUS\r
289EFIAPI\r
290SerialPortSetAttributes (\r
291 IN OUT UINT64 *BaudRate,\r
292 IN OUT UINT32 *ReceiveFifoDepth,\r
293 IN OUT UINT32 *Timeout,\r
294 IN OUT EFI_PARITY_TYPE *Parity,\r
295 IN OUT UINT8 *DataBits,\r
296 IN OUT EFI_STOP_BITS_TYPE *StopBits\r
297 )\r
298{\r
299 return RETURN_UNSUPPORTED;\r
300}\r
301\r