Skip to contentSkip to conversation

serial: Add MMIO & SPCR support

PATCH RESEND Superseded Benjamin Herrenschmidt 2 participants 17 messages Dec 1, 2022 – Dec 22, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover serial: Add MMIO & SPCR support

This series adds support for MMIO serial ports and auto-configuration via ACPI SPCR.

This is necessary for the serial port to work on AWS EC2 "metal" x86 systems.

An MMIO serial port can be specified explicitely using the "mmio," prefix for the --port argument to the 'serial' command, the register access size can also be specified via a suffix, and when 'serial' is used without arguments, it will try to use an ACPI SPCR entry (if any) to add the default port and configure it appropriately (speed, parity etc...)

This was tested using SPCR on an actual c5.metal instance, and using explicit instanciation via serial -p mmioXXXXXXXX on a modified qemu hacked to create MMIO PCI serial ports.

#1 lore raw

PATCH 1/7 acpi: Export a generic grub_acpi_find_table

From: Matthias Lange <matthias.lange () kernkonzept ! com>

And convert grub_acpi_find_fadt to use it

---
 grub-core/kern/acpi.c | 43 +++++++++++++++++++++++++------------------
 include/grub/acpi.h   |  3 +++
 2 files changed, 28 insertions(+), 18 deletions(-)
grub-core/kern/acpi.c +25 -18
index 70898ddcd..5d5b54b72 100644
@@ -85,35 +85,42 @@ grub_acpi_xsdt_find_table (struct grub_acpi_table_header *xsdt, const char *sig)
   return 0;
 }
 
-struct grub_acpi_fadt *
-grub_acpi_find_fadt (void)
+void *
+grub_acpi_find_table (const char *sig)
 {
-  struct grub_acpi_fadt *fadt = 0;
+  struct grub_acpi_fadt *r = 0;
   struct grub_acpi_rsdp_v10 *rsdpv1;
   struct grub_acpi_rsdp_v20 *rsdpv2;
+
   rsdpv1 = grub_machine_acpi_get_rsdpv1 ();
   if (rsdpv1)
-    fadt = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
-				      (grub_addr_t) rsdpv1->rsdt_addr,
-				      GRUB_ACPI_FADT_SIGNATURE);
-  if (fadt)
-    return fadt;
+    r = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
+				   (grub_addr_t) rsdpv1->rsdt_addr,
+				   sig);
+  if (r)
+    return r;
   rsdpv2 = grub_machine_acpi_get_rsdpv2 ();
   if (rsdpv2)
-    fadt = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
-				      (grub_addr_t) rsdpv2->rsdpv1.rsdt_addr,
-				      GRUB_ACPI_FADT_SIGNATURE);
-  if (fadt)
-    return fadt;
+    r = grub_acpi_rsdt_find_table ((struct grub_acpi_table_header *)
+				   (grub_addr_t) rsdpv2->rsdpv1.rsdt_addr,
+				   sig);
+  if (r)
+    return r;
   if (rsdpv2
 #if GRUB_CPU_SIZEOF_VOID_P != 8
       && !(rsdpv2->xsdt_addr >> 32)
 #endif
       )
-    fadt = grub_acpi_xsdt_find_table ((struct grub_acpi_table_header *)
-				      (grub_addr_t) rsdpv2->xsdt_addr,
-				      GRUB_ACPI_FADT_SIGNATURE);
-  if (fadt)
-    return fadt;
+    r = grub_acpi_xsdt_find_table ((struct grub_acpi_table_header *)
+				   (grub_addr_t) rsdpv2->xsdt_addr,
+				   sig);
+  if (r)
+    return r;
   return 0;
 }
+
+struct grub_acpi_fadt *
+grub_acpi_find_fadt (void)
+{
+  return grub_acpi_find_table (GRUB_ACPI_FADT_SIGNATURE);
+}
include/grub/acpi.h +3 -0
index 84f49487d..8c126b2b9 100644
@@ -244,4 +244,7 @@ enum
 struct grub_acpi_fadt *
 EXPORT_FUNC(grub_acpi_find_fadt) (void);
 
+void *
+EXPORT_FUNC(grub_acpi_find_table) (const char *sig);
+
 #endif /* ! GRUB_ACPI_HEADER */
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/7 acpi: Add SPCR and generic address definitions

In reply to Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

---
 include/grub/acpi.h | 51 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
include/grub/acpi.h +51 -0
index 8c126b2b9..17aadb802 100644
@@ -179,6 +179,57 @@ enum
     GRUB_ACPI_MADT_ENTRY_SAPIC_FLAGS_ENABLED = 1
   };
 
+struct grub_acpi_genaddr {
+  grub_uint8_t space_id;
+#define GRUB_ACPI_GENADDR_MEM_SPACE 0x00
+#define GRUB_ACPI_GENADDR_IO_SPACE  0x01
+  grub_uint8_t bit_width;
+  grub_uint8_t bit_offset;
+  grub_uint8_t access_size;
+#define GRUB_ACPI_GENADDR_SIZE_LGCY  0x00
+#define GRUB_ACPI_GENADDR_SIZE_BYTE  0x01
+#define GRUB_ACPI_GENADDR_SIZE_WORD  0x02
+#define GRUB_ACPI_GENADDR_SIZE_DWORD 0x03
+#define GRUB_ACPI_GENADDR_SIZE_QWORD 0x04
+  grub_uint64_t addr;
+} GRUB_PACKED;
+
+#define GRUB_ACPI_SPCR_SIGNATURE "SPCR"
+
+struct grub_acpi_spcr {
+  struct grub_acpi_table_header hdr;
+  grub_uint8_t intf_type;
+#define GRUB_ACPI_SPCR_INTF_TYPE_16550  0x00
+#define GRUB_ACPI_SPCR_INTF_TYPE_16550X 0x01
+  grub_uint8_t reserved_0[3];
+  struct grub_acpi_genaddr base_addr;
+  grub_uint8_t interrupt_type;
+  grub_uint8_t irq;
+  grub_uint32_t gsi;
+  grub_uint8_t baud_rate;
+#define GRUB_ACPI_SPCR_BAUD_CURRENT  0x00
+#define GRUB_ACPI_SPCR_BAUD_9600     0x03
+#define GRUB_ACPI_SPCR_BAUD_19200    0x04
+#define GRUB_ACPI_SPCR_BAUD_57600    0x06
+#define GRUB_ACPI_SPCR_BAUD_115200   0x07
+  grub_uint8_t parity;
+  grub_uint8_t stop_bits;
+  grub_uint8_t flow_control;
+#define GRUB_ACPI_SPCR_FC_DCD_TX     (1 << 0)
+#define GRUB_ACPI_SPCR_FC_RTSCTS     (1 << 1)
+#define GRUB_ACPI_SPCR_FC_XONXOFF    (1 << 2)
+  grub_uint8_t terminal_type;
+  grub_uint8_t language;
+  grub_uint16_t	pci_device_id;
+  grub_uint16_t pci_vendor_id;
+  grub_uint8_t pci_bus;
+  grub_uint8_t pci_device;
+  grub_uint8_t pci_function;
+  grub_uint32_t pci_flags;
+  grub_uint8_t pci_segment;
+  grub_uint32_t reserved_1;
+} GRUB_PACKED;
+
 #ifndef GRUB_DSDT_TEST
 struct grub_acpi_rsdp_v10 *grub_acpi_get_rsdpv1 (void);
 struct grub_acpi_rsdp_v20 *grub_acpi_get_rsdpv2 (void);
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/7 ns8250: Add base support for MMIO UARTs

In reply to Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

This adds the ability for the driver to access UARTs via MMIO instead of PIO selectively at runtime, and exposes a new function to add an MMIO port.

---
 grub-core/term/ns8250.c | 78 ++++++++++++++++++++++++++++++++---------
 grub-core/term/serial.c |  8 +++--
 include/grub/serial.h   | 11 +++++-
 3 files changed, 78 insertions(+), 19 deletions(-)
grub-core/term/ns8250.c +62 -16
index 83b25990c..b640508d0 100644
@@ -43,6 +43,24 @@ static int dead_ports = 0;
 #define DEFAULT_BASE_CLOCK 115200
 #endif
 
