Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost

Pull virtio updates from Michael Tsirkin:

- A new virtio RTC driver

- vhost scsi now logs write descriptors so migration works

- Some hardening work in virtio core

- An old spec compliance issue fixed in vhost net

- A couple of cleanups, fixes in vringh, virtio-pci, vdpa

* tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost:
virtio: reject shm region if length is zero
virtio_rtc: Add RTC class driver
virtio_rtc: Add Arm Generic Timer cross-timestamping
virtio_rtc: Add PTP clocks
virtio_rtc: Add module and driver core
vringh: use bvec_kmap_local
vhost: vringh: Use matching allocation type in resize_iovec()
virtio-pci: Fix result size returned for the admin command completion
vdpa/octeon_ep: Control PCI dev enabling manually
vhost-scsi: log event queue write descriptors
vhost-scsi: log control queue write descriptors
vhost-scsi: log I/O queue write descriptors
vhost-scsi: adjust vhost_scsi_get_desc() to log vring descriptors
vhost: modify vhost_log_write() for broader users

+2707 -36
+7
MAINTAINERS
··· 26073 26073 F: drivers/nvdimm/nd_virtio.c 26074 26074 F: drivers/nvdimm/virtio_pmem.c 26075 26075 26076 + VIRTIO RTC DRIVER 26077 + M: Peter Hilber <quic_philber@quicinc.com> 26078 + L: virtualization@lists.linux.dev 26079 + S: Maintained 26080 + F: drivers/virtio/virtio_rtc_* 26081 + F: include/uapi/linux/virtio_rtc.h 26082 + 26076 26083 VIRTIO SOUND DRIVER 26077 26084 M: Anton Yakovlev <anton.yakovlev@opensynergy.com> 26078 26085 M: "Michael S. Tsirkin" <mst@redhat.com>
+12 -5
drivers/vdpa/octeon_ep/octep_vdpa_main.c
··· 454 454 octep_iounmap_region(pdev, octpf->base, OCTEP_HW_MBOX_BAR); 455 455 456 456 octep_vdpa_pf_bar_expand(octpf); 457 + 458 + /* The pf version does not use managed PCI. */ 459 + pci_disable_device(pdev); 457 460 } 458 461 459 462 static void octep_vdpa_vf_bar_shrink(struct pci_dev *pdev) ··· 828 825 struct octep_pf *octpf; 829 826 int ret; 830 827 831 - ret = pcim_enable_device(pdev); 828 + ret = pci_enable_device(pdev); 832 829 if (ret) { 833 830 dev_err(dev, "Failed to enable device\n"); 834 831 return ret; ··· 837 834 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 838 835 if (ret) { 839 836 dev_err(dev, "No usable DMA configuration\n"); 840 - return ret; 837 + goto disable_pci; 841 838 } 842 839 octpf = devm_kzalloc(dev, sizeof(*octpf), GFP_KERNEL); 843 - if (!octpf) 844 - return -ENOMEM; 840 + if (!octpf) { 841 + ret = -ENOMEM; 842 + goto disable_pci; 843 + } 845 844 846 845 ret = octep_iomap_region(pdev, octpf->base, OCTEP_HW_MBOX_BAR); 847 846 if (ret) 848 - return ret; 847 + goto disable_pci; 849 848 850 849 pci_set_master(pdev); 851 850 pci_set_drvdata(pdev, octpf); ··· 861 856 862 857 unmap_region: 863 858 octep_iounmap_region(pdev, octpf->base, OCTEP_HW_MBOX_BAR); 859 + disable_pci: 860 + pci_disable_device(pdev); 864 861 return ret; 865 862 } 866 863
+179 -11
drivers/vhost/scsi.c
··· 133 133 struct se_cmd tvc_se_cmd; 134 134 /* Sense buffer that will be mapped into outgoing status */ 135 135 unsigned char tvc_sense_buf[TRANSPORT_SENSE_BUFFER]; 136 + /* 137 + * Dirty write descriptors of this command. 138 + */ 139 + struct vhost_log *tvc_log; 140 + unsigned int tvc_log_num; 136 141 /* Completed commands list, serviced from vhost worker thread */ 137 142 struct llist_node tvc_completion_list; 138 143 /* Used to track inflight cmd */ ··· 263 258 struct iovec resp_iov; 264 259 int in_iovs; 265 260 int vq_desc; 261 + 262 + /* 263 + * Dirty write descriptors of this command. 264 + */ 265 + struct vhost_log *tmf_log; 266 + unsigned int tmf_log_num; 266 267 }; 267 268 268 269 /* ··· 373 362 return tpg->tv_fabric_prot_type; 374 363 } 375 364 365 + static int vhost_scsi_copy_cmd_log(struct vhost_virtqueue *vq, 366 + struct vhost_scsi_cmd *cmd, 367 + struct vhost_log *log, 368 + unsigned int log_num) 369 + { 370 + if (!cmd->tvc_log) 371 + cmd->tvc_log = kmalloc_array(vq->dev->iov_limit, 372 + sizeof(*cmd->tvc_log), 373 + GFP_KERNEL); 374 + 375 + if (unlikely(!cmd->tvc_log)) { 376 + vq_err(vq, "Failed to alloc tvc_log\n"); 377 + return -ENOMEM; 378 + } 379 + 380 + memcpy(cmd->tvc_log, log, sizeof(*cmd->tvc_log) * log_num); 381 + cmd->tvc_log_num = log_num; 382 + 383 + return 0; 384 + } 385 + 386 + static void vhost_scsi_log_write(struct vhost_virtqueue *vq, 387 + struct vhost_log *log, 388 + unsigned int log_num) 389 + { 390 + if (likely(!vhost_has_feature(vq, VHOST_F_LOG_ALL))) 391 + return; 392 + 393 + if (likely(!log_num || !log)) 394 + return; 395 + 396 + /* 397 + * vhost-scsi doesn't support VIRTIO_F_ACCESS_PLATFORM. 398 + * No requirement for vq->iotlb case. 399 + */ 400 + WARN_ON_ONCE(unlikely(vq->iotlb)); 401 + vhost_log_write(vq, log, log_num, U64_MAX, NULL, 0); 402 + } 403 + 376 404 static void vhost_scsi_release_cmd_res(struct se_cmd *se_cmd) 377 405 { 378 406 struct vhost_scsi_cmd *tv_cmd = container_of(se_cmd, ··· 458 408 { 459 409 struct vhost_scsi_inflight *inflight = tmf->inflight; 460 410 411 + /* 412 + * tmf->tmf_log is default NULL unless VHOST_F_LOG_ALL is set. 413 + */ 414 + kfree(tmf->tmf_log); 461 415 kfree(tmf); 462 416 vhost_scsi_put_inflight(inflight); 463 417 } ··· 571 517 struct vhost_virtqueue *vq = &vs->vqs[VHOST_SCSI_VQ_EVT].vq; 572 518 struct virtio_scsi_event *event = &evt->event; 573 519 struct virtio_scsi_event __user *eventp; 520 + struct vhost_log *vq_log; 521 + unsigned int log_num; 574 522 unsigned out, in; 575 523 int head, ret; 576 524 ··· 583 527 584 528 again: 585 529 vhost_disable_notify(&vs->dev, vq); 530 + 531 + vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? 532 + vq->log : NULL; 533 + 534 + /* 535 + * Reset 'log_num' since vhost_get_vq_desc() may reset it only 536 + * after certain condition checks. 537 + */ 538 + log_num = 0; 539 + 586 540 head = vhost_get_vq_desc(vq, vq->iov, 587 541 ARRAY_SIZE(vq->iov), &out, &in, 588 - NULL, NULL); 542 + vq_log, &log_num); 589 543 if (head < 0) { 590 544 vs->vs_events_missed = true; 591 545 return; ··· 625 559 vhost_add_used_and_signal(&vs->dev, vq, head, 0); 626 560 else 627 561 vq_err(vq, "Faulted on vhost_scsi_send_event\n"); 562 + 563 + vhost_scsi_log_write(vq, vq_log, log_num); 628 564 } 629 565 630 566 static void vhost_scsi_complete_events(struct vhost_scsi *vs, bool drop) ··· 728 660 } else 729 661 pr_err("Faulted on virtio_scsi_cmd_resp\n"); 730 662 663 + vhost_scsi_log_write(cmd->tvc_vq, cmd->tvc_log, 664 + cmd->tvc_log_num); 665 + 731 666 vhost_scsi_release_cmd_res(se_cmd); 732 667 } 733 668 ··· 747 676 struct vhost_scsi_virtqueue, vq); 748 677 struct vhost_scsi_cmd *cmd; 749 678 struct scatterlist *sgl, *prot_sgl; 679 + struct vhost_log *log; 750 680 int tag; 751 681 752 682 tag = sbitmap_get(&svq->scsi_tags); ··· 759 687 cmd = &svq->scsi_cmds[tag]; 760 688 sgl = cmd->sgl; 761 689 prot_sgl = cmd->prot_sgl; 690 + log = cmd->tvc_log; 762 691 memset(cmd, 0, sizeof(*cmd)); 763 692 cmd->sgl = sgl; 764 693 cmd->prot_sgl = prot_sgl; 694 + cmd->tvc_log = log; 765 695 cmd->tvc_se_cmd.map_tag = tag; 766 696 cmd->inflight = vhost_scsi_get_inflight(vq); 767 697 ··· 1137 1063 1138 1064 static int 1139 1065 vhost_scsi_get_desc(struct vhost_scsi *vs, struct vhost_virtqueue *vq, 1140 - struct vhost_scsi_ctx *vc) 1066 + struct vhost_scsi_ctx *vc, 1067 + struct vhost_log *log, unsigned int *log_num) 1141 1068 { 1142 1069 int ret = -ENXIO; 1143 1070 1071 + if (likely(log_num)) 1072 + *log_num = 0; 1073 + 1144 1074 vc->head = vhost_get_vq_desc(vq, vq->iov, 1145 1075 ARRAY_SIZE(vq->iov), &vc->out, &vc->in, 1146 - NULL, NULL); 1076 + log, log_num); 1147 1077 1148 1078 pr_debug("vhost_get_vq_desc: head: %d, out: %u in: %u\n", 1149 1079 vc->head, vc->out, vc->in); ··· 1299 1221 u8 task_attr; 1300 1222 bool t10_pi = vhost_has_feature(vq, VIRTIO_SCSI_F_T10_PI); 1301 1223 u8 *cdb; 1224 + struct vhost_log *vq_log; 1225 + unsigned int log_num; 1302 1226 1303 1227 mutex_lock(&vq->mutex); 1304 1228 /* ··· 1316 1236 1317 1237 vhost_disable_notify(&vs->dev, vq); 1318 1238 1239 + vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? 1240 + vq->log : NULL; 1241 + 1319 1242 do { 1320 - ret = vhost_scsi_get_desc(vs, vq, &vc); 1243 + ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num); 1321 1244 if (ret) 1322 1245 goto err; 1323 1246 ··· 1469 1386 goto err; 1470 1387 } 1471 1388 1389 + if (unlikely(vq_log && log_num)) { 1390 + ret = vhost_scsi_copy_cmd_log(vq, cmd, vq_log, log_num); 1391 + if (unlikely(ret)) { 1392 + vhost_scsi_release_cmd_res(&cmd->tvc_se_cmd); 1393 + goto err; 1394 + } 1395 + } 1396 + 1472 1397 pr_debug("vhost_scsi got command opcode: %#02x, lun: %d\n", 1473 1398 cdb[0], lun); 1474 1399 pr_debug("cmd: %p exp_data_len: %d, prot_bytes: %d data_direction:" ··· 1512 1421 */ 1513 1422 if (ret == -ENXIO) 1514 1423 break; 1515 - else if (ret == -EIO) 1424 + else if (ret == -EIO) { 1516 1425 vhost_scsi_send_bad_target(vs, vq, &vc, TYPE_IO_CMD); 1517 - else if (ret == -ENOMEM) 1426 + vhost_scsi_log_write(vq, vq_log, log_num); 1427 + } else if (ret == -ENOMEM) { 1518 1428 vhost_scsi_send_status(vs, vq, &vc, 1519 1429 SAM_STAT_TASK_SET_FULL); 1430 + vhost_scsi_log_write(vq, vq_log, log_num); 1431 + } 1520 1432 } while (likely(!vhost_exceeds_weight(vq, ++c, 0))); 1521 1433 out: 1522 1434 mutex_unlock(&vq->mutex); ··· 1561 1467 mutex_lock(&tmf->svq->vq.mutex); 1562 1468 vhost_scsi_send_tmf_resp(tmf->vhost, &tmf->svq->vq, tmf->in_iovs, 1563 1469 tmf->vq_desc, &tmf->resp_iov, resp_code); 1470 + vhost_scsi_log_write(&tmf->svq->vq, tmf->tmf_log, 1471 + tmf->tmf_log_num); 1564 1472 mutex_unlock(&tmf->svq->vq.mutex); 1565 1473 1566 1474 vhost_scsi_release_tmf_res(tmf); ··· 1586 1490 vhost_scsi_handle_tmf(struct vhost_scsi *vs, struct vhost_scsi_tpg *tpg, 1587 1491 struct vhost_virtqueue *vq, 1588 1492 struct virtio_scsi_ctrl_tmf_req *vtmf, 1589 - struct vhost_scsi_ctx *vc) 1493 + struct vhost_scsi_ctx *vc, 1494 + struct vhost_log *log, unsigned int log_num) 1590 1495 { 1591 1496 struct vhost_scsi_virtqueue *svq = container_of(vq, 1592 1497 struct vhost_scsi_virtqueue, vq); ··· 1615 1518 tmf->in_iovs = vc->in; 1616 1519 tmf->inflight = vhost_scsi_get_inflight(vq); 1617 1520 1521 + if (unlikely(log && log_num)) { 1522 + tmf->tmf_log = kmalloc_array(log_num, sizeof(*tmf->tmf_log), 1523 + GFP_KERNEL); 1524 + if (tmf->tmf_log) { 1525 + memcpy(tmf->tmf_log, log, sizeof(*tmf->tmf_log) * log_num); 1526 + tmf->tmf_log_num = log_num; 1527 + } else { 1528 + pr_err("vhost_scsi tmf log allocation error\n"); 1529 + vhost_scsi_release_tmf_res(tmf); 1530 + goto send_reject; 1531 + } 1532 + } 1533 + 1618 1534 if (target_submit_tmr(&tmf->se_cmd, tpg->tpg_nexus->tvn_se_sess, NULL, 1619 1535 vhost_buf_to_lun(vtmf->lun), NULL, 1620 1536 TMR_LUN_RESET, GFP_KERNEL, 0, ··· 1641 1531 send_reject: 1642 1532 vhost_scsi_send_tmf_resp(vs, vq, vc->in, vc->head, &vq->iov[vc->out], 1643 1533 VIRTIO_SCSI_S_FUNCTION_REJECTED); 1534 + vhost_scsi_log_write(vq, log, log_num); 1644 1535 } 1645 1536 1646 1537 static void ··· 1678 1567 struct vhost_scsi_ctx vc; 1679 1568 size_t typ_size; 1680 1569 int ret, c = 0; 1570 + struct vhost_log *vq_log; 1571 + unsigned int log_num; 1681 1572 1682 1573 mutex_lock(&vq->mutex); 1683 1574 /* ··· 1693 1580 1694 1581 vhost_disable_notify(&vs->dev, vq); 1695 1582 1583 + vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ? 1584 + vq->log : NULL; 1585 + 1696 1586 do { 1697 - ret = vhost_scsi_get_desc(vs, vq, &vc); 1587 + ret = vhost_scsi_get_desc(vs, vq, &vc, vq_log, &log_num); 1698 1588 if (ret) 1699 1589 goto err; 1700 1590 ··· 1761 1645 goto err; 1762 1646 1763 1647 if (v_req.type == VIRTIO_SCSI_T_TMF) 1764 - vhost_scsi_handle_tmf(vs, tpg, vq, &v_req.tmf, &vc); 1765 - else 1648 + vhost_scsi_handle_tmf(vs, tpg, vq, &v_req.tmf, &vc, 1649 + vq_log, log_num); 1650 + else { 1766 1651 vhost_scsi_send_an_resp(vs, vq, &vc); 1652 + vhost_scsi_log_write(vq, vq_log, log_num); 1653 + } 1767 1654 err: 1768 1655 /* 1769 1656 * ENXIO: No more requests, or read error, wait for next kick ··· 1776 1657 */ 1777 1658 if (ret == -ENXIO) 1778 1659 break; 1779 - else if (ret == -EIO) 1660 + else if (ret == -EIO) { 1780 1661 vhost_scsi_send_bad_target(vs, vq, &vc, 1781 1662 v_req.type == VIRTIO_SCSI_T_TMF ? 1782 1663 TYPE_CTRL_TMF : 1783 1664 TYPE_CTRL_AN); 1665 + vhost_scsi_log_write(vq, vq_log, log_num); 1666 + } 1784 1667 } while (likely(!vhost_exceeds_weight(vq, ++c, 0))); 1785 1668 out: 1786 1669 mutex_unlock(&vq->mutex); ··· 1877 1756 wait_for_completion(&vs->old_inflight[i]->comp); 1878 1757 } 1879 1758 1759 + static void vhost_scsi_destroy_vq_log(struct vhost_virtqueue *vq) 1760 + { 1761 + struct vhost_scsi_virtqueue *svq = container_of(vq, 1762 + struct vhost_scsi_virtqueue, vq); 1763 + struct vhost_scsi_cmd *tv_cmd; 1764 + unsigned int i; 1765 + 1766 + if (!svq->scsi_cmds) 1767 + return; 1768 + 1769 + for (i = 0; i < svq->max_cmds; i++) { 1770 + tv_cmd = &svq->scsi_cmds[i]; 1771 + kfree(tv_cmd->tvc_log); 1772 + tv_cmd->tvc_log = NULL; 1773 + tv_cmd->tvc_log_num = 0; 1774 + } 1775 + } 1776 + 1880 1777 static void vhost_scsi_destroy_vq_cmds(struct vhost_virtqueue *vq) 1881 1778 { 1882 1779 struct vhost_scsi_virtqueue *svq = container_of(vq, ··· 1914 1775 1915 1776 sbitmap_free(&svq->scsi_tags); 1916 1777 kfree(svq->upages); 1778 + vhost_scsi_destroy_vq_log(vq); 1917 1779 kfree(svq->scsi_cmds); 1918 1780 svq->scsi_cmds = NULL; 1919 1781 } ··· 2224 2084 static int vhost_scsi_set_features(struct vhost_scsi *vs, u64 features) 2225 2085 { 2226 2086 struct vhost_virtqueue *vq; 2087 + bool is_log, was_log; 2227 2088 int i; 2228 2089 2229 2090 if (features & ~VHOST_SCSI_FEATURES) ··· 2237 2096 return -EFAULT; 2238 2097 } 2239 2098 2099 + if (!vs->dev.nvqs) 2100 + goto out; 2101 + 2102 + is_log = features & (1 << VHOST_F_LOG_ALL); 2103 + /* 2104 + * All VQs should have same feature. 2105 + */ 2106 + was_log = vhost_has_feature(&vs->vqs[0].vq, VHOST_F_LOG_ALL); 2107 + 2240 2108 for (i = 0; i < vs->dev.nvqs; i++) { 2241 2109 vq = &vs->vqs[i].vq; 2242 2110 mutex_lock(&vq->mutex); 2243 2111 vq->acked_features = features; 2244 2112 mutex_unlock(&vq->mutex); 2245 2113 } 2114 + 2115 + /* 2116 + * If VHOST_F_LOG_ALL is removed, free tvc_log after 2117 + * vq->acked_features is committed. 2118 + */ 2119 + if (!is_log && was_log) { 2120 + for (i = VHOST_SCSI_VQ_IO; i < vs->dev.nvqs; i++) { 2121 + if (!vs->vqs[i].scsi_cmds) 2122 + continue; 2123 + 2124 + vq = &vs->vqs[i].vq; 2125 + mutex_lock(&vq->mutex); 2126 + vhost_scsi_destroy_vq_log(vq); 2127 + mutex_unlock(&vq->mutex); 2128 + } 2129 + } 2130 + 2131 + out: 2246 2132 mutex_unlock(&vs->dev.mutex); 2247 2133 return 0; 2248 2134 }
+20 -8
drivers/vhost/vhost.c
··· 2304 2304 return 0; 2305 2305 } 2306 2306 2307 + /* 2308 + * vhost_log_write() - Log in dirty page bitmap 2309 + * @vq: vhost virtqueue. 2310 + * @log: Array of dirty memory in GPA. 2311 + * @log_num: Size of vhost_log arrary. 2312 + * @len: The total length of memory buffer to log in the dirty bitmap. 2313 + * Some drivers may only partially use pages shared via the last 2314 + * vring descriptor (i.e. vhost-net RX buffer). 2315 + * Use (len == U64_MAX) to indicate the driver would log all 2316 + * pages of vring descriptors. 2317 + * @iov: Array of dirty memory in HVA. 2318 + * @count: Size of iovec array. 2319 + */ 2307 2320 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, 2308 2321 unsigned int log_num, u64 len, struct iovec *iov, int count) 2309 2322 { ··· 2340 2327 r = log_write(vq->log_base, log[i].addr, l); 2341 2328 if (r < 0) 2342 2329 return r; 2343 - len -= l; 2344 - if (!len) { 2345 - if (vq->log_ctx) 2346 - eventfd_signal(vq->log_ctx); 2347 - return 0; 2348 - } 2330 + 2331 + if (len != U64_MAX) 2332 + len -= l; 2349 2333 } 2350 - /* Length written exceeds what we have stored. This is a bug. */ 2351 - BUG(); 2334 + 2335 + if (vq->log_ctx) 2336 + eventfd_signal(vq->log_ctx); 2337 + 2352 2338 return 0; 2353 2339 } 2354 2340 EXPORT_SYMBOL_GPL(vhost_log_write);
+8 -11
drivers/vhost/vringh.c
··· 225 225 226 226 flag = (iov->max_num & VRINGH_IOV_ALLOCATED); 227 227 if (flag) 228 - new = krealloc_array(iov->iov, new_num, 229 - sizeof(struct iovec), gfp); 228 + new = krealloc_array(iov->iov, new_num, sizeof(*new), gfp); 230 229 else { 231 - new = kmalloc_array(new_num, sizeof(struct iovec), gfp); 230 + new = kmalloc_array(new_num, sizeof(*new), gfp); 232 231 if (new) { 233 232 memcpy(new, iov->iov, 234 233 iov->max_num * sizeof(struct iovec)); ··· 1290 1291 if (ret) 1291 1292 return ret; 1292 1293 } else { 1293 - void *kaddr = kmap_local_page(ivec.iov.bvec[0].bv_page); 1294 - void *from = kaddr + ivec.iov.bvec[0].bv_offset; 1294 + __virtio16 *from = bvec_kmap_local(&ivec.iov.bvec[0]); 1295 1295 1296 - tmp = READ_ONCE(*(__virtio16 *)from); 1297 - kunmap_local(kaddr); 1296 + tmp = READ_ONCE(*from); 1297 + kunmap_local(from); 1298 1298 } 1299 1299 1300 1300 *val = vringh16_to_cpu(vrh, tmp); ··· 1328 1330 if (ret) 1329 1331 return ret; 1330 1332 } else { 1331 - void *kaddr = kmap_local_page(ivec.iov.bvec[0].bv_page); 1332 - void *to = kaddr + ivec.iov.bvec[0].bv_offset; 1333 + __virtio16 *to = bvec_kmap_local(&ivec.iov.bvec[0]); 1333 1334 1334 - WRITE_ONCE(*(__virtio16 *)to, tmp); 1335 - kunmap_local(kaddr); 1335 + WRITE_ONCE(*to, tmp); 1336 + kunmap_local(to); 1336 1337 } 1337 1338 1338 1339 return 0;
+64
drivers/virtio/Kconfig
··· 188 188 189 189 If unsure, say N. 190 190 191 + config VIRTIO_RTC 192 + tristate "Virtio RTC driver" 193 + depends on VIRTIO 194 + depends on PTP_1588_CLOCK_OPTIONAL 195 + help 196 + This driver provides current time from a Virtio RTC device. The driver 197 + provides the time through one or more clocks. The Virtio RTC PTP 198 + clocks and/or the Real Time Clock driver for Virtio RTC must be 199 + enabled to expose the clocks to userspace. 200 + 201 + To compile this code as a module, choose M here: the module will be 202 + called virtio_rtc. 203 + 204 + If unsure, say M. 205 + 206 + if VIRTIO_RTC 207 + 208 + comment "WARNING: Consider enabling VIRTIO_RTC_PTP and/or VIRTIO_RTC_CLASS." 209 + depends on !VIRTIO_RTC_PTP && !VIRTIO_RTC_CLASS 210 + 211 + comment "Enable PTP_1588_CLOCK in order to enable VIRTIO_RTC_PTP." 212 + depends on PTP_1588_CLOCK=n 213 + 214 + config VIRTIO_RTC_PTP 215 + bool "Virtio RTC PTP clocks" 216 + default y 217 + depends on PTP_1588_CLOCK 218 + help 219 + This exposes any Virtio RTC clocks as PTP Hardware Clocks (PHCs) to 220 + userspace. The PHC sysfs attribute "clock_name" describes the clock 221 + type. 222 + 223 + If unsure, say Y. 224 + 225 + config VIRTIO_RTC_ARM 226 + bool "Virtio RTC cross-timestamping using Arm Generic Timer" 227 + default y 228 + depends on VIRTIO_RTC_PTP && ARM_ARCH_TIMER 229 + help 230 + This enables Virtio RTC cross-timestamping using the Arm Generic Timer. 231 + It only has an effect if the Virtio RTC device also supports this. The 232 + cross-timestamp is available through the PTP clock driver precise 233 + cross-timestamp ioctl (PTP_SYS_OFFSET_PRECISE2 aka 234 + PTP_SYS_OFFSET_PRECISE). 235 + 236 + If unsure, say Y. 237 + 238 + comment "Enable RTC_CLASS in order to enable VIRTIO_RTC_CLASS." 239 + depends on RTC_CLASS=n 240 + 241 + config VIRTIO_RTC_CLASS 242 + bool "Real Time Clock driver for Virtio RTC" 243 + default y 244 + depends on RTC_CLASS 245 + help 246 + This exposes the Virtio RTC UTC-like clock as a Linux Real Time Clock. 247 + It only has an effect if the Virtio RTC device has a UTC-like clock 248 + which smears leap seconds to avoid steps. The Real Time Clock is 249 + read-only, and may support setting an alarm. 250 + 251 + If unsure, say Y. 252 + 253 + endif # VIRTIO_RTC 254 + 191 255 endif # VIRTIO_MENU
+5
drivers/virtio/Makefile
··· 14 14 obj-$(CONFIG_VIRTIO_MEM) += virtio_mem.o 15 15 obj-$(CONFIG_VIRTIO_DMA_SHARED_BUFFER) += virtio_dma_buf.o 16 16 obj-$(CONFIG_VIRTIO_DEBUG) += virtio_debug.o 17 + obj-$(CONFIG_VIRTIO_RTC) += virtio_rtc.o 18 + virtio_rtc-y := virtio_rtc_driver.o 19 + virtio_rtc-$(CONFIG_VIRTIO_RTC_PTP) += virtio_rtc_ptp.o 20 + virtio_rtc-$(CONFIG_VIRTIO_RTC_ARM) += virtio_rtc_arm.o 21 + virtio_rtc-$(CONFIG_VIRTIO_RTC_CLASS) += virtio_rtc_class.o
+12 -1
drivers/virtio/virtio_pci_modern.c
··· 48 48 { 49 49 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); 50 50 struct virtio_pci_admin_vq *admin_vq = &vp_dev->admin_vq; 51 + unsigned int status_size = sizeof(struct virtio_admin_cmd_status); 51 52 struct virtio_admin_cmd *cmd; 52 53 unsigned long flags; 53 54 unsigned int len; ··· 57 56 do { 58 57 virtqueue_disable_cb(vq); 59 58 while ((cmd = virtqueue_get_buf(vq, &len))) { 60 - cmd->result_sg_size = len; 59 + /* If the number of bytes written by the device is less 60 + * than the size of struct virtio_admin_cmd_status, the 61 + * remaining status bytes will remain zero-initialized, 62 + * since the buffer was zeroed during allocation. 63 + * In this case, set the size of command_specific_result 64 + * to 0. 65 + */ 66 + if (len < status_size) 67 + cmd->result_sg_size = 0; 68 + else 69 + cmd->result_sg_size = len - status_size; 61 70 complete(&cmd->completion); 62 71 } 63 72 } while (!virtqueue_enable_cb(vq));
+23
drivers/virtio/virtio_rtc_arm.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Provides cross-timestamp params for Arm. 4 + * 5 + * Copyright (C) 2022-2023 OpenSynergy GmbH 6 + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. 7 + */ 8 + 9 + #include <linux/clocksource_ids.h> 10 + 11 + #include <uapi/linux/virtio_rtc.h> 12 + 13 + #include "virtio_rtc_internal.h" 14 + 15 + /* see header for doc */ 16 + 17 + int viortc_hw_xtstamp_params(u8 *hw_counter, enum clocksource_ids *cs_id) 18 + { 19 + *hw_counter = VIRTIO_RTC_COUNTER_ARM_VCT; 20 + *cs_id = CSID_ARM_ARCH_COUNTER; 21 + 22 + return 0; 23 + }
+262
drivers/virtio/virtio_rtc_class.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * virtio_rtc RTC class driver 4 + * 5 + * Copyright (C) 2023 OpenSynergy GmbH 6 + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. 7 + */ 8 + 9 + #include <linux/math64.h> 10 + #include <linux/overflow.h> 11 + #include <linux/rtc.h> 12 + #include <linux/time64.h> 13 + 14 + #include <uapi/linux/virtio_rtc.h> 15 + 16 + #include "virtio_rtc_internal.h" 17 + 18 + /** 19 + * struct viortc_class - RTC class wrapper 20 + * @viortc: virtio_rtc device data 21 + * @rtc: RTC device 22 + * @vio_clk_id: virtio_rtc clock id 23 + * @stopped: Whether RTC ops are disallowed. Access protected by rtc_lock(). 24 + */ 25 + struct viortc_class { 26 + struct viortc_dev *viortc; 27 + struct rtc_device *rtc; 28 + u16 vio_clk_id; 29 + bool stopped; 30 + }; 31 + 32 + /** 33 + * viortc_class_get_locked() - get RTC class wrapper, if ops allowed 34 + * @dev: virtio device 35 + * 36 + * Gets the RTC class wrapper from the virtio device, if it is available and 37 + * ops are allowed. 38 + * 39 + * Context: Caller must hold rtc_lock(). 40 + * Return: RTC class wrapper if available and ops allowed, ERR_PTR otherwise. 41 + */ 42 + static struct viortc_class *viortc_class_get_locked(struct device *dev) 43 + { 44 + struct viortc_class *viortc_class; 45 + 46 + viortc_class = viortc_class_from_dev(dev); 47 + if (IS_ERR(viortc_class)) 48 + return viortc_class; 49 + 50 + if (viortc_class->stopped) 51 + return ERR_PTR(-EBUSY); 52 + 53 + return viortc_class; 54 + } 55 + 56 + /** 57 + * viortc_class_read_time() - RTC class op read_time 58 + * @dev: virtio device 59 + * @tm: read time 60 + * 61 + * Context: Process context. 62 + * Return: Zero on success, negative error code otherwise. 63 + */ 64 + static int viortc_class_read_time(struct device *dev, struct rtc_time *tm) 65 + { 66 + struct viortc_class *viortc_class; 67 + time64_t sec; 68 + int ret; 69 + u64 ns; 70 + 71 + viortc_class = viortc_class_get_locked(dev); 72 + if (IS_ERR(viortc_class)) 73 + return PTR_ERR(viortc_class); 74 + 75 + ret = viortc_read(viortc_class->viortc, viortc_class->vio_clk_id, &ns); 76 + if (ret) 77 + return ret; 78 + 79 + sec = div_u64(ns, NSEC_PER_SEC); 80 + 81 + rtc_time64_to_tm(sec, tm); 82 + 83 + return 0; 84 + } 85 + 86 + /** 87 + * viortc_class_read_alarm() - RTC class op read_alarm 88 + * @dev: virtio device 89 + * @alrm: alarm read out 90 + * 91 + * Context: Process context. 92 + * Return: Zero on success, negative error code otherwise. 93 + */ 94 + static int viortc_class_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 95 + { 96 + struct viortc_class *viortc_class; 97 + time64_t alarm_time_sec; 98 + u64 alarm_time_ns; 99 + bool enabled; 100 + int ret; 101 + 102 + viortc_class = viortc_class_get_locked(dev); 103 + if (IS_ERR(viortc_class)) 104 + return PTR_ERR(viortc_class); 105 + 106 + ret = viortc_read_alarm(viortc_class->viortc, viortc_class->vio_clk_id, 107 + &alarm_time_ns, &enabled); 108 + if (ret) 109 + return ret; 110 + 111 + alarm_time_sec = div_u64(alarm_time_ns, NSEC_PER_SEC); 112 + rtc_time64_to_tm(alarm_time_sec, &alrm->time); 113 + 114 + alrm->enabled = enabled; 115 + 116 + return 0; 117 + } 118 + 119 + /** 120 + * viortc_class_set_alarm() - RTC class op set_alarm 121 + * @dev: virtio device 122 + * @alrm: alarm to set 123 + * 124 + * Context: Process context. 125 + * Return: Zero on success, negative error code otherwise. 126 + */ 127 + static int viortc_class_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 128 + { 129 + struct viortc_class *viortc_class; 130 + time64_t alarm_time_sec; 131 + u64 alarm_time_ns; 132 + 133 + viortc_class = viortc_class_get_locked(dev); 134 + if (IS_ERR(viortc_class)) 135 + return PTR_ERR(viortc_class); 136 + 137 + alarm_time_sec = rtc_tm_to_time64(&alrm->time); 138 + 139 + if (alarm_time_sec < 0) 140 + return -EINVAL; 141 + 142 + if (check_mul_overflow((u64)alarm_time_sec, (u64)NSEC_PER_SEC, 143 + &alarm_time_ns)) 144 + return -EINVAL; 145 + 146 + return viortc_set_alarm(viortc_class->viortc, viortc_class->vio_clk_id, 147 + alarm_time_ns, alrm->enabled); 148 + } 149 + 150 + /** 151 + * viortc_class_alarm_irq_enable() - RTC class op alarm_irq_enable 152 + * @dev: virtio device 153 + * @enabled: enable or disable alarm IRQ 154 + * 155 + * Context: Process context. 156 + * Return: Zero on success, negative error code otherwise. 157 + */ 158 + static int viortc_class_alarm_irq_enable(struct device *dev, 159 + unsigned int enabled) 160 + { 161 + struct viortc_class *viortc_class; 162 + 163 + viortc_class = viortc_class_get_locked(dev); 164 + if (IS_ERR(viortc_class)) 165 + return PTR_ERR(viortc_class); 166 + 167 + return viortc_set_alarm_enabled(viortc_class->viortc, 168 + viortc_class->vio_clk_id, enabled); 169 + } 170 + 171 + static const struct rtc_class_ops viortc_class_ops = { 172 + .read_time = viortc_class_read_time, 173 + .read_alarm = viortc_class_read_alarm, 174 + .set_alarm = viortc_class_set_alarm, 175 + .alarm_irq_enable = viortc_class_alarm_irq_enable, 176 + }; 177 + 178 + /** 179 + * viortc_class_alarm() - propagate alarm notification as alarm interrupt 180 + * @viortc_class: RTC class wrapper 181 + * @vio_clk_id: virtio_rtc clock id 182 + * 183 + * Context: Any context. 184 + */ 185 + void viortc_class_alarm(struct viortc_class *viortc_class, u16 vio_clk_id) 186 + { 187 + if (vio_clk_id != viortc_class->vio_clk_id) { 188 + dev_warn_ratelimited(&viortc_class->rtc->dev, 189 + "ignoring alarm for clock id %d, expected id %d\n", 190 + vio_clk_id, viortc_class->vio_clk_id); 191 + return; 192 + } 193 + 194 + rtc_update_irq(viortc_class->rtc, 1, RTC_AF | RTC_IRQF); 195 + } 196 + 197 + /** 198 + * viortc_class_stop() - disallow RTC class ops 199 + * @viortc_class: RTC class wrapper 200 + * 201 + * Context: Process context. Caller must NOT hold rtc_lock(). 202 + */ 203 + void viortc_class_stop(struct viortc_class *viortc_class) 204 + { 205 + rtc_lock(viortc_class->rtc); 206 + 207 + viortc_class->stopped = true; 208 + 209 + rtc_unlock(viortc_class->rtc); 210 + } 211 + 212 + /** 213 + * viortc_class_register() - register RTC class device 214 + * @viortc_class: RTC class wrapper 215 + * 216 + * Context: Process context. 217 + * Return: Zero on success, negative error code otherwise. 218 + */ 219 + int viortc_class_register(struct viortc_class *viortc_class) 220 + { 221 + return devm_rtc_register_device(viortc_class->rtc); 222 + } 223 + 224 + /** 225 + * viortc_class_init() - init RTC class wrapper and device 226 + * @viortc: device data 227 + * @vio_clk_id: virtio_rtc clock id 228 + * @have_alarm: have alarm feature 229 + * @parent_dev: virtio device 230 + * 231 + * Context: Process context. 232 + * Return: RTC class wrapper on success, ERR_PTR otherwise. 233 + */ 234 + struct viortc_class *viortc_class_init(struct viortc_dev *viortc, 235 + u16 vio_clk_id, bool have_alarm, 236 + struct device *parent_dev) 237 + { 238 + struct viortc_class *viortc_class; 239 + struct rtc_device *rtc; 240 + 241 + viortc_class = 242 + devm_kzalloc(parent_dev, sizeof(*viortc_class), GFP_KERNEL); 243 + if (!viortc_class) 244 + return ERR_PTR(-ENOMEM); 245 + 246 + rtc = devm_rtc_allocate_device(parent_dev); 247 + if (IS_ERR(rtc)) 248 + return ERR_CAST(rtc); 249 + 250 + viortc_class->viortc = viortc; 251 + viortc_class->rtc = rtc; 252 + viortc_class->vio_clk_id = vio_clk_id; 253 + 254 + if (!have_alarm) 255 + clear_bit(RTC_FEATURE_ALARM, rtc->features); 256 + clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features); 257 + 258 + rtc->ops = &viortc_class_ops; 259 + rtc->range_max = div_u64(U64_MAX, NSEC_PER_SEC); 260 + 261 + return viortc_class; 262 + }
+1407
drivers/virtio/virtio_rtc_driver.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * virtio_rtc driver core 4 + * 5 + * Copyright (C) 2022-2024 OpenSynergy GmbH 6 + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. 7 + */ 8 + 9 + #include <linux/completion.h> 10 + #include <linux/device.h> 11 + #include <linux/module.h> 12 + #include <linux/pm.h> 13 + #include <linux/virtio.h> 14 + #include <linux/virtio_config.h> 15 + #include <linux/virtio_ids.h> 16 + 17 + #include <uapi/linux/virtio_rtc.h> 18 + 19 + #include "virtio_rtc_internal.h" 20 + 21 + #define VIORTC_ALARMQ_BUF_CAP sizeof(union virtio_rtc_notif_alarmq) 22 + 23 + /* virtqueue order */ 24 + enum { 25 + VIORTC_REQUESTQ, 26 + VIORTC_ALARMQ, 27 + VIORTC_MAX_NR_QUEUES, 28 + }; 29 + 30 + /** 31 + * struct viortc_vq - virtqueue abstraction 32 + * @vq: virtqueue 33 + * @lock: protects access to vq 34 + */ 35 + struct viortc_vq { 36 + struct virtqueue *vq; 37 + spinlock_t lock; 38 + }; 39 + 40 + /** 41 + * struct viortc_dev - virtio_rtc device data 42 + * @vdev: virtio device 43 + * @viortc_class: RTC class wrapper for UTC-like clock, NULL if not available 44 + * @vqs: virtqueues 45 + * @clocks_to_unregister: Clock references, which are only used during device 46 + * removal. 47 + * For other uses, there would be a race between device 48 + * creation and setting the pointers here. 49 + * @alarmq_bufs: alarmq buffers list 50 + * @num_alarmq_bufs: # of alarmq buffers 51 + * @num_clocks: # of virtio_rtc clocks 52 + */ 53 + struct viortc_dev { 54 + struct virtio_device *vdev; 55 + struct viortc_class *viortc_class; 56 + struct viortc_vq vqs[VIORTC_MAX_NR_QUEUES]; 57 + struct viortc_ptp_clock **clocks_to_unregister; 58 + void **alarmq_bufs; 59 + unsigned int num_alarmq_bufs; 60 + u16 num_clocks; 61 + }; 62 + 63 + /** 64 + * struct viortc_msg - Message requested by driver, responded by device. 65 + * @viortc: device data 66 + * @req: request buffer 67 + * @resp: response buffer 68 + * @responded: vqueue callback signals response reception 69 + * @refcnt: Message reference count, message and buffers will be deallocated 70 + * once 0. refcnt is decremented in the vqueue callback and in the 71 + * thread waiting on the responded completion. 72 + * If a message response wait function times out, the message will be 73 + * freed upon late reception (refcnt will reach 0 in the callback), or 74 + * device removal. 75 + * @req_size: size of request in bytes 76 + * @resp_cap: maximum size of response in bytes 77 + * @resp_actual_size: actual size of response 78 + */ 79 + struct viortc_msg { 80 + struct viortc_dev *viortc; 81 + void *req; 82 + void *resp; 83 + struct completion responded; 84 + refcount_t refcnt; 85 + unsigned int req_size; 86 + unsigned int resp_cap; 87 + unsigned int resp_actual_size; 88 + }; 89 + 90 + /** 91 + * viortc_class_from_dev() - Get RTC class object from virtio device. 92 + * @dev: virtio device 93 + * 94 + * Context: Any context. 95 + * Return: RTC class object if available, ERR_PTR otherwise. 96 + */ 97 + struct viortc_class *viortc_class_from_dev(struct device *dev) 98 + { 99 + struct virtio_device *vdev; 100 + struct viortc_dev *viortc; 101 + 102 + vdev = container_of(dev, typeof(*vdev), dev); 103 + viortc = vdev->priv; 104 + 105 + return viortc->viortc_class ?: ERR_PTR(-ENODEV); 106 + } 107 + 108 + /** 109 + * viortc_alarms_supported() - Whether device and driver support alarms. 110 + * @vdev: virtio device 111 + * 112 + * NB: Device and driver may not support alarms for the same clocks. 113 + * 114 + * Context: Any context. 115 + * Return: True if both device and driver can support alarms. 116 + */ 117 + static bool viortc_alarms_supported(struct virtio_device *vdev) 118 + { 119 + return IS_ENABLED(CONFIG_VIRTIO_RTC_CLASS) && 120 + virtio_has_feature(vdev, VIRTIO_RTC_F_ALARM); 121 + } 122 + 123 + /** 124 + * viortc_feed_vq() - Make a device write-only buffer available. 125 + * @viortc: device data 126 + * @vq: notification virtqueue 127 + * @buf: buffer 128 + * @buf_len: buffer capacity in bytes 129 + * @data: token, identifying buffer 130 + * 131 + * Context: Caller must prevent concurrent access to vq. 132 + * Return: Zero on success, negative error code otherwise. 133 + */ 134 + static int viortc_feed_vq(struct viortc_dev *viortc, struct virtqueue *vq, 135 + void *buf, unsigned int buf_len, void *data) 136 + { 137 + struct scatterlist sg; 138 + 139 + sg_init_one(&sg, buf, buf_len); 140 + 141 + return virtqueue_add_inbuf(vq, &sg, 1, data, GFP_ATOMIC); 142 + } 143 + 144 + /** 145 + * viortc_msg_init() - Allocate and initialize requestq message. 146 + * @viortc: device data 147 + * @msg_type: virtio_rtc message type 148 + * @req_size: size of request buffer to be allocated 149 + * @resp_cap: size of response buffer to be allocated 150 + * 151 + * Initializes the message refcnt to 2. The refcnt will be decremented once in 152 + * the virtqueue callback, and once in the thread waiting on the message (on 153 + * completion or timeout). 154 + * 155 + * Context: Process context. 156 + * Return: non-NULL on success. 157 + */ 158 + static struct viortc_msg *viortc_msg_init(struct viortc_dev *viortc, 159 + u16 msg_type, unsigned int req_size, 160 + unsigned int resp_cap) 161 + { 162 + struct device *dev = &viortc->vdev->dev; 163 + struct virtio_rtc_req_head *req_head; 164 + struct viortc_msg *msg; 165 + 166 + msg = devm_kzalloc(dev, sizeof(*msg), GFP_KERNEL); 167 + if (!msg) 168 + return NULL; 169 + 170 + init_completion(&msg->responded); 171 + 172 + msg->req = devm_kzalloc(dev, req_size, GFP_KERNEL); 173 + if (!msg->req) 174 + goto err_free_msg; 175 + 176 + req_head = msg->req; 177 + 178 + msg->resp = devm_kzalloc(dev, resp_cap, GFP_KERNEL); 179 + if (!msg->resp) 180 + goto err_free_msg_req; 181 + 182 + msg->viortc = viortc; 183 + msg->req_size = req_size; 184 + msg->resp_cap = resp_cap; 185 + 186 + refcount_set(&msg->refcnt, 2); 187 + 188 + req_head->msg_type = virtio_cpu_to_le(msg_type, req_head->msg_type); 189 + 190 + return msg; 191 + 192 + err_free_msg_req: 193 + devm_kfree(dev, msg->req); 194 + 195 + err_free_msg: 196 + devm_kfree(dev, msg); 197 + 198 + return NULL; 199 + } 200 + 201 + /** 202 + * viortc_msg_release() - Decrement message refcnt, potentially free message. 203 + * @msg: message requested by driver 204 + * 205 + * Context: Any context. 206 + */ 207 + static void viortc_msg_release(struct viortc_msg *msg) 208 + { 209 + struct device *dev; 210 + 211 + if (refcount_dec_and_test(&msg->refcnt)) { 212 + dev = &msg->viortc->vdev->dev; 213 + 214 + devm_kfree(dev, msg->req); 215 + devm_kfree(dev, msg->resp); 216 + devm_kfree(dev, msg); 217 + } 218 + } 219 + 220 + /** 221 + * viortc_do_cb() - generic virtqueue callback logic 222 + * @vq: virtqueue 223 + * @handle_buf: function to process a used buffer 224 + * 225 + * Context: virtqueue callback, typically interrupt. Takes and releases vq lock. 226 + */ 227 + static void viortc_do_cb(struct virtqueue *vq, 228 + void (*handle_buf)(void *token, unsigned int len, 229 + struct virtqueue *vq, 230 + struct viortc_vq *viortc_vq, 231 + struct viortc_dev *viortc)) 232 + { 233 + struct viortc_dev *viortc = vq->vdev->priv; 234 + struct viortc_vq *viortc_vq; 235 + bool cb_enabled = true; 236 + unsigned long flags; 237 + unsigned int len; 238 + void *token; 239 + 240 + viortc_vq = &viortc->vqs[vq->index]; 241 + 242 + for (;;) { 243 + spin_lock_irqsave(&viortc_vq->lock, flags); 244 + 245 + if (cb_enabled) { 246 + virtqueue_disable_cb(vq); 247 + cb_enabled = false; 248 + } 249 + 250 + token = virtqueue_get_buf(vq, &len); 251 + if (!token) { 252 + if (virtqueue_enable_cb(vq)) { 253 + spin_unlock_irqrestore(&viortc_vq->lock, flags); 254 + return; 255 + } 256 + cb_enabled = true; 257 + } 258 + 259 + spin_unlock_irqrestore(&viortc_vq->lock, flags); 260 + 261 + if (token) 262 + handle_buf(token, len, vq, viortc_vq, viortc); 263 + } 264 + } 265 + 266 + /** 267 + * viortc_requestq_hdlr() - process a requestq used buffer 268 + * @token: token identifying the buffer 269 + * @len: bytes written by device 270 + * @vq: virtqueue 271 + * @viortc_vq: device specific data for virtqueue 272 + * @viortc: device data 273 + * 274 + * Signals completion for each received message. 275 + * 276 + * Context: virtqueue callback 277 + */ 278 + static void viortc_requestq_hdlr(void *token, unsigned int len, 279 + struct virtqueue *vq, 280 + struct viortc_vq *viortc_vq, 281 + struct viortc_dev *viortc) 282 + { 283 + struct viortc_msg *msg = token; 284 + 285 + msg->resp_actual_size = len; 286 + 287 + complete(&msg->responded); 288 + viortc_msg_release(msg); 289 + } 290 + 291 + /** 292 + * viortc_cb_requestq() - callback for requestq 293 + * @vq: virtqueue 294 + * 295 + * Context: virtqueue callback 296 + */ 297 + static void viortc_cb_requestq(struct virtqueue *vq) 298 + { 299 + viortc_do_cb(vq, viortc_requestq_hdlr); 300 + } 301 + 302 + /** 303 + * viortc_alarmq_hdlr() - process an alarmq used buffer 304 + * @token: token identifying the buffer 305 + * @len: bytes written by device 306 + * @vq: virtqueue 307 + * @viortc_vq: device specific data for virtqueue 308 + * @viortc: device data 309 + * 310 + * Processes a VIRTIO_RTC_NOTIF_ALARM notification by calling the RTC class 311 + * driver. Makes the buffer available again. 312 + * 313 + * Context: virtqueue callback 314 + */ 315 + static void viortc_alarmq_hdlr(void *token, unsigned int len, 316 + struct virtqueue *vq, 317 + struct viortc_vq *viortc_vq, 318 + struct viortc_dev *viortc) 319 + { 320 + struct virtio_rtc_notif_alarm *notif = token; 321 + struct virtio_rtc_notif_head *head = token; 322 + unsigned long flags; 323 + u16 clock_id; 324 + bool notify; 325 + 326 + if (len < sizeof(*head)) { 327 + dev_err_ratelimited(&viortc->vdev->dev, 328 + "%s: ignoring notification with short header\n", 329 + __func__); 330 + goto feed_vq; 331 + } 332 + 333 + if (virtio_le_to_cpu(head->msg_type) != VIRTIO_RTC_NOTIF_ALARM) { 334 + dev_err_ratelimited(&viortc->vdev->dev, 335 + "%s: ignoring unknown notification type 0x%x\n", 336 + __func__, virtio_le_to_cpu(head->msg_type)); 337 + goto feed_vq; 338 + } 339 + 340 + if (len < sizeof(*notif)) { 341 + dev_err_ratelimited(&viortc->vdev->dev, 342 + "%s: ignoring too small alarm notification\n", 343 + __func__); 344 + goto feed_vq; 345 + } 346 + 347 + clock_id = virtio_le_to_cpu(notif->clock_id); 348 + 349 + if (!viortc->viortc_class) 350 + dev_warn_ratelimited(&viortc->vdev->dev, 351 + "ignoring alarm, no RTC class device available\n"); 352 + else 353 + viortc_class_alarm(viortc->viortc_class, clock_id); 354 + 355 + feed_vq: 356 + spin_lock_irqsave(&viortc_vq->lock, flags); 357 + 358 + if (viortc_feed_vq(viortc, vq, notif, VIORTC_ALARMQ_BUF_CAP, token)) 359 + dev_warn(&viortc->vdev->dev, 360 + "%s: failed to re-expose input buffer\n", __func__); 361 + 362 + notify = virtqueue_kick_prepare(vq); 363 + 364 + spin_unlock_irqrestore(&viortc_vq->lock, flags); 365 + 366 + if (notify) 367 + virtqueue_notify(vq); 368 + } 369 + 370 + /** 371 + * viortc_cb_alarmq() - callback for alarmq 372 + * @vq: virtqueue 373 + * 374 + * Context: virtqueue callback 375 + */ 376 + static void viortc_cb_alarmq(struct virtqueue *vq) 377 + { 378 + viortc_do_cb(vq, viortc_alarmq_hdlr); 379 + } 380 + 381 + /** 382 + * viortc_get_resp_errno() - converts virtio_rtc errnos to system errnos 383 + * @resp_head: message response header 384 + * 385 + * Return: negative system errno, or 0 386 + */ 387 + static int viortc_get_resp_errno(struct virtio_rtc_resp_head *resp_head) 388 + { 389 + switch (virtio_le_to_cpu(resp_head->status)) { 390 + case VIRTIO_RTC_S_OK: 391 + return 0; 392 + case VIRTIO_RTC_S_EOPNOTSUPP: 393 + return -EOPNOTSUPP; 394 + case VIRTIO_RTC_S_EINVAL: 395 + return -EINVAL; 396 + case VIRTIO_RTC_S_ENODEV: 397 + return -ENODEV; 398 + case VIRTIO_RTC_S_EIO: 399 + default: 400 + return -EIO; 401 + } 402 + } 403 + 404 + /** 405 + * viortc_msg_xfer() - send message request, wait until message response 406 + * @vq: virtqueue 407 + * @msg: message with driver request 408 + * @timeout_jiffies: message response timeout, 0 for no timeout 409 + * 410 + * Context: Process context. Takes and releases vq.lock. May sleep. 411 + * Return: Zero on success, negative error code otherwise. 412 + */ 413 + static int viortc_msg_xfer(struct viortc_vq *vq, struct viortc_msg *msg, 414 + unsigned long timeout_jiffies) 415 + { 416 + struct scatterlist out_sg[1]; 417 + struct scatterlist in_sg[1]; 418 + struct scatterlist *sgs[2]; 419 + unsigned long flags; 420 + long timeout_ret; 421 + bool notify; 422 + int ret; 423 + 424 + sgs[0] = out_sg; 425 + sgs[1] = in_sg; 426 + 427 + sg_init_one(out_sg, msg->req, msg->req_size); 428 + sg_init_one(in_sg, msg->resp, msg->resp_cap); 429 + 430 + spin_lock_irqsave(&vq->lock, flags); 431 + 432 + ret = virtqueue_add_sgs(vq->vq, sgs, 1, 1, msg, GFP_ATOMIC); 433 + if (ret) { 434 + spin_unlock_irqrestore(&vq->lock, flags); 435 + /* 436 + * Release in place of the response callback, which will never 437 + * come. 438 + */ 439 + viortc_msg_release(msg); 440 + return ret; 441 + } 442 + 443 + notify = virtqueue_kick_prepare(vq->vq); 444 + 445 + spin_unlock_irqrestore(&vq->lock, flags); 446 + 447 + if (notify) 448 + virtqueue_notify(vq->vq); 449 + 450 + if (timeout_jiffies) { 451 + timeout_ret = wait_for_completion_interruptible_timeout( 452 + &msg->responded, timeout_jiffies); 453 + 454 + if (!timeout_ret) 455 + return -ETIMEDOUT; 456 + else if (timeout_ret < 0) 457 + return (int)timeout_ret; 458 + } else { 459 + ret = wait_for_completion_interruptible(&msg->responded); 460 + if (ret) 461 + return ret; 462 + } 463 + 464 + if (msg->resp_actual_size < sizeof(struct virtio_rtc_resp_head)) 465 + return -EINVAL; 466 + 467 + ret = viortc_get_resp_errno(msg->resp); 468 + if (ret) 469 + return ret; 470 + 471 + /* 472 + * There is not yet a case where returning a short message would make 473 + * sense, so consider any deviation an error. 474 + */ 475 + if (msg->resp_actual_size != msg->resp_cap) 476 + return -EINVAL; 477 + 478 + return 0; 479 + } 480 + 481 + /* 482 + * common message handle macros for messages of different types 483 + */ 484 + 485 + /** 486 + * VIORTC_DECLARE_MSG_HDL_ONSTACK() - declare message handle on stack 487 + * @hdl: message handle name 488 + * @msg_id: message type id 489 + * @msg_req: message request type 490 + * @msg_resp: message response type 491 + */ 492 + #define VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, msg_id, msg_req, msg_resp) \ 493 + struct { \ 494 + struct viortc_msg *msg; \ 495 + msg_req *req; \ 496 + msg_resp *resp; \ 497 + unsigned int req_size; \ 498 + unsigned int resp_cap; \ 499 + u16 msg_type; \ 500 + } hdl = { \ 501 + NULL, NULL, NULL, sizeof(msg_req), sizeof(msg_resp), (msg_id), \ 502 + } 503 + 504 + /** 505 + * VIORTC_MSG() - extract message from message handle 506 + * @hdl: message handle 507 + * 508 + * Return: struct viortc_msg 509 + */ 510 + #define VIORTC_MSG(hdl) ((hdl).msg) 511 + 512 + /** 513 + * VIORTC_MSG_INIT() - initialize message handle 514 + * @hdl: message handle 515 + * @viortc: device data (struct viortc_dev *) 516 + * 517 + * Context: Process context. 518 + * Return: 0 on success, -ENOMEM otherwise. 519 + */ 520 + #define VIORTC_MSG_INIT(hdl, viortc) \ 521 + ({ \ 522 + typeof(hdl) *_hdl = &(hdl); \ 523 + \ 524 + _hdl->msg = viortc_msg_init((viortc), _hdl->msg_type, \ 525 + _hdl->req_size, _hdl->resp_cap); \ 526 + if (_hdl->msg) { \ 527 + _hdl->req = _hdl->msg->req; \ 528 + _hdl->resp = _hdl->msg->resp; \ 529 + } \ 530 + _hdl->msg ? 0 : -ENOMEM; \ 531 + }) 532 + 533 + /** 534 + * VIORTC_MSG_WRITE() - write a request message field 535 + * @hdl: message handle 536 + * @dest_member: request message field name 537 + * @src_ptr: pointer to data of compatible type 538 + * 539 + * Writes the field in little-endian format. 540 + */ 541 + #define VIORTC_MSG_WRITE(hdl, dest_member, src_ptr) \ 542 + do { \ 543 + typeof(hdl) _hdl = (hdl); \ 544 + typeof(src_ptr) _src_ptr = (src_ptr); \ 545 + \ 546 + /* Sanity check: must match the member's type */ \ 547 + typecheck(typeof(virtio_le_to_cpu(_hdl.req->dest_member)), \ 548 + *_src_ptr); \ 549 + \ 550 + _hdl.req->dest_member = \ 551 + virtio_cpu_to_le(*_src_ptr, _hdl.req->dest_member); \ 552 + } while (0) 553 + 554 + /** 555 + * VIORTC_MSG_READ() - read from a response message field 556 + * @hdl: message handle 557 + * @src_member: response message field name 558 + * @dest_ptr: pointer to data of compatible type 559 + * 560 + * Converts from little-endian format and writes to dest_ptr. 561 + */ 562 + #define VIORTC_MSG_READ(hdl, src_member, dest_ptr) \ 563 + do { \ 564 + typeof(dest_ptr) _dest_ptr = (dest_ptr); \ 565 + \ 566 + /* Sanity check: must match the member's type */ \ 567 + typecheck(typeof(virtio_le_to_cpu((hdl).resp->src_member)), \ 568 + *_dest_ptr); \ 569 + \ 570 + *_dest_ptr = virtio_le_to_cpu((hdl).resp->src_member); \ 571 + } while (0) 572 + 573 + /* 574 + * read requests 575 + */ 576 + 577 + /** timeout for clock readings, where timeouts are considered non-fatal */ 578 + #define VIORTC_MSG_READ_TIMEOUT secs_to_jiffies(60) 579 + 580 + /** 581 + * viortc_read() - VIRTIO_RTC_REQ_READ wrapper 582 + * @viortc: device data 583 + * @vio_clk_id: virtio_rtc clock id 584 + * @reading: clock reading [ns] 585 + * 586 + * Context: Process context. 587 + * Return: Zero on success, negative error code otherwise. 588 + */ 589 + int viortc_read(struct viortc_dev *viortc, u16 vio_clk_id, u64 *reading) 590 + { 591 + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_READ, 592 + struct virtio_rtc_req_read, 593 + struct virtio_rtc_resp_read); 594 + int ret; 595 + 596 + ret = VIORTC_MSG_INIT(hdl, viortc); 597 + if (ret) 598 + return ret; 599 + 600 + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); 601 + 602 + ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl), 603 + VIORTC_MSG_READ_TIMEOUT); 604 + if (ret) { 605 + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, 606 + ret); 607 + goto out_release; 608 + } 609 + 610 + VIORTC_MSG_READ(hdl, clock_reading, reading); 611 + 612 + out_release: 613 + viortc_msg_release(VIORTC_MSG(hdl)); 614 + 615 + return ret; 616 + } 617 + 618 + /** 619 + * viortc_read_cross() - VIRTIO_RTC_REQ_READ_CROSS wrapper 620 + * @viortc: device data 621 + * @vio_clk_id: virtio_rtc clock id 622 + * @hw_counter: virtio_rtc HW counter type 623 + * @reading: clock reading [ns] 624 + * @cycles: HW counter cycles during clock reading 625 + * 626 + * Context: Process context. 627 + * Return: Zero on success, negative error code otherwise. 628 + */ 629 + int viortc_read_cross(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter, 630 + u64 *reading, u64 *cycles) 631 + { 632 + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_READ_CROSS, 633 + struct virtio_rtc_req_read_cross, 634 + struct virtio_rtc_resp_read_cross); 635 + int ret; 636 + 637 + ret = VIORTC_MSG_INIT(hdl, viortc); 638 + if (ret) 639 + return ret; 640 + 641 + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); 642 + VIORTC_MSG_WRITE(hdl, hw_counter, &hw_counter); 643 + 644 + ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl), 645 + VIORTC_MSG_READ_TIMEOUT); 646 + if (ret) { 647 + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, 648 + ret); 649 + goto out_release; 650 + } 651 + 652 + VIORTC_MSG_READ(hdl, clock_reading, reading); 653 + VIORTC_MSG_READ(hdl, counter_cycles, cycles); 654 + 655 + out_release: 656 + viortc_msg_release(VIORTC_MSG(hdl)); 657 + 658 + return ret; 659 + } 660 + 661 + /* 662 + * control requests 663 + */ 664 + 665 + /** 666 + * viortc_cfg() - VIRTIO_RTC_REQ_CFG wrapper 667 + * @viortc: device data 668 + * @num_clocks: # of virtio_rtc clocks 669 + * 670 + * Context: Process context. 671 + * Return: Zero on success, negative error code otherwise. 672 + */ 673 + static int viortc_cfg(struct viortc_dev *viortc, u16 *num_clocks) 674 + { 675 + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_CFG, 676 + struct virtio_rtc_req_cfg, 677 + struct virtio_rtc_resp_cfg); 678 + int ret; 679 + 680 + ret = VIORTC_MSG_INIT(hdl, viortc); 681 + if (ret) 682 + return ret; 683 + 684 + ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl), 685 + 0); 686 + if (ret) { 687 + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, 688 + ret); 689 + goto out_release; 690 + } 691 + 692 + VIORTC_MSG_READ(hdl, num_clocks, num_clocks); 693 + 694 + out_release: 695 + viortc_msg_release(VIORTC_MSG(hdl)); 696 + 697 + return ret; 698 + } 699 + 700 + /** 701 + * viortc_clock_cap() - VIRTIO_RTC_REQ_CLOCK_CAP wrapper 702 + * @viortc: device data 703 + * @vio_clk_id: virtio_rtc clock id 704 + * @type: virtio_rtc clock type 705 + * @leap_second_smearing: virtio_rtc smearing variant 706 + * @flags: struct virtio_rtc_resp_clock_cap.flags 707 + * 708 + * Context: Process context. 709 + * Return: Zero on success, negative error code otherwise. 710 + */ 711 + static int viortc_clock_cap(struct viortc_dev *viortc, u16 vio_clk_id, u8 *type, 712 + u8 *leap_second_smearing, u8 *flags) 713 + { 714 + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_CLOCK_CAP, 715 + struct virtio_rtc_req_clock_cap, 716 + struct virtio_rtc_resp_clock_cap); 717 + int ret; 718 + 719 + ret = VIORTC_MSG_INIT(hdl, viortc); 720 + if (ret) 721 + return ret; 722 + 723 + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); 724 + 725 + ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl), 726 + 0); 727 + if (ret) { 728 + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, 729 + ret); 730 + goto out_release; 731 + } 732 + 733 + VIORTC_MSG_READ(hdl, type, type); 734 + VIORTC_MSG_READ(hdl, leap_second_smearing, leap_second_smearing); 735 + VIORTC_MSG_READ(hdl, flags, flags); 736 + 737 + out_release: 738 + viortc_msg_release(VIORTC_MSG(hdl)); 739 + 740 + return ret; 741 + } 742 + 743 + /** 744 + * viortc_cross_cap() - VIRTIO_RTC_REQ_CROSS_CAP wrapper 745 + * @viortc: device data 746 + * @vio_clk_id: virtio_rtc clock id 747 + * @hw_counter: virtio_rtc HW counter type 748 + * @supported: xtstamping is supported for the vio_clk_id/hw_counter pair 749 + * 750 + * Context: Process context. 751 + * Return: Zero on success, negative error code otherwise. 752 + */ 753 + int viortc_cross_cap(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter, 754 + bool *supported) 755 + { 756 + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_CROSS_CAP, 757 + struct virtio_rtc_req_cross_cap, 758 + struct virtio_rtc_resp_cross_cap); 759 + u8 flags; 760 + int ret; 761 + 762 + ret = VIORTC_MSG_INIT(hdl, viortc); 763 + if (ret) 764 + return ret; 765 + 766 + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); 767 + VIORTC_MSG_WRITE(hdl, hw_counter, &hw_counter); 768 + 769 + ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl), 770 + 0); 771 + if (ret) { 772 + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, 773 + ret); 774 + goto out_release; 775 + } 776 + 777 + VIORTC_MSG_READ(hdl, flags, &flags); 778 + *supported = !!(flags & VIRTIO_RTC_FLAG_CROSS_CAP); 779 + 780 + out_release: 781 + viortc_msg_release(VIORTC_MSG(hdl)); 782 + 783 + return ret; 784 + } 785 + 786 + /** 787 + * viortc_read_alarm() - VIRTIO_RTC_REQ_READ_ALARM wrapper 788 + * @viortc: device data 789 + * @vio_clk_id: virtio_rtc clock id 790 + * @alarm_time: alarm time in ns 791 + * @enabled: whether alarm is enabled 792 + * 793 + * Context: Process context. 794 + * Return: Zero on success, negative error code otherwise. 795 + */ 796 + int viortc_read_alarm(struct viortc_dev *viortc, u16 vio_clk_id, 797 + u64 *alarm_time, bool *enabled) 798 + { 799 + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_READ_ALARM, 800 + struct virtio_rtc_req_read_alarm, 801 + struct virtio_rtc_resp_read_alarm); 802 + u8 flags; 803 + int ret; 804 + 805 + ret = VIORTC_MSG_INIT(hdl, viortc); 806 + if (ret) 807 + return ret; 808 + 809 + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); 810 + 811 + ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl), 812 + 0); 813 + if (ret) { 814 + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, 815 + ret); 816 + goto out_release; 817 + } 818 + 819 + VIORTC_MSG_READ(hdl, alarm_time, alarm_time); 820 + VIORTC_MSG_READ(hdl, flags, &flags); 821 + 822 + *enabled = !!(flags & VIRTIO_RTC_FLAG_ALARM_ENABLED); 823 + 824 + out_release: 825 + viortc_msg_release(VIORTC_MSG(hdl)); 826 + 827 + return ret; 828 + } 829 + 830 + /** 831 + * viortc_set_alarm() - VIRTIO_RTC_REQ_SET_ALARM wrapper 832 + * @viortc: device data 833 + * @vio_clk_id: virtio_rtc clock id 834 + * @alarm_time: alarm time in ns 835 + * @alarm_enable: enable or disable alarm 836 + * 837 + * Context: Process context. 838 + * Return: Zero on success, negative error code otherwise. 839 + */ 840 + int viortc_set_alarm(struct viortc_dev *viortc, u16 vio_clk_id, u64 alarm_time, 841 + bool alarm_enable) 842 + { 843 + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_SET_ALARM, 844 + struct virtio_rtc_req_set_alarm, 845 + struct virtio_rtc_resp_set_alarm); 846 + u8 flags = 0; 847 + int ret; 848 + 849 + ret = VIORTC_MSG_INIT(hdl, viortc); 850 + if (ret) 851 + return ret; 852 + 853 + if (alarm_enable) 854 + flags |= VIRTIO_RTC_FLAG_ALARM_ENABLED; 855 + 856 + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); 857 + VIORTC_MSG_WRITE(hdl, alarm_time, &alarm_time); 858 + VIORTC_MSG_WRITE(hdl, flags, &flags); 859 + 860 + ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl), 861 + 0); 862 + if (ret) { 863 + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, 864 + ret); 865 + goto out_release; 866 + } 867 + 868 + out_release: 869 + viortc_msg_release(VIORTC_MSG(hdl)); 870 + 871 + return ret; 872 + } 873 + 874 + /** 875 + * viortc_set_alarm_enabled() - VIRTIO_RTC_REQ_SET_ALARM_ENABLED wrapper 876 + * @viortc: device data 877 + * @vio_clk_id: virtio_rtc clock id 878 + * @alarm_enable: enable or disable alarm 879 + * 880 + * Context: Process context. 881 + * Return: Zero on success, negative error code otherwise. 882 + */ 883 + int viortc_set_alarm_enabled(struct viortc_dev *viortc, u16 vio_clk_id, 884 + bool alarm_enable) 885 + { 886 + VIORTC_DECLARE_MSG_HDL_ONSTACK(hdl, VIRTIO_RTC_REQ_SET_ALARM_ENABLED, 887 + struct virtio_rtc_req_set_alarm_enabled, 888 + struct virtio_rtc_resp_set_alarm_enabled); 889 + u8 flags = 0; 890 + int ret; 891 + 892 + ret = VIORTC_MSG_INIT(hdl, viortc); 893 + if (ret) 894 + return ret; 895 + 896 + if (alarm_enable) 897 + flags |= VIRTIO_RTC_FLAG_ALARM_ENABLED; 898 + 899 + VIORTC_MSG_WRITE(hdl, clock_id, &vio_clk_id); 900 + VIORTC_MSG_WRITE(hdl, flags, &flags); 901 + 902 + ret = viortc_msg_xfer(&viortc->vqs[VIORTC_REQUESTQ], VIORTC_MSG(hdl), 903 + 0); 904 + if (ret) { 905 + dev_dbg(&viortc->vdev->dev, "%s: xfer returned %d\n", __func__, 906 + ret); 907 + goto out_release; 908 + } 909 + 910 + out_release: 911 + viortc_msg_release(VIORTC_MSG(hdl)); 912 + 913 + return ret; 914 + } 915 + 916 + /* 917 + * init, deinit 918 + */ 919 + 920 + /** 921 + * viortc_init_rtc_class_clock() - init and register a RTC class device 922 + * @viortc: device data 923 + * @vio_clk_id: virtio_rtc clock id 924 + * @clock_type: virtio_rtc clock type 925 + * @flags: struct virtio_rtc_resp_clock_cap.flags 926 + * 927 + * The clock must be a UTC-like clock. 928 + * 929 + * Context: Process context. 930 + * Return: Positive if registered, zero if not supported by configuration, 931 + * negative error code otherwise. 932 + */ 933 + static int viortc_init_rtc_class_clock(struct viortc_dev *viortc, 934 + u16 vio_clk_id, u8 clock_type, u8 flags) 935 + { 936 + struct virtio_device *vdev = viortc->vdev; 937 + struct viortc_class *viortc_class; 938 + struct device *dev = &vdev->dev; 939 + bool have_alarm; 940 + 941 + if (clock_type != VIRTIO_RTC_CLOCK_UTC_SMEARED) { 942 + dev_info(dev, 943 + "not creating RTC class device for clock %d, which may step on leap seconds\n", 944 + vio_clk_id); 945 + return 0; 946 + } 947 + 948 + if (viortc->viortc_class) { 949 + dev_warn_once(dev, 950 + "multiple UTC-like clocks are present, but creating only one RTC class device\n"); 951 + return 0; 952 + } 953 + 954 + have_alarm = viortc_alarms_supported(vdev) && 955 + !!(flags & VIRTIO_RTC_FLAG_ALARM_CAP); 956 + 957 + viortc_class = viortc_class_init(viortc, vio_clk_id, have_alarm, dev); 958 + if (IS_ERR(viortc_class)) 959 + return PTR_ERR(viortc_class); 960 + 961 + viortc->viortc_class = viortc_class; 962 + 963 + if (have_alarm) 964 + devm_device_init_wakeup(dev); 965 + 966 + return viortc_class_register(viortc_class) ?: 1; 967 + } 968 + 969 + /** 970 + * viortc_init_ptp_clock() - init and register PTP clock 971 + * @viortc: device data 972 + * @vio_clk_id: virtio_rtc clock id 973 + * @clock_type: virtio_rtc clock type 974 + * @leap_second_smearing: virtio_rtc leap second smearing 975 + * 976 + * Context: Process context. 977 + * Return: Positive if registered, zero if not supported by configuration, 978 + * negative error code otherwise. 979 + */ 980 + static int viortc_init_ptp_clock(struct viortc_dev *viortc, u16 vio_clk_id, 981 + u8 clock_type, u8 leap_second_smearing) 982 + { 983 + struct device *dev = &viortc->vdev->dev; 984 + char ptp_clock_name[PTP_CLOCK_NAME_LEN]; 985 + struct viortc_ptp_clock *vio_ptp; 986 + 987 + snprintf(ptp_clock_name, PTP_CLOCK_NAME_LEN, 988 + "Virtio PTP type %hhu/variant %hhu", clock_type, 989 + leap_second_smearing); 990 + 991 + vio_ptp = viortc_ptp_register(viortc, dev, vio_clk_id, ptp_clock_name); 992 + if (IS_ERR(vio_ptp)) { 993 + dev_err(dev, "failed to register PTP clock '%s'\n", 994 + ptp_clock_name); 995 + return PTR_ERR(vio_ptp); 996 + } 997 + 998 + viortc->clocks_to_unregister[vio_clk_id] = vio_ptp; 999 + 1000 + return !!vio_ptp; 1001 + } 1002 + 1003 + /** 1004 + * viortc_init_clock() - init local representation of virtio_rtc clock 1005 + * @viortc: device data 1006 + * @vio_clk_id: virtio_rtc clock id 1007 + * 1008 + * Initializes PHC and/or RTC class device to represent virtio_rtc clock. 1009 + * 1010 + * Context: Process context. 1011 + * Return: Zero on success, negative error code otherwise. 1012 + */ 1013 + static int viortc_init_clock(struct viortc_dev *viortc, u16 vio_clk_id) 1014 + { 1015 + u8 clock_type, leap_second_smearing, flags; 1016 + bool is_exposed = false; 1017 + int ret; 1018 + 1019 + ret = viortc_clock_cap(viortc, vio_clk_id, &clock_type, 1020 + &leap_second_smearing, &flags); 1021 + if (ret) 1022 + return ret; 1023 + 1024 + if (IS_ENABLED(CONFIG_VIRTIO_RTC_CLASS) && 1025 + (clock_type == VIRTIO_RTC_CLOCK_UTC || 1026 + clock_type == VIRTIO_RTC_CLOCK_UTC_SMEARED || 1027 + clock_type == VIRTIO_RTC_CLOCK_UTC_MAYBE_SMEARED)) { 1028 + ret = viortc_init_rtc_class_clock(viortc, vio_clk_id, 1029 + clock_type, flags); 1030 + if (ret < 0) 1031 + return ret; 1032 + if (ret > 0) 1033 + is_exposed = true; 1034 + } 1035 + 1036 + if (IS_ENABLED(CONFIG_VIRTIO_RTC_PTP)) { 1037 + ret = viortc_init_ptp_clock(viortc, vio_clk_id, clock_type, 1038 + leap_second_smearing); 1039 + if (ret < 0) 1040 + return ret; 1041 + if (ret > 0) 1042 + is_exposed = true; 1043 + } 1044 + 1045 + if (!is_exposed) 1046 + dev_warn(&viortc->vdev->dev, 1047 + "cannot expose clock %d (type %d, variant %d) to userspace\n", 1048 + vio_clk_id, clock_type, leap_second_smearing); 1049 + 1050 + return 0; 1051 + } 1052 + 1053 + /** 1054 + * viortc_clocks_deinit() - unregister PHCs, stop RTC ops 1055 + * @viortc: device data 1056 + */ 1057 + static void viortc_clocks_deinit(struct viortc_dev *viortc) 1058 + { 1059 + struct viortc_ptp_clock *vio_ptp; 1060 + unsigned int i; 1061 + 1062 + for (i = 0; i < viortc->num_clocks; i++) { 1063 + vio_ptp = viortc->clocks_to_unregister[i]; 1064 + 1065 + if (!vio_ptp) 1066 + continue; 1067 + 1068 + viortc->clocks_to_unregister[i] = NULL; 1069 + 1070 + WARN_ON(viortc_ptp_unregister(vio_ptp, &viortc->vdev->dev)); 1071 + } 1072 + 1073 + if (viortc->viortc_class) 1074 + viortc_class_stop(viortc->viortc_class); 1075 + } 1076 + 1077 + /** 1078 + * viortc_clocks_init() - init local representations of virtio_rtc clocks 1079 + * @viortc: device data 1080 + * 1081 + * Context: Process context. 1082 + * Return: Zero on success, negative error code otherwise. 1083 + */ 1084 + static int viortc_clocks_init(struct viortc_dev *viortc) 1085 + { 1086 + u16 num_clocks; 1087 + unsigned int i; 1088 + int ret; 1089 + 1090 + ret = viortc_cfg(viortc, &num_clocks); 1091 + if (ret) 1092 + return ret; 1093 + 1094 + if (num_clocks < 1) { 1095 + dev_err(&viortc->vdev->dev, "device reported 0 clocks\n"); 1096 + return -ENODEV; 1097 + } 1098 + 1099 + viortc->num_clocks = num_clocks; 1100 + 1101 + viortc->clocks_to_unregister = 1102 + devm_kcalloc(&viortc->vdev->dev, num_clocks, 1103 + sizeof(*viortc->clocks_to_unregister), GFP_KERNEL); 1104 + if (!viortc->clocks_to_unregister) 1105 + return -ENOMEM; 1106 + 1107 + for (i = 0; i < num_clocks; i++) { 1108 + ret = viortc_init_clock(viortc, i); 1109 + if (ret) 1110 + goto err_deinit_clocks; 1111 + } 1112 + 1113 + return 0; 1114 + 1115 + err_deinit_clocks: 1116 + viortc_clocks_deinit(viortc); 1117 + 1118 + return ret; 1119 + } 1120 + 1121 + /** 1122 + * viortc_populate_vq() - populate alarmq with device-writable buffers 1123 + * @viortc: device data 1124 + * @viortc_vq: device specific data for virtqueue 1125 + * @buf_cap: device-writable buffer size in bytes 1126 + * @lock: lock queue during accesses 1127 + * 1128 + * Populates the alarmq with pre-allocated buffers. 1129 + * 1130 + * The caller is responsible for kicking the device. 1131 + * 1132 + * Context: Process context. 1133 + * Return: Zero on success, negative error code otherwise. 1134 + */ 1135 + static int viortc_populate_vq(struct viortc_dev *viortc, 1136 + struct viortc_vq *viortc_vq, u32 buf_cap, 1137 + bool lock) 1138 + { 1139 + unsigned int num_elems, i; 1140 + struct virtqueue *vq; 1141 + unsigned long flags; 1142 + void *buf; 1143 + int ret; 1144 + 1145 + num_elems = viortc->num_alarmq_bufs; 1146 + vq = viortc_vq->vq; 1147 + 1148 + for (i = 0; i < num_elems; i++) { 1149 + buf = viortc->alarmq_bufs[i]; 1150 + 1151 + if (lock) { 1152 + spin_lock_irqsave(&viortc_vq->lock, flags); 1153 + 1154 + ret = viortc_feed_vq(viortc, vq, buf, buf_cap, buf); 1155 + 1156 + spin_unlock_irqrestore(&viortc_vq->lock, flags); 1157 + } else { 1158 + ret = viortc_feed_vq(viortc, vq, buf, buf_cap, buf); 1159 + } 1160 + 1161 + if (ret) 1162 + return ret; 1163 + } 1164 + 1165 + return 0; 1166 + } 1167 + 1168 + /** 1169 + * viortc_alloc_vq_bufs() - allocate alarmq buffers 1170 + * @viortc: device data 1171 + * @num_elems: # of buffers 1172 + * @buf_cap: per-buffer device-writable bytes 1173 + * 1174 + * Context: Process context. 1175 + * Return: Zero on success, negative error code otherwise. 1176 + */ 1177 + static int viortc_alloc_vq_bufs(struct viortc_dev *viortc, 1178 + unsigned int num_elems, u32 buf_cap) 1179 + { 1180 + struct device *dev = &viortc->vdev->dev; 1181 + void **buf_list; 1182 + unsigned int i; 1183 + void *buf; 1184 + 1185 + buf_list = devm_kcalloc(dev, num_elems, sizeof(*buf_list), GFP_KERNEL); 1186 + if (!buf_list) 1187 + return -ENOMEM; 1188 + 1189 + viortc->alarmq_bufs = buf_list; 1190 + viortc->num_alarmq_bufs = num_elems; 1191 + 1192 + for (i = 0; i < num_elems; i++) { 1193 + buf = devm_kzalloc(dev, buf_cap, GFP_KERNEL); 1194 + if (!buf) 1195 + return -ENOMEM; 1196 + 1197 + buf_list[i] = buf; 1198 + } 1199 + 1200 + return 0; 1201 + } 1202 + 1203 + /** 1204 + * viortc_init_vqs() - init virtqueues 1205 + * @viortc: device data 1206 + * 1207 + * Inits virtqueues and associated data. 1208 + * 1209 + * Context: Process context. 1210 + * Return: Zero on success, negative error code otherwise. 1211 + */ 1212 + static int viortc_init_vqs(struct viortc_dev *viortc) 1213 + { 1214 + struct virtqueue *vqs[VIORTC_MAX_NR_QUEUES]; 1215 + struct virtqueue_info vqs_info[] = { 1216 + { "requestq", viortc_cb_requestq }, 1217 + { "alarmq", viortc_cb_alarmq }, 1218 + }; 1219 + struct virtio_device *vdev = viortc->vdev; 1220 + unsigned int num_elems; 1221 + int nr_queues, ret; 1222 + bool have_alarms; 1223 + 1224 + have_alarms = viortc_alarms_supported(vdev); 1225 + 1226 + if (have_alarms) 1227 + nr_queues = VIORTC_ALARMQ + 1; 1228 + else 1229 + nr_queues = VIORTC_REQUESTQ + 1; 1230 + 1231 + ret = virtio_find_vqs(vdev, nr_queues, vqs, vqs_info, NULL); 1232 + if (ret) 1233 + return ret; 1234 + 1235 + viortc->vqs[VIORTC_REQUESTQ].vq = vqs[VIORTC_REQUESTQ]; 1236 + spin_lock_init(&viortc->vqs[VIORTC_REQUESTQ].lock); 1237 + 1238 + if (have_alarms) { 1239 + viortc->vqs[VIORTC_ALARMQ].vq = vqs[VIORTC_ALARMQ]; 1240 + spin_lock_init(&viortc->vqs[VIORTC_ALARMQ].lock); 1241 + 1242 + num_elems = virtqueue_get_vring_size(vqs[VIORTC_ALARMQ]); 1243 + if (num_elems == 0) 1244 + return -ENOSPC; 1245 + 1246 + if (!viortc->alarmq_bufs) { 1247 + ret = viortc_alloc_vq_bufs(viortc, num_elems, 1248 + VIORTC_ALARMQ_BUF_CAP); 1249 + if (ret) 1250 + return ret; 1251 + } else { 1252 + viortc->num_alarmq_bufs = 1253 + min(num_elems, viortc->num_alarmq_bufs); 1254 + } 1255 + } 1256 + 1257 + return 0; 1258 + } 1259 + 1260 + /** 1261 + * viortc_probe() - probe a virtio_rtc virtio device 1262 + * @vdev: virtio device 1263 + * 1264 + * Context: Process context. 1265 + * Return: Zero on success, negative error code otherwise. 1266 + */ 1267 + static int viortc_probe(struct virtio_device *vdev) 1268 + { 1269 + struct viortc_vq *alarm_viortc_vq; 1270 + struct virtqueue *alarm_vq; 1271 + struct viortc_dev *viortc; 1272 + unsigned long flags; 1273 + bool notify; 1274 + int ret; 1275 + 1276 + viortc = devm_kzalloc(&vdev->dev, sizeof(*viortc), GFP_KERNEL); 1277 + if (!viortc) 1278 + return -ENOMEM; 1279 + 1280 + vdev->priv = viortc; 1281 + viortc->vdev = vdev; 1282 + 1283 + ret = viortc_init_vqs(viortc); 1284 + if (ret) 1285 + return ret; 1286 + 1287 + virtio_device_ready(vdev); 1288 + 1289 + ret = viortc_clocks_init(viortc); 1290 + if (ret) 1291 + goto err_reset_vdev; 1292 + 1293 + if (viortc_alarms_supported(vdev)) { 1294 + alarm_viortc_vq = &viortc->vqs[VIORTC_ALARMQ]; 1295 + alarm_vq = alarm_viortc_vq->vq; 1296 + 1297 + ret = viortc_populate_vq(viortc, alarm_viortc_vq, 1298 + VIORTC_ALARMQ_BUF_CAP, true); 1299 + if (ret) 1300 + goto err_deinit_clocks; 1301 + 1302 + spin_lock_irqsave(&alarm_viortc_vq->lock, flags); 1303 + notify = virtqueue_kick_prepare(alarm_vq); 1304 + spin_unlock_irqrestore(&alarm_viortc_vq->lock, flags); 1305 + 1306 + if (notify && !virtqueue_notify(alarm_vq)) { 1307 + ret = -EIO; 1308 + goto err_deinit_clocks; 1309 + } 1310 + } 1311 + 1312 + return 0; 1313 + 1314 + err_deinit_clocks: 1315 + viortc_clocks_deinit(viortc); 1316 + 1317 + err_reset_vdev: 1318 + virtio_reset_device(vdev); 1319 + vdev->config->del_vqs(vdev); 1320 + 1321 + return ret; 1322 + } 1323 + 1324 + /** 1325 + * viortc_remove() - remove a virtio_rtc virtio device 1326 + * @vdev: virtio device 1327 + */ 1328 + static void viortc_remove(struct virtio_device *vdev) 1329 + { 1330 + struct viortc_dev *viortc = vdev->priv; 1331 + 1332 + viortc_clocks_deinit(viortc); 1333 + 1334 + virtio_reset_device(vdev); 1335 + vdev->config->del_vqs(vdev); 1336 + } 1337 + 1338 + static int viortc_freeze(struct virtio_device *dev) 1339 + { 1340 + /* 1341 + * Do not reset the device, so that the device may still wake up the 1342 + * system through an alarmq notification. 1343 + */ 1344 + 1345 + return 0; 1346 + } 1347 + 1348 + static int viortc_restore(struct virtio_device *dev) 1349 + { 1350 + struct viortc_dev *viortc = dev->priv; 1351 + struct viortc_vq *alarm_viortc_vq; 1352 + struct virtqueue *alarm_vq; 1353 + bool notify = false; 1354 + int ret; 1355 + 1356 + ret = viortc_init_vqs(viortc); 1357 + if (ret) 1358 + return ret; 1359 + 1360 + alarm_viortc_vq = &viortc->vqs[VIORTC_ALARMQ]; 1361 + alarm_vq = alarm_viortc_vq->vq; 1362 + 1363 + if (viortc_alarms_supported(dev)) { 1364 + ret = viortc_populate_vq(viortc, alarm_viortc_vq, 1365 + VIORTC_ALARMQ_BUF_CAP, false); 1366 + if (ret) 1367 + return ret; 1368 + 1369 + notify = virtqueue_kick_prepare(alarm_vq); 1370 + } 1371 + 1372 + virtio_device_ready(dev); 1373 + 1374 + if (notify && !virtqueue_notify(alarm_vq)) 1375 + ret = -EIO; 1376 + 1377 + return ret; 1378 + } 1379 + 1380 + static unsigned int features[] = { 1381 + #if IS_ENABLED(CONFIG_VIRTIO_RTC_CLASS) 1382 + VIRTIO_RTC_F_ALARM, 1383 + #endif 1384 + }; 1385 + 1386 + static struct virtio_device_id id_table[] = { 1387 + { VIRTIO_ID_CLOCK, VIRTIO_DEV_ANY_ID }, 1388 + { 0 }, 1389 + }; 1390 + MODULE_DEVICE_TABLE(virtio, id_table); 1391 + 1392 + static struct virtio_driver virtio_rtc_drv = { 1393 + .driver.name = KBUILD_MODNAME, 1394 + .feature_table = features, 1395 + .feature_table_size = ARRAY_SIZE(features), 1396 + .id_table = id_table, 1397 + .probe = viortc_probe, 1398 + .remove = viortc_remove, 1399 + .freeze = pm_sleep_ptr(viortc_freeze), 1400 + .restore = pm_sleep_ptr(viortc_restore), 1401 + }; 1402 + 1403 + module_virtio_driver(virtio_rtc_drv); 1404 + 1405 + MODULE_DESCRIPTION("Virtio RTC driver"); 1406 + MODULE_AUTHOR("Qualcomm Innovation Center, Inc."); 1407 + MODULE_LICENSE("GPL");
+122
drivers/virtio/virtio_rtc_internal.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * virtio_rtc internal interfaces 4 + * 5 + * Copyright (C) 2022-2023 OpenSynergy GmbH 6 + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. 7 + */ 8 + 9 + #ifndef _VIRTIO_RTC_INTERNAL_H_ 10 + #define _VIRTIO_RTC_INTERNAL_H_ 11 + 12 + #include <linux/device.h> 13 + #include <linux/err.h> 14 + #include <linux/ptp_clock_kernel.h> 15 + #include <linux/types.h> 16 + 17 + /* driver core IFs */ 18 + 19 + struct viortc_dev; 20 + 21 + int viortc_read(struct viortc_dev *viortc, u16 vio_clk_id, u64 *reading); 22 + int viortc_read_cross(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter, 23 + u64 *reading, u64 *cycles); 24 + int viortc_cross_cap(struct viortc_dev *viortc, u16 vio_clk_id, u8 hw_counter, 25 + bool *supported); 26 + int viortc_read_alarm(struct viortc_dev *viortc, u16 vio_clk_id, 27 + u64 *alarm_time, bool *enabled); 28 + int viortc_set_alarm(struct viortc_dev *viortc, u16 vio_clk_id, u64 alarm_time, 29 + bool alarm_enable); 30 + int viortc_set_alarm_enabled(struct viortc_dev *viortc, u16 vio_clk_id, 31 + bool alarm_enable); 32 + 33 + struct viortc_class; 34 + 35 + struct viortc_class *viortc_class_from_dev(struct device *dev); 36 + 37 + /* PTP IFs */ 38 + 39 + struct viortc_ptp_clock; 40 + 41 + #if IS_ENABLED(CONFIG_VIRTIO_RTC_PTP) 42 + 43 + struct viortc_ptp_clock *viortc_ptp_register(struct viortc_dev *viortc, 44 + struct device *parent_dev, 45 + u16 vio_clk_id, 46 + const char *ptp_clock_name); 47 + int viortc_ptp_unregister(struct viortc_ptp_clock *vio_ptp, 48 + struct device *parent_dev); 49 + 50 + #else 51 + 52 + static inline struct viortc_ptp_clock * 53 + viortc_ptp_register(struct viortc_dev *viortc, struct device *parent_dev, 54 + u16 vio_clk_id, const char *ptp_clock_name) 55 + { 56 + return NULL; 57 + } 58 + 59 + static inline int viortc_ptp_unregister(struct viortc_ptp_clock *vio_ptp, 60 + struct device *parent_dev) 61 + { 62 + return -ENODEV; 63 + } 64 + 65 + #endif 66 + 67 + /* HW counter IFs */ 68 + 69 + /** 70 + * viortc_hw_xtstamp_params() - get HW-specific xtstamp params 71 + * @hw_counter: virtio_rtc HW counter type 72 + * @cs_id: clocksource id corresponding to hw_counter 73 + * 74 + * Gets the HW-specific xtstamp params. Returns an error if the driver cannot 75 + * support xtstamp. 76 + * 77 + * Context: Process context. 78 + * Return: Zero on success, negative error code otherwise. 79 + */ 80 + int viortc_hw_xtstamp_params(u8 *hw_counter, enum clocksource_ids *cs_id); 81 + 82 + /* RTC class IFs */ 83 + 84 + #if IS_ENABLED(CONFIG_VIRTIO_RTC_CLASS) 85 + 86 + void viortc_class_alarm(struct viortc_class *viortc_class, u16 vio_clk_id); 87 + 88 + void viortc_class_stop(struct viortc_class *viortc_class); 89 + 90 + int viortc_class_register(struct viortc_class *viortc_class); 91 + 92 + struct viortc_class *viortc_class_init(struct viortc_dev *viortc, 93 + u16 vio_clk_id, bool have_alarm, 94 + struct device *parent_dev); 95 + 96 + #else /* CONFIG_VIRTIO_RTC_CLASS */ 97 + 98 + static inline void viortc_class_alarm(struct viortc_class *viortc_class, 99 + u16 vio_clk_id) 100 + { 101 + } 102 + 103 + static inline void viortc_class_stop(struct viortc_class *viortc_class) 104 + { 105 + } 106 + 107 + static inline int viortc_class_register(struct viortc_class *viortc_class) 108 + { 109 + return -ENODEV; 110 + } 111 + 112 + static inline struct viortc_class *viortc_class_init(struct viortc_dev *viortc, 113 + u16 vio_clk_id, 114 + bool have_alarm, 115 + struct device *parent_dev) 116 + { 117 + return ERR_PTR(-ENODEV); 118 + } 119 + 120 + #endif /* CONFIG_VIRTIO_RTC_CLASS */ 121 + 122 + #endif /* _VIRTIO_RTC_INTERNAL_H_ */
+347
drivers/virtio/virtio_rtc_ptp.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Expose virtio_rtc clocks as PTP clocks. 4 + * 5 + * Copyright (C) 2022-2023 OpenSynergy GmbH 6 + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. 7 + * 8 + * Derived from ptp_kvm_common.c, virtual PTP 1588 clock for use with KVM 9 + * guests. 10 + * 11 + * Copyright (C) 2017 Red Hat Inc. 12 + */ 13 + 14 + #include <linux/device.h> 15 + #include <linux/err.h> 16 + #include <linux/ptp_clock_kernel.h> 17 + 18 + #include <uapi/linux/virtio_rtc.h> 19 + 20 + #include "virtio_rtc_internal.h" 21 + 22 + /** 23 + * struct viortc_ptp_clock - PTP clock abstraction 24 + * @ptp_clock: PTP clock handle for unregistering 25 + * @viortc: virtio_rtc device data 26 + * @ptp_info: PTP clock description 27 + * @vio_clk_id: virtio_rtc clock id 28 + * @have_cross: device supports crosststamp with available HW counter 29 + */ 30 + struct viortc_ptp_clock { 31 + struct ptp_clock *ptp_clock; 32 + struct viortc_dev *viortc; 33 + struct ptp_clock_info ptp_info; 34 + u16 vio_clk_id; 35 + bool have_cross; 36 + }; 37 + 38 + /** 39 + * struct viortc_ptp_cross_ctx - context for get_device_system_crosststamp() 40 + * @device_time: device clock reading 41 + * @system_counterval: HW counter value at device_time 42 + * 43 + * Provides the already obtained crosststamp to get_device_system_crosststamp(). 44 + */ 45 + struct viortc_ptp_cross_ctx { 46 + ktime_t device_time; 47 + struct system_counterval_t system_counterval; 48 + }; 49 + 50 + /* Weak function in case get_device_system_crosststamp() is not supported */ 51 + int __weak viortc_hw_xtstamp_params(u8 *hw_counter, enum clocksource_ids *cs_id) 52 + { 53 + return -EOPNOTSUPP; 54 + } 55 + 56 + /** 57 + * viortc_ptp_get_time_fn() - callback for get_device_system_crosststamp() 58 + * @device_time: device clock reading 59 + * @system_counterval: HW counter value at device_time 60 + * @ctx: context with already obtained crosststamp 61 + * 62 + * Return: zero (success). 63 + */ 64 + static int viortc_ptp_get_time_fn(ktime_t *device_time, 65 + struct system_counterval_t *system_counterval, 66 + void *ctx) 67 + { 68 + struct viortc_ptp_cross_ctx *vio_ctx = ctx; 69 + 70 + *device_time = vio_ctx->device_time; 71 + *system_counterval = vio_ctx->system_counterval; 72 + 73 + return 0; 74 + } 75 + 76 + /** 77 + * viortc_ptp_do_xtstamp() - get crosststamp from device 78 + * @vio_ptp: virtio_rtc PTP clock 79 + * @hw_counter: virtio_rtc HW counter type 80 + * @cs_id: clocksource id corresponding to hw_counter 81 + * @ctx: context for get_device_system_crosststamp() 82 + * 83 + * Reads HW-specific crosststamp from device. 84 + * 85 + * Context: Process context. 86 + * Return: Zero on success, negative error code otherwise. 87 + */ 88 + static int viortc_ptp_do_xtstamp(struct viortc_ptp_clock *vio_ptp, 89 + u8 hw_counter, enum clocksource_ids cs_id, 90 + struct viortc_ptp_cross_ctx *ctx) 91 + { 92 + u64 max_ns, ns; 93 + int ret; 94 + 95 + ctx->system_counterval.cs_id = cs_id; 96 + 97 + ret = viortc_read_cross(vio_ptp->viortc, vio_ptp->vio_clk_id, 98 + hw_counter, &ns, 99 + &ctx->system_counterval.cycles); 100 + if (ret) 101 + return ret; 102 + 103 + max_ns = (u64)ktime_to_ns(KTIME_MAX); 104 + if (ns > max_ns) 105 + return -EINVAL; 106 + 107 + ctx->device_time = ns_to_ktime(ns); 108 + 109 + return 0; 110 + } 111 + 112 + /* 113 + * PTP clock operations 114 + */ 115 + 116 + /** 117 + * viortc_ptp_getcrosststamp() - PTP clock getcrosststamp op 118 + * @ptp: PTP clock info 119 + * @xtstamp: crosststamp 120 + * 121 + * Context: Process context. 122 + * Return: Zero on success, negative error code otherwise. 123 + */ 124 + static int viortc_ptp_getcrosststamp(struct ptp_clock_info *ptp, 125 + struct system_device_crosststamp *xtstamp) 126 + { 127 + struct viortc_ptp_clock *vio_ptp = 128 + container_of(ptp, struct viortc_ptp_clock, ptp_info); 129 + struct system_time_snapshot history_begin; 130 + struct viortc_ptp_cross_ctx ctx; 131 + enum clocksource_ids cs_id; 132 + u8 hw_counter; 133 + int ret; 134 + 135 + if (!vio_ptp->have_cross) 136 + return -EOPNOTSUPP; 137 + 138 + ret = viortc_hw_xtstamp_params(&hw_counter, &cs_id); 139 + if (ret) 140 + return ret; 141 + 142 + ktime_get_snapshot(&history_begin); 143 + if (history_begin.cs_id != cs_id) 144 + return -EOPNOTSUPP; 145 + 146 + /* 147 + * Getting the timestamp can take many milliseconds with a slow Virtio 148 + * device. This is too long for viortc_ptp_get_time_fn() passed to 149 + * get_device_system_crosststamp(), which has to usually return before 150 + * the timekeeper seqcount increases (every tick or so). 151 + * 152 + * So, get the actual cross-timestamp first. 153 + */ 154 + ret = viortc_ptp_do_xtstamp(vio_ptp, hw_counter, cs_id, &ctx); 155 + if (ret) 156 + return ret; 157 + 158 + ret = get_device_system_crosststamp(viortc_ptp_get_time_fn, &ctx, 159 + &history_begin, xtstamp); 160 + if (ret) 161 + pr_debug("%s: get_device_system_crosststamp() returned %d\n", 162 + __func__, ret); 163 + 164 + return ret; 165 + } 166 + 167 + /* viortc_ptp_adjfine() - unsupported PTP clock adjfine op */ 168 + static int viortc_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 169 + { 170 + return -EOPNOTSUPP; 171 + } 172 + 173 + /* viortc_ptp_adjtime() - unsupported PTP clock adjtime op */ 174 + static int viortc_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 175 + { 176 + return -EOPNOTSUPP; 177 + } 178 + 179 + /* viortc_ptp_settime64() - unsupported PTP clock settime64 op */ 180 + static int viortc_ptp_settime64(struct ptp_clock_info *ptp, 181 + const struct timespec64 *ts) 182 + { 183 + return -EOPNOTSUPP; 184 + } 185 + 186 + /* 187 + * viortc_ptp_gettimex64() - PTP clock gettimex64 op 188 + * 189 + * Context: Process context. 190 + */ 191 + static int viortc_ptp_gettimex64(struct ptp_clock_info *ptp, 192 + struct timespec64 *ts, 193 + struct ptp_system_timestamp *sts) 194 + { 195 + struct viortc_ptp_clock *vio_ptp = 196 + container_of(ptp, struct viortc_ptp_clock, ptp_info); 197 + int ret; 198 + u64 ns; 199 + 200 + ptp_read_system_prets(sts); 201 + ret = viortc_read(vio_ptp->viortc, vio_ptp->vio_clk_id, &ns); 202 + ptp_read_system_postts(sts); 203 + 204 + if (ret) 205 + return ret; 206 + 207 + if (ns > (u64)S64_MAX) 208 + return -EINVAL; 209 + 210 + *ts = ns_to_timespec64((s64)ns); 211 + 212 + return 0; 213 + } 214 + 215 + /* viortc_ptp_enable() - unsupported PTP clock enable op */ 216 + static int viortc_ptp_enable(struct ptp_clock_info *ptp, 217 + struct ptp_clock_request *rq, int on) 218 + { 219 + return -EOPNOTSUPP; 220 + } 221 + 222 + /* 223 + * viortc_ptp_info_template - ptp_clock_info template 224 + * 225 + * The .name member will be set for individual virtio_rtc PTP clocks. 226 + * 227 + * The .getcrosststamp member will be cleared for PTP clocks not supporting 228 + * crosststamp. 229 + */ 230 + static const struct ptp_clock_info viortc_ptp_info_template = { 231 + .owner = THIS_MODULE, 232 + /* .name is set according to clock type */ 233 + .adjfine = viortc_ptp_adjfine, 234 + .adjtime = viortc_ptp_adjtime, 235 + .gettimex64 = viortc_ptp_gettimex64, 236 + .settime64 = viortc_ptp_settime64, 237 + .enable = viortc_ptp_enable, 238 + .getcrosststamp = viortc_ptp_getcrosststamp, 239 + }; 240 + 241 + /** 242 + * viortc_ptp_unregister() - PTP clock unregistering wrapper 243 + * @vio_ptp: virtio_rtc PTP clock 244 + * @parent_dev: parent device of PTP clock 245 + * 246 + * Return: Zero on success, negative error code otherwise. 247 + */ 248 + int viortc_ptp_unregister(struct viortc_ptp_clock *vio_ptp, 249 + struct device *parent_dev) 250 + { 251 + int ret = ptp_clock_unregister(vio_ptp->ptp_clock); 252 + 253 + if (!ret) 254 + devm_kfree(parent_dev, vio_ptp); 255 + 256 + return ret; 257 + } 258 + 259 + /** 260 + * viortc_ptp_get_cross_cap() - get xtstamp support info from device 261 + * @viortc: virtio_rtc device data 262 + * @vio_ptp: virtio_rtc PTP clock abstraction 263 + * 264 + * Context: Process context. 265 + * Return: Zero on success, negative error code otherwise. 266 + */ 267 + static int viortc_ptp_get_cross_cap(struct viortc_dev *viortc, 268 + struct viortc_ptp_clock *vio_ptp) 269 + { 270 + enum clocksource_ids cs_id; 271 + bool xtstamp_supported; 272 + u8 hw_counter; 273 + int ret; 274 + 275 + ret = viortc_hw_xtstamp_params(&hw_counter, &cs_id); 276 + if (ret) { 277 + vio_ptp->have_cross = false; 278 + return 0; 279 + } 280 + 281 + ret = viortc_cross_cap(viortc, vio_ptp->vio_clk_id, hw_counter, 282 + &xtstamp_supported); 283 + if (ret) 284 + return ret; 285 + 286 + vio_ptp->have_cross = xtstamp_supported; 287 + 288 + return 0; 289 + } 290 + 291 + /** 292 + * viortc_ptp_register() - prepare and register PTP clock 293 + * @viortc: virtio_rtc device data 294 + * @parent_dev: parent device for PTP clock 295 + * @vio_clk_id: id of virtio_rtc clock which backs PTP clock 296 + * @ptp_clock_name: PTP clock name 297 + * 298 + * Context: Process context. 299 + * Return: Pointer on success, ERR_PTR() otherwise; NULL if PTP clock support 300 + * not available. 301 + */ 302 + struct viortc_ptp_clock *viortc_ptp_register(struct viortc_dev *viortc, 303 + struct device *parent_dev, 304 + u16 vio_clk_id, 305 + const char *ptp_clock_name) 306 + { 307 + struct viortc_ptp_clock *vio_ptp; 308 + struct ptp_clock *ptp_clock; 309 + ssize_t len; 310 + int ret; 311 + 312 + vio_ptp = devm_kzalloc(parent_dev, sizeof(*vio_ptp), GFP_KERNEL); 313 + if (!vio_ptp) 314 + return ERR_PTR(-ENOMEM); 315 + 316 + vio_ptp->viortc = viortc; 317 + vio_ptp->vio_clk_id = vio_clk_id; 318 + vio_ptp->ptp_info = viortc_ptp_info_template; 319 + len = strscpy(vio_ptp->ptp_info.name, ptp_clock_name, 320 + sizeof(vio_ptp->ptp_info.name)); 321 + if (len < 0) { 322 + ret = len; 323 + goto err_free_dev; 324 + } 325 + 326 + ret = viortc_ptp_get_cross_cap(viortc, vio_ptp); 327 + if (ret) 328 + goto err_free_dev; 329 + 330 + if (!vio_ptp->have_cross) 331 + vio_ptp->ptp_info.getcrosststamp = NULL; 332 + 333 + ptp_clock = ptp_clock_register(&vio_ptp->ptp_info, parent_dev); 334 + if (IS_ERR(ptp_clock)) 335 + goto err_on_register; 336 + 337 + vio_ptp->ptp_clock = ptp_clock; 338 + 339 + return vio_ptp; 340 + 341 + err_on_register: 342 + ret = PTR_ERR(ptp_clock); 343 + 344 + err_free_dev: 345 + devm_kfree(parent_dev, vio_ptp); 346 + return ERR_PTR(ret); 347 + }
+2
include/linux/virtio_config.h
··· 329 329 bool virtio_get_shm_region(struct virtio_device *vdev, 330 330 struct virtio_shm_region *region, u8 id) 331 331 { 332 + if (!region->len) 333 + return false; 332 334 if (!vdev->config->get_shm_region) 333 335 return false; 334 336 return vdev->config->get_shm_region(vdev, region, id);
+237
include/uapi/linux/virtio_rtc.h
··· 1 + /* SPDX-License-Identifier: ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) */ 2 + /* 3 + * Copyright (C) 2022-2024 OpenSynergy GmbH 4 + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. 5 + */ 6 + 7 + #ifndef _LINUX_VIRTIO_RTC_H 8 + #define _LINUX_VIRTIO_RTC_H 9 + 10 + #include <linux/types.h> 11 + 12 + /* alarm feature */ 13 + #define VIRTIO_RTC_F_ALARM 0 14 + 15 + /* read request message types */ 16 + 17 + #define VIRTIO_RTC_REQ_READ 0x0001 18 + #define VIRTIO_RTC_REQ_READ_CROSS 0x0002 19 + 20 + /* control request message types */ 21 + 22 + #define VIRTIO_RTC_REQ_CFG 0x1000 23 + #define VIRTIO_RTC_REQ_CLOCK_CAP 0x1001 24 + #define VIRTIO_RTC_REQ_CROSS_CAP 0x1002 25 + #define VIRTIO_RTC_REQ_READ_ALARM 0x1003 26 + #define VIRTIO_RTC_REQ_SET_ALARM 0x1004 27 + #define VIRTIO_RTC_REQ_SET_ALARM_ENABLED 0x1005 28 + 29 + /* alarmq message types */ 30 + 31 + #define VIRTIO_RTC_NOTIF_ALARM 0x2000 32 + 33 + /* Message headers */ 34 + 35 + /** common request header */ 36 + struct virtio_rtc_req_head { 37 + __le16 msg_type; 38 + __u8 reserved[6]; 39 + }; 40 + 41 + /** common response header */ 42 + struct virtio_rtc_resp_head { 43 + #define VIRTIO_RTC_S_OK 0 44 + #define VIRTIO_RTC_S_EOPNOTSUPP 2 45 + #define VIRTIO_RTC_S_ENODEV 3 46 + #define VIRTIO_RTC_S_EINVAL 4 47 + #define VIRTIO_RTC_S_EIO 5 48 + __u8 status; 49 + __u8 reserved[7]; 50 + }; 51 + 52 + /** common notification header */ 53 + struct virtio_rtc_notif_head { 54 + __le16 msg_type; 55 + __u8 reserved[6]; 56 + }; 57 + 58 + /* read requests */ 59 + 60 + /* VIRTIO_RTC_REQ_READ message */ 61 + 62 + struct virtio_rtc_req_read { 63 + struct virtio_rtc_req_head head; 64 + __le16 clock_id; 65 + __u8 reserved[6]; 66 + }; 67 + 68 + struct virtio_rtc_resp_read { 69 + struct virtio_rtc_resp_head head; 70 + __le64 clock_reading; 71 + }; 72 + 73 + /* VIRTIO_RTC_REQ_READ_CROSS message */ 74 + 75 + struct virtio_rtc_req_read_cross { 76 + struct virtio_rtc_req_head head; 77 + __le16 clock_id; 78 + /* Arm Generic Timer Counter-timer Virtual Count Register (CNTVCT_EL0) */ 79 + #define VIRTIO_RTC_COUNTER_ARM_VCT 0 80 + /* x86 Time-Stamp Counter */ 81 + #define VIRTIO_RTC_COUNTER_X86_TSC 1 82 + /* Invalid */ 83 + #define VIRTIO_RTC_COUNTER_INVALID 0xFF 84 + __u8 hw_counter; 85 + __u8 reserved[5]; 86 + }; 87 + 88 + struct virtio_rtc_resp_read_cross { 89 + struct virtio_rtc_resp_head head; 90 + __le64 clock_reading; 91 + __le64 counter_cycles; 92 + }; 93 + 94 + /* control requests */ 95 + 96 + /* VIRTIO_RTC_REQ_CFG message */ 97 + 98 + struct virtio_rtc_req_cfg { 99 + struct virtio_rtc_req_head head; 100 + /* no request params */ 101 + }; 102 + 103 + struct virtio_rtc_resp_cfg { 104 + struct virtio_rtc_resp_head head; 105 + /** # of clocks -> clock ids < num_clocks are valid */ 106 + __le16 num_clocks; 107 + __u8 reserved[6]; 108 + }; 109 + 110 + /* VIRTIO_RTC_REQ_CLOCK_CAP message */ 111 + 112 + struct virtio_rtc_req_clock_cap { 113 + struct virtio_rtc_req_head head; 114 + __le16 clock_id; 115 + __u8 reserved[6]; 116 + }; 117 + 118 + struct virtio_rtc_resp_clock_cap { 119 + struct virtio_rtc_resp_head head; 120 + #define VIRTIO_RTC_CLOCK_UTC 0 121 + #define VIRTIO_RTC_CLOCK_TAI 1 122 + #define VIRTIO_RTC_CLOCK_MONOTONIC 2 123 + #define VIRTIO_RTC_CLOCK_UTC_SMEARED 3 124 + #define VIRTIO_RTC_CLOCK_UTC_MAYBE_SMEARED 4 125 + __u8 type; 126 + #define VIRTIO_RTC_SMEAR_UNSPECIFIED 0 127 + #define VIRTIO_RTC_SMEAR_NOON_LINEAR 1 128 + #define VIRTIO_RTC_SMEAR_UTC_SLS 2 129 + __u8 leap_second_smearing; 130 + #define VIRTIO_RTC_FLAG_ALARM_CAP (1 << 0) 131 + __u8 flags; 132 + __u8 reserved[5]; 133 + }; 134 + 135 + /* VIRTIO_RTC_REQ_CROSS_CAP message */ 136 + 137 + struct virtio_rtc_req_cross_cap { 138 + struct virtio_rtc_req_head head; 139 + __le16 clock_id; 140 + __u8 hw_counter; 141 + __u8 reserved[5]; 142 + }; 143 + 144 + struct virtio_rtc_resp_cross_cap { 145 + struct virtio_rtc_resp_head head; 146 + #define VIRTIO_RTC_FLAG_CROSS_CAP (1 << 0) 147 + __u8 flags; 148 + __u8 reserved[7]; 149 + }; 150 + 151 + /* VIRTIO_RTC_REQ_READ_ALARM message */ 152 + 153 + struct virtio_rtc_req_read_alarm { 154 + struct virtio_rtc_req_head head; 155 + __le16 clock_id; 156 + __u8 reserved[6]; 157 + }; 158 + 159 + struct virtio_rtc_resp_read_alarm { 160 + struct virtio_rtc_resp_head head; 161 + __le64 alarm_time; 162 + #define VIRTIO_RTC_FLAG_ALARM_ENABLED (1 << 0) 163 + __u8 flags; 164 + __u8 reserved[7]; 165 + }; 166 + 167 + /* VIRTIO_RTC_REQ_SET_ALARM message */ 168 + 169 + struct virtio_rtc_req_set_alarm { 170 + struct virtio_rtc_req_head head; 171 + __le64 alarm_time; 172 + __le16 clock_id; 173 + /* flag VIRTIO_RTC_FLAG_ALARM_ENABLED */ 174 + __u8 flags; 175 + __u8 reserved[5]; 176 + }; 177 + 178 + struct virtio_rtc_resp_set_alarm { 179 + struct virtio_rtc_resp_head head; 180 + /* no response params */ 181 + }; 182 + 183 + /* VIRTIO_RTC_REQ_SET_ALARM_ENABLED message */ 184 + 185 + struct virtio_rtc_req_set_alarm_enabled { 186 + struct virtio_rtc_req_head head; 187 + __le16 clock_id; 188 + /* flag VIRTIO_RTC_ALARM_ENABLED */ 189 + __u8 flags; 190 + __u8 reserved[5]; 191 + }; 192 + 193 + struct virtio_rtc_resp_set_alarm_enabled { 194 + struct virtio_rtc_resp_head head; 195 + /* no response params */ 196 + }; 197 + 198 + /** Union of request types for requestq */ 199 + union virtio_rtc_req_requestq { 200 + struct virtio_rtc_req_read read; 201 + struct virtio_rtc_req_read_cross read_cross; 202 + struct virtio_rtc_req_cfg cfg; 203 + struct virtio_rtc_req_clock_cap clock_cap; 204 + struct virtio_rtc_req_cross_cap cross_cap; 205 + struct virtio_rtc_req_read_alarm read_alarm; 206 + struct virtio_rtc_req_set_alarm set_alarm; 207 + struct virtio_rtc_req_set_alarm_enabled set_alarm_enabled; 208 + }; 209 + 210 + /** Union of response types for requestq */ 211 + union virtio_rtc_resp_requestq { 212 + struct virtio_rtc_resp_read read; 213 + struct virtio_rtc_resp_read_cross read_cross; 214 + struct virtio_rtc_resp_cfg cfg; 215 + struct virtio_rtc_resp_clock_cap clock_cap; 216 + struct virtio_rtc_resp_cross_cap cross_cap; 217 + struct virtio_rtc_resp_read_alarm read_alarm; 218 + struct virtio_rtc_resp_set_alarm set_alarm; 219 + struct virtio_rtc_resp_set_alarm_enabled set_alarm_enabled; 220 + }; 221 + 222 + /* alarmq notifications */ 223 + 224 + /* VIRTIO_RTC_NOTIF_ALARM notification */ 225 + 226 + struct virtio_rtc_notif_alarm { 227 + struct virtio_rtc_notif_head head; 228 + __le16 clock_id; 229 + __u8 reserved[6]; 230 + }; 231 + 232 + /** Union of notification types for alarmq */ 233 + union virtio_rtc_notif_alarmq { 234 + struct virtio_rtc_notif_alarm alarm; 235 + }; 236 + 237 + #endif /* _LINUX_VIRTIO_RTC_H */