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