1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Trace Module (STM) infrastructure apis
4  * Copyright (C) 2014 Intel Corporation.
5  */
6 
7 #ifndef _STM_H_
8 #define _STM_H_
9 
10 #include <linux/device.h>
11 
12 /**
13  * enum stp_packet_type - STP packets that an STM driver sends
14  */
15 enum stp_packet_type {
16 	STP_PACKET_DATA = 0,
17 	STP_PACKET_FLAG,
18 	STP_PACKET_USER,
19 	STP_PACKET_MERR,
20 	STP_PACKET_GERR,
21 	STP_PACKET_TRIG,
22 	STP_PACKET_XSYNC,
23 };
24 
25 /**
26  * enum stp_packet_flags - STP packet modifiers
27  */
28 enum stp_packet_flags {
29 	STP_PACKET_MARKED	= 0x1,
30 	STP_PACKET_TIMESTAMPED	= 0x2,
31 };
32 
33 /**
34  * enum stm_source_type - STM source driver
35  * @STM_USER: any STM trace source
36  * @STM_FTRACE: ftrace STM source
37  */
38 enum stm_source_type {
39 	STM_USER,
40 	STM_FTRACE,
41 };
42 
43 struct stp_policy;
44 
45 struct stm_device;
46 
47 /**
48  * struct stm_data - STM device description and callbacks
49  * @name:		device name
50  * @stm:		internal structure, only used by stm class code
51  * @sw_start:		first STP master available to software
52  * @sw_end:		last STP master available to software
53  * @sw_nchannels:	number of STP channels per master
54  * @sw_mmiosz:		size of one channel's IO space, for mmap, optional
55  * @hw_override:	masters in the STP stream will not match the ones
56  *			assigned by software, but are up to the STM hardware
57  * @packet:		callback that sends an STP packet
58  * @mmio_addr:		mmap callback, optional
59  * @link:		called when a new stm_source gets linked to us, optional
60  * @unlink:		likewise for unlinking, again optional
61  * @set_options:	set device-specific options on a channel
62  *
63  * Fill out this structure before calling stm_register_device() to create
64  * an STM device and stm_unregister_device() to destroy it. It will also be
65  * passed back to @packet(), @mmio_addr(), @link(), @unlink() and @set_options()
66  * callbacks.
67  *
68  * Normally, an STM device will have a range of masters available to software
69  * and the rest being statically assigned to various hardware trace sources.
70  * The former is defined by the range [@sw_start..@sw_end] of the device
71  * description. That is, the lowest master that can be allocated to software
72  * writers is @sw_start and data from this writer will appear is @sw_start
73  * master in the STP stream.
74  *
75  * The @packet callback should adhere to the following rules:
76  *   1) it must return the number of bytes it consumed from the payload;
77  *   2) therefore, if it sent a packet that does not have payload (like FLAG),
78  *      it must return zero;
79  *   3) if it does not support the requested packet type/flag combination,
80  *      it must return -ENOTSUPP.
81  *
82  * The @unlink callback is called when there are no more active writers so
83  * that the master/channel can be quiesced.
84  */
85 struct stm_data {
86 	const char		*name;
87 	struct stm_device	*stm;
88 	unsigned int		sw_start;
89 	unsigned int		sw_end;
90 	unsigned int		sw_nchannels;
91 	unsigned int		sw_mmiosz;
92 	unsigned int		hw_override;
93 	ssize_t			(*packet)(struct stm_data *, unsigned int,
94 					  unsigned int, unsigned int,
95 					  unsigned int, unsigned int,
96 					  const unsigned char *);
97 	phys_addr_t		(*mmio_addr)(struct stm_data *, unsigned int,
98 					     unsigned int, unsigned int);
99 	int			(*link)(struct stm_data *, unsigned int,
100 					unsigned int);
101 	void			(*unlink)(struct stm_data *, unsigned int,
102 					  unsigned int);
103 	long			(*set_options)(struct stm_data *, unsigned int,
104 					       unsigned int, unsigned int,
105 					       unsigned long);
106 };
107 
108 int stm_register_device(struct device *parent, struct stm_data *stm_data,
109 			struct module *owner);
110 void stm_unregister_device(struct stm_data *stm_data);
111 
112 struct stm_source_device;
113 
114 /**
115  * struct stm_source_data - STM source device description and callbacks
116  * @name:	device name, will be used for policy lookup
117  * @src:	internal structure, only used by stm class code
118  * @nr_chans:	number of channels to allocate
119  * @type:	type of STM source driver represented by stm_source_type
120  * @link:	called when this source gets linked to an STM device
121  * @unlink:	called when this source is about to get unlinked from its STM
122  *
123  * Fill in this structure before calling stm_source_register_device() to
124  * register a source device. Also pass it to unregister and write calls.
125  */
126 struct stm_source_data {
127 	const char		*name;
128 	struct stm_source_device *src;
129 	unsigned int		percpu;
130 	unsigned int		nr_chans;
131 	unsigned int		type;
132 	int			(*link)(struct stm_source_data *data);
133 	void			(*unlink)(struct stm_source_data *data);
134 };
135 
136 int stm_source_register_device(struct device *parent,
137 			       struct stm_source_data *data);
138 void stm_source_unregister_device(struct stm_source_data *data);
139 
140 int notrace stm_source_write(struct stm_source_data *data, unsigned int chan,
141 			     const char *buf, size_t count);
142 
143 #endif /* _STM_H_ */
144