1e79f15a4SChen Yu /* SPDX-License-Identifier: GPL-2.0 */ 2e79f15a4SChen Yu #ifndef _RESCTRL_H 3e79f15a4SChen Yu #define _RESCTRL_H 4e79f15a4SChen Yu 5328ea688STony Luck #include <linux/cacheinfo.h> 663c8b123SJames Morse #include <linux/kernel.h> 763c8b123SJames Morse #include <linux/list.h> 8a21a4391SJames Morse #include <linux/pid.h> 9a21a4391SJames Morse 1040fc735bSJames Morse /* CLOSID, RMID value used by the default control group */ 1140fc735bSJames Morse #define RESCTRL_RESERVED_CLOSID 0 1240fc735bSJames Morse #define RESCTRL_RESERVED_RMID 0 1340fc735bSJames Morse 14978fcca9SJames Morse #define RESCTRL_PICK_ANY_CPU -1 15978fcca9SJames Morse 16e79f15a4SChen Yu #ifdef CONFIG_PROC_CPU_RESCTRL 17e79f15a4SChen Yu 18e79f15a4SChen Yu int proc_resctrl_show(struct seq_file *m, 19e79f15a4SChen Yu struct pid_namespace *ns, 20e79f15a4SChen Yu struct pid *pid, 21e79f15a4SChen Yu struct task_struct *tsk); 22e79f15a4SChen Yu 23e79f15a4SChen Yu #endif 24e79f15a4SChen Yu 25781096d9SJames Morse /* max value for struct rdt_domain's mbps_val */ 26781096d9SJames Morse #define MBA_MAX_MBPS U32_MAX 27781096d9SJames Morse 28792e0f6fSJames Morse /** 29208ab168SJames Morse * enum resctrl_conf_type - The type of configuration. 30208ab168SJames Morse * @CDP_NONE: No prioritisation, both code and data are controlled or monitored. 31208ab168SJames Morse * @CDP_CODE: Configuration applies to instruction fetches. 32208ab168SJames Morse * @CDP_DATA: Configuration applies to reads and writes. 33208ab168SJames Morse */ 34208ab168SJames Morse enum resctrl_conf_type { 35208ab168SJames Morse CDP_NONE, 36208ab168SJames Morse CDP_CODE, 37208ab168SJames Morse CDP_DATA, 38208ab168SJames Morse }; 39208ab168SJames Morse 403c021531SJames Morse enum resctrl_res_level { 413c021531SJames Morse RDT_RESOURCE_L3, 423c021531SJames Morse RDT_RESOURCE_L2, 433c021531SJames Morse RDT_RESOURCE_MBA, 443c021531SJames Morse RDT_RESOURCE_SMBA, 453c021531SJames Morse 463c021531SJames Morse /* Must be the last */ 473c021531SJames Morse RDT_NUM_RESOURCES, 483c021531SJames Morse }; 493c021531SJames Morse 5075408e43SJames Morse #define CDP_NUM_TYPES (CDP_DATA + 1) 5175408e43SJames Morse 52fea62d37SJames Morse /* 53fea62d37SJames Morse * Event IDs, the values match those used to program IA32_QM_EVTSEL before 54fea62d37SJames Morse * reading IA32_QM_CTR on RDT systems. 55fea62d37SJames Morse */ 56fea62d37SJames Morse enum resctrl_event_id { 57fea62d37SJames Morse QOS_L3_OCCUP_EVENT_ID = 0x01, 58fea62d37SJames Morse QOS_L3_MBM_TOTAL_EVENT_ID = 0x02, 59fea62d37SJames Morse QOS_L3_MBM_LOCAL_EVENT_ID = 0x03, 60fea62d37SJames Morse }; 61fea62d37SJames Morse 62208ab168SJames Morse /** 63e8f72825SJames Morse * struct resctrl_staged_config - parsed configuration to be applied 64e8f72825SJames Morse * @new_ctrl: new ctrl value to be loaded 65e8f72825SJames Morse * @have_new_ctrl: whether the user provided new_ctrl is valid 66e8f72825SJames Morse */ 67e8f72825SJames Morse struct resctrl_staged_config { 68e8f72825SJames Morse u32 new_ctrl; 69e8f72825SJames Morse bool have_new_ctrl; 70e8f72825SJames Morse }; 71e8f72825SJames Morse 72cd84f72bSTony Luck enum resctrl_domain_type { 73cd84f72bSTony Luck RESCTRL_CTRL_DOMAIN, 74cd84f72bSTony Luck RESCTRL_MON_DOMAIN, 75cd84f72bSTony Luck }; 76cd84f72bSTony Luck 77e8f72825SJames Morse /** 78c103d4d4STony Luck * struct rdt_domain_hdr - common header for different domain types 79792e0f6fSJames Morse * @list: all instances of this resource 80792e0f6fSJames Morse * @id: unique id for this instance 81cd84f72bSTony Luck * @type: type of this instance 82792e0f6fSJames Morse * @cpu_mask: which CPUs share this resource 83c103d4d4STony Luck */ 84c103d4d4STony Luck struct rdt_domain_hdr { 85c103d4d4STony Luck struct list_head list; 86c103d4d4STony Luck int id; 87cd84f72bSTony Luck enum resctrl_domain_type type; 88c103d4d4STony Luck struct cpumask cpu_mask; 89c103d4d4STony Luck }; 90c103d4d4STony Luck 91c103d4d4STony Luck /** 92cae2bcb6STony Luck * struct rdt_ctrl_domain - group of CPUs sharing a resctrl control resource 93cae2bcb6STony Luck * @hdr: common header for different domain types 94cae2bcb6STony Luck * @plr: pseudo-locked region (if any) associated with domain 95cae2bcb6STony Luck * @staged_config: parsed configuration to be applied 96cae2bcb6STony Luck * @mbps_val: When mba_sc is enabled, this holds the array of user 97cae2bcb6STony Luck * specified control values for mba_sc in MBps, indexed 98cae2bcb6STony Luck * by closid 99cae2bcb6STony Luck */ 100cae2bcb6STony Luck struct rdt_ctrl_domain { 101cae2bcb6STony Luck struct rdt_domain_hdr hdr; 102cae2bcb6STony Luck struct pseudo_lock_region *plr; 103cae2bcb6STony Luck struct resctrl_staged_config staged_config[CDP_NUM_TYPES]; 104cae2bcb6STony Luck u32 *mbps_val; 105cae2bcb6STony Luck }; 106cae2bcb6STony Luck 107cae2bcb6STony Luck /** 108cae2bcb6STony Luck * struct rdt_mon_domain - group of CPUs sharing a resctrl monitor resource 109c103d4d4STony Luck * @hdr: common header for different domain types 110328ea688STony Luck * @ci: cache info for this domain 111792e0f6fSJames Morse * @rmid_busy_llc: bitmap of which limbo RMIDs are above threshold 112792e0f6fSJames Morse * @mbm_total: saved state for MBM total bandwidth 113792e0f6fSJames Morse * @mbm_local: saved state for MBM local bandwidth 114792e0f6fSJames Morse * @mbm_over: worker to periodically read MBM h/w counters 115792e0f6fSJames Morse * @cqm_limbo: worker to periodically read CQM h/w counters 116792e0f6fSJames Morse * @mbm_work_cpu: worker CPU for MBM h/w counters 117792e0f6fSJames Morse * @cqm_work_cpu: worker CPU for CQM h/w counters 118792e0f6fSJames Morse */ 119cae2bcb6STony Luck struct rdt_mon_domain { 120c103d4d4STony Luck struct rdt_domain_hdr hdr; 121328ea688STony Luck struct cacheinfo *ci; 122792e0f6fSJames Morse unsigned long *rmid_busy_llc; 123792e0f6fSJames Morse struct mbm_state *mbm_total; 124792e0f6fSJames Morse struct mbm_state *mbm_local; 125792e0f6fSJames Morse struct delayed_work mbm_over; 126792e0f6fSJames Morse struct delayed_work cqm_limbo; 127792e0f6fSJames Morse int mbm_work_cpu; 128792e0f6fSJames Morse int cqm_work_cpu; 129792e0f6fSJames Morse }; 13063c8b123SJames Morse 13163c8b123SJames Morse /** 13263c8b123SJames Morse * struct resctrl_cache - Cache allocation related data 13363c8b123SJames Morse * @cbm_len: Length of the cache bit mask 1342d4daa54SBabu Moger * @min_cbm_bits: Minimum number of consecutive bits to be set. 1352d4daa54SBabu Moger * The value 0 means the architecture can support 1362d4daa54SBabu Moger * zero CBM. 13763c8b123SJames Morse * @shareable_bits: Bitmask of shareable resource with other 13863c8b123SJames Morse * executing entities 13939c6eed1SMaciej Wieczor-Retman * @arch_has_sparse_bitmasks: True if a bitmask like f00f is valid. 14063c8b123SJames Morse * @arch_has_per_cpu_cfg: True if QOS_CFG register for this cache 14163c8b123SJames Morse * level has CPU scope. 14263c8b123SJames Morse */ 14363c8b123SJames Morse struct resctrl_cache { 14463c8b123SJames Morse unsigned int cbm_len; 14563c8b123SJames Morse unsigned int min_cbm_bits; 14663c8b123SJames Morse unsigned int shareable_bits; 14739c6eed1SMaciej Wieczor-Retman bool arch_has_sparse_bitmasks; 14863c8b123SJames Morse bool arch_has_per_cpu_cfg; 14963c8b123SJames Morse }; 15063c8b123SJames Morse 15163c8b123SJames Morse /** 15263c8b123SJames Morse * enum membw_throttle_mode - System's memory bandwidth throttling mode 15363c8b123SJames Morse * @THREAD_THROTTLE_UNDEFINED: Not relevant to the system 15463c8b123SJames Morse * @THREAD_THROTTLE_MAX: Memory bandwidth is throttled at the core 15563c8b123SJames Morse * always using smallest bandwidth percentage 15663c8b123SJames Morse * assigned to threads, aka "max throttling" 15763c8b123SJames Morse * @THREAD_THROTTLE_PER_THREAD: Memory bandwidth is throttled at the thread 15863c8b123SJames Morse */ 15963c8b123SJames Morse enum membw_throttle_mode { 16063c8b123SJames Morse THREAD_THROTTLE_UNDEFINED = 0, 16163c8b123SJames Morse THREAD_THROTTLE_MAX, 16263c8b123SJames Morse THREAD_THROTTLE_PER_THREAD, 16363c8b123SJames Morse }; 16463c8b123SJames Morse 16563c8b123SJames Morse /** 16663c8b123SJames Morse * struct resctrl_membw - Memory bandwidth allocation related data 16763c8b123SJames Morse * @min_bw: Minimum memory bandwidth percentage user can request 16863c8b123SJames Morse * @bw_gran: Granularity at which the memory bandwidth is allocated 16963c8b123SJames Morse * @delay_linear: True if memory B/W delay is in linear scale 17063c8b123SJames Morse * @arch_needs_linear: True if we can't configure non-linear resources 17163c8b123SJames Morse * @throttle_mode: Bandwidth throttling mode when threads request 17263c8b123SJames Morse * different memory bandwidths 17363c8b123SJames Morse * @mba_sc: True if MBA software controller(mba_sc) is enabled 17463c8b123SJames Morse * @mb_map: Mapping of memory B/W percentage to memory B/W delay 17563c8b123SJames Morse */ 17663c8b123SJames Morse struct resctrl_membw { 17763c8b123SJames Morse u32 min_bw; 17863c8b123SJames Morse u32 bw_gran; 17963c8b123SJames Morse u32 delay_linear; 18063c8b123SJames Morse bool arch_needs_linear; 18163c8b123SJames Morse enum membw_throttle_mode throttle_mode; 18263c8b123SJames Morse bool mba_sc; 18363c8b123SJames Morse u32 *mb_map; 18463c8b123SJames Morse }; 18563c8b123SJames Morse 1861c290682SJames Morse struct resctrl_schema; 18763c8b123SJames Morse 188f436cb69STony Luck enum resctrl_scope { 189f436cb69STony Luck RESCTRL_L2_CACHE = 2, 190f436cb69STony Luck RESCTRL_L3_CACHE = 3, 1911a171608STony Luck RESCTRL_L3_NODE, 192f436cb69STony Luck }; 193f436cb69STony Luck 19463c8b123SJames Morse /** 195c24f5eabSJames Morse * enum resctrl_schema_fmt - The format user-space provides for a schema. 196c24f5eabSJames Morse * @RESCTRL_SCHEMA_BITMAP: The schema is a bitmap in hex. 197c24f5eabSJames Morse * @RESCTRL_SCHEMA_RANGE: The schema is a decimal number. 198c24f5eabSJames Morse */ 199c24f5eabSJames Morse enum resctrl_schema_fmt { 200c24f5eabSJames Morse RESCTRL_SCHEMA_BITMAP, 201c24f5eabSJames Morse RESCTRL_SCHEMA_RANGE, 202c24f5eabSJames Morse }; 203c24f5eabSJames Morse 204c24f5eabSJames Morse /** 20563c8b123SJames Morse * struct rdt_resource - attributes of a resctrl resource 20663c8b123SJames Morse * @rid: The index of the resource 20763c8b123SJames Morse * @alloc_capable: Is allocation available on this machine 20863c8b123SJames Morse * @mon_capable: Is monitor feature available on this machine 20963c8b123SJames Morse * @num_rmid: Number of RMIDs available 210cd84f72bSTony Luck * @ctrl_scope: Scope of this resource for control functions 211cd84f72bSTony Luck * @mon_scope: Scope of this resource for monitor functions 21263c8b123SJames Morse * @cache: Cache allocation related data 21363c8b123SJames Morse * @membw: If the component has bandwidth controls, their properties. 214cd84f72bSTony Luck * @ctrl_domains: RCU list of all control domains for this resource 215cd84f72bSTony Luck * @mon_domains: RCU list of all monitor domains for this resource 21663c8b123SJames Morse * @name: Name to use in "schemata" file. 21763c8b123SJames Morse * @data_width: Character width of data when displaying 21863c8b123SJames Morse * @default_ctrl: Specifies default cache cbm or memory B/W percent. 219c24f5eabSJames Morse * @schema_fmt: Which format string and parser is used for this schema. 22063c8b123SJames Morse * @evt_list: List of monitoring events 221c091e907SJames Morse * @cdp_capable: Is the CDP feature available on this resource 22263c8b123SJames Morse */ 22363c8b123SJames Morse struct rdt_resource { 22463c8b123SJames Morse int rid; 22563c8b123SJames Morse bool alloc_capable; 22663c8b123SJames Morse bool mon_capable; 22763c8b123SJames Morse int num_rmid; 228cd84f72bSTony Luck enum resctrl_scope ctrl_scope; 229cd84f72bSTony Luck enum resctrl_scope mon_scope; 23063c8b123SJames Morse struct resctrl_cache cache; 23163c8b123SJames Morse struct resctrl_membw membw; 232cd84f72bSTony Luck struct list_head ctrl_domains; 233cd84f72bSTony Luck struct list_head mon_domains; 23463c8b123SJames Morse char *name; 23563c8b123SJames Morse int data_width; 23663c8b123SJames Morse u32 default_ctrl; 237c24f5eabSJames Morse enum resctrl_schema_fmt schema_fmt; 23863c8b123SJames Morse struct list_head evt_list; 239c091e907SJames Morse bool cdp_capable; 24063c8b123SJames Morse }; 24163c8b123SJames Morse 2423c021531SJames Morse /* 2433c021531SJames Morse * Get the resource that exists at this level. If the level is not supported 2443c021531SJames Morse * a dummy/not-capable resource can be returned. Levels >= RDT_NUM_RESOURCES 2453c021531SJames Morse * will return NULL. 2463c021531SJames Morse */ 2473c021531SJames Morse struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l); 2483c021531SJames Morse 249cdb9ebc9SJames Morse /** 250cdb9ebc9SJames Morse * struct resctrl_schema - configuration abilities of a resource presented to 251cdb9ebc9SJames Morse * user-space 252cdb9ebc9SJames Morse * @list: Member of resctrl_schema_all. 253e198fde3SJames Morse * @name: The name to use in the "schemata" file. 254*bb9343c8SJames Morse * @fmt_str: Format string to show domain value. 255208ab168SJames Morse * @conf_type: Whether this schema is specific to code/data. 256cdb9ebc9SJames Morse * @res: The resource structure exported by the architecture to describe 257cdb9ebc9SJames Morse * the hardware that is configured by this schema. 2583183e87cSJames Morse * @num_closid: The number of closid that can be used with this schema. When 2593183e87cSJames Morse * features like CDP are enabled, this will be lower than the 2603183e87cSJames Morse * hardware supports for the resource. 261cdb9ebc9SJames Morse */ 262cdb9ebc9SJames Morse struct resctrl_schema { 263cdb9ebc9SJames Morse struct list_head list; 264e198fde3SJames Morse char name[8]; 265*bb9343c8SJames Morse const char *fmt_str; 266208ab168SJames Morse enum resctrl_conf_type conf_type; 267cdb9ebc9SJames Morse struct rdt_resource *res; 268eb6f3187SJames Morse u32 num_closid; 269cdb9ebc9SJames Morse }; 270eb6f3187SJames Morse 271eb6f3187SJames Morse /* The number of closid supported by this resource regardless of CDP */ 272eb6f3187SJames Morse u32 resctrl_arch_get_num_closid(struct rdt_resource *r); 273a547a588SPeter Newman u32 resctrl_arch_system_num_rmid_idx(void); 2742e667819SJames Morse int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid); 275ff6357bbSJames Morse 276ff6357bbSJames Morse /* 277ff6357bbSJames Morse * Update the ctrl_val and apply this config right now. 278ff6357bbSJames Morse * Must be called on one of the domain's CPUs. 279ff6357bbSJames Morse */ 280cae2bcb6STony Luck int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d, 281ff6357bbSJames Morse u32 closid, enum resctrl_conf_type t, u32 cfg_val); 282ff6357bbSJames Morse 283cae2bcb6STony Luck u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d, 284111136e6SJames Morse u32 closid, enum resctrl_conf_type type); 285cae2bcb6STony Luck int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d); 286cae2bcb6STony Luck int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d); 287cae2bcb6STony Luck void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d); 288cae2bcb6STony Luck void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d); 2891b3e50ceSJames Morse void resctrl_online_cpu(unsigned int cpu); 290258c91e8SJames Morse void resctrl_offline_cpu(unsigned int cpu); 2918286618aSJames Morse 2928286618aSJames Morse /** 2938286618aSJames Morse * resctrl_arch_rmid_read() - Read the eventid counter corresponding to rmid 2948286618aSJames Morse * for this resource and domain. 2958286618aSJames Morse * @r: resource that the counter should be read from. 2968286618aSJames Morse * @d: domain that the counter should be read from. 29740fc735bSJames Morse * @closid: closid that matches the rmid. Depending on the architecture, the 29840fc735bSJames Morse * counter may match traffic of both @closid and @rmid, or @rmid 29940fc735bSJames Morse * only. 3008286618aSJames Morse * @rmid: rmid of the counter to read. 3018286618aSJames Morse * @eventid: eventid to read, e.g. L3 occupancy. 302f7b1843eSJames Morse * @val: result of the counter read in bytes. 303e557999fSJames Morse * @arch_mon_ctx: An architecture specific value from 304e557999fSJames Morse * resctrl_arch_mon_ctx_alloc(), for MPAM this identifies 305e557999fSJames Morse * the hardware monitor allocated for this read request. 3068286618aSJames Morse * 3076fde1424SJames Morse * Some architectures need to sleep when first programming some of the counters. 3086fde1424SJames Morse * (specifically: arm64's MPAM cache occupancy counters can return 'not ready' 3096fde1424SJames Morse * for a short period of time). Call from a non-migrateable process context on 3106fde1424SJames Morse * a CPU that belongs to domain @d. e.g. use smp_call_on_cpu() or 3116fde1424SJames Morse * schedule_work_on(). This function can be called with interrupts masked, 3126fde1424SJames Morse * e.g. using smp_call_function_any(), but may consistently return an error. 3138286618aSJames Morse * 3148286618aSJames Morse * Return: 3158286618aSJames Morse * 0 on success, or -EIO, -EINVAL etc on error. 3168286618aSJames Morse */ 317cae2bcb6STony Luck int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d, 31840fc735bSJames Morse u32 closid, u32 rmid, enum resctrl_event_id eventid, 319e557999fSJames Morse u64 *val, void *arch_mon_ctx); 32040fc735bSJames Morse 3216fde1424SJames Morse /** 3226fde1424SJames Morse * resctrl_arch_rmid_read_context_check() - warn about invalid contexts 3236fde1424SJames Morse * 3246fde1424SJames Morse * When built with CONFIG_DEBUG_ATOMIC_SLEEP generate a warning when 3256fde1424SJames Morse * resctrl_arch_rmid_read() is called with preemption disabled. 3266fde1424SJames Morse * 3276fde1424SJames Morse * The contract with resctrl_arch_rmid_read() is that if interrupts 3286fde1424SJames Morse * are unmasked, it can sleep. This allows NOHZ_FULL systems to use an 3296fde1424SJames Morse * IPI, (and fail if the call needed to sleep), while most of the time 3306fde1424SJames Morse * the work is scheduled, allowing the call to sleep. 3316fde1424SJames Morse */ 3326fde1424SJames Morse static inline void resctrl_arch_rmid_read_context_check(void) 3336fde1424SJames Morse { 3346fde1424SJames Morse if (!irqs_disabled()) 3356fde1424SJames Morse might_sleep(); 3366fde1424SJames Morse } 337eb6f3187SJames Morse 338fea62d37SJames Morse /** 339fea62d37SJames Morse * resctrl_arch_reset_rmid() - Reset any private state associated with rmid 340fea62d37SJames Morse * and eventid. 341fea62d37SJames Morse * @r: The domain's resource. 342fea62d37SJames Morse * @d: The rmid's domain. 34340fc735bSJames Morse * @closid: closid that matches the rmid. Depending on the architecture, the 34440fc735bSJames Morse * counter may match traffic of both @closid and @rmid, or @rmid only. 345fea62d37SJames Morse * @rmid: The rmid whose counter values should be reset. 346fea62d37SJames Morse * @eventid: The eventid whose counter values should be reset. 347fea62d37SJames Morse * 348fea62d37SJames Morse * This can be called from any CPU. 349fea62d37SJames Morse */ 350cae2bcb6STony Luck void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_mon_domain *d, 35140fc735bSJames Morse u32 closid, u32 rmid, 35240fc735bSJames Morse enum resctrl_event_id eventid); 353fea62d37SJames Morse 35492bd5a13SBabu Moger /** 35592bd5a13SBabu Moger * resctrl_arch_reset_rmid_all() - Reset all private state associated with 35692bd5a13SBabu Moger * all rmids and eventids. 35792bd5a13SBabu Moger * @r: The resctrl resource. 35892bd5a13SBabu Moger * @d: The domain for which all architectural counter state will 35992bd5a13SBabu Moger * be cleared. 36092bd5a13SBabu Moger * 36192bd5a13SBabu Moger * This can be called from any CPU. 36292bd5a13SBabu Moger */ 363cae2bcb6STony Luck void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *d); 36492bd5a13SBabu Moger 365ae2328b5SJames Morse extern unsigned int resctrl_rmid_realloc_threshold; 366d80975e2SJames Morse extern unsigned int resctrl_rmid_realloc_limit; 367ae2328b5SJames Morse 368e79f15a4SChen Yu #endif /* _RESCTRL_H */ 369