linux/arch/riscv/include/asm/qos.h
Drew Fustini 326ba66db9 riscv: Add support for srmcfg CSR from Ssqosid extension
Add support for the srmcfg CSR defined in the Ssqosid ISA extension.
The CSR contains two fields:

  - Resource Control ID (RCID) for resource allocation
  - Monitoring Counter ID (MCID) for tracking resource usage

Requests from a hart to shared resources are tagged with these IDs,
allowing resource usage to be associated with the running task.

Add a srmcfg field to thread_struct with the same format as the CSR.
The context-switch path writes the field to the CSR, and
resctrl_arch_set_closid_rmid() updates it when a task is assigned to a
resctrl control or monitoring group.

A per-cpu cpu_srmcfg_default holds the default srmcfg for each CPU, set
by resctrl_arch_set_cpu_default_closid_rmid() on CPU group assignment.
On context switch, RCID and MCID inherit from the CPU default
independently: a task whose thread RCID field is zero takes the CPU
default's RCID, and likewise for MCID.

A per-cpu cpu_srmcfg variable mirrors the CSR state to avoid
redundant writes. L1D-hot memory access is faster than a CSR read and
avoids traps under virtualization.

Link: https://github.com/riscv/riscv-ssqosid/releases/tag/v1.0
Assisted-by: Claude:claude-opus-4-7
Co-developed-by: Kornel Dulęba <mindal@semihalf.com>
Signed-off-by: Kornel Dulęba <mindal@semihalf.com>
Signed-off-by: Drew Fustini <fustini@kernel.org>
Link: https://patch.msgid.link/20260729-dfustini-atl-sc-cbqri-dt-v6-3-7c22b05d461b@kernel.org
Signed-off-by: Paul Walmsley <pjw@kernel.org>
2026-08-07 19:24:32 -06:00

75 lines
2.1 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _ASM_RISCV_QOS_H
#define _ASM_RISCV_QOS_H
#include <linux/percpu-defs.h>
#ifdef CONFIG_RISCV_ISA_SSQOSID
#include <linux/bitfield.h>
#include <linux/cpufeature.h>
#include <linux/sched.h>
#include <asm/csr.h>
#include <asm/hwcap.h>
/* cached value of srmcfg csr for each cpu */
DECLARE_PER_CPU(u32, cpu_srmcfg);
/* default srmcfg value for each cpu, set via resctrl cpu assignment */
DECLARE_PER_CPU(u32, cpu_srmcfg_default);
static inline void __switch_to_srmcfg(struct task_struct *next)
{
u32 thread_srmcfg, default_srmcfg;
thread_srmcfg = READ_ONCE(next->thread.srmcfg);
default_srmcfg = __this_cpu_read(cpu_srmcfg_default);
/*
* RCID and MCID inherit from cpu_srmcfg_default independently.
* RESCTRL_RESERVED_CLOSID and RESCTRL_RESERVED_RMID are both 0, so a
* zero field means "unassigned" and takes the CPU default.
*/
if (thread_srmcfg == 0) {
thread_srmcfg = default_srmcfg;
} else {
u32 rcid = FIELD_GET(SRMCFG_RCID_MASK, thread_srmcfg);
u32 mcid = FIELD_GET(SRMCFG_MCID_MASK, thread_srmcfg);
if (rcid == 0 || mcid == 0) {
if (rcid == 0)
rcid = FIELD_GET(SRMCFG_RCID_MASK, default_srmcfg);
if (mcid == 0)
mcid = FIELD_GET(SRMCFG_MCID_MASK, default_srmcfg);
thread_srmcfg = FIELD_PREP(SRMCFG_RCID_MASK, rcid) |
FIELD_PREP(SRMCFG_MCID_MASK, mcid);
}
}
if (thread_srmcfg != __this_cpu_read(cpu_srmcfg)) {
/*
* No fence around the csrw. Ssqosid is silent on srmcfg
* ordering versus memory accesses, so a few accesses at the
* switch boundary may carry the previous RCID/MCID. The
* tagging inaccuracy is bounded and acceptable for QoS.
*/
__this_cpu_write(cpu_srmcfg, thread_srmcfg);
csr_write(CSR_SRMCFG, thread_srmcfg);
}
}
static __always_inline bool has_srmcfg(void)
{
return riscv_has_extension_unlikely(RISCV_ISA_EXT_SSQOSID);
}
#else /* ! CONFIG_RISCV_ISA_SSQOSID */
struct task_struct;
static __always_inline bool has_srmcfg(void) { return false; }
static inline void __switch_to_srmcfg(struct task_struct *next) { }
#endif /* CONFIG_RISCV_ISA_SSQOSID */
#endif /* _ASM_RISCV_QOS_H */