Skip to content

Commit 9d4f22f

Browse files
quic-philbermstsirkin
authored andcommitted
virtio_rtc: Add RTC class driver
Expose the virtio-rtc UTC-like clock as an RTC clock to userspace - if it is present, and if it does not step on leap seconds. The RTC class enables the virtio-rtc device to resume the system from sleep states on RTC alarm. Support RTC alarm if the virtio-rtc alarm feature is present. The virtio-rtc device signals an alarm by marking an alarmq buffer as used. Peculiarities ------------- A virtio-rtc clock is a bit special for an RTC clock in that - the clock may step (also backwards) autonomously at any time and - the device, and its notification mechanism, will be reset during boot or resume from sleep. The virtio-rtc device avoids that the driver might miss an alarm. The device signals an alarm whenever the clock has reached or passed the alarm time, and also when the device is reset (on boot or resume from sleep), if the alarm time is in the past. Open Issue ---------- The CLOCK_BOOTTIME_ALARM will use the RTC clock to wake up from sleep, and implicitly assumes that no RTC clock steps will occur during sleep. The RTC class driver does not know whether the current alarm is a real-time alarm or a boot-time alarm. Perhaps this might be handled by the driver also setting a virtio-rtc monotonic alarm (which uses a clock similar to CLOCK_BOOTTIME_ALARM). The virtio-rtc monotonic alarm would just be used to wake up in case it was a CLOCK_BOOTTIME_ALARM alarm. Otherwise, the behavior should not differ from other RTC class drivers. Signed-off-by: Peter Hilber <quic_philber@quicinc.com> Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Message-Id: <20250509160734.1772-5-quic_philber@quicinc.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent e2ef167 commit 9d4f22f

File tree

6 files changed

+934
-11
lines changed

6 files changed

+934
-11
lines changed

drivers/virtio/Kconfig

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ config VIRTIO_RTC
195195
help
196196
This driver provides current time from a Virtio RTC device. The driver
197197
provides the time through one or more clocks. The Virtio RTC PTP
198-
clocks must be enabled to expose the clocks to userspace.
198+
clocks and/or the Real Time Clock driver for Virtio RTC must be
199+
enabled to expose the clocks to userspace.
199200

200201
To compile this code as a module, choose M here: the module will be
201202
called virtio_rtc.
@@ -204,8 +205,8 @@ config VIRTIO_RTC
204205

205206
if VIRTIO_RTC
206207

207-
comment "WARNING: Consider enabling VIRTIO_RTC_PTP."
208-
depends on !VIRTIO_RTC_PTP
208+
comment "WARNING: Consider enabling VIRTIO_RTC_PTP and/or VIRTIO_RTC_CLASS."
209+
depends on !VIRTIO_RTC_PTP && !VIRTIO_RTC_CLASS
209210

210211
comment "Enable PTP_1588_CLOCK in order to enable VIRTIO_RTC_PTP."
211212
depends on PTP_1588_CLOCK=n
@@ -234,6 +235,21 @@ config VIRTIO_RTC_ARM
234235

235236
If unsure, say Y.
236237

238+
comment "Enable RTC_CLASS in order to enable VIRTIO_RTC_CLASS."
239+
depends on RTC_CLASS=n
240+
241+
config VIRTIO_RTC_CLASS
242+
bool "Real Time Clock driver for Virtio RTC"
243+
default y
244+
depends on RTC_CLASS
245+
help
246+
This exposes the Virtio RTC UTC-like clock as a Linux Real Time Clock.
247+
It only has an effect if the Virtio RTC device has a UTC-like clock
248+
which smears leap seconds to avoid steps. The Real Time Clock is
249+
read-only, and may support setting an alarm.
250+
251+
If unsure, say Y.
252+
237253
endif # VIRTIO_RTC
238254

239255
endif # VIRTIO_MENU

drivers/virtio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ obj-$(CONFIG_VIRTIO_RTC) += virtio_rtc.o
1818
virtio_rtc-y := virtio_rtc_driver.o
1919
virtio_rtc-$(CONFIG_VIRTIO_RTC_PTP) += virtio_rtc_ptp.o
2020
virtio_rtc-$(CONFIG_VIRTIO_RTC_ARM) += virtio_rtc_arm.o
21+
virtio_rtc-$(CONFIG_VIRTIO_RTC_CLASS) += virtio_rtc_class.o

