Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  4 *
  5 * Description: CoreSight Embedded Trace Buffer driver
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/atomic.h>
  9#include <linux/kernel.h>
 10#include <linux/init.h>
 11#include <linux/types.h>
 12#include <linux/device.h>
 13#include <linux/io.h>
 14#include <linux/err.h>
 15#include <linux/fs.h>
 16#include <linux/miscdevice.h>
 17#include <linux/uaccess.h>
 18#include <linux/slab.h>
 19#include <linux/spinlock.h>
 20#include <linux/pm_runtime.h>
 21#include <linux/seq_file.h>
 22#include <linux/coresight.h>
 23#include <linux/amba/bus.h>
 24#include <linux/clk.h>
 25#include <linux/circ_buf.h>
 26#include <linux/mm.h>
 27#include <linux/perf_event.h>
 28
 
 29
 30#include "coresight-priv.h"
 31#include "coresight-etm-perf.h"
 32
 33#define ETB_RAM_DEPTH_REG	0x004
 34#define ETB_STATUS_REG		0x00c
 35#define ETB_RAM_READ_DATA_REG	0x010
 36#define ETB_RAM_READ_POINTER	0x014
 37#define ETB_RAM_WRITE_POINTER	0x018
 38#define ETB_TRG			0x01c
 39#define ETB_CTL_REG		0x020
 40#define ETB_RWD_REG		0x024
 41#define ETB_FFSR		0x300
 42#define ETB_FFCR		0x304
 43#define ETB_ITMISCOP0		0xee0
 44#define ETB_ITTRFLINACK		0xee4
 45#define ETB_ITTRFLIN		0xee8
 46#define ETB_ITATBDATA0		0xeeC
 47#define ETB_ITATBCTR2		0xef0
 48#define ETB_ITATBCTR1		0xef4
 49#define ETB_ITATBCTR0		0xef8
 50
 51/* register description */
 52/* STS - 0x00C */
 53#define ETB_STATUS_RAM_FULL	BIT(0)
 54/* CTL - 0x020 */
 55#define ETB_CTL_CAPT_EN		BIT(0)
 56/* FFCR - 0x304 */
 57#define ETB_FFCR_EN_FTC		BIT(0)
 58#define ETB_FFCR_FON_MAN	BIT(6)
 59#define ETB_FFCR_STOP_FI	BIT(12)
 60#define ETB_FFCR_STOP_TRIGGER	BIT(13)
 61
 62#define ETB_FFCR_BIT		6
 63#define ETB_FFSR_BIT		1
 64#define ETB_FRAME_SIZE_WORDS	4
 65
 66DEFINE_CORESIGHT_DEVLIST(etb_devs, "etb");
 67
 68/**
 69 * struct etb_drvdata - specifics associated to an ETB component
 70 * @base:	memory mapped base address for this component.
 
 71 * @atclk:	optional clock for the core parts of the ETB.
 72 * @csdev:	component vitals needed by the framework.
 73 * @miscdev:	specifics to handle "/dev/xyz.etb" entry.
 74 * @spinlock:	only one at a time pls.
 75 * @reading:	synchronise user space access to etb buffer.
 76 * @pid:	Process ID of the process being monitored by the session
 77 *		that is using this component.
 78 * @buf:	area of memory where ETB buffer content gets sent.
 79 * @buffer_depth: size of @buf.
 80 * @trigger_cntr: amount of words to store after a trigger.
 81 */
 82struct etb_drvdata {
 83	void __iomem		*base;
 
 84	struct clk		*atclk;
 85	struct coresight_device	*csdev;
 86	struct miscdevice	miscdev;
 87	spinlock_t		spinlock;
 88	local_t			reading;
 89	pid_t			pid;
 90	u8			*buf;
 91	u32			buffer_depth;
 92	u32			trigger_cntr;
 93};
 94
 95static int etb_set_buffer(struct coresight_device *csdev,
 96			  struct perf_output_handle *handle);
 97
 98static inline unsigned int etb_get_buffer_depth(struct etb_drvdata *drvdata)
 99{
100	return readl_relaxed(drvdata->base + ETB_RAM_DEPTH_REG);
 
 
 
 
 
 
 
 
101}
102
103static void __etb_enable_hw(struct etb_drvdata *drvdata)
104{
105	int i;
106	u32 depth;
107
108	CS_UNLOCK(drvdata->base);
109
110	depth = drvdata->buffer_depth;
111	/* reset write RAM pointer address */
112	writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
113	/* clear entire RAM buffer */
114	for (i = 0; i < depth; i++)
115		writel_relaxed(0x0, drvdata->base + ETB_RWD_REG);
116
117	/* reset write RAM pointer address */
118	writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
119	/* reset read RAM pointer address */
120	writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
121
122	writel_relaxed(drvdata->trigger_cntr, drvdata->base + ETB_TRG);
123	writel_relaxed(ETB_FFCR_EN_FTC | ETB_FFCR_STOP_TRIGGER,
124		       drvdata->base + ETB_FFCR);
125	/* ETB trace capture enable */
126	writel_relaxed(ETB_CTL_CAPT_EN, drvdata->base + ETB_CTL_REG);
127
128	CS_LOCK(drvdata->base);
129}
130
131static int etb_enable_hw(struct etb_drvdata *drvdata)
132{
133	int rc = coresight_claim_device(drvdata->csdev);
134
135	if (rc)
136		return rc;
137
138	__etb_enable_hw(drvdata);
139	return 0;
140}
141
142static int etb_enable_sysfs(struct coresight_device *csdev)
143{
144	int ret = 0;
145	unsigned long flags;
146	struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
147
148	spin_lock_irqsave(&drvdata->spinlock, flags);
149
150	/* Don't messup with perf sessions. */
151	if (coresight_get_mode(csdev) == CS_MODE_PERF) {
152		ret = -EBUSY;
153		goto out;
154	}
155
156	if (coresight_get_mode(csdev) == CS_MODE_DISABLED) {
157		ret = etb_enable_hw(drvdata);
158		if (ret)
159			goto out;
160
161		coresight_set_mode(csdev, CS_MODE_SYSFS);
162	}
163
164	csdev->refcnt++;
165out:
166	spin_unlock_irqrestore(&drvdata->spinlock, flags);
167	return ret;
168}
169
170static int etb_enable_perf(struct coresight_device *csdev, void *data)
171{
172	int ret = 0;
173	pid_t pid;
174	unsigned long flags;
175	struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
176	struct perf_output_handle *handle = data;
177	struct cs_buffers *buf = etm_perf_sink_config(handle);
178
179	spin_lock_irqsave(&drvdata->spinlock, flags);
180
181	/* No need to continue if the component is already in used by sysFS. */
182	if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS) {
183		ret = -EBUSY;
184		goto out;
185	}
186
187	/* Get a handle on the pid of the process to monitor */
188	pid = buf->pid;
189
190	if (drvdata->pid != -1 && drvdata->pid != pid) {
191		ret = -EBUSY;
192		goto out;
193	}
194
195	/*
196	 * No HW configuration is needed if the sink is already in
197	 * use for this session.
 
198	 */
199	if (drvdata->pid == pid) {
200		csdev->refcnt++;
201		goto out;
202	}
203
204	/*
205	 * We don't have an internal state to clean up if we fail to setup
206	 * the perf buffer. So we can perform the step before we turn the
207	 * ETB on and leave without cleaning up.
208	 */
209	ret = etb_set_buffer(csdev, handle);
210	if (ret)
211		goto out;
212
213	ret = etb_enable_hw(drvdata);
214	if (!ret) {
215		/* Associate with monitored process. */
216		drvdata->pid = pid;
217		coresight_set_mode(drvdata->csdev, CS_MODE_PERF);
218		csdev->refcnt++;
219	}
220
221out:
222	spin_unlock_irqrestore(&drvdata->spinlock, flags);
223	return ret;
224}
225
226static int etb_enable(struct coresight_device *csdev, enum cs_mode mode,
227		      void *data)
228{
229	int ret;
230
231	switch (mode) {
232	case CS_MODE_SYSFS:
233		ret = etb_enable_sysfs(csdev);
234		break;
235	case CS_MODE_PERF:
236		ret = etb_enable_perf(csdev, data);
237		break;
238	default:
239		ret = -EINVAL;
240		break;
241	}
242
243	if (ret)
244		return ret;
245
246	dev_dbg(&csdev->dev, "ETB enabled\n");
 
247	return 0;
248}
249
250static void __etb_disable_hw(struct etb_drvdata *drvdata)
251{
252	u32 ffcr;
253	struct device *dev = &drvdata->csdev->dev;
254	struct csdev_access *csa = &drvdata->csdev->access;
255
256	CS_UNLOCK(drvdata->base);
257
258	ffcr = readl_relaxed(drvdata->base + ETB_FFCR);
259	/* stop formatter when a stop has completed */
260	ffcr |= ETB_FFCR_STOP_FI;
261	writel_relaxed(ffcr, drvdata->base + ETB_FFCR);
262	/* manually generate a flush of the system */
263	ffcr |= ETB_FFCR_FON_MAN;
264	writel_relaxed(ffcr, drvdata->base + ETB_FFCR);
265
266	if (coresight_timeout(csa, ETB_FFCR, ETB_FFCR_BIT, 0)) {
267		dev_err(dev,
268		"timeout while waiting for completion of Manual Flush\n");
269	}
270
271	/* disable trace capture */
272	writel_relaxed(0x0, drvdata->base + ETB_CTL_REG);
273
274	if (coresight_timeout(csa, ETB_FFSR, ETB_FFSR_BIT, 1)) {
275		dev_err(dev,
276			"timeout while waiting for Formatter to Stop\n");
277	}
278
279	CS_LOCK(drvdata->base);
280}
281
282static void etb_dump_hw(struct etb_drvdata *drvdata)
283{
284	bool lost = false;
285	int i;
286	u8 *buf_ptr;
287	u32 read_data, depth;
288	u32 read_ptr, write_ptr;
289	u32 frame_off, frame_endoff;
290	struct device *dev = &drvdata->csdev->dev;
291
292	CS_UNLOCK(drvdata->base);
293
294	read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
295	write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
296
297	frame_off = write_ptr % ETB_FRAME_SIZE_WORDS;
298	frame_endoff = ETB_FRAME_SIZE_WORDS - frame_off;
299	if (frame_off) {
300		dev_err(dev,
301			"write_ptr: %lu not aligned to formatter frame size\n",
302			(unsigned long)write_ptr);
303		dev_err(dev, "frameoff: %lu, frame_endoff: %lu\n",
304			(unsigned long)frame_off, (unsigned long)frame_endoff);
305		write_ptr += frame_endoff;
306	}
307
308	if ((readl_relaxed(drvdata->base + ETB_STATUS_REG)
309		      & ETB_STATUS_RAM_FULL) == 0) {
310		writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
311	} else {
312		writel_relaxed(write_ptr, drvdata->base + ETB_RAM_READ_POINTER);
313		lost = true;
314	}
315
316	depth = drvdata->buffer_depth;
317	buf_ptr = drvdata->buf;
318	for (i = 0; i < depth; i++) {
319		read_data = readl_relaxed(drvdata->base +
320					  ETB_RAM_READ_DATA_REG);
321		*(u32 *)buf_ptr = read_data;
322		buf_ptr += 4;
 
 
323	}
324
325	if (lost)
326		coresight_insert_barrier_packet(drvdata->buf);
327
328	if (frame_off) {
329		buf_ptr -= (frame_endoff * 4);
330		for (i = 0; i < frame_endoff; i++) {
331			*buf_ptr++ = 0x0;
332			*buf_ptr++ = 0x0;
333			*buf_ptr++ = 0x0;
334			*buf_ptr++ = 0x0;
335		}
336	}
337
338	writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
339
340	CS_LOCK(drvdata->base);
341}
342
343static void etb_disable_hw(struct etb_drvdata *drvdata)
344{
345	__etb_disable_hw(drvdata);
346	etb_dump_hw(drvdata);
347	coresight_disclaim_device(drvdata->csdev);
348}
349
350static int etb_disable(struct coresight_device *csdev)
351{
352	struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
353	unsigned long flags;
354
355	spin_lock_irqsave(&drvdata->spinlock, flags);
356
357	csdev->refcnt--;
358	if (csdev->refcnt) {
359		spin_unlock_irqrestore(&drvdata->spinlock, flags);
360		return -EBUSY;
361	}
362
363	/* Complain if we (somehow) got out of sync */
364	WARN_ON_ONCE(coresight_get_mode(csdev) == CS_MODE_DISABLED);
365	etb_disable_hw(drvdata);
366	/* Dissociate from monitored process. */
367	drvdata->pid = -1;
368	coresight_set_mode(csdev, CS_MODE_DISABLED);
369	spin_unlock_irqrestore(&drvdata->spinlock, flags);
370
371	dev_dbg(&csdev->dev, "ETB disabled\n");
372	return 0;
 
373}
374
375static void *etb_alloc_buffer(struct coresight_device *csdev,
376			      struct perf_event *event, void **pages,
377			      int nr_pages, bool overwrite)
378{
379	int node;
380	struct cs_buffers *buf;
381
382	node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
 
 
383
384	buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
385	if (!buf)
386		return NULL;
387
388	buf->pid = task_pid_nr(event->owner);
389	buf->snapshot = overwrite;
390	buf->nr_pages = nr_pages;
391	buf->data_pages = pages;
392
393	return buf;
394}
395
396static void etb_free_buffer(void *config)
397{
398	struct cs_buffers *buf = config;
399
400	kfree(buf);
401}
402
403static int etb_set_buffer(struct coresight_device *csdev,
404			  struct perf_output_handle *handle)
 
