Skip to contentSkip to conversation

MIPS: SiByte: Add HW PMU support

PATCH Superseded Florian Fainelli 2 participants 7 messages Nov 13

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover MIPS: SiByte: Add HW PMU support

This patch series resurrects the SiByte HW PMU support which used to be supported with oprofile but was later removed.

Changes in v2:

• Preserve usercopy fault state during callchain unwinding. Save and restore current->thread.cp0_baduaddr so faulting unwinder reads cannot corrupt an interrupted usercopy’s recovery

• Balance PMU event initialization cleanup. Release resources exactly once on initialization failure and install the destroy callback only on success. Also check event type before rejecting unsupported attributes, allowing unrelated events to fall back to another PMU

• Keep disabled PMU interrupts masked after EOI. Add an SB1 IRQ flow wrapper that reapplies the mask when interrupt work disables the last event; the generic CPU IRQ EOI would otherwise unmask IP7 again

• Fix a documentation heading underline in sb1-pmu.rst (Randy, sashiko)

Florian Fainelli (3):
MIPS: SiByte: Wire up the CPU interrupt controller
MIPS: perf: Add SB1 userspace callchain unwinding
MIPS: perf: Add SB1 and SB1A performance counter support

 Documentation/arch/mips/index.rst       |   1 +
 Documentation/arch/mips/sb1-pmu.rst     | 162 ++++++++++
 arch/mips/include/asm/mach-sibyte/irq.h |  10 +
 arch/mips/kernel/perf_event.c           | 110 ++++++-
 arch/mips/kernel/perf_event_mipsxx.c    | 397 ++++++++++++++++++++++--
 arch/mips/sibyte/bcm1480/irq.c          |   7 +-
 arch/mips/sibyte/sb1250/irq.c           |   3 +
 include/linux/cpuhotplug.h              |   1 +
 8 files changed, 666 insertions(+), 25 deletions(-)
 create mode 100644 Documentation/arch/mips/sb1-pmu.rst
 create mode 100644 arch/mips/include/asm/mach-sibyte/irq.h
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/3 MIPS: SiByte: Wire up the CPU interrupt controller

Initialize the MIPS CPU interrupt controller on SB1250 and BCM1480 so CPU-local interrupts have a configured irq_chip and handler. Reserve IRQs 128 through 135 above both interrupt mapper ranges, and dispatch BCM1480 IP7 through the CPU controller as SB1250 already does.

This provides the CPU-local IP7 interrupt needed by the SB1 performance counters without overlapping the SiByte interrupt mapper IRQ numbers.

---
 arch/mips/include/asm/mach-sibyte/irq.h | 10 ++++++++++
 arch/mips/sibyte/bcm1480/irq.c          |  7 ++++++-
 arch/mips/sibyte/sb1250/irq.c           |  3 +++
 3 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 arch/mips/include/asm/mach-sibyte/irq.h
arch/mips/include/asm/mach-sibyte/irq.h new file +10 -0
new file mode 100644
index 000000000000..b7073830d2ec
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __ASM_MACH_SIBYTE_IRQ_H
+#define __ASM_MACH_SIBYTE_IRQ_H
+
+/* Keep CPU-local interrupts above both SiByte interrupt mapper ranges. */
+#define MIPS_CPU_IRQ_BASE	128
+
+#include <asm/mach-generic/irq.h>
+
+#endif
arch/mips/sibyte/bcm1480/irq.c +6 -1
index 4c18a2c755d2..5eff85a8659b 100644
@@ -15,6 +15,7 @@
 #include <asm/irq_regs.h>
 #include <asm/signal.h>
 #include <asm/io.h>
+#include <asm/irq_cpu.h>
 
 #include <asm/sibyte/bcm1480_regs.h>
 #include <asm/sibyte/bcm1480_int.h>
@@ -242,6 +243,8 @@ void __init arch_init_irq(void)
 	unsigned int imask = STATUSF_IP4 | STATUSF_IP3 | STATUSF_IP2 |
 		STATUSF_IP1 | STATUSF_IP0;
 
