Home | History | Annotate | Line # | Download | only in fdt
cpu_fdt.c revision 1.4
      1 /* $NetBSD: cpu_fdt.c,v 1.4 2017/12/10 21:38:26 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared McNeill <jmcneill (at) 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: cpu_fdt.c,v 1.4 2017/12/10 21:38:26 skrll Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/lwp.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 
     39 #include <dev/fdt/fdtvar.h>
     40 
     41 #include <arm/cpu.h>
     42 
     43 static int	cpu_fdt_match(device_t, cfdata_t, void *);
     44 static void	cpu_fdt_attach(device_t, device_t, void *);
     45 
     46 enum cpu_fdt_type {
     47 	ARM_CPU_UP = 1,
     48 	ARM_CPU_ARMV7,
     49 	ARM_CPU_ARMV8,
     50 };
     51 
     52 struct cpu_fdt_softc {
     53 	device_t		sc_dev;
     54 	int			sc_phandle;
     55 };
     56 
     57 static const struct of_compat_data compat_data[] = {
     58 	{ "arm,arm1176jzf-s",		ARM_CPU_UP },
     59 
     60 	{ "arm,cortex-a5",		ARM_CPU_ARMV7 },
     61 	{ "arm,cortex-a7",		ARM_CPU_ARMV7 },
     62 	{ "arm,cortex-a8",		ARM_CPU_ARMV7 },
     63 	{ "arm,cortex-a9",		ARM_CPU_ARMV7 },
     64 	{ "arm,cortex-a12",		ARM_CPU_ARMV7 },
     65 	{ "arm,cortex-a15",		ARM_CPU_ARMV7 },
     66 	{ "arm,cortex-a17",		ARM_CPU_ARMV7 },
     67 
     68 	{ "arm,cortex-a53",		ARM_CPU_ARMV8 },
     69 	{ "arm,cortex-a57",		ARM_CPU_ARMV8 },
     70 	{ "arm,cortex-a72",		ARM_CPU_ARMV8 },
     71 	{ "arm,cortex-a73",		ARM_CPU_ARMV8 },
     72 	{ NULL }
     73 };
     74 
     75 CFATTACH_DECL_NEW(cpu_fdt, sizeof(struct cpu_fdt_softc),
     76 	cpu_fdt_match, cpu_fdt_attach, NULL, NULL);
     77 
     78 static int
     79 cpu_fdt_match(device_t parent, cfdata_t cf, void *aux)
     80 {
     81 	struct fdt_attach_args * const faa = aux;
     82 	const int phandle = faa->faa_phandle;
     83 	enum cpu_fdt_type type;
     84 	int is_compatible;
     85 	bus_addr_t mpidr;
     86 
     87 	is_compatible = of_match_compat_data(phandle, compat_data);
     88 	if (!is_compatible)
     89 		return 0;
     90 
     91 	type = of_search_compatible(phandle, compat_data)->data;
     92 	switch (type) {
     93 	case ARM_CPU_ARMV7:
     94 	case ARM_CPU_ARMV8:
     95 		/* XXX NetBSD requires all CPUs to be in the same cluster */
     96 		if (fdtbus_get_reg(phandle, 0, &mpidr, NULL) != 0)
     97 			return 0;
     98 		const uint32_t bp_mpidr = armreg_mpidr_read();
     99 		const u_int bp_clid = __SHIFTOUT(bp_mpidr, CORTEXA9_MPIDR_CLID);
    100 		const u_int clid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CLID);
    101 		if (bp_clid != clid)
    102 			return 0;
    103 		break;
    104 	default:
    105 		break;
    106 	}
    107 
    108 	return is_compatible;
    109 }
    110 
    111 static void
    112 cpu_fdt_attach(device_t parent, device_t self, void *aux)
    113 {
    114 	struct cpu_fdt_softc * const sc = device_private(self);
    115 	struct fdt_attach_args * const faa = aux;
    116 	const int phandle = faa->faa_phandle;
    117 	enum cpu_fdt_type type;
    118 	bus_addr_t mpidr;
    119 	cpuid_t cpuid;
    120 
    121 	sc->sc_dev = self;
    122 	sc->sc_phandle = phandle;
    123 
    124 	type = of_search_compatible(phandle, compat_data)->data;
    125 
    126 	switch (type) {
    127 	case ARM_CPU_ARMV7:
    128 	case ARM_CPU_ARMV8:
    129 		if (fdtbus_get_reg(phandle, 0, &mpidr, NULL) != 0) {
    130 			aprint_error(": missing 'reg' property\n");
    131 			return;
    132 		}
    133 		cpuid = __SHIFTOUT(mpidr, CORTEXA9_MPIDR_CPUID);
    134 		break;
    135 	default:
    136 		cpuid = 0;
    137 		break;
    138 	}
    139 
    140 	/* Attach the CPU */
    141 	cpu_attach(self, cpuid);
    142 }
    143