term/serial: Add support for PCI serial devices

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"

Documentation is added to note that serial devices found on the PCI bus will
be exposed as "pci,XX:XX.X" and how to find serial terminal logical names.
Also, some minor documentation improvements were added.

This can be tested in QEMU by adding a pci-serial device, e.g. using the option
"-device pci-serial".

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Tested-by: Glenn Washburn <development@efficientek.com>
Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Peter Zijlstra (Intel) authored and Daniel Kiper committed 2023-05-25 16:46:37 +02:00
1 parent 3ee57bd277
commit 8eb3d4df3f
6 files changed
+116 -4

No files matched your search

+91
View File
@@ -0,0 +1,91 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2023 Free Software Foundation, Inc.
*
* 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/term.h>
#include <grub/types.h>
#include <grub/pci.h>
#include <grub/mm.h>
#include <grub/misc.h>
static int
find_pciserial (grub_pci_device_t dev, grub_pci_id_t pciid __attribute__ ((unused)), void *data __attribute__ ((unused)))
{
grub_pci_address_t cmd_addr, class_addr, bar_addr;
struct grub_serial_port *port;
grub_uint32_t class, bar;
grub_uint16_t cmdreg;
grub_err_t err;
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);
/* 16550 compatible MODEM or SERIAL. */
if (((class >> 16) != GRUB_PCI_CLASS_COMMUNICATION_MODEM &&
(class >> 16) != GRUB_PCI_CLASS_COMMUNICATION_SERIAL) ||
((class >> 8) & 0xff) != GRUB_PCI_SERIAL_16550_COMPATIBLE)
return 0;
if ((bar & GRUB_PCI_ADDR_SPACE_MASK) != GRUB_PCI_ADDR_SPACE_IO)
return 0;
port = grub_zalloc (sizeof (*port));
if (port == NULL)
return 0;
port->name = grub_xasprintf ("pci,%02x:%02x.%x",
grub_pci_get_bus (dev),
grub_pci_get_device (dev),
grub_pci_get_function (dev));
if (port->name == NULL)
goto fail;
grub_pci_write (cmd_addr, cmdreg | GRUB_PCI_COMMAND_IO_ENABLED);
port->driver = &grub_ns8250_driver;
port->port = bar & GRUB_PCI_ADDR_IO_MASK;
err = grub_serial_config_defaults (port);
if (err != GRUB_ERR_NONE)
{
grub_print_error ();
goto fail;
}
err = grub_serial_register (port);
if (err != GRUB_ERR_NONE)
goto fail;
return 0;
fail:
grub_free (port->name);
grub_free (port);
return 0;
}
void
grub_pciserial_init (void)
{
grub_pci_iterate (find_pciserial, NULL);
}