Home | History | Annotate | Line # | Download | only in acpi
cpu_acpi.c revision 1.4.2.2
      1 /* $NetBSD: cpu_acpi.c,v 1.4.2.2 2018/10/20 06:58:24 pgoyette 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.4.2.2 2018/10/20 06:58:24 pgoyette 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 extern struct cpu_info cpu_info_store[];
     51 
     52 static int	cpu_acpi_match(device_t, cfdata_t, void *);
     53 static void	cpu_acpi_attach(device_t, device_t, void *);
     54 
     55 CFATTACH_DECL_NEW(cpu_acpi, 0, cpu_acpi_match, cpu_acpi_attach, NULL, NULL);
     56 
     57 static register_t
     58 cpu_acpi_mpstart_pa(void)
     59 {
     60 
     61 	return (register_t)KERN_VTOPHYS((vaddr_t)cpu_mpstart);
     62 }
     63 
     64 static int
     65 cpu_acpi_match(device_t parent, cfdata_t cf, void *aux)
     66 {
     67 	ACPI_SUBTABLE_HEADER *hdrp = aux;
     68 	ACPI_MADT_GENERIC_INTERRUPT *gicc;
     69 
     70 	if (hdrp->Type != ACPI_MADT_TYPE_GENERIC_INTERRUPT)
     71 		return 0;
     72 
     73 	gicc = (ACPI_MADT_GENERIC_INTERRUPT *)hdrp;
     74 
     75 	return (gicc->Flags & ACPI_MADT_ENABLED) != 0;
     76 }
     77 
     78 static void
     79 cpu_acpi_attach(device_t parent, device_t self, void *aux)
     80 {
     81 	ACPI_MADT_GENERIC_INTERRUPT *gicc = aux;
     82 	const uint64_t mpidr = gicc->ArmMpidr;
     83 	const int unit = device_unit(self);
     84 	struct cpu_info *ci = &cpu_info_store[unit];
     85 	int error;
     86 
     87 	if (cpu_mpidr_aff_read() != mpidr) {
     88 		const u_int cpuindex = device_unit(self);
     89 
     90 		cpu_mpidr[cpuindex] = mpidr;
     91 		cpu_dcache_wb_range((vaddr_t)&cpu_mpidr[cpuindex], sizeof(cpu_mpidr[cpuindex]));
     92 
     93 		/* XXX support spin table */
     94 		error = psci_cpu_on(mpidr, cpu_acpi_mpstart_pa(), 0);
     95 		if (error != PSCI_SUCCESS) {
     96 			aprint_error_dev(self, "failed to start CPU\n");
     97 			return;
     98 		}
     99 
    100 		__asm __volatile("sev" ::: "memory");
    101 
    102 		for (u_int i = 0x10000000; i > 0; i--) {
    103 			membar_consumer();
    104 			if (arm_cpu_hatched & __BIT(cpuindex))
    105 				break;
    106 		}
    107 	}
    108 
    109 	/* Store the ACPI Processor UID in cpu_info */
    110 	ci->ci_acpiid = gicc->Uid;
    111 
    112 	/* Attach the CPU */
    113 	cpu_attach(self, mpidr);
    114 }
    115