From owner-svn-src-all@freebsd.org Fri Mar 16 18:16:28 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E969EF63191; Fri, 16 Mar 2018 18:16:27 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 958FC6D6AE; Fri, 16 Mar 2018 18:16:27 +0000 (UTC) (envelope-from ian@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 9067827435; Fri, 16 Mar 2018 18:16:27 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w2GIGRPQ057464; Fri, 16 Mar 2018 18:16:27 GMT (envelope-from ian@FreeBSD.org) Received: (from ian@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w2GIGR3h057463; Fri, 16 Mar 2018 18:16:27 GMT (envelope-from ian@FreeBSD.org) Message-Id: <201803161816.w2GIGR3h057463@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ian set sender to ian@FreeBSD.org using -f From: Ian Lepore Date: Fri, 16 Mar 2018 18:16:27 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r331068 - head/sys/dev/efidev X-SVN-Group: head X-SVN-Commit-Author: ian X-SVN-Commit-Paths: head/sys/dev/efidev X-SVN-Commit-Revision: 331068 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 16 Mar 2018 18:16:28 -0000 Author: ian Date: Fri Mar 16 18:16:27 2018 New Revision: 331068 URL: https://svnweb.freebsd.org/changeset/base/331068 Log: Use EFI RTC capabilities info when registering, add bootverbose diagnostics. Make some small improvements to the efirtc driver by obtaining the clock capabilities (resolution and whether the sub-second counters are reset) and using the info when registering the clock. When the hardware zeroes out the subsecond info on clock-set, schedule clock updates to happen just before top-of-second, so that the RTC time is closely in-sync with kernel time. Also, in the identify() routine, always add the driver if EFI runtime services are available, then decide in probe() whether to attach the driver or not. If not attaching and bootverbose is on, say why. All of this is basically to avoid "silent failure" -- if someone thinks there should be an efi rtc and it's not attaching, at least they can set bootverbose and maybe get a clue from the output. Differential Revision: https://reviews.freebsd.org/D14565 (timed out) Modified: head/sys/dev/efidev/efirtc.c Modified: head/sys/dev/efidev/efirtc.c ============================================================================== --- head/sys/dev/efidev/efirtc.c Fri Mar 16 18:12:50 2018 (r331067) +++ head/sys/dev/efidev/efirtc.c Fri Mar 16 18:16:27 2018 (r331068) @@ -41,21 +41,20 @@ __FBSDID("$FreeBSD$"); #include "clock_if.h" +static bool efirtc_zeroes_subseconds; +static struct timespec efirtc_resadj; + +static const u_int us_per_s = 1000000; +static const u_int ns_per_s = 1000000000; +static const u_int ns_per_us = 1000; + static void efirtc_identify(driver_t *driver, device_t parent) { - struct efi_tm tm; - int error; - /* - * Check if we can read the time. This will stop us attaching when - * there is no EFI Runtime support, or the gettime function is - * unimplemented, e.g. on some builds of U-Boot. - */ - error = efi_get_time(&tm); - if (error != 0) + /* Don't add the driver unless we have working runtime services. */ + if (efi_rt_ok() != 0) return; - if (device_find_child(parent, "efirtc", -1) != NULL) return; if (BUS_ADD_CHILD(parent, 0, "efirtc", -1) == NULL) @@ -65,16 +64,58 @@ efirtc_identify(driver_t *driver, device_t parent) static int efirtc_probe(device_t dev) { + struct efi_tm tm; + int error; - device_quiet(dev); - return (0); + /* + * Check whether we can read the time. This will stop us from attaching + * when there is EFI Runtime support but the gettime function is + * unimplemented, e.g. on some builds of U-Boot. + */ + if ((error = efi_get_time(&tm)) != 0) { + if (bootverbose) + device_printf(dev, "cannot read EFI realtime clock\n"); + return (error); + } + device_set_desc(dev, "EFI Realtime Clock"); + return (BUS_PROBE_DEFAULT); } static int efirtc_attach(device_t dev) { + struct efi_tmcap tmcap; + long res; + int error; - clock_register(dev, 1000000); + bzero(&tmcap, sizeof(tmcap)); + if ((error = efi_get_time_capabilities(&tmcap)) != 0) { + device_printf(dev, "cannot get EFI time capabilities"); + return (error); + } + + /* Translate resolution in Hz to tick length in usec. */ + if (tmcap.tc_res == 0) + res = us_per_s; /* 0 is insane, assume 1 Hz. */ + else if (tmcap.tc_res > us_per_s) + res = 1; /* 1us is the best we can represent */ + else + res = us_per_s / tmcap.tc_res; + + /* Clock rounding adjustment is 1/2 of resolution, in nsec. */ + efirtc_resadj.tv_nsec = (res * ns_per_us) / 2; + + /* Does the clock zero the subseconds when time is set? */ + efirtc_zeroes_subseconds = tmcap.tc_stz; + + /* + * Register. If the clock zeroes out the subseconds when it's set, + * schedule the SetTime calls to happen just before top-of-second. + */ + clock_register_flags(dev, res, CLOCKF_SETTIME_NO_ADJ); + if (efirtc_zeroes_subseconds) + clock_schedule(dev, ns_per_s - ns_per_us); + return (0); } @@ -105,6 +146,7 @@ efirtc_gettime(device_t dev, struct timespec *ts) ct.year = tm.tm_year; ct.nsec = tm.tm_nsec; + clock_dbgprint_ct(dev, CLOCK_DBG_READ, &ct); return (clock_ct_to_ts(&ct, ts)); } @@ -114,7 +156,17 @@ efirtc_settime(device_t dev, struct timespec *ts) struct clocktime ct; struct efi_tm tm; + /* + * We request a timespec with no resolution-adjustment so that we can + * apply it ourselves based on whether or not the clock zeroes the + * sub-second part of the time when setting the time. + */ + ts->tv_sec -= utc_offset(); + if (!efirtc_zeroes_subseconds) + timespecadd(ts, &efirtc_resadj); + clock_ts_to_ct(ts, &ct); + clock_dbgprint_ct(dev, CLOCK_DBG_WRITE, &ct); bzero(&tm, sizeof(tm)); tm.tm_sec = ct.sec;