2018-01-02 05:10:31

by Baolin Wang

[permalink] [raw]
Subject: [RFC PATCH 1/4] rtc: Introduce one interface to save the RTC hardware time range

In order to the setting time values are not beyond the limitation
supported by RTC hardware, we introduce one interface to tell the
hardware range to the RTC core, which are used to valid if the
setting time values are in the RTC hardware range.

Moreover we also need the RTC hardware range to expand the RTC
range in next patches by adding one offset.

Signed-off-by: Baolin Wang <[email protected]>
---
drivers/rtc/class.c | 13 ++++++++++
drivers/rtc/interface.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++
include/linux/rtc.h | 9 +++++++
3 files changed, 84 insertions(+)

diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index 722d683..31fc0f1 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -247,6 +247,12 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,

dev_set_name(&rtc->dev, "rtc%d", id);

+ err = rtc_read_range(rtc, &rtc->max_hw_secs, &rtc->min_hw_secs);
+ if (err) {
+ dev_err(&rtc->dev, "%s: failed to get RTC range\n", name);
+ goto exit_ida;
+ }
+
/* Check to see if there is an ALARM already set in hw */
err = __rtc_read_alarm(rtc, &alrm);

@@ -436,6 +442,13 @@ int __rtc_register_device(struct module *owner, struct rtc_device *rtc)

rtc->owner = owner;

+ err = rtc_read_range(rtc, &rtc->max_hw_secs, &rtc->min_hw_secs);
+ if (err) {
+ dev_err(&rtc->dev, "%s: failed to get RTC range\n",
+ dev_name(&rtc->dev));
+ return err;
+ }
+
/* Check to see if there is an ALARM already set in hw */
err = __rtc_read_alarm(rtc, &alrm);
if (!err && !rtc_valid_tm(&alrm.time))
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 672b192..c8090e3 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -65,6 +65,10 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
if (err != 0)
return err;

+ err = rtc_valid_range(rtc, tm);
+ if (err)
+ return err;
+
err = mutex_lock_interruptible(&rtc->ops_lock);
if (err)
return err;
@@ -329,6 +333,11 @@ static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
err = rtc_valid_tm(&alarm->time);
if (err)
return err;
+
+ err = rtc_valid_range(rtc, &alarm->time);
+ if (err)
+ return err;
+
scheduled = rtc_tm_to_time64(&alarm->time);

/* Make sure we're not setting alarms in the past */
@@ -1027,3 +1036,56 @@ int rtc_set_offset(struct rtc_device *rtc, long offset)
mutex_unlock(&rtc->ops_lock);
return ret;
}
+
+/* rtc_read_range - Read the max and min hardware count supported by RTC device
+ * @ rtc: rtc device to be used.
+ * @ max_hw_secs: maximum hardware count in seconds, which can represent
+ * the maximum time values in RTC hardware.
+ * @ min_hw_secs: minimum hardware count in seconds, which can represent
+ * the minimum time values in RTC hardware.
+ *
+ * The max_hw_secs and min_hw_secs implemented by user must represent the
+ * correct hardware start time and maximum time, which means the count
+ * will wrap around to min_hw_secs after the maximum count.
+ *
+ * If user did not implement the read_range() interface, we can set max_hw_secs
+ * and min_hw_secs to 0, which avoids validing the range.
+ */
+int rtc_read_range(struct rtc_device *rtc, time64_t *max_hw_secs,
+ time64_t *min_hw_secs)
+{
+ int ret;
+
+ if (!rtc->ops || !rtc->ops->read_range) {
+ *max_hw_secs = 0;
+ *min_hw_secs = 0;
+ return 0;
+ }
+
+ mutex_lock(&rtc->ops_lock);
+ ret = rtc->ops->read_range(rtc->dev.parent, max_hw_secs, min_hw_secs);
+ mutex_unlock(&rtc->ops_lock);
+
+ return ret;
+}
+
+/* rtc_valid_range - Valid if the setting time in the RTC range
+ * @ rtc: rtc device to be used.
+ * @ tm: time values need to valid.
+ *
+ * Only the rtc->max_hw_secs was set, then we can valid if the setting time
+ * values are beyond the RTC range.
+ */
+int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
+{
+ time64_t secs;
+
+ if (!rtc->max_hw_secs)
+ return 0;
+
+ secs = rtc_tm_to_time64(tm);
+ if (secs < rtc->min_hw_secs || secs > rtc->max_hw_secs)
+ return -EINVAL;
+
+ return 0;
+}
diff --git a/include/linux/rtc.h b/include/linux/rtc.h
index 41319a2..19a8989 100644
--- a/include/linux/rtc.h
+++ b/include/linux/rtc.h
@@ -85,6 +85,8 @@ struct rtc_class_ops {
int (*alarm_irq_enable)(struct device *, unsigned int enabled);
int (*read_offset)(struct device *, long *offset);
int (*set_offset)(struct device *, long offset);
+ int (*read_range)(struct device *, time64_t *max_hw_secs,
+ time64_t *min_hw_secs);
};

