1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <asm/local.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/types.h>
18 #include <linux/device.h>
20 #include <linux/err.h>
22 #include <linux/miscdevice.h>
23 #include <linux/uaccess.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/seq_file.h>
28 #include <linux/coresight.h>
29 #include <linux/amba/bus.h>
30 #include <linux/clk.h>
31 #include <linux/circ_buf.h>
33 #include <linux/perf_event.h>
35 #include <asm/local.h>
37 #include "coresight-priv.h"
39 #define ETB_RAM_DEPTH_REG 0x004
40 #define ETB_STATUS_REG 0x00c
41 #define ETB_RAM_READ_DATA_REG 0x010
42 #define ETB_RAM_READ_POINTER 0x014
43 #define ETB_RAM_WRITE_POINTER 0x018
45 #define ETB_CTL_REG 0x020
46 #define ETB_RWD_REG 0x024
47 #define ETB_FFSR 0x300
48 #define ETB_FFCR 0x304
49 #define ETB_ITMISCOP0 0xee0
50 #define ETB_ITTRFLINACK 0xee4
51 #define ETB_ITTRFLIN 0xee8
52 #define ETB_ITATBDATA0 0xeeC
53 #define ETB_ITATBCTR2 0xef0
54 #define ETB_ITATBCTR1 0xef4
55 #define ETB_ITATBCTR0 0xef8
57 /* register description */
59 #define ETB_STATUS_RAM_FULL BIT(0)
61 #define ETB_CTL_CAPT_EN BIT(0)
63 #define ETB_FFCR_EN_FTC BIT(0)
64 #define ETB_FFCR_FON_MAN BIT(6)
65 #define ETB_FFCR_STOP_FI BIT(12)
66 #define ETB_FFCR_STOP_TRIGGER BIT(13)
68 #define ETB_FFCR_BIT 6
69 #define ETB_FFSR_BIT 1
70 #define ETB_FRAME_SIZE_WORDS 4
73 * struct cs_buffer - keep track of a recording session' specifics
74 * @cur: index of the current buffer
75 * @nr_pages: max number of pages granted to us
76 * @offset: offset within the current buffer
77 * @data_size: how much we collected in this run
78 * @lost: other than zero if we had a HW buffer wrap around
79 * @snapshot: is this run in snapshot mode
80 * @data_pages: a handle the ring buffer
84 unsigned int nr_pages;
93 * struct etb_drvdata - specifics associated to an ETB component
94 * @base: memory mapped base address for this component.
95 * @dev: the device entity associated to this component.
96 * @atclk: optional clock for the core parts of the ETB.
97 * @csdev: component vitals needed by the framework.
98 * @miscdev: specifics to handle "/dev/xyz.etb" entry.
99 * @spinlock: only one at a time pls.
100 * @reading: synchronise user space access to etb buffer.
101 * @mode: this ETB is being used.
102 * @buf: area of memory where ETB buffer content gets sent.
103 * @buffer_depth: size of @buf.
104 * @trigger_cntr: amount of words to store after a trigger.
110 struct coresight_device *csdev;
111 struct miscdevice miscdev;
120 static unsigned int etb_get_buffer_depth(struct etb_drvdata *drvdata)
124 pm_runtime_get_sync(drvdata->dev);
126 /* RO registers don't need locking */
127 depth = readl_relaxed(drvdata->base + ETB_RAM_DEPTH_REG);
129 pm_runtime_put(drvdata->dev);
133 static void etb_enable_hw(struct etb_drvdata *drvdata)
138 CS_UNLOCK(drvdata->base);
140 depth = drvdata->buffer_depth;
141 /* reset write RAM pointer address */
142 writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
143 /* clear entire RAM buffer */
144 for (i = 0; i < depth; i++)
145 writel_relaxed(0x0, drvdata->base + ETB_RWD_REG);
147 /* reset write RAM pointer address */
148 writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
149 /* reset read RAM pointer address */
150 writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
152 writel_relaxed(drvdata->trigger_cntr, drvdata->base + ETB_TRG);
153 writel_relaxed(ETB_FFCR_EN_FTC | ETB_FFCR_STOP_TRIGGER,
154 drvdata->base + ETB_FFCR);
155 /* ETB trace capture enable */
156 writel_relaxed(ETB_CTL_CAPT_EN, drvdata->base + ETB_CTL_REG);
158 CS_LOCK(drvdata->base);
161 static int etb_enable(struct coresight_device *csdev, u32 mode)
165 struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
167 val = local_cmpxchg(&drvdata->mode,
168 CS_MODE_DISABLED, mode);
170 * When accessing from Perf, a HW buffer can be handled
171 * by a single trace entity. In sysFS mode many tracers
172 * can be logging to the same HW buffer.
174 if (val == CS_MODE_PERF)
177 /* Nothing to do, the tracer is already enabled. */
178 if (val == CS_MODE_SYSFS)
181 spin_lock_irqsave(&drvdata->spinlock, flags);
182 etb_enable_hw(drvdata);
183 spin_unlock_irqrestore(&drvdata->spinlock, flags);
186 dev_info(drvdata->dev, "ETB enabled\n");
190 static void etb_disable_hw(struct etb_drvdata *drvdata)
194 CS_UNLOCK(drvdata->base);
196 ffcr = readl_relaxed(drvdata->base + ETB_FFCR);
197 /* stop formatter when a stop has completed */
198 ffcr |= ETB_FFCR_STOP_FI;
199 writel_relaxed(ffcr, drvdata->base + ETB_FFCR);
200 /* manually generate a flush of the system */
201 ffcr |= ETB_FFCR_FON_MAN;
202 writel_relaxed(ffcr, drvdata->base + ETB_FFCR);
204 if (coresight_timeout(drvdata->base, ETB_FFCR, ETB_FFCR_BIT, 0)) {
205 dev_err(drvdata->dev,
206 "timeout observed when probing at offset %#x\n",
210 /* disable trace capture */
211 writel_relaxed(0x0, drvdata->base + ETB_CTL_REG);
213 if (coresight_timeout(drvdata->base, ETB_FFSR, ETB_FFSR_BIT, 1)) {
214 dev_err(drvdata->dev,
215 "timeout observed when probing at offset %#x\n",
219 CS_LOCK(drvdata->base);
222 static void etb_dump_hw(struct etb_drvdata *drvdata)
226 u32 read_data, depth;
227 u32 read_ptr, write_ptr;
228 u32 frame_off, frame_endoff;
230 CS_UNLOCK(drvdata->base);
232 read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
233 write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
235 frame_off = write_ptr % ETB_FRAME_SIZE_WORDS;
236 frame_endoff = ETB_FRAME_SIZE_WORDS - frame_off;
238 dev_err(drvdata->dev,
239 "write_ptr: %lu not aligned to formatter frame size\n",
240 (unsigned long)write_ptr);
241 dev_err(drvdata->dev, "frameoff: %lu, frame_endoff: %lu\n",
242 (unsigned long)frame_off, (unsigned long)frame_endoff);
243 write_ptr += frame_endoff;
246 if ((readl_relaxed(drvdata->base + ETB_STATUS_REG)
247 & ETB_STATUS_RAM_FULL) == 0)
248 writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
250 writel_relaxed(write_ptr, drvdata->base + ETB_RAM_READ_POINTER);
252 depth = drvdata->buffer_depth;
253 buf_ptr = drvdata->buf;
254 for (i = 0; i < depth; i++) {
255 read_data = readl_relaxed(drvdata->base +
256 ETB_RAM_READ_DATA_REG);
257 *buf_ptr++ = read_data >> 0;
258 *buf_ptr++ = read_data >> 8;
259 *buf_ptr++ = read_data >> 16;
260 *buf_ptr++ = read_data >> 24;
264 buf_ptr -= (frame_endoff * 4);
265 for (i = 0; i < frame_endoff; i++) {
273 writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
275 CS_LOCK(drvdata->base);
278 static void etb_disable(struct coresight_device *csdev)
280 struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
283 spin_lock_irqsave(&drvdata->spinlock, flags);
284 etb_disable_hw(drvdata);
285 etb_dump_hw(drvdata);
286 spin_unlock_irqrestore(&drvdata->spinlock, flags);
288 local_set(&drvdata->mode, CS_MODE_DISABLED);
290 dev_info(drvdata->dev, "ETB disabled\n");
293 static void *etb_alloc_buffer(struct coresight_device *csdev, int cpu,
294 void **pages, int nr_pages, bool overwrite)
297 struct cs_buffers *buf;
300 cpu = smp_processor_id();
301 node = cpu_to_node(cpu);
303 buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
307 buf->snapshot = overwrite;
308 buf->nr_pages = nr_pages;
309 buf->data_pages = pages;
314 static void etb_free_buffer(void *config)
316 struct cs_buffers *buf = config;
321 static int etb_set_buffer(struct coresight_device *csdev,
322 struct perf_output_handle *handle,
327 struct cs_buffers *buf = sink_config;
329 /* wrap head around to the amount of space we have */
330 head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
332 /* find the page to write to */
333 buf->cur = head / PAGE_SIZE;
335 /* and offset within that page */
336 buf->offset = head % PAGE_SIZE;
338 local_set(&buf->data_size, 0);
343 static unsigned long etb_reset_buffer(struct coresight_device *csdev,
344 struct perf_output_handle *handle,
345 void *sink_config, bool *lost)
347 unsigned long size = 0;
348 struct cs_buffers *buf = sink_config;
352 * In snapshot mode ->data_size holds the new address of the
353 * ring buffer's head. The size itself is the whole address
354 * range since we want the latest information.
357 handle->head = local_xchg(&buf->data_size,
358 buf->nr_pages << PAGE_SHIFT);
361 * Tell the tracer PMU how much we got in this run and if
362 * something went wrong along the way. Nobody else can use
363 * this cs_buffers instance until we are done. As such
364 * resetting parameters here and squaring off with the ring
365 * buffer API in the tracer PMU is fine.
367 *lost = !!local_xchg(&buf->lost, 0);
368 size = local_xchg(&buf->data_size, 0);
374 static void etb_update_buffer(struct coresight_device *csdev,
375 struct perf_output_handle *handle,
380 u32 read_ptr, write_ptr, capacity;
381 u32 status, read_data, to_read;
382 unsigned long offset;
383 struct cs_buffers *buf = sink_config;
384 struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
389 capacity = drvdata->buffer_depth * ETB_FRAME_SIZE_WORDS;
391 CS_UNLOCK(drvdata->base);
392 etb_disable_hw(drvdata);
394 /* unit is in words, not bytes */
395 read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
396 write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
399 * Entries should be aligned to the frame size. If they are not
400 * go back to the last alignement point to give decoding tools a
401 * chance to fix things.
403 if (write_ptr % ETB_FRAME_SIZE_WORDS) {
404 dev_err(drvdata->dev,
405 "write_ptr: %lu not aligned to formatter frame size\n",
406 (unsigned long)write_ptr);
408 write_ptr &= ~(ETB_FRAME_SIZE_WORDS - 1);
409 local_inc(&buf->lost);
413 * Get a hold of the status register and see if a wrap around
414 * has occurred. If so adjust things accordingly. Otherwise
415 * start at the beginning and go until the write pointer has
418 status = readl_relaxed(drvdata->base + ETB_STATUS_REG);
419 if (status & ETB_STATUS_RAM_FULL) {
420 local_inc(&buf->lost);
422 read_ptr = write_ptr;
424 to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->buffer_depth);
425 to_read *= ETB_FRAME_SIZE_WORDS;
429 * Make sure we don't overwrite data that hasn't been consumed yet.
430 * It is entirely possible that the HW buffer has more data than the
431 * ring buffer can currently handle. If so adjust the start address
432 * to take only the last traces.
434 * In snapshot mode we are looking to get the latest traces only and as
435 * such, we don't care about not overwriting data that hasn't been
436 * processed by user space.
438 if (!buf->snapshot && to_read > handle->size) {
439 u32 mask = ~(ETB_FRAME_SIZE_WORDS - 1);
441 /* The new read pointer must be frame size aligned */
442 to_read -= handle->size & mask;
444 * Move the RAM read pointer up, keeping in mind that
445 * everything is in frame size units.
447 read_ptr = (write_ptr + drvdata->buffer_depth) -
448 to_read / ETB_FRAME_SIZE_WORDS;
449 /* Wrap around if need be*/
450 read_ptr &= ~(drvdata->buffer_depth - 1);
451 /* let the decoder know we've skipped ahead */
452 local_inc(&buf->lost);
455 /* finally tell HW where we want to start reading from */
456 writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
459 offset = buf->offset;
460 for (i = 0; i < to_read; i += 4) {
461 buf_ptr = buf->data_pages[cur] + offset;
462 read_data = readl_relaxed(drvdata->base +
463 ETB_RAM_READ_DATA_REG);
464 *buf_ptr++ = read_data >> 0;
465 *buf_ptr++ = read_data >> 8;
466 *buf_ptr++ = read_data >> 16;
467 *buf_ptr++ = read_data >> 24;
470 if (offset >= PAGE_SIZE) {
473 /* wrap around at the end of the buffer */
474 cur &= buf->nr_pages - 1;
478 /* reset ETB buffer for next run */
479 writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
480 writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
483 * In snapshot mode all we have to do is communicate to
484 * perf_aux_output_end() the address of the current head. In full
485 * trace mode the same function expects a size to move rb->aux_head
489 local_set(&buf->data_size, (cur * PAGE_SIZE) + offset);
491 local_add(to_read, &buf->data_size);
493 etb_enable_hw(drvdata);
494 CS_LOCK(drvdata->base);
497 static const struct coresight_ops_sink etb_sink_ops = {
498 .enable = etb_enable,
499 .disable = etb_disable,
500 .alloc_buffer = etb_alloc_buffer,
501 .free_buffer = etb_free_buffer,
502 .set_buffer = etb_set_buffer,
503 .reset_buffer = etb_reset_buffer,
504 .update_buffer = etb_update_buffer,
507 static const struct coresight_ops etb_cs_ops = {
508 .sink_ops = &etb_sink_ops,
511 static void etb_dump(struct etb_drvdata *drvdata)
515 spin_lock_irqsave(&drvdata->spinlock, flags);
516 if (local_read(&drvdata->mode) == CS_MODE_SYSFS) {
517 etb_disable_hw(drvdata);
518 etb_dump_hw(drvdata);
519 etb_enable_hw(drvdata);
521 spin_unlock_irqrestore(&drvdata->spinlock, flags);
523 dev_info(drvdata->dev, "ETB dumped\n");
526 static int etb_open(struct inode *inode, struct file *file)
528 struct etb_drvdata *drvdata = container_of(file->private_data,
529 struct etb_drvdata, miscdev);
531 if (local_cmpxchg(&drvdata->reading, 0, 1))
534 dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
538 static ssize_t etb_read(struct file *file, char __user *data,
539 size_t len, loff_t *ppos)
542 struct etb_drvdata *drvdata = container_of(file->private_data,
543 struct etb_drvdata, miscdev);
547 depth = drvdata->buffer_depth;
548 if (*ppos + len > depth * 4)
549 len = depth * 4 - *ppos;
551 if (copy_to_user(data, drvdata->buf + *ppos, len)) {
552 dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
558 dev_dbg(drvdata->dev, "%s: %zu bytes copied, %d bytes left\n",
559 __func__, len, (int)(depth * 4 - *ppos));
563 static int etb_release(struct inode *inode, struct file *file)
565 struct etb_drvdata *drvdata = container_of(file->private_data,
566 struct etb_drvdata, miscdev);
567 local_set(&drvdata->reading, 0);
569 dev_dbg(drvdata->dev, "%s: released\n", __func__);
573 static const struct file_operations etb_fops = {
574 .owner = THIS_MODULE,
577 .release = etb_release,
581 static ssize_t status_show(struct device *dev,
582 struct device_attribute *attr, char *buf)
585 u32 etb_rdr, etb_sr, etb_rrp, etb_rwp;
586 u32 etb_trg, etb_cr, etb_ffsr, etb_ffcr;
587 struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
589 pm_runtime_get_sync(drvdata->dev);
590 spin_lock_irqsave(&drvdata->spinlock, flags);
591 CS_UNLOCK(drvdata->base);
593 etb_rdr = readl_relaxed(drvdata->base + ETB_RAM_DEPTH_REG);
594 etb_sr = readl_relaxed(drvdata->base + ETB_STATUS_REG);
595 etb_rrp = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
596 etb_rwp = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
597 etb_trg = readl_relaxed(drvdata->base + ETB_TRG);
598 etb_cr = readl_relaxed(drvdata->base + ETB_CTL_REG);
599 etb_ffsr = readl_relaxed(drvdata->base + ETB_FFSR);
600 etb_ffcr = readl_relaxed(drvdata->base + ETB_FFCR);
602 CS_LOCK(drvdata->base);
603 spin_unlock_irqrestore(&drvdata->spinlock, flags);
605 pm_runtime_put(drvdata->dev);
610 "RAM read ptr:\t0x%x\n"
611 "RAM wrt ptr:\t0x%x\n"
612 "Trigger cnt:\t0x%x\n"
614 "Flush status:\t0x%x\n"
615 "Flush ctrl:\t0x%x\n",
616 etb_rdr, etb_sr, etb_rrp, etb_rwp,
617 etb_trg, etb_cr, etb_ffsr, etb_ffcr);
621 static DEVICE_ATTR_RO(status);
623 static ssize_t trigger_cntr_show(struct device *dev,
624 struct device_attribute *attr, char *buf)
626 struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
627 unsigned long val = drvdata->trigger_cntr;
629 return sprintf(buf, "%#lx\n", val);
632 static ssize_t trigger_cntr_store(struct device *dev,
633 struct device_attribute *attr,
634 const char *buf, size_t size)
638 struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
640 ret = kstrtoul(buf, 16, &val);
644 drvdata->trigger_cntr = val;
647 static DEVICE_ATTR_RW(trigger_cntr);
649 static struct attribute *coresight_etb_attrs[] = {
650 &dev_attr_trigger_cntr.attr,
651 &dev_attr_status.attr,
654 ATTRIBUTE_GROUPS(coresight_etb);
656 static int etb_probe(struct amba_device *adev, const struct amba_id *id)
660 struct device *dev = &adev->dev;
661 struct coresight_platform_data *pdata = NULL;
662 struct etb_drvdata *drvdata;
663 struct resource *res = &adev->res;
664 struct coresight_desc *desc;
665 struct device_node *np = adev->dev.of_node;
668 pdata = of_get_coresight_platform_data(dev, np);
670 return PTR_ERR(pdata);
671 adev->dev.platform_data = pdata;
674 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
678 drvdata->dev = &adev->dev;
679 drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
680 if (!IS_ERR(drvdata->atclk)) {
681 ret = clk_prepare_enable(drvdata->atclk);
685 dev_set_drvdata(dev, drvdata);
687 /* validity for the resource is already checked by the AMBA core */
688 base = devm_ioremap_resource(dev, res);
690 return PTR_ERR(base);
692 drvdata->base = base;
694 spin_lock_init(&drvdata->spinlock);
696 drvdata->buffer_depth = etb_get_buffer_depth(drvdata);
697 pm_runtime_put(&adev->dev);
699 if (drvdata->buffer_depth & 0x80000000)
702 drvdata->buf = devm_kzalloc(dev,
703 drvdata->buffer_depth * 4, GFP_KERNEL);
705 dev_err(dev, "Failed to allocate %u bytes for buffer data\n",
706 drvdata->buffer_depth * 4);
710 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
714 desc->type = CORESIGHT_DEV_TYPE_SINK;
715 desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
716 desc->ops = &etb_cs_ops;
719 desc->groups = coresight_etb_groups;
720 drvdata->csdev = coresight_register(desc);
721 if (IS_ERR(drvdata->csdev))
722 return PTR_ERR(drvdata->csdev);
724 drvdata->miscdev.name = pdata->name;
725 drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
726 drvdata->miscdev.fops = &etb_fops;
727 ret = misc_register(&drvdata->miscdev);
729 goto err_misc_register;
731 dev_info(dev, "ETB initialized\n");
735 coresight_unregister(drvdata->csdev);
740 static int etb_runtime_suspend(struct device *dev)
742 struct etb_drvdata *drvdata = dev_get_drvdata(dev);
744 if (drvdata && !IS_ERR(drvdata->atclk))
745 clk_disable_unprepare(drvdata->atclk);
750 static int etb_runtime_resume(struct device *dev)
752 struct etb_drvdata *drvdata = dev_get_drvdata(dev);
754 if (drvdata && !IS_ERR(drvdata->atclk))
755 clk_prepare_enable(drvdata->atclk);
761 static const struct dev_pm_ops etb_dev_pm_ops = {
762 SET_RUNTIME_PM_OPS(etb_runtime_suspend, etb_runtime_resume, NULL)
765 static struct amba_id etb_ids[] = {
773 static struct amba_driver etb_driver = {
775 .name = "coresight-etb10",
776 .owner = THIS_MODULE,
777 .pm = &etb_dev_pm_ops,
778 .suppress_bind_attrs = true,
785 module_amba_driver(etb_driver);
787 MODULE_LICENSE("GPL v2");
788 MODULE_DESCRIPTION("CoreSight Embedded Trace Buffer driver");