xref: /netbsd-src/sys/arch/arm/fdt/pmu_fdt.c (revision 122b5006ee1bd67145794b4cde92f4fe4781a5ec)
1 /* $NetBSD: pmu_fdt.c,v 1.9 2021/09/27 09:54:52 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pmu_fdt.c,v 1.9 2021/09/27 09:54:52 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/cpu.h>
38 #include <sys/interrupt.h>
39 #include <sys/kmem.h>
40 #include <sys/xcall.h>
41 
42 #include <dev/fdt/fdtvar.h>
43 
44 #if defined(__aarch64__)
45 #include <dev/tprof/tprof_armv8.h>
46 #define arm_pmu_intr armv8_pmu_intr
47 #define arm_pmu_init armv8_pmu_init
48 #elif defined(_ARM_ARCH_7)
49 #include <dev/tprof/tprof_armv7.h>
50 #define arm_pmu_intr armv7_pmu_intr
51 #define arm_pmu_init armv7_pmu_init
52 #endif
53 
54 #include <arm/armreg.h>
55 
56 static bool	pmu_fdt_uses_ppi;
57 static int	pmu_fdt_count;
58 
59 static int	pmu_fdt_match(device_t, cfdata_t, void *);
60 static void	pmu_fdt_attach(device_t, device_t, void *);
61 
62 static void	pmu_fdt_init(device_t);
63 static int	pmu_fdt_intr_distribute(const int, int, void *);
64 
65 static const struct device_compatible_entry compat_data[] = {
66 	{ .compat = "arm,armv8-pmuv3" },
67 	{ .compat = "arm,cortex-a73-pmu" },
68 	{ .compat = "arm,cortex-a72-pmu" },
69 	{ .compat = "arm,cortex-a57-pmu" },
70 	{ .compat = "arm,cortex-a53-pmu" },
71 
72 	{ .compat = "arm,cortex-a35-pmu" },
73 	{ .compat = "arm,cortex-a17-pmu" },
74 	{ .compat = "arm,cortex-a12-pmu" },
75 	{ .compat = "arm,cortex-a9-pmu" },
76 	{ .compat = "arm,cortex-a8-pmu" },
77 	{ .compat = "arm,cortex-a7-pmu" },
78 	{ .compat = "arm,cortex-a5-pmu" },
79 
80 	DEVICE_COMPAT_EOL
81 };
82 
83 struct pmu_fdt_softc {
84 	device_t	sc_dev;
85 	int		sc_phandle;
86 };
87 
88 CFATTACH_DECL_NEW(pmu_fdt, sizeof(struct pmu_fdt_softc),
89     pmu_fdt_match, pmu_fdt_attach, NULL, NULL);
90 
91 static int
92 pmu_fdt_match(device_t parent, cfdata_t cf, void *aux)
93 {
94 	struct fdt_attach_args * const faa = aux;
95 
96 	return of_compatible_match(faa->faa_phandle, compat_data);
97 }
98 
99 static void
100 pmu_fdt_attach(device_t parent, device_t self, void *aux)
101 {
102 	struct pmu_fdt_softc * const sc = device_private(self);
103 	struct fdt_attach_args * const faa = aux;
104 	const int phandle = faa->faa_phandle;
105 
106 	aprint_naive("\n");
107 	aprint_normal(": Performance Monitor Unit\n");
108 
109 	sc->sc_dev = self;
110 	sc->sc_phandle = phandle;
111 
112 	config_interrupts(self, pmu_fdt_init);
113 }
114 
115 static void
116 pmu_fdt_init(device_t self)
117 {
118 	struct pmu_fdt_softc * const sc = device_private(self);
119 	const int phandle = sc->sc_phandle;
120 	char intrstr[128];
121 	int error, n;
122 	void **ih;
123 
124 	if (pmu_fdt_uses_ppi && pmu_fdt_count > 0) {
125 		/*
126 		 * Second instance of a PMU where PPIs are used. Since the PMU
127 		 * is already initialized and the PPI interrupt handler has
128 		 * already been installed, there is nothing left to do here.
129 		 */
130 		if (fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr)))
131 			aprint_normal_dev(self, "interrupting on %s\n", intrstr);
132 		return;
133 	}
134 
135 	if (pmu_fdt_count == 0) {
136 		arm_pmu_init();
137 	}
138 
139 	ih = kmem_zalloc(sizeof(void *) * ncpu, KM_SLEEP);
140 
141 	for (n = 0; n < ncpu; n++) {
142 		ih[n] = fdtbus_intr_establish_xname(phandle, n, IPL_HIGH,
143 		    FDT_INTR_MPSAFE, arm_pmu_intr, NULL, device_xname(self));
144 		if (ih[n] == NULL)
145 			break;
146 		if (!fdtbus_intr_str(phandle, n, intrstr, sizeof(intrstr))) {
147 			aprint_error_dev(self,
148 			    "couldn't decode interrupt %u\n", n);
149 			goto cleanup;
150 		}
151 		aprint_normal_dev(self, "interrupting on %s\n", intrstr);
152 	}
153 
154 	/* We need either one IRQ (PPI), or one per CPU (SPI) */
155 	const int nirq = n;
156 	if (nirq == 0) {
157 		aprint_error_dev(self, "couldn't establish interrupts\n");
158 		goto cleanup;
159 	}
160 
161 	/* Set interrupt affinity if we have more than one interrupt */
162 	if (nirq > 1) {
163 		for (n = 0; n < nirq; n++) {
164 			error = pmu_fdt_intr_distribute(phandle, n, ih[n]);
165 			if (error != 0) {
166 				aprint_error_dev(self,
167 				    "failed to distribute interrupt %u: %d\n",
168 				    n, error);
169 				goto cleanup;
170 			}
171 		}
172 	}
173 
174 	pmu_fdt_count++;
175 	pmu_fdt_uses_ppi = nirq == 1 && ncpu > 1;
176 
177 cleanup:
178 	kmem_free(ih, sizeof(void *) * ncpu);
179 }
180 
181 static int
182 pmu_fdt_intr_distribute(const int phandle, int index, void *ih)
183 {
184 	CPU_INFO_ITERATOR cii;
185 	struct cpu_info *ci;
186 	bus_addr_t mpidr;
187 	int len, cpunode;
188 	const u_int *aff;
189 	kcpuset_t *set;
190 	int error;
191 
192 	kcpuset_create(&set, true);
193 
194 	if (of_hasprop(phandle, "interrupt-affinity")) {
195 		aff = fdtbus_get_prop(phandle, "interrupt-affinity", &len);
196 		if (len < (index + 1) * 4)
197 			return EINVAL;
198 		cpunode = fdtbus_get_phandle_from_native(be32toh(aff[index]));
199 		if (fdtbus_get_reg(cpunode, 0, &mpidr, NULL) != 0)
200 			return ENXIO;
201 		for (CPU_INFO_FOREACH(cii, ci)) {
202 			const uint32_t ci_mpidr =
203 			    __SHIFTIN(ci->ci_core_id, MPIDR_AFF0) |
204 			    __SHIFTIN(ci->ci_package_id, MPIDR_AFF1);
205 			if (ci_mpidr == mpidr) {
206 				kcpuset_set(set, cpu_index(ci));
207 				break;
208 			}
209 		}
210 	} else {
211 		kcpuset_set(set, index);
212 	}
213 
214 	if (kcpuset_iszero(set)) {
215 		kcpuset_destroy(set);
216 		return ENOENT;
217 	}
218 
219 	error = interrupt_distribute(ih, set, NULL);
220 
221 	kcpuset_destroy(set);
222 
223 	return error;
224 }
225