2 * Copyright(C) 2016 Linaro Limited. All rights reserved.
3 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/circ_buf.h>
19 #include <linux/coresight.h>
20 #include <linux/perf_event.h>
21 #include <linux/slab.h>
22 #include "coresight-priv.h"
23 #include "coresight-tmc.h"
25 static void tmc_etb_enable_hw(struct tmc_drvdata
*drvdata
)
27 CS_UNLOCK(drvdata
->base
);
29 /* Wait for TMCSReady bit to be set */
30 tmc_wait_for_tmcready(drvdata
);
32 writel_relaxed(TMC_MODE_CIRCULAR_BUFFER
, drvdata
->base
+ TMC_MODE
);
33 writel_relaxed(TMC_FFCR_EN_FMT
| TMC_FFCR_EN_TI
|
34 TMC_FFCR_FON_FLIN
| TMC_FFCR_FON_TRIG_EVT
|
35 TMC_FFCR_TRIGON_TRIGIN
,
36 drvdata
->base
+ TMC_FFCR
);
38 writel_relaxed(drvdata
->trigger_cntr
, drvdata
->base
+ TMC_TRG
);
39 tmc_enable_hw(drvdata
);
41 CS_LOCK(drvdata
->base
);
44 static void tmc_etb_dump_hw(struct tmc_drvdata
*drvdata
)
53 for (i
= 0; i
< drvdata
->memwidth
; i
++) {
54 read_data
= readl_relaxed(drvdata
->base
+ TMC_RRD
);
55 if (read_data
== 0xFFFFFFFF)
57 memcpy(bufp
, &read_data
, 4);
64 static void tmc_etb_disable_hw(struct tmc_drvdata
*drvdata
)
66 CS_UNLOCK(drvdata
->base
);
68 tmc_flush_and_stop(drvdata
);
70 * When operating in sysFS mode the content of the buffer needs to be
71 * read before the TMC is disabled.
73 if (drvdata
->mode
== CS_MODE_SYSFS
)
74 tmc_etb_dump_hw(drvdata
);
75 tmc_disable_hw(drvdata
);
77 CS_LOCK(drvdata
->base
);
80 static void tmc_etf_enable_hw(struct tmc_drvdata
*drvdata
)
82 CS_UNLOCK(drvdata
->base
);
84 /* Wait for TMCSReady bit to be set */
85 tmc_wait_for_tmcready(drvdata
);
87 writel_relaxed(TMC_MODE_HARDWARE_FIFO
, drvdata
->base
+ TMC_MODE
);
88 writel_relaxed(TMC_FFCR_EN_FMT
| TMC_FFCR_EN_TI
,
89 drvdata
->base
+ TMC_FFCR
);
90 writel_relaxed(0x0, drvdata
->base
+ TMC_BUFWM
);
91 tmc_enable_hw(drvdata
);
93 CS_LOCK(drvdata
->base
);
96 static void tmc_etf_disable_hw(struct tmc_drvdata
*drvdata
)
98 CS_UNLOCK(drvdata
->base
);
100 tmc_flush_and_stop(drvdata
);
101 tmc_disable_hw(drvdata
);
103 CS_LOCK(drvdata
->base
);
106 static int tmc_enable_etf_sink_sysfs(struct coresight_device
*csdev
)
112 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
115 * If we don't have a buffer release the lock and allocate memory.
116 * Otherwise keep the lock and move along.
118 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
120 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
122 /* Allocating the memory here while outside of the spinlock */
123 buf
= kzalloc(drvdata
->size
, GFP_KERNEL
);
127 /* Let's try again */
128 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
131 if (drvdata
->reading
) {
137 * In sysFS mode we can have multiple writers per sink. Since this
138 * sink is already enabled no memory is needed and the HW need not be
141 if (drvdata
->mode
== CS_MODE_SYSFS
)
145 * If drvdata::buf isn't NULL, memory was allocated for a previous
146 * trace run but wasn't read. If so simply zero-out the memory.
147 * Otherwise use the memory allocated above.
149 * The memory is freed when users read the buffer using the
150 * /dev/xyz.{etf|etb} interface. See tmc_read_unprepare_etf() for
154 memset(drvdata
->buf
, 0, drvdata
->size
);
160 drvdata
->mode
= CS_MODE_SYSFS
;
161 tmc_etb_enable_hw(drvdata
);
163 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
165 /* Free memory outside the spinlock if need be */
170 dev_info(drvdata
->dev
, "TMC-ETB/ETF enabled\n");
175 static int tmc_enable_etf_sink_perf(struct coresight_device
*csdev
)
179 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
181 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
182 if (drvdata
->reading
) {
188 * In Perf mode there can be only one writer per sink. There
189 * is also no need to continue if the ETB/ETR is already operated
192 if (drvdata
->mode
!= CS_MODE_DISABLED
) {
197 drvdata
->mode
= CS_MODE_PERF
;
198 tmc_etb_enable_hw(drvdata
);
200 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
205 static int tmc_enable_etf_sink(struct coresight_device
*csdev
, u32 mode
)
209 return tmc_enable_etf_sink_sysfs(csdev
);
211 return tmc_enable_etf_sink_perf(csdev
);
214 /* We shouldn't be here */
218 static void tmc_disable_etf_sink(struct coresight_device
*csdev
)
221 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
223 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
224 if (drvdata
->reading
) {
225 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
229 /* Disable the TMC only if it needs to */
230 if (drvdata
->mode
!= CS_MODE_DISABLED
) {
231 tmc_etb_disable_hw(drvdata
);
232 drvdata
->mode
= CS_MODE_DISABLED
;
235 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
237 dev_info(drvdata
->dev
, "TMC-ETB/ETF disabled\n");
240 static int tmc_enable_etf_link(struct coresight_device
*csdev
,
241 int inport
, int outport
)
244 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
246 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
247 if (drvdata
->reading
) {
248 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
252 tmc_etf_enable_hw(drvdata
);
253 drvdata
->mode
= CS_MODE_SYSFS
;
254 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
256 dev_info(drvdata
->dev
, "TMC-ETF enabled\n");
260 static void tmc_disable_etf_link(struct coresight_device
*csdev
,
261 int inport
, int outport
)
264 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
266 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
267 if (drvdata
->reading
) {
268 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
272 tmc_etf_disable_hw(drvdata
);
273 drvdata
->mode
= CS_MODE_DISABLED
;
274 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
276 dev_info(drvdata
->dev
, "TMC disabled\n");
279 static void *tmc_alloc_etf_buffer(struct coresight_device
*csdev
, int cpu
,
280 void **pages
, int nr_pages
, bool overwrite
)
283 struct cs_buffers
*buf
;
286 cpu
= smp_processor_id();
287 node
= cpu_to_node(cpu
);
289 /* Allocate memory structure for interaction with Perf */
290 buf
= kzalloc_node(sizeof(struct cs_buffers
), GFP_KERNEL
, node
);
294 buf
->snapshot
= overwrite
;
295 buf
->nr_pages
= nr_pages
;
296 buf
->data_pages
= pages
;
301 static void tmc_free_etf_buffer(void *config
)
303 struct cs_buffers
*buf
= config
;
308 static int tmc_set_etf_buffer(struct coresight_device
*csdev
,
309 struct perf_output_handle
*handle
,
314 struct cs_buffers
*buf
= sink_config
;
316 /* wrap head around to the amount of space we have */
317 head
= handle
->head
& ((buf
->nr_pages
<< PAGE_SHIFT
) - 1);
319 /* find the page to write to */
320 buf
->cur
= head
/ PAGE_SIZE
;
322 /* and offset within that page */
323 buf
->offset
= head
% PAGE_SIZE
;
325 local_set(&buf
->data_size
, 0);
330 static unsigned long tmc_reset_etf_buffer(struct coresight_device
*csdev
,
331 struct perf_output_handle
*handle
,
332 void *sink_config
, bool *lost
)
335 struct cs_buffers
*buf
= sink_config
;
339 * In snapshot mode ->data_size holds the new address of the
340 * ring buffer's head. The size itself is the whole address
341 * range since we want the latest information.
344 handle
->head
= local_xchg(&buf
->data_size
,
345 buf
->nr_pages
<< PAGE_SHIFT
);
347 * Tell the tracer PMU how much we got in this run and if
348 * something went wrong along the way. Nobody else can use
349 * this cs_buffers instance until we are done. As such
350 * resetting parameters here and squaring off with the ring
351 * buffer API in the tracer PMU is fine.
353 *lost
= !!local_xchg(&buf
->lost
, 0);
354 size
= local_xchg(&buf
->data_size
, 0);
360 static void tmc_update_etf_buffer(struct coresight_device
*csdev
,
361 struct perf_output_handle
*handle
,
366 u32 read_ptr
, write_ptr
;
368 unsigned long offset
;
369 struct cs_buffers
*buf
= sink_config
;
370 struct tmc_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
375 /* This shouldn't happen */
376 if (WARN_ON_ONCE(drvdata
->mode
!= CS_MODE_PERF
))
379 CS_UNLOCK(drvdata
->base
);
381 tmc_flush_and_stop(drvdata
);
383 read_ptr
= readl_relaxed(drvdata
->base
+ TMC_RRP
);
384 write_ptr
= readl_relaxed(drvdata
->base
+ TMC_RWP
);
387 * Get a hold of the status register and see if a wrap around
388 * has occurred. If so adjust things accordingly.
390 status
= readl_relaxed(drvdata
->base
+ TMC_STS
);
391 if (status
& TMC_STS_FULL
) {
392 local_inc(&buf
->lost
);
393 to_read
= drvdata
->size
;
395 to_read
= CIRC_CNT(write_ptr
, read_ptr
, drvdata
->size
);
399 * The TMC RAM buffer may be bigger than the space available in the
400 * perf ring buffer (handle->size). If so advance the RRP so that we
401 * get the latest trace data.
403 if (to_read
> handle
->size
) {
407 * The value written to RRP must be byte-address aligned to
408 * the width of the trace memory databus _and_ to a frame
409 * boundary (16 byte), whichever is the biggest. For example,
410 * for 32-bit, 64-bit and 128-bit wide trace memory, the four
411 * LSBs must be 0s. For 256-bit wide trace memory, the five
414 switch (drvdata
->memwidth
) {
415 case TMC_MEM_INTF_WIDTH_32BITS
:
416 case TMC_MEM_INTF_WIDTH_64BITS
:
417 case TMC_MEM_INTF_WIDTH_128BITS
:
418 mask
= GENMASK(31, 5);
420 case TMC_MEM_INTF_WIDTH_256BITS
:
421 mask
= GENMASK(31, 6);
426 * Make sure the new size is aligned in accordance with the
427 * requirement explained above.
429 to_read
= handle
->size
& mask
;
430 /* Move the RAM read pointer up */
431 read_ptr
= (write_ptr
+ drvdata
->size
) - to_read
;
432 /* Make sure we are still within our limits */
433 if (read_ptr
> (drvdata
->size
- 1))
434 read_ptr
-= drvdata
->size
;
436 writel_relaxed(read_ptr
, drvdata
->base
+ TMC_RRP
);
437 local_inc(&buf
->lost
);
441 offset
= buf
->offset
;
443 /* for every byte to read */
444 for (i
= 0; i
< to_read
; i
+= 4) {
445 buf_ptr
= buf
->data_pages
[cur
] + offset
;
446 *buf_ptr
= readl_relaxed(drvdata
->base
+ TMC_RRD
);
449 if (offset
>= PAGE_SIZE
) {
452 /* wrap around at the end of the buffer */
453 cur
&= buf
->nr_pages
- 1;
458 * In snapshot mode all we have to do is communicate to
459 * perf_aux_output_end() the address of the current head. In full
460 * trace mode the same function expects a size to move rb->aux_head
464 local_set(&buf
->data_size
, (cur
* PAGE_SIZE
) + offset
);
466 local_add(to_read
, &buf
->data_size
);
468 CS_LOCK(drvdata
->base
);
471 static const struct coresight_ops_sink tmc_etf_sink_ops
= {
472 .enable
= tmc_enable_etf_sink
,
473 .disable
= tmc_disable_etf_sink
,
474 .alloc_buffer
= tmc_alloc_etf_buffer
,
475 .free_buffer
= tmc_free_etf_buffer
,
476 .set_buffer
= tmc_set_etf_buffer
,
477 .reset_buffer
= tmc_reset_etf_buffer
,
478 .update_buffer
= tmc_update_etf_buffer
,
481 static const struct coresight_ops_link tmc_etf_link_ops
= {
482 .enable
= tmc_enable_etf_link
,
483 .disable
= tmc_disable_etf_link
,
486 const struct coresight_ops tmc_etb_cs_ops
= {
487 .sink_ops
= &tmc_etf_sink_ops
,
490 const struct coresight_ops tmc_etf_cs_ops
= {
491 .sink_ops
= &tmc_etf_sink_ops
,
492 .link_ops
= &tmc_etf_link_ops
,
495 int tmc_read_prepare_etb(struct tmc_drvdata
*drvdata
)
501 /* config types are set a boot time and never change */
502 if (WARN_ON_ONCE(drvdata
->config_type
!= TMC_CONFIG_TYPE_ETB
&&
503 drvdata
->config_type
!= TMC_CONFIG_TYPE_ETF
))
506 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
508 if (drvdata
->reading
) {
513 /* There is no point in reading a TMC in HW FIFO mode */
514 mode
= readl_relaxed(drvdata
->base
+ TMC_MODE
);
515 if (mode
!= TMC_MODE_CIRCULAR_BUFFER
) {
520 /* Don't interfere if operated from Perf */
521 if (drvdata
->mode
== CS_MODE_PERF
) {
526 /* If drvdata::buf is NULL the trace data has been read already */
527 if (drvdata
->buf
== NULL
) {
532 /* Disable the TMC if need be */
533 if (drvdata
->mode
== CS_MODE_SYSFS
)
534 tmc_etb_disable_hw(drvdata
);
536 drvdata
->reading
= true;
538 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
543 int tmc_read_unprepare_etb(struct tmc_drvdata
*drvdata
)
549 /* config types are set a boot time and never change */
550 if (WARN_ON_ONCE(drvdata
->config_type
!= TMC_CONFIG_TYPE_ETB
&&
551 drvdata
->config_type
!= TMC_CONFIG_TYPE_ETF
))
554 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
556 /* There is no point in reading a TMC in HW FIFO mode */
557 mode
= readl_relaxed(drvdata
->base
+ TMC_MODE
);
558 if (mode
!= TMC_MODE_CIRCULAR_BUFFER
) {
559 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
563 /* Re-enable the TMC if need be */
564 if (drvdata
->mode
== CS_MODE_SYSFS
) {
566 * The trace run will continue with the same allocated trace
567 * buffer. As such zero-out the buffer so that we don't end
568 * up with stale data.
570 * Since the tracer is still enabled drvdata::buf
573 memset(drvdata
->buf
, 0, drvdata
->size
);
574 tmc_etb_enable_hw(drvdata
);
577 * The ETB/ETF is not tracing and the buffer was just read.
578 * As such prepare to free the trace buffer.
584 drvdata
->reading
= false;
585 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
588 * Free allocated memory outside of the spinlock. There is no need
589 * to assert the validity of 'buf' since calling kfree(NULL) is safe.