Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * PCI Express Precision Time Measurement
4 * Copyright (c) 2016, Intel Corporation.
5 */
6
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/pci.h>
10#include "../pci.h"
11
12static void pci_ptm_info(struct pci_dev *dev)
13{
14 char clock_desc[8];
15
16 switch (dev->ptm_granularity) {
17 case 0:
18 snprintf(clock_desc, sizeof(clock_desc), "unknown");
19 break;
20 case 255:
21 snprintf(clock_desc, sizeof(clock_desc), ">254ns");
22 break;
23 default:
24 snprintf(clock_desc, sizeof(clock_desc), "%udns",
25 dev->ptm_granularity);
26 break;
27 }
28 pci_info(dev, "PTM enabled%s, %s granularity\n",
29 dev->ptm_root ? " (root)" : "", clock_desc);
30}
31
32void pci_ptm_init(struct pci_dev *dev)
33{
34 int pos;
35 u32 cap, ctrl;
36 u8 local_clock;
37 struct pci_dev *ups;
38
39 if (!pci_is_pcie(dev))
40 return;
41
42 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_PTM);
43 if (!pos)
44 return;
45
46 /*
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.
50 */
51 if ((pci_pcie_type(dev) == PCI_EXP_TYPE_ENDPOINT ||
52 pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END))
53 return;
54
55 pci_read_config_dword(dev, pos + PCI_PTM_CAP, &cap);
56 local_clock = (cap & PCI_PTM_GRANULARITY_MASK) >> 8;
57
58 /*
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.
63 */
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;
71 } else {
72 if (cap & PCI_PTM_CAP_ROOT) {
73 ctrl = PCI_PTM_CTRL_ENABLE | PCI_PTM_CTRL_ROOT;
74 dev->ptm_root = 1;
75 dev->ptm_granularity = local_clock;
76 } else
77 return;
78 }
79
80 ctrl |= dev->ptm_granularity << 8;
81 pci_write_config_dword(dev, pos + PCI_PTM_CTRL, ctrl);
82 dev->ptm_enabled = 1;
83
84 pci_ptm_info(dev);
85}
86
87int pci_enable_ptm(struct pci_dev *dev, u8 *granularity)
88{
89 int pos;
90 u32 cap, ctrl;
91 struct pci_dev *ups;
92
93 if (!pci_is_pcie(dev))
94 return -EINVAL;
95
96 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_PTM);
97 if (!pos)
98 return -EINVAL;
99
100 pci_read_config_dword(dev, pos + PCI_PTM_CAP, &cap);
101 if (!(cap & PCI_PTM_CAP_REQ))
102 return -EINVAL;
103
104 /*
105 * For a PCIe Endpoint, PTM is only useful if the endpoint can
106 * issue PTM requests to upstream devices that have PTM enabled.
107 *
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.
111 */
112 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ENDPOINT) {
113 ups = pci_upstream_bridge(dev);
114 if (!ups || !ups->ptm_enabled)
115 return -EINVAL;
116
117 dev->ptm_granularity = ups->ptm_granularity;
118 } else if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) {
119 dev->ptm_granularity = 0;
120 } else
121 return -EINVAL;
122
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;
127
128 pci_ptm_info(dev);
129
130 if (granularity)
131 *granularity = dev->ptm_granularity;
132 return 0;
133}
134EXPORT_SYMBOL(pci_enable_ptm);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * PCI Express Precision Time Measurement
4 * Copyright (c) 2016, Intel Corporation.
5 */
6
7#include <linux/bitfield.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/pci.h>
11#include "../pci.h"
12
13/*
14 * If the next upstream device supports PTM, return it; otherwise return
15 * NULL. PTM Messages are local, so both link partners must support it.
16 */
17static struct pci_dev *pci_upstream_ptm(struct pci_dev *dev)
18{
19 struct pci_dev *ups = pci_upstream_bridge(dev);
20
21 /*
22 * Switch Downstream Ports are not permitted to have a PTM
23 * capability; their PTM behavior is controlled by the Upstream
24 * Port (PCIe r5.0, sec 7.9.16), so if the upstream bridge is a
25 * Switch Downstream Port, look up one more level.
26 */
27 if (ups && pci_pcie_type(ups) == PCI_EXP_TYPE_DOWNSTREAM)
28 ups = pci_upstream_bridge(ups);
29
30 if (ups && ups->ptm_cap)
31 return ups;
32
33 return NULL;
34}
35
36/*
37 * Find the PTM Capability (if present) and extract the information we need
38 * to use it.
39 */
40void pci_ptm_init(struct pci_dev *dev)
41{
42 u16 ptm;
43 u32 cap;
44 struct pci_dev *ups;
45
46 if (!pci_is_pcie(dev))
47 return;
48
49 ptm = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_PTM);
50 if (!ptm)
51 return;
52
53 dev->ptm_cap = ptm;
54 pci_add_ext_cap_save_buffer(dev, PCI_EXT_CAP_ID_PTM, sizeof(u32));
55
56 pci_read_config_dword(dev, ptm + PCI_PTM_CAP, &cap);
57 dev->ptm_granularity = FIELD_GET(PCI_PTM_GRANULARITY_MASK, cap);
58
59 /*
60 * Per the spec recommendation (PCIe r6.0, sec 7.9.15.3), select the
61 * furthest upstream Time Source as the PTM Root. For Endpoints,
62 * "the Effective Granularity is the maximum Local Clock Granularity
63 * reported by the PTM Root and all intervening PTM Time Sources."
64 */
65 ups = pci_upstream_ptm(dev);
66 if (ups) {
67 if (ups->ptm_granularity == 0)
68 dev->ptm_granularity = 0;
69 else if (ups->ptm_granularity > dev->ptm_granularity)
70 dev->ptm_granularity = ups->ptm_granularity;
71 } else if (cap & PCI_PTM_CAP_ROOT) {
72 dev->ptm_root = 1;
73 } else if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) {
74
75 /*
76 * Per sec 7.9.15.3, this should be the Local Clock
77 * Granularity of the associated Time Source. But it
78 * doesn't say how to find that Time Source.
79 */
80 dev->ptm_granularity = 0;
81 }
82
83 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT ||
84 pci_pcie_type(dev) == PCI_EXP_TYPE_UPSTREAM)
85 pci_enable_ptm(dev, NULL);
86}
87
88void pci_save_ptm_state(struct pci_dev *dev)
89{
90 u16 ptm = dev->ptm_cap;
91 struct pci_cap_saved_state *save_state;
92 u32 *cap;
93
94 if (!ptm)
95 return;
96
97 save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_PTM);
98 if (!save_state)
99 return;
100
101 cap = (u32 *)&save_state->cap.data[0];
102 pci_read_config_dword(dev, ptm + PCI_PTM_CTRL, cap);
103}
104
105void pci_restore_ptm_state(struct pci_dev *dev)
106{
107 u16 ptm = dev->ptm_cap;
108 struct pci_cap_saved_state *save_state;
109 u32 *cap;
110
111 if (!ptm)
112 return;
113
114 save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_PTM);
115 if (!save_state)
116 return;
117
118 cap = (u32 *)&save_state->cap.data[0];
119 pci_write_config_dword(dev, ptm + PCI_PTM_CTRL, *cap);
120}
121
122/* Enable PTM in the Control register if possible */
123static int __pci_enable_ptm(struct pci_dev *dev)
124{
125 u16 ptm = dev->ptm_cap;
126 struct pci_dev *ups;
127 u32 ctrl;
128
129 if (!ptm)
130 return -EINVAL;
131
132 /*
133 * A device uses local PTM Messages to request time information
134 * from a PTM Root that's farther upstream. Every device along the
135 * path must support PTM and have it enabled so it can handle the
136 * messages. Therefore, if this device is not a PTM Root, the
137 * upstream link partner must have PTM enabled before we can enable
138 * PTM.
139 */
140 if (!dev->ptm_root) {
141 ups = pci_upstream_ptm(dev);
142 if (!ups || !ups->ptm_enabled)
143 return -EINVAL;
144 }
145
146 pci_read_config_dword(dev, ptm + PCI_PTM_CTRL, &ctrl);
147
148 ctrl |= PCI_PTM_CTRL_ENABLE;
149 ctrl &= ~PCI_PTM_GRANULARITY_MASK;
150 ctrl |= FIELD_PREP(PCI_PTM_GRANULARITY_MASK, dev->ptm_granularity);
151 if (dev->ptm_root)
152 ctrl |= PCI_PTM_CTRL_ROOT;
153
154 pci_write_config_dword(dev, ptm + PCI_PTM_CTRL, ctrl);
155 return 0;
156}
157
158/**
159 * pci_enable_ptm() - Enable Precision Time Measurement
160 * @dev: PCI device
161 * @granularity: pointer to return granularity
162 *
163 * Enable Precision Time Measurement for @dev. If successful and
164 * @granularity is non-NULL, return the Effective Granularity.
165 *
166 * Return: zero if successful, or -EINVAL if @dev lacks a PTM Capability or
167 * is not a PTM Root and lacks an upstream path of PTM-enabled devices.
168 */
169int pci_enable_ptm(struct pci_dev *dev, u8 *granularity)
170{
171 int rc;
172 char clock_desc[8];
173
174 rc = __pci_enable_ptm(dev);
175 if (rc)
176 return rc;
177
178 dev->ptm_enabled = 1;
179
180 if (granularity)
181 *granularity = dev->ptm_granularity;
182
183 switch (dev->ptm_granularity) {
184 case 0:
185 snprintf(clock_desc, sizeof(clock_desc), "unknown");
186 break;
187 case 255:
188 snprintf(clock_desc, sizeof(clock_desc), ">254ns");
189 break;
190 default:
191 snprintf(clock_desc, sizeof(clock_desc), "%uns",
192 dev->ptm_granularity);
193 break;
194 }
195 pci_info(dev, "PTM enabled%s, %s granularity\n",
196 dev->ptm_root ? " (root)" : "", clock_desc);
197
198 return 0;
199}
200EXPORT_SYMBOL(pci_enable_ptm);
201
202static void __pci_disable_ptm(struct pci_dev *dev)
203{
204 u16 ptm = dev->ptm_cap;
205 u32 ctrl;
206
207 if (!ptm)
208 return;
209
210 pci_read_config_dword(dev, ptm + PCI_PTM_CTRL, &ctrl);
211 ctrl &= ~(PCI_PTM_CTRL_ENABLE | PCI_PTM_CTRL_ROOT);
212 pci_write_config_dword(dev, ptm + PCI_PTM_CTRL, ctrl);
213}
214
215/**
216 * pci_disable_ptm() - Disable Precision Time Measurement
217 * @dev: PCI device
218 *
219 * Disable Precision Time Measurement for @dev.
220 */
221void pci_disable_ptm(struct pci_dev *dev)
222{
223 if (dev->ptm_enabled) {
224 __pci_disable_ptm(dev);
225 dev->ptm_enabled = 0;
226 }
227}
228EXPORT_SYMBOL(pci_disable_ptm);
229
230/*
231 * Disable PTM, but preserve dev->ptm_enabled so we silently re-enable it on
232 * resume if necessary.
233 */
234void pci_suspend_ptm(struct pci_dev *dev)
235{
236 if (dev->ptm_enabled)
237 __pci_disable_ptm(dev);
238}
239
240/* If PTM was enabled before suspend, re-enable it when resuming */
241void pci_resume_ptm(struct pci_dev *dev)
242{
243 if (dev->ptm_enabled)
244 __pci_enable_ptm(dev);
245}
246
247bool pcie_ptm_enabled(struct pci_dev *dev)
248{
249 if (!dev)
250 return false;
251
252 return dev->ptm_enabled;
253}
254EXPORT_SYMBOL(pcie_ptm_enabled);