From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E6DC606A322 for ; Thu, 3 Dec 2026 00:26:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=iozZUSsd Received: from list by lists.xenproject.org with outflank-mailman.1511318.1703763 (Exim 4.92) (envelope-from ) id 1xYue0-0003oo-NV; Thu, 03 Dec 2026 00:25:52 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1511318.1703763; Thu, 03 Dec 2026 00:25:52 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xYue0-0003oh-JZ; Thu, 03 Dec 2026 00:25:52 +0000 Received: by outflank-mailman (input) for mailman id 1511318; Thu, 03 Dec 2026 00:25:50 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xYudy-0003n1-IP for xen-devel@lists.xenproject.org; Thu, 03 Dec 2026 00:25:50 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xYudx-00AJgF-9e for xen-devel@lists.xenproject.org; Thu, 03 Dec 2026 01:25:49 +0100 Received: from [10.42.69.2] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b10b70a-e002-0a2a0a5209dd-0a2a4502d970-6 for ; Thu, 03 Dec 2026 01:25:49 +0100 Received: from [172.105.4.254] (helo=tor.source.kernel.org) by tlsNG-720697.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b10b70b-4900-0a2a45020019-ac6904feda84-3 for ; Thu, 03 Dec 2026 01:25:49 +0100 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 25EFF6022A; Thu, 3 Dec 2026 00:25:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E408B1F000C1; Thu, 3 Dec 2026 00:25:44 +0000 (UTC) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Post: List-Help: List-Subscribe: , Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=k20260515 header.d=kernel.org header.i="@kernel.org" header.h="Date:From:To:cc:Subject:In-Reply-To:References" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796257546; bh=StZRwCR5HQM0mPPfrvIY7BLmC+Py35vrMz7zFunfCFo=; h=Date:From:To:cc:Subject:In-Reply-To:References; b=iozZUSsd1Z74CRsBPVeXM3uCvVtBjKTucPHiTz5lm7s0b6M4ME5kZ8ftDawW8SEhz anQ599/p+dU44X6pyzX5C0Ch0UGPZYn56uXejTutXf6U5aVjhg13iWWCehoJHFI2G4 KAl5WYkY3RIT8g3Py5ircDTQLqkdwHP3mTtxf6ANf5QiPkBwTomfM3sdUJKPU4lgrQ 96gbg4y8TyDFLUBHLDFyZGhEZgFcQ/I3UZ5w21Lg5qduK9i4N2iPIvpPDyiNYUGAvj MKxsMu6o1GdYn08aP8F7Im1r+/6jbW05MmPexnI/juSMI0AEmmKbISS5lcnGJkvkhK g+5hObT1VgQAg== Date: Wed, 2 Dec 2026 16:25:44 -0800 (PST) From: Stefano Stabellini To: Denis Mukhin cc: xen-devel@lists.xenproject.org, andrew.cooper3@citrix.com, anthony.perard@vates.tech, jbeulich@suse.com, julien@xen.org, michal.orzel@amd.com, roger@xenproject.org, sstabellini@kernel.org, dmukhin@xen.org Subject: Re: [XTF PATCH v1 5/6] common: add simplistic NS16550-compatible UART driver In-Reply-To: <20261111195109.1070169-6-dmukhin@ford.com> Message-ID: <809ff2a2-d86e-7d10-2840-5e78228e30b6@kernel.org> References: <20261111195109.1070169-1-dmukhin@ford.com> <20261111195109.1070169-6-dmukhin@ford.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-purgate-ID: tlsNG-720697/1796257549-F9B286C6-D2356284/0/0 X-purgate-type: clean X-purgate-size: 11521 X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Wed, 11 Nov 2026, dmukhin@ford.com wrote: > From: Denis Mukhin > > Add a miniature NS16550-compatible driver for hypervisor validation. > > Implement polling mode and putchar only. > > Place the code in a common place since NS16550 hardware is not limited to > x86 systems (although the current driver uses few x86-ishms like I/O ports). > > Signed-off-by: Denis Mukhin > --- > build/files.mk | 1 + > common/console.c | 3 +- > common/ns16550.c | 153 ++++++++++++++++++++++++++++++++++++++++++ > include/xtf/console.h | 15 +++-- > include/xtf/ns16550.h | 117 ++++++++++++++++++++++++++++++++ > 5 files changed, 284 insertions(+), 5 deletions(-) > create mode 100644 common/ns16550.c > create mode 100644 include/xtf/ns16550.h > > diff --git a/build/files.mk b/build/files.mk > index 11624a89a21c..841ea9ebc70a 100644 > --- a/build/files.mk > +++ b/build/files.mk > @@ -34,6 +34,7 @@ obj-hvm += $(ROOT)/arch/x86/hvm/head.o > obj-hvm += $(ROOT)/arch/x86/hvm/pagetables.o > obj-hvm += $(ROOT)/arch/x86/hvm/traps.o > obj-hvm += $(ROOT)/arch/x86/io-apic.o > +obj-hvm += $(ROOT)/common/ns16550.o > > # Arguably common objects, but PV guests will have no interest in them. > obj-hvm += $(ROOT)/arch/x86/vmx.o > diff --git a/common/console.c b/common/console.c > index b3e89473a7b4..02ff90c5428d 100644 > --- a/common/console.c > +++ b/common/console.c > @@ -13,8 +13,9 @@ > * - Xen hypervisor console > * - PV console > * - Qemu debug console > + * - NS16550-compatible emulator > */ > -static cons_output_cb output_fns[3]; > +static cons_output_cb output_fns[4]; > static unsigned int nr_cons_cb; > > /* Guest PV console details. */ > diff --git a/common/ns16550.c b/common/ns16550.c > new file mode 100644 > index 000000000000..fa2bae5ebf98 > --- /dev/null > +++ b/common/ns16550.c > @@ -0,0 +1,153 @@ > +// SPDX-License-Identifier: BSD-2-Clause > +/** > + * Simple NS16550-compatible device driver. > + */ > + > +#include > +#include > + > +#include > +#include > +#include > + > +static const struct ns16550 *ns16550; > + > +static inline unsigned long > +ns16550_reg(const struct ns16550 *uart, unsigned int reg) > +{ > + return uart->base + (reg << uart->reg_shift); > +} > + > +uint8_t ns16550_inb(const struct ns16550 *uart, unsigned int reg) > +{ > + unsigned long addr = ns16550_reg(uart, reg); > + > + return inb(addr); > +} > + > +void ns16550_outb(const struct ns16550 *uart, unsigned int reg, > + unsigned char val) > +{ > + unsigned long addr = ns16550_reg(uart, reg); > + > + outb(val, addr); > +} > + > +void ns16550_wait_tx(const struct ns16550 *uart) > +{ > + while ( !(ns16550_inb(uart, UART_LSR) & UART_LSR_THRE) ) > + cpu_relax(); > +} > + > +void ns16550_drain_rx(const struct ns16550 *uart) > +{ > + unsigned int i; > + > + for ( i = 0; i < 128 && (ns16550_inb(uart, UART_LSR) & UART_LSR_DR); i++ ) > + (void)ns16550_inb(uart, UART_RBR); > +} > + > +void ns16550_putc(const struct ns16550 *uart, char c) > +{ > + ns16550_wait_tx(uart); > + > + /* Inject carriage return. */ > + if ( c == '\n' ) > + { > + ns16550_outb(uart, UART_THR, '\r'); > + ns16550_wait_tx(uart); > + } > + > + ns16550_outb(uart, UART_THR, c); > +} > + > +static void ns16550_write(const struct ns16550 *uart, const char *buf, > + size_t len) > +{ > + size_t i; > + > + for ( i = 0; i < len; i++ ) > + ns16550_putc(uart, buf[i]); > +} > + > +void ns16550_puts(const struct ns16550 *uart, const char *buf) > +{ > + ns16550_write(uart, buf, strlen(buf)); > +} > + > +int ns16550_init(const struct ns16550 *uart) > +{ > + unsigned int divisor = uart->clk_freq / (16 * uart->baud); This divides by zero if baud == 0 > + if ( !divisor ) > + divisor = 1; > + > + /* Disable interrupts. */ > + ns16550_outb(uart, UART_IER, 0x00); > + > + /* Set baud rate. */ > + ns16550_outb(uart, UART_LCR, UART_LCR_DLAB); > + ns16550_outb(uart, UART_DLL, divisor & 0xff); > + ns16550_outb(uart, UART_DLM, (divisor >> 8) & 0xff); > + > + /* Support only 8 data bits, no parity, 1 stop bit. */ > + ns16550_outb(uart, UART_LCR, UART_LCR_8N1); /* DLAB=0 */ > + > + /* Reset FIFOs. */ > + ns16550_outb(uart, UART_FCR, > + UART_FCR_ENABLE | UART_FCR_CLRX | > + UART_FCR_CLTX | UART_FCR_TRG14); > + > + /* Assert modem control lines. */ > + ns16550_outb(uart, UART_MCR, > + UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2); > + > + /* Drain any pending status/data to avoid stale state. */ > + (void)ns16550_inb(uart, UART_LSR); > + (void)ns16550_inb(uart, UART_RBR); > + (void)ns16550_inb(uart, UART_IIR); > + (void)ns16550_inb(uart, UART_MSR); > + > + return 0; > +} > + > +/* > + * Console driver. > + */ > + > +static void ns16550_console_write(const char *buf, size_t len) > +{ > + if (ns16550) code style > + ns16550_write(ns16550, buf, len); > +} > + > +void ns16550_console_puts(const char *buf) > +{ > + ns16550_console_write(buf, strlen(buf)); > +} > + > +int ns16550_console_register(const struct ns16550 *uart) > +{ > + if ( ns16550_init(uart) ) > + return -1; > + > + register_console_callback(ns16550_console_write); > + ns16550 = uart; > + > + return 0; > +} > + > +void ns16550_console_unregister(void) > +{ > + ns16550 = NULL; > +} > + > +/* > + * Local variables: > + * mode: C > + * c-file-style: "BSD" > + * c-basic-offset: 4 > + * tab-width: 4 > + * indent-tabs-mode: nil > + * End: > + */ > diff --git a/include/xtf/console.h b/include/xtf/console.h > index 16a6a23e3f56..4830e511b080 100644 > --- a/include/xtf/console.h > +++ b/include/xtf/console.h > @@ -3,6 +3,7 @@ > > #include > #include > +#include don't include this from console.h as it puts every UART_* define into every test > #include > #include > @@ -16,17 +17,23 @@ typedef void (*cons_output_cb)(const char *buf, size_t len); > */ > void register_console_callback(cons_output_cb cb); > > +void vprintk(const char *fmt, va_list args) __printf(1, 0); > +void printk(const char *fmt, ...) __printf(1, 2); > + > /* > * Initialise the PV console. Will register a callback. > */ > void init_pv_console(xencons_interface_t *ring, > evtchn_port_t port); > - > -void vprintk(const char *fmt, va_list args) __printf(1, 0); > -void printk(const char *fmt, ...) __printf(1, 2); > - > size_t pv_console_read_some(char *buf, size_t len); > > +/* > + * NS16550-backed console. > + */ > +int ns16550_console_register(const struct ns16550 *uart); > +void ns16550_console_unregister(void); > +void ns16550_console_puts(const char *buf); > + > #endif /* XTF_CONSOLE_H */ > > /* > diff --git a/include/xtf/ns16550.h b/include/xtf/ns16550.h > new file mode 100644 > index 000000000000..5db515b7fb91 > --- /dev/null > +++ b/include/xtf/ns16550.h > @@ -0,0 +1,117 @@ > +// SPDX-License-Identifier: BSD-2-Clause > +/** > + * Simple console driver for NS16550-compatible UART. > + */ > + > +#ifndef XTF_NS16550_H > +#define XTF_NS16550_H > + > +#define UART_RBR 0x00 > +#define UART_THR 0x00 > +#define UART_DLL 0x00 > +#define UART_IER 0x01 > +#define UART_IER_ERDAI 0x01 > +#define UART_IER_ETHREI 0x02 > +#define UART_IER_ELSI 0x04 > +#define UART_IER_EMSI 0x08 > +#define UART_IER_MASK 0x0f > +#define UART_IER_RSRVD 0xf0 > +#define UART_DLM 0x01 > +#define UART_IIR 0x02 > +#define UART_IIR_NOINT 0x01 > +#define UART_IIR_IMA 0x06 > +#define UART_IIR_LSI 0x06 > +#define UART_IIR_RDA 0x04 > +#define UART_IIR_THR 0x02 > +#define UART_IIR_MSI 0x00 > +#define UART_IIR_BSY 0x07 > +#define UART_IIR_FE 0xc0 > +#define UART_IIR_MASK 0x0f > +#define UART_FCR 0x02 > +#define UART_FCR_ENABLE 0x01 > +#define UART_FCR_CLRX 0x02 > +#define UART_FCR_CLTX 0x04 > +#define UART_FCR_DMA 0x08 > +#define UART_FCR_TRG1 0x00 > +#define UART_FCR_TRG4 0x40 > +#define UART_FCR_TRG8 0x80 > +#define UART_FCR_TRG14 0xc0 > +#define UART_LCR 0x03 > +#define UART_LCR_WLEN5 0x00 > +#define UART_LCR_WLEN6 0x01 > +#define UART_LCR_WLEN7 0x02 > +#define UART_LCR_WLEN8 0x03 > +#define UART_LCR_STB 0x04 > +#define UART_LCR_PE 0x08 > +#define UART_LCR_EP 0x10 > +#define UART_LCR_SP 0x20 > +#define UART_LCR_SB 0x40 > +#define UART_LCR_DLAB 0x80 > +#define UART_LCR_7N2 (UART_LCR_WLEN7 | UART_LCR_STB) > +#define UART_LCR_8N1 (UART_LCR_WLEN8) > +#define UART_LSR_ERRORS (UART_LSR_OE | UART_LSR_PE | \ > + UART_LSR_FE | UART_LSR_BI) > +#define UART_MCR 0x04 > +#define UART_MCR_DTR 0x01 > +#define UART_MCR_RTS 0x02 > +#define UART_MCR_OUT1 0x04 > +#define UART_MCR_OUT2 0x08 > +#define UART_MCR_LOOP 0x10 > +#define UART_MCR_RSRVD0 0x20 > +#define UART_MCR_TCRTLR 0x40 > +#define UART_MCR_RSRVD1 0x80 > +#define UART_MCR_MASK (UART_MCR_DTR | UART_MCR_RTS | \ > + UART_MCR_OUT1 | UART_MCR_OUT2 | \ > + UART_MCR_LOOP) > +#define UART_LSR 0x05 > +#define UART_LSR_DR 0x01 > +#define UART_LSR_OE 0x02 > +#define UART_LSR_PE 0x04 > +#define UART_LSR_FE 0x08 > +#define UART_LSR_BI 0x10 > +#define UART_LSR_THRE 0x20 > +#define UART_LSR_TEMT 0x40 > +#define UART_LSR_ERR 0x80 > +#define UART_MSR 0x06 > +#define UART_MSR_DCTS 0x01 > +#define UART_MSR_DDSR 0x02 > +#define UART_MSR_TERI 0x04 > +#define UART_MSR_DDCD 0x08 > +#define UART_MSR_CTS 0x10 > +#define UART_MSR_DSR 0x20 > +#define UART_MSR_RI 0x40 > +#define UART_MSR_DCD 0x80 > +#define UART_SCR 0x07 > + > +struct ns16550 { > + const char *name; > + unsigned long base; > + unsigned int reg_shift; > + unsigned int reg_width; > + unsigned int clk_freq; > + unsigned int baud; > +}; > + > +uint8_t ns16550_inb(const struct ns16550 *uart, unsigned int reg); > +void ns16550_outb(const struct ns16550 *uart, unsigned int reg, > + unsigned char val); > + > +void ns16550_wait_tx(const struct ns16550 *uart); > +void ns16550_drain_rx(const struct ns16550 *uart); > + > +void ns16550_putc(const struct ns16550 *uart, char c); > +void ns16550_puts(const struct ns16550 *uart, const char *buf); > + > +int ns16550_init(const struct ns16550 *uart); > + > +#endif /* XTF_NS16550_H */ > + > +/* > + * Local variables: > + * mode: C > + * c-file-style: "BSD" > + * c-basic-offset: 4 > + * tab-width: 4 > + * indent-tabs-mode: nil > + * End: > + */ > -- > 2.56.0 >