]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/XenConsoleSerialPortLib/XenConsoleSerialPortLib.c
OvmfPkg/PlatformBootManagerLib: eliminate unchecked PcdSetXX() calls
[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 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
11
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.
14
15 **/
16
17 #include <Base.h>
18 #include <Uefi/UefiBaseType.h>
19
20 #include <Library/BaseLib.h>
21 #include <Library/SerialPortLib.h>
22 #include <Library/XenHypercallLib.h>
23
24 #include <IndustryStandard/Xen/io/console.h>
25 #include <IndustryStandard/Xen/hvm/params.h>
26 #include <IndustryStandard/Xen/event_channel.h>
27
28 //
29 // We can't use DebugLib due to a constructor dependency cycle between DebugLib
30 // and ourselves.
31 //
32 #define ASSERT(Expression) \
33 do { \
34 if (!(Expression)) { \
35 CpuDeadLoop (); \
36 } \
37 } while (FALSE)
38
39 //
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.
45 //
46 STATIC evtchn_send_t mXenConsoleEventChain;
47 STATIC struct xencons_interface *mXenConsoleInterface;
48
49 /**
50 Initialize the serial device hardware.
51
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.
55
56 @retval RETURN_SUCCESS The serial device was initialized.
57 @retval RETURN_DEVICE_ERROR The serial device could not be initialized.
58
59 **/
60 RETURN_STATUS
61 EFIAPI
62 SerialPortInitialize (
63 VOID
64 )
65 {
66 if (! XenHypercallIsAvailable ()) {
67 return RETURN_DEVICE_ERROR;
68 }
69
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);
74
75 //
76 // No point in ASSERT'ing here as we won't be seeing the output
77 //
78 }
79 return RETURN_SUCCESS;
80 }
81
82 /**
83 Write data from buffer to serial device.
84
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.
90
91 @param Buffer Pointer to the data buffer to be written.
92 @param NumberOfBytes Number of bytes to written to the serial device.
93
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.
97
98 **/
99 UINTN
100 EFIAPI
101 SerialPortWrite (
102 IN UINT8 *Buffer,
103 IN UINTN NumberOfBytes
104 )
105 {
106 XENCONS_RING_IDX Consumer, Producer;
107 UINTN Sent;
108
109 ASSERT (Buffer != NULL);
110
111 if (NumberOfBytes == 0) {
112 return 0;
113 }
114
115 if (!mXenConsoleInterface) {
116 return 0;
117 }
118
119 Sent = 0;
120 do {
121 Consumer = mXenConsoleInterface->out_cons;
122 Producer = mXenConsoleInterface->out_prod;
123
124 MemoryFence ();
125
126 while (Sent < NumberOfBytes && ((Producer - Consumer) < sizeof (mXenConsoleInterface->out)))
127 mXenConsoleInterface->out[MASK_XENCONS_IDX(Producer++, mXenConsoleInterface->out)] = Buffer[Sent++];
128
129 MemoryFence ();
130
131 mXenConsoleInterface->out_prod = Producer;
132
133 XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);
134
135 } while (Sent < NumberOfBytes);
136
137 return Sent;
138 }
139
140 /**
141 Read data from serial device and save the datas in buffer.
142
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.
147
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.
150
151 @retval 0 Read data failed, no data is to be read.
152 @retval >0 Actual number of bytes read from serial device.
153
154 **/
155 UINTN
156 EFIAPI
157 SerialPortRead (
158 OUT UINT8 *Buffer,
159 IN UINTN NumberOfBytes
160 )
161 {
162 XENCONS_RING_IDX Consumer, Producer;
163 UINTN Received;
164
165 ASSERT (Buffer != NULL);
166
167 if (NumberOfBytes == 0) {
168 return 0;
169 }
170
171 if (!mXenConsoleInterface) {
172 return 0;
173 }
174
175 Consumer = mXenConsoleInterface->in_cons;
176 Producer = mXenConsoleInterface->in_prod;
177
178 MemoryFence ();
179
180 Received = 0;
181 while (Received < NumberOfBytes && Consumer < Producer)
182 Buffer[Received++] = mXenConsoleInterface->in[MASK_XENCONS_IDX(Consumer++, mXenConsoleInterface->in)];
183
184 MemoryFence ();
185
186 mXenConsoleInterface->in_cons = Consumer;
187
188 XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);
189
190 return Received;
191 }
192
193 /**
194 Polls a serial device to see if there is any data waiting to be read.
195
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.
198
199 **/
200 BOOLEAN
201 EFIAPI
202 SerialPortPoll (
203 VOID
204 )
205 {
206 return mXenConsoleInterface &&
207 mXenConsoleInterface->in_cons != mXenConsoleInterface->in_prod;
208 }
209
210 /**
211 Sets the control bits on a serial device.
212
213 @param Control Sets the bits of Control that are settable.
214
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.
218
219 **/
220 RETURN_STATUS
221 EFIAPI
222 SerialPortSetControl (
223 IN UINT32 Control
224 )
225 {
226 return RETURN_UNSUPPORTED;
227 }
228
229 /**
230 Retrieve the status of the control bits on a serial device.
231
232 @param Control A pointer to return the current control signals from the serial device.
233
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.
237
238 **/
239 RETURN_STATUS
240 EFIAPI
241 SerialPortGetControl (
242 OUT UINT32 *Control
243 )
244 {
245 if (!mXenConsoleInterface) {
246 return RETURN_UNSUPPORTED;
247 }
248
249 *Control = 0;
250 if (!SerialPortPoll ()) {
251 *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY;
252 }
253 return RETURN_SUCCESS;
254 }
255
256 /**
257 Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
258 data bits, and stop bits on a serial device.
259
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
270 out value.
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
280 stop bits.
281 On output, the value actually set.
282
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.
287
288 **/
289 RETURN_STATUS
290 EFIAPI
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
298 )
299 {
300 return RETURN_UNSUPPORTED;
301 }
302