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.
19 #include <Library/BaseLib.h>
20 #include <Library/SerialPortLib.h>
21 #include <Library/XenHypercallLib.h>
23 #include <IndustryStandard/Xen/io/console.h>
24 #include <IndustryStandard/Xen/hvm/params.h>
25 #include <IndustryStandard/Xen/event_channel.h>
28 // We can't use DebugLib due to a constructor dependency cycle between DebugLib
31 #define ASSERT(Expression) \
33 if (!(Expression)) { \
39 // The code below expects these global variables to be mutable, even in the case
40 // that we have been incorporated into SEC or PEIM phase modules (which is
41 // allowed by our INF description). While this is a dangerous assumption to make
42 // in general, it is actually fine for the Xen domU (guest) environment that
43 // this module is intended for, as UEFI always executes from DRAM in that case.
45 STATIC evtchn_send_t mXenConsoleEventChain
;
46 STATIC
struct xencons_interface
*mXenConsoleInterface
;
49 Initialize the serial device hardware.
51 If no initialization is required, then return RETURN_SUCCESS.
52 If the serial device was successfully initialized, then return RETURN_SUCCESS.
53 If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
55 @retval RETURN_SUCCESS The serial device was initialized.
56 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
61 SerialPortInitialize (
65 if (! XenHypercallIsAvailable ()) {
66 return RETURN_DEVICE_ERROR
;
69 if (!mXenConsoleInterface
) {
70 mXenConsoleEventChain
.port
= (UINT32
)XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_EVTCHN
);
71 mXenConsoleInterface
= (struct xencons_interface
*)(UINTN
)
72 (XenHypercallHvmGetParam (HVM_PARAM_CONSOLE_PFN
) << EFI_PAGE_SHIFT
);
75 // No point in ASSERT'ing here as we won't be seeing the output
78 return RETURN_SUCCESS
;
82 Write data from buffer to serial device.
84 Writes NumberOfBytes data bytes from Buffer to the serial device.
85 The number of bytes actually written to the serial device is returned.
86 If the return value is less than NumberOfBytes, then the write operation failed.
87 If Buffer is NULL, then ASSERT().
88 If NumberOfBytes is zero, then return 0.
90 @param Buffer Pointer to the data buffer to be written.
91 @param NumberOfBytes Number of bytes to written to the serial device.
93 @retval 0 NumberOfBytes is 0.
94 @retval >0 The number of bytes written to the serial device.
95 If this value is less than NumberOfBytes, then the write operation failed.
102 IN UINTN NumberOfBytes
105 XENCONS_RING_IDX Consumer
, Producer
;
108 ASSERT (Buffer
!= NULL
);
110 if (NumberOfBytes
== 0) {
114 if (!mXenConsoleInterface
) {
120 Consumer
= mXenConsoleInterface
->out_cons
;
121 Producer
= mXenConsoleInterface
->out_prod
;
125 while (Sent
< NumberOfBytes
&& ((Producer
- Consumer
) < sizeof (mXenConsoleInterface
->out
)))
126 mXenConsoleInterface
->out
[MASK_XENCONS_IDX(Producer
++, mXenConsoleInterface
->out
)] = Buffer
[Sent
++];
130 mXenConsoleInterface
->out_prod
= Producer
;
132 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
134 } while (Sent
< NumberOfBytes
);
140 Read data from serial device and save the datas in buffer.
142 Reads NumberOfBytes data bytes from a serial device into the buffer
143 specified by Buffer. The number of bytes actually read is returned.
144 If Buffer is NULL, then ASSERT().
145 If NumberOfBytes is zero, then return 0.
147 @param Buffer Pointer to the data buffer to store the data read from the serial device.
148 @param NumberOfBytes Number of bytes which will be read.
150 @retval 0 Read data failed, no data is to be read.
151 @retval >0 Actual number of bytes read from serial device.
158 IN UINTN NumberOfBytes
161 XENCONS_RING_IDX Consumer
, Producer
;
164 ASSERT (Buffer
!= NULL
);
166 if (NumberOfBytes
== 0) {
170 if (!mXenConsoleInterface
) {
174 Consumer
= mXenConsoleInterface
->in_cons
;
175 Producer
= mXenConsoleInterface
->in_prod
;
180 while (Received
< NumberOfBytes
&& Consumer
< Producer
)
181 Buffer
[Received
++] = mXenConsoleInterface
->in
[MASK_XENCONS_IDX(Consumer
++, mXenConsoleInterface
->in
)];
185 mXenConsoleInterface
->in_cons
= Consumer
;
187 XenHypercallEventChannelOp (EVTCHNOP_send
, &mXenConsoleEventChain
);
193 Polls a serial device to see if there is any data waiting to be read.
195 @retval TRUE Data is waiting to be read from the serial device.
196 @retval FALSE There is no data waiting to be read from the serial device.
205 return mXenConsoleInterface
&&
206 mXenConsoleInterface
->in_cons
!= mXenConsoleInterface
->in_prod
;
210 Sets the control bits on a serial device.
212 @param Control Sets the bits of Control that are settable.
214 @retval RETURN_SUCCESS The new control bits were set on the serial device.
215 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
216 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
221 SerialPortSetControl (
225 return RETURN_UNSUPPORTED
;
229 Retrieve the status of the control bits on a serial device.
231 @param Control A pointer to return the current control signals from the serial device.
233 @retval RETURN_SUCCESS The control bits were read from the serial device.
234 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
235 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
240 SerialPortGetControl (
244 if (!mXenConsoleInterface
) {
245 return RETURN_UNSUPPORTED
;
249 if (!SerialPortPoll ()) {
250 *Control
= EFI_SERIAL_INPUT_BUFFER_EMPTY
;
252 return RETURN_SUCCESS
;
256 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
257 data bits, and stop bits on a serial device.
259 @param BaudRate The requested baud rate. A BaudRate value of 0 will use the
260 device's default interface speed.
261 On output, the value actually set.
262 @param ReveiveFifoDepth The requested depth of the FIFO on the receive side of the
263 serial interface. A ReceiveFifoDepth value of 0 will use
264 the device's default FIFO depth.
265 On output, the value actually set.
266 @param Timeout The requested time out for a single character in microseconds.
267 This timeout applies to both the transmit and receive side of the
268 interface. A Timeout value of 0 will use the device's default time
270 On output, the value actually set.
271 @param Parity The type of parity to use on this serial device. A Parity value of
272 DefaultParity will use the device's default parity value.
273 On output, the value actually set.
274 @param DataBits The number of data bits to use on the serial device. A DataBits
275 vaule of 0 will use the device's default data bit setting.
276 On output, the value actually set.
277 @param StopBits The number of stop bits to use on this serial device. A StopBits
278 value of DefaultStopBits will use the device's default number of
280 On output, the value actually set.
282 @retval RETURN_SUCCESS The new attributes were set on the serial device.
283 @retval RETURN_UNSUPPORTED The serial device does not support this operation.
284 @retval RETURN_INVALID_PARAMETER One or more of the attributes has an unsupported value.
285 @retval RETURN_DEVICE_ERROR The serial device is not functioning correctly.
290 SerialPortSetAttributes (
291 IN OUT UINT64
*BaudRate
,
292 IN OUT UINT32
*ReceiveFifoDepth
,
293 IN OUT UINT32
*Timeout
,
294 IN OUT EFI_PARITY_TYPE
*Parity
,
295 IN OUT UINT8
*DataBits
,
296 IN OUT EFI_STOP_BITS_TYPE
*StopBits
299 return RETURN_UNSUPPORTED
;