+	mips_cpu_irq_init();
+
 	/* Default everything to IP2 */
 	/* Start with _high registers which has no bit 0 interrupt source */
 	for (i = 1; i < BCM1480_NR_IRQS_HALF; i++) {	/* was I0 */
@@ -337,7 +340,9 @@ asmlinkage void plat_irq_dispatch(void)
 
 	pending = read_c0_cause() & read_c0_status();
 
-	if (pending & CAUSEF_IP4)
+	if (pending & CAUSEF_IP7)
+		do_IRQ(MIPS_CPU_IRQ_BASE + 7);
+	else if (pending & CAUSEF_IP4)
 		do_IRQ(K_BCM1480_INT_TIMER_0 + cpu);
 #ifdef CONFIG_SMP
 	else if (pending & CAUSEF_IP3)
arch/mips/sibyte/sb1250/irq.c +3 -0
index 57943fe3ac03..2df115269f81 100644
@@ -15,6 +15,7 @@
 #include <asm/signal.h>
 #include <asm/time.h>
 #include <asm/io.h>
+#include <asm/irq_cpu.h>
 
 #include <asm/sibyte/sb1250_regs.h>
 #include <asm/sibyte/sb1250_int.h>
@@ -225,6 +226,8 @@ void __init arch_init_irq(void)
 	unsigned int imask = STATUSF_IP4 | STATUSF_IP3 | STATUSF_IP2 |
 		STATUSF_IP1 | STATUSF_IP0;
 
+	mips_cpu_irq_init();
+
 	/* Default everything to IP2 */
 	for (i = 0; i < SB1250_NR_IRQS; i++) {	/* was I0 */
 		__raw_writeq(IMR_IP2_VAL,
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v2 1/3] MIPS: SiByte: Wire up the CPU interrupt controller

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

Initialize the MIPS CPU interrupt controller on SB1250 and BCM1480 so CPU-local interrupts have a configured irq_chip and handler. Reserve IRQs 128 through 135 above both interrupt mapper ranges, and dispatch BCM1480 IP7 through the CPU controller as SB1250 already does.

This provides the CPU-local IP7 interrupt needed by the SB1 performance counters without overlapping the SiByte interrupt mapper IRQ numbers.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/3 MIPS: perf: Add SB1 userspace callchain unwinding

In reply to Florian Fainelli

Restore the userspace callchain capability formerly available through MIPS OProfile, exposing it through perf for SB1 and SB1A. Commit 5f307491f3a0 ("MIPS: oprofile: Add callgraph support") introduced an in-kernel userspace prologue scanner, which disappeared with the removal of MIPS OProfile support in e258958945c6 ("arch: mips: Remove CONFIG_OPROFILE support"). The MIPS perf userspace callchain hook remained unimplemented.

Follow the former OProfile approach of examining instructions before the sampled PC to find standard MIPS stack adjustments and saved return addresses. Bound each scan to 128 instructions, use nofault user reads, validate frame data and stack progress, and stop at the perf stack limit. Handle o32, n32 and n64 address widths and the actual sw/sd return-address slot widths, rather than relying on the kernel's unsigned long size. Allow the sampled leaf to use the live return-address register, and permit repeated return PCs when the stack advances so recursion is not truncated.

This complements the existing MIPS DWARF callchain mechanism, enabled by 1ddc96bd42da ("MIPS: kernel: Support extracting off-line stack traces from user-space with perf") and b5f184fbdb03 ("perf tools: Support MIPS unwinding and dwarf-regs"). With --call-graph dwarf, the kernel records user registers and a stack snapshot for the perf tool to unwind using DWARF information. The new walker instead produces PERF_SAMPLE_CALLCHAIN entries in the kernel for ordinary perf -g/--call-graph fp sampling, without requiring DWARF unwind information or a userspace unwinding library. It does not change or replace the DWARF path, which can handle frames beyond the standard prologues recognized by this best-effort scanner.

Limit enablement to SB1 and SB1A, whose standard instruction encoding is handled here and which have neither MIPS16 nor microMIPS. Other MIPS CPU types retain their existing userspace callchain behavior. Nonstandard prologues, dynamic stack adjustments and signal trampolines can truncate chains.

---
 arch/mips/kernel/perf_event.c | 110 ++++++++++++++++++++++++++++++++--
 1 file changed, 106 insertions(+), 4 deletions(-)
arch/mips/kernel/perf_event.c +106 -4
index 5d7a9c03903b..cb2099416ac6 100644
@@ -13,15 +13,117 @@
 
 #include <linux/perf_event.h>
 #include <linux/sched/task_stack.h>
+#include <linux/uaccess.h>
 
+#include <asm/cpu-type.h>
+#include <asm/inst.h>
 #include <asm/stacktrace.h>
 
 /* Callchain handling code. */
 
-/*
- * Leave userspace callchain empty for now. When we find a way to trace
- * the user stack callchains, we will add it here.
- */
+struct sb1_user_frame {
+	unsigned long pc;
+	unsigned long sp;
+	unsigned long ra;
+};
+
+static unsigned long sb1_user_address(unsigned long addr)
+{
+	if (IS_ENABLED(CONFIG_64BIT) && test_thread_flag(TIF_32BIT_ADDR))
+		return (u32)addr;
+	return addr;
+}
+
+/* Best-effort prologue unwinding, as provided by the former MIPS OProfile. */
+static bool sb1_unwind_user(struct sb1_user_frame *frame)
+{
+	unsigned long pc = frame->pc, caller = frame->ra;
+	unsigned int frame_size = 0, ra_size = 0, ra_offset = 0, n;
+	union mips_instruction insn;
+
+	/* SB1 has neither MIPS16 nor microMIPS instructions. */
+	if ((pc & 3) || (frame->sp & 3))
+		return false;
+
+	for (n = 0; n < 128 && pc >= sizeof(insn); n++) {
+		pc -= sizeof(insn);
+		if (copy_from_user_nofault(&insn, (void __user *)pc, sizeof(insn)))
+			break;
+		if (insn.i_format.rs == 29 && insn.i_format.rt == 29 &&
+		    (insn.i_format.opcode == addiu_op ||
+		     insn.i_format.opcode == daddiu_op)) {
+			/* A positive adjustment belongs to an epilogue. */
+			if (insn.i_format.simmediate >= 0)
+				break;
+			frame_size = -insn.i_format.simmediate;
+			break;
+		}
+		if (insn.i_format.rs == 29 && insn.i_format.rt == 31 &&
+		    insn.i_format.simmediate >= 0) {
+			if (insn.i_format.opcode == sw_op)
+				ra_size = sizeof(u32);
+			else if (insn.i_format.opcode == sd_op)
+				ra_size = sizeof(u64);
+			else
+				continue;
+			ra_offset = insn.i_format.simmediate;
+		}
+		/* Do not scan into the preceding function through a return. */
+		if (insn.r_format.opcode == spec_op &&
+		    insn.r_format.func == jr_op)
+			break;
+	}
+
+	if (ra_size) {
+		u64 ra64;
+		u32 ra32;
+		void __user *slot = (void __user *)(frame->sp + ra_offset);
+
+		if (!frame_size || ra_offset + ra_size > frame_size)
+			return false;
+		if (ra_size == sizeof(ra32)) {
+			if (copy_from_user_nofault(&ra32, slot, sizeof(ra32)))
+				return false;
+			caller = ra32;
+		} else {
+			if (copy_from_user_nofault(&ra64, slot, sizeof(ra64)))
+				return false;
+			caller = ra64;
+		}
+	}
+
+	caller = sb1_user_address(caller);
+	if (!caller || (caller == frame->pc && !frame_size) || (caller & 3) ||
+	    !access_ok((void __user *)caller, sizeof(insn)) ||
+	    frame->sp + frame_size < frame->sp)
+		return false;
+	frame->sp += frame_size;
+	frame->pc = caller;
+	/* Only the sampled leaf may use the live return-address register. */
+	frame->ra = 0;
+	return true;
+}
+
+void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
+			 struct pt_regs *regs)
+{
+	struct sb1_user_frame frame = {
+		.pc = sb1_user_address(regs->cp0_epc),
+		.sp = sb1_user_address(regs->regs[29]),
+		.ra = sb1_user_address(regs->regs[31]),
+	};
+	unsigned long saved_baduaddr;
+
+	if (current_cpu_type() != CPU_SB1 && current_cpu_type() != CPU_SB1A)
+		return;
+	/* Nofault reads must not clobber an interrupted usercopy's fault address. */
+	saved_baduaddr = current->thread.cp0_baduaddr;
+	do {
+		if (perf_callchain_store(entry, frame.pc))
+			break;
+	} while (sb1_unwind_user(&frame));
+	current->thread.cp0_baduaddr = saved_baduaddr;
+}
 
 static void save_raw_perf_callchain(struct perf_callchain_entry_ctx *entry,
 				    unsigned long reg29)
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH v2 2/3] MIPS: perf: Add SB1 userspace callchain unwinding