#define RTC_DEVICE_NAME_SIZE 20
@@ -152,6 +154,9 @@ struct rtc_device {
bool nvram_old_abi;
struct bin_attribute *nvram;

+ time64_t max_hw_secs;
+ time64_t min_hw_secs;
+
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
struct work_struct uie_task;
struct timer_list uie_timer;
@@ -225,6 +230,10 @@ int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
int rtc_set_offset(struct rtc_device *rtc, long offset);
void rtc_timer_do_work(struct work_struct *work);

+int rtc_read_range(struct rtc_device *rtc, time64_t *max_hw_secs,
+ time64_t *min_hw_secs);
+int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm);
+
static inline bool is_leap_year(unsigned int year)
{
return (!(year % 4) && (year % 100)) || !(year % 400);
--
1.7.9.5


2018-01-02 05:10:35

by Baolin Wang

[permalink] [raw]
Subject: [RFC PATCH 2/4] rtc: sysfs: Export the valid range supported by RTC hardware

We have introduced one interface to get the RTC range, so this patch
exports the valid range supported by RTC hardware to userspace.

Signed-off-by: Baolin Wang <[email protected]>
---
Documentation/rtc.txt | 2 ++
drivers/rtc/rtc-sysfs.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 32 insertions(+)

diff --git a/Documentation/rtc.txt b/Documentation/rtc.txt
index c0c9774..4fe437b 100644
--- a/Documentation/rtc.txt
+++ b/Documentation/rtc.txt
@@ -164,6 +164,8 @@ offset The amount which the rtc clock has been adjusted in firmware.
which are added to or removed from the rtc's base clock per
billion ticks. A positive value makes a day pass more slowly,
longer, and a negative value makes a day pass more quickly.
+range_max The maximum time values in seconds supported by RTC hardware.
+range_min The minimum time values in seconds supported by RTC hardware.
*/nvmem The non volatile storage exported as a raw file, as described
in Documentation/nvmem/nvmem.txt
================ ==============================================================
diff --git a/drivers/rtc/rtc-sysfs.c b/drivers/rtc/rtc-sysfs.c
index 92ff2ed..60e1f6c 100644
--- a/drivers/rtc/rtc-sysfs.c
+++ b/drivers/rtc/rtc-sysfs.c
@@ -248,6 +248,34 @@
}
static DEVICE_ATTR_RW(offset);

+static ssize_t
+range_max_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ ssize_t retval;
+ time64_t max_hw_secs, min_hw_secs;
+
+ retval = rtc_read_range(to_rtc_device(dev), &max_hw_secs, &min_hw_secs);
+ if (retval == 0)
+ retval = sprintf(buf, "%lld\n", max_hw_secs);
+
+ return retval;
+}
+static DEVICE_ATTR_RO(range_max);
+
+static ssize_t
+range_min_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ ssize_t retval;
+ time64_t max_hw_secs, min_hw_secs;
+
+ retval = rtc_read_range(to_rtc_device(dev), &max_hw_secs, &min_hw_secs);
+ if (retval == 0)
+ retval = sprintf(buf, "%lld\n", min_hw_secs);
+
+ return retval;
+}
+static DEVICE_ATTR_RO(range_min);
+
static struct attribute *rtc_attrs[] = {
&dev_attr_name.attr,
&dev_attr_date.attr,
@@ -257,6 +285,8 @@
&dev_attr_hctosys.attr,
&dev_attr_wakealarm.attr,
&dev_attr_offset.attr,
+ &dev_attr_range_max.attr,
+ &dev_attr_range_min.attr,
NULL,
};

--
1.7.9.5

2018-01-02 05:10:41

by Baolin Wang

[permalink] [raw]
Subject: [RFC PATCH 4/4] rtc: sc27xx: Add the get_range interface

Add the get_range interface for sc27xx RTC driver to tell the RTC
core what is the valid range for RTC hardware device.

Signed-off-by: Baolin Wang <[email protected]>
---
drivers/rtc/rtc-sc27xx.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/drivers/rtc/rtc-sc27xx.c b/drivers/rtc/rtc-sc27xx.c
index d544d52..97b1120 100644
--- a/drivers/rtc/rtc-sc27xx.c
+++ b/drivers/rtc/rtc-sc27xx.c
@@ -536,12 +536,22 @@ static int sprd_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
return ret;
}

