apple_platform.c revision 1.6 1 1.6 skrll /* $NetBSD: apple_platform.c,v 1.6 2023/04/07 08:55:29 skrll Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2021 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.6 skrll __KERNEL_RCSID(0, "$NetBSD: apple_platform.c,v 1.6 2023/04/07 08:55:29 skrll Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/cpu.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/termios.h>
37 1.1 jmcneill
38 1.1 jmcneill #include <dev/fdt/fdtvar.h>
39 1.6 skrll
40 1.1 jmcneill #include <arm/fdt/arm_fdtvar.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <uvm/uvm_extern.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <machine/bootconfig.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <net/if_ether.h>
47 1.1 jmcneill
48 1.1 jmcneill #include <dev/pci/pcivar.h>
49 1.1 jmcneill #include <machine/pci_machdep.h>
50 1.1 jmcneill
51 1.1 jmcneill #include <arm/cpufunc.h>
52 1.1 jmcneill
53 1.1 jmcneill #include <arm/cortex/gtmr_var.h>
54 1.1 jmcneill
55 1.1 jmcneill #include <arm/arm/psci.h>
56 1.1 jmcneill #include <arm/fdt/psci_fdtvar.h>
57 1.1 jmcneill
58 1.1 jmcneill #include <libfdt.h>
59 1.1 jmcneill
60 1.1 jmcneill #include <arch/evbarm/fdt/platform.h>
61 1.1 jmcneill
62 1.1 jmcneill extern struct bus_space arm_generic_bs_tag;
63 1.1 jmcneill
64 1.4 jmcneill static struct bus_space apple_nonposted_bs_tag;
65 1.4 jmcneill
66 1.1 jmcneill struct arm32_bus_dma_tag apple_coherent_dma_tag;
67 1.1 jmcneill static struct arm32_dma_range apple_coherent_ranges[] = {
68 1.1 jmcneill [0] = {
69 1.1 jmcneill .dr_sysbase = 0,
70 1.1 jmcneill .dr_busbase = 0,
71 1.1 jmcneill .dr_len = UINTPTR_MAX,
72 1.1 jmcneill .dr_flags = _BUS_DMAMAP_COHERENT,
73 1.1 jmcneill }
74 1.1 jmcneill };
75 1.1 jmcneill
76 1.4 jmcneill static int
77 1.4 jmcneill apple_nonposted_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
78 1.4 jmcneill bus_space_handle_t *bshp)
79 1.4 jmcneill {
80 1.4 jmcneill if (flag == 0) {
81 1.5 jmcneill flag |= BUS_SPACE_MAP_NONPOSTED;
82 1.4 jmcneill }
83 1.4 jmcneill
84 1.4 jmcneill return bus_space_map(&arm_generic_bs_tag, bpa, size, flag, bshp);
85 1.4 jmcneill }
86 1.4 jmcneill
87 1.1 jmcneill static void
88 1.1 jmcneill apple_platform_bootstrap(void)
89 1.1 jmcneill {
90 1.1 jmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag;
91 1.1 jmcneill
92 1.4 jmcneill apple_nonposted_bs_tag = arm_generic_bs_tag;
93 1.4 jmcneill apple_nonposted_bs_tag.bs_map = apple_nonposted_bs_map;
94 1.4 jmcneill
95 1.1 jmcneill apple_coherent_dma_tag = arm_generic_dma_tag;
96 1.1 jmcneill apple_coherent_dma_tag._ranges = apple_coherent_ranges;
97 1.1 jmcneill apple_coherent_dma_tag._nranges = __arraycount(apple_coherent_ranges);
98 1.1 jmcneill
99 1.1 jmcneill arm_fdt_cpu_bootstrap();
100 1.1 jmcneill }
101 1.1 jmcneill
102 1.1 jmcneill static void
103 1.1 jmcneill apple_platform_init_attach_args(struct fdt_attach_args *faa)
104 1.1 jmcneill {
105 1.4 jmcneill faa->faa_bst = &apple_nonposted_bs_tag;
106 1.1 jmcneill faa->faa_dmat = &apple_coherent_dma_tag;
107 1.1 jmcneill }
108 1.1 jmcneill
109 1.1 jmcneill static const struct pmap_devmap *
110 1.1 jmcneill apple_platform_devmap(void)
111 1.1 jmcneill {
112 1.1 jmcneill /* Size this to hold possible entries for the UART and SMP spin-table */
113 1.1 jmcneill static struct pmap_devmap devmap[] = {
114 1.1 jmcneill DEVMAP_ENTRY_END,
115 1.1 jmcneill DEVMAP_ENTRY_END,
116 1.1 jmcneill DEVMAP_ENTRY_END
117 1.1 jmcneill };
118 1.1 jmcneill bus_addr_t uart_base;
119 1.1 jmcneill vaddr_t devmap_va = KERNEL_IO_VBASE;
120 1.1 jmcneill u_int devmap_index = 0;
121 1.1 jmcneill uint64_t release_addr;
122 1.1 jmcneill int phandle;
123 1.1 jmcneill
124 1.1 jmcneill phandle = fdtbus_get_stdout_phandle();
125 1.1 jmcneill if (phandle > 0 && fdtbus_get_reg(phandle, 0, &uart_base, NULL) == 0) {
126 1.1 jmcneill devmap[devmap_index].pd_pa = DEVMAP_ALIGN(uart_base);
127 1.1 jmcneill devmap[devmap_index].pd_va = DEVMAP_ALIGN(devmap_va);
128 1.1 jmcneill devmap[devmap_index].pd_size = DEVMAP_SIZE(L3_SIZE);
129 1.1 jmcneill devmap[devmap_index].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
130 1.5 jmcneill devmap[devmap_index].pd_flags = PMAP_DEV_NP;
131 1.1 jmcneill devmap_va = DEVMAP_SIZE(devmap[devmap_index].pd_va +
132 1.1 jmcneill devmap[devmap_index].pd_size);
133 1.1 jmcneill devmap_index++;
134 1.1 jmcneill }
135 1.1 jmcneill
136 1.1 jmcneill /* XXX hopefully all release addresses are in the same 2M */
137 1.1 jmcneill phandle = OF_finddevice("/cpus/cpu@1");
138 1.1 jmcneill if (phandle > 0 &&
139 1.1 jmcneill of_getprop_uint64(phandle, "cpu-release-addr", &release_addr) == 0) {
140 1.1 jmcneill devmap[devmap_index].pd_pa = DEVMAP_ALIGN(release_addr);
141 1.1 jmcneill devmap[devmap_index].pd_va = DEVMAP_ALIGN(devmap_va);
142 1.1 jmcneill devmap[devmap_index].pd_size = DEVMAP_SIZE(L2_SIZE);
143 1.1 jmcneill devmap[devmap_index].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
144 1.1 jmcneill devmap[devmap_index].pd_flags = PMAP_WRITE_BACK;
145 1.1 jmcneill devmap_va = DEVMAP_SIZE(devmap[devmap_index].pd_va +
146 1.1 jmcneill devmap[devmap_index].pd_size);
147 1.1 jmcneill devmap_index++;
148 1.1 jmcneill }
149 1.1 jmcneill
150 1.1 jmcneill return devmap;
151 1.1 jmcneill }
152 1.1 jmcneill
153 1.1 jmcneill static u_int
154 1.6 skrll apple_platform_uart_freq(void)
155 1.1 jmcneill {
156 1.1 jmcneill return 0;
157 1.1 jmcneill }
158 1.1 jmcneill
159 1.1 jmcneill static int
160 1.1 jmcneill apple_platform_get_mac_address(pci_chipset_tag_t pc, pcitag_t tag,
161 1.1 jmcneill uint8_t *eaddr)
162 1.1 jmcneill {
163 1.1 jmcneill int b, d, f;
164 1.1 jmcneill int bridge, len;
165 1.1 jmcneill u_int bdf;
166 1.1 jmcneill
167 1.1 jmcneill const int pcie = of_find_bycompat(OF_finddevice("/"), "apple,pcie");
168 1.1 jmcneill if (pcie == -1) {
169 1.1 jmcneill return -1;
170 1.1 jmcneill }
171 1.1 jmcneill
172 1.1 jmcneill /* Convert PCI tag to encoding of phys.hi for PCI-PCI bridge regs */
173 1.1 jmcneill pci_decompose_tag(pc, tag, &b, &d, &f);
174 1.1 jmcneill bdf = (b << 16) | (d << 11) | (f << 8);
175 1.1 jmcneill
176 1.1 jmcneill for (bridge = OF_child(pcie); bridge; bridge = OF_peer(bridge)) {
177 1.1 jmcneill const int ethernet =
178 1.1 jmcneill of_find_firstchild_byname(bridge, "ethernet");
179 1.1 jmcneill if (ethernet == -1) {
180 1.1 jmcneill continue;
181 1.1 jmcneill }
182 1.1 jmcneill const u_int *data = fdtbus_get_prop(ethernet, "reg", &len);
183 1.1 jmcneill if (data == NULL || len < 4) {
184 1.1 jmcneill continue;
185 1.1 jmcneill }
186 1.1 jmcneill if (bdf != be32toh(data[0])) {
187 1.1 jmcneill continue;
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill return OF_getprop(ethernet, "local-mac-address",
191 1.1 jmcneill eaddr, ETHER_ADDR_LEN);
192 1.1 jmcneill }
193 1.1 jmcneill
194 1.1 jmcneill return -1;
195 1.1 jmcneill }
196 1.1 jmcneill
197 1.1 jmcneill static void
198 1.1 jmcneill apple_platform_device_register(device_t self, void *aux)
199 1.1 jmcneill {
200 1.1 jmcneill prop_dictionary_t prop = device_properties(self);
201 1.1 jmcneill uint8_t eaddr[ETHER_ADDR_LEN];
202 1.1 jmcneill int len;
203 1.1 jmcneill
204 1.2 jmcneill if (device_is_a(self, "cpu")) {
205 1.2 jmcneill struct fdt_attach_args * const faa = aux;
206 1.3 jmcneill bus_addr_t cpuid;
207 1.2 jmcneill
208 1.3 jmcneill if (fdtbus_get_reg(faa->faa_phandle, 0, &cpuid, NULL) != 0) {
209 1.3 jmcneill cpuid = 0;
210 1.2 jmcneill }
211 1.3 jmcneill
212 1.3 jmcneill /*
213 1.3 jmcneill * On Apple M1 (and hopefully later models), AFF2 is 0 for
214 1.3 jmcneill * efficiency and 1 for performance cores. Use this value
215 1.3 jmcneill * to provide a fake DMIPS/MHz value -- the actual number
216 1.3 jmcneill * only matters in relation to the value presented by other
217 1.3 jmcneill * cores.
218 1.3 jmcneill */
219 1.3 jmcneill const u_int aff2 = __SHIFTOUT(cpuid, MPIDR_AFF2);
220 1.3 jmcneill prop_dictionary_set_uint32(prop, "capacity_dmips_mhz", aff2);
221 1.2 jmcneill return;
222 1.2 jmcneill }
223 1.2 jmcneill
224 1.1 jmcneill if (device_is_a(self, "bge") &&
225 1.1 jmcneill device_is_a(device_parent(self), "pci")) {
226 1.1 jmcneill struct pci_attach_args * const pa = aux;
227 1.1 jmcneill
228 1.1 jmcneill len = apple_platform_get_mac_address(pa->pa_pc, pa->pa_tag,
229 1.1 jmcneill eaddr);
230 1.1 jmcneill if (len == ETHER_ADDR_LEN) {
231 1.1 jmcneill prop_dictionary_set_bool(prop, "without-seeprom", true);
232 1.1 jmcneill prop_dictionary_set_data(prop, "mac-address", eaddr,
233 1.1 jmcneill sizeof(eaddr));
234 1.1 jmcneill }
235 1.1 jmcneill return;
236 1.1 jmcneill }
237 1.1 jmcneill }
238 1.1 jmcneill
239 1.6 skrll static const struct fdt_platform apple_fdt_platform = {
240 1.6 skrll .fp_devmap = apple_platform_devmap,
241 1.6 skrll .fp_bootstrap = apple_platform_bootstrap,
242 1.6 skrll .fp_init_attach_args = apple_platform_init_attach_args,
243 1.6 skrll .fp_reset = psci_fdt_reset,
244 1.6 skrll .fp_delay = gtmr_delay,
245 1.6 skrll .fp_uart_freq = apple_platform_uart_freq,
246 1.6 skrll .fp_device_register = apple_platform_device_register,
247 1.6 skrll .fp_mpstart = arm_fdt_cpu_mpstart,
248 1.1 jmcneill };
249 1.1 jmcneill
250 1.6 skrll FDT_PLATFORM(apple_arm, "apple,arm-platform", &apple_fdt_platform);
251