1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright 2021 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #ifndef _VMWGFX_MKSSTAT_H_
29 #define _VMWGFX_MKSSTAT_H_
32 #include <linux/kconfig.h>
34 /* Reservation marker for mksstat pid's */
35 #define MKSSTAT_PID_RESERVED -1
37 #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
39 * Kernel-internal mksGuestStat counters. The order of this enum dictates the
40 * order of instantiation of these counters in the mksGuestStat pages.
44 MKSSTAT_KERN_EXECBUF, /* vmw_execbuf_ioctl */
45 MKSSTAT_KERN_COTABLE_RESIZE,
47 MKSSTAT_KERN_COUNT /* Reserved entry; always last */
48 } mksstat_kern_stats_t;
51 * vmw_mksstat_get_kern_pstat: Computes the address of the MKSGuestStatCounterTime
52 * array from the address of the base page.
54 * @page_addr: Pointer to the base page.
55 * Return: Pointer to the MKSGuestStatCounterTime array.
58 static inline void *vmw_mksstat_get_kern_pstat(void *page_addr)
60 return page_addr + PAGE_SIZE * 1;
64 * vmw_mksstat_get_kern_pinfo: Computes the address of the MKSGuestStatInfoEntry
65 * array from the address of the base page.
67 * @page_addr: Pointer to the base page.
68 * Return: Pointer to the MKSGuestStatInfoEntry array.
71 static inline void *vmw_mksstat_get_kern_pinfo(void *page_addr)
73 return page_addr + PAGE_SIZE * 2;
77 * vmw_mksstat_get_kern_pstrs: Computes the address of the mksGuestStat strings
78 * sequence from the address of the base page.
80 * @page_addr: Pointer to the base page.
81 * Return: Pointer to the mksGuestStat strings sequence.
84 static inline void *vmw_mksstat_get_kern_pstrs(void *page_addr)
86 return page_addr + PAGE_SIZE * 3;
90 * MKS_STAT_TIME_DECL/PUSH/POP macros to be used in timer-counted routines.
93 struct mksstat_timer_t {
94 /* mutable */ mksstat_kern_stats_t old_top;
99 #define MKS_STAT_TIME_DECL(kern_cntr) \
100 struct mksstat_timer_t _##kern_cntr = { \
102 .slot = vmw_mksstat_get_kern_slot(current->pid, dev_priv) \
105 #define MKS_STAT_TIME_PUSH(kern_cntr) \
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; \
113 #define MKS_STAT_TIME_POP(kern_cntr) \
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; \
119 if (pid == current->pid) { \
120 const u64 dt = rdtsc() - _##kern_cntr.t0; \
121 MKSGuestStatCounterTime *pstat; \
123 BUG_ON(!dev_priv->mksstat_kern_pages[_##kern_cntr.slot]); \
125 pstat = vmw_mksstat_get_kern_pstat(page_address(dev_priv->mksstat_kern_pages[_##kern_cntr.slot])); \
127 atomic64_inc(&pstat[kern_cntr].counter.count); \
128 atomic64_add(dt, &pstat[kern_cntr].selfCycles); \
129 atomic64_add(dt, &pstat[kern_cntr].totalCycles); \
131 if (_##kern_cntr.old_top != MKSSTAT_KERN_COUNT) \
132 atomic64_sub(dt, &pstat[_##kern_cntr.old_top].selfCycles); \
134 atomic_set(&dev_priv->mksstat_kern_pids[_##kern_cntr.slot], current->pid); \
140 #define MKS_STAT_TIME_DECL(kern_cntr)
141 #define MKS_STAT_TIME_PUSH(kern_cntr)
142 #define MKS_STAT_TIME_POP(kern_cntr)
144 #endif /* IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS */