internal.h 16 KB

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