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