xref: /freebsd/sys/powerpc/ofw/ofw_machdep.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (C) 1996 Wolfgang Solfrank.
5  * Copyright (C) 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $NetBSD: ofw_machdep.c,v 1.5 2000/05/23 13:25:43 tsubai Exp $
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_platform.h"
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/disk.h>
45 #include <sys/fcntl.h>
46 #include <sys/malloc.h>
47 #include <sys/smp.h>
48 #include <sys/stat.h>
49 #include <sys/endian.h>
50 
51 #include <net/ethernet.h>
52 
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_pci.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_subr.h>
58 
59 #include <vm/vm.h>
60 #include <vm/vm_param.h>
61 #include <vm/vm_page.h>
62 
63 #include <machine/bus.h>
64 #include <machine/cpu.h>
65 #include <machine/md_var.h>
66 #include <machine/platform.h>
67 #include <machine/ofw_machdep.h>
68 #include <machine/trap.h>
69 
70 #include <contrib/libfdt/libfdt.h>
71 
72 static void	*fdt;
73 int		ofw_real_mode;
74 
75 #ifdef AIM
76 extern register_t ofmsr[5];
77 extern void	*openfirmware_entry;
78 char		save_trap_init[0x2f00];          /* EXC_LAST */
79 char		save_trap_of[0x2f00];            /* EXC_LAST */
80 
81 int		ofwcall(void *);
82 static int	openfirmware(void *args);
83 
84 __inline void
85 ofw_save_trap_vec(char *save_trap_vec)
86 {
87 	if (!ofw_real_mode)
88                 return;
89 
90 	bcopy((void *)EXC_RST, save_trap_vec, EXC_LAST - EXC_RST);
91 }
92 
93 static __inline void
94 ofw_restore_trap_vec(char *restore_trap_vec)
95 {
96 	if (!ofw_real_mode)
97                 return;
98 
99 	bcopy(restore_trap_vec, (void *)EXC_RST, EXC_LAST - EXC_RST);
100 	__syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
101 }
102 
103 /*
104  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
105  */
106 register_t	ofw_sprg0_save;
107 
108 static __inline void
109 ofw_sprg_prepare(void)
110 {
111 	if (ofw_real_mode)
112 		return;
113 
114 	/*
115 	 * Assume that interrupt are disabled at this point, or
116 	 * SPRG1-3 could be trashed
117 	 */
118 #ifdef __powerpc64__
119 	__asm __volatile("mtsprg1 %0\n\t"
120 	    		 "mtsprg2 %1\n\t"
121 			 "mtsprg3 %2\n\t"
122 			 :
123 			 : "r"(ofmsr[2]),
124 			 "r"(ofmsr[3]),
125 			 "r"(ofmsr[4]));
126 #else
127 	__asm __volatile("mfsprg0 %0\n\t"
128 			 "mtsprg0 %1\n\t"
129 	    		 "mtsprg1 %2\n\t"
130 	    		 "mtsprg2 %3\n\t"
131 			 "mtsprg3 %4\n\t"
132 			 : "=&r"(ofw_sprg0_save)
133 			 : "r"(ofmsr[1]),
134 			 "r"(ofmsr[2]),
135 			 "r"(ofmsr[3]),
136 			 "r"(ofmsr[4]));
137 #endif
138 }
139 
140 static __inline void
141 ofw_sprg_restore(void)
142 {
143 	if (ofw_real_mode)
144 		return;
145 
146 	/*
147 	 * Note that SPRG1-3 contents are irrelevant. They are scratch
148 	 * registers used in the early portion of trap handling when
149 	 * interrupts are disabled.
150 	 *
151 	 * PCPU data cannot be used until this routine is called !
152 	 */
153 #ifndef __powerpc64__
154 	__asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
155 #endif
156 }
157 #endif
158 
159 static int
160 parse_ofw_memory(phandle_t node, const char *prop, struct mem_region *output)
161 {
162 	cell_t address_cells, size_cells;
163 	cell_t OFmem[4 * PHYS_AVAIL_SZ];
164 	int sz, i, j;
165 	phandle_t phandle;
166 
167 	sz = 0;
168 
169 	/*
170 	 * Get #address-cells from root node, defaulting to 1 if it cannot
171 	 * be found.
172 	 */
173 	phandle = OF_finddevice("/");
174 	if (OF_getencprop(phandle, "#address-cells", &address_cells,
175 	    sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
176 		address_cells = 1;
177 	if (OF_getencprop(phandle, "#size-cells", &size_cells,
178 	    sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
179 		size_cells = 1;
180 
181 	/*
182 	 * Get memory.
183 	 */
184 	if (node == -1 || (sz = OF_getencprop(node, prop,
185 	    OFmem, sizeof(OFmem))) <= 0)
186 		panic("Physical memory map not found");
187 
188 	i = 0;
189 	j = 0;
190 	while (i < sz/sizeof(cell_t)) {
191 		output[j].mr_start = OFmem[i++];
192 		if (address_cells == 2) {
193 			output[j].mr_start <<= 32;
194 			output[j].mr_start += OFmem[i++];
195 		}
196 
197 		output[j].mr_size = OFmem[i++];
198 		if (size_cells == 2) {
199 			output[j].mr_size <<= 32;
200 			output[j].mr_size += OFmem[i++];
201 		}
202 
203 		if (output[j].mr_start > BUS_SPACE_MAXADDR)
204 			continue;
205 
206 		/*
207 		 * Constrain memory to that which we can access.
208 		 * 32-bit AIM can only reference 32 bits of address currently,
209 		 * but Book-E can access 36 bits.
210 		 */
211 		if (((uint64_t)output[j].mr_start +
212 		    (uint64_t)output[j].mr_size - 1) >
213 		    BUS_SPACE_MAXADDR) {
214 			output[j].mr_size = BUS_SPACE_MAXADDR -
215 			    output[j].mr_start + 1;
216 		}
217 
218 		j++;
219 	}
220 	sz = j*sizeof(output[0]);
221 
222 	return (sz);
223 }
224 
225 static int
226 excise_fdt_reserved(struct mem_region *avail, int asz)
227 {
228 	struct {
229 		uint64_t address;
230 		uint64_t size;
231 	} fdtmap[16];
232 	ssize_t fdtmapsize;
233 	phandle_t chosen;
234 	int i, j, k;
235 
236 	chosen = OF_finddevice("/chosen");
237 	fdtmapsize = OF_getprop(chosen, "fdtmemreserv", fdtmap, sizeof(fdtmap));
238 
239 	for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
240 		fdtmap[j].address = be64toh(fdtmap[j].address) & ~PAGE_MASK;
241 		fdtmap[j].size = round_page(be64toh(fdtmap[j].size));
242 	}
243 
244 	KASSERT(j*sizeof(fdtmap[0]) < sizeof(fdtmap),
245 	    ("Exceeded number of FDT reservations"));
246 	/* Add a virtual entry for the FDT itself */
247 	if (fdt != NULL) {
248 		fdtmap[j].address = (vm_offset_t)fdt & ~PAGE_MASK;
249 		fdtmap[j].size = round_page(fdt_totalsize(fdt));
250 		fdtmapsize += sizeof(fdtmap[0]);
251 	}
252 
253 	for (i = 0; i < asz; i++) {
254 		for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
255 			/*
256 			 * Case 1: Exclusion region encloses complete
257 			 * available entry. Drop it and move on.
258 			 */
259 			if (fdtmap[j].address <= avail[i].mr_start &&
260 			    fdtmap[j].address + fdtmap[j].size >=
261 			    avail[i].mr_start + avail[i].mr_size) {
262 				for (k = i+1; k < asz; k++)
263 					avail[k-1] = avail[k];
264 				asz--;
265 				i--; /* Repeat some entries */
266 				continue;
267 			}
268 
269 			/*
270 			 * Case 2: Exclusion region starts in available entry.
271 			 * Trim it to where the entry begins and append
272 			 * a new available entry with the region after
273 			 * the excluded region, if any.
274 			 */
275 			if (fdtmap[j].address >= avail[i].mr_start &&
276 			    fdtmap[j].address < avail[i].mr_start +
277 			    avail[i].mr_size) {
278 				if (fdtmap[j].address + fdtmap[j].size <
279 				    avail[i].mr_start + avail[i].mr_size) {
280 					avail[asz].mr_start =
281 					    fdtmap[j].address + fdtmap[j].size;
282 					avail[asz].mr_size = avail[i].mr_start +
283 					     avail[i].mr_size -
284 					     avail[asz].mr_start;
285 					asz++;
286 				}
287 
288 				avail[i].mr_size = fdtmap[j].address -
289 				    avail[i].mr_start;
290 			}
291 
292 			/*
293 			 * Case 3: Exclusion region ends in available entry.
294 			 * Move start point to where the exclusion zone ends.
295 			 * The case of a contained exclusion zone has already
296 			 * been caught in case 2.
297 			 */
298 			if (fdtmap[j].address + fdtmap[j].size >=
299 			    avail[i].mr_start && fdtmap[j].address +
300 			    fdtmap[j].size < avail[i].mr_start +
301 			    avail[i].mr_size) {
302 				avail[i].mr_size += avail[i].mr_start;
303 				avail[i].mr_start =
304 				    fdtmap[j].address + fdtmap[j].size;
305 				avail[i].mr_size -= avail[i].mr_start;
306 			}
307 		}
308 	}
309 
310 	return (asz);
311 }
312 
313 /*
314  * This is called during powerpc_init, before the system is really initialized.
315  * It shall provide the total and the available regions of RAM.
316  * The available regions need not take the kernel into account.
317  */
318 void
319 ofw_mem_regions(struct mem_region *memp, int *memsz,
320 		struct mem_region *availp, int *availsz)
321 {
322 	phandle_t phandle;
323 	int asz, msz;
324 	int res;
325 	char name[31];
326 
327 	asz = msz = 0;
328 
329 	/*
330 	 * Get memory from all the /memory nodes.
331 	 */
332 	for (phandle = OF_child(OF_peer(0)); phandle != 0;
333 	    phandle = OF_peer(phandle)) {
334 		if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
335 			continue;
336 		if (strncmp(name, "memory", sizeof(name)) != 0 &&
337 		    strncmp(name, "memory@", strlen("memory@")) != 0)
338 			continue;
339 
340 		res = parse_ofw_memory(phandle, "reg", &memp[msz]);
341 		msz += res/sizeof(struct mem_region);
342 		if (OF_getproplen(phandle, "available") >= 0)
343 			res = parse_ofw_memory(phandle, "available",
344 			    &availp[asz]);
345 		else
346 			res = parse_ofw_memory(phandle, "reg", &availp[asz]);
347 		asz += res/sizeof(struct mem_region);
348 	}
349 
350 	phandle = OF_finddevice("/chosen");
351 	if (OF_hasprop(phandle, "fdtmemreserv"))
352 		asz = excise_fdt_reserved(availp, asz);
353 
354 	*memsz = msz;
355 	*availsz = asz;
356 }
357 
358 void
359 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
360 {
361 #ifdef AIM
362 	ofmsr[0] = mfmsr();
363 	#ifdef __powerpc64__
364 	ofmsr[0] &= ~PSL_SF;
365 	#else
366 	__asm __volatile("mfsprg0 %0" : "=&r"(ofmsr[1]));
367 	#endif
368 	__asm __volatile("mfsprg1 %0" : "=&r"(ofmsr[2]));
369 	__asm __volatile("mfsprg2 %0" : "=&r"(ofmsr[3]));
370 	__asm __volatile("mfsprg3 %0" : "=&r"(ofmsr[4]));
371 	openfirmware_entry = openfirm;
372 
373 	if (ofmsr[0] & PSL_DR)
374 		ofw_real_mode = 0;
375 	else
376 		ofw_real_mode = 1;
377 
378 	ofw_save_trap_vec(save_trap_init);
379 #else
380 	ofw_real_mode = 1;
381 #endif
382 
383 	fdt = fdt_ptr;
384 
385 	#ifdef FDT_DTB_STATIC
386 	/* Check for a statically included blob */
387 	if (fdt == NULL)
388 		fdt = &fdt_static_dtb;
389 	#endif
390 }
391 
392 boolean_t
393 OF_bootstrap()
394 {
395 	boolean_t status = FALSE;
396 
397 #ifdef AIM
398 	if (openfirmware_entry != NULL) {
399 		if (ofw_real_mode) {
400 			status = OF_install(OFW_STD_REAL, 0);
401 		} else {
402 			#ifdef __powerpc64__
403 			status = OF_install(OFW_STD_32BIT, 0);
404 			#else
405 			status = OF_install(OFW_STD_DIRECT, 0);
406 			#endif
407 		}
408 
409 		if (status != TRUE)
410 			return status;
411 
412 		OF_init(openfirmware);
413 	} else
414 #endif
415 	if (fdt != NULL) {
416 		status = OF_install(OFW_FDT, 0);
417 
418 		if (status != TRUE)
419 			return status;
420 
421 		OF_init(fdt);
422 		OF_interpret("perform-fixup", 0);
423 	}
424 
425 	return (status);
426 }
427 
428 #ifdef AIM
429 void
430 ofw_quiesce(void)
431 {
432 	struct {
433 		cell_t name;
434 		cell_t nargs;
435 		cell_t nreturns;
436 	} args;
437 
438 	KASSERT(!pmap_bootstrapped, ("Cannot call ofw_quiesce after VM is up"));
439 
440 	args.name = (cell_t)(uintptr_t)"quiesce";
441 	args.nargs = 0;
442 	args.nreturns = 0;
443 	openfirmware(&args);
444 }
445 
446 static int
447 openfirmware_core(void *args)
448 {
449 	int		result;
450 	register_t	oldmsr;
451 
452 	if (openfirmware_entry == NULL)
453 		return (-1);
454 
455 	/*
456 	 * Turn off exceptions - we really don't want to end up
457 	 * anywhere unexpected with PCPU set to something strange
458 	 * or the stack pointer wrong.
459 	 */
460 	oldmsr = intr_disable();
461 
462 	ofw_sprg_prepare();
463 
464 	/* Save trap vectors */
465 	ofw_save_trap_vec(save_trap_of);
466 
467 	/* Restore initially saved trap vectors */
468 	ofw_restore_trap_vec(save_trap_init);
469 
470 #ifndef __powerpc64__
471 	/*
472 	 * Clear battable[] translations
473 	 */
474 	if (!(cpu_features & PPC_FEATURE_64))
475 		__asm __volatile("mtdbatu 2, %0\n"
476 				 "mtdbatu 3, %0" : : "r" (0));
477 	isync();
478 #endif
479 
480 	result = ofwcall(args);
481 
482 	/* Restore trap vecotrs */
483 	ofw_restore_trap_vec(save_trap_of);
484 
485 	ofw_sprg_restore();
486 
487 	intr_restore(oldmsr);
488 
489 	return (result);
490 }
491 
492 #ifdef SMP
493 struct ofw_rv_args {
494 	void *args;
495 	int retval;
496 	volatile int in_progress;
497 };
498 
499 static void
500 ofw_rendezvous_dispatch(void *xargs)
501 {
502 	struct ofw_rv_args *rv_args = xargs;
503 
504 	/* NOTE: Interrupts are disabled here */
505 
506 	if (PCPU_GET(cpuid) == 0) {
507 		/*
508 		 * Execute all OF calls on CPU 0
509 		 */
510 		rv_args->retval = openfirmware_core(rv_args->args);
511 		rv_args->in_progress = 0;
512 	} else {
513 		/*
514 		 * Spin with interrupts off on other CPUs while OF has
515 		 * control of the machine.
516 		 */
517 		while (rv_args->in_progress)
518 			cpu_spinwait();
519 	}
520 }
521 #endif
522 
523 static int
524 openfirmware(void *args)
525 {
526 	int result;
527 	#ifdef SMP
528 	struct ofw_rv_args rv_args;
529 	#endif
530 
531 	if (openfirmware_entry == NULL)
532 		return (-1);
533 
534 	#ifdef SMP
535 	if (cold) {
536 		result = openfirmware_core(args);
537 	} else {
538 		rv_args.args = args;
539 		rv_args.in_progress = 1;
540 		smp_rendezvous(smp_no_rendezvous_barrier,
541 		    ofw_rendezvous_dispatch, smp_no_rendezvous_barrier,
542 		    &rv_args);
543 		result = rv_args.retval;
544 	}
545 	#else
546 	result = openfirmware_core(args);
547 	#endif
548 
549 	return (result);
550 }
551 
552 void
553 OF_reboot()
554 {
555 	struct {
556 		cell_t name;
557 		cell_t nargs;
558 		cell_t nreturns;
559 		cell_t arg;
560 	} args;
561 
562 	args.name = (cell_t)(uintptr_t)"interpret";
563 	args.nargs = 1;
564 	args.nreturns = 0;
565 	args.arg = (cell_t)(uintptr_t)"reset-all";
566 	openfirmware_core(&args); /* Don't do rendezvous! */
567 
568 	for (;;);	/* just in case */
569 }
570 
571 #endif /* AIM */
572 
573 void
574 OF_getetheraddr(device_t dev, u_char *addr)
575 {
576 	phandle_t	node;
577 
578 	node = ofw_bus_get_node(dev);
579 	OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
580 }
581 
582 /*
583  * Return a bus handle and bus tag that corresponds to the register
584  * numbered regno for the device referenced by the package handle
585  * dev. This function is intended to be used by console drivers in
586  * early boot only. It works by mapping the address of the device's
587  * register in the address space of its parent and recursively walk
588  * the device tree upward this way.
589  */
590 int
591 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
592     bus_space_handle_t *handle, bus_size_t *sz)
593 {
594 	bus_addr_t addr;
595 	bus_size_t size;
596 	pcell_t pci_hi;
597 	int flags, res;
598 
599 	res = ofw_reg_to_paddr(dev, regno, &addr, &size, &pci_hi);
600 	if (res < 0)
601 		return (res);
602 
603 	if (pci_hi == OFW_PADDR_NOT_PCI) {
604 		*tag = &bs_be_tag;
605 		flags = 0;
606 	} else {
607 		*tag = &bs_le_tag;
608 		flags = (pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) ?
609 		    BUS_SPACE_MAP_PREFETCHABLE: 0;
610 	}
611 
612 	if (sz != NULL)
613 		*sz = size;
614 
615 	return (bus_space_map(*tag, addr, size, flags, handle));
616 }
617 
618