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/jump_label.h> 8 #include <linux/percpu.h> 9 #include <linux/resctrl_types.h> 10 #include <linux/sched.h> 11 12 #include <asm/msr.h> 13 14 /* 15 * This value can never be a valid CLOSID, and is used when mapping a 16 * (closid, rmid) pair to an index and back. On x86 only the RMID is 17 * needed. The index is a software defined value. 18 */ 19 #define X86_RESCTRL_EMPTY_CLOSID ((u32)~0) 20 21 /** 22 * struct resctrl_pqr_state - State cache for the PQR MSR 23 * @cur_rmid: The cached Resource Monitoring ID 24 * @cur_closid: The cached Class Of Service ID 25 * @default_rmid: The user assigned Resource Monitoring ID 26 * @default_closid: The user assigned cached Class Of Service ID 27 * 28 * The upper 32 bits of MSR_IA32_PQR_ASSOC contain closid and the 29 * lower 10 bits rmid. The update to MSR_IA32_PQR_ASSOC always 30 * contains both parts, so we need to cache them. This also 31 * stores the user configured per cpu CLOSID and RMID. 32 * 33 * The cache also helps to avoid pointless updates if the value does 34 * not change. 35 */ 36 struct resctrl_pqr_state { 37 u32 cur_rmid; 38 u32 cur_closid; 39 u32 default_rmid; 40 u32 default_closid; 41 }; 42 43 DECLARE_PER_CPU(struct resctrl_pqr_state, pqr_state); 44 45 extern bool rdt_alloc_capable; 46 extern bool rdt_mon_capable; 47 48 DECLARE_STATIC_KEY_FALSE(rdt_enable_key); 49 DECLARE_STATIC_KEY_FALSE(rdt_alloc_enable_key); 50 DECLARE_STATIC_KEY_FALSE(rdt_mon_enable_key); 51 52 static inline bool resctrl_arch_alloc_capable(void) 53 { 54 return rdt_alloc_capable; 55 } 56 57 static inline void resctrl_arch_enable_alloc(void) 58 { 59 static_branch_enable_cpuslocked(&rdt_alloc_enable_key); 60 static_branch_inc_cpuslocked(&rdt_enable_key); 61 } 62 63 static inline void resctrl_arch_disable_alloc(void) 64 { 65 static_branch_disable_cpuslocked(&rdt_alloc_enable_key); 66 static_branch_dec_cpuslocked(&rdt_enable_key); 67 } 68 69 static inline bool resctrl_arch_mon_capable(void) 70 { 71 return rdt_mon_capable; 72 } 73 74 static inline void resctrl_arch_enable_mon(void) 75 { 76 static_branch_enable_cpuslocked(&rdt_mon_enable_key); 77 static_branch_inc_cpuslocked(&rdt_enable_key); 78 } 79 80 static inline void resctrl_arch_disable_mon(void) 81 { 82 static_branch_disable_cpuslocked(&rdt_mon_enable_key); 83 static_branch_dec_cpuslocked(&rdt_enable_key); 84 } 85 86 /* 87 * __resctrl_sched_in() - Writes the task's CLOSid/RMID to IA32_PQR_MSR 88 * 89 * Following considerations are made so that this has minimal impact 90 * on scheduler hot path: 91 * - This will stay as no-op unless we are running on an Intel SKU 92 * which supports resource control or monitoring and we enable by 93 * mounting the resctrl file system. 94 * - Caches the per cpu CLOSid/RMID values and does the MSR write only 95 * when a task with a different CLOSid/RMID is scheduled in. 96 * - We allocate RMIDs/CLOSids globally in order to keep this as 97 * simple as possible. 98 * Must be called with preemption disabled. 99 */ 100 static inline void __resctrl_sched_in(struct task_struct *tsk) 101 { 102 struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state); 103 u32 closid = READ_ONCE(state->default_closid); 104 u32 rmid = READ_ONCE(state->default_rmid); 105 struct msr val; 106 u32 tmp; 107 108 /* 109 * If this task has a closid/rmid assigned, use it. 110 * Else use the closid/rmid assigned to this cpu. 111 */ 112 if (static_branch_likely(&rdt_alloc_enable_key)) { 113 tmp = READ_ONCE(tsk->closid); 114 if (tmp) 115 closid = tmp; 116 } 117 118 if (static_branch_likely(&rdt_mon_enable_key)) { 119 tmp = READ_ONCE(tsk->rmid); 120 if (tmp) 121 rmid = tmp; 122 } 123 124 if (closid != state->cur_closid || rmid != state->cur_rmid) { 125 state->cur_closid = closid; 126 state->cur_rmid = rmid; 127 val.l = rmid; 128 val.h = closid; 129 wrmsrq(MSR_IA32_PQR_ASSOC, val.q); 130 } 131 } 132 133 static inline unsigned int resctrl_arch_round_mon_val(unsigned int val) 134 { 135 unsigned int scale = boot_cpu_data.x86_cache_occ_scale; 136 137 /* h/w works in units of "boot_cpu_data.x86_cache_occ_scale" */ 138 val /= scale; 139 return val * scale; 140 } 141 142 static inline void resctrl_arch_set_cpu_default_closid_rmid(int cpu, u32 closid, 143 u32 rmid) 144 { 145 WRITE_ONCE(per_cpu(pqr_state.default_closid, cpu), closid); 146 WRITE_ONCE(per_cpu(pqr_state.default_rmid, cpu), rmid); 147 } 148 149 static inline void resctrl_arch_set_closid_rmid(struct task_struct *tsk, 150 u32 closid, u32 rmid) 151 { 152 WRITE_ONCE(tsk->closid, closid); 153 WRITE_ONCE(tsk->rmid, rmid); 154 } 155 156 static inline bool resctrl_arch_match_closid(struct task_struct *tsk, u32 closid) 157 { 158 return READ_ONCE(tsk->closid) == closid; 159 } 160 161 static inline bool resctrl_arch_match_rmid(struct task_struct *tsk, u32 ignored, 162 u32 rmid) 163 { 164 return READ_ONCE(tsk->rmid) == rmid; 165 } 166 167 static inline void resctrl_arch_sched_in(struct task_struct *tsk) 168 { 169 if (static_branch_likely(&rdt_enable_key)) 170 __resctrl_sched_in(tsk); 171 } 172 173 static inline void resctrl_arch_rmid_idx_decode(u32 idx, u32 *closid, u32 *rmid) 174 { 175 *rmid = idx; 176 *closid = X86_RESCTRL_EMPTY_CLOSID; 177 } 178 179 static inline u32 resctrl_arch_rmid_idx_encode(u32 ignored, u32 rmid) 180 { 181 return rmid; 182 } 183 184 /* x86 can always read an rmid, nothing needs allocating */ 185 struct rdt_resource; 186 static inline void *resctrl_arch_mon_ctx_alloc(struct rdt_resource *r, 187 enum resctrl_event_id evtid) 188 { 189 might_sleep(); 190 return NULL; 191 } 192 193 static inline void resctrl_arch_mon_ctx_free(struct rdt_resource *r, 194 enum resctrl_event_id evtid, 195 void *ctx) { } 196 197 void resctrl_cpu_detect(struct cpuinfo_x86 *c); 198 199 #else 200 201 static inline void resctrl_arch_sched_in(struct task_struct *tsk) {} 202 static inline void resctrl_cpu_detect(struct cpuinfo_x86 *c) {} 203 204 #endif /* CONFIG_X86_CPU_RESCTRL */ 205 206 #endif /* _ASM_X86_RESCTRL_H */ 207