From patchwork Thu Nov 27 12:02:39 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "pang.xunlei" X-Patchwork-Id: 415470 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-qa0-x238.google.com (mail-qa0-x238.google.com [IPv6:2607:f8b0:400d:c00::238]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 26B851401EF for ; Thu, 27 Nov 2014 23:03:34 +1100 (AEDT) Received: by mail-qa0-f56.google.com with SMTP id x12sf632144qac.1 for ; Thu, 27 Nov 2014 04:03:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:x-original-sender:x-original-authentication-results :reply-to:content-type:precedence:mailing-list:list-id:list-post :list-help:list-archive:sender:list-subscribe:list-unsubscribe; bh=rVeIUBf0JcbLpqmHZZxf0MZ4FC1OJ63Kct5TlDidLD0=; b=DDbMRj2ZmXGfL0ZOnvW87+XqjgjwhdvQcNpH45ox898Gau8nDgMperRYTJ78ya/3PN 3W77B5Wp9kyv084F9Xu97RZh5VMoBW7MXLXntCxsmt9mb08pcg4vCtw1bTvnMH83/Pes 6n14+WQ6wgXr9v0S+sCG3l3QyqdCQt5q6PTLWJsgl8rONjLnUu1MHvOzfUZuaS/xZDxK g4gMVMuVZ0ovLAd4J5eX8gAKFIcwvjZmPgThg+O+V6Z5LTrUx1a8z79tUqK644JaDxOj B+reN5vVJYUGh0tJ/bWDbHq/mH9EpbCExG1prAjYplwzX2Ae9x3Han5QygR4MUQDa3nX nCGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :in-reply-to:references:x-original-sender :x-original-authentication-results:reply-to:content-type:precedence :mailing-list:list-id:list-post:list-help:list-archive:sender :list-subscribe:list-unsubscribe; bh=rVeIUBf0JcbLpqmHZZxf0MZ4FC1OJ63Kct5TlDidLD0=; b=mfoI59YtqKPHHrF/ITqhHMIHQ8y1q9k6rShRxQJUauAp7dAgmhIdr/OG4zGRvySg6R e+1R9wFSniiPQDo59ziPzdroyucKJ2dWUtib4hoUTyLwuJOG6Q/lF5I4qZYAOyUf0xo4 wqJQZM0Kr7NBcC0rcnk3kxsi0861TvNwIAJGz/7ds2T1fou9yfyHqIfkAJAiDuRHoFvT lBR4sEqWKK4TC43wDH9WkoE3dQxQUCjtqrRPFv7SsNH/tVGZtDYX0GN2qSQjbYbTNOBj scHnk+NWZ8oRXIhHZ+MZTV3TUnb8aNbYX6cp4LdhCNrS0+AeHltOlljZ2GL3+Um+LRGm KqHQ== X-Received: by 10.140.92.176 with SMTP id b45mr81860qge.10.1417089812243; Thu, 27 Nov 2014 04:03:32 -0800 (PST) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.140.34.21 with SMTP id k21ls26701qgk.16.gmail; Thu, 27 Nov 2014 04:03:31 -0800 (PST) X-Received: by 10.236.53.34 with SMTP id f22mr37874381yhc.51.1417089811926; Thu, 27 Nov 2014 04:03:31 -0800 (PST) Received: from mail-pd0-f180.google.com (mail-pd0-f180.google.com. [209.85.192.180]) by gmr-mx.google.com with ESMTPS id jy5si844321pbc.2.2014.11.27.04.03.31 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 27 Nov 2014 04:03:31 -0800 (PST) Received-SPF: pass (google.com: domain of pang.xunlei@linaro.org designates 209.85.192.180 as permitted sender) client-ip=209.85.192.180; Received: by mail-pd0-f180.google.com with SMTP id p10so4783359pdj.25 for ; Thu, 27 Nov 2014 04:03:31 -0800 (PST) X-Gm-Message-State: ALoCoQmk8RLKx1mxmcR+0GGAwcDyXuwxHNZCYliLswjOzZwUwL0lsN1tnRaudI+pbEKY+Ybwjdmo X-Received: by 10.68.216.65 with SMTP id oo1mr41317853pbc.105.1417089811685; Thu, 27 Nov 2014 04:03:31 -0800 (PST) Received: from wangdeqiang.com ([210.21.223.3]) by mx.google.com with ESMTPSA id cf12sm6955128pdb.77.2014.11.27.04.03.26 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Nov 2014 04:03:31 -0800 (PST) From: Xunlei Pang To: linux-kernel@vger.kernel.org Cc: rtc-linux@googlegroups.com, Thomas Gleixner , Alessandro Zummo , Sven Schnelle , Xunlei Pang , John Stultz , Arnd Bergmann Subject: [rtc-linux] [RFC PATCH 3/4] rtc/lib: Provide interfaces to map between 32bit hardware and 64bit time Date: Thu, 27 Nov 2014 20:02:39 +0800 Message-Id: <1417089760-26848-4-git-send-email-pang.xunlei@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1417089760-26848-1-git-send-email-pang.xunlei@linaro.org> References: <1417089760-26848-1-git-send-email-pang.xunlei@linaro.org> X-Original-Sender: pang.xunlei@linaro.org X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of pang.xunlei@linaro.org designates 209.85.192.180 as permitted sender) smtp.mail=pang.xunlei@linaro.org Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , To attack the 32bit rtc hardware limitations which will bring about y2106 issues to their rtc drivers as those marked "y2106 issue" tags in the former patch, this patch adds a common rtc epoch offset, and provides two interfaces to map between 32bit rtc hardware and 64bit time using this rtc epoch offset. These two interfaces can be used to write rtc drivers for 32-bit rtc hardware. - Add "rtc_epoch" boot parameter, default offset value is hardcoded to 1970 for consistency with Unix epoch. This default value can be overrided through boot command if you have problematic rtc hardware. Once you are doing this, should be aware of following side effects: a) Can't set time before your rtc epoch. b) Can't boot into different OSes relying on the same rtc hardware. "rtc epoch" must stay invariable once finalized on one machine. - Add rtc_time64_to_hw32(): This interface converts 64-bit seconds since unix 1970 epoch to 32bit seconds since rtc epoch which can be maintained by 32-bit rtc hardware. - Add rtc_hw32_to_time64(): This interface converts 32bit seconds since rtc epoch which can be used by 32-bit rtc hardware to 64bit seconds since unix 1970 epoch. There're also other ways to implement this function: For example, some driver(vr41xx) implements the same logic using RTC_EPOCH_SET ioctl and an rtc epoch offset variable. But the approach in this patch is more flexible and common than that. Cc: John Stultz Cc: Arnd Bergmann Signed-off-by: Xunlei Pang --- drivers/rtc/class.c | 25 +++++++++++++++++++++++++ drivers/rtc/rtc-lib.c | 28 ++++++++++++++++++++++++++++ include/linux/rtc.h | 3 +++ 3 files changed, 56 insertions(+) diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c index 38e26be..7f1950f8 100644 --- a/drivers/rtc/class.c +++ b/drivers/rtc/class.c @@ -358,6 +358,29 @@ void devm_rtc_device_unregister(struct device *dev, struct rtc_device *rtc) } EXPORT_SYMBOL_GPL(devm_rtc_device_unregister); +/* Rtc epoch year, can be overrided by command line */ +static unsigned int rtc_epoch = 1970; +time64_t rtc_epoch_secs_since_1970; + +static int __init +set_rtc_epoch(char *str) +{ + unsigned int epoch; + + if (kstrtou32(str, 0, &epoch) != 0) { + printk(KERN_WARNING "Invalid setup epoch %u!\n", epoch); + return 1; + } + + if (rtc_epoch < 1970) + printk(KERN_WARNING "Epoch %u is smaller than 1970!\n", epoch); + else + rtc_epoch = epoch; + + return 1; +} +__setup("rtc_epoch=", set_rtc_epoch); + static int __init rtc_init(void) { rtc_class = class_create(THIS_MODULE, "rtc"); @@ -366,6 +389,8 @@ static int __init rtc_init(void) return PTR_ERR(rtc_class); } rtc_class->pm = RTC_CLASS_DEV_PM_OPS; + + rtc_epoch_secs_since_1970 = mktime64(rtc_epoch, 1, 1, 0, 0, 0); rtc_dev_init(); rtc_sysfs_init(rtc_class); return 0; diff --git a/drivers/rtc/rtc-lib.c b/drivers/rtc/rtc-lib.c index e6bfb9c..d26814c 100644 --- a/drivers/rtc/rtc-lib.c +++ b/drivers/rtc/rtc-lib.c @@ -124,6 +124,34 @@ time64_t rtc_tm_to_time64(struct rtc_time *tm) EXPORT_SYMBOL(rtc_tm_to_time64); /* + * Convert seconds since rtc epoch to seconds since Unix epoch. + */ +time64_t rtc_hw32_to_time64(u32 hwtime) +{ + return (rtc_epoch_secs_since_1970 + hwtime); +} +EXPORT_SYMBOL_GPL(rtc_hw32_to_time64); + +/* + * Convert seconds since Unix epoch to seconds since rtc epoch. + */ +int rtc_time64_to_hw32(time64_t time, u32 *hwtime) +{ + /* time must be after rtc epoch */ + if (time < rtc_epoch_secs_since_1970) + return -EINVAL; + + /* rtc epoch may be too small? */ + if (time - rtc_epoch_secs_since_1970 > UINT_MAX) + return -EOVERFLOW; + + *hwtime = time - rtc_epoch_secs_since_1970; + + return 0; +} +EXPORT_SYMBOL_GPL(rtc_time64_to_hw32); + +/* * Convert rtc_time to ktime */ ktime_t rtc_tm_to_ktime(struct rtc_time tm) diff --git a/include/linux/rtc.h b/include/linux/rtc.h index d080606..32f7c22 100644 --- a/include/linux/rtc.h +++ b/include/linux/rtc.h @@ -16,11 +16,14 @@ #include #include +extern time64_t rtc_epoch_secs_since_1970; extern int rtc_month_days(unsigned int month, unsigned int year); extern int rtc_year_days(unsigned int day, unsigned int month, unsigned int year); extern int rtc_valid_tm(struct rtc_time *tm); extern time64_t rtc_tm_to_time64(struct rtc_time *tm); extern void rtc_time64_to_tm(time64_t time, struct rtc_time *tm); +extern int rtc_time64_to_hw32(time64_t time, u32 *hwtime); +extern time64_t rtc_hw32_to_time64(u32 hwtime); ktime_t rtc_tm_to_ktime(struct rtc_time tm); struct rtc_time rtc_ktime_to_tm(ktime_t kt);