+static inline unsigned char
+ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg)
+{
+  asm volatile("" : : : "memory");
+  if (port->mmio)
+    return *((volatile unsigned char *)(port->mmio_base + reg));
+  return grub_inb (port->port + reg);
+}
+
+static inline void
+ns8250_reg_write (struct grub_serial_port *port, unsigned char value, grub_addr_t reg)
+{
+  asm volatile("" : : : "memory");
+  if (port->mmio)
+    *((volatile char *)(port->mmio_base + reg)) = value;
+  else
+    grub_outb (value, port->port + reg);
+}
 
 /* Convert speed to divisor.  */
 static unsigned short
@@ -93,43 +111,42 @@ do_real_config (struct grub_serial_port *port)
   divisor = serial_get_divisor (port, &port->config);
 
   /* Turn off the interrupt.  */
-  grub_outb (0, port->port + UART_IER);
+  ns8250_reg_write (port, 0, UART_IER);
 
   /* Set DLAB.  */
-  grub_outb (UART_DLAB, port->port + UART_LCR);
+  ns8250_reg_write (port, UART_DLAB, UART_LCR);
 
-  /* Set the baud rate.  */
-  grub_outb (divisor & 0xFF, port->port + UART_DLL);
-  grub_outb (divisor >> 8, port->port + UART_DLH);
+  ns8250_reg_write (port, divisor & 0xFF, UART_DLL);
+  ns8250_reg_write (port, divisor >> 8, UART_DLH);
 
   /* Set the line status.  */
   status |= (parities[port->config.parity]
 	     | (port->config.word_len - 5)
 	     | stop_bits[port->config.stop_bits]);
-  grub_outb (status, port->port + UART_LCR);
+  ns8250_reg_write (port, status, UART_LCR);
 
   if (port->config.rtscts)
     {
       /* Enable the FIFO.  */
-      grub_outb (UART_ENABLE_FIFO_TRIGGER1, port->port + UART_FCR);
+      ns8250_reg_write (port, UART_ENABLE_FIFO_TRIGGER1, UART_FCR);
 
       /* Turn on DTR and RTS.  */
-      grub_outb (UART_ENABLE_DTRRTS, port->port + UART_MCR);
+      ns8250_reg_write (port, UART_ENABLE_DTRRTS, UART_MCR);
     }
   else
     {
       /* Enable the FIFO.  */
-      grub_outb (UART_ENABLE_FIFO_TRIGGER14, port->port + UART_FCR);
+      ns8250_reg_write (port, UART_ENABLE_FIFO_TRIGGER14, UART_FCR);
 
       /* Turn on DTR, RTS, and OUT2.  */
-      grub_outb (UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, port->port + UART_MCR);
+      ns8250_reg_write (port, UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, UART_MCR);
     }
 
   /* Drain the input buffer.  */
   endtime = grub_get_time_ms () + 1000;