+static int sprd_rtc_read_range(struct device *dev, time64_t *max_hw_secs,
+ time64_t *min_hw_secs)
+{
+ *min_hw_secs = 0;
+ *max_hw_secs = (((time64_t)(SPRD_RTC_DAY_MASK * 24) + 23) * 60 + 59) * 60 + 59;
+
+ return 0;
+}
+
static const struct rtc_class_ops sprd_rtc_ops = {
.read_time = sprd_rtc_read_time,
.set_time = sprd_rtc_set_time,
.read_alarm = sprd_rtc_read_alarm,
.set_alarm = sprd_rtc_set_alarm,
.alarm_irq_enable = sprd_rtc_alarm_irq_enable,
+ .read_range = sprd_rtc_read_range,
};

static irqreturn_t sprd_rtc_handler(int irq, void *dev_id)
--
1.7.9.5

2018-01-02 05:10:59

by Baolin Wang

[permalink] [raw]
Subject: [RFC PATCH 3/4] rtc: Add one offset seconds to expand RTC range

>From our investigation for all RTC drivers, 1 driver will be expired before
year 2017, 7 drivers will be expired before year 2038, 23 drivers will be
expired before year 2069, 72 drivers will be expired before 2100 and 104
drivers will be expired before 2106. Especially for these early expired
drivers, we need to expand the RTC range to make the RTC can still work
after the expired year.

So we can expand the RTC range by adding one offset to the time when reading
from hardware, and subtracting it when writing back. For example, if you have
an RTC that can do 100 years, and currently is configured to be based in
Jan 1 1970, so it can represents times from 1970 to 2069. Then if you change
the start year from 1970 to 2000, which means it can represents times from
2000 to 2099. By adding or subtracting the offset produced by moving the wrap
point, all times between 1970 and 1999 from RTC hardware could get interpreted
as times from 2070 to 2099, but the interpretation of dates between 2000 and
2069 would not change.

Signed-off-by: Baolin Wang <[email protected]>
---
drivers/rtc/class.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++
drivers/rtc/interface.c | 53 +++++++++++++++++++++++++++++++++++++++++++++--
include/linux/rtc.h | 2 ++
3 files changed, 106 insertions(+), 2 deletions(-)

diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index 31fc0f1..8e59cf0 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -211,6 +211,55 @@ static int rtc_device_get_id(struct device *dev)
return id;
}

+static void rtc_device_get_offset(struct rtc_device *rtc)
+{
+ u32 start_year;
+ int ret;
+
+ rtc->offset_secs = 0;
+ rtc->start_secs = rtc->min_hw_secs;
+
+ /*
+ * If RTC driver did not implement the range of RTC hardware device,
+ * then we can not expand the RTC range by adding or subtracting one
+ * offset.
+ */
+ if (!rtc->max_hw_secs)
+ return;
+
+ ret = device_property_read_u32(rtc->dev.parent, "start-year",
+ &start_year);
+ if (ret)
+ return;
+
+ /*
+ * Record the start time values in seconds, which are used to valid if
+ * the setting time values are in the new expanded range.
+ */
+ rtc->start_secs = max_t(time64_t, mktime64(start_year, 1, 1, 0, 0, 0),
+ rtc->min_hw_secs);
+
+ /*
+ * If the start_secs is larger than the maximum seconds (max_hw_secs)
+ * support by RTC hardware, which means the minimum seconds
+ * (min_hw_secs) of RTC hardware will be mapped to start_secs by adding
+ * one offset, so the offset seconds calculation formula should be:
+ * rtc->offset_secs = rtc->start_secs - rtc->min_hw_secs;
+ *
+ * If the start_secs is less than max_hw_secs, then there is one region
+ * is overlapped between the original RTC hardware range and the new
+ * expanded range, and this overlapped region do not need to be mapped
+ * into the new expanded range due to it is valid for RTC device. So
+ * the minimum seconds of RTC hardware (min_hw_secs) should be mapped to
+ * max_hw_secs + 1, then the offset seconds formula should be:
+ * rtc->offset_secs = rtc->max_hw_secs - rtc->min_hw_secs + 1;
+ */
+ if (rtc->start_secs > rtc->max_hw_secs)
+ rtc->offset_secs = rtc->start_secs - rtc->min_hw_secs;
+ else
+ rtc->offset_secs = rtc->max_hw_secs - rtc->min_hw_secs + 1;
+}
+
/**
* rtc_device_register - register w/ RTC class
* @dev: the device to register
@@ -253,6 +302,8 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
goto exit_ida;
}

+ rtc_device_get_offset(rtc);
+
/* Check to see if there is an ALARM already set in hw */
err = __rtc_read_alarm(rtc, &alrm);

@@ -449,6 +500,8 @@ int __rtc_register_device(struct module *owner, struct rtc_device *rtc)
return err;
}

+ rtc_device_get_offset(rtc);
+
/* Check to see if there is an ALARM already set in hw */
err = __rtc_read_alarm(rtc, &alrm);
if (!err && !rtc_valid_tm(&alrm.time))
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index c8090e3..eb96a90 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -20,6 +20,46 @@
static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);

