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