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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Uefi/UefiBaseType.h>
20 #include <Library/BaseLib.h>
21 #include <Library/SerialPortLib.h>
22 #include <Library/XenHypercallLib.h>
24 #include <IndustryStandard/Xen/io/console.h>
25 #include <IndustryStandard/Xen/hvm/params.h>
26 #include <IndustryStandard/Xen/event_channel.h>
29 // We can't use DebugLib due to a constructor dependency cycle between DebugLib
32 #define ASSERT(Expression) \
34 if (!(Expression)) { \
40 // The code below expects these global variables to be mutable, even in the case
41 // that we have been incorporated into SEC or PEIM phase modules (which is
42 // allowed by our INF description). While this is a dangerous assumption to make
43 // in general, it is actually fine for the Xen domU (guest) environment that
44 // this module is intended for, as UEFI always executes from DRAM in that case.
46 STATIC evtchn_send_t mXenConsoleEventChain
;
47 STATIC
struct xencons_interface
*mXenConsoleInterface
;
50 Initialize the serial device hardware.
52 If no initialization is required, then return RETURN_SUCCESS.
53 If the serial device was successfully initialized, then return RETURN_SUCCESS.
54 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
56 @retval RETURN_SUCCESS The serial device was initialized.
57 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
62 SerialPortInitialize (
66 if (! XenHypercallIsAvailable ()) {
67 return RETURN_DEVICE_ERROR
;
70 if (!mXenConsoleInterface
) {
71 mXenConsoleEventChain
.port
= (UINT32
)XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_EVTCHN
);
72 mXenConsoleInterface
= (struct xencons_interface
*)(UINTN
)
73 (XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_PFN
) << EFI_PAGE_SHIFT
);
76 // No point in ASSERT'ing here as we won't be seeing the output
79 return RETURN_SUCCESS
;
83 Write data from buffer to serial device.
85 Writes NumberOfBytes data bytes from Buffer to the serial device.
86 The number of bytes actually written to the serial device is returned.
87 If the return value is less than NumberOfBytes, then the write operation failed.
88 If Buffer is NULL, then ASSERT().
89 If NumberOfBytes is zero, then return 0.
91 @param Buffer Pointer to the data buffer to be written.
92 @param NumberOfBytes Number of bytes to written to the serial device.
94 @retval 0 NumberOfBytes is 0.
95 @retval >0 The number of bytes written to the serial device.
96 If this value is less than NumberOfBytes, then the write operation failed.
103 IN UINTN NumberOfBytes
106 XENCONS_RING_IDX Consumer
, Producer
;
109 ASSERT (Buffer
!= NULL
);
111 if (NumberOfBytes
== 0) {
115 if (!mXenConsoleInterface
) {
121 Consumer
= mXenConsoleInterface
->out_cons
;
122 Producer
= mXenConsoleInterface
->out_prod
;
126 while (Sent
< NumberOfBytes
&& ((Producer
- Consumer
) < sizeof (mXenConsoleInterface
->out
)))
127 mXenConsoleInterface
->out
[MASK_XENCONS_IDX(Producer
++, mXenConsoleInterface
->out
)] = Buffer
[Sent
++];
131 mXenConsoleInterface
->out_prod
= Producer
;
133 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
135 } while (Sent
< NumberOfBytes
);
141 Read data from serial device and save the datas in buffer.
143 Reads NumberOfBytes data bytes from a serial device into the buffer
144 specified by Buffer. The number of bytes actually read is returned.
145 If Buffer is NULL, then ASSERT().
146 If NumberOfBytes is zero, then return 0.
148 @param Buffer Pointer to the data buffer to store the data read from the serial device.
149 @param NumberOfBytes Number of bytes which will be read.
151 @retval 0 Read data failed, no data is to be read.
152 @retval >0 Actual number of bytes read from serial device.
159 IN UINTN NumberOfBytes
162 XENCONS_RING_IDX Consumer
, Producer
;
165 ASSERT (Buffer
!= NULL
);
167 if (NumberOfBytes
== 0) {
171 if (!mXenConsoleInterface
) {
175 Consumer
= mXenConsoleInterface
->in_cons
;
176 Producer
= mXenConsoleInterface
->in_prod
;
181 while (Received
< NumberOfBytes
&& Consumer
< Producer
)
182 Buffer
[Received
++] = mXenConsoleInterface
->in
[MASK_XENCONS_IDX(Consumer
++, mXenConsoleInterface
->in
)];
186 mXenConsoleInterface
->in_cons
= Consumer
;
188 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
194 Polls a serial device to see if there is any data waiting to be read.
196 @retval TRUE Data is waiting to be read from the serial device.
197 @retval FALSE There is no data waiting to be read from the serial device.
206 return mXenConsoleInterface
&&
207 mXenConsoleInterface
->in_cons
!= mXenConsoleInterface
->in_prod
;
211 Sets the control bits on a serial device.
213 @param Control Sets the bits of Control that are settable.
215 @retval RETURN_SUCCESS The new control bits were set on the serial device.
216 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
217 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
222 SerialPortSetControl (
226 return RETURN_UNSUPPORTED
;
230 Retrieve the status of the control bits on a serial device.
232 @param Control A pointer to return the current control signals from the serial device.
234 @retval RETURN_SUCCESS The control bits were read from the serial device.
235 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
236 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
241 SerialPortGetControl (
245 if (!mXenConsoleInterface
) {
246 return RETURN_UNSUPPORTED
;
250 if (!SerialPortPoll ()) {
251 *Control
= EFI_SERIAL_INPUT_BUFFER_EMPTY
;
253 return RETURN_SUCCESS
;
257 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
258 data bits, and stop bits on a serial device.
260 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
261 device's default interface speed.
262 On output, the value actually set.
263 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
264 serial interface. A ReceiveFifoDepth value of 0 will use
265 the device's default FIFO depth.
266 On output, the value actually set.
267 @param Timeout The requested time out for a single character in microseconds.
268 This timeout applies to both the transmit and receive side of the
269 interface. A Timeout value of 0 will use the device's default time
271 On output, the value actually set.
272 @param Parity The type of parity to use on this serial device. A Parity value of
273 DefaultParity will use the device's default parity value.
274 On output, the value actually set.
275 @param DataBits The number of data bits to use on the serial device. A DataBits
276 vaule of 0 will use the device's default data bit setting.
277 On output, the value actually set.
278 @param StopBits The number of stop bits to use on this serial device. A StopBits
279 value of DefaultStopBits will use the device's default number of
281 On output, the value actually set.
283 @retval RETURN_SUCCESS The new attributes were set on the serial device.
284 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
285 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
286 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
291 SerialPortSetAttributes (
292 IN OUT UINT64
*BaudRate
,
293 IN OUT UINT32
*ReceiveFifoDepth
,
294 IN OUT UINT32
*Timeout
,
295 IN OUT EFI_PARITY_TYPE
*Parity
,
296 IN OUT UINT8
*DataBits
,
297 IN OUT EFI_STOP_BITS_TYPE
*StopBits
300 return RETURN_UNSUPPORTED
;