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