405{
406	int ret = 0;
407	unsigned long head;
408	struct cs_buffers *buf = etm_perf_sink_config(handle);
409
410	if (!buf)
411		return -EINVAL;
412
413	/* wrap head around to the amount of space we have */
414	head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
415
416	/* find the page to write to */
417	buf->cur = head / PAGE_SIZE;
418
419	/* and offset within that page */
420	buf->offset = head % PAGE_SIZE;
421
422	local_set(&buf->data_size, 0);
423
424	return ret;
425}
426
427static unsigned long etb_update_buffer(struct coresight_device *csdev,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428			      struct perf_output_handle *handle,
429			      void *sink_config)
430{
431	bool lost = false;
432	int i, cur;
433	u8 *buf_ptr;
434	const u32 *barrier;
435	u32 read_ptr, write_ptr, capacity;
436	u32 status, read_data;
437	unsigned long offset, to_read = 0, flags;
438	struct cs_buffers *buf = sink_config;
439	struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
440
441	if (!buf)
442		return 0;
443
444	capacity = drvdata->buffer_depth * ETB_FRAME_SIZE_WORDS;
445
446	spin_lock_irqsave(&drvdata->spinlock, flags);
447
448	/* Don't do anything if another tracer is using this sink */
449	if (csdev->refcnt != 1)
450		goto out;
451
452	__etb_disable_hw(drvdata);
453	CS_UNLOCK(drvdata->base);
 
454
455	/* unit is in words, not bytes */
456	read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
457	write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
458
459	/*
460	 * Entries should be aligned to the frame size.  If they are not
461	 * go back to the last alignment point to give decoding tools a
462	 * chance to fix things.
463	 */
464	if (write_ptr % ETB_FRAME_SIZE_WORDS) {
465		dev_err(&csdev->dev,
466			"write_ptr: %lu not aligned to formatter frame size\n",
467			(unsigned long)write_ptr);
468
469		write_ptr &= ~(ETB_FRAME_SIZE_WORDS - 1);
470		lost = true;
471	}
472
473	/*
474	 * Get a hold of the status register and see if a wrap around
475	 * has occurred.  If so adjust things accordingly.  Otherwise
476	 * start at the beginning and go until the write pointer has
477	 * been reached.
478	 */
479	status = readl_relaxed(drvdata->base + ETB_STATUS_REG);
480	if (status & ETB_STATUS_RAM_FULL) {
481		lost = true;
482		to_read = capacity;
483		read_ptr = write_ptr;
484	} else {
485		to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->buffer_depth);
486		to_read *= ETB_FRAME_SIZE_WORDS;
487	}
488
489	/*
490	 * Make sure we don't overwrite data that hasn't been consumed yet.
491	 * It is entirely possible that the HW buffer has more data than the
492	 * ring buffer can currently handle.  If so adjust the start address
493	 * to take only the last traces.
494	 *
495	 * In snapshot mode we are looking to get the latest traces only and as
496	 * such, we don't care about not overwriting data that hasn't been
497	 * processed by user space.
498	 */
499	if (!buf->snapshot && to_read > handle->size) {
500		u32 mask = ~(ETB_FRAME_SIZE_WORDS - 1);
501
502		/* The new read pointer must be frame size aligned */
503		to_read = handle->size & mask;
504		/*
505		 * Move the RAM read pointer up, keeping in mind that
506		 * everything is in frame size units.
507		 */
508		read_ptr = (write_ptr + drvdata->buffer_depth) -
509					to_read / ETB_FRAME_SIZE_WORDS;
510		/* Wrap around if need be*/
511		if (read_ptr > (drvdata->buffer_depth - 1))
512			read_ptr -= drvdata->buffer_depth;
513		/* let the decoder know we've skipped ahead */
514		lost = true;
515	}
516
517	/*
518	 * Don't set the TRUNCATED flag in snapshot mode because 1) the
519	 * captured buffer is expected to be truncated and 2) a full buffer
520	 * prevents the event from being re-enabled by the perf core,
521	 * resulting in stale data being send to user space.
522	 */
523	if (!buf->snapshot && lost)
524		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
525
526	/* finally tell HW where we want to start reading from */
527	writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
528
529	cur = buf->cur;
530	offset = buf->offset;
531	barrier = coresight_barrier_pkt;
532
533	for (i = 0; i < to_read; i += 4) {
534		buf_ptr = buf->data_pages[cur] + offset;
535		read_data = readl_relaxed(drvdata->base +
536					  ETB_RAM_READ_DATA_REG);
537		if (lost && i < CORESIGHT_BARRIER_PKT_SIZE) {
538			read_data = *barrier;
539			barrier++;
540		}
541
542		*(u32 *)buf_ptr = read_data;
543		buf_ptr += 4;
544
545		offset += 4;
546		if (offset >= PAGE_SIZE) {
547			offset = 0;
548			cur++;
549			/* wrap around at the end of the buffer */
550			cur &= buf->nr_pages - 1;
551		}
552	}
553
554	/* reset ETB buffer for next run */
555	writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
556	writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
557
558	/*
559	 * In snapshot mode we simply increment the head by the number of byte
560	 * that were written.  User space will figure out how many bytes to get
561	 * from the AUX buffer based on the position of the head.
 
562	 */
563	if (buf->snapshot)
564		handle->head += to_read;
 
 
565
566	__etb_enable_hw(drvdata);
567	CS_LOCK(drvdata->base);
568out:
569	spin_unlock_irqrestore(&drvdata->spinlock, flags);
570
571	return to_read;
572}
573
574static const struct coresight_ops_sink etb_sink_ops = {
575	.enable		= etb_enable,
576	.disable	= etb_disable,
577	.alloc_buffer	= etb_alloc_buffer,
578	.free_buffer	= etb_free_buffer,
 
 
579	.update_buffer	= etb_update_buffer,
580};
581
582static const struct coresight_ops etb_cs_ops = {
583	.sink_ops	= &etb_sink_ops,
584};
585
586static void etb_dump(struct etb_drvdata *drvdata)
587{
588	unsigned long flags;
589
590	spin_lock_irqsave(&drvdata->spinlock, flags);
591	if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS) {
592		__etb_disable_hw(drvdata);
593		etb_dump_hw(drvdata);
594		__etb_enable_hw(drvdata);
595	}
596	spin_unlock_irqrestore(&drvdata->spinlock, flags);
597
598	dev_dbg(&drvdata->csdev->dev, "ETB dumped\n");
599}
600
601static int etb_open(struct inode *inode, struct file *file)
602{
603	struct etb_drvdata *drvdata = container_of(file->private_data,
604						   struct etb_drvdata, miscdev);
605
606	if (local_cmpxchg(&drvdata->reading, 0, 1))
607		return -EBUSY;
608
609	dev_dbg(&drvdata->csdev->dev, "%s: successfully opened\n", __func__);
610	return 0;
611}
612
613static ssize_t etb_read(struct file *file, char __user *data,
614				size_t len, loff_t *ppos)
615{
616	u32 depth;
617	struct etb_drvdata *drvdata = container_of(file->private_data,
618						   struct etb_drvdata, miscdev);
619	struct device *dev = &drvdata->csdev->dev;
620
621	etb_dump(drvdata);
622
623	depth = drvdata->buffer_depth;
624	if (*ppos + len > depth * 4)
625		len = depth * 4 - *ppos;
626
627	if (copy_to_user(data, drvdata->buf + *ppos, len)) {
628		dev_dbg(dev,
629			"%s: copy_to_user failed\n", __func__);
630		return -EFAULT;
631	}
632
633	*ppos += len;
634
635	dev_dbg(dev, "%s: %zu bytes copied, %d bytes left\n",
636		__func__, len, (int)(depth * 4 - *ppos));
637	return len;
638}
639
640static int etb_release(struct inode *inode, struct file *file)
641{
642	struct etb_drvdata *drvdata = container_of(file->private_data,
643						   struct etb_drvdata, miscdev);
644	local_set(&drvdata->reading, 0);
645
646	dev_dbg(&drvdata->csdev->dev, "%s: released\n", __func__);
647	return 0;
648}
649
650static const struct file_operations etb_fops = {
651	.owner		= THIS_MODULE,
652	.open		= etb_open,
653	.read		= etb_read,
654	.release	= etb_release,
 
655};
656
 
 
 
 
 
 
 
 
 
 
 
 
657static struct attribute *coresight_etb_mgmt_attrs[] = {
658	coresight_simple_reg32(rdp, ETB_RAM_DEPTH_REG),
659	coresight_simple_reg32(sts, ETB_STATUS_REG),
660	coresight_simple_reg32(rrp, ETB_RAM_READ_POINTER),
661	coresight_simple_reg32(rwp, ETB_RAM_WRITE_POINTER),
662	coresight_simple_reg32(trg, ETB_TRG),
663	coresight_simple_reg32(ctl, ETB_CTL_REG),
664	coresight_simple_reg32(ffsr, ETB_FFSR),
665	coresight_simple_reg32(ffcr, ETB_FFCR),
666	NULL,
667};
668
669static ssize_t trigger_cntr_show(struct device *dev,
670			    struct device_attribute *attr, char *buf)
671{
672	struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
673	unsigned long val = drvdata->trigger_cntr;
674
675	return sprintf(buf, "%#lx\n", val);
676}
677
678static ssize_t trigger_cntr_store(struct device *dev,
679			     struct device_attribute *attr,
680			     const char *buf, size_t size)
681{
682	int ret;
683	unsigned long val;
684	struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
685
686	ret = kstrtoul(buf, 16, &val);
687	if (ret)
688		return ret;
689
690	drvdata->trigger_cntr = val;
691	return size;
692}
693static DEVICE_ATTR_RW(trigger_cntr);
694
695static struct attribute *coresight_etb_attrs[] = {
696	&dev_attr_trigger_cntr.attr,
697	NULL,
698};
699
700static const struct attribute_group coresight_etb_group = {
701	.attrs = coresight_etb_attrs,
702};
703
704static const struct attribute_group coresight_etb_mgmt_group = {
705	.attrs = coresight_etb_mgmt_attrs,
706	.name = "mgmt",
707};
708
709static const struct attribute_group *coresight_etb_groups[] = {
710	&coresight_etb_group,
711	&coresight_etb_mgmt_group,
712	NULL,
713};
714
715static int etb_probe(struct amba_device *adev, const struct amba_id *id)
716{
717	int ret;
718	void __iomem *base;
719	struct device *dev = &adev->dev;
720	struct coresight_platform_data *pdata = NULL;
721	struct etb_drvdata *drvdata;
722	struct resource *res = &adev->res;
723	struct coresight_desc desc = { 0 };
 
724
725	desc.name = coresight_alloc_device_name(&etb_devs, dev);
726	if (!desc.name)
727		return -ENOMEM;
 
 
 
728
729	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
730	if (!drvdata)
731		return -ENOMEM;
732
 
733	drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
734	if (!IS_ERR(drvdata->atclk)) {
735		ret = clk_prepare_enable(drvdata->atclk);
736		if (ret)
737			return ret;
738	}
739	dev_set_drvdata(dev, drvdata);
740
741	/* validity for the resource is already checked by the AMBA core */
742	base = devm_ioremap_resource(dev, res);
743	if (IS_ERR(base))
744		return PTR_ERR(base);
745
746	drvdata->base = base;
747	desc.access = CSDEV_ACCESS_IOMEM(base);
748
749	spin_lock_init(&drvdata->spinlock);
750
751	drvdata->buffer_depth = etb_get_buffer_depth(drvdata);
 
752
753	if (drvdata->buffer_depth & 0x80000000)
754		return -EINVAL;
755
756	drvdata->buf = devm_kcalloc(dev,
757				    drvdata->buffer_depth, 4, GFP_KERNEL);
758	if (!drvdata->buf)
 
 
759		return -ENOMEM;
760
761	/* This device is not associated with a session */
762	drvdata->pid = -1;
763
764	pdata = coresight_get_platform_data(dev);
765	if (IS_ERR(pdata))
766		return PTR_ERR(pdata);
767	adev->dev.platform_data = pdata;
768
769	desc.type = CORESIGHT_DEV_TYPE_SINK;
770	desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
771	desc.ops = &etb_cs_ops;
772	desc.pdata = pdata;
773	desc.dev = dev;
774	desc.groups = coresight_etb_groups;
775	drvdata->csdev = coresight_register(&desc);
776	if (IS_ERR(drvdata->csdev))
777		return PTR_ERR(drvdata->csdev);
778
779	drvdata->miscdev.name = desc.name;
780	drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
781	drvdata->miscdev.fops = &etb_fops;
782	ret = misc_register(&drvdata->miscdev);
783	if (ret)
784		goto err_misc_register;
785
786	pm_runtime_put(&adev->dev);
787	return 0;
788
789err_misc_register:
790	coresight_unregister(drvdata->csdev);
791	return ret;
792}
793
794static void etb_remove(struct amba_device *adev)
795{
796	struct etb_drvdata *drvdata = dev_get_drvdata(&adev->dev);
797
798	/*
799	 * Since misc_open() holds a refcount on the f_ops, which is
800	 * etb fops in this case, device is there until last file
801	 * handler to this device is closed.
802	 */
803	misc_deregister(&drvdata->miscdev);
804	coresight_unregister(drvdata->csdev);
805}
806
807#ifdef CONFIG_PM
808static int etb_runtime_suspend(struct device *dev)
809{
810	struct etb_drvdata *drvdata = dev_get_drvdata(dev);
811
812	if (drvdata && !IS_ERR(drvdata->atclk))
813		clk_disable_unprepare(drvdata->atclk);
814
815	return 0;
816}
817
818static int etb_runtime_resume(struct device *dev)
819{
820	struct etb_drvdata *drvdata = dev_get_drvdata(dev);
821
822	if (drvdata && !IS_ERR(drvdata->atclk))
823		clk_prepare_enable(drvdata->atclk);
824
825	return 0;
826}
827#endif
828
829static const struct dev_pm_ops etb_dev_pm_ops = {
830	SET_RUNTIME_PM_OPS(etb_runtime_suspend, etb_runtime_resume, NULL)
831};
832
833static const struct amba_id etb_ids[] = {
834	{
835		.id	= 0x000bb907,
836		.mask	= 0x000fffff,
837	},
838	{ 0, 0, NULL },
839};
840
841MODULE_DEVICE_TABLE(amba, etb_ids);
842
843static struct amba_driver etb_driver = {
844	.drv = {
845		.name	= "coresight-etb10",
 
846		.pm	= &etb_dev_pm_ops,
847		.suppress_bind_attrs = true,
848
849	},
850	.probe		= etb_probe,
851	.remove		= etb_remove,
852	.id_table	= etb_ids,
853};
854
855module_amba_driver(etb_driver);
856
857MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
858MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
859MODULE_DESCRIPTION("Arm CoreSight Embedded Trace Buffer driver");
860MODULE_LICENSE("GPL v2");
v4.10.11
  1/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
 
 
  2 *
  3 * Description: CoreSight Embedded Trace Buffer driver
  4 *
  5 * This program is free software; you can redistribute it and/or modify
  6 * it under the terms of the GNU General Public License version 2 and
  7 * only version 2 as published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 */
 14
 15#include <asm/local.h>
 16#include <linux/kernel.h>
 17#include <linux/init.h>
 18#include <linux/types.h>
 19#include <linux/device.h>
 20#include <linux/io.h>
 21#include <linux/err.h>
 22#include <linux/fs.h>
 23#include <linux/miscdevice.h>
 24#include <linux/uaccess.h>
 25#include <linux/slab.h>
 26#include <linux/spinlock.h>
 27#include <linux/pm_runtime.h>
 28#include <linux/seq_file.h>
 29#include <linux/coresight.h>
 30#include <linux/amba/bus.h>
 31#include <linux/clk.h>
 32#include <linux/circ_buf.h>
 33#include <linux/mm.h>
 34#include <linux/perf_event.h>
 35
 36#include <asm/local.h>
 37
 38#include "coresight-priv.h"
 
 39
 40#define ETB_RAM_DEPTH_REG	0x004
 41#define ETB_STATUS_REG		0x00c
 42#define ETB_RAM_READ_DATA_REG	0x010
 43#define ETB_RAM_READ_POINTER	0x014
 44#define ETB_RAM_WRITE_POINTER	0x018
 45#define ETB_TRG			0x01c
 46#define ETB_CTL_REG		0x020
 47#define ETB_RWD_REG		0x024
 48#define ETB_FFSR		0x300
 49#define ETB_FFCR		0x304
 50#define ETB_ITMISCOP0		0xee0
 51#define ETB_ITTRFLINACK		0xee4
 52#define ETB_ITTRFLIN		0xee8
 53#define ETB_ITATBDATA0		0xeeC
 54#define ETB_ITATBCTR2		0xef0
 55#define ETB_ITATBCTR1		0xef4
 56#define ETB_ITATBCTR0		0xef8
 57
 58/* register description */
 59/* STS - 0x00C */
 60#define ETB_STATUS_RAM_FULL	BIT(0)
 61/* CTL - 0x020 */
 62#define ETB_CTL_CAPT_EN		BIT(0)
 63/* FFCR - 0x304 */
 64#define ETB_FFCR_EN_FTC		BIT(0)
 65#define ETB_FFCR_FON_MAN	BIT(6)
 66#define ETB_FFCR_STOP_FI	BIT(12)
 67#define ETB_FFCR_STOP_TRIGGER	BIT(13)
 68
 69#define ETB_FFCR_BIT		6
 70#define ETB_FFSR_BIT		1
 71#define ETB_FRAME_SIZE_WORDS	4
 72
 
 
 73/**
 74 * struct etb_drvdata - specifics associated to an ETB component
 75 * @base:	memory mapped base address for this component.
 76 * @dev:	the device entity associated to this component.
 77 * @atclk:	optional clock for the core parts of the ETB.
 78 * @csdev:	component vitals needed by the framework.
 79 * @miscdev:	specifics to handle "/dev/xyz.etb" entry.
 80 * @spinlock:	only one at a time pls.
 81 * @reading:	synchronise user space access to etb buffer.
 82 * @mode:	this ETB is being used.
 
 83 * @buf:	area of memory where ETB buffer content gets sent.
 84 * @buffer_depth: size of @buf.
 85 * @trigger_cntr: amount of words to store after a trigger.
 86 */
 87struct etb_drvdata {
 88	void __iomem		*base;
 89	struct device		*dev;
 90	struct clk		*atclk;
 91	struct coresight_device	*csdev;
 92	struct miscdevice	miscdev;
 93	spinlock_t		spinlock;
 94	local_t			reading;
 95	local_t			mode;
 96	u8			*buf;
 97	u32			buffer_depth;
 98	u32			trigger_cntr;
 99};
