xref: /linux-6.15/include/linux/coresight.h (revision c56cdd7a)
1a06ae860SPratik Patel /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2a06ae860SPratik Patel  *
3a06ae860SPratik Patel  * This program is free software; you can redistribute it and/or modify
4a06ae860SPratik Patel  * it under the terms of the GNU General Public License version 2 and
5a06ae860SPratik Patel  * only version 2 as published by the Free Software Foundation.
6a06ae860SPratik Patel  *
7a06ae860SPratik Patel  * This program is distributed in the hope that it will be useful,
8a06ae860SPratik Patel  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9a06ae860SPratik Patel  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10a06ae860SPratik Patel  * GNU General Public License for more details.
11a06ae860SPratik Patel  */
12a06ae860SPratik Patel 
13a06ae860SPratik Patel #ifndef _LINUX_CORESIGHT_H
14a06ae860SPratik Patel #define _LINUX_CORESIGHT_H
15a06ae860SPratik Patel 
16a06ae860SPratik Patel #include <linux/device.h>
17882d5e11SMathieu Poirier #include <linux/perf_event.h>
18ff63ec13SMark Brown #include <linux/sched.h>
19a06ae860SPratik Patel 
20a06ae860SPratik Patel /* Peripheral id registers (0xFD0-0xFEC) */
21a06ae860SPratik Patel #define CORESIGHT_PERIPHIDR4	0xfd0
22a06ae860SPratik Patel #define CORESIGHT_PERIPHIDR5	0xfd4
23a06ae860SPratik Patel #define CORESIGHT_PERIPHIDR6	0xfd8
24a06ae860SPratik Patel #define CORESIGHT_PERIPHIDR7	0xfdC
25a06ae860SPratik Patel #define CORESIGHT_PERIPHIDR0	0xfe0
26a06ae860SPratik Patel #define CORESIGHT_PERIPHIDR1	0xfe4
27a06ae860SPratik Patel #define CORESIGHT_PERIPHIDR2	0xfe8
28a06ae860SPratik Patel #define CORESIGHT_PERIPHIDR3	0xfeC
29a06ae860SPratik Patel /* Component id registers (0xFF0-0xFFC) */
30a06ae860SPratik Patel #define CORESIGHT_COMPIDR0	0xff0
31a06ae860SPratik Patel #define CORESIGHT_COMPIDR1	0xff4
32a06ae860SPratik Patel #define CORESIGHT_COMPIDR2	0xff8
33a06ae860SPratik Patel #define CORESIGHT_COMPIDR3	0xffC
34a06ae860SPratik Patel 
35a06ae860SPratik Patel #define ETM_ARCH_V3_3		0x23
36a06ae860SPratik Patel #define ETM_ARCH_V3_5		0x25
37a06ae860SPratik Patel #define PFT_ARCH_V1_0		0x30
38a06ae860SPratik Patel #define PFT_ARCH_V1_1		0x31
39a06ae860SPratik Patel 
40a06ae860SPratik Patel #define CORESIGHT_UNLOCK	0xc5acce55
41a06ae860SPratik Patel 
42a06ae860SPratik Patel extern struct bus_type coresight_bustype;
43a06ae860SPratik Patel 
44a06ae860SPratik Patel enum coresight_dev_type {
45a06ae860SPratik Patel 	CORESIGHT_DEV_TYPE_NONE,
46a06ae860SPratik Patel 	CORESIGHT_DEV_TYPE_SINK,
47a06ae860SPratik Patel 	CORESIGHT_DEV_TYPE_LINK,
48a06ae860SPratik Patel 	CORESIGHT_DEV_TYPE_LINKSINK,
49a06ae860SPratik Patel 	CORESIGHT_DEV_TYPE_SOURCE,
50a06ae860SPratik Patel };
51a06ae860SPratik Patel 
52a06ae860SPratik Patel enum coresight_dev_subtype_sink {
53a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_SINK_NONE,
54a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_SINK_PORT,
55a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_SINK_BUFFER,
56a06ae860SPratik Patel };
57a06ae860SPratik Patel 
58a06ae860SPratik Patel enum coresight_dev_subtype_link {
59a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_LINK_NONE,
60a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_LINK_MERG,
61a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_LINK_SPLIT,
62a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_LINK_FIFO,
63a06ae860SPratik Patel };
64a06ae860SPratik Patel 
65a06ae860SPratik Patel enum coresight_dev_subtype_source {
66a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_SOURCE_NONE,
67a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_SOURCE_PROC,
68a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_SOURCE_BUS,
69a06ae860SPratik Patel 	CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE,
70a06ae860SPratik Patel };
71a06ae860SPratik Patel 
72a06ae860SPratik Patel /**
73a06ae860SPratik Patel  * struct coresight_dev_subtype - further characterisation of a type
74a06ae860SPratik Patel  * @sink_subtype:	type of sink this component is, as defined
75a06ae860SPratik Patel 			by @coresight_dev_subtype_sink.
76a06ae860SPratik Patel  * @link_subtype:	type of link this component is, as defined
77a06ae860SPratik Patel 			by @coresight_dev_subtype_link.
78a06ae860SPratik Patel  * @source_subtype:	type of source this component is, as defined
79a06ae860SPratik Patel 			by @coresight_dev_subtype_source.
80a06ae860SPratik Patel  */
81a06ae860SPratik Patel struct coresight_dev_subtype {
82a06ae860SPratik Patel 	enum coresight_dev_subtype_sink sink_subtype;
83a06ae860SPratik Patel 	enum coresight_dev_subtype_link link_subtype;
84a06ae860SPratik Patel 	enum coresight_dev_subtype_source source_subtype;
85a06ae860SPratik Patel };
86a06ae860SPratik Patel 
87a06ae860SPratik Patel /**
88a06ae860SPratik Patel  * struct coresight_platform_data - data harvested from the DT specification
89a06ae860SPratik Patel  * @cpu:	the CPU a source belongs to. Only applicable for ETM/PTMs.
90a06ae860SPratik Patel  * @name:	name of the component as shown under sysfs.
91a06ae860SPratik Patel  * @nr_inport:	number of input ports for this component.
928ee885a9SPankaj Dubey  * @outports:	list of remote endpoint port number.
93a06ae860SPratik Patel  * @child_names:name of all child components connected to this device.
94a06ae860SPratik Patel  * @child_ports:child component port number the current component is
95a06ae860SPratik Patel 		connected  to.
96a06ae860SPratik Patel  * @nr_outport:	number of output ports for this component.
97a06ae860SPratik Patel  * @clk:	The clock this component is associated to.
98a06ae860SPratik Patel  */
99a06ae860SPratik Patel struct coresight_platform_data {
100a06ae860SPratik Patel 	int cpu;
101a06ae860SPratik Patel 	const char *name;
102a06ae860SPratik Patel 	int nr_inport;
103a06ae860SPratik Patel 	int *outports;
104a06ae860SPratik Patel 	const char **child_names;
105a06ae860SPratik Patel 	int *child_ports;
106a06ae860SPratik Patel 	int nr_outport;
107a06ae860SPratik Patel 	struct clk *clk;
108a06ae860SPratik Patel };
109a06ae860SPratik Patel 
110a06ae860SPratik Patel /**
111a06ae860SPratik Patel  * struct coresight_desc - description of a component required from drivers
112a06ae860SPratik Patel  * @type:	as defined by @coresight_dev_type.
113a06ae860SPratik Patel  * @subtype:	as defined by @coresight_dev_subtype.
114a06ae860SPratik Patel  * @ops:	generic operations for this component, as defined
115a06ae860SPratik Patel 		by @coresight_ops.
116a06ae860SPratik Patel  * @pdata:	platform data collected from DT.
117a06ae860SPratik Patel  * @dev:	The device entity associated to this component.
118a06ae860SPratik Patel  * @groups:	operations specific to this component. These will end up
119a06ae860SPratik Patel 		in the component's sysfs sub-directory.
120a06ae860SPratik Patel  */
121a06ae860SPratik Patel struct coresight_desc {
122a06ae860SPratik Patel 	enum coresight_dev_type type;
123a06ae860SPratik Patel 	struct coresight_dev_subtype subtype;
124a06ae860SPratik Patel 	const struct coresight_ops *ops;
125a06ae860SPratik Patel 	struct coresight_platform_data *pdata;
126a06ae860SPratik Patel 	struct device *dev;
127a06ae860SPratik Patel 	const struct attribute_group **groups;
128a06ae860SPratik Patel };
129a06ae860SPratik Patel 
130a06ae860SPratik Patel /**
131a06ae860SPratik Patel  * struct coresight_connection - representation of a single connection
132a06ae860SPratik Patel  * @outport:	a connection's output port number.
133a06ae860SPratik Patel  * @chid_name:	remote component's name.
134a06ae860SPratik Patel  * @child_port:	remote component's port number @output is connected to.
135a06ae860SPratik Patel  * @child_dev:	a @coresight_device representation of the component
136a06ae860SPratik Patel 		connected to @outport.
137a06ae860SPratik Patel  */
138a06ae860SPratik Patel struct coresight_connection {
139a06ae860SPratik Patel 	int outport;
140a06ae860SPratik Patel 	const char *child_name;
141a06ae860SPratik Patel 	int child_port;
142a06ae860SPratik Patel 	struct coresight_device *child_dev;
143a06ae860SPratik Patel };
144a06ae860SPratik Patel 
145a06ae860SPratik Patel /**
146a06ae860SPratik Patel  * struct coresight_device - representation of a device as used by the framework
1478ee885a9SPankaj Dubey  * @conns:	array of coresight_connections associated to this component.
148a06ae860SPratik Patel  * @nr_inport:	number of input port associated to this component.
149a06ae860SPratik Patel  * @nr_outport:	number of output port associated to this component.
150a06ae860SPratik Patel  * @type:	as defined by @coresight_dev_type.
151a06ae860SPratik Patel  * @subtype:	as defined by @coresight_dev_subtype.
152a06ae860SPratik Patel  * @ops:	generic operations for this component, as defined
153a06ae860SPratik Patel 		by @coresight_ops.
154a06ae860SPratik Patel  * @dev:	The device entity associated to this component.
155a06ae860SPratik Patel  * @refcnt:	keep track of what is in use.
156a06ae860SPratik Patel  * @orphan:	true if the component has connections that haven't been linked.
157a06ae860SPratik Patel  * @enable:	'true' if component is currently part of an active path.
158a06ae860SPratik Patel  * @activated:	'true' only if a _sink_ has been activated.  A sink can be
159a06ae860SPratik Patel 		activated but not yet enabled.  Enabling for a _sink_
160a06ae860SPratik Patel 		happens when a source has been selected for that it.
161a06ae860SPratik Patel  */
162a06ae860SPratik Patel struct coresight_device {
163a06ae860SPratik Patel 	struct coresight_connection *conns;
164a06ae860SPratik Patel 	int nr_inport;
165a06ae860SPratik Patel 	int nr_outport;
166a06ae860SPratik Patel 	enum coresight_dev_type type;
167a06ae860SPratik Patel 	struct coresight_dev_subtype subtype;
168a06ae860SPratik Patel 	const struct coresight_ops *ops;
169a06ae860SPratik Patel 	struct device dev;
170a06ae860SPratik Patel 	atomic_t *refcnt;
171a06ae860SPratik Patel 	bool orphan;
172a06ae860SPratik Patel 	bool enable;	/* true only if configured as part of a path */
173a06ae860SPratik Patel 	bool activated;	/* true only if a sink is part of a path */
174a06ae860SPratik Patel };
175a06ae860SPratik Patel 
176a06ae860SPratik Patel #define to_coresight_device(d) container_of(d, struct coresight_device, dev)
177a06ae860SPratik Patel 
178a06ae860SPratik Patel #define source_ops(csdev)	csdev->ops->source_ops
179a06ae860SPratik Patel #define sink_ops(csdev)		csdev->ops->sink_ops
180a06ae860SPratik Patel #define link_ops(csdev)		csdev->ops->link_ops
181a06ae860SPratik Patel 
182a06ae860SPratik Patel /**
183a06ae860SPratik Patel  * struct coresight_ops_sink - basic operations for a sink
184a06ae860SPratik Patel  * Operations available for sinks
185a06ae860SPratik Patel  * @enable:		enables the sink.
186a06ae860SPratik Patel  * @disable:		disables the sink.
1872997aa40SMathieu Poirier  * @alloc_buffer:	initialises perf's ring buffer for trace collection.
1882997aa40SMathieu Poirier  * @free_buffer:	release memory allocated in @get_config.
1892997aa40SMathieu Poirier  * @set_buffer:		initialises buffer mechanic before a trace session.
1902997aa40SMathieu Poirier  * @reset_buffer:	finalises buffer mechanic after a trace session.
1912997aa40SMathieu Poirier  * @update_buffer:	update buffer pointers after a trace session.
192a06ae860SPratik Patel  */
193a06ae860SPratik Patel struct coresight_ops_sink {
194e827d455SMathieu Poirier 	int (*enable)(struct coresight_device *csdev, u32 mode);
195a06ae860SPratik Patel 	void (*disable)(struct coresight_device *csdev);
1962997aa40SMathieu Poirier 	void *(*alloc_buffer)(struct coresight_device *csdev, int cpu,
1972997aa40SMathieu Poirier 			      void **pages, int nr_pages, bool overwrite);
1982997aa40SMathieu Poirier 	void (*free_buffer)(void *config);
1992997aa40SMathieu Poirier 	int (*set_buffer)(struct coresight_device *csdev,
2002997aa40SMathieu Poirier 			  struct perf_output_handle *handle,
2012997aa40SMathieu Poirier 			  void *sink_config);
2022997aa40SMathieu Poirier 	unsigned long (*reset_buffer)(struct coresight_device *csdev,
2032997aa40SMathieu Poirier 				      struct perf_output_handle *handle,
204f4c0b0aaSWill Deacon 				      void *sink_config);
2052997aa40SMathieu Poirier 	void (*update_buffer)(struct coresight_device *csdev,
2062997aa40SMathieu Poirier 			      struct perf_output_handle *handle,
2072997aa40SMathieu Poirier 			      void *sink_config);
208a06ae860SPratik Patel };
209a06ae860SPratik Patel 
210a06ae860SPratik Patel /**
211a06ae860SPratik Patel  * struct coresight_ops_link - basic operations for a link
212a06ae860SPratik Patel  * Operations available for links.
213a06ae860SPratik Patel  * @enable:	enables flow between iport and oport.
214a06ae860SPratik Patel  * @disable:	disables flow between iport and oport.
215a06ae860SPratik Patel  */
216a06ae860SPratik Patel struct coresight_ops_link {
217a06ae860SPratik Patel 	int (*enable)(struct coresight_device *csdev, int iport, int oport);
218a06ae860SPratik Patel 	void (*disable)(struct coresight_device *csdev, int iport, int oport);
219a06ae860SPratik Patel };
220a06ae860SPratik Patel 
221a06ae860SPratik Patel /**
222a06ae860SPratik Patel  * struct coresight_ops_source - basic operations for a source
223a06ae860SPratik Patel  * Operations available for sources.
22452210c87SMathieu Poirier  * @cpu_id:	returns the value of the CPU number this component
22552210c87SMathieu Poirier  *		is associated to.
226a06ae860SPratik Patel  * @trace_id:	returns the value of the component's trace ID as known
227882d5e11SMathieu Poirier  *		to the HW.
2281d27ff5aSMathieu Poirier  * @enable:	enables tracing for a source.
229a06ae860SPratik Patel  * @disable:	disables tracing for a source.
230a06ae860SPratik Patel  */
231a06ae860SPratik Patel struct coresight_ops_source {
23252210c87SMathieu Poirier 	int (*cpu_id)(struct coresight_device *csdev);
233a06ae860SPratik Patel 	int (*trace_id)(struct coresight_device *csdev);
234882d5e11SMathieu Poirier 	int (*enable)(struct coresight_device *csdev,
23568905d73SMathieu Poirier 		      struct perf_event *event,  u32 mode);
23668905d73SMathieu Poirier 	void (*disable)(struct coresight_device *csdev,
23768905d73SMathieu Poirier 			struct perf_event *event);
238a06ae860SPratik Patel };
239a06ae860SPratik Patel 
240a06ae860SPratik Patel struct coresight_ops {
241a06ae860SPratik Patel 	const struct coresight_ops_sink *sink_ops;
242a06ae860SPratik Patel 	const struct coresight_ops_link *link_ops;
243a06ae860SPratik Patel 	const struct coresight_ops_source *source_ops;
244a06ae860SPratik Patel };
245a06ae860SPratik Patel 
246a06ae860SPratik Patel #ifdef CONFIG_CORESIGHT
247a06ae860SPratik Patel extern struct coresight_device *
248a06ae860SPratik Patel coresight_register(struct coresight_desc *desc);
249a06ae860SPratik Patel extern void coresight_unregister(struct coresight_device *csdev);
250a06ae860SPratik Patel extern int coresight_enable(struct coresight_device *csdev);
251a06ae860SPratik Patel extern void coresight_disable(struct coresight_device *csdev);
252a06ae860SPratik Patel extern int coresight_timeout(void __iomem *addr, u32 offset,
253a06ae860SPratik Patel 			     int position, int value);
254a06ae860SPratik Patel #else
255a06ae860SPratik Patel static inline struct coresight_device *
256a06ae860SPratik Patel coresight_register(struct coresight_desc *desc) { return NULL; }
257a06ae860SPratik Patel static inline void coresight_unregister(struct coresight_device *csdev) {}
258a06ae860SPratik Patel static inline int
259a06ae860SPratik Patel coresight_enable(struct coresight_device *csdev) { return -ENOSYS; }
260a06ae860SPratik Patel static inline void coresight_disable(struct coresight_device *csdev) {}
261a06ae860SPratik Patel static inline int coresight_timeout(void __iomem *addr, u32 offset,
262a06ae860SPratik Patel 				     int position, int value) { return 1; }
263c61c4b5dSMathieu Poirier #endif
264c61c4b5dSMathieu Poirier 
265a06ae860SPratik Patel #ifdef CONFIG_OF
266*c56cdd7aSLeo Yan extern int of_coresight_get_cpu(const struct device_node *node);
267f42fe520SLeo Yan extern struct coresight_platform_data *
268f42fe520SLeo Yan of_get_coresight_platform_data(struct device *dev,
269f42fe520SLeo Yan 			       const struct device_node *node);
270c61c4b5dSMathieu Poirier #else
271*c56cdd7aSLeo Yan static inline int of_coresight_get_cpu(const struct device_node *node)
272*c56cdd7aSLeo Yan { return 0; }
273a06ae860SPratik Patel static inline struct coresight_platform_data *of_get_coresight_platform_data(
274f42fe520SLeo Yan 	struct device *dev, const struct device_node *node) { return NULL; }
275a06ae860SPratik Patel #endif
276a06ae860SPratik Patel 
27772210227SChunyan Zhang #ifdef CONFIG_PID_NS
27872210227SChunyan Zhang static inline unsigned long
27972210227SChunyan Zhang coresight_vpid_to_pid(unsigned long vpid)
28072210227SChunyan Zhang {
28172210227SChunyan Zhang 	struct task_struct *task = NULL;
28272210227SChunyan Zhang 	unsigned long pid = 0;
28372210227SChunyan Zhang 
28472210227SChunyan Zhang 	rcu_read_lock();
28572210227SChunyan Zhang 	task = find_task_by_vpid(vpid);
28672210227SChunyan Zhang 	if (task)
28772210227SChunyan Zhang 		pid = task_pid_nr(task);
28872210227SChunyan Zhang 	rcu_read_unlock();
28972210227SChunyan Zhang 
29072210227SChunyan Zhang 	return pid;
29172210227SChunyan Zhang }
29272210227SChunyan Zhang #else
29372210227SChunyan Zhang static inline unsigned long
29472210227SChunyan Zhang coresight_vpid_to_pid(unsigned long vpid) { return vpid; }
29572210227SChunyan Zhang #endif
29672210227SChunyan Zhang 
297a06ae860SPratik Patel #endif
298