pai.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Processor Activity Instrumentation support for cryptography counters
  4. *
  5. * Copyright IBM Corp. 2022
  6. * Author(s): Thomas Richter <[email protected]>
  7. */
  8. #ifndef _ASM_S390_PAI_H
  9. #define _ASM_S390_PAI_H
  10. #include <linux/jump_label.h>
  11. #include <asm/lowcore.h>
  12. #include <asm/ptrace.h>
  13. struct qpaci_info_block {
  14. u64 header;
  15. struct {
  16. u64 : 8;
  17. u64 num_cc : 8; /* # of supported crypto counters */
  18. u64 : 9;
  19. u64 num_nnpa : 7; /* # of supported NNPA counters */
  20. u64 : 32;
  21. };
  22. };
  23. static inline int qpaci(struct qpaci_info_block *info)
  24. {
  25. /* Size of info (in double words minus one) */
  26. size_t size = sizeof(*info) / sizeof(u64) - 1;
  27. int cc;
  28. asm volatile(
  29. " lgr 0,%[size]\n"
  30. " .insn s,0xb28f0000,%[info]\n"
  31. " lgr %[size],0\n"
  32. " ipm %[cc]\n"
  33. " srl %[cc],28\n"
  34. : [cc] "=d" (cc), [info] "=Q" (*info), [size] "+&d" (size)
  35. :
  36. : "0", "cc", "memory");
  37. return cc ? (size + 1) * sizeof(u64) : 0;
  38. }
  39. #define PAI_CRYPTO_BASE 0x1000 /* First event number */
  40. #define PAI_CRYPTO_MAXCTR 256 /* Max # of event counters */
  41. #define PAI_CRYPTO_KERNEL_OFFSET 2048
  42. #define PAI_NNPA_BASE 0x1800 /* First event number */
  43. #define PAI_NNPA_MAXCTR 128 /* Max # of event counters */
  44. DECLARE_STATIC_KEY_FALSE(pai_key);
  45. static __always_inline void pai_kernel_enter(struct pt_regs *regs)
  46. {
  47. if (!IS_ENABLED(CONFIG_PERF_EVENTS))
  48. return;
  49. if (!static_branch_unlikely(&pai_key))
  50. return;
  51. if (!S390_lowcore.ccd)
  52. return;
  53. if (!user_mode(regs))
  54. return;
  55. WRITE_ONCE(S390_lowcore.ccd, S390_lowcore.ccd | PAI_CRYPTO_KERNEL_OFFSET);
  56. }
  57. static __always_inline void pai_kernel_exit(struct pt_regs *regs)
  58. {
  59. if (!IS_ENABLED(CONFIG_PERF_EVENTS))
  60. return;
  61. if (!static_branch_unlikely(&pai_key))
  62. return;
  63. if (!S390_lowcore.ccd)
  64. return;
  65. if (!user_mode(regs))
  66. return;
  67. WRITE_ONCE(S390_lowcore.ccd, S390_lowcore.ccd & ~PAI_CRYPTO_KERNEL_OFFSET);
  68. }
  69. #endif