For scenerios when ACPI is disabled, allow ptp_vmw driver to be loaded by directly probing for the device using VMware hypercalls. VMware precision clock virtual device is exposed as a platform ACPI device in its virtual chipset hardware. Its driver - ptp_vmw - is registered with the ACPI bus for discovery and binding. On systems where ACPI is disabled, such as virtual machines optimized for fast boot times, this means that the device is not discoverable and cannot be loaded. Since the device operations are performed via VMware hypercalls, the ACPI sub-system can be by-passed and manually loaded. Cc: Shubham Gupta Cc: Nick Shi Tested-by: Karen Wang Tested-by: Hari Krishna Ginka Signed-off-by: Ajay Kaher --- drivers/ptp/ptp_vmw.c | 71 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/drivers/ptp/ptp_vmw.c b/drivers/ptp/ptp_vmw.c index a18ba729e..ce44b12ce 100644 --- a/drivers/ptp/ptp_vmw.c +++ b/drivers/ptp/ptp_vmw.c @@ -98,25 +98,41 @@ static struct ptp_clock_info ptp_vmw_clock_info = { .enable = ptp_vmw_enable, }; +static int ptp_vmw_clock_register(void) +{ + ptp_vmw_clock = ptp_clock_register(&ptp_vmw_clock_info, NULL); + if (IS_ERR(ptp_vmw_clock)) { + pr_err("ptp_vmw: Failed to register ptp clock\n"); + return PTR_ERR(ptp_vmw_clock); + } + pr_debug("ptp_vmw: ptp clock registered\n"); + return 0; +} + +static void ptp_vmw_clock_unregister(void) +{ + ptp_clock_unregister(ptp_vmw_clock); + ptp_vmw_clock = NULL; + pr_debug("ptp_vmw: ptp clock unregistered\n"); +} + /* * ACPI driver ops for VMware "precision clock" virtual device. */ static int ptp_vmw_acpi_add(struct acpi_device *device) { - ptp_vmw_clock = ptp_clock_register(&ptp_vmw_clock_info, NULL); - if (IS_ERR(ptp_vmw_clock)) { - pr_err("failed to register ptp clock\n"); - return PTR_ERR(ptp_vmw_clock); - } + int ret = ptp_vmw_clock_register(); - ptp_vmw_acpi_device = device; - return 0; + if (ret == 0) + ptp_vmw_acpi_device = device; + return ret; } static void ptp_vmw_acpi_remove(struct acpi_device *device) { - ptp_clock_unregister(ptp_vmw_clock); + ptp_vmw_clock_unregister(); + ptp_vmw_acpi_device = NULL; } static const struct acpi_device_id ptp_vmw_acpi_device_ids[] = { @@ -135,16 +151,47 @@ static struct acpi_driver ptp_vmw_acpi_driver = { }, }; +/* + * Probe existence of device by poking at a command. If successful, + * register as a PTP clock. This is a fallback option for when ACPI + * is not available. + */ +static int ptp_vmw_probe(void) +{ + u64 ns; + + return ptp_vmw_pclk_read(VMWARE_CMD_PCLK_GETTIME, &ns); +} + static int __init ptp_vmw_init(void) { - if (x86_hyper_type != X86_HYPER_VMWARE) - return -1; - return acpi_bus_register_driver(&ptp_vmw_acpi_driver); + + int error = -ENODEV; + + if (x86_hyper_type != X86_HYPER_VMWARE) { + error = -EINVAL; + goto out; + } + + if (!acpi_disabled) { + error = acpi_bus_register_driver(&ptp_vmw_acpi_driver); + if (!error) + goto out; + } + + if (!ptp_vmw_probe()) + error = ptp_vmw_clock_register(); + +out: + return error; } static void __exit ptp_vmw_exit(void) { - acpi_bus_unregister_driver(&ptp_vmw_acpi_driver); + if (!acpi_disabled && ptp_vmw_acpi_device) + acpi_bus_unregister_driver(&ptp_vmw_acpi_driver); + else if (ptp_vmw_clock) + ptp_vmw_clock_unregister(); } module_init(ptp_vmw_init); -- 2.40.4