Home | History | Annotate | Line # | Download | only in acpi
cpu_acpi.c revision 1.3
      1 /* $NetBSD: cpu_acpi.c,v 1.3 2018/10/18 09:01:52 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: cpu_acpi.c,v 1.3 2018/10/18 09:01:52 skrll Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/cpu.h>
     38 #include <sys/device.h>
     39 
     40 #include <dev/acpi/acpireg.h>
     41 #include <dev/acpi/acpivar.h>
     42 
     43 #include <arm/armreg.h>
     44 #include <arm/cpu.h>
     45 #include <arm/cpufunc.h>
     46 #include <arm/locore.h>
     47 
     48 #include <arm/arm/psci.h>
     49 
     50 static int	cpu_acpi_match(device_t, cfdata_t, void *);
     51 static void	cpu_acpi_attach(device_t, device_t, void *);
     52 
     53 CFATTACH_DECL_NEW(cpu_acpi, 0, cpu_acpi_match, cpu_acpi_attach, NULL, NULL);
     54 
     55 static register_t
     56 cpu_acpi_mpstart_pa(void)
     57 {
     58 
     59 	return (register_t)KERN_VTOPHYS((vaddr_t)cpu_mpstart);
     60 }
     61 
     62 static int
     63 cpu_acpi_match(device_t parent, cfdata_t cf, void *aux)
     64 {
     65 	ACPI_SUBTABLE_HEADER *hdrp = aux;
     66 	ACPI_MADT_GENERIC_INTERRUPT *gicc;
     67 
     68 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
     69 		return 0;
     70 
     71 	gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
     72 
     73 	return (gicc->Flags & ACPI_MADT_ENABLED) != 0;
     74 }
     75 
     76 static void
     77 cpu_acpi_attach(device_t parent, device_t self, void *aux)
     78 {
     79 	ACPI_MADT_GENERIC_INTERRUPT *gicc = aux;
     80 	const uint64_t mpidr = gicc->ArmMpidr;
     81 	int error;
     82 
     83 	if (cpu_mpidr_aff_read() != mpidr) {
     84 		const u_int cpuindex = device_unit(self);
     85 
     86 		cpu_mpidr[cpuindex] = mpidr;
     87 		cpu_dcache_wb_range((vaddr_t)&cpu_mpidr[cpuindex], sizeof(cpu_mpidr[cpuindex]));
     88 
     89 		/* XXX support spin table */
     90 		error = psci_cpu_on(mpidr, cpu_acpi_mpstart_pa(), 0);
     91 		if (error != PSCI_SUCCESS) {
     92 			aprint_error_dev(self, "failed to start CPU\n");
     93 			return;
     94 		}
     95 
     96 		__asm __volatile("sev" ::: "memory");
     97 
     98 		for (u_int i = 0x10000000; i > 0; i--) {
     99 			membar_consumer();
    100 			if (arm_cpu_hatched & __BIT(cpuindex))
    101 				break;
    102 		}
    103 	}
    104 
    105 	/* Attach the CPU */
    106 	cpu_attach(self, mpidr);
    107 }
    108