1.. SPDX-License-Identifier: GPL-2.0
2
3===================
4System Trace Module
5===================
6
7System Trace Module (STM) is a device described in MIPI STP specs as
8STP trace stream generator. STP (System Trace Protocol) is a trace
9protocol multiplexing data from multiple trace sources, each one of
10which is assigned a unique pair of master and channel. While some of
11these masters and channels are statically allocated to certain
12hardware trace sources, others are available to software. Software
13trace sources are usually free to pick for themselves any
14master/channel combination from this pool.
15
16On the receiving end of this STP stream (the decoder side), trace
17sources can only be identified by master/channel combination, so in
18order for the decoder to be able to make sense of the trace that
19involves multiple trace sources, it needs to be able to map those
20master/channel pairs to the trace sources that it understands.
21
22For instance, it is helpful to know that syslog messages come on
23master 7 channel 15, while arbitrary user applications can use masters
2448 to 63 and channels 0 to 127.
25
26To solve this mapping problem, stm class provides a policy management
27mechanism via configfs, that allows defining rules that map string
28identifiers to ranges of masters and channels. If these rules (policy)
29are consistent with what decoder expects, it will be able to properly
30process the trace data.
31
32This policy is a tree structure containing rules (policy_node) that
33have a name (string identifier) and a range of masters and channels
34associated with it, located in "stp-policy" subsystem directory in
35configfs. The topmost directory's name (the policy) is formatted as
36the STM device name to which this policy applies and an arbitrary
37string identifier separated by a stop. From the example above, a rule
38may look like this::
39
40	$ ls /config/stp-policy/dummy_stm.my-policy/user
41	channels masters
42	$ cat /config/stp-policy/dummy_stm.my-policy/user/masters
43	48 63
44	$ cat /config/stp-policy/dummy_stm.my-policy/user/channels
45	0 127
46
47which means that the master allocation pool for this rule consists of
48masters 48 through 63 and channel allocation pool has channels 0
49through 127 in it. Now, any producer (trace source) identifying itself
50with "user" identification string will be allocated a master and
51channel from within these ranges.
52
53These rules can be nested, for example, one can define a rule "dummy"
54under "user" directory from the example above and this new rule will
55be used for trace sources with the id string of "user/dummy".
56
57Trace sources have to open the stm class device's node and write their
58trace data into its file descriptor.
59
60In order to find an appropriate policy node for a given trace source,
61several mechanisms can be used. First, a trace source can explicitly
62identify itself by calling an STP_POLICY_ID_SET ioctl on the character
63device's file descriptor, providing their id string, before they write
64any data there. Secondly, if they chose not to perform the explicit
65identification (because you may not want to patch existing software
66to do this), they can just start writing the data, at which point the
67stm core will try to find a policy node with the name matching the
68task's name (e.g., "syslogd") and if one exists, it will be used.
69Thirdly, if the task name can't be found among the policy nodes, the
70catch-all entry "default" will be used, if it exists. This entry also
71needs to be created and configured by the system administrator or
72whatever tools are taking care of the policy configuration. Finally,
73if all the above steps failed, the write() to an stm file descriptor
74will return a error (EINVAL).
75
76Previously, if no policy nodes were found for a trace source, the stm
77class would silently fall back to allocating the first available
78contiguous range of master/channels from the beginning of the device's
79master/channel range. The new requirement for a policy node to exist
80will help programmers and sysadmins identify gaps in configuration
81and have better control over the un-identified sources.
82
83Some STM devices may allow direct mapping of the channel mmio regions
84to userspace for zero-copy writing. One mappable page (in terms of
85mmu) will usually contain multiple channels' mmios, so the user will
86need to allocate that many channels to themselves (via the
87aforementioned ioctl() call) to be able to do this. That is, if your
88stm device's channel mmio region is 64 bytes and hardware page size is
894096 bytes, after a successful STP_POLICY_ID_SET ioctl() call with
90width==64, you should be able to mmap() one page on this file
91descriptor and obtain direct access to an mmio region for 64 channels.
92
93Examples of STM devices are Intel(R) Trace Hub [1] and Coresight STM
94[2].
95
96stm_source
97==========
98
99For kernel-based trace sources, there is "stm_source" device
100class. Devices of this class can be connected and disconnected to/from
101stm devices at runtime via a sysfs attribute called "stm_source_link"
102by writing the name of the desired stm device there, for example::
103
104	$ echo dummy_stm.0 > /sys/class/stm_source/console/stm_source_link
105
106For examples on how to use stm_source interface in the kernel, refer
107to stm_console, stm_heartbeat or stm_ftrace drivers.
108
109Each stm_source device will need to assume a master and a range of
110channels, depending on how many channels it requires. These are
111allocated for the device according to the policy configuration. If
112there's a node in the root of the policy directory that matches the
113stm_source device's name (for example, "console"), this node will be
114used to allocate master and channel numbers. If there's no such policy
115node, the stm core will use the catch-all entry "default", if one
116exists. If neither policy nodes exist, the write() to stm_source_link
117will return an error.
118
119stm_console
120===========
121
122One implementation of this interface also used in the example above is
123the "stm_console" driver, which basically provides a one-way console
124for kernel messages over an stm device.
125
126To configure the master/channel pair that will be assigned to this
127console in the STP stream, create a "console" policy entry (see the
128beginning of this text on how to do that). When initialized, it will
129consume one channel.
130
131stm_ftrace
132==========
133
134This is another "stm_source" device, once the stm_ftrace has been
135linked with an stm device, and if "function" tracer is enabled,
136function address and parent function address which Ftrace subsystem
137would store into ring buffer will be exported via the stm device at
138the same time.
139
140Currently only Ftrace "function" tracer is supported.
141
142* [1] https://software.intel.com/sites/default/files/managed/d3/3c/intel-th-developer-manual.pdf
143* [2] http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0444b/index.html
144