1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Intel(R) Trace Hub data structures for implementing buffer sinks.
4  *
5  * Copyright (C) 2019 Intel Corporation.
6  */
7 
8 #ifndef _INTEL_TH_H_
9 #define _INTEL_TH_H_
10 
11 #include <linux/scatterlist.h>
12 
13 /* MSC operating modes (MSC_MODE) */
14 enum {
15 	MSC_MODE_SINGLE	= 0,
16 	MSC_MODE_MULTI,
17 	MSC_MODE_EXI,
18 	MSC_MODE_DEBUG,
19 };
20 
21 struct msu_buffer {
22 	const char	*name;
23 	/*
24 	 * ->assign() called when buffer 'mode' is set to this driver
25 	 *   (aka mode_store())
26 	 * @device:	struct device * of the msc
27 	 * @mode:	allows the driver to set HW mode (see the enum above)
28 	 * Returns:	a pointer to a private structure associated with this
29 	 *		msc or NULL in case of error. This private structure
30 	 *		will then be passed into all other callbacks.
31 	 */
32 	void	*(*assign)(struct device *dev, int *mode);
33 	/* ->unassign():	some other mode is selected, clean up */
34 	void	(*unassign)(void *priv);
35 	/*
36 	 * ->alloc_window(): allocate memory for the window of a given
37 	 *		size
38 	 * @sgt:	pointer to sg_table, can be overridden by the buffer
39 	 *		driver, or kept intact
40 	 * Returns:	number of sg table entries <= number of pages;
41 	 *		0 is treated as an allocation failure.
42 	 */
43 	int	(*alloc_window)(void *priv, struct sg_table **sgt,
44 				size_t size);
45 	void	(*free_window)(void *priv, struct sg_table *sgt);
46 	/* ->activate():	trace has started */
47 	void	(*activate)(void *priv);
48 	/* ->deactivate():	trace is about to stop */
49 	void	(*deactivate)(void *priv);
50 	/*
51 	 * ->ready():	window @sgt is filled up to the last block OR
52 	 *		tracing is stopped by the user; this window contains
53 	 *		@bytes data. The window in question transitions into
54 	 *		the "LOCKED" state, indicating that it can't be used
55 	 *		by hardware. To clear this state and make the window
56 	 *		available to the hardware again, call
57 	 *		intel_th_msc_window_unlock().
58 	 */
59 	int	(*ready)(void *priv, struct sg_table *sgt, size_t bytes);
60 };
61 
62 int intel_th_msu_buffer_register(const struct msu_buffer *mbuf,
63 				 struct module *owner);
64 void intel_th_msu_buffer_unregister(const struct msu_buffer *mbuf);
65 void intel_th_msc_window_unlock(struct device *dev, struct sg_table *sgt);
66 
67 #define module_intel_th_msu_buffer(__buffer) \
68 static int __init __buffer##_init(void) \
69 { \
70 	return intel_th_msu_buffer_register(&(__buffer), THIS_MODULE); \
71 } \
72 module_init(__buffer##_init); \
73 static void __exit __buffer##_exit(void) \
74 { \
75 	intel_th_msu_buffer_unregister(&(__buffer)); \
76 } \
77 module_exit(__buffer##_exit);
78 
79 #endif /* _INTEL_TH_H_ */
80