]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/pci/pcie/ptm.c
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_disable_ptm(struct pci_dev
*dev
)
37 if (!pci_is_pcie(dev
))
40 ptm
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_PTM
);
44 pci_read_config_word(dev
, ptm
+ PCI_PTM_CTRL
, &ctrl
);
45 ctrl
&= ~(PCI_PTM_CTRL_ENABLE
| PCI_PTM_CTRL_ROOT
);
46 pci_write_config_word(dev
, ptm
+ PCI_PTM_CTRL
, ctrl
);
49 void pci_save_ptm_state(struct pci_dev
*dev
)
52 struct pci_cap_saved_state
*save_state
;
55 if (!pci_is_pcie(dev
))
58 ptm
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_PTM
);
62 save_state
= pci_find_saved_ext_cap(dev
, PCI_EXT_CAP_ID_PTM
);
66 cap
= (u16
*)&save_state
->cap
.data
[0];
67 pci_read_config_word(dev
, ptm
+ PCI_PTM_CTRL
, cap
);
70 void pci_restore_ptm_state(struct pci_dev
*dev
)
72 struct pci_cap_saved_state
*save_state
;
76 if (!pci_is_pcie(dev
))
79 save_state
= pci_find_saved_ext_cap(dev
, PCI_EXT_CAP_ID_PTM
);
80 ptm
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_PTM
);
81 if (!save_state
|| !ptm
)
84 cap
= (u16
*)&save_state
->cap
.data
[0];
85 pci_write_config_word(dev
, ptm
+ PCI_PTM_CTRL
, *cap
);
88 void pci_ptm_init(struct pci_dev
*dev
)
95 if (!pci_is_pcie(dev
))
99 * Enable PTM only on interior devices (root ports, switch ports,
100 * etc.) on the assumption that it causes no link traffic until an
101 * endpoint enables it.
103 if ((pci_pcie_type(dev
) == PCI_EXP_TYPE_ENDPOINT
||
104 pci_pcie_type(dev
) == PCI_EXP_TYPE_RC_END
))
108 * Switch Downstream Ports are not permitted to have a PTM
109 * capability; their PTM behavior is controlled by the Upstream
110 * Port (PCIe r5.0, sec 7.9.16).
112 ups
= pci_upstream_bridge(dev
);
113 if (pci_pcie_type(dev
) == PCI_EXP_TYPE_DOWNSTREAM
&&
114 ups
&& ups
->ptm_enabled
) {
115 dev
->ptm_granularity
= ups
->ptm_granularity
;
116 dev
->ptm_enabled
= 1;
120 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_PTM
);
124 pci_add_ext_cap_save_buffer(dev
, PCI_EXT_CAP_ID_PTM
, sizeof(u16
));
126 pci_read_config_dword(dev
, pos
+ PCI_PTM_CAP
, &cap
);
127 local_clock
= (cap
& PCI_PTM_GRANULARITY_MASK
) >> 8;
130 * There's no point in enabling PTM unless it's enabled in the
131 * upstream device or this device can be a PTM Root itself. Per
132 * the spec recommendation (PCIe r3.1, sec 7.32.3), select the
133 * furthest upstream Time Source as the PTM Root.
135 if (ups
&& ups
->ptm_enabled
) {
136 ctrl
= PCI_PTM_CTRL_ENABLE
;
137 if (ups
->ptm_granularity
== 0)
138 dev
->ptm_granularity
= 0;
139 else if (ups
->ptm_granularity
> local_clock
)
140 dev
->ptm_granularity
= ups
->ptm_granularity
;
142 if (cap
& PCI_PTM_CAP_ROOT
) {
143 ctrl
= PCI_PTM_CTRL_ENABLE
| PCI_PTM_CTRL_ROOT
;
145 dev
->ptm_granularity
= local_clock
;
150 ctrl
|= dev
->ptm_granularity
<< 8;
151 pci_write_config_dword(dev
, pos
+ PCI_PTM_CTRL
, ctrl
);
152 dev
->ptm_enabled
= 1;
157 int pci_enable_ptm(struct pci_dev
*dev
, u8
*granularity
)
163 if (!pci_is_pcie(dev
))
166 pos
= pci_find_ext_capability(dev
, PCI_EXT_CAP_ID_PTM
);
170 pci_read_config_dword(dev
, pos
+ PCI_PTM_CAP
, &cap
);
171 if (!(cap
& PCI_PTM_CAP_REQ
))
175 * For a PCIe Endpoint, PTM is only useful if the endpoint can
176 * issue PTM requests to upstream devices that have PTM enabled.
178 * For Root Complex Integrated Endpoints, there is no upstream
179 * device, so there must be some implementation-specific way to
180 * associate the endpoint with a time source.
182 if (pci_pcie_type(dev
) == PCI_EXP_TYPE_ENDPOINT
) {
183 ups
= pci_upstream_bridge(dev
);
184 if (!ups
|| !ups
->ptm_enabled
)
187 dev
->ptm_granularity
= ups
->ptm_granularity
;
188 } else if (pci_pcie_type(dev
) == PCI_EXP_TYPE_RC_END
) {
189 dev
->ptm_granularity
= 0;
193 ctrl
= PCI_PTM_CTRL_ENABLE
;
194 ctrl
|= dev
->ptm_granularity
<< 8;
195 pci_write_config_dword(dev
, pos
+ PCI_PTM_CTRL
, ctrl
);
196 dev
->ptm_enabled
= 1;
201 *granularity
= dev
->ptm_granularity
;
204 EXPORT_SYMBOL(pci_enable_ptm
);
206 bool pcie_ptm_enabled(struct pci_dev
*dev
)
211 return dev
->ptm_enabled
;
213 EXPORT_SYMBOL(pcie_ptm_enabled
);