+static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
+{
+ time64_t secs;
+
+ if (!rtc->offset_secs)
+ return;
+
+ secs = rtc_tm_to_time64(tm);
+ /*
+ * Since the reading time values from RTC device are always less than
+ * rtc->max_hw_secs, then if the reading time values are larger than
+ * the rtc->start_secs, which means they did not subtract the offset
+ * when writing into RTC device, so we do not need to add the offset.
+ */
+ if (secs >= rtc->start_secs)
+ return;
+
+ rtc_time64_to_tm(secs + rtc->offset_secs, tm);
+}
+
+static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
+{
+ time64_t secs;
+
+ if (!rtc->offset_secs)
+ return;
+
+ secs = rtc_tm_to_time64(tm);
+ /*
+ * If the setting time values are in the valid range of RTC hardware
+ * device, then no need to subtract the offset when setting time to RTC
+ * device. Otherwise we need to subtract the offset to make the time
+ * values are valid for RTC hardware device.
+ */
+ if (secs <= rtc->max_hw_secs)
+ return;
+
+ rtc_time64_to_tm(secs - rtc->offset_secs, tm);
+}
+
static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
{
int err;
@@ -36,6 +76,8 @@ static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
return err;
}

+ rtc_add_offset(rtc, tm);
+
err = rtc_valid_tm(tm);
if (err < 0)
dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
@@ -69,6 +111,8 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
if (err)
return err;

+ rtc_subtract_offset(rtc, tm);
+
err = mutex_lock_interruptible(&rtc->ops_lock);
if (err)
return err;
@@ -123,6 +167,8 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *al
}

mutex_unlock(&rtc->ops_lock);
+
+ rtc_add_offset(rtc, &alarm->time);
return err;
}

@@ -338,6 +384,7 @@ static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
if (err)
return err;

+ rtc_subtract_offset(rtc, &alarm->time);
scheduled = rtc_tm_to_time64(&alarm->time);

