1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2016 Linaro Limited. All rights reserved.
4 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
7 #include <linux/atomic.h>
8 #include <linux/circ_buf.h>
9 #include <linux/coresight.h>
10 #include <linux/perf_event.h>
11 #include <linux/slab.h>
12 #include "coresight-priv.h"
13 #include "coresight-tmc.h"
14 #include "coresight-etm-perf.h"
16 static int tmc_set_etf_buffer(struct coresight_device
*csdev
,
17 struct perf_output_handle
*handle
);
19 static int __tmc_etb_enable_hw(struct tmc_drvdata
*drvdata
)
23 CS_UNLOCK(drvdata
->base
);
25 /* Wait for TMCSReady bit to be set */
26 rc
= tmc_wait_for_tmcready(drvdata
);
28 dev_err(&drvdata
->csdev
->dev
,
29 "Failed to enable: TMC not ready\n");
30 CS_LOCK(drvdata
->base
);
34 writel_relaxed(TMC_MODE_CIRCULAR_BUFFER
, drvdata
->base
+ TMC_MODE
);
35 writel_relaxed(TMC_FFCR_EN_FMT
| TMC_FFCR_EN_TI
|
36 TMC_FFCR_FON_FLIN
| TMC_FFCR_FON_TRIG_EVT
|
37 TMC_FFCR_TRIGON_TRIGIN
,
38 drvdata
->base
+ TMC_FFCR
);
40 writel_relaxed(drvdata
->trigger_cntr
, drvdata
->base
+ TMC_TRG
);
41 tmc_enable_hw(drvdata
);
43 CS_LOCK(drvdata
->base
);
47 static int tmc_etb_enable_hw(struct tmc_drvdata
*drvdata
)
49 int rc
= coresight_claim_device(drvdata
->csdev
);
54 rc
= __tmc_etb_enable_hw(drvdata
);
56 coresight_disclaim_device(drvdata
->csdev
);
60 static void tmc_etb_dump_hw(struct tmc_drvdata
*drvdata
)
65 /* Check if the buffer wrapped around. */
66 lost
= readl_relaxed(drvdata
->base
+ TMC_STS
) & TMC_STS_FULL
;
70 read_data
= readl_relaxed(drvdata
->base
+ TMC_RRD
);
71 if (read_data
== 0xFFFFFFFF)
73 memcpy(bufp
, &read_data
, 4);
79 coresight_insert_barrier_packet(drvdata
->buf
);
83 static void __tmc_etb_disable_hw(struct tmc_drvdata
*drvdata
)
85 CS_UNLOCK(drvdata
->base
);
87 tmc_flush_and_stop(drvdata
);
89 * When operating in sysFS mode the content of the buffer needs to be
90 * read before the TMC is disabled.
92 if (coresight_get_mode(drvdata
->csdev
) == CS_MODE_SYSFS
)
93 tmc_etb_dump_hw(drvdata
);
94 tmc_disable_hw(drvdata
);
96 CS_LOCK(drvdata
->base
);
99 static void tmc_etb_disable_hw(struct tmc_drvdata
*drvdata
)
101 __tmc_etb_disable_hw(drvdata
);
102 coresight_disclaim_device(drvdata
->csdev
);
105 static int __tmc_etf_enable_hw(struct tmc_drvdata
*drvdata
)
109 CS_UNLOCK(drvdata
->base
);
111 /* Wait for TMCSReady bit to be set */
112 rc
= tmc_wait_for_tmcready(drvdata
);
114 dev_err(&drvdata
->csdev
->dev
,
115 "Failed to enable : TMC is not ready\n");
116 CS_LOCK(drvdata
->base
);
120 writel_relaxed(TMC_MODE_HARDWARE_FIFO
, drvdata
->base
+ TMC_MODE
);
121 writel_relaxed(TMC_FFCR_EN_FMT
| TMC_FFCR_EN_TI
,
122 drvdata
->base
+ TMC_FFCR
);
123 writel_relaxed(0x0, drvdata
->base
+ TMC_BUFWM
);
124 tmc_enable_hw(drvdata
);
126 CS_LOCK(drvdata
->base
);
130 static int tmc_etf_enable_hw(struct tmc_drvdata
*drvdata
)
132 int rc
= coresight_claim_device(drvdata
->csdev
);
137 rc
= __tmc_etf_enable_hw(drvdata
);
139 coresight_disclaim_device(drvdata
->csdev
);
143 static void tmc_etf_disable_hw(struct tmc_drvdata
*drvdata
)
145 struct coresight_device
*csdev
= drvdata
->csdev
;
147 CS_UNLOCK(drvdata
->base
);
149 tmc_flush_and_stop(drvdata
);
150 tmc_disable_hw(drvdata
);
151 coresight_disclaim_device_unlocked(csdev
);
152 CS_LOCK(drvdata
->base
);
156 * Return the available trace data in the buffer from @pos, with
157 * a maximum limit of @len, updating the @bufpp on where to
160 ssize_t
tmc_etb_get_sysfs_trace(struct tmc_drvdata
*drvdata
,
161 loff_t pos
, size_t len
, char **bufpp
)
163 ssize_t actual
= len
;
165 /* Adjust the len to available size @pos */
166 if (pos
+ actual
> drvdata
->len
)
167 actual
= drvdata
->len
- pos
;
169 *bufpp
= drvdata
->buf
+ pos
;
173 static int tmc_enable_etf_sink_sysfs(struct coresight_device
*csdev
)
179 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
182 * If we don't have a buffer release the lock and allocate memory.
183 * Otherwise keep the lock and move along.
185 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
187 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
189 /* Allocating the memory here while outside of the spinlock */
190 buf
= kzalloc(drvdata
->size
, GFP_KERNEL
);
194 /* Let's try again */
195 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
198 if (drvdata
->reading
) {
204 * In sysFS mode we can have multiple writers per sink. Since this
205 * sink is already enabled no memory is needed and the HW need not be
208 if (coresight_get_mode(csdev
) == CS_MODE_SYSFS
) {
214 * If drvdata::buf isn't NULL, memory was allocated for a previous
215 * trace run but wasn't read. If so simply zero-out the memory.
216 * Otherwise use the memory allocated above.
218 * The memory is freed when users read the buffer using the
219 * /dev/xyz.{etf|etb} interface. See tmc_read_unprepare_etf() for
223 memset(drvdata
->buf
, 0, drvdata
->size
);
229 ret
= tmc_etb_enable_hw(drvdata
);
231 coresight_set_mode(csdev
, CS_MODE_SYSFS
);
234 /* Free up the buffer if we failed to enable */
238 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
240 /* Free memory outside the spinlock if need be */
247 static int tmc_enable_etf_sink_perf(struct coresight_device
*csdev
, void *data
)
252 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
253 struct perf_output_handle
*handle
= data
;
254 struct cs_buffers
*buf
= etm_perf_sink_config(handle
);
256 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
259 if (drvdata
->reading
)
262 * No need to continue if the ETB/ETF is already operated
265 if (coresight_get_mode(csdev
) == CS_MODE_SYSFS
) {
270 /* Get a handle on the pid of the process to monitor */
273 if (drvdata
->pid
!= -1 && drvdata
->pid
!= pid
) {
278 ret
= tmc_set_etf_buffer(csdev
, handle
);
283 * No HW configuration is needed if the sink is already in
284 * use for this session.
286 if (drvdata
->pid
== pid
) {
291 ret
= tmc_etb_enable_hw(drvdata
);
293 /* Associate with monitored process. */
295 coresight_set_mode(csdev
, CS_MODE_PERF
);
299 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
304 static int tmc_enable_etf_sink(struct coresight_device
*csdev
,
305 enum cs_mode mode
, void *data
)
311 ret
= tmc_enable_etf_sink_sysfs(csdev
);
314 ret
= tmc_enable_etf_sink_perf(csdev
, data
);
316 /* We shouldn't be here */
325 dev_dbg(&csdev
->dev
, "TMC-ETB/ETF enabled\n");
329 static int tmc_disable_etf_sink(struct coresight_device
*csdev
)
332 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
334 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
336 if (drvdata
->reading
) {
337 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
343 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
347 /* Complain if we (somehow) got out of sync */
348 WARN_ON_ONCE(coresight_get_mode(csdev
) == CS_MODE_DISABLED
);
349 tmc_etb_disable_hw(drvdata
);
350 /* Dissociate from monitored process. */
352 coresight_set_mode(csdev
, CS_MODE_DISABLED
);
354 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
356 dev_dbg(&csdev
->dev
, "TMC-ETB/ETF disabled\n");
360 static int tmc_enable_etf_link(struct coresight_device
*csdev
,
361 struct coresight_connection
*in
,
362 struct coresight_connection
*out
)
366 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
367 bool first_enable
= false;
369 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
370 if (drvdata
->reading
) {
371 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
375 if (csdev
->refcnt
== 0) {
376 ret
= tmc_etf_enable_hw(drvdata
);
378 coresight_set_mode(csdev
, CS_MODE_SYSFS
);
384 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
387 dev_dbg(&csdev
->dev
, "TMC-ETF enabled\n");
391 static void tmc_disable_etf_link(struct coresight_device
*csdev
,
392 struct coresight_connection
*in
,
393 struct coresight_connection
*out
)
396 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
397 bool last_disable
= false;
399 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
400 if (drvdata
->reading
) {
401 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
406 if (csdev
->refcnt
== 0) {
407 tmc_etf_disable_hw(drvdata
);
408 coresight_set_mode(csdev
, CS_MODE_DISABLED
);
411 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
414 dev_dbg(&csdev
->dev
, "TMC-ETF disabled\n");
417 static void *tmc_alloc_etf_buffer(struct coresight_device
*csdev
,
418 struct perf_event
*event
, void **pages
,
419 int nr_pages
, bool overwrite
)
422 struct cs_buffers
*buf
;
424 node
= (event
->cpu
== -1) ? NUMA_NO_NODE
: cpu_to_node(event
->cpu
);
426 /* Allocate memory structure for interaction with Perf */
427 buf
= kzalloc_node(sizeof(struct cs_buffers
), GFP_KERNEL
, node
);
431 buf
->pid
= task_pid_nr(event
->owner
);
432 buf
->snapshot
= overwrite
;
433 buf
->nr_pages
= nr_pages
;
434 buf
->data_pages
= pages
;
439 static void tmc_free_etf_buffer(void *config
)
441 struct cs_buffers
*buf
= config
;
446 static int tmc_set_etf_buffer(struct coresight_device
*csdev
,
447 struct perf_output_handle
*handle
)
451 struct cs_buffers
*buf
= etm_perf_sink_config(handle
);
456 /* wrap head around to the amount of space we have */
457 head
= handle
->head
& (((unsigned long)buf
->nr_pages
<< PAGE_SHIFT
) - 1);
459 /* find the page to write to */
460 buf
->cur
= head
/ PAGE_SIZE
;
462 /* and offset within that page */
463 buf
->offset
= head
% PAGE_SIZE
;
465 local_set(&buf
->data_size
, 0);
470 static unsigned long tmc_update_etf_buffer(struct coresight_device
*csdev
,
471 struct perf_output_handle
*handle
,
478 u64 read_ptr
, write_ptr
;
480 unsigned long offset
, to_read
= 0, flags
;
481 struct cs_buffers
*buf
= sink_config
;
482 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
487 /* This shouldn't happen */
488 if (WARN_ON_ONCE(coresight_get_mode(csdev
) != CS_MODE_PERF
))
491 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
493 /* Don't do anything if another tracer is using this sink */
494 if (csdev
->refcnt
!= 1)
497 CS_UNLOCK(drvdata
->base
);
499 tmc_flush_and_stop(drvdata
);
501 read_ptr
= tmc_read_rrp(drvdata
);
502 write_ptr
= tmc_read_rwp(drvdata
);
505 * Get a hold of the status register and see if a wrap around
506 * has occurred. If so adjust things accordingly.
508 status
= readl_relaxed(drvdata
->base
+ TMC_STS
);
509 if (status
& TMC_STS_FULL
) {
511 to_read
= drvdata
->size
;
513 to_read
= CIRC_CNT(write_ptr
, read_ptr
, drvdata
->size
);
517 * The TMC RAM buffer may be bigger than the space available in the
518 * perf ring buffer (handle->size). If so advance the RRP so that we
519 * get the latest trace data. In snapshot mode none of that matters
520 * since we are expected to clobber stale data in favour of the latest
523 if (!buf
->snapshot
&& to_read
> handle
->size
) {
524 u32 mask
= tmc_get_memwidth_mask(drvdata
);
527 * Make sure the new size is aligned in accordance with the
528 * requirement explained in function tmc_get_memwidth_mask().
530 to_read
= handle
->size
& mask
;
531 /* Move the RAM read pointer up */
532 read_ptr
= (write_ptr
+ drvdata
->size
) - to_read
;
533 /* Make sure we are still within our limits */
534 if (read_ptr
> (drvdata
->size
- 1))
535 read_ptr
-= drvdata
->size
;
537 tmc_write_rrp(drvdata
, read_ptr
);
542 * Don't set the TRUNCATED flag in snapshot mode because 1) the
543 * captured buffer is expected to be truncated and 2) a full buffer
544 * prevents the event from being re-enabled by the perf core,
545 * resulting in stale data being send to user space.
547 if (!buf
->snapshot
&& lost
)
548 perf_aux_output_flag(handle
, PERF_AUX_FLAG_TRUNCATED
);
551 offset
= buf
->offset
;
552 barrier
= coresight_barrier_pkt
;
554 /* for every byte to read */
555 for (i
= 0; i
< to_read
; i
+= 4) {
556 buf_ptr
= buf
->data_pages
[cur
] + offset
;
557 *buf_ptr
= readl_relaxed(drvdata
->base
+ TMC_RRD
);
559 if (lost
&& i
< CORESIGHT_BARRIER_PKT_SIZE
) {
565 if (offset
>= PAGE_SIZE
) {
568 /* wrap around at the end of the buffer */
569 cur
&= buf
->nr_pages
- 1;
574 * In snapshot mode we simply increment the head by the number of byte
575 * that were written. User space will figure out how many bytes to get
576 * from the AUX buffer based on the position of the head.
579 handle
->head
+= to_read
;
582 * CS_LOCK() contains mb() so it can ensure visibility of the AUX trace
583 * data before the aux_head is updated via perf_aux_output_end(), which
584 * is expected by the perf ring buffer.
586 CS_LOCK(drvdata
->base
);
588 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
593 static const struct coresight_ops_sink tmc_etf_sink_ops
= {
594 .enable
= tmc_enable_etf_sink
,
595 .disable
= tmc_disable_etf_sink
,
596 .alloc_buffer
= tmc_alloc_etf_buffer
,
597 .free_buffer
= tmc_free_etf_buffer
,
598 .update_buffer
= tmc_update_etf_buffer
,
601 static const struct coresight_ops_link tmc_etf_link_ops
= {
602 .enable
= tmc_enable_etf_link
,
603 .disable
= tmc_disable_etf_link
,
606 const struct coresight_ops tmc_etb_cs_ops
= {
607 .sink_ops
= &tmc_etf_sink_ops
,
610 const struct coresight_ops tmc_etf_cs_ops
= {
611 .sink_ops
= &tmc_etf_sink_ops
,
612 .link_ops
= &tmc_etf_link_ops
,
615 int tmc_read_prepare_etb(struct tmc_drvdata
*drvdata
)
621 /* config types are set a boot time and never change */
622 if (WARN_ON_ONCE(drvdata
->config_type
!= TMC_CONFIG_TYPE_ETB
&&
623 drvdata
->config_type
!= TMC_CONFIG_TYPE_ETF
))
626 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
628 if (drvdata
->reading
) {
633 /* Don't interfere if operated from Perf */
634 if (coresight_get_mode(drvdata
->csdev
) == CS_MODE_PERF
) {
639 /* If drvdata::buf is NULL the trace data has been read already */
640 if (drvdata
->buf
== NULL
) {
645 /* Disable the TMC if need be */
646 if (coresight_get_mode(drvdata
->csdev
) == CS_MODE_SYSFS
) {
647 /* There is no point in reading a TMC in HW FIFO mode */
648 mode
= readl_relaxed(drvdata
->base
+ TMC_MODE
);
649 if (mode
!= TMC_MODE_CIRCULAR_BUFFER
) {
653 __tmc_etb_disable_hw(drvdata
);
656 drvdata
->reading
= true;
658 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
663 int tmc_read_unprepare_etb(struct tmc_drvdata
*drvdata
)
670 /* config types are set a boot time and never change */
671 if (WARN_ON_ONCE(drvdata
->config_type
!= TMC_CONFIG_TYPE_ETB
&&
672 drvdata
->config_type
!= TMC_CONFIG_TYPE_ETF
))
675 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
677 /* Re-enable the TMC if need be */
678 if (coresight_get_mode(drvdata
->csdev
) == CS_MODE_SYSFS
) {
679 /* There is no point in reading a TMC in HW FIFO mode */
680 mode
= readl_relaxed(drvdata
->base
+ TMC_MODE
);
681 if (mode
!= TMC_MODE_CIRCULAR_BUFFER
) {
682 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
686 * The trace run will continue with the same allocated trace
687 * buffer. As such zero-out the buffer so that we don't end
688 * up with stale data.
690 * Since the tracer is still enabled drvdata::buf
693 memset(drvdata
->buf
, 0, drvdata
->size
);
694 rc
= __tmc_etb_enable_hw(drvdata
);
696 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
701 * The ETB/ETF is not tracing and the buffer was just read.
702 * As such prepare to free the trace buffer.
708 drvdata
->reading
= false;
709 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
712 * Free allocated memory outside of the spinlock. There is no need
713 * to assert the validity of 'buf' since calling kfree(NULL) is safe.