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