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