-  while (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
+  while (ns8250_reg_read (port, UART_LSR) & UART_DATA_READY)
     {
-      grub_inb (port->port + UART_RX);
+      ns8250_reg_read (port, UART_RX);
       if (grub_get_time_ms () > endtime)
 	{
 	  port->broken = 1;
@@ -145,8 +162,8 @@ static int
 serial_hw_fetch (struct grub_serial_port *port)
 {
   do_real_config (port);
-  if (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
-    return grub_inb (port->port + UART_RX);
+  if (ns8250_reg_read (port, UART_LSR) & UART_DATA_READY)
+    return ns8250_reg_read (port, UART_RX);
 
   return -1;
 }
@@ -166,7 +183,7 @@ serial_hw_put (struct grub_serial_port *port, const int c)
   else
     endtime = grub_get_time_ms () + 200;
   /* Wait until the transmitter holding register is empty.  */
-  while ((grub_inb (port->port + UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
+  while ((ns8250_reg_read (port, UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
     {
       if (grub_get_time_ms () > endtime)
 	{
@@ -179,7 +196,7 @@ serial_hw_put (struct grub_serial_port *port, const int c)
   if (port->broken)
     port->broken--;
 
-  grub_outb (c, port->port + UART_TX);
+  ns8250_reg_write (port, c, UART_TX);
 }
 
 /* Initialize a serial device. PORT is the port number for a serial device.
@@ -262,6 +279,7 @@ grub_ns8250_init (void)
 	com_ports[i].name = com_names[i];
 	com_ports[i].driver = &grub_ns8250_driver;
 	com_ports[i].port = serial_hw_io_addr[i];
+	com_ports[i].mmio = 0;
 	err = grub_serial_config_defaults (&com_ports[i]);
 	if (err)
 	  grub_print_error ();
@@ -316,8 +334,36 @@ grub_serial_ns8250_add_port (grub_port_t port)
     }
   p->driver = &grub_ns8250_driver;
   grub_serial_config_defaults (p);
+  p->mmio = 0;
   p->port = port;
   grub_serial_register (p);
 
   return p->name;
 }
+
+char *
+grub_serial_ns8250_add_mmio(grub_addr_t addr)
+{
+  struct grub_serial_port *p;
+  unsigned i;
+  for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
+    if (com_ports[i].mmio &&  com_ports[i].mmio_base == addr)
+	return com_names[i];
+
+  p = grub_malloc (sizeof (*p));
+  if (!p)
+    return NULL;
+  p->name = grub_xasprintf ("mmio,%llx", (unsigned long long) addr);
+  if (!p->name)
+    {
+      grub_free (p);
+      return NULL;
+    }
+  p->driver = &grub_ns8250_driver;
+  grub_serial_config_defaults (p);
+  p->mmio = 1;
+  p->mmio_base = addr;
+  grub_serial_register (p);
+
+  return p->name;
+}
grub-core/term/serial.c +6 -2
index 842322b1c..c8f5f02db 100644
@@ -201,8 +201,12 @@ grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args)
 
   if (state[OPTION_PORT].set)
     {
-      grub_snprintf (pname, sizeof (pname), "port%lx",
-		     grub_strtoul (state[1].arg, 0, 0));
+      if (grub_memcmp (state[OPTION_PORT].arg, "mmio", 4) == 0)
+          grub_snprintf(pname, sizeof (pname), "%s", state[1].arg);
+      else
+          grub_snprintf (pname, sizeof (pname), "port%lx",
+                         grub_strtoul (state[1].arg, 0, 0));
+
       name = pname;
     }
 
include/grub/serial.h +10 -1
index 67379de45..ccf464d41 100644
@@ -86,9 +86,17 @@ struct grub_serial_port
    */
   union
   {
+    struct
+    {
+      int mmio;
+      union
+      {
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
-    grub_port_t port;
+        grub_port_t port;
 #endif
+        grub_addr_t mmio_base;
+      };
+    };
     struct
     {
       grub_usb_device_t usbdev;
@@ -178,6 +186,7 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
 void grub_ns8250_init (void);
 char *grub_serial_ns8250_add_port (grub_port_t port);
+char *grub_serial_ns8250_add_mmio(grub_addr_t addr);
 #endif
 #ifdef GRUB_MACHINE_IEEE1275
 void grub_ofserial_init (void);
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 3/7] ns8250: Add base support for MMIO UARTs

Quoted 18 lines · Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

This adds the ability for the driver to access UARTs via MMIO instead of PIO selectively at runtime, and exposes a new function to add an MMIO port.

---
 grub-core/term/ns8250.c | 78 ++++++++++++++++++++++++++++++++---------
 grub-core/term/serial.c |  8 +++--
 include/grub/serial.h   | 11 +++++-
 3 files changed, 78 insertions(+), 19 deletions(-)
grub-core/term/ns8250.c +0 -0
index 83b25990c..b640508d0 100644

grub-core/term/ns8250.c static int dead_ports = 0; · line 46

Quoted 4 lines
grub-core/term/ns8250.c +1 -0
@@ -43,6 +43,24 @@ static int dead_ports = 0;
 #define DEFAULT_BASE_CLOCK 115200
 #endif
 
+static inline unsigned char

Please drop inline from here. Then compiler will be free to optimize out or not this function.

grub-core/term/ns8250.c static int dead_ports = 0; · line 55

Quoted 9 lines
grub-core/term/ns8250.c +9 -0
+ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg)
+{
+  asm volatile("" : : : "memory");
+  if (port->mmio)
+    return *((volatile unsigned char *)(port->mmio_base + reg));
+  return grub_inb (port->port + reg);
+}
+
+static inline void

Ditto...

grub-core/term/ns8250.c static int dead_ports = 0; · line 60

Quoted 5 lines
grub-core/term/ns8250.c +5 -0
+ns8250_reg_write (struct grub_serial_port *port, unsigned char value, grub_addr_t reg)
+{
+  asm volatile("" : : : "memory");
+  if (port->mmio)
+    *((volatile char *)(port->mmio_base + reg)) = value;

Casts require space like this: (volatile char *) (port->mmio_base + reg). Please fix casts in other places/patches too.

Quoted 104 lines
grub-core/term/ns8250.c +20 -16
+  else
+    grub_outb (value, port->port + reg);
+}
 
 /* Convert speed to divisor.  */
 static unsigned short
@@ -93,43 +111,42 @@ do_real_config (struct grub_serial_port *port)
   divisor = serial_get_divisor (port, &port->config);
 
   /* Turn off the interrupt.  */
-  grub_outb (0, port->port + UART_IER);
+  ns8250_reg_write (port, 0, UART_IER);
 
   /* Set DLAB.  */
-  grub_outb (UART_DLAB, port->port + UART_LCR);
+  ns8250_reg_write (port, UART_DLAB, UART_LCR);
 
-  /* Set the baud rate.  */
-  grub_outb (divisor & 0xFF, port->port + UART_DLL);
-  grub_outb (divisor >> 8, port->port + UART_DLH);
+  ns8250_reg_write (port, divisor & 0xFF, UART_DLL);
+  ns8250_reg_write (port, divisor >> 8, UART_DLH);
 
   /* Set the line status.  */
   status |= (parities[port->config.parity]
 	     | (port->config.word_len - 5)
 	     | stop_bits[port->config.stop_bits]);
-  grub_outb (status, port->port + UART_LCR);
+  ns8250_reg_write (port, status, UART_LCR);
 
   if (port->config.rtscts)
     {
       /* Enable the FIFO.  */
-      grub_outb (UART_ENABLE_FIFO_TRIGGER1, port->port + UART_FCR);
+      ns8250_reg_write (port, UART_ENABLE_FIFO_TRIGGER1, UART_FCR);
 
       /* Turn on DTR and RTS.  */
-      grub_outb (UART_ENABLE_DTRRTS, port->port + UART_MCR);
+      ns8250_reg_write (port, UART_ENABLE_DTRRTS, UART_MCR);
     }
   else
     {
       /* Enable the FIFO.  */
-      grub_outb (UART_ENABLE_FIFO_TRIGGER14, port->port + UART_FCR);
+      ns8250_reg_write (port, UART_ENABLE_FIFO_TRIGGER14, UART_FCR);
 
       /* Turn on DTR, RTS, and OUT2.  */
-      grub_outb (UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, port->port + UART_MCR);
+      ns8250_reg_write (port, UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, UART_MCR);
     }
 
   /* Drain the input buffer.  */
   endtime = grub_get_time_ms () + 1000;
-  while (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
+  while (ns8250_reg_read (port, UART_LSR) & UART_DATA_READY)
     {
-      grub_inb (port->port + UART_RX);
+      ns8250_reg_read (port, UART_RX);
       if (grub_get_time_ms () > endtime)
 	{
 	  port->broken = 1;
@@ -145,8 +162,8 @@ static int
 serial_hw_fetch (struct grub_serial_port *port)
 {
   do_real_config (port);
-  if (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
-    return grub_inb (port->port + UART_RX);
+  if (ns8250_reg_read (port, UART_LSR) & UART_DATA_READY)
+    return ns8250_reg_read (port, UART_RX);
 
   return -1;
 }
@@ -166,7 +183,7 @@ serial_hw_put (struct grub_serial_port *port, const int c)
   else
     endtime = grub_get_time_ms () + 200;
   /* Wait until the transmitter holding register is empty.  */
-  while ((grub_inb (port->port + UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
+  while ((ns8250_reg_read (port, UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
     {
       if (grub_get_time_ms () > endtime)
 	{
@@ -179,7 +196,7 @@ serial_hw_put (struct grub_serial_port *port, const int c)
   if (port->broken)
     port->broken--;
 
-  grub_outb (c, port->port + UART_TX);
+  ns8250_reg_write (port, c, UART_TX);
 }
 
 /* Initialize a serial device. PORT is the port number for a serial device.
@@ -262,6 +279,7 @@ grub_ns8250_init (void)
 	com_ports[i].name = com_names[i];
 	com_ports[i].driver = &grub_ns8250_driver;
 	com_ports[i].port = serial_hw_io_addr[i];
+	com_ports[i].mmio = 0;
 	err = grub_serial_config_defaults (&com_ports[i]);
 	if (err)
 	  grub_print_error ();
@@ -316,8 +334,36 @@ grub_serial_ns8250_add_port (grub_port_t port)
     }
   p->driver = &grub_ns8250_driver;
   grub_serial_config_defaults (p);
+  p->mmio = 0;
   p->port = port;

grub-core/term/ns8250.c grub_serial_ns8250_add_port (grub_port_t port) · line 348

Quoted 10 lines
grub-core/term/ns8250.c +6 -0
   grub_serial_register (p);
 
   return p->name;
 }
+
+char *
+grub_serial_ns8250_add_mmio(grub_addr_t addr)
+{
+  struct grub_serial_port *p;
+  unsigned i;

Please add en empty line here.

grub-core/term/ns8250.c grub_serial_ns8250_add_port (grub_port_t port) · line 354

Quoted 6 lines
grub-core/term/ns8250.c +6 -0
+  for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
+    if (com_ports[i].mmio &&  com_ports[i].mmio_base == addr)
+	return com_names[i];
+
+  p = grub_malloc (sizeof (*p));
+  if (!p)

I prefer "p == NULL" instead of "!p". If you could fix that here and in the other places/patches that will be nice.

Quoted 39 lines
grub-core/term/ns8250.c +15 -0
+    return NULL;
+  p->name = grub_xasprintf ("mmio,%llx", (unsigned long long) addr);
+  if (!p->name)
+    {
+      grub_free (p);
+      return NULL;
+    }
+  p->driver = &grub_ns8250_driver;
+  grub_serial_config_defaults (p);
+  p->mmio = 1;
+  p->mmio_base = addr;
+  grub_serial_register (p);
+
+  return p->name;
+}
grub-core/term/serial.c +6 -2
index 842322b1c..c8f5f02db 100644
@@ -201,8 +201,12 @@ grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args)
 
   if (state[OPTION_PORT].set)
     {
-      grub_snprintf (pname, sizeof (pname), "port%lx",
-		     grub_strtoul (state[1].arg, 0, 0));
+      if (grub_memcmp (state[OPTION_PORT].arg, "mmio", 4) == 0)
+          grub_snprintf(pname, sizeof (pname), "%s", state[1].arg);
+      else
+          grub_snprintf (pname, sizeof (pname), "port%lx",
+                         grub_strtoul (state[1].arg, 0, 0));
+
       name = pname;
     }
 
include/grub/serial.h +0 -0
index 67379de45..ccf464d41 100644

include/grub/serial.h struct grub_serial_port · line 91

Quoted 6 lines
include/grub/serial.h +3 -0
@@ -86,9 +86,17 @@ struct grub_serial_port
    */
   union
   {
+    struct
+    {
+      int mmio;

Could you use bool here and true/false in the code?

Quoted 13 lines
include/grub/serial.h +6 -1
+      union
+      {
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
-    grub_port_t port;
+        grub_port_t port;
 #endif
+        grub_addr_t mmio_base;
+      };
+    };
     struct
     {
       grub_usb_device_t usbdev;

include/grub/serial.h grub_serial_config_defaults (struct grub_serial_port *port) · line 189

Quoted 4 lines
include/grub/serial.h +1 -0
@@ -178,6 +186,7 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
 void grub_ns8250_init (void);
 char *grub_serial_ns8250_add_port (grub_port_t port);
+char *grub_serial_ns8250_add_mmio(grub_addr_t addr);

Please do not drop space before "(". I saw similar mistakes in the other patches.

Daniel

#5 lore raw

Re: [PATCH 3/7] ns8250: Add base support for MMIO UARTs

Thanks for your review !

I'll address everything. Small "nits":

Quoted 8 lines · Daniel Kiper

grub-core/term/ns8250.c grub_serial_ns8250_add_port (grub_port_t port) · line 348

Quoted 6 lines
grub-core/term/ns8250.c +6 -0
+
+char *
+grub_serial_ns8250_add_mmio(grub_addr_t addr)
+{
+  struct grub_serial_port *p;
+  unsigned i;

Please add en empty line here.

Ack. I copied grub_serial_ns8250_add_port() :-) I'll fix that one as well

Quoted 9 lines
Quoted 6 lines

+  for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
+    if (com_ports[i].mmio &&  com_ports[i].mmio_base == addr) +       return com_names[i];
+
+  p = grub_malloc (sizeof (*p));
+  if (!p)

I prefer "p == NULL" instead of "!p". If you could fix that here and in the other places/patches that will be nice.

Ditto (pre-existing construct).

I'll fix them in the other code too, holler if you object.

Cheers,
Ben.

#6 lore raw

PATCH 4/7 ns8250: Add configuration parameter when adding ports

In reply to Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

This will allow ports to be added with a pre-set configuration

---
 grub-core/term/ns8250.c | 25 +++++++++++++++++++------
 grub-core/term/serial.c |  2 +-
 include/grub/serial.h   |  4 ++--
 3 files changed, 22 insertions(+), 9 deletions(-)
grub-core/term/ns8250.c +19 -6
index b640508d0..08dfb99da 100644
@@ -303,7 +303,7 @@ grub_ns8250_hw_get_port (const unsigned int unit)
 }
 
 char *
-grub_serial_ns8250_add_port (grub_port_t port)
+grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config)
 {
   struct grub_serial_port *p;
   unsigned i;
@@ -312,6 +312,9 @@ grub_serial_ns8250_add_port (grub_port_t port)
       {
 	if (dead_ports & (1 << i))
 	  return NULL;
+	/* give the opportunity for SPCR to configure a default com port */
+	if (config)
+	  grub_serial_port_configure (&com_ports[i], config);
 	return com_names[i];
       }
 
@@ -333,22 +336,29 @@ grub_serial_ns8250_add_port (grub_port_t port)
       return NULL;
     }
   p->driver = &grub_ns8250_driver;
-  grub_serial_config_defaults (p);
   p->mmio = 0;
   p->port = port;
+  if (config)
+    grub_serial_port_configure (p, config);
+  else
+    grub_serial_config_defaults (p);
   grub_serial_register (p);
 
   return p->name;
 }
 
 char *
-grub_serial_ns8250_add_mmio(grub_addr_t addr)
+grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config)
 {
   struct grub_serial_port *p;
   unsigned i;
   for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
-    if (com_ports[i].mmio &&  com_ports[i].mmio_base == addr)
-	return com_names[i];
+    if (com_ports[i].mmio && com_ports[i].mmio_base == addr)
+      {
+        if (config)
+          grub_serial_port_configure (&com_ports[i], config);
+        return com_names[i];
+      }
 
   p = grub_malloc (sizeof (*p));
   if (!p)
@@ -360,9 +370,12 @@ grub_serial_ns8250_add_mmio(grub_addr_t addr)
       return NULL;
     }
   p->driver = &grub_ns8250_driver;
-  grub_serial_config_defaults (p);
   p->mmio = 1;
   p->mmio_base = addr;
+  if (config)
+    grub_serial_port_configure (p, config);
+  else
+    grub_serial_config_defaults (p);
   grub_serial_register (p);
 
   return p->name;
grub-core/term/serial.c +1 -1
index c8f5f02db..a1707d2f7 100644
@@ -156,7 +156,7 @@ grub_serial_find (const char *name)
       && grub_isxdigit (name [sizeof ("port") - 1]))
     {
       name = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
-							0, 16));
+							0, 16), NULL);
       if (!name)
 	return NULL;
 
include/grub/serial.h +2 -2
index ccf464d41..7efc956b0 100644
@@ -185,8 +185,8 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
 void grub_ns8250_init (void);
-char *grub_serial_ns8250_add_port (grub_port_t port);
-char *grub_serial_ns8250_add_mmio(grub_addr_t addr);
+char *grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config);
+char *grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config);
 #endif
 #ifdef GRUB_MACHINE_IEEE1275
 void grub_ofserial_init (void);
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH 4/7] ns8250: Add configuration parameter when adding ports

Quoted 43 lines · Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

This will allow ports to be added with a pre-set configuration

---
 grub-core/term/ns8250.c | 25 +++++++++++++++++++------
 grub-core/term/serial.c |  2 +-
 include/grub/serial.h   |  4 ++--
 3 files changed, 22 insertions(+), 9 deletions(-)
grub-core/term/ns8250.c +6 -2
index b640508d0..08dfb99da 100644
@@ -303,7 +303,7 @@ grub_ns8250_hw_get_port (const unsigned int unit)
 }
 
 char *
-grub_serial_ns8250_add_port (grub_port_t port)
+grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config)
 {
   struct grub_serial_port *p;
   unsigned i;
@@ -312,6 +312,9 @@ grub_serial_ns8250_add_port (grub_port_t port)
       {
 	if (dead_ports & (1 << i))
 	  return NULL;
+	/* give the opportunity for SPCR to configure a default com port */
+	if (config)
+	  grub_serial_port_configure (&com_ports[i], config);
 	return com_names[i];
       }
 
@@ -333,22 +336,29 @@ grub_serial_ns8250_add_port (grub_port_t port)
       return NULL;
     }
   p->driver = &grub_ns8250_driver;
-  grub_serial_config_defaults (p);
   p->mmio = 0;
   p->port = port;
+  if (config)
+    grub_serial_port_configure (p, config);

grub-core/term/ns8250.c grub_serial_ns8250_add_port (grub_port_t port) · line 351

Quoted 10 lines
grub-core/term/ns8250.c +3 -1
+  else
+    grub_serial_config_defaults (p);
   grub_serial_register (p);
 
   return p->name;
 }
 
 char *
-grub_serial_ns8250_add_mmio(grub_addr_t addr)
+grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config)

If you touch the code and its coding style is broken please fix it. Here a space is missing before "(".

Quoted 47 lines
grub-core/term/ns8250.c +10 -3
 {
   struct grub_serial_port *p;
   unsigned i;
   for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
-    if (com_ports[i].mmio &&  com_ports[i].mmio_base == addr)
-	return com_names[i];
+    if (com_ports[i].mmio && com_ports[i].mmio_base == addr)
+      {
+        if (config)
+          grub_serial_port_configure (&com_ports[i], config);
+        return com_names[i];
+      }
 
   p = grub_malloc (sizeof (*p));
   if (!p)
@@ -360,9 +370,12 @@ grub_serial_ns8250_add_mmio(grub_addr_t addr)
       return NULL;
     }
   p->driver = &grub_ns8250_driver;
-  grub_serial_config_defaults (p);
   p->mmio = 1;
   p->mmio_base = addr;
+  if (config)
+    grub_serial_port_configure (p, config);
+  else
+    grub_serial_config_defaults (p);
   grub_serial_register (p);
 
   return p->name;
grub-core/term/serial.c +1 -1
index c8f5f02db..a1707d2f7 100644
@@ -156,7 +156,7 @@ grub_serial_find (const char *name)
       && grub_isxdigit (name [sizeof ("port") - 1]))
     {
       name = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
-							0, 16));
+							0, 16), NULL);
       if (!name)
 	return NULL;
 
include/grub/serial.h +0 -0
index ccf464d41..7efc956b0 100644

include/grub/serial.h grub_serial_config_defaults (struct grub_serial_port *port) · line 189

Quoted 7 lines
include/grub/serial.h +2 -2
@@ -185,8 +185,8 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
 void grub_ns8250_init (void);
-char *grub_serial_ns8250_add_port (grub_port_t port);
-char *grub_serial_ns8250_add_mmio(grub_addr_t addr);
+char *grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config);
+char *grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config);

Again, please do not break coding style...

Daniel

#8 lore raw

PATCH 5/7 ns8250: Use ACPI SPCR table when available to configure serial

In reply to Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

"serial auto" is now equivalent to just "serial" and will use the SPCR to discover the port if present, otherwise defaults to "com0" as before.

This allows to support MMIO ports specified by ACPI which is needed on AWS EC2 "metal" instances, and will enable grub to pickup the port configuration specified by ACPI in other cases.

---
 docs/grub.texi               | 10 ++++-
 grub-core/Makefile.core.def  |  1 +
 grub-core/term/ns8250-spcr.c | 82 ++++++++++++++++++++++++++++++++++++
 grub-core/term/serial.c      | 13 +++++-
 include/grub/serial.h        |  1 +
 5 files changed, 105 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/term/ns8250-spcr.c
docs/grub.texi +9 -1
index 50c811a88..bbebc2aef 100644
@@ -2719,6 +2719,10 @@ you want to use COM2, you must specify @samp{--unit=1} instead. This
 command accepts many other options, so please refer to @ref{serial},
 for more details.
 
+Without argument or with @samp{--port=auto}, GRUB will attempt to use
+ACPI when available to auto-detect the default serial port and its
+configuration.
+
 The commands @command{terminal_input} (@pxref{terminal_input}) and
 @command{terminal_output} (@pxref{terminal_output}) choose which type of
 terminal you want to use. In the case above, the terminal will be a
@@ -2737,7 +2741,6 @@ implements few VT100 escape sequences. If you specify this option then
 GRUB provides you with an alternative menu interface, because the normal
 menu requires several fancy features of your terminal.
 
-
 @node Vendor power-on keys
 @chapter Using GRUB with vendor power-on keys
 
@@ -4172,6 +4175,11 @@ be in the range 5-8 and stop bits must be 1 or 2. Default is 8 data
 bits and one stop bit. @var{parity} is one of @samp{no}, @samp{odd},
 @samp{even} and defaults to @samp{no}.
 
+In passed no @var{unit} nor @var{port}, or if @var{port} is set to
+@samp{auto} then GRUB will attempt to use ACPI to automatically detect
+the system default serial port and its configuration. If this information
+is not available, it will default to @var{unit} 0.
+
 The serial port is not used as a communication channel unless the
 @command{terminal_input} or @command{terminal_output} command is used
 (@pxref{terminal_input}, @pxref{terminal_output}).
grub-core/Makefile.core.def +1 -0
index 95942fc8c..b656bdb44 100644
@@ -2048,6 +2048,7 @@ module = {
   name = serial;
   common = term/serial.c;
   x86 = term/ns8250.c;
+  x86 = term/ns8250-spcr.c;
   ieee1275 = term/ieee1275/serial.c;
   mips_arc = term/arc/serial.c;
   efi = term/efi/serial.c;
grub-core/term/ns8250-spcr.c new file +82 -0

82 lines not on this page · raw message

grub-core/term/serial.c +12 -1
index a1707d2f7..f57fe45ef 100644
@@ -160,6 +160,17 @@ grub_serial_find (const char *name)
       if (!name)
 	return NULL;
 
+      FOR_SERIAL_PORTS (port)
+	if (grub_strcmp (port->name, name) == 0)
+	  break;
+    }
+  if (!port && grub_strcmp (name, "auto") == 0)
+    {
+      /* Look for an SPCR if any. If not, default to com0 */
+      name = grub_ns8250_spcr_init();
+      if (!name)
+        name = "com0";
+
       FOR_SERIAL_PORTS (port)
 	if (grub_strcmp (port->name, name) == 0)
 	  break;
@@ -214,7 +225,7 @@ grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args)
     name = args[0];
 
   if (!name)
-    name = "com0";
+    name = "auto";
 
   port = grub_serial_find (name);
   if (!port)
include/grub/serial.h +1 -0
index 7efc956b0..646bdf1bb 100644
@@ -185,6 +185,7 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
 void grub_ns8250_init (void);
+char * grub_ns8250_spcr_init (void);
 char *grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config);
 char *grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config);
 #endif
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH 5/7] ns8250: Use ACPI SPCR table when available to configure serial

