Home | History | Annotate | Line # | Download | only in acpi
      1  1.18  jmcneill /* $NetBSD: cpu_acpi.c,v 1.18 2025/01/30 00:43:56 jmcneill Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  jmcneill  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9   1.1  jmcneill  *
     10   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     11   1.1  jmcneill  * modification, are permitted provided that the following conditions
     12   1.1  jmcneill  * are met:
     13   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     14   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     15   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     18   1.1  jmcneill  *
     19   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  jmcneill  */
     31   1.1  jmcneill 
     32   1.5  jmcneill #include "tprof.h"
     33   1.6       ryo #include "opt_multiprocessor.h"
     34   1.5  jmcneill 
     35   1.1  jmcneill #include <sys/cdefs.h>
     36  1.18  jmcneill __KERNEL_RCSID(0, "$NetBSD: cpu_acpi.c,v 1.18 2025/01/30 00:43:56 jmcneill Exp $");
     37   1.1  jmcneill 
     38   1.1  jmcneill #include <sys/param.h>
     39   1.1  jmcneill #include <sys/bus.h>
     40   1.1  jmcneill #include <sys/cpu.h>
     41   1.1  jmcneill #include <sys/device.h>
     42   1.5  jmcneill #include <sys/interrupt.h>
     43   1.5  jmcneill #include <sys/kcpuset.h>
     44  1.17  jmcneill #include <sys/kmem.h>
     45   1.7  jmcneill #include <sys/reboot.h>
     46   1.1  jmcneill 
     47   1.1  jmcneill #include <dev/acpi/acpireg.h>
     48   1.1  jmcneill #include <dev/acpi/acpivar.h>
     49  1.16  jmcneill #include <dev/acpi/acpi_srat.h>
     50  1.17  jmcneill #include <external/bsd/acpica/dist/include/amlresrc.h>
     51   1.1  jmcneill 
     52   1.1  jmcneill #include <arm/armreg.h>
     53   1.1  jmcneill #include <arm/cpu.h>
     54   1.1  jmcneill #include <arm/cpufunc.h>
     55   1.8     skrll #include <arm/cpuvar.h>
     56   1.1  jmcneill #include <arm/locore.h>
     57   1.1  jmcneill 
     58   1.1  jmcneill #include <arm/arm/psci.h>
     59   1.1  jmcneill 
     60  1.17  jmcneill #define LPI_IDLE_FACTOR		3
     61  1.17  jmcneill 
     62   1.5  jmcneill #if NTPROF > 0
     63   1.5  jmcneill #include <dev/tprof/tprof_armv8.h>
     64   1.5  jmcneill #endif
     65   1.5  jmcneill 
     66   1.1  jmcneill static int	cpu_acpi_match(device_t, cfdata_t, void *);
     67   1.1  jmcneill static void	cpu_acpi_attach(device_t, device_t, void *);
     68   1.1  jmcneill 
     69  1.17  jmcneill static void	cpu_acpi_probe_lpi(device_t, struct cpu_info *ci);
     70  1.17  jmcneill void		cpu_acpi_lpi_idle(void);
     71  1.17  jmcneill 
     72   1.5  jmcneill #if NTPROF > 0
     73   1.5  jmcneill static void	cpu_acpi_tprof_init(device_t);
     74   1.5  jmcneill #endif
     75   1.5  jmcneill 
     76  1.15       pho CFATTACH_DECL2_NEW(cpu_acpi, 0,
     77  1.15       pho     cpu_acpi_match, cpu_acpi_attach, NULL, NULL,
     78  1.15       pho     cpu_rescan, cpu_childdetached);
     79   1.1  jmcneill 
     80   1.6       ryo #ifdef MULTIPROCESSOR
     81   1.1  jmcneill static register_t
     82   1.1  jmcneill cpu_acpi_mpstart_pa(void)
     83   1.1  jmcneill {
     84   1.3     skrll 
     85   1.3     skrll 	return (register_t)KERN_VTOPHYS((vaddr_t)cpu_mpstart);
     86   1.1  jmcneill }
     87   1.6       ryo #endif /* MULTIPROCESSOR */
     88   1.1  jmcneill 
     89   1.1  jmcneill static int
     90   1.1  jmcneill cpu_acpi_match(device_t parent, cfdata_t cf, void *aux)
     91   1.1  jmcneill {
     92   1.1  jmcneill 	ACPI_SUBTABLE_HEADER *hdrp = aux;
     93   1.2  jmcneill 	ACPI_MADT_GENERIC_INTERRUPT *gicc;
     94   1.1  jmcneill 
     95   1.2  jmcneill 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
     96   1.2  jmcneill 		return 0;
     97   1.2  jmcneill 
     98   1.2  jmcneill 	gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
     99   1.2  jmcneill 
    100   1.2  jmcneill 	return (gicc->Flags & ACPI_MADT_ENABLED) != 0;
    101   1.1  jmcneill }
    102   1.1  jmcneill 
    103   1.1  jmcneill static void
    104   1.1  jmcneill cpu_acpi_attach(device_t parent, device_t self, void *aux)
    105   1.1  jmcneill {
    106  1.12  jmcneill 	prop_dictionary_t dict = device_properties(self);
    107   1.1  jmcneill 	ACPI_MADT_GENERIC_INTERRUPT *gicc = aux;
    108   1.1  jmcneill 	const uint64_t mpidr = gicc->ArmMpidr;
    109   1.4  jmcneill 	const int unit = device_unit(self);
    110   1.4  jmcneill 	struct cpu_info *ci = &cpu_info_store[unit];
    111  1.16  jmcneill 	struct acpisrat_node *node;
    112   1.1  jmcneill 
    113   1.6       ryo #ifdef MULTIPROCESSOR
    114   1.7  jmcneill 	if (cpu_mpidr_aff_read() != mpidr && (boothowto & RB_MD1) == 0) {
    115   1.1  jmcneill 		const u_int cpuindex = device_unit(self);
    116   1.6       ryo 		int error;
    117   1.1  jmcneill 
    118   1.1  jmcneill 		cpu_mpidr[cpuindex] = mpidr;
    119  1.10  jmcneill 		cpu_dcache_wb_range((vaddr_t)&cpu_mpidr[cpuindex],
    120  1.10  jmcneill 		    sizeof(cpu_mpidr[cpuindex]));
    121   1.1  jmcneill 
    122   1.1  jmcneill 		/* XXX support spin table */
    123   1.1  jmcneill 		error = psci_cpu_on(mpidr, cpu_acpi_mpstart_pa(), 0);
    124   1.1  jmcneill 		if (error != PSCI_SUCCESS) {
    125   1.1  jmcneill 			aprint_error_dev(self, "failed to start CPU\n");
    126   1.1  jmcneill 			return;
    127   1.1  jmcneill 		}
    128   1.1  jmcneill 
    129   1.9     skrll 		sev();
    130   1.1  jmcneill 
    131   1.1  jmcneill 		for (u_int i = 0x10000000; i > 0; i--) {
    132   1.7  jmcneill 			if (cpu_hatched_p(cpuindex))
    133   1.7  jmcneill 				 break;
    134   1.1  jmcneill 		}
    135   1.1  jmcneill 	}
    136   1.6       ryo #endif /* MULTIPROCESSOR */
    137   1.1  jmcneill 
    138  1.12  jmcneill 	/* Assume that less efficient processors are faster. */
    139  1.12  jmcneill 	prop_dictionary_set_uint32(dict, "capacity_dmips_mhz",
    140  1.12  jmcneill 	    gicc->EfficiencyClass);
    141  1.12  jmcneill 
    142   1.4  jmcneill 	/* Store the ACPI Processor UID in cpu_info */
    143   1.4  jmcneill 	ci->ci_acpiid = gicc->Uid;
    144   1.4  jmcneill 
    145  1.16  jmcneill 	/* Scan SRAT for NUMA info. */
    146  1.16  jmcneill 	if (cpu_mpidr_aff_read() == mpidr) {
    147  1.16  jmcneill 		acpisrat_init();
    148  1.16  jmcneill 	}
    149  1.16  jmcneill 	node = acpisrat_get_node(gicc->Uid);
    150  1.16  jmcneill 	if (node != NULL) {
    151  1.16  jmcneill 		ci->ci_numa_id = node->nodeid;
    152  1.16  jmcneill 	}
    153  1.16  jmcneill 
    154   1.1  jmcneill 	/* Attach the CPU */
    155   1.1  jmcneill 	cpu_attach(self, mpidr);
    156   1.5  jmcneill 
    157  1.18  jmcneill 	if (ci->ci_dev == NULL) {
    158  1.18  jmcneill 		/* Not configured */
    159  1.18  jmcneill 		return;
    160  1.18  jmcneill 	}
    161  1.18  jmcneill 
    162  1.17  jmcneill 	/* Probe for low-power idle states. */
    163  1.17  jmcneill 	cpu_acpi_probe_lpi(self, ci);
    164  1.17  jmcneill 
    165   1.5  jmcneill #if NTPROF > 0
    166  1.14  jmcneill 	if (cpu_mpidr_aff_read() == mpidr && armv8_pmu_detect())
    167   1.5  jmcneill 		config_interrupts(self, cpu_acpi_tprof_init);
    168   1.5  jmcneill #endif
    169   1.5  jmcneill }
    170   1.5  jmcneill 
    171  1.17  jmcneill static void
    172  1.17  jmcneill cpu_acpi_probe_lpi(device_t dev, struct cpu_info *ci)
    173  1.17  jmcneill {
    174  1.17  jmcneill 	ACPI_HANDLE hdl;
    175  1.17  jmcneill 	ACPI_BUFFER buf;
    176  1.17  jmcneill 	ACPI_OBJECT *obj, *lpi;
    177  1.17  jmcneill 	ACPI_STATUS rv;
    178  1.17  jmcneill 	uint32_t levelid;
    179  1.17  jmcneill 	uint32_t numlpi;
    180  1.17  jmcneill 	uint32_t n;
    181  1.17  jmcneill 	int enable_lpi;
    182  1.17  jmcneill 
    183  1.17  jmcneill 	if (get_bootconf_option(boot_args, "nolpi",
    184  1.17  jmcneill 				BOOTOPT_TYPE_BOOLEAN, &enable_lpi) &&
    185  1.17  jmcneill 	    !enable_lpi) {
    186  1.17  jmcneill 		return;
    187  1.17  jmcneill 	}
    188  1.17  jmcneill 
    189  1.17  jmcneill 	hdl = acpi_match_cpu_info(ci);
    190  1.17  jmcneill 	if (hdl == NULL) {
    191  1.17  jmcneill 		return;
    192  1.17  jmcneill 	}
    193  1.17  jmcneill 	rv = AcpiGetHandle(hdl, "_LPI", &hdl);
    194  1.17  jmcneill 	if (ACPI_FAILURE(rv)) {
    195  1.17  jmcneill 		return;
    196  1.17  jmcneill 	}
    197  1.17  jmcneill 	rv = acpi_eval_struct(hdl, NULL, &buf);
    198  1.17  jmcneill 	if (ACPI_FAILURE(rv)) {
    199  1.17  jmcneill 		return;
    200  1.17  jmcneill 	}
    201  1.17  jmcneill 
    202  1.17  jmcneill 	obj = buf.Pointer;
    203  1.17  jmcneill 	if (obj->Type != ACPI_TYPE_PACKAGE ||
    204  1.17  jmcneill 	    obj->Package.Count < 3 ||
    205  1.17  jmcneill 	    obj->Package.Elements[1].Type != ACPI_TYPE_INTEGER ||
    206  1.17  jmcneill 	    obj->Package.Elements[2].Type != ACPI_TYPE_INTEGER) {
    207  1.17  jmcneill 		goto out;
    208  1.17  jmcneill 	}
    209  1.17  jmcneill 	levelid = obj->Package.Elements[1].Integer.Value;
    210  1.17  jmcneill 	if (levelid != 0) {
    211  1.17  jmcneill 		/* We depend on platform coordination for now. */
    212  1.17  jmcneill 		goto out;
    213  1.17  jmcneill 	}
    214  1.17  jmcneill 	numlpi = obj->Package.Elements[2].Integer.Value;
    215  1.17  jmcneill 	if (obj->Package.Count < 3 + numlpi || numlpi == 0) {
    216  1.17  jmcneill 		goto out;
    217  1.17  jmcneill 	}
    218  1.17  jmcneill 	ci->ci_lpi = kmem_zalloc(sizeof(*ci->ci_lpi) * numlpi, KM_SLEEP);
    219  1.17  jmcneill 	for (n = 0; n < numlpi; n++) {
    220  1.17  jmcneill 		lpi = &obj->Package.Elements[3 + n];
    221  1.17  jmcneill 		if (lpi->Type != ACPI_TYPE_PACKAGE ||
    222  1.17  jmcneill 		    lpi->Package.Count < 10 ||
    223  1.17  jmcneill 		    lpi->Package.Elements[0].Type != ACPI_TYPE_INTEGER ||
    224  1.17  jmcneill 		    lpi->Package.Elements[1].Type != ACPI_TYPE_INTEGER ||
    225  1.17  jmcneill 		    lpi->Package.Elements[2].Type != ACPI_TYPE_INTEGER ||
    226  1.17  jmcneill 		    lpi->Package.Elements[3].Type != ACPI_TYPE_INTEGER ||
    227  1.17  jmcneill 		    !(lpi->Package.Elements[6].Type == ACPI_TYPE_BUFFER ||
    228  1.17  jmcneill 		      lpi->Package.Elements[6].Type == ACPI_TYPE_INTEGER)) {
    229  1.17  jmcneill 			continue;
    230  1.17  jmcneill 		}
    231  1.17  jmcneill 
    232  1.17  jmcneill 		if ((lpi->Package.Elements[2].Integer.Value & 1) == 0) {
    233  1.17  jmcneill 			/* LPI state is not enabled */
    234  1.17  jmcneill 			continue;
    235  1.17  jmcneill 		}
    236  1.17  jmcneill 
    237  1.17  jmcneill 		ci->ci_lpi[ci->ci_nlpi].min_res
    238  1.17  jmcneill 		    = lpi->Package.Elements[0].Integer.Value;
    239  1.17  jmcneill 		ci->ci_lpi[ci->ci_nlpi].wakeup_latency =
    240  1.17  jmcneill 		    lpi->Package.Elements[1].Integer.Value;
    241  1.17  jmcneill 		ci->ci_lpi[ci->ci_nlpi].save_restore_flags =
    242  1.17  jmcneill 		    lpi->Package.Elements[3].Integer.Value;
    243  1.17  jmcneill 		if (ci->ci_lpi[ci->ci_nlpi].save_restore_flags != 0) {
    244  1.17  jmcneill 			/* Not implemented yet */
    245  1.17  jmcneill 			continue;
    246  1.17  jmcneill 		}
    247  1.17  jmcneill 		if (lpi->Package.Elements[6].Type == ACPI_TYPE_INTEGER) {
    248  1.17  jmcneill 			ci->ci_lpi[ci->ci_nlpi].reg_addr =
    249  1.17  jmcneill 			    lpi->Package.Elements[6].Integer.Value;
    250  1.17  jmcneill 		} else {
    251  1.17  jmcneill 			ACPI_GENERIC_ADDRESS addr;
    252  1.17  jmcneill 
    253  1.17  jmcneill 			KASSERT(lpi->Package.Elements[6].Type ==
    254  1.17  jmcneill 				ACPI_TYPE_BUFFER);
    255  1.17  jmcneill 
    256  1.17  jmcneill 			if (lpi->Package.Elements[6].Buffer.Length <
    257  1.17  jmcneill 			    sizeof(AML_RESOURCE_GENERIC_REGISTER)) {
    258  1.17  jmcneill 				continue;
    259  1.17  jmcneill 			}
    260  1.17  jmcneill 			memcpy(&addr, lpi->Package.Elements[6].Buffer.Pointer +
    261  1.17  jmcneill 			    sizeof(AML_RESOURCE_LARGE_HEADER), sizeof(addr));
    262  1.17  jmcneill 			ci->ci_lpi[ci->ci_nlpi].reg_addr = addr.Address;
    263  1.17  jmcneill 		}
    264  1.17  jmcneill 
    265  1.17  jmcneill 		if (lpi->Package.Elements[9].Type == ACPI_TYPE_STRING) {
    266  1.17  jmcneill 			ci->ci_lpi[ci->ci_nlpi].name =
    267  1.17  jmcneill 			    kmem_asprintf("LPI state %s",
    268  1.17  jmcneill 				lpi->Package.Elements[9].String.Pointer);
    269  1.17  jmcneill 		} else {
    270  1.17  jmcneill 			ci->ci_lpi[ci->ci_nlpi].name =
    271  1.17  jmcneill 			    kmem_asprintf("LPI state %u", n + 1);
    272  1.17  jmcneill 		}
    273  1.17  jmcneill 
    274  1.17  jmcneill 		aprint_verbose_dev(ci->ci_dev,
    275  1.17  jmcneill 		    "%s: min res %u, wakeup latency %u, flags %#x, "
    276  1.17  jmcneill 		    "register %#x\n",
    277  1.17  jmcneill 		    ci->ci_lpi[ci->ci_nlpi].name,
    278  1.17  jmcneill 		    ci->ci_lpi[ci->ci_nlpi].min_res,
    279  1.17  jmcneill 		    ci->ci_lpi[ci->ci_nlpi].wakeup_latency,
    280  1.17  jmcneill 		    ci->ci_lpi[ci->ci_nlpi].save_restore_flags,
    281  1.17  jmcneill 		    ci->ci_lpi[ci->ci_nlpi].reg_addr);
    282  1.17  jmcneill 
    283  1.17  jmcneill 		evcnt_attach_dynamic(&ci->ci_lpi[ci->ci_nlpi].events,
    284  1.17  jmcneill 		    EVCNT_TYPE_MISC, NULL, ci->ci_cpuname,
    285  1.17  jmcneill 		    ci->ci_lpi[ci->ci_nlpi].name);
    286  1.17  jmcneill 
    287  1.17  jmcneill 		ci->ci_nlpi++;
    288  1.17  jmcneill 	}
    289  1.17  jmcneill 
    290  1.17  jmcneill 	if (ci->ci_nlpi > 0) {
    291  1.17  jmcneill 		extern void (*arm_cpu_idle)(void);
    292  1.17  jmcneill 		arm_cpu_idle = cpu_acpi_lpi_idle;
    293  1.17  jmcneill 	}
    294  1.17  jmcneill 
    295  1.17  jmcneill out:
    296  1.17  jmcneill 	ACPI_FREE(buf.Pointer);
    297  1.17  jmcneill }
    298  1.17  jmcneill 
    299  1.17  jmcneill static inline void
    300  1.17  jmcneill cpu_acpi_idle(uint32_t addr)
    301  1.17  jmcneill {
    302  1.17  jmcneill 	if (addr == LPI_REG_ADDR_WFI) {
    303  1.17  jmcneill 		asm volatile("dsb sy; wfi");
    304  1.17  jmcneill 	} else {
    305  1.17  jmcneill 		psci_cpu_suspend(addr);
    306  1.17  jmcneill 	}
    307  1.17  jmcneill }
    308  1.17  jmcneill 
    309  1.17  jmcneill void
    310  1.17  jmcneill cpu_acpi_lpi_idle(void)
    311  1.17  jmcneill {
    312  1.17  jmcneill 	struct cpu_info *ci = curcpu();
    313  1.17  jmcneill 	struct timeval start, end;
    314  1.17  jmcneill 	int n;
    315  1.17  jmcneill 
    316  1.17  jmcneill 	DISABLE_INTERRUPT();
    317  1.17  jmcneill 
    318  1.17  jmcneill 	microuptime(&start);
    319  1.17  jmcneill 	for (n = ci->ci_nlpi - 1; n >= 0; n--) {
    320  1.17  jmcneill 		if (ci->ci_last_idle >
    321  1.17  jmcneill 		    LPI_IDLE_FACTOR * ci->ci_lpi[n].min_res) {
    322  1.17  jmcneill 			cpu_acpi_idle(ci->ci_lpi[n].reg_addr);
    323  1.17  jmcneill 			ci->ci_lpi[n].events.ev_count++;
    324  1.17  jmcneill 			break;
    325  1.17  jmcneill 		}
    326  1.17  jmcneill 	}
    327  1.17  jmcneill 	if (n == -1) {
    328  1.17  jmcneill 		/* Nothing in _LPI, let's just WFI. */
    329  1.17  jmcneill 		cpu_acpi_idle(LPI_REG_ADDR_WFI);
    330  1.17  jmcneill 	}
    331  1.17  jmcneill 	microuptime(&end);
    332  1.17  jmcneill 	timersub(&end, &start, &end);
    333  1.17  jmcneill 
    334  1.17  jmcneill 	ci->ci_last_idle = end.tv_sec * 1000000 + end.tv_usec;
    335  1.17  jmcneill 
    336  1.17  jmcneill 	ENABLE_INTERRUPT();
    337  1.17  jmcneill }
    338  1.17  jmcneill 
    339   1.5  jmcneill #if NTPROF > 0
    340   1.5  jmcneill static struct cpu_info *
    341   1.5  jmcneill cpu_acpi_find_processor(UINT32 uid)
    342   1.5  jmcneill {
    343   1.5  jmcneill 	CPU_INFO_ITERATOR cii;
    344   1.5  jmcneill 	struct cpu_info *ci;
    345   1.5  jmcneill 
    346   1.5  jmcneill 	for (CPU_INFO_FOREACH(cii, ci)) {
    347   1.5  jmcneill 		if (ci->ci_acpiid == uid)
    348   1.5  jmcneill 			return ci;
    349   1.5  jmcneill 	}
    350   1.5  jmcneill 
    351   1.5  jmcneill 	return NULL;
    352   1.5  jmcneill }
    353   1.5  jmcneill 
    354   1.5  jmcneill static ACPI_STATUS
    355   1.5  jmcneill cpu_acpi_tprof_intr_establish(ACPI_SUBTABLE_HEADER *hdrp, void *aux)
    356   1.5  jmcneill {
    357   1.5  jmcneill 	device_t dev = aux;
    358   1.5  jmcneill 	ACPI_MADT_GENERIC_INTERRUPT *gicc;
    359   1.5  jmcneill 	struct cpu_info *ci;
    360   1.5  jmcneill 	char xname[16];
    361   1.5  jmcneill 	kcpuset_t *set;
    362   1.5  jmcneill 	int error;
    363   1.5  jmcneill 	void *ih;
    364   1.5  jmcneill 
    365   1.5  jmcneill 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
    366   1.5  jmcneill 		return AE_OK;
    367   1.5  jmcneill 
    368   1.5  jmcneill 	gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
    369   1.5  jmcneill 	if ((gicc->Flags & ACPI_MADT_ENABLED) == 0)
    370   1.5  jmcneill 		return AE_OK;
    371   1.5  jmcneill 
    372  1.11  jmcneill 	const bool cpu_primary_p = cpu_info_store[0].ci_cpuid == gicc->ArmMpidr;
    373   1.5  jmcneill 	const bool intr_ppi_p = gicc->PerformanceInterrupt < 32;
    374  1.10  jmcneill 	const int type = (gicc->Flags & ACPI_MADT_PERFORMANCE_IRQ_MODE) ?
    375  1.10  jmcneill 	    IST_EDGE : IST_LEVEL;
    376   1.5  jmcneill 
    377   1.5  jmcneill 	if (intr_ppi_p && !cpu_primary_p)
    378   1.5  jmcneill 		return AE_OK;
    379   1.5  jmcneill 
    380   1.5  jmcneill 	ci = cpu_acpi_find_processor(gicc->Uid);
    381   1.5  jmcneill 	if (ci == NULL) {
    382  1.10  jmcneill 		aprint_error_dev(dev, "couldn't find processor %#x\n",
    383  1.10  jmcneill 		    gicc->Uid);
    384   1.5  jmcneill 		return AE_OK;
    385   1.5  jmcneill 	}
    386   1.5  jmcneill 
    387   1.5  jmcneill 	if (intr_ppi_p) {
    388   1.5  jmcneill 		strlcpy(xname, "pmu", sizeof(xname));
    389   1.5  jmcneill 	} else {
    390   1.5  jmcneill 		snprintf(xname, sizeof(xname), "pmu %s", cpu_name(ci));
    391   1.5  jmcneill 	}
    392   1.5  jmcneill 
    393  1.10  jmcneill 	ih = intr_establish_xname(gicc->PerformanceInterrupt, IPL_HIGH,
    394  1.10  jmcneill 	    type | IST_MPSAFE, armv8_pmu_intr, NULL, xname);
    395   1.5  jmcneill 	if (ih == NULL) {
    396  1.10  jmcneill 		aprint_error_dev(dev, "couldn't establish %s interrupt\n",
    397  1.10  jmcneill 		    xname);
    398   1.5  jmcneill 		return AE_OK;
    399   1.5  jmcneill 	}
    400   1.5  jmcneill 
    401   1.5  jmcneill 	if (!intr_ppi_p) {
    402   1.5  jmcneill 		kcpuset_create(&set, true);
    403   1.5  jmcneill 		kcpuset_set(set, cpu_index(ci));
    404   1.5  jmcneill 		error = interrupt_distribute(ih, set, NULL);
    405   1.5  jmcneill 		kcpuset_destroy(set);
    406   1.5  jmcneill 
    407   1.5  jmcneill 		if (error) {
    408  1.10  jmcneill 			aprint_error_dev(dev,
    409  1.10  jmcneill 			    "failed to distribute %s interrupt: %d\n",
    410   1.5  jmcneill 			    xname, error);
    411   1.5  jmcneill 			return AE_OK;
    412   1.5  jmcneill 		}
    413   1.5  jmcneill 	}
    414   1.5  jmcneill 
    415  1.10  jmcneill 	aprint_normal("%s: PMU interrupting on irq %d\n", cpu_name(ci),
    416  1.10  jmcneill 	    gicc->PerformanceInterrupt);
    417   1.5  jmcneill 
    418   1.5  jmcneill 	return AE_OK;
    419   1.5  jmcneill }
    420   1.5  jmcneill 
    421   1.5  jmcneill static void
    422   1.5  jmcneill cpu_acpi_tprof_init(device_t self)
    423   1.5  jmcneill {
    424  1.13     skrll 	int err = armv8_pmu_init();
    425  1.13     skrll 	if (err) {
    426  1.13     skrll 		aprint_error_dev(self,
    427  1.13     skrll 		    "failed to initialize PMU event counter\n");
    428  1.13     skrll 		return;
    429  1.13     skrll 	}
    430   1.5  jmcneill 
    431   1.5  jmcneill 	if (acpi_madt_map() != AE_OK) {
    432  1.10  jmcneill 		aprint_error_dev(self,
    433  1.10  jmcneill 		    "failed to map MADT, performance counters not available\n");
    434   1.5  jmcneill 		return;
    435   1.5  jmcneill 	}
    436   1.5  jmcneill 	acpi_madt_walk(cpu_acpi_tprof_intr_establish, self);
    437   1.5  jmcneill 	acpi_madt_unmap();
    438   1.1  jmcneill }
    439   1.5  jmcneill #endif
    440