GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / hwtracing / coresight / coresight-etm4x-sysfs.c
1 /*
2  * Copyright(C) 2015 Linaro Limited. All rights reserved.
3  * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <linux/pm_runtime.h>
19 #include <linux/sysfs.h>
20 #include "coresight-etm4x.h"
21 #include "coresight-priv.h"
22
23 static int etm4_set_mode_exclude(struct etmv4_drvdata *drvdata, bool exclude)
24 {
25         u8 idx;
26         struct etmv4_config *config = &drvdata->config;
27
28         idx = config->addr_idx;
29
30         /*
31          * TRCACATRn.TYPE bit[1:0]: type of comparison
32          * the trace unit performs
33          */
34         if (BMVAL(config->addr_acc[idx], 0, 1) == ETM_INSTR_ADDR) {
35                 if (idx % 2 != 0)
36                         return -EINVAL;
37
38                 /*
39                  * We are performing instruction address comparison. Set the
40                  * relevant bit of ViewInst Include/Exclude Control register
41                  * for corresponding address comparator pair.
42                  */
43                 if (config->addr_type[idx] != ETM_ADDR_TYPE_RANGE ||
44                     config->addr_type[idx + 1] != ETM_ADDR_TYPE_RANGE)
45                         return -EINVAL;
46
47                 if (exclude == true) {
48                         /*
49                          * Set exclude bit and unset the include bit
50                          * corresponding to comparator pair
51                          */
52                         config->viiectlr |= BIT(idx / 2 + 16);
53                         config->viiectlr &= ~BIT(idx / 2);
54                 } else {
55                         /*
56                          * Set include bit and unset exclude bit
57                          * corresponding to comparator pair
58                          */
59                         config->viiectlr |= BIT(idx / 2);
60                         config->viiectlr &= ~BIT(idx / 2 + 16);
61                 }
62         }
63         return 0;
64 }
65
66 static ssize_t nr_pe_cmp_show(struct device *dev,
67                               struct device_attribute *attr,
68                               char *buf)
69 {
70         unsigned long val;
71         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
72
73         val = drvdata->nr_pe_cmp;
74         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
75 }
76 static DEVICE_ATTR_RO(nr_pe_cmp);
77
78 static ssize_t nr_addr_cmp_show(struct device *dev,
79                                 struct device_attribute *attr,
80                                 char *buf)
81 {
82         unsigned long val;
83         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
84
85         val = drvdata->nr_addr_cmp;
86         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
87 }
88 static DEVICE_ATTR_RO(nr_addr_cmp);
89
90 static ssize_t nr_cntr_show(struct device *dev,
91                             struct device_attribute *attr,
92                             char *buf)
93 {
94         unsigned long val;
95         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
96
97         val = drvdata->nr_cntr;
98         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
99 }
100 static DEVICE_ATTR_RO(nr_cntr);
101
102 static ssize_t nr_ext_inp_show(struct device *dev,
103                                struct device_attribute *attr,
104                                char *buf)
105 {
106         unsigned long val;
107         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
108
109         val = drvdata->nr_ext_inp;
110         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
111 }
112 static DEVICE_ATTR_RO(nr_ext_inp);
113
114 static ssize_t numcidc_show(struct device *dev,
115                             struct device_attribute *attr,
116                             char *buf)
117 {
118         unsigned long val;
119         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
120
121         val = drvdata->numcidc;
122         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
123 }
124 static DEVICE_ATTR_RO(numcidc);
125
126 static ssize_t numvmidc_show(struct device *dev,
127                              struct device_attribute *attr,
128                              char *buf)
129 {
130         unsigned long val;
131         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
132
133         val = drvdata->numvmidc;
134         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
135 }
136 static DEVICE_ATTR_RO(numvmidc);
137
138 static ssize_t nrseqstate_show(struct device *dev,
139                                struct device_attribute *attr,
140                                char *buf)
141 {
142         unsigned long val;
143         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
144
145         val = drvdata->nrseqstate;
146         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
147 }
148 static DEVICE_ATTR_RO(nrseqstate);
149
150 static ssize_t nr_resource_show(struct device *dev,
151                                 struct device_attribute *attr,
152                                 char *buf)
153 {
154         unsigned long val;
155         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
156
157         val = drvdata->nr_resource;
158         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
159 }
160 static DEVICE_ATTR_RO(nr_resource);
161
162 static ssize_t nr_ss_cmp_show(struct device *dev,
163                               struct device_attribute *attr,
164                               char *buf)
165 {
166         unsigned long val;
167         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
168
169         val = drvdata->nr_ss_cmp;
170         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
171 }
172 static DEVICE_ATTR_RO(nr_ss_cmp);
173
174 static ssize_t reset_store(struct device *dev,
175                            struct device_attribute *attr,
176                            const char *buf, size_t size)
177 {
178         int i;
179         unsigned long val;
180         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
181         struct etmv4_config *config = &drvdata->config;
182
183         if (kstrtoul(buf, 16, &val))
184                 return -EINVAL;
185
186         spin_lock(&drvdata->spinlock);
187         if (val)
188                 config->mode = 0x0;
189
190         /* Disable data tracing: do not trace load and store data transfers */
191         config->mode &= ~(ETM_MODE_LOAD | ETM_MODE_STORE);
192         config->cfg &= ~(BIT(1) | BIT(2));
193
194         /* Disable data value and data address tracing */
195         config->mode &= ~(ETM_MODE_DATA_TRACE_ADDR |
196                            ETM_MODE_DATA_TRACE_VAL);
197         config->cfg &= ~(BIT(16) | BIT(17));
198
199         /* Disable all events tracing */
200         config->eventctrl0 = 0x0;
201         config->eventctrl1 = 0x0;
202
203         /* Disable timestamp event */
204         config->ts_ctrl = 0x0;
205
206         /* Disable stalling */
207         config->stall_ctrl = 0x0;
208
209         /* Reset trace synchronization period  to 2^8 = 256 bytes*/
210         if (drvdata->syncpr == false)
211                 config->syncfreq = 0x8;
212
213         /*
214          * Enable ViewInst to trace everything with start-stop logic in
215          * started state. ARM recommends start-stop logic is set before
216          * each trace run.
217          */
218         config->vinst_ctrl |= BIT(0);
219         if (drvdata->nr_addr_cmp == true) {
220                 config->mode |= ETM_MODE_VIEWINST_STARTSTOP;
221                 /* SSSTATUS, bit[9] */
222                 config->vinst_ctrl |= BIT(9);
223         }
224
225         /* No address range filtering for ViewInst */
226         config->viiectlr = 0x0;
227
228         /* No start-stop filtering for ViewInst */
229         config->vissctlr = 0x0;
230
231         /* Disable seq events */
232         for (i = 0; i < drvdata->nrseqstate-1; i++)
233                 config->seq_ctrl[i] = 0x0;
234         config->seq_rst = 0x0;
235         config->seq_state = 0x0;
236
237         /* Disable external input events */
238         config->ext_inp = 0x0;
239
240         config->cntr_idx = 0x0;
241         for (i = 0; i < drvdata->nr_cntr; i++) {
242                 config->cntrldvr[i] = 0x0;
243                 config->cntr_ctrl[i] = 0x0;
244                 config->cntr_val[i] = 0x0;
245         }
246
247         config->res_idx = 0x0;
248         for (i = 0; i < drvdata->nr_resource; i++)
249                 config->res_ctrl[i] = 0x0;
250
251         for (i = 0; i < drvdata->nr_ss_cmp; i++) {
252                 config->ss_ctrl[i] = 0x0;
253                 config->ss_pe_cmp[i] = 0x0;
254         }
255
256         config->addr_idx = 0x0;
257         for (i = 0; i < drvdata->nr_addr_cmp * 2; i++) {
258                 config->addr_val[i] = 0x0;
259                 config->addr_acc[i] = 0x0;
260                 config->addr_type[i] = ETM_ADDR_TYPE_NONE;
261         }
262
263         config->ctxid_idx = 0x0;
264         for (i = 0; i < drvdata->numcidc; i++) {
265                 config->ctxid_pid[i] = 0x0;
266                 config->ctxid_vpid[i] = 0x0;
267         }
268
269         config->ctxid_mask0 = 0x0;
270         config->ctxid_mask1 = 0x0;
271
272         config->vmid_idx = 0x0;
273         for (i = 0; i < drvdata->numvmidc; i++)
274                 config->vmid_val[i] = 0x0;
275         config->vmid_mask0 = 0x0;
276         config->vmid_mask1 = 0x0;
277
278         drvdata->trcid = drvdata->cpu + 1;
279
280         spin_unlock(&drvdata->spinlock);
281
282         return size;
283 }
284 static DEVICE_ATTR_WO(reset);
285
286 static ssize_t mode_show(struct device *dev,
287                          struct device_attribute *attr,
288                          char *buf)
289 {
290         unsigned long val;
291         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
292         struct etmv4_config *config = &drvdata->config;
293
294         val = config->mode;
295         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
296 }
297
298 static ssize_t mode_store(struct device *dev,
299                           struct device_attribute *attr,
300                           const char *buf, size_t size)
301 {
302         unsigned long val, mode;
303         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
304         struct etmv4_config *config = &drvdata->config;
305
306         if (kstrtoul(buf, 16, &val))
307                 return -EINVAL;
308
309         spin_lock(&drvdata->spinlock);
310         config->mode = val & ETMv4_MODE_ALL;
311
312         if (config->mode & ETM_MODE_EXCLUDE)
313                 etm4_set_mode_exclude(drvdata, true);
314         else
315                 etm4_set_mode_exclude(drvdata, false);
316
317         if (drvdata->instrp0 == true) {
318                 /* start by clearing instruction P0 field */
319                 config->cfg  &= ~(BIT(1) | BIT(2));
320                 if (config->mode & ETM_MODE_LOAD)
321                         /* 0b01 Trace load instructions as P0 instructions */
322                         config->cfg  |= BIT(1);
323                 if (config->mode & ETM_MODE_STORE)
324                         /* 0b10 Trace store instructions as P0 instructions */
325                         config->cfg  |= BIT(2);
326                 if (config->mode & ETM_MODE_LOAD_STORE)
327                         /*
328                          * 0b11 Trace load and store instructions
329                          * as P0 instructions
330                          */
331                         config->cfg  |= BIT(1) | BIT(2);
332         }
333
334         /* bit[3], Branch broadcast mode */
335         if ((config->mode & ETM_MODE_BB) && (drvdata->trcbb == true))
336                 config->cfg |= BIT(3);
337         else
338                 config->cfg &= ~BIT(3);
339
340         /* bit[4], Cycle counting instruction trace bit */
341         if ((config->mode & ETMv4_MODE_CYCACC) &&
342                 (drvdata->trccci == true))
343                 config->cfg |= BIT(4);
344         else
345                 config->cfg &= ~BIT(4);
346
347         /* bit[6], Context ID tracing bit */
348         if ((config->mode & ETMv4_MODE_CTXID) && (drvdata->ctxid_size))
349                 config->cfg |= BIT(6);
350         else
351                 config->cfg &= ~BIT(6);
352
353         if ((config->mode & ETM_MODE_VMID) && (drvdata->vmid_size))
354                 config->cfg |= BIT(7);
355         else
356                 config->cfg &= ~BIT(7);
357
358         /* bits[10:8], Conditional instruction tracing bit */
359         mode = ETM_MODE_COND(config->mode);
360         if (drvdata->trccond == true) {
361                 config->cfg &= ~(BIT(8) | BIT(9) | BIT(10));
362                 config->cfg |= mode << 8;
363         }
364
365         /* bit[11], Global timestamp tracing bit */
366         if ((config->mode & ETMv4_MODE_TIMESTAMP) && (drvdata->ts_size))
367                 config->cfg |= BIT(11);
368         else
369                 config->cfg &= ~BIT(11);
370
371         /* bit[12], Return stack enable bit */
372         if ((config->mode & ETM_MODE_RETURNSTACK) &&
373                                         (drvdata->retstack == true))
374                 config->cfg |= BIT(12);
375         else
376                 config->cfg &= ~BIT(12);
377
378         /* bits[14:13], Q element enable field */
379         mode = ETM_MODE_QELEM(config->mode);
380         /* start by clearing QE bits */
381         config->cfg &= ~(BIT(13) | BIT(14));
382         /*
383          * if supported, Q elements with instruction counts are enabled.
384          * Always set the low bit for any requested mode. Valid combos are
385          * 0b00, 0b01 and 0b11.
386          */
387         if (mode && drvdata->q_support)
388                 config->cfg |= BIT(13);
389         /*
390          * if supported, Q elements with and without instruction
391          * counts are enabled
392          */
393         if ((mode & BIT(1)) && (drvdata->q_support & BIT(1)))
394                 config->cfg |= BIT(14);
395
396         /* bit[11], AMBA Trace Bus (ATB) trigger enable bit */
397         if ((config->mode & ETM_MODE_ATB_TRIGGER) &&
398             (drvdata->atbtrig == true))
399                 config->eventctrl1 |= BIT(11);
400         else
401                 config->eventctrl1 &= ~BIT(11);
402
403         /* bit[12], Low-power state behavior override bit */
404         if ((config->mode & ETM_MODE_LPOVERRIDE) &&
405             (drvdata->lpoverride == true))
406                 config->eventctrl1 |= BIT(12);
407         else
408                 config->eventctrl1 &= ~BIT(12);
409
410         /* bit[8], Instruction stall bit */
411         if (config->mode & ETM_MODE_ISTALL_EN)
412                 config->stall_ctrl |= BIT(8);
413         else
414                 config->stall_ctrl &= ~BIT(8);
415
416         /* bit[10], Prioritize instruction trace bit */
417         if (config->mode & ETM_MODE_INSTPRIO)
418                 config->stall_ctrl |= BIT(10);
419         else
420                 config->stall_ctrl &= ~BIT(10);
421
422         /* bit[13], Trace overflow prevention bit */
423         if ((config->mode & ETM_MODE_NOOVERFLOW) &&
424                 (drvdata->nooverflow == true))
425                 config->stall_ctrl |= BIT(13);
426         else
427                 config->stall_ctrl &= ~BIT(13);
428
429         /* bit[9] Start/stop logic control bit */
430         if (config->mode & ETM_MODE_VIEWINST_STARTSTOP)
431                 config->vinst_ctrl |= BIT(9);
432         else
433                 config->vinst_ctrl &= ~BIT(9);
434
435         /* bit[10], Whether a trace unit must trace a Reset exception */
436         if (config->mode & ETM_MODE_TRACE_RESET)
437                 config->vinst_ctrl |= BIT(10);
438         else
439                 config->vinst_ctrl &= ~BIT(10);
440
441         /* bit[11], Whether a trace unit must trace a system error exception */
442         if ((config->mode & ETM_MODE_TRACE_ERR) &&
443                 (drvdata->trc_error == true))
444                 config->vinst_ctrl |= BIT(11);
445         else
446                 config->vinst_ctrl &= ~BIT(11);
447
448         if (config->mode & (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
449                 etm4_config_trace_mode(config);
450
451         spin_unlock(&drvdata->spinlock);
452
453         return size;
454 }
455 static DEVICE_ATTR_RW(mode);
456
457 static ssize_t pe_show(struct device *dev,
458                        struct device_attribute *attr,
459                        char *buf)
460 {
461         unsigned long val;
462         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
463         struct etmv4_config *config = &drvdata->config;
464
465         val = config->pe_sel;
466         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
467 }
468
469 static ssize_t pe_store(struct device *dev,
470                         struct device_attribute *attr,
471                         const char *buf, size_t size)
472 {
473         unsigned long val;
474         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
475         struct etmv4_config *config = &drvdata->config;
476
477         if (kstrtoul(buf, 16, &val))
478                 return -EINVAL;
479
480         spin_lock(&drvdata->spinlock);
481         if (val > drvdata->nr_pe) {
482                 spin_unlock(&drvdata->spinlock);
483                 return -EINVAL;
484         }
485
486         config->pe_sel = val;
487         spin_unlock(&drvdata->spinlock);
488         return size;
489 }
490 static DEVICE_ATTR_RW(pe);
491
492 static ssize_t event_show(struct device *dev,
493                           struct device_attribute *attr,
494                           char *buf)
495 {
496         unsigned long val;
497         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
498         struct etmv4_config *config = &drvdata->config;
499
500         val = config->eventctrl0;
501         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
502 }
503
504 static ssize_t event_store(struct device *dev,
505                            struct device_attribute *attr,
506                            const char *buf, size_t size)
507 {
508         unsigned long val;
509         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
510         struct etmv4_config *config = &drvdata->config;
511
512         if (kstrtoul(buf, 16, &val))
513                 return -EINVAL;
514
515         spin_lock(&drvdata->spinlock);
516         switch (drvdata->nr_event) {
517         case 0x0:
518                 /* EVENT0, bits[7:0] */
519                 config->eventctrl0 = val & 0xFF;
520                 break;
521         case 0x1:
522                  /* EVENT1, bits[15:8] */
523                 config->eventctrl0 = val & 0xFFFF;
524                 break;
525         case 0x2:
526                 /* EVENT2, bits[23:16] */
527                 config->eventctrl0 = val & 0xFFFFFF;
528                 break;
529         case 0x3:
530                 /* EVENT3, bits[31:24] */
531                 config->eventctrl0 = val;
532                 break;
533         default:
534                 break;
535         }
536         spin_unlock(&drvdata->spinlock);
537         return size;
538 }
539 static DEVICE_ATTR_RW(event);
540
541 static ssize_t event_instren_show(struct device *dev,
542                                   struct device_attribute *attr,
543                                   char *buf)
544 {
545         unsigned long val;
546         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
547         struct etmv4_config *config = &drvdata->config;
548
549         val = BMVAL(config->eventctrl1, 0, 3);
550         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
551 }
552
553 static ssize_t event_instren_store(struct device *dev,
554                                    struct device_attribute *attr,
555                                    const char *buf, size_t size)
556 {
557         unsigned long val;
558         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
559         struct etmv4_config *config = &drvdata->config;
560
561         if (kstrtoul(buf, 16, &val))
562                 return -EINVAL;
563
564         spin_lock(&drvdata->spinlock);
565         /* start by clearing all instruction event enable bits */
566         config->eventctrl1 &= ~(BIT(0) | BIT(1) | BIT(2) | BIT(3));
567         switch (drvdata->nr_event) {
568         case 0x0:
569                 /* generate Event element for event 1 */
570                 config->eventctrl1 |= val & BIT(1);
571                 break;
572         case 0x1:
573                 /* generate Event element for event 1 and 2 */
574                 config->eventctrl1 |= val & (BIT(0) | BIT(1));
575                 break;
576         case 0x2:
577                 /* generate Event element for event 1, 2 and 3 */
578                 config->eventctrl1 |= val & (BIT(0) | BIT(1) | BIT(2));
579                 break;
580         case 0x3:
581                 /* generate Event element for all 4 events */
582                 config->eventctrl1 |= val & 0xF;
583                 break;
584         default:
585                 break;
586         }
587         spin_unlock(&drvdata->spinlock);
588         return size;
589 }
590 static DEVICE_ATTR_RW(event_instren);
591
592 static ssize_t event_ts_show(struct device *dev,
593                              struct device_attribute *attr,
594                              char *buf)
595 {
596         unsigned long val;
597         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
598         struct etmv4_config *config = &drvdata->config;
599
600         val = config->ts_ctrl;
601         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
602 }
603
604 static ssize_t event_ts_store(struct device *dev,
605                               struct device_attribute *attr,
606                               const char *buf, size_t size)
607 {
608         unsigned long val;
609         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
610         struct etmv4_config *config = &drvdata->config;
611
612         if (kstrtoul(buf, 16, &val))
613                 return -EINVAL;
614         if (!drvdata->ts_size)
615                 return -EINVAL;
616
617         config->ts_ctrl = val & ETMv4_EVENT_MASK;
618         return size;
619 }
620 static DEVICE_ATTR_RW(event_ts);
621
622 static ssize_t syncfreq_show(struct device *dev,
623                              struct device_attribute *attr,
624                              char *buf)
625 {
626         unsigned long val;
627         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
628         struct etmv4_config *config = &drvdata->config;
629
630         val = config->syncfreq;
631         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
632 }
633
634 static ssize_t syncfreq_store(struct device *dev,
635                               struct device_attribute *attr,
636                               const char *buf, size_t size)
637 {
638         unsigned long val;
639         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
640         struct etmv4_config *config = &drvdata->config;
641
642         if (kstrtoul(buf, 16, &val))
643                 return -EINVAL;
644         if (drvdata->syncpr == true)
645                 return -EINVAL;
646
647         config->syncfreq = val & ETMv4_SYNC_MASK;
648         return size;
649 }
650 static DEVICE_ATTR_RW(syncfreq);
651
652 static ssize_t cyc_threshold_show(struct device *dev,
653                                   struct device_attribute *attr,
654                                   char *buf)
655 {
656         unsigned long val;
657         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
658         struct etmv4_config *config = &drvdata->config;
659
660         val = config->ccctlr;
661         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
662 }
663
664 static ssize_t cyc_threshold_store(struct device *dev,
665                                    struct device_attribute *attr,
666                                    const char *buf, size_t size)
667 {
668         unsigned long val;
669         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
670         struct etmv4_config *config = &drvdata->config;
671
672         if (kstrtoul(buf, 16, &val))
673                 return -EINVAL;
674
675         /* mask off max threshold before checking min value */
676         val &= ETM_CYC_THRESHOLD_MASK;
677         if (val < drvdata->ccitmin)
678                 return -EINVAL;
679
680         config->ccctlr = val;
681         return size;
682 }
683 static DEVICE_ATTR_RW(cyc_threshold);
684
685 static ssize_t bb_ctrl_show(struct device *dev,
686                             struct device_attribute *attr,
687                             char *buf)
688 {
689         unsigned long val;
690         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
691         struct etmv4_config *config = &drvdata->config;
692
693         val = config->bb_ctrl;
694         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
695 }
696
697 static ssize_t bb_ctrl_store(struct device *dev,
698                              struct device_attribute *attr,
699                              const char *buf, size_t size)
700 {
701         unsigned long val;
702         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
703         struct etmv4_config *config = &drvdata->config;
704
705         if (kstrtoul(buf, 16, &val))
706                 return -EINVAL;
707         if (drvdata->trcbb == false)
708                 return -EINVAL;
709         if (!drvdata->nr_addr_cmp)
710                 return -EINVAL;
711
712         /*
713          * Bit[8] controls include(1) / exclude(0), bits[0-7] select
714          * individual range comparators. If include then at least 1
715          * range must be selected.
716          */
717         if ((val & BIT(8)) && (BMVAL(val, 0, 7) == 0))
718                 return -EINVAL;
719
720         config->bb_ctrl = val & GENMASK(8, 0);
721         return size;
722 }
723 static DEVICE_ATTR_RW(bb_ctrl);
724
725 static ssize_t event_vinst_show(struct device *dev,
726                                 struct device_attribute *attr,
727                                 char *buf)
728 {
729         unsigned long val;
730         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
731         struct etmv4_config *config = &drvdata->config;
732
733         val = config->vinst_ctrl & ETMv4_EVENT_MASK;
734         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
735 }
736
737 static ssize_t event_vinst_store(struct device *dev,
738                                  struct device_attribute *attr,
739                                  const char *buf, size_t size)
740 {
741         unsigned long val;
742         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
743         struct etmv4_config *config = &drvdata->config;
744
745         if (kstrtoul(buf, 16, &val))
746                 return -EINVAL;
747
748         spin_lock(&drvdata->spinlock);
749         val &= ETMv4_EVENT_MASK;
750         config->vinst_ctrl &= ~ETMv4_EVENT_MASK;
751         config->vinst_ctrl |= val;
752         spin_unlock(&drvdata->spinlock);
753         return size;
754 }
755 static DEVICE_ATTR_RW(event_vinst);
756
757 static ssize_t s_exlevel_vinst_show(struct device *dev,
758                                     struct device_attribute *attr,
759                                     char *buf)
760 {
761         unsigned long val;
762         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
763         struct etmv4_config *config = &drvdata->config;
764
765         val = BMVAL(config->vinst_ctrl, 16, 19);
766         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
767 }
768
769 static ssize_t s_exlevel_vinst_store(struct device *dev,
770                                      struct device_attribute *attr,
771                                      const char *buf, size_t size)
772 {
773         unsigned long val;
774         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
775         struct etmv4_config *config = &drvdata->config;
776
777         if (kstrtoul(buf, 16, &val))
778                 return -EINVAL;
779
780         spin_lock(&drvdata->spinlock);
781         /* clear all EXLEVEL_S bits (bit[18] is never implemented) */
782         config->vinst_ctrl &= ~(BIT(16) | BIT(17) | BIT(19));
783         /* enable instruction tracing for corresponding exception level */
784         val &= drvdata->s_ex_level;
785         config->vinst_ctrl |= (val << 16);
786         spin_unlock(&drvdata->spinlock);
787         return size;
788 }
789 static DEVICE_ATTR_RW(s_exlevel_vinst);
790
791 static ssize_t ns_exlevel_vinst_show(struct device *dev,
792                                      struct device_attribute *attr,
793                                      char *buf)
794 {
795         unsigned long val;
796         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
797         struct etmv4_config *config = &drvdata->config;
798
799         /* EXLEVEL_NS, bits[23:20] */
800         val = BMVAL(config->vinst_ctrl, 20, 23);
801         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
802 }
803
804 static ssize_t ns_exlevel_vinst_store(struct device *dev,
805                                       struct device_attribute *attr,
806                                       const char *buf, size_t size)
807 {
808         unsigned long val;
809         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
810         struct etmv4_config *config = &drvdata->config;
811
812         if (kstrtoul(buf, 16, &val))
813                 return -EINVAL;
814
815         spin_lock(&drvdata->spinlock);
816         /* clear EXLEVEL_NS bits (bit[23] is never implemented */
817         config->vinst_ctrl &= ~(BIT(20) | BIT(21) | BIT(22));
818         /* enable instruction tracing for corresponding exception level */
819         val &= drvdata->ns_ex_level;
820         config->vinst_ctrl |= (val << 20);
821         spin_unlock(&drvdata->spinlock);
822         return size;
823 }
824 static DEVICE_ATTR_RW(ns_exlevel_vinst);
825
826 static ssize_t addr_idx_show(struct device *dev,
827                              struct device_attribute *attr,
828                              char *buf)
829 {
830         unsigned long val;
831         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
832         struct etmv4_config *config = &drvdata->config;
833
834         val = config->addr_idx;
835         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
836 }
837
838 static ssize_t addr_idx_store(struct device *dev,
839                               struct device_attribute *attr,
840                               const char *buf, size_t size)
841 {
842         unsigned long val;
843         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
844         struct etmv4_config *config = &drvdata->config;
845
846         if (kstrtoul(buf, 16, &val))
847                 return -EINVAL;
848         if (val >= drvdata->nr_addr_cmp * 2)
849                 return -EINVAL;
850
851         /*
852          * Use spinlock to ensure index doesn't change while it gets
853          * dereferenced multiple times within a spinlock block elsewhere.
854          */
855         spin_lock(&drvdata->spinlock);
856         config->addr_idx = val;
857         spin_unlock(&drvdata->spinlock);
858         return size;
859 }
860 static DEVICE_ATTR_RW(addr_idx);
861
862 static ssize_t addr_instdatatype_show(struct device *dev,
863                                       struct device_attribute *attr,
864                                       char *buf)
865 {
866         ssize_t len;
867         u8 val, idx;
868         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
869         struct etmv4_config *config = &drvdata->config;
870
871         spin_lock(&drvdata->spinlock);
872         idx = config->addr_idx;
873         val = BMVAL(config->addr_acc[idx], 0, 1);
874         len = scnprintf(buf, PAGE_SIZE, "%s\n",
875                         val == ETM_INSTR_ADDR ? "instr" :
876                         (val == ETM_DATA_LOAD_ADDR ? "data_load" :
877                         (val == ETM_DATA_STORE_ADDR ? "data_store" :
878                         "data_load_store")));
879         spin_unlock(&drvdata->spinlock);
880         return len;
881 }
882
883 static ssize_t addr_instdatatype_store(struct device *dev,
884                                        struct device_attribute *attr,
885                                        const char *buf, size_t size)
886 {
887         u8 idx;
888         char str[20] = "";
889         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
890         struct etmv4_config *config = &drvdata->config;
891
892         if (strlen(buf) >= 20)
893                 return -EINVAL;
894         if (sscanf(buf, "%s", str) != 1)
895                 return -EINVAL;
896
897         spin_lock(&drvdata->spinlock);
898         idx = config->addr_idx;
899         if (!strcmp(str, "instr"))
900                 /* TYPE, bits[1:0] */
901                 config->addr_acc[idx] &= ~(BIT(0) | BIT(1));
902
903         spin_unlock(&drvdata->spinlock);
904         return size;
905 }
906 static DEVICE_ATTR_RW(addr_instdatatype);
907
908 static ssize_t addr_single_show(struct device *dev,
909                                 struct device_attribute *attr,
910                                 char *buf)
911 {
912         u8 idx;
913         unsigned long val;
914         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
915         struct etmv4_config *config = &drvdata->config;
916
917         idx = config->addr_idx;
918         spin_lock(&drvdata->spinlock);
919         if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
920               config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
921                 spin_unlock(&drvdata->spinlock);
922                 return -EPERM;
923         }
924         val = (unsigned long)config->addr_val[idx];
925         spin_unlock(&drvdata->spinlock);
926         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
927 }
928
929 static ssize_t addr_single_store(struct device *dev,
930                                  struct device_attribute *attr,
931                                  const char *buf, size_t size)
932 {
933         u8 idx;
934         unsigned long val;
935         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
936         struct etmv4_config *config = &drvdata->config;
937
938         if (kstrtoul(buf, 16, &val))
939                 return -EINVAL;
940
941         spin_lock(&drvdata->spinlock);
942         idx = config->addr_idx;
943         if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
944               config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
945                 spin_unlock(&drvdata->spinlock);
946                 return -EPERM;
947         }
948
949         config->addr_val[idx] = (u64)val;
950         config->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
951         spin_unlock(&drvdata->spinlock);
952         return size;
953 }
954 static DEVICE_ATTR_RW(addr_single);
955
956 static ssize_t addr_range_show(struct device *dev,
957                                struct device_attribute *attr,
958                                char *buf)
959 {
960         u8 idx;
961         unsigned long val1, val2;
962         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
963         struct etmv4_config *config = &drvdata->config;
964
965         spin_lock(&drvdata->spinlock);
966         idx = config->addr_idx;
967         if (idx % 2 != 0) {
968                 spin_unlock(&drvdata->spinlock);
969                 return -EPERM;
970         }
971         if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
972                config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
973               (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
974                config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
975                 spin_unlock(&drvdata->spinlock);
976                 return -EPERM;
977         }
978
979         val1 = (unsigned long)config->addr_val[idx];
980         val2 = (unsigned long)config->addr_val[idx + 1];
981         spin_unlock(&drvdata->spinlock);
982         return scnprintf(buf, PAGE_SIZE, "%#lx %#lx\n", val1, val2);
983 }
984
985 static ssize_t addr_range_store(struct device *dev,
986                                 struct device_attribute *attr,
987                                 const char *buf, size_t size)
988 {
989         u8 idx;
990         unsigned long val1, val2;
991         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
992         struct etmv4_config *config = &drvdata->config;
993
994         if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
995                 return -EINVAL;
996         /* lower address comparator cannot have a higher address value */
997         if (val1 > val2)
998                 return -EINVAL;
999
1000         spin_lock(&drvdata->spinlock);
1001         idx = config->addr_idx;
1002         if (idx % 2 != 0) {
1003                 spin_unlock(&drvdata->spinlock);
1004                 return -EPERM;
1005         }
1006
1007         if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
1008                config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
1009               (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
1010                config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
1011                 spin_unlock(&drvdata->spinlock);
1012                 return -EPERM;
1013         }
1014
1015         config->addr_val[idx] = (u64)val1;
1016         config->addr_type[idx] = ETM_ADDR_TYPE_RANGE;
1017         config->addr_val[idx + 1] = (u64)val2;
1018         config->addr_type[idx + 1] = ETM_ADDR_TYPE_RANGE;
1019         /*
1020          * Program include or exclude control bits for vinst or vdata
1021          * whenever we change addr comparators to ETM_ADDR_TYPE_RANGE
1022          */
1023         if (config->mode & ETM_MODE_EXCLUDE)
1024                 etm4_set_mode_exclude(drvdata, true);
1025         else
1026                 etm4_set_mode_exclude(drvdata, false);
1027
1028         spin_unlock(&drvdata->spinlock);
1029         return size;
1030 }
1031 static DEVICE_ATTR_RW(addr_range);
1032
1033 static ssize_t addr_start_show(struct device *dev,
1034                                struct device_attribute *attr,
1035                                char *buf)
1036 {
1037         u8 idx;
1038         unsigned long val;
1039         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1040         struct etmv4_config *config = &drvdata->config;
1041
1042         spin_lock(&drvdata->spinlock);
1043         idx = config->addr_idx;
1044
1045         if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
1046               config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
1047                 spin_unlock(&drvdata->spinlock);
1048                 return -EPERM;
1049         }
1050
1051         val = (unsigned long)config->addr_val[idx];
1052         spin_unlock(&drvdata->spinlock);
1053         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1054 }
1055
1056 static ssize_t addr_start_store(struct device *dev,
1057                                 struct device_attribute *attr,
1058                                 const char *buf, size_t size)
1059 {
1060         u8 idx;
1061         unsigned long val;
1062         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1063         struct etmv4_config *config = &drvdata->config;
1064
1065         if (kstrtoul(buf, 16, &val))
1066                 return -EINVAL;
1067
1068         spin_lock(&drvdata->spinlock);
1069         idx = config->addr_idx;
1070         if (!drvdata->nr_addr_cmp) {
1071                 spin_unlock(&drvdata->spinlock);
1072                 return -EINVAL;
1073         }
1074         if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
1075               config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
1076                 spin_unlock(&drvdata->spinlock);
1077                 return -EPERM;
1078         }
1079
1080         config->addr_val[idx] = (u64)val;
1081         config->addr_type[idx] = ETM_ADDR_TYPE_START;
1082         config->vissctlr |= BIT(idx);
1083         /* SSSTATUS, bit[9] - turn on start/stop logic */
1084         config->vinst_ctrl |= BIT(9);
1085         spin_unlock(&drvdata->spinlock);
1086         return size;
1087 }
1088 static DEVICE_ATTR_RW(addr_start);
1089
1090 static ssize_t addr_stop_show(struct device *dev,
1091                               struct device_attribute *attr,
1092                               char *buf)
1093 {
1094         u8 idx;
1095         unsigned long val;
1096         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1097         struct etmv4_config *config = &drvdata->config;
1098
1099         spin_lock(&drvdata->spinlock);
1100         idx = config->addr_idx;
1101
1102         if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
1103               config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
1104                 spin_unlock(&drvdata->spinlock);
1105                 return -EPERM;
1106         }
1107
1108         val = (unsigned long)config->addr_val[idx];
1109         spin_unlock(&drvdata->spinlock);
1110         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1111 }
1112
1113 static ssize_t addr_stop_store(struct device *dev,
1114                                struct device_attribute *attr,
1115                                const char *buf, size_t size)
1116 {
1117         u8 idx;
1118         unsigned long val;
1119         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1120         struct etmv4_config *config = &drvdata->config;
1121
1122         if (kstrtoul(buf, 16, &val))
1123                 return -EINVAL;
1124
1125         spin_lock(&drvdata->spinlock);
1126         idx = config->addr_idx;
1127         if (!drvdata->nr_addr_cmp) {
1128                 spin_unlock(&drvdata->spinlock);
1129                 return -EINVAL;
1130         }
1131         if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
1132                config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
1133                 spin_unlock(&drvdata->spinlock);
1134                 return -EPERM;
1135         }
1136
1137         config->addr_val[idx] = (u64)val;
1138         config->addr_type[idx] = ETM_ADDR_TYPE_STOP;
1139         config->vissctlr |= BIT(idx + 16);
1140         /* SSSTATUS, bit[9] - turn on start/stop logic */
1141         config->vinst_ctrl |= BIT(9);
1142         spin_unlock(&drvdata->spinlock);
1143         return size;
1144 }
1145 static DEVICE_ATTR_RW(addr_stop);
1146
1147 static ssize_t addr_ctxtype_show(struct device *dev,
1148                                  struct device_attribute *attr,
1149                                  char *buf)
1150 {
1151         ssize_t len;
1152         u8 idx, val;
1153         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1154         struct etmv4_config *config = &drvdata->config;
1155
1156         spin_lock(&drvdata->spinlock);
1157         idx = config->addr_idx;
1158         /* CONTEXTTYPE, bits[3:2] */
1159         val = BMVAL(config->addr_acc[idx], 2, 3);
1160         len = scnprintf(buf, PAGE_SIZE, "%s\n", val == ETM_CTX_NONE ? "none" :
1161                         (val == ETM_CTX_CTXID ? "ctxid" :
1162                         (val == ETM_CTX_VMID ? "vmid" : "all")));
1163         spin_unlock(&drvdata->spinlock);
1164         return len;
1165 }
1166
1167 static ssize_t addr_ctxtype_store(struct device *dev,
1168                                   struct device_attribute *attr,
1169                                   const char *buf, size_t size)
1170 {
1171         u8 idx;
1172         char str[10] = "";
1173         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1174         struct etmv4_config *config = &drvdata->config;
1175
1176         if (strlen(buf) >= 10)
1177                 return -EINVAL;
1178         if (sscanf(buf, "%s", str) != 1)
1179                 return -EINVAL;
1180
1181         spin_lock(&drvdata->spinlock);
1182         idx = config->addr_idx;
1183         if (!strcmp(str, "none"))
1184                 /* start by clearing context type bits */
1185                 config->addr_acc[idx] &= ~(BIT(2) | BIT(3));
1186         else if (!strcmp(str, "ctxid")) {
1187                 /* 0b01 The trace unit performs a Context ID */
1188                 if (drvdata->numcidc) {
1189                         config->addr_acc[idx] |= BIT(2);
1190                         config->addr_acc[idx] &= ~BIT(3);
1191                 }
1192         } else if (!strcmp(str, "vmid")) {
1193                 /* 0b10 The trace unit performs a VMID */
1194                 if (drvdata->numvmidc) {
1195                         config->addr_acc[idx] &= ~BIT(2);
1196                         config->addr_acc[idx] |= BIT(3);
1197                 }
1198         } else if (!strcmp(str, "all")) {
1199                 /*
1200                  * 0b11 The trace unit performs a Context ID
1201                  * comparison and a VMID
1202                  */
1203                 if (drvdata->numcidc)
1204                         config->addr_acc[idx] |= BIT(2);
1205                 if (drvdata->numvmidc)
1206                         config->addr_acc[idx] |= BIT(3);
1207         }
1208         spin_unlock(&drvdata->spinlock);
1209         return size;
1210 }
1211 static DEVICE_ATTR_RW(addr_ctxtype);
1212
1213 static ssize_t addr_context_show(struct device *dev,
1214                                  struct device_attribute *attr,
1215                                  char *buf)
1216 {
1217         u8 idx;
1218         unsigned long val;
1219         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1220         struct etmv4_config *config = &drvdata->config;
1221
1222         spin_lock(&drvdata->spinlock);
1223         idx = config->addr_idx;
1224         /* context ID comparator bits[6:4] */
1225         val = BMVAL(config->addr_acc[idx], 4, 6);
1226         spin_unlock(&drvdata->spinlock);
1227         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1228 }
1229
1230 static ssize_t addr_context_store(struct device *dev,
1231                                   struct device_attribute *attr,
1232                                   const char *buf, size_t size)
1233 {
1234         u8 idx;
1235         unsigned long val;
1236         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1237         struct etmv4_config *config = &drvdata->config;
1238
1239         if (kstrtoul(buf, 16, &val))
1240                 return -EINVAL;
1241         if ((drvdata->numcidc <= 1) && (drvdata->numvmidc <= 1))
1242                 return -EINVAL;
1243         if (val >=  (drvdata->numcidc >= drvdata->numvmidc ?
1244                      drvdata->numcidc : drvdata->numvmidc))
1245                 return -EINVAL;
1246
1247         spin_lock(&drvdata->spinlock);
1248         idx = config->addr_idx;
1249         /* clear context ID comparator bits[6:4] */
1250         config->addr_acc[idx] &= ~(BIT(4) | BIT(5) | BIT(6));
1251         config->addr_acc[idx] |= (val << 4);
1252         spin_unlock(&drvdata->spinlock);
1253         return size;
1254 }
1255 static DEVICE_ATTR_RW(addr_context);
1256
1257 static ssize_t seq_idx_show(struct device *dev,
1258                             struct device_attribute *attr,
1259                             char *buf)
1260 {
1261         unsigned long val;
1262         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1263         struct etmv4_config *config = &drvdata->config;
1264
1265         val = config->seq_idx;
1266         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1267 }
1268
1269 static ssize_t seq_idx_store(struct device *dev,
1270                              struct device_attribute *attr,
1271                              const char *buf, size_t size)
1272 {
1273         unsigned long val;
1274         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1275         struct etmv4_config *config = &drvdata->config;
1276
1277         if (kstrtoul(buf, 16, &val))
1278                 return -EINVAL;
1279         if (val >= drvdata->nrseqstate - 1)
1280                 return -EINVAL;
1281
1282         /*
1283          * Use spinlock to ensure index doesn't change while it gets
1284          * dereferenced multiple times within a spinlock block elsewhere.
1285          */
1286         spin_lock(&drvdata->spinlock);
1287         config->seq_idx = val;
1288         spin_unlock(&drvdata->spinlock);
1289         return size;
1290 }
1291 static DEVICE_ATTR_RW(seq_idx);
1292
1293 static ssize_t seq_state_show(struct device *dev,
1294                               struct device_attribute *attr,
1295                               char *buf)
1296 {
1297         unsigned long val;
1298         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1299         struct etmv4_config *config = &drvdata->config;
1300
1301         val = config->seq_state;
1302         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1303 }
1304
1305 static ssize_t seq_state_store(struct device *dev,
1306                                struct device_attribute *attr,
1307                                const char *buf, size_t size)
1308 {
1309         unsigned long val;
1310         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1311         struct etmv4_config *config = &drvdata->config;
1312
1313         if (kstrtoul(buf, 16, &val))
1314                 return -EINVAL;
1315         if (val >= drvdata->nrseqstate)
1316                 return -EINVAL;
1317
1318         config->seq_state = val;
1319         return size;
1320 }
1321 static DEVICE_ATTR_RW(seq_state);
1322
1323 static ssize_t seq_event_show(struct device *dev,
1324                               struct device_attribute *attr,
1325                               char *buf)
1326 {
1327         u8 idx;
1328         unsigned long val;
1329         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1330         struct etmv4_config *config = &drvdata->config;
1331
1332         spin_lock(&drvdata->spinlock);
1333         idx = config->seq_idx;
1334         val = config->seq_ctrl[idx];
1335         spin_unlock(&drvdata->spinlock);
1336         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1337 }
1338
1339 static ssize_t seq_event_store(struct device *dev,
1340                                struct device_attribute *attr,
1341                                const char *buf, size_t size)
1342 {
1343         u8 idx;
1344         unsigned long val;
1345         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1346         struct etmv4_config *config = &drvdata->config;
1347
1348         if (kstrtoul(buf, 16, &val))
1349                 return -EINVAL;
1350
1351         spin_lock(&drvdata->spinlock);
1352         idx = config->seq_idx;
1353         /* Seq control has two masks B[15:8] F[7:0] */
1354         config->seq_ctrl[idx] = val & 0xFFFF;
1355         spin_unlock(&drvdata->spinlock);
1356         return size;
1357 }
1358 static DEVICE_ATTR_RW(seq_event);
1359
1360 static ssize_t seq_reset_event_show(struct device *dev,
1361                                     struct device_attribute *attr,
1362                                     char *buf)
1363 {
1364         unsigned long val;
1365         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1366         struct etmv4_config *config = &drvdata->config;
1367
1368         val = config->seq_rst;
1369         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1370 }
1371
1372 static ssize_t seq_reset_event_store(struct device *dev,
1373                                      struct device_attribute *attr,
1374                                      const char *buf, size_t size)
1375 {
1376         unsigned long val;
1377         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1378         struct etmv4_config *config = &drvdata->config;
1379
1380         if (kstrtoul(buf, 16, &val))
1381                 return -EINVAL;
1382         if (!(drvdata->nrseqstate))
1383                 return -EINVAL;
1384
1385         config->seq_rst = val & ETMv4_EVENT_MASK;
1386         return size;
1387 }
1388 static DEVICE_ATTR_RW(seq_reset_event);
1389
1390 static ssize_t cntr_idx_show(struct device *dev,
1391                              struct device_attribute *attr,
1392                              char *buf)
1393 {
1394         unsigned long val;
1395         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1396         struct etmv4_config *config = &drvdata->config;
1397
1398         val = config->cntr_idx;
1399         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1400 }
1401
1402 static ssize_t cntr_idx_store(struct device *dev,
1403                               struct device_attribute *attr,
1404                               const char *buf, size_t size)
1405 {
1406         unsigned long val;
1407         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1408         struct etmv4_config *config = &drvdata->config;
1409
1410         if (kstrtoul(buf, 16, &val))
1411                 return -EINVAL;
1412         if (val >= drvdata->nr_cntr)
1413                 return -EINVAL;
1414
1415         /*
1416          * Use spinlock to ensure index doesn't change while it gets
1417          * dereferenced multiple times within a spinlock block elsewhere.
1418          */
1419         spin_lock(&drvdata->spinlock);
1420         config->cntr_idx = val;
1421         spin_unlock(&drvdata->spinlock);
1422         return size;
1423 }
1424 static DEVICE_ATTR_RW(cntr_idx);
1425
1426 static ssize_t cntrldvr_show(struct device *dev,
1427                              struct device_attribute *attr,
1428                              char *buf)
1429 {
1430         u8 idx;
1431         unsigned long val;
1432         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1433         struct etmv4_config *config = &drvdata->config;
1434
1435         spin_lock(&drvdata->spinlock);
1436         idx = config->cntr_idx;
1437         val = config->cntrldvr[idx];
1438         spin_unlock(&drvdata->spinlock);
1439         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1440 }
1441
1442 static ssize_t cntrldvr_store(struct device *dev,
1443                               struct device_attribute *attr,
1444                               const char *buf, size_t size)
1445 {
1446         u8 idx;
1447         unsigned long val;
1448         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1449         struct etmv4_config *config = &drvdata->config;
1450
1451         if (kstrtoul(buf, 16, &val))
1452                 return -EINVAL;
1453         if (val > ETM_CNTR_MAX_VAL)
1454                 return -EINVAL;
1455
1456         spin_lock(&drvdata->spinlock);
1457         idx = config->cntr_idx;
1458         config->cntrldvr[idx] = val;
1459         spin_unlock(&drvdata->spinlock);
1460         return size;
1461 }
1462 static DEVICE_ATTR_RW(cntrldvr);
1463
1464 static ssize_t cntr_val_show(struct device *dev,
1465                              struct device_attribute *attr,
1466                              char *buf)
1467 {
1468         u8 idx;
1469         unsigned long val;
1470         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1471         struct etmv4_config *config = &drvdata->config;
1472
1473         spin_lock(&drvdata->spinlock);
1474         idx = config->cntr_idx;
1475         val = config->cntr_val[idx];
1476         spin_unlock(&drvdata->spinlock);
1477         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1478 }
1479
1480 static ssize_t cntr_val_store(struct device *dev,
1481                               struct device_attribute *attr,
1482                               const char *buf, size_t size)
1483 {
1484         u8 idx;
1485         unsigned long val;
1486         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1487         struct etmv4_config *config = &drvdata->config;
1488
1489         if (kstrtoul(buf, 16, &val))
1490                 return -EINVAL;
1491         if (val > ETM_CNTR_MAX_VAL)
1492                 return -EINVAL;
1493
1494         spin_lock(&drvdata->spinlock);
1495         idx = config->cntr_idx;
1496         config->cntr_val[idx] = val;
1497         spin_unlock(&drvdata->spinlock);
1498         return size;
1499 }
1500 static DEVICE_ATTR_RW(cntr_val);
1501
1502 static ssize_t cntr_ctrl_show(struct device *dev,
1503                               struct device_attribute *attr,
1504                               char *buf)
1505 {
1506         u8 idx;
1507         unsigned long val;
1508         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1509         struct etmv4_config *config = &drvdata->config;
1510
1511         spin_lock(&drvdata->spinlock);
1512         idx = config->cntr_idx;
1513         val = config->cntr_ctrl[idx];
1514         spin_unlock(&drvdata->spinlock);
1515         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1516 }
1517
1518 static ssize_t cntr_ctrl_store(struct device *dev,
1519                                struct device_attribute *attr,
1520                                const char *buf, size_t size)
1521 {
1522         u8 idx;
1523         unsigned long val;
1524         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1525         struct etmv4_config *config = &drvdata->config;
1526
1527         if (kstrtoul(buf, 16, &val))
1528                 return -EINVAL;
1529
1530         spin_lock(&drvdata->spinlock);
1531         idx = config->cntr_idx;
1532         config->cntr_ctrl[idx] = val;
1533         spin_unlock(&drvdata->spinlock);
1534         return size;
1535 }
1536 static DEVICE_ATTR_RW(cntr_ctrl);
1537
1538 static ssize_t res_idx_show(struct device *dev,
1539                             struct device_attribute *attr,
1540                             char *buf)
1541 {
1542         unsigned long val;
1543         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1544         struct etmv4_config *config = &drvdata->config;
1545
1546         val = config->res_idx;
1547         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1548 }
1549
1550 static ssize_t res_idx_store(struct device *dev,
1551                              struct device_attribute *attr,
1552                              const char *buf, size_t size)
1553 {
1554         unsigned long val;
1555         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1556         struct etmv4_config *config = &drvdata->config;
1557
1558         if (kstrtoul(buf, 16, &val))
1559                 return -EINVAL;
1560         /* Resource selector pair 0 is always implemented and reserved */
1561         if ((val == 0) || (val >= drvdata->nr_resource))
1562                 return -EINVAL;
1563
1564         /*
1565          * Use spinlock to ensure index doesn't change while it gets
1566          * dereferenced multiple times within a spinlock block elsewhere.
1567          */
1568         spin_lock(&drvdata->spinlock);
1569         config->res_idx = val;
1570         spin_unlock(&drvdata->spinlock);
1571         return size;
1572 }
1573 static DEVICE_ATTR_RW(res_idx);
1574
1575 static ssize_t res_ctrl_show(struct device *dev,
1576                              struct device_attribute *attr,
1577                              char *buf)
1578 {
1579         u8 idx;
1580         unsigned long val;
1581         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1582         struct etmv4_config *config = &drvdata->config;
1583
1584         spin_lock(&drvdata->spinlock);
1585         idx = config->res_idx;
1586         val = config->res_ctrl[idx];
1587         spin_unlock(&drvdata->spinlock);
1588         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1589 }
1590
1591 static ssize_t res_ctrl_store(struct device *dev,
1592                               struct device_attribute *attr,
1593                               const char *buf, size_t size)
1594 {
1595         u8 idx;
1596         unsigned long val;
1597         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1598         struct etmv4_config *config = &drvdata->config;
1599
1600         if (kstrtoul(buf, 16, &val))
1601                 return -EINVAL;
1602
1603         spin_lock(&drvdata->spinlock);
1604         idx = config->res_idx;
1605         /* For odd idx pair inversal bit is RES0 */
1606         if (idx % 2 != 0)
1607                 /* PAIRINV, bit[21] */
1608                 val &= ~BIT(21);
1609         config->res_ctrl[idx] = val & GENMASK(21, 0);
1610         spin_unlock(&drvdata->spinlock);
1611         return size;
1612 }
1613 static DEVICE_ATTR_RW(res_ctrl);
1614
1615 static ssize_t ctxid_idx_show(struct device *dev,
1616                               struct device_attribute *attr,
1617                               char *buf)
1618 {
1619         unsigned long val;
1620         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1621         struct etmv4_config *config = &drvdata->config;
1622
1623         val = config->ctxid_idx;
1624         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1625 }
1626
1627 static ssize_t ctxid_idx_store(struct device *dev,
1628                                struct device_attribute *attr,
1629                                const char *buf, size_t size)
1630 {
1631         unsigned long val;
1632         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1633         struct etmv4_config *config = &drvdata->config;
1634
1635         if (kstrtoul(buf, 16, &val))
1636                 return -EINVAL;
1637         if (val >= drvdata->numcidc)
1638                 return -EINVAL;
1639
1640         /*
1641          * Use spinlock to ensure index doesn't change while it gets
1642          * dereferenced multiple times within a spinlock block elsewhere.
1643          */
1644         spin_lock(&drvdata->spinlock);
1645         config->ctxid_idx = val;
1646         spin_unlock(&drvdata->spinlock);
1647         return size;
1648 }
1649 static DEVICE_ATTR_RW(ctxid_idx);
1650
1651 static ssize_t ctxid_pid_show(struct device *dev,
1652                               struct device_attribute *attr,
1653                               char *buf)
1654 {
1655         u8 idx;
1656         unsigned long val;
1657         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1658         struct etmv4_config *config = &drvdata->config;
1659
1660         spin_lock(&drvdata->spinlock);
1661         idx = config->ctxid_idx;
1662         val = (unsigned long)config->ctxid_vpid[idx];
1663         spin_unlock(&drvdata->spinlock);
1664         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1665 }
1666
1667 static ssize_t ctxid_pid_store(struct device *dev,
1668                                struct device_attribute *attr,
1669                                const char *buf, size_t size)
1670 {
1671         u8 idx;
1672         unsigned long vpid, pid;
1673         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1674         struct etmv4_config *config = &drvdata->config;
1675
1676         /*
1677          * only implemented when ctxid tracing is enabled, i.e. at least one
1678          * ctxid comparator is implemented and ctxid is greater than 0 bits
1679          * in length
1680          */
1681         if (!drvdata->ctxid_size || !drvdata->numcidc)
1682                 return -EINVAL;
1683         if (kstrtoul(buf, 16, &vpid))
1684                 return -EINVAL;
1685
1686         pid = coresight_vpid_to_pid(vpid);
1687
1688         spin_lock(&drvdata->spinlock);
1689         idx = config->ctxid_idx;
1690         config->ctxid_pid[idx] = (u64)pid;
1691         config->ctxid_vpid[idx] = (u64)vpid;
1692         spin_unlock(&drvdata->spinlock);
1693         return size;
1694 }
1695 static DEVICE_ATTR_RW(ctxid_pid);
1696
1697 static ssize_t ctxid_masks_show(struct device *dev,
1698                                 struct device_attribute *attr,
1699                                 char *buf)
1700 {
1701         unsigned long val1, val2;
1702         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1703         struct etmv4_config *config = &drvdata->config;
1704
1705         spin_lock(&drvdata->spinlock);
1706         val1 = config->ctxid_mask0;
1707         val2 = config->ctxid_mask1;
1708         spin_unlock(&drvdata->spinlock);
1709         return scnprintf(buf, PAGE_SIZE, "%#lx %#lx\n", val1, val2);
1710 }
1711
1712 static ssize_t ctxid_masks_store(struct device *dev,
1713                                 struct device_attribute *attr,
1714                                 const char *buf, size_t size)
1715 {
1716         u8 i, j, maskbyte;
1717         unsigned long val1, val2, mask;
1718         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1719         struct etmv4_config *config = &drvdata->config;
1720
1721         /*
1722          * only implemented when ctxid tracing is enabled, i.e. at least one
1723          * ctxid comparator is implemented and ctxid is greater than 0 bits
1724          * in length
1725          */
1726         if (!drvdata->ctxid_size || !drvdata->numcidc)
1727                 return -EINVAL;
1728         if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
1729                 return -EINVAL;
1730
1731         spin_lock(&drvdata->spinlock);
1732         /*
1733          * each byte[0..3] controls mask value applied to ctxid
1734          * comparator[0..3]
1735          */
1736         switch (drvdata->numcidc) {
1737         case 0x1:
1738                 /* COMP0, bits[7:0] */
1739                 config->ctxid_mask0 = val1 & 0xFF;
1740                 break;
1741         case 0x2:
1742                 /* COMP1, bits[15:8] */
1743                 config->ctxid_mask0 = val1 & 0xFFFF;
1744                 break;
1745         case 0x3:
1746                 /* COMP2, bits[23:16] */
1747                 config->ctxid_mask0 = val1 & 0xFFFFFF;
1748                 break;
1749         case 0x4:
1750                  /* COMP3, bits[31:24] */
1751                 config->ctxid_mask0 = val1;
1752                 break;
1753         case 0x5:
1754                 /* COMP4, bits[7:0] */
1755                 config->ctxid_mask0 = val1;
1756                 config->ctxid_mask1 = val2 & 0xFF;
1757                 break;
1758         case 0x6:
1759                 /* COMP5, bits[15:8] */
1760                 config->ctxid_mask0 = val1;
1761                 config->ctxid_mask1 = val2 & 0xFFFF;
1762                 break;
1763         case 0x7:
1764                 /* COMP6, bits[23:16] */
1765                 config->ctxid_mask0 = val1;
1766                 config->ctxid_mask1 = val2 & 0xFFFFFF;
1767                 break;
1768         case 0x8:
1769                 /* COMP7, bits[31:24] */
1770                 config->ctxid_mask0 = val1;
1771                 config->ctxid_mask1 = val2;
1772                 break;
1773         default:
1774                 break;
1775         }
1776         /*
1777          * If software sets a mask bit to 1, it must program relevant byte
1778          * of ctxid comparator value 0x0, otherwise behavior is unpredictable.
1779          * For example, if bit[3] of ctxid_mask0 is 1, we must clear bits[31:24]
1780          * of ctxid comparator0 value (corresponding to byte 0) register.
1781          */
1782         mask = config->ctxid_mask0;
1783         for (i = 0; i < drvdata->numcidc; i++) {
1784                 /* mask value of corresponding ctxid comparator */
1785                 maskbyte = mask & ETMv4_EVENT_MASK;
1786                 /*
1787                  * each bit corresponds to a byte of respective ctxid comparator
1788                  * value register
1789                  */
1790                 for (j = 0; j < 8; j++) {
1791                         if (maskbyte & 1)
1792                                 config->ctxid_pid[i] &= ~(0xFF << (j * 8));
1793                         maskbyte >>= 1;
1794                 }
1795                 /* Select the next ctxid comparator mask value */
1796                 if (i == 3)
1797                         /* ctxid comparators[4-7] */
1798                         mask = config->ctxid_mask1;
1799                 else
1800                         mask >>= 0x8;
1801         }
1802
1803         spin_unlock(&drvdata->spinlock);
1804         return size;
1805 }
1806 static DEVICE_ATTR_RW(ctxid_masks);
1807
1808 static ssize_t vmid_idx_show(struct device *dev,
1809                              struct device_attribute *attr,
1810                              char *buf)
1811 {
1812         unsigned long val;
1813         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1814         struct etmv4_config *config = &drvdata->config;
1815
1816         val = config->vmid_idx;
1817         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1818 }
1819
1820 static ssize_t vmid_idx_store(struct device *dev,
1821                               struct device_attribute *attr,
1822                               const char *buf, size_t size)
1823 {
1824         unsigned long val;
1825         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1826         struct etmv4_config *config = &drvdata->config;
1827
1828         if (kstrtoul(buf, 16, &val))
1829                 return -EINVAL;
1830         if (val >= drvdata->numvmidc)
1831                 return -EINVAL;
1832
1833         /*
1834          * Use spinlock to ensure index doesn't change while it gets
1835          * dereferenced multiple times within a spinlock block elsewhere.
1836          */
1837         spin_lock(&drvdata->spinlock);
1838         config->vmid_idx = val;
1839         spin_unlock(&drvdata->spinlock);
1840         return size;
1841 }
1842 static DEVICE_ATTR_RW(vmid_idx);
1843
1844 static ssize_t vmid_val_show(struct device *dev,
1845                              struct device_attribute *attr,
1846                              char *buf)
1847 {
1848         unsigned long val;
1849         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1850         struct etmv4_config *config = &drvdata->config;
1851
1852         val = (unsigned long)config->vmid_val[config->vmid_idx];
1853         return scnprintf(buf, PAGE_SIZE, "%#lx\n", val);
1854 }
1855
1856 static ssize_t vmid_val_store(struct device *dev,
1857                               struct device_attribute *attr,
1858                               const char *buf, size_t size)
1859 {
1860         unsigned long val;
1861         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1862         struct etmv4_config *config = &drvdata->config;
1863
1864         /*
1865          * only implemented when vmid tracing is enabled, i.e. at least one
1866          * vmid comparator is implemented and at least 8 bit vmid size
1867          */
1868         if (!drvdata->vmid_size || !drvdata->numvmidc)
1869                 return -EINVAL;
1870         if (kstrtoul(buf, 16, &val))
1871                 return -EINVAL;
1872
1873         spin_lock(&drvdata->spinlock);
1874         config->vmid_val[config->vmid_idx] = (u64)val;
1875         spin_unlock(&drvdata->spinlock);
1876         return size;
1877 }
1878 static DEVICE_ATTR_RW(vmid_val);
1879
1880 static ssize_t vmid_masks_show(struct device *dev,
1881                                struct device_attribute *attr, char *buf)
1882 {
1883         unsigned long val1, val2;
1884         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1885         struct etmv4_config *config = &drvdata->config;
1886
1887         spin_lock(&drvdata->spinlock);
1888         val1 = config->vmid_mask0;
1889         val2 = config->vmid_mask1;
1890         spin_unlock(&drvdata->spinlock);
1891         return scnprintf(buf, PAGE_SIZE, "%#lx %#lx\n", val1, val2);
1892 }
1893
1894 static ssize_t vmid_masks_store(struct device *dev,
1895                                 struct device_attribute *attr,
1896                                 const char *buf, size_t size)
1897 {
1898         u8 i, j, maskbyte;
1899         unsigned long val1, val2, mask;
1900         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1901         struct etmv4_config *config = &drvdata->config;
1902
1903         /*
1904          * only implemented when vmid tracing is enabled, i.e. at least one
1905          * vmid comparator is implemented and at least 8 bit vmid size
1906          */
1907         if (!drvdata->vmid_size || !drvdata->numvmidc)
1908                 return -EINVAL;
1909         if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
1910                 return -EINVAL;
1911
1912         spin_lock(&drvdata->spinlock);
1913
1914         /*
1915          * each byte[0..3] controls mask value applied to vmid
1916          * comparator[0..3]
1917          */
1918         switch (drvdata->numvmidc) {
1919         case 0x1:
1920                 /* COMP0, bits[7:0] */
1921                 config->vmid_mask0 = val1 & 0xFF;
1922                 break;
1923         case 0x2:
1924                 /* COMP1, bits[15:8] */
1925                 config->vmid_mask0 = val1 & 0xFFFF;
1926                 break;
1927         case 0x3:
1928                 /* COMP2, bits[23:16] */
1929                 config->vmid_mask0 = val1 & 0xFFFFFF;
1930                 break;
1931         case 0x4:
1932                 /* COMP3, bits[31:24] */
1933                 config->vmid_mask0 = val1;
1934                 break;
1935         case 0x5:
1936                 /* COMP4, bits[7:0] */
1937                 config->vmid_mask0 = val1;
1938                 config->vmid_mask1 = val2 & 0xFF;
1939                 break;
1940         case 0x6:
1941                 /* COMP5, bits[15:8] */
1942                 config->vmid_mask0 = val1;
1943                 config->vmid_mask1 = val2 & 0xFFFF;
1944                 break;
1945         case 0x7:
1946                 /* COMP6, bits[23:16] */
1947                 config->vmid_mask0 = val1;
1948                 config->vmid_mask1 = val2 & 0xFFFFFF;
1949                 break;
1950         case 0x8:
1951                 /* COMP7, bits[31:24] */
1952                 config->vmid_mask0 = val1;
1953                 config->vmid_mask1 = val2;
1954                 break;
1955         default:
1956                 break;
1957         }
1958
1959         /*
1960          * If software sets a mask bit to 1, it must program relevant byte
1961          * of vmid comparator value 0x0, otherwise behavior is unpredictable.
1962          * For example, if bit[3] of vmid_mask0 is 1, we must clear bits[31:24]
1963          * of vmid comparator0 value (corresponding to byte 0) register.
1964          */
1965         mask = config->vmid_mask0;
1966         for (i = 0; i < drvdata->numvmidc; i++) {
1967                 /* mask value of corresponding vmid comparator */
1968                 maskbyte = mask & ETMv4_EVENT_MASK;
1969                 /*
1970                  * each bit corresponds to a byte of respective vmid comparator
1971                  * value register
1972                  */
1973                 for (j = 0; j < 8; j++) {
1974                         if (maskbyte & 1)
1975                                 config->vmid_val[i] &= ~(0xFF << (j * 8));
1976                         maskbyte >>= 1;
1977                 }
1978                 /* Select the next vmid comparator mask value */
1979                 if (i == 3)
1980                         /* vmid comparators[4-7] */
1981                         mask = config->vmid_mask1;
1982                 else
1983                         mask >>= 0x8;
1984         }
1985         spin_unlock(&drvdata->spinlock);
1986         return size;
1987 }
1988 static DEVICE_ATTR_RW(vmid_masks);
1989
1990 static ssize_t cpu_show(struct device *dev,
1991                         struct device_attribute *attr, char *buf)
1992 {
1993         int val;
1994         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev->parent);
1995
1996         val = drvdata->cpu;
1997         return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1998
1999 }
2000 static DEVICE_ATTR_RO(cpu);
2001
2002 static struct attribute *coresight_etmv4_attrs[] = {
2003         &dev_attr_nr_pe_cmp.attr,
2004         &dev_attr_nr_addr_cmp.attr,
2005         &dev_attr_nr_cntr.attr,
2006         &dev_attr_nr_ext_inp.attr,
2007         &dev_attr_numcidc.attr,
2008         &dev_attr_numvmidc.attr,
2009         &dev_attr_nrseqstate.attr,
2010         &dev_attr_nr_resource.attr,
2011         &dev_attr_nr_ss_cmp.attr,
2012         &dev_attr_reset.attr,
2013         &dev_attr_mode.attr,
2014         &dev_attr_pe.attr,
2015         &dev_attr_event.attr,
2016         &dev_attr_event_instren.attr,
2017         &dev_attr_event_ts.attr,
2018         &dev_attr_syncfreq.attr,
2019         &dev_attr_cyc_threshold.attr,
2020         &dev_attr_bb_ctrl.attr,
2021         &dev_attr_event_vinst.attr,
2022         &dev_attr_s_exlevel_vinst.attr,
2023         &dev_attr_ns_exlevel_vinst.attr,
2024         &dev_attr_addr_idx.attr,
2025         &dev_attr_addr_instdatatype.attr,
2026         &dev_attr_addr_single.attr,
2027         &dev_attr_addr_range.attr,
2028         &dev_attr_addr_start.attr,
2029         &dev_attr_addr_stop.attr,
2030         &dev_attr_addr_ctxtype.attr,
2031         &dev_attr_addr_context.attr,
2032         &dev_attr_seq_idx.attr,
2033         &dev_attr_seq_state.attr,
2034         &dev_attr_seq_event.attr,
2035         &dev_attr_seq_reset_event.attr,
2036         &dev_attr_cntr_idx.attr,
2037         &dev_attr_cntrldvr.attr,
2038         &dev_attr_cntr_val.attr,
2039         &dev_attr_cntr_ctrl.attr,
2040         &dev_attr_res_idx.attr,
2041         &dev_attr_res_ctrl.attr,
2042         &dev_attr_ctxid_idx.attr,
2043         &dev_attr_ctxid_pid.attr,
2044         &dev_attr_ctxid_masks.attr,
2045         &dev_attr_vmid_idx.attr,
2046         &dev_attr_vmid_val.attr,
2047         &dev_attr_vmid_masks.attr,
2048         &dev_attr_cpu.attr,
2049         NULL,
2050 };
2051
2052 struct etmv4_reg {
2053         void __iomem *addr;
2054         u32 data;
2055 };
2056
2057 static void do_smp_cross_read(void *data)
2058 {
2059         struct etmv4_reg *reg = data;
2060
2061         reg->data = readl_relaxed(reg->addr);
2062 }
2063
2064 static u32 etmv4_cross_read(const struct device *dev, u32 offset)
2065 {
2066         struct etmv4_drvdata *drvdata = dev_get_drvdata(dev);
2067         struct etmv4_reg reg;
2068
2069         reg.addr = drvdata->base + offset;
2070         /*
2071          * smp cross call ensures the CPU will be powered up before
2072          * accessing the ETMv4 trace core registers
2073          */
2074         smp_call_function_single(drvdata->cpu, do_smp_cross_read, &reg, 1);
2075         return reg.data;
2076 }
2077
2078 #define coresight_etm4x_reg(name, offset)                       \
2079         coresight_simple_reg32(struct etmv4_drvdata, name, offset)
2080
2081 #define coresight_etm4x_cross_read(name, offset)                        \
2082         coresight_simple_func(struct etmv4_drvdata, etmv4_cross_read,   \
2083                               name, offset)
2084
2085 coresight_etm4x_reg(trcpdcr, TRCPDCR);
2086 coresight_etm4x_reg(trcpdsr, TRCPDSR);
2087 coresight_etm4x_reg(trclsr, TRCLSR);
2088 coresight_etm4x_reg(trcauthstatus, TRCAUTHSTATUS);
2089 coresight_etm4x_reg(trcdevid, TRCDEVID);
2090 coresight_etm4x_reg(trcdevtype, TRCDEVTYPE);
2091 coresight_etm4x_reg(trcpidr0, TRCPIDR0);
2092 coresight_etm4x_reg(trcpidr1, TRCPIDR1);
2093 coresight_etm4x_reg(trcpidr2, TRCPIDR2);
2094 coresight_etm4x_reg(trcpidr3, TRCPIDR3);
2095 coresight_etm4x_cross_read(trcoslsr, TRCOSLSR);
2096 coresight_etm4x_cross_read(trcconfig, TRCCONFIGR);
2097 coresight_etm4x_cross_read(trctraceid, TRCTRACEIDR);
2098
2099 static struct attribute *coresight_etmv4_mgmt_attrs[] = {
2100         &dev_attr_trcoslsr.attr,
2101         &dev_attr_trcpdcr.attr,
2102         &dev_attr_trcpdsr.attr,
2103         &dev_attr_trclsr.attr,
2104         &dev_attr_trcconfig.attr,
2105         &dev_attr_trctraceid.attr,
2106         &dev_attr_trcauthstatus.attr,
2107         &dev_attr_trcdevid.attr,
2108         &dev_attr_trcdevtype.attr,
2109         &dev_attr_trcpidr0.attr,
2110         &dev_attr_trcpidr1.attr,
2111         &dev_attr_trcpidr2.attr,
2112         &dev_attr_trcpidr3.attr,
2113         NULL,
2114 };
2115
2116 coresight_etm4x_cross_read(trcidr0, TRCIDR0);
2117 coresight_etm4x_cross_read(trcidr1, TRCIDR1);
2118 coresight_etm4x_cross_read(trcidr2, TRCIDR2);
2119 coresight_etm4x_cross_read(trcidr3, TRCIDR3);
2120 coresight_etm4x_cross_read(trcidr4, TRCIDR4);
2121 coresight_etm4x_cross_read(trcidr5, TRCIDR5);
2122 /* trcidr[6,7] are reserved */
2123 coresight_etm4x_cross_read(trcidr8, TRCIDR8);
2124 coresight_etm4x_cross_read(trcidr9, TRCIDR9);
2125 coresight_etm4x_cross_read(trcidr10, TRCIDR10);
2126 coresight_etm4x_cross_read(trcidr11, TRCIDR11);
2127 coresight_etm4x_cross_read(trcidr12, TRCIDR12);
2128 coresight_etm4x_cross_read(trcidr13, TRCIDR13);
2129
2130 static struct attribute *coresight_etmv4_trcidr_attrs[] = {
2131         &dev_attr_trcidr0.attr,
2132         &dev_attr_trcidr1.attr,
2133         &dev_attr_trcidr2.attr,
2134         &dev_attr_trcidr3.attr,
2135         &dev_attr_trcidr4.attr,
2136         &dev_attr_trcidr5.attr,
2137         /* trcidr[6,7] are reserved */
2138         &dev_attr_trcidr8.attr,
2139         &dev_attr_trcidr9.attr,
2140         &dev_attr_trcidr10.attr,
2141         &dev_attr_trcidr11.attr,
2142         &dev_attr_trcidr12.attr,
2143         &dev_attr_trcidr13.attr,
2144         NULL,
2145 };
2146
2147 static const struct attribute_group coresight_etmv4_group = {
2148         .attrs = coresight_etmv4_attrs,
2149 };
2150
2151 static const struct attribute_group coresight_etmv4_mgmt_group = {
2152         .attrs = coresight_etmv4_mgmt_attrs,
2153         .name = "mgmt",
2154 };
2155
2156 static const struct attribute_group coresight_etmv4_trcidr_group = {
2157         .attrs = coresight_etmv4_trcidr_attrs,
2158         .name = "trcidr",
2159 };
2160
2161 const struct attribute_group *coresight_etmv4_groups[] = {
2162         &coresight_etmv4_group,
2163         &coresight_etmv4_mgmt_group,
2164         &coresight_etmv4_trcidr_group,
2165         NULL,
2166 };