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/circ_buf.h>
8 #include <linux/coresight.h>
9 #include <linux/perf_event.h>
10 #include <linux/slab.h>
11 #include "coresight-priv.h"
12 #include "coresight-tmc.h"
13 #include "coresight-etm-perf.h"
15 static int tmc_set_etf_buffer(struct coresight_device *csdev,
16 struct perf_output_handle *handle);
18 static void __tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
20 CS_UNLOCK(drvdata->base);
22 /* Wait for TMCSReady bit to be set */
23 tmc_wait_for_tmcready(drvdata);
25 writel_relaxed(TMC_MODE_CIRCULAR_BUFFER, drvdata->base + TMC_MODE);
26 writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI |
27 TMC_FFCR_FON_FLIN | TMC_FFCR_FON_TRIG_EVT |
28 TMC_FFCR_TRIGON_TRIGIN,
29 drvdata->base + TMC_FFCR);
31 writel_relaxed(drvdata->trigger_cntr, drvdata->base + TMC_TRG);
32 tmc_enable_hw(drvdata);
34 CS_LOCK(drvdata->base);
37 static int tmc_etb_enable_hw(struct tmc_drvdata *drvdata)
39 int rc = coresight_claim_device(drvdata->base);
44 __tmc_etb_enable_hw(drvdata);
48 static void tmc_etb_dump_hw(struct tmc_drvdata *drvdata)
53 /* Check if the buffer wrapped around. */
54 lost = readl_relaxed(drvdata->base + TMC_STS) & TMC_STS_FULL;
58 read_data = readl_relaxed(drvdata->base + TMC_RRD);
59 if (read_data == 0xFFFFFFFF)
61 memcpy(bufp, &read_data, 4);
67 coresight_insert_barrier_packet(drvdata->buf);
71 static void __tmc_etb_disable_hw(struct tmc_drvdata *drvdata)
73 CS_UNLOCK(drvdata->base);
75 tmc_flush_and_stop(drvdata);
77 * When operating in sysFS mode the content of the buffer needs to be
78 * read before the TMC is disabled.
80 if (drvdata->mode == CS_MODE_SYSFS)
81 tmc_etb_dump_hw(drvdata);
82 tmc_disable_hw(drvdata);
84 CS_LOCK(drvdata->base);
87 static void tmc_etb_disable_hw(struct tmc_drvdata *drvdata)
89 __tmc_etb_disable_hw(drvdata);
90 coresight_disclaim_device(drvdata->base);
93 static void __tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
95 CS_UNLOCK(drvdata->base);
97 /* Wait for TMCSReady bit to be set */
98 tmc_wait_for_tmcready(drvdata);
100 writel_relaxed(TMC_MODE_HARDWARE_FIFO, drvdata->base + TMC_MODE);
101 writel_relaxed(TMC_FFCR_EN_FMT | TMC_FFCR_EN_TI,
102 drvdata->base + TMC_FFCR);
103 writel_relaxed(0x0, drvdata->base + TMC_BUFWM);
104 tmc_enable_hw(drvdata);
106 CS_LOCK(drvdata->base);
109 static int tmc_etf_enable_hw(struct tmc_drvdata *drvdata)
111 int rc = coresight_claim_device(drvdata->base);
116 __tmc_etf_enable_hw(drvdata);
120 static void tmc_etf_disable_hw(struct tmc_drvdata *drvdata)
122 CS_UNLOCK(drvdata->base);
124 tmc_flush_and_stop(drvdata);
125 tmc_disable_hw(drvdata);
126 coresight_disclaim_device_unlocked(drvdata->base);
127 CS_LOCK(drvdata->base);
131 * Return the available trace data in the buffer from @pos, with
132 * a maximum limit of @len, updating the @bufpp on where to
135 ssize_t tmc_etb_get_sysfs_trace(struct tmc_drvdata *drvdata,
136 loff_t pos, size_t len, char **bufpp)
138 ssize_t actual = len;
140 /* Adjust the len to available size @pos */
141 if (pos + actual > drvdata->len)
142 actual = drvdata->len - pos;
144 *bufpp = drvdata->buf + pos;
148 static int tmc_enable_etf_sink_sysfs(struct coresight_device *csdev)
154 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
157 * If we don't have a buffer release the lock and allocate memory.
158 * Otherwise keep the lock and move along.
160 spin_lock_irqsave(&drvdata->spinlock, flags);
162 spin_unlock_irqrestore(&drvdata->spinlock, flags);
164 /* Allocating the memory here while outside of the spinlock */
165 buf = kzalloc(drvdata->size, GFP_KERNEL);
169 /* Let's try again */
170 spin_lock_irqsave(&drvdata->spinlock, flags);
173 if (drvdata->reading) {
179 * In sysFS mode we can have multiple writers per sink. Since this
180 * sink is already enabled no memory is needed and the HW need not be
183 if (drvdata->mode == CS_MODE_SYSFS)
187 * If drvdata::buf isn't NULL, memory was allocated for a previous
188 * trace run but wasn't read. If so simply zero-out the memory.
189 * Otherwise use the memory allocated above.
191 * The memory is freed when users read the buffer using the
192 * /dev/xyz.{etf|etb} interface. See tmc_read_unprepare_etf() for
196 memset(drvdata->buf, 0, drvdata->size);
202 ret = tmc_etb_enable_hw(drvdata);
204 drvdata->mode = CS_MODE_SYSFS;
206 /* Free up the buffer if we failed to enable */
209 spin_unlock_irqrestore(&drvdata->spinlock, flags);
211 /* Free memory outside the spinlock if need be */
218 static int tmc_enable_etf_sink_perf(struct coresight_device *csdev, void *data)
222 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
223 struct perf_output_handle *handle = data;
225 spin_lock_irqsave(&drvdata->spinlock, flags);
228 if (drvdata->reading)
231 * In Perf mode there can be only one writer per sink. There
232 * is also no need to continue if the ETB/ETF is already
233 * operated from sysFS.
235 if (drvdata->mode != CS_MODE_DISABLED)
238 ret = tmc_set_etf_buffer(csdev, handle);
241 ret = tmc_etb_enable_hw(drvdata);
243 drvdata->mode = CS_MODE_PERF;
245 spin_unlock_irqrestore(&drvdata->spinlock, flags);
250 static int tmc_enable_etf_sink(struct coresight_device *csdev,
251 u32 mode, void *data)
254 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
258 ret = tmc_enable_etf_sink_sysfs(csdev);
261 ret = tmc_enable_etf_sink_perf(csdev, data);
263 /* We shouldn't be here */
272 dev_dbg(drvdata->dev, "TMC-ETB/ETF enabled\n");
276 static void tmc_disable_etf_sink(struct coresight_device *csdev)
279 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
281 spin_lock_irqsave(&drvdata->spinlock, flags);
282 if (drvdata->reading) {
283 spin_unlock_irqrestore(&drvdata->spinlock, flags);
287 /* Disable the TMC only if it needs to */
288 if (drvdata->mode != CS_MODE_DISABLED) {
289 tmc_etb_disable_hw(drvdata);
290 drvdata->mode = CS_MODE_DISABLED;
293 spin_unlock_irqrestore(&drvdata->spinlock, flags);
295 dev_dbg(drvdata->dev, "TMC-ETB/ETF disabled\n");
298 static int tmc_enable_etf_link(struct coresight_device *csdev,
299 int inport, int outport)
303 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
305 spin_lock_irqsave(&drvdata->spinlock, flags);
306 if (drvdata->reading) {
307 spin_unlock_irqrestore(&drvdata->spinlock, flags);
311 ret = tmc_etf_enable_hw(drvdata);
313 drvdata->mode = CS_MODE_SYSFS;
314 spin_unlock_irqrestore(&drvdata->spinlock, flags);
317 dev_dbg(drvdata->dev, "TMC-ETF enabled\n");
321 static void tmc_disable_etf_link(struct coresight_device *csdev,
322 int inport, int outport)
325 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
327 spin_lock_irqsave(&drvdata->spinlock, flags);
328 if (drvdata->reading) {
329 spin_unlock_irqrestore(&drvdata->spinlock, flags);
333 tmc_etf_disable_hw(drvdata);
334 drvdata->mode = CS_MODE_DISABLED;
335 spin_unlock_irqrestore(&drvdata->spinlock, flags);
337 dev_dbg(drvdata->dev, "TMC-ETF disabled\n");
340 static void *tmc_alloc_etf_buffer(struct coresight_device *csdev, int cpu,
341 void **pages, int nr_pages, bool overwrite)
344 struct cs_buffers *buf;
347 cpu = smp_processor_id();
348 node = cpu_to_node(cpu);
350 /* Allocate memory structure for interaction with Perf */
351 buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
355 buf->snapshot = overwrite;
356 buf->nr_pages = nr_pages;
357 buf->data_pages = pages;
362 static void tmc_free_etf_buffer(void *config)
364 struct cs_buffers *buf = config;
369 static int tmc_set_etf_buffer(struct coresight_device *csdev,
370 struct perf_output_handle *handle)
374 struct cs_buffers *buf = etm_perf_sink_config(handle);
379 /* wrap head around to the amount of space we have */
380 head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
382 /* find the page to write to */
383 buf->cur = head / PAGE_SIZE;
385 /* and offset within that page */
386 buf->offset = head % PAGE_SIZE;
388 local_set(&buf->data_size, 0);
393 static unsigned long tmc_update_etf_buffer(struct coresight_device *csdev,
394 struct perf_output_handle *handle,
401 u64 read_ptr, write_ptr;
403 unsigned long offset, to_read;
404 struct cs_buffers *buf = sink_config;
405 struct tmc_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
410 /* This shouldn't happen */
411 if (WARN_ON_ONCE(drvdata->mode != CS_MODE_PERF))
414 CS_UNLOCK(drvdata->base);
416 tmc_flush_and_stop(drvdata);
418 read_ptr = tmc_read_rrp(drvdata);
419 write_ptr = tmc_read_rwp(drvdata);
422 * Get a hold of the status register and see if a wrap around
423 * has occurred. If so adjust things accordingly.
425 status = readl_relaxed(drvdata->base + TMC_STS);
426 if (status & TMC_STS_FULL) {
428 to_read = drvdata->size;
430 to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->size);
434 * The TMC RAM buffer may be bigger than the space available in the
435 * perf ring buffer (handle->size). If so advance the RRP so that we
436 * get the latest trace data.
438 if (to_read > handle->size) {
442 * The value written to RRP must be byte-address aligned to
443 * the width of the trace memory databus _and_ to a frame
444 * boundary (16 byte), whichever is the biggest. For example,
445 * for 32-bit, 64-bit and 128-bit wide trace memory, the four
446 * LSBs must be 0s. For 256-bit wide trace memory, the five
449 switch (drvdata->memwidth) {
450 case TMC_MEM_INTF_WIDTH_32BITS:
451 case TMC_MEM_INTF_WIDTH_64BITS:
452 case TMC_MEM_INTF_WIDTH_128BITS:
453 mask = GENMASK(31, 4);
455 case TMC_MEM_INTF_WIDTH_256BITS:
456 mask = GENMASK(31, 5);
461 * Make sure the new size is aligned in accordance with the
462 * requirement explained above.
464 to_read = handle->size & mask;
465 /* Move the RAM read pointer up */
466 read_ptr = (write_ptr + drvdata->size) - to_read;
467 /* Make sure we are still within our limits */
468 if (read_ptr > (drvdata->size - 1))
469 read_ptr -= drvdata->size;
471 tmc_write_rrp(drvdata, read_ptr);
476 perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
479 offset = buf->offset;
480 barrier = barrier_pkt;
482 /* for every byte to read */
483 for (i = 0; i < to_read; i += 4) {
484 buf_ptr = buf->data_pages[cur] + offset;
485 *buf_ptr = readl_relaxed(drvdata->base + TMC_RRD);
487 if (lost && *barrier) {
493 if (offset >= PAGE_SIZE) {
496 /* wrap around at the end of the buffer */
497 cur &= buf->nr_pages - 1;
501 /* In snapshot mode we have to update the head */
503 handle->head = (cur * PAGE_SIZE) + offset;
504 to_read = buf->nr_pages << PAGE_SHIFT;
506 CS_LOCK(drvdata->base);
511 static const struct coresight_ops_sink tmc_etf_sink_ops = {
512 .enable = tmc_enable_etf_sink,
513 .disable = tmc_disable_etf_sink,
514 .alloc_buffer = tmc_alloc_etf_buffer,
515 .free_buffer = tmc_free_etf_buffer,
516 .update_buffer = tmc_update_etf_buffer,
519 static const struct coresight_ops_link tmc_etf_link_ops = {
520 .enable = tmc_enable_etf_link,
521 .disable = tmc_disable_etf_link,
524 const struct coresight_ops tmc_etb_cs_ops = {
525 .sink_ops = &tmc_etf_sink_ops,
528 const struct coresight_ops tmc_etf_cs_ops = {
529 .sink_ops = &tmc_etf_sink_ops,
530 .link_ops = &tmc_etf_link_ops,
533 int tmc_read_prepare_etb(struct tmc_drvdata *drvdata)
539 /* config types are set a boot time and never change */
540 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETB &&
541 drvdata->config_type != TMC_CONFIG_TYPE_ETF))
544 spin_lock_irqsave(&drvdata->spinlock, flags);
546 if (drvdata->reading) {
551 /* There is no point in reading a TMC in HW FIFO mode */
552 mode = readl_relaxed(drvdata->base + TMC_MODE);
553 if (mode != TMC_MODE_CIRCULAR_BUFFER) {
558 /* Don't interfere if operated from Perf */
559 if (drvdata->mode == CS_MODE_PERF) {
564 /* If drvdata::buf is NULL the trace data has been read already */
565 if (drvdata->buf == NULL) {
570 /* Disable the TMC if need be */
571 if (drvdata->mode == CS_MODE_SYSFS)
572 __tmc_etb_disable_hw(drvdata);
574 drvdata->reading = true;
576 spin_unlock_irqrestore(&drvdata->spinlock, flags);
581 int tmc_read_unprepare_etb(struct tmc_drvdata *drvdata)
587 /* config types are set a boot time and never change */
588 if (WARN_ON_ONCE(drvdata->config_type != TMC_CONFIG_TYPE_ETB &&
589 drvdata->config_type != TMC_CONFIG_TYPE_ETF))
592 spin_lock_irqsave(&drvdata->spinlock, flags);
594 /* There is no point in reading a TMC in HW FIFO mode */
595 mode = readl_relaxed(drvdata->base + TMC_MODE);
596 if (mode != TMC_MODE_CIRCULAR_BUFFER) {
597 spin_unlock_irqrestore(&drvdata->spinlock, flags);
601 /* Re-enable the TMC if need be */
602 if (drvdata->mode == CS_MODE_SYSFS) {
604 * The trace run will continue with the same allocated trace
605 * buffer. As such zero-out the buffer so that we don't end
606 * up with stale data.
608 * Since the tracer is still enabled drvdata::buf
611 memset(drvdata->buf, 0, drvdata->size);
612 __tmc_etb_enable_hw(drvdata);
615 * The ETB/ETF is not tracing and the buffer was just read.
616 * As such prepare to free the trace buffer.
622 drvdata->reading = false;
623 spin_unlock_irqrestore(&drvdata->spinlock, flags);
626 * Free allocated memory outside of the spinlock. There is no need
627 * to assert the validity of 'buf' since calling kfree(NULL) is safe.