Debian Codegraph @

drivers/hwtracing/coresight/coresight-tnoc.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2025 Qualcomm Innovation Center, Inc. All rights reserved.
4 */
5
6 #include <linux/amba/bus.h>
7 #include <linux/coresight.h>
8 #include <linux/device.h>
9 #include <linux/io.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/platform_device.h>
14
15 #include "coresight-priv.h"
16 #include "coresight-trace-id.h"
17
18 #define TRACE_NOC_CTRL 0x008
19 #define TRACE_NOC_XLD 0x010
20 #define TRACE_NOC_FREQVAL 0x018
21 #define TRACE_NOC_SYNCR 0x020
22
23 /* Enable generation of output ATB traffic.*/
24 #define TRACE_NOC_CTRL_PORTEN BIT(0)
25 /* Sets the type of issued ATB FLAG packets.*/
26 #define TRACE_NOC_CTRL_FLAGTYPE BIT(7)
27 /* Sets the type of issued ATB FREQ packet*/
28 #define TRACE_NOC_CTRL_FREQTYPE BIT(8)
29
30 #define TRACE_NOC_SYNC_INTERVAL 0xFFFF
31
32 /*
33 * struct trace_noc_drvdata - specifics associated to a trace noc component
34 * @base: memory mapped base address for this component.
35 * @dev: device node for trace_noc_drvdata.
36 * @csdev: component vitals needed by the framework.
37 * @pclk: APB clock if present, otherwise NULL
38 * @spinlock: serialize enable/disable operation.
39 * @atid: id for the trace packet.
40 */
41 struct trace_noc_drvdata {
42 void __iomem *base;
43 struct device *dev;
44 struct coresight_device *csdev;
45 struct clk *pclk;
46 spinlock_t spinlock;
47 int atid;
48 };
49
50 DEFINE_CORESIGHT_DEVLIST(trace_noc_devs, "traceNoc");
51
52 static void trace_noc_enable_hw(struct trace_noc_drvdata *drvdata)
53 {
54 u32 val;
55
56 /* No valid ATID, simply enable the unit */
57 if (drvdata->atid == -EOPNOTSUPP) {
58 writel(TRACE_NOC_CTRL_PORTEN, drvdata->base + TRACE_NOC_CTRL);
59 return;
60 }
61
62 /* Set ATID */
63 writel_relaxed(drvdata->atid, drvdata->base + TRACE_NOC_XLD);
64
65 /* Set the data word count between 'SYNC' packets */
66 writel_relaxed(TRACE_NOC_SYNC_INTERVAL, drvdata->base + TRACE_NOC_SYNCR);
67
68 /* Set the Control register:
69 * - Set the FLAG packets to 'FLAG' packets
70 * - Set the FREQ packets to 'FREQ_TS' packets
71 * - Enable generation of output ATB traffic
72 */
73
74 val = readl_relaxed(drvdata->base + TRACE_NOC_CTRL);
75
76 val &= ~TRACE_NOC_CTRL_FLAGTYPE;
77 val |= TRACE_NOC_CTRL_FREQTYPE;
78 val |= TRACE_NOC_CTRL_PORTEN;
79
80 writel(val, drvdata->base + TRACE_NOC_CTRL);
81 }
82
83 static int trace_noc_enable(struct coresight_device *csdev, struct coresight_connection *inport,
84 struct coresight_connection *outport)
85 {
86 struct trace_noc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
87
88 scoped_guard(spinlock, &drvdata->spinlock) {
89 if (csdev->refcnt == 0)
90 trace_noc_enable_hw(drvdata);
91
92 csdev->refcnt++;
93 }
94
95 dev_dbg(drvdata->dev, "Trace NOC is enabled\n");
96 return 0;
97 }
98
99 static void trace_noc_disable(struct coresight_device *csdev, struct coresight_connection *inport,
100 struct coresight_connection *outport)
101 {
102 struct trace_noc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
103
104 scoped_guard(spinlock, &drvdata->spinlock) {
105 if (--csdev->refcnt == 0)
106 writel(0x0, drvdata->base + TRACE_NOC_CTRL);
107 }
108 dev_dbg(drvdata->dev, "Trace NOC is disabled\n");
109 }
110
111 static int trace_noc_id(struct coresight_device *csdev, __maybe_unused enum cs_mode mode,
112 __maybe_unused struct coresight_device *sink)
113 {
114 struct trace_noc_drvdata *drvdata;
115
116 drvdata = dev_get_drvdata(csdev->dev.parent);
117
118 return drvdata->atid;
119 }
120
121 static const struct coresight_ops_link trace_noc_link_ops = {
122 .enable = trace_noc_enable,
123 .disable = trace_noc_disable,
124 };
125
126 static const struct coresight_ops trace_noc_cs_ops = {
127 .trace_id = trace_noc_id,
128 .link_ops = &trace_noc_link_ops,
129 };
130
131 static int trace_noc_init_default_data(struct trace_noc_drvdata *drvdata)
132 {
133 int atid;
134
135 if (!dev_is_amba(drvdata->dev)) {
136 drvdata->atid = -EOPNOTSUPP;
137 return 0;
138 }
139
140 atid = coresight_trace_id_get_system_id();
141 if (atid < 0)
142 return atid;
143
144 drvdata->atid = atid;
145
146 return 0;
147 }
148
149 static ssize_t traceid_show(struct device *dev,
150 struct device_attribute *attr, char *buf)
151 {
152 unsigned long val;
153 struct trace_noc_drvdata *drvdata = dev_get_drvdata(dev->parent);
154
155 val = drvdata->atid;
156 return sprintf(buf, "%#lx\n", val);
157 }
158 static DEVICE_ATTR_RO(traceid);
159
160 static struct attribute *coresight_tnoc_attrs[] = {
161 &dev_attr_traceid.attr,
162 NULL,
163 };
164
165 static umode_t trace_id_is_visible(struct kobject *kobj,
166 struct attribute *attr, int idx)
167 {
168 struct device *dev = kobj_to_dev(kobj);
169 struct trace_noc_drvdata *drvdata = dev_get_drvdata(dev->parent);
170
171 if (attr == &dev_attr_traceid.attr && drvdata->atid < 0)
172 return 0;
173
174 return attr->mode;
175 }
176
177 static const struct attribute_group coresight_tnoc_group = {
178 .attrs = coresight_tnoc_attrs,
179 .is_visible = trace_id_is_visible,
180 };
181
182 static const struct attribute_group *coresight_tnoc_groups[] = {
183 &coresight_tnoc_group,
184 NULL,
185 };
186
187 static int _tnoc_probe(struct device *dev, struct resource *res)
188 {
189 struct coresight_platform_data *pdata;
190 struct trace_noc_drvdata *drvdata;
191 struct coresight_desc desc = { 0 };
192 int ret;
193
194 desc.name = coresight_alloc_device_name(&trace_noc_devs, dev);
195 if (!desc.name)
196 return -ENOMEM;
197
198 pdata = coresight_get_platform_data(dev);
199 if (IS_ERR(pdata))
200 return PTR_ERR(pdata);
201 dev->platform_data = pdata;
202
203 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
204 if (!drvdata)
205 return -ENOMEM;
206
207 drvdata->dev = dev;
208 dev_set_drvdata(dev, drvdata);
209
210 ret = coresight_get_enable_clocks(dev, &drvdata->pclk, NULL);
211 if (ret)
212 return ret;
213
214 drvdata->base = devm_ioremap_resource(dev, res);
215 if (IS_ERR(drvdata->base))
216 return PTR_ERR(drvdata->base);
217
218 spin_lock_init(&drvdata->spinlock);
219
220 ret = trace_noc_init_default_data(drvdata);
221 if (ret)
222 return ret;
223
224 desc.ops = &trace_noc_cs_ops;
225 desc.type = CORESIGHT_DEV_TYPE_LINK;
226 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
227 desc.pdata = pdata;
228 desc.dev = dev;
229 desc.access = CSDEV_ACCESS_IOMEM(drvdata->base);
230 desc.groups = coresight_tnoc_groups;
231 drvdata->csdev = coresight_register(&desc);
232 if (IS_ERR(drvdata->csdev)) {
233 if (drvdata->atid > 0)
234 coresight_trace_id_put_system_id(drvdata->atid);
235 return PTR_ERR(drvdata->csdev);
236 }
237
238 return 0;
239 }
240
241 static int trace_noc_probe(struct amba_device *adev, const struct amba_id *id)
242 {
243 int ret;
244
245 ret = _tnoc_probe(&adev->dev, &adev->res);
246 if (!ret)
247 pm_runtime_put(&adev->dev);
248
249 return ret;
250 }
251
252 static void trace_noc_remove(struct amba_device *adev)
253 {
254 struct trace_noc_drvdata *drvdata = dev_get_drvdata(&adev->dev);
255
256 coresight_unregister(drvdata->csdev);
257 coresight_trace_id_put_system_id(drvdata->atid);
258 }
259
260 static struct amba_id trace_noc_ids[] = {
261 {
262 .id = 0x000f0c00,
263 .mask = 0x00ffff00,
264 },
265 {
266 .id = 0x001f0c00,
267 .mask = 0x00ffff00,
268 },
269 {},
270 };
271 MODULE_DEVICE_TABLE(amba, trace_noc_ids);
272
273 static struct amba_driver trace_noc_driver = {
274 .drv = {
275 .name = "coresight-trace-noc",
276 .suppress_bind_attrs = true,
277 },
278 .probe = trace_noc_probe,
279 .remove = trace_noc_remove,
280 .id_table = trace_noc_ids,
281 };
282
283 static int itnoc_probe(struct platform_device *pdev)
284 {
285 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
286 int ret;
287
288 pm_runtime_get_noresume(&pdev->dev);
289 pm_runtime_set_active(&pdev->dev);
290 pm_runtime_enable(&pdev->dev);
291
292 ret = _tnoc_probe(&pdev->dev, res);
293 pm_runtime_put(&pdev->dev);
294 if (ret)
295 pm_runtime_disable(&pdev->dev);
296
297 return ret;
298 }
299
300 static void itnoc_remove(struct platform_device *pdev)
301 {
302 struct trace_noc_drvdata *drvdata = platform_get_drvdata(pdev);
303
304 coresight_unregister(drvdata->csdev);
305 pm_runtime_disable(&pdev->dev);
306 }
307
308 #ifdef CONFIG_PM
309 static int itnoc_runtime_suspend(struct device *dev)
310 {
311 struct trace_noc_drvdata *drvdata = dev_get_drvdata(dev);
312
313 clk_disable_unprepare(drvdata->pclk);
314
315 return 0;
316 }
317
318 static int itnoc_runtime_resume(struct device *dev)
319 {
320 struct trace_noc_drvdata *drvdata = dev_get_drvdata(dev);
321
322 return clk_prepare_enable(drvdata->pclk);
323 }
324 #endif
325
326 static const struct dev_pm_ops itnoc_dev_pm_ops = {
327 SET_RUNTIME_PM_OPS(itnoc_runtime_suspend, itnoc_runtime_resume, NULL)
328 };
329
330 static const struct of_device_id itnoc_of_match[] = {
331 { .compatible = "qcom,coresight-itnoc" },
332 {}
333 };
334 MODULE_DEVICE_TABLE(of, itnoc_of_match);
335
336 static struct platform_driver itnoc_driver = {
337 .probe = itnoc_probe,
338 .remove = itnoc_remove,
339 .driver = {
340 .name = "coresight-itnoc",
341 .of_match_table = itnoc_of_match,
342 .suppress_bind_attrs = true,
343 .pm = &itnoc_dev_pm_ops,
344 },
345 };
346
347 static int __init tnoc_init(void)
348 {
349 return coresight_init_driver("tnoc", &trace_noc_driver, &itnoc_driver, THIS_MODULE);
350 }
351
352 static void __exit tnoc_exit(void)
353 {
354 coresight_remove_driver(&trace_noc_driver, &itnoc_driver);
355 }
356 module_init(tnoc_init);
357 module_exit(tnoc_exit);
358
359 MODULE_LICENSE("GPL");
360 MODULE_DESCRIPTION("Trace NOC driver");