xref: /netbsd-src/sys/arch/sparc/include/promlib.h (revision 3944ff70a48a86bb622139705a33f8ef461e24f7)
1 /*	$NetBSD: promlib.h,v 1.27 2022/01/22 11:49:16 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * OPENPROM functions.  These are here mainly to hide the OPENPROM interface
34  * from the rest of the kernel.
35  */
36 
37 #ifndef _SPARC_PROMLIB_H_
38 #define _SPARC_PROMLIB_H_
39 
40 #ifdef _KERNEL_OPT
41 #include "opt_multiprocessor.h"
42 #endif
43 
44 #include <sys/device.h>			/* for devhandle_t */
45 
46 #include <machine/idprom.h>
47 #include <machine/bsd_openprom.h>
48 #include <machine/openfirm.h>
49 
50 /*
51  * A set of methods to access the firmware.
52  * We use this to support various versions of Open Boot Prom
53  * or Open Firmware implementations.
54  */
55 struct promops {
56 	int	po_version;		/* PROM version number */
57 #define PROM_OLDMON	0
58 #define PROM_OBP_V0	1
59 #define PROM_OBP_V2	2
60 #define PROM_OBP_V3	3
61 #define PROM_OPENFIRM	4
62 	int	po_revision;		/* revision level */
63 	int	po_stdin;		/* stdio handles */
64 	int	po_stdout;		/* */
65 	void	*po_bootcookie;
66 
67 	/* Access to boot arguments */
68 	const char *(*po_bootpath)(void);
69 	const char *(*po_bootfile)(void);
70 	const char *(*po_bootargs)(void);
71 
72 	/* I/O functions */
73 	int	(*po_getchar)(void);
74 	int	(*po_peekchar)(void);
75 	void	(*po_putchar)(int);
76 	void	(*po_putstr)(const char *, int);
77 	int	(*po_open)(const char *);
78 	void	(*po_close)(int);
79 	int	(*po_read)(int, void *, int);
80 	int	(*po_write)(int, const void *, int);
81 	int	(*po_seek)(int, u_quad_t);
82 
83 	int	(*po_instance_to_package)(int);
84 
85 	/* Misc functions (common in OBP 0,2,3) */
86 	void	(*po_halt)(void)	__attribute__((__noreturn__));
87 	void	(*po_reboot)(const char *)	__attribute__((__noreturn__));
88 	void	(*po_abort)(void);
89 	void	(*po_interpret)(const char *);
90 	void	(*po_setcallback)(void (*)(void));
91 	int	(*po_ticks)(void);
92 	void	*po_tickdata;
93 
94 	/* sun4/sun4c only */
95 	void	(*po_setcontext)(int ctxt, void *va, int pmeg);
96 
97 	/* MP functions (OBP v3 only) */
98 	int	(*po_cpustart)(int, struct openprom_addr *, int, void *);
99 	int	(*po_cpustop)(int);
100 	int	(*po_cpuidle)(int);
101 	int	(*po_cpuresume)(int);
102 
103 	/* Device node traversal (OBP v0, v2, v3; but not sun4) */
104 	int	(*po_firstchild)(int);
105 	int	(*po_nextsibling)(int);
106 
107 	/* Device node properties */
108 	int	(*po_getproplen)(int, const char *);
109 	int	(*po_getprop)(int, const char *, void *, int);
110 	int	(*po_setprop)(int, const char *, const void *, int);
111 	char	*(*po_nextprop)(int, const char *);
112 
113 	int	(*po_finddevice)(const char *);
114 
115 	/* devhandle_t interface */
116 	devhandle_t (*po_node_to_devhandle)(devhandle_t, int);
117 	int	(*po_devhandle_to_node)(devhandle_t);
118 };
119 
120 extern struct promops	promops;
121 
122 /*
123  * Memory description array.
124  * Same as version 2 rom meminfo property.
125  */
126 struct memarr {
127 	long	zero;
128 	u_long	addr;
129 	u_long	len;
130 };
131 int	prom_makememarr(struct memarr *, int, int);
132 #define	MEMARR_AVAILPHYS	0
133 #define	MEMARR_TOTALPHYS	1
134 
135 struct idprom	*prom_getidprom(void);
136 void		prom_getether(int, u_char *);
137 bool		prom_get_node_ether(int, u_char*);
138 const char	*prom_pa_location(u_int, u_int);
139 
140 void	prom_init(void);	/* To setup promops */
141 
142 /* Utility routines */
143 int	prom_getprop(int, const char *, size_t, int *, void *);
144 int	prom_getpropint(int, const char *, int);
145 uint64_t prom_getpropuint64(int, const char *, uint64_t);
146 char	*prom_getpropstring(int, const char *);
147 char	*prom_getpropstringA(int, const char *, char *, size_t);
148 void	prom_printf(const char *, ...);
149 
150 int	prom_findroot(void);
151 int	prom_findnode(int, const char *);
152 int	prom_search(int, const char *);
153 int	prom_opennode(const char *);
154 int	prom_node_has_property(int, const char *);
155 int	prom_getoptionsnode(void);
156 int	prom_getoption(const char *, char *, int);
157 
158 #define	findroot()		prom_findroot()
159 #define	findnode(node,name)	prom_findnode(node,name)
160 #define	opennode(name)		prom_opennode(name)
161 #define	node_has_property(node,prop)	prom_node_has_property(node,prop)
162 
163 void	prom_halt(void)		__attribute__((__noreturn__));
164 void	prom_boot(char *)	__attribute__((__noreturn__));
165 
166 #if defined(MULTIPROCESSOR)
167 #define callrom() do {		\
168 	mp_pause_cpus();	\
169 	prom_abort();		\
170 	mp_resume_cpus();	\
171 } while (0)
172 #else
173 #define callrom()		prom_abort()
174 #endif
175 
176 #define prom_version()		(promops.po_version)
177 #define prom_revision()		(promops.po_revision)
178 #define prom_stdin()		(promops.po_stdin)
179 #define prom_stdout()		(promops.po_stdout)
180 #define _prom_halt()		((*promops.po_halt)(/*void*/))
181 #define _prom_boot(a)		((*promops.po_reboot)(a))
182 #define prom_abort()		((*promops.po_abort)(/*void*/))
183 #define prom_interpret(a)	((*promops.po_interpret)(a))
184 #define prom_setcallback(f)	((*promops.po_setcallback)(f))
185 #define prom_setcontext(c,a,p)	((*promops.po_setcontext)(c,a,p))
186 #define prom_getbootpath()	((*promops.po_bootpath)(/*void*/))
187 #define prom_getbootfile()	((*promops.po_bootfile)(/*void*/))
188 #define prom_getbootargs()	((*promops.po_bootargs)(/*void*/))
189 #define prom_ticks()		((*promops.po_ticks)(/*void*/))
190 
191 
192 #define prom_open(name)		((*promops.po_open)(name))
193 #define prom_close(fd)		((*promops.po_close)(fd))
194 #define prom_instance_to_package(fd) \
195 				((*promops.po_instance_to_package)(fd))
196 #define prom_read(fd,b,n)	((*promops.po_read)(fd,b,n))
197 #define prom_write(fd,b,n)	((*promops.po_write)(fd,b,n))
198 #define prom_seek(fd,o)		((*promops.po_seek)(fd,o))
199 #define prom_getchar()		((*promops.po_getchar)(/*void*/))
200 #define prom_peekchar()		((*promops.po_peekchar)(/*void*/))
201 #define prom_putchar(c)		((*promops.po_putchar)(c))
202 #define prom_putstr(b,n)	((*promops.po_putstr)(b,n))
203 
204 /* Node traversal */
205 #define prom_firstchild(node)	((*promops.po_firstchild)(node))
206 #define prom_nextsibling(node)	((*promops.po_nextsibling)(node))
207 #define prom_proplen(node,name)	((*promops.po_getproplen)(node, name))
208 #define _prom_getprop(node, name, buf, len) \
209 				((*promops.po_getprop)(node, name, buf, len))
210 #define prom_setprop(node, name, value, len) \
211 				((*promops.po_setprop)(node, name, value, len))
212 #define prom_nextprop(node,name)	((*promops.po_nextprop)(node, name))
213 #define prom_finddevice(name)	((*promops.po_finddevice)(name))
214 
215 #define firstchild(node)	prom_firstchild(node)
216 #define nextsibling(node)	prom_nextsibling(node)
217 #define prom_getproplen(node,name)	prom_proplen(node, name)
218 
219 /* devhandle_t interface */
220 #define	prom_node_to_devhandle(s, n) ((*promops.po_node_to_devhandle)((s), (n)))
221 #define	prom_devhandle_to_node(dh)   ((*promops.po_devhandle_to_node)(dh))
222 
223 /* MP stuff - not currently used */
224 #define prom_cpustart(m,a,c,pc)	((*promops.po_cpustart)(m,a,c,pc))
225 #define prom_cpustop(m)		((*promops.po_cpustop)(m))
226 #define prom_cpuidle(m)		((*promops.po_cpuidle)(m))
227 #define prom_cpuresume(m)	((*promops.po_cpuresume)(m))
228 
229 extern void	*romp;		/* PROM-supplied argument (see locore) */
230 
231 #ifdef _KERNEL
232 #define	OBP_DEVICE_CALL_REGISTER(_n_, _c_)				\
233 	DEVICE_CALL_REGISTER(obp_device_calls, _n_, _c_)
234 #endif /* _KERNEL */
235 
236 #endif /* _SPARC_PROMLIB_H_ */
237