1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
6 #ifndef _LINUX_CORESIGHT_H
7 #define _LINUX_CORESIGHT_H
9 #include <linux/amba/bus.h>
10 #include <linux/clk.h>
11 #include <linux/device.h>
13 #include <linux/perf_event.h>
14 #include <linux/sched.h>
15 #include <linux/platform_device.h>
17 /* Peripheral id registers (0xFD0-0xFEC) */
18 #define CORESIGHT_PERIPHIDR4 0xfd0
19 #define CORESIGHT_PERIPHIDR5 0xfd4
20 #define CORESIGHT_PERIPHIDR6 0xfd8
21 #define CORESIGHT_PERIPHIDR7 0xfdC
22 #define CORESIGHT_PERIPHIDR0 0xfe0
23 #define CORESIGHT_PERIPHIDR1 0xfe4
24 #define CORESIGHT_PERIPHIDR2 0xfe8
25 #define CORESIGHT_PERIPHIDR3 0xfeC
26 /* Component id registers (0xFF0-0xFFC) */
27 #define CORESIGHT_COMPIDR0 0xff0
28 #define CORESIGHT_COMPIDR1 0xff4
29 #define CORESIGHT_COMPIDR2 0xff8
30 #define CORESIGHT_COMPIDR3 0xffC
32 #define ETM_ARCH_V3_3 0x23
33 #define ETM_ARCH_V3_5 0x25
34 #define PFT_ARCH_V1_0 0x30
35 #define PFT_ARCH_V1_1 0x31
37 #define CORESIGHT_UNLOCK 0xc5acce55
39 extern const struct bus_type coresight_bustype;
41 enum coresight_dev_type {
42 CORESIGHT_DEV_TYPE_SINK,
43 CORESIGHT_DEV_TYPE_LINK,
44 CORESIGHT_DEV_TYPE_LINKSINK,
45 CORESIGHT_DEV_TYPE_SOURCE,
46 CORESIGHT_DEV_TYPE_HELPER,
47 CORESIGHT_DEV_TYPE_MAX
50 enum coresight_dev_subtype_sink {
51 CORESIGHT_DEV_SUBTYPE_SINK_DUMMY,
52 CORESIGHT_DEV_SUBTYPE_SINK_PORT,
53 CORESIGHT_DEV_SUBTYPE_SINK_BUFFER,
54 CORESIGHT_DEV_SUBTYPE_SINK_SYSMEM,
55 CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM,
58 enum coresight_dev_subtype_link {
59 CORESIGHT_DEV_SUBTYPE_LINK_MERG,
60 CORESIGHT_DEV_SUBTYPE_LINK_SPLIT,
61 CORESIGHT_DEV_SUBTYPE_LINK_FIFO,
64 enum coresight_dev_subtype_source {
65 CORESIGHT_DEV_SUBTYPE_SOURCE_PROC,
66 CORESIGHT_DEV_SUBTYPE_SOURCE_BUS,
67 CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE,
68 CORESIGHT_DEV_SUBTYPE_SOURCE_TPDM,
69 CORESIGHT_DEV_SUBTYPE_SOURCE_OTHERS,
72 enum coresight_dev_subtype_helper {
73 CORESIGHT_DEV_SUBTYPE_HELPER_CATU,
74 CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI
78 * union coresight_dev_subtype - further characterisation of a type
79 * @sink_subtype: type of sink this component is, as defined
80 * by @coresight_dev_subtype_sink.
81 * @link_subtype: type of link this component is, as defined
82 * by @coresight_dev_subtype_link.
83 * @source_subtype: type of source this component is, as defined
84 * by @coresight_dev_subtype_source.
85 * @helper_subtype: type of helper this component is, as defined
86 * by @coresight_dev_subtype_helper.
88 union coresight_dev_subtype {
89 /* We have some devices which acts as LINK and SINK */
91 enum coresight_dev_subtype_sink sink_subtype;
92 enum coresight_dev_subtype_link link_subtype;
94 enum coresight_dev_subtype_source source_subtype;
95 enum coresight_dev_subtype_helper helper_subtype;
99 * struct coresight_platform_data - data harvested from the firmware
102 * @nr_inconns: Number of elements for the input connections.
103 * @nr_outconns: Number of elements for the output connections.
104 * @out_conns: Array of nr_outconns pointers to connections from this
106 * @in_conns: Sparse array of pointers to input connections. Sparse
107 * because the source device owns the connection so when it's
108 * unloaded the connection leaves an empty slot.
110 struct coresight_platform_data {
113 struct coresight_connection **out_conns;
114 struct coresight_connection **in_conns;
118 * struct csdev_access - Abstraction of a CoreSight device access.
120 * @io_mem : True if the device has memory mapped I/O
121 * @base : When io_mem == true, base address of the component
122 * @read : Read from the given "offset" of the given instance.
123 * @write : Write "val" to the given "offset".
125 struct csdev_access {
130 u64 (*read)(u32 offset, bool relaxed, bool _64bit);
131 void (*write)(u64 val, u32 offset, bool relaxed,
137 #define CSDEV_ACCESS_IOMEM(_addr) \
138 ((struct csdev_access) { \
144 * struct coresight_desc - description of a component required from drivers
145 * @type: as defined by @coresight_dev_type.
146 * @subtype: as defined by @coresight_dev_subtype.
147 * @ops: generic operations for this component, as defined
149 * @pdata: platform data collected from DT.
150 * @dev: The device entity associated to this component.
151 * @groups: operations specific to this component. These will end up
152 * in the component's sysfs sub-directory.
153 * @name: name for the coresight device, also shown under sysfs.
154 * @access: Describe access to the device
156 struct coresight_desc {
157 enum coresight_dev_type type;
158 union coresight_dev_subtype subtype;
159 const struct coresight_ops *ops;
160 struct coresight_platform_data *pdata;
162 const struct attribute_group **groups;
164 struct csdev_access access;
168 * struct coresight_connection - representation of a single connection
169 * @src_port: a connection's output port number.
170 * @dest_port: destination's input port number @src_port is connected to.
171 * @dest_fwnode: destination component's fwnode handle.
172 * @dest_dev: a @coresight_device representation of the component
173 connected to @src_port. NULL until the device is created
174 * @link: Representation of the connection as a sysfs link.
176 * The full connection structure looks like this, where in_conns store
177 * references to same connection as the source device's out_conns.
179 * +-----------------------------+ +-----------------------------+
180 * |coresight_device | |coresight_connection |
181 * |-----------------------------| |-----------------------------|
183 * | | | dest_dev*|<--
184 * |pdata->out_conns[nr_outconns]|<->|src_dev* | |
186 * +-----------------------------+ +-----------------------------+ |
188 * +-----------------------------+ |
189 * |coresight_device | |
190 * |------------------------------ |
192 * | pdata->in_conns[nr_inconns]|<--
194 * +-----------------------------+
196 struct coresight_connection {
199 struct fwnode_handle *dest_fwnode;
200 struct coresight_device *dest_dev;
201 struct coresight_sysfs_link *link;
202 struct coresight_device *src_dev;
204 atomic_t dest_refcnt;
208 * struct coresight_sysfs_link - representation of a connection in sysfs.
209 * @orig: Originating (master) coresight device for the link.
210 * @orig_name: Name to use for the link orig->target.
211 * @target: Target (slave) coresight device for the link.
212 * @target_name: Name to use for the link target->orig.
214 struct coresight_sysfs_link {
215 struct coresight_device *orig;
216 const char *orig_name;
217 struct coresight_device *target;
218 const char *target_name;
222 * struct coresight_device - representation of a device as used by the framework
223 * @pdata: Platform data with device connections associated to this device.
224 * @type: as defined by @coresight_dev_type.
225 * @subtype: as defined by @coresight_dev_subtype.
226 * @ops: generic operations for this component, as defined
228 * @access: Device i/o access abstraction for this device.
229 * @dev: The device entity associated to this component.
230 * @mode: This tracer's mode, i.e sysFS, Perf or disabled. This is
231 * actually an 'enum cs_mode', but is stored in an atomic type.
232 * This is always accessed through local_read() and local_set(),
233 * but wherever it's done from within the Coresight device's lock,
234 * a non-atomic read would also work. This is the main point of
235 * synchronisation between code happening inside the sysfs mode's
236 * coresight_mutex and outside when running in Perf mode. A compare
237 * and exchange swap is done to atomically claim one mode or the
239 * @refcnt: keep track of what is in use. Only access this outside of the
240 * device's spinlock when the coresight_mutex held and mode ==
241 * CS_MODE_SYSFS. Otherwise it must be accessed from inside the
243 * @orphan: true if the component has connections that haven't been linked.
244 * @sysfs_sink_activated: 'true' when a sink has been selected for use via sysfs
245 * by writing a 1 to the 'enable_sink' file. A sink can be
246 * activated but not yet enabled. Enabling for a _sink_ happens
247 * when a source has been selected and a path is enabled from
248 * source to that sink. A sink can also become enabled but not
249 * activated if it's used via Perf.
250 * @ea: Device attribute for sink representation under PMU directory.
251 * @def_sink: cached reference to default sink found for this device.
252 * @nr_links: number of sysfs links created to other components from this
253 * device. These will appear in the "connections" group.
254 * @has_conns_grp: Have added a "connections" group for sysfs links.
255 * @feature_csdev_list: List of complex feature programming added to the device.
256 * @config_csdev_list: List of system configurations added to the device.
257 * @cscfg_csdev_lock: Protect the lists of configurations and features.
258 * @active_cscfg_ctxt: Context information for current active system configuration.
260 struct coresight_device {
261 struct coresight_platform_data *pdata;
262 enum coresight_dev_type type;
263 union coresight_dev_subtype subtype;
264 const struct coresight_ops *ops;
265 struct csdev_access access;
270 /* sink specific fields */
271 bool sysfs_sink_activated;
272 struct dev_ext_attribute *ea;
273 struct coresight_device *def_sink;
274 /* sysfs links between components */
277 /* system configuration and feature lists */
278 struct list_head feature_csdev_list;
279 struct list_head config_csdev_list;
280 spinlock_t cscfg_csdev_lock;
281 void *active_cscfg_ctxt;
285 * coresight_dev_list - Mapping for devices to "name" index for device
288 * @nr_idx: Number of entries already allocated.
289 * @pfx: Prefix pattern for device name.
290 * @fwnode_list: Array of fwnode_handles associated with each allocated
291 * index, upto nr_idx entries.
293 struct coresight_dev_list {
296 struct fwnode_handle **fwnode_list;
299 #define DEFINE_CORESIGHT_DEVLIST(var, dev_pfx) \
300 static struct coresight_dev_list (var) = { \
303 .fwnode_list = NULL, \
306 #define to_coresight_device(d) container_of(d, struct coresight_device, dev)
314 #define source_ops(csdev) csdev->ops->source_ops
315 #define sink_ops(csdev) csdev->ops->sink_ops
316 #define link_ops(csdev) csdev->ops->link_ops
317 #define helper_ops(csdev) csdev->ops->helper_ops
318 #define ect_ops(csdev) csdev->ops->ect_ops
321 * struct coresight_ops_sink - basic operations for a sink
322 * Operations available for sinks
323 * @enable: enables the sink.
324 * @disable: disables the sink.
325 * @alloc_buffer: initialises perf's ring buffer for trace collection.
326 * @free_buffer: release memory allocated in @get_config.
327 * @update_buffer: update buffer pointers after a trace session.
329 struct coresight_ops_sink {
330 int (*enable)(struct coresight_device *csdev, enum cs_mode mode,
332 int (*disable)(struct coresight_device *csdev);
333 void *(*alloc_buffer)(struct coresight_device *csdev,
334 struct perf_event *event, void **pages,
335 int nr_pages, bool overwrite);
336 void (*free_buffer)(void *config);
337 unsigned long (*update_buffer)(struct coresight_device *csdev,
338 struct perf_output_handle *handle,
343 * struct coresight_ops_link - basic operations for a link
344 * Operations available for links.
345 * @enable: enables flow between iport and oport.
346 * @disable: disables flow between iport and oport.
348 struct coresight_ops_link {
349 int (*enable)(struct coresight_device *csdev,
350 struct coresight_connection *in,
351 struct coresight_connection *out);
352 void (*disable)(struct coresight_device *csdev,
353 struct coresight_connection *in,
354 struct coresight_connection *out);
358 * struct coresight_ops_source - basic operations for a source
359 * Operations available for sources.
360 * @cpu_id: returns the value of the CPU number this component
362 * @enable: enables tracing for a source.
363 * @disable: disables tracing for a source.
365 struct coresight_ops_source {
366 int (*cpu_id)(struct coresight_device *csdev);
367 int (*enable)(struct coresight_device *csdev, struct perf_event *event,
369 void (*disable)(struct coresight_device *csdev,
370 struct perf_event *event);
374 * struct coresight_ops_helper - Operations for a helper device.
376 * All operations could pass in a device specific data, which could
377 * help the helper device to determine what to do.
379 * @enable : Enable the device
380 * @disable : Disable the device
382 struct coresight_ops_helper {
383 int (*enable)(struct coresight_device *csdev, enum cs_mode mode,
385 int (*disable)(struct coresight_device *csdev, void *data);
388 struct coresight_ops {
389 const struct coresight_ops_sink *sink_ops;
390 const struct coresight_ops_link *link_ops;
391 const struct coresight_ops_source *source_ops;
392 const struct coresight_ops_helper *helper_ops;
395 static inline u32 csdev_access_relaxed_read32(struct csdev_access *csa,
398 if (likely(csa->io_mem))
399 return readl_relaxed(csa->base + offset);
401 return csa->read(offset, true, false);
404 #define CORESIGHT_CIDRn(i) (0xFF0 + ((i) * 4))
406 static inline u32 coresight_get_cid(void __iomem *base)
410 for (i = 0; i < 4; i++)
411 cid |= readl(base + CORESIGHT_CIDRn(i)) << (i * 8);
416 static inline bool is_coresight_device(void __iomem *base)
418 u32 cid = coresight_get_cid(base);
420 return cid == CORESIGHT_CID;
424 * Attempt to find and enable "APB clock" for the given device
428 * clk - Clock is found and enabled
429 * NULL - clock is not found
430 * ERROR - Clock is found but failed to enable
432 static inline struct clk *coresight_get_enable_apb_pclk(struct device *dev)
437 pclk = clk_get(dev, "apb_pclk");
441 ret = clk_prepare_enable(pclk);
449 #define CORESIGHT_PIDRn(i) (0xFE0 + ((i) * 4))
451 static inline u32 coresight_get_pid(struct csdev_access *csa)
455 for (i = 0; i < 4; i++)
456 pid |= csdev_access_relaxed_read32(csa, CORESIGHT_PIDRn(i)) << (i * 8);
461 static inline u64 csdev_access_relaxed_read_pair(struct csdev_access *csa,
462 u32 lo_offset, u32 hi_offset)
464 if (likely(csa->io_mem)) {
465 return readl_relaxed(csa->base + lo_offset) |
466 ((u64)readl_relaxed(csa->base + hi_offset) << 32);
469 return csa->read(lo_offset, true, false) | (csa->read(hi_offset, true, false) << 32);
472 static inline void csdev_access_relaxed_write_pair(struct csdev_access *csa, u64 val,
473 u32 lo_offset, u32 hi_offset)
475 if (likely(csa->io_mem)) {
476 writel_relaxed((u32)val, csa->base + lo_offset);
477 writel_relaxed((u32)(val >> 32), csa->base + hi_offset);
479 csa->write((u32)val, lo_offset, true, false);
480 csa->write((u32)(val >> 32), hi_offset, true, false);
484 static inline u32 csdev_access_read32(struct csdev_access *csa, u32 offset)
486 if (likely(csa->io_mem))
487 return readl(csa->base + offset);
489 return csa->read(offset, false, false);
492 static inline void csdev_access_relaxed_write32(struct csdev_access *csa,
495 if (likely(csa->io_mem))
496 writel_relaxed(val, csa->base + offset);
498 csa->write(val, offset, true, false);
501 static inline void csdev_access_write32(struct csdev_access *csa, u32 val, u32 offset)
503 if (likely(csa->io_mem))
504 writel(val, csa->base + offset);
506 csa->write(val, offset, false, false);
511 static inline u64 csdev_access_relaxed_read64(struct csdev_access *csa,
514 if (likely(csa->io_mem))
515 return readq_relaxed(csa->base + offset);
517 return csa->read(offset, true, true);
520 static inline u64 csdev_access_read64(struct csdev_access *csa, u32 offset)
522 if (likely(csa->io_mem))
523 return readq(csa->base + offset);
525 return csa->read(offset, false, true);
528 static inline void csdev_access_relaxed_write64(struct csdev_access *csa,
531 if (likely(csa->io_mem))
532 writeq_relaxed(val, csa->base + offset);
534 csa->write(val, offset, true, true);
537 static inline void csdev_access_write64(struct csdev_access *csa, u64 val, u32 offset)
539 if (likely(csa->io_mem))
540 writeq(val, csa->base + offset);
542 csa->write(val, offset, false, true);
545 #else /* !CONFIG_64BIT */
547 static inline u64 csdev_access_relaxed_read64(struct csdev_access *csa,
554 static inline u64 csdev_access_read64(struct csdev_access *csa, u32 offset)
560 static inline void csdev_access_relaxed_write64(struct csdev_access *csa,
566 static inline void csdev_access_write64(struct csdev_access *csa, u64 val, u32 offset)
570 #endif /* CONFIG_64BIT */
572 static inline bool coresight_is_percpu_source(struct coresight_device *csdev)
574 return csdev && (csdev->type == CORESIGHT_DEV_TYPE_SOURCE) &&
575 (csdev->subtype.source_subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_PROC);
578 static inline bool coresight_is_percpu_sink(struct coresight_device *csdev)
580 return csdev && (csdev->type == CORESIGHT_DEV_TYPE_SINK) &&
581 (csdev->subtype.sink_subtype == CORESIGHT_DEV_SUBTYPE_SINK_PERCPU_SYSMEM);
585 * Atomically try to take the device and set a new mode. Returns true on
586 * success, false if the device is already taken by someone else.
588 static inline bool coresight_take_mode(struct coresight_device *csdev,
589 enum cs_mode new_mode)
591 return local_cmpxchg(&csdev->mode, CS_MODE_DISABLED, new_mode) ==
595 static inline enum cs_mode coresight_get_mode(struct coresight_device *csdev)
597 return local_read(&csdev->mode);
600 static inline void coresight_set_mode(struct coresight_device *csdev,
601 enum cs_mode new_mode)
603 enum cs_mode current_mode = coresight_get_mode(csdev);
606 * Changing to a new mode must be done from an already disabled state
607 * unless it's synchronized with coresight_take_mode(). Otherwise the
608 * device is already in use and signifies a locking issue.
610 WARN(new_mode != CS_MODE_DISABLED && current_mode != CS_MODE_DISABLED &&
611 current_mode != new_mode, "Device already in use\n");
613 local_set(&csdev->mode, new_mode);
616 extern struct coresight_device *
617 coresight_register(struct coresight_desc *desc);
618 extern void coresight_unregister(struct coresight_device *csdev);
619 extern int coresight_enable_sysfs(struct coresight_device *csdev);
620 extern void coresight_disable_sysfs(struct coresight_device *csdev);
621 extern int coresight_timeout(struct csdev_access *csa, u32 offset,
622 int position, int value);
624 extern int coresight_claim_device(struct coresight_device *csdev);
625 extern int coresight_claim_device_unlocked(struct coresight_device *csdev);
627 extern void coresight_disclaim_device(struct coresight_device *csdev);
628 extern void coresight_disclaim_device_unlocked(struct coresight_device *csdev);
629 extern char *coresight_alloc_device_name(struct coresight_dev_list *devs,
632 extern bool coresight_loses_context_with_cpu(struct device *dev);
634 u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset);
635 u32 coresight_read32(struct coresight_device *csdev, u32 offset);
636 void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset);
637 void coresight_relaxed_write32(struct coresight_device *csdev,
638 u32 val, u32 offset);
639 u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset);
640 u64 coresight_read64(struct coresight_device *csdev, u32 offset);
641 void coresight_relaxed_write64(struct coresight_device *csdev,
642 u64 val, u32 offset);
643 void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset);
645 extern int coresight_get_cpu(struct device *dev);
647 struct coresight_platform_data *coresight_get_platform_data(struct device *dev);
648 struct coresight_connection *
649 coresight_add_out_conn(struct device *dev,
650 struct coresight_platform_data *pdata,
651 const struct coresight_connection *new_conn);
652 int coresight_add_in_conn(struct coresight_connection *conn);
653 struct coresight_device *
654 coresight_find_input_type(struct coresight_platform_data *pdata,
655 enum coresight_dev_type type,
656 union coresight_dev_subtype subtype);
657 struct coresight_device *
658 coresight_find_output_type(struct coresight_platform_data *pdata,
659 enum coresight_dev_type type,
660 union coresight_dev_subtype subtype);
662 int coresight_init_driver(const char *drv, struct amba_driver *amba_drv,
663 struct platform_driver *pdev_drv);
665 void coresight_remove_driver(struct amba_driver *amba_drv,
666 struct platform_driver *pdev_drv);
667 #endif /* _LINUX_COREISGHT_H */