Sashiko has reviewed this patch and found no issues.
Quoted 20 lines

Restore the userspace callchain capability formerly available through MIPS OProfile, exposing it through perf for SB1 and SB1A. Commit 5f307491f3a0 ("MIPS: oprofile: Add callgraph support") introduced an in-kernel userspace prologue scanner, which disappeared with the removal of MIPS OProfile support in e258958945c6 ("arch: mips: Remove CONFIG_OPROFILE support"). The MIPS perf userspace callchain hook remained unimplemented.

Follow the former OProfile approach of examining instructions before the sampled PC to find standard MIPS stack adjustments and saved return addresses. Bound each scan to 128 instructions, use nofault user reads, validate frame data and stack progress, and stop at the perf stack limit. Handle o32, n32 and n64 address widths and the actual sw/sd return-address slot widths, rather than relying on the kernel's unsigned long size. Allow the sampled leaf to use the live return-address register, and permit repeated return PCs when the stack advances so recursion is not truncated. [ ... ]
types retain their existing userspace callchain behavior. Nonstandard prologues, dynamic stack adjustments and signal trampolines can truncate chains.

Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

PATCH v2 3/3 MIPS: perf: Add SB1 and SB1A performance counter support

In reply to Florian Fainelli

Support the four CPU-local SB1 performance counters through the MIPS PMU driver. Use 64-bit CP0 accesses for the 40 count bits and overflow bit, validate the documented layout at initialization, and handle sampling on CPU-local IP7. Keep the interrupt masked while no events are scheduled and clear the unused Count/Compare source sharing IP7 before enabling it and in the handler. Reset counters through CPU hotplug callbacks.