Quoted 8 lines · Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

"serial auto" is now equivalent to just "serial" and will use the SPCR to discover the port if present, otherwise defaults to "com0" as before.

This allows to support MMIO ports specified by ACPI which is needed on AWS EC2 "metal" instances, and will enable grub to pickup the

s/grub/GRUB/

Quoted 28 lines

port configuration specified by ACPI in other cases.

---
 docs/grub.texi               | 10 ++++-
 grub-core/Makefile.core.def  |  1 +
 grub-core/term/ns8250-spcr.c | 82 ++++++++++++++++++++++++++++++++++++
 grub-core/term/serial.c      | 13 +++++-
 include/grub/serial.h        |  1 +
 5 files changed, 105 insertions(+), 2 deletions(-)
 create mode 100644 grub-core/term/ns8250-spcr.c
docs/grub.texi +4 -0
index 50c811a88..bbebc2aef 100644
@@ -2719,6 +2719,10 @@ you want to use COM2, you must specify @samp{--unit=1} instead. This
 command accepts many other options, so please refer to @ref{serial},
 for more details.
 
+Without argument or with @samp{--port=auto}, GRUB will attempt to use
+ACPI when available to auto-detect the default serial port and its
+configuration.
+
 The commands @command{terminal_input} (@pxref{terminal_input}) and
 @command{terminal_output} (@pxref{terminal_output}) choose which type of
 terminal you want to use. In the case above, the terminal will be a