/* Make sure we're not setting alarms in the past */
@@ -1074,7 +1121,8 @@ int rtc_read_range(struct rtc_device *rtc, time64_t *max_hw_secs,
* @ tm: time values need to valid.
*
* Only the rtc->max_hw_secs was set, then we can valid if the setting time
- * values are beyond the RTC range.
+ * values are beyond the RTC range. When drivers set one start time values,
+ * we need to valid if the setting time values are in the new expanded range.
*/
int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
{
@@ -1084,7 +1132,8 @@ int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
return 0;

secs = rtc_tm_to_time64(tm);
- if (secs < rtc->min_hw_secs || secs > rtc->max_hw_secs)
+ if (secs < rtc->start_secs ||
+ secs > (rtc->start_secs + rtc->max_hw_secs - rtc->min_hw_secs))
return -EINVAL;

return 0;
diff --git a/include/linux/rtc.h b/include/linux/rtc.h
index 19a8989..11879b7 100644
--- a/include/linux/rtc.h
+++ b/include/linux/rtc.h
@@ -156,6 +156,8 @@ struct rtc_device {

time64_t max_hw_secs;
time64_t min_hw_secs;
+ time64_t start_secs;
+ time64_t offset_secs;

#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
struct work_struct uie_task;
--
1.7.9.5

2018-01-02 07:47:17

by Alexandre Belloni

[permalink] [raw]
Subject: Re: [RFC PATCH 1/4] rtc: Introduce one interface to save the RTC hardware time range

Hi Baolin,

Could you have a look at
https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git/commit/?h=rtc-ranges

My approach has multiple advantages as it works for 64-bit counters and
the range can be updated at runtime.

On 02/01/2018 at 13:10:05 +0800, Baolin Wang wrote:
> In order to the setting time values are not beyond the limitation
> supported by RTC hardware, we introduce one interface to tell the
> hardware range to the RTC core, which are used to valid if the
> setting time values are in the RTC hardware range.
>
> Moreover we also need the RTC hardware range to expand the RTC
> range in next patches by adding one offset.
>
> Signed-off-by: Baolin Wang <[email protected]>
> ---
> drivers/rtc/class.c | 13 ++++++++++
> drivers/rtc/interface.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++
> include/linux/rtc.h | 9 +++++++
> 3 files changed, 84 insertions(+)
>
> diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
> index 722d683..31fc0f1 100644
> --- a/drivers/rtc/class.c
> +++ b/drivers/rtc/class.c
> @@ -247,6 +247,12 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
>
> dev_set_name(&rtc->dev, "rtc%d", id);
>
> + err = rtc_read_range(rtc, &rtc->max_hw_secs, &rtc->min_hw_secs);
> + if (err) {
> + dev_err(&rtc->dev, "%s: failed to get RTC range\n", name);
> + goto exit_ida;
> + }
> +
> /* Check to see if there is an ALARM already set in hw */
> err = __rtc_read_alarm(rtc, &alrm);
>
> @@ -436,6 +442,13 @@ int __rtc_register_device(struct module *owner, struct rtc_device *rtc)
>
> rtc->owner = owner;
>
> + err = rtc_read_range(rtc, &rtc->max_hw_secs, &rtc->min_hw_secs);
> + if (err) {
> + dev_err(&rtc->dev, "%s: failed to get RTC range\n",
> + dev_name(&rtc->dev));
> + return err;
> + }
> +
> /* Check to see if there is an ALARM already set in hw */
> err = __rtc_read_alarm(rtc, &alrm);
> if (!err && !rtc_valid_tm(&alrm.time))
> diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> index 672b192..c8090e3 100644
> --- a/drivers/rtc/interface.c
> +++ b/drivers/rtc/interface.c
> @@ -65,6 +65,10 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
> if (err != 0)
> return err;
>
> + err = rtc_valid_range(rtc, tm);
> + if (err)
> + return err;
> +
> err = mutex_lock_interruptible(&rtc->ops_lock);
> if (err)
> return err;
> @@ -329,6 +333,11 @@ static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
> err = rtc_valid_tm(&alarm->time);
> if (err)
> return err;
> +
> + err = rtc_valid_range(rtc, &alarm->time);
> + if (err)
> + return err;
> +
> scheduled = rtc_tm_to_time64(&alarm->time);
>
> /* Make sure we're not setting alarms in the past */
> @@ -1027,3 +1036,56 @@ int rtc_set_offset(struct rtc_device *rtc, long offset)
> mutex_unlock(&rtc->ops_lock);
> return ret;
> }
> +
> +/* rtc_read_range - Read the max and min hardware count supported by RTC device
> + * @ rtc: rtc device to be used.
> + * @ max_hw_secs: maximum hardware count in seconds, which can represent
> + * the maximum time values in RTC hardware.
> + * @ min_hw_secs: minimum hardware count in seconds, which can represent
> + * the minimum time values in RTC hardware.
> + *
> + * The max_hw_secs and min_hw_secs implemented by user must represent the
> + * correct hardware start time and maximum time, which means the count
> + * will wrap around to min_hw_secs after the maximum count.
> + *
> + * If user did not implement the read_range() interface, we can set max_hw_secs
> + * and min_hw_secs to 0, which avoids validing the range.
> + */
> +int rtc_read_range(struct rtc_device *rtc, time64_t *max_hw_secs,
> + time64_t *min_hw_secs)
> +{
> + int ret;
> +
> + if (!rtc->ops || !rtc->ops->read_range) {
> + *max_hw_secs = 0;
> + *min_hw_secs = 0;
> + return 0;
> + }
> +
> + mutex_lock(&rtc->ops_lock);
> + ret = rtc->ops->read_range(rtc->dev.parent, max_hw_secs, min_hw_secs);
> + mutex_unlock(&rtc->ops_lock);
> +
> + return ret;
> +}
> +
> +/* rtc_valid_range - Valid if the setting time in the RTC range
> + * @ rtc: rtc device to be used.
> + * @ tm: time values need to valid.
> + *
> + * Only the rtc->max_hw_secs was set, then we can valid if the setting time
> + * values are beyond the RTC range.
> + */
> +int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
> +{
> + time64_t secs;
> +
> + if (!rtc->max_hw_secs)
> + return 0;
> +
> + secs = rtc_tm_to_time64(tm);
> + if (secs < rtc->min_hw_secs || secs > rtc->max_hw_secs)
> + return -EINVAL;
> +
> + return 0;
> +}
> diff --git a/include/linux/rtc.h b/include/linux/rtc.h
> index 41319a2..19a8989 100644
> --- a/include/linux/rtc.h
> +++ b/include/linux/rtc.h
> @@ -85,6 +85,8 @@ struct rtc_class_ops {
> int (*alarm_irq_enable)(struct device *, unsigned int enabled);
> int (*read_offset)(struct device *, long *offset);
> int (*set_offset)(struct device *, long offset);
> + int (*read_range)(struct device *, time64_t *max_hw_secs,
> + time64_t *min_hw_secs);
> };
>
> #define RTC_DEVICE_NAME_SIZE 20
> @@ -152,6 +154,9 @@ struct rtc_device {
> bool nvram_old_abi;
> struct bin_attribute *nvram;
>
> + time64_t max_hw_secs;
> + time64_t min_hw_secs;
> +
> #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
> struct work_struct uie_task;
> struct timer_list uie_timer;
> @@ -225,6 +230,10 @@ int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
> int rtc_set_offset(struct rtc_device *rtc, long offset);
> void rtc_timer_do_work(struct work_struct *work);
>
> +int rtc_read_range(struct rtc_device *rtc, time64_t *max_hw_secs,
> + time64_t *min_hw_secs);
> +int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm);
> +
> static inline bool is_leap_year(unsigned int year)
> {
> return (!(year % 4) && (year % 100)) || !(year % 400);
> --
> 1.7.9.5
>

--
Alexandre Belloni, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

2018-01-02 08:37:59

by Baolin Wang

[permalink] [raw]
Subject: Re: [RFC PATCH 1/4] rtc: Introduce one interface to save the RTC hardware time range

Hi Alexandre,

On 2 January 2018 at 15:47, Alexandre Belloni
<[email protected]> wrote:
> Hi Baolin,
>
> Could you have a look at
> https://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux.git/commit/?h=rtc-ranges
>
> My approach has multiple advantages as it works for 64-bit counters and
> the range can be updated at runtime.

Ah, I missed your approach. I will look at it. Thanks.

--
Baolin.wang
Best Regards

2018-01-02 09:50:48

by Alexandre Belloni

[permalink] [raw]
Subject: Re: [RFC PATCH 3/4] rtc: Add one offset seconds to expand RTC range

On 02/01/2018 at 13:10:07 +0800, Baolin Wang wrote:
> From our investigation for all RTC drivers, 1 driver will be expired before
> year 2017, 7 drivers will be expired before year 2038, 23 drivers will be
> expired before year 2069, 72 drivers will be expired before 2100 and 104
> drivers will be expired before 2106. Especially for these early expired
> drivers, we need to expand the RTC range to make the RTC can still work
> after the expired year.
>
> So we can expand the RTC range by adding one offset to the time when reading
> from hardware, and subtracting it when writing back. For example, if you have
> an RTC that can do 100 years, and currently is configured to be based in
> Jan 1 1970, so it can represents times from 1970 to 2069. Then if you change
> the start year from 1970 to 2000, which means it can represents times from
> 2000 to 2099. By adding or subtracting the offset produced by moving the wrap
> point, all times between 1970 and 1999 from RTC hardware could get interpreted
> as times from 2070 to 2099, but the interpretation of dates between 2000 and
> 2069 would not change.
>
> Signed-off-by: Baolin Wang <[email protected]>
> ---
> drivers/rtc/class.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++
> drivers/rtc/interface.c | 53 +++++++++++++++++++++++++++++++++++++++++++++--
> include/linux/rtc.h | 2 ++
> 3 files changed, 106 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
> index 31fc0f1..8e59cf0 100644
> --- a/drivers/rtc/class.c
> +++ b/drivers/rtc/class.c
> @@ -211,6 +211,55 @@ static int rtc_device_get_id(struct device *dev)
> return id;
> }
>
> +static void rtc_device_get_offset(struct rtc_device *rtc)
> +{
> + u32 start_year;
> + int ret;
> +
> + rtc->offset_secs = 0;
> + rtc->start_secs = rtc->min_hw_secs;
> +
> + /*
> + * If RTC driver did not implement the range of RTC hardware device,
> + * then we can not expand the RTC range by adding or subtracting one
> + * offset.
> + */
> + if (!rtc->max_hw_secs)
> + return;
> +
> + ret = device_property_read_u32(rtc->dev.parent, "start-year",
> + &start_year);
> + if (ret)
> + return;
> +

I think we need to have a way for drivers to set the start_secs value
because then we can fix all the drivers using a variation of
if (tm->tm_year < 70)
tm->tm_year += 100;

The main issue is that they will want to set start_secs to 0 so we can't
use start_secs != 0 to know whether it has already been set. Maybe we
can rely on offset_secs being set.


> + /*
> + * Record the start time values in seconds, which are used to valid if
> + * the setting time values are in the new expanded range.
> + */
> + rtc->start_secs = max_t(time64_t, mktime64(start_year, 1, 1, 0, 0, 0),
> + rtc->min_hw_secs);
> +
> + /*
> + * If the start_secs is larger than the maximum seconds (max_hw_secs)
> + * support by RTC hardware, which means the minimum seconds
> + * (min_hw_secs) of RTC hardware will be mapped to start_secs by adding
> + * one offset, so the offset seconds calculation formula should be:
> + * rtc->offset_secs = rtc->start_secs - rtc->min_hw_secs;
> + *
> + * If the start_secs is less than max_hw_secs, then there is one region
> + * is overlapped between the original RTC hardware range and the new
> + * expanded range, and this overlapped region do not need to be mapped
> + * into the new expanded range due to it is valid for RTC device. So
> + * the minimum seconds of RTC hardware (min_hw_secs) should be mapped to
> + * max_hw_secs + 1, then the offset seconds formula should be:
> + * rtc->offset_secs = rtc->max_hw_secs - rtc->min_hw_secs + 1;
> + */
> + if (rtc->start_secs > rtc->max_hw_secs)
> + rtc->offset_secs = rtc->start_secs - rtc->min_hw_secs;
> + else
> + rtc->offset_secs = rtc->max_hw_secs - rtc->min_hw_secs + 1;

And so we have the case where start_secs < rtc->min_hw_secs. Those are
the RTC failing in 2069. Wee need to handle those drivers generically
here.

> +}
> +
> /**
> * rtc_device_register - register w/ RTC class
> * @dev: the device to register
> @@ -253,6 +302,8 @@ struct rtc_device *rtc_device_register(const char *name, struct device *dev,
> goto exit_ida;
> }
>
> + rtc_device_get_offset(rtc);
> +
> /* Check to see if there is an ALARM already set in hw */
> err = __rtc_read_alarm(rtc, &alrm);
>
> @@ -449,6 +500,8 @@ int __rtc_register_device(struct module *owner, struct rtc_device *rtc)
> return err;
> }
>
> + rtc_device_get_offset(rtc);
> +
> /* Check to see if there is an ALARM already set in hw */
> err = __rtc_read_alarm(rtc, &alrm);
> if (!err && !rtc_valid_tm(&alrm.time))
> diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
> index c8090e3..eb96a90 100644
> --- a/drivers/rtc/interface.c
> +++ b/drivers/rtc/interface.c
> @@ -20,6 +20,46 @@
> static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
> static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
>
> +static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
> +{
> + time64_t secs;
> +
> + if (!rtc->offset_secs)
> + return;
> +
> + secs = rtc_tm_to_time64(tm);
> + /*
> + * Since the reading time values from RTC device are always less than
> + * rtc->max_hw_secs, then if the reading time values are larger than
> + * the rtc->start_secs, which means they did not subtract the offset
> + * when writing into RTC device, so we do not need to add the offset.
> + */
> + if (secs >= rtc->start_secs)
> + return;
> +
> + rtc_time64_to_tm(secs + rtc->offset_secs, tm);
> +}
> +
> +static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
> +{
> + time64_t secs;
> +
> + if (!rtc->offset_secs)
> + return;
> +
> + secs = rtc_tm_to_time64(tm);
> + /*
> + * If the setting time values are in the valid range of RTC hardware
> + * device, then no need to subtract the offset when setting time to RTC
> + * device. Otherwise we need to subtract the offset to make the time
> + * values are valid for RTC hardware device.
> + */
> + if (secs <= rtc->max_hw_secs)
> + return;
> +
> + rtc_time64_to_tm(secs - rtc->offset_secs, tm);
> +}
> +
> static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
> {
> int err;
> @@ -36,6 +76,8 @@ static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
> return err;
> }
>
> + rtc_add_offset(rtc, tm);
> +
> err = rtc_valid_tm(tm);
> if (err < 0)
> dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
> @@ -69,6 +111,8 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
> if (err)
> return err;
>
> + rtc_subtract_offset(rtc, tm);
> +
> err = mutex_lock_interruptible(&rtc->ops_lock);
> if (err)
> return err;
> @@ -123,6 +167,8 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *al
> }
>
> mutex_unlock(&rtc->ops_lock);
> +
> + rtc_add_offset(rtc, &alarm->time);
> return err;
> }
>
> @@ -338,6 +384,7 @@ static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
> if (err)
> return err;
>
> + rtc_subtract_offset(rtc, &alarm->time);
> scheduled = rtc_tm_to_time64(&alarm->time);
>
> /* Make sure we're not setting alarms in the past */
> @@ -1074,7 +1121,8 @@ int rtc_read_range(struct rtc_device *rtc, time64_t *max_hw_secs,
> * @ tm: time values need to valid.
> *
> * Only the rtc->max_hw_secs was set, then we can valid if the setting time
> - * values are beyond the RTC range.
> + * values are beyond the RTC range. When drivers set one start time values,
> + * we need to valid if the setting time values are in the new expanded range.
> */
> int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
> {
> @@ -1084,7 +1132,8 @@ int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
> return 0;
>
> secs = rtc_tm_to_time64(tm);
> - if (secs < rtc->min_hw_secs || secs > rtc->max_hw_secs)
> + if (secs < rtc->start_secs ||
> + secs > (rtc->start_secs + rtc->max_hw_secs - rtc->min_hw_secs))
> return -EINVAL;
>
> return 0;
> diff --git a/include/linux/rtc.h b/include/linux/rtc.h
> index 19a8989..11879b7 100644
> --- a/include/linux/rtc.h
> +++ b/include/linux/rtc.h
> @@ -156,6 +156,8 @@ struct rtc_device {
>
> time64_t max_hw_secs;
> time64_t min_hw_secs;
> + time64_t start_secs;
> + time64_t offset_secs;
>
> #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
> struct work_struct uie_task;
> --
> 1.7.9.5
>

--
Alexandre Belloni, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

2018-01-02 12:16:12

by Baolin Wang

[permalink] [raw]
Subject: Re: [RFC PATCH 3/4] rtc: Add one offset seconds to expand RTC range

On 2 January 2018 at 17:50, Alexandre Belloni
<[email protected]> wrote:
> On 02/01/2018 at 13:10:07 +0800, Baolin Wang wrote:
>> From our investigation for all RTC drivers, 1 driver will be expired before
>> year 2017, 7 drivers will be expired before year 2038, 23 drivers will be
>> expired before year 2069, 72 drivers will be expired before 2100 and 104
>> drivers will be expired before 2106. Especially for these early expired
>> drivers, we need to expand the RTC range to make the RTC can still work
>> after the expired year.
>>
>> So we can expand the RTC range by adding one offset to the time when reading
>> from hardware, and subtracting it when writing back. For example, if you have
>> an RTC that can do 100 years, and currently is configured to be based in
>> Jan 1 1970, so it can represents times from 1970 to 2069. Then if you change
>> the start year from 1970 to 2000, which means it can represents times from
>> 2000 to 2099. By adding or subtracting the offset produced by moving the wrap
>> point, all times between 1970 and 1999 from RTC hardware could get interpreted
>> as times from 2070 to 2099, but the interpretation of dates between 2000 and
>> 2069 would not change.
>>
>> Signed-off-by: Baolin Wang <[email protected]>
>> ---
>> drivers/rtc/class.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++
>> drivers/rtc/interface.c | 53 +++++++++++++++++++++++++++++++++++++++++++++--
>> include/linux/rtc.h | 2 ++
>> 3 files changed, 106 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
>> index 31fc0f1..8e59cf0 100644
>> --- a/drivers/rtc/class.c
>> +++ b/drivers/rtc/class.c
>> @@ -211,6 +211,55 @@ static int rtc_device_get_id(struct device *dev)
>> return id;
>> }
>>
>> +static void rtc_device_get_offset(struct rtc_device *rtc)
>> +{
>> + u32 start_year;
>> + int ret;
>> +
>> + rtc->offset_secs = 0;
>> + rtc->start_secs = rtc->min_hw_secs;
>> +
>> + /*
>> + * If RTC driver did not implement the range of RTC hardware device,
>> + * then we can not expand the RTC range by adding or subtracting one
>> + * offset.
>> + */
>> + if (!rtc->max_hw_secs)
>> + return;
>> +
>> + ret = device_property_read_u32(rtc->dev.parent, "start-year",
>> + &start_year);
>> + if (ret)
>> + return;
>> +
>
> I think we need to have a way for drivers to set the start_secs value
> because then we can fix all the drivers using a variation of
> if (tm->tm_year < 70)
> tm->tm_year += 100;

Yes.

>
> The main issue is that they will want to set start_secs to 0 so we can't
> use start_secs != 0 to know whether it has already been set. Maybe we
> can rely on offset_secs being set.

Make sense.

>> + /*
>> + * Record the start time values in seconds, which are used to valid if
>> + * the setting time values are in the new expanded range.
>> + */
>> + rtc->start_secs = max_t(time64_t, mktime64(start_year, 1, 1, 0, 0, 0),
>> + rtc->min_hw_secs);
>> +
>> + /*
>> + * If the start_secs is larger than the maximum seconds (max_hw_secs)
>> + * support by RTC hardware, which means the minimum seconds
>> + * (min_hw_secs) of RTC hardware will be mapped to start_secs by adding
>> + * one offset, so the offset seconds calculation formula should be:
>> + * rtc->offset_secs = rtc->start_secs - rtc->min_hw_secs;
>> + *
>> + * If the start_secs is less than max_hw_secs, then there is one region
>> + * is overlapped between the original RTC hardware range and the new
>> + * expanded range, and this overlapped region do not need to be mapped
>> + * into the new expanded range due to it is valid for RTC device. So
>> + * the minimum seconds of RTC hardware (min_hw_secs) should be mapped to
>> + * max_hw_secs + 1, then the offset seconds formula should be:
>> + * rtc->offset_secs = rtc->max_hw_secs - rtc->min_hw_secs + 1;
>> + */
>> + if (rtc->start_secs > rtc->max_hw_secs)
>> + rtc->offset_secs = rtc->start_secs - rtc->min_hw_secs;
>> + else
>> + rtc->offset_secs = rtc->max_hw_secs - rtc->min_hw_secs + 1;
>
> And so we have the case where start_secs < rtc->min_hw_secs. Those are
> the RTC failing in 2069. Wee need to handle those drivers generically
> here.

I missed this case and I will check it. Thanks for your comments.

--
Baolin.wang
Best Regards