xref: /netbsd-src/sys/arch/arm/fdt/pmu_fdt.c (revision 4caf62b3a6305b2d09ddaed6050e4e10ac9eef5d)
1*4caf62b3Sriastradh /* $NetBSD: pmu_fdt.c,v 1.12 2023/10/02 08:42:20 riastradh Exp $ */
25ab5803cSjmcneill 
35ab5803cSjmcneill /*-
45ab5803cSjmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
55ab5803cSjmcneill  * All rights reserved.
65ab5803cSjmcneill  *
75ab5803cSjmcneill  * Redistribution and use in source and binary forms, with or without
85ab5803cSjmcneill  * modification, are permitted provided that the following conditions
95ab5803cSjmcneill  * are met:
105ab5803cSjmcneill  * 1. Redistributions of source code must retain the above copyright
115ab5803cSjmcneill  *    notice, this list of conditions and the following disclaimer.
125ab5803cSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
135ab5803cSjmcneill  *    notice, this list of conditions and the following disclaimer in the
145ab5803cSjmcneill  *    documentation and/or other materials provided with the distribution.
155ab5803cSjmcneill  *
165ab5803cSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175ab5803cSjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185ab5803cSjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
195ab5803cSjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
205ab5803cSjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
215ab5803cSjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
225ab5803cSjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
235ab5803cSjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
245ab5803cSjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
255ab5803cSjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265ab5803cSjmcneill  * SUCH DAMAGE.
275ab5803cSjmcneill  */
285ab5803cSjmcneill 
295ab5803cSjmcneill #include <sys/cdefs.h>
30*4caf62b3Sriastradh __KERNEL_RCSID(0, "$NetBSD: pmu_fdt.c,v 1.12 2023/10/02 08:42:20 riastradh Exp $");
315ab5803cSjmcneill 
325ab5803cSjmcneill #include <sys/param.h>
335ab5803cSjmcneill #include <sys/bus.h>
345ab5803cSjmcneill #include <sys/device.h>
355ab5803cSjmcneill #include <sys/systm.h>
365ab5803cSjmcneill #include <sys/kernel.h>
375ab5803cSjmcneill #include <sys/cpu.h>
385ab5803cSjmcneill #include <sys/interrupt.h>
398b9358a3Sjmcneill #include <sys/kmem.h>
408a273b31Sjmcneill #include <sys/xcall.h>
415ab5803cSjmcneill 
425ab5803cSjmcneill #include <dev/fdt/fdtvar.h>
435ab5803cSjmcneill 
442cb9b388Sjmcneill #if defined(__aarch64__)
455ab5803cSjmcneill #include <dev/tprof/tprof_armv8.h>
465ab5803cSjmcneill #define arm_pmu_intr armv8_pmu_intr
475ab5803cSjmcneill #define arm_pmu_init armv8_pmu_init
48b561895dSjmcneill #elif defined(_ARM_ARCH_7)
49b561895dSjmcneill #include <dev/tprof/tprof_armv7.h>
50b561895dSjmcneill #define arm_pmu_intr armv7_pmu_intr
51b561895dSjmcneill #define arm_pmu_init armv7_pmu_init
525ab5803cSjmcneill #endif
535ab5803cSjmcneill 
545ab5803cSjmcneill #include <arm/armreg.h>
555ab5803cSjmcneill 
568a273b31Sjmcneill static bool	pmu_fdt_uses_ppi;
578a273b31Sjmcneill static int	pmu_fdt_count;
588a273b31Sjmcneill 
595ab5803cSjmcneill static int	pmu_fdt_match(device_t, cfdata_t, void *);
605ab5803cSjmcneill static void	pmu_fdt_attach(device_t, device_t, void *);
615ab5803cSjmcneill 
625ab5803cSjmcneill static void	pmu_fdt_init(device_t);
635ab5803cSjmcneill static int	pmu_fdt_intr_distribute(const int, int, void *);
645ab5803cSjmcneill 
656e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
666e54367aSthorpej 	{ .compat = "arm,armv8-pmuv3" },
676e54367aSthorpej 	{ .compat = "arm,cortex-a73-pmu" },
686e54367aSthorpej 	{ .compat = "arm,cortex-a72-pmu" },
696e54367aSthorpej 	{ .compat = "arm,cortex-a57-pmu" },
706e54367aSthorpej 	{ .compat = "arm,cortex-a53-pmu" },
71b561895dSjmcneill 
726e54367aSthorpej 	{ .compat = "arm,cortex-a35-pmu" },
736e54367aSthorpej 	{ .compat = "arm,cortex-a17-pmu" },
746e54367aSthorpej 	{ .compat = "arm,cortex-a12-pmu" },
756e54367aSthorpej 	{ .compat = "arm,cortex-a9-pmu" },
766e54367aSthorpej 	{ .compat = "arm,cortex-a8-pmu" },
776e54367aSthorpej 	{ .compat = "arm,cortex-a7-pmu" },
786e54367aSthorpej 	{ .compat = "arm,cortex-a5-pmu" },
79b561895dSjmcneill 
806e54367aSthorpej 	DEVICE_COMPAT_EOL
815ab5803cSjmcneill };
825ab5803cSjmcneill 
835ab5803cSjmcneill struct pmu_fdt_softc {
845ab5803cSjmcneill 	device_t	sc_dev;
855ab5803cSjmcneill 	int		sc_phandle;
865ab5803cSjmcneill };
875ab5803cSjmcneill 
885ab5803cSjmcneill CFATTACH_DECL_NEW(pmu_fdt, sizeof(struct pmu_fdt_softc),
895ab5803cSjmcneill     pmu_fdt_match, pmu_fdt_attach, NULL, NULL);
905ab5803cSjmcneill 
915ab5803cSjmcneill static int
pmu_fdt_match(device_t parent,cfdata_t cf,void * aux)925ab5803cSjmcneill pmu_fdt_match(device_t parent, cfdata_t cf, void *aux)
935ab5803cSjmcneill {
945ab5803cSjmcneill 	struct fdt_attach_args * const faa = aux;
955ab5803cSjmcneill 
966e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
975ab5803cSjmcneill }
985ab5803cSjmcneill 
995ab5803cSjmcneill static void
pmu_fdt_attach(device_t parent,device_t self,void * aux)1005ab5803cSjmcneill pmu_fdt_attach(device_t parent, device_t self, void *aux)
1015ab5803cSjmcneill {
1025ab5803cSjmcneill 	struct pmu_fdt_softc * const sc = device_private(self);
1035ab5803cSjmcneill 	struct fdt_attach_args * const faa = aux;
1045ab5803cSjmcneill 	const int phandle = faa->faa_phandle;
1055ab5803cSjmcneill 
1065ab5803cSjmcneill 	aprint_naive("\n");
1075ab5803cSjmcneill 	aprint_normal(": Performance Monitor Unit\n");
1085ab5803cSjmcneill 
1095ab5803cSjmcneill 	sc->sc_dev = self;
1105ab5803cSjmcneill 	sc->sc_phandle = phandle;
1115ab5803cSjmcneill 
1125ab5803cSjmcneill 	config_interrupts(self, pmu_fdt_init);
1135ab5803cSjmcneill }
1145ab5803cSjmcneill 
1155ab5803cSjmcneill static void
pmu_fdt_init(device_t self)1165ab5803cSjmcneill pmu_fdt_init(device_t self)
1175ab5803cSjmcneill {
1185ab5803cSjmcneill 	struct pmu_fdt_softc * const sc = device_private(self);
1195ab5803cSjmcneill 	const int phandle = sc->sc_phandle;
1205ab5803cSjmcneill 	char intrstr[128];
1215ab5803cSjmcneill 	int error, n;
1228b9358a3Sjmcneill 	void **ih;
1235ab5803cSjmcneill 
1248a273b31Sjmcneill 	if (pmu_fdt_uses_ppi && pmu_fdt_count > 0) {
1258a273b31Sjmcneill 		/*
1268a273b31Sjmcneill 		 * Second instance of a PMU where PPIs are used. Since the PMU
1278a273b31Sjmcneill 		 * is already initialized and the PPI interrupt handler has
1288a273b31Sjmcneill 		 * already been installed, there is nothing left to do here.
1298a273b31Sjmcneill 		 */
1308a273b31Sjmcneill 		if (fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr)))
1318a273b31Sjmcneill 			aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1325ab5803cSjmcneill 		return;
1335ab5803cSjmcneill 	}
1348b9358a3Sjmcneill 
1358a273b31Sjmcneill 	if (pmu_fdt_count == 0) {
136367f8a70Sskrll 		error = arm_pmu_init();
137367f8a70Sskrll 		if (error) {
138367f8a70Sskrll 			aprint_error_dev(self,
139*4caf62b3Sriastradh 			    "couldn't initialise PMU event counter\n");
140367f8a70Sskrll 			return;
1418a273b31Sjmcneill 		}
142c07e1b52Sryo 	}
1438a273b31Sjmcneill 
1448b9358a3Sjmcneill 	ih = kmem_zalloc(sizeof(void *) * ncpu, KM_SLEEP);
1458b9358a3Sjmcneill 
1468b9358a3Sjmcneill 	for (n = 0; n < ncpu; n++) {
14764e248edSryo 		ih[n] = fdtbus_intr_establish_xname(phandle, n, IPL_HIGH,
14864e248edSryo 		    FDT_INTR_MPSAFE, arm_pmu_intr, NULL, device_xname(self));
1498b9358a3Sjmcneill 		if (ih[n] == NULL)
1508b9358a3Sjmcneill 			break;
1518b9358a3Sjmcneill 		if (!fdtbus_intr_str(phandle, n, intrstr, sizeof(intrstr))) {
1528b9358a3Sjmcneill 			aprint_error_dev(self,
1538b9358a3Sjmcneill 			    "couldn't decode interrupt %u\n", n);
1548b9358a3Sjmcneill 			goto cleanup;
1558b9358a3Sjmcneill 		}
1568b9358a3Sjmcneill 		aprint_normal_dev(self, "interrupting on %s\n", intrstr);
1578b9358a3Sjmcneill 	}
1588b9358a3Sjmcneill 
1598b9358a3Sjmcneill 	/* We need either one IRQ (PPI), or one per CPU (SPI) */
1608b9358a3Sjmcneill 	const int nirq = n;
1618b9358a3Sjmcneill 	if (nirq == 0) {
1628b9358a3Sjmcneill 		aprint_error_dev(self, "couldn't establish interrupts\n");
1638b9358a3Sjmcneill 		goto cleanup;
1648b9358a3Sjmcneill 	}
1658b9358a3Sjmcneill 
1668b9358a3Sjmcneill 	/* Set interrupt affinity if we have more than one interrupt */
1678b9358a3Sjmcneill 	if (nirq > 1) {
1688b9358a3Sjmcneill 		for (n = 0; n < nirq; n++) {
1698b9358a3Sjmcneill 			error = pmu_fdt_intr_distribute(phandle, n, ih[n]);
1708b9358a3Sjmcneill 			if (error != 0) {
1718b9358a3Sjmcneill 				aprint_error_dev(self,
1728b9358a3Sjmcneill 				    "failed to distribute interrupt %u: %d\n",
1738b9358a3Sjmcneill 				    n, error);
1748b9358a3Sjmcneill 				goto cleanup;
1758b9358a3Sjmcneill 			}
1768b9358a3Sjmcneill 		}
1778b9358a3Sjmcneill 	}
1788b9358a3Sjmcneill 
1798a273b31Sjmcneill 	pmu_fdt_count++;
1808a273b31Sjmcneill 	pmu_fdt_uses_ppi = nirq == 1 && ncpu > 1;
1818a273b31Sjmcneill 
1828b9358a3Sjmcneill cleanup:
1838b9358a3Sjmcneill 	kmem_free(ih, sizeof(void *) * ncpu);
1845ab5803cSjmcneill }
1855ab5803cSjmcneill 
1865ab5803cSjmcneill static int
pmu_fdt_intr_distribute(const int phandle,int index,void * ih)1875ab5803cSjmcneill pmu_fdt_intr_distribute(const int phandle, int index, void *ih)
1885ab5803cSjmcneill {
1895ab5803cSjmcneill 	CPU_INFO_ITERATOR cii;
1905ab5803cSjmcneill 	struct cpu_info *ci;
1915ab5803cSjmcneill 	bus_addr_t mpidr;
1925ab5803cSjmcneill 	int len, cpunode;
1935ab5803cSjmcneill 	const u_int *aff;
1945ab5803cSjmcneill 	kcpuset_t *set;
1955ab5803cSjmcneill 	int error;
1965ab5803cSjmcneill 
1975ab5803cSjmcneill 	kcpuset_create(&set, true);
1985ab5803cSjmcneill 
1995ab5803cSjmcneill 	if (of_hasprop(phandle, "interrupt-affinity")) {
2005ab5803cSjmcneill 		aff = fdtbus_get_prop(phandle, "interrupt-affinity", &len);
2015ab5803cSjmcneill 		if (len < (index + 1) * 4)
2025ab5803cSjmcneill 			return EINVAL;
2035ab5803cSjmcneill 		cpunode = fdtbus_get_phandle_from_native(be32toh(aff[index]));
2045ab5803cSjmcneill 		if (fdtbus_get_reg(cpunode, 0, &mpidr, NULL) != 0)
2055ab5803cSjmcneill 			return ENXIO;
2065ab5803cSjmcneill 		for (CPU_INFO_FOREACH(cii, ci)) {
2075ab5803cSjmcneill 			const uint32_t ci_mpidr =
208d29b3abaSskrll 			    __SHIFTIN(ci->ci_core_id, MPIDR_AFF0) |
209d29b3abaSskrll 			    __SHIFTIN(ci->ci_package_id, MPIDR_AFF1);
2105ab5803cSjmcneill 			if (ci_mpidr == mpidr) {
2115ab5803cSjmcneill 				kcpuset_set(set, cpu_index(ci));
2125ab5803cSjmcneill 				break;
2135ab5803cSjmcneill 			}
2145ab5803cSjmcneill 		}
2155ab5803cSjmcneill 	} else {
2165ab5803cSjmcneill 		kcpuset_set(set, index);
2175ab5803cSjmcneill 	}
2185ab5803cSjmcneill 
2195ab5803cSjmcneill 	if (kcpuset_iszero(set)) {
2205ab5803cSjmcneill 		kcpuset_destroy(set);
2215ab5803cSjmcneill 		return ENOENT;
2225ab5803cSjmcneill 	}
2235ab5803cSjmcneill 
2245ab5803cSjmcneill 	error = interrupt_distribute(ih, set, NULL);
2255ab5803cSjmcneill 
2265ab5803cSjmcneill 	kcpuset_destroy(set);
2275ab5803cSjmcneill 
2285ab5803cSjmcneill 	return error;
2295ab5803cSjmcneill }
230