intel_rdt.h 13 KB

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