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