vmwgfx_mksstat.h 6.9 KB

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