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
SerialPortInitialize(VOID)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   return RETURN_SUCCESS;
73 }
74 
75 /**
76   Write data from buffer to serial device.
77 
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.
83 
84   @param  Buffer           Pointer to the data buffer to be written.
85   @param  NumberOfBytes    Number of bytes to written to the serial device.
86 
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.
90 
91 **/
92 UINTN
93 EFIAPI
SerialPortWrite(IN UINT8 * Buffer,IN UINTN NumberOfBytes)94 SerialPortWrite (
95   IN UINT8     *Buffer,
96   IN UINTN     NumberOfBytes
97   )
98 {
99   XENCONS_RING_IDX  Consumer, Producer;
100   UINTN             Sent;
101 
102   ASSERT (Buffer != NULL);
103 
104   if (NumberOfBytes == 0) {
105     return 0;
106   }
107 
108   if (!mXenConsoleInterface) {
109     return 0;
110   }
111 
112   Sent = 0;
113   do {
114     Consumer = mXenConsoleInterface->out_cons;
115     Producer = mXenConsoleInterface->out_prod;
116 
117     MemoryFence ();
118 
119     while (Sent < NumberOfBytes && ((Producer - Consumer) < sizeof (mXenConsoleInterface->out)))
120       mXenConsoleInterface->out[MASK_XENCONS_IDX(Producer++, mXenConsoleInterface->out)] = Buffer[Sent++];
121 
122     MemoryFence ();
123 
124     mXenConsoleInterface->out_prod = Producer;
125 
126     XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);
127 
128   } while (Sent < NumberOfBytes);
129 
130   return Sent;
131 }
132 
133 /**
134   Read data from serial device and save the datas in buffer.
135 
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.
140 
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.
143 
144   @retval 0                Read data failed, no data is to be read.
145   @retval >0               Actual number of bytes read from serial device.
146 
147 **/
148 UINTN
149 EFIAPI
SerialPortRead(OUT UINT8 * Buffer,IN UINTN NumberOfBytes)150 SerialPortRead (
151   OUT UINT8     *Buffer,
152   IN  UINTN     NumberOfBytes
153 )
154 {
155   XENCONS_RING_IDX  Consumer, Producer;
156   UINTN             Received;
157 
158   ASSERT (Buffer != NULL);
159 
160   if (NumberOfBytes == 0) {
161     return 0;
162   }
163 
164   if (!mXenConsoleInterface) {
165     return 0;
166   }
167 
168   Consumer = mXenConsoleInterface->in_cons;
169   Producer = mXenConsoleInterface->in_prod;
170 
171   MemoryFence ();
172 
173   Received = 0;
174   while (Received < NumberOfBytes && Consumer < Producer)
175      Buffer[Received++] = mXenConsoleInterface->in[MASK_XENCONS_IDX(Consumer++, mXenConsoleInterface->in)];
176 
177   MemoryFence ();
178 
179   mXenConsoleInterface->in_cons = Consumer;
180 
181   XenHypercallEventChannelOp (EVTCHNOP_send, &mXenConsoleEventChain);
182 
183   return Received;
184 }
185 
186 /**
187   Polls a serial device to see if there is any data waiting to be read.
188 
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.
191 
192 **/
193 BOOLEAN
194 EFIAPI
SerialPortPoll(VOID)195 SerialPortPoll (
196   VOID
197   )
198 {
199   return mXenConsoleInterface &&
200     mXenConsoleInterface->in_cons != mXenConsoleInterface->in_prod;
201 }
202 
203 /**
204   Sets the control bits on a serial device.
205 
206   @param Control                Sets the bits of Control that are settable.
207 
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.
211 
212 **/
213 RETURN_STATUS
214 EFIAPI
SerialPortSetControl(IN UINT32 Control)215 SerialPortSetControl (
216   IN UINT32 Control
217   )
218 {
219   return RETURN_UNSUPPORTED;
220 }
221 
222 /**
223   Retrieve the status of the control bits on a serial device.
224 
225   @param Control                A pointer to return the current control signals from the serial device.
226 
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.
230 
231 **/
232 RETURN_STATUS
233 EFIAPI
SerialPortGetControl(OUT UINT32 * Control)234 SerialPortGetControl (
235   OUT UINT32 *Control
236   )
237 {
238   if (!mXenConsoleInterface) {
239     return RETURN_UNSUPPORTED;
240   }
241 
242   *Control = 0;
243   if (!SerialPortPoll ()) {
244     *Control = EFI_SERIAL_INPUT_BUFFER_EMPTY;
245   }
246   return RETURN_SUCCESS;
247 }
248 
249 /**
250   Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
251   data bits, and stop bits on a serial device.
252 
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
263                             out value.
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
273                             stop bits.
274                             On output, the value actually set.
275 
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.
280 
281 **/
282 RETURN_STATUS
283 EFIAPI
SerialPortSetAttributes(IN OUT UINT64 * BaudRate,IN OUT UINT32 * ReceiveFifoDepth,IN OUT UINT32 * Timeout,IN OUT EFI_PARITY_TYPE * Parity,IN OUT UINT8 * DataBits,IN OUT EFI_STOP_BITS_TYPE * StopBits)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
291   )
292 {
293   return RETURN_UNSUPPORTED;
294 }
295 
296