1ad0dfdfdSMathieu Poirier /* SPDX-License-Identifier: GPL-2.0 */
2ad0dfdfdSMathieu Poirier /*
3ad0dfdfdSMathieu Poirier * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
401081f5aSMathieu Poirier */
501081f5aSMathieu Poirier
601081f5aSMathieu Poirier #ifndef _CORESIGHT_PRIV_H
701081f5aSMathieu Poirier #define _CORESIGHT_PRIV_H
801081f5aSMathieu Poirier
9e85fa28eSMike Leach #include <linux/amba/bus.h>
1001081f5aSMathieu Poirier #include <linux/bitops.h>
1101081f5aSMathieu Poirier #include <linux/io.h>
1201081f5aSMathieu Poirier #include <linux/coresight.h>
13cd9e3474SMathieu Poirier #include <linux/pm_runtime.h>
1401081f5aSMathieu Poirier
1501081f5aSMathieu Poirier /*
1601081f5aSMathieu Poirier * Coresight management registers (0xf00-0xfcc)
1701081f5aSMathieu Poirier * 0xfa0 - 0xfa4: Management registers in PFTv1.0
1801081f5aSMathieu Poirier * Trace registers in PFTv1.1
1901081f5aSMathieu Poirier */
2001081f5aSMathieu Poirier #define CORESIGHT_ITCTRL 0xf00
2101081f5aSMathieu Poirier #define CORESIGHT_CLAIMSET 0xfa0
2201081f5aSMathieu Poirier #define CORESIGHT_CLAIMCLR 0xfa4
2301081f5aSMathieu Poirier #define CORESIGHT_LAR 0xfb0
2401081f5aSMathieu Poirier #define CORESIGHT_LSR 0xfb4
251a556ca6SMike Leach #define CORESIGHT_DEVARCH 0xfbc
2601081f5aSMathieu Poirier #define CORESIGHT_AUTHSTATUS 0xfb8
2701081f5aSMathieu Poirier #define CORESIGHT_DEVID 0xfc8
2801081f5aSMathieu Poirier #define CORESIGHT_DEVTYPE 0xfcc
2901081f5aSMathieu Poirier
302478a6aeSSuzuki K Poulose
312478a6aeSSuzuki K Poulose /*
322478a6aeSSuzuki K Poulose * Coresight device CLAIM protocol.
332478a6aeSSuzuki K Poulose * See PSCI - ARM DEN 0022D, Section: 6.8.1 Debug and Trace save and restore.
342478a6aeSSuzuki K Poulose */
352478a6aeSSuzuki K Poulose #define CORESIGHT_CLAIM_SELF_HOSTED BIT(1)
362478a6aeSSuzuki K Poulose
3701081f5aSMathieu Poirier #define TIMEOUT_US 100
3801081f5aSMathieu Poirier #define BMVAL(val, lsb, msb) ((val & GENMASK(msb, lsb)) >> lsb)
3901081f5aSMathieu Poirier
402127154dSMathieu Poirier #define ETM_MODE_EXCL_KERN BIT(30)
412127154dSMathieu Poirier #define ETM_MODE_EXCL_USER BIT(31)
4208e9fa5fSJames Clark struct cs_pair_attribute {
4308e9fa5fSJames Clark struct device_attribute attr;
440a98181fSJames Clark u32 lo_off;
450a98181fSJames Clark u32 hi_off;
4608e9fa5fSJames Clark };
472127154dSMathieu Poirier
48fbca79e5SJames Clark struct cs_off_attribute {
49fbca79e5SJames Clark struct device_attribute attr;
50fbca79e5SJames Clark u32 off;
51fbca79e5SJames Clark };
52fbca79e5SJames Clark
530a98181fSJames Clark extern ssize_t coresight_simple_show32(struct device *_dev,
540a98181fSJames Clark struct device_attribute *attr, char *buf);
550a98181fSJames Clark extern ssize_t coresight_simple_show_pair(struct device *_dev,
5608e9fa5fSJames Clark struct device_attribute *attr, char *buf);
57154f3520SMathieu Poirier
58b6df1cbbSJames Clark #define coresight_simple_reg32(name, offset) \
590a98181fSJames Clark (&((struct cs_off_attribute[]) { \
6008e9fa5fSJames Clark { \
610a98181fSJames Clark __ATTR(name, 0444, coresight_simple_show32, NULL), \
620a98181fSJames Clark offset \
6308e9fa5fSJames Clark } \
6408e9fa5fSJames Clark })[0].attr.attr)
6508e9fa5fSJames Clark
66b6df1cbbSJames Clark #define coresight_simple_reg64(name, lo_off, hi_off) \
6708e9fa5fSJames Clark (&((struct cs_pair_attribute[]) { \
6808e9fa5fSJames Clark { \
690a98181fSJames Clark __ATTR(name, 0444, coresight_simple_show_pair, NULL), \
7008e9fa5fSJames Clark lo_off, hi_off \
7108e9fa5fSJames Clark } \
7208e9fa5fSJames Clark })[0].attr.attr)
73b4523c87SSuzuki K Poulose
7492fc7d81STingwei Zhang extern const u32 coresight_barrier_pkt[4];
7592fc7d81STingwei Zhang #define CORESIGHT_BARRIER_PKT_SIZE (sizeof(coresight_barrier_pkt))
760c3fc4d5SMathieu Poirier
772b7adc46SMathieu Poirier enum etm_addr_type {
782b7adc46SMathieu Poirier ETM_ADDR_TYPE_NONE,
792b7adc46SMathieu Poirier ETM_ADDR_TYPE_SINGLE,
802b7adc46SMathieu Poirier ETM_ADDR_TYPE_RANGE,
812b7adc46SMathieu Poirier ETM_ADDR_TYPE_START,
822b7adc46SMathieu Poirier ETM_ADDR_TYPE_STOP,
832b7adc46SMathieu Poirier };
842b7adc46SMathieu Poirier
85a02e81f7SMathieu Poirier /**
86a02e81f7SMathieu Poirier * struct cs_buffer - keep track of a recording session' specifics
87a02e81f7SMathieu Poirier * @cur: index of the current buffer
88a02e81f7SMathieu Poirier * @nr_pages: max number of pages granted to us
89868663ddSSai Prakash Ranjan * @pid: PID this cs_buffer belongs to
90a02e81f7SMathieu Poirier * @offset: offset within the current buffer
91a02e81f7SMathieu Poirier * @data_size: how much we collected in this run
92a02e81f7SMathieu Poirier * @snapshot: is this run in snapshot mode
93a02e81f7SMathieu Poirier * @data_pages: a handle the ring buffer
94a02e81f7SMathieu Poirier */
95a02e81f7SMathieu Poirier struct cs_buffers {
96a02e81f7SMathieu Poirier unsigned int cur;
97a02e81f7SMathieu Poirier unsigned int nr_pages;
98868663ddSSai Prakash Ranjan pid_t pid;
99a02e81f7SMathieu Poirier unsigned long offset;
100a02e81f7SMathieu Poirier local_t data_size;
101a02e81f7SMathieu Poirier bool snapshot;
102a02e81f7SMathieu Poirier void **data_pages;
103a02e81f7SMathieu Poirier };
104a02e81f7SMathieu Poirier
coresight_insert_barrier_packet(void * buf)1056f755e85SSuzuki K Poulose static inline void coresight_insert_barrier_packet(void *buf)
1066f755e85SSuzuki K Poulose {
1076f755e85SSuzuki K Poulose if (buf)
10892fc7d81STingwei Zhang memcpy(buf, coresight_barrier_pkt, CORESIGHT_BARRIER_PKT_SIZE);
1096f755e85SSuzuki K Poulose }
1106f755e85SSuzuki K Poulose
CS_LOCK(void __iomem * addr)11101081f5aSMathieu Poirier static inline void CS_LOCK(void __iomem *addr)
11201081f5aSMathieu Poirier {
11301081f5aSMathieu Poirier do {
11401081f5aSMathieu Poirier /* Wait for things to settle */
11501081f5aSMathieu Poirier mb();
11601081f5aSMathieu Poirier writel_relaxed(0x0, addr + CORESIGHT_LAR);
11701081f5aSMathieu Poirier } while (0);
11801081f5aSMathieu Poirier }
11901081f5aSMathieu Poirier
CS_UNLOCK(void __iomem * addr)12001081f5aSMathieu Poirier static inline void CS_UNLOCK(void __iomem *addr)
12101081f5aSMathieu Poirier {
12201081f5aSMathieu Poirier do {
12301081f5aSMathieu Poirier writel_relaxed(CORESIGHT_UNLOCK, addr + CORESIGHT_LAR);
12401081f5aSMathieu Poirier /* Make sure everyone has seen this */
12501081f5aSMathieu Poirier mb();
12601081f5aSMathieu Poirier } while (0);
12701081f5aSMathieu Poirier }
12801081f5aSMathieu Poirier
129b3e94405SMathieu Poirier void coresight_disable_path(struct list_head *path);
1309fa36828SJames Clark int coresight_enable_path(struct list_head *path, enum cs_mode mode,
1319fa36828SJames Clark void *sink_data);
132b6404e21SMathieu Poirier struct coresight_device *coresight_get_sink(struct list_head *path);
1336d578258SLinu Cherian struct coresight_device *
1346d578258SLinu Cherian coresight_get_enabled_sink(struct coresight_device *source);
13522644392SMathieu Poirier struct coresight_device *coresight_get_sink_by_id(u32 id);
1360336bdfdSMike Leach struct coresight_device *
1370336bdfdSMike Leach coresight_find_default_sink(struct coresight_device *csdev);
138d52c9750SMathieu Poirier struct list_head *coresight_build_path(struct coresight_device *csdev,
139d52c9750SMathieu Poirier struct coresight_device *sink);
140b3e94405SMathieu Poirier void coresight_release_path(struct list_head *path);
14180961525SMike Leach int coresight_add_sysfs_link(struct coresight_sysfs_link *info);
14280961525SMike Leach void coresight_remove_sysfs_link(struct coresight_sysfs_link *info);
14380961525SMike Leach int coresight_create_conns_sysfs_group(struct coresight_device *csdev);
14480961525SMike Leach void coresight_remove_conns_sysfs_group(struct coresight_device *csdev);
1458a7365c2SSuzuki K Poulose int coresight_make_links(struct coresight_device *orig,
1468a7365c2SSuzuki K Poulose struct coresight_connection *conn,
1478a7365c2SSuzuki K Poulose struct coresight_device *target);
1488a7365c2SSuzuki K Poulose void coresight_remove_links(struct coresight_device *orig,
1498a7365c2SSuzuki K Poulose struct coresight_connection *conn);
150b3e94405SMathieu Poirier
151b8127113SKim Phillips #if IS_ENABLED(CONFIG_CORESIGHT_SOURCE_ETM3X)
15201081f5aSMathieu Poirier extern int etm_readl_cp14(u32 off, unsigned int *val);
15301081f5aSMathieu Poirier extern int etm_writel_cp14(u32 off, u32 val);
15401081f5aSMathieu Poirier #else
etm_readl_cp14(u32 off,unsigned int * val)15501081f5aSMathieu Poirier static inline int etm_readl_cp14(u32 off, unsigned int *val) { return 0; }
etm_writel_cp14(u32 off,u32 val)15601081f5aSMathieu Poirier static inline int etm_writel_cp14(u32 off, u32 val) { return 0; }
15701081f5aSMathieu Poirier #endif
15801081f5aSMathieu Poirier
1597b0fc5d2STingwei Zhang struct cti_assoc_op {
1607b0fc5d2STingwei Zhang void (*add)(struct coresight_device *csdev);
1617b0fc5d2STingwei Zhang void (*remove)(struct coresight_device *csdev);
1627b0fc5d2STingwei Zhang };
163177af828SMike Leach
1647b0fc5d2STingwei Zhang extern void coresight_set_cti_ops(const struct cti_assoc_op *cti_op);
1657b0fc5d2STingwei Zhang extern void coresight_remove_cti_ops(void);
166177af828SMike Leach
167e85fa28eSMike Leach /*
168e85fa28eSMike Leach * Macros and inline functions to handle CoreSight UCI data and driver
169e85fa28eSMike Leach * private data in AMBA ID table entries, and extract data values.
170e85fa28eSMike Leach */
171e85fa28eSMike Leach
172e85fa28eSMike Leach /* coresight AMBA ID, no UCI, no driver data: id table entry */
173e85fa28eSMike Leach #define CS_AMBA_ID(pid) \
174e85fa28eSMike Leach { \
175e85fa28eSMike Leach .id = pid, \
176e85fa28eSMike Leach .mask = 0x000fffff, \
177e85fa28eSMike Leach }
178e85fa28eSMike Leach
179e85fa28eSMike Leach /* coresight AMBA ID, UCI with driver data only: id table entry. */
180e85fa28eSMike Leach #define CS_AMBA_ID_DATA(pid, dval) \
181e85fa28eSMike Leach { \
182e85fa28eSMike Leach .id = pid, \
183e85fa28eSMike Leach .mask = 0x000fffff, \
184e85fa28eSMike Leach .data = (void *)&(struct amba_cs_uci_id) \
185e85fa28eSMike Leach { \
186e85fa28eSMike Leach .data = (void *)dval, \
187e85fa28eSMike Leach } \
188e85fa28eSMike Leach }
189e85fa28eSMike Leach
19028941701SMike Leach /* coresight AMBA ID, full UCI structure: id table entry. */
191ab5ca626SSuzuki K Poulose #define __CS_AMBA_UCI_ID(pid, m, uci_ptr) \
19228941701SMike Leach { \
19328941701SMike Leach .id = pid, \
194ab5ca626SSuzuki K Poulose .mask = m, \
195df4473faSSai Prakash Ranjan .data = (void *)uci_ptr \
19628941701SMike Leach }
197ab5ca626SSuzuki K Poulose #define CS_AMBA_UCI_ID(pid, uci) __CS_AMBA_UCI_ID(pid, 0x000fffff, uci)
198ab5ca626SSuzuki K Poulose /*
199ab5ca626SSuzuki K Poulose * PIDR2[JEDEC], BIT(3) must be 1 (Read As One) to indicate that rest of the
200ab5ca626SSuzuki K Poulose * PIDR1, PIDR2 DES_* fields follow JEDEC encoding for the designer. Use that
201ab5ca626SSuzuki K Poulose * as a match value for blanket matching all devices in the given CoreSight
202ab5ca626SSuzuki K Poulose * device type and architecture.
203ab5ca626SSuzuki K Poulose */
204ab5ca626SSuzuki K Poulose #define PIDR2_JEDEC BIT(3)
205ab5ca626SSuzuki K Poulose #define PID_PIDR2_JEDEC (PIDR2_JEDEC << 16)
206ab5ca626SSuzuki K Poulose /*
207ab5ca626SSuzuki K Poulose * Match all PIDs in a given CoreSight device type and architecture, defined
208ab5ca626SSuzuki K Poulose * by the uci.
209ab5ca626SSuzuki K Poulose */
210ab5ca626SSuzuki K Poulose #define CS_AMBA_MATCH_ALL_UCI(uci) \
211ab5ca626SSuzuki K Poulose __CS_AMBA_UCI_ID(PID_PIDR2_JEDEC, PID_PIDR2_JEDEC, uci)
21228941701SMike Leach
213e85fa28eSMike Leach /* extract the data value from a UCI structure given amba_id pointer. */
coresight_get_uci_data(const struct amba_id * id)214e85fa28eSMike Leach static inline void *coresight_get_uci_data(const struct amba_id *id)
215e85fa28eSMike Leach {
2169712c092SStephen Boyd struct amba_cs_uci_id *uci_id = id->data;
2179712c092SStephen Boyd
2189712c092SStephen Boyd if (!uci_id)
2199712c092SStephen Boyd return NULL;
2209712c092SStephen Boyd
2219712c092SStephen Boyd return uci_id->data;
222e85fa28eSMike Leach }
223e85fa28eSMike Leach
224d60250a4SSuzuki K Poulose void coresight_release_platform_data(struct coresight_device *csdev,
2254e8fe7e5SJames Clark struct device *dev,
226d60250a4SSuzuki K Poulose struct coresight_platform_data *pdata);
227cffd054fSMike Leach struct coresight_device *
228cffd054fSMike Leach coresight_find_csdev_by_fwnode(struct fwnode_handle *r_fwnode);
2291b5b1646SJames Clark void coresight_add_helper(struct coresight_device *csdev,
2301b5b1646SJames Clark struct coresight_device *helper);
23137ea1ffdSSuzuki K Poulose
2322cd87a7bSAnshuman Khandual void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev);
2332cd87a7bSAnshuman Khandual struct coresight_device *coresight_get_percpu_sink(int cpu);
23461486528SJames Clark int coresight_enable_source(struct coresight_device *csdev, enum cs_mode mode,
23561486528SJames Clark void *data);
236*b7bcc2c2SJames Clark void coresight_disable_source(struct coresight_device *csdev, void *data);
2372cd87a7bSAnshuman Khandual
23801081f5aSMathieu Poirier #endif
239