docs/grub.texi implements few VT100 escape sequences. If you specify this option then · line 2740

Quoted 4 lines
docs/grub.texi +0 -1
@@ -2737,7 +2741,6 @@ implements few VT100 escape sequences. If you specify this option then
 GRUB provides you with an alternative menu interface, because the normal
 menu requires several fancy features of your terminal.
 
-

Please drop this change.

docs/grub.texi be in the range 5-8 and stop bits must be 1 or 2. Default is 8 data · line 4178

Quoted 8 lines
docs/grub.texi +1 -0
 @node Vendor power-on keys
 @chapter Using GRUB with vendor power-on keys
 
@@ -4172,6 +4175,11 @@ be in the range 5-8 and stop bits must be 1 or 2. Default is 8 data
 bits and one stop bit. @var{parity} is one of @samp{no}, @samp{odd},
 @samp{even} and defaults to @samp{no}.
 
+In passed no @var{unit} nor @var{port}, or if @var{port} is set to

s/In/If/?

Quoted 25 lines
docs/grub.texi +4 -0
+@samp{auto} then GRUB will attempt to use ACPI to automatically detect
+the system default serial port and its configuration. If this information
+is not available, it will default to @var{unit} 0.
+
 The serial port is not used as a communication channel unless the
 @command{terminal_input} or @command{terminal_output} command is used
 (@pxref{terminal_input}, @pxref{terminal_output}).
