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