xref: /freebsd/stand/kshim/bsd_kernel.h (revision f0cfa1b168014f56c02b83e5f28412cc5f78d117)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2011 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _BSD_KERNEL_H_
28 #define	_BSD_KERNEL_H_
29 
30 #define	_KERNEL
31 #undef __FreeBSD_version
32 #define	__FreeBSD_version 1100000
33 
34 #include <sys/cdefs.h>
35 #include <sys/queue.h>
36 #include <sys/errno.h>
37 
38 #define	howmany(x, y)	(((x)+((y)-1))/(y))
39 #define	nitems(x)	(sizeof((x)) / sizeof((x)[0]))
40 #define	isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
41 #define	isdigit(x) ((x) >= '0' && (x) <= '9')
42 #define	panic(...) do { printf("USB PANIC: " __VA_ARGS__); while (1) ; } while (0)
43 #define	rebooting 0
44 #define	M_USB 0
45 #define	M_USBDEV 0
46 #define	USB_PROC_MAX 3
47 #define	USB_BUS_GIANT_PROC(bus) (usb_process + 2)
48 #define	USB_BUS_NON_GIANT_BULK_PROC(bus) (usb_process + 2)
49 #define	USB_BUS_NON_GIANT_ISOC_PROC(bus) (usb_process + 2)
50 #define	USB_BUS_EXPLORE_PROC(bus) (usb_process + 0)
51 #define	USB_BUS_CONTROL_XFER_PROC(bus) (usb_process + 1)
52 #define	SYSCTL_DECL(...)
53 struct sysctl_req {
54 	void		*newptr;
55 };
56 #define	SYSCTL_HANDLER_ARGS void *oidp, void *arg1,	\
57 	uint32_t arg2, struct sysctl_req *req
58 #define	SYSCTL_NODE(name,...) struct { } name __used
59 #define	SYSCTL_INT(...)
60 #define	SYSCTL_UINT(...)
61 #define	SYSCTL_PROC(...)
62 #define	TUNABLE_INT(...)
63 #define	MALLOC_DECLARE(...)
64 #define	MALLOC_DEFINE(...)
65 #define	EVENTHANDLER_DECLARE(...)
66 #define	EVENTHANDLER_INVOKE(...)
67 #define	KASSERT(...)
68 #define	SCHEDULER_STOPPED(x) (0)
69 #define	PI_SWI(...) (0)
70 #define	UNIQ_NAME(x) x
71 #define	UNIQ_NAME_STR(x) #x
72 #define	DEVCLASS_MAXUNIT 32
73 #define	MOD_LOAD 1
74 #define	MOD_UNLOAD 2
75 #define	DEVMETHOD(what,func) { #what, (void *)&func }
76 #define	DEVMETHOD_END {0,0}
77 #define	EARLY_DRIVER_MODULE(a, b, c, d, e, f, g)	DRIVER_MODULE(a, b, c, d, e, f)
78 #define	DRIVER_MODULE(name, busname, driver, devclass, evh, arg)	\
79   static struct module_data bsd_##name##_##busname##_driver_mod = {	\
80 	evh, arg, #busname, #name, #busname "/" #name,			\
81 	&driver, &devclass, { 0, 0 } };					\
82 SYSINIT(bsd_##name##_##busname##_driver_mod, SI_SUB_DRIVERS,		\
83   SI_ORDER_MIDDLE, module_register,					\
84   &bsd_##name##_##busname##_driver_mod)
85 #define	SYSINIT(uniq, subs, order, _func, _data)	\
86 const struct sysinit UNIQ_NAME(sysinit_##uniq) = {	\
87 	.func = (_func),				\
88 	.data = __DECONST(void *, _data)		\
89 };							\
90 SYSINIT_ENTRY(uniq##_entry, "sysinit", (subs),		\
91     (order), "const struct sysinit",			\
92     UNIQ_NAME_STR(sysinit_##uniq), "SYSINIT")
93 
94 #define	SYSUNINIT(uniq, subs, order, _func, _data)	\
95 const struct sysinit UNIQ_NAME(sysuninit_##uniq) = {	\
96 	.func = (_func),				\
97 	.data = __DECONST(void *, _data)		\
98 };							\
99 SYSINIT_ENTRY(uniq##_entry, "sysuninit", (subs),	\
100     (order), "const struct sysuninit",			\
101     UNIQ_NAME_STR(sysuninit_##uniq), "SYSUNINIT")
102 #define	MODULE_DEPEND(...)
103 #define	MODULE_VERSION(...)
104 #define	NULL ((void *)0)
105 #define	BUS_SPACE_BARRIER_READ 0x01
106 #define	BUS_SPACE_BARRIER_WRITE 0x02
107 #define	hz 1000
108 #undef PAGE_SIZE
109 #define	PAGE_SIZE 4096
110 #undef MIN
111 #define	MIN(a,b) (((a) < (b)) ? (a) : (b))
112 #undef MAX
113 #define	MAX(a,b) (((a) > (b)) ? (a) : (b))
114 #define	MTX_DEF 0
115 #define	MTX_SPIN 0
116 #define	MTX_RECURSE 0
117 #define	SX_DUPOK 0
118 #define	SX_NOWITNESS 0
119 #define	WITNESS_WARN(...)
120 #define	cold 0
121 #define	BUS_PROBE_GENERIC 0
122 #define	BUS_PROBE_DEFAULT (-20)
123 #define	CALLOUT_RETURNUNLOCKED 0x1
124 #undef ffs
125 #define	ffs(x) __builtin_ffs(x)
126 #undef va_list
127 #define	va_list __builtin_va_list
128 #undef va_size
129 #define	va_size(type) __builtin_va_size(type)
130 #undef va_start
131 #define	va_start(ap, last) __builtin_va_start(ap, last)
132 #undef va_end
133 #define	va_end(ap) __builtin_va_end(ap)
134 #undef va_arg
135 #define	va_arg(ap, type) __builtin_va_arg((ap), type)
136 #define	DEVICE_ATTACH(dev, ...) \
137   (((device_attach_t *)(device_get_method(dev, "device_attach")))(dev,## __VA_ARGS__))
138 #define	DEVICE_DETACH(dev, ...) \
139   (((device_detach_t *)(device_get_method(dev, "device_detach")))(dev,## __VA_ARGS__))
140 #define	DEVICE_PROBE(dev, ...) \
141   (((device_probe_t *)(device_get_method(dev, "device_probe")))(dev,## __VA_ARGS__))
142 #define	DEVICE_RESUME(dev, ...) \
143   (((device_resume_t *)(device_get_method(dev, "device_resume")))(dev,## __VA_ARGS__))
144 #define	DEVICE_SHUTDOWN(dev, ...) \
145   (((device_shutdown_t *)(device_get_method(dev, "device_shutdown")))(dev,## __VA_ARGS__))
146 #define	DEVICE_SUSPEND(dev, ...) \
147   (((device_suspend_t *)(device_get_method(dev, "device_suspend")))(dev,## __VA_ARGS__))
148 #define	USB_HANDLE_REQUEST(dev, ...) \
149   (((usb_handle_request_t *)(device_get_method(dev, "usb_handle_request")))(dev,## __VA_ARGS__))
150 #define	USB_TAKE_CONTROLLER(dev, ...) \
151   (((usb_take_controller_t *)(device_get_method(dev, "usb_take_controller")))(dev,## __VA_ARGS__))
152 #define	GPIO_PIN_SET(dev, ...) \
153   (((gpio_pin_set_t *)(device_get_method(dev, "gpio_pin_set")))(dev,## __VA_ARGS__))
154 #define	GPIO_PIN_SETFLAGS(dev, ...) \
155   (((gpio_pin_setflags_t *)(device_get_method(dev, "gpio_pin_setflags")))(dev,## __VA_ARGS__))
156 
157 enum {
158 	SI_SUB_DUMMY = 0x0000000,
159 	SI_SUB_LOCK = 0x1B00000,
160 	SI_SUB_KLD = 0x2000000,
161 	SI_SUB_DRIVERS = 0x3100000,
162 	SI_SUB_PSEUDO = 0x7000000,
163 	SI_SUB_KICK_SCHEDULER = 0xa000000,
164 	SI_SUB_RUN_SCHEDULER = 0xfffffff
165 };
166 
167 enum {
168 	SI_ORDER_FIRST = 0x0000000,
169 	SI_ORDER_SECOND = 0x0000001,
170 	SI_ORDER_THIRD = 0x0000002,
171 	SI_ORDER_FOURTH = 0x0000003,
172 	SI_ORDER_MIDDLE = 0x1000000,
173 	SI_ORDER_ANY = 0xfffffff	/* last */
174 };
175 
176 struct uio;
177 struct thread;
178 struct malloc_type;
179 struct usb_process;
180 
181 #ifndef HAVE_STANDARD_DEFS
182 #define	_UINT8_T_DECLARED
183 typedef unsigned char uint8_t;
184 #define	_INT8_T_DECLARED
185 typedef signed char int8_t;
186 #define	_UINT16_T_DECLARED
187 typedef unsigned short uint16_t;
188 #define	_INT16_T_DECLARED
189 typedef signed short int16_t;
190 #define	_UINT32_T_DECLARED
191 typedef unsigned int uint32_t;
192 #define	_INT32_T_DECLARED
193 typedef signed int int32_t;
194 #define	_UINT64_T_DECLARED
195 typedef unsigned long long uint64_t;
196 #define	_INT16_T_DECLARED
197 typedef signed long long int64_t;
198 
199 typedef uint16_t uid_t;
200 typedef uint16_t gid_t;
201 typedef uint16_t mode_t;
202 
203 typedef uint8_t *caddr_t;
204 #define	_UINTPTR_T_DECLARED
205 typedef unsigned long uintptr_t;
206 
207 #define	_SIZE_T_DECLARED
208 typedef unsigned long size_t;
209 typedef unsigned long u_long;
210 #endif
211 
212 typedef unsigned long bus_addr_t;
213 typedef unsigned long bus_size_t;
214 
215 typedef struct bus_dma_segment {
216 	bus_addr_t	ds_addr;	/* DMA address */
217 	bus_size_t	ds_len;		/* length of transfer */
218 } bus_dma_segment_t;
219 
220 struct bus_dma_tag {
221 	uint32_t	alignment;
222 	uint32_t	maxsize;
223 };
224 
225 typedef void *bus_dmamap_t;
226 typedef struct bus_dma_tag *bus_dma_tag_t;
227 
228 typedef enum {
229 	BUS_DMA_LOCK	= 0x01,
230 	BUS_DMA_UNLOCK	= 0x02,
231 } bus_dma_lock_op_t;
232 
233 typedef void *bus_space_tag_t;
234 typedef uint8_t *bus_space_handle_t;
235 typedef int bus_dma_filter_t(void *, bus_addr_t);
236 typedef void bus_dma_lock_t(void *, bus_dma_lock_op_t);
237 
238 typedef uint32_t bool;
239 
240 /* SYSINIT API */
241 
242 #include <sysinit.h>
243 
244 struct sysinit {
245 	void    (*func) (void *arg);
246 	void   *data;
247 };
248 
249 /* MUTEX API */
250 
251 struct mtx {
252 	int	owned;
253 	struct mtx *parent;
254 };
255 
256 #define	mtx_assert(...) do { } while (0)
257 void	mtx_init(struct mtx *, const char *, const char *, int);
258 void	mtx_lock(struct mtx *);
259 void	mtx_unlock(struct mtx *);
260 #define	mtx_lock_spin(x) mtx_lock(x)
261 #define	mtx_unlock_spin(x) mtx_unlock(x)
262 int	mtx_owned(struct mtx *);
263 void	mtx_destroy(struct mtx *);
264 
265 extern struct mtx Giant;
266 
267 /* SX API */
268 
269 struct sx {
270 	int	owned;
271 };
272 
273 #define	sx_assert(...) do { } while (0)
274 #define	sx_init(...) sx_init_flags(__VA_ARGS__, 0)
275 void	sx_init_flags(struct sx *, const char *, int);
276 void	sx_destroy(struct sx *);
277 void	sx_xlock(struct sx *);
278 void	sx_xunlock(struct sx *);
279 int	sx_xlocked(struct sx *);
280 
281 /* CONDVAR API */
282 
283 struct cv {
284 	int	sleeping;
285 };
286 
287 void	cv_init(struct cv *, const char *desc);
288 void	cv_destroy(struct cv *);
289 void	cv_wait(struct cv *, struct mtx *);
290 int	cv_timedwait(struct cv *, struct mtx *, int);
291 void	cv_signal(struct cv *);
292 void	cv_broadcast(struct cv *);
293 
294 /* CALLOUT API */
295 
296 typedef void callout_fn_t (void *);
297 
298 extern volatile int ticks;
299 
300 struct callout {
301 	LIST_ENTRY(callout) entry;
302 	callout_fn_t *c_func;
303 	void   *c_arg;
304 	struct mtx *mtx;
305 	int	flags;
306 	int	timeout;
307 };
308 
309 void	callout_init_mtx(struct callout *, struct mtx *, int);
310 void	callout_reset(struct callout *, int, callout_fn_t *, void *);
311 void	callout_stop(struct callout *);
312 void	callout_drain(struct callout *);
313 int	callout_pending(struct callout *);
314 void	callout_process(int timeout);
315 
316 /* DEVICE API */
317 
318 struct driver;
319 struct devclass;
320 struct device;
321 struct module;
322 struct module_data;
323 
324 typedef struct driver driver_t;
325 typedef struct devclass *devclass_t;
326 typedef struct device *device_t;
327 typedef void (driver_intr_t)(void *arg);
328 typedef int (driver_filter_t)(void *arg);
329 #define	FILTER_STRAY		0x01
330 #define	FILTER_HANDLED		0x02
331 #define	FILTER_SCHEDULE_THREAD	0x04
332 
333 typedef int device_attach_t (device_t dev);
334 typedef int device_detach_t (device_t dev);
335 typedef int device_resume_t (device_t dev);
336 typedef int device_shutdown_t (device_t dev);
337 typedef int device_probe_t (device_t dev);
338 typedef int device_suspend_t (device_t dev);
339 typedef int gpio_pin_set_t (device_t dev, uint32_t, unsigned int);
340 typedef int gpio_pin_setflags_t (device_t dev, uint32_t, uint32_t);
341 
342 typedef int bus_child_location_str_t (device_t parent, device_t child, char *buf, size_t buflen);
343 typedef int bus_child_pnpinfo_str_t (device_t parent, device_t child, char *buf, size_t buflen);
344 typedef void bus_driver_added_t (device_t dev, driver_t *driver);
345 
346 struct device_method {
347 	const char *desc;
348 	void   *const func;
349 };
350 
351 typedef struct device_method device_method_t;
352 
353 struct device {
354 	TAILQ_HEAD(device_list, device) dev_children;
355 	TAILQ_ENTRY(device) dev_link;
356 
357 	struct device *dev_parent;
358 	const struct module_data *dev_module;
359 	void   *dev_sc;
360 	void   *dev_aux;
361 	driver_filter_t *dev_irq_filter;
362 	driver_intr_t *dev_irq_fn;
363 	void   *dev_irq_arg;
364 
365 	uint16_t dev_unit;
366 
367 	char	dev_nameunit[64];
368 	char	dev_desc[64];
369 
370 	uint8_t	dev_res_alloc:1;
371 	uint8_t	dev_quiet:1;
372 	uint8_t	dev_softc_set:1;
373 	uint8_t	dev_softc_alloc:1;
374 	uint8_t	dev_attached:1;
375 	uint8_t	dev_fixed_class:1;
376 	uint8_t	dev_unit_manual:1;
377 };
378 
379 struct devclass {
380 	device_t dev_list[DEVCLASS_MAXUNIT];
381 };
382 
383 struct driver {
384 	const char *name;
385 	const struct device_method *methods;
386 	uint32_t size;
387 };
388 
389 struct module_data {
390 	int     (*callback) (struct module *, int, void *arg);
391 	void   *arg;
392 	const char *bus_name;
393 	const char *mod_name;
394 	const char *long_name;
395 	const struct driver *driver;
396 	struct devclass **devclass_pp;
397 	TAILQ_ENTRY(module_data) entry;
398 };
399 
400 device_t device_get_parent(device_t dev);
401 void   *device_get_method(device_t dev, const char *what);
402 const char *device_get_name(device_t dev);
403 const char *device_get_nameunit(device_t dev);
404 
405 #define	device_printf(dev, fmt,...) \
406 	printf("%s: " fmt, device_get_nameunit(dev),## __VA_ARGS__)
407 device_t device_add_child(device_t dev, const char *name, int unit);
408 void	device_quiet(device_t dev);
409 void	device_set_interrupt(device_t dev, driver_filter_t *, driver_intr_t *, void *);
410 void	device_run_interrupts(device_t parent);
411 void	device_set_ivars(device_t dev, void *ivars);
412 void   *device_get_ivars(device_t dev);
413 const char *device_get_desc(device_t dev);
414 int	device_probe_and_attach(device_t dev);
415 int	device_detach(device_t dev);
416 void   *device_get_softc(device_t dev);
417 void	device_set_softc(device_t dev, void *softc);
418 int	device_delete_child(device_t dev, device_t child);
419 int	device_delete_children(device_t dev);
420 int	device_is_attached(device_t dev);
421 void	device_set_desc(device_t dev, const char *desc);
422 void	device_set_desc_copy(device_t dev, const char *desc);
423 int	device_get_unit(device_t dev);
424 void   *devclass_get_softc(devclass_t dc, int unit);
425 int	devclass_get_maxunit(devclass_t dc);
426 device_t devclass_get_device(devclass_t dc, int unit);
427 devclass_t devclass_find(const char *classname);
428 
429 #define	bus_get_dma_tag(...) (NULL)
430 int	bus_generic_detach(device_t dev);
431 int	bus_generic_resume(device_t dev);
432 int	bus_generic_shutdown(device_t dev);
433 int	bus_generic_suspend(device_t dev);
434 int	bus_generic_print_child(device_t dev, device_t child);
435 void	bus_generic_driver_added(device_t dev, driver_t *driver);
436 int	bus_space_subregion(bus_space_tag_t t, bus_space_handle_t bsh,
437     bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp);
438 
439 /* BUS SPACE API */
440 
441 void	bus_space_write_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t data);
442 void	bus_space_write_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t data);
443 void	bus_space_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t data);
444 
445 uint8_t	bus_space_read_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
446 uint16_t bus_space_read_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
447 uint32_t bus_space_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
448 
449 void	bus_space_read_multi_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t *datap, bus_size_t count);
450 void	bus_space_read_multi_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t *datap, bus_size_t count);
451 void	bus_space_read_multi_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t *datap, bus_size_t count);
452 
453 void	bus_space_write_multi_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t *datap, bus_size_t count);
454 void	bus_space_write_multi_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t *datap, bus_size_t count);
455 void	bus_space_write_multi_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t *datap, bus_size_t count);
456 
457 void	bus_space_read_region_1(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint8_t *datap, bus_size_t count);
458 void	bus_space_write_region_1(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint8_t *datap, bus_size_t count);
459 void	bus_space_read_region_4(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint32_t *datap, bus_size_t count);
460 void	bus_space_write_region_4(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint32_t *datap, bus_size_t count);
461 
462 void	bus_space_barrier(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, bus_size_t length, int flags);
463 
464 void	module_register(void *);
465 
466 /* LIB-C */
467 
468 void   *memset(void *, int, size_t len);
469 void   *memcpy(void *, const void *, size_t len);
470 int	printf(const char *,...) __printflike(1, 2);
471 int	snprintf(char *restrict str, size_t size, const char *restrict format,...) __printflike(3, 4);
472 size_t	strlen(const char *s);
473 
474 /* MALLOC API */
475 
476 #undef malloc
477 #define	malloc(s,x,f) usb_malloc(s)
478 void   *usb_malloc(size_t);
479 
480 #undef free
481 #define	free(p,x) usb_free(p)
482 void	usb_free(void *);
483 
484 #define	strdup(p,x) usb_strdup(p)
485 char   *usb_strdup(const char *str);
486 
487 /* ENDIANNESS */
488 
489 #ifndef HAVE_ENDIAN_DEFS
490 
491 /* Assume little endian */
492 
493 #define	htole64(x) ((uint64_t)(x))
494 #define	le64toh(x) ((uint64_t)(x))
495 
496 #define	htole32(x) ((uint32_t)(x))
497 #define	le32toh(x) ((uint32_t)(x))
498 
499 #define	htole16(x) ((uint16_t)(x))
500 #define	le16toh(x) ((uint16_t)(x))
501 
502 #define	be32toh(x) ((uint32_t)(x))
503 #define	htobe32(x) ((uint32_t)(x))
504 
505 #else
506 #include <sys/endian.h>
507 #endif
508 
509 /* USB */
510 
511 typedef int usb_handle_request_t (device_t dev, const void *req, void **pptr, uint16_t *plen, uint16_t offset, uint8_t *pstate);
512 typedef int usb_take_controller_t (device_t dev);
513 
514 void	usb_idle(void);
515 void	usb_init(void);
516 void	usb_uninit(void);
517 
518 /* set some defaults */
519 
520 #ifndef USB_POOL_SIZE
521 #define	USB_POOL_SIZE (1024*1024)	/* 1 MByte */
522 #endif
523 
524 int	pause(const char *, int);
525 void	DELAY(unsigned int);
526 
527 /* OTHER */
528 
529 struct selinfo {
530 };
531 
532 /* SYSTEM STARTUP API */
533 
534 extern const void *sysinit_data[];
535 extern const void *sysuninit_data[];
536 
537 /* Resources */
538 
539 enum intr_type {
540 	INTR_TYPE_TTY = 1,
541 	INTR_TYPE_BIO = 2,
542 	INTR_TYPE_NET = 4,
543 	INTR_TYPE_CAM = 8,
544 	INTR_TYPE_MISC = 16,
545 	INTR_TYPE_CLK = 32,
546 	INTR_TYPE_AV = 64,
547 	INTR_EXCL = 256,		/* exclusive interrupt */
548 	INTR_MPSAFE = 512,		/* this interrupt is SMP safe */
549 	INTR_ENTROPY = 1024,		/* this interrupt provides entropy */
550 	INTR_MD1 = 4096,		/* flag reserved for MD use */
551 	INTR_MD2 = 8192,		/* flag reserved for MD use */
552 	INTR_MD3 = 16384,		/* flag reserved for MD use */
553 	INTR_MD4 = 32768		/* flag reserved for MD use */
554 };
555 
556 struct resource_i {
557 	u_long		r_start;	/* index of the first entry in this resource */
558 	u_long		r_end;		/* index of the last entry (inclusive) */
559 };
560 
561 struct resource {
562 	struct resource_i	*__r_i;
563 	bus_space_tag_t		r_bustag; /* bus_space tag */
564 	bus_space_handle_t	r_bushandle;	/* bus_space handle */
565 };
566 
567 struct resource_spec {
568 	int	type;
569 	int	rid;
570 	int	flags;
571 };
572 
573 #define	SYS_RES_IRQ	1	/* interrupt lines */
574 #define	SYS_RES_DRQ	2	/* isa dma lines */
575 #define	SYS_RES_MEMORY	3	/* i/o memory */
576 #define	SYS_RES_IOPORT	4	/* i/o ports */
577 
578 #define	RF_ALLOCATED	0x0001	/* resource has been reserved */
579 #define	RF_ACTIVE	0x0002	/* resource allocation has been activated */
580 #define	RF_SHAREABLE	0x0004	/* resource permits contemporaneous sharing */
581 #define	RF_SPARE1	0x0008
582 #define	RF_SPARE2	0x0010
583 #define	RF_FIRSTSHARE	0x0020	/* first in sharing list */
584 #define	RF_PREFETCHABLE	0x0040	/* resource is prefetchable */
585 #define	RF_OPTIONAL	0x0080	/* for bus_alloc_resources() */
586 
587 int bus_alloc_resources(device_t, struct resource_spec *, struct resource **);
588 int bus_release_resource(device_t, int, int, struct resource *);
589 void bus_release_resources(device_t, const struct resource_spec *,
590     struct resource **);
591 struct resource *bus_alloc_resource_any(device_t, int, int *, unsigned int);
592 int bus_generic_attach(device_t);
593 bus_space_tag_t rman_get_bustag(struct resource *);
594 bus_space_handle_t rman_get_bushandle(struct resource *);
595 u_long rman_get_size(struct resource *);
596 int bus_setup_intr(device_t, struct resource *, int, driver_filter_t,
597     driver_intr_t, void *, void **);
598 int bus_teardown_intr(device_t, struct resource *, void *);
599 int ofw_bus_status_okay(device_t);
600 int ofw_bus_is_compatible(device_t, char *);
601 
602 extern int (*bus_alloc_resource_any_cb)(struct resource *res, device_t dev,
603     int type, int *rid, unsigned int flags);
604 extern int (*ofw_bus_status_ok_cb)(device_t dev);
605 extern int (*ofw_bus_is_compatible_cb)(device_t dev, char *name);
606 
607 #ifndef strlcpy
608 #define	strlcpy(d,s,n) snprintf((d),(n),"%s",(s))
609 #endif
610 
611 /* Should be defined in user application since it is machine-dependent */
612 extern int delay(unsigned int);
613 
614 /* BUS dma */
615 #define	BUS_SPACE_MAXADDR_24BIT	0xFFFFFF
616 #define	BUS_SPACE_MAXADDR_32BIT	0xFFFFFFFF
617 #define	BUS_SPACE_MAXADDR	0xFFFFFFFF
618 #define	BUS_SPACE_MAXSIZE_24BIT	0xFFFFFF
619 #define	BUS_SPACE_MAXSIZE_32BIT	0xFFFFFFFF
620 #define	BUS_SPACE_MAXSIZE	0xFFFFFFFF
621 
622 #define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
623 #define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
624 #define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
625 #define	BUS_DMA_COHERENT	0x04	/* hint: map memory in a coherent way */
626 #define	BUS_DMA_ZERO		0x08	/* allocate zero'ed memory */
627 #define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
628 #define	BUS_DMA_BUS2		0x20
629 #define	BUS_DMA_BUS3		0x40
630 #define	BUS_DMA_BUS4		0x80
631 
632 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
633 
634 int
635 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
636 		   bus_size_t boundary, bus_addr_t lowaddr,
637 		   bus_addr_t highaddr, bus_dma_filter_t *filter,
638 		   void *filterarg, bus_size_t maxsize, int nsegments,
639 		   bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
640 		   void *lockfuncarg, bus_dma_tag_t *dmat);
641 
642 int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
643     bus_dmamap_t *mapp);
644 void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
645 int bus_dma_tag_destroy(bus_dma_tag_t dmat);
646 
647 #endif					/* _BSD_KERNEL_H_ */
648