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