1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_RESCTRL_INTERNAL_H
3 #define _ASM_X86_RESCTRL_INTERNAL_H
4
5 #include <linux/resctrl.h>
6 #include <linux/sched.h>
7 #include <linux/kernfs.h>
8 #include <linux/fs_context.h>
9 #include <linux/jump_label.h>
10
11 #define L3_QOS_CDP_ENABLE 0x01ULL
12
13 #define L2_QOS_CDP_ENABLE 0x01ULL
14
15 #define CQM_LIMBOCHECK_INTERVAL 1000
16
17 #define MBM_CNTR_WIDTH_BASE 24
18 #define MBM_OVERFLOW_INTERVAL 1000
19 #define MAX_MBA_BW 100u
20 #define MBA_IS_LINEAR 0x4
21 #define MBM_CNTR_WIDTH_OFFSET_AMD 20
22
23 #define RMID_VAL_ERROR BIT_ULL(63)
24 #define RMID_VAL_UNAVAIL BIT_ULL(62)
25 /*
26 * With the above fields in use 62 bits remain in MSR_IA32_QM_CTR for
27 * data to be returned. The counter width is discovered from the hardware
28 * as an offset from MBM_CNTR_WIDTH_BASE.
29 */
30 #define MBM_CNTR_WIDTH_OFFSET_MAX (62 - MBM_CNTR_WIDTH_BASE)
31
32 /* Reads to Local DRAM Memory */
33 #define READS_TO_LOCAL_MEM BIT(0)
34
35 /* Reads to Remote DRAM Memory */
36 #define READS_TO_REMOTE_MEM BIT(1)
37
38 /* Non-Temporal Writes to Local Memory */
39 #define NON_TEMP_WRITE_TO_LOCAL_MEM BIT(2)
40
41 /* Non-Temporal Writes to Remote Memory */
42 #define NON_TEMP_WRITE_TO_REMOTE_MEM BIT(3)
43
44 /* Reads to Local Memory the system identifies as "Slow Memory" */
45 #define READS_TO_LOCAL_S_MEM BIT(4)
46
47 /* Reads to Remote Memory the system identifies as "Slow Memory" */
48 #define READS_TO_REMOTE_S_MEM BIT(5)
49
50 /* Dirty Victims to All Types of Memory */
51 #define DIRTY_VICTIMS_TO_ALL_MEM BIT(6)
52
53 /* Max event bits supported */
54 #define MAX_EVT_CONFIG_BITS GENMASK(6, 0)
55
56 struct rdt_fs_context {
57 struct kernfs_fs_context kfc;
58 bool enable_cdpl2;
59 bool enable_cdpl3;
60 bool enable_mba_mbps;
61 };
62
rdt_fc2context(struct fs_context * fc)63 static inline struct rdt_fs_context *rdt_fc2context(struct fs_context *fc)
64 {
65 struct kernfs_fs_context *kfc = fc->fs_private;
66
67 return container_of(kfc, struct rdt_fs_context, kfc);
68 }
69
70 DECLARE_STATIC_KEY_FALSE(rdt_enable_key);
71 DECLARE_STATIC_KEY_FALSE(rdt_mon_enable_key);
72
73 /**
74 * struct mon_evt - Entry in the event list of a resource
75 * @evtid: event id
76 * @name: name of the event
77 * @configurable: true if the event is configurable
78 * @list: entry in &rdt_resource->evt_list
79 */
80 struct mon_evt {
81 enum resctrl_event_id evtid;
82 char *name;
83 bool configurable;
84 struct list_head list;
85 };
86
87 /**
88 * union mon_data_bits - Monitoring details for each event file
89 * @priv: Used to store monitoring event data in @u
90 * as kernfs private data
91 * @rid: Resource id associated with the event file
92 * @evtid: Event id associated with the event file
93 * @domid: The domain to which the event file belongs
94 * @u: Name of the bit fields struct
95 */
96 union mon_data_bits {
97 void *priv;
98 struct {
99 unsigned int rid : 10;
100 enum resctrl_event_id evtid : 8;
101 unsigned int domid : 14;
102 } u;
103 };
104
105 struct rmid_read {
106 struct rdtgroup *rgrp;
107 struct rdt_resource *r;
108 struct rdt_domain *d;
109 enum resctrl_event_id evtid;
110 bool first;
111 int err;
112 u64 val;
113 };
114
115 extern bool rdt_alloc_capable;
116 extern bool rdt_mon_capable;
117 extern unsigned int rdt_mon_features;
118 extern struct list_head resctrl_schema_all;
119
120 enum rdt_group_type {
121 RDTCTRL_GROUP = 0,
122 RDTMON_GROUP,
123 RDT_NUM_GROUP,
124 };
125
126 /**
127 * enum rdtgrp_mode - Mode of a RDT resource group
128 * @RDT_MODE_SHAREABLE: This resource group allows sharing of its allocations
129 * @RDT_MODE_EXCLUSIVE: No sharing of this resource group's allocations allowed
130 * @RDT_MODE_PSEUDO_LOCKSETUP: Resource group will be used for Pseudo-Locking
131 * @RDT_MODE_PSEUDO_LOCKED: No sharing of this resource group's allocations
132 * allowed AND the allocations are Cache Pseudo-Locked
133 * @RDT_NUM_MODES: Total number of modes
134 *
135 * The mode of a resource group enables control over the allowed overlap
136 * between allocations associated with different resource groups (classes
137 * of service). User is able to modify the mode of a resource group by
138 * writing to the "mode" resctrl file associated with the resource group.
139 *
140 * The "shareable", "exclusive", and "pseudo-locksetup" modes are set by
141 * writing the appropriate text to the "mode" file. A resource group enters
142 * "pseudo-locked" mode after the schemata is written while the resource
143 * group is in "pseudo-locksetup" mode.
144 */
145 enum rdtgrp_mode {
146 RDT_MODE_SHAREABLE = 0,
147 RDT_MODE_EXCLUSIVE,
148 RDT_MODE_PSEUDO_LOCKSETUP,
149 RDT_MODE_PSEUDO_LOCKED,
150
151 /* Must be last */
152 RDT_NUM_MODES,
153 };
154
155 /**
156 * struct mongroup - store mon group's data in resctrl fs.
157 * @mon_data_kn: kernfs node for the mon_data directory
158 * @parent: parent rdtgrp
159 * @crdtgrp_list: child rdtgroup node list
160 * @rmid: rmid for this rdtgroup
161 */
162 struct mongroup {
163 struct kernfs_node *mon_data_kn;
164 struct rdtgroup *parent;
165 struct list_head crdtgrp_list;
166 u32 rmid;
167 };
168
169 /**
170 * struct pseudo_lock_region - pseudo-lock region information
171 * @s: Resctrl schema for the resource to which this
172 * pseudo-locked region belongs
173 * @d: RDT domain to which this pseudo-locked region
174 * belongs
175 * @cbm: bitmask of the pseudo-locked region
176 * @lock_thread_wq: waitqueue used to wait on the pseudo-locking thread
177 * completion
178 * @thread_done: variable used by waitqueue to test if pseudo-locking
179 * thread completed
180 * @cpu: core associated with the cache on which the setup code
181 * will be run
182 * @line_size: size of the cache lines
183 * @size: size of pseudo-locked region in bytes
184 * @kmem: the kernel memory associated with pseudo-locked region
185 * @minor: minor number of character device associated with this
186 * region
187 * @debugfs_dir: pointer to this region's directory in the debugfs
188 * filesystem
189 * @pm_reqs: Power management QoS requests related to this region
190 */
191 struct pseudo_lock_region {
192 struct resctrl_schema *s;
193 struct rdt_domain *d;
194 u32 cbm;
195 wait_queue_head_t lock_thread_wq;
196 int thread_done;
197 int cpu;
198 unsigned int line_size;
199 unsigned int size;
200 void *kmem;
201 unsigned int minor;
202 struct dentry *debugfs_dir;
203 struct list_head pm_reqs;
204 };
205
206 /**
207 * struct rdtgroup - store rdtgroup's data in resctrl file system.
208 * @kn: kernfs node
209 * @rdtgroup_list: linked list for all rdtgroups
210 * @closid: closid for this rdtgroup
211 * @cpu_mask: CPUs assigned to this rdtgroup
212 * @flags: status bits
213 * @waitcount: how many cpus expect to find this
214 * group when they acquire rdtgroup_mutex
215 * @type: indicates type of this rdtgroup - either
216 * monitor only or ctrl_mon group
217 * @mon: mongroup related data
218 * @mode: mode of resource group
219 * @plr: pseudo-locked region
220 */
221 struct rdtgroup {
222 struct kernfs_node *kn;
223 struct list_head rdtgroup_list;
224 u32 closid;
225 struct cpumask cpu_mask;
226 int flags;
227 atomic_t waitcount;
228 enum rdt_group_type type;
229 struct mongroup mon;
230 enum rdtgrp_mode mode;
231 struct pseudo_lock_region *plr;
232 };
233
234 /* rdtgroup.flags */
235 #define RDT_DELETED 1
236
237 /* rftype.flags */
238 #define RFTYPE_FLAGS_CPUS_LIST 1
239
240 /*
241 * Define the file type flags for base and info directories.
242 */
243 #define RFTYPE_INFO BIT(0)
244 #define RFTYPE_BASE BIT(1)
245 #define RF_CTRLSHIFT 4
246 #define RF_MONSHIFT 5
247 #define RF_TOPSHIFT 6
248 #define RFTYPE_CTRL BIT(RF_CTRLSHIFT)
249 #define RFTYPE_MON BIT(RF_MONSHIFT)
250 #define RFTYPE_TOP BIT(RF_TOPSHIFT)
251 #define RFTYPE_RES_CACHE BIT(8)
252 #define RFTYPE_RES_MB BIT(9)
253 #define RF_CTRL_INFO (RFTYPE_INFO | RFTYPE_CTRL)
254 #define RF_MON_INFO (RFTYPE_INFO | RFTYPE_MON)
255 #define RF_TOP_INFO (RFTYPE_INFO | RFTYPE_TOP)
256 #define RF_CTRL_BASE (RFTYPE_BASE | RFTYPE_CTRL)
257
258 /* List of all resource groups */
259 extern struct list_head rdt_all_groups;
260
261 extern int max_name_width, max_data_width;
262
263 int __init rdtgroup_init(void);
264 void __exit rdtgroup_exit(void);
265
266 /**
267 * struct rftype - describe each file in the resctrl file system
268 * @name: File name
269 * @mode: Access mode
270 * @kf_ops: File operations
271 * @flags: File specific RFTYPE_FLAGS_* flags
272 * @fflags: File specific RF_* or RFTYPE_* flags
273 * @seq_show: Show content of the file
274 * @write: Write to the file
275 */
276 struct rftype {
277 char *name;
278 umode_t mode;
279 const struct kernfs_ops *kf_ops;
280 unsigned long flags;
281 unsigned long fflags;
282
283 int (*seq_show)(struct kernfs_open_file *of,
284 struct seq_file *sf, void *v);
285 /*
286 * write() is the generic write callback which maps directly to
287 * kernfs write operation and overrides all other operations.
288 * Maximum write size is determined by ->max_write_len.
289 */
290 ssize_t (*write)(struct kernfs_open_file *of,
291 char *buf, size_t nbytes, loff_t off);
292 };
293
294 /**
295 * struct mbm_state - status for each MBM counter in each domain
296 * @prev_bw_bytes: Previous bytes value read for bandwidth calculation
297 * @prev_bw: The most recent bandwidth in MBps
298 */
299 struct mbm_state {
300 u64 prev_bw_bytes;
301 u32 prev_bw;
302 };
303
304 /**
305 * struct arch_mbm_state - values used to compute resctrl_arch_rmid_read()s
306 * return value.
307 * @chunks: Total data moved (multiply by rdt_group.mon_scale to get bytes)
308 * @prev_msr: Value of IA32_QM_CTR last time it was read for the RMID used to
309 * find this struct.
310 */
311 struct arch_mbm_state {
312 u64 chunks;
313 u64 prev_msr;
314 };
315
316 /**
317 * struct rdt_hw_domain - Arch private attributes of a set of CPUs that share
318 * a resource
319 * @d_resctrl: Properties exposed to the resctrl file system
320 * @ctrl_val: array of cache or mem ctrl values (indexed by CLOSID)
321 * @arch_mbm_total: arch private state for MBM total bandwidth
322 * @arch_mbm_local: arch private state for MBM local bandwidth
323 *
324 * Members of this structure are accessed via helpers that provide abstraction.
325 */
326 struct rdt_hw_domain {
327 struct rdt_domain d_resctrl;
328 u32 *ctrl_val;
329 struct arch_mbm_state *arch_mbm_total;
330 struct arch_mbm_state *arch_mbm_local;
331 };
332
resctrl_to_arch_dom(struct rdt_domain * r)333 static inline struct rdt_hw_domain *resctrl_to_arch_dom(struct rdt_domain *r)
334 {
335 return container_of(r, struct rdt_hw_domain, d_resctrl);
336 }
337
338 /**
339 * struct msr_param - set a range of MSRs from a domain
340 * @res: The resource to use
341 * @low: Beginning index from base MSR
342 * @high: End index
343 */
344 struct msr_param {
345 struct rdt_resource *res;
346 u32 low;
347 u32 high;
348 };
349
is_llc_occupancy_enabled(void)350 static inline bool is_llc_occupancy_enabled(void)
351 {
352 return (rdt_mon_features & (1 << QOS_L3_OCCUP_EVENT_ID));
353 }
354
is_mbm_total_enabled(void)355 static inline bool is_mbm_total_enabled(void)
356 {
357 return (rdt_mon_features & (1 << QOS_L3_MBM_TOTAL_EVENT_ID));
358 }
359
is_mbm_local_enabled(void)360 static inline bool is_mbm_local_enabled(void)
361 {
362 return (rdt_mon_features & (1 << QOS_L3_MBM_LOCAL_EVENT_ID));
363 }
364
is_mbm_enabled(void)365 static inline bool is_mbm_enabled(void)
366 {
367 return (is_mbm_total_enabled() || is_mbm_local_enabled());
368 }
369
is_mbm_event(int e)370 static inline bool is_mbm_event(int e)
371 {
372 return (e >= QOS_L3_MBM_TOTAL_EVENT_ID &&
373 e <= QOS_L3_MBM_LOCAL_EVENT_ID);
374 }
375
376 struct rdt_parse_data {
377 struct rdtgroup *rdtgrp;
378 char *buf;
379 };
380
381 /**
382 * struct rdt_hw_resource - arch private attributes of a resctrl resource
383 * @r_resctrl: Attributes of the resource used directly by resctrl.
384 * @num_closid: Maximum number of closid this hardware can support,
385 * regardless of CDP. This is exposed via
386 * resctrl_arch_get_num_closid() to avoid confusion
387 * with struct resctrl_schema's property of the same name,
388 * which has been corrected for features like CDP.
389 * @msr_base: Base MSR address for CBMs
390 * @msr_update: Function pointer to update QOS MSRs
391 * @mon_scale: cqm counter * mon_scale = occupancy in bytes
392 * @mbm_width: Monitor width, to detect and correct for overflow.
393 * @mbm_cfg_mask: Bandwidth sources that can be tracked when Bandwidth
394 * Monitoring Event Configuration (BMEC) is supported.
395 * @cdp_enabled: CDP state of this resource
396 *
397 * Members of this structure are either private to the architecture
398 * e.g. mbm_width, or accessed via helpers that provide abstraction. e.g.
399 * msr_update and msr_base.
400 */
401 struct rdt_hw_resource {
402 struct rdt_resource r_resctrl;
403 u32 num_closid;
404 unsigned int msr_base;
405 void (*msr_update) (struct rdt_domain *d, struct msr_param *m,
406 struct rdt_resource *r);
407 unsigned int mon_scale;
408 unsigned int mbm_width;
409 unsigned int mbm_cfg_mask;
410 bool cdp_enabled;
411 };
412
resctrl_to_arch_res(struct rdt_resource * r)413 static inline struct rdt_hw_resource *resctrl_to_arch_res(struct rdt_resource *r)
414 {
415 return container_of(r, struct rdt_hw_resource, r_resctrl);
416 }
417
418 int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
419 struct rdt_domain *d);
420 int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
421 struct rdt_domain *d);
422
423 extern struct mutex rdtgroup_mutex;
424
425 extern struct rdt_hw_resource rdt_resources_all[];
426 extern struct rdtgroup rdtgroup_default;
427 DECLARE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
428
429 extern struct dentry *debugfs_resctrl;
430
431 enum resctrl_res_level {
432 RDT_RESOURCE_L3,
433 RDT_RESOURCE_L2,
434 RDT_RESOURCE_MBA,
435 RDT_RESOURCE_SMBA,
436
437 /* Must be the last */
438 RDT_NUM_RESOURCES,
439 };
440
resctrl_inc(struct rdt_resource * res)441 static inline struct rdt_resource *resctrl_inc(struct rdt_resource *res)
442 {
443 struct rdt_hw_resource *hw_res = resctrl_to_arch_res(res);
444
445 hw_res++;
446 return &hw_res->r_resctrl;
447 }
448
resctrl_arch_get_cdp_enabled(enum resctrl_res_level l)449 static inline bool resctrl_arch_get_cdp_enabled(enum resctrl_res_level l)
450 {
451 return rdt_resources_all[l].cdp_enabled;
452 }
453
454 int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable);
455
456 /*
457 * To return the common struct rdt_resource, which is contained in struct
458 * rdt_hw_resource, walk the resctrl member of struct rdt_hw_resource.
459 */
460 #define for_each_rdt_resource(r) \
461 for (r = &rdt_resources_all[0].r_resctrl; \
462 r <= &rdt_resources_all[RDT_NUM_RESOURCES - 1].r_resctrl; \
463 r = resctrl_inc(r))
464
465 #define for_each_capable_rdt_resource(r) \
466 for_each_rdt_resource(r) \
467 if (r->alloc_capable || r->mon_capable)
468
469 #define for_each_alloc_capable_rdt_resource(r) \
470 for_each_rdt_resource(r) \
471 if (r->alloc_capable)
472
473 #define for_each_mon_capable_rdt_resource(r) \
474 for_each_rdt_resource(r) \
475 if (r->mon_capable)
476
477 /* CPUID.(EAX=10H, ECX=ResID=1).EAX */
478 union cpuid_0x10_1_eax {
479 struct {
480 unsigned int cbm_len:5;
481 } split;
482 unsigned int full;
483 };
484
485 /* CPUID.(EAX=10H, ECX=ResID=3).EAX */
486 union cpuid_0x10_3_eax {
487 struct {
488 unsigned int max_delay:12;
489 } split;
490 unsigned int full;
491 };
492
493 /* CPUID.(EAX=10H, ECX=ResID).EDX */
494 union cpuid_0x10_x_edx {
495 struct {
496 unsigned int cos_max:16;
497 } split;
498 unsigned int full;
499 };
500
501 void rdt_last_cmd_clear(void);
502 void rdt_last_cmd_puts(const char *s);
503 __printf(1, 2)
504 void rdt_last_cmd_printf(const char *fmt, ...);
505
506 void rdt_ctrl_update(void *arg);
507 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn);
508 void rdtgroup_kn_unlock(struct kernfs_node *kn);
509 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name);
510 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
511 umode_t mask);
512 struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
513 struct list_head **pos);
514 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
515 char *buf, size_t nbytes, loff_t off);
516 int rdtgroup_schemata_show(struct kernfs_open_file *of,
517 struct seq_file *s, void *v);
518 bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
519 unsigned long cbm, int closid, bool exclusive);
520 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, struct rdt_domain *d,
521 unsigned long cbm);
522 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid);
523 int rdtgroup_tasks_assigned(struct rdtgroup *r);
524 int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp);
525 int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp);
526 bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain *d, unsigned long cbm);
527 bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_domain *d);
528 int rdt_pseudo_lock_init(void);
529 void rdt_pseudo_lock_release(void);
530 int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp);
531 void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp);
532 struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r);
533 int closids_supported(void);
534 void closid_free(int closid);
535 int alloc_rmid(void);
536 void free_rmid(u32 rmid);
537 int rdt_get_mon_l3_config(struct rdt_resource *r);
538 bool __init rdt_cpu_has(int flag);
539 void mon_event_count(void *info);
540 int rdtgroup_mondata_show(struct seq_file *m, void *arg);
541 void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
542 struct rdt_domain *d, struct rdtgroup *rdtgrp,
543 int evtid, int first);
544 void mbm_setup_overflow_handler(struct rdt_domain *dom,
545 unsigned long delay_ms);
546 void mbm_handle_overflow(struct work_struct *work);
547 void __init intel_rdt_mbm_apply_quirk(void);
548 bool is_mba_sc(struct rdt_resource *r);
549 void cqm_setup_limbo_handler(struct rdt_domain *dom, unsigned long delay_ms);
550 void cqm_handle_limbo(struct work_struct *work);
551 bool has_busy_rmid(struct rdt_resource *r, struct rdt_domain *d);
552 void __check_limbo(struct rdt_domain *d, bool force_free);
553 void rdt_domain_reconfigure_cdp(struct rdt_resource *r);
554 void __init thread_throttle_mode_init(void);
555 void __init mbm_config_rftype_init(const char *config);
556 void rdt_staged_configs_clear(void);
557
558 #endif /* _ASM_X86_RESCTRL_INTERNAL_H */
559