coresight: etb10: implementing AUX API
[firefly-linux-kernel-4.4.55.git] / drivers / hwtracing / coresight / coresight-etb10.c
1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
2  *
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.
6  *
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.
11  */
12
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>
19 #include <linux/io.h>
20 #include <linux/err.h>
21 #include <linux/fs.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>
32 #include <linux/mm.h>
33 #include <linux/perf_event.h>
34
35 #include <asm/local.h>
36
37 #include "coresight-priv.h"
38
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
44 #define ETB_TRG                 0x01c
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
56
57 /* register description */
58 /* STS - 0x00C */
59 #define ETB_STATUS_RAM_FULL     BIT(0)
60 /* CTL - 0x020 */
61 #define ETB_CTL_CAPT_EN         BIT(0)
62 /* FFCR - 0x304 */
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)
67
68 #define ETB_FFCR_BIT            6
69 #define ETB_FFSR_BIT            1
70 #define ETB_FRAME_SIZE_WORDS    4
71
72 /**
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
81  */
82 struct cs_buffers {
83         unsigned int            cur;
84         unsigned int            nr_pages;
85         unsigned long           offset;
86         local_t                 data_size;
87         local_t                 lost;
88         bool                    snapshot;
89         void                    **data_pages;
90 };
91
92 /**
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.
105  */
106 struct etb_drvdata {
107         void __iomem            *base;
108         struct device           *dev;
109         struct clk              *atclk;
110         struct coresight_device *csdev;
111         struct miscdevice       miscdev;
112         spinlock_t              spinlock;
113         local_t                 reading;
114         local_t                 mode;
115         u8                      *buf;
116         u32                     buffer_depth;
117         u32                     trigger_cntr;
118 };
119
120 static unsigned int etb_get_buffer_depth(struct etb_drvdata *drvdata)
121 {
122         u32 depth = 0;
123
124         pm_runtime_get_sync(drvdata->dev);
125
126         /* RO registers don't need locking */
127         depth = readl_relaxed(drvdata->base + ETB_RAM_DEPTH_REG);
128
129         pm_runtime_put(drvdata->dev);
130         return depth;
131 }
132
133 static void etb_enable_hw(struct etb_drvdata *drvdata)
134 {
135         int i;
136         u32 depth;
137
138         CS_UNLOCK(drvdata->base);
139
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);
146
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);
151
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);
157
158         CS_LOCK(drvdata->base);
159 }
160
161 static int etb_enable(struct coresight_device *csdev, u32 mode)
162 {
163         u32 val;
164         unsigned long flags;
165         struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
166
167         val = local_cmpxchg(&drvdata->mode,
168                             CS_MODE_DISABLED, mode);
169         /*
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.
173          */
174         if (val == CS_MODE_PERF)
175                 return -EBUSY;
176
177         /* Nothing to do, the tracer is already enabled. */
178         if (val == CS_MODE_SYSFS)
179                 goto out;
180
181         spin_lock_irqsave(&drvdata->spinlock, flags);
182         etb_enable_hw(drvdata);
183         spin_unlock_irqrestore(&drvdata->spinlock, flags);
184
185 out:
186         dev_info(drvdata->dev, "ETB enabled\n");
187         return 0;
188 }
189
190 static void etb_disable_hw(struct etb_drvdata *drvdata)
191 {
192         u32 ffcr;
193
194         CS_UNLOCK(drvdata->base);
195
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);
203
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",
207                         ETB_FFCR);
208         }
209
210         /* disable trace capture */
211         writel_relaxed(0x0, drvdata->base + ETB_CTL_REG);
212
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",
216                         ETB_FFCR);
217         }
218
219         CS_LOCK(drvdata->base);
220 }
221
222 static void etb_dump_hw(struct etb_drvdata *drvdata)
223 {
224         int i;
225         u8 *buf_ptr;
226         u32 read_data, depth;
227         u32 read_ptr, write_ptr;
228         u32 frame_off, frame_endoff;
229
230         CS_UNLOCK(drvdata->base);
231
232         read_ptr = readl_relaxed(drvdata->base + ETB_RAM_READ_POINTER);
233         write_ptr = readl_relaxed(drvdata->base + ETB_RAM_WRITE_POINTER);
234
235         frame_off = write_ptr % ETB_FRAME_SIZE_WORDS;
236         frame_endoff = ETB_FRAME_SIZE_WORDS - frame_off;
237         if (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;
244         }
245
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);
249         else
250                 writel_relaxed(write_ptr, drvdata->base + ETB_RAM_READ_POINTER);
251
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;
261         }
262
263         if (frame_off) {
264                 buf_ptr -= (frame_endoff * 4);
265                 for (i = 0; i < frame_endoff; i++) {
266                         *buf_ptr++ = 0x0;
267                         *buf_ptr++ = 0x0;
268                         *buf_ptr++ = 0x0;
269                         *buf_ptr++ = 0x0;
270                 }
271         }
272
273         writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
274
275         CS_LOCK(drvdata->base);
276 }
277
278 static void etb_disable(struct coresight_device *csdev)
279 {
280         struct etb_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
281         unsigned long flags;
282
283         spin_lock_irqsave(&drvdata->spinlock, flags);
284         etb_disable_hw(drvdata);
285         etb_dump_hw(drvdata);
286         spin_unlock_irqrestore(&drvdata->spinlock, flags);
287
288         local_set(&drvdata->mode, CS_MODE_DISABLED);
289
290         dev_info(drvdata->dev, "ETB disabled\n");
291 }
292
293 static void *etb_alloc_buffer(struct coresight_device *csdev, int cpu,
294                               void **pages, int nr_pages, bool overwrite)
295 {
296         int node;
297         struct cs_buffers *buf;
298
299         if (cpu == -1)
300                 cpu = smp_processor_id();
301         node = cpu_to_node(cpu);
302
303         buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
304         if (!buf)
305                 return NULL;
306
307         buf->snapshot = overwrite;
308         buf->nr_pages = nr_pages;
309         buf->data_pages = pages;
310
311         return buf;
312 }
313
314 static void etb_free_buffer(void *config)
315 {
316         struct cs_buffers *buf = config;
317
318         kfree(buf);
319 }
320
321 static int etb_set_buffer(struct coresight_device *csdev,
322                           struct perf_output_handle *handle,
323                           void *sink_config)
324 {
325         int ret = 0;
326         unsigned long head;
327         struct cs_buffers *buf = sink_config;
328
329         /* wrap head around to the amount of space we have */
330         head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
331
332         /* find the page to write to */
333         buf->cur = head / PAGE_SIZE;
334
335         /* and offset within that page */
336         buf->offset = head % PAGE_SIZE;
337
338         local_set(&buf->data_size, 0);
339
340         return ret;
341 }
342
343 static unsigned long etb_reset_buffer(struct coresight_device *csdev,
344                                       struct perf_output_handle *handle,
345                                       void *sink_config, bool *lost)
346 {
347         unsigned long size = 0;
348         struct cs_buffers *buf = sink_config;
349
350         if (buf) {
351                 /*
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.
355                  */
356                 if (buf->snapshot)
357                         handle->head = local_xchg(&buf->data_size,
358                                                   buf->nr_pages << PAGE_SHIFT);
359
360                 /*
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.
366                  */
367                 *lost = !!local_xchg(&buf->lost, 0);
368                 size = local_xchg(&buf->data_size, 0);
369         }
370
371         return size;
372 }
373
374 static void etb_update_buffer(struct coresight_device *csdev,
375                               struct perf_output_handle *handle,
376                               void *sink_config)
377 {
378         int i, cur;
379         u8 *buf_ptr;
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);
385
386         if (!buf)
387                 return;
388
389         capacity = drvdata->buffer_depth * ETB_FRAME_SIZE_WORDS;
390
391         CS_UNLOCK(drvdata->base);
392         etb_disable_hw(drvdata);
393
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);
397
398         /*
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.
402          */
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);
407
408                 write_ptr &= ~(ETB_FRAME_SIZE_WORDS - 1);
409                 local_inc(&buf->lost);
410         }
411
412         /*
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
416          * been reached.
417          */
418         status = readl_relaxed(drvdata->base + ETB_STATUS_REG);
419         if (status & ETB_STATUS_RAM_FULL) {
420                 local_inc(&buf->lost);
421                 to_read = capacity;
422                 read_ptr = write_ptr;
423         } else {
424                 to_read = CIRC_CNT(write_ptr, read_ptr, drvdata->buffer_depth);
425                 to_read *= ETB_FRAME_SIZE_WORDS;
426         }
427
428         /*
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.
433          *
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.
437          */
438         if (!buf->snapshot && to_read > handle->size) {
439                 u32 mask = ~(ETB_FRAME_SIZE_WORDS - 1);
440
441                 /* The new read pointer must be frame size aligned */
442                 to_read -= handle->size & mask;
443                 /*
444                  * Move the RAM read pointer up, keeping in mind that
445                  * everything is in frame size units.
446                  */
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);
453         }
454
455         /* finally tell HW where we want to start reading from */
456         writel_relaxed(read_ptr, drvdata->base + ETB_RAM_READ_POINTER);
457
458         cur = buf->cur;
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;
468
469                 offset += 4;
470                 if (offset >= PAGE_SIZE) {
471                         offset = 0;
472                         cur++;
473                         /* wrap around at the end of the buffer */
474                         cur &= buf->nr_pages - 1;
475                 }
476         }
477
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);
481
482         /*
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
486          * forward.
487          */
488         if (buf->snapshot)
489                 local_set(&buf->data_size, (cur * PAGE_SIZE) + offset);
490         else
491                 local_add(to_read, &buf->data_size);
492
493         etb_enable_hw(drvdata);
494         CS_LOCK(drvdata->base);
495 }
496
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,
505 };
506
507 static const struct coresight_ops etb_cs_ops = {
508         .sink_ops       = &etb_sink_ops,
509 };
510
511 static void etb_dump(struct etb_drvdata *drvdata)
512 {
513         unsigned long flags;
514
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);
520         }
521         spin_unlock_irqrestore(&drvdata->spinlock, flags);
522
523         dev_info(drvdata->dev, "ETB dumped\n");
524 }
525
526 static int etb_open(struct inode *inode, struct file *file)
527 {
528         struct etb_drvdata *drvdata = container_of(file->private_data,
529                                                    struct etb_drvdata, miscdev);
530
531         if (local_cmpxchg(&drvdata->reading, 0, 1))
532                 return -EBUSY;
533
534         dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
535         return 0;
536 }
537
538 static ssize_t etb_read(struct file *file, char __user *data,
539                                 size_t len, loff_t *ppos)
540 {
541         u32 depth;
542         struct etb_drvdata *drvdata = container_of(file->private_data,
543                                                    struct etb_drvdata, miscdev);
544
545         etb_dump(drvdata);
546
547         depth = drvdata->buffer_depth;
548         if (*ppos + len > depth * 4)
549                 len = depth * 4 - *ppos;
550
551         if (copy_to_user(data, drvdata->buf + *ppos, len)) {
552                 dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
553                 return -EFAULT;
554         }
555
556         *ppos += len;
557
558         dev_dbg(drvdata->dev, "%s: %zu bytes copied, %d bytes left\n",
559                 __func__, len, (int)(depth * 4 - *ppos));
560         return len;
561 }
562
563 static int etb_release(struct inode *inode, struct file *file)
564 {
565         struct etb_drvdata *drvdata = container_of(file->private_data,
566                                                    struct etb_drvdata, miscdev);
567         local_set(&drvdata->reading, 0);
568
569         dev_dbg(drvdata->dev, "%s: released\n", __func__);
570         return 0;
571 }
572
573 static const struct file_operations etb_fops = {
574         .owner          = THIS_MODULE,
575         .open           = etb_open,
576         .read           = etb_read,
577         .release        = etb_release,
578         .llseek         = no_llseek,
579 };
580
581 static ssize_t status_show(struct device *dev,
582                            struct device_attribute *attr, char *buf)
583 {
584         unsigned long flags;
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);
588
589         pm_runtime_get_sync(drvdata->dev);
590         spin_lock_irqsave(&drvdata->spinlock, flags);
591         CS_UNLOCK(drvdata->base);
592
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);
601
602         CS_LOCK(drvdata->base);
603         spin_unlock_irqrestore(&drvdata->spinlock, flags);
604
605         pm_runtime_put(drvdata->dev);
606
607         return sprintf(buf,
608                        "Depth:\t\t0x%x\n"
609                        "Status:\t\t0x%x\n"
610                        "RAM read ptr:\t0x%x\n"
611                        "RAM wrt ptr:\t0x%x\n"
612                        "Trigger cnt:\t0x%x\n"
613                        "Control:\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);
618
619         return -EINVAL;
620 }
621 static DEVICE_ATTR_RO(status);
622
623 static ssize_t trigger_cntr_show(struct device *dev,
624                             struct device_attribute *attr, char *buf)
625 {
626         struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
627         unsigned long val = drvdata->trigger_cntr;
628
629         return sprintf(buf, "%#lx\n", val);
630 }
631
632 static ssize_t trigger_cntr_store(struct device *dev,
633                              struct device_attribute *attr,
634                              const char *buf, size_t size)
635 {
636         int ret;
637         unsigned long val;
638         struct etb_drvdata *drvdata = dev_get_drvdata(dev->parent);
639
640         ret = kstrtoul(buf, 16, &val);
641         if (ret)
642                 return ret;
643
644         drvdata->trigger_cntr = val;
645         return size;
646 }
647 static DEVICE_ATTR_RW(trigger_cntr);
648
649 static struct attribute *coresight_etb_attrs[] = {
650         &dev_attr_trigger_cntr.attr,
651         &dev_attr_status.attr,
652         NULL,
653 };
654 ATTRIBUTE_GROUPS(coresight_etb);
655
656 static int etb_probe(struct amba_device *adev, const struct amba_id *id)
657 {
658         int ret;
659         void __iomem *base;
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;
666
667         if (np) {
668                 pdata = of_get_coresight_platform_data(dev, np);
669                 if (IS_ERR(pdata))
670                         return PTR_ERR(pdata);
671                 adev->dev.platform_data = pdata;
672         }
673
674         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
675         if (!drvdata)
676                 return -ENOMEM;
677
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);
682                 if (ret)
683                         return ret;
684         }
685         dev_set_drvdata(dev, drvdata);
686
687         /* validity for the resource is already checked by the AMBA core */
688         base = devm_ioremap_resource(dev, res);
689         if (IS_ERR(base))
690                 return PTR_ERR(base);
691
692         drvdata->base = base;
693
694         spin_lock_init(&drvdata->spinlock);
695
696         drvdata->buffer_depth = etb_get_buffer_depth(drvdata);
697         pm_runtime_put(&adev->dev);
698
699         if (drvdata->buffer_depth & 0x80000000)
700                 return -EINVAL;
701
702         drvdata->buf = devm_kzalloc(dev,
703                                     drvdata->buffer_depth * 4, GFP_KERNEL);
704         if (!drvdata->buf) {
705                 dev_err(dev, "Failed to allocate %u bytes for buffer data\n",
706                         drvdata->buffer_depth * 4);
707                 return -ENOMEM;
708         }
709
710         desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
711         if (!desc)
712                 return -ENOMEM;
713
714         desc->type = CORESIGHT_DEV_TYPE_SINK;
715         desc->subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
716         desc->ops = &etb_cs_ops;
717         desc->pdata = pdata;
718         desc->dev = dev;
719         desc->groups = coresight_etb_groups;
720         drvdata->csdev = coresight_register(desc);
721         if (IS_ERR(drvdata->csdev))
722                 return PTR_ERR(drvdata->csdev);
723
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);
728         if (ret)
729                 goto err_misc_register;
730
731         dev_info(dev, "ETB initialized\n");
732         return 0;
733
734 err_misc_register:
735         coresight_unregister(drvdata->csdev);
736         return ret;
737 }
738
739 #ifdef CONFIG_PM
740 static int etb_runtime_suspend(struct device *dev)
741 {
742         struct etb_drvdata *drvdata = dev_get_drvdata(dev);
743
744         if (drvdata && !IS_ERR(drvdata->atclk))
745                 clk_disable_unprepare(drvdata->atclk);
746
747         return 0;
748 }
749
750 static int etb_runtime_resume(struct device *dev)
751 {
752         struct etb_drvdata *drvdata = dev_get_drvdata(dev);
753
754         if (drvdata && !IS_ERR(drvdata->atclk))
755                 clk_prepare_enable(drvdata->atclk);
756
757         return 0;
758 }
759 #endif
760
761 static const struct dev_pm_ops etb_dev_pm_ops = {
762         SET_RUNTIME_PM_OPS(etb_runtime_suspend, etb_runtime_resume, NULL)
763 };
764
765 static struct amba_id etb_ids[] = {
766         {
767                 .id     = 0x0003b907,
768                 .mask   = 0x0003ffff,
769         },
770         { 0, 0},
771 };
772
773 static struct amba_driver etb_driver = {
774         .drv = {
775                 .name   = "coresight-etb10",
776                 .owner  = THIS_MODULE,
777                 .pm     = &etb_dev_pm_ops,
778                 .suppress_bind_attrs = true,
779
780         },
781         .probe          = etb_probe,
782         .id_table       = etb_ids,
783 };
784
785 module_amba_driver(etb_driver);
786
787 MODULE_LICENSE("GPL v2");
788 MODULE_DESCRIPTION("CoreSight Embedded Trace Buffer driver");