grub-core/Makefile.core.def +1 -0
index 95942fc8c..b656bdb44 100644
@@ -2048,6 +2048,7 @@ module = {
   name = serial;
   common = term/serial.c;
   x86 = term/ns8250.c;
+  x86 = term/ns8250-spcr.c;
   ieee1275 = term/ieee1275/serial.c;
   mips_arc = term/arc/serial.c;
   efi = term/efi/serial.c;
grub-core/term/ns8250-spcr.c new file +0 -0
new file mode 100644
index 000000000..0786aee1b

grub-core/term/ns8250-spcr.c line 3

Quoted 3 lines
grub-core/term/ns8250-spcr.c new file +3 -0
@@ -0,0 +1,82 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010  Free Software Foundation, Inc.

s/2000,2001,2002,2003,2004,2005,2007,2008,2009,2010/2022/

Quoted 19 lines
grub-core/term/ns8250-spcr.c new file +19 -0
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/serial.h>
+#include <grub/ns8250.h>
+#include <grub/types.h>
+#include <grub/dl.h>

grub-core/term/ns8250-spcr.c line 32

Quoted 10 lines
grub-core/term/ns8250-spcr.c new file +10 -0
+#include <grub/acpi.h>
+
+char *
+grub_ns8250_spcr_init (void)
+{
+  struct grub_acpi_spcr *spcr;
+  struct grub_serial_config config;
+
+  spcr = grub_acpi_find_table (GRUB_ACPI_SPCR_SIGNATURE);
+  if (!spcr)

spcr == NULL

grub-core/term/ns8250-spcr.c line 33

Quoted 1 line
grub-core/term/ns8250-spcr.c new file +1 -0
+    return 0;

Please use NULL instead of 0 in pointer context. Please fix same problem in the other places/patches too.

grub-core/term/ns8250-spcr.c line 46

Quoted 13 lines
grub-core/term/ns8250-spcr.c new file +13 -0
+  if (spcr->hdr.revision < 2)
+    return 0;
+  if (spcr->intf_type != GRUB_ACPI_SPCR_INTF_TYPE_16550 &&
+      spcr->intf_type != GRUB_ACPI_SPCR_INTF_TYPE_16550X)
+    return 0;
+  /* For now, we only support byte accesses */
+  if (spcr->base_addr.access_size != GRUB_ACPI_GENADDR_SIZE_BYTE &&
+      spcr->base_addr.access_size != GRUB_ACPI_GENADDR_SIZE_LGCY)
+    return 0;
+  config.word_len = 8;
+  config.parity = GRUB_SERIAL_PARITY_NONE;
+  config.stop_bits = GRUB_SERIAL_STOP_BITS_1;
+  config.base_clock = 1843200;

Where this number come from? Please define a constant or at least put a comment before this line.

Quoted 13 lines
grub-core/term/ns8250-spcr.c new file +13 -0
+  if (spcr->flow_control & GRUB_ACPI_SPCR_FC_RTSCTS)
+    config.rtscts = 1;
+  else
+    config.rtscts = 0;
+  switch (spcr->baud_rate)
+    {
+      case GRUB_ACPI_SPCR_BAUD_9600:
+        config.speed = 9600;
+        break;
+      case GRUB_ACPI_SPCR_BAUD_19200:
+        config.speed = 19200;
+        break;
+      case GRUB_ACPI_SPCR_BAUD_57600:

grub-core/term/ns8250-spcr.c line 69

Quoted 10 lines
grub-core/term/ns8250-spcr.c new file +10 -0
+        config.speed = 57600;
+        break;
+      case GRUB_ACPI_SPCR_BAUD_115200:
+        config.speed = 115200;
+        break;
+      case GRUB_ACPI_SPCR_BAUD_CURRENT:
+      default:
+       /* We don't (yet) have a way to read the currently
+        * configured speed in HW, so let's use a sane default
+        */

Please align the comment with GRUB coding style [1].

Quoted 19 lines
grub-core/term/ns8250-spcr.c new file +13 -0
+        config.speed = 115200;
+        break;
+    };
+  switch (spcr->base_addr.space_id)
+    {
+      case GRUB_ACPI_GENADDR_MEM_SPACE:
+        return grub_serial_ns8250_add_mmio(spcr->base_addr.addr, &config);
+      case GRUB_ACPI_GENADDR_IO_SPACE:
+        return grub_serial_ns8250_add_port(spcr->base_addr.addr, &config);
+      default:
+        return 0;
+    };
+}
grub-core/term/serial.c +0 -0
index a1707d2f7..f57fe45ef 100644
@@ -160,6 +160,17 @@ grub_serial_find (const char *name)
       if (!name)

grub-core/term/serial.c grub_serial_find (const char *name) · line 170

Quoted 10 lines
grub-core/term/serial.c +8 -0
 	return NULL;
 
+      FOR_SERIAL_PORTS (port)
+	if (grub_strcmp (port->name, name) == 0)
+	  break;
+    }
+  if (!port && grub_strcmp (name, "auto") == 0)
+    {
+      /* Look for an SPCR if any. If not, default to com0 */
+      name = grub_ns8250_spcr_init();

Missing space before "(".

Quoted 20 lines
grub-core/term/serial.c +4 -1
+      if (!name)
+        name = "com0";
+
       FOR_SERIAL_PORTS (port)
 	if (grub_strcmp (port->name, name) == 0)
 	  break;
@@ -214,7 +225,7 @@ grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args)
     name = args[0];
 
   if (!name)
-    name = "com0";
+    name = "auto";
 
   port = grub_serial_find (name);
   if (!port)
include/grub/serial.h +0 -0
index 7efc956b0..646bdf1bb 100644

include/grub/serial.h grub_serial_config_defaults (struct grub_serial_port *port) · line 188

Quoted 4 lines
include/grub/serial.h +1 -0
@@ -185,6 +185,7 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
 void grub_ns8250_init (void);
+char * grub_ns8250_spcr_init (void);

Please drop space after "*".

include/grub/serial.h grub_serial_config_defaults (struct grub_serial_port *port) · line 191

Quoted 3 lines
include/grub/serial.h +0 -0
 char *grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config);
 char *grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config);
 #endif

Daniel

[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments

#10 lore raw

PATCH 6/7 ns8250: Support more MMIO access sizes

In reply to Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

It is common for PCI based UARTs to use larger than one byte access sizes. This adds support for this and uses the information present in SPCR accordingly.

---
 grub-core/term/ns8250-spcr.c |  3 ++-
 grub-core/term/ns8250.c      | 45 +++++++++++++++++++++++++++++++++---
 include/grub/serial.h        |  9 ++++++--
 3 files changed, 51 insertions(+), 6 deletions(-)
grub-core/term/ns8250-spcr.c +2 -1
index 0786aee1b..dd589af60 100644
@@ -73,7 +73,8 @@ grub_ns8250_spcr_init (void)
   switch (spcr->base_addr.space_id)
     {
       case GRUB_ACPI_GENADDR_MEM_SPACE:
-        return grub_serial_ns8250_add_mmio(spcr->base_addr.addr, &config);
+        return grub_serial_ns8250_add_mmio(spcr->base_addr.addr,
+                                           spcr->base_addr.access_size, &config);
       case GRUB_ACPI_GENADDR_IO_SPACE:
         return grub_serial_ns8250_add_port(spcr->base_addr.addr, &config);
       default:
grub-core/term/ns8250.c +42 -3

81 lines not on this page · raw message

include/grub/serial.h +7 -2
index 646bdf1bb..8875eaf82 100644
@@ -94,7 +94,12 @@ struct grub_serial_port
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
         grub_port_t port;
 #endif
-        grub_addr_t mmio_base;
+        struct
+        {
+          grub_addr_t mmio_base;
+          /* Access size uses ACPI definition */
+          unsigned int access_size;
+        };
       };
     };
     struct
@@ -187,7 +192,7 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 void grub_ns8250_init (void);
 char * grub_ns8250_spcr_init (void);
 char *grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config);
-char *grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config);
+char *grub_serial_ns8250_add_mmio(grub_addr_t addr, unsigned int acc_size, struct grub_serial_config *config);
 #endif
 #ifdef GRUB_MACHINE_IEEE1275
 void grub_ofserial_init (void);
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH 6/7] ns8250: Support more MMIO access sizes

Quoted 35 lines · Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

It is common for PCI based UARTs to use larger than one byte access sizes. This adds support for this and uses the information present in SPCR accordingly.

---
 grub-core/term/ns8250-spcr.c |  3 ++-
 grub-core/term/ns8250.c      | 45 +++++++++++++++++++++++++++++++++---
 include/grub/serial.h        |  9 ++++++--
 3 files changed, 51 insertions(+), 6 deletions(-)
grub-core/term/ns8250-spcr.c +2 -1
index 0786aee1b..dd589af60 100644
@@ -73,7 +73,8 @@ grub_ns8250_spcr_init (void)
   switch (spcr->base_addr.space_id)
     {
       case GRUB_ACPI_GENADDR_MEM_SPACE:
-        return grub_serial_ns8250_add_mmio(spcr->base_addr.addr, &config);
+        return grub_serial_ns8250_add_mmio(spcr->base_addr.addr,
+                                           spcr->base_addr.access_size, &config);
       case GRUB_ACPI_GENADDR_IO_SPACE:
         return grub_serial_ns8250_add_port(spcr->base_addr.addr, &config);
       default:
grub-core/term/ns8250.c +0 -0
index 08dfb99da..76f68c037 100644
@@ -48,7 +48,26 @@ ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg)
 {
   asm volatile("" : : : "memory");
   if (port->mmio)

grub-core/term/ns8250.c ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg) · line 59

Quoted 10 lines
grub-core/term/ns8250.c +9 -1
-    return *((volatile unsigned char *)(port->mmio_base + reg));
+    {
+      /* Note: we assume MMIO UARTs are little endian. This is not true of all
+       * embedded platforms but will do for now
+       */
+      switch(port->access_size)
+        {
+        default:
+        case 1:
+          return *((volatile unsigned char  *)(port->mmio_base + reg));

s/unsigned char/grub_uint8_t/a

And now I think ns8250_reg_read()/ns8250_reg_write() in patch #3 probably should use grub_uint8_t instead of "unsigned char" everywhere...

grub-core/term/ns8250.c ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg) · line 61

Quoted 2 lines
grub-core/term/ns8250.c +2 -0
+        case 2:
+          return grub_le_to_cpu16(*((volatile unsigned short *)(port->mmio_base + (reg << 1))));

s/unsigned short/grub_uint16_t/

grub-core/term/ns8250.c ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg) · line 63

