• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/kconfig.h>
33 
34 /* Reservation marker for mksstat pid's */
35 #define MKSSTAT_PID_RESERVED -1
36 
37 #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
38 /*
39  * Kernel-internal mksGuestStat counters. The order of this enum dictates the
40  * order of instantiation of these counters in the mksGuestStat pages.
41  */
42 
43 typedef enum {
44 	MKSSTAT_KERN_EXECBUF, /* vmw_execbuf_ioctl */
45 	MKSSTAT_KERN_COTABLE_RESIZE,
46 
47 	MKSSTAT_KERN_COUNT /* Reserved entry; always last */
48 } mksstat_kern_stats_t;
49 
50 /**
51  * vmw_mksstat_get_kern_pstat: Computes the address of the MKSGuestStatCounterTime
52  * array from the address of the base page.
53  *
54  * @page_addr: Pointer to the base page.
55  * Return: Pointer to the MKSGuestStatCounterTime array.
56  */
57 
vmw_mksstat_get_kern_pstat(void * page_addr)58 static inline void *vmw_mksstat_get_kern_pstat(void *page_addr)
59 {
60 	return page_addr + PAGE_SIZE * 1;
61 }
62 
63 /**
64  * vmw_mksstat_get_kern_pinfo: Computes the address of the MKSGuestStatInfoEntry
65  * array from the address of the base page.
66  *
67  * @page_addr: Pointer to the base page.
68  * Return: Pointer to the MKSGuestStatInfoEntry array.
69  */
70 
vmw_mksstat_get_kern_pinfo(void * page_addr)71 static inline void *vmw_mksstat_get_kern_pinfo(void *page_addr)
72 {
73 	return page_addr + PAGE_SIZE * 2;
74 }
75 
76 /**
77  * vmw_mksstat_get_kern_pstrs: Computes the address of the mksGuestStat strings
78  * sequence from the address of the base page.
79  *
80  * @page_addr: Pointer to the base page.
81  * Return: Pointer to the mksGuestStat strings sequence.
82  */
83 
vmw_mksstat_get_kern_pstrs(void * page_addr)84 static inline void *vmw_mksstat_get_kern_pstrs(void *page_addr)
85 {
86 	return page_addr + PAGE_SIZE * 3;
87 }
88 
89 /*
90  * MKS_STAT_TIME_DECL/PUSH/POP macros to be used in timer-counted routines.
91  */
92 
93 struct mksstat_timer_t {
94 /* mutable */ mksstat_kern_stats_t old_top;
95 	const u64 t0;
96 	const int slot;
97 };
98 
99 #define MKS_STAT_TIME_DECL(kern_cntr)                                     \
100 	struct mksstat_timer_t _##kern_cntr = {                           \
101 		.t0 = rdtsc(),                                            \
102 		.slot = vmw_mksstat_get_kern_slot(current->pid, dev_priv) \
103 	}
104 
105 #define MKS_STAT_TIME_PUSH(kern_cntr)                                                               \
106 	do {                                                                                        \
107 		if (_##kern_cntr.slot >= 0) {                                                       \
108 			_##kern_cntr.old_top = dev_priv->mksstat_kern_top_timer[_##kern_cntr.slot]; \
109 			dev_priv->mksstat_kern_top_timer[_##kern_cntr.slot] = kern_cntr;            \
110 		}                                                                                   \
111 	} while (0)
112 
113 #define MKS_STAT_TIME_POP(kern_cntr)                                                                                                           \
114 	do {                                                                                                                                   \
115 		if (_##kern_cntr.slot >= 0) {                                                                                                  \
116 			const pid_t pid = atomic_cmpxchg(&dev_priv->mksstat_kern_pids[_##kern_cntr.slot], current->pid, MKSSTAT_PID_RESERVED); \
117 			dev_priv->mksstat_kern_top_timer[_##kern_cntr.slot] = _##kern_cntr.old_top;                                            \
118 			                                                                                                                       \
119 			if (pid == current->pid) {                                                                                             \
120 				const u64 dt = rdtsc() - _##kern_cntr.t0;                                                                      \
121 				MKSGuestStatCounterTime *pstat;                                                                                \
122 				                                                                                                               \
123 				BUG_ON(!dev_priv->mksstat_kern_pages[_##kern_cntr.slot]);                                                      \
124 				                                                                                                               \
125 				pstat = vmw_mksstat_get_kern_pstat(page_address(dev_priv->mksstat_kern_pages[_##kern_cntr.slot]));             \
126 				                                                                                                               \
127 				atomic64_inc(&pstat[kern_cntr].counter.count);                                                                 \
128 				atomic64_add(dt, &pstat[kern_cntr].selfCycles);                                                                \
129 				atomic64_add(dt, &pstat[kern_cntr].totalCycles);                                                               \
130 				                                                                                                               \
131 				if (_##kern_cntr.old_top != MKSSTAT_KERN_COUNT)                                                                \
132 					atomic64_sub(dt, &pstat[_##kern_cntr.old_top].selfCycles);                                             \
133 					                                                                                                       \
134 				atomic_set(&dev_priv->mksstat_kern_pids[_##kern_cntr.slot], current->pid);                                     \
135 			}                                                                                                                      \
136 		}                                                                                                                              \
137 	} while (0)
138 
139 #else
140 #define MKS_STAT_TIME_DECL(kern_cntr)
141 #define MKS_STAT_TIME_PUSH(kern_cntr)
142 #define MKS_STAT_TIME_POP(kern_cntr)
143 
144 #endif /* IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS */
145 
146 #endif
147