blob: b4bbf8b21512f9423bbe557ad3255c39650565af [file] [log] [blame]
Vikas Shivappa05830202017-07-25 14:14:23 -07001#ifndef _ASM_X86_INTEL_RDT_SCHED_H
2#define _ASM_X86_INTEL_RDT_SCHED_H
3
4#ifdef CONFIG_INTEL_RDT
5
6#include <linux/sched.h>
7#include <linux/jump_label.h>
8
9#define IA32_PQR_ASSOC 0x0c8f
10
11/**
12 * struct intel_pqr_state - State cache for the PQR MSR
Vikas Shivappaa9110b52017-08-09 11:46:34 -070013 * @cur_rmid: The cached Resource Monitoring ID
14 * @cur_closid: The cached Class Of Service ID
15 * @default_rmid: The user assigned Resource Monitoring ID
16 * @default_closid: The user assigned cached Class Of Service ID
Vikas Shivappa05830202017-07-25 14:14:23 -070017 *
18 * The upper 32 bits of IA32_PQR_ASSOC contain closid and the
19 * lower 10 bits rmid. The update to IA32_PQR_ASSOC always
Vikas Shivappa748b6b82017-07-25 14:14:43 -070020 * contains both parts, so we need to cache them. This also
21 * stores the user configured per cpu CLOSID and RMID.
Vikas Shivappa05830202017-07-25 14:14:23 -070022 *
23 * The cache also helps to avoid pointless updates if the value does
24 * not change.
25 */
26struct intel_pqr_state {
Vikas Shivappaa9110b52017-08-09 11:46:34 -070027 u32 cur_rmid;
28 u32 cur_closid;
29 u32 default_rmid;
30 u32 default_closid;
Vikas Shivappa05830202017-07-25 14:14:23 -070031};
32
33DECLARE_PER_CPU(struct intel_pqr_state, pqr_state);
Vikas Shivappa4be6c072017-07-25 14:14:42 -070034
35DECLARE_STATIC_KEY_FALSE(rdt_enable_key);
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -070036DECLARE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
Vikas Shivappa748b6b82017-07-25 14:14:43 -070037DECLARE_STATIC_KEY_FALSE(rdt_mon_enable_key);
Vikas Shivappa05830202017-07-25 14:14:23 -070038
39/*
Vikas Shivappa748b6b82017-07-25 14:14:43 -070040 * __intel_rdt_sched_in() - Writes the task's CLOSid/RMID to IA32_PQR_MSR
Vikas Shivappa05830202017-07-25 14:14:23 -070041 *
42 * Following considerations are made so that this has minimal impact
43 * on scheduler hot path:
44 * - This will stay as no-op unless we are running on an Intel SKU
Vikas Shivappa748b6b82017-07-25 14:14:43 -070045 * which supports resource control or monitoring and we enable by
46 * mounting the resctrl file system.
47 * - Caches the per cpu CLOSid/RMID values and does the MSR write only
48 * when a task with a different CLOSid/RMID is scheduled in.
49 * - We allocate RMIDs/CLOSids globally in order to keep this as
50 * simple as possible.
Vikas Shivappa05830202017-07-25 14:14:23 -070051 * Must be called with preemption disabled.
52 */
Vikas Shivappa748b6b82017-07-25 14:14:43 -070053static void __intel_rdt_sched_in(void)
Vikas Shivappa05830202017-07-25 14:14:23 -070054{
Vikas Shivappaa9110b52017-08-09 11:46:34 -070055 struct intel_pqr_state *state = this_cpu_ptr(&pqr_state);
56 u32 closid = state->default_closid;
57 u32 rmid = state->default_rmid;
Vikas Shivappa748b6b82017-07-25 14:14:43 -070058
59 /*
60 * If this task has a closid/rmid assigned, use it.
61 * Else use the closid/rmid assigned to this cpu.
62 */
Vikas Shivappa1b5c0b72017-07-25 14:14:25 -070063 if (static_branch_likely(&rdt_alloc_enable_key)) {
Vikas Shivappa748b6b82017-07-25 14:14:43 -070064 if (current->closid)
Vikas Shivappaa9110b52017-08-09 11:46:34 -070065 closid = current->closid;
Vikas Shivappa748b6b82017-07-25 14:14:43 -070066 }
Vikas Shivappa05830202017-07-25 14:14:23 -070067
Vikas Shivappa748b6b82017-07-25 14:14:43 -070068 if (static_branch_likely(&rdt_mon_enable_key)) {
69 if (current->rmid)
Vikas Shivappaa9110b52017-08-09 11:46:34 -070070 rmid = current->rmid;
Vikas Shivappa748b6b82017-07-25 14:14:43 -070071 }
Vikas Shivappa05830202017-07-25 14:14:23 -070072
Vikas Shivappaa9110b52017-08-09 11:46:34 -070073 if (closid != state->cur_closid || rmid != state->cur_rmid) {
74 state->cur_closid = closid;
75 state->cur_rmid = rmid;
76 wrmsr(IA32_PQR_ASSOC, rmid, closid);
Vikas Shivappa05830202017-07-25 14:14:23 -070077 }
78}
79
Vikas Shivappa4be6c072017-07-25 14:14:42 -070080static inline void intel_rdt_sched_in(void)
81{
82 if (static_branch_likely(&rdt_enable_key))
83 __intel_rdt_sched_in();
84}
85
Vikas Shivappa05830202017-07-25 14:14:23 -070086#else
87
88static inline void intel_rdt_sched_in(void) {}
89
90#endif /* CONFIG_INTEL_RDT */
91
92#endif /* _ASM_X86_INTEL_RDT_SCHED_H */