drivers/virtio/virtio_rtc_class.c

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* virtio_rtc RTC class driver
4+
*
5+
* Copyright (C) 2023 OpenSynergy GmbH
6+
* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
7+
*/
8+
9+
#include <linux/math64.h>
10+
#include <linux/overflow.h>
11+
#include <linux/rtc.h>
12+
#include <linux/time64.h>
13+
14+
#include <uapi/linux/virtio_rtc.h>
15+
16+
#include "virtio_rtc_internal.h"
17+
18+
/**
19+
* struct viortc_class - RTC class wrapper
20+
* @viortc: virtio_rtc device data
21+
* @rtc: RTC device
22+
* @vio_clk_id: virtio_rtc clock id
23+
* @stopped: Whether RTC ops are disallowed. Access protected by rtc_lock().
24+
*/
25+
struct viortc_class {
26+
struct viortc_dev *viortc;
27+
struct rtc_device *rtc;
28+
u16 vio_clk_id;
29+
bool stopped;
30+
};
31+
32+
/**
33+
* viortc_class_get_locked() - get RTC class wrapper, if ops allowed
34+
* @dev: virtio device
35+
*
36+
* Gets the RTC class wrapper from the virtio device, if it is available and
37+
* ops are allowed.
38+
*
39+
* Context: Caller must hold rtc_lock().
40+
* Return: RTC class wrapper if available and ops allowed, ERR_PTR otherwise.
41+
*/
42+
static struct viortc_class *viortc_class_get_locked(struct device *dev)
43+
{
44+
struct viortc_class *viortc_class;
45+
46+
viortc_class = viortc_class_from_dev(dev);
47+
if (IS_ERR(viortc_class))
48+
return viortc_class;
49+
50+
if (viortc_class->stopped)
51+
return ERR_PTR(-EBUSY);
52+
53+
return viortc_class;
54+
}
55+
56+
/**
57+
* viortc_class_read_time() - RTC class op read_time
58+
* @dev: virtio device
59+
* @tm: read time
60+
*
61+
* Context: Process context.
62+
* Return: Zero on success, negative error code otherwise.
63+
*/
64+
static int viortc_class_read_time(struct device *dev, struct rtc_time *tm)
65+
{
66+
struct viortc_class *viortc_class;
67+
time64_t sec;
68+
int ret;
69+
u64 ns;
70+
71+
viortc_class = viortc_class_get_locked(dev);
72+
if (IS_ERR(viortc_class))
73+
return PTR_ERR(viortc_class);
74+
75+
ret = viortc_read(viortc_class->viortc, viortc_class->vio_clk_id, &ns);
76+
if (ret)
77+
return ret;
78+
79+
sec = div_u64(ns, NSEC_PER_SEC);
80+
81+
rtc_time64_to_tm(sec, tm);
82+
83+
return 0;
84+
}
85+
86+
/**
87+
* viortc_class_read_alarm() - RTC class op read_alarm
88+
* @dev: virtio device
89+
* @alrm: alarm read out
90+
*
91+
* Context: Process context.
92+
* Return: Zero on success, negative error code otherwise.
93+
*/
94+
static int viortc_class_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
95+
{
96+
struct viortc_class *viortc_class;
97+
time64_t alarm_time_sec;
98+
u64 alarm_time_ns;
99+
bool enabled;
100+
int ret;
101+
102+
viortc_class = viortc_class_get_locked(dev);
103+
if (IS_ERR(viortc_class))
104+
return PTR_ERR(viortc_class);
105+
106+
ret = viortc_read_alarm(viortc_class->viortc, viortc_class->vio_clk_id,
107+
&alarm_time_ns, &enabled);
108+
if (ret)
109+
return ret;
110+
111+
alarm_time_sec = div_u64(alarm_time_ns, NSEC_PER_SEC);
112+
rtc_time64_to_tm(alarm_time_sec, &alrm->time);
113+
114+
alrm->enabled = enabled;
115+
116+
return 0;
117+
}
118+
119+
/**
120+
* viortc_class_set_alarm() - RTC class op set_alarm
121+
* @dev: virtio device
122+
* @alrm: alarm to set
123+
*
124+
* Context: Process context.
125+
* Return: Zero on success, negative error code otherwise.
126+
*/
127+
static int viortc_class_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
128+
{
129+
struct viortc_class *viortc_class;
130+
time64_t alarm_time_sec;
131+
u64 alarm_time_ns;
132+
133+
viortc_class = viortc_class_get_locked(dev);
134+
if (IS_ERR(viortc_class))
135+
return PTR_ERR(viortc_class);
136+
137+
alarm_time_sec = rtc_tm_to_time64(&alrm->time);
138+
139+
if (alarm_time_sec < 0)
140+
return -EINVAL;
141+
142+
if (check_mul_overflow((u64)alarm_time_sec, (u64)NSEC_PER_SEC,
143+
&alarm_time_ns))
144+
return -EINVAL;
145+
146+
return viortc_set_alarm(viortc_class->viortc, viortc_class->vio_clk_id,
147+
alarm_time_ns, alrm->enabled);
148+
}
149+
150+
/**
151+
* viortc_class_alarm_irq_enable() - RTC class op alarm_irq_enable
152+
* @dev: virtio device
153+
* @enabled: enable or disable alarm IRQ
154+
*
155+
* Context: Process context.
156+
* Return: Zero on success, negative error code otherwise.
157+
*/
158+
static int viortc_class_alarm_irq_enable(struct device *dev,
159+
unsigned int enabled)
160+
{
161+
struct viortc_class *viortc_class;
162+
163+
viortc_class = viortc_class_get_locked(dev);
164+
if (IS_ERR(viortc_class))
165+
return PTR_ERR(viortc_class);
166+
167+
return viortc_set_alarm_enabled(viortc_class->viortc,
168+
viortc_class->vio_clk_id, enabled);
169+
}
170+
171+
static const struct rtc_class_ops viortc_class_ops = {
172+
.read_time = viortc_class_read_time,
173+
.read_alarm = viortc_class_read_alarm,
174+
.set_alarm = viortc_class_set_alarm,
175+
.alarm_irq_enable = viortc_class_alarm_irq_enable,
176+
};
177+
178+
/**
179+
* viortc_class_alarm() - propagate alarm notification as alarm interrupt
180+
* @viortc_class: RTC class wrapper
181+
* @vio_clk_id: virtio_rtc clock id
182+
*
183+
* Context: Any context.
184+
*/
185+
void viortc_class_alarm(struct viortc_class *viortc_class, u16 vio_clk_id)
186+
{
187+
if (vio_clk_id != viortc_class->vio_clk_id) {
188+
dev_warn_ratelimited(&viortc_class->rtc->dev,
189+
"ignoring alarm for clock id %d, expected id %d\n",
190+
vio_clk_id, viortc_class->vio_clk_id);
191+
return;
192+
}
193+
194+
rtc_update_irq(viortc_class->rtc, 1, RTC_AF | RTC_IRQF);
195+
}
196+
197+
/**
198+
* viortc_class_stop() - disallow RTC class ops
199+
* @viortc_class: RTC class wrapper
200+
*
201+
* Context: Process context. Caller must NOT hold rtc_lock().
202+
*/
203+
void viortc_class_stop(struct viortc_class *viortc_class)
204+
{
205+
rtc_lock(viortc_class->rtc);
206+
207+
viortc_class->stopped = true;
208+
209+
rtc_unlock(viortc_class->rtc);
210+
}
211+
212+
/**
213+
* viortc_class_register() - register RTC class device
214+
* @viortc_class: RTC class wrapper
215+
*
216+
* Context: Process context.
217+
* Return: Zero on success, negative error code otherwise.
218+
*/
219+
int viortc_class_register(struct viortc_class *viortc_class)
220+
{
221+
return devm_rtc_register_device(viortc_class->rtc);
222+
}
223+
224+
/**
225+
* viortc_class_init() - init RTC class wrapper and device
226+
* @viortc: device data
227+
* @vio_clk_id: virtio_rtc clock id
228+
* @have_alarm: have alarm feature
229+
* @parent_dev: virtio device
230+
*
231+
* Context: Process context.
232+
* Return: RTC class wrapper on success, ERR_PTR otherwise.
233+
*/
234+
struct viortc_class *viortc_class_init(struct viortc_dev *viortc,
235+
u16 vio_clk_id, bool have_alarm,
236+
struct device *parent_dev)
237+
{
238+
struct viortc_class *viortc_class;
239+
struct rtc_device *rtc;
240+
241+
viortc_class =
242+
devm_kzalloc(parent_dev, sizeof(*viortc_class), GFP_KERNEL);
243+
if (!viortc_class)
244+
return ERR_PTR(-ENOMEM);
245+
246+
rtc = devm_rtc_allocate_device(parent_dev);
247+
if (IS_ERR(rtc))
248+
return ERR_CAST(rtc);
249+
250+
viortc_class->viortc = viortc;
251+
viortc_class->rtc = rtc;
252+
viortc_class->vio_clk_id = vio_clk_id;
253+
254+
if (!have_alarm)
255+
clear_bit(RTC_FEATURE_ALARM, rtc->features);
256+
clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);
257+
258+
rtc->ops = &viortc_class_ops;
259+
rtc->range_max = div_u64(U64_MAX, NSEC_PER_SEC);
260+
261+
return viortc_class;
262+
}

0 commit comments

Comments
 (0)