intel_rdt.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_INTEL_RDT_H
  3. #define _ASM_X86_INTEL_RDT_H
  4. #include <linux/sched.h>
  5. #include <linux/kernfs.h>
  6. #include <linux/jump_label.h>
  7. #define IA32_L3_QOS_CFG 0xc81
  8. #define IA32_L2_QOS_CFG 0xc82
  9. #define IA32_L3_CBM_BASE 0xc90
  10. #define IA32_L2_CBM_BASE 0xd10
  11. #define IA32_MBA_THRTL_BASE 0xd50
  12. #define L3_QOS_CDP_ENABLE 0x01ULL
  13. #define L2_QOS_CDP_ENABLE 0x01ULL
  14. /*
  15. * Event IDs are used to program IA32_QM_EVTSEL before reading event
  16. * counter from IA32_QM_CTR
  17. */
  18. #define QOS_L3_OCCUP_EVENT_ID 0x01
  19. #define QOS_L3_MBM_TOTAL_EVENT_ID 0x02
  20. #define QOS_L3_MBM_LOCAL_EVENT_ID 0x03
  21. #define CQM_LIMBOCHECK_INTERVAL 1000
  22. #define MBM_CNTR_WIDTH 24
  23. #define MBM_OVERFLOW_INTERVAL 1000
  24. #define MAX_MBA_BW 100u
  25. #define RMID_VAL_ERROR BIT_ULL(63)
  26. #define RMID_VAL_UNAVAIL BIT_ULL(62)
  27. DECLARE_STATIC_KEY_FALSE(rdt_enable_key);
  28. /**
  29. * struct mon_evt - Entry in the event list of a resource
  30. * @evtid: event id
  31. * @name: name of the event
  32. */
  33. struct mon_evt {
  34. u32 evtid;
  35. char *name;
  36. struct list_head list;
  37. };
  38. /**
  39. * struct mon_data_bits - Monitoring details for each event file
  40. * @rid: Resource id associated with the event file.
  41. * @evtid: Event id associated with the event file
  42. * @domid: The domain to which the event file belongs
  43. */
  44. union mon_data_bits {
  45. void *priv;
  46. struct {
  47. unsigned int rid : 10;
  48. unsigned int evtid : 8;
  49. unsigned int domid : 14;
  50. } u;
  51. };
  52. struct rmid_read {
  53. struct rdtgroup *rgrp;
  54. struct rdt_domain *d;
  55. int evtid;
  56. bool first;
  57. u64 val;
  58. };
  59. extern unsigned int intel_cqm_threshold;
  60. extern bool rdt_alloc_capable;
  61. extern bool rdt_mon_capable;
  62. extern unsigned int rdt_mon_features;
  63. enum rdt_group_type {
  64. RDTCTRL_GROUP = 0,
  65. RDTMON_GROUP,
  66. RDT_NUM_GROUP,
  67. };
  68. /**
  69. * struct mongroup - store mon group's data in resctrl fs.
  70. * @mon_data_kn kernlfs node for the mon_data directory
  71. * @parent: parent rdtgrp
  72. * @crdtgrp_list: child rdtgroup node list
  73. * @rmid: rmid for this rdtgroup
  74. */
  75. struct mongroup {
  76. struct kernfs_node *mon_data_kn;
  77. struct rdtgroup *parent;
  78. struct list_head crdtgrp_list;
  79. u32 rmid;
  80. };
  81. /**
  82. * struct rdtgroup - store rdtgroup's data in resctrl file system.
  83. * @kn: kernfs node
  84. * @rdtgroup_list: linked list for all rdtgroups
  85. * @closid: closid for this rdtgroup
  86. * @cpu_mask: CPUs assigned to this rdtgroup
  87. * @flags: status bits
  88. * @waitcount: how many cpus expect to find this
  89. * group when they acquire rdtgroup_mutex
  90. * @type: indicates type of this rdtgroup - either
  91. * monitor only or ctrl_mon group
  92. * @mon: mongroup related data
  93. */
  94. struct rdtgroup {
  95. struct kernfs_node *kn;
  96. struct list_head rdtgroup_list;
  97. u32 closid;
  98. struct cpumask cpu_mask;
  99. int flags;
  100. atomic_t waitcount;
  101. enum rdt_group_type type;
  102. struct mongroup mon;
  103. };
  104. /* rdtgroup.flags */
  105. #define RDT_DELETED 1
  106. /* rftype.flags */
  107. #define RFTYPE_FLAGS_CPUS_LIST 1
  108. /*
  109. * Define the file type flags for base and info directories.
  110. */
  111. #define RFTYPE_INFO BIT(0)
  112. #define RFTYPE_BASE BIT(1)
  113. #define RF_CTRLSHIFT 4
  114. #define RF_MONSHIFT 5
  115. #define RF_TOPSHIFT 6
  116. #define RFTYPE_CTRL BIT(RF_CTRLSHIFT)
  117. #define RFTYPE_MON BIT(RF_MONSHIFT)
  118. #define RFTYPE_TOP BIT(RF_TOPSHIFT)
  119. #define RFTYPE_RES_CACHE BIT(8)
  120. #define RFTYPE_RES_MB BIT(9)
  121. #define RF_CTRL_INFO (RFTYPE_INFO | RFTYPE_CTRL)
  122. #define RF_MON_INFO (RFTYPE_INFO | RFTYPE_MON)
  123. #define RF_TOP_INFO (RFTYPE_INFO | RFTYPE_TOP)
  124. #define RF_CTRL_BASE (RFTYPE_BASE | RFTYPE_CTRL)
  125. /* List of all resource groups */
  126. extern struct list_head rdt_all_groups;
  127. extern int max_name_width, max_data_width;
  128. int __init rdtgroup_init(void);
  129. /**
  130. * struct rftype - describe each file in the resctrl file system
  131. * @name: File name
  132. * @mode: Access mode
  133. * @kf_ops: File operations
  134. * @flags: File specific RFTYPE_FLAGS_* flags
  135. * @fflags: File specific RF_* or RFTYPE_* flags
  136. * @seq_show: Show content of the file
  137. * @write: Write to the file
  138. */
  139. struct rftype {
  140. char *name;
  141. umode_t mode;
  142. struct kernfs_ops *kf_ops;
  143. unsigned long flags;
  144. unsigned long fflags;
  145. int (*seq_show)(struct kernfs_open_file *of,
  146. struct seq_file *sf, void *v);
  147. /*
  148. * write() is the generic write callback which maps directly to
  149. * kernfs write operation and overrides all other operations.
  150. * Maximum write size is determined by ->max_write_len.
  151. */
  152. ssize_t (*write)(struct kernfs_open_file *of,
  153. char *buf, size_t nbytes, loff_t off);
  154. };
  155. /**
  156. * struct mbm_state - status for each MBM counter in each domain
  157. * @chunks: Total data moved (multiply by rdt_group.mon_scale to get bytes)
  158. * @prev_msr Value of IA32_QM_CTR for this RMID last time we read it
  159. * @chunks_bw Total local data moved. Used for bandwidth calculation
  160. * @prev_bw_msr:Value of previous IA32_QM_CTR for bandwidth counting
  161. * @prev_bw The most recent bandwidth in MBps
  162. * @delta_bw Difference between the current and previous bandwidth
  163. * @delta_comp Indicates whether to compute the delta_bw
  164. */
  165. struct mbm_state {
  166. u64 chunks;
  167. u64 prev_msr;
  168. u64 chunks_bw;
  169. u64 prev_bw_msr;
  170. u32 prev_bw;
  171. u32 delta_bw;
  172. bool delta_comp;
  173. };
  174. /**
  175. * struct rdt_domain - group of cpus sharing an RDT resource
  176. * @list: all instances of this resource
  177. * @id: unique id for this instance
  178. * @cpu_mask: which cpus share this resource
  179. * @rmid_busy_llc:
  180. * bitmap of which limbo RMIDs are above threshold
  181. * @mbm_total: saved state for MBM total bandwidth
  182. * @mbm_local: saved state for MBM local bandwidth
  183. * @mbm_over: worker to periodically read MBM h/w counters
  184. * @cqm_limbo: worker to periodically read CQM h/w counters
  185. * @mbm_work_cpu:
  186. * worker cpu for MBM h/w counters
  187. * @cqm_work_cpu:
  188. * worker cpu for CQM h/w counters
  189. * @ctrl_val: array of cache or mem ctrl values (indexed by CLOSID)
  190. * @mbps_val: When mba_sc is enabled, this holds the bandwidth in MBps
  191. * @new_ctrl: new ctrl value to be loaded
  192. * @have_new_ctrl: did user provide new_ctrl for this domain
  193. */
  194. struct rdt_domain {
  195. struct list_head list;
  196. int id;
  197. struct cpumask cpu_mask;
  198. unsigned long *rmid_busy_llc;
  199. struct mbm_state *mbm_total;
  200. struct mbm_state *mbm_local;
  201. struct delayed_work mbm_over;
  202. struct delayed_work cqm_limbo;
  203. int mbm_work_cpu;
  204. int cqm_work_cpu;
  205. u32 *ctrl_val;
  206. u32 *mbps_val;
  207. u32 new_ctrl;
  208. bool have_new_ctrl;
  209. };
  210. /**
  211. * struct msr_param - set a range of MSRs from a domain
  212. * @res: The resource to use
  213. * @low: Beginning index from base MSR
  214. * @high: End index
  215. */
  216. struct msr_param {
  217. struct rdt_resource *res;
  218. int low;
  219. int high;
  220. };
  221. /**
  222. * struct rdt_cache - Cache allocation related data
  223. * @cbm_len: Length of the cache bit mask
  224. * @min_cbm_bits: Minimum number of consecutive bits to be set
  225. * @cbm_idx_mult: Multiplier of CBM index
  226. * @cbm_idx_offset: Offset of CBM index. CBM index is computed by:
  227. * closid * cbm_idx_multi + cbm_idx_offset
  228. * in a cache bit mask
  229. * @shareable_bits: Bitmask of shareable resource with other
  230. * executing entities
  231. */
  232. struct rdt_cache {
  233. unsigned int cbm_len;
  234. unsigned int min_cbm_bits;
  235. unsigned int cbm_idx_mult;
  236. unsigned int cbm_idx_offset;
  237. unsigned int shareable_bits;
  238. };
  239. /**
  240. * struct rdt_membw - Memory bandwidth allocation related data
  241. * @max_delay: Max throttle delay. Delay is the hardware
  242. * representation for memory bandwidth.
  243. * @min_bw: Minimum memory bandwidth percentage user can request
  244. * @bw_gran: Granularity at which the memory bandwidth is allocated
  245. * @delay_linear: True if memory B/W delay is in linear scale
  246. * @mba_sc: True if MBA software controller(mba_sc) is enabled
  247. * @mb_map: Mapping of memory B/W percentage to memory B/W delay
  248. */
  249. struct rdt_membw {
  250. u32 max_delay;
  251. u32 min_bw;
  252. u32 bw_gran;
  253. u32 delay_linear;
  254. bool mba_sc;
  255. u32 *mb_map;
  256. };
  257. static inline bool is_llc_occupancy_enabled(void)
  258. {
  259. return (rdt_mon_features & (1 << QOS_L3_OCCUP_EVENT_ID));
  260. }
  261. static inline bool is_mbm_total_enabled(void)
  262. {
  263. return (rdt_mon_features & (1 << QOS_L3_MBM_TOTAL_EVENT_ID));
  264. }
  265. static inline bool is_mbm_local_enabled(void)
  266. {
  267. return (rdt_mon_features & (1 << QOS_L3_MBM_LOCAL_EVENT_ID));
  268. }
  269. static inline bool is_mbm_enabled(void)
  270. {
  271. return (is_mbm_total_enabled() || is_mbm_local_enabled());
  272. }
  273. static inline bool is_mbm_event(int e)
  274. {
  275. return (e >= QOS_L3_MBM_TOTAL_EVENT_ID &&
  276. e <= QOS_L3_MBM_LOCAL_EVENT_ID);
  277. }
  278. /**
  279. * struct rdt_resource - attributes of an RDT resource
  280. * @rid: The index of the resource
  281. * @alloc_enabled: Is allocation enabled on this machine
  282. * @mon_enabled: Is monitoring enabled for this feature
  283. * @alloc_capable: Is allocation available on this machine
  284. * @mon_capable: Is monitor feature available on this machine
  285. * @name: Name to use in "schemata" file
  286. * @num_closid: Number of CLOSIDs available
  287. * @cache_level: Which cache level defines scope of this resource
  288. * @default_ctrl: Specifies default cache cbm or memory B/W percent.
  289. * @msr_base: Base MSR address for CBMs
  290. * @msr_update: Function pointer to update QOS MSRs
  291. * @data_width: Character width of data when displaying
  292. * @domains: All domains for this resource
  293. * @cache: Cache allocation related data
  294. * @format_str: Per resource format string to show domain value
  295. * @parse_ctrlval: Per resource function pointer to parse control values
  296. * @evt_list: List of monitoring events
  297. * @num_rmid: Number of RMIDs available
  298. * @mon_scale: cqm counter * mon_scale = occupancy in bytes
  299. * @fflags: flags to choose base and info files
  300. */
  301. struct rdt_resource {
  302. int rid;
  303. bool alloc_enabled;
  304. bool mon_enabled;
  305. bool alloc_capable;
  306. bool mon_capable;
  307. char *name;
  308. int num_closid;
  309. int cache_level;
  310. u32 default_ctrl;
  311. unsigned int msr_base;
  312. void (*msr_update) (struct rdt_domain *d, struct msr_param *m,
  313. struct rdt_resource *r);
  314. int data_width;
  315. struct list_head domains;
  316. struct rdt_cache cache;
  317. struct rdt_membw membw;
  318. const char *format_str;
  319. int (*parse_ctrlval) (char *buf, struct rdt_resource *r,
  320. struct rdt_domain *d);
  321. struct list_head evt_list;
  322. int num_rmid;
  323. unsigned int mon_scale;
  324. unsigned long fflags;
  325. };
  326. int parse_cbm(char *buf, struct rdt_resource *r, struct rdt_domain *d);
  327. int parse_bw(char *buf, struct rdt_resource *r, struct rdt_domain *d);
  328. extern struct mutex rdtgroup_mutex;
  329. extern struct rdt_resource rdt_resources_all[];
  330. extern struct rdtgroup rdtgroup_default;
  331. DECLARE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
  332. int __init rdtgroup_init(void);
  333. enum {
  334. RDT_RESOURCE_L3,
  335. RDT_RESOURCE_L3DATA,
  336. RDT_RESOURCE_L3CODE,
  337. RDT_RESOURCE_L2,
  338. RDT_RESOURCE_L2DATA,
  339. RDT_RESOURCE_L2CODE,
  340. RDT_RESOURCE_MBA,
  341. /* Must be the last */
  342. RDT_NUM_RESOURCES,
  343. };
  344. #define for_each_capable_rdt_resource(r) \
  345. for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\
  346. r++) \
  347. if (r->alloc_capable || r->mon_capable)
  348. #define for_each_alloc_capable_rdt_resource(r) \
  349. for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\
  350. r++) \
  351. if (r->alloc_capable)
  352. #define for_each_mon_capable_rdt_resource(r) \
  353. for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\
  354. r++) \
  355. if (r->mon_capable)
  356. #define for_each_alloc_enabled_rdt_resource(r) \
  357. for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\
  358. r++) \
  359. if (r->alloc_enabled)
  360. #define for_each_mon_enabled_rdt_resource(r) \
  361. for (r = rdt_resources_all; r < rdt_resources_all + RDT_NUM_RESOURCES;\
  362. r++) \
  363. if (r->mon_enabled)
  364. /* CPUID.(EAX=10H, ECX=ResID=1).EAX */
  365. union cpuid_0x10_1_eax {
  366. struct {
  367. unsigned int cbm_len:5;
  368. } split;
  369. unsigned int full;
  370. };
  371. /* CPUID.(EAX=10H, ECX=ResID=3).EAX */
  372. union cpuid_0x10_3_eax {
  373. struct {
  374. unsigned int max_delay:12;
  375. } split;
  376. unsigned int full;
  377. };
  378. /* CPUID.(EAX=10H, ECX=ResID).EDX */
  379. union cpuid_0x10_x_edx {
  380. struct {
  381. unsigned int cos_max:16;
  382. } split;
  383. unsigned int full;
  384. };
  385. void rdt_last_cmd_clear(void);
  386. void rdt_last_cmd_puts(const char *s);
  387. void rdt_last_cmd_printf(const char *fmt, ...);
  388. void rdt_ctrl_update(void *arg);
  389. struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn);
  390. void rdtgroup_kn_unlock(struct kernfs_node *kn);
  391. struct rdt_domain *rdt_find_domain(struct rdt_resource *r, int id,
  392. struct list_head **pos);
  393. ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
  394. char *buf, size_t nbytes, loff_t off);
  395. int rdtgroup_schemata_show(struct kernfs_open_file *of,
  396. struct seq_file *s, void *v);
  397. struct rdt_domain *get_domain_from_cpu(int cpu, struct rdt_resource *r);
  398. int alloc_rmid(void);
  399. void free_rmid(u32 rmid);
  400. int rdt_get_mon_l3_config(struct rdt_resource *r);
  401. void mon_event_count(void *info);
  402. int rdtgroup_mondata_show(struct seq_file *m, void *arg);
  403. void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
  404. unsigned int dom_id);
  405. void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
  406. struct rdt_domain *d);
  407. void mon_event_read(struct rmid_read *rr, struct rdt_domain *d,
  408. struct rdtgroup *rdtgrp, int evtid, int first);
  409. void mbm_setup_overflow_handler(struct rdt_domain *dom,
  410. unsigned long delay_ms);
  411. void mbm_handle_overflow(struct work_struct *work);
  412. bool is_mba_sc(struct rdt_resource *r);
  413. void setup_default_ctrlval(struct rdt_resource *r, u32 *dc, u32 *dm);
  414. u32 delay_bw_map(unsigned long bw, struct rdt_resource *r);
  415. void cqm_setup_limbo_handler(struct rdt_domain *dom, unsigned long delay_ms);
  416. void cqm_handle_limbo(struct work_struct *work);
  417. bool has_busy_rmid(struct rdt_resource *r, struct rdt_domain *d);
  418. void __check_limbo(struct rdt_domain *d, bool force_free);
  419. #endif /* _ASM_X86_INTEL_RDT_H */