apple_platform.c revision 1.2 1 /* $NetBSD: apple_platform.c,v 1.2 2021/09/01 23:05:03 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.2 2021/09/01 23:05:03 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 /*
62 * Faux DMIPS/MHz values for known CPU types. The values themselves are
63 * unimportant except as relative comparisons between different CPUs on
64 * the same system.
65 */
66 static const struct device_compatible_entry cpu_capacity_compat_data[] = {
67 /* Apple M1 */
68 { .compat = "apple,icestorm", .value = 0 }, /* efficiency core */
69 { .compat = "apple,firestorm", .value = 1 }, /* performance core */
70 DEVICE_COMPAT_EOL
71 };
72
73 extern struct bus_space arm_generic_bs_tag;
74
75 struct arm32_bus_dma_tag apple_coherent_dma_tag;
76 static struct arm32_dma_range apple_coherent_ranges[] = {
77 [0] = {
78 .dr_sysbase = 0,
79 .dr_busbase = 0,
80 .dr_len = UINTPTR_MAX,
81 .dr_flags = _BUS_DMAMAP_COHERENT,
82 }
83 };
84
85 static void
86 apple_platform_bootstrap(void)
87 {
88 extern struct arm32_bus_dma_tag arm_generic_dma_tag;
89
90 apple_coherent_dma_tag = arm_generic_dma_tag;
91 apple_coherent_dma_tag._ranges = apple_coherent_ranges;
92 apple_coherent_dma_tag._nranges = __arraycount(apple_coherent_ranges);
93
94 arm_fdt_cpu_bootstrap();
95 }
96
97 static void
98 apple_platform_init_attach_args(struct fdt_attach_args *faa)
99 {
100 faa->faa_bst = &arm_generic_bs_tag;
101 faa->faa_dmat = &apple_coherent_dma_tag;
102 }
103
104 static const struct pmap_devmap *
105 apple_platform_devmap(void)
106 {
107 /* Size this to hold possible entries for the UART and SMP spin-table */
108 static struct pmap_devmap devmap[] = {
109 DEVMAP_ENTRY_END,
110 DEVMAP_ENTRY_END,
111 DEVMAP_ENTRY_END
112 };
113 bus_addr_t uart_base;
114 vaddr_t devmap_va = KERNEL_IO_VBASE;
115 u_int devmap_index = 0;
116 uint64_t release_addr;
117 int phandle;
118
119 phandle = fdtbus_get_stdout_phandle();
120 if (phandle > 0 && fdtbus_get_reg(phandle, 0, &uart_base, NULL) == 0) {
121 devmap[devmap_index].pd_pa = DEVMAP_ALIGN(uart_base);
122 devmap[devmap_index].pd_va = DEVMAP_ALIGN(devmap_va);
123 devmap[devmap_index].pd_size = DEVMAP_SIZE(L3_SIZE);
124 devmap[devmap_index].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
125 devmap[devmap_index].pd_flags = PMAP_DEV_SO;
126 devmap_va = DEVMAP_SIZE(devmap[devmap_index].pd_va +
127 devmap[devmap_index].pd_size);
128 devmap_index++;
129 }
130
131 /* XXX hopefully all release addresses are in the same 2M */
132 phandle = OF_finddevice("/cpus/cpu@1");
133 if (phandle > 0 &&
134 of_getprop_uint64(phandle, "cpu-release-addr", &release_addr) == 0) {
135 devmap[devmap_index].pd_pa = DEVMAP_ALIGN(release_addr);
136 devmap[devmap_index].pd_va = DEVMAP_ALIGN(devmap_va);
137 devmap[devmap_index].pd_size = DEVMAP_SIZE(L2_SIZE);
138 devmap[devmap_index].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
139 devmap[devmap_index].pd_flags = PMAP_WRITE_BACK;
140 devmap_va = DEVMAP_SIZE(devmap[devmap_index].pd_va +
141 devmap[devmap_index].pd_size);
142 devmap_index++;
143 }
144
145 return devmap;
146 }
147
148 static u_int
149 arm_platform_uart_freq(void)
150 {
151 return 0;
152 }
153
154 static int
155 apple_platform_get_mac_address(pci_chipset_tag_t pc, pcitag_t tag,
156 uint8_t *eaddr)
157 {
158 int b, d, f;
159 int bridge, len;
160 u_int bdf;
161
162 const int pcie = of_find_bycompat(OF_finddevice("/"), "apple,pcie");
163 if (pcie == -1) {
164 return -1;
165 }
166
167 /* Convert PCI tag to encoding of phys.hi for PCI-PCI bridge regs */
168 pci_decompose_tag(pc, tag, &b, &d, &f);
169 bdf = (b << 16) | (d << 11) | (f << 8);
170
171 for (bridge = OF_child(pcie); bridge; bridge = OF_peer(bridge)) {
172 const int ethernet =
173 of_find_firstchild_byname(bridge, "ethernet");
174 if (ethernet == -1) {
175 continue;
176 }
177 const u_int *data = fdtbus_get_prop(ethernet, "reg", &len);
178 if (data == NULL || len < 4) {
179 continue;
180 }
181 if (bdf != be32toh(data[0])) {
182 continue;
183 }
184
185 return OF_getprop(ethernet, "local-mac-address",
186 eaddr, ETHER_ADDR_LEN);
187 }
188
189 return -1;
190 }
191
192 static void
193 apple_platform_device_register(device_t self, void *aux)
194 {
195 prop_dictionary_t prop = device_properties(self);
196 uint8_t eaddr[ETHER_ADDR_LEN];
197 int len;
198
199 if (device_is_a(self, "cpu")) {
200 struct fdt_attach_args * const faa = aux;
201 const int phandle = faa->faa_phandle;
202 const struct device_compatible_entry *dce;
203
204 dce = of_compatible_lookup(phandle, cpu_capacity_compat_data);
205 if (dce != NULL) {
206 prop_dictionary_set_uint32(prop, "capacity_dmips_mhz",
207 dce->value);
208 }
209 return;
210 }
211
212 if (device_is_a(self, "bge") &&
213 device_is_a(device_parent(self), "pci")) {
214 struct pci_attach_args * const pa = aux;
215
216 len = apple_platform_get_mac_address(pa->pa_pc, pa->pa_tag,
217 eaddr);
218 if (len == ETHER_ADDR_LEN) {
219 prop_dictionary_set_bool(prop, "without-seeprom", true);
220 prop_dictionary_set_data(prop, "mac-address", eaddr,
221 sizeof(eaddr));
222 }
223 return;
224 }
225 }
226
227 static const struct arm_platform apple_arm_platform = {
228 .ap_devmap = apple_platform_devmap,
229 .ap_bootstrap = apple_platform_bootstrap,
230 .ap_init_attach_args = apple_platform_init_attach_args,
231 .ap_reset = psci_fdt_reset,
232 .ap_delay = gtmr_delay,
233 .ap_uart_freq = arm_platform_uart_freq,
234 .ap_device_register = apple_platform_device_register,
235 .ap_mpstart = arm_fdt_cpu_mpstart,
236 };
237
238 ARM_PLATFORM(apple_arm, "apple,arm-platform", &apple_arm_platform);
239