xref: /freebsd/sys/powerpc/ofw/ofw_machdep.c (revision 2a2234c0f41da33b8cfc938e46b54a8234b64135)
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 || !hw_direct_map)
88                 return;
89 
90 	bcopy((void *)PHYS_TO_DMAP(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 || !hw_direct_map)
97                 return;
98 
99 	bcopy(restore_trap_vec, (void *)PHYS_TO_DMAP(EXC_RST),
100 	    EXC_LAST - EXC_RST);
101 	__syncicache((void *)PHYS_TO_DMAP(EXC_RSVD), EXC_LAST - EXC_RSVD);
102 }
103 
104 /*
105  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
106  */
107 register_t	ofw_sprg0_save;
108 
109 static __inline void
110 ofw_sprg_prepare(void)
111 {
112 	if (ofw_real_mode)
113 		return;
114 
115 	/*
116 	 * Assume that interrupt are disabled at this point, or
117 	 * SPRG1-3 could be trashed
118 	 */
119 #ifdef __powerpc64__
120 	__asm __volatile("mtsprg1 %0\n\t"
121 	    		 "mtsprg2 %1\n\t"
122 			 "mtsprg3 %2\n\t"
123 			 :
124 			 : "r"(ofmsr[2]),
125 			 "r"(ofmsr[3]),
126 			 "r"(ofmsr[4]));
127 #else
128 	__asm __volatile("mfsprg0 %0\n\t"
129 			 "mtsprg0 %1\n\t"
130 	    		 "mtsprg1 %2\n\t"
131 	    		 "mtsprg2 %3\n\t"
132 			 "mtsprg3 %4\n\t"
133 			 : "=&r"(ofw_sprg0_save)
134 			 : "r"(ofmsr[1]),
135 			 "r"(ofmsr[2]),
136 			 "r"(ofmsr[3]),
137 			 "r"(ofmsr[4]));
138 #endif
139 }
140 
141 static __inline void
142 ofw_sprg_restore(void)
143 {
144 	if (ofw_real_mode)
145 		return;
146 
147 	/*
148 	 * Note that SPRG1-3 contents are irrelevant. They are scratch
149 	 * registers used in the early portion of trap handling when
150 	 * interrupts are disabled.
151 	 *
152 	 * PCPU data cannot be used until this routine is called !
153 	 */
154 #ifndef __powerpc64__
155 	__asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
156 #endif
157 }
158 #endif
159 
160 static int
161 parse_ofw_memory(phandle_t node, const char *prop, struct mem_region *output)
162 {
163 	cell_t address_cells, size_cells;
164 	cell_t OFmem[4 * PHYS_AVAIL_SZ];
165 	int sz, i, j;
166 	phandle_t phandle;
167 
168 	sz = 0;
169 
170 	/*
171 	 * Get #address-cells from root node, defaulting to 1 if it cannot
172 	 * be found.
173 	 */
174 	phandle = OF_finddevice("/");
175 	if (OF_getencprop(phandle, "#address-cells", &address_cells,
176 	    sizeof(address_cells)) < (ssize_t)sizeof(address_cells))
177 		address_cells = 1;
178 	if (OF_getencprop(phandle, "#size-cells", &size_cells,
179 	    sizeof(size_cells)) < (ssize_t)sizeof(size_cells))
180 		size_cells = 1;
181 
182 	/*
183 	 * Get memory.
184 	 */
185 	if (node == -1 || (sz = OF_getencprop(node, prop,
186 	    OFmem, sizeof(OFmem))) <= 0)
187 		panic("Physical memory map not found");
188 
189 	i = 0;
190 	j = 0;
191 	while (i < sz/sizeof(cell_t)) {
192 		output[j].mr_start = OFmem[i++];
193 		if (address_cells == 2) {
194 			output[j].mr_start <<= 32;
195 			output[j].mr_start += OFmem[i++];
196 		}
197 
198 		output[j].mr_size = OFmem[i++];
199 		if (size_cells == 2) {
200 			output[j].mr_size <<= 32;
201 			output[j].mr_size += OFmem[i++];
202 		}
203 
204 		if (output[j].mr_start > BUS_SPACE_MAXADDR)
205 			continue;
206 
207 		/*
208 		 * Constrain memory to that which we can access.
209 		 * 32-bit AIM can only reference 32 bits of address currently,
210 		 * but Book-E can access 36 bits.
211 		 */
212 		if (((uint64_t)output[j].mr_start +
213 		    (uint64_t)output[j].mr_size - 1) >
214 		    BUS_SPACE_MAXADDR) {
215 			output[j].mr_size = BUS_SPACE_MAXADDR -
216 			    output[j].mr_start + 1;
217 		}
218 
219 		j++;
220 	}
221 	sz = j*sizeof(output[0]);
222 
223 	return (sz);
224 }
225 
226 #ifdef FDT
227 static int
228 excise_fdt_reserved(struct mem_region *avail, int asz)
229 {
230 	struct {
231 		uint64_t address;
232 		uint64_t size;
233 	} fdtmap[16];
234 	ssize_t fdtmapsize;
235 	phandle_t chosen;
236 	int i, j, k;
237 
238 	chosen = OF_finddevice("/chosen");
239 	fdtmapsize = OF_getprop(chosen, "fdtmemreserv", fdtmap, sizeof(fdtmap));
240 
241 	for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
242 		fdtmap[j].address = be64toh(fdtmap[j].address) & ~PAGE_MASK;
243 		fdtmap[j].size = round_page(be64toh(fdtmap[j].size));
244 	}
245 
246 	KASSERT(j*sizeof(fdtmap[0]) < sizeof(fdtmap),
247 	    ("Exceeded number of FDT reservations"));
248 	/* Add a virtual entry for the FDT itself */
249 	if (fdt != NULL) {
250 		fdtmap[j].address = (vm_offset_t)fdt & ~PAGE_MASK;
251 		fdtmap[j].size = round_page(fdt_totalsize(fdt));
252 		fdtmapsize += sizeof(fdtmap[0]);
253 	}
254 
255 	for (i = 0; i < asz; i++) {
256 		for (j = 0; j < fdtmapsize/sizeof(fdtmap[0]); j++) {
257 			/*
258 			 * Case 1: Exclusion region encloses complete
259 			 * available entry. Drop it and move on.
260 			 */
261 			if (fdtmap[j].address <= avail[i].mr_start &&
262 			    fdtmap[j].address + fdtmap[j].size >=
263 			    avail[i].mr_start + avail[i].mr_size) {
264 				for (k = i+1; k < asz; k++)
265 					avail[k-1] = avail[k];
266 				asz--;
267 				i--; /* Repeat some entries */
268 				continue;
269 			}
270 
271 			/*
272 			 * Case 2: Exclusion region starts in available entry.
273 			 * Trim it to where the entry begins and append
274 			 * a new available entry with the region after
275 			 * the excluded region, if any.
276 			 */
277 			if (fdtmap[j].address >= avail[i].mr_start &&
278 			    fdtmap[j].address < avail[i].mr_start +
279 			    avail[i].mr_size) {
280 				if (fdtmap[j].address + fdtmap[j].size <
281 				    avail[i].mr_start + avail[i].mr_size) {
282 					avail[asz].mr_start =
283 					    fdtmap[j].address + fdtmap[j].size;
284 					avail[asz].mr_size = avail[i].mr_start +
285 					     avail[i].mr_size -
286 					     avail[asz].mr_start;
287 					asz++;
288 				}
289 
290 				avail[i].mr_size = fdtmap[j].address -
291 				    avail[i].mr_start;
292 			}
293 
294 			/*
295 			 * Case 3: Exclusion region ends in available entry.
296 			 * Move start point to where the exclusion zone ends.
297 			 * The case of a contained exclusion zone has already
298 			 * been caught in case 2.
299 			 */
300 			if (fdtmap[j].address + fdtmap[j].size >=
301 			    avail[i].mr_start && fdtmap[j].address +
302 			    fdtmap[j].size < avail[i].mr_start +
303 			    avail[i].mr_size) {
304 				avail[i].mr_size += avail[i].mr_start;
305 				avail[i].mr_start =
306 				    fdtmap[j].address + fdtmap[j].size;
307 				avail[i].mr_size -= avail[i].mr_start;
308 			}
309 		}
310 	}
311 
312 	return (asz);
313 }
314 #endif
315 
316 /*
317  * This is called during powerpc_init, before the system is really initialized.
318  * It shall provide the total and the available regions of RAM.
319  * The available regions need not take the kernel into account.
320  */
321 void
322 ofw_mem_regions(struct mem_region *memp, int *memsz,
323 		struct mem_region *availp, int *availsz)
324 {
325 	phandle_t phandle;
326 	int asz, msz;
327 	int res;
328 	char name[31];
329 
330 	asz = msz = 0;
331 
332 	/*
333 	 * Get memory from all the /memory nodes.
334 	 */
335 	for (phandle = OF_child(OF_peer(0)); phandle != 0;
336 	    phandle = OF_peer(phandle)) {
337 		if (OF_getprop(phandle, "name", name, sizeof(name)) <= 0)
338 			continue;
339 		if (strncmp(name, "memory", sizeof(name)) != 0 &&
340 		    strncmp(name, "memory@", strlen("memory@")) != 0)
341 			continue;
342 
343 		res = parse_ofw_memory(phandle, "reg", &memp[msz]);
344 		msz += res/sizeof(struct mem_region);
345 		if (OF_getproplen(phandle, "available") >= 0)
346 			res = parse_ofw_memory(phandle, "available",
347 			    &availp[asz]);
348 		else
349 			res = parse_ofw_memory(phandle, "reg", &availp[asz]);
350 		asz += res/sizeof(struct mem_region);
351 	}
352 
353 #ifdef FDT
354 	phandle = OF_finddevice("/chosen");
355 	if (OF_hasprop(phandle, "fdtmemreserv"))
356 		asz = excise_fdt_reserved(availp, asz);
357 #endif
358 
359 	*memsz = msz;
360 	*availsz = asz;
361 }
362 
363 void
364 OF_initial_setup(void *fdt_ptr, void *junk, int (*openfirm)(void *))
365 {
366 #ifdef AIM
367 	ofmsr[0] = mfmsr();
368 	#ifdef __powerpc64__
369 	ofmsr[0] &= ~PSL_SF;
370 	#else
371 	__asm __volatile("mfsprg0 %0" : "=&r"(ofmsr[1]));
372 	#endif
373 	__asm __volatile("mfsprg1 %0" : "=&r"(ofmsr[2]));
374 	__asm __volatile("mfsprg2 %0" : "=&r"(ofmsr[3]));
375 	__asm __volatile("mfsprg3 %0" : "=&r"(ofmsr[4]));
376 	openfirmware_entry = openfirm;
377 
378 	if (ofmsr[0] & PSL_DR)
379 		ofw_real_mode = 0;
380 	else
381 		ofw_real_mode = 1;
382 
383 	ofw_save_trap_vec(save_trap_init);
384 #else
385 	ofw_real_mode = 1;
386 #endif
387 
388 	fdt = fdt_ptr;
389 }
390 
391 boolean_t
392 OF_bootstrap()
393 {
394 	boolean_t status = FALSE;
395 	int err = 0;
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 		err = OF_init(openfirmware);
413 	} else
414 #endif
415 	if (fdt != NULL) {
416 #ifdef FDT
417 #ifdef AIM
418 		bus_space_tag_t fdt_bt;
419 		vm_offset_t tmp_fdt_ptr;
420 		vm_size_t fdt_size;
421 		uintptr_t fdt_va;
422 #endif
423 
424 		status = OF_install(OFW_FDT, 0);
425 		if (status != TRUE)
426 			return status;
427 
428 #ifdef AIM /* AIM-only for now -- Book-E does this remapping in early init */
429 		/* Get the FDT size for mapping if we can */
430 		tmp_fdt_ptr = pmap_early_io_map((vm_paddr_t)fdt, PAGE_SIZE);
431 		if (fdt_check_header((void *)tmp_fdt_ptr) != 0) {
432 			pmap_early_io_unmap(tmp_fdt_ptr, PAGE_SIZE);
433 			return FALSE;
434 		}
435 		fdt_size = fdt_totalsize((void *)tmp_fdt_ptr);
436 		pmap_early_io_unmap(tmp_fdt_ptr, PAGE_SIZE);
437 
438 		/*
439 		 * Map this for real. Use bus_space_map() to take advantage
440 		 * of its auto-remapping function once the kernel is loaded.
441 		 * This is a dirty hack, but what we have.
442 		 */
443 #ifdef _LITTLE_ENDIAN
444 		fdt_bt = &bs_le_tag;
445 #else
446 		fdt_bt = &bs_be_tag;
447 #endif
448 		bus_space_map(fdt_bt, (vm_paddr_t)fdt, fdt_size, 0, &fdt_va);
449 
450 		err = OF_init((void *)fdt_va);
451 #else
452 		err = OF_init(fdt);
453 #endif
454 #endif
455 	}
456 
457 	#ifdef FDT_DTB_STATIC
458 	/*
459 	 * Check for a statically included blob already in the kernel and
460 	 * needing no mapping.
461 	 */
462 	else {
463 		status = OF_install(OFW_FDT, 0);
464 		if (status != TRUE)
465 			return status;
466 		err = OF_init(&fdt_static_dtb);
467 	}
468 	#endif
469 
470 	if (err != 0) {
471 		OF_install(NULL, 0);
472 		status = FALSE;
473 	}
474 
475 	return (status);
476 }
477 
478 #ifdef AIM
479 void
480 ofw_quiesce(void)
481 {
482 	struct {
483 		cell_t name;
484 		cell_t nargs;
485 		cell_t nreturns;
486 	} args;
487 
488 	KASSERT(!pmap_bootstrapped, ("Cannot call ofw_quiesce after VM is up"));
489 
490 	args.name = (cell_t)(uintptr_t)"quiesce";
491 	args.nargs = 0;
492 	args.nreturns = 0;
493 	openfirmware(&args);
494 }
495 
496 static int
497 openfirmware_core(void *args)
498 {
499 	int		result;
500 	register_t	oldmsr;
501 
502 	if (openfirmware_entry == NULL)
503 		return (-1);
504 
505 	/*
506 	 * Turn off exceptions - we really don't want to end up
507 	 * anywhere unexpected with PCPU set to something strange
508 	 * or the stack pointer wrong.
509 	 */
510 	oldmsr = intr_disable();
511 
512 	ofw_sprg_prepare();
513 
514 	/* Save trap vectors */
515 	ofw_save_trap_vec(save_trap_of);
516 
517 	/* Restore initially saved trap vectors */
518 	ofw_restore_trap_vec(save_trap_init);
519 
520 #ifndef __powerpc64__
521 	/*
522 	 * Clear battable[] translations
523 	 */
524 	if (!(cpu_features & PPC_FEATURE_64))
525 		__asm __volatile("mtdbatu 2, %0\n"
526 				 "mtdbatu 3, %0" : : "r" (0));
527 	isync();
528 #endif
529 
530 	result = ofwcall(args);
531 
532 	/* Restore trap vecotrs */
533 	ofw_restore_trap_vec(save_trap_of);
534 
535 	ofw_sprg_restore();
536 
537 	intr_restore(oldmsr);
538 
539 	return (result);
540 }
541 
542 #ifdef SMP
543 struct ofw_rv_args {
544 	void *args;
545 	int retval;
546 	volatile int in_progress;
547 };
548 
549 static void
550 ofw_rendezvous_dispatch(void *xargs)
551 {
552 	struct ofw_rv_args *rv_args = xargs;
553 
554 	/* NOTE: Interrupts are disabled here */
555 
556 	if (PCPU_GET(cpuid) == 0) {
557 		/*
558 		 * Execute all OF calls on CPU 0
559 		 */
560 		rv_args->retval = openfirmware_core(rv_args->args);
561 		rv_args->in_progress = 0;
562 	} else {
563 		/*
564 		 * Spin with interrupts off on other CPUs while OF has
565 		 * control of the machine.
566 		 */
567 		while (rv_args->in_progress)
568 			cpu_spinwait();
569 	}
570 }
571 #endif
572 
573 static int
574 openfirmware(void *args)
575 {
576 	int result;
577 	#ifdef SMP
578 	struct ofw_rv_args rv_args;
579 	#endif
580 
581 	if (openfirmware_entry == NULL)
582 		return (-1);
583 
584 	#ifdef SMP
585 	if (cold) {
586 		result = openfirmware_core(args);
587 	} else {
588 		rv_args.args = args;
589 		rv_args.in_progress = 1;
590 		smp_rendezvous(smp_no_rendezvous_barrier,
591 		    ofw_rendezvous_dispatch, smp_no_rendezvous_barrier,
592 		    &rv_args);
593 		result = rv_args.retval;
594 	}
595 	#else
596 	result = openfirmware_core(args);
597 	#endif
598 
599 	return (result);
600 }
601 
602 void
603 OF_reboot()
604 {
605 	struct {
606 		cell_t name;
607 		cell_t nargs;
608 		cell_t nreturns;
609 		cell_t arg;
610 	} args;
611 
612 	args.name = (cell_t)(uintptr_t)"interpret";
613 	args.nargs = 1;
614 	args.nreturns = 0;
615 	args.arg = (cell_t)(uintptr_t)"reset-all";
616 	openfirmware_core(&args); /* Don't do rendezvous! */
617 
618 	for (;;);	/* just in case */
619 }
620 
621 #endif /* AIM */
622 
623 void
624 OF_getetheraddr(device_t dev, u_char *addr)
625 {
626 	phandle_t	node;
627 
628 	node = ofw_bus_get_node(dev);
629 	OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
630 }
631 
632 /*
633  * Return a bus handle and bus tag that corresponds to the register
634  * numbered regno for the device referenced by the package handle
635  * dev. This function is intended to be used by console drivers in
636  * early boot only. It works by mapping the address of the device's
637  * register in the address space of its parent and recursively walk
638  * the device tree upward this way.
639  */
640 int
641 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
642     bus_space_handle_t *handle, bus_size_t *sz)
643 {
644 	bus_addr_t addr;
645 	bus_size_t size;
646 	pcell_t pci_hi;
647 	int flags, res;
648 
649 	res = ofw_reg_to_paddr(dev, regno, &addr, &size, &pci_hi);
650 	if (res < 0)
651 		return (res);
652 
653 	if (pci_hi == OFW_PADDR_NOT_PCI) {
654 		*tag = &bs_be_tag;
655 		flags = 0;
656 	} else {
657 		*tag = &bs_le_tag;
658 		flags = (pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) ?
659 		    BUS_SPACE_MAP_PREFETCHABLE: 0;
660 	}
661 
662 	if (sz != NULL)
663 		*sz = size;
664 
665 	return (bus_space_map(*tag, addr, size, flags, handle));
666 }
667 
668