xref: /linux/drivers/ptp/ptp_vmw.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
17d10001eSVivek Thampi // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
27d10001eSVivek Thampi /*
37d10001eSVivek Thampi  * Copyright (C) 2020 VMware, Inc., Palo Alto, CA., USA
47d10001eSVivek Thampi  *
57d10001eSVivek Thampi  * PTP clock driver for VMware precision clock virtual device.
67d10001eSVivek Thampi  */
77d10001eSVivek Thampi 
87d10001eSVivek Thampi #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
97d10001eSVivek Thampi 
107d10001eSVivek Thampi #include <linux/acpi.h>
117d10001eSVivek Thampi #include <linux/kernel.h>
127d10001eSVivek Thampi #include <linux/module.h>
137d10001eSVivek Thampi #include <linux/ptp_clock_kernel.h>
147d10001eSVivek Thampi #include <asm/hypervisor.h>
157d10001eSVivek Thampi #include <asm/vmware.h>
167d10001eSVivek Thampi 
177d10001eSVivek Thampi #define VMWARE_CMD_PCLK(nr) ((nr << 16) | 97)
187d10001eSVivek Thampi #define VMWARE_CMD_PCLK_GETTIME VMWARE_CMD_PCLK(0)
197d10001eSVivek Thampi 
207d10001eSVivek Thampi static struct acpi_device *ptp_vmw_acpi_device;
217d10001eSVivek Thampi static struct ptp_clock *ptp_vmw_clock;
227d10001eSVivek Thampi 
237d10001eSVivek Thampi 
ptp_vmw_pclk_read(u64 * ns)247d10001eSVivek Thampi static int ptp_vmw_pclk_read(u64 *ns)
257d10001eSVivek Thampi {
26*54651bb4SAlexey Makhalov 	u32 ret, nsec_hi, nsec_lo;
277d10001eSVivek Thampi 
28*54651bb4SAlexey Makhalov 	ret = vmware_hypercall3(VMWARE_CMD_PCLK_GETTIME, 0,
29*54651bb4SAlexey Makhalov 				&nsec_hi, &nsec_lo);
307d10001eSVivek Thampi 	if (ret == 0)
317d10001eSVivek Thampi 		*ns = ((u64)nsec_hi << 32) | nsec_lo;
327d10001eSVivek Thampi 	return ret;
337d10001eSVivek Thampi }
347d10001eSVivek Thampi 
357d10001eSVivek Thampi /*
367d10001eSVivek Thampi  * PTP clock ops.
377d10001eSVivek Thampi  */
387d10001eSVivek Thampi 
ptp_vmw_adjtime(struct ptp_clock_info * info,s64 delta)397d10001eSVivek Thampi static int ptp_vmw_adjtime(struct ptp_clock_info *info, s64 delta)
407d10001eSVivek Thampi {
417d10001eSVivek Thampi 	return -EOPNOTSUPP;
427d10001eSVivek Thampi }
437d10001eSVivek Thampi 
ptp_vmw_adjfine(struct ptp_clock_info * info,long delta)4473aa29a2SJacob Keller static int ptp_vmw_adjfine(struct ptp_clock_info *info, long delta)
457d10001eSVivek Thampi {
467d10001eSVivek Thampi 	return -EOPNOTSUPP;
477d10001eSVivek Thampi }
487d10001eSVivek Thampi 
ptp_vmw_gettime(struct ptp_clock_info * info,struct timespec64 * ts)497d10001eSVivek Thampi static int ptp_vmw_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
507d10001eSVivek Thampi {
517d10001eSVivek Thampi 	u64 ns;
527d10001eSVivek Thampi 
537d10001eSVivek Thampi 	if (ptp_vmw_pclk_read(&ns) != 0)
547d10001eSVivek Thampi 		return -EIO;
557d10001eSVivek Thampi 	*ts = ns_to_timespec64(ns);
567d10001eSVivek Thampi 	return 0;
577d10001eSVivek Thampi }
587d10001eSVivek Thampi 
ptp_vmw_settime(struct ptp_clock_info * info,const struct timespec64 * ts)597d10001eSVivek Thampi static int ptp_vmw_settime(struct ptp_clock_info *info,
607d10001eSVivek Thampi 			  const struct timespec64 *ts)
617d10001eSVivek Thampi {
627d10001eSVivek Thampi 	return -EOPNOTSUPP;
637d10001eSVivek Thampi }
647d10001eSVivek Thampi 
ptp_vmw_enable(struct ptp_clock_info * info,struct ptp_clock_request * request,int on)657d10001eSVivek Thampi static int ptp_vmw_enable(struct ptp_clock_info *info,
667d10001eSVivek Thampi 			 struct ptp_clock_request *request, int on)
677d10001eSVivek Thampi {
687d10001eSVivek Thampi 	return -EOPNOTSUPP;
697d10001eSVivek Thampi }
707d10001eSVivek Thampi 
717d10001eSVivek Thampi static struct ptp_clock_info ptp_vmw_clock_info = {
727d10001eSVivek Thampi 	.owner		= THIS_MODULE,
737d10001eSVivek Thampi 	.name		= "ptp_vmw",
747d10001eSVivek Thampi 	.max_adj	= 0,
757d10001eSVivek Thampi 	.adjtime	= ptp_vmw_adjtime,
7673aa29a2SJacob Keller 	.adjfine	= ptp_vmw_adjfine,
777d10001eSVivek Thampi 	.gettime64	= ptp_vmw_gettime,
787d10001eSVivek Thampi 	.settime64	= ptp_vmw_settime,
797d10001eSVivek Thampi 	.enable		= ptp_vmw_enable,
807d10001eSVivek Thampi };
817d10001eSVivek Thampi 
827d10001eSVivek Thampi /*
837d10001eSVivek Thampi  * ACPI driver ops for VMware "precision clock" virtual device.
847d10001eSVivek Thampi  */
857d10001eSVivek Thampi 
ptp_vmw_acpi_add(struct acpi_device * device)867d10001eSVivek Thampi static int ptp_vmw_acpi_add(struct acpi_device *device)
877d10001eSVivek Thampi {
887d10001eSVivek Thampi 	ptp_vmw_clock = ptp_clock_register(&ptp_vmw_clock_info, NULL);
897d10001eSVivek Thampi 	if (IS_ERR(ptp_vmw_clock)) {
907d10001eSVivek Thampi 		pr_err("failed to register ptp clock\n");
917d10001eSVivek Thampi 		return PTR_ERR(ptp_vmw_clock);
927d10001eSVivek Thampi 	}
937d10001eSVivek Thampi 
947d10001eSVivek Thampi 	ptp_vmw_acpi_device = device;
957d10001eSVivek Thampi 	return 0;
967d10001eSVivek Thampi }
977d10001eSVivek Thampi 
ptp_vmw_acpi_remove(struct acpi_device * device)986c0eb5baSDawei Li static void ptp_vmw_acpi_remove(struct acpi_device *device)
997d10001eSVivek Thampi {
1007d10001eSVivek Thampi 	ptp_clock_unregister(ptp_vmw_clock);
1017d10001eSVivek Thampi }
1027d10001eSVivek Thampi 
1037d10001eSVivek Thampi static const struct acpi_device_id ptp_vmw_acpi_device_ids[] = {
1047d10001eSVivek Thampi 	{ "VMW0005", 0 },
1057d10001eSVivek Thampi 	{ "", 0 },
1067d10001eSVivek Thampi };
1077d10001eSVivek Thampi 
1087d10001eSVivek Thampi MODULE_DEVICE_TABLE(acpi, ptp_vmw_acpi_device_ids);
1097d10001eSVivek Thampi 
1107d10001eSVivek Thampi static struct acpi_driver ptp_vmw_acpi_driver = {
1117d10001eSVivek Thampi 	.name = "ptp_vmw",
1127d10001eSVivek Thampi 	.ids = ptp_vmw_acpi_device_ids,
1137d10001eSVivek Thampi 	.ops = {
1147d10001eSVivek Thampi 		.add = ptp_vmw_acpi_add,
1157d10001eSVivek Thampi 		.remove	= ptp_vmw_acpi_remove
1167d10001eSVivek Thampi 	},
1177d10001eSVivek Thampi };
1187d10001eSVivek Thampi 
ptp_vmw_init(void)1197d10001eSVivek Thampi static int __init ptp_vmw_init(void)
1207d10001eSVivek Thampi {
1217d10001eSVivek Thampi 	if (x86_hyper_type != X86_HYPER_VMWARE)
1227d10001eSVivek Thampi 		return -1;
1237d10001eSVivek Thampi 	return acpi_bus_register_driver(&ptp_vmw_acpi_driver);
1247d10001eSVivek Thampi }
1257d10001eSVivek Thampi 
ptp_vmw_exit(void)1267d10001eSVivek Thampi static void __exit ptp_vmw_exit(void)
1277d10001eSVivek Thampi {
1287d10001eSVivek Thampi 	acpi_bus_unregister_driver(&ptp_vmw_acpi_driver);
1297d10001eSVivek Thampi }
1307d10001eSVivek Thampi 
1317d10001eSVivek Thampi module_init(ptp_vmw_init);
1327d10001eSVivek Thampi module_exit(ptp_vmw_exit);
1337d10001eSVivek Thampi 
1347d10001eSVivek Thampi MODULE_DESCRIPTION("VMware virtual PTP clock driver");
1357d10001eSVivek Thampi MODULE_AUTHOR("VMware, Inc.");
1367d10001eSVivek Thampi MODULE_LICENSE("Dual BSD/GPL");
137