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