Quoted 2 lines
grub-core/term/ns8250.c +2 -0
+        case 3:
+          return grub_le_to_cpu32(*((volatile unsigned long *)(port->mmio_base + (reg << 2))));

s/unsigned long/grub_uint32_t/

I do not mention cast coding style issue...

grub-core/term/ns8250.c ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg) · line 68

Quoted 5 lines
grub-core/term/ns8250.c +5 -0
+        case 4:
+          /* This will only work properly on 64-bit systems, thankfully it
+           * also probably doesn't exist
+           */
+          return grub_le_to_cpu64(*((volatile unsigned long long *)(port->mmio_base + (reg << 3))));

s/unsigned long long/grub_uint64_t/

Then maybe comment after "case 4" can be dropped...

Quoted 6 lines
grub-core/term/ns8250.c +2 -0
+        }
+    }
   return grub_inb (port->port + reg);
 }
 

grub-core/term/ns8250.c ns8250_reg_write (struct grub_serial_port *port, unsigned char value, grub_addr_ · line 84

Quoted 10 lines
grub-core/term/ns8250.c +6 -1
@@ -57,7 +76,24 @@ ns8250_reg_write (struct grub_serial_port *port, unsigned char value, grub_addr_
 {
   asm volatile("" : : : "memory");
   if (port->mmio)
-    *((volatile char *)(port->mmio_base + reg)) = value;
+    {
+      switch(port->access_size)
+        {
+        default:
+        case 1:
+          *((volatile unsigned char *)(port->mmio_base + reg)) = value;

s/unsigned char/grub_uint8_t/a

And please fix the similar problems accordingly in this and other patches...

Quoted 53 lines
grub-core/term/ns8250.c +16 -1
+          break;
+        case 2:
+          *((volatile unsigned short *)(port->mmio_base + (reg << 1))) = grub_cpu_to_le16(value);
+          break;
+        case 3:
+          *((volatile unsigned long *)(port->mmio_base + (reg << 2))) = grub_cpu_to_le32(value);
+          break;
+        case 4:
+          *((volatile unsigned long long *)(port->mmio_base + (reg << 3))) = grub_cpu_to_le64(value);
+          break;
+        }
+    }
   else
     grub_outb (value, port->port + reg);
 }
@@ -285,6 +321,7 @@ grub_ns8250_init (void)
 	  grub_print_error ();
 
 	grub_serial_register (&com_ports[i]);
+	com_ports[i].access_size = 1;
       }
 }
 
@@ -338,6 +375,7 @@ grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config
   p->driver = &grub_ns8250_driver;
   p->mmio = 0;
   p->port = port;
+  p->access_size = 1;
   if (config)
     grub_serial_port_configure (p, config);
   else
@@ -348,7 +386,7 @@ grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config
 }
 
 char *
-grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config)
+grub_serial_ns8250_add_mmio(grub_addr_t addr, unsigned int acc_size, struct grub_serial_config *config)
 {
   struct grub_serial_port *p;
   unsigned i;
@@ -372,6 +410,7 @@ grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config)
   p->driver = &grub_ns8250_driver;
   p->mmio = 1;
   p->mmio_base = addr;
+  p->access_size = acc_size;
   if (config)
     grub_serial_port_configure (p, config);
   else
include/grub/serial.h +0 -0
index 646bdf1bb..8875eaf82 100644

include/grub/serial.h struct grub_serial_port · line 101

Quoted 9 lines
include/grub/serial.h +5 -1
@@ -94,7 +94,12 @@ struct grub_serial_port
 #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
         grub_port_t port;
 #endif
-        grub_addr_t mmio_base;
+        struct
+        {
+          grub_addr_t mmio_base;
+          /* Access size uses ACPI definition */
+          unsigned int access_size;

Is this 32-bit value? If yes then s/unsigned int/grub_uint32_t/...

include/grub/serial.h grub_serial_config_defaults (struct grub_serial_port *port) · line 195

Quoted 10 lines
include/grub/serial.h +2 -1
+        };
       };
     };
     struct
@@ -187,7 +192,7 @@ grub_serial_config_defaults (struct grub_serial_port *port)
 void grub_ns8250_init (void);
 char * grub_ns8250_spcr_init (void);
 char *grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config);
-char *grub_serial_ns8250_add_mmio(grub_addr_t addr, struct grub_serial_config *config);
+char *grub_serial_ns8250_add_mmio(grub_addr_t addr, unsigned int acc_size, struct grub_serial_config *config);

Please fix coding style...

Daniel

#12 lore raw

PATCH 7/7 serial: Add ability to specify MMIO ports via 'serial'

In reply to Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

This adds the ability to explicitely add an MMIO based serial port via the 'serial' command. The syntax is:

serial --port=mmio,<hex_address>{.b,.w,.l,.q}

---
 docs/grub.texi          | 26 ++++++++++++++++++++++++--
 grub-core/term/serial.c | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 54 insertions(+), 3 deletions(-)
docs/grub.texi +24 -2
index bbebc2aef..a25235636 100644
@@ -4167,8 +4167,24 @@ Commands usable anywhere in the menu and in the command-line.
 @deffn Command serial [@option{--unit=unit}] [@option{--port=port}] [@option{--speed=speed}] [@option{--word=word}] [@option{--parity=parity}] [@option{--stop=stop}]
 Initialize a serial device. @var{unit} is a number in the range 0-3
 specifying which serial port to use; default is 0, which corresponds to
-the port often called COM1. @var{port} is the I/O port where the UART
-is to be found; if specified it takes precedence over @var{unit}.
+the port often called COM1.
+
+@var{port} is the I/O port where the UART is to be found or, if prefixed
+with @samp{mmio,}, the MMIO address of the UART. If specified it takes
+precedence over @var{unit}.
+
+Additionally, an MMIO address can be suffixed with:
+@itemize @bullet
+@item
+@samp{.b} for bytes access (default)
+@item
+@samp{.w} for 16-bit word access
+@item
+@samp{.l} for 32-bit long word access or
+@item
+@samp{.q} for 64-bit long long word access
+@end itemize
+
 @var{speed} is the transmission speed; default is 9600. @var{word} and
 @var{stop} are the number of data bits and stop bits. Data bits must
 be in the range 5-8 and stop bits must be 1 or 2. Default is 8 data
@@ -4184,6 +4200,12 @@ The serial port is not used as a communication channel unless the
 @command{terminal_input} or @command{terminal_output} command is used
 (@pxref{terminal_input}, @pxref{terminal_output}).
 
+Examples:
+@example
+serial --port=3f8 --speed=9600
+serial --port=mmio,fefb0000.l --speed=115200
+@end example
+
 See also @ref{Serial terminal}.
 @end deffn
 
grub-core/term/serial.c +30 -1
index f57fe45ef..96a5ad725 100644
@@ -164,6 +164,35 @@ grub_serial_find (const char *name)
 	if (grub_strcmp (port->name, name) == 0)
 	  break;
     }
