VirtualBox

source: vbox/trunk/src/VBox/Devices/Serial/UartCore.cpp@ 81894

Last change on this file since 81894 was 81894, checked in by vboxsync, 5 years ago

UartCore: doxygen fix. bugref:9218

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 73.9 KB
Line 
1/* $Id: UartCore.cpp 81894 2019-11-16 12:27:02Z vboxsync $ */
2/** @file
3 * UartCore - UART (16550A up to 16950) emulation.
4 *
5 * The documentation for this device was taken from the PC16550D spec from TI.
6 */
7
8/*
9 * Copyright (C) 2018-2019 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#define LOG_GROUP LOG_GROUP_DEV_SERIAL
25#include <VBox/vmm/tm.h>
26#include <iprt/log.h>
27#include <iprt/uuid.h>
28#include <iprt/assert.h>
29
30#include "VBoxDD.h"
31#include "UartCore.h"
32
33
34/*********************************************************************************************************************************
35* Defined Constants And Macros *
36*********************************************************************************************************************************/
37
38/** The RBR/DLL register index (from the base of the port range). */
39#define UART_REG_RBR_DLL_INDEX 0
40
41/** The THR/DLL register index (from the base of the port range). */
42#define UART_REG_THR_DLL_INDEX 0
43
44/** The IER/DLM register index (from the base of the port range). */
45#define UART_REG_IER_DLM_INDEX 1
46/** Enable received data available interrupt */
47# define UART_REG_IER_ERBFI RT_BIT(0)
48/** Enable transmitter holding register empty interrupt */
49# define UART_REG_IER_ETBEI RT_BIT(1)
50/** Enable receiver line status interrupt */
51# define UART_REG_IER_ELSI RT_BIT(2)
52/** Enable modem status interrupt. */
53# define UART_REG_IER_EDSSI RT_BIT(3)
54/** Sleep mode enable. */
55# define UART_REG_IER_SLEEP_MODE_EN RT_BIT(4)
56/** Low power mode enable. */
57# define UART_REG_IER_LP_MODE_EN RT_BIT(5)
58/** Mask of writeable bits. */
59# define UART_REG_IER_MASK_WR 0x0f
60/** Mask of writeable bits for 16750+. */
61# define UART_REG_IER_MASK_WR_16750 0x3f
62
63/** The IIR register index (from the base of the port range). */
64#define UART_REG_IIR_INDEX 2
65/** Interrupt Pending - high means no interrupt pending. */
66# define UART_REG_IIR_IP_NO_INT RT_BIT(0)
67/** Interrupt identification mask. */
68# define UART_REG_IIR_ID_MASK 0x0e
69/** Sets the interrupt identification to the given value. */
70# define UART_REG_IIR_ID_SET(a_Val) (((a_Val) << 1) & UART_REG_IIR_ID_MASK)
71/** Gets the interrupt identification from the given IIR register value. */
72# define UART_REG_IIR_ID_GET(a_Val) (((a_Val) & UART_REG_IIR_ID_MASK) >> 1)
73/** Receiver Line Status interrupt. */
74# define UART_REG_IIR_ID_RCL 0x3
75/** Received Data Available interrupt. */
76# define UART_REG_IIR_ID_RDA 0x2
77/** Character Timeou Indicator interrupt. */
78# define UART_REG_IIR_ID_CTI 0x6
79/** Transmitter Holding Register Empty interrupt. */
80# define UART_REG_IIR_ID_THRE 0x1
81/** Modem Status interrupt. */
82# define UART_REG_IIR_ID_MS 0x0
83/** 64 byte FIFOs enabled (15750+ only). */
84# define UART_REG_IIR_64BYTE_FIFOS_EN RT_BIT(5)
85/** FIFOs enabled. */
86# define UART_REG_IIR_FIFOS_EN 0xc0
87/** Bits relevant for checking whether the interrupt status has changed. */
88# define UART_REG_IIR_CHANGED_MASK 0x0f
89
90/** The FCR register index (from the base of the port range). */
91#define UART_REG_FCR_INDEX 2
92/** Enable the TX/RX FIFOs. */
93# define UART_REG_FCR_FIFO_EN RT_BIT(0)
94/** Reset the receive FIFO. */
95# define UART_REG_FCR_RCV_FIFO_RST RT_BIT(1)
96/** Reset the transmit FIFO. */
97# define UART_REG_FCR_XMIT_FIFO_RST RT_BIT(2)
98/** DMA Mode Select. */
99# define UART_REG_FCR_DMA_MODE_SEL RT_BIT(3)
100/** 64 Byte FIFO enable (15750+ only). */
101# define UART_REG_FCR_64BYTE_FIFO_EN RT_BIT(5)
102/** Receiver level interrupt trigger. */
103# define UART_REG_FCR_RCV_LVL_IRQ_MASK 0xc0
104/** Returns the receive level trigger value from the given FCR register. */
105# define UART_REG_FCR_RCV_LVL_IRQ_GET(a_Fcr) (((a_Fcr) & UART_REG_FCR_RCV_LVL_IRQ_MASK) >> 6)
106/** RCV Interrupt trigger level - 1 byte. */
107# define UART_REG_FCR_RCV_LVL_IRQ_1 0x0
108/** RCV Interrupt trigger level - 4 bytes. */
109# define UART_REG_FCR_RCV_LVL_IRQ_4 0x1
110/** RCV Interrupt trigger level - 8 bytes. */
111# define UART_REG_FCR_RCV_LVL_IRQ_8 0x2
112/** RCV Interrupt trigger level - 14 bytes. */
113# define UART_REG_FCR_RCV_LVL_IRQ_14 0x3
114/** Mask of writeable bits. */
115# define UART_REG_FCR_MASK_WR 0xcf
116/** Mask of sticky bits. */
117# define UART_REG_FCR_MASK_STICKY 0xe9
118
119/** The LCR register index (from the base of the port range). */
120#define UART_REG_LCR_INDEX 3
121/** Word Length Select Mask. */
122# define UART_REG_LCR_WLS_MASK 0x3
123/** Returns the WLS value form the given LCR register value. */
124# define UART_REG_LCR_WLS_GET(a_Lcr) ((a_Lcr) & UART_REG_LCR_WLS_MASK)
125/** Number of stop bits. */
126# define UART_REG_LCR_STB RT_BIT(2)
127/** Parity Enable. */
128# define UART_REG_LCR_PEN RT_BIT(3)
129/** Even Parity. */
130# define UART_REG_LCR_EPS RT_BIT(4)
131/** Stick parity. */
132# define UART_REG_LCR_PAR_STICK RT_BIT(5)
133/** Set Break. */
134# define UART_REG_LCR_BRK_SET RT_BIT(6)
135/** Divisor Latch Access Bit. */
136# define UART_REG_LCR_DLAB RT_BIT(7)
137
138/** The MCR register index (from the base of the port range). */
139#define UART_REG_MCR_INDEX 4
140/** Data Terminal Ready. */
141# define UART_REG_MCR_DTR RT_BIT(0)
142/** Request To Send. */
143# define UART_REG_MCR_RTS RT_BIT(1)
144/** Out1. */
145# define UART_REG_MCR_OUT1 RT_BIT(2)
146/** Out2. */
147# define UART_REG_MCR_OUT2 RT_BIT(3)
148/** Loopback connection. */
149# define UART_REG_MCR_LOOP RT_BIT(4)
150/** Flow Control Enable (15750+ only). */
151# define UART_REG_MCR_AFE RT_BIT(5)
152/** Mask of writeable bits (15450 and 15550A). */
153# define UART_REG_MCR_MASK_WR 0x1f
154/** Mask of writeable bits (15750+). */
155# define UART_REG_MCR_MASK_WR_15750 0x3f
156
157/** The LSR register index (from the base of the port range). */
158#define UART_REG_LSR_INDEX 5
159/** Data Ready. */
160# define UART_REG_LSR_DR RT_BIT(0)
161/** Overrun Error. */
162# define UART_REG_LSR_OE RT_BIT(1)
163/** Parity Error. */
164# define UART_REG_LSR_PE RT_BIT(2)
165/** Framing Error. */
166# define UART_REG_LSR_FE RT_BIT(3)
167/** Break Interrupt. */
168# define UART_REG_LSR_BI RT_BIT(4)
169/** Transmitter Holding Register. */
170# define UART_REG_LSR_THRE RT_BIT(5)
171/** Transmitter Empty. */
172# define UART_REG_LSR_TEMT RT_BIT(6)
173/** Error in receiver FIFO. */
174# define UART_REG_LSR_RCV_FIFO_ERR RT_BIT(7)
175/** The bits to check in this register when checking for the RCL interrupt. */
176# define UART_REG_LSR_BITS_IIR_RCL 0x1e
177
178/** The MSR register index (from the base of the port range). */
179#define UART_REG_MSR_INDEX 6
180/** Delta Clear to Send. */
181# define UART_REG_MSR_DCTS RT_BIT(0)
182/** Delta Data Set Ready. */
183# define UART_REG_MSR_DDSR RT_BIT(1)
184/** Trailing Edge Ring Indicator. */
185# define UART_REG_MSR_TERI RT_BIT(2)
186/** Delta Data Carrier Detect. */
187# define UART_REG_MSR_DDCD RT_BIT(3)
188/** Clear to Send. */
189# define UART_REG_MSR_CTS RT_BIT(4)
190/** Data Set Ready. */
191# define UART_REG_MSR_DSR RT_BIT(5)
192/** Ring Indicator. */
193# define UART_REG_MSR_RI RT_BIT(6)
194/** Data Carrier Detect. */
195# define UART_REG_MSR_DCD RT_BIT(7)
196/** The bits to check in this register when checking for the MS interrupt. */
197# define UART_REG_MSR_BITS_IIR_MS 0x0f
198
199/** The SCR register index (from the base of the port range). */
200#define UART_REG_SCR_INDEX 7
201
202/** Set the specified bits in the given register. */
203#define UART_REG_SET(a_Reg, a_Set) ((a_Reg) |= (a_Set))
204/** Clear the specified bits in the given register. */
205#define UART_REG_CLR(a_Reg, a_Clr) ((a_Reg) &= ~(a_Clr))
206
207
208/*********************************************************************************************************************************
209* Structures and Typedefs *
210*********************************************************************************************************************************/
211
212#ifndef VBOX_DEVICE_STRUCT_TESTCASE
213
214
215/*********************************************************************************************************************************
216* Global Variables *
217*********************************************************************************************************************************/
218
219#ifdef IN_RING3
220/**
221 * FIFO ITL levels.
222 */
223static struct
224{
225 /** ITL level for a 16byte FIFO. */
226 uint8_t cbItl16;
227 /** ITL level for a 64byte FIFO. */
228 uint8_t cbItl64;
229} s_aFifoItl[] =
230{
231 /* cbItl16 cbItl64 */
232 { 1, 1 },
233 { 4, 16 },
234 { 8, 32 },
235 { 14, 56 }
236};
237
238
239/**
240 * String versions of the parity enum.
241 */
242static const char *s_aszParity[] =
243{
244 "INVALID",
245 "NONE",
246 "EVEN",
247 "ODD",
248 "MARK",
249 "SPACE",
250 "INVALID"
251};
252
253
254/**
255 * String versions of the stop bits enum.
256 */
257static const char *s_aszStopBits[] =
258{
259 "INVALID",
260 "1",
261 "1.5",
262 "2",
263 "INVALID"
264};
265#endif
266
267
268/*********************************************************************************************************************************
269* Internal Functions *
270*********************************************************************************************************************************/
271
272
273/**
274 * Updates the IRQ state based on the current device state.
275 *
276 * @returns nothing.
277 * @param pDevIns The device instance.
278 * @param pThis The shared serial port instance data.
279 * @param pThisCC The serial port instance data for the current context.
280 */
281static void uartIrqUpdate(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC)
282{
283 LogFlowFunc(("pThis=%#p\n", pThis));
284
285 /*
286 * The interrupt uses a priority scheme, only the interrupt with the
287 * highest priority is indicated in the interrupt identification register.
288 *
289 * The priorities are as follows (high to low):
290 * * Receiver line status
291 * * Received data available
292 * * Character timeout indication (only in FIFO mode).
293 * * Transmitter holding register empty
294 * * Modem status change.
295 */
296 uint8_t uRegIirNew = UART_REG_IIR_IP_NO_INT;
297 if ( (pThis->uRegLsr & UART_REG_LSR_BITS_IIR_RCL)
298 && (pThis->uRegIer & UART_REG_IER_ELSI))
299 uRegIirNew = UART_REG_IIR_ID_SET(UART_REG_IIR_ID_RCL);
300 else if ( (pThis->uRegIer & UART_REG_IER_ERBFI)
301 && pThis->fIrqCtiPending)
302 uRegIirNew = UART_REG_IIR_ID_SET(UART_REG_IIR_ID_CTI);
303 else if ( (pThis->uRegLsr & UART_REG_LSR_DR)
304 && (pThis->uRegIer & UART_REG_IER_ERBFI)
305 && ( !(pThis->uRegFcr & UART_REG_FCR_FIFO_EN)
306 || pThis->FifoRecv.cbUsed >= pThis->FifoRecv.cbItl))
307 uRegIirNew = UART_REG_IIR_ID_SET(UART_REG_IIR_ID_RDA);
308 else if ( (pThis->uRegIer & UART_REG_IER_ETBEI)
309 && pThis->fThreEmptyPending)
310 uRegIirNew = UART_REG_IIR_ID_SET(UART_REG_IIR_ID_THRE);
311 else if ( (pThis->uRegMsr & UART_REG_MSR_BITS_IIR_MS)
312 && (pThis->uRegIer & UART_REG_IER_EDSSI))
313 uRegIirNew = UART_REG_IIR_ID_SET(UART_REG_IIR_ID_MS);
314
315 LogFlowFunc((" uRegIirNew=%#x uRegIir=%#x\n", uRegIirNew, pThis->uRegIir));
316
317 /* Change interrupt only if the interrupt status really changed from the previous value. */
318 if (uRegIirNew != (pThis->uRegIir & UART_REG_IIR_CHANGED_MASK))
319 {
320 LogFlow((" Interrupt source changed from %#x -> %#x (IRQ %d -> %d)\n",
321 pThis->uRegIir, uRegIirNew,
322 pThis->uRegIir == UART_REG_IIR_IP_NO_INT ? 0 : 1,
323 uRegIirNew == UART_REG_IIR_IP_NO_INT ? 0 : 1));
324 if (uRegIirNew == UART_REG_IIR_IP_NO_INT)
325 pThisCC->pfnUartIrqReq(pDevIns, pThis, pThis->iLUN, 0);
326 else
327 pThisCC->pfnUartIrqReq(pDevIns, pThis, pThis->iLUN, 1);
328 }
329 else
330 LogFlow((" No change in interrupt source\n"));
331
332 if (pThis->uRegFcr & UART_REG_FCR_FIFO_EN)
333 uRegIirNew |= UART_REG_IIR_FIFOS_EN;
334 if (pThis->uRegFcr & UART_REG_FCR_64BYTE_FIFO_EN)
335 uRegIirNew |= UART_REG_IIR_64BYTE_FIFOS_EN;
336
337 pThis->uRegIir = uRegIirNew;
338}
339
340
341/**
342 * Returns the amount of bytes stored in the given FIFO.
343 *
344 * @returns Amount of bytes stored in the FIFO.
345 * @param pFifo The FIFO.
346 */
347DECLINLINE(size_t) uartFifoUsedGet(PUARTFIFO pFifo)
348{
349 return pFifo->cbUsed;
350}
351
352
353/**
354 * Puts a new character into the given FIFO.
355 *
356 * @returns Flag whether the FIFO overflowed.
357 * @param pFifo The FIFO to put the data into.
358 * @param fOvrWr Flag whether to overwrite data if the FIFO is full.
359 * @param bData The data to add.
360 */
361DECLINLINE(bool) uartFifoPut(PUARTFIFO pFifo, bool fOvrWr, uint8_t bData)
362{
363 if (fOvrWr || pFifo->cbUsed < pFifo->cbMax)
364 {
365 pFifo->abBuf[pFifo->offWrite] = bData;
366 pFifo->offWrite = (pFifo->offWrite + 1) % pFifo->cbMax;
367 }
368
369 bool fOverFlow = false;
370 if (pFifo->cbUsed < pFifo->cbMax)
371 pFifo->cbUsed++;
372 else
373 {
374 fOverFlow = true;
375 if (fOvrWr) /* Advance the read position to account for the lost character. */
376 pFifo->offRead = (pFifo->offRead + 1) % pFifo->cbMax;
377 }
378
379 return fOverFlow;
380}
381
382
383/**
384 * Returns the next character in the FIFO.
385 *
386 * @return Next byte in the FIFO.
387 * @param pFifo The FIFO to get data from.
388 */
389DECLINLINE(uint8_t) uartFifoGet(PUARTFIFO pFifo)
390{
391 uint8_t bRet = 0;
392
393 if (pFifo->cbUsed)
394 {
395 bRet = pFifo->abBuf[pFifo->offRead];
396 pFifo->offRead = (pFifo->offRead + 1) % pFifo->cbMax;
397 pFifo->cbUsed--;
398 }
399
400 return bRet;
401}
402
403#ifdef IN_RING3
404
405/**
406 * Clears the given FIFO.
407 *
408 * @returns nothing.
409 * @param pFifo The FIFO to clear.
410 */
411DECLINLINE(void) uartFifoClear(PUARTFIFO pFifo)
412{
413 memset(&pFifo->abBuf[0], 0, sizeof(pFifo->abBuf));
414 pFifo->cbUsed = 0;
415 pFifo->offWrite = 0;
416 pFifo->offRead = 0;
417}
418
419
420/**
421 * Returns the amount of free bytes in the given FIFO.
422 *
423 * @returns The amount of bytes free in the given FIFO.
424 * @param pFifo The FIFO.
425 */
426DECLINLINE(size_t) uartFifoFreeGet(PUARTFIFO pFifo)
427{
428 return pFifo->cbMax - pFifo->cbUsed;
429}
430
431
432/**
433 * Tries to copy the requested amount of data from the given FIFO into the provided buffer.
434 *
435 * @returns Amount of bytes actually copied.
436 * @param pFifo The FIFO to copy data from.
437 * @param pvDst Where to copy the data to.
438 * @param cbCopy How much to copy.
439 */
440DECLINLINE(size_t) uartFifoCopyTo(PUARTFIFO pFifo, void *pvDst, size_t cbCopy)
441{
442 size_t cbCopied = 0;
443 uint8_t *pbDst = (uint8_t *)pvDst;
444
445 cbCopy = RT_MIN(cbCopy, pFifo->cbUsed);
446 while (cbCopy)
447 {
448 uint8_t cbThisCopy = (uint8_t)RT_MIN(cbCopy, (uint8_t)(pFifo->cbMax - pFifo->offRead));
449 memcpy(pbDst, &pFifo->abBuf[pFifo->offRead], cbThisCopy);
450
451 pFifo->offRead = (pFifo->offRead + cbThisCopy) % pFifo->cbMax;
452 pFifo->cbUsed -= cbThisCopy;
453 pbDst += cbThisCopy;
454 cbCopied += cbThisCopy;
455 cbCopy -= cbThisCopy;
456 }
457
458 return cbCopied;
459}
460
461
462#if 0 /* unused */
463/**
464 * Tries to copy the requested amount of data from the provided buffer into the given FIFO.
465 *
466 * @returns Amount of bytes actually copied.
467 * @param pFifo The FIFO to copy data to.
468 * @param pvSrc Where to copy the data from.
469 * @param cbCopy How much to copy.
470 */
471DECLINLINE(size_t) uartFifoCopyFrom(PUARTFIFO pFifo, void *pvSrc, size_t cbCopy)
472{
473 size_t cbCopied = 0;
474 uint8_t *pbSrc = (uint8_t *)pvSrc;
475
476 cbCopy = RT_MIN(cbCopy, uartFifoFreeGet(pFifo));
477 while (cbCopy)
478 {
479 uint8_t cbThisCopy = (uint8_t)RT_MIN(cbCopy, (uint8_t)(pFifo->cbMax - pFifo->offWrite));
480 memcpy(&pFifo->abBuf[pFifo->offWrite], pbSrc, cbThisCopy);
481
482 pFifo->offWrite = (pFifo->offWrite + cbThisCopy) % pFifo->cbMax;
483 pFifo->cbUsed += cbThisCopy;
484 pbSrc += cbThisCopy;
485 cbCopied += cbThisCopy;
486 cbCopy -= cbThisCopy;
487 }
488
489 return cbCopied;
490}
491#endif
492
493
494/**
495 * Updates the delta bits for the given MSR register value which has the status line
496 * bits set.
497 *
498 * @returns nothing.
499 * @param pDevIns The device instance.
500 * @param pThis The shared serial port instance data.
501 * @param pThisCC The serial port instance data for the current context.
502 * @param uMsrSts MSR value with the appropriate status bits set.
503 */
504static void uartR3MsrUpdate(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint8_t uMsrSts)
505{
506 /* Compare current and new states and set remaining bits accordingly. */
507 if ((uMsrSts & UART_REG_MSR_CTS) != (pThis->uRegMsr & UART_REG_MSR_CTS))
508 uMsrSts |= UART_REG_MSR_DCTS;
509 if ((uMsrSts & UART_REG_MSR_DSR) != (pThis->uRegMsr & UART_REG_MSR_DSR))
510 uMsrSts |= UART_REG_MSR_DDSR;
511 if ((uMsrSts & UART_REG_MSR_RI) != 0 && (pThis->uRegMsr & UART_REG_MSR_RI) == 0)
512 uMsrSts |= UART_REG_MSR_TERI;
513 if ((uMsrSts & UART_REG_MSR_DCD) != (pThis->uRegMsr & UART_REG_MSR_DCD))
514 uMsrSts |= UART_REG_MSR_DDCD;
515
516 pThis->uRegMsr = uMsrSts;
517
518 uartIrqUpdate(pDevIns, pThis, pThisCC);
519}
520
521
522/**
523 * Updates the serial port parameters of the attached driver with the current configuration.
524 *
525 * @returns nothing.
526 * @param pDevIns The device instance.
527 * @param pThis The shared serial port instance data.
528 * @param pThisCC The serial port instance data for the current context.
529 */
530static void uartR3ParamsUpdate(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC)
531{
532 if ( pThis->uRegDivisor != 0
533 && pThisCC->pDrvSerial)
534 {
535 uint32_t uBps = 115200 / pThis->uRegDivisor; /* This is for PC compatible serial port with a 1.8432 MHz crystal. */
536 unsigned cDataBits = UART_REG_LCR_WLS_GET(pThis->uRegLcr) + 5;
537 uint32_t cFrameBits = cDataBits;
538 PDMSERIALSTOPBITS enmStopBits = PDMSERIALSTOPBITS_ONE;
539 PDMSERIALPARITY enmParity = PDMSERIALPARITY_NONE;
540
541 if (pThis->uRegLcr & UART_REG_LCR_STB)
542 {
543 enmStopBits = cDataBits == 5 ? PDMSERIALSTOPBITS_ONEPOINTFIVE : PDMSERIALSTOPBITS_TWO;
544 cFrameBits += 2;
545 }
546 else
547 cFrameBits++;
548
549 if (pThis->uRegLcr & UART_REG_LCR_PEN)
550 {
551 /* Select the correct parity mode based on the even and stick parity bits. */
552 switch (pThis->uRegLcr & (UART_REG_LCR_EPS | UART_REG_LCR_PAR_STICK))
553 {
554 case 0:
555 enmParity = PDMSERIALPARITY_ODD;
556 break;
557 case UART_REG_LCR_EPS:
558 enmParity = PDMSERIALPARITY_EVEN;
559 break;
560 case UART_REG_LCR_EPS | UART_REG_LCR_PAR_STICK:
561 enmParity = PDMSERIALPARITY_SPACE;
562 break;
563 case UART_REG_LCR_PAR_STICK:
564 enmParity = PDMSERIALPARITY_MARK;
565 break;
566 default:
567 /* We should never get here as all cases where caught earlier. */
568 AssertMsgFailed(("This shouldn't happen at all: %#x\n",
569 pThis->uRegLcr & (UART_REG_LCR_EPS | UART_REG_LCR_PAR_STICK)));
570 }
571
572 cFrameBits++;
573 }
574
575 uint64_t uTimerFreq = PDMDevHlpTimerGetFreq(pDevIns, pThis->hTimerRcvFifoTimeout);
576 pThis->cSymbolXferTicks = (uTimerFreq / uBps) * cFrameBits;
577
578 LogFlowFunc(("Changing parameters to: %u,%s,%u,%s\n",
579 uBps, s_aszParity[enmParity], cDataBits, s_aszStopBits[enmStopBits]));
580
581 int rc = pThisCC->pDrvSerial->pfnChgParams(pThisCC->pDrvSerial, uBps, enmParity, cDataBits, enmStopBits);
582 if (RT_FAILURE(rc))
583 LogRelMax(10, ("Serial#%d: Failed to change parameters to %u,%s,%u,%s -> %Rrc\n",
584 pDevIns->iInstance, uBps, s_aszParity[enmParity], cDataBits, s_aszStopBits[enmStopBits], rc));
585
586 /* Changed parameters will flush all receive queues, so there won't be any data to read even if indicated. */
587 pThisCC->pDrvSerial->pfnQueuesFlush(pThisCC->pDrvSerial, true /*fQueueRecv*/, false /*fQueueXmit*/);
588 ASMAtomicWriteU32(&pThis->cbAvailRdr, 0);
589 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_DR);
590 }
591}
592
593
594/**
595 * Updates the internal device state with the given PDM status line states.
596 *
597 * @returns nothing.
598 * @param pDevIns The device instance.
599 * @param pThis The shared serial port instance data.
600 * @param pThisCC The serial port instance data for the current context.
601 * @param fStsLines The PDM status line states.
602 */
603static void uartR3StsLinesUpdate(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint32_t fStsLines)
604{
605 uint8_t uRegMsrNew = 0; /* The new MSR value. */
606
607 if (fStsLines & PDMISERIALPORT_STS_LINE_DCD)
608 uRegMsrNew |= UART_REG_MSR_DCD;
609 if (fStsLines & PDMISERIALPORT_STS_LINE_RI)
610 uRegMsrNew |= UART_REG_MSR_RI;
611 if (fStsLines & PDMISERIALPORT_STS_LINE_DSR)
612 uRegMsrNew |= UART_REG_MSR_DSR;
613 if (fStsLines & PDMISERIALPORT_STS_LINE_CTS)
614 uRegMsrNew |= UART_REG_MSR_CTS;
615
616 uartR3MsrUpdate(pDevIns, pThis, pThisCC, uRegMsrNew);
617}
618
619
620/**
621 * Fills up the receive FIFO with as much data as possible.
622 *
623 * @returns nothing.
624 * @param pDevIns The device instance.
625 * @param pThis The shared serial port instance data.
626 * @param pThisCC The serial port instance data for the current context.
627 */
628static void uartR3RecvFifoFill(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC)
629{
630 LogFlowFunc(("pThis=%#p\n", pThis));
631
632 PUARTFIFO pFifo = &pThis->FifoRecv;
633 size_t cbFill = RT_MIN(uartFifoFreeGet(pFifo),
634 ASMAtomicReadU32(&pThis->cbAvailRdr));
635 size_t cbFilled = 0;
636
637 while (cbFilled < cbFill)
638 {
639 size_t cbThisRead = cbFill - cbFilled;
640
641 if (pFifo->offRead <= pFifo->offWrite)
642 cbThisRead = RT_MIN(cbThisRead, (uint8_t)(pFifo->cbMax - pFifo->offWrite));
643 else
644 cbThisRead = RT_MIN(cbThisRead, (uint8_t)(pFifo->offRead - pFifo->offWrite));
645
646 size_t cbRead = 0;
647 int rc = pThisCC->pDrvSerial->pfnReadRdr(pThisCC->pDrvSerial, &pFifo->abBuf[pFifo->offWrite], cbThisRead, &cbRead);
648 AssertRC(rc); Assert(cbRead <= UINT8_MAX); RT_NOREF(rc);
649
650 pFifo->offWrite = (pFifo->offWrite + (uint8_t)cbRead) % pFifo->cbMax;
651 pFifo->cbUsed += (uint8_t)cbRead;
652 cbFilled += cbRead;
653
654 if (cbRead < cbThisRead)
655 break;
656 }
657
658 if (cbFilled)
659 {
660 UART_REG_SET(pThis->uRegLsr, UART_REG_LSR_DR);
661 if (pFifo->cbUsed < pFifo->cbItl)
662 {
663 pThis->fIrqCtiPending = false;
664 PDMDevHlpTimerSetRelative(pDevIns, pThis->hTimerRcvFifoTimeout, pThis->cSymbolXferTicks * 4, NULL);
665 }
666 uartIrqUpdate(pDevIns, pThis, pThisCC);
667 }
668
669 Assert(cbFilled <= (size_t)pThis->cbAvailRdr);
670 ASMAtomicSubU32(&pThis->cbAvailRdr, (uint32_t)cbFilled);
671}
672
673
674/**
675 * Fetches a single byte and writes it to RBR.
676 *
677 * @returns nothing.
678 * @param pDevIns The device instance.
679 * @param pThis The shared serial port instance data.
680 * @param pThisCC The serial port instance data for the current context.
681 */
682static void uartR3ByteFetch(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC)
683{
684 if (ASMAtomicReadU32(&pThis->cbAvailRdr))
685 {
686 size_t cbRead = 0;
687 int rc2 = pThisCC->pDrvSerial->pfnReadRdr(pThisCC->pDrvSerial, &pThis->uRegRbr, 1, &cbRead);
688 AssertMsg(RT_SUCCESS(rc2) && cbRead == 1, ("This shouldn't fail and always return one byte!\n")); RT_NOREF(rc2);
689 UART_REG_SET(pThis->uRegLsr, UART_REG_LSR_DR);
690 uartIrqUpdate(pDevIns, pThis, pThisCC);
691 }
692}
693
694
695/**
696 * Fetches a ready data based on the FIFO setting.
697 *
698 * @returns nothing.
699 * @param pDevIns The device instance.
700 * @param pThis The shared serial port instance data.
701 * @param pThisCC The serial port instance data for the current context.
702 */
703static void uartR3DataFetch(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC)
704{
705 AssertPtrReturnVoid(pThisCC->pDrvSerial);
706
707 if (pThis->uRegFcr & UART_REG_FCR_FIFO_EN)
708 uartR3RecvFifoFill(pDevIns, pThis, pThisCC);
709 else
710 uartR3ByteFetch(pDevIns, pThis, pThisCC);
711}
712
713
714/**
715 * Reset the transmit/receive related bits to the standard values
716 * (after a detach/attach/reset event).
717 *
718 * @returns nothing.
719 * @param pDevIns The device instance.
720 * @param pThis The shared serial port instance data.
721 * @param pThisCC The serial port instance data for the current context.
722 */
723static void uartR3XferReset(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC)
724{
725 PDMDevHlpTimerStop(pDevIns, pThis->hTimerRcvFifoTimeout);
726 PDMDevHlpTimerStop(pDevIns, pThis->hTimerTxUnconnected);
727 pThis->uRegLsr = UART_REG_LSR_THRE | UART_REG_LSR_TEMT;
728 pThis->fThreEmptyPending = false;
729
730 uartFifoClear(&pThis->FifoXmit);
731 uartFifoClear(&pThis->FifoRecv);
732 uartR3ParamsUpdate(pDevIns, pThis, pThisCC);
733 uartIrqUpdate(pDevIns, pThis, pThisCC);
734
735 if (pThisCC->pDrvSerial)
736 {
737 /* Set the modem lines to reflect the current state. */
738 int rc = pThisCC->pDrvSerial->pfnChgModemLines(pThisCC->pDrvSerial, false /*fRts*/, false /*fDtr*/);
739 if (RT_FAILURE(rc))
740 LogRel(("Serial#%d: Failed to set modem lines with %Rrc during reset\n",
741 pDevIns->iInstance, rc));
742
743 uint32_t fStsLines = 0;
744 rc = pThisCC->pDrvSerial->pfnQueryStsLines(pThisCC->pDrvSerial, &fStsLines);
745 if (RT_SUCCESS(rc))
746 uartR3StsLinesUpdate(pDevIns, pThis, pThisCC, fStsLines);
747 else
748 LogRel(("Serial#%d: Failed to query status line status with %Rrc during reset\n",
749 pDevIns->iInstance, rc));
750 }
751
752}
753
754
755/**
756 * Tries to copy the specified amount of data from the active TX queue (register or FIFO).
757 *
758 * @returns nothing.
759 * @param pDevIns The device instance.
760 * @param pThis The shared serial port instance data.
761 * @param pThisCC The serial port instance data for the current context.
762 * @param pvBuf Where to store the data.
763 * @param cbRead How much to read from the TX queue.
764 * @param pcbRead Where to store the amount of data read.
765 */
766static void uartR3TxQueueCopyFrom(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC,
767 void *pvBuf, size_t cbRead, size_t *pcbRead)
768{
769 if (pThis->uRegFcr & UART_REG_FCR_FIFO_EN)
770 {
771 *pcbRead = uartFifoCopyTo(&pThis->FifoXmit, pvBuf, cbRead);
772 if (!pThis->FifoXmit.cbUsed)
773 {
774 UART_REG_SET(pThis->uRegLsr, UART_REG_LSR_THRE);
775 pThis->fThreEmptyPending = true;
776 }
777 if (*pcbRead)
778 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_TEMT);
779 uartIrqUpdate(pDevIns, pThis, pThisCC);
780 }
781 else if (!(pThis->uRegLsr & UART_REG_LSR_THRE))
782 {
783 *(uint8_t *)pvBuf = pThis->uRegThr;
784 *pcbRead = 1;
785 UART_REG_SET(pThis->uRegLsr, UART_REG_LSR_THRE);
786 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_TEMT);
787 pThis->fThreEmptyPending = true;
788 uartIrqUpdate(pDevIns, pThis, pThisCC);
789 }
790 else
791 {
792 /*
793 * This can happen if there was data in the FIFO when the connection was closed,
794 * indicate this condition to the lower driver by returning 0 bytes.
795 */
796 *pcbRead = 0;
797 }
798}
799
800#endif /* IN_RING3 */
801
802
803/**
804 * Transmits the given byte.
805 *
806 * @returns Strict VBox status code.
807 * @param pDevIns The device instance.
808 * @param pThis The shared serial port instance data.
809 * @param pThisCC The serial port instance data for the current context.
810 * @param bVal Byte to transmit.
811 */
812static VBOXSTRICTRC uartXmit(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint8_t bVal)
813{
814 int rc = VINF_SUCCESS;
815
816 if (pThis->uRegFcr & UART_REG_FCR_FIFO_EN)
817 {
818#ifndef IN_RING3
819 RT_NOREF(pDevIns, pThisCC);
820 if (!uartFifoUsedGet(&pThis->FifoXmit))
821 rc = VINF_IOM_R3_IOPORT_WRITE;
822 else
823 {
824 uartFifoPut(&pThis->FifoXmit, true /*fOvrWr*/, bVal);
825 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_THRE | UART_REG_LSR_TEMT);
826 }
827#else
828 uartFifoPut(&pThis->FifoXmit, true /*fOvrWr*/, bVal);
829 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_THRE | UART_REG_LSR_TEMT);
830 pThis->fThreEmptyPending = false;
831 uartIrqUpdate(pDevIns, pThis, pThisCC);
832 if (uartFifoUsedGet(&pThis->FifoXmit) == 1)
833 {
834 if ( pThisCC->pDrvSerial
835 && !(pThis->uRegMcr & UART_REG_MCR_LOOP))
836 {
837 int rc2 = pThisCC->pDrvSerial->pfnDataAvailWrNotify(pThisCC->pDrvSerial);
838 if (RT_FAILURE(rc2))
839 LogRelMax(10, ("Serial#%d: Failed to send data with %Rrc\n", pDevIns->iInstance, rc2));
840 }
841 else
842 PDMDevHlpTimerSetRelative(pDevIns, pThis->hTimerTxUnconnected, pThis->cSymbolXferTicks, NULL);
843 }
844#endif
845 }
846 else
847 {
848 /* Notify the lower driver about available data only if the register was empty before. */
849 if (pThis->uRegLsr & UART_REG_LSR_THRE)
850 {
851#ifndef IN_RING3
852 rc = VINF_IOM_R3_IOPORT_WRITE;
853#else
854 pThis->uRegThr = bVal;
855 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_THRE | UART_REG_LSR_TEMT);
856 pThis->fThreEmptyPending = false;
857 uartIrqUpdate(pDevIns, pThis, pThisCC);
858 if ( pThisCC->pDrvSerial
859 && !(pThis->uRegMcr & UART_REG_MCR_LOOP))
860 {
861 int rc2 = pThisCC->pDrvSerial->pfnDataAvailWrNotify(pThisCC->pDrvSerial);
862 if (RT_FAILURE(rc2))
863 LogRelMax(10, ("Serial#%d: Failed to send data with %Rrc\n", pDevIns->iInstance, rc2));
864 }
865 else
866 PDMDevHlpTimerSetRelative(pDevIns, pThis->hTimerTxUnconnected, pThis->cSymbolXferTicks, NULL);
867#endif
868 }
869 else
870 pThis->uRegThr = bVal;
871 }
872
873 return rc;
874}
875
876
877/**
878 * Write handler for the THR/DLL register (depending on the DLAB bit in LCR).
879 *
880 * @returns Strict VBox status code.
881 * @param pDevIns The device instance.
882 * @param pThis The shared serial port instance data.
883 * @param pThisCC The serial port instance data for the current context.
884 * @param uVal The value to write.
885 */
886DECLINLINE(VBOXSTRICTRC) uartRegThrDllWrite(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint8_t uVal)
887{
888 VBOXSTRICTRC rc = VINF_SUCCESS;
889
890 /* A set DLAB causes a write to the lower 8bits of the divisor latch. */
891 if (pThis->uRegLcr & UART_REG_LCR_DLAB)
892 {
893 if (uVal != (pThis->uRegDivisor & 0xff))
894 {
895#ifndef IN_RING3
896 rc = VINF_IOM_R3_IOPORT_WRITE;
897#else
898 pThis->uRegDivisor = (pThis->uRegDivisor & 0xff00) | uVal;
899 uartR3ParamsUpdate(pDevIns, pThis, pThisCC);
900#endif
901 }
902 }
903 else
904 rc = uartXmit(pDevIns, pThis, pThisCC, uVal);
905
906 return rc;
907}
908
909
910/**
911 * Write handler for the IER/DLM register (depending on the DLAB bit in LCR).
912 *
913 * @returns Strict VBox status code.
914 * @param pDevIns The device instance.
915 * @param pThis The shared serial port instance data.
916 * @param pThisCC The serial port instance data for the current context.
917 * @param uVal The value to write.
918 */
919DECLINLINE(VBOXSTRICTRC) uartRegIerDlmWrite(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint8_t uVal)
920{
921 /* A set DLAB causes a write to the higher 8bits of the divisor latch. */
922 if (pThis->uRegLcr & UART_REG_LCR_DLAB)
923 {
924 if (uVal != (pThis->uRegDivisor & 0xff00) >> 8)
925 {
926#ifndef IN_RING3
927 return VINF_IOM_R3_IOPORT_WRITE;
928#else
929 pThis->uRegDivisor = (pThis->uRegDivisor & 0xff) | (uVal << 8);
930 uartR3ParamsUpdate(pDevIns, pThis, pThisCC);
931#endif
932 }
933 }
934 else
935 {
936 if (pThis->enmType < UARTTYPE_16750)
937 pThis->uRegIer = uVal & UART_REG_IER_MASK_WR;
938 else
939 pThis->uRegIer = uVal & UART_REG_IER_MASK_WR_16750;
940
941 if (pThis->uRegLsr & UART_REG_LSR_THRE)
942 pThis->fThreEmptyPending = true;
943
944 uartIrqUpdate(pDevIns, pThis, pThisCC);
945 }
946 return VINF_SUCCESS;
947}
948
949
950/**
951 * Write handler for the FCR register.
952 *
953 * @returns Strict VBox status code.
954 * @param pDevIns The device instance.
955 * @param pThis The shared serial port instance data.
956 * @param pThisCC The serial port instance data for the current context.
957 * @param uVal The value to write.
958 */
959DECLINLINE(VBOXSTRICTRC) uartRegFcrWrite(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint8_t uVal)
960{
961#ifndef IN_RING3
962 RT_NOREF(pDevIns, pThis, pThisCC, uVal);
963 return VINF_IOM_R3_IOPORT_WRITE;
964#else /* IN_RING3 */
965 if ( pThis->enmType >= UARTTYPE_16550A
966 && uVal != pThis->uRegFcr)
967 {
968 /* A change in the FIFO enable bit clears both FIFOs automatically. */
969 if ((uVal ^ pThis->uRegFcr) & UART_REG_FCR_FIFO_EN)
970 {
971 uartFifoClear(&pThis->FifoXmit);
972 uartFifoClear(&pThis->FifoRecv);
973
974 /*
975 * If the FIFO is about to be enabled and the DR bit is ready we have an unacknowledged
976 * byte in the RBR register which will be lost so we have to adjust the available bytes.
977 */
978 if ( ASMAtomicReadU32(&pThis->cbAvailRdr) > 0
979 && (uVal & UART_REG_FCR_FIFO_EN))
980 ASMAtomicDecU32(&pThis->cbAvailRdr);
981
982 /* Clear the DR bit too. */
983 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_DR);
984 }
985
986 /** @todo r=bird: Why was this here: if (rc == VINF_SUCCESS) */
987 {
988 if (uVal & UART_REG_FCR_RCV_FIFO_RST)
989 {
990 PDMDevHlpTimerStop(pDevIns, pThis->hTimerRcvFifoTimeout);
991 pThis->fIrqCtiPending = false;
992 uartFifoClear(&pThis->FifoRecv);
993 }
994 if (uVal & UART_REG_FCR_XMIT_FIFO_RST)
995 uartFifoClear(&pThis->FifoXmit);
996
997 /*
998 * The 64byte FIFO enable bit is only changeable for 16750
999 * and if the DLAB bit in LCR is set.
1000 */
1001 if ( pThis->enmType < UARTTYPE_16750
1002 || !(pThis->uRegLcr & UART_REG_LCR_DLAB))
1003 uVal &= ~UART_REG_FCR_64BYTE_FIFO_EN;
1004 else /* Use previous value. */
1005 uVal |= pThis->uRegFcr & UART_REG_FCR_64BYTE_FIFO_EN;
1006
1007 if (uVal & UART_REG_FCR_64BYTE_FIFO_EN)
1008 {
1009 pThis->FifoRecv.cbMax = 64;
1010 pThis->FifoXmit.cbMax = 64;
1011 }
1012 else
1013 {
1014 pThis->FifoRecv.cbMax = 16;
1015 pThis->FifoXmit.cbMax = 16;
1016 }
1017
1018 if (uVal & UART_REG_FCR_FIFO_EN)
1019 {
1020 uint8_t idxItl = UART_REG_FCR_RCV_LVL_IRQ_GET(uVal);
1021 if (uVal & UART_REG_FCR_64BYTE_FIFO_EN)
1022 pThis->FifoRecv.cbItl = s_aFifoItl[idxItl].cbItl64;
1023 else
1024 pThis->FifoRecv.cbItl = s_aFifoItl[idxItl].cbItl16;
1025 }
1026
1027 /* The FIFO reset bits are self clearing. */
1028 pThis->uRegFcr = uVal & UART_REG_FCR_MASK_STICKY;
1029 uartIrqUpdate(pDevIns, pThis, pThisCC);
1030 }
1031
1032 /* Fill in the next data. */
1033 if (ASMAtomicReadU32(&pThis->cbAvailRdr))
1034 uartR3DataFetch(pDevIns, pThis, pThisCC);
1035 }
1036
1037 return VINF_SUCCESS;
1038#endif /* IN_RING3 */
1039}
1040
1041
1042/**
1043 * Write handler for the LCR register.
1044 *
1045 * @returns Strict VBox status code.
1046 * @param pDevIns The device instance.
1047 * @param pThis The shared serial port instance data.
1048 * @param pThisCC The serial port instance data for the current context.
1049 * @param uVal The value to write.
1050 */
1051DECLINLINE(VBOXSTRICTRC) uartRegLcrWrite(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint8_t uVal)
1052{
1053 /* Any change except the DLAB bit causes a switch to R3. */
1054 if ((pThis->uRegLcr & ~UART_REG_LCR_DLAB) != (uVal & ~UART_REG_LCR_DLAB))
1055 {
1056#ifndef IN_RING3
1057 RT_NOREF(pThisCC, pDevIns);
1058 return VINF_IOM_R3_IOPORT_WRITE;
1059#else
1060 /* Check whether the BREAK bit changed before updating the LCR value. */
1061 bool fBrkEn = RT_BOOL(uVal & UART_REG_LCR_BRK_SET);
1062 bool fBrkChg = fBrkEn != RT_BOOL(pThis->uRegLcr & UART_REG_LCR_BRK_SET);
1063 pThis->uRegLcr = uVal;
1064 uartR3ParamsUpdate(pDevIns, pThis, pThisCC);
1065
1066 if ( fBrkChg
1067 && pThisCC->pDrvSerial)
1068 pThisCC->pDrvSerial->pfnChgBrk(pThisCC->pDrvSerial, fBrkEn);
1069#endif
1070 }
1071 else
1072 pThis->uRegLcr = uVal;
1073
1074 return VINF_SUCCESS;
1075}
1076
1077
1078/**
1079 * Write handler for the MCR register.
1080 *
1081 * @returns Strict VBox status code.
1082 * @param pDevIns The device instance.
1083 * @param pThis The shared serial port instance data.
1084 * @param pThisCC The serial port instance data for the current context.
1085 * @param uVal The value to write.
1086 */
1087DECLINLINE(VBOXSTRICTRC) uartRegMcrWrite(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint8_t uVal)
1088{
1089 if (pThis->enmType < UARTTYPE_16750)
1090 uVal &= UART_REG_MCR_MASK_WR;
1091 else
1092 uVal &= UART_REG_MCR_MASK_WR_15750;
1093 if (pThis->uRegMcr != uVal)
1094 {
1095#ifndef IN_RING3
1096 RT_NOREF(pThisCC, pDevIns);
1097 return VINF_IOM_R3_IOPORT_WRITE;
1098#else
1099 /*
1100 * When loopback mode is activated the RTS, DTR, OUT1 and OUT2 lines are
1101 * disconnected and looped back to MSR.
1102 */
1103 if ( (uVal & UART_REG_MCR_LOOP)
1104 && !(pThis->uRegMcr & UART_REG_MCR_LOOP)
1105 && pThisCC->pDrvSerial)
1106 pThisCC->pDrvSerial->pfnChgModemLines(pThisCC->pDrvSerial, false /*fRts*/, false /*fDtr*/);
1107
1108 pThis->uRegMcr = uVal;
1109 if (uVal & UART_REG_MCR_LOOP)
1110 {
1111 uint8_t uRegMsrSts = 0;
1112
1113 if (uVal & UART_REG_MCR_RTS)
1114 uRegMsrSts |= UART_REG_MSR_CTS;
1115 if (uVal & UART_REG_MCR_DTR)
1116 uRegMsrSts |= UART_REG_MSR_DSR;
1117 if (uVal & UART_REG_MCR_OUT1)
1118 uRegMsrSts |= UART_REG_MSR_RI;
1119 if (uVal & UART_REG_MCR_OUT2)
1120 uRegMsrSts |= UART_REG_MSR_DCD;
1121 uartR3MsrUpdate(pDevIns, pThis, pThisCC, uRegMsrSts);
1122 }
1123 else if (pThisCC->pDrvSerial)
1124 pThisCC->pDrvSerial->pfnChgModemLines(pThisCC->pDrvSerial,
1125 RT_BOOL(uVal & UART_REG_MCR_RTS),
1126 RT_BOOL(uVal & UART_REG_MCR_DTR));
1127#endif
1128 }
1129
1130 return VINF_SUCCESS;
1131}
1132
1133
1134/**
1135 * Read handler for the RBR/DLL register (depending on the DLAB bit in LCR).
1136 *
1137 * @returns VBox status code.
1138 * @param pDevIns The device instance.
1139 * @param pThis The shared serial port instance data.
1140 * @param pThisCC The serial port instance data for the current context.
1141 * @param puVal Where to store the read value on success.
1142 */
1143DECLINLINE(VBOXSTRICTRC) uartRegRbrDllRead(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint32_t *puVal)
1144{
1145 VBOXSTRICTRC rc = VINF_SUCCESS;
1146
1147 /* A set DLAB causes a read from the lower 8bits of the divisor latch. */
1148 if (pThis->uRegLcr & UART_REG_LCR_DLAB)
1149 *puVal = pThis->uRegDivisor & 0xff;
1150 else
1151 {
1152 if (pThis->uRegFcr & UART_REG_FCR_FIFO_EN)
1153 {
1154 /*
1155 * Only go back to R3 if there is new data available for the FIFO
1156 * and we would clear the interrupt to fill it up again.
1157 */
1158 if ( pThis->FifoRecv.cbUsed <= pThis->FifoRecv.cbItl
1159 && ASMAtomicReadU32(&pThis->cbAvailRdr) > 0)
1160 {
1161#ifndef IN_RING3
1162 rc = VINF_IOM_R3_IOPORT_READ;
1163#else
1164 uartR3RecvFifoFill(pDevIns, pThis, pThisCC);
1165#endif
1166 }
1167
1168 if (rc == VINF_SUCCESS)
1169 {
1170 *puVal = uartFifoGet(&pThis->FifoRecv);
1171 pThis->fIrqCtiPending = false;
1172 if (!pThis->FifoRecv.cbUsed)
1173 {
1174 PDMDevHlpTimerStop(pDevIns, pThis->hTimerRcvFifoTimeout);
1175 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_DR);
1176 }
1177 else if (pThis->FifoRecv.cbUsed < pThis->FifoRecv.cbItl)
1178 PDMDevHlpTimerSetRelative(pDevIns, pThis->hTimerRcvFifoTimeout,
1179 pThis->cSymbolXferTicks * 4, NULL);
1180 uartIrqUpdate(pDevIns, pThis, pThisCC);
1181 }
1182 }
1183 else
1184 {
1185 *puVal = pThis->uRegRbr;
1186
1187 if (pThis->uRegLsr & UART_REG_LSR_DR)
1188 {
1189 Assert(pThis->cbAvailRdr);
1190 uint32_t cbAvail = ASMAtomicDecU32(&pThis->cbAvailRdr);
1191 if (!cbAvail)
1192 {
1193 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_DR);
1194 uartIrqUpdate(pDevIns, pThis, pThisCC);
1195 }
1196 else
1197 {
1198#ifndef IN_RING3
1199 /* Restore state and go back to R3. */
1200 ASMAtomicIncU32(&pThis->cbAvailRdr);
1201 rc = VINF_IOM_R3_IOPORT_READ;
1202#else
1203 /* Fetch new data and keep the DR bit set. */
1204 uartR3DataFetch(pDevIns, pThis, pThisCC);
1205#endif
1206 }
1207 }
1208 }
1209 }
1210
1211 return rc;
1212}
1213
1214
1215/**
1216 * Read handler for the IER/DLM register (depending on the DLAB bit in LCR).
1217 *
1218 * @param pThis The shared serial port instance data.
1219 * @param puVal Where to store the read value on success.
1220 */
1221DECLINLINE(void) uartRegIerDlmRead(PUARTCORE pThis, uint32_t *puVal)
1222{
1223 /* A set DLAB causes a read from the upper 8bits of the divisor latch. */
1224 if (pThis->uRegLcr & UART_REG_LCR_DLAB)
1225 *puVal = (pThis->uRegDivisor & 0xff00) >> 8;
1226 else
1227 *puVal = pThis->uRegIer;
1228}
1229
1230
1231/**
1232 * Read handler for the IIR register.
1233 *
1234 * @param pDevIns The device instance.
1235 * @param pThis The shared serial port instance data.
1236 * @param pThisCC The serial port instance data for the current context.
1237 * @param puVal Where to store the read value on success.
1238 */
1239DECLINLINE(void) uartRegIirRead(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint32_t *puVal)
1240{
1241 *puVal = pThis->uRegIir;
1242 /* Reset the THRE empty interrupt id when this gets returned to the guest (see table 3 UART Reset configuration). */
1243 if (UART_REG_IIR_ID_GET(pThis->uRegIir) == UART_REG_IIR_ID_THRE)
1244 {
1245 pThis->fThreEmptyPending = false;
1246 uartIrqUpdate(pDevIns, pThis, pThisCC);
1247 }
1248}
1249
1250
1251/**
1252 * Read handler for the LSR register.
1253 *
1254 * @returns Strict VBox status code.
1255 * @param pDevIns The device instance.
1256 * @param pThis The shared serial port instance data.
1257 * @param pThisCC The serial port instance data for the current context.
1258 * @param puVal Where to store the read value on success.
1259 */
1260DECLINLINE(VBOXSTRICTRC) uartRegLsrRead(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint32_t *puVal)
1261{
1262 /* Yield if configured and there is no data available. */
1263 if ( !(pThis->uRegLsr & UART_REG_LSR_DR)
1264 && (pThis->fFlags & UART_CORE_YIELD_ON_LSR_READ))
1265 {
1266#ifndef IN_RING3
1267 return VINF_IOM_R3_IOPORT_READ;
1268#else
1269 RTThreadYield();
1270#endif
1271 }
1272
1273 *puVal = pThis->uRegLsr;
1274 /*
1275 * Reading this register clears the Overrun (OE), Parity (PE) and Framing (FE) error
1276 * as well as the Break Interrupt (BI).
1277 */
1278 UART_REG_CLR(pThis->uRegLsr, UART_REG_LSR_BITS_IIR_RCL);
1279 uartIrqUpdate(pDevIns, pThis, pThisCC);
1280
1281 return VINF_SUCCESS;
1282}
1283
1284
1285/**
1286 * Read handler for the MSR register.
1287 *
1288 * @param pDevIns The device instance.
1289 * @param pThis The shared serial port instance data.
1290 * @param pThisCC The serial port instance data for the current context.
1291 * @param puVal Where to store the read value on success.
1292 */
1293DECLINLINE(void) uartRegMsrRead(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, uint32_t *puVal)
1294{
1295 *puVal = pThis->uRegMsr;
1296
1297 /* Clear any of the delta bits. */
1298 UART_REG_CLR(pThis->uRegMsr, UART_REG_MSR_BITS_IIR_MS);
1299 uartIrqUpdate(pDevIns, pThis, pThisCC);
1300}
1301
1302
1303#ifdef LOG_ENABLED
1304/**
1305 * Converts the register index into a sensible memnonic.
1306 *
1307 * @returns Register memnonic.
1308 * @param pThis The shared serial port instance data.
1309 * @param idxReg Register index.
1310 * @param fWrite Flag whether the register gets written.
1311 */
1312DECLINLINE(const char *) uartRegIdx2Str(PUARTCORE pThis, uint8_t idxReg, bool fWrite)
1313{
1314 const char *psz = "INV";
1315
1316 switch (idxReg)
1317 {
1318 /*case UART_REG_THR_DLL_INDEX:*/
1319 case UART_REG_RBR_DLL_INDEX:
1320 if (pThis->uRegLcr & UART_REG_LCR_DLAB)
1321 psz = "DLL";
1322 else if (fWrite)
1323 psz = "THR";
1324 else
1325 psz = "RBR";
1326 break;
1327 case UART_REG_IER_DLM_INDEX:
1328 if (pThis->uRegLcr & UART_REG_LCR_DLAB)
1329 psz = "DLM";
1330 else
1331 psz = "IER";
1332 break;
1333 /*case UART_REG_IIR_INDEX:*/
1334 case UART_REG_FCR_INDEX:
1335 if (fWrite)
1336 psz = "FCR";
1337 else
1338 psz = "IIR";
1339 break;
1340 case UART_REG_LCR_INDEX:
1341 psz = "LCR";
1342 break;
1343 case UART_REG_MCR_INDEX:
1344 psz = "MCR";
1345 break;
1346 case UART_REG_LSR_INDEX:
1347 psz = "LSR";
1348 break;
1349 case UART_REG_MSR_INDEX:
1350 psz = "MSR";
1351 break;
1352 case UART_REG_SCR_INDEX:
1353 psz = "SCR";
1354 break;
1355 }
1356
1357 return psz;
1358}
1359#endif
1360
1361
1362/**
1363 * Performs a register write to the given register offset.
1364 *
1365 * @returns Strict VBox status code.
1366 * @param pDevIns The device instance.
1367 * @param pThis The shared UART core instance data.
1368 * @param pThisCC The current context UART core instance data.
1369 * @param uReg The register offset (byte offset) to start writing to.
1370 * @param u32 The value to write.
1371 * @param cb Number of bytes to write.
1372 */
1373DECLHIDDEN(VBOXSTRICTRC) uartRegWrite(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC,
1374 uint32_t uReg, uint32_t u32, size_t cb)
1375{
1376 AssertMsgReturn(cb == 1, ("uReg=%#x cb=%d u32=%#x\n", uReg, cb, u32), VINF_SUCCESS);
1377
1378 VBOXSTRICTRC rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VINF_IOM_R3_IOPORT_WRITE);
1379 if (rc == VINF_SUCCESS)
1380 {
1381 uint8_t idxReg = uReg & 0x7;
1382 LogFlowFunc(("pThis=%#p uReg=%u{%s} u32=%#x cb=%u\n",
1383 pThis, uReg, uartRegIdx2Str(pThis, idxReg, true /*fWrite*/), u32, cb));
1384
1385 uint8_t uVal = (uint8_t)u32;
1386 switch (idxReg)
1387 {
1388 case UART_REG_THR_DLL_INDEX:
1389 rc = uartRegThrDllWrite(pDevIns, pThis, pThisCC, uVal);
1390 break;
1391 case UART_REG_IER_DLM_INDEX:
1392 rc = uartRegIerDlmWrite(pDevIns, pThis, pThisCC, uVal);
1393 break;
1394 case UART_REG_FCR_INDEX:
1395 rc = uartRegFcrWrite(pDevIns, pThis, pThisCC, uVal);
1396 break;
1397 case UART_REG_LCR_INDEX:
1398 rc = uartRegLcrWrite(pDevIns, pThis, pThisCC, uVal);
1399 break;
1400 case UART_REG_MCR_INDEX:
1401 rc = uartRegMcrWrite(pDevIns, pThis, pThisCC, uVal);
1402 break;
1403 case UART_REG_SCR_INDEX:
1404 pThis->uRegScr = u32;
1405 break;
1406 default:
1407 break;
1408 }
1409
1410 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1411 }
1412 LogFlowFunc(("-> %Rrc\n", VBOXSTRICTRC_VAL(rc)));
1413 return rc;
1414}
1415
1416
1417/**
1418 * Performs a register read from the given register offset.
1419 *
1420 * @returns VBox status code.
1421 * @param pDevIns The device instance.
1422 * @param pThis The shared UART core instance data.
1423 * @param pThisCC The current context UART core instance data.
1424 * @param uReg The register offset (byte offset) to start reading from.
1425 * @param pu32 Where to store the read value.
1426 * @param cb Number of bytes to read.
1427 */
1428DECLHIDDEN(VBOXSTRICTRC) uartRegRead(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC,
1429 uint32_t uReg, uint32_t *pu32, size_t cb)
1430{
1431 if (cb != 1)
1432 return VERR_IOM_IOPORT_UNUSED;
1433
1434 VBOXSTRICTRC rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VINF_IOM_R3_IOPORT_READ);
1435 if (rc == VINF_SUCCESS)
1436 {
1437 uint8_t idxReg = uReg & 0x7;
1438 switch (idxReg)
1439 {
1440 case UART_REG_RBR_DLL_INDEX:
1441 rc = uartRegRbrDllRead(pDevIns, pThis, pThisCC, pu32);
1442 break;
1443 case UART_REG_IER_DLM_INDEX:
1444 uartRegIerDlmRead(pThis, pu32);
1445 break;
1446 case UART_REG_IIR_INDEX:
1447 uartRegIirRead(pDevIns, pThis, pThisCC, pu32);
1448 break;
1449 case UART_REG_LCR_INDEX:
1450 *pu32 = pThis->uRegLcr;
1451 break;
1452 case UART_REG_MCR_INDEX:
1453 *pu32 = pThis->uRegMcr;
1454 break;
1455 case UART_REG_LSR_INDEX:
1456 rc = uartRegLsrRead(pDevIns, pThis, pThisCC, pu32);
1457 break;
1458 case UART_REG_MSR_INDEX:
1459 uartRegMsrRead(pDevIns, pThis, pThisCC, pu32);
1460 break;
1461 case UART_REG_SCR_INDEX:
1462 *pu32 = pThis->uRegScr;
1463 break;
1464 default:
1465 rc = VERR_IOM_IOPORT_UNUSED;
1466 }
1467 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1468 LogFlowFunc(("pThis=%#p uReg=%u{%s} u32=%#x cb=%u -> %Rrc\n",
1469 pThis, uReg, uartRegIdx2Str(pThis, idxReg, false /*fWrite*/), *pu32, cb, VBOXSTRICTRC_VAL(rc)));
1470 }
1471 else
1472 LogFlowFunc(("-> %Rrc\n", VBOXSTRICTRC_VAL(rc)));
1473 return rc;
1474}
1475
1476
1477#ifdef IN_RING3
1478
1479/* -=-=-=-=-=-=-=-=- Timer callbacks -=-=-=-=-=-=-=-=- */
1480
1481/**
1482 * @callback_method_impl{FNTMTIMERDEV, Fifo timer function.}
1483 */
1484static DECLCALLBACK(void) uartR3RcvFifoTimeoutTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
1485{
1486 LogFlowFunc(("pDevIns=%#p pTimer=%#p pvUser=%#p\n", pDevIns, pTimer, pvUser));
1487 PUARTCORER3 pThisCC = (PUARTCORECC)pvUser;
1488 PUARTCORE pThis = pThisCC->pShared;
1489 RT_NOREF(pTimer);
1490
1491 PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED); /** @todo r=bird: You already own this crit sect, don't you?!? */
1492
1493 if (pThis->FifoRecv.cbUsed < pThis->FifoRecv.cbItl)
1494 {
1495 pThis->fIrqCtiPending = true;
1496 uartIrqUpdate(pDevIns, pThis, pThisCC);
1497 }
1498
1499 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1500}
1501
1502/**
1503 * @callback_method_impl{FNTMTIMERDEV, TX timer function when there is no driver connected for draining the THR/FIFO.}
1504 */
1505static DECLCALLBACK(void) uartR3TxUnconnectedTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
1506{
1507 LogFlowFunc(("pDevIns=%#p pTimer=%#p pvUser=%#p\n", pDevIns, pTimer, pvUser));
1508 PUARTCORER3 pThisCC = (PUARTCORECC)pvUser;
1509 PUARTCORE pThis = pThisCC->pShared;
1510 RT_NOREF(pTimer);
1511
1512 PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_IGNORED); /** @todo r=bird: You already own this crit sect, don't you?!? */
1513
1514 uint8_t bVal = 0;
1515 size_t cbRead = 0;
1516 uartR3TxQueueCopyFrom(pDevIns, pThis, pThisCC, &bVal, sizeof(bVal), &cbRead);
1517 if (pThis->uRegMcr & UART_REG_MCR_LOOP)
1518 {
1519 /* Loopback mode is active, feed in the data at the receiving end. */
1520 uint32_t cbAvailOld = ASMAtomicAddU32(&pThis->cbAvailRdr, 1);
1521 if (pThis->uRegFcr & UART_REG_FCR_FIFO_EN)
1522 {
1523 PUARTFIFO pFifo = &pThis->FifoRecv;
1524 if (uartFifoFreeGet(pFifo) > 0)
1525 {
1526 pFifo->abBuf[pFifo->offWrite] = bVal;
1527 pFifo->offWrite = (pFifo->offWrite + 1) % pFifo->cbMax;
1528 pFifo->cbUsed++;
1529
1530 UART_REG_SET(pThis->uRegLsr, UART_REG_LSR_DR);
1531 if (pFifo->cbUsed < pFifo->cbItl)
1532 {
1533 pThis->fIrqCtiPending = false;
1534 PDMDevHlpTimerSetRelative(pDevIns, pThis->hTimerRcvFifoTimeout,
1535 pThis->cSymbolXferTicks * 4, NULL);
1536 }
1537 uartIrqUpdate(pDevIns, pThis, pThisCC);
1538 }
1539
1540 ASMAtomicSubU32(&pThis->cbAvailRdr, 1);
1541 }
1542 else if (!cbAvailOld)
1543 {
1544 pThis->uRegRbr = bVal;
1545 UART_REG_SET(pThis->uRegLsr, UART_REG_LSR_DR);
1546 uartIrqUpdate(pDevIns, pThis, pThisCC);
1547 }
1548 else
1549 ASMAtomicSubU32(&pThis->cbAvailRdr, 1);
1550 }
1551 if (cbRead == 1)
1552 PDMDevHlpTimerSetRelative(pDevIns, pThis->hTimerTxUnconnected, pThis->cSymbolXferTicks, NULL);
1553
1554 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1555}
1556
1557
1558/* -=-=-=-=-=-=-=-=- PDMISERIALPORT on LUN#0 -=-=-=-=-=-=-=-=- */
1559
1560
1561/**
1562 * @interface_method_impl{PDMISERIALPORT,pfnDataAvailRdrNotify}
1563 */
1564static DECLCALLBACK(int) uartR3DataAvailRdrNotify(PPDMISERIALPORT pInterface, size_t cbAvail)
1565{
1566 LogFlowFunc(("pInterface=%#p cbAvail=%zu\n", pInterface, cbAvail));
1567 PUARTCORECC pThisCC = RT_FROM_MEMBER(pInterface, UARTCORECC, ISerialPort);
1568 PUARTCORE pThis = pThisCC->pShared;
1569 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1570
1571 AssertMsg((uint32_t)cbAvail == cbAvail, ("Too much data available\n"));
1572
1573 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
1574 uint32_t cbAvailOld = ASMAtomicAddU32(&pThis->cbAvailRdr, (uint32_t)cbAvail);
1575 LogFlow((" cbAvailRdr=%u -> cbAvailRdr=%u\n", cbAvailOld, cbAvail + cbAvailOld));
1576 if (pThis->uRegFcr & UART_REG_FCR_FIFO_EN)
1577 uartR3RecvFifoFill(pDevIns, pThis, pThisCC);
1578 else if (!cbAvailOld)
1579 {
1580 size_t cbRead = 0;
1581 int rc = pThisCC->pDrvSerial->pfnReadRdr(pThisCC->pDrvSerial, &pThis->uRegRbr, 1, &cbRead);
1582 AssertMsg(RT_SUCCESS(rc) && cbRead == 1, ("This shouldn't fail and always return one byte!\n")); RT_NOREF(rc);
1583 UART_REG_SET(pThis->uRegLsr, UART_REG_LSR_DR);
1584 uartIrqUpdate(pDevIns, pThis, pThisCC);
1585 }
1586 PDMCritSectLeave(&pThis->CritSect);
1587
1588 return VINF_SUCCESS;
1589}
1590
1591
1592/**
1593 * @interface_method_impl{PDMISERIALPORT,pfnDataSentNotify}
1594 */
1595static DECLCALLBACK(int) uartR3DataSentNotify(PPDMISERIALPORT pInterface)
1596{
1597 LogFlowFunc(("pInterface=%#p\n", pInterface));
1598 PUARTCORECC pThisCC = RT_FROM_MEMBER(pInterface, UARTCORECC, ISerialPort);
1599 PUARTCORE pThis = pThisCC->pShared;
1600 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1601
1602 /* Set the transmitter empty bit because everything was sent. */
1603 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
1604 UART_REG_SET(pThis->uRegLsr, UART_REG_LSR_TEMT);
1605 uartIrqUpdate(pDevIns, pThis, pThisCC);
1606 PDMCritSectLeave(&pThis->CritSect);
1607 return VINF_SUCCESS;
1608}
1609
1610
1611/**
1612 * @interface_method_impl{PDMISERIALPORT,pfnReadWr}
1613 */
1614static DECLCALLBACK(int) uartR3ReadWr(PPDMISERIALPORT pInterface, void *pvBuf, size_t cbRead, size_t *pcbRead)
1615{
1616 LogFlowFunc(("pInterface=%#p pvBuf=%#p cbRead=%zu pcbRead=%#p\n", pInterface, pvBuf, cbRead, pcbRead));
1617 PUARTCORECC pThisCC = RT_FROM_MEMBER(pInterface, UARTCORECC, ISerialPort);
1618 PUARTCORE pThis = pThisCC->pShared;
1619 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1620
1621 AssertReturn(cbRead > 0, VERR_INVALID_PARAMETER);
1622
1623 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
1624 uartR3TxQueueCopyFrom(pDevIns, pThis, pThisCC, pvBuf, cbRead, pcbRead);
1625 PDMCritSectLeave(&pThis->CritSect);
1626
1627 LogFlowFunc(("-> VINF_SUCCESS{*pcbRead=%zu}\n", *pcbRead));
1628 return VINF_SUCCESS;
1629}
1630
1631
1632/**
1633 * @interface_method_impl{PDMISERIALPORT,pfnNotifyStsLinesChanged}
1634 */
1635static DECLCALLBACK(int) uartR3NotifyStsLinesChanged(PPDMISERIALPORT pInterface, uint32_t fNewStatusLines)
1636{
1637 LogFlowFunc(("pInterface=%#p fNewStatusLines=%#x\n", pInterface, fNewStatusLines));
1638 PUARTCORECC pThisCC = RT_FROM_MEMBER(pInterface, UARTCORECC, ISerialPort);
1639 PUARTCORE pThis = pThisCC->pShared;
1640 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1641
1642 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
1643 uartR3StsLinesUpdate(pDevIns, pThis, pThisCC, fNewStatusLines);
1644 PDMCritSectLeave(&pThis->CritSect);
1645 return VINF_SUCCESS;
1646}
1647
1648
1649/**
1650 * @interface_method_impl{PDMISERIALPORT,pfnNotifyBrk}
1651 */
1652static DECLCALLBACK(int) uartR3NotifyBrk(PPDMISERIALPORT pInterface)
1653{
1654 LogFlowFunc(("pInterface=%#p\n", pInterface));
1655 PUARTCORECC pThisCC = RT_FROM_MEMBER(pInterface, UARTCORECC, ISerialPort);
1656 PUARTCORE pThis = pThisCC->pShared;
1657 PPDMDEVINS pDevIns = pThisCC->pDevIns;
1658
1659 PDMCritSectEnter(&pThis->CritSect, VERR_IGNORED);
1660 UART_REG_SET(pThis->uRegLsr, UART_REG_LSR_BI);
1661 uartIrqUpdate(pDevIns, pThis, pThisCC);
1662 PDMCritSectLeave(&pThis->CritSect);
1663 return VINF_SUCCESS;
1664}
1665
1666
1667/* -=-=-=-=-=-=-=-=- PDMIBASE -=-=-=-=-=-=-=-=- */
1668
1669/**
1670 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1671 */
1672static DECLCALLBACK(void *) uartR3QueryInterface(PPDMIBASE pInterface, const char *pszIID)
1673{
1674 PUARTCORECC pThisCC = RT_FROM_MEMBER(pInterface, UARTCORECC, IBase);
1675 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->IBase);
1676 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISERIALPORT, &pThisCC->ISerialPort);
1677 return NULL;
1678}
1679
1680
1681/**
1682 * Saves the UART state to the given SSM handle.
1683 *
1684 * @returns VBox status code.
1685 * @param pDevIns The device instance.
1686 * @param pThis The UART core instance.
1687 * @param pSSM The SSM handle to save to.
1688 */
1689DECLHIDDEN(int) uartR3SaveExec(PPDMDEVINS pDevIns, PUARTCORE pThis, PSSMHANDLE pSSM)
1690{
1691 SSMR3PutU16(pSSM, pThis->uRegDivisor);
1692 SSMR3PutU8(pSSM, pThis->uRegRbr);
1693 SSMR3PutU8(pSSM, pThis->uRegThr);
1694 SSMR3PutU8(pSSM, pThis->uRegIer);
1695 SSMR3PutU8(pSSM, pThis->uRegIir);
1696 SSMR3PutU8(pSSM, pThis->uRegFcr);
1697 SSMR3PutU8(pSSM, pThis->uRegLcr);
1698 SSMR3PutU8(pSSM, pThis->uRegMcr);
1699 SSMR3PutU8(pSSM, pThis->uRegLsr);
1700 SSMR3PutU8(pSSM, pThis->uRegMsr);
1701 SSMR3PutU8(pSSM, pThis->uRegScr);
1702 SSMR3PutBool(pSSM, pThis->fIrqCtiPending);
1703 SSMR3PutBool(pSSM, pThis->fThreEmptyPending);
1704 SSMR3PutU8(pSSM, pThis->FifoXmit.cbMax);
1705 SSMR3PutU8(pSSM, pThis->FifoXmit.cbItl);
1706 SSMR3PutU8(pSSM, pThis->FifoRecv.cbMax);
1707 SSMR3PutU8(pSSM, pThis->FifoRecv.cbItl);
1708
1709 int rc = PDMDevHlpTimerSave(pDevIns, pThis->hTimerRcvFifoTimeout, pSSM);
1710 if (RT_SUCCESS(rc))
1711 rc = PDMDevHlpTimerSave(pDevIns, pThis->hTimerTxUnconnected, pSSM);
1712
1713 return rc;
1714}
1715
1716
1717/**
1718 * Loads the UART state from the given SSM handle.
1719 *
1720 * @returns VBox status code.
1721 * @param pDevIns The device instance.
1722 * @param pThis The UART core instance.
1723 * @param pSSM The SSM handle to load from.
1724 * @param uVersion Saved state version.
1725 * @param uPass The SSM pass the call is done in.
1726 * @param pbIrq Where to store the IRQ value for legacy
1727 * saved states - optional.
1728 * @param pPortBase Where to store the I/O port base for legacy
1729 * saved states - optional.
1730 */
1731DECLHIDDEN(int) uartR3LoadExec(PPDMDEVINS pDevIns, PUARTCORE pThis, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass,
1732 uint8_t *pbIrq, RTIOPORT *pPortBase)
1733{
1734 RT_NOREF(uPass);
1735 int rc;
1736
1737 if (uVersion > UART_SAVED_STATE_VERSION_LEGACY_CODE)
1738 {
1739 SSMR3GetU16(pSSM, &pThis->uRegDivisor);
1740 SSMR3GetU8(pSSM, &pThis->uRegRbr);
1741 SSMR3GetU8(pSSM, &pThis->uRegThr);
1742 SSMR3GetU8(pSSM, &pThis->uRegIer);
1743 SSMR3GetU8(pSSM, &pThis->uRegIir);
1744 SSMR3GetU8(pSSM, &pThis->uRegFcr);
1745 SSMR3GetU8(pSSM, &pThis->uRegLcr);
1746 SSMR3GetU8(pSSM, &pThis->uRegMcr);
1747 SSMR3GetU8(pSSM, &pThis->uRegLsr);
1748 SSMR3GetU8(pSSM, &pThis->uRegMsr);
1749 SSMR3GetU8(pSSM, &pThis->uRegScr);
1750 SSMR3GetBool(pSSM, &pThis->fIrqCtiPending);
1751 SSMR3GetBool(pSSM, &pThis->fThreEmptyPending);
1752 SSMR3GetU8(pSSM, &pThis->FifoXmit.cbMax);
1753 SSMR3GetU8(pSSM, &pThis->FifoXmit.cbItl);
1754 SSMR3GetU8(pSSM, &pThis->FifoRecv.cbMax);
1755 SSMR3GetU8(pSSM, &pThis->FifoRecv.cbItl);
1756
1757 rc = PDMDevHlpTimerLoad(pDevIns, pThis->hTimerRcvFifoTimeout, pSSM);
1758 if (uVersion > UART_SAVED_STATE_VERSION_PRE_UNCONNECTED_TX_TIMER)
1759 rc = PDMDevHlpTimerLoad(pDevIns, pThis->hTimerTxUnconnected, pSSM);
1760 }
1761 else
1762 {
1763 AssertPtr(pbIrq);
1764 AssertPtr(pPortBase);
1765 if (uVersion == UART_SAVED_STATE_VERSION_16450)
1766 {
1767 pThis->enmType = UARTTYPE_16450;
1768 LogRel(("Serial#%d: falling back to 16450 mode from load state\n", pDevIns->iInstance));
1769 }
1770
1771 SSMR3GetU16(pSSM, &pThis->uRegDivisor);
1772 SSMR3GetU8(pSSM, &pThis->uRegRbr);
1773 SSMR3GetU8(pSSM, &pThis->uRegIer);
1774 SSMR3GetU8(pSSM, &pThis->uRegLcr);
1775 SSMR3GetU8(pSSM, &pThis->uRegMcr);
1776 SSMR3GetU8(pSSM, &pThis->uRegLsr);
1777 SSMR3GetU8(pSSM, &pThis->uRegMsr);
1778 SSMR3GetU8(pSSM, &pThis->uRegScr);
1779 if (uVersion > UART_SAVED_STATE_VERSION_16450)
1780 SSMR3GetU8(pSSM, &pThis->uRegFcr);
1781
1782 int32_t iTmp = 0;
1783 SSMR3GetS32(pSSM, &iTmp);
1784 pThis->fThreEmptyPending = RT_BOOL(iTmp);
1785
1786 rc = SSMR3GetS32(pSSM, &iTmp);
1787 AssertRCReturn(rc, rc);
1788 *pbIrq = (uint8_t)iTmp;
1789
1790 SSMR3Skip(pSSM, sizeof(int32_t)); /* was: last_break_enable */
1791
1792 uint32_t uPortBaseTmp = 0;
1793 rc = SSMR3GetU32(pSSM, &uPortBaseTmp);
1794 AssertRCReturn(rc, rc);
1795 *pPortBase = (RTIOPORT)uPortBaseTmp;
1796
1797 rc = SSMR3Skip(pSSM, sizeof(bool)); /* was: msr_changed */
1798 if ( RT_SUCCESS(rc)
1799 && uVersion > UART_SAVED_STATE_VERSION_MISSING_BITS)
1800 {
1801 SSMR3GetU8(pSSM, &pThis->uRegThr);
1802 SSMR3Skip(pSSM, sizeof(uint8_t)); /* The old transmit shift register, not used anymore. */
1803 SSMR3GetU8(pSSM, &pThis->uRegIir);
1804
1805 int32_t iTimeoutPending = 0;
1806 SSMR3GetS32(pSSM, &iTimeoutPending);
1807 pThis->fIrqCtiPending = RT_BOOL(iTimeoutPending);
1808
1809 rc = PDMDevHlpTimerLoad(pDevIns, pThis->hTimerRcvFifoTimeout, pSSM);
1810 AssertRCReturn(rc, rc);
1811
1812 bool fWasActiveIgn;
1813 rc = TMR3TimerSkip(pSSM, &fWasActiveIgn); /* was: transmit_timerR3 */
1814 AssertRCReturn(rc, rc);
1815
1816 SSMR3GetU8(pSSM, &pThis->FifoRecv.cbItl);
1817 rc = SSMR3GetU8(pSSM, &pThis->FifoRecv.cbItl);
1818 }
1819 }
1820
1821 return rc;
1822}
1823
1824
1825/**
1826 * Called when loading the state completed, updates the parameters of any driver underneath.
1827 *
1828 * @returns VBox status code.
1829 * @param pDevIns The device instance.
1830 * @param pThis The shared serial port instance data.
1831 * @param pThisCC The serial port instance data for the current context.
1832 * @param pSSM The SSM handle.
1833 */
1834DECLHIDDEN(int) uartR3LoadDone(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, PSSMHANDLE pSSM)
1835{
1836 RT_NOREF(pSSM);
1837
1838 uartR3ParamsUpdate(pDevIns, pThis, pThisCC);
1839 uartIrqUpdate(pDevIns, pThis, pThisCC);
1840
1841 if (pThisCC->pDrvSerial)
1842 {
1843 /* Set the modem lines to reflect the current state. */
1844 int rc = pThisCC->pDrvSerial->pfnChgModemLines(pThisCC->pDrvSerial,
1845 RT_BOOL(pThis->uRegMcr & UART_REG_MCR_RTS),
1846 RT_BOOL(pThis->uRegMcr & UART_REG_MCR_DTR));
1847 if (RT_FAILURE(rc))
1848 LogRel(("Serial#%d: Failed to set modem lines with %Rrc during saved state load\n",
1849 pDevIns->iInstance, rc));
1850
1851 uint32_t fStsLines = 0;
1852 rc = pThisCC->pDrvSerial->pfnQueryStsLines(pThisCC->pDrvSerial, &fStsLines);
1853 if (RT_SUCCESS(rc))
1854 uartR3StsLinesUpdate(pDevIns, pThis, pThisCC, fStsLines);
1855 else
1856 LogRel(("Serial#%d: Failed to query status line status with %Rrc during reset\n",
1857 pDevIns->iInstance, rc));
1858 }
1859
1860 return VINF_SUCCESS;
1861}
1862
1863
1864/**
1865 * Resets the given UART core instance.
1866 *
1867 * @returns nothing.
1868 * @param pDevIns The device instance.
1869 * @param pThis The shared serial port instance data.
1870 * @param pThisCC The serial port instance data for the current context.
1871 */
1872DECLHIDDEN(void) uartR3Reset(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC)
1873{
1874 pThis->uRegDivisor = 0x0c; /* Default to 9600 Baud. */
1875 pThis->uRegRbr = 0;
1876 pThis->uRegThr = 0;
1877 pThis->uRegIer = 0;
1878 pThis->uRegIir = UART_REG_IIR_IP_NO_INT;
1879 pThis->uRegFcr = 0;
1880 pThis->uRegLcr = 0; /* 5 data bits, no parity, 1 stop bit. */
1881 pThis->uRegMcr = 0;
1882 pThis->uRegLsr = UART_REG_LSR_THRE | UART_REG_LSR_TEMT;
1883 pThis->uRegMsr = 0; /* Updated below. */
1884 pThis->uRegScr = 0;
1885 pThis->fIrqCtiPending = false;
1886 pThis->fThreEmptyPending = true;
1887
1888 /* Standard FIFO size for 15550A. */
1889 pThis->FifoXmit.cbMax = 16;
1890 pThis->FifoRecv.cbMax = 16;
1891 pThis->FifoRecv.cbItl = 1;
1892
1893 uartR3XferReset(pDevIns, pThis, pThisCC);
1894}
1895
1896
1897/**
1898 * Attaches the given UART core instance to the drivers at the given LUN.
1899 *
1900 * @returns VBox status code.
1901 * @param pDevIns The device instance.
1902 * @param pThis The shared serial port instance data.
1903 * @param pThisCC The serial port instance data for the current context.
1904 * @param iLUN The LUN being attached.
1905 */
1906DECLHIDDEN(int) uartR3Attach(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC, unsigned iLUN)
1907{
1908 int rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThisCC->IBase, &pThisCC->pDrvBase, "Serial Char");
1909 if (RT_SUCCESS(rc))
1910 {
1911 pThisCC->pDrvSerial = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMISERIALCONNECTOR);
1912 if (!pThisCC->pDrvSerial)
1913 {
1914 AssertLogRelMsgFailed(("Configuration error: instance %d has no serial interface!\n", pDevIns->iInstance));
1915 return VERR_PDM_MISSING_INTERFACE;
1916 }
1917 uartR3XferReset(pDevIns, pThis, pThisCC);
1918 }
1919 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1920 {
1921 pThisCC->pDrvBase = NULL;
1922 pThisCC->pDrvSerial = NULL;
1923 rc = VINF_SUCCESS;
1924 uartR3XferReset(pDevIns, pThis, pThisCC);
1925 LogRel(("Serial#%d: no unit\n", pDevIns->iInstance));
1926 }
1927 else /* Don't call VMSetError here as we assume that the driver already set an appropriate error */
1928 LogRel(("Serial#%d: Failed to attach to serial driver. rc=%Rrc\n", pDevIns->iInstance, rc));
1929
1930 return rc;
1931}
1932
1933
1934/**
1935 * Detaches any attached driver from the given UART core instance.
1936 *
1937 * @returns nothing.
1938 * @param pDevIns The device instance.
1939 * @param pThis The shared serial port instance data.
1940 * @param pThisCC The serial port instance data for the current context.
1941 */
1942DECLHIDDEN(void) uartR3Detach(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC)
1943{
1944 /* Zero out important members. */
1945 pThisCC->pDrvBase = NULL;
1946 pThisCC->pDrvSerial = NULL;
1947 uartR3XferReset(pDevIns, pThis, pThisCC);
1948}
1949
1950
1951/**
1952 * Destroys the given UART core instance freeing all allocated resources.
1953 *
1954 * @returns nothing.
1955 * @param pDevIns The device instance.
1956 * @param pThis The shared UART core instance data..
1957 */
1958DECLHIDDEN(void) uartR3Destruct(PPDMDEVINS pDevIns, PUARTCORE pThis)
1959{
1960 PDMDevHlpCritSectDelete(pDevIns, &pThis->CritSect);
1961}
1962
1963
1964/**
1965 * Initializes the given UART core instance using the provided configuration.
1966 *
1967 * @returns VBox status code.
1968 * @param pDevIns The device instance pointer.
1969 * @param pThis The shared UART core instance data to
1970 * initialize.
1971 * @param pThisCC The ring-3 UART core instance data to
1972 * initialize.
1973 * @param enmType The type of UART emulated.
1974 * @param iLUN The LUN the UART should look for attached drivers.
1975 * @param fFlags Additional flags controlling device behavior.
1976 * @param pfnUartIrqReq Pointer to the interrupt request callback.
1977 */
1978DECLHIDDEN(int) uartR3Init(PPDMDEVINS pDevIns, PUARTCORE pThis, PUARTCORECC pThisCC,
1979 UARTTYPE enmType, unsigned iLUN, uint32_t fFlags, PFNUARTCOREIRQREQ pfnUartIrqReq)
1980{
1981 /*
1982 * Initialize the instance data.
1983 * (Do this early or the destructor might choke on something!)
1984 */
1985 pThis->iLUN = iLUN;
1986 pThis->enmType = enmType;
1987 pThis->fFlags = fFlags;
1988
1989 pThisCC->iLUN = iLUN;
1990 pThisCC->pDevIns = pDevIns;
1991 pThisCC->pShared = pThis;
1992 pThisCC->pfnUartIrqReq = pfnUartIrqReq;
1993
1994 /* IBase */
1995 pThisCC->IBase.pfnQueryInterface = uartR3QueryInterface;
1996
1997 /* ISerialPort */
1998 pThisCC->ISerialPort.pfnDataAvailRdrNotify = uartR3DataAvailRdrNotify;
1999 pThisCC->ISerialPort.pfnDataSentNotify = uartR3DataSentNotify;
2000 pThisCC->ISerialPort.pfnReadWr = uartR3ReadWr;
2001 pThisCC->ISerialPort.pfnNotifyStsLinesChanged = uartR3NotifyStsLinesChanged;
2002 pThisCC->ISerialPort.pfnNotifyBrk = uartR3NotifyBrk;
2003
2004 int rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "Uart{%s#%d}#%d",
2005 pDevIns->pReg->szName, pDevIns->iInstance, iLUN);
2006 AssertRCReturn(rc, rc);
2007
2008 /*
2009 * Attach the char driver and get the interfaces.
2010 */
2011 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThisCC->IBase, &pThisCC->pDrvBase, "UART");
2012 if (RT_SUCCESS(rc))
2013 {
2014 pThisCC->pDrvSerial = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMISERIALCONNECTOR);
2015 if (!pThisCC->pDrvSerial)
2016 {
2017 AssertLogRelMsgFailed(("Configuration error: instance %d has no serial interface!\n", iLUN));
2018 return VERR_PDM_MISSING_INTERFACE;
2019 }
2020 }
2021 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
2022 {
2023 pThisCC->pDrvBase = NULL;
2024 pThisCC->pDrvSerial = NULL;
2025 LogRel(("Serial#%d: no unit\n", iLUN));
2026 }
2027 else
2028 {
2029 AssertLogRelMsgFailed(("Serial#%d: Failed to attach to char driver. rc=%Rrc\n", iLUN, rc));
2030 /* Don't call VMSetError here as we assume that the driver already set an appropriate error */
2031 return rc;
2032 }
2033
2034 /*
2035 * Create the receive FIFO character timeout indicator timer.
2036 */
2037 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, uartR3RcvFifoTimeoutTimer, pThisCC,
2038 TMTIMER_FLAGS_NO_CRIT_SECT, "UART Rcv FIFO Timer",
2039 &pThis->hTimerRcvFifoTimeout);
2040 AssertRCReturn(rc, rc);
2041
2042 rc = PDMDevHlpTimerSetCritSect(pDevIns, pThis->hTimerRcvFifoTimeout, &pThis->CritSect);
2043 AssertRCReturn(rc, rc);
2044
2045 /*
2046 * Create the transmit timer when no device is connected.
2047 */
2048 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, uartR3TxUnconnectedTimer, pThisCC,
2049 TMTIMER_FLAGS_NO_CRIT_SECT, "UART TX uncon. Timer",
2050 &pThis->hTimerTxUnconnected);
2051 AssertRCReturn(rc, rc);
2052
2053 rc = PDMDevHlpTimerSetCritSect(pDevIns, pThis->hTimerTxUnconnected, &pThis->CritSect);
2054 AssertRCReturn(rc, rc);
2055
2056 uartR3Reset(pDevIns, pThis, pThisCC);
2057 return VINF_SUCCESS;
2058}
2059
2060#else /* !IN_RING3 */
2061
2062/**
2063 * Initializes the ring-0 / raw-mode instance data.
2064 *
2065 * @returns VBox status code.
2066 * @param pThisCC The serial port instance data for the current context.
2067 * @param pfnUartIrqReq Pointer to the interrupt request callback.
2068 */
2069DECLHIDDEN(int) uartRZInit(PUARTCORECC pThisCC, PFNUARTCOREIRQREQ pfnUartIrqReq)
2070{
2071 AssertPtrReturn(pfnUartIrqReq, VERR_INVALID_POINTER);
2072 AssertPtrReturn(pThisCC, VERR_INVALID_POINTER);
2073 pThisCC->pfnUartIrqReq = pfnUartIrqReq;
2074 return VINF_SUCCESS;
2075}
2076
2077#endif /* !IN_RING3 */
2078
2079#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette