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/sched.h> 6 #include <linux/kernfs.h> 7 #include <linux/jump_label.h> 8 9 #define MSR_IA32_L3_QOS_CFG 0xc81 10 #define MSR_IA32_L2_QOS_CFG 0xc82 11 #define MSR_IA32_L3_CBM_BASE 0xc90 12 #define MSR_IA32_L2_CBM_BASE 0xd10 13 #define MSR_IA32_MBA_THRTL_BASE 0xd50 14 #define MSR_IA32_MBA_BW_BASE 0xc0000200 15 16 #define MSR_IA32_QM_CTR 0x0c8e 17 #define MSR_IA32_QM_EVTSEL 0x0c8d 18 19 #define L3_QOS_CDP_ENABLE 0x01ULL 20 21 #define L2_QOS_CDP_ENABLE 0x01ULL 22 23 /* 24 * Event IDs are used to program IA32_QM_EVTSEL before reading event 25 * counter from IA32_QM_CTR 26 */ 27 #define QOS_L3_OCCUP_EVENT_ID 0x01 28 #define QOS_L3_MBM_TOTAL_EVENT_ID 0x02 29 #define QOS_L3_MBM_LOCAL_EVENT_ID 0x03 30 31 #define CQM_LIMBOCHECK_INTERVAL 1000 32 33 #define MBM_CNTR_WIDTH 24 34 #define MBM_OVERFLOW_INTERVAL 1000 35 #define MAX_MBA_BW 100u 36 #define MBA_IS_LINEAR 0x4 37 #define MBA_MAX_MBPS U32_MAX 38 #define MAX_MBA_BW_AMD 0x800 39 40 #define RMID_VAL_ERROR BIT_ULL(63) 41 #define RMID_VAL_UNAVAIL BIT_ULL(62) 42 43 DECLARE_STATIC_KEY_FALSE(rdt_enable_key); 44 45 /** 46 * struct mon_evt - Entry in the event list of a resource 47 * @evtid: event id 48 * @name: name of the event 49 */ 50 struct mon_evt { 51 u32 evtid; 52 char *name; 53 struct list_head list; 54 }; 55 56 /** 57 * struct mon_data_bits - Monitoring details for each event file 58 * @rid: Resource id associated with the event file. 59 * @evtid: Event id associated with the event file 60 * @domid: The domain to which the event file belongs 61 */ 62 union mon_data_bits { 63 void *priv; 64 struct { 65 unsigned int rid : 10; 66 unsigned int evtid : 8; 67 unsigned int domid : 14; 68 } u; 69 }; 70 71 struct rmid_read { 72 struct rdtgroup *rgrp; 73 struct rdt_domain *d; 74 int evtid; 75 bool first; 76 u64 val; 77 }; 78 79 extern unsigned int resctrl_cqm_threshold; 80 extern bool rdt_alloc_capable; 81 extern bool rdt_mon_capable; 82 extern unsigned int rdt_mon_features; 83 84 enum rdt_group_type { 85 RDTCTRL_GROUP = 0, 86 RDTMON_GROUP, 87 RDT_NUM_GROUP, 88 }; 89 90 /** 91 * enum rdtgrp_mode - Mode of a RDT resource group 92 * @RDT_MODE_SHAREABLE: This resource group allows sharing of its allocations 93 * @RDT_MODE_EXCLUSIVE: No sharing of this resource group's allocations allowed 94 * @RDT_MODE_PSEUDO_LOCKSETUP: Resource group will be used for Pseudo-Locking 95 * @RDT_MODE_PSEUDO_LOCKED: No sharing of this resource group's allocations 96 * allowed AND the allocations are Cache Pseudo-Locked 97 * 98 * The mode of a resource group enables control over the allowed overlap 99 * between allocations associated with different resource groups (classes 100 * of service). User is able to modify the mode of a resource group by 101 * writing to the "mode" resctrl file associated with the resource group. 102 * 103 * The "shareable", "exclusive", and "pseudo-locksetup" modes are set by 104 * writing the appropriate text to the "mode" file. A resource group enters 105 * "pseudo-locked" mode after the schemata is written while the resource 106 * group is in "pseudo-locksetup" mode. 107 */ 108 enum rdtgrp_mode { 109 RDT_MODE_SHAREABLE = 0, 110 RDT_MODE_EXCLUSIVE, 111 RDT_MODE_PSEUDO_LOCKSETUP, 112 RDT_MODE_PSEUDO_LOCKED, 113 114 /* Must be last */ 115 RDT_NUM_MODES, 116 }; 117 118 /** 119 * struct mongroup - store mon group's data in resctrl fs. 120 * @mon_data_kn kernlfs node for the mon_data directory 121 * @parent: parent rdtgrp 122 * @crdtgrp_list: child rdtgroup node list 123 * @rmid: rmid for this rdtgroup 124 */ 125 struct mongroup { 126 struct kernfs_node *mon_data_kn; 127 struct rdtgroup *parent; 128 struct list_head crdtgrp_list; 129 u32 rmid; 130 }; 131 132 /** 133 * struct pseudo_lock_region - pseudo-lock region information 134 * @r: RDT resource to which this pseudo-locked region 135 * belongs 136 * @d: RDT domain to which this pseudo-locked region 137 * belongs 138 * @cbm: bitmask of the pseudo-locked region 139 * @lock_thread_wq: waitqueue used to wait on the pseudo-locking thread 140 * completion 141 * @thread_done: variable used by waitqueue to test if pseudo-locking 142 * thread completed 143 * @cpu: core associated with the cache on which the setup code 144 * will be run 145 * @line_size: size of the cache lines 146 * @size: size of pseudo-locked region in bytes 147 * @kmem: the kernel memory associated with pseudo-locked region 148 * @minor: minor number of character device associated with this 149 * region 150 * @debugfs_dir: pointer to this region's directory in the debugfs 151 * filesystem 152 * @pm_reqs: Power management QoS requests related to this region 153 */ 154 struct pseudo_lock_region { 155 struct rdt_resource *r; 156 struct rdt_domain *d; 157 u32 cbm; 158 wait_queue_head_t lock_thread_wq; 159 int thread_done; 160 int cpu; 161 unsigned int line_size; 162 unsigned int size; 163 void *kmem; 164 unsigned int minor; 165 struct dentry *debugfs_dir; 166 struct list_head pm_reqs; 167 }; 168 169 /** 170 * struct rdtgroup - store rdtgroup's data in resctrl file system. 171 * @kn: kernfs node 172 * @rdtgroup_list: linked list for all rdtgroups 173 * @closid: closid for this rdtgroup 174 * @cpu_mask: CPUs assigned to this rdtgroup 175 * @flags: status bits 176 * @waitcount: how many cpus expect to find this 177 * group when they acquire rdtgroup_mutex 178 * @type: indicates type of this rdtgroup - either 179 * monitor only or ctrl_mon group 180 * @mon: mongroup related data 181 * @mode: mode of resource group 182 * @plr: pseudo-locked region 183 */ 184 struct rdtgroup { 185 struct kernfs_node *kn; 186 struct list_head rdtgroup_list; 187 u32 closid; 188 struct cpumask cpu_mask; 189 int flags; 190 atomic_t waitcount; 191 enum rdt_group_type type; 192 struct mongroup mon; 193 enum rdtgrp_mode mode; 194 struct pseudo_lock_region *plr; 195 }; 196 197 /* rdtgroup.flags */ 198 #define RDT_DELETED 1 199 200 /* rftype.flags */ 201 #define RFTYPE_FLAGS_CPUS_LIST 1 202 203 /* 204 * Define the file type flags for base and info directories. 205 */ 206 #define RFTYPE_INFO BIT(0) 207 #define RFTYPE_BASE BIT(1) 208 #define RF_CTRLSHIFT 4 209 #define RF_MONSHIFT 5 210 #define RF_TOPSHIFT 6 211 #define RFTYPE_CTRL BIT(RF_CTRLSHIFT) 212 #define RFTYPE_MON BIT(RF_MONSHIFT) 213 #define RFTYPE_TOP BIT(RF_TOPSHIFT) 214 #define RFTYPE_RES_CACHE BIT(8) 215 #define RFTYPE_RES_MB BIT(9) 216 #define RF_CTRL_INFO (RFTYPE_INFO | RFTYPE_CTRL) 217 #define RF_MON_INFO (RFTYPE_INFO | RFTYPE_MON) 218 #define RF_TOP_INFO (RFTYPE_INFO | RFTYPE_TOP) 219 #define RF_CTRL_BASE (RFTYPE_BASE | RFTYPE_CTRL) 220 221 /* List of all resource groups */ 222 extern struct list_head rdt_all_groups; 223 224 extern int max_name_width, max_data_width; 225 226 int __init rdtgroup_init(void); 227 void __exit rdtgroup_exit(void); 228 229 /** 230 * struct rftype - describe each file in the resctrl file system 231 * @name: File name 232 * @mode: Access mode 233 * @kf_ops: File operations 234 * @flags: File specific RFTYPE_FLAGS_* flags 235 * @fflags: File specific RF_* or RFTYPE_* flags 236 * @seq_show: Show content of the file 237 * @write: Write to the file 238 */ 239 struct rftype { 240 char *name; 241 umode_t mode; 242 struct kernfs_ops *kf_ops; 243 unsigned long flags; 244 unsigned long fflags; 245 246 int (*seq_show)(struct kernfs_open_file *of, 247 struct seq_file *sf, void *v); 248 /* 249 * write() is the generic write callback which maps directly to 250 * kernfs write operation and overrides all other operations. 251 * Maximum write size is determined by ->max_write_len. 252 */ 253 ssize_t (*write)(struct kernfs_open_file *of, 254 char *buf, size_t nbytes, loff_t off); 255 }; 256 257 /** 258 * struct mbm_state - status for each MBM counter in each domain 259 * @chunks: Total data moved (multiply by rdt_group.mon_scale to get bytes) 260 * @prev_msr Value of IA32_QM_CTR for this RMID last time we read it 261 * @chunks_bw Total local data moved. Used for bandwidth calculation 262 * @prev_bw_msr:Value of previous IA32_QM_CTR for bandwidth counting 263 * @prev_bw The most recent bandwidth in MBps 264 * @delta_bw Difference between the current and previous bandwidth 265 * @delta_comp Indicates whether to compute the delta_bw 266 */ 267 struct mbm_state { 268 u64 chunks; 269 u64 prev_msr; 270 u64 chunks_bw; 271 u64 prev_bw_msr; 272 u32 prev_bw; 273 u32 delta_bw; 274 bool delta_comp; 275 }; 276 277 /** 278 * struct rdt_domain - group of cpus sharing an RDT resource 279 * @list: all instances of this resource 280 * @id: unique id for this instance 281 * @cpu_mask: which cpus share this resource 282 * @rmid_busy_llc: 283 * bitmap of which limbo RMIDs are above threshold 284 * @mbm_total: saved state for MBM total bandwidth 285 * @mbm_local: saved state for MBM local bandwidth 286 * @mbm_over: worker to periodically read MBM h/w counters 287 * @cqm_limbo: worker to periodically read CQM h/w counters 288 * @mbm_work_cpu: 289 * worker cpu for MBM h/w counters 290 * @cqm_work_cpu: 291 * worker cpu for CQM h/w counters 292 * @ctrl_val: array of cache or mem ctrl values (indexed by CLOSID) 293 * @mbps_val: When mba_sc is enabled, this holds the bandwidth in MBps 294 * @new_ctrl: new ctrl value to be loaded 295 * @have_new_ctrl: did user provide new_ctrl for this domain 296 * @plr: pseudo-locked region (if any) associated with domain 297 */ 298 struct rdt_domain { 299 struct list_head list; 300 int id; 301 struct cpumask cpu_mask; 302 unsigned long *rmid_busy_llc; 303 struct mbm_state *mbm_total; 304 struct mbm_state *mbm_local; 305 struct delayed_work mbm_over; 306 struct delayed_work cqm_limbo; 307 int mbm_work_cpu; 308 int cqm_work_cpu; 309 u32 *ctrl_val; 310 u32 *mbps_val; 311 u32 new_ctrl; 312 bool have_new_ctrl; 313 struct pseudo_lock_region *plr; 314 }; 315 316 /** 317 * struct msr_param - set a range of MSRs from a domain 318 * @res: The resource to use 319 * @low: Beginning index from base MSR 320 * @high: End index 321 */ 322 struct msr_param { 323 struct rdt_resource *res; 324 int low; 325 int high; 326 }; 327 328 /** 329 * struct rdt_cache - Cache allocation related data 330 * @cbm_len: Length of the cache bit mask 331 * @min_cbm_bits: Minimum number of consecutive bits to be set 332 * @cbm_idx_mult: Multiplier of CBM index 333 * @cbm_idx_offset: Offset of CBM index. CBM index is computed by: 334 * closid * cbm_idx_multi + cbm_idx_offset 335 * in a cache bit mask 336 * @shareable_bits: Bitmask of shareable resource with other 337 * executing entities 338 */ 339 struct rdt_cache { 340 unsigned int cbm_len; 341 unsigned int min_cbm_bits; 342 unsigned int cbm_idx_mult; 343 unsigned int cbm_idx_offset; 344 unsigned int shareable_bits; 345 }; 346 347 /** 348 * struct rdt_membw - Memory bandwidth allocation related data 349 * @max_delay: Max throttle delay. Delay is the hardware 350 * representation for memory bandwidth. 351 * @min_bw: Minimum memory bandwidth percentage user can request 352 * @bw_gran: Granularity at which the memory bandwidth is allocated 353 * @delay_linear: True if memory B/W delay is in linear scale 354 * @mba_sc: True if MBA software controller(mba_sc) is enabled 355 * @mb_map: Mapping of memory B/W percentage to memory B/W delay 356 */ 357 struct rdt_membw { 358 u32 max_delay; 359 u32 min_bw; 360 u32 bw_gran; 361 u32 delay_linear; 362 bool mba_sc; 363 u32 *mb_map; 364 }; 365 366 static inline bool is_llc_occupancy_enabled(void) 367 { 368 return (rdt_mon_features & (1 << QOS_L3_OCCUP_EVENT_ID)); 369 } 370 371 static inline bool is_mbm_total_enabled(void) 372 { 373 return (rdt_mon_features & (1 << QOS_L3_MBM_TOTAL_EVENT_ID)); 374 } 375 376 static inline bool is_mbm_local_enabled(void) 377 { 378 return (rdt_mon_features & (1 << QOS_L3_MBM_LOCAL_EVENT_ID)); 379 } 380 381 static inline bool is_mbm_enabled(void) 382 { 383 return (is_mbm_total_enabled() || is_mbm_local_enabled()); 384 } 385 386 static inline bool is_mbm_event(int e) 387 { 388 return (e >= QOS_L3_MBM_TOTAL_EVENT_ID && 389 e <= QOS_L3_MBM_LOCAL_EVENT_ID); 390 } 391 392 struct rdt_parse_data { 393 struct rdtgroup *rdtgrp; 394 char *buf; 395 }; 396 397 /** 398 * struct rdt_resource - attributes of an RDT resource 399 * @rid: The index of the resource 400 * @alloc_enabled: Is allocation enabled on this machine 401 * @mon_enabled: Is monitoring enabled for this feature 402 * @alloc_capable: Is allocation available on this machine 403 * @mon_capable: Is monitor feature available on this machine 404 * @name: Name to use in "schemata" file 405 * @num_closid: Number of CLOSIDs available 406 * @cache_level: Which cache level defines scope of this resource 407 * @default_ctrl: Specifies default cache cbm or memory B/W percent. 408 * @msr_base: Base MSR address for CBMs 409 * @msr_update: Function pointer to update QOS MSRs 410 * @data_width: Character width of data when displaying 411 * @domains: All domains for this resource 412 * @cache: Cache allocation related data 413 * @format_str: Per resource format string to show domain value 414 * @parse_ctrlval: Per resource function pointer to parse control values 415 * @cbm_validate Cache bitmask validate function 416 * @evt_list: List of monitoring events 417 * @num_rmid: Number of RMIDs available 418 * @mon_scale: cqm counter * mon_scale = occupancy in bytes 419 * @fflags: flags to choose base and info files 420 */ 421 struct rdt_resource { 422 int rid; 423 bool alloc_enabled; 424 bool mon_enabled; 425 bool alloc_capable; 426 bool mon_capable; 427 char *name; 428 int num_closid; 429 int cache_level; 430 u32 default_ctrl; 431 unsigned int msr_base; 432 void (*msr_update) (struct rdt_domain *d, struct msr_param *m, 433 struct rdt_resource *r); 434 int data_width; 435 struct list_head domains; 436 struct rdt_cache cache; 437 struct rdt_membw membw; 438 const char *format_str; 439 int (*parse_ctrlval)(struct rdt_parse_data *data, 440 struct rdt_resource *r, 441 struct rdt_domain *d); 442 bool (*cbm_validate)(char *buf, u32 *data, struct rdt_resource *r); 443 struct list_head evt_list; 444 int num_rmid; 445 unsigned int mon_scale; 446 unsigned long fflags; 447 }; 448 449 int parse_cbm(struct rdt_parse_data *data, struct rdt_resource *r, 450 struct rdt_domain *d); 451 int parse_bw_intel(struct rdt_parse_data *data, struct rdt_resource *r, 452 struct rdt_domain *d); 453 int parse_bw_amd(struct rdt_parse_data *data, struct rdt_resource *r, 454 struct rdt_domain *d); 455 456 extern struct mutex rdtgroup_mutex; 457 458 extern struct rdt_resource rdt_resources_all[]; 459 extern struct rdtgroup rdtgroup_default; 460 DECLARE_STATIC_KEY_FALSE(rdt_alloc_enable_key); 461 462 extern struct dentry *debugfs_resctrl; 463 464 enum { 465 RDT_RESOURCE_L3, 466 RDT_RESOURCE_L3DATA, 467 RDT_RESOURCE_L3CODE, 468 RDT_RESOURCE_L2, 469 RDT_RESOURCE_L2DATA, 470 RDT_RESOURCE_L2CODE, 471 RDT_RESOURCE_MBA, 472 473 /* Must be the last */ 474 RDT_NUM_RESOURCES, 475 }; 476 477 #define for_each_rdt_resource(r) \ 478 for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\ 479 r++) 480 481 #define for_each_capable_rdt_resource(r) \ 482 for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\ 483 r++) \ 484 if (r->alloc_capable || r->mon_capable) 485 486 #define for_each_alloc_capable_rdt_resource(r) \ 487 for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\ 488 r++) \ 489 if (r->alloc_capable) 490 491 #define for_each_mon_capable_rdt_resource(r) \ 492 for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\ 493 r++) \ 494 if (r->mon_capable) 495 496 #define for_each_alloc_enabled_rdt_resource(r) \ 497 for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\ 498 r++) \ 499 if (r->alloc_enabled) 500 501 #define for_each_mon_enabled_rdt_resource(r) \ 502 for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\ 503 r++) \ 504 if (r->mon_enabled) 505 506 /* CPUID.(EAX=10H, ECX=ResID=1).EAX */ 507 union cpuid_0x10_1_eax { 508 struct { 509 unsigned int cbm_len:5; 510 } split; 511 unsigned int full; 512 }; 513 514 /* CPUID.(EAX=10H, ECX=ResID=3).EAX */ 515 union cpuid_0x10_3_eax { 516 struct { 517 unsigned int max_delay:12; 518 } split; 519 unsigned int full; 520 }; 521 522 /* CPUID.(EAX=10H, ECX=ResID).EDX */ 523 union cpuid_0x10_x_edx { 524 struct { 525 unsigned int cos_max:16; 526 } split; 527 unsigned int full; 528 }; 529 530 void rdt_last_cmd_clear(void); 531 void rdt_last_cmd_puts(const char *s); 532 void rdt_last_cmd_printf(const char *fmt, ...); 533 534 void rdt_ctrl_update(void *arg); 535 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn); 536 void rdtgroup_kn_unlock(struct kernfs_node *kn); 537 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name); 538 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name, 539 umode_t mask); 540 struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id, 541 struct list_head **pos); 542 ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of, 543 char *buf, size_t nbytes, loff_t off); 544 int rdtgroup_schemata_show(struct kernfs_open_file *of, 545 struct seq_file *s, void *v); 546 bool rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d, 547 unsigned long cbm, int closid, bool exclusive); 548 unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, struct rdt_domain *d, 549 unsigned long cbm); 550 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid); 551 int rdtgroup_tasks_assigned(struct rdtgroup *r); 552 int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp); 553 int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp); 554 bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain *d, unsigned long cbm); 555 bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_domain *d); 556 int rdt_pseudo_lock_init(void); 557 void rdt_pseudo_lock_release(void); 558 int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp); 559 void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp); 560 struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r); 561 int update_domains(struct rdt_resource *r, int closid); 562 int closids_supported(void); 563 void closid_free(int closid); 564 int alloc_rmid(void); 565 void free_rmid(u32 rmid); 566 int rdt_get_mon_l3_config(struct rdt_resource *r); 567 void mon_event_count(void *info); 568 int rdtgroup_mondata_show(struct seq_file *m, void *arg); 569 void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, 570 unsigned int dom_id); 571 void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, 572 struct rdt_domain *d); 573 void mon_event_read(struct rmid_read *rr, struct rdt_domain *d, 574 struct rdtgroup *rdtgrp, int evtid, int first); 575 void mbm_setup_overflow_handler(struct rdt_domain *dom, 576 unsigned long delay_ms); 577 void mbm_handle_overflow(struct work_struct *work); 578 bool is_mba_sc(struct rdt_resource *r); 579 void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm); 580 u32 delay_bw_map(unsigned long bw, struct rdt_resource *r); 581 void cqm_setup_limbo_handler(struct rdt_domain *dom, unsigned long delay_ms); 582 void cqm_handle_limbo(struct work_struct *work); 583 bool has_busy_rmid(struct rdt_resource *r, struct rdt_domain *d); 584 void __check_limbo(struct rdt_domain *d, bool force_free); 585 bool cbm_validate_intel(char *buf, u32 *data, struct rdt_resource *r); 586 bool cbm_validate_amd(char *buf, u32 *data, struct rdt_resource *r); 587 588 #endif /* _ASM_X86_RESCTRL_INTERNAL_H */ 589