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