All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: grub-devel@gnu.org
Subject: [PATCH] term/serial: Add support for PCI serial devices
Date: Wed, 24 Aug 2022 13:28:03 +0200	[thread overview]
Message-ID: <YwYLQ0YUNq387CzN@worktop.programming.kicks-ass.net> (raw)


Loosely based on early_pci_serial_init() from Linux, allow GRUB to make
use of PCI serial devices.

Specifically, my Alderlake NUC exposes the Intel AMT SoL UART as a PCI
enumerated device but doesn't include it in the EFI tables.

Tested and confirmed working on a "Lenovo P360 Tiny" with Intel AMT
enabled. This specific machine has (from lspci -vv):

00:16.3 Serial controller: Intel Corporation Device 7aeb (rev 11) (prog-if 02 [16550])
        DeviceName: Onboard - Other
        Subsystem: Lenovo Device 330e
        Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx-
        Status: Cap+ 66MHz+ UDF- FastB2B+ ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx-
        Interrupt: pin D routed to IRQ 19
        Region 0: I/O ports at 40a0 [size=8]
        Region 1: Memory at b4224000 (32-bit, non-prefetchable) [size=4K]
        Capabilities: [40] MSI: Enable- Count=1/1 Maskable- 64bit+
                Address: 0000000000000000  Data: 0000
        Capabilities: [50] Power Management version 3
                Flags: PMEClk- DSI+ D1- D2- AuxCurrent=0mA PME(D0-,D1-,D2-,D3hot-,D3cold-)
                Status: D0 NoSoftRst+ PME-Enable- DSel=0 DScale=0 PME-
        Kernel driver in use: serial

From which the following config (/etc/default/grub) gets a working
serial setup:

GRUB_CMDLINE_LINUX="console=tty0 earlyprintk=pciserial,00:16.3,115200 console=ttyS0,115200"
GRUB_SERIAL_COMMAND="serial --port=0x40a0 --speed=115200"
GRUB_TERMINAL="serial console"

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 grub-core/term/serial.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/grub/pci.h      |    3 ++
 include/grub/serial.h   |    2 +
 3 files changed, 64 insertions(+)

Index: grub2-2.06/grub-core/term/serial.c
===================================================================
--- grub2-2.06.orig/grub-core/term/serial.c
+++ grub2-2.06/grub-core/term/serial.c
@@ -34,6 +34,7 @@
 #ifdef GRUB_MACHINE_IEEE1275
 #include <grub/ieee1275/console.h>
 #endif
+#include <grub/pci.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -448,6 +449,7 @@ GRUB_MOD_INIT(serial)
 #ifdef GRUB_MACHINE_ARC
   grub_arcserial_init ();
 #endif
+  grub_pciserial_init ();
 }
 
 GRUB_MOD_FINI(serial)
@@ -461,3 +463,60 @@ GRUB_MOD_FINI(serial)
     }
   grub_unregister_extcmd (cmd);
 }
+
+#define GRUB_SERIAL_PORT_NUM 4
+static char com_names[GRUB_SERIAL_PORT_NUM][20];
+static struct grub_serial_port com_ports[GRUB_SERIAL_PORT_NUM];
+static int ports = 0;
+
+static int
+find_pciserial (grub_pci_device_t dev, grub_pci_id_t pciid, void *data)
+{
+  grub_pci_address_t cmd_addr, class_addr, bar_addr;
+  grub_uint32_t class, bar;
+  grub_uint16_t cmdreg;
+  int err, i = ports;
+
+  (void)data;
+  (void)pciid;
+
+  cmd_addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
+  cmdreg = grub_pci_read (cmd_addr);
+
+  class_addr = grub_pci_make_address (dev, GRUB_PCI_REG_REVISION);
+  class = grub_pci_read (class_addr);
+
+  bar_addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
+  bar = grub_pci_read (bar_addr);
+
+  /* MODEM, SERIAL or MAGIC */
+  if (((class >> 16) != GRUB_PCI_CLASS_COMMUNICATION_MODEM &&
+       (class >> 16) != GRUB_PCI_CLASS_COMMUNICATION_SERIAL) ||
+      ((class >> 8) & 0xff) != 0x02)
+	  return 0;
+
+  if (!(bar & 1)) /* Not I/O */
+	  return 0;
+
+  grub_pci_write (cmd_addr, cmdreg | GRUB_PCI_COMMAND_IO_ENABLED);
+
+  grub_snprintf (com_names[i], sizeof (com_names[i]), "pci%d", i);
+  com_ports[i].name = com_names[i];
+  com_ports[i].driver = &grub_ns8250_driver;
+  com_ports[i].port = bar & 0xfffffffc;
+  err = grub_serial_config_defaults (&com_ports[i]);
+  if (err) {
+	  grub_print_error();
+	  return 0;
+  }
+
+  grub_serial_register (&com_ports[i]);
+
+  return ++ports >= GRUB_SERIAL_PORT_NUM;
+}
+
+void
+grub_pciserial_init (void)
+{
+	grub_pci_iterate (find_pciserial, NULL);
+}
Index: grub2-2.06/include/grub/serial.h
===================================================================
--- grub2-2.06.orig/include/grub/serial.h
+++ grub2-2.06/include/grub/serial.h
@@ -193,6 +193,8 @@ const char *
 grub_arcserial_add_port (const char *path);
 #endif
 
+void grub_pciserial_init (void);
+
 struct grub_serial_port *grub_serial_find (const char *name);
 extern struct grub_serial_driver grub_ns8250_driver;
 void EXPORT_FUNC(grub_serial_unregister_driver) (struct grub_serial_driver *driver);
Index: grub2-2.06/include/grub/pci.h
===================================================================
--- grub2-2.06.orig/include/grub/pci.h
+++ grub2-2.06/include/grub/pci.h
@@ -83,6 +83,9 @@
 #define  GRUB_PCI_CLASS_SUBCLASS_VGA  0x0300
 #define  GRUB_PCI_CLASS_SUBCLASS_USB  0x0c03
 
+#define  GRUB_PCI_CLASS_COMMUNICATION_SERIAL  0x0700
+#define  GRUB_PCI_CLASS_COMMUNICATION_MODEM   0x0703
+
 #ifndef ASM_FILE
 
 enum



             reply	other threads:[~2022-08-24 15:28 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-24 11:28 Peter Zijlstra [this message]
2022-08-24 20:13 ` [PATCH] term/serial: Add support for PCI serial devices Glenn Washburn
2022-08-24 21:01   ` Peter Zijlstra
2022-08-24 23:36     ` Glenn Washburn
2022-08-25  7:36       ` Peter Zijlstra
2022-08-25  7:57       ` Peter Zijlstra
2022-08-25 15:51         ` Glenn Washburn
2022-08-25 19:33           ` Peter Zijlstra
2022-08-26  1:01             ` Glenn Washburn
2022-08-25  8:10       ` Peter Zijlstra
2022-08-25 19:23         ` Glenn Washburn
2022-08-25 19:59           ` Peter Zijlstra
2022-08-26  4:02             ` Glenn Washburn
2022-08-25  8:17   ` Peter Zijlstra
2022-08-25 15:46     ` Glenn Washburn
2022-08-25  8:03 ` Gerd Hoffmann

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=YwYLQ0YUNq387CzN@worktop.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=grub-devel@gnu.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.