All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Ashish Kalra <ashish.kalra@amd.com>,
	"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
	linux-kernel@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	David Woodhouse <dwmw@amazon.co.uk>,
	Denis Mukhin <dmukhin@ford.com>
Subject: [PATCH v1 2/6] x86/boot: Introduce helpers for serial I/O
Date: Fri,  2 May 2025 15:29:38 +0300	[thread overview]
Message-ID: <20250502123145.4066635-3-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20250502123145.4066635-1-andriy.shevchenko@linux.intel.com>

As preparatory to enable earlyprintk on non-standard ports on x86,
introduce serial_in() and serial_out() helpers to perform serial I/O.

No functional change intended.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 arch/x86/boot/boot.h                          |  2 +
 .../boot/compressed/early_serial_console.c    |  3 +
 arch/x86/boot/compressed/misc.c               |  4 +-
 arch/x86/boot/compressed/misc.h               |  4 ++
 arch/x86/boot/early_serial_console.c          | 63 +++++++++++++------
 arch/x86/boot/tty.c                           |  7 ++-
 6 files changed, 61 insertions(+), 22 deletions(-)

diff --git a/arch/x86/boot/boot.h b/arch/x86/boot/boot.h
index 753f429b28cb..6688c2053707 100644
--- a/arch/x86/boot/boot.h
+++ b/arch/x86/boot/boot.h
@@ -274,6 +274,8 @@ int check_knl_erratum(void);
 int validate_cpu(void);
 
 /* early_serial_console.c */
+extern unsigned int (*serial_in)(unsigned long addr, int offset);
+extern void (*serial_out)(unsigned long addr, int offset, int value);
 extern unsigned long early_serial_base;
 void console_init(void);
 
diff --git a/arch/x86/boot/compressed/early_serial_console.c b/arch/x86/boot/compressed/early_serial_console.c
index 5b8db03f40ad..a5e0459c3d2f 100644
--- a/arch/x86/boot/compressed/early_serial_console.c
+++ b/arch/x86/boot/compressed/early_serial_console.c
@@ -1,5 +1,8 @@
 #include "misc.h"
 
+unsigned int (*serial_in)(unsigned long addr, int offset);
+void (*serial_out)(unsigned long addr, int offset, int value);
+
 /* This might be accessed before .bss is cleared, so use .data instead. */
 unsigned long early_serial_base __section(".data");
 
diff --git a/arch/x86/boot/compressed/misc.c b/arch/x86/boot/compressed/misc.c
index 94b5991da001..056fa5f01598 100644
--- a/arch/x86/boot/compressed/misc.c
+++ b/arch/x86/boot/compressed/misc.c
@@ -109,10 +109,10 @@ static void serial_putchar(int ch)
 {
 	unsigned timeout = 0xffff;
 
-	while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
+	while ((serial_in(early_serial_base, LSR) & XMTRDY) == 0 && --timeout)
 		cpu_relax();
 
-	outb(ch, early_serial_base + TXR);
+	serial_out(early_serial_base, TXR, ch);
 }
 
 void __putstr(const char *s)
diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h
index f083360c84c1..a70e5f31765b 100644
--- a/arch/x86/boot/compressed/misc.h
+++ b/arch/x86/boot/compressed/misc.h
@@ -127,9 +127,13 @@ extern unsigned char _pgtable[];
 
 #ifdef CONFIG_EARLY_PRINTK
 /* early_serial_console.c */
+extern unsigned int (*serial_in)(unsigned long addr, int offset);
+extern void (*serial_out)(unsigned long addr, int offset, int value);
 extern unsigned long early_serial_base;
 void console_init(void);
 #else
+static unsigned int (*serial_in)(unsigned long addr, int offset);
+static void (*serial_out)(unsigned long addr, int offset, int value);
 static const unsigned long early_serial_base;
 static inline void console_init(void)
 { }
diff --git a/arch/x86/boot/early_serial_console.c b/arch/x86/boot/early_serial_console.c
index 7a7766fa16e5..f52a14284854 100644
--- a/arch/x86/boot/early_serial_console.c
+++ b/arch/x86/boot/early_serial_console.c
@@ -23,22 +23,45 @@
 
 #define DEFAULT_BAUD 9600
 
-static void early_serial_init(unsigned long port, int baud)
+static unsigned int io_serial_in(unsigned long addr, int offset)
+{
+	return inb(addr + offset);
+}
+
+static void io_serial_out(unsigned long addr, int offset, int value)
+{
+	outb(value, addr + offset);
+}
+
+static void early_serial_configure(unsigned long port, int baud)
 {
 	unsigned char c;
 	unsigned divisor;
 
-	outb(0x3, port + LCR);	/* 8n1 */
-	outb(0, port + IER);	/* no interrupt */
-	outb(0, port + FCR);	/* no fifo */
-	outb(0x3, port + MCR);	/* DTR + RTS */
+	serial_out(port, LCR, 0x3);	/* 8n1 */
+	serial_out(port, IER, 0);	/* no interrupt */
+	serial_out(port, FCR, 0);	/* no fifo */
+	serial_out(port, MCR, 0x3);	/* DTR + RTS */
 
 	divisor	= 115200 / baud;
-	c = inb(port + LCR);
-	outb(c | DLAB, port + LCR);
-	outb(divisor & 0xff, port + DLL);
-	outb((divisor >> 8) & 0xff, port + DLH);
-	outb(c & ~DLAB, port + LCR);
+	c = serial_in(port, LCR);
+	serial_out(port, LCR, c | DLAB);
+	serial_out(port, DLL, divisor & 0xff);
+	serial_out(port, DLH, (divisor >> 8) & 0xff);
+	serial_out(port, LCR, c & ~DLAB);
+}
+
+/* Assign serial I/O accessors */
+static void early_serial_use_io_accessors(void)
+{
+	/* These will always be IO based ports */
+	serial_in = io_serial_in;
+	serial_out = io_serial_out;
+}
+
+static void early_serial_init(unsigned long port, int baud)
+{
+	early_serial_configure(port, baud);
 
 	early_serial_base = port;
 }
