From owner-svn-src-stable@freebsd.org Tue Sep 3 14:05:46 2019 Return-Path: Delivered-To: svn-src-stable@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 961E2DBE2B; Tue, 3 Sep 2019 14:05:46 +0000 (UTC) (envelope-from yuripv@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [96.47.72.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "freefall.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 46N7yf3GwWz4Npm; Tue, 3 Sep 2019 14:05:46 +0000 (UTC) (envelope-from yuripv@freebsd.org) Received: by freefall.freebsd.org (Postfix, from userid 1452) id 4872419AF9; Tue, 3 Sep 2019 14:05:46 +0000 (UTC) X-Original-To: yuripv@localmail.freebsd.org Delivered-To: yuripv@localmail.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client CN "mx1.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by freefall.freebsd.org (Postfix) with ESMTPS id 00D2D1CC17; Wed, 27 Mar 2019 19:17:46 +0000 (UTC) (envelope-from owner-src-committers@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2610:1c1:1:6074::16:84]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "freefall.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4057F8974B; Wed, 27 Mar 2019 19:17:46 +0000 (UTC) (envelope-from owner-src-committers@freebsd.org) Received: by freefall.freebsd.org (Postfix, from userid 538) id 140BB1CBAD; Wed, 27 Mar 2019 19:17:46 +0000 (UTC) Delivered-To: src-committers@localmail.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [96.47.72.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client CN "mx1.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by freefall.freebsd.org (Postfix) with ESMTPS id 9C0E71CBA7 for ; Wed, 27 Mar 2019 19:17:43 +0000 (UTC) (envelope-from wulf@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 5148889741; Wed, 27 Mar 2019 19:17:43 +0000 (UTC) (envelope-from wulf@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 2A1C8B8F6; Wed, 27 Mar 2019 19:17:43 +0000 (UTC) (envelope-from wulf@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x2RJHgii065368; Wed, 27 Mar 2019 19:17:42 GMT (envelope-from wulf@FreeBSD.org) Received: (from wulf@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x2RJHgEB065367; Wed, 27 Mar 2019 19:17:42 GMT (envelope-from wulf@FreeBSD.org) Message-Id: <201903271917.x2RJHgEB065367@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: wulf set sender to wulf@FreeBSD.org using -f From: Vladimir Kondratyev To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r345590 - stable/11/sys/x86/isa X-SVN-Group: stable-11 X-SVN-Commit-Author: wulf X-SVN-Commit-Paths: stable/11/sys/x86/isa X-SVN-Commit-Revision: 345590 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk X-Loop: FreeBSD.org Sender: owner-src-committers@freebsd.org X-Rspamd-Queue-Id: 4057F8974B X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.96 / 15.00]; local_wl_from(0.00)[freebsd.org]; NEURAL_HAM_MEDIUM(-0.99)[-0.993,0]; NEURAL_HAM_SHORT(-0.97)[-0.969,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; NEURAL_HAM_LONG(-1.00)[-1.000,0] Status: O X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.29 List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Date: Tue, 03 Sep 2019 14:05:47 -0000 X-Original-Date: Wed, 27 Mar 2019 19:17:42 +0000 (UTC) X-List-Received-Date: Tue, 03 Sep 2019 14:05:47 -0000 Author: wulf Date: Wed Mar 27 19:17:42 2019 New Revision: 345590 URL: https://svnweb.freebsd.org/changeset/base/345590 Log: MFC: r344982, r345022 atrtc(4): install ACPI RTC/CMOS operation region handler FreeBSD base system does not provide an ACPI handler for the PC/AT RTC/CMOS device with PnP ID PNP0B00; on some HP laptops, the absence of this handler causes suspend/resume and poweroff(8) to hang or fail [1], [2]. On these laptops EC _REG method queries the RTC date/time registers via ACPI before suspending/powering off. The handler should be registered before acpi_ec driver is loaded. This change adds handler to access CMOS RTC operation region described in section 9.15 of ACPI-6.2 specification [3]. It is installed only for ACPI version of atrtc(4) so it should not affect old ACPI-less i386 systems. It is possible to disable the handler with loader tunable: debug.acpi.disabled=atrtc Informational debugging printf can be enabled by setting hw.acpi.verbose=1 in loader.conf [1] https://wiki.freebsd.org/Laptops/HP_Envy_6Z-1100 [2] https://wiki.freebsd.org/Laptops/HP_Notebook_15-af104ur [3] https://uefi.org/sites/default/files/resources/ACPI_6_2.pdf PR: 207419, 213039 Submitted by: Anthony Jenkins Reviewed by: ian Discussed on: acpi@, 2013-2015, several threads Differential Revision: https://reviews.freebsd.org/D19314 Modified: stable/11/sys/x86/isa/atrtc.c Directory Properties: stable/11/ (props changed) Modified: stable/11/sys/x86/isa/atrtc.c ============================================================================== --- stable/11/sys/x86/isa/atrtc.c Wed Mar 27 19:16:03 2019 (r345589) +++ stable/11/sys/x86/isa/atrtc.c Wed Mar 27 19:17:42 2019 (r345590) @@ -30,6 +30,7 @@ #include __FBSDID("$FreeBSD$"); +#include "opt_acpi.h" #include "opt_isa.h" #include @@ -52,6 +53,12 @@ __FBSDID("$FreeBSD$"); #endif #include #include "clock_if.h" +#ifdef DEV_ACPI +#include +#include +#include +#include +#endif /* * atrtc_lock protects low-level access to individual hardware registers. @@ -70,6 +77,11 @@ static int rtc_reg = -1; static u_char rtc_statusa = RTCSA_DIVIDER | RTCSA_NOPROF; static u_char rtc_statusb = RTCSB_24HR; +#ifdef DEV_ACPI +#define _COMPONENT ACPI_TIMER +ACPI_MODULE_NAME("ATRTC") +#endif + /* * RTC support routines */ @@ -185,6 +197,9 @@ struct atrtc_softc { struct resource *intr_res; void *intr_handler; struct eventtimer et; +#ifdef DEV_ACPI + ACPI_HANDLE acpi_handle; +#endif }; static int @@ -239,7 +254,145 @@ rtc_intr(void *arg) return(flag ? FILTER_HANDLED : FILTER_STRAY); } +#ifdef DEV_ACPI /* + * ACPI RTC CMOS address space handler + */ +#define ATRTC_LAST_REG 0x40 + +static void +rtcin_region(int reg, void *buf, int len) +{ + u_char *ptr = buf; + + /* Drop lock after each IO as intr and settime have greater priority */ + while (len-- > 0) + *ptr++ = rtcin(reg++) & 0xff; +} + +static void +rtcout_region(int reg, const void *buf, int len) +{ + const u_char *ptr = buf; + + while (len-- > 0) + writertc(reg++, *ptr++); +} + +static bool +atrtc_check_cmos_access(bool is_read, ACPI_PHYSICAL_ADDRESS addr, UINT32 len) +{ + + /* Block address space wrapping on out-of-bound access */ + if (addr >= ATRTC_LAST_REG || addr + len > ATRTC_LAST_REG) + return (false); + + if (is_read) { + /* Reading 0x0C will muck with interrupts */ + if (addr <= RTC_INTR && addr + len > RTC_INTR) + return (false); + } else { + /* + * Allow single-byte writes to alarm registers and + * multi-byte writes to addr >= 0x30, else deny. + */ + if (!((len == 1 && (addr == RTC_SECALRM || + addr == RTC_MINALRM || + addr == RTC_HRSALRM)) || + addr >= 0x30)) + return (false); + } + return (true); +} + +static ACPI_STATUS +atrtc_acpi_cmos_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS addr, + UINT32 bitwidth, UINT64 *value, void *context, void *region_context) +{ + device_t dev = context; + UINT32 bytewidth = howmany(bitwidth, 8); + bool is_read = func == ACPI_READ; + + /* ACPICA is very verbose on CMOS handler failures, so we, too */ +#define CMOS_HANDLER_ERR(fmt, ...) \ + device_printf(dev, "ACPI [SystemCMOS] handler: " fmt, ##__VA_ARGS__) + + ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); + + if (value == NULL) { + CMOS_HANDLER_ERR("NULL parameter\n"); + return (AE_BAD_PARAMETER); + } + if (bitwidth == 0 || (bitwidth & 0x07) != 0) { + CMOS_HANDLER_ERR("Invalid bitwidth: %u\n", bitwidth); + return (AE_BAD_PARAMETER); + } + if (!atrtc_check_cmos_access(is_read, addr, bytewidth)) { + CMOS_HANDLER_ERR("%s access rejected: addr=%#04jx, len=%u\n", + is_read ? "Read" : "Write", (uintmax_t)addr, bytewidth); + return (AE_BAD_PARAMETER); + } + + switch (func) { + case ACPI_READ: + rtcin_region(addr, value, bytewidth); + break; + case ACPI_WRITE: + rtcout_region(addr, value, bytewidth); + break; + default: + CMOS_HANDLER_ERR("Invalid function: %u\n", func); + return (AE_BAD_PARAMETER); + } + + ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev), + "ACPI RTC CMOS %s access: addr=%#04x, len=%u, val=%*D\n", + is_read ? "read" : "write", (unsigned)addr, bytewidth, + bytewidth, value, " "); + + return (AE_OK); +} + +static int +atrtc_reg_acpi_cmos_handler(device_t dev) +{ + struct atrtc_softc *sc = device_get_softc(dev); + + ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); + + /* Don't handle address space events if driver is disabled. */ + if (acpi_disabled("atrtc")) + return (ENXIO); + + sc->acpi_handle = acpi_get_handle(dev); + if (sc->acpi_handle == NULL || + ACPI_FAILURE(AcpiInstallAddressSpaceHandler(sc->acpi_handle, + ACPI_ADR_SPACE_CMOS, atrtc_acpi_cmos_handler, NULL, dev))) { + sc->acpi_handle = NULL; + device_printf(dev, + "Can't register ACPI CMOS address space handler\n"); + return (ENXIO); + } + + return (0); +} + +static int +atrtc_unreg_acpi_cmos_handler(device_t dev) +{ + struct atrtc_softc *sc = device_get_softc(dev); + + ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); + + if (sc->acpi_handle != NULL) + AcpiRemoveAddressSpaceHandler(sc->acpi_handle, + ACPI_ADR_SPACE_CMOS, atrtc_acpi_cmos_handler); + + return (0); +} +#endif /* DEV_ACPI */ + +/* * Attach to the ISA PnP descriptors for the timer and realtime clock. */ static struct isa_pnp_id atrtc_ids[] = { @@ -311,6 +464,37 @@ atrtc_attach(device_t dev) } static int +atrtc_isa_attach(device_t dev) +{ + + return (atrtc_attach(dev)); +} + +#ifdef DEV_ACPI +static int +atrtc_acpi_attach(device_t dev) +{ + int ret; + + ret = atrtc_attach(dev); + if (ret) + return (ret); + + (void)atrtc_reg_acpi_cmos_handler(dev); + + return (0); +} + +static int +atrtc_acpi_detach(device_t dev) +{ + + (void)atrtc_unreg_acpi_cmos_handler(dev); + return (0); +} +#endif /* DEV_ACPI */ + +static int atrtc_resume(device_t dev) { @@ -396,10 +580,10 @@ atrtc_gettime(device_t dev, struct timespec *ts) return (clock_bcd_to_ts(&bct, ts, false)); } -static device_method_t atrtc_methods[] = { +static device_method_t atrtc_isa_methods[] = { /* Device interface */ DEVMETHOD(device_probe, atrtc_probe), - DEVMETHOD(device_attach, atrtc_attach), + DEVMETHOD(device_attach, atrtc_isa_attach), DEVMETHOD(device_detach, bus_generic_detach), DEVMETHOD(device_shutdown, bus_generic_shutdown), DEVMETHOD(device_suspend, bus_generic_suspend), @@ -413,13 +597,38 @@ static device_method_t atrtc_methods[] = { { 0, 0 } }; -static driver_t atrtc_driver = { +static driver_t atrtc_isa_driver = { "atrtc", - atrtc_methods, + atrtc_isa_methods, sizeof(struct atrtc_softc), }; +#ifdef DEV_ACPI +static device_method_t atrtc_acpi_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, atrtc_probe), + DEVMETHOD(device_attach, atrtc_acpi_attach), + DEVMETHOD(device_detach, atrtc_acpi_detach), + /* XXX stop statclock? */ + DEVMETHOD(device_resume, atrtc_resume), + + /* clock interface */ + DEVMETHOD(clock_gettime, atrtc_gettime), + DEVMETHOD(clock_settime, atrtc_settime), + + { 0, 0 } +}; + +static driver_t atrtc_acpi_driver = { + "atrtc", + atrtc_acpi_methods, + sizeof(struct atrtc_softc), +}; +#endif /* DEV_ACPI */ + static devclass_t atrtc_devclass; -DRIVER_MODULE(atrtc, isa, atrtc_driver, atrtc_devclass, 0, 0); -DRIVER_MODULE(atrtc, acpi, atrtc_driver, atrtc_devclass, 0, 0); +DRIVER_MODULE(atrtc, isa, atrtc_isa_driver, atrtc_devclass, 0, 0); +#ifdef DEV_ACPI +DRIVER_MODULE(atrtc, acpi, atrtc_acpi_driver, atrtc_devclass, 0, 0); +#endif