From: Oleksii Kurochko <oleksii.kurochko@gmail.com>
To: dmukhin@ford.com, xen-devel@lists.xenproject.org
Cc: andrew.cooper3@citrix.com, anthony.perard@vates.tech,
jbeulich@suse.com, julien@xen.org, michal.orzel@amd.com,
roger@xenproject.org, sstabellini@kernel.org,
xakep.amatop@gmail.com, dmukhin@xen.org
Subject: Re: [PATCH v8 05/26] emul/ns16x50: implement DLL/DLM registers
Date: Mon, 16 Nov 2026 11:36:18 +0100 [thread overview]
Message-ID: <baf72de4-2fe9-4c11-91ad-46cbce2c9b82@gmail.com> (raw)
In-Reply-To: <20261111194800.1070016-6-dmukhin@ford.com>
On 11/11/26 8:47 PM, dmukhin@ford.com wrote:
> From: Denis Mukhin <dmukhin@ford.com>
>
> Add DLL/DLM registers emulation.
>
> DLL/DLM registers report hardcoded 115200 baud rate to the guest OS.
They are initialised to the divisor for 115200, but guest writes are
stored and read back, so the guest sees whatever it programmed. Maybe
"DLL/DLM are reset to the divisor for 115200 baud; the divisor doesn't
affect emulation otherwise".
Also, LCR only becomes writable in 08/26, so until then DLAB is always
0 and none of the code added here is reachable. Would it make sense to
order the LCR patch first?
>
> Signed-off-by: Denis Mukhin <dmukhin@ford.com>
> ---
> xen/common/emul/vuart/ns16x50.c | 82 +++++++++++++++++++++++++++++++++
> 1 file changed, 82 insertions(+)
>
> diff --git a/xen/common/emul/vuart/ns16x50.c b/xen/common/emul/vuart/ns16x50.c
> index 6e1cfecbaa55..80805ee6ecc6 100644
> --- a/xen/common/emul/vuart/ns16x50.c
> +++ b/xen/common/emul/vuart/ns16x50.c
> @@ -119,8 +119,37 @@ static inline void ns16x50_unlock(struct vuart_ns16x50 *vdev,
> static int ns16x50_io_write8(
> struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
> {
> + uint8_t *regs = vdev->regs;
> + uint8_t val = *data;
> int rc = 0;
>
> + switch ( reg )
> + {
> + case UART_THR: /* DLAB=0 --> UART_RBR, DLAB=1 --> UART_DLL */
On the write side, offset 0 with DLAB=0 is THR, not RBR.
> + /* DLAB=1 */
> + if ( regs[UART_LCR] & UART_LCR_DLAB )
> + {
> + regs[NS16X50_REGS_NUM + UART_DLL] = val;
> + break;
> + }
> +
> + /* DLAB=0 */
> + break;
> +
> + case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
> + /* DLAB=1 */
> + if ( regs[UART_LCR] & UART_LCR_DLAB )
> + {
> + regs[NS16X50_REGS_NUM + UART_DLM] = val;
> + }
Nit: no braces around a single statement (and it's inconsistent with
the read side, which uses break inside the if).
> + /* DLAB=0 */
> + break;
> +
> + default:
> + rc = -EINVAL;
> + break;
> + }
> +
> return rc;
> }
>
> @@ -131,8 +160,17 @@ static int ns16x50_io_write8(
> static int ns16x50_io_write16(
> struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
> {
> + uint8_t *regs = vdev->regs;
> + uint16_t val = *data;
> int rc = -EINVAL;
>
> + if ( (regs[UART_LCR] & UART_LCR_DLAB) && reg == UART_DLL )
> + {
> + vdev->regs[NS16X50_REGS_NUM + UART_DLL] = val & UINT8_MAX;
> + vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (val >> 8) & UINT8_MAX;
Nit: regs is declared but vdev->regs is used right below (same in
ns16x50_io_read16()).
More importantly, special-casing a 16-bit access to DLL with DLAB=1
covers just one case. E.g. QEMU's 16550 model accepts 2- and 4-byte
accesses but implements byte accesses only, so an outw() to base+0 ends
up as byte accesses to base+0 and base+1, each decoded according to the
current DLAB (serial_io_ops in hw/char/serial.c, with the splitting done
by access_with_adjusted_size() in system/memory.c). As far as I know,
that's also what happens on real hardware for an 8-bit device behind
ISA/LPC. Am I missing somethings? if no doing the same here would cover
all registers and drop the ns16x50_io_{read,write}16() helpers,
something like:
for ( i = 0; i < size; i++ )
{
uint8_t byte = *data >> (i * 8);
rc = ns16x50_io_write8(vdev, reg + i, &byte);
if ( rc )
break;
}
(and the reverse for reads, assembling *data from the bytes). As it
stands, e.g. an outw() to THR/IER with DLAB=0 is rejected and logged as
an error.
> + rc = 0;
> + }
> +
> return rc;
> }
>
> @@ -168,9 +206,39 @@ static int ns16x50_io_write(
> static int ns16x50_io_read8(
> struct vuart_ns16x50 *vdev, uint32_t reg, uint8_t *data)
> {
> + uint8_t *regs = vdev->regs;
> uint8_t val = UINT8_MAX;
> int rc = 0;
>
> + switch ( reg )
> + {
> + case UART_RBR: /* DLAB=0 --> UART_RBR, DLAB=1 --> UART_DLL */
> + /* DLAB=1 */
> + if ( regs[UART_LCR] & UART_LCR_DLAB )
> + {
> + val = regs[NS16X50_REGS_NUM + UART_DLL];
> + break;
> + }
> +
> + /* DLAB=0 */
> + break;
> +
> + case UART_IER: /* DLAB=0 --> UART_IER, DLAB=1 --> UART_DLM */
> + /* DLAB=1 */
> + if ( regs[UART_LCR] & UART_LCR_DLAB )
> + {
> + val = regs[NS16X50_REGS_NUM + UART_DLM];
> + break;
> + }
> +
> + /* DLAB=0 */
> + break;
> +
> + default:
> + rc = -EINVAL;
> + break;
> + }
> +
> *data = val;
>
> return rc;
> @@ -182,9 +250,17 @@ static int ns16x50_io_read8(
> static int ns16x50_io_read16(
> struct vuart_ns16x50 *vdev, uint32_t reg, uint16_t *data)
> {
> + uint8_t *regs = vdev->regs;
> uint16_t val = UINT16_MAX;
> int rc = -EINVAL;
>
> + if ( (regs[UART_LCR] & UART_LCR_DLAB) && reg == UART_DLL )
> + {
> + val = vdev->regs[NS16X50_REGS_NUM + UART_DLM] << 8 |
> + vdev->regs[NS16X50_REGS_NUM + UART_DLL];
> + rc = 0;
> + }
> +
> *data = val;
>
> return rc;
> @@ -310,7 +386,13 @@ static int cf_check ns16x50_io_handle(
>
> static void ns16x50_reset(struct vuart_ns16x50 *vdev)
> {
> + const uint16_t divisor = (UART_CLOCK_HZ / 115200) >> 4;
Nit: UART_CLOCK_HZ / (16 * 115200) reads more directly as the divisor
formula. A comment on why the divisor is preset at all would also help:
a real 16550 doesn't reset DLL/DLM, so this is a choice made for guests
that never program the baud rate.
> +
> memset(vdev->regs, 0, sizeof(vdev->regs));
> +
> + /* NB: report 115200 baud rate. */
> + vdev->regs[NS16X50_REGS_NUM + UART_DLL] = divisor & UINT8_MAX;
> + vdev->regs[NS16X50_REGS_NUM + UART_DLM] = (divisor >> 8) & UINT8_MAX;
> }
>
> static int ns16x50_init(void *arg)
~ Oleksii
next prev parent reply other threads:[~2026-11-16 10:36 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-11 19:47 [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator dmukhin
2026-11-11 19:47 ` [PATCH v8 01/26] emul/vuart: introduce framework for UART emulators dmukhin
2026-11-12 15:35 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 02/26] xen/8250-uart: update definitions dmukhin
2026-11-12 14:49 ` Jan Beulich
2026-11-12 16:34 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 03/26] x86: fixup dependencies for asm/x86_emulate.h dmukhin
2026-11-12 20:25 ` Oleksii Kurochko
2026-11-13 7:20 ` Jan Beulich
2026-11-11 19:47 ` [PATCH v8 04/26] emul/ns16x50: implement emulator stub dmukhin
2026-11-13 11:48 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 05/26] emul/ns16x50: implement DLL/DLM registers dmukhin
2026-11-16 10:36 ` Oleksii Kurochko [this message]
2026-11-11 19:47 ` [PATCH v8 06/26] emul/ns16x50: implement SCR register dmukhin
2026-11-16 10:42 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 07/26] emul/ns16x50: implement IER/IIR registers dmukhin
2026-11-16 16:59 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 08/26] emul/ns16x50: implement LCR/LSR registers dmukhin
2026-11-17 16:33 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 09/26] emul/ns16x50: implement MCR/MSR registers dmukhin
2026-11-17 18:32 ` Oleksii Kurochko
2026-11-11 19:47 ` [PATCH v8 10/26] emul/ns16x50: implement RBR register dmukhin
2026-11-11 19:47 ` [PATCH v8 11/26] emul/ns16x50: implement THR register dmukhin
2026-11-11 19:47 ` [PATCH v8 12/26] emul/ns16x50: implement FCR register (write-only) dmukhin
2026-11-11 19:47 ` [PATCH v8 13/26] emul/ns16550: implement dump_state() hook dmukhin
2026-11-11 19:47 ` [PATCH v8 14/26] emul/vuart: add global UART emulator configuration knob dmukhin
2026-11-11 19:47 ` [PATCH v8 15/26] emul/ns16x50: enable emulator dmukhin
2026-11-11 19:47 ` [PATCH v8 16/26] emul/ns16x50: add Kconfig options dmukhin
2026-11-11 19:47 ` [PATCH v8 17/26] x86/domain: enable per-domain I/O port bitmaps dmukhin
2026-11-11 19:47 ` [PATCH v8 18/26] emul/ns16x50: resolve physical I/O port sharing dmukhin
2026-11-11 19:47 ` [PATCH v8 19/26] xen/domain: allocate d->irq_caps before arch-specific initialization dmukhin
2026-11-11 19:47 ` [PATCH v8 20/26] emul/ns16x50: resolve physical IRQ sharing dmukhin
2026-11-11 19:47 ` [PATCH v8 21/26] emul/ns16x50: implement IRQ emulation via vIOAPIC dmukhin
2026-11-11 19:47 ` [PATCH v8 22/26] MAINTAINERS: add myself as vUART framework maintainer dmukhin
2026-11-11 19:47 ` [PATCH v8 23/26] docs/fusa: add NS16x50 UART requirements for x86 dmukhin
2026-11-11 19:47 ` [PATCH v8 24/26] ci: add initial NS16550-compatible XTF test dmukhin
2026-11-11 19:47 ` [PATCH v8 25/26] ci: add smoke NS16550-compatible PVH Linux test dmukhin
2026-11-11 19:48 ` [PATCH v8 26/26] ci: do not merge: test dmukhin
2026-11-12 14:53 ` [PATCH v8 00/26] x86: introduce NS16550-compatible UART emulator Oleksii Kurochko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=baf72de4-2fe9-4c11-91ad-46cbce2c9b82@gmail.com \
--to=oleksii.kurochko@gmail.com \
--cc=andrew.cooper3@citrix.com \
--cc=anthony.perard@vates.tech \
--cc=dmukhin@ford.com \
--cc=dmukhin@xen.org \
--cc=jbeulich@suse.com \
--cc=julien@xen.org \
--cc=michal.orzel@amd.com \
--cc=roger@xenproject.org \
--cc=sstabellini@kernel.org \
--cc=xakep.amatop@gmail.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.