1 // SPDX-License-Identifier: GPL-2.0
3 * Intel(R) Trace Hub Global Trace Hub
5 * Copyright (C) 2014-2015 Intel Corporation.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/bitmap.h>
17 #include <linux/pm_runtime.h>
25 * struct gth_output - GTH view on an output port
26 * @gth: backlink to the GTH device
27 * @output: link to output device's output descriptor
28 * @index: output port number
29 * @port_type: one of GTH_* port type values
30 * @master: bitmap of masters configured for this output
33 struct gth_device
*gth
;
34 struct intel_th_output
*output
;
36 unsigned int port_type
;
37 DECLARE_BITMAP(master
, TH_CONFIGURABLE_MASTERS
+ 1);
41 * struct gth_device - GTH device
42 * @dev: driver core's device
43 * @base: register window base address
44 * @output_group: attributes describing output ports
45 * @master_group: attributes describing master assignments
46 * @output: output ports
47 * @master: master/output port assignments
48 * @gth_lock: serializes accesses to GTH bits
54 struct attribute_group output_group
;
55 struct attribute_group master_group
;
56 struct gth_output output
[TH_POSSIBLE_OUTPUTS
];
57 signed char master
[TH_CONFIGURABLE_MASTERS
+ 1];
61 static void gth_output_set(struct gth_device
*gth
, int port
,
64 unsigned long reg
= port
& 4 ? REG_GTH_GTHOPT1
: REG_GTH_GTHOPT0
;
66 int shift
= (port
& 3) * 8;
68 val
= ioread32(gth
->base
+ reg
);
69 val
&= ~(0xff << shift
);
70 val
|= config
<< shift
;
71 iowrite32(val
, gth
->base
+ reg
);
74 static unsigned int gth_output_get(struct gth_device
*gth
, int port
)
76 unsigned long reg
= port
& 4 ? REG_GTH_GTHOPT1
: REG_GTH_GTHOPT0
;
78 int shift
= (port
& 3) * 8;
80 val
= ioread32(gth
->base
+ reg
);
87 static void gth_smcfreq_set(struct gth_device
*gth
, int port
,
90 unsigned long reg
= REG_GTH_SMCR0
+ ((port
/ 2) * 4);
91 int shift
= (port
& 1) * 16;
94 val
= ioread32(gth
->base
+ reg
);
95 val
&= ~(0xffff << shift
);
97 iowrite32(val
, gth
->base
+ reg
);
100 static unsigned int gth_smcfreq_get(struct gth_device
*gth
, int port
)
102 unsigned long reg
= REG_GTH_SMCR0
+ ((port
/ 2) * 4);
103 int shift
= (port
& 1) * 16;
106 val
= ioread32(gth
->base
+ reg
);
107 val
&= 0xffff << shift
;
114 * "masters" attribute group
117 struct master_attribute
{
118 struct device_attribute attr
;
119 struct gth_device
*gth
;
124 gth_master_set(struct gth_device
*gth
, unsigned int master
, int port
)
126 unsigned int reg
= REG_GTH_SWDEST0
+ ((master
>> 1) & ~3u);
127 unsigned int shift
= (master
& 0x7) * 4;
131 reg
= REG_GTH_GSWTDEST
;
135 val
= ioread32(gth
->base
+ reg
);
136 val
&= ~(0xf << shift
);
138 val
|= (0x8 | port
) << shift
;
139 iowrite32(val
, gth
->base
+ reg
);
142 static ssize_t
master_attr_show(struct device
*dev
,
143 struct device_attribute
*attr
,
146 struct master_attribute
*ma
=
147 container_of(attr
, struct master_attribute
, attr
);
148 struct gth_device
*gth
= ma
->gth
;
152 spin_lock(>h
->gth_lock
);
153 port
= gth
->master
[ma
->master
];
154 spin_unlock(>h
->gth_lock
);
157 count
= snprintf(buf
, PAGE_SIZE
, "%x\n", port
);
159 count
= snprintf(buf
, PAGE_SIZE
, "disabled\n");
164 static ssize_t
master_attr_store(struct device
*dev
,
165 struct device_attribute
*attr
,
166 const char *buf
, size_t count
)
168 struct master_attribute
*ma
=
169 container_of(attr
, struct master_attribute
, attr
);
170 struct gth_device
*gth
= ma
->gth
;
173 if (kstrtoint(buf
, 10, &port
) < 0)
176 if (port
>= TH_POSSIBLE_OUTPUTS
|| port
< -1)
179 spin_lock(>h
->gth_lock
);
181 /* disconnect from the previous output port, if any */
182 old_port
= gth
->master
[ma
->master
];
184 gth
->master
[ma
->master
] = -1;
185 clear_bit(ma
->master
, gth
->output
[old_port
].master
);
188 * if the port is active, program this setting,
189 * implies that runtime PM is on
191 if (gth
->output
[old_port
].output
->active
)
192 gth_master_set(gth
, ma
->master
, -1);
195 /* connect to the new output port, if any */
197 /* check if there's a driver for this port */
198 if (!gth
->output
[port
].output
) {
203 set_bit(ma
->master
, gth
->output
[port
].master
);
205 /* if the port is active, program this setting, see above */
206 if (gth
->output
[port
].output
->active
)
207 gth_master_set(gth
, ma
->master
, port
);
210 gth
->master
[ma
->master
] = port
;
213 spin_unlock(>h
->gth_lock
);
218 struct output_attribute
{
219 struct device_attribute attr
;
220 struct gth_device
*gth
;
225 #define OUTPUT_PARM(_name, _mask, _r, _w, _what) \
226 [TH_OUTPUT_PARM(_name)] = { .name = __stringify(_name), \
227 .get = gth_ ## _what ## _get, \
228 .set = gth_ ## _what ## _set, \
233 static const struct output_parm
{
235 unsigned int (*get
)(struct gth_device
*gth
, int port
);
236 void (*set
)(struct gth_device
*gth
, int port
,
239 unsigned int readable
: 1,
242 OUTPUT_PARM(port
, 0x7, 1, 0, output
),
243 OUTPUT_PARM(null
, BIT(3), 1, 1, output
),
244 OUTPUT_PARM(drop
, BIT(4), 1, 1, output
),
245 OUTPUT_PARM(reset
, BIT(5), 1, 0, output
),
246 OUTPUT_PARM(flush
, BIT(7), 0, 1, output
),
247 OUTPUT_PARM(smcfreq
, 0xffff, 1, 1, smcfreq
),
251 gth_output_parm_set(struct gth_device
*gth
, int port
, unsigned int parm
,
254 unsigned int config
= output_parms
[parm
].get(gth
, port
);
255 unsigned int mask
= output_parms
[parm
].mask
;
256 unsigned int shift
= __ffs(mask
);
259 config
|= (val
<< shift
) & mask
;
260 output_parms
[parm
].set(gth
, port
, config
);
264 gth_output_parm_get(struct gth_device
*gth
, int port
, unsigned int parm
)
266 unsigned int config
= output_parms
[parm
].get(gth
, port
);
267 unsigned int mask
= output_parms
[parm
].mask
;
268 unsigned int shift
= __ffs(mask
);
276 * Reset outputs and sources
278 static int intel_th_gth_reset(struct gth_device
*gth
)
283 reg
= ioread32(gth
->base
+ REG_GTH_SCRPD0
);
284 if (reg
& SCRPD_DEBUGGER_IN_USE
)
287 /* Always save/restore STH and TU registers in S0ix entry/exit */
288 reg
|= SCRPD_STH_IS_ENABLED
| SCRPD_TRIGGER_IS_ENABLED
;
289 iowrite32(reg
, gth
->base
+ REG_GTH_SCRPD0
);
292 for (port
= 0; port
< 8; port
++) {
293 if (gth_output_parm_get(gth
, port
, TH_OUTPUT_PARM(port
)) ==
297 gth_output_set(gth
, port
, 0);
298 gth_smcfreq_set(gth
, port
, 16);
300 /* disable overrides */
301 iowrite32(0, gth
->base
+ REG_GTH_DESTOVR
);
303 /* masters swdest_0~31 and gswdest */
304 for (i
= 0; i
< 33; i
++)
305 iowrite32(0, gth
->base
+ REG_GTH_SWDEST0
+ i
* 4);
308 iowrite32(0, gth
->base
+ REG_GTH_SCR
);
309 iowrite32(0xfc, gth
->base
+ REG_GTH_SCR2
);
311 /* setup CTS for single trigger */
312 iowrite32(CTS_EVENT_ENABLE_IF_ANYTHING
, gth
->base
+ REG_CTS_C0S0_EN
);
313 iowrite32(CTS_ACTION_CONTROL_SET_STATE(CTS_STATE_IDLE
) |
314 CTS_ACTION_CONTROL_TRIGGER
, gth
->base
+ REG_CTS_C0S0_ACT
);
320 * "outputs" attribute group
323 static ssize_t
output_attr_show(struct device
*dev
,
324 struct device_attribute
*attr
,
327 struct output_attribute
*oa
=
328 container_of(attr
, struct output_attribute
, attr
);
329 struct gth_device
*gth
= oa
->gth
;
332 pm_runtime_get_sync(dev
);
334 spin_lock(>h
->gth_lock
);
335 count
= snprintf(buf
, PAGE_SIZE
, "%x\n",
336 gth_output_parm_get(gth
, oa
->port
, oa
->parm
));
337 spin_unlock(>h
->gth_lock
);
344 static ssize_t
output_attr_store(struct device
*dev
,
345 struct device_attribute
*attr
,
346 const char *buf
, size_t count
)
348 struct output_attribute
*oa
=
349 container_of(attr
, struct output_attribute
, attr
);
350 struct gth_device
*gth
= oa
->gth
;
353 if (kstrtouint(buf
, 16, &config
) < 0)
356 pm_runtime_get_sync(dev
);
358 spin_lock(>h
->gth_lock
);
359 gth_output_parm_set(gth
, oa
->port
, oa
->parm
, config
);
360 spin_unlock(>h
->gth_lock
);
367 static int intel_th_master_attributes(struct gth_device
*gth
)
369 struct master_attribute
*master_attrs
;
370 struct attribute
**attrs
;
371 int i
, nattrs
= TH_CONFIGURABLE_MASTERS
+ 2;
373 attrs
= devm_kcalloc(gth
->dev
, nattrs
, sizeof(void *), GFP_KERNEL
);
377 master_attrs
= devm_kcalloc(gth
->dev
, nattrs
,
378 sizeof(struct master_attribute
),
383 for (i
= 0; i
< TH_CONFIGURABLE_MASTERS
+ 1; i
++) {
386 name
= devm_kasprintf(gth
->dev
, GFP_KERNEL
, "%d%s", i
,
387 i
== TH_CONFIGURABLE_MASTERS
? "+" : "");
391 master_attrs
[i
].attr
.attr
.name
= name
;
392 master_attrs
[i
].attr
.attr
.mode
= S_IRUGO
| S_IWUSR
;
393 master_attrs
[i
].attr
.show
= master_attr_show
;
394 master_attrs
[i
].attr
.store
= master_attr_store
;
396 sysfs_attr_init(&master_attrs
[i
].attr
.attr
);
397 attrs
[i
] = &master_attrs
[i
].attr
.attr
;
399 master_attrs
[i
].gth
= gth
;
400 master_attrs
[i
].master
= i
;
403 gth
->master_group
.name
= "masters";
404 gth
->master_group
.attrs
= attrs
;
406 return sysfs_create_group(>h
->dev
->kobj
, >h
->master_group
);
409 static int intel_th_output_attributes(struct gth_device
*gth
)
411 struct output_attribute
*out_attrs
;
412 struct attribute
**attrs
;
413 int i
, j
, nouts
= TH_POSSIBLE_OUTPUTS
;
414 int nparms
= ARRAY_SIZE(output_parms
);
415 int nattrs
= nouts
* nparms
+ 1;
417 attrs
= devm_kcalloc(gth
->dev
, nattrs
, sizeof(void *), GFP_KERNEL
);
421 out_attrs
= devm_kcalloc(gth
->dev
, nattrs
,
422 sizeof(struct output_attribute
),
427 for (i
= 0; i
< nouts
; i
++) {
428 for (j
= 0; j
< nparms
; j
++) {
429 unsigned int idx
= i
* nparms
+ j
;
432 name
= devm_kasprintf(gth
->dev
, GFP_KERNEL
, "%d_%s", i
,
433 output_parms
[j
].name
);
437 out_attrs
[idx
].attr
.attr
.name
= name
;
439 if (output_parms
[j
].readable
) {
440 out_attrs
[idx
].attr
.attr
.mode
|= S_IRUGO
;
441 out_attrs
[idx
].attr
.show
= output_attr_show
;
444 if (output_parms
[j
].writable
) {
445 out_attrs
[idx
].attr
.attr
.mode
|= S_IWUSR
;
446 out_attrs
[idx
].attr
.store
= output_attr_store
;
449 sysfs_attr_init(&out_attrs
[idx
].attr
.attr
);
450 attrs
[idx
] = &out_attrs
[idx
].attr
.attr
;
452 out_attrs
[idx
].gth
= gth
;
453 out_attrs
[idx
].port
= i
;
454 out_attrs
[idx
].parm
= j
;
458 gth
->output_group
.name
= "outputs";
459 gth
->output_group
.attrs
= attrs
;
461 return sysfs_create_group(>h
->dev
->kobj
, >h
->output_group
);
465 * intel_th_gth_stop() - stop tracing to an output device
467 * @output: output device's descriptor
468 * @capture_done: set when no more traces will be captured
470 * This will stop tracing using force storeEn off signal and wait for the
471 * pipelines to be empty for the corresponding output port.
473 static void intel_th_gth_stop(struct gth_device
*gth
,
474 struct intel_th_output
*output
,
477 struct intel_th_device
*outdev
=
478 container_of(output
, struct intel_th_device
, output
);
479 struct intel_th_driver
*outdrv
=
480 to_intel_th_driver(outdev
->dev
.driver
);
483 u32 scr2
= 0xfc | (capture_done
? 1 : 0);
485 iowrite32(0, gth
->base
+ REG_GTH_SCR
);
486 iowrite32(scr2
, gth
->base
+ REG_GTH_SCR2
);
488 /* wait on pipeline empty for the given port */
489 for (reg
= 0, count
= GTH_PLE_WAITLOOP_DEPTH
;
490 count
&& !(reg
& BIT(output
->port
)); count
--) {
491 reg
= ioread32(gth
->base
+ REG_GTH_STAT
);
496 dev_dbg(gth
->dev
, "timeout waiting for GTH[%d] PLE\n",
499 /* wait on output piepline empty */
500 if (outdrv
->wait_empty
)
501 outdrv
->wait_empty(outdev
);
503 /* clear force capture done for next captures */
504 iowrite32(0xfc, gth
->base
+ REG_GTH_SCR2
);
508 * intel_th_gth_start() - start tracing to an output device
510 * @output: output device's descriptor
512 * This will start tracing using force storeEn signal.
514 static void intel_th_gth_start(struct gth_device
*gth
,
515 struct intel_th_output
*output
)
519 if (output
->multiblock
)
522 iowrite32(scr
, gth
->base
+ REG_GTH_SCR
);
523 iowrite32(0, gth
->base
+ REG_GTH_SCR2
);
527 * intel_th_gth_disable() - disable tracing to an output device
529 * @output: output device's descriptor
531 * This will deconfigure all masters set to output to this device,
532 * disable tracing using force storeEn off signal and wait for the
533 * "pipeline empty" bit for corresponding output port.
535 static void intel_th_gth_disable(struct intel_th_device
*thdev
,
536 struct intel_th_output
*output
)
538 struct gth_device
*gth
= dev_get_drvdata(&thdev
->dev
);
542 spin_lock(>h
->gth_lock
);
543 output
->active
= false;
545 for_each_set_bit(master
, gth
->output
[output
->port
].master
,
546 TH_CONFIGURABLE_MASTERS
) {
547 gth_master_set(gth
, master
, -1);
549 spin_unlock(>h
->gth_lock
);
551 intel_th_gth_stop(gth
, output
, true);
553 reg
= ioread32(gth
->base
+ REG_GTH_SCRPD0
);
554 reg
&= ~output
->scratchpad
;
555 iowrite32(reg
, gth
->base
+ REG_GTH_SCRPD0
);
558 static void gth_tscu_resync(struct gth_device
*gth
)
562 reg
= ioread32(gth
->base
+ REG_TSCU_TSUCTRL
);
563 reg
&= ~TSUCTRL_CTCRESYNC
;
564 iowrite32(reg
, gth
->base
+ REG_TSCU_TSUCTRL
);
568 * intel_th_gth_enable() - enable tracing to an output device
570 * @output: output device's descriptor
572 * This will configure all masters set to output to this device and
573 * enable tracing using force storeEn signal.
575 static void intel_th_gth_enable(struct intel_th_device
*thdev
,
576 struct intel_th_output
*output
)
578 struct gth_device
*gth
= dev_get_drvdata(&thdev
->dev
);
579 struct intel_th
*th
= to_intel_th(thdev
);
583 spin_lock(>h
->gth_lock
);
584 for_each_set_bit(master
, gth
->output
[output
->port
].master
,
585 TH_CONFIGURABLE_MASTERS
+ 1) {
586 gth_master_set(gth
, master
, output
->port
);
589 output
->active
= true;
590 spin_unlock(>h
->gth_lock
);
592 if (INTEL_TH_CAP(th
, tscu_enable
))
593 gth_tscu_resync(gth
);
595 scrpd
= ioread32(gth
->base
+ REG_GTH_SCRPD0
);
596 scrpd
|= output
->scratchpad
;
597 iowrite32(scrpd
, gth
->base
+ REG_GTH_SCRPD0
);
599 intel_th_gth_start(gth
, output
);
603 * intel_th_gth_switch() - execute a switch sequence
605 * @output: output device's descriptor
607 * This will execute a switch sequence that will trigger a switch window
608 * when tracing to MSC in multi-block mode.
610 static void intel_th_gth_switch(struct intel_th_device
*thdev
,
611 struct intel_th_output
*output
)
613 struct gth_device
*gth
= dev_get_drvdata(&thdev
->dev
);
618 iowrite32(0, gth
->base
+ REG_CTS_CTL
);
619 iowrite32(CTS_CTL_SEQUENCER_ENABLE
, gth
->base
+ REG_CTS_CTL
);
620 /* wait on trigger status */
621 for (reg
= 0, count
= CTS_TRIG_WAITLOOP_DEPTH
;
622 count
&& !(reg
& BIT(4)); count
--) {
623 reg
= ioread32(gth
->base
+ REG_CTS_STAT
);
627 dev_dbg(&thdev
->dev
, "timeout waiting for CTS Trigger\n");
629 /* De-assert the trigger */
630 iowrite32(0, gth
->base
+ REG_CTS_CTL
);
632 intel_th_gth_stop(gth
, output
, false);
633 intel_th_gth_start(gth
, output
);
637 * intel_th_gth_assign() - assign output device to a GTH output port
639 * @othdev: output device
641 * This will match a given output device parameters against present
642 * output ports on the GTH and fill out relevant bits in output device's
645 * Return: 0 on success, -errno on error.
647 static int intel_th_gth_assign(struct intel_th_device
*thdev
,
648 struct intel_th_device
*othdev
)
650 struct gth_device
*gth
= dev_get_drvdata(&thdev
->dev
);
653 if (thdev
->host_mode
)
656 if (othdev
->type
!= INTEL_TH_OUTPUT
)
659 for (i
= 0, id
= 0; i
< TH_POSSIBLE_OUTPUTS
; i
++) {
660 if (gth
->output
[i
].port_type
!= othdev
->output
.type
)
663 if (othdev
->id
== -1 || othdev
->id
== id
)
672 spin_lock(>h
->gth_lock
);
673 othdev
->output
.port
= i
;
674 othdev
->output
.active
= false;
675 gth
->output
[i
].output
= &othdev
->output
;
676 spin_unlock(>h
->gth_lock
);
682 * intel_th_gth_unassign() - deassociate an output device from its output port
684 * @othdev: output device
686 static void intel_th_gth_unassign(struct intel_th_device
*thdev
,
687 struct intel_th_device
*othdev
)
689 struct gth_device
*gth
= dev_get_drvdata(&thdev
->dev
);
690 int port
= othdev
->output
.port
;
693 if (thdev
->host_mode
)
696 spin_lock(>h
->gth_lock
);
697 othdev
->output
.port
= -1;
698 othdev
->output
.active
= false;
699 gth
->output
[port
].output
= NULL
;
700 for (master
= 0; master
<= TH_CONFIGURABLE_MASTERS
; master
++)
701 if (gth
->master
[master
] == port
)
702 gth
->master
[master
] = -1;
703 spin_unlock(>h
->gth_lock
);
707 intel_th_gth_set_output(struct intel_th_device
*thdev
, unsigned int master
)
709 struct gth_device
*gth
= dev_get_drvdata(&thdev
->dev
);
710 int port
= 0; /* FIXME: make default output configurable */
713 * everything above TH_CONFIGURABLE_MASTERS is controlled by the
716 if (master
> TH_CONFIGURABLE_MASTERS
)
717 master
= TH_CONFIGURABLE_MASTERS
;
719 spin_lock(>h
->gth_lock
);
720 if (gth
->master
[master
] == -1) {
721 set_bit(master
, gth
->output
[port
].master
);
722 gth
->master
[master
] = port
;
724 spin_unlock(>h
->gth_lock
);
729 static int intel_th_gth_probe(struct intel_th_device
*thdev
)
731 struct device
*dev
= &thdev
->dev
;
732 struct intel_th
*th
= dev_get_drvdata(dev
->parent
);
733 struct gth_device
*gth
;
734 struct resource
*res
;
738 res
= intel_th_device_get_resource(thdev
, IORESOURCE_MEM
, 0);
742 base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
746 gth
= devm_kzalloc(dev
, sizeof(*gth
), GFP_KERNEL
);
752 spin_lock_init(>h
->gth_lock
);
754 dev_set_drvdata(dev
, gth
);
757 * Host mode can be signalled via SW means or via SCRPD_DEBUGGER_IN_USE
758 * bit. Either way, don't reset HW in this case, and don't export any
759 * capture configuration attributes. Also, refuse to assign output
760 * drivers to ports, see intel_th_gth_assign().
762 if (thdev
->host_mode
)
765 ret
= intel_th_gth_reset(gth
);
770 thdev
->host_mode
= true;
775 for (i
= 0; i
< TH_CONFIGURABLE_MASTERS
+ 1; i
++)
778 for (i
= 0; i
< TH_POSSIBLE_OUTPUTS
; i
++) {
779 gth
->output
[i
].gth
= gth
;
780 gth
->output
[i
].index
= i
;
781 gth
->output
[i
].port_type
=
782 gth_output_parm_get(gth
, i
, TH_OUTPUT_PARM(port
));
783 if (gth
->output
[i
].port_type
== GTH_NONE
)
786 ret
= intel_th_output_enable(th
, gth
->output
[i
].port_type
);
787 /* -ENODEV is ok, we just won't have that device enumerated */
788 if (ret
&& ret
!= -ENODEV
)
792 if (intel_th_output_attributes(gth
) ||
793 intel_th_master_attributes(gth
)) {
794 pr_warn("Can't initialize sysfs attributes\n");
796 if (gth
->output_group
.attrs
)
797 sysfs_remove_group(>h
->dev
->kobj
, >h
->output_group
);
804 static void intel_th_gth_remove(struct intel_th_device
*thdev
)
806 struct gth_device
*gth
= dev_get_drvdata(&thdev
->dev
);
808 sysfs_remove_group(>h
->dev
->kobj
, >h
->output_group
);
809 sysfs_remove_group(>h
->dev
->kobj
, >h
->master_group
);
812 static struct intel_th_driver intel_th_gth_driver
= {
813 .probe
= intel_th_gth_probe
,
814 .remove
= intel_th_gth_remove
,
815 .assign
= intel_th_gth_assign
,
816 .unassign
= intel_th_gth_unassign
,
817 .set_output
= intel_th_gth_set_output
,
818 .enable
= intel_th_gth_enable
,
819 .trig_switch
= intel_th_gth_switch
,
820 .disable
= intel_th_gth_disable
,
823 .owner
= THIS_MODULE
,
827 module_driver(intel_th_gth_driver
,
828 intel_th_driver_register
,
829 intel_th_driver_unregister
);
831 MODULE_ALIAS("intel_th_switch");
832 MODULE_LICENSE("GPL v2");
833 MODULE_DESCRIPTION("Intel(R) Trace Hub Global Trace Hub driver");
834 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");