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