2 Xen console SerialPortLib instance
4 Copyright (c) 2015, Linaro Ltd. All rights reserved.<BR>
5 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/BaseLib.h>
14 #include <Library/SerialPortLib.h>
15 #include <Library/XenHypercallLib.h>
17 #include <IndustryStandard/Xen/io/console.h>
18 #include <IndustryStandard/Xen/hvm/params.h>
19 #include <IndustryStandard/Xen/event_channel.h>
22 // We can't use DebugLib due to a constructor dependency cycle between DebugLib
25 #define ASSERT(Expression) \
27 if (!(Expression)) { \
33 // The code below expects these global variables to be mutable, even in the case
34 // that we have been incorporated into SEC or PEIM phase modules (which is
35 // allowed by our INF description). While this is a dangerous assumption to make
36 // in general, it is actually fine for the Xen domU (guest) environment that
37 // this module is intended for, as UEFI always executes from DRAM in that case.
39 STATIC evtchn_send_t mXenConsoleEventChain
;
40 STATIC
struct xencons_interface
*mXenConsoleInterface
;
43 Initialize the serial device hardware.
45 If no initialization is required, then return RETURN_SUCCESS.
46 If the serial device was successfully initialized, then return RETURN_SUCCESS.
47 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
49 @retval RETURN_SUCCESS The serial device was initialized.
50 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
55 SerialPortInitialize (
59 if (! XenHypercallIsAvailable ()) {
60 return RETURN_DEVICE_ERROR
;
63 if (!mXenConsoleInterface
) {
64 mXenConsoleEventChain
.port
= (UINT32
)XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_EVTCHN
);
65 mXenConsoleInterface
= (struct xencons_interface
*)(UINTN
)
66 (XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_PFN
) << EFI_PAGE_SHIFT
);
69 // No point in ASSERT'ing here as we won't be seeing the output
72 return RETURN_SUCCESS
;
76 Write data from buffer to serial device.
78 Writes NumberOfBytes data bytes from Buffer to the serial device.
79 The number of bytes actually written to the serial device is returned.
80 If the return value is less than NumberOfBytes, then the write operation failed.
81 If Buffer is NULL, then ASSERT().
82 If NumberOfBytes is zero, then return 0.
84 @param Buffer Pointer to the data buffer to be written.
85 @param NumberOfBytes Number of bytes to written to the serial device.
87 @retval 0 NumberOfBytes is 0.
88 @retval >0 The number of bytes written to the serial device.
89 If this value is less than NumberOfBytes, then the write operation failed.
96 IN UINTN NumberOfBytes
99 XENCONS_RING_IDX Consumer
, Producer
;
102 ASSERT (Buffer
!= NULL
);
104 if (NumberOfBytes
== 0) {
108 if (!mXenConsoleInterface
) {
114 Consumer
= mXenConsoleInterface
->out_cons
;
115 Producer
= mXenConsoleInterface
->out_prod
;
119 while (Sent
< NumberOfBytes
&& ((Producer
- Consumer
) < sizeof (mXenConsoleInterface
->out
)))
120 mXenConsoleInterface
->out
[MASK_XENCONS_IDX(Producer
++, mXenConsoleInterface
->out
)] = Buffer
[Sent
++];
124 mXenConsoleInterface
->out_prod
= Producer
;
126 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
128 } while (Sent
< NumberOfBytes
);
134 Read data from serial device and save the datas in buffer.
136 Reads NumberOfBytes data bytes from a serial device into the buffer
137 specified by Buffer. The number of bytes actually read is returned.
138 If Buffer is NULL, then ASSERT().
139 If NumberOfBytes is zero, then return 0.
141 @param Buffer Pointer to the data buffer to store the data read from the serial device.
142 @param NumberOfBytes Number of bytes which will be read.
144 @retval 0 Read data failed, no data is to be read.
145 @retval >0 Actual number of bytes read from serial device.
152 IN UINTN NumberOfBytes
155 XENCONS_RING_IDX Consumer
, Producer
;
158 ASSERT (Buffer
!= NULL
);
160 if (NumberOfBytes
== 0) {
164 if (!mXenConsoleInterface
) {
168 Consumer
= mXenConsoleInterface
->in_cons
;
169 Producer
= mXenConsoleInterface
->in_prod
;
174 while (Received
< NumberOfBytes
&& Consumer
< Producer
)
175 Buffer
[Received
++] = mXenConsoleInterface
->in
[MASK_XENCONS_IDX(Consumer
++, mXenConsoleInterface
->in
)];
179 mXenConsoleInterface
->in_cons
= Consumer
;
181 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
187 Polls a serial device to see if there is any data waiting to be read.
189 @retval TRUE Data is waiting to be read from the serial device.
190 @retval FALSE There is no data waiting to be read from the serial device.
199 return mXenConsoleInterface
&&
200 mXenConsoleInterface
->in_cons
!= mXenConsoleInterface
->in_prod
;
204 Sets the control bits on a serial device.
206 @param Control Sets the bits of Control that are settable.
208 @retval RETURN_SUCCESS The new control bits were set on the serial device.
209 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
210 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
215 SerialPortSetControl (
219 return RETURN_UNSUPPORTED
;
223 Retrieve the status of the control bits on a serial device.
225 @param Control A pointer to return the current control signals from the serial device.
227 @retval RETURN_SUCCESS The control bits were read from the serial device.
228 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
229 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
234 SerialPortGetControl (
238 if (!mXenConsoleInterface
) {
239 return RETURN_UNSUPPORTED
;
243 if (!SerialPortPoll ()) {
244 *Control
= EFI_SERIAL_INPUT_BUFFER_EMPTY
;
246 return RETURN_SUCCESS
;
250 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
251 data bits, and stop bits on a serial device.
253 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
254 device's default interface speed.
255 On output, the value actually set.
256 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
257 serial interface. A ReceiveFifoDepth value of 0 will use
258 the device's default FIFO depth.
259 On output, the value actually set.
260 @param Timeout The requested time out for a single character in microseconds.
261 This timeout applies to both the transmit and receive side of the
262 interface. A Timeout value of 0 will use the device's default time
264 On output, the value actually set.
265 @param Parity The type of parity to use on this serial device. A Parity value of
266 DefaultParity will use the device's default parity value.
267 On output, the value actually set.
268 @param DataBits The number of data bits to use on the serial device. A DataBits
269 vaule of 0 will use the device's default data bit setting.
270 On output, the value actually set.
271 @param StopBits The number of stop bits to use on this serial device. A StopBits
272 value of DefaultStopBits will use the device's default number of
274 On output, the value actually set.
276 @retval RETURN_SUCCESS The new attributes were set on the serial device.
277 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
278 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
279 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
284 SerialPortSetAttributes (
285 IN OUT UINT64
*BaudRate
,
286 IN OUT UINT32
*ReceiveFifoDepth
,
287 IN OUT UINT32
*Timeout
,
288 IN OUT EFI_PARITY_TYPE
*Parity
,
289 IN OUT UINT8
*DataBits
,
290 IN OUT EFI_STOP_BITS_TYPE
*StopBits
293 return RETURN_UNSUPPORTED
;