1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Virtual PTP 1588 clock for use with LM-safe VMclock device. 4 * 5 * Copyright © 2024 Amazon.com, Inc. or its affiliates. 6 */ 7 8 #include "linux/poll.h" 9 #include "linux/types.h" 10 #include "linux/wait.h" 11 #include <linux/acpi.h> 12 #include <linux/device.h> 13 #include <linux/err.h> 14 #include <linux/file.h> 15 #include <linux/fs.h> 16 #include <linux/init.h> 17 #include <linux/io.h> 18 #include <linux/interrupt.h> 19 #include <linux/kernel.h> 20 #include <linux/miscdevice.h> 21 #include <linux/mm.h> 22 #include <linux/module.h> 23 #include <linux/of.h> 24 #include <linux/platform_device.h> 25 #include <linux/slab.h> 26 27 #include <uapi/linux/vmclock-abi.h> 28 29 #include <linux/ptp_clock_kernel.h> 30 31 #ifdef CONFIG_X86 32 #include <asm/pvclock.h> 33 #include <asm/kvmclock.h> 34 #endif 35 36 #ifdef CONFIG_KVM_GUEST 37 #define SUPPORT_KVMCLOCK 38 #endif 39 40 static DEFINE_IDA(vmclock_ida); 41 42 ACPI_MODULE_NAME("vmclock"); 43 44 struct vmclock_state { 45 struct resource res; 46 struct vmclock_abi *clk; 47 struct miscdevice miscdev; 48 wait_queue_head_t disrupt_wait; 49 struct ptp_clock_info ptp_clock_info; 50 struct ptp_clock *ptp_clock; 51 enum clocksource_ids cs_id, sys_cs_id; 52 int index; 53 char *name; 54 }; 55 56 #define VMCLOCK_MAX_WAIT ms_to_ktime(100) 57 58 /* Require at least the flags field to be present. All else can be optional. */ 59 #define VMCLOCK_MIN_SIZE offsetof(struct vmclock_abi, pad) 60 61 #define VMCLOCK_FIELD_PRESENT(_c, _f) \ 62 (le32_to_cpu((_c)->size) >= (offsetof(struct vmclock_abi, _f) + \ 63 sizeof((_c)->_f))) 64 65 /* 66 * Multiply a 64-bit count by a 64-bit tick 'period' in units of seconds >> 64 67 * and add the fractional second part of the reference time. 68 * 69 * The result is a 128-bit value, the top 64 bits of which are seconds, and 70 * the low 64 bits are (seconds >> 64). 71 */ 72 static uint64_t mul_u64_u64_shr_add_u64(uint64_t *res_hi, uint64_t delta, 73 uint64_t period, uint8_t shift, 74 uint64_t frac_sec) 75 { 76 unsigned __int128 res = (unsigned __int128)delta * period; 77 78 res >>= shift; 79 res += frac_sec; 80 *res_hi = res >> 64; 81 return (uint64_t)res; 82 } 83 84 static bool tai_adjust(struct vmclock_abi *clk, uint64_t *sec) 85 { 86 if (clk->time_type == VMCLOCK_TIME_TAI) 87 return true; 88 89 if (clk->time_type == VMCLOCK_TIME_UTC && 90 (le64_to_cpu(clk->flags) & VMCLOCK_FLAG_TAI_OFFSET_VALID)) { 91 if (sec) 92 *sec -= (int16_t)le16_to_cpu(clk->tai_offset_sec); 93 return true; 94 } 95 return false; 96 } 97 98 static int vmclock_get_crosststamp(struct vmclock_state *st, 99 struct ptp_system_timestamp *sts, 100 struct system_counterval_t *system_counter, 101 struct timespec64 *tspec) 102 { 103 ktime_t deadline = ktime_add(ktime_get(), VMCLOCK_MAX_WAIT); 104 uint64_t cycle, delta, seq, frac_sec; 105 106 #ifdef CONFIG_X86 107 /* 108 * We'd expect the hypervisor to know this and to report the clock 109 * status as VMCLOCK_STATUS_UNRELIABLE. But be paranoid. 110 */ 111 if (check_tsc_unstable()) 112 return -EINVAL; 113 #endif 114 115 while (1) { 116 seq = le32_to_cpu(st->clk->seq_count) & ~1ULL; 117 118 /* 119 * This pairs with a write barrier in the hypervisor 120 * which populates this structure. 121 */ 122 virt_rmb(); 123 124 if (st->clk->clock_status == VMCLOCK_STATUS_UNRELIABLE) 125 return -EINVAL; 126 127 /* 128 * When invoked for gettimex64(), fill in the pre/post system 129 * times. The simple case is when system time is based on the 130 * same counter as st->cs_id, in which case all three times 131 * will be derived from the *same* counter value. 132 * 133 * If the system isn't using the same counter, then the value 134 * from ptp_read_system_prets() will still be used as pre_ts, 135 * and ptp_read_system_postts() is called to populate postts 136 * after calling get_cycles(). 137 */ 138 if (sts) { 139 ptp_read_system_prets(sts); 140 if (sts->pre_sts.cs_id == st->cs_id) { 141 cycle = sts->pre_sts.cycles; 142 sts->post_sts = sts->pre_sts; 143 } else if (sts->pre_sts.hw_csid == st->cs_id && 144 sts->pre_sts.hw_cycles) { 145 cycle = sts->pre_sts.hw_cycles; 146 sts->post_sts = sts->pre_sts; 147 } else { 148 cycle = get_cycles(); 149 ptp_read_system_postts(sts); 150 } 151 } else { 152 cycle = get_cycles(); 153 } 154 155 delta = cycle - le64_to_cpu(st->clk->counter_value); 156 157 frac_sec = mul_u64_u64_shr_add_u64(&tspec->tv_sec, delta, 158 le64_to_cpu(st->clk->counter_period_frac_sec), 159 st->clk->counter_period_shift, 160 le64_to_cpu(st->clk->time_frac_sec)); 161 tspec->tv_nsec = mul_u64_u64_shr(frac_sec, NSEC_PER_SEC, 64); 162 tspec->tv_sec += le64_to_cpu(st->clk->time_sec); 163 164 if (!tai_adjust(st->clk, &tspec->tv_sec)) 165 return -EINVAL; 166 167 /* 168 * This pairs with a write barrier in the hypervisor 169 * which populates this structure. 170 */ 171 virt_rmb(); 172 if (seq == le32_to_cpu(st->clk->seq_count)) 173 break; 174 175 if (ktime_after(ktime_get(), deadline)) 176 return -ETIMEDOUT; 177 } 178 179 if (system_counter) { 180 system_counter->cycles = cycle; 181 system_counter->cs_id = st->cs_id; 182 } 183 184 return 0; 185 } 186 187 #ifdef SUPPORT_KVMCLOCK 188 /* 189 * In the case where the system is using the KVM clock for timekeeping, convert 190 * the TSC value into a KVM clock time in order to return a paired reading that 191 * get_device_system_crosststamp() can cope with. 192 */ 193 static int vmclock_get_crosststamp_kvmclock(struct vmclock_state *st, 194 struct ptp_system_timestamp *sts, 195 struct system_counterval_t *system_counter, 196 struct timespec64 *tspec) 197 { 198 struct pvclock_vcpu_time_info *pvti = this_cpu_pvti(); 199 unsigned int pvti_ver; 200 int ret; 201 202 preempt_disable_notrace(); 203 204 do { 205 pvti_ver = pvclock_read_begin(pvti); 206 207 ret = vmclock_get_crosststamp(st, sts, system_counter, tspec); 208 if (ret) 209 break; 210 211 system_counter->cycles = __pvclock_read_cycles(pvti, 212 system_counter->cycles); 213 system_counter->cs_id = CSID_X86_KVM_CLK; 214 215 /* 216 * This retry should never really happen; if the TSC is 217 * stable and reliable enough across vCPUS that it is sane 218 * for the hypervisor to expose a VMCLOCK device which uses 219 * it as the reference counter, then the KVM clock sohuld be 220 * in 'master clock mode' and basically never changed. But 221 * the KVM clock is a fickle and often broken thing, so do 222 * it "properly" just in case. 223 */ 224 } while (pvclock_read_retry(pvti, pvti_ver)); 225 226 preempt_enable_notrace(); 227 228 return ret; 229 } 230 #endif 231 232 static int ptp_vmclock_get_time_fn(ktime_t *device_time, 233 struct system_counterval_t *system_counter, 234 void *ctx) 235 { 236 struct vmclock_state *st = ctx; 237 struct timespec64 tspec; 238 int ret; 239 240 #ifdef SUPPORT_KVMCLOCK 241 if (READ_ONCE(st->sys_cs_id) == CSID_X86_KVM_CLK) 242 ret = vmclock_get_crosststamp_kvmclock(st, NULL, system_counter, 243 &tspec); 244 else 245 #endif 246 ret = vmclock_get_crosststamp(st, NULL, system_counter, &tspec); 247 248 if (!ret) 249 *device_time = timespec64_to_ktime(tspec); 250 251 return ret; 252 } 253 254 static int ptp_vmclock_getcrosststamp(struct ptp_clock_info *ptp, 255 struct system_device_crosststamp *xtstamp) 256 { 257 struct vmclock_state *st = container_of(ptp, struct vmclock_state, 258 ptp_clock_info); 259 int ret = get_device_system_crosststamp(ptp_vmclock_get_time_fn, st, 260 NULL, xtstamp); 261 #ifdef SUPPORT_KVMCLOCK 262 /* 263 * On x86, the KVM clock may be used for the system time. We can 264 * actually convert a TSC reading to that, and return a paired 265 * timestamp that get_device_system_crosststamp() *can* handle. 266 */ 267 if (ret == -ENODEV) { 268 struct system_time_snapshot systime_snapshot; 269 270 ktime_get_snapshot_id(CLOCK_REALTIME, &systime_snapshot); 271 272 if (systime_snapshot.cs_id == CSID_X86_TSC || 273 systime_snapshot.cs_id == CSID_X86_KVM_CLK) { 274 WRITE_ONCE(st->sys_cs_id, systime_snapshot.cs_id); 275 ret = get_device_system_crosststamp(ptp_vmclock_get_time_fn, 276 st, NULL, xtstamp); 277 } 278 } 279 #endif 280 return ret; 281 } 282 283 /* 284 * PTP clock operations 285 */ 286 287 static int ptp_vmclock_adjfine(struct ptp_clock_info *ptp, long delta) 288 { 289 return -EOPNOTSUPP; 290 } 291 292 static int ptp_vmclock_adjtime(struct ptp_clock_info *ptp, s64 delta) 293 { 294 return -EOPNOTSUPP; 295 } 296 297 static int ptp_vmclock_settime(struct ptp_clock_info *ptp, 298 const struct timespec64 *ts) 299 { 300 return -EOPNOTSUPP; 301 } 302 303 static int ptp_vmclock_gettimex(struct ptp_clock_info *ptp, struct timespec64 *ts, 304 struct ptp_system_timestamp *sts) 305 { 306 struct vmclock_state *st = container_of(ptp, struct vmclock_state, 307 ptp_clock_info); 308 309 return vmclock_get_crosststamp(st, sts, NULL, ts); 310 } 311 312 static int ptp_vmclock_enable(struct ptp_clock_info *ptp, 313 struct ptp_clock_request *rq, int on) 314 { 315 return -EOPNOTSUPP; 316 } 317 318 static const struct ptp_clock_info ptp_vmclock_info = { 319 .owner = THIS_MODULE, 320 .max_adj = 0, 321 .n_ext_ts = 0, 322 .n_pins = 0, 323 .pps = 0, 324 .adjfine = ptp_vmclock_adjfine, 325 .adjtime = ptp_vmclock_adjtime, 326 .gettimex64 = ptp_vmclock_gettimex, 327 .settime64 = ptp_vmclock_settime, 328 .enable = ptp_vmclock_enable, 329 .getcrosststamp = ptp_vmclock_getcrosststamp, 330 }; 331 332 static struct ptp_clock *vmclock_ptp_register(struct device *dev, 333 struct vmclock_state *st) 334 { 335 enum clocksource_ids cs_id; 336 337 if (IS_ENABLED(CONFIG_ARM64) && 338 st->clk->counter_id == VMCLOCK_COUNTER_ARM_VCNT) { 339 /* Can we check it's the virtual counter? */ 340 cs_id = CSID_ARM_ARCH_COUNTER; 341 } else if (IS_ENABLED(CONFIG_X86) && 342 st->clk->counter_id == VMCLOCK_COUNTER_X86_TSC) { 343 cs_id = CSID_X86_TSC; 344 } else { 345 return NULL; 346 } 347 348 /* Accept TAI directly, or UTC with valid offset for conversion to TAI */ 349 if (!tai_adjust(st->clk, NULL)) { 350 dev_info(dev, "vmclock does not provide unambiguous time\n"); 351 return NULL; 352 } 353 354 st->sys_cs_id = cs_id; 355 st->cs_id = cs_id; 356 st->ptp_clock_info = ptp_vmclock_info; 357 strscpy(st->ptp_clock_info.name, st->name); 358 359 return ptp_clock_register(&st->ptp_clock_info, dev); 360 } 361 362 struct vmclock_file_state { 363 struct vmclock_state *st; 364 atomic_t seq; 365 }; 366 367 static int vmclock_miscdev_mmap(struct file *fp, struct vm_area_struct *vma) 368 { 369 struct vmclock_file_state *fst = fp->private_data; 370 struct vmclock_state *st = fst->st; 371 372 if ((vma->vm_flags & (VM_READ|VM_WRITE)) != VM_READ) 373 return -EROFS; 374 375 /* 376 * Restrict the read-only mapping so it cannot be upgraded to 377 * writable later with mprotect(). 378 */ 379 vm_flags_clear(vma, VM_MAYWRITE); 380 381 if (vma->vm_end - vma->vm_start != PAGE_SIZE || vma->vm_pgoff) 382 return -EINVAL; 383 384 if (io_remap_pfn_range(vma, vma->vm_start, 385 st->res.start >> PAGE_SHIFT, PAGE_SIZE, 386 vma->vm_page_prot)) 387 return -EAGAIN; 388 389 return 0; 390 } 391 392 static ssize_t vmclock_miscdev_read(struct file *fp, char __user *buf, 393 size_t count, loff_t *ppos) 394 { 395 ktime_t deadline = ktime_add(ktime_get(), VMCLOCK_MAX_WAIT); 396 struct vmclock_file_state *fst = fp->private_data; 397 struct vmclock_state *st = fst->st; 398 uint32_t seq, old_seq; 399 size_t max_count; 400 401 if (*ppos >= PAGE_SIZE) 402 return 0; 403 404 max_count = PAGE_SIZE - *ppos; 405 if (count > max_count) 406 count = max_count; 407 408 old_seq = atomic_read(&fst->seq); 409 while (1) { 410 seq = le32_to_cpu(st->clk->seq_count) & ~1U; 411 /* Pairs with hypervisor wmb */ 412 virt_rmb(); 413 414 if (copy_to_user(buf, ((char *)st->clk) + *ppos, count)) 415 return -EFAULT; 416 417 /* Pairs with hypervisor wmb */ 418 virt_rmb(); 419 if (seq == le32_to_cpu(st->clk->seq_count)) { 420 /* 421 * Either we updated fst->seq to seq (the latest version we observed) 422 * or someone else did (old_seq == seq), so we can break. 423 */ 424 if (atomic_try_cmpxchg(&fst->seq, &old_seq, seq) || 425 old_seq == seq) { 426 break; 427 } 428 } 429 430 if (ktime_after(ktime_get(), deadline)) 431 return -ETIMEDOUT; 432 } 433 434 *ppos += count; 435 return count; 436 } 437 438 static __poll_t vmclock_miscdev_poll(struct file *fp, poll_table *wait) 439 { 440 struct vmclock_file_state *fst = fp->private_data; 441 struct vmclock_state *st = fst->st; 442 uint32_t seq; 443 444 /* 445 * Hypervisor will not send us any notifications, so fail immediately 446 * to avoid having caller sleeping for ever. 447 */ 448 if (!(le64_to_cpu(st->clk->flags) & VMCLOCK_FLAG_NOTIFICATION_PRESENT)) 449 return POLLHUP; 450 451 poll_wait(fp, &st->disrupt_wait, wait); 452 453 seq = le32_to_cpu(st->clk->seq_count); 454 if (atomic_read(&fst->seq) != seq) 455 return POLLIN | POLLRDNORM; 456 457 return 0; 458 } 459 460 static int vmclock_miscdev_open(struct inode *inode, struct file *fp) 461 { 462 struct vmclock_state *st = container_of(fp->private_data, 463 struct vmclock_state, miscdev); 464 struct vmclock_file_state *fst = kzalloc_obj(*fst); 465 466 if (!fst) 467 return -ENOMEM; 468 469 fst->st = st; 470 atomic_set(&fst->seq, 0); 471 472 fp->private_data = fst; 473 474 return 0; 475 } 476 477 static int vmclock_miscdev_release(struct inode *inode, struct file *fp) 478 { 479 kfree(fp->private_data); 480 return 0; 481 } 482 483 static const struct file_operations vmclock_miscdev_fops = { 484 .owner = THIS_MODULE, 485 .open = vmclock_miscdev_open, 486 .release = vmclock_miscdev_release, 487 .mmap = vmclock_miscdev_mmap, 488 .read = vmclock_miscdev_read, 489 .poll = vmclock_miscdev_poll, 490 }; 491 492 /* module operations */ 493 494 #if IS_ENABLED(CONFIG_ACPI) 495 static acpi_status vmclock_acpi_resources(struct acpi_resource *ares, void *data) 496 { 497 struct vmclock_state *st = data; 498 struct resource_win win; 499 struct resource *res = &win.res; 500 501 if (ares->type == ACPI_RESOURCE_TYPE_END_TAG) 502 return AE_OK; 503 504 /* There can be only one */ 505 if (resource_type(&st->res) == IORESOURCE_MEM) 506 return AE_ERROR; 507 508 if (acpi_dev_resource_memory(ares, res) || 509 acpi_dev_resource_address_space(ares, &win)) { 510 511 if (resource_type(res) != IORESOURCE_MEM || 512 resource_size(res) < sizeof(st->clk)) 513 return AE_ERROR; 514 515 st->res = *res; 516 return AE_OK; 517 } 518 519 return AE_ERROR; 520 } 521 522 static void 523 vmclock_acpi_notification_handler(acpi_handle __always_unused handle, 524 u32 __always_unused event, void *dev) 525 { 526 struct device *device = dev; 527 struct vmclock_state *st = device->driver_data; 528 529 wake_up_interruptible(&st->disrupt_wait); 530 } 531 532 static int vmclock_setup_acpi_notification(struct device *dev) 533 { 534 struct acpi_device *adev = ACPI_COMPANION(dev); 535 acpi_status status; 536 537 /* 538 * This should never happen as this function is only called when 539 * has_acpi_companion(dev) is true, but the logic is sufficiently 540 * complex that Coverity can't see the tautology. 541 */ 542 if (!adev) 543 return -ENODEV; 544 545 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, 546 vmclock_acpi_notification_handler, 547 dev); 548 if (ACPI_FAILURE(status)) { 549 dev_err(dev, "failed to install notification handler"); 550 return -ENODEV; 551 } 552 553 return 0; 554 } 555 556 static int vmclock_probe_acpi(struct device *dev, struct vmclock_state *st) 557 { 558 struct acpi_device *adev = ACPI_COMPANION(dev); 559 acpi_status status; 560 561 /* 562 * This should never happen as this function is only called when 563 * has_acpi_companion(dev) is true, but the logic is sufficiently 564 * complex that Coverity can't see the tautology. 565 */ 566 if (!adev) 567 return -ENODEV; 568 569 status = acpi_walk_resources(adev->handle, METHOD_NAME__CRS, 570 vmclock_acpi_resources, st); 571 if (ACPI_FAILURE(status) || resource_type(&st->res) != IORESOURCE_MEM) { 572 dev_err(dev, "failed to get resources\n"); 573 return -ENODEV; 574 } 575 576 return 0; 577 } 578 #endif /* CONFIG_ACPI */ 579 580 static irqreturn_t vmclock_of_irq_handler(int __always_unused irq, void *_st) 581 { 582 struct vmclock_state *st = _st; 583 584 wake_up_interruptible(&st->disrupt_wait); 585 return IRQ_HANDLED; 586 } 587 588 static int vmclock_probe_dt(struct device *dev, struct vmclock_state *st) 589 { 590 struct platform_device *pdev = to_platform_device(dev); 591 struct resource *res; 592 593 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 594 if (!res) 595 return -ENODEV; 596 597 st->res = *res; 598 599 return 0; 600 } 601 602 static int vmclock_setup_of_notification(struct device *dev) 603 { 604 struct platform_device *pdev = to_platform_device(dev); 605 int irq; 606 607 irq = platform_get_irq(pdev, 0); 608 if (irq < 0) 609 return irq; 610 611 return devm_request_irq(dev, irq, vmclock_of_irq_handler, IRQF_SHARED, 612 "vmclock", dev->driver_data); 613 } 614 615 static int vmclock_setup_notification(struct device *dev, 616 struct vmclock_state *st) 617 { 618 /* The device does not support notifications. Nothing else to do */ 619 if (!(le64_to_cpu(st->clk->flags) & VMCLOCK_FLAG_NOTIFICATION_PRESENT)) 620 return 0; 621 622 #if IS_ENABLED(CONFIG_ACPI) 623 if (has_acpi_companion(dev)) 624 return vmclock_setup_acpi_notification(dev); 625 #endif 626 return vmclock_setup_of_notification(dev); 627 } 628 629 static void vmclock_remove(void *data) 630 { 631 struct device *dev = data; 632 struct vmclock_state *st = dev->driver_data; 633 634 if (!st) { 635 dev_err(dev, "%s called with NULL driver_data", __func__); 636 return; 637 } 638 639 #if IS_ENABLED(CONFIG_ACPI) 640 if (has_acpi_companion(dev)) 641 acpi_remove_notify_handler(ACPI_COMPANION(dev)->handle, 642 ACPI_DEVICE_NOTIFY, 643 vmclock_acpi_notification_handler); 644 #endif 645 646 if (st->ptp_clock) 647 ptp_clock_unregister(st->ptp_clock); 648 649 if (st->miscdev.minor != MISC_DYNAMIC_MINOR) 650 misc_deregister(&st->miscdev); 651 652 dev->driver_data = NULL; 653 } 654 655 static void vmclock_put_idx(void *data) 656 { 657 struct vmclock_state *st = data; 658 659 ida_free(&vmclock_ida, st->index); 660 } 661 662 static int vmclock_probe(struct platform_device *pdev) 663 { 664 struct device *dev = &pdev->dev; 665 struct vmclock_state *st; 666 int ret; 667 668 st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL); 669 if (!st) 670 return -ENOMEM; 671 672 #if IS_ENABLED(CONFIG_ACPI) 673 if (has_acpi_companion(dev)) 674 ret = vmclock_probe_acpi(dev, st); 675 else 676 #endif 677 ret = vmclock_probe_dt(dev, st); 678 679 if (ret) { 680 dev_info(dev, "Failed to obtain physical address: %d\n", ret); 681 return ret; 682 } 683 684 if (resource_size(&st->res) < VMCLOCK_MIN_SIZE) { 685 dev_info(dev, "Region too small (0x%llx)\n", 686 resource_size(&st->res)); 687 return -EINVAL; 688 } 689 st->clk = devm_memremap(dev, st->res.start, resource_size(&st->res), 690 MEMREMAP_WB | MEMREMAP_DEC); 691 if (IS_ERR(st->clk)) { 692 ret = PTR_ERR(st->clk); 693 dev_info(dev, "failed to map shared memory\n"); 694 st->clk = NULL; 695 return ret; 696 } 697 698 if (le32_to_cpu(st->clk->magic) != VMCLOCK_MAGIC || 699 le32_to_cpu(st->clk->size) > resource_size(&st->res) || 700 le16_to_cpu(st->clk->version) != 1) { 701 dev_info(dev, "vmclock magic fields invalid\n"); 702 return -EINVAL; 703 } 704 705 ret = ida_alloc(&vmclock_ida, GFP_KERNEL); 706 if (ret < 0) 707 return ret; 708 709 st->index = ret; 710 ret = devm_add_action_or_reset(&pdev->dev, vmclock_put_idx, st); 711 if (ret) 712 return ret; 713 714 st->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "vmclock%d", st->index); 715 if (!st->name) 716 return -ENOMEM; 717 718 st->miscdev.minor = MISC_DYNAMIC_MINOR; 719 720 init_waitqueue_head(&st->disrupt_wait); 721 dev->driver_data = st; 722 723 ret = devm_add_action_or_reset(&pdev->dev, vmclock_remove, dev); 724 if (ret) 725 return ret; 726 727 ret = vmclock_setup_notification(dev, st); 728 if (ret) 729 return ret; 730 731 /* 732 * If the structure is big enough, it can be mapped to userspace. 733 * Theoretically a guest OS even using larger pages could still 734 * use 4KiB PTEs to map smaller MMIO regions like this, but let's 735 * cross that bridge if/when we come to it. 736 */ 737 if (le32_to_cpu(st->clk->size) >= PAGE_SIZE) { 738 st->miscdev.fops = &vmclock_miscdev_fops; 739 st->miscdev.name = st->name; 740 741 ret = misc_register(&st->miscdev); 742 if (ret) 743 return ret; 744 } 745 746 /* If there is valid clock information, register a PTP clock */ 747 if (VMCLOCK_FIELD_PRESENT(st->clk, time_frac_sec)) { 748 /* Can return a silent NULL, or an error. */ 749 st->ptp_clock = vmclock_ptp_register(dev, st); 750 if (IS_ERR(st->ptp_clock)) { 751 ret = PTR_ERR(st->ptp_clock); 752 st->ptp_clock = NULL; 753 return ret; 754 } 755 } 756 757 if (!st->miscdev.minor && !st->ptp_clock) { 758 /* Neither miscdev nor PTP registered */ 759 dev_info(dev, "vmclock: Neither miscdev nor PTP available; not registering\n"); 760 return -ENODEV; 761 } 762 763 dev_info(dev, "%s: registered %s%s%s\n", st->name, 764 st->miscdev.minor ? "miscdev" : "", 765 (st->miscdev.minor && st->ptp_clock) ? ", " : "", 766 st->ptp_clock ? "PTP" : ""); 767 768 return 0; 769 } 770 771 static const struct acpi_device_id vmclock_acpi_ids[] = { 772 { "AMZNC10C", 0 }, 773 { "VMCLOCK", 0 }, 774 {} 775 }; 776 MODULE_DEVICE_TABLE(acpi, vmclock_acpi_ids); 777 778 static const struct of_device_id vmclock_of_ids[] = { 779 { .compatible = "amazon,vmclock", }, 780 { }, 781 }; 782 MODULE_DEVICE_TABLE(of, vmclock_of_ids); 783 784 static struct platform_driver vmclock_platform_driver = { 785 .probe = vmclock_probe, 786 .driver = { 787 .name = "vmclock", 788 .acpi_match_table = vmclock_acpi_ids, 789 .of_match_table = vmclock_of_ids, 790 }, 791 }; 792 793 module_platform_driver(vmclock_platform_driver) 794 795 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 796 MODULE_DESCRIPTION("PTP clock using VMCLOCK"); 797 MODULE_LICENSE("GPL"); 798