From nobody Tue Apr 05 13:32:34 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 6B6511A9FFE0; Tue, 5 Apr 2022 13:32:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KXpVB2JMRz3tSW; Tue, 5 Apr 2022 13:32:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1649165554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=keZi+Qf3wnsE0tMT39oCfS+Hzlh/Dn+A1qGBllPXhkE=; b=bUt5K7TJjM36+ARqqvwTpkCP9Rm592HiPb4FYrndmEXOcem47+fUazH5fFsKLVwuTxuTKi bwgjexGg/0RTsLDPxAVjflIwTXjwuBvL9b2hci2K6u66URc6ffStS4A91btuoIvkGgRi1t gYTO1qHa7ntVH2csJdgBfUm6sPTmyRdMOfdAz5TjffM7DyU3iJuIhES89sOfo201Y8rVeS YuBdnc3WIRZ1OiaoXu6d1/TASpqU6WIKl3jx4+JYsI1Q7crPECdOgYMVIS5I3l4f/mxZFG WOilW8T1Rm8g+vBayAijf+Ol37/48ggA9S2kAHK8zQRsuoxknykssKW+8FQBmQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 30ADF2778B; Tue, 5 Apr 2022 13:32:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 235DWYI1037209; Tue, 5 Apr 2022 13:32:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 235DWYBc037208; Tue, 5 Apr 2022 13:32:34 GMT (envelope-from git) Date: Tue, 5 Apr 2022 13:32:34 GMT Message-Id: <202204051332.235DWYBc037208@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Colin Percival Subject: git: ce528519ec6e - releng/13.1 - uart(4): Add a concept of "unique" serial devices List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: cperciva X-Git-Repository: src X-Git-Refname: refs/heads/releng/13.1 X-Git-Reftype: branch X-Git-Commit: ce528519ec6ebacdb68beeac51a32943a67ae648 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1649165554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=keZi+Qf3wnsE0tMT39oCfS+Hzlh/Dn+A1qGBllPXhkE=; b=ho223hSs++Wl7Q/I5AokRiWdhYhxSXnRiom1LHNyhJxZHcBfbJTVfw6VPVlFbC66CO7HCK kI9jwJx3GXXQvdYxKImvSWW27tWUxtv0DfyPuE3KraJACwLTqOfv7RcoB75Z/1fCwVgA3f jWCMaKTI9RdiVqpBlq0AJ1/tJzTG3OZVr0bJh9DnyEN1HjR125tXgCnhUZvGavj72fNGDJ hTfRGzkH1K15rvV+Fo5dv8AoW9+IJHgRwVQlIHY3L8IFby9MAD2IKz3fJCS7LfuHd0qVi7 hFkhlFymoSM9cP58Z2+5DVxjfPhrjMuh+7EXMfHElI9Rjz8Tv/UxLrEmYDIK0w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1649165554; a=rsa-sha256; cv=none; b=QIz+j8GckYi0jXyUWsHB3UuJMegMc3ABS+xUZ19BqkpP9SgYK/jtKxklxQUtwVciafTJr0 5sOMx81xIl6+W3+QAz8BY4g5Deg367q65WyVmJCFAfk/IOk3SQOgb8v6bs9wyIJZ81jgOT 70AceF0SRLtDDf6zv974htJ0SWu++n1bg2+0r0ZnLPpFvL+7iBeQWJ73lZV78XF3+j699E 4W+K4OlKN5kegNQfdBNtP88YS7heymBvwYMTI3009WwQNhuYsXnYJQZ9nArTrDAcZkfFj8 TWNotIM/IhZ9ZV5PmYRckX68waEUdw843bxrYmc2eiY1IDFG+tn5wuaQFWSJ7A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch releng/13.1 has been updated by cperciva: URL: https://cgit.FreeBSD.org/src/commit/?id=ce528519ec6ebacdb68beeac51a32943a67ae648 commit ce528519ec6ebacdb68beeac51a32943a67ae648 Author: Colin Percival AuthorDate: 2022-03-29 07:41:37 +0000 Commit: Colin Percival CommitDate: 2022-04-05 13:32:24 +0000 uart(4): Add a concept of "unique" serial devices FreeBSD detects serial ports twice: First, very early in the boot process, in order to obtain a usable console; and second, during the device probe/attach process. When a UART is discovered during device probing, FreeBSD attempts to determine whether it is a device which was already being used as a console; without this, the console doesn't work in userland. Unfortunately it's possible for a UART to be mapped to a different location in memory when it is discovered on a bus than it has when it is announced via the ACPI SPCR table; this breaks the matching process, which relies on comparing bus addresses. To address this, we introduce a concept of "unique" serial devices, i.e. devices which are guaranteed to be present *only once* on any system. If we discover one of these during device probing, we can match it to a same-PCI-vendor-and-device-numbers console which was announced via the ACPI SPCR table, regardless of the differing bus addresses. At present, the only unique serial device is the "Amazon PCI serial device" (vendor 0x1d0f, device 0x8250) found in some EC2 instances. This unbreaks the serial console on those systems. Reviewed by: imp Approved by: re (gjb) Sponsored by: https://www.patreon.com/cperciva Differential Revision: https://reviews.freebsd.org/D34703 (cherry picked from commit ad93649d230428561db983153c546b39336fa4f1) (cherry picked from commit 5cc5cd739ef15cc762d2f417bccb44daefc86d56) --- sys/dev/uart/uart_bus_pci.c | 51 ++++++++++++++++++++++++++++++++++++++++++++ sys/dev/uart/uart_cpu.h | 9 ++++++++ sys/dev/uart/uart_cpu_acpi.c | 7 ++++++ 3 files changed, 67 insertions(+) diff --git a/sys/dev/uart/uart_bus_pci.c b/sys/dev/uart/uart_bus_pci.c index 92046261f544..b7612cda9f58 100644 --- a/sys/dev/uart/uart_bus_pci.c +++ b/sys/dev/uart/uart_bus_pci.c @@ -42,6 +42,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #define DEFAULT_RCLK 1843200 @@ -75,6 +76,11 @@ struct pci_id { int regshft; }; +struct pci_unique_id { + uint16_t vendor; + uint16_t device; +}; + static const struct pci_id pci_ns8250_ids[] = { { 0x1028, 0x0008, 0xffff, 0, "Dell Remote Access Card III", 0x14, 128 * DEFAULT_RCLK }, @@ -209,6 +215,44 @@ uart_pci_match(device_t dev, const struct pci_id *id) return ((id->vendor == vendor && id->device == device) ? id : NULL); } +extern SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs; + +/* PCI vendor/device pairs of devices guaranteed to be unique on a system. */ +static const struct pci_unique_id pci_unique_devices[] = { +{ 0x1d0f, 0x8250 } /* Amazon PCI serial device */ +}; + +/* Match a UART to a console if it's a PCI device known to be unique. */ +static void +uart_pci_unique_console_match(device_t dev) +{ + struct uart_softc *sc; + struct uart_devinfo * sysdev; + const struct pci_unique_id * id; + uint16_t vendor, device; + + sc = device_get_softc(dev); + vendor = pci_get_vendor(dev); + device = pci_get_device(dev); + + /* Is this a device known to exist only once in a system? */ + for (id = pci_unique_devices; ; id++) { + if (id == &pci_unique_devices[nitems(pci_unique_devices)]) + return; + if (id->vendor == vendor && id->device == device) + break; + } + + /* If it matches a console, it must be the same device. */ + SLIST_FOREACH(sysdev, &uart_sysdevs, next) { + if (sysdev->pci_info.vendor == vendor && + sysdev->pci_info.device == device) { + sc->sc_sysdev = sysdev; + sysdev->bas.rclk = sc->sc_bas.rclk; + } + } +} + static int uart_pci_probe(device_t dev) { @@ -231,6 +275,13 @@ uart_pci_probe(device_t dev) /* Bail out on error. */ if (result > 0) return (result); + /* + * If we haven't already matched this to a console, check if it's a + * PCI device which is known to only exist once in any given system + * and we can match it that way. + */ + if (sc->sc_sysdev == NULL) + uart_pci_unique_console_match(dev); /* Set/override the device description. */ if (id->desc) device_set_desc(dev, id->desc); diff --git a/sys/dev/uart/uart_cpu.h b/sys/dev/uart/uart_cpu.h index 4f34c767efc0..4eb7327fc788 100644 --- a/sys/dev/uart/uart_cpu.h +++ b/sys/dev/uart/uart_cpu.h @@ -52,6 +52,14 @@ struct uart_ops { extern bus_space_tag_t uart_bus_space_io; extern bus_space_tag_t uart_bus_space_mem; +/* + * PCI ID used for matching "unique" devices to a console. + */ +struct uart_pci_info { + uint16_t vendor; + uint16_t device; +}; + /* * Console and debug port device info. */ @@ -72,6 +80,7 @@ struct uart_devinfo { void *cookie; /* Type dependent use. */ struct mtx *hwmtx; struct uart_softc *sc; /* valid only from start of attach */ + struct uart_pci_info pci_info; }; int uart_cpu_eqres(struct uart_bas *, struct uart_bas *); diff --git a/sys/dev/uart/uart_cpu_acpi.c b/sys/dev/uart/uart_cpu_acpi.c index 1e46462fc58a..68f743c8e8d2 100644 --- a/sys/dev/uart/uart_cpu_acpi.c +++ b/sys/dev/uart/uart_cpu_acpi.c @@ -36,6 +36,8 @@ __FBSDID("$FreeBSD$"); #include +#include + #include #include #include @@ -182,6 +184,11 @@ uart_cpu_acpi_spcr(int devtype, struct uart_devinfo *di) (int)spcr->BaudRate); goto out; } + if (spcr->PciVendorId != PCIV_INVALID && + spcr->PciDeviceId != PCIV_INVALID) { + di->pci_info.vendor = spcr->PciVendorId; + di->pci_info.device = spcr->PciDeviceId; + } /* Apply device tweaks. */ if ((cd->cd_quirks & UART_F_IGNORE_SPCR_REGSHFT) ==