Commit 9d4f22fd authored by Peter Hilber's avatar Peter Hilber Committed by Michael S. Tsirkin
Browse files

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: default avatarPeter Hilber <quic_philber@quicinc.com>
Acked-by: default avatarAlexandre Belloni <alexandre.belloni@bootlin.com>
Message-Id: <20250509160734.1772-5-quic_philber@quicinc.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent e2ef1675
Loading
Loading
Loading
Loading
+19 −3
Original line number Diff line number Diff line
@@ -195,7 +195,8 @@ config VIRTIO_RTC
	help
	 This driver provides current time from a Virtio RTC device. The driver
	 provides the time through one or more clocks. The Virtio RTC PTP
	 clocks must be enabled to expose the clocks to userspace.
	 clocks and/or the Real Time Clock driver for Virtio RTC must be
	 enabled to expose the clocks to userspace.

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

if VIRTIO_RTC

comment "WARNING: Consider enabling VIRTIO_RTC_PTP."
	depends on !VIRTIO_RTC_PTP
comment "WARNING: Consider enabling VIRTIO_RTC_PTP and/or VIRTIO_RTC_CLASS."
	depends on !VIRTIO_RTC_PTP && !VIRTIO_RTC_CLASS

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

	 If unsure, say Y.

comment "Enable RTC_CLASS in order to enable VIRTIO_RTC_CLASS."
	depends on RTC_CLASS=n

config VIRTIO_RTC_CLASS
	bool "Real Time Clock driver for Virtio RTC"
	default y
	depends on RTC_CLASS
	help
	 This exposes the Virtio RTC UTC-like clock as a Linux Real Time Clock.
	 It only has an effect if the Virtio RTC device has a UTC-like clock
	 which smears leap seconds to avoid steps. The Real Time Clock is
	 read-only, and may support setting an alarm.

	 If unsure, say Y.

endif # VIRTIO_RTC

endif # VIRTIO_MENU
+1 −0
Original line number Diff line number Diff line
@@ -18,3 +18,4 @@ obj-$(CONFIG_VIRTIO_RTC) += virtio_rtc.o
virtio_rtc-y := virtio_rtc_driver.o
virtio_rtc-$(CONFIG_VIRTIO_RTC_PTP) += virtio_rtc_ptp.o
virtio_rtc-$(CONFIG_VIRTIO_RTC_ARM) += virtio_rtc_arm.o
virtio_rtc-$(CONFIG_VIRTIO_RTC_CLASS) += virtio_rtc_class.o
+262 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * virtio_rtc RTC class driver
 *
 * Copyright (C) 2023 OpenSynergy GmbH
 * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
 */

#include <linux/math64.h>
#include <linux/overflow.h>
#include <linux/rtc.h>
#include <linux/time64.h>

#include <uapi/linux/virtio_rtc.h>

#include "virtio_rtc_internal.h"

/**
 * struct viortc_class - RTC class wrapper
 * @viortc: virtio_rtc device data
 * @rtc: RTC device
 * @vio_clk_id: virtio_rtc clock id
 * @stopped: Whether RTC ops are disallowed. Access protected by rtc_lock().
 */
struct viortc_class {
	struct viortc_dev *viortc;
	struct rtc_device *rtc;
	u16 vio_clk_id;
	bool stopped;
};

/**
 * viortc_class_get_locked() - get RTC class wrapper, if ops allowed
 * @dev: virtio device
 *
 * Gets the RTC class wrapper from the virtio device, if it is available and
 * ops are allowed.
 *
 * Context: Caller must hold rtc_lock().
 * Return: RTC class wrapper if available and ops allowed, ERR_PTR otherwise.
 */
static struct viortc_class *viortc_class_get_locked(struct device *dev)
{
	struct viortc_class *viortc_class;

	viortc_class = viortc_class_from_dev(dev);
	if (IS_ERR(viortc_class))
		return viortc_class;

	if (viortc_class->stopped)
		return ERR_PTR(-EBUSY);

	return viortc_class;
}

/**
 * viortc_class_read_time() - RTC class op read_time
 * @dev: virtio device
 * @tm: read time
 *
 * Context: Process context.
 * Return: Zero on success, negative error code otherwise.
 */