100
101static unsigned int etb_get_buffer_depth(struct etb_drvdata *drvdata)
 
 
 
102{
103	u32 depth = 0;
104
105	pm_runtime_get_sync(drvdata->dev);
106
107	/* RO registers don't need locking */
108	depth = readl_relaxed(drvdata->base + ETB_RAM_DEPTH_REG);
109
110	pm_runtime_put(drvdata->dev);
111	return depth;
112}
113
114static void etb_enable_hw(struct etb_drvdata *drvdata)
115{
116	int i;
117	u32 depth;
118
119	CS_UNLOCK(drvdata->base);
120
121	depth = drvdata->buffer_depth;
122	/* reset write RAM pointer address */
123	writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
124	/* clear entire RAM buffer */
125	for (i = 0; i < depth; i++)
126		writel_relaxed(0x0, drvdata->base + ETB_RWD_REG);
127
128	/* reset write RAM pointer address */
129	writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
130	/* reset read RAM pointer address */
131	writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
132
133	writel_relaxed(drvdata->trigger_cntr, drvdata->base + ETB_TRG);
134	writel_relaxed(ETB_FFCR_EN_FTC | ETB_FFCR_STOP_TRIGGER,
135		       drvdata->base + ETB_FFCR);
136	/* ETB trace capture enable */
137	writel_relaxed(ETB_CTL_CAPT_EN, drvdata->base + ETB_CTL_REG);
138
139	CS_LOCK(drvdata->base);
140}
141
142static int etb_enable(struct coresight_device *csdev, u32 mode)
143{
144	u32 val;
 
 
 
 
 
 
 
 
 
 
 
145	unsigned long flags;
146	struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
147
148	val = local_cmpxchg(&drvdata->mode,
149			    CS_MODE_DISABLED, mode);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150	/*
151	 * When accessing from Perf, a HW buffer can be handled
152	 * by a single trace entity.  In sysFS mode many tracers
153	 * can be logging to the same HW buffer.
154	 */
155	if (val == CS_MODE_PERF)
156		return -EBUSY;
 
 
157
158	/* Nothing to do, the tracer is already enabled. */
159	if (val == CS_MODE_SYSFS)
 
 
 
 
 
160		goto out;
161
162	spin_lock_irqsave(&drvdata->spinlock, flags);
163	etb_enable_hw(drvdata);
 
 
 
 
 
 
 
164	spin_unlock_irqrestore(&drvdata->spinlock, flags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
166out:
167	dev_info(drvdata->dev, "ETB enabled\n");
168	return 0;
169}
170
171static void etb_disable_hw(struct etb_drvdata *drvdata)
172{
173	u32 ffcr;
 
 
174
175	CS_UNLOCK(drvdata->base);
176
177	ffcr = readl_relaxed(drvdata->base + ETB_FFCR);
178	/* stop formatter when a stop has completed */
179	ffcr |= ETB_FFCR_STOP_FI;
180	writel_relaxed(ffcr, drvdata->base + ETB_FFCR);
181	/* manually generate a flush of the system */
182	ffcr |= ETB_FFCR_FON_MAN;
183	writel_relaxed(ffcr, drvdata->base + ETB_FFCR);
184
185	if (coresight_timeout(drvdata->base, ETB_FFCR, ETB_FFCR_BIT, 0)) {
186		dev_err(drvdata->dev,
187		"timeout while waiting for completion of Manual Flush\n");
188	}
189
190	/* disable trace capture */
191	writel_relaxed(0x0, drvdata->base + ETB_CTL_REG);
192
193	if (coresight_timeout(drvdata->base, ETB_FFSR, ETB_FFSR_BIT, 1)) {
194		dev_err(drvdata->dev,
195			"timeout while waiting for Formatter to Stop\n");
196	}
197
198	CS_LOCK(drvdata->base);
199}
200
201static void etb_dump_hw(struct etb_drvdata *drvdata)
202{
 
203	int i;
204	u8 *buf_ptr;
205	u32 read_data, depth;
206	u32 read_ptr, write_ptr;
207	u32 frame_off, frame_endoff;
 
208
209	CS_UNLOCK(drvdata->base);
210
211	read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
212	write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
213
214	frame_off = write_ptr % ETB_FRAME_SIZE_WORDS;
215	frame_endoff = ETB_FRAME_SIZE_WORDS - frame_off;
216	if (frame_off) {
217		dev_err(drvdata->dev,
218			"write_ptr: %lu not aligned to formatter frame size\n",
219			(unsigned long)write_ptr);
220		dev_err(drvdata->dev, "frameoff: %lu, frame_endoff: %lu\n",
221			(unsigned long)frame_off, (unsigned long)frame_endoff);
222		write_ptr += frame_endoff;
223	}
224
225	if ((readl_relaxed(drvdata->base + ETB_STATUS_REG)
226		      & ETB_STATUS_RAM_FULL) == 0)
227		writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
228	else
229		writel_relaxed(write_ptr, drvdata->base + ETB_RAM_READ_POINTER);
 
 
230
231	depth = drvdata->buffer_depth;
232	buf_ptr = drvdata->buf;
233	for (i = 0; i < depth; i++) {
234		read_data = readl_relaxed(drvdata->base +
235					  ETB_RAM_READ_DATA_REG);
236		*buf_ptr++ = read_data >> 0;
237		*buf_ptr++ = read_data >> 8;
238		*buf_ptr++ = read_data >> 16;
239		*buf_ptr++ = read_data >> 24;
240	}
241
 
 
 
242	if (frame_off) {
243		buf_ptr -= (frame_endoff * 4);
244		for (i = 0; i < frame_endoff; i++) {
245			*buf_ptr++ = 0x0;
246			*buf_ptr++ = 0x0;
247			*buf_ptr++ = 0x0;
248			*buf_ptr++ = 0x0;
249		}
250	}
251
252	writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
253
254	CS_LOCK(drvdata->base);
255}
256
257static void etb_disable(struct coresight_device *csdev)
 
 
 
 
 
 
 
258{
259	struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
260	unsigned long flags;
261
262	spin_lock_irqsave(&drvdata->spinlock, flags);
 
 
 
 
 
 
 
 
 
263	etb_disable_hw(drvdata);
264	etb_dump_hw(drvdata);
 
 
265	spin_unlock_irqrestore(&drvdata->spinlock, flags);
266
267	local_set(&drvdata->mode, CS_MODE_DISABLED);
268
269	dev_info(drvdata->dev, "ETB disabled\n");
270}
271
272static void *etb_alloc_buffer(struct coresight_device *csdev, int cpu,
273			      void **pages, int nr_pages, bool overwrite)
 
274{
275	int node;
276	struct cs_buffers *buf;
277
278	if (cpu == -1)
279		cpu = smp_processor_id();
280	node = cpu_to_node(cpu);
281
282	buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
283	if (!buf)
284		return NULL;
285
 
286	buf->snapshot = overwrite;
287	buf->nr_pages = nr_pages;
288	buf->data_pages = pages;
289
290	return buf;
291}
292
293static void etb_free_buffer(void *config)
294{
295	struct cs_buffers *buf = config;
296
297	kfree(buf);
298}
299
300static int etb_set_buffer(struct coresight_device *csdev,
301			  struct perf_output_handle *handle,
302			  void *sink_config)
303{
304	int ret = 0;
305	unsigned long head;
306	struct cs_buffers *buf = sink_config;
 
 
 
307
308	/* wrap head around to the amount of space we have */
309	head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
310
311	/* find the page to write to */
312	buf->cur = head / PAGE_SIZE;
313
314	/* and offset within that page */
315	buf->offset = head % PAGE_SIZE;
316
317	local_set(&buf->data_size, 0);
318
319	return ret;
320}
321
322static unsigned long etb_reset_buffer(struct coresight_device *csdev,
323				      struct perf_output_handle *handle,
324				      void *sink_config, bool *lost)
325{
326	unsigned long size = 0;
327	struct cs_buffers *buf = sink_config;
328
329	if (buf) {
330		/*
331		 * In snapshot mode ->data_size holds the new address of the
332		 * ring buffer's head.  The size itself is the whole address
333		 * range since we want the latest information.
334		 */
335		if (buf->snapshot)
336			handle->head = local_xchg(&buf->data_size,
337						  buf->nr_pages << PAGE_SHIFT);
338
339		/*
340		 * Tell the tracer PMU how much we got in this run and if
341		 * something went wrong along the way.  Nobody else can use
342		 * this cs_buffers instance until we are done.  As such
343		 * resetting parameters here and squaring off with the ring
344		 * buffer API in the tracer PMU is fine.
345		 */
346		*lost = !!local_xchg(&buf->lost, 0);
347		size = local_xchg(&buf->data_size, 0);
348	}
349
350	return size;
351}
352
353static void etb_update_buffer(struct coresight_device *csdev,
354			      struct perf_output_handle *handle,
355			      void *sink_config)
356{
 
357	int i, cur;
358	u8 *buf_ptr;
 
359	u32 read_ptr, write_ptr, capacity;
360	u32 status, read_data, to_read;
361	unsigned long offset;
362	struct cs_buffers *buf = sink_config;
363	struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
364
365	if (!buf)
366		return;
367
368	capacity = drvdata->buffer_depth * ETB_FRAME_SIZE_WORDS;
369
 
 
 
 
 
 
 
370	CS_UNLOCK(drvdata->base);
371	etb_disable_hw(drvdata);
372
373	/* unit is in words, not bytes */
374	read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
375	write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
376
377	/*
378	 * Entries should be aligned to the frame size.  If they are not
379	 * go back to the last alignement point to give decoding tools a
380	 * chance to fix things.
381	 */
382	if (write_ptr % ETB_FRAME_SIZE_WORDS) {
383		dev_err(drvdata->dev,
384			"write_ptr: %lu not aligned to formatter frame size\n",
385			(unsigned long)write_ptr);
386
387		write_ptr &= ~(ETB_FRAME_SIZE_WORDS - 1);
388		local_inc(&buf->lost);
389	}
390
391	/*
392	 * Get a hold of the status register and see if a wrap around
393	 * has occurred.  If so adjust things accordingly.  Otherwise
394	 * start at the beginning and go until the write pointer has
395	 * been reached.
396	 */
397	status = readl_relaxed(drvdata->base + ETB_STATUS_REG);
398	if (status & ETB_STATUS_RAM_FULL) {
399		local_inc(&buf->lost);
400		to_read = capacity;
401		read_ptr = write_ptr;
402	} else {
403		to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->buffer_depth);
404		to_read *= ETB_FRAME_SIZE_WORDS;
405	}
406
407	/*
408	 * Make sure we don't overwrite data that hasn't been consumed yet.
409	 * It is entirely possible that the HW buffer has more data than the
410	 * ring buffer can currently handle.  If so adjust the start address
411	 * to take only the last traces.
412	 *
413	 * In snapshot mode we are looking to get the latest traces only and as
414	 * such, we don't care about not overwriting data that hasn't been
415	 * processed by user space.
416	 */
417	if (!buf->snapshot && to_read > handle->size) {
418		u32 mask = ~(ETB_FRAME_SIZE_WORDS - 1);
419
420		/* The new read pointer must be frame size aligned */
421		to_read = handle->size & mask;
422		/*
423		 * Move the RAM read pointer up, keeping in mind that
424		 * everything is in frame size units.
425		 */
426		read_ptr = (write_ptr + drvdata->buffer_depth) -
427					to_read / ETB_FRAME_SIZE_WORDS;
428		/* Wrap around if need be*/
429		if (read_ptr > (drvdata->buffer_depth - 1))
430			read_ptr -= drvdata->buffer_depth;
431		/* let the decoder know we've skipped ahead */
432		local_inc(&buf->lost);
433	}
434
 
 
 
 
 
 
 
 
 
435	/* finally tell HW where we want to start reading from */
436	writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
437
438	cur = buf->cur;
439	offset = buf->offset;
 
 
440	for (i = 0; i < to_read; i += 4) {
441		buf_ptr = buf->data_pages[cur] + offset;
442		read_data = readl_relaxed(drvdata->base +
443					  ETB_RAM_READ_DATA_REG);
444		*buf_ptr++ = read_data >> 0;
445		*buf_ptr++ = read_data >> 8;
446		*buf_ptr++ = read_data >> 16;
447		*buf_ptr++ = read_data >> 24;
 
 
 
448
449		offset += 4;
450		if (offset >= PAGE_SIZE) {
451			offset = 0;
452			cur++;
453			/* wrap around at the end of the buffer */
454			cur &= buf->nr_pages - 1;
455		}
456	}
457
458	/* reset ETB buffer for next run */
459	writel_relaxed(0x0, drvdata->base + ETB_RAM_READ_POINTER);
460	writel_relaxed(0x0, drvdata->base + ETB_RAM_WRITE_POINTER);
461
462	/*
463	 * In snapshot mode all we have to do is communicate to
464	 * perf_aux_output_end() the address of the current head.  In full
465	 * trace mode the same function expects a size to move rb->aux_head
466	 * forward.
467	 */
468	if (buf->snapshot)
469		local_set(&buf->data_size, (cur * PAGE_SIZE) + offset);
470	else
471		local_add(to_read, &buf->data_size);
472
473	etb_enable_hw(drvdata);
474	CS_LOCK(drvdata->base);
 
 
 
 
475}
476
477static const struct coresight_ops_sink etb_sink_ops = {
478	.enable		= etb_enable,
479	.disable	= etb_disable,
480	.alloc_buffer	= etb_alloc_buffer,
481	.free_buffer	= etb_free_buffer,
482	.set_buffer	= etb_set_buffer,
483	.reset_buffer	= etb_reset_buffer,
484	.update_buffer	= etb_update_buffer,
485};
486
487static const struct coresight_ops etb_cs_ops = {
488	.sink_ops	= &etb_sink_ops,
489};
490
491static void etb_dump(struct etb_drvdata *drvdata)
492{
493	unsigned long flags;
494
495	spin_lock_irqsave(&drvdata->spinlock, flags);
496	if (local_read(&drvdata->mode) == CS_MODE_SYSFS) {
497		etb_disable_hw(drvdata);
498		etb_dump_hw(drvdata);
499		etb_enable_hw(drvdata);
500	}
501	spin_unlock_irqrestore(&drvdata->spinlock, flags);
502
503	dev_info(drvdata->dev, "ETB dumped\n");
504}
505
506static int etb_open(struct inode *inode, struct file *file)
507{
508	struct etb_drvdata *drvdata = container_of(file->private_data,
509						   struct etb_drvdata, miscdev);
510
511	if (local_cmpxchg(&drvdata->reading, 0, 1))
512		return -EBUSY;
513
514	dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
515	return 0;
516}
517
518static ssize_t etb_read(struct file *file, char __user *data,
519				size_t len, loff_t *ppos)
520{
521	u32 depth;
522	struct etb_drvdata *drvdata = container_of(file->private_data,
523						   struct etb_drvdata, miscdev);
 
524
525	etb_dump(drvdata);
526
527	depth = drvdata->buffer_depth;
528	if (*ppos + len > depth * 4)
529		len = depth * 4 - *ppos;
530
531	if (copy_to_user(data, drvdata->buf + *ppos, len)) {
532		dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
 
533		return -EFAULT;
534	}
535
536	*ppos += len;
537
538	dev_dbg(drvdata->dev, "%s: %zu bytes copied, %d bytes left\n",
539		__func__, len, (int)(depth * 4 - *ppos));
540	return len;
541}
542
543static int etb_release(struct inode *inode, struct file *file)
544{
545	struct etb_drvdata *drvdata = container_of(file->private_data,
546						   struct etb_drvdata, miscdev);
547	local_set(&drvdata->reading, 0);
548
549	dev_dbg(drvdata->dev, "%s: released\n", __func__);
550	return 0;
551}
552
553static const struct file_operations etb_fops = {
554	.owner		= THIS_MODULE,
555	.open		= etb_open,
556	.read		= etb_read,
557	.release	= etb_release,
558	.llseek		= no_llseek,
559};
560
561#define coresight_etb10_simple_func(name, offset)                       \
562	coresight_simple_func(struct etb_drvdata, NULL, name, offset)
563
564coresight_etb10_simple_func(rdp, ETB_RAM_DEPTH_REG);
565coresight_etb10_simple_func(sts, ETB_STATUS_REG);
566coresight_etb10_simple_func(rrp, ETB_RAM_READ_POINTER);
567coresight_etb10_simple_func(rwp, ETB_RAM_WRITE_POINTER);
568coresight_etb10_simple_func(trg, ETB_TRG);
569coresight_etb10_simple_func(ctl, ETB_CTL_REG);
570coresight_etb10_simple_func(ffsr, ETB_FFSR);
571coresight_etb10_simple_func(ffcr, ETB_FFCR);
572
573static struct attribute *coresight_etb_mgmt_attrs[] = {
574	&dev_attr_rdp.attr,
575	&dev_attr_sts.attr,
576	&dev_attr_rrp.attr,
577	&dev_attr_rwp.attr,
578	&dev_attr_trg.attr,
579	&dev_attr_ctl.attr,
580	&dev_attr_ffsr.attr,
581	&dev_attr_ffcr.attr,
582	NULL,
583};
584
585static ssize_t trigger_cntr_show(struct device *dev,
586			    struct device_attribute *attr, char *buf)
587{
588	struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
589	unsigned long val = drvdata->trigger_cntr;
590
591	return sprintf(buf, "%#lx\n", val);
592}
593
594static ssize_t trigger_cntr_store(struct device *dev,
595			     struct device_attribute *attr,
596			     const char *buf, size_t size)
597{
598	int ret;
599	unsigned long val;
600	struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
601
602	ret = kstrtoul(buf, 16, &val);
603	if (ret)
604		return ret;
605
606	drvdata->trigger_cntr = val;
607	return size;
608}
609static DEVICE_ATTR_RW(trigger_cntr);
610
611static struct attribute *coresight_etb_attrs[] = {
612	&dev_attr_trigger_cntr.attr,
613	NULL,
614};
615
616static const struct attribute_group coresight_etb_group = {
617	.attrs = coresight_etb_attrs,
618};
619
620static const struct attribute_group coresight_etb_mgmt_group = {
621	.attrs = coresight_etb_mgmt_attrs,
622	.name = "mgmt",
623};
624
625const struct attribute_group *coresight_etb_groups[] = {
626	&coresight_etb_group,
627	&coresight_etb_mgmt_group,
628	NULL,
629};
630
631static int etb_probe(struct amba_device *adev, const struct amba_id *id)
632{
633	int ret;
634	void __iomem *base;
635	struct device *dev = &adev->dev;
636	struct coresight_platform_data *pdata = NULL;
637	struct etb_drvdata *drvdata;
638	struct resource *res = &adev->res;
639	struct coresight_desc desc = { 0 };
640	struct device_node *np = adev->dev.of_node;
641
642	if (np) {
643		pdata = of_get_coresight_platform_data(dev, np);
644		if (IS_ERR(pdata))
645			return PTR_ERR(pdata);
646		adev->dev.platform_data = pdata;
647	}
648
649	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
650	if (!drvdata)
651		return -ENOMEM;
652
653	drvdata->dev = &adev->dev;
654	drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
655	if (!IS_ERR(drvdata->atclk)) {
656		ret = clk_prepare_enable(drvdata->atclk);
657		if (ret)
658			return ret;
659	}
660	dev_set_drvdata(dev, drvdata);
661
662	/* validity for the resource is already checked by the AMBA core */
663	base = devm_ioremap_resource(dev, res);
664	if (IS_ERR(base))
665		return PTR_ERR(base);
666
667	drvdata->base = base;
 
668
669	spin_lock_init(&drvdata->spinlock);
670
671	drvdata->buffer_depth = etb_get_buffer_depth(drvdata);
672	pm_runtime_put(&adev->dev);
673
674	if (drvdata->buffer_depth & 0x80000000)
675		return -EINVAL;
676
677	drvdata->buf = devm_kzalloc(dev,
678				    drvdata->buffer_depth * 4, GFP_KERNEL);
679	if (!drvdata->buf) {
680		dev_err(dev, "Failed to allocate %u bytes for buffer data\n",
681			drvdata->buffer_depth * 4);
682		return -ENOMEM;
683	}
 
 
 
 
 
 
 
684
685	desc.type = CORESIGHT_DEV_TYPE_SINK;
686	desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
687	desc.ops = &etb_cs_ops;
688	desc.pdata = pdata;
689	desc.dev = dev;
690	desc.groups = coresight_etb_groups;
691	drvdata->csdev = coresight_register(&desc);
692	if (IS_ERR(drvdata->csdev))
693		return PTR_ERR(drvdata->csdev);
694
695	drvdata->miscdev.name = pdata->name;
696	drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
697	drvdata->miscdev.fops = &etb_fops;
698	ret = misc_register(&drvdata->miscdev);
699	if (ret)
700		goto err_misc_register;
701
 
702	return 0;
703
704err_misc_register:
705	coresight_unregister(drvdata->csdev);
706	return ret;
707}
708
 
 
 
 
 
 
 
 
 
 
 
 
 