+  if (!port && grub_memcmp (name, "mmio,", sizeof ("mmio,") - 1) == 0
+      && grub_isxdigit (name [sizeof ("mmio,") - 1]))
+    {
+      const char *p1, *p = &name[sizeof ("mmio,") - 1];
+      grub_addr_t addr = grub_strtoul (p, &p1, 16);
+      unsigned int acc_size = 1;
+      unsigned int nlen = p1 - p;
+      if (p1[0] == '.')
+        switch(p1[1])
+	  {
+	  case 'w':
+	     acc_size = 2;
+	     break;
+	  case 'l':
+	     acc_size = 3;
+	     break;
+	  case 'q':
+	     acc_size = 4;
+	     break;
+          }
+      name = grub_serial_ns8250_add_mmio (addr, acc_size, NULL);
+      if (!name)
+	return NULL;
+
+      FOR_SERIAL_PORTS (port)
+        if (grub_strncmp (port->name, name, nlen) == 0) {
+	  break;
+	}
+    }
   if (!port && grub_strcmp (name, "auto") == 0)
     {
       /* Look for an SPCR if any. If not, default to com0 */
@@ -212,7 +241,7 @@ grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args)
 
   if (state[OPTION_PORT].set)
     {
-      if (grub_memcmp (state[OPTION_PORT].arg, "mmio", 4) == 0)
+      if (grub_memcmp (state[OPTION_PORT].arg, "mmio,", 5) == 0)
           grub_snprintf(pname, sizeof (pname), "%s", state[1].arg);
       else
           grub_snprintf (pname, sizeof (pname), "port%lx",
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH 7/7] serial: Add ability to specify MMIO ports via 'serial'

Quoted 62 lines · Benjamin Herrenschmidt

From: Benjamin Herrenschmidt <[email protected]>

This adds the ability to explicitely add an MMIO based serial port via the 'serial' command. The syntax is:

serial --port=mmio,<hex_address>{.b,.w,.l,.q}

---
 docs/grub.texi          | 26 ++++++++++++++++++++++++--
 grub-core/term/serial.c | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 54 insertions(+), 3 deletions(-)
docs/grub.texi +24 -2
index bbebc2aef..a25235636 100644
@@ -4167,8 +4167,24 @@ Commands usable anywhere in the menu and in the command-line.
 @deffn Command serial [@option{--unit=unit}] [@option{--port=port}] [@option{--speed=speed}] [@option{--word=word}] [@option{--parity=parity}] [@option{--stop=stop}]
 Initialize a serial device. @var{unit} is a number in the range 0-3
 specifying which serial port to use; default is 0, which corresponds to
-the port often called COM1. @var{port} is the I/O port where the UART
-is to be found; if specified it takes precedence over @var{unit}.
+the port often called COM1.
+
+@var{port} is the I/O port where the UART is to be found or, if prefixed
+with @samp{mmio,}, the MMIO address of the UART. If specified it takes
+precedence over @var{unit}.
+
+Additionally, an MMIO address can be suffixed with:
+@itemize @bullet
+@item
+@samp{.b} for bytes access (default)
+@item
+@samp{.w} for 16-bit word access
+@item
+@samp{.l} for 32-bit long word access or
+@item
+@samp{.q} for 64-bit long long word access
+@end itemize
+
 @var{speed} is the transmission speed; default is 9600. @var{word} and
 @var{stop} are the number of data bits and stop bits. Data bits must
 be in the range 5-8 and stop bits must be 1 or 2. Default is 8 data
@@ -4184,6 +4200,12 @@ The serial port is not used as a communication channel unless the
 @command{terminal_input} or @command{terminal_output} command is used
 (@pxref{terminal_input}, @pxref{terminal_output}).
 
+Examples:
+@example
+serial --port=3f8 --speed=9600
+serial --port=mmio,fefb0000.l --speed=115200
+@end example
+
 See also @ref{Serial terminal}.
 @end deffn
 
grub-core/term/serial.c +0 -0
index f57fe45ef..96a5ad725 100644

grub-core/term/serial.c grub_serial_find (const char *name) · line 167

Quoted 4 lines
grub-core/term/serial.c +1 -0
@@ -164,6 +164,35 @@ grub_serial_find (const char *name)
 	if (grub_strcmp (port->name, name) == 0)
 	  break;
     }
+  if (!port && grub_memcmp (name, "mmio,", sizeof ("mmio,") - 1) == 0

I think grub_strncmp() in string context would be more appropriate. Please replace grub_memcmp() with grub_strncmp() where possible.

grub-core/term/serial.c grub_serial_find (const char *name) · line 171

Quoted 4 lines
grub-core/term/serial.c +4 -0
+      && grub_isxdigit (name [sizeof ("mmio,") - 1]))
+    {
+      const char *p1, *p = &name[sizeof ("mmio,") - 1];
+      grub_addr_t addr = grub_strtoul (p, &p1, 16);

You blindly assume grub_strtoul() will not fail. It is not true. Please take a look at commit ac8a37dda (net/http: Allow use of non-standard TCP/IP ports) how properly detect grub_strtoul() failures.

grub-core/term/serial.c grub_serial_find (const char *name) · line 173

Quoted 2 lines
grub-core/term/serial.c +2 -0
+      unsigned int acc_size = 1;
+      unsigned int nlen = p1 - p;

Please add empty line here.

grub-core/term/serial.c grub_serial_find (const char *name) · line 185

Quoted 12 lines
grub-core/term/serial.c +12 -0
+      if (p1[0] == '.')
+        switch(p1[1])
+	  {
+	  case 'w':
+	     acc_size = 2;
+	     break;
+	  case 'l':
+	     acc_size = 3;
+	     break;
+	  case 'q':
+	     acc_size = 4;
+	     break;

Does not compiler complain there is no default here? I think I saw such warnings somewhere when it is missing.

Quoted 14 lines
grub-core/term/serial.c +10 -0
+          }
+      name = grub_serial_ns8250_add_mmio (addr, acc_size, NULL);
+      if (!name)
+	return NULL;
+
+      FOR_SERIAL_PORTS (port)
+        if (grub_strncmp (port->name, name, nlen) == 0) {
+	  break;
+	}
+    }
   if (!port && grub_strcmp (name, "auto") == 0)
     {
       /* Look for an SPCR if any. If not, default to com0 */

grub-core/term/serial.c grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args) · line 244

Quoted 5 lines
grub-core/term/serial.c +1 -1
@@ -212,7 +241,7 @@ grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args)
 
   if (state[OPTION_PORT].set)
     {
-      if (grub_memcmp (state[OPTION_PORT].arg, "mmio", 4) == 0)
+      if (grub_memcmp (state[OPTION_PORT].arg, "mmio,", 5) == 0)

s/grub_memcmp/grub_strncmp/

Daniel

#14 lore raw

Re: [PATCH 7/7] serial: Add ability to specify MMIO ports via 'serial'

Quoted 4 lines · Daniel Kiper
Quoted 1 line

+  if (!port && grub_memcmp (name, "mmio,", sizeof ("mmio,") - 1) == 0

I think grub_strncmp() in string context would be more appropriate. Please replace grub_memcmp() with grub_strncmp() where possible.

Same comment as patch 3, this is a pre-existing construct (look a few lines above in the original code:

  if (!port && grub_memcmp (name, "port", sizeof ("port") - 1) == 0
      && grub_isxdigit (name [sizeof ("port") - 1]))
    {

I'll fix them all in the respun patch.

Quoted 8 lines
Quoted 4 lines

+      && grub_isxdigit (name [sizeof ("mmio,") - 1])) +    {
+      const char *p1, *p = &name[sizeof ("mmio,") - 1]; +      grub_addr_t addr = grub_strtoul (p, &p1, 16);

You blindly assume grub_strtoul() will not fail. It is not true. Please take a look at commit ac8a37dda (net/http: Allow use of non-standard TCP/IP ports) how properly detect grub_strtoul() failures.

Indeed, that's not great, I'll fix.

Quoted 18 lines
Quoted 2 lines

+      unsigned int acc_size = 1;
+      unsigned int nlen = p1 - p;

Please add empty line here.

Quoted 4 lines

+      if (p1[0] == '.')
+        switch(p1[1])
+         {
+         case 'w':

Quoted 8 lines

+            acc_size = 2;
+            break;
+         case 'l':
+            acc_size = 3;
+            break;
+         case 'q':
+            acc_size = 4;
+            break;

Quoted 2 lines

Does not compiler complain there is no default here? I think I saw such warnings somewhere when it is missing.

I'm pretty sure it didn't but it might depend on a specific gcc version, I'll fix.

Cheers,
Ben.

#15 lore raw
In reply to Benjamin Herrenschmidt
Quoted 13 lines · Benjamin Herrenschmidt

This series adds support for MMIO serial ports and auto-configuration via ACPI SPCR.

This is necessary for the serial port to work on AWS EC2 "metal" x86 systems.

An MMIO serial port can be specified explicitely using the "mmio," prefix for the --port argument to the 'serial' command, the register access size can also be specified via a suffix, and when 'serial' is used without arguments, it will try to use an ACPI SPCR entry (if any) to add the default port and configure it appropriately (speed, parity etc...)

Quoted 3 lines

This was tested using SPCR on an actual c5.metal instance, and using explicit instanciation via serial -p mmioXXXXXXXX on a modified qemu hacked to create MMIO PCI serial ports.

First of all sorry for very late reply. I sent you my comments. There are some important problems plus some nitpicks which have to be fixed. Though I like the patch set in general. Than you for doing this work.

Daniel

#16 lore raw
Quoted 5 lines · Daniel Kiper

First of all sorry for very late reply. I sent you my comments. There are some important problems plus some nitpicks which have to be fixed.
Though I like the patch set in general. Than you for doing this work.

Thanks ! I'll try to address all of them asap.

Cheers,
Ben.