resctrl.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_RESCTRL_H
  3. #define _ASM_X86_RESCTRL_H
  4. #ifdef CONFIG_X86_CPU_RESCTRL
  5. #include <linux/sched.h>
  6. #include <linux/jump_label.h>
  7. #define IA32_PQR_ASSOC 0x0c8f
  8. /**
  9. * struct resctrl_pqr_state - State cache for the PQR MSR
  10. * @cur_rmid: The cached Resource Monitoring ID
  11. * @cur_closid: The cached Class Of Service ID
  12. * @default_rmid: The user assigned Resource Monitoring ID
  13. * @default_closid: The user assigned cached Class Of Service ID
  14. *
  15. * The upper 32 bits of IA32_PQR_ASSOC contain closid and the
  16. * lower 10 bits rmid. The update to IA32_PQR_ASSOC always
  17. * contains both parts, so we need to cache them. This also
  18. * stores the user configured per cpu CLOSID and RMID.
  19. *
  20. * The cache also helps to avoid pointless updates if the value does
  21. * not change.
  22. */
  23. struct resctrl_pqr_state {
  24. u32 cur_rmid;
  25. u32 cur_closid;
  26. u32 default_rmid;
  27. u32 default_closid;
  28. };
  29. DECLARE_PER_CPU(struct resctrl_pqr_state, pqr_state);
  30. DECLARE_STATIC_KEY_FALSE(rdt_enable_key);
  31. DECLARE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
  32. DECLARE_STATIC_KEY_FALSE(rdt_mon_enable_key);
  33. /*
  34. * __resctrl_sched_in() - Writes the task's CLOSid/RMID to IA32_PQR_MSR
  35. *
  36. * Following considerations are made so that this has minimal impact
  37. * on scheduler hot path:
  38. * - This will stay as no-op unless we are running on an Intel SKU
  39. * which supports resource control or monitoring and we enable by
  40. * mounting the resctrl file system.
  41. * - Caches the per cpu CLOSid/RMID values and does the MSR write only
  42. * when a task with a different CLOSid/RMID is scheduled in.
  43. * - We allocate RMIDs/CLOSids globally in order to keep this as
  44. * simple as possible.
  45. * Must be called with preemption disabled.
  46. */
  47. static inline void __resctrl_sched_in(struct task_struct *tsk)
  48. {
  49. struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
  50. u32 closid = state->default_closid;
  51. u32 rmid = state->default_rmid;
  52. u32 tmp;
  53. /*
  54. * If this task has a closid/rmid assigned, use it.
  55. * Else use the closid/rmid assigned to this cpu.
  56. */
  57. if (static_branch_likely(&rdt_alloc_enable_key)) {
  58. tmp = READ_ONCE(tsk->closid);
  59. if (tmp)
  60. closid = tmp;
  61. }
  62. if (static_branch_likely(&rdt_mon_enable_key)) {
  63. tmp = READ_ONCE(tsk->rmid);
  64. if (tmp)
  65. rmid = tmp;
  66. }
  67. if (closid != state->cur_closid || rmid != state->cur_rmid) {
  68. state->cur_closid = closid;
  69. state->cur_rmid = rmid;
  70. wrmsr(IA32_PQR_ASSOC, rmid, closid);
  71. }
  72. }
  73. static inline unsigned int resctrl_arch_round_mon_val(unsigned int val)
  74. {
  75. unsigned int scale = boot_cpu_data.x86_cache_occ_scale;
  76. /* h/w works in units of "boot_cpu_data.x86_cache_occ_scale" */
  77. val /= scale;
  78. return val * scale;
  79. }
  80. static inline void resctrl_sched_in(struct task_struct *tsk)
  81. {
  82. if (static_branch_likely(&rdt_enable_key))
  83. __resctrl_sched_in(tsk);
  84. }
  85. void resctrl_cpu_detect(struct cpuinfo_x86 *c);
  86. #else
  87. static inline void resctrl_sched_in(struct task_struct *tsk) {}
  88. static inline void resctrl_cpu_detect(struct cpuinfo_x86 *c) {}
  89. #endif /* CONFIG_X86_CPU_RESCTRL */
  90. #endif /* _ASM_X86_RESCTRL_H */