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_MAGIC 0x564D5868 187d10001eSVivek Thampi #define VMWARE_CMD_PCLK(nr) ((nr << 16) | 97) 197d10001eSVivek Thampi #define VMWARE_CMD_PCLK_GETTIME VMWARE_CMD_PCLK(0) 207d10001eSVivek Thampi 217d10001eSVivek Thampi static struct acpi_device *ptp_vmw_acpi_device; 227d10001eSVivek Thampi static struct ptp_clock *ptp_vmw_clock; 237d10001eSVivek Thampi 247d10001eSVivek Thampi 257d10001eSVivek Thampi static int ptp_vmw_pclk_read(u64 *ns) 267d10001eSVivek Thampi { 277d10001eSVivek Thampi u32 ret, nsec_hi, nsec_lo, unused1, unused2, unused3; 287d10001eSVivek Thampi 297d10001eSVivek Thampi asm volatile (VMWARE_HYPERCALL : 307d10001eSVivek Thampi "=a"(ret), "=b"(nsec_hi), "=c"(nsec_lo), "=d"(unused1), 317d10001eSVivek Thampi "=S"(unused2), "=D"(unused3) : 327d10001eSVivek Thampi "a"(VMWARE_MAGIC), "b"(0), 337d10001eSVivek Thampi "c"(VMWARE_CMD_PCLK_GETTIME), "d"(0) : 347d10001eSVivek Thampi "memory"); 357d10001eSVivek Thampi 367d10001eSVivek Thampi if (ret == 0) 377d10001eSVivek Thampi *ns = ((u64)nsec_hi << 32) | nsec_lo; 387d10001eSVivek Thampi return ret; 397d10001eSVivek Thampi } 407d10001eSVivek Thampi 417d10001eSVivek Thampi /* 427d10001eSVivek Thampi * PTP clock ops. 437d10001eSVivek Thampi */ 447d10001eSVivek Thampi 457d10001eSVivek Thampi static int ptp_vmw_adjtime(struct ptp_clock_info *info, s64 delta) 467d10001eSVivek Thampi { 477d10001eSVivek Thampi return -EOPNOTSUPP; 487d10001eSVivek Thampi } 497d10001eSVivek Thampi 507d10001eSVivek Thampi static int ptp_vmw_adjfreq(struct ptp_clock_info *info, s32 delta) 517d10001eSVivek Thampi { 527d10001eSVivek Thampi return -EOPNOTSUPP; 537d10001eSVivek Thampi } 547d10001eSVivek Thampi 557d10001eSVivek Thampi static int ptp_vmw_gettime(struct ptp_clock_info *info, struct timespec64 *ts) 567d10001eSVivek Thampi { 577d10001eSVivek Thampi u64 ns; 587d10001eSVivek Thampi 597d10001eSVivek Thampi if (ptp_vmw_pclk_read(&ns) != 0) 607d10001eSVivek Thampi return -EIO; 617d10001eSVivek Thampi *ts = ns_to_timespec64(ns); 627d10001eSVivek Thampi return 0; 637d10001eSVivek Thampi } 647d10001eSVivek Thampi 657d10001eSVivek Thampi static int ptp_vmw_settime(struct ptp_clock_info *info, 667d10001eSVivek Thampi const struct timespec64 *ts) 677d10001eSVivek Thampi { 687d10001eSVivek Thampi return -EOPNOTSUPP; 697d10001eSVivek Thampi } 707d10001eSVivek Thampi 717d10001eSVivek Thampi static int ptp_vmw_enable(struct ptp_clock_info *info, 727d10001eSVivek Thampi struct ptp_clock_request *request, int on) 737d10001eSVivek Thampi { 747d10001eSVivek Thampi return -EOPNOTSUPP; 757d10001eSVivek Thampi } 767d10001eSVivek Thampi 777d10001eSVivek Thampi static struct ptp_clock_info ptp_vmw_clock_info = { 787d10001eSVivek Thampi .owner = THIS_MODULE, 797d10001eSVivek Thampi .name = "ptp_vmw", 807d10001eSVivek Thampi .max_adj = 0, 817d10001eSVivek Thampi .adjtime = ptp_vmw_adjtime, 827d10001eSVivek Thampi .adjfreq = ptp_vmw_adjfreq, 837d10001eSVivek Thampi .gettime64 = ptp_vmw_gettime, 847d10001eSVivek Thampi .settime64 = ptp_vmw_settime, 857d10001eSVivek Thampi .enable = ptp_vmw_enable, 867d10001eSVivek Thampi }; 877d10001eSVivek Thampi 887d10001eSVivek Thampi /* 897d10001eSVivek Thampi * ACPI driver ops for VMware "precision clock" virtual device. 907d10001eSVivek Thampi */ 917d10001eSVivek Thampi 927d10001eSVivek Thampi static int ptp_vmw_acpi_add(struct acpi_device *device) 937d10001eSVivek Thampi { 947d10001eSVivek Thampi ptp_vmw_clock = ptp_clock_register(&ptp_vmw_clock_info, NULL); 957d10001eSVivek Thampi if (IS_ERR(ptp_vmw_clock)) { 967d10001eSVivek Thampi pr_err("failed to register ptp clock\n"); 977d10001eSVivek Thampi return PTR_ERR(ptp_vmw_clock); 987d10001eSVivek Thampi } 997d10001eSVivek Thampi 1007d10001eSVivek Thampi ptp_vmw_acpi_device = device; 1017d10001eSVivek Thampi return 0; 1027d10001eSVivek Thampi } 1037d10001eSVivek Thampi 104*6c0eb5baSDawei Li static void ptp_vmw_acpi_remove(struct acpi_device *device) 1057d10001eSVivek Thampi { 1067d10001eSVivek Thampi ptp_clock_unregister(ptp_vmw_clock); 1077d10001eSVivek Thampi } 1087d10001eSVivek Thampi 1097d10001eSVivek Thampi static const struct acpi_device_id ptp_vmw_acpi_device_ids[] = { 1107d10001eSVivek Thampi { "VMW0005", 0 }, 1117d10001eSVivek Thampi { "", 0 }, 1127d10001eSVivek Thampi }; 1137d10001eSVivek Thampi 1147d10001eSVivek Thampi MODULE_DEVICE_TABLE(acpi, ptp_vmw_acpi_device_ids); 1157d10001eSVivek Thampi 1167d10001eSVivek Thampi static struct acpi_driver ptp_vmw_acpi_driver = { 1177d10001eSVivek Thampi .name = "ptp_vmw", 1187d10001eSVivek Thampi .ids = ptp_vmw_acpi_device_ids, 1197d10001eSVivek Thampi .ops = { 1207d10001eSVivek Thampi .add = ptp_vmw_acpi_add, 1217d10001eSVivek Thampi .remove = ptp_vmw_acpi_remove 1227d10001eSVivek Thampi }, 1237d10001eSVivek Thampi .owner = THIS_MODULE 1247d10001eSVivek Thampi }; 1257d10001eSVivek Thampi 1267d10001eSVivek Thampi static int __init ptp_vmw_init(void) 1277d10001eSVivek Thampi { 1287d10001eSVivek Thampi if (x86_hyper_type != X86_HYPER_VMWARE) 1297d10001eSVivek Thampi return -1; 1307d10001eSVivek Thampi return acpi_bus_register_driver(&ptp_vmw_acpi_driver); 1317d10001eSVivek Thampi } 1327d10001eSVivek Thampi 1337d10001eSVivek Thampi static void __exit ptp_vmw_exit(void) 1347d10001eSVivek Thampi { 1357d10001eSVivek Thampi acpi_bus_unregister_driver(&ptp_vmw_acpi_driver); 1367d10001eSVivek Thampi } 1377d10001eSVivek Thampi 1387d10001eSVivek Thampi module_init(ptp_vmw_init); 1397d10001eSVivek Thampi module_exit(ptp_vmw_exit); 1407d10001eSVivek Thampi 1417d10001eSVivek Thampi MODULE_DESCRIPTION("VMware virtual PTP clock driver"); 1427d10001eSVivek Thampi MODULE_AUTHOR("VMware, Inc."); 1437d10001eSVivek Thampi MODULE_LICENSE("Dual BSD/GPL"); 144