709#ifdef CONFIG_PM
710static int etb_runtime_suspend(struct device *dev)
711{
712	struct etb_drvdata *drvdata = dev_get_drvdata(dev);
713
714	if (drvdata && !IS_ERR(drvdata->atclk))
715		clk_disable_unprepare(drvdata->atclk);
716
717	return 0;
718}
719
720static int etb_runtime_resume(struct device *dev)
721{
722	struct etb_drvdata *drvdata = dev_get_drvdata(dev);
723
724	if (drvdata && !IS_ERR(drvdata->atclk))
725		clk_prepare_enable(drvdata->atclk);
726
727	return 0;
728}
729#endif
730
731static const struct dev_pm_ops etb_dev_pm_ops = {
732	SET_RUNTIME_PM_OPS(etb_runtime_suspend, etb_runtime_resume, NULL)
733};
734
735static struct amba_id etb_ids[] = {
736	{
737		.id	= 0x0003b907,
738		.mask	= 0x0003ffff,
739	},
740	{ 0, 0},
741};
742
 
 
743static struct amba_driver etb_driver = {
744	.drv = {
745		.name	= "coresight-etb10",
746		.owner	= THIS_MODULE,
747		.pm	= &etb_dev_pm_ops,
748		.suppress_bind_attrs = true,
749
750	},
751	.probe		= etb_probe,
 
752	.id_table	= etb_ids,
753};
754builtin_amba_driver(etb_driver);