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