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-etm.h"
21 #include "coresight-priv.h"
22 
23 static ssize_t nr_addr_cmp_show(struct device *dev,
24 				struct device_attribute *attr, char *buf)
25 {
26 	unsigned long val;
27 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
28 
29 	val = drvdata->nr_addr_cmp;
30 	return sprintf(buf, "%#lx\n", val);
31 }
32 static DEVICE_ATTR_RO(nr_addr_cmp);
33 
34 static ssize_t nr_cntr_show(struct device *dev,
35 			    struct device_attribute *attr, char *buf)
36 {	unsigned long val;
37 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
38 
39 	val = drvdata->nr_cntr;
40 	return sprintf(buf, "%#lx\n", val);
41 }
42 static DEVICE_ATTR_RO(nr_cntr);
43 
44 static ssize_t nr_ctxid_cmp_show(struct device *dev,
45 				 struct device_attribute *attr, char *buf)
46 {
47 	unsigned long val;
48 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
49 
50 	val = drvdata->nr_ctxid_cmp;
51 	return sprintf(buf, "%#lx\n", val);
52 }
53 static DEVICE_ATTR_RO(nr_ctxid_cmp);
54 
55 static ssize_t etmsr_show(struct device *dev,
56 			  struct device_attribute *attr, char *buf)
57 {
58 	unsigned long flags, val;
59 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
60 
61 	pm_runtime_get_sync(drvdata->dev);
62 	spin_lock_irqsave(&drvdata->spinlock, flags);
63 	CS_UNLOCK(drvdata->base);
64 
65 	val = etm_readl(drvdata, ETMSR);
66 
67 	CS_LOCK(drvdata->base);
68 	spin_unlock_irqrestore(&drvdata->spinlock, flags);
69 	pm_runtime_put(drvdata->dev);
70 
71 	return sprintf(buf, "%#lx\n", val);
72 }
73 static DEVICE_ATTR_RO(etmsr);
74 
75 static ssize_t reset_store(struct device *dev,
76 			   struct device_attribute *attr,
77 			   const char *buf, size_t size)
78 {
79 	int i, ret;
80 	unsigned long val;
81 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
82 	struct etm_config *config = &drvdata->config;
83 
84 	ret = kstrtoul(buf, 16, &val);
85 	if (ret)
86 		return ret;
87 
88 	if (val) {
89 		spin_lock(&drvdata->spinlock);
90 		memset(config, 0, sizeof(struct etm_config));
91 		config->mode = ETM_MODE_EXCLUDE;
92 		config->trigger_event = ETM_DEFAULT_EVENT_VAL;
93 		for (i = 0; i < drvdata->nr_addr_cmp; i++) {
94 			config->addr_type[i] = ETM_ADDR_TYPE_NONE;
95 		}
96 
97 		etm_set_default(config);
98 		spin_unlock(&drvdata->spinlock);
99 	}
100 
101 	return size;
102 }
103 static DEVICE_ATTR_WO(reset);
104 
105 static ssize_t mode_show(struct device *dev,
106 			 struct device_attribute *attr, char *buf)
107 {
108 	unsigned long val;
109 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
110 	struct etm_config *config = &drvdata->config;
111 
112 	val = config->mode;
113 	return sprintf(buf, "%#lx\n", val);
114 }
115 
116 static ssize_t mode_store(struct device *dev,
117 			  struct device_attribute *attr,
118 			  const char *buf, size_t size)
119 {
120 	int ret;
121 	unsigned long val;
122 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
123 	struct etm_config *config = &drvdata->config;
124 
125 	ret = kstrtoul(buf, 16, &val);
126 	if (ret)
127 		return ret;
128 
129 	spin_lock(&drvdata->spinlock);
130 	config->mode = val & ETM_MODE_ALL;
131 
132 	if (config->mode & ETM_MODE_EXCLUDE)
133 		config->enable_ctrl1 |= ETMTECR1_INC_EXC;
134 	else
135 		config->enable_ctrl1 &= ~ETMTECR1_INC_EXC;
136 
137 	if (config->mode & ETM_MODE_CYCACC)
138 		config->ctrl |= ETMCR_CYC_ACC;
139 	else
140 		config->ctrl &= ~ETMCR_CYC_ACC;
141 
142 	if (config->mode & ETM_MODE_STALL) {
143 		if (!(drvdata->etmccr & ETMCCR_FIFOFULL)) {
144 			dev_warn(drvdata->dev, "stall mode not supported\n");
145 			ret = -EINVAL;
146 			goto err_unlock;
147 		}
148 		config->ctrl |= ETMCR_STALL_MODE;
149 	} else
150 		config->ctrl &= ~ETMCR_STALL_MODE;
151 
152 	if (config->mode & ETM_MODE_TIMESTAMP) {
153 		if (!(drvdata->etmccer & ETMCCER_TIMESTAMP)) {
154 			dev_warn(drvdata->dev, "timestamp not supported\n");
155 			ret = -EINVAL;
156 			goto err_unlock;
157 		}
158 		config->ctrl |= ETMCR_TIMESTAMP_EN;
159 	} else
160 		config->ctrl &= ~ETMCR_TIMESTAMP_EN;
161 
162 	if (config->mode & ETM_MODE_CTXID)
163 		config->ctrl |= ETMCR_CTXID_SIZE;
164 	else
165 		config->ctrl &= ~ETMCR_CTXID_SIZE;
166 
167 	if (config->mode & ETM_MODE_BBROAD)
168 		config->ctrl |= ETMCR_BRANCH_BROADCAST;
169 	else
170 		config->ctrl &= ~ETMCR_BRANCH_BROADCAST;
171 
172 	if (config->mode & ETM_MODE_RET_STACK)
173 		config->ctrl |= ETMCR_RETURN_STACK;
174 	else
175 		config->ctrl &= ~ETMCR_RETURN_STACK;
176 
177 	if (config->mode & (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
178 		etm_config_trace_mode(config);
179 
180 	spin_unlock(&drvdata->spinlock);
181 
182 	return size;
183 
184 err_unlock:
185 	spin_unlock(&drvdata->spinlock);
186 	return ret;
187 }
188 static DEVICE_ATTR_RW(mode);
189 
190 static ssize_t trigger_event_show(struct device *dev,
191 				  struct device_attribute *attr, char *buf)
192 {
193 	unsigned long val;
194 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
195 	struct etm_config *config = &drvdata->config;
196 
197 	val = config->trigger_event;
198 	return sprintf(buf, "%#lx\n", val);
199 }
200 
201 static ssize_t trigger_event_store(struct device *dev,
202 				   struct device_attribute *attr,
203 				   const char *buf, size_t size)
204 {
205 	int ret;
206 	unsigned long val;
207 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
208 	struct etm_config *config = &drvdata->config;
209 
210 	ret = kstrtoul(buf, 16, &val);
211 	if (ret)
212 		return ret;
213 
214 	config->trigger_event = val & ETM_EVENT_MASK;
215 
216 	return size;
217 }
218 static DEVICE_ATTR_RW(trigger_event);
219 
220 static ssize_t enable_event_show(struct device *dev,
221 				 struct device_attribute *attr, char *buf)
222 {
223 	unsigned long val;
224 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
225 	struct etm_config *config = &drvdata->config;
226 
227 	val = config->enable_event;
228 	return sprintf(buf, "%#lx\n", val);
229 }
230 
231 static ssize_t enable_event_store(struct device *dev,
232 				  struct device_attribute *attr,
233 				  const char *buf, size_t size)
234 {
235 	int ret;
236 	unsigned long val;
237 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
238 	struct etm_config *config = &drvdata->config;
239 
240 	ret = kstrtoul(buf, 16, &val);
241 	if (ret)
242 		return ret;
243 
244 	config->enable_event = val & ETM_EVENT_MASK;
245 
246 	return size;
247 }
248 static DEVICE_ATTR_RW(enable_event);
249 
250 static ssize_t fifofull_level_show(struct device *dev,
251 				   struct device_attribute *attr, char *buf)
252 {
253 	unsigned long val;
254 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
255 	struct etm_config *config = &drvdata->config;
256 
257 	val = config->fifofull_level;
258 	return sprintf(buf, "%#lx\n", val);
259 }
260 
261 static ssize_t fifofull_level_store(struct device *dev,
262 				    struct device_attribute *attr,
263 				    const char *buf, size_t size)
264 {
265 	int ret;
266 	unsigned long val;
267 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
268 	struct etm_config *config = &drvdata->config;
269 
270 	ret = kstrtoul(buf, 16, &val);
271 	if (ret)
272 		return ret;
273 
274 	config->fifofull_level = val;
275 
276 	return size;
277 }
278 static DEVICE_ATTR_RW(fifofull_level);
279 
280 static ssize_t addr_idx_show(struct device *dev,
281 			     struct device_attribute *attr, char *buf)
282 {
283 	unsigned long val;
284 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
285 	struct etm_config *config = &drvdata->config;
286 
287 	val = config->addr_idx;
288 	return sprintf(buf, "%#lx\n", val);
289 }
290 
291 static ssize_t addr_idx_store(struct device *dev,
292 			      struct device_attribute *attr,
293 			      const char *buf, size_t size)
294 {
295 	int ret;
296 	unsigned long val;
297 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
298 	struct etm_config *config = &drvdata->config;
299 
300 	ret = kstrtoul(buf, 16, &val);
301 	if (ret)
302 		return ret;
303 
304 	if (val >= drvdata->nr_addr_cmp)
305 		return -EINVAL;
306 
307 	/*
308 	 * Use spinlock to ensure index doesn't change while it gets
309 	 * dereferenced multiple times within a spinlock block elsewhere.
310 	 */
311 	spin_lock(&drvdata->spinlock);
312 	config->addr_idx = val;
313 	spin_unlock(&drvdata->spinlock);
314 
315 	return size;
316 }
317 static DEVICE_ATTR_RW(addr_idx);
318 
319 static ssize_t addr_single_show(struct device *dev,
320 				struct device_attribute *attr, char *buf)
321 {
322 	u8 idx;
323 	unsigned long val;
324 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
325 	struct etm_config *config = &drvdata->config;
326 
327 	spin_lock(&drvdata->spinlock);
328 	idx = config->addr_idx;
329 	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
330 	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
331 		spin_unlock(&drvdata->spinlock);
332 		return -EINVAL;
333 	}
334 
335 	val = config->addr_val[idx];
336 	spin_unlock(&drvdata->spinlock);
337 
338 	return sprintf(buf, "%#lx\n", val);
339 }
340 
341 static ssize_t addr_single_store(struct device *dev,
342 				 struct device_attribute *attr,
343 				 const char *buf, size_t size)
344 {
345 	u8 idx;
346 	int ret;
347 	unsigned long val;
348 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
349 	struct etm_config *config = &drvdata->config;
350 
351 	ret = kstrtoul(buf, 16, &val);
352 	if (ret)
353 		return ret;
354 
355 	spin_lock(&drvdata->spinlock);
356 	idx = config->addr_idx;
357 	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
358 	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
359 		spin_unlock(&drvdata->spinlock);
360 		return -EINVAL;
361 	}
362 
363 	config->addr_val[idx] = val;
364 	config->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
365 	spin_unlock(&drvdata->spinlock);
366 
367 	return size;
368 }
369 static DEVICE_ATTR_RW(addr_single);
370 
371 static ssize_t addr_range_show(struct device *dev,
372 			       struct device_attribute *attr, char *buf)
373 {
374 	u8 idx;
375 	unsigned long val1, val2;
376 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
377 	struct etm_config *config = &drvdata->config;
378 
379 	spin_lock(&drvdata->spinlock);
380 	idx = config->addr_idx;
381 	if (idx % 2 != 0) {
382 		spin_unlock(&drvdata->spinlock);
383 		return -EPERM;
384 	}
385 	if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
386 	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
387 	      (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
388 	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
389 		spin_unlock(&drvdata->spinlock);
390 		return -EPERM;
391 	}
392 
393 	val1 = config->addr_val[idx];
394 	val2 = config->addr_val[idx + 1];
395 	spin_unlock(&drvdata->spinlock);
396 
397 	return sprintf(buf, "%#lx %#lx\n", val1, val2);
398 }
399 
400 static ssize_t addr_range_store(struct device *dev,
401 			      struct device_attribute *attr,
402 			      const char *buf, size_t size)
403 {
404 	u8 idx;
405 	unsigned long val1, val2;
406 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
407 	struct etm_config *config = &drvdata->config;
408 
409 	if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
410 		return -EINVAL;
411 	/* Lower address comparator cannot have a higher address value */
412 	if (val1 > val2)
413 		return -EINVAL;
414 
415 	spin_lock(&drvdata->spinlock);
416 	idx = config->addr_idx;
417 	if (idx % 2 != 0) {
418 		spin_unlock(&drvdata->spinlock);
419 		return -EPERM;
420 	}
421 	if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
422 	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
423 	      (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
424 	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
425 		spin_unlock(&drvdata->spinlock);
426 		return -EPERM;
427 	}
428 
429 	config->addr_val[idx] = val1;
430 	config->addr_type[idx] = ETM_ADDR_TYPE_RANGE;
431 	config->addr_val[idx + 1] = val2;
432 	config->addr_type[idx + 1] = ETM_ADDR_TYPE_RANGE;
433 	config->enable_ctrl1 |= (1 << (idx/2));
434 	spin_unlock(&drvdata->spinlock);
435 
436 	return size;
437 }
438 static DEVICE_ATTR_RW(addr_range);
439 
440 static ssize_t addr_start_show(struct device *dev,
441 			       struct device_attribute *attr, char *buf)
442 {
443 	u8 idx;
444 	unsigned long val;
445 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
446 	struct etm_config *config = &drvdata->config;
447 
448 	spin_lock(&drvdata->spinlock);
449 	idx = config->addr_idx;
450 	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
451 	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
452 		spin_unlock(&drvdata->spinlock);
453 		return -EPERM;
454 	}
455 
456 	val = config->addr_val[idx];
457 	spin_unlock(&drvdata->spinlock);
458 
459 	return sprintf(buf, "%#lx\n", val);
460 }
461 
462 static ssize_t addr_start_store(struct device *dev,
463 				struct device_attribute *attr,
464 				const char *buf, size_t size)
465 {
466 	u8 idx;
467 	int ret;
468 	unsigned long val;
469 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
470 	struct etm_config *config = &drvdata->config;
471 
472 	ret = kstrtoul(buf, 16, &val);
473 	if (ret)
474 		return ret;
475 
476 	spin_lock(&drvdata->spinlock);
477 	idx = config->addr_idx;
478 	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
479 	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
480 		spin_unlock(&drvdata->spinlock);
481 		return -EPERM;
482 	}
483 
484 	config->addr_val[idx] = val;
485 	config->addr_type[idx] = ETM_ADDR_TYPE_START;
486 	config->startstop_ctrl |= (1 << idx);
487 	config->enable_ctrl1 |= BIT(25);
488 	spin_unlock(&drvdata->spinlock);
489 
490 	return size;
491 }
492 static DEVICE_ATTR_RW(addr_start);
493 
494 static ssize_t addr_stop_show(struct device *dev,
495 			      struct device_attribute *attr, char *buf)
496 {
497 	u8 idx;
498 	unsigned long val;
499 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
500 	struct etm_config *config = &drvdata->config;
501 
502 	spin_lock(&drvdata->spinlock);
503 	idx = config->addr_idx;
504 	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
505 	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
506 		spin_unlock(&drvdata->spinlock);
507 		return -EPERM;
508 	}
509 
510 	val = config->addr_val[idx];
511 	spin_unlock(&drvdata->spinlock);
512 
513 	return sprintf(buf, "%#lx\n", val);
514 }
515 
516 static ssize_t addr_stop_store(struct device *dev,
517 			       struct device_attribute *attr,
518 			       const char *buf, size_t size)
519 {
520 	u8 idx;
521 	int ret;
522 	unsigned long val;
523 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
524 	struct etm_config *config = &drvdata->config;
525 
526 	ret = kstrtoul(buf, 16, &val);
527 	if (ret)
528 		return ret;
529 
530 	spin_lock(&drvdata->spinlock);
531 	idx = config->addr_idx;
532 	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
533 	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
534 		spin_unlock(&drvdata->spinlock);
535 		return -EPERM;
536 	}
537 
538 	config->addr_val[idx] = val;
539 	config->addr_type[idx] = ETM_ADDR_TYPE_STOP;
540 	config->startstop_ctrl |= (1 << (idx + 16));
541 	config->enable_ctrl1 |= ETMTECR1_START_STOP;
542 	spin_unlock(&drvdata->spinlock);
543 
544 	return size;
545 }
546 static DEVICE_ATTR_RW(addr_stop);
547 
548 static ssize_t addr_acctype_show(struct device *dev,
549 				 struct device_attribute *attr, char *buf)
550 {
551 	unsigned long val;
552 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
553 	struct etm_config *config = &drvdata->config;
554 
555 	spin_lock(&drvdata->spinlock);
556 	val = config->addr_acctype[config->addr_idx];
557 	spin_unlock(&drvdata->spinlock);
558 
559 	return sprintf(buf, "%#lx\n", val);
560 }
561 
562 static ssize_t addr_acctype_store(struct device *dev,
563 				  struct device_attribute *attr,
564 				  const char *buf, size_t size)
565 {
566 	int ret;
567 	unsigned long val;
568 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
569 	struct etm_config *config = &drvdata->config;
570 
571 	ret = kstrtoul(buf, 16, &val);
572 	if (ret)
573 		return ret;
574 
575 	spin_lock(&drvdata->spinlock);
576 	config->addr_acctype[config->addr_idx] = val;
577 	spin_unlock(&drvdata->spinlock);
578 
579 	return size;
580 }
581 static DEVICE_ATTR_RW(addr_acctype);
582 
583 static ssize_t cntr_idx_show(struct device *dev,
584 			     struct device_attribute *attr, char *buf)
585 {
586 	unsigned long val;
587 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
588 	struct etm_config *config = &drvdata->config;
589 
590 	val = config->cntr_idx;
591 	return sprintf(buf, "%#lx\n", val);
592 }
593 
594 static ssize_t cntr_idx_store(struct device *dev,
595 			      struct device_attribute *attr,
596 			      const char *buf, size_t size)
597 {
598 	int ret;
599 	unsigned long val;
600 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
601 	struct etm_config *config = &drvdata->config;
602 
603 	ret = kstrtoul(buf, 16, &val);
604 	if (ret)
605 		return ret;
606 
607 	if (val >= drvdata->nr_cntr)
608 		return -EINVAL;
609 	/*
610 	 * Use spinlock to ensure index doesn't change while it gets
611 	 * dereferenced multiple times within a spinlock block elsewhere.
612 	 */
613 	spin_lock(&drvdata->spinlock);
614 	config->cntr_idx = val;
615 	spin_unlock(&drvdata->spinlock);
616 
617 	return size;
618 }
619 static DEVICE_ATTR_RW(cntr_idx);
620 
621 static ssize_t cntr_rld_val_show(struct device *dev,
622 				 struct device_attribute *attr, char *buf)
623 {
624 	unsigned long val;
625 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
626 	struct etm_config *config = &drvdata->config;
627 
628 	spin_lock(&drvdata->spinlock);
629 	val = config->cntr_rld_val[config->cntr_idx];
630 	spin_unlock(&drvdata->spinlock);
631 
632 	return sprintf(buf, "%#lx\n", val);
633 }
634 
635 static ssize_t cntr_rld_val_store(struct device *dev,
636 				  struct device_attribute *attr,
637 				  const char *buf, size_t size)
638 {
639 	int ret;
640 	unsigned long val;
641 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
642 	struct etm_config *config = &drvdata->config;
643 
644 	ret = kstrtoul(buf, 16, &val);
645 	if (ret)
646 		return ret;
647 
648 	spin_lock(&drvdata->spinlock);
649 	config->cntr_rld_val[config->cntr_idx] = val;
650 	spin_unlock(&drvdata->spinlock);
651 
652 	return size;
653 }
654 static DEVICE_ATTR_RW(cntr_rld_val);
655 
656 static ssize_t cntr_event_show(struct device *dev,
657 			       struct device_attribute *attr, char *buf)
658 {
659 	unsigned long val;
660 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
661 	struct etm_config *config = &drvdata->config;
662 
663 	spin_lock(&drvdata->spinlock);
664 	val = config->cntr_event[config->cntr_idx];
665 	spin_unlock(&drvdata->spinlock);
666 
667 	return sprintf(buf, "%#lx\n", val);
668 }
669 
670 static ssize_t cntr_event_store(struct device *dev,
671 				struct device_attribute *attr,
672 				const char *buf, size_t size)
673 {
674 	int ret;
675 	unsigned long val;
676 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
677 	struct etm_config *config = &drvdata->config;
678 
679 	ret = kstrtoul(buf, 16, &val);
680 	if (ret)
681 		return ret;
682 
683 	spin_lock(&drvdata->spinlock);
684 	config->cntr_event[config->cntr_idx] = val & ETM_EVENT_MASK;
685 	spin_unlock(&drvdata->spinlock);
686 
687 	return size;
688 }
689 static DEVICE_ATTR_RW(cntr_event);
690 
691 static ssize_t cntr_rld_event_show(struct device *dev,
692 				   struct device_attribute *attr, char *buf)
693 {
694 	unsigned long val;
695 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
696 	struct etm_config *config = &drvdata->config;
697 
698 	spin_lock(&drvdata->spinlock);
699 	val = config->cntr_rld_event[config->cntr_idx];
700 	spin_unlock(&drvdata->spinlock);
701 
702 	return sprintf(buf, "%#lx\n", val);
703 }
704 
705 static ssize_t cntr_rld_event_store(struct device *dev,
706 				    struct device_attribute *attr,
707 				    const char *buf, size_t size)
708 {
709 	int ret;
710 	unsigned long val;
711 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
712 	struct etm_config *config = &drvdata->config;
713 
714 	ret = kstrtoul(buf, 16, &val);
715 	if (ret)
716 		return ret;
717 
718 	spin_lock(&drvdata->spinlock);
719 	config->cntr_rld_event[config->cntr_idx] = val & ETM_EVENT_MASK;
720 	spin_unlock(&drvdata->spinlock);
721 
722 	return size;
723 }
724 static DEVICE_ATTR_RW(cntr_rld_event);
725 
726 static ssize_t cntr_val_show(struct device *dev,
727 			     struct device_attribute *attr, char *buf)
728 {
729 	int i, ret = 0;
730 	u32 val;
731 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
732 	struct etm_config *config = &drvdata->config;
733 
734 	if (!local_read(&drvdata->mode)) {
735 		spin_lock(&drvdata->spinlock);
736 		for (i = 0; i < drvdata->nr_cntr; i++)
737 			ret += sprintf(buf, "counter %d: %x\n",
738 				       i, config->cntr_val[i]);
739 		spin_unlock(&drvdata->spinlock);
740 		return ret;
741 	}
742 
743 	for (i = 0; i < drvdata->nr_cntr; i++) {
744 		val = etm_readl(drvdata, ETMCNTVRn(i));
745 		ret += sprintf(buf, "counter %d: %x\n", i, val);
746 	}
747 
748 	return ret;
749 }
750 
751 static ssize_t cntr_val_store(struct device *dev,
752 			      struct device_attribute *attr,
753 			      const char *buf, size_t size)
754 {
755 	int ret;
756 	unsigned long val;
757 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
758 	struct etm_config *config = &drvdata->config;
759 
760 	ret = kstrtoul(buf, 16, &val);
761 	if (ret)
762 		return ret;
763 
764 	spin_lock(&drvdata->spinlock);
765 	config->cntr_val[config->cntr_idx] = val;
766 	spin_unlock(&drvdata->spinlock);
767 
768 	return size;
769 }
770 static DEVICE_ATTR_RW(cntr_val);
771 
772 static ssize_t seq_12_event_show(struct device *dev,
773 				 struct device_attribute *attr, char *buf)
774 {
775 	unsigned long val;
776 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
777 	struct etm_config *config = &drvdata->config;
778 
779 	val = config->seq_12_event;
780 	return sprintf(buf, "%#lx\n", val);
781 }
782 
783 static ssize_t seq_12_event_store(struct device *dev,
784 				  struct device_attribute *attr,
785 				  const char *buf, size_t size)
786 {
787 	int ret;
788 	unsigned long val;
789 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
790 	struct etm_config *config = &drvdata->config;
791 
792 	ret = kstrtoul(buf, 16, &val);
793 	if (ret)
794 		return ret;
795 
796 	config->seq_12_event = val & ETM_EVENT_MASK;
797 	return size;
798 }
799 static DEVICE_ATTR_RW(seq_12_event);
800 
801 static ssize_t seq_21_event_show(struct device *dev,
802 				 struct device_attribute *attr, char *buf)
803 {
804 	unsigned long val;
805 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
806 	struct etm_config *config = &drvdata->config;
807 
808 	val = config->seq_21_event;
809 	return sprintf(buf, "%#lx\n", val);
810 }
811 
812 static ssize_t seq_21_event_store(struct device *dev,
813 				  struct device_attribute *attr,
814 				  const char *buf, size_t size)
815 {
816 	int ret;
817 	unsigned long val;
818 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
819 	struct etm_config *config = &drvdata->config;
820 
821 	ret = kstrtoul(buf, 16, &val);
822 	if (ret)
823 		return ret;
824 
825 	config->seq_21_event = val & ETM_EVENT_MASK;
826 	return size;
827 }
828 static DEVICE_ATTR_RW(seq_21_event);
829 
830 static ssize_t seq_23_event_show(struct device *dev,
831 				 struct device_attribute *attr, char *buf)
832 {
833 	unsigned long val;
834 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
835 	struct etm_config *config = &drvdata->config;
836 
837 	val = config->seq_23_event;
838 	return sprintf(buf, "%#lx\n", val);
839 }
840 
841 static ssize_t seq_23_event_store(struct device *dev,
842 				  struct device_attribute *attr,
843 				  const char *buf, size_t size)
844 {
845 	int ret;
846 	unsigned long val;
847 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
848 	struct etm_config *config = &drvdata->config;
849 
850 	ret = kstrtoul(buf, 16, &val);
851 	if (ret)
852 		return ret;
853 
854 	config->seq_23_event = val & ETM_EVENT_MASK;
855 	return size;
856 }
857 static DEVICE_ATTR_RW(seq_23_event);
858 
859 static ssize_t seq_31_event_show(struct device *dev,
860 				 struct device_attribute *attr, char *buf)
861 {
862 	unsigned long val;
863 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
864 	struct etm_config *config = &drvdata->config;
865 
866 	val = config->seq_31_event;
867 	return sprintf(buf, "%#lx\n", val);
868 }
869 
870 static ssize_t seq_31_event_store(struct device *dev,
871 				  struct device_attribute *attr,
872 				  const char *buf, size_t size)
873 {
874 	int ret;
875 	unsigned long val;
876 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
877 	struct etm_config *config = &drvdata->config;
878 
879 	ret = kstrtoul(buf, 16, &val);
880 	if (ret)
881 		return ret;
882 
883 	config->seq_31_event = val & ETM_EVENT_MASK;
884 	return size;
885 }
886 static DEVICE_ATTR_RW(seq_31_event);
887 
888 static ssize_t seq_32_event_show(struct device *dev,
889 				 struct device_attribute *attr, char *buf)
890 {
891 	unsigned long val;
892 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
893 	struct etm_config *config = &drvdata->config;
894 
895 	val = config->seq_32_event;
896 	return sprintf(buf, "%#lx\n", val);
897 }
898 
899 static ssize_t seq_32_event_store(struct device *dev,
900 				  struct device_attribute *attr,
901 				  const char *buf, size_t size)
902 {
903 	int ret;
904 	unsigned long val;
905 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
906 	struct etm_config *config = &drvdata->config;
907 
908 	ret = kstrtoul(buf, 16, &val);
909 	if (ret)
910 		return ret;
911 
912 	config->seq_32_event = val & ETM_EVENT_MASK;
913 	return size;
914 }
915 static DEVICE_ATTR_RW(seq_32_event);
916 
917 static ssize_t seq_13_event_show(struct device *dev,
918 				 struct device_attribute *attr, char *buf)
919 {
920 	unsigned long val;
921 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
922 	struct etm_config *config = &drvdata->config;
923 
924 	val = config->seq_13_event;
925 	return sprintf(buf, "%#lx\n", val);
926 }
927 
928 static ssize_t seq_13_event_store(struct device *dev,
929 				  struct device_attribute *attr,
930 				  const char *buf, size_t size)
931 {
932 	int ret;
933 	unsigned long val;
934 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
935 	struct etm_config *config = &drvdata->config;
936 
937 	ret = kstrtoul(buf, 16, &val);
938 	if (ret)
939 		return ret;
940 
941 	config->seq_13_event = val & ETM_EVENT_MASK;
942 	return size;
943 }
944 static DEVICE_ATTR_RW(seq_13_event);
945 
946 static ssize_t seq_curr_state_show(struct device *dev,
947 				   struct device_attribute *attr, char *buf)
948 {
949 	unsigned long val, flags;
950 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
951 	struct etm_config *config = &drvdata->config;
952 
953 	if (!local_read(&drvdata->mode)) {
954 		val = config->seq_curr_state;
955 		goto out;
956 	}
957 
958 	pm_runtime_get_sync(drvdata->dev);
959 	spin_lock_irqsave(&drvdata->spinlock, flags);
960 
961 	CS_UNLOCK(drvdata->base);
962 	val = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
963 	CS_LOCK(drvdata->base);
964 
965 	spin_unlock_irqrestore(&drvdata->spinlock, flags);
966 	pm_runtime_put(drvdata->dev);
967 out:
968 	return sprintf(buf, "%#lx\n", val);
969 }
970 
971 static ssize_t seq_curr_state_store(struct device *dev,
972 				    struct device_attribute *attr,
973 				    const char *buf, size_t size)
974 {
975 	int ret;
976 	unsigned long val;
977 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
978 	struct etm_config *config = &drvdata->config;
979 
980 	ret = kstrtoul(buf, 16, &val);
981 	if (ret)
982 		return ret;
983 
984 	if (val > ETM_SEQ_STATE_MAX_VAL)
985 		return -EINVAL;
986 
987 	config->seq_curr_state = val;
988 
989 	return size;
990 }
991 static DEVICE_ATTR_RW(seq_curr_state);
992 
993 static ssize_t ctxid_idx_show(struct device *dev,
994 			      struct device_attribute *attr, char *buf)
995 {
996 	unsigned long val;
997 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
998 	struct etm_config *config = &drvdata->config;
999 
1000 	val = config->ctxid_idx;
1001 	return sprintf(buf, "%#lx\n", val);
1002 }
1003 
1004 static ssize_t ctxid_idx_store(struct device *dev,
1005 				struct device_attribute *attr,
1006 				const char *buf, size_t size)
1007 {
1008 	int ret;
1009 	unsigned long val;
1010 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1011 	struct etm_config *config = &drvdata->config;
1012 
1013 	ret = kstrtoul(buf, 16, &val);
1014 	if (ret)
1015 		return ret;
1016 
1017 	if (val >= drvdata->nr_ctxid_cmp)
1018 		return -EINVAL;
1019 
1020 	/*
1021 	 * Use spinlock to ensure index doesn't change while it gets
1022 	 * dereferenced multiple times within a spinlock block elsewhere.
1023 	 */
1024 	spin_lock(&drvdata->spinlock);
1025 	config->ctxid_idx = val;
1026 	spin_unlock(&drvdata->spinlock);
1027 
1028 	return size;
1029 }
1030 static DEVICE_ATTR_RW(ctxid_idx);
1031 
1032 static ssize_t ctxid_pid_show(struct device *dev,
1033 			      struct device_attribute *attr, char *buf)
1034 {
1035 	unsigned long val;
1036 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1037 	struct etm_config *config = &drvdata->config;
1038 
1039 	spin_lock(&drvdata->spinlock);
1040 	val = config->ctxid_vpid[config->ctxid_idx];
1041 	spin_unlock(&drvdata->spinlock);
1042 
1043 	return sprintf(buf, "%#lx\n", val);
1044 }
1045 
1046 static ssize_t ctxid_pid_store(struct device *dev,
1047 			       struct device_attribute *attr,
1048 			       const char *buf, size_t size)
1049 {
1050 	int ret;
1051 	unsigned long vpid, pid;
1052 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1053 	struct etm_config *config = &drvdata->config;
1054 
1055 	ret = kstrtoul(buf, 16, &vpid);
1056 	if (ret)
1057 		return ret;
1058 
1059 	pid = coresight_vpid_to_pid(vpid);
1060 
1061 	spin_lock(&drvdata->spinlock);
1062 	config->ctxid_pid[config->ctxid_idx] = pid;
1063 	config->ctxid_vpid[config->ctxid_idx] = vpid;
1064 	spin_unlock(&drvdata->spinlock);
1065 
1066 	return size;
1067 }
1068 static DEVICE_ATTR_RW(ctxid_pid);
1069 
1070 static ssize_t ctxid_mask_show(struct device *dev,
1071 			       struct device_attribute *attr, char *buf)
1072 {
1073 	unsigned long val;
1074 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1075 	struct etm_config *config = &drvdata->config;
1076 
1077 	val = config->ctxid_mask;
1078 	return sprintf(buf, "%#lx\n", val);
1079 }
1080 
1081 static ssize_t ctxid_mask_store(struct device *dev,
1082 				struct device_attribute *attr,
1083 				const char *buf, size_t size)
1084 {
1085 	int ret;
1086 	unsigned long val;
1087 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1088 	struct etm_config *config = &drvdata->config;
1089 
1090 	ret = kstrtoul(buf, 16, &val);
1091 	if (ret)
1092 		return ret;
1093 
1094 	config->ctxid_mask = val;
1095 	return size;
1096 }
1097 static DEVICE_ATTR_RW(ctxid_mask);
1098 
1099 static ssize_t sync_freq_show(struct device *dev,
1100 			      struct device_attribute *attr, char *buf)
1101 {
1102 	unsigned long val;
1103 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1104 	struct etm_config *config = &drvdata->config;
1105 
1106 	val = config->sync_freq;
1107 	return sprintf(buf, "%#lx\n", val);
1108 }
1109 
1110 static ssize_t sync_freq_store(struct device *dev,
1111 			       struct device_attribute *attr,
1112 			       const char *buf, size_t size)
1113 {
1114 	int ret;
1115 	unsigned long val;
1116 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1117 	struct etm_config *config = &drvdata->config;
1118 
1119 	ret = kstrtoul(buf, 16, &val);
1120 	if (ret)
1121 		return ret;
1122 
1123 	config->sync_freq = val & ETM_SYNC_MASK;
1124 	return size;
1125 }
1126 static DEVICE_ATTR_RW(sync_freq);
1127 
1128 static ssize_t timestamp_event_show(struct device *dev,
1129 				    struct device_attribute *attr, char *buf)
1130 {
1131 	unsigned long val;
1132 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1133 	struct etm_config *config = &drvdata->config;
1134 
1135 	val = config->timestamp_event;
1136 	return sprintf(buf, "%#lx\n", val);
1137 }
1138 
1139 static ssize_t timestamp_event_store(struct device *dev,
1140 				     struct device_attribute *attr,
1141 				     const char *buf, size_t size)
1142 {
1143 	int ret;
1144 	unsigned long val;
1145 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1146 	struct etm_config *config = &drvdata->config;
1147 
1148 	ret = kstrtoul(buf, 16, &val);
1149 	if (ret)
1150 		return ret;
1151 
1152 	config->timestamp_event = val & ETM_EVENT_MASK;
1153 	return size;
1154 }
1155 static DEVICE_ATTR_RW(timestamp_event);
1156 
1157 static ssize_t cpu_show(struct device *dev,
1158 			struct device_attribute *attr, char *buf)
1159 {
1160 	int val;
1161 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1162 
1163 	val = drvdata->cpu;
1164 	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1165 
1166 }
1167 static DEVICE_ATTR_RO(cpu);
1168 
1169 static ssize_t traceid_show(struct device *dev,
1170 			    struct device_attribute *attr, char *buf)
1171 {
1172 	unsigned long val;
1173 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1174 
1175 	val = etm_get_trace_id(drvdata);
1176 
1177 	return sprintf(buf, "%#lx\n", val);
1178 }
1179 
1180 static ssize_t traceid_store(struct device *dev,
1181 			     struct device_attribute *attr,
1182 			     const char *buf, size_t size)
1183 {
1184 	int ret;
1185 	unsigned long val;
1186 	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1187 
1188 	ret = kstrtoul(buf, 16, &val);
1189 	if (ret)
1190 		return ret;
1191 
1192 	drvdata->traceid = val & ETM_TRACEID_MASK;
1193 	return size;
1194 }
1195 static DEVICE_ATTR_RW(traceid);
1196 
1197 static struct attribute *coresight_etm_attrs[] = {
1198 	&dev_attr_nr_addr_cmp.attr,
1199 	&dev_attr_nr_cntr.attr,
1200 	&dev_attr_nr_ctxid_cmp.attr,
1201 	&dev_attr_etmsr.attr,
1202 	&dev_attr_reset.attr,
1203 	&dev_attr_mode.attr,
1204 	&dev_attr_trigger_event.attr,
1205 	&dev_attr_enable_event.attr,
1206 	&dev_attr_fifofull_level.attr,
1207 	&dev_attr_addr_idx.attr,
1208 	&dev_attr_addr_single.attr,
1209 	&dev_attr_addr_range.attr,
1210 	&dev_attr_addr_start.attr,
1211 	&dev_attr_addr_stop.attr,
1212 	&dev_attr_addr_acctype.attr,
1213 	&dev_attr_cntr_idx.attr,
1214 	&dev_attr_cntr_rld_val.attr,
1215 	&dev_attr_cntr_event.attr,
1216 	&dev_attr_cntr_rld_event.attr,
1217 	&dev_attr_cntr_val.attr,
1218 	&dev_attr_seq_12_event.attr,
1219 	&dev_attr_seq_21_event.attr,
1220 	&dev_attr_seq_23_event.attr,
1221 	&dev_attr_seq_31_event.attr,
1222 	&dev_attr_seq_32_event.attr,
1223 	&dev_attr_seq_13_event.attr,
1224 	&dev_attr_seq_curr_state.attr,
1225 	&dev_attr_ctxid_idx.attr,
1226 	&dev_attr_ctxid_pid.attr,
1227 	&dev_attr_ctxid_mask.attr,
1228 	&dev_attr_sync_freq.attr,
1229 	&dev_attr_timestamp_event.attr,
1230 	&dev_attr_traceid.attr,
1231 	&dev_attr_cpu.attr,
1232 	NULL,
1233 };
1234 
1235 #define coresight_etm3x_simple_func(name, offset)			\
1236 	coresight_simple_func(struct etm_drvdata, NULL, name, offset)
1237 
1238 coresight_etm3x_simple_func(etmccr, ETMCCR);
1239 coresight_etm3x_simple_func(etmccer, ETMCCER);
1240 coresight_etm3x_simple_func(etmscr, ETMSCR);
1241 coresight_etm3x_simple_func(etmidr, ETMIDR);
1242 coresight_etm3x_simple_func(etmcr, ETMCR);
1243 coresight_etm3x_simple_func(etmtraceidr, ETMTRACEIDR);
1244 coresight_etm3x_simple_func(etmteevr, ETMTEEVR);
1245 coresight_etm3x_simple_func(etmtssvr, ETMTSSCR);
1246 coresight_etm3x_simple_func(etmtecr1, ETMTECR1);
1247 coresight_etm3x_simple_func(etmtecr2, ETMTECR2);
1248 
1249 static struct attribute *coresight_etm_mgmt_attrs[] = {
1250 	&dev_attr_etmccr.attr,
1251 	&dev_attr_etmccer.attr,
1252 	&dev_attr_etmscr.attr,
1253 	&dev_attr_etmidr.attr,
1254 	&dev_attr_etmcr.attr,
1255 	&dev_attr_etmtraceidr.attr,
1256 	&dev_attr_etmteevr.attr,
1257 	&dev_attr_etmtssvr.attr,
1258 	&dev_attr_etmtecr1.attr,
1259 	&dev_attr_etmtecr2.attr,
1260 	NULL,
1261 };
1262 
1263 static const struct attribute_group coresight_etm_group = {
1264 	.attrs = coresight_etm_attrs,
1265 };
1266 
1267 static const struct attribute_group coresight_etm_mgmt_group = {
1268 	.attrs = coresight_etm_mgmt_attrs,
1269 	.name = "mgmt",
1270 };
1271 
1272 const struct attribute_group *coresight_etm_groups[] = {
1273 	&coresight_etm_group,
1274 	&coresight_etm_mgmt_group,
1275 	NULL,
1276 };
1277