static int viortc_class_read_time(struct device *dev, struct rtc_time *tm)
{
	struct viortc_class *viortc_class;
	time64_t sec;
	int ret;
	u64 ns;

	viortc_class = viortc_class_get_locked(dev);
	if (IS_ERR(viortc_class))
		return PTR_ERR(viortc_class);

	ret = viortc_read(viortc_class->viortc, viortc_class->vio_clk_id, &ns);
	if (ret)
		return ret;

	sec = div_u64(ns, NSEC_PER_SEC);

	rtc_time64_to_tm(sec, tm);

	return 0;
}

/**
 * viortc_class_read_alarm() - RTC class op read_alarm
 * @dev: virtio device
 * @alrm: alarm read out
 *
 * Context: Process context.
 * Return: Zero on success, negative error code otherwise.
 */
static int viortc_class_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct viortc_class *viortc_class;
	time64_t alarm_time_sec;
	u64 alarm_time_ns;
	bool enabled;
	int ret;

	viortc_class = viortc_class_get_locked(dev);
	if (IS_ERR(viortc_class))
		return PTR_ERR(viortc_class);

	ret = viortc_read_alarm(viortc_class->viortc, viortc_class->vio_clk_id,
				&alarm_time_ns, &enabled);
	if (ret)
		return ret;

	alarm_time_sec = div_u64(alarm_time_ns, NSEC_PER_SEC);
	rtc_time64_to_tm(alarm_time_sec, &alrm->time);

	alrm->enabled = enabled;

	return 0;
}

/**
 * viortc_class_set_alarm() - RTC class op set_alarm
 * @dev: virtio device
 * @alrm: alarm to set
 *
 * Context: Process context.
 * Return: Zero on success, negative error code otherwise.
 */
static int viortc_class_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	struct viortc_class *viortc_class;
	time64_t alarm_time_sec;
	u64 alarm_time_ns;

	viortc_class = viortc_class_get_locked(dev);
	if (IS_ERR(viortc_class))
		return PTR_ERR(viortc_class);

	alarm_time_sec = rtc_tm_to_time64(&alrm->time);

	if (alarm_time_sec < 0)
		return -EINVAL;

	if (check_mul_overflow((u64)alarm_time_sec, (u64)NSEC_PER_SEC,
			       &alarm_time_ns))
		return -EINVAL;

	return viortc_set_alarm(viortc_class->viortc, viortc_class->vio_clk_id,
				alarm_time_ns, alrm->enabled);
}

/**
 * viortc_class_alarm_irq_enable() - RTC class op alarm_irq_enable
 * @dev: virtio device
 * @enabled: enable or disable alarm IRQ
 *
 * Context: Process context.
 * Return: Zero on success, negative error code otherwise.
 */
static int viortc_class_alarm_irq_enable(struct device *dev,
					 unsigned int enabled)
{
	struct viortc_class *viortc_class;

	viortc_class = viortc_class_get_locked(dev);
	if (IS_ERR(viortc_class))
		return PTR_ERR(viortc_class);

	return viortc_set_alarm_enabled(viortc_class->viortc,
					viortc_class->vio_clk_id, enabled);
}

static const struct rtc_class_ops viortc_class_ops = {
	.read_time = viortc_class_read_time,
	.read_alarm = viortc_class_read_alarm,
	.set_alarm = viortc_class_set_alarm,
	.alarm_irq_enable = viortc_class_alarm_irq_enable,
};

/**
 * viortc_class_alarm() - propagate alarm notification as alarm interrupt
 * @viortc_class: RTC class wrapper
 * @vio_clk_id: virtio_rtc clock id
 *
 * Context: Any context.
 */
void viortc_class_alarm(struct viortc_class *viortc_class, u16 vio_clk_id)
{
	if (vio_clk_id != viortc_class->vio_clk_id) {
		dev_warn_ratelimited(&viortc_class->rtc->dev,
				     "ignoring alarm for clock id %d, expected id %d\n",
				     vio_clk_id, viortc_class->vio_clk_id);
		return;
	}

	rtc_update_irq(viortc_class->rtc, 1, RTC_AF | RTC_IRQF);
}

/**
 * viortc_class_stop() - disallow RTC class ops
 * @viortc_class: RTC class wrapper
 *
 * Context: Process context. Caller must NOT hold rtc_lock().
 */
void viortc_class_stop(struct viortc_class *viortc_class)
{
	rtc_lock(viortc_class->rtc);

	viortc_class->stopped = true;

	rtc_unlock(viortc_class->rtc);
}

/**
 * viortc_class_register() - register RTC class device
 * @viortc_class: RTC class wrapper
 *
 * Context: Process context.
 * Return: Zero on success, negative error code otherwise.
 */
int viortc_class_register(struct viortc_class *viortc_class)
{
	return devm_rtc_register_device(viortc_class->rtc);
}

/**
 * viortc_class_init() - init RTC class wrapper and device
 * @viortc: device data
 * @vio_clk_id: virtio_rtc clock id
 * @have_alarm: have alarm feature
 * @parent_dev: virtio device
 *
 * Context: Process context.
 * Return: RTC class wrapper on success, ERR_PTR otherwise.
 */
struct viortc_class *viortc_class_init(struct viortc_dev *viortc,
				       u16 vio_clk_id, bool have_alarm,
				       struct device *parent_dev)
{
	struct viortc_class *viortc_class;
	struct rtc_device *rtc;

	viortc_class =
		devm_kzalloc(parent_dev, sizeof(*viortc_class), GFP_KERNEL);
	if (!viortc_class)
		return ERR_PTR(-ENOMEM);

	rtc = devm_rtc_allocate_device(parent_dev);
	if (IS_ERR(rtc))
		return ERR_CAST(rtc);

	viortc_class->viortc = viortc;
	viortc_class->rtc = rtc;
	viortc_class->vio_clk_id = vio_clk_id;

	if (!have_alarm)
		clear_bit(RTC_FEATURE_ALARM, rtc->features);
	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features);

	rtc->ops = &viortc_class_ops;
	rtc->range_max = div_u64(U64_MAX, NSEC_PER_SEC);

	return viortc_class;
}
+513 −7

File changed.

Preview size limit exceeded, changes collapsed.

+52 −0
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@
#ifndef _VIRTIO_RTC_INTERNAL_H_
#define _VIRTIO_RTC_INTERNAL_H_

#include <linux/device.h>
#include <linux/err.h>
#include <linux/ptp_clock_kernel.h>
#include <linux/types.h>

@@ -21,6 +23,16 @@ int viortc_read_cross(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter,
		      u64 *reading, u64 *cycles);
int viortc_cross_cap(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter,
		     bool *supported);
int viortc_read_alarm(struct viortc_dev *viortc, u16 vio_clk_id,
		      u64 *alarm_time, bool *enabled);
int viortc_set_alarm(struct viortc_dev *viortc, u16 vio_clk_id, u64 alarm_time,
		     bool alarm_enable);
int viortc_set_alarm_enabled(struct viortc_dev *viortc, u16 vio_clk_id,
			     bool alarm_enable);

struct viortc_class;

struct viortc_class *viortc_class_from_dev(struct device *dev);

/* PTP IFs */

@@ -67,4 +79,44 @@ static inline int viortc_ptp_unregister(struct viortc_ptp_clock *vio_ptp,
 */
int viortc_hw_xtstamp_params(u8 *hw_counter, enum clocksource_ids *cs_id);

/* RTC class IFs */

#if IS_ENABLED(CONFIG_VIRTIO_RTC_CLASS)

void viortc_class_alarm(struct viortc_class *viortc_class, u16 vio_clk_id);

void viortc_class_stop(struct viortc_class *viortc_class);

int viortc_class_register(struct viortc_class *viortc_class);

struct viortc_class *viortc_class_init(struct viortc_dev *viortc,
				       u16 vio_clk_id, bool have_alarm,
				       struct device *parent_dev);

#else /* CONFIG_VIRTIO_RTC_CLASS */

static inline void viortc_class_alarm(struct viortc_class *viortc_class,
				      u16 vio_clk_id)
{
}

static inline void viortc_class_stop(struct viortc_class *viortc_class)
{
}

static inline int viortc_class_register(struct viortc_class *viortc_class)
{
	return -ENODEV;
}

static inline struct viortc_class *viortc_class_init(struct viortc_dev *viortc,
						     u16 vio_clk_id,
						     bool have_alarm,
						     struct device *parent_dev)
{
	return ERR_PTR(-ENODEV);
}

#endif /* CONFIG_VIRTIO_RTC_CLASS */

#endif /* _VIRTIO_RTC_INTERNAL_H_ */
Loading