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