1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright 2021 VMware, Inc., Palo Alto, CA., USA
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #ifndef _VMWGFX_MKSSTAT_H_
29 #define _VMWGFX_MKSSTAT_H_
30 
31 #include <asm/page.h>
32 
33 /* Reservation marker for mksstat pid's */
34 #define MKSSTAT_PID_RESERVED -1
35 
36 #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
37 /*
38  * Kernel-internal mksGuestStat counters. The order of this enum dictates the
39  * order of instantiation of these counters in the mksGuestStat pages.
40  */
41 
42 typedef enum {
43 	MKSSTAT_KERN_EXECBUF, /* vmw_execbuf_ioctl */
44 
45 	MKSSTAT_KERN_COUNT /* Reserved entry; always last */
46 } mksstat_kern_stats_t;
47 
48 /**
49  * vmw_mksstat_get_kern_pstat: Computes the address of the MKSGuestStatCounterTime
50  * array from the address of the base page.
51  *
52  * @page_addr: Pointer to the base page.
53  * Return: Pointer to the MKSGuestStatCounterTime array.
54  */
55 
56 static inline void *vmw_mksstat_get_kern_pstat(void *page_addr)
57 {
58 	return page_addr + PAGE_SIZE * 1;
59 }
60 
61 /**
62  * vmw_mksstat_get_kern_pinfo: Computes the address of the MKSGuestStatInfoEntry
63  * array from the address of the base page.
64  *
65  * @page_addr: Pointer to the base page.
66  * Return: Pointer to the MKSGuestStatInfoEntry array.
67  */
68 
69 static inline void *vmw_mksstat_get_kern_pinfo(void *page_addr)
70 {
71 	return page_addr + PAGE_SIZE * 2;
72 }
73 
74 /**
75  * vmw_mksstat_get_kern_pstrs: Computes the address of the mksGuestStat strings
76  * sequence from the address of the base page.
77  *
78  * @page_addr: Pointer to the base page.
79  * Return: Pointer to the mksGuestStat strings sequence.
80  */
81 
82 static inline void *vmw_mksstat_get_kern_pstrs(void *page_addr)
83 {
84 	return page_addr + PAGE_SIZE * 3;
85 }
86 
87 /*
88  * MKS_STAT_TIME_DECL/PUSH/POP macros to be used in timer-counted routines.
89  */
90 
91 struct mksstat_timer_t {
92 /* mutable */ mksstat_kern_stats_t old_top;
93 	const u64 t0;
94 	const int slot;
95 };
96 
97 #define MKS_STAT_TIME_DECL(kern_cntr)                                     \
98 	struct mksstat_timer_t _##kern_cntr = {                           \
99 		.t0 = rdtsc(),                                            \
100 		.slot = vmw_mksstat_get_kern_slot(current->pid, dev_priv) \
101 	}
102 
103 #define MKS_STAT_TIME_PUSH(kern_cntr)                                                               \
104 	do {                                                                                        \
105 		if (_##kern_cntr.slot >= 0) {                                                       \
106 			_##kern_cntr.old_top = dev_priv->mksstat_kern_top_timer[_##kern_cntr.slot]; \
107 			dev_priv->mksstat_kern_top_timer[_##kern_cntr.slot] = kern_cntr;            \
108 		}                                                                                   \
109 	} while (0)
110 
111 #define MKS_STAT_TIME_POP(kern_cntr)                                                                                                           \
112 	do {                                                                                                                                   \
113 		if (_##kern_cntr.slot >= 0) {                                                                                                  \
114 			const pid_t pid = atomic_cmpxchg(&dev_priv->mksstat_kern_pids[_##kern_cntr.slot], current->pid, MKSSTAT_PID_RESERVED); \
115 			dev_priv->mksstat_kern_top_timer[_##kern_cntr.slot] = _##kern_cntr.old_top;                                            \
116 			                                                                                                                       \
117 			if (pid == current->pid) {                                                                                             \
118 				const u64 dt = rdtsc() - _##kern_cntr.t0;                                                                      \
119 				MKSGuestStatCounterTime *pstat;                                                                                \
120 				                                                                                                               \
121 				BUG_ON(!dev_priv->mksstat_kern_pages[_##kern_cntr.slot]);                                                      \
122 				                                                                                                               \
123 				pstat = vmw_mksstat_get_kern_pstat(page_address(dev_priv->mksstat_kern_pages[_##kern_cntr.slot]));             \
124 				                                                                                                               \
125 				atomic64_inc(&pstat[kern_cntr].counter.count);                                                                 \
126 				atomic64_add(dt, &pstat[kern_cntr].selfCycles);                                                                \
127 				atomic64_add(dt, &pstat[kern_cntr].totalCycles);                                                               \
128 				                                                                                                               \
129 				if (_##kern_cntr.old_top != MKSSTAT_KERN_COUNT)                                                                \
130 					atomic64_sub(dt, &pstat[_##kern_cntr.old_top].selfCycles);                                             \
131 					                                                                                                       \
132 				atomic_set(&dev_priv->mksstat_kern_pids[_##kern_cntr.slot], current->pid);                                     \
133 			}                                                                                                                      \
134 		}                                                                                                                              \
135 	} while (0)
136 
137 #else
138 #define MKS_STAT_TIME_DECL(kern_cntr)
139 #define MKS_STAT_TIME_PUSH(kern_cntr)
140 #define MKS_STAT_TIME_POP(kern_cntr)
141 
142 #endif /* IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS */
143 
144 #endif
145