ptp_vmw.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/acpi.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/ptp_clock_kernel.h>
  12. #include <asm/hypervisor.h>
  13. #include <asm/vmware.h>
  14. #define VMWARE_MAGIC 0x564D5868
  15. #define VMWARE_CMD_PCLK(nr) ((nr << 16) | 97)
  16. #define VMWARE_CMD_PCLK_GETTIME VMWARE_CMD_PCLK(0)
  17. static struct acpi_device *ptp_vmw_acpi_device;
  18. static struct ptp_clock *ptp_vmw_clock;
  19. static int ptp_vmw_pclk_read(u64 *ns)
  20. {
  21. u32 ret, nsec_hi, nsec_lo, unused1, unused2, unused3;
  22. asm volatile (VMWARE_HYPERCALL :
  23. "=a"(ret), "=b"(nsec_hi), "=c"(nsec_lo), "=d"(unused1),
  24. "=S"(unused2), "=D"(unused3) :
  25. "a"(VMWARE_MAGIC), "b"(0),
  26. "c"(VMWARE_CMD_PCLK_GETTIME), "d"(0) :
  27. "memory");
  28. if (ret == 0)
  29. *ns = ((u64)nsec_hi << 32) | nsec_lo;
  30. return ret;
  31. }
  32. /*
  33. * PTP clock ops.
  34. */
  35. static int ptp_vmw_adjtime(struct ptp_clock_info *info, s64 delta)
  36. {
  37. return -EOPNOTSUPP;
  38. }
  39. static int ptp_vmw_adjfreq(struct ptp_clock_info *info, s32 delta)
  40. {
  41. return -EOPNOTSUPP;
  42. }
  43. static int ptp_vmw_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
  44. {
  45. u64 ns;
  46. if (ptp_vmw_pclk_read(&ns) != 0)
  47. return -EIO;
  48. *ts = ns_to_timespec64(ns);
  49. return 0;
  50. }
  51. static int ptp_vmw_settime(struct ptp_clock_info *info,
  52. const struct timespec64 *ts)
  53. {
  54. return -EOPNOTSUPP;
  55. }
  56. static int ptp_vmw_enable(struct ptp_clock_info *info,
  57. struct ptp_clock_request *request, int on)
  58. {
  59. return -EOPNOTSUPP;
  60. }
  61. static struct ptp_clock_info ptp_vmw_clock_info = {
  62. .owner = THIS_MODULE,
  63. .name = "ptp_vmw",
  64. .max_adj = 0,
  65. .adjtime = ptp_vmw_adjtime,
  66. .adjfreq = ptp_vmw_adjfreq,
  67. .gettime64 = ptp_vmw_gettime,
  68. .settime64 = ptp_vmw_settime,
  69. .enable = ptp_vmw_enable,
  70. };
  71. /*
  72. * ACPI driver ops for VMware "precision clock" virtual device.
  73. */
  74. static int ptp_vmw_acpi_add(struct acpi_device *device)
  75. {
  76. ptp_vmw_clock = ptp_clock_register(&ptp_vmw_clock_info, NULL);
  77. if (IS_ERR(ptp_vmw_clock)) {
  78. pr_err("failed to register ptp clock\n");
  79. return PTR_ERR(ptp_vmw_clock);
  80. }
  81. ptp_vmw_acpi_device = device;
  82. return 0;
  83. }
  84. static int ptp_vmw_acpi_remove(struct acpi_device *device)
  85. {
  86. ptp_clock_unregister(ptp_vmw_clock);
  87. return 0;
  88. }
  89. static const struct acpi_device_id ptp_vmw_acpi_device_ids[] = {
  90. { "VMW0005", 0 },
  91. { "", 0 },
  92. };
  93. MODULE_DEVICE_TABLE(acpi, ptp_vmw_acpi_device_ids);
  94. static struct acpi_driver ptp_vmw_acpi_driver = {
  95. .name = "ptp_vmw",
  96. .ids = ptp_vmw_acpi_device_ids,
  97. .ops = {
  98. .add = ptp_vmw_acpi_add,
  99. .remove = ptp_vmw_acpi_remove
  100. },
  101. .owner = THIS_MODULE
  102. };
  103. static int __init ptp_vmw_init(void)
  104. {
  105. if (x86_hyper_type != X86_HYPER_VMWARE)
  106. return -1;
  107. return acpi_bus_register_driver(&ptp_vmw_acpi_driver);
  108. }
  109. static void __exit ptp_vmw_exit(void)
  110. {
  111. acpi_bus_unregister_driver(&ptp_vmw_acpi_driver);
  112. }
  113. module_init(ptp_vmw_init);
  114. module_exit(ptp_vmw_exit);
  115. MODULE_DESCRIPTION("VMware virtual PTP clock driver");
  116. MODULE_AUTHOR("VMware, Inc.");
  117. MODULE_LICENSE("Dual BSD/GPL");