Expose the historical OProfile event catalog through raw events and 48 sysfs aliases, plus generic cycles, instructions, branches and branch misses. Preserve the catalog's duplicate encodings and disambiguate its duplicate names. Leave generic cache events unmapped where the historical descriptions do not establish a reliable mapping.

Add explicit physical counter selection and independent exclusions for EXL and ordinary kernel execution. These exclusions only remove modes already permitted by perf attributes. Enforce counter 0's cycles-only restriction and reject reserved configurations. Reassign flexible events when fixed-counter requests require it, preserving counts and sampling state, and support groups led by software events. Honor sampling throttling.

Document the ABI, hardware assumptions, usage and validation limits.

---
 Documentation/arch/mips/index.rst    |   1 +
 Documentation/arch/mips/sb1-pmu.rst  | 162 +++++++++++
 arch/mips/kernel/perf_event_mipsxx.c | 397 +++++++++++++++++++++++++--
 include/linux/cpuhotplug.h           |   1 +
 4 files changed, 541 insertions(+), 20 deletions(-)
 create mode 100644 Documentation/arch/mips/sb1-pmu.rst
Documentation/arch/mips/index.rst +1 -0
index 703e195b933d..71a06ccac173 100644
@@ -10,5 +10,6 @@ MIPS-specific Documentation
 
    booting
    ingenic-tcu
+   sb1-pmu
 
    features
Documentation/arch/mips/sb1-pmu.rst new file +162 -0

162 lines not on this page · raw message

arch/mips/kernel/perf_event_mipsxx.c +377 -20

554 lines not on this page · raw message

include/linux/cpuhotplug.h +1 -0
index 1b6dba159bb2..554f47fb5dd4 100644
@@ -151,6 +151,7 @@ enum cpuhp_state {
 	CPUHP_AP_PERF_X86_STARTING,
 	CPUHP_AP_PERF_X86_AMD_IBS_STARTING,
 	CPUHP_AP_PERF_XTENSA_STARTING,
+	CPUHP_AP_PERF_MIPS_SB1_STARTING,
 	CPUHP_AP_ARM_VFP_STARTING,
 	CPUHP_AP_ARM64_DEBUG_MONITORS_STARTING,
 	CPUHP_AP_PERF_ARM_HW_BREAKPOINT_STARTING,
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH v2 3/3] MIPS: perf: Add SB1 and SB1A performance counter support

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] MIPS: perf: preemptible smp_processor_id() call in is_sb1_pmu() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] MIPS: perf: preemptible smp_processor_id() call in is_sb1_pmu() --

commit f968ced28e6948da3887ac13c02f7c72caf1a26e
Author: Florian Fainelli <[email protected]>

MIPS: perf: Add SB1 and SB1A performance counter support

This commit adds performance counter support for SB1 and SB1A CPUs to the MIPS PMU driver, along with documentation and CPU hotplug state handling.

Quoted 4 lines
arch/mips/kernel/perf_event_mipsxx.c +0 -0
index 196a070349b09..7a212d4757a0a 100644

[ ... ]

arch/mips/kernel/perf_event_mipsxx.c struct mips_pmu { · line 109

Quoted 9 lines
arch/mips/kernel/perf_event_mipsxx.c +5 -0
@@ -93,6 +102,12 @@ struct mips_pmu {
 static int counter_bits;
 static struct mips_pmu mipspmu;
 
+static bool is_sb1_pmu(void)
+{
+	return current_cpu_type() == CPU_SB1 ||
+	       current_cpu_type() == CPU_SB1A;
+}

[Severity: Low]
Should is_sb1_pmu() use boot_cpu_type() (like get_loongson3_pmu_type() in this file) instead of current_cpu_type()?

current_cpu_type() reads current_cpu_data.cputype, which expands to cpu_data[smp_processor_id()].cputype. Because is_sb1_pmu() is called from preemptible process context in paths such as mipspmu_event_init(), opening or closing a perf event from a task that is not pinned to a single CPU triggers a debug_smp_processor_id() warning when CONFIG_DEBUG_PREEMPT is enabled.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3