1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Express Precision Time Measurement
4 * Copyright (c) 2016, Intel Corporation.
7 #include <linux/module.h>
8 #include <linux/init.h>
12 static void pci_ptm_info(struct pci_dev *dev)
16 switch (dev->ptm_granularity) {
18 snprintf(clock_desc, sizeof(clock_desc), "unknown");
21 snprintf(clock_desc, sizeof(clock_desc), ">254ns");
24 snprintf(clock_desc, sizeof(clock_desc), "%uns",
25 dev->ptm_granularity);
28 pci_info(dev, "PTM enabled%s, %s granularity\n",
29 dev->ptm_root ? " (root)" : "", clock_desc);
32 void pci_ptm_init(struct pci_dev *dev)
39 if (!pci_is_pcie(dev))
42 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_PTM);
47 * Enable PTM only on interior devices (root ports, switch ports,
48 * etc.) on the assumption that it causes no link traffic until an
49 * endpoint enables it.
51 if ((pci_pcie_type(dev) == PCI_EXP_TYPE_ENDPOINT ||
52 pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END))
55 pci_read_config_dword(dev, pos + PCI_PTM_CAP, &cap);
56 local_clock = (cap & PCI_PTM_GRANULARITY_MASK) >> 8;
59 * There's no point in enabling PTM unless it's enabled in the
60 * upstream device or this device can be a PTM Root itself. Per
61 * the spec recommendation (PCIe r3.1, sec 7.32.3), select the
62 * furthest upstream Time Source as the PTM Root.
64 ups = pci_upstream_bridge(dev);
65 if (ups && ups->ptm_enabled) {
66 ctrl = PCI_PTM_CTRL_ENABLE;
67 if (ups->ptm_granularity == 0)
68 dev->ptm_granularity = 0;
69 else if (ups->ptm_granularity > local_clock)
70 dev->ptm_granularity = ups->ptm_granularity;
72 if (cap & PCI_PTM_CAP_ROOT) {
73 ctrl = PCI_PTM_CTRL_ENABLE | PCI_PTM_CTRL_ROOT;
75 dev->ptm_granularity = local_clock;
80 ctrl |= dev->ptm_granularity << 8;
81 pci_write_config_dword(dev, pos + PCI_PTM_CTRL, ctrl);
87 int pci_enable_ptm(struct pci_dev *dev, u8 *granularity)
93 if (!pci_is_pcie(dev))
96 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_PTM);
100 pci_read_config_dword(dev, pos + PCI_PTM_CAP, &cap);
101 if (!(cap & PCI_PTM_CAP_REQ))
105 * For a PCIe Endpoint, PTM is only useful if the endpoint can
106 * issue PTM requests to upstream devices that have PTM enabled.
108 * For Root Complex Integrated Endpoints, there is no upstream
109 * device, so there must be some implementation-specific way to
110 * associate the endpoint with a time source.
112 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ENDPOINT) {
113 ups = pci_upstream_bridge(dev);
114 if (!ups || !ups->ptm_enabled)
117 dev->ptm_granularity = ups->ptm_granularity;
118 } else if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) {
119 dev->ptm_granularity = 0;
123 ctrl = PCI_PTM_CTRL_ENABLE;
124 ctrl |= dev->ptm_granularity << 8;
125 pci_write_config_dword(dev, pos + PCI_PTM_CTRL, ctrl);
126 dev->ptm_enabled = 1;
131 *granularity = dev->ptm_granularity;
134 EXPORT_SYMBOL(pci_enable_ptm);