blob: 66959557cf3989377aa241c3c4e6f2dac2728597 [file] [log] [blame]
Mathieu Poirierad0dfdf2018-05-09 12:06:04 -06001/* SPDX-License-Identifier: GPL-2.0 */
Mathieu Poirier4c324b52016-05-03 11:33:48 -06002/*
3 * Copyright(C) 2015 Linaro Limited. All rights reserved.
4 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
Mathieu Poirier4c324b52016-05-03 11:33:48 -06005 */
6
7#ifndef _CORESIGHT_TMC_H
8#define _CORESIGHT_TMC_H
9
Suzuki K Poulose99443ea2018-07-11 13:40:20 -060010#include <linux/dma-mapping.h>
Mathieu Poirierc5ff73442019-04-25 13:53:06 -060011#include <linux/idr.h>
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060012#include <linux/miscdevice.h>
Mathieu Poirierc5ff73442019-04-25 13:53:06 -060013#include <linux/mutex.h>
Mathieu Poirier57549992019-04-25 13:53:05 -060014#include <linux/refcount.h>
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -060015
Mathieu Poirier4c324b52016-05-03 11:33:48 -060016#define TMC_RSZ 0x004
17#define TMC_STS 0x00c
18#define TMC_RRD 0x010
19#define TMC_RRP 0x014
20#define TMC_RWP 0x018
21#define TMC_TRG 0x01c
22#define TMC_CTL 0x020
23#define TMC_RWD 0x024
24#define TMC_MODE 0x028
25#define TMC_LBUFLEVEL 0x02c
26#define TMC_CBUFLEVEL 0x030
27#define TMC_BUFWM 0x034
28#define TMC_RRPHI 0x038
29#define TMC_RWPHI 0x03c
30#define TMC_AXICTL 0x110
31#define TMC_DBALO 0x118
32#define TMC_DBAHI 0x11c
33#define TMC_FFSR 0x300
34#define TMC_FFCR 0x304
35#define TMC_PSCR 0x308
36#define TMC_ITMISCOP0 0xee0
37#define TMC_ITTRFLIN 0xee8
38#define TMC_ITATBDATA0 0xeec
39#define TMC_ITATBCTR2 0xef0
40#define TMC_ITATBCTR1 0xef4
41#define TMC_ITATBCTR0 0xef8
Suzuki K Poulose8a4bc4f2019-08-29 14:28:31 -060042#define TMC_AUTHSTATUS 0xfb8
Mathieu Poirier4c324b52016-05-03 11:33:48 -060043
44/* register description */
45/* TMC_CTL - 0x020 */
46#define TMC_CTL_CAPT_EN BIT(0)
47/* TMC_STS - 0x00C */
Mathieu Poiriera8ab42682016-05-03 11:33:49 -060048#define TMC_STS_TMCREADY_BIT 2
Mathieu Poirier2e499bb2016-05-03 11:33:59 -060049#define TMC_STS_FULL BIT(0)
Mathieu Poirier4c324b52016-05-03 11:33:48 -060050#define TMC_STS_TRIGGERED BIT(1)
Suzuki K Poulosef52ff9b2019-08-29 14:28:30 -060051#define TMC_STS_MEMERR BIT(5)
Suzuki K Poulosecd407ab2017-08-02 10:22:14 -060052/*
53 * TMC_AXICTL - 0x110
54 *
55 * TMC AXICTL format for SoC-400
56 * Bits [0-1] : ProtCtrlBit0-1
Suzuki K Pouloseebab6a72017-08-02 10:22:15 -060057 * Bits [2-5] : CacheCtrlBits 0-3 (AXCACHE)
Suzuki K Poulosecd407ab2017-08-02 10:22:14 -060058 * Bit 6 : Reserved
59 * Bit 7 : ScatterGatherMode
60 * Bits [8-11] : WrBurstLen
61 * Bits [12-31] : Reserved.
Suzuki K Pouloseebab6a72017-08-02 10:22:15 -060062 * TMC AXICTL format for SoC-600, as above except:
63 * Bits [2-5] : AXI WCACHE
64 * Bits [16-19] : AXI RCACHE
65 * Bits [20-31] : Reserved
Suzuki K Poulosecd407ab2017-08-02 10:22:14 -060066 */
67#define TMC_AXICTL_CLEAR_MASK 0xfbf
Suzuki K Pouloseebab6a72017-08-02 10:22:15 -060068#define TMC_AXICTL_ARCACHE_MASK (0xf << 16)
Suzuki K Poulosecd407ab2017-08-02 10:22:14 -060069
Mathieu Poirier4c324b52016-05-03 11:33:48 -060070#define TMC_AXICTL_PROT_CTL_B0 BIT(0)
71#define TMC_AXICTL_PROT_CTL_B1 BIT(1)
72#define TMC_AXICTL_SCT_GAT_MODE BIT(7)
Tanmay Jagdale4d5d88b2021-09-01 18:40:49 +053073#define TMC_AXICTL_WR_BURST(v) (((v) & 0xf) << 8)
74#define TMC_AXICTL_WR_BURST_16 0xf
Suzuki K Poulosecd407ab2017-08-02 10:22:14 -060075/* Write-back Read and Write-allocate */
76#define TMC_AXICTL_AXCACHE_OS (0xf << 2)
Suzuki K Pouloseebab6a72017-08-02 10:22:15 -060077#define TMC_AXICTL_ARCACHE_OS (0xf << 16)
Suzuki K Poulosecd407ab2017-08-02 10:22:14 -060078
Mathieu Poirier4c324b52016-05-03 11:33:48 -060079/* TMC_FFCR - 0x304 */
Mathieu Poiriera8ab42682016-05-03 11:33:49 -060080#define TMC_FFCR_FLUSHMAN_BIT 6
Mathieu Poirier4c324b52016-05-03 11:33:48 -060081#define TMC_FFCR_EN_FMT BIT(0)
82#define TMC_FFCR_EN_TI BIT(1)
83#define TMC_FFCR_FON_FLIN BIT(4)
84#define TMC_FFCR_FON_TRIG_EVT BIT(5)
Mathieu Poirier4c324b52016-05-03 11:33:48 -060085#define TMC_FFCR_TRIGON_TRIGIN BIT(8)
86#define TMC_FFCR_STOP_ON_FLUSH BIT(12)
87
Mathieu Poirier4c324b52016-05-03 11:33:48 -060088
Suzuki K Poulose2e219342017-08-02 10:22:12 -060089#define TMC_DEVID_NOSCAT BIT(24)
90
Suzuki K Pouloseff11f5b2017-08-02 10:22:13 -060091#define TMC_DEVID_AXIAW_VALID BIT(16)
92#define TMC_DEVID_AXIAW_SHIFT 17
93#define TMC_DEVID_AXIAW_MASK 0x7f
94
Suzuki K Poulose8a4bc4f2019-08-29 14:28:31 -060095#define TMC_AUTH_NSID_MASK GENMASK(1, 0)
96
Mathieu Poirier4c324b52016-05-03 11:33:48 -060097enum tmc_config_type {
98 TMC_CONFIG_TYPE_ETB,
99 TMC_CONFIG_TYPE_ETR,
100 TMC_CONFIG_TYPE_ETF,
101};
102
103enum tmc_mode {
104 TMC_MODE_CIRCULAR_BUFFER,
105 TMC_MODE_SOFTWARE_FIFO,
106 TMC_MODE_HARDWARE_FIFO,
107};
108
109enum tmc_mem_intf_width {
Mathieu Poirier4f1ff3d2016-05-03 11:33:57 -0600110 TMC_MEM_INTF_WIDTH_32BITS = 1,
111 TMC_MEM_INTF_WIDTH_64BITS = 2,
112 TMC_MEM_INTF_WIDTH_128BITS = 4,
113 TMC_MEM_INTF_WIDTH_256BITS = 8,
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600114};
115
Suzuki K Poulose2e219342017-08-02 10:22:12 -0600116/* TMC ETR Capability bit definitions */
117#define TMC_ETR_SG (0x1U << 0)
Suzuki K Pouloseebab6a72017-08-02 10:22:15 -0600118/* ETR has separate read/write cache encodings */
119#define TMC_ETR_AXI_ARCACHE (0x1U << 1)
Suzuki K Poulosef2e931a2017-08-02 10:22:16 -0600120/*
121 * TMC_ETR_SAVE_RESTORE - Values of RRP/RWP/STS.Full are
122 * retained when TMC leaves Disabled state, allowing us to continue
123 * the tracing from a point where we stopped. This also implies that
124 * the RRP/RWP/STS.Full should always be programmed to the correct
125 * value. Unfortunately this is not advertised by the hardware,
126 * so we have to rely on PID of the IP to detect the functionality.
127 */
128#define TMC_ETR_SAVE_RESTORE (0x1U << 2)
Suzuki K Poulose2e219342017-08-02 10:22:12 -0600129
Suzuki K Poulose64958922017-08-02 10:22:17 -0600130/* Coresight SoC-600 TMC-ETR unadvertised capabilities */
131#define CORESIGHT_SOC_600_ETR_CAPS \
132 (TMC_ETR_SAVE_RESTORE | TMC_ETR_AXI_ARCACHE)
133
Suzuki K Poulose75f4e362018-07-11 13:40:22 -0600134enum etr_mode {
135 ETR_MODE_FLAT, /* Uses contiguous flat buffer */
Suzuki K Poulosee8e3b772018-07-11 13:40:23 -0600136 ETR_MODE_ETR_SG, /* Uses in-built TMC ETR SG mechanism */
Suzuki K Poulose434d6112018-07-11 13:40:34 -0600137 ETR_MODE_CATU, /* Use SG mechanism in CATU */
Suzuki K Poulose75f4e362018-07-11 13:40:22 -0600138};
139
140struct etr_buf_operations;
141
142/**
143 * struct etr_buf - Details of the buffer used by ETR
Mathieu Poirier57549992019-04-25 13:53:05 -0600144 * refcount ; Number of sources currently using this etr_buf.
Suzuki K Poulose75f4e362018-07-11 13:40:22 -0600145 * @mode : Mode of the ETR buffer, contiguous, Scatter Gather etc.
146 * @full : Trace data overflow
147 * @size : Size of the buffer.
148 * @hwaddr : Address to be programmed in the TMC:DBA{LO,HI}
149 * @offset : Offset of the trace data in the buffer for consumption.
150 * @len : Available trace data @buf (may round up to the beginning).
151 * @ops : ETR buffer operations for the mode.
152 * @private : Backend specific information for the buf
153 */
154struct etr_buf {
Mathieu Poirier57549992019-04-25 13:53:05 -0600155 refcount_t refcount;
Suzuki K Poulose75f4e362018-07-11 13:40:22 -0600156 enum etr_mode mode;
157 bool full;
158 ssize_t size;
159 dma_addr_t hwaddr;
160 unsigned long offset;
161 s64 len;
162 const struct etr_buf_operations *ops;
163 void *private;
164};
165
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600166/**
167 * struct tmc_drvdata - specifics associated to an TMC component
168 * @base: memory mapped base address for this component.
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600169 * @csdev: component vitals needed by the framework.
170 * @miscdev: specifics to handle "/dev/xyz.tmc" entry.
171 * @spinlock: only one at a time pls.
Mathieu Poirier8d03cfd2019-04-25 13:53:08 -0600172 * @pid: Process ID of the process being monitored by the session
173 * that is using this component.
Suzuki K Poulose75f4e362018-07-11 13:40:22 -0600174 * @buf: Snapshot of the trace data for ETF/ETB.
175 * @etr_buf: details of buffer used in TMC-ETR
176 * @len: size of the available trace for ETF/ETB.
177 * @size: trace buffer size for this TMC (common for all modes).
Tanmay Jagdale4d5d88b2021-09-01 18:40:49 +0530178 * @max_burst_size: The maximum burst size that can be initiated by
179 * TMC-ETR on AXI bus.
Mathieu Poirierf2facc32016-05-03 11:33:54 -0600180 * @mode: how this TMC is being used.
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600181 * @config_type: TMC variant, must be of type @tmc_config_type.
Mathieu Poirier4f1ff3d2016-05-03 11:33:57 -0600182 * @memwidth: width of the memory interface databus, in bytes.
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600183 * @trigger_cntr: amount of words to store after a trigger.
Suzuki K Poulose28841322017-08-02 10:22:11 -0600184 * @etr_caps: Bitmask of capabilities of the TMC ETR, inferred from the
185 * device configuration register (DEVID)
Mathieu Poirierc5ff73442019-04-25 13:53:06 -0600186 * @idr: Holds etr_bufs allocated for this ETR.
187 * @idr_mutex: Access serialisation for idr.
Yabin Cuibbedcb92019-08-29 14:28:39 -0600188 * @sysfs_buf: SYSFS buffer for ETR.
189 * @perf_buf: PERF buffer for ETR.
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600190 */
191struct tmc_drvdata {
192 void __iomem *base;
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600193 struct coresight_device *csdev;
194 struct miscdevice miscdev;
195 spinlock_t spinlock;
Mathieu Poirier8d03cfd2019-04-25 13:53:08 -0600196 pid_t pid;
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600197 bool reading;
Suzuki K Poulose75f4e362018-07-11 13:40:22 -0600198 union {
199 char *buf; /* TMC ETB */
200 struct etr_buf *etr_buf; /* TMC ETR */
201 };
Suzuki K Poulose8505fea2016-08-25 15:18:57 -0600202 u32 len;
Suzuki K Poulose75f4e362018-07-11 13:40:22 -0600203 u32 size;
Tanmay Jagdale4d5d88b2021-09-01 18:40:49 +0530204 u32 max_burst_size;
Suzuki K. Poulose297ab902016-11-29 09:47:15 -0700205 u32 mode;
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600206 enum tmc_config_type config_type;
Mathieu Poirier4f1ff3d2016-05-03 11:33:57 -0600207 enum tmc_mem_intf_width memwidth;
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600208 u32 trigger_cntr;
Suzuki K Poulose28841322017-08-02 10:22:11 -0600209 u32 etr_caps;
Mathieu Poirierc5ff73442019-04-25 13:53:06 -0600210 struct idr idr;
211 struct mutex idr_mutex;
Suzuki K Poulose96a7f642018-09-20 13:17:51 -0600212 struct etr_buf *sysfs_buf;
Yabin Cuibbedcb92019-08-29 14:28:39 -0600213 struct etr_buf *perf_buf;
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600214};
215
Suzuki K Poulose75f4e362018-07-11 13:40:22 -0600216struct etr_buf_operations {
217 int (*alloc)(struct tmc_drvdata *drvdata, struct etr_buf *etr_buf,
218 int node, void **pages);
219 void (*sync)(struct etr_buf *etr_buf, u64 rrp, u64 rwp);
220 ssize_t (*get_data)(struct etr_buf *etr_buf, u64 offset, size_t len,
221 char **bufpp);
222 void (*free)(struct etr_buf *etr_buf);
223};
224
Suzuki K Poulose99443ea2018-07-11 13:40:20 -0600225/**
226 * struct tmc_pages - Collection of pages used for SG.
227 * @nr_pages: Number of pages in the list.
228 * @daddrs: Array of DMA'able page address.
229 * @pages: Array pages for the buffer.
230 */
231struct tmc_pages {
232 int nr_pages;
233 dma_addr_t *daddrs;
234 struct page **pages;
235};
236
237/*
238 * struct tmc_sg_table - Generic SG table for TMC
239 * @dev: Device for DMA allocations
240 * @table_vaddr: Contiguous Virtual address for PageTable
241 * @data_vaddr: Contiguous Virtual address for Data Buffer
242 * @table_daddr: DMA address of the PageTable base
243 * @node: Node for Page allocations
244 * @table_pages: List of pages & dma address for Table
245 * @data_pages: List of pages & dma address for Data
246 */
247struct tmc_sg_table {
248 struct device *dev;
249 void *table_vaddr;
250 void *data_vaddr;
251 dma_addr_t table_daddr;
252 int node;
253 struct tmc_pages table_pages;
254 struct tmc_pages data_pages;
255};
256
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600257/* Generic functions */
258void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata);
259void tmc_flush_and_stop(struct tmc_drvdata *drvdata);
260void tmc_enable_hw(struct tmc_drvdata *drvdata);
261void tmc_disable_hw(struct tmc_drvdata *drvdata);
Mathieu Poirier00bb4852019-08-29 14:28:40 -0600262u32 tmc_get_memwidth_mask(struct tmc_drvdata *drvdata);
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600263
264/* ETB/ETF functions */
Mathieu Poirier45254122016-05-03 11:33:51 -0600265int tmc_read_prepare_etb(struct tmc_drvdata *drvdata);
266int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata);
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600267extern const struct coresight_ops tmc_etb_cs_ops;
268extern const struct coresight_ops tmc_etf_cs_ops;
269
Suzuki K Poulose34957222018-07-11 13:40:15 -0600270ssize_t tmc_etb_get_sysfs_trace(struct tmc_drvdata *drvdata,
271 loff_t pos, size_t len, char **bufpp);
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600272/* ETR functions */
Mathieu Poirier45254122016-05-03 11:33:51 -0600273int tmc_read_prepare_etr(struct tmc_drvdata *drvdata);
274int tmc_read_unprepare_etr(struct tmc_drvdata *drvdata);
Sai Prakash Ranjan865d3072020-07-16 11:57:41 -0600275void tmc_etr_disable_hw(struct tmc_drvdata *drvdata);
Mathieu Poirier6c6ed1e2016-05-03 11:33:50 -0600276extern const struct coresight_ops tmc_etr_cs_ops;
Suzuki K Poulose34957222018-07-11 13:40:15 -0600277ssize_t tmc_etr_get_sysfs_trace(struct tmc_drvdata *drvdata,
278 loff_t pos, size_t len, char **bufpp);
Suzuki K Poulose6f6ab4f2017-08-02 10:22:07 -0600279
280
281#define TMC_REG_PAIR(name, lo_off, hi_off) \
282static inline u64 \
283tmc_read_##name(struct tmc_drvdata *drvdata) \
284{ \
James Clark0a981812022-08-30 18:26:13 +0100285 return csdev_access_relaxed_read_pair(&drvdata->csdev->access, lo_off, hi_off); \
Suzuki K Poulose6f6ab4f2017-08-02 10:22:07 -0600286} \
287static inline void \
288tmc_write_##name(struct tmc_drvdata *drvdata, u64 val) \
289{ \
James Clark0a981812022-08-30 18:26:13 +0100290 csdev_access_relaxed_write_pair(&drvdata->csdev->access, val, lo_off, hi_off); \
Suzuki K Poulose6f6ab4f2017-08-02 10:22:07 -0600291}
292
293TMC_REG_PAIR(rrp, TMC_RRP, TMC_RRPHI)
294TMC_REG_PAIR(rwp, TMC_RWP, TMC_RWPHI)
295TMC_REG_PAIR(dba, TMC_DBALO, TMC_DBAHI)
296
Suzuki K Poulose28841322017-08-02 10:22:11 -0600297/* Initialise the caps from unadvertised static capabilities of the device */
298static inline void tmc_etr_init_caps(struct tmc_drvdata *drvdata, u32 dev_caps)
299{
300 WARN_ON(drvdata->etr_caps);
301 drvdata->etr_caps = dev_caps;
302}
303
304static inline void tmc_etr_set_cap(struct tmc_drvdata *drvdata, u32 cap)
305{
306 drvdata->etr_caps |= cap;
307}
308
309static inline bool tmc_etr_has_cap(struct tmc_drvdata *drvdata, u32 cap)
310{
311 return !!(drvdata->etr_caps & cap);
312}
313
Suzuki K Poulose99443ea2018-07-11 13:40:20 -0600314struct tmc_sg_table *tmc_alloc_sg_table(struct device *dev,
315 int node,
316 int nr_tpages,
317 int nr_dpages,
318 void **pages);
319void tmc_free_sg_table(struct tmc_sg_table *sg_table);
320void tmc_sg_table_sync_table(struct tmc_sg_table *sg_table);
321void tmc_sg_table_sync_data_range(struct tmc_sg_table *table,
322 u64 offset, u64 size);
323ssize_t tmc_sg_table_get_data(struct tmc_sg_table *sg_table,
324 u64 offset, size_t len, char **bufpp);
325static inline unsigned long
326tmc_sg_table_buf_size(struct tmc_sg_table *sg_table)
327{
328 return sg_table->data_pages.nr_pages << PAGE_SHIFT;
329}
330
Suzuki K Poulose434d6112018-07-11 13:40:34 -0600331struct coresight_device *tmc_etr_get_catu_device(struct tmc_drvdata *drvdata);
332
Mian Yousaf Kaukab66af4162020-09-28 10:35:10 -0600333void tmc_etr_set_catu_ops(const struct etr_buf_operations *catu);
334void tmc_etr_remove_catu_ops(void);
335
Mathieu Poirier4c324b52016-05-03 11:33:48 -0600336#endif