Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oRXK4-0006qI-VJ for mharc-grub-devel@gnu.org; Fri, 26 Aug 2022 07:16:24 -0400 Received: from eggs.gnu.org ([209.51.188.92]:34912) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oRXK2-0006lQ-Lm for grub-devel@gnu.org; Fri, 26 Aug 2022 07:16:22 -0400 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]:50490) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oRXJy-00067E-Rn for grub-devel@gnu.org; Fri, 26 Aug 2022 07:16:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:From:Date:Message-ID:Sender:Reply-To:To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=4FNevoK2mWmXjh5moarPDoag0qEcSXjsjRRK4MIN2Cs=; b=SaKIx1x/GyOtlBNTWsKb4TGofV n/IQdBdyTfkZad8TnakpgnMJSs/MO88rt8P1dtju+9dGgaGObtBClHC2/1OW//41sjbS2ckX5bvnX x2XbRBR2gkrYXI52l62pvsYGYnqWyqGyQ4GHZaiIBI3kzKeEx6L/1Ry1ic4vv/0M0N92Bk/BgLn29 W3NDesq5ZIuzwbAf/noPskkUytnY+rEnTKFQNfQCROtaknWuDhv7nYOTzZZYOyJN9B0nPnbBgmGtU G2rAWYmOt7P3PRHE6Uquu+O4Ka6Nbiox91GWsfFLdDmLs74iudAp0oJVxk+ZxBABj+QqFGyN4H50I mtpGV+Xg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1oRXJv-000UZC-Jw; Fri, 26 Aug 2022 11:16:15 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id 49DBF3007A2; Fri, 26 Aug 2022 13:16:13 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 0) id 230872016C580; Fri, 26 Aug 2022 13:16:13 +0200 (CEST) Message-ID: <20220826111358.334758397@alderlake.programming.kicks-ass.net> User-Agent: quilt/0.66 Date: Fri, 26 Aug 2022 13:01:44 +0200 From: peterz@infradead.org Cc: grub-devel@gnu.org, development@efficientek.com, dkiper@net-space.pl, peterz@infradead.org Subject: [PATCH v2 2/3] term/serial: Add support for PCI serial devices References: <20220826110142.966628595@alderlake.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Received-SPF: none client-ip=2001:8b0:10b:1236::1; envelope-from=peterz@infradead.org; helo=casper.infradead.org X-Spam_score_int: -33 X-Spam_score: -3.4 X-Spam_bar: --- X-Spam_report: (-3.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MISSING_HEADERS=1.021, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Aug 2022 11:16:23 -0000 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- SERR- --- Index: grub/grub-core/Makefile.core.def =================================================================== --- grub.orig/grub-core/Makefile.core.def +++ grub/grub-core/Makefile.core.def @@ -2047,6 +2047,7 @@ module = { ieee1275 = term/ieee1275/serial.c; mips_arc = term/arc/serial.c; efi = term/efi/serial.c; + pci = term/pci/serial.c; enable = terminfomodule; enable = ieee1275; Index: grub/grub-core/term/pci/serial.c =================================================================== --- /dev/null +++ grub/grub-core/term/pci/serial.c @@ -0,0 +1,95 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2022 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 . + */ + +#include +#include +#include +#include +#include +#include + +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; + struct grub_serial_port *port; + grub_uint32_t class, bar; + grub_uint16_t cmdreg; + int *port_num = data; + grub_err_t err; + + (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); + + /* 16550 compattible 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%d", (*port_num)); + if (port->name == NULL) + goto error; + + 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 error; + } + + err = grub_serial_register (port); + if (err != GRUB_ERR_NONE) + goto error; + + (*port_num)++; + + return 0; + +error: + grub_free (port->name); + grub_free (port); + return 0; +} + +void +grub_pciserial_init (void) +{ + int port_num; + + grub_pci_iterate (find_pciserial, &port_num); +} Index: grub/grub-core/term/serial.c =================================================================== --- grub.orig/grub-core/term/serial.c +++ grub/grub-core/term/serial.c @@ -448,6 +448,9 @@ GRUB_MOD_INIT(serial) #ifdef GRUB_MACHINE_ARC grub_arcserial_init (); #endif +#ifdef GRUB_HAS_PCI + grub_pciserial_init (); +#endif } GRUB_MOD_FINI(serial) Index: grub/include/grub/pci.h =================================================================== --- grub.orig/include/grub/pci.h +++ grub/include/grub/pci.h @@ -80,8 +80,13 @@ #define GRUB_PCI_STATUS_DEVSEL_TIMING_SHIFT 9 #define GRUB_PCI_STATUS_DEVSEL_TIMING_MASK 0x0600 + #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 + +#define GRUB_PCI_SERIAL_16550_COMPATIBLE 0x02 #ifndef ASM_FILE Index: grub/include/grub/serial.h =================================================================== --- grub.orig/include/grub/serial.h +++ grub/include/grub/serial.h @@ -193,6 +193,10 @@ const char * grub_arcserial_add_port (const char *path); #endif +#ifdef GRUB_HAS_PCI +void grub_pciserial_init (void); +#endif + 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);