Home | History | Annotate | Line # | Download | only in apple
apple_platform.c revision 1.3
      1 /* $NetBSD: apple_platform.c,v 1.3 2021/09/02 20:57:57 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2021 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: apple_platform.c,v 1.3 2021/09/02 20:57:57 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/cpu.h>
     35 #include <sys/device.h>
     36 #include <sys/termios.h>
     37 
     38 #include <dev/fdt/fdtvar.h>
     39 #include <arm/fdt/arm_fdtvar.h>
     40 
     41 #include <uvm/uvm_extern.h>
     42 
     43 #include <machine/bootconfig.h>
     44 
     45 #include <net/if_ether.h>
     46 
     47 #include <dev/pci/pcivar.h>
     48 #include <machine/pci_machdep.h>
     49 
     50 #include <arm/cpufunc.h>
     51 
     52 #include <arm/cortex/gtmr_var.h>
     53 
     54 #include <arm/arm/psci.h>
     55 #include <arm/fdt/psci_fdtvar.h>
     56 
     57 #include <libfdt.h>
     58 
     59 #include <arch/evbarm/fdt/platform.h>
     60 
     61 extern struct bus_space arm_generic_bs_tag;
     62 
     63 struct arm32_bus_dma_tag apple_coherent_dma_tag;
     64 static struct arm32_dma_range apple_coherent_ranges[] = {
     65 	[0] = {
     66 		.dr_sysbase = 0,
     67 		.dr_busbase = 0,
     68 		.dr_len = UINTPTR_MAX,
     69 		.dr_flags = _BUS_DMAMAP_COHERENT,
     70 	}
     71 };
     72 
     73 static void
     74 apple_platform_bootstrap(void)
     75 {
     76 	extern struct arm32_bus_dma_tag arm_generic_dma_tag;
     77 
     78 	apple_coherent_dma_tag = arm_generic_dma_tag;
     79 	apple_coherent_dma_tag._ranges = apple_coherent_ranges;
     80 	apple_coherent_dma_tag._nranges = __arraycount(apple_coherent_ranges);
     81 
     82 	arm_fdt_cpu_bootstrap();
     83 }
     84 
     85 static void
     86 apple_platform_init_attach_args(struct fdt_attach_args *faa)
     87 {
     88 	faa->faa_bst = &arm_generic_bs_tag;
     89 	faa->faa_dmat = &apple_coherent_dma_tag;
     90 }
     91 
     92 static const struct pmap_devmap *
     93 apple_platform_devmap(void)
     94 {
     95 	/* Size this to hold possible entries for the UART and SMP spin-table */
     96 	static struct pmap_devmap devmap[] = {
     97 		DEVMAP_ENTRY_END,
     98 		DEVMAP_ENTRY_END,
     99 		DEVMAP_ENTRY_END
    100 	};
    101 	bus_addr_t uart_base;
    102 	vaddr_t devmap_va = KERNEL_IO_VBASE;
    103 	u_int devmap_index = 0;
    104 	uint64_t release_addr;
    105 	int phandle;
    106 
    107 	phandle = fdtbus_get_stdout_phandle();
    108 	if (phandle > 0 && fdtbus_get_reg(phandle, 0, &uart_base, NULL) == 0) {
    109 		devmap[devmap_index].pd_pa = DEVMAP_ALIGN(uart_base);
    110 		devmap[devmap_index].pd_va = DEVMAP_ALIGN(devmap_va);
    111 		devmap[devmap_index].pd_size = DEVMAP_SIZE(L3_SIZE);
    112 		devmap[devmap_index].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
    113 		devmap[devmap_index].pd_flags = PMAP_DEV_SO;
    114 		devmap_va = DEVMAP_SIZE(devmap[devmap_index].pd_va +
    115 		    devmap[devmap_index].pd_size);
    116 		devmap_index++;
    117 	}
    118 
    119 	/* XXX hopefully all release addresses are in the same 2M */
    120 	phandle = OF_finddevice("/cpus/cpu@1");
    121 	if (phandle > 0 &&
    122 	    of_getprop_uint64(phandle, "cpu-release-addr", &release_addr) == 0) {
    123 		devmap[devmap_index].pd_pa = DEVMAP_ALIGN(release_addr);
    124 		devmap[devmap_index].pd_va = DEVMAP_ALIGN(devmap_va);
    125 		devmap[devmap_index].pd_size = DEVMAP_SIZE(L2_SIZE);
    126 		devmap[devmap_index].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
    127 		devmap[devmap_index].pd_flags = PMAP_WRITE_BACK;
    128 		devmap_va = DEVMAP_SIZE(devmap[devmap_index].pd_va +
    129 		    devmap[devmap_index].pd_size);
    130 		devmap_index++;
    131 	}
    132 
    133 	return devmap;
    134 }
    135 
    136 static u_int
    137 arm_platform_uart_freq(void)
    138 {
    139 	return 0;
    140 }
    141 
    142 static int
    143 apple_platform_get_mac_address(pci_chipset_tag_t pc, pcitag_t tag,
    144     uint8_t *eaddr)
    145 {
    146 	int b, d, f;
    147 	int bridge, len;
    148 	u_int bdf;
    149 
    150 	const int pcie = of_find_bycompat(OF_finddevice("/"), "apple,pcie");
    151 	if (pcie == -1) {
    152 		return -1;
    153 	}
    154 
    155 	/* Convert PCI tag to encoding of phys.hi for PCI-PCI bridge regs */
    156 	pci_decompose_tag(pc, tag, &b, &d, &f);
    157 	bdf = (b << 16) | (d << 11) | (f << 8);
    158 
    159 	for (bridge = OF_child(pcie); bridge; bridge = OF_peer(bridge)) {
    160 		const int ethernet =
    161 		    of_find_firstchild_byname(bridge, "ethernet");
    162 		if (ethernet == -1) {
    163 			continue;
    164 		}
    165 		const u_int *data = fdtbus_get_prop(ethernet, "reg", &len);
    166 		if (data == NULL || len < 4) {
    167 			continue;
    168 		}
    169 		if (bdf != be32toh(data[0])) {
    170 			continue;
    171 		}
    172 
    173 		return OF_getprop(ethernet, "local-mac-address",
    174 		    eaddr, ETHER_ADDR_LEN);
    175 	}
    176 
    177 	return -1;
    178 }
    179 
    180 static void
    181 apple_platform_device_register(device_t self, void *aux)
    182 {
    183 	prop_dictionary_t prop = device_properties(self);
    184 	uint8_t eaddr[ETHER_ADDR_LEN];
    185 	int len;
    186 
    187 	if (device_is_a(self, "cpu")) {
    188 		struct fdt_attach_args * const faa = aux;
    189 		bus_addr_t cpuid;
    190 
    191 		if (fdtbus_get_reg(faa->faa_phandle, 0, &cpuid, NULL) != 0) {
    192 			cpuid = 0;
    193 		}
    194 
    195 		/*
    196 		 * On Apple M1 (and hopefully later models), AFF2 is 0 for
    197 		 * efficiency and 1 for performance cores. Use this value
    198 		 * to provide a fake DMIPS/MHz value -- the actual number
    199 		 * only matters in relation to the value presented by other
    200 		 * cores.
    201 		 */
    202 		const u_int aff2 = __SHIFTOUT(cpuid, MPIDR_AFF2);
    203 		prop_dictionary_set_uint32(prop, "capacity_dmips_mhz", aff2);
    204 		return;
    205 	}
    206 
    207 	if (device_is_a(self, "bge") &&
    208 	    device_is_a(device_parent(self), "pci")) {
    209 		struct pci_attach_args * const pa = aux;
    210 
    211 		len = apple_platform_get_mac_address(pa->pa_pc, pa->pa_tag,
    212 		    eaddr);
    213 		if (len == ETHER_ADDR_LEN) {
    214 			prop_dictionary_set_bool(prop, "without-seeprom", true);
    215 			prop_dictionary_set_data(prop, "mac-address", eaddr,
    216 			    sizeof(eaddr));
    217 		}
    218 		return;
    219 	}
    220 }
    221 
    222 static const struct arm_platform apple_arm_platform = {
    223 	.ap_devmap = apple_platform_devmap,
    224 	.ap_bootstrap = apple_platform_bootstrap,
    225 	.ap_init_attach_args = apple_platform_init_attach_args,
    226 	.ap_reset = psci_fdt_reset,
    227 	.ap_delay = gtmr_delay,
    228 	.ap_uart_freq = arm_platform_uart_freq,
    229 	.ap_device_register = apple_platform_device_register,
    230 	.ap_mpstart = arm_fdt_cpu_mpstart,
    231 };
    232 
    233 ARM_PLATFORM(apple_arm, "apple,arm-platform", &apple_arm_platform);
    234