vmwgfx_mksstat.h 6.9 KB

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