Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 2 | /* |
| 3 | * Virtual PTP 1588 clock for use with KVM guests |
| 4 | * |
| 5 | * Copyright (C) 2017 Red Hat Inc. |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/device.h> |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/kernel.h> |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 11 | #include <linux/slab.h> |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 12 | #include <linux/module.h> |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 13 | #include <linux/ptp_kvm.h> |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 14 | #include <uapi/linux/kvm_para.h> |
| 15 | #include <asm/kvm_para.h> |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 16 | #include <uapi/asm/kvm_para.h> |
| 17 | |
| 18 | #include <linux/ptp_clock_kernel.h> |
| 19 | |
| 20 | struct kvm_ptp_clock { |
| 21 | struct ptp_clock *ptp_clock; |
| 22 | struct ptp_clock_info caps; |
| 23 | }; |
| 24 | |
YueHaibing | 0673f97 | 2020-04-18 09:51:54 +0800 | [diff] [blame] | 25 | static DEFINE_SPINLOCK(kvm_ptp_lock); |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 26 | |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 27 | static int ptp_kvm_get_time_fn(ktime_t *device_time, |
| 28 | struct system_counterval_t *system_counter, |
| 29 | void *ctx) |
| 30 | { |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 31 | long ret; |
| 32 | u64 cycle; |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 33 | struct timespec64 tspec; |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 34 | struct clocksource *cs; |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 35 | |
| 36 | spin_lock(&kvm_ptp_lock); |
| 37 | |
| 38 | preempt_disable_notrace(); |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 39 | ret = kvm_arch_ptp_get_crosststamp(&cycle, &tspec, &cs); |
| 40 | if (ret) { |
| 41 | spin_unlock(&kvm_ptp_lock); |
| 42 | preempt_enable_notrace(); |
| 43 | return ret; |
| 44 | } |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 45 | |
| 46 | preempt_enable_notrace(); |
| 47 | |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 48 | system_counter->cycles = cycle; |
| 49 | system_counter->cs = cs; |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 50 | |
| 51 | *device_time = timespec64_to_ktime(tspec); |
| 52 | |
| 53 | spin_unlock(&kvm_ptp_lock); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int ptp_kvm_getcrosststamp(struct ptp_clock_info *ptp, |
| 59 | struct system_device_crosststamp *xtstamp) |
| 60 | { |
| 61 | return get_device_system_crosststamp(ptp_kvm_get_time_fn, NULL, |
| 62 | NULL, xtstamp); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * PTP clock operations |
| 67 | */ |
| 68 | |
| 69 | static int ptp_kvm_adjfreq(struct ptp_clock_info *ptp, s32 ppb) |
| 70 | { |
| 71 | return -EOPNOTSUPP; |
| 72 | } |
| 73 | |
| 74 | static int ptp_kvm_adjtime(struct ptp_clock_info *ptp, s64 delta) |
| 75 | { |
| 76 | return -EOPNOTSUPP; |
| 77 | } |
| 78 | |
| 79 | static int ptp_kvm_settime(struct ptp_clock_info *ptp, |
| 80 | const struct timespec64 *ts) |
| 81 | { |
| 82 | return -EOPNOTSUPP; |
| 83 | } |
| 84 | |
| 85 | static int ptp_kvm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts) |
| 86 | { |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 87 | long ret; |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 88 | struct timespec64 tspec; |
| 89 | |
| 90 | spin_lock(&kvm_ptp_lock); |
| 91 | |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 92 | ret = kvm_arch_ptp_get_clock(&tspec); |
| 93 | if (ret) { |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 94 | spin_unlock(&kvm_ptp_lock); |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 95 | return ret; |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 96 | } |
| 97 | |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 98 | spin_unlock(&kvm_ptp_lock); |
| 99 | |
| 100 | memcpy(ts, &tspec, sizeof(struct timespec64)); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int ptp_kvm_enable(struct ptp_clock_info *ptp, |
| 106 | struct ptp_clock_request *rq, int on) |
| 107 | { |
| 108 | return -EOPNOTSUPP; |
| 109 | } |
| 110 | |
Bhumika Goyal | 7d47e9a | 2017-08-21 23:01:12 +0530 | [diff] [blame] | 111 | static const struct ptp_clock_info ptp_kvm_caps = { |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 112 | .owner = THIS_MODULE, |
| 113 | .name = "KVM virtual PTP", |
| 114 | .max_adj = 0, |
| 115 | .n_ext_ts = 0, |
| 116 | .n_pins = 0, |
| 117 | .pps = 0, |
| 118 | .adjfreq = ptp_kvm_adjfreq, |
| 119 | .adjtime = ptp_kvm_adjtime, |
| 120 | .gettime64 = ptp_kvm_gettime, |
| 121 | .settime64 = ptp_kvm_settime, |
| 122 | .enable = ptp_kvm_enable, |
| 123 | .getcrosststamp = ptp_kvm_getcrosststamp, |
| 124 | }; |
| 125 | |
| 126 | /* module operations */ |
| 127 | |
| 128 | static struct kvm_ptp_clock kvm_ptp_clock; |
| 129 | |
| 130 | static void __exit ptp_kvm_exit(void) |
| 131 | { |
| 132 | ptp_clock_unregister(kvm_ptp_clock.ptp_clock); |
| 133 | } |
| 134 | |
| 135 | static int __init ptp_kvm_init(void) |
| 136 | { |
Radim Krčmář | 4306f20 | 2017-02-15 20:27:20 +0100 | [diff] [blame] | 137 | long ret; |
| 138 | |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 139 | ret = kvm_arch_ptp_init(); |
| 140 | if (ret) { |
Jon Hunter | a86ed2c | 2021-04-20 14:24:19 +0100 | [diff] [blame] | 141 | if (ret != -EOPNOTSUPP) |
| 142 | pr_err("fail to initialize ptp_kvm"); |
Jianyong Wu | a8cf291 | 2020-12-09 14:09:26 +0800 | [diff] [blame] | 143 | return ret; |
| 144 | } |
Radim Krčmář | 4306f20 | 2017-02-15 20:27:20 +0100 | [diff] [blame] | 145 | |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 146 | kvm_ptp_clock.caps = ptp_kvm_caps; |
| 147 | |
| 148 | kvm_ptp_clock.ptp_clock = ptp_clock_register(&kvm_ptp_clock.caps, NULL); |
| 149 | |
kbuild test robot | 49e190e | 2017-01-28 05:01:51 +0800 | [diff] [blame] | 150 | return PTR_ERR_OR_ZERO(kvm_ptp_clock.ptp_clock); |
Marcelo Tosatti | a0e136d | 2017-01-24 15:09:42 -0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | module_init(ptp_kvm_init); |
| 154 | module_exit(ptp_kvm_exit); |
| 155 | |
| 156 | MODULE_AUTHOR("Marcelo Tosatti <mtosatti@redhat.com>"); |
| 157 | MODULE_DESCRIPTION("PTP clock using KVMCLOCK"); |
| 158 | MODULE_LICENSE("GPL"); |