1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
2 /*
3 * Copyright (C) 2020 VMware, Inc., Palo Alto, CA., USA
4 *
5 * PTP clock driver for VMware precision clock virtual device.
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/acpi.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/ptp_clock_kernel.h>
14 #include <asm/hypervisor.h>
15 #include <asm/vmware.h>
16
17 #define VMWARE_CMD_PCLK(nr) ((nr << 16) | 97)
18 #define VMWARE_CMD_PCLK_GETTIME VMWARE_CMD_PCLK(0)
19
20 static struct acpi_device *ptp_vmw_acpi_device;
21 static struct ptp_clock *ptp_vmw_clock;
22
23
ptp_vmw_pclk_read(u64 * ns)24 static int ptp_vmw_pclk_read(u64 *ns)
25 {
26 u32 ret, nsec_hi, nsec_lo;
27
28 ret = vmware_hypercall3(VMWARE_CMD_PCLK_GETTIME, 0,
29 &nsec_hi, &nsec_lo);
30 if (ret == 0)
31 *ns = ((u64)nsec_hi << 32) | nsec_lo;
32 return ret;
33 }
34
35 /*
36 * PTP clock ops.
37 */
38
ptp_vmw_adjtime(struct ptp_clock_info * info,s64 delta)39 static int ptp_vmw_adjtime(struct ptp_clock_info *info, s64 delta)
40 {
41 return -EOPNOTSUPP;
42 }
43
ptp_vmw_adjfine(struct ptp_clock_info * info,long delta)44 static int ptp_vmw_adjfine(struct ptp_clock_info *info, long delta)
45 {
46 return -EOPNOTSUPP;
47 }
48
ptp_vmw_gettime(struct ptp_clock_info * info,struct timespec64 * ts)49 static int ptp_vmw_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
50 {
51 u64 ns;
52
53 if (ptp_vmw_pclk_read(&ns) != 0)
54 return -EIO;
55 *ts = ns_to_timespec64(ns);
56 return 0;
57 }
58
ptp_vmw_settime(struct ptp_clock_info * info,const struct timespec64 * ts)59 static int ptp_vmw_settime(struct ptp_clock_info *info,
60 const struct timespec64 *ts)
61 {
62 return -EOPNOTSUPP;
63 }
64
ptp_vmw_enable(struct ptp_clock_info * info,struct ptp_clock_request * request,int on)65 static int ptp_vmw_enable(struct ptp_clock_info *info,
66 struct ptp_clock_request *request, int on)
67 {
68 return -EOPNOTSUPP;
69 }
70
71 static struct ptp_clock_info ptp_vmw_clock_info = {
72 .owner = THIS_MODULE,
73 .name = "ptp_vmw",
74 .max_adj = 0,
75 .adjtime = ptp_vmw_adjtime,
76 .adjfine = ptp_vmw_adjfine,
77 .gettime64 = ptp_vmw_gettime,
78 .settime64 = ptp_vmw_settime,
79 .enable = ptp_vmw_enable,
80 };
81
82 /*
83 * ACPI driver ops for VMware "precision clock" virtual device.
84 */
85
ptp_vmw_acpi_add(struct acpi_device * device)86 static int ptp_vmw_acpi_add(struct acpi_device *device)
87 {
88 ptp_vmw_clock = ptp_clock_register(&ptp_vmw_clock_info, NULL);
89 if (IS_ERR(ptp_vmw_clock)) {
90 pr_err("failed to register ptp clock\n");
91 return PTR_ERR(ptp_vmw_clock);
92 }
93
94 ptp_vmw_acpi_device = device;
95 return 0;
96 }
97
ptp_vmw_acpi_remove(struct acpi_device * device)98 static void ptp_vmw_acpi_remove(struct acpi_device *device)
99 {
100 ptp_clock_unregister(ptp_vmw_clock);
101 }
102
103 static const struct acpi_device_id ptp_vmw_acpi_device_ids[] = {
104 { "VMW0005", 0 },
105 { "", 0 },
106 };
107
108 MODULE_DEVICE_TABLE(acpi, ptp_vmw_acpi_device_ids);
109
110 static struct acpi_driver ptp_vmw_acpi_driver = {
111 .name = "ptp_vmw",
112 .ids = ptp_vmw_acpi_device_ids,
113 .ops = {
114 .add = ptp_vmw_acpi_add,
115 .remove = ptp_vmw_acpi_remove
116 },
117 };
118
ptp_vmw_init(void)119 static int __init ptp_vmw_init(void)
120 {
121 if (x86_hyper_type != X86_HYPER_VMWARE)
122 return -1;
123 return acpi_bus_register_driver(&ptp_vmw_acpi_driver);
124 }
125
ptp_vmw_exit(void)126 static void __exit ptp_vmw_exit(void)
127 {
128 acpi_bus_unregister_driver(&ptp_vmw_acpi_driver);
129 }
130
131 module_init(ptp_vmw_init);
132 module_exit(ptp_vmw_exit);
133
134 MODULE_DESCRIPTION("VMware virtual PTP clock driver");
135 MODULE_AUTHOR("VMware, Inc.");
136 MODULE_LICENSE("Dual BSD/GPL");
137