@@ -73,6 +96,7 @@ static void parse_earlyprintk(void)
 				port = DEFAULT_SERIAL_PORT;
 			else
 				pos = e - arg;
+			early_serial_use_io_accessors();
 		} else if (!strncmp(arg + pos, "ttyS", 4)) {
 			static const int bases[] = { 0x3f8, 0x2f8 };
 			int idx = 0;
@@ -84,6 +108,7 @@ static void parse_earlyprintk(void)
 				idx = 1;
 
 			port = bases[idx];
+			early_serial_use_io_accessors();
 		}
 
 		if (arg[pos] == ',')
@@ -104,11 +129,11 @@ static unsigned int probe_baud(int port)
 	unsigned char lcr, dll, dlh;
 	unsigned int quot;
 
-	lcr = inb(port + LCR);
-	outb(lcr | DLAB, port + LCR);
-	dll = inb(port + DLL);
-	dlh = inb(port + DLH);
-	outb(lcr, port + LCR);
+	lcr = serial_in(port, LCR);
+	serial_out(port, LCR, lcr | DLAB);
+	dll = serial_in(port, DLL);
+	dlh = serial_in(port, DLH);
+	serial_out(port, LCR, lcr);
 	quot = (dlh << 8) | dll;
 
 	return BASE_BAUD / quot;
@@ -129,11 +154,13 @@ static void parse_console_uart8250(void)
 
 	options = optstr;
 
-	if (!strncmp(options, "uart8250,io,", 12))
+	if (!strncmp(options, "uart8250,io,", 12)) {
 		port = simple_strtoull(options + 12, &options, 0);
-	else if (!strncmp(options, "uart,io,", 8))
+		early_serial_use_io_accessors();
+	} else if (!strncmp(options, "uart,io,", 8)) {
 		port = simple_strtoull(options + 8, &options, 0);
-	else
+		early_serial_use_io_accessors();
+	} else
 		return;
 
 	if (options && (options[0] == ','))
diff --git a/arch/x86/boot/tty.c b/arch/x86/boot/tty.c
index e2ab8b8076ef..eb0eacd88db7 100644
--- a/arch/x86/boot/tty.c
+++ b/arch/x86/boot/tty.c
@@ -13,6 +13,9 @@
 
 #include "boot.h"
 
+unsigned int (*serial_in)(unsigned long addr, int offset);
+void (*serial_out)(unsigned long addr, int offset, int value);
+
 unsigned long early_serial_base;
 
 #define XMTRDY          0x20
@@ -29,10 +32,10 @@ static void __section(".inittext") serial_putchar(int ch)
 {
 	unsigned timeout = 0xffff;
 
-	while ((inb(early_serial_base + LSR) & XMTRDY) == 0 && --timeout)
+	while ((serial_in(early_serial_base, LSR) & XMTRDY) == 0 && --timeout)
 		cpu_relax();
 
-	outb(ch, early_serial_base + TXR);
+	serial_out(early_serial_base, TXR, ch);
 }
 
 static void __section(".inittext") bios_putchar(int ch)
-- 
2.47.2


  parent reply	other threads:[~2025-05-02 12:31 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-02 12:29 [PATCH v1 0/6] x86/boot: Enable earlyprintk on MMIO (8-bit) Andy Shevchenko
2025-05-02 12:29 ` [PATCH v1 1/6] x86/boot: Convert early_serial_base to unsigned long Andy Shevchenko
2025-05-02 12:29 ` Andy Shevchenko [this message]
2025-05-02 12:29 ` [PATCH v1 3/6] x86/boot: Split out parse_serial_port() helper for earlyprintk Andy Shevchenko
2025-05-02 12:29 ` [PATCH v1 4/6] x86/boot: Allow longer parameter list " Andy Shevchenko
2025-05-02 12:29 ` [PATCH v1 5/6] x86/boot: Also share MMIO accessors Andy Shevchenko
2025-05-02 12:29 ` [PATCH v1 6/6] x86/boot: Introduce MMIO accessors and their support in earlyprintk Andy Shevchenko
2025-05-02 17:33 ` [PATCH v1 0/6] x86/boot: Enable earlyprintk on MMIO (8-bit) David Woodhouse
2025-05-05 14:05   ` Andy Shevchenko
2025-05-05 14:35     ` David Woodhouse
2025-05-05 14:59       ` Andy Shevchenko
2025-05-05 15:01         ` Andy Shevchenko
2025-05-05 23:32           ` David Woodhouse
2025-05-06  3:19             ` David Woodhouse
2025-05-06 19:38 ` David Woodhouse
2025-05-07 13:54   ` Andy Shevchenko
  -- strict thread matches above, loose matches on Subject: below --
2018-01-14 14:32 [PATCH v1 1/6] x86/boot: Convert early_serial_base to unsigned long Andy Shevchenko
2018-01-14 14:32 ` [PATCH v1 2/6] x86/boot: Introduce helpers for serial I/O Andy Shevchenko

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=20250502123145.4066635-3-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=ashish.kalra@amd.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=dmukhin@ford.com \
    --cc=dwmw@amazon.co.uk \
    --cc=hpa@zytor.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.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.