xref: /freebsd/sys/dev/bhnd/bhnd.h (revision 907b59d76938e654f0d040a888e8dfca3de1e222)
1 /*-
2  * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
3  * 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  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _BHND_BHND_H_
33 #define _BHND_BHND_H_
34 
35 #include <sys/types.h>
36 #include <sys/bus.h>
37 
38 #include <machine/bus.h>
39 
40 #include "bhnd_ids.h"
41 #include "bhnd_types.h"
42 #include "bhnd_debug.h"
43 #include "bhnd_bus_if.h"
44 #include "bhnd_match.h"
45 
46 #include "nvram/bhnd_nvram.h"
47 
48 extern devclass_t bhnd_devclass;
49 extern devclass_t bhnd_hostb_devclass;
50 extern devclass_t bhnd_nvram_devclass;
51 
52 /**
53  * bhnd child instance variables
54  */
55 enum bhnd_device_vars {
56 	BHND_IVAR_VENDOR,	/**< Designer's JEP-106 manufacturer ID. */
57 	BHND_IVAR_DEVICE,	/**< Part number */
58 	BHND_IVAR_HWREV,	/**< Core revision */
59 	BHND_IVAR_DEVICE_CLASS,	/**< Core class (@sa bhnd_devclass_t) */
60 	BHND_IVAR_VENDOR_NAME,	/**< Core vendor name */
61 	BHND_IVAR_DEVICE_NAME,	/**< Core name */
62 	BHND_IVAR_CORE_INDEX,	/**< Bus-assigned core number */
63 	BHND_IVAR_CORE_UNIT,	/**< Bus-assigned core unit number,
64 				     assigned sequentially (starting at 0) for
65 				     each vendor/device pair. */
66 };
67 
68 /**
69  * bhnd device probe priority bands.
70  */
71 enum {
72 	BHND_PROBE_ROOT         = 0,    /**< Nexus or host bridge */
73 	BHND_PROBE_BUS		= 1000,	/**< Busses and bridges */
74 	BHND_PROBE_CPU		= 2000,	/**< CPU devices */
75 	BHND_PROBE_INTERRUPT	= 3000,	/**< Interrupt controllers. */
76 	BHND_PROBE_TIMER	= 4000,	/**< Timers and clocks. */
77 	BHND_PROBE_RESOURCE	= 5000,	/**< Resource discovery (including NVRAM/SPROM) */
78 	BHND_PROBE_DEFAULT	= 6000,	/**< Default device priority */
79 };
80 
81 /**
82  * Constants defining fine grained ordering within a BHND_PROBE_* priority band.
83  *
84  * Example:
85  * @code
86  * BHND_PROBE_BUS + BHND_PROBE_ORDER_FIRST
87  * @endcode
88  */
89 enum {
90 	BHND_PROBE_ORDER_FIRST		= 0,
91 	BHND_PROBE_ORDER_EARLY		= 25,
92 	BHND_PROBE_ORDER_MIDDLE		= 50,
93 	BHND_PROBE_ORDER_LATE		= 75,
94 	BHND_PROBE_ORDER_LAST		= 100
95 
96 };
97 
98 /*
99  * Simplified accessors for bhnd device ivars
100  */
101 #define	BHND_ACCESSOR(var, ivar, type) \
102 	__BUS_ACCESSOR(bhnd, var, BHND, ivar, type)
103 
104 BHND_ACCESSOR(vendor,		VENDOR,		uint16_t);
105 BHND_ACCESSOR(device,		DEVICE,		uint16_t);
106 BHND_ACCESSOR(hwrev,		HWREV,		uint8_t);
107 BHND_ACCESSOR(class,		DEVICE_CLASS,	bhnd_devclass_t);
108 BHND_ACCESSOR(vendor_name,	VENDOR_NAME,	const char *);
109 BHND_ACCESSOR(device_name,	DEVICE_NAME,	const char *);
110 BHND_ACCESSOR(core_index,	CORE_INDEX,	u_int);
111 BHND_ACCESSOR(core_unit,	CORE_UNIT,	int);
112 
113 #undef	BHND_ACCESSOR
114 
115 /**
116  * A bhnd(4) board descriptor.
117  */
118 struct bhnd_board_info {
119 	uint16_t	board_vendor;	/**< PCI-SIG vendor ID (even on non-PCI
120 					  *  devices).
121 					  *
122 					  *  On PCI devices, this will generally
123 					  *  be the subsystem vendor ID, but the
124 					  *  value may be overridden in device
125 					  *  NVRAM.
126 					  */
127 	uint16_t	board_type;	/**< Board type (See BHND_BOARD_*)
128 					  *
129 					  *  On PCI devices, this will generally
130 					  *  be the subsystem device ID, but the
131 					  *  value may be overridden in device
132 					  *  NVRAM.
133 					  */
134 	uint16_t	board_rev;	/**< Board revision. */
135 	uint8_t		board_srom_rev;	/**< Board SROM format revision */
136 
137 	uint32_t	board_flags;	/**< Board flags (see BHND_BFL_*) */
138 	uint32_t	board_flags2;	/**< Board flags 2 (see BHND_BFL2_*) */
139 	uint32_t	board_flags3;	/**< Board flags 3 (see BHND_BFL3_*) */
140 };
141 
142 
143 /**
144  * Chip Identification
145  *
146  * This is read from the ChipCommon ID register; on earlier bhnd(4) devices
147  * where ChipCommon is unavailable, known values must be supplied.
148  */
149 struct bhnd_chipid {
150 	uint16_t	chip_id;	/**< chip id (BHND_CHIPID_*) */
151 	uint8_t		chip_rev;	/**< chip revision */
152 	uint8_t		chip_pkg;	/**< chip package (BHND_PKGID_*) */
153 	uint8_t		chip_type;	/**< chip type (BHND_CHIPTYPE_*) */
154 
155 	bhnd_addr_t	enum_addr;	/**< chip_type-specific enumeration
156 					  *  address; either the siba(4) base
157 					  *  core register block, or the bcma(4)
158 					  *  EROM core address. */
159 
160 	uint8_t		ncores;		/**< number of cores, if known. 0 if
161 					  *  not available. */
162 };
163 
164 /**
165  * A bhnd(4) core descriptor.
166  */
167 struct bhnd_core_info {
168 	uint16_t	vendor;		/**< JEP-106 vendor (BHND_MFGID_*) */
169 	uint16_t	device;		/**< device */
170 	uint16_t	hwrev;		/**< hardware revision */
171 	u_int		core_idx;	/**< bus-assigned core index */
172 	int		unit;		/**< bus-assigned core unit */
173 };
174 
175 /**
176 * A bhnd(4) bus resource.
177 *
178 * This provides an abstract interface to per-core resources that may require
179 * bus-level remapping of address windows prior to access.
180 */
181 struct bhnd_resource {
182 	struct resource	*res;		/**< the system resource. */
183 	bool		 direct;	/**< false if the resource requires
184 					 *   bus window remapping before it
185 					 *   is MMIO accessible. */
186 };
187 
188 /**
189  * Device quirk table descriptor.
190  */
191 struct bhnd_device_quirk {
192 	struct bhnd_device_match desc;		/**< device match descriptor */
193 	uint32_t		 quirks;	/**< quirk flags */
194 };
195 
196 #define	BHND_CORE_QUIRK(_rev, _flags)		\
197 	{{ BHND_MATCH_CORE_REV(_rev) }, (_flags) }
198 
199 #define	BHND_CHIP_QUIRK(_chip, _rev, _flags)	\
200 	{{ BHND_CHIP_IR(BCM ## _chip, _rev) }, (_flags) }
201 
202 #define	BHND_PKG_QUIRK(_chip, _pkg, _flags)	\
203 	{{ BHND_CHIP_IP(BCM ## _chip, BCM ## _chip ## _pkg) }, (_flags) }
204 
205 #define	BHND_BOARD_QUIRK(_board, _flags)	\
206 	{{ BHND_MATCH_BOARD_TYPE(_board) },	\
207 	    (_flags) }
208 
209 #define	BHND_DEVICE_QUIRK_END		{ { BHND_MATCH_ANY }, 0 }
210 #define	BHND_DEVICE_QUIRK_IS_END(_q)	\
211 	(((_q)->desc.m.match_flags == 0) && (_q)->quirks == 0)
212 
213 enum {
214 	BHND_DF_ANY	= 0,
215 	BHND_DF_HOSTB	= (1<<0),	/**< core is serving as the bus' host
216 					  *  bridge. implies BHND_DF_ADAPTER */
217 	BHND_DF_SOC	= (1<<1),	/**< core is attached to a native
218 					     bus (BHND_ATTACH_NATIVE) */
219 	BHND_DF_ADAPTER	= (1<<2),	/**< core is attached to a bridged
220 					  *  adapter (BHND_ATTACH_ADAPTER) */
221 };
222 
223 /** Device probe table descriptor */
224 struct bhnd_device {
225 	const struct bhnd_device_match	 core;		/**< core match descriptor */
226 	const char			*desc;		/**< device description, or NULL. */
227 	const struct bhnd_device_quirk	*quirks_table;	/**< quirks table for this device, or NULL */
228 	uint32_t			 device_flags;	/**< required BHND_DF_* flags */
229 };
230 
231 #define	_BHND_DEVICE(_vendor, _device, _desc, _quirks,		\
232      _flags, ...)						\
233 	{ { BHND_MATCH_CORE(BHND_MFGID_ ## _vendor,		\
234 	    BHND_COREID_ ## _device) }, _desc, _quirks,		\
235 	    _flags }
236 
237 #define	BHND_DEVICE(_vendor, _device, _desc, _quirks, ...)	\
238 	_BHND_DEVICE(_vendor, _device, _desc, _quirks,		\
239 	    ## __VA_ARGS__, 0)
240 
241 #define	BHND_DEVICE_END		{ { BHND_MATCH_ANY }, NULL, NULL, 0 }
242 #define	BHND_DEVICE_IS_END(_d)	\
243 	(BHND_MATCH_IS_ANY(&(_d)->core) && (_d)->desc == NULL)
244 
245 const char			*bhnd_vendor_name(uint16_t vendor);
246 const char			*bhnd_port_type_name(bhnd_port_type port_type);
247 const char			*bhnd_nvram_src_name(bhnd_nvram_src nvram_src);
248 
249 const char 			*bhnd_find_core_name(uint16_t vendor,
250 				     uint16_t device);
251 bhnd_devclass_t			 bhnd_find_core_class(uint16_t vendor,
252 				     uint16_t device);
253 
254 const char			*bhnd_core_name(const struct bhnd_core_info *ci);
255 bhnd_devclass_t			 bhnd_core_class(const struct bhnd_core_info *ci);
256 
257 
258 device_t			 bhnd_match_child(device_t dev,
259 				     const struct bhnd_core_match *desc);
260 
261 device_t			 bhnd_find_child(device_t dev,
262 				     bhnd_devclass_t class, int unit);
263 
264 device_t			 bhnd_find_bridge_root(device_t dev,
265 				     devclass_t bus_class);
266 
267 const struct bhnd_core_info	*bhnd_match_core(
268 				     const struct bhnd_core_info *cores,
269 				     u_int num_cores,
270 				     const struct bhnd_core_match *desc);
271 
272 const struct bhnd_core_info	*bhnd_find_core(
273 				     const struct bhnd_core_info *cores,
274 				     u_int num_cores, bhnd_devclass_t class);
275 
276 bool				 bhnd_core_matches(
277 				     const struct bhnd_core_info *core,
278 				     const struct bhnd_core_match *desc);
279 
280 bool				 bhnd_chip_matches(
281 				     const struct bhnd_chipid *chipid,
282 				     const struct bhnd_chip_match *desc);
283 
284 bool				 bhnd_board_matches(
285 				     const struct bhnd_board_info *info,
286 				     const struct bhnd_board_match *desc);
287 
288 bool				 bhnd_hwrev_matches(uint16_t hwrev,
289 				     const struct bhnd_hwrev_match *desc);
290 
291 bool				 bhnd_device_matches(device_t dev,
292 				     const struct bhnd_device_match *desc);
293 
294 const struct bhnd_device	*bhnd_device_lookup(device_t dev,
295 				     const struct bhnd_device *table,
296 				     size_t entry_size);
297 
298 uint32_t			 bhnd_device_quirks(device_t dev,
299 				     const struct bhnd_device *table,
300 				     size_t entry_size);
301 
302 struct bhnd_core_info		 bhnd_get_core_info(device_t dev);
303 
304 int				 bhnd_alloc_resources(device_t dev,
305 				     struct resource_spec *rs,
306 				     struct bhnd_resource **res);
307 
308 void				 bhnd_release_resources(device_t dev,
309 				     const struct resource_spec *rs,
310 				     struct bhnd_resource **res);
311 
312 struct bhnd_chipid		 bhnd_parse_chipid(uint32_t idreg,
313 				     bhnd_addr_t enum_addr);
314 
315 int				 bhnd_read_chipid(device_t dev,
316 				     struct resource_spec *rs,
317 				     bus_size_t chipc_offset,
318 				     struct bhnd_chipid *result);
319 
320 void				 bhnd_set_custom_core_desc(device_t dev,
321 				     const char *name);
322 void				 bhnd_set_default_core_desc(device_t dev);
323 
324 
325 bool				 bhnd_bus_generic_is_hw_disabled(device_t dev,
326 				     device_t child);
327 bool				 bhnd_bus_generic_is_region_valid(device_t dev,
328 				     device_t child, bhnd_port_type type,
329 				     u_int port, u_int region);
330 int				 bhnd_bus_generic_get_nvram_var(device_t dev,
331 				     device_t child, const char *name,
332 				     void *buf, size_t *size);
333 const struct bhnd_chipid	*bhnd_bus_generic_get_chipid(device_t dev,
334 				     device_t child);
335 int				 bhnd_bus_generic_read_board_info(device_t dev,
336 				     device_t child,
337 				     struct bhnd_board_info *info);
338 struct bhnd_resource		*bhnd_bus_generic_alloc_resource (device_t dev,
339 				     device_t child, int type, int *rid,
340 				     rman_res_t start, rman_res_t end,
341 				     rman_res_t count, u_int flags);
342 int				 bhnd_bus_generic_release_resource (device_t dev,
343 				     device_t child, int type, int rid,
344 				     struct bhnd_resource *r);
345 int				 bhnd_bus_generic_activate_resource (device_t dev,
346 				     device_t child, int type, int rid,
347 				     struct bhnd_resource *r);
348 int				 bhnd_bus_generic_deactivate_resource (device_t dev,
349 				     device_t child, int type, int rid,
350 				     struct bhnd_resource *r);
351 bhnd_attach_type		 bhnd_bus_generic_get_attach_type(device_t dev,
352 				     device_t child);
353 
354 
355 
356 /**
357  * Return the active host bridge core for the bhnd bus, if any, or NULL if
358  * not found.
359  *
360  * @param dev A bhnd bus device.
361  */
362 static inline device_t
363 bhnd_find_hostb_device(device_t dev) {
364 	return (BHND_BUS_FIND_HOSTB_DEVICE(dev));
365 }
366 
367 /**
368  * Return true if the hardware components required by @p dev are known to be
369  * unpopulated or otherwise unusable.
370  *
371  * In some cases, enumerated devices may have pins that are left floating, or
372  * the hardware may otherwise be non-functional; this method allows a parent
373  * device to explicitly specify if a successfully enumerated @p dev should
374  * be disabled.
375  *
376  * @param dev A bhnd bus child device.
377  */
378 static inline bool
379 bhnd_is_hw_disabled(device_t dev) {
380 	return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), dev));
381 }
382 
383 /**
384  * Return the BHND chip identification info for the bhnd bus.
385  *
386  * @param dev A bhnd bus child device.
387  */
388 static inline const struct bhnd_chipid *
389 bhnd_get_chipid(device_t dev) {
390 	return (BHND_BUS_GET_CHIPID(device_get_parent(dev), dev));
391 };
392 
393 /**
394  * Return the BHND attachment type of the parent bhnd bus.
395  *
396  * @param dev A bhnd bus child device.
397  *
398  * @retval BHND_ATTACH_ADAPTER if the bus is resident on a bridged adapter,
399  * such as a WiFi chipset.
400  * @retval BHND_ATTACH_NATIVE if the bus provides hardware services (clock,
401  * CPU, etc) to a directly attached native host.
402  */
403 static inline bhnd_attach_type
404 bhnd_get_attach_type (device_t dev) {
405 	return (BHND_BUS_GET_ATTACH_TYPE(device_get_parent(dev), dev));
406 }
407 
408 /**
409  * Attempt to read the BHND board identification from the bhnd bus.
410  *
411  * This relies on NVRAM access, and will fail if a valid NVRAM device cannot
412  * be found, or is not yet attached.
413  *
414  * @param dev The parent of @p child.
415  * @param child The bhnd device requesting board info.
416  * @param[out] info On success, will be populated with the bhnd(4) device's
417  * board information.
418  *
419  * @retval 0 success
420  * @retval ENODEV	No valid NVRAM source could be found.
421  * @retval non-zero	If reading @p name otherwise fails, a regular unix
422  *			error code will be returned.
423  */
424 static inline int
425 bhnd_read_board_info(device_t dev, struct bhnd_board_info *info)
426 {
427 	return (BHND_BUS_READ_BOARD_INFO(device_get_parent(dev), dev, info));
428 }
429 
430 /**
431  * Determine an NVRAM variable's expected size.
432  *
433  * @param 	dev	A bhnd bus child device.
434  * @param	name	The variable name.
435  * @param[out]	len	On success, the variable's size, in bytes.
436  *
437  * @retval 0		success
438  * @retval ENOENT	The requested variable was not found.
439  * @retval ENODEV	No valid NVRAM source could be found.
440  * @retval non-zero	If reading @p name otherwise fails, a regular unix
441  *			error code will be returned.
442  */
443 static inline int
444 bhnd_nvram_getvarlen(device_t dev, const char *name, size_t *len)
445 {
446 	return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), dev, name, NULL,
447 	    len));
448 }
449 
450 /**
451  * Read an NVRAM variable.
452  *
453  * @param 	dev	A bhnd bus child device.
454  * @param	name	The NVRAM variable name.
455  * @param	buf	A buffer large enough to hold @p len bytes. On success,
456  * 			the requested value will be written to this buffer.
457  * @param	len	The required variable length.
458  *
459  * @retval 0		success
460  * @retval ENOENT	The requested variable was not found.
461  * @retval EINVAL	If @p len does not match the actual variable size.
462  * @retval ENODEV	No valid NVRAM source could be found.
463  * @retval non-zero	If reading @p name otherwise fails, a regular unix
464  *			error code will be returned.
465  */
466 static inline int
467 bhnd_nvram_getvar(device_t dev, const char *name, void *buf, size_t len)
468 {
469 	size_t	var_len;
470 	int	error;
471 
472 	if ((error = bhnd_nvram_getvarlen(dev, name, &var_len)))
473 		return (error);
474 
475 	if (len != var_len)
476 		return (EINVAL);
477 
478 	return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), dev, name, buf,
479 	    &len));
480 }
481 
482 /**
483  * Allocate a resource from a device's parent bhnd(4) bus.
484  *
485  * @param dev The device requesting resource ownership.
486  * @param type The type of resource to allocate. This may be any type supported
487  * by the standard bus APIs.
488  * @param rid The bus-specific handle identifying the resource being allocated.
489  * @param start The start address of the resource.
490  * @param end The end address of the resource.
491  * @param count The size of the resource.
492  * @param flags The flags for the resource to be allocated. These may be any
493  * values supported by the standard bus APIs.
494  *
495  * To request the resource's default addresses, pass @p start and
496  * @p end values of @c 0 and @c ~0, respectively, and
497  * a @p count of @c 1.
498  *
499  * @retval NULL The resource could not be allocated.
500  * @retval resource The allocated resource.
501  */
502 static inline struct bhnd_resource *
503 bhnd_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
504     rman_res_t end, rman_res_t count, u_int flags)
505 {
506 	return BHND_BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, type, rid,
507 	    start, end, count, flags);
508 }
509 
510 
511 /**
512  * Allocate a resource from a device's parent bhnd(4) bus, using the
513  * resource's default start, end, and count values.
514  *
515  * @param dev The device requesting resource ownership.
516  * @param type The type of resource to allocate. This may be any type supported
517  * by the standard bus APIs.
518  * @param rid The bus-specific handle identifying the resource being allocated.
519  * @param flags The flags for the resource to be allocated. These may be any
520  * values supported by the standard bus APIs.
521  *
522  * @retval NULL The resource could not be allocated.
523  * @retval resource The allocated resource.
524  */
525 static inline struct bhnd_resource *
526 bhnd_alloc_resource_any(device_t dev, int type, int *rid, u_int flags)
527 {
528 	return bhnd_alloc_resource(dev, type, rid, 0, ~0, 1, flags);
529 }
530 
531 /**
532  * Activate a previously allocated bhnd resource.
533  *
534  * @param dev The device holding ownership of the allocated resource.
535  * @param type The type of the resource.
536  * @param rid The bus-specific handle identifying the resource.
537  * @param r A pointer to the resource returned by bhnd_alloc_resource or
538  * BHND_BUS_ALLOC_RESOURCE.
539  *
540  * @retval 0 success
541  * @retval non-zero an error occurred while activating the resource.
542  */
543 static inline int
544 bhnd_activate_resource(device_t dev, int type, int rid,
545    struct bhnd_resource *r)
546 {
547 	return BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
548 	    rid, r);
549 }
550 
551 /**
552  * Deactivate a previously activated bhnd resource.
553  *
554  * @param dev The device holding ownership of the activated resource.
555  * @param type The type of the resource.
556  * @param rid The bus-specific handle identifying the resource.
557  * @param r A pointer to the resource returned by bhnd_alloc_resource or
558  * BHND_BUS_ALLOC_RESOURCE.
559  *
560  * @retval 0 success
561  * @retval non-zero an error occurred while activating the resource.
562  */
563 static inline int
564 bhnd_deactivate_resource(device_t dev, int type, int rid,
565    struct bhnd_resource *r)
566 {
567 	return BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
568 	    rid, r);
569 }
570 
571 /**
572  * Free a resource allocated by bhnd_alloc_resource().
573  *
574  * @param dev The device holding ownership of the resource.
575  * @param type The type of the resource.
576  * @param rid The bus-specific handle identifying the resource.
577  * @param r A pointer to the resource returned by bhnd_alloc_resource or
578  * BHND_ALLOC_RESOURCE.
579  *
580  * @retval 0 success
581  * @retval non-zero an error occurred while activating the resource.
582  */
583 static inline int
584 bhnd_release_resource(device_t dev, int type, int rid,
585    struct bhnd_resource *r)
586 {
587 	return BHND_BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, type,
588 	    rid, r);
589 }
590 
591 /**
592  * Return true if @p region_num is a valid region on @p port_num of
593  * @p type attached to @p dev.
594  *
595  * @param dev A bhnd bus child device.
596  * @param type The port type being queried.
597  * @param port_num The port number being queried.
598  * @param region_num The region number being queried.
599  */
600 static inline bool
601 bhnd_is_region_valid(device_t dev, bhnd_port_type type, u_int port_num,
602     u_int region_num)
603 {
604 	return (BHND_BUS_IS_REGION_VALID(device_get_parent(dev), dev, type,
605 	    port_num, region_num));
606 }
607 
608 /**
609  * Return the number of ports of type @p type attached to @p def.
610  *
611  * @param dev A bhnd bus child device.
612  * @param type The port type being queried.
613  */
614 static inline u_int
615 bhnd_get_port_count(device_t dev, bhnd_port_type type) {
616 	return (BHND_BUS_GET_PORT_COUNT(device_get_parent(dev), dev, type));
617 }
618 
619 /**
620  * Return the number of memory regions mapped to @p child @p port of
621  * type @p type.
622  *
623  * @param dev A bhnd bus child device.
624  * @param port The port number being queried.
625  * @param type The port type being queried.
626  */
627 static inline u_int
628 bhnd_get_region_count(device_t dev, bhnd_port_type type, u_int port) {
629 	return (BHND_BUS_GET_REGION_COUNT(device_get_parent(dev), dev, type,
630 	    port));
631 }
632 
633 /**
634  * Return the resource-ID for a memory region on the given device port.
635  *
636  * @param dev A bhnd bus child device.
637  * @param type The port type.
638  * @param port The port identifier.
639  * @param region The identifier of the memory region on @p port.
640  *
641  * @retval int The RID for the given @p port and @p region on @p device.
642  * @retval -1 No such port/region found.
643  */
644 static inline int
645 bhnd_get_port_rid(device_t dev, bhnd_port_type type, u_int port, u_int region)
646 {
647 	return BHND_BUS_GET_PORT_RID(device_get_parent(dev), dev, type, port,
648 	    region);
649 }
650 
651 /**
652  * Decode a port / region pair on @p dev defined by @p rid.
653  *
654  * @param dev A bhnd bus child device.
655  * @param type The resource type.
656  * @param rid The resource identifier.
657  * @param[out] port_type The decoded port type.
658  * @param[out] port The decoded port identifier.
659  * @param[out] region The decoded region identifier.
660  *
661  * @retval 0 success
662  * @retval non-zero No matching port/region found.
663  */
664 static inline int
665 bhnd_decode_port_rid(device_t dev, int type, int rid, bhnd_port_type *port_type,
666     u_int *port, u_int *region)
667 {
668 	return BHND_BUS_DECODE_PORT_RID(device_get_parent(dev), dev, type, rid,
669 	    port_type, port, region);
670 }
671 
672 /**
673  * Get the address and size of @p region on @p port.
674  *
675  * @param dev A bhnd bus child device.
676  * @param port_type The port type.
677  * @param port The port identifier.
678  * @param region The identifier of the memory region on @p port.
679  * @param[out] region_addr The region's base address.
680  * @param[out] region_size The region's size.
681  *
682  * @retval 0 success
683  * @retval non-zero No matching port/region found.
684  */
685 static inline int
686 bhnd_get_region_addr(device_t dev, bhnd_port_type port_type, u_int port,
687     u_int region, bhnd_addr_t *region_addr, bhnd_size_t *region_size)
688 {
689 	return BHND_BUS_GET_REGION_ADDR(device_get_parent(dev), dev, port_type,
690 	    port, region, region_addr, region_size);
691 }
692 
693 /*
694  * bhnd bus-level equivalents of the bus_(read|write|set|barrier|...)
695  * macros (compatible with bhnd_resource).
696  *
697  * Generated with bhnd/tools/bus_macro.sh
698  */
699 #define bhnd_bus_barrier(r, o, l, f) \
700     ((r)->direct) ? \
701 	bus_barrier((r)->res, (o), (l), (f)) : \
702 	BHND_BUS_BARRIER( \
703 	    device_get_parent(rman_get_device((r)->res)),	\
704 	    rman_get_device((r)->res), (r), (o), (l), (f))
705 #define bhnd_bus_read_1(r, o) \
706     ((r)->direct) ? \
707 	bus_read_1((r)->res, (o)) : \
708 	BHND_BUS_READ_1( \
709 	    device_get_parent(rman_get_device((r)->res)),	\
710 	    rman_get_device((r)->res), (r), (o))
711 #define bhnd_bus_read_multi_1(r, o, d, c) \
712     ((r)->direct) ? \
713 	bus_read_multi_1((r)->res, (o), (d), (c)) : \
714 	BHND_BUS_READ_MULTI_1( \
715 	    device_get_parent(rman_get_device((r)->res)),	\
716 	    rman_get_device((r)->res), (r), (o), (d), (c))
717 #define bhnd_bus_read_region_1(r, o, d, c) \
718     ((r)->direct) ? \
719 	bus_read_region_1((r)->res, (o), (d), (c)) : \
720 	BHND_BUS_READ_REGION_1( \
721 	    device_get_parent(rman_get_device((r)->res)),	\
722 	    rman_get_device((r)->res), (r), (o), (d), (c))
723 #define bhnd_bus_write_1(r, o, v) \
724     ((r)->direct) ? \
725 	bus_write_1((r)->res, (o), (v)) : \
726 	BHND_BUS_WRITE_1( \
727 	    device_get_parent(rman_get_device((r)->res)),	\
728 	    rman_get_device((r)->res), (r), (o), (v))
729 #define bhnd_bus_write_multi_1(r, o, d, c) \
730     ((r)->direct) ? \
731 	bus_write_multi_1((r)->res, (o), (d), (c)) : \
732 	BHND_BUS_WRITE_MULTI_1( \
733 	    device_get_parent(rman_get_device((r)->res)),	\
734 	    rman_get_device((r)->res), (r), (o), (d), (c))
735 #define bhnd_bus_write_region_1(r, o, d, c) \
736     ((r)->direct) ? \
737 	bus_write_region_1((r)->res, (o), (d), (c)) : \
738 	BHND_BUS_WRITE_REGION_1( \
739 	    device_get_parent(rman_get_device((r)->res)),	\
740 	    rman_get_device((r)->res), (r), (o), (d), (c))
741 #define bhnd_bus_read_stream_1(r, o) \
742     ((r)->direct) ? \
743 	bus_read_stream_1((r)->res, (o)) : \
744 	BHND_BUS_READ_STREAM_1( \
745 	    device_get_parent(rman_get_device((r)->res)),	\
746 	    rman_get_device((r)->res), (r), (o))
747 #define bhnd_bus_read_multi_stream_1(r, o, d, c) \
748     ((r)->direct) ? \
749 	bus_read_multi_stream_1((r)->res, (o), (d), (c)) : \
750 	BHND_BUS_READ_MULTI_STREAM_1( \
751 	    device_get_parent(rman_get_device((r)->res)),	\
752 	    rman_get_device((r)->res), (r), (o), (d), (c))
753 #define bhnd_bus_read_region_stream_1(r, o, d, c) \
754     ((r)->direct) ? \
755 	bus_read_region_stream_1((r)->res, (o), (d), (c)) : \
756 	BHND_BUS_READ_REGION_STREAM_1( \
757 	    device_get_parent(rman_get_device((r)->res)),	\
758 	    rman_get_device((r)->res), (r), (o), (d), (c))
759 #define bhnd_bus_write_stream_1(r, o, v) \
760     ((r)->direct) ? \
761 	bus_write_stream_1((r)->res, (o), (v)) : \
762 	BHND_BUS_WRITE_STREAM_1( \
763 	    device_get_parent(rman_get_device((r)->res)),	\
764 	    rman_get_device((r)->res), (r), (o), (v))
765 #define bhnd_bus_write_multi_stream_1(r, o, d, c) \
766     ((r)->direct) ? \
767 	bus_write_multi_stream_1((r)->res, (o), (d), (c)) : \
768 	BHND_BUS_WRITE_MULTI_STREAM_1( \
769 	    device_get_parent(rman_get_device((r)->res)),	\
770 	    rman_get_device((r)->res), (r), (o), (d), (c))
771 #define bhnd_bus_write_region_stream_1(r, o, d, c) \
772     ((r)->direct) ? \
773 	bus_write_region_stream_1((r)->res, (o), (d), (c)) : \
774 	BHND_BUS_WRITE_REGION_STREAM_1( \
775 	    device_get_parent(rman_get_device((r)->res)),	\
776 	    rman_get_device((r)->res), (r), (o), (d), (c))
777 #define bhnd_bus_set_multi_1(r, o, v, c) \
778     ((r)->direct) ? \
779 	bus_set_multi_1((r)->res, (o), (v), (c)) : \
780 	BHND_BUS_SET_MULTI_1( \
781 	    device_get_parent(rman_get_device((r)->res)),	\
782 	    rman_get_device((r)->res), (r), (o), (v), (c))
783 #define bhnd_bus_set_region_1(r, o, v, c) \
784     ((r)->direct) ? \
785 	bus_set_region_1((r)->res, (o), (v), (c)) : \
786 	BHND_BUS_SET_REGION_1( \
787 	    device_get_parent(rman_get_device((r)->res)),	\
788 	    rman_get_device((r)->res), (r), (o), (v), (c))
789 #define bhnd_bus_read_2(r, o) \
790     ((r)->direct) ? \
791 	bus_read_2((r)->res, (o)) : \
792 	BHND_BUS_READ_2( \
793 	    device_get_parent(rman_get_device((r)->res)),	\
794 	    rman_get_device((r)->res), (r), (o))
795 #define bhnd_bus_read_multi_2(r, o, d, c) \
796     ((r)->direct) ? \
797 	bus_read_multi_2((r)->res, (o), (d), (c)) : \
798 	BHND_BUS_READ_MULTI_2( \
799 	    device_get_parent(rman_get_device((r)->res)),	\
800 	    rman_get_device((r)->res), (r), (o), (d), (c))
801 #define bhnd_bus_read_region_2(r, o, d, c) \
802     ((r)->direct) ? \
803 	bus_read_region_2((r)->res, (o), (d), (c)) : \
804 	BHND_BUS_READ_REGION_2( \
805 	    device_get_parent(rman_get_device((r)->res)),	\
806 	    rman_get_device((r)->res), (r), (o), (d), (c))
807 #define bhnd_bus_write_2(r, o, v) \
808     ((r)->direct) ? \
809 	bus_write_2((r)->res, (o), (v)) : \
810 	BHND_BUS_WRITE_2( \
811 	    device_get_parent(rman_get_device((r)->res)),	\
812 	    rman_get_device((r)->res), (r), (o), (v))
813 #define bhnd_bus_write_multi_2(r, o, d, c) \
814     ((r)->direct) ? \
815 	bus_write_multi_2((r)->res, (o), (d), (c)) : \
816 	BHND_BUS_WRITE_MULTI_2( \
817 	    device_get_parent(rman_get_device((r)->res)),	\
818 	    rman_get_device((r)->res), (r), (o), (d), (c))
819 #define bhnd_bus_write_region_2(r, o, d, c) \
820     ((r)->direct) ? \
821 	bus_write_region_2((r)->res, (o), (d), (c)) : \
822 	BHND_BUS_WRITE_REGION_2( \
823 	    device_get_parent(rman_get_device((r)->res)),	\
824 	    rman_get_device((r)->res), (r), (o), (d), (c))
825 #define bhnd_bus_read_stream_2(r, o) \
826     ((r)->direct) ? \
827 	bus_read_stream_2((r)->res, (o)) : \
828 	BHND_BUS_READ_STREAM_2( \
829 	    device_get_parent(rman_get_device((r)->res)),	\
830 	    rman_get_device((r)->res), (r), (o))
831 #define bhnd_bus_read_multi_stream_2(r, o, d, c) \
832     ((r)->direct) ? \
833 	bus_read_multi_stream_2((r)->res, (o), (d), (c)) : \
834 	BHND_BUS_READ_MULTI_STREAM_2( \
835 	    device_get_parent(rman_get_device((r)->res)),	\
836 	    rman_get_device((r)->res), (r), (o), (d), (c))
837 #define bhnd_bus_read_region_stream_2(r, o, d, c) \
838     ((r)->direct) ? \
839 	bus_read_region_stream_2((r)->res, (o), (d), (c)) : \
840 	BHND_BUS_READ_REGION_STREAM_2( \
841 	    device_get_parent(rman_get_device((r)->res)),	\
842 	    rman_get_device((r)->res), (r), (o), (d), (c))
843 #define bhnd_bus_write_stream_2(r, o, v) \
844     ((r)->direct) ? \
845 	bus_write_stream_2((r)->res, (o), (v)) : \
846 	BHND_BUS_WRITE_STREAM_2( \
847 	    device_get_parent(rman_get_device((r)->res)),	\
848 	    rman_get_device((r)->res), (r), (o), (v))
849 #define bhnd_bus_write_multi_stream_2(r, o, d, c) \
850     ((r)->direct) ? \
851 	bus_write_multi_stream_2((r)->res, (o), (d), (c)) : \
852 	BHND_BUS_WRITE_MULTI_STREAM_2( \
853 	    device_get_parent(rman_get_device((r)->res)),	\
854 	    rman_get_device((r)->res), (r), (o), (d), (c))
855 #define bhnd_bus_write_region_stream_2(r, o, d, c) \
856     ((r)->direct) ? \
857 	bus_write_region_stream_2((r)->res, (o), (d), (c)) : \
858 	BHND_BUS_WRITE_REGION_STREAM_2( \
859 	    device_get_parent(rman_get_device((r)->res)),	\
860 	    rman_get_device((r)->res), (r), (o), (d), (c))
861 #define bhnd_bus_set_multi_2(r, o, v, c) \
862     ((r)->direct) ? \
863 	bus_set_multi_2((r)->res, (o), (v), (c)) : \
864 	BHND_BUS_SET_MULTI_2( \
865 	    device_get_parent(rman_get_device((r)->res)),	\
866 	    rman_get_device((r)->res), (r), (o), (v), (c))
867 #define bhnd_bus_set_region_2(r, o, v, c) \
868     ((r)->direct) ? \
869 	bus_set_region_2((r)->res, (o), (v), (c)) : \
870 	BHND_BUS_SET_REGION_2( \
871 	    device_get_parent(rman_get_device((r)->res)),	\
872 	    rman_get_device((r)->res), (r), (o), (v), (c))
873 #define bhnd_bus_read_4(r, o) \
874     ((r)->direct) ? \
875 	bus_read_4((r)->res, (o)) : \
876 	BHND_BUS_READ_4( \
877 	    device_get_parent(rman_get_device((r)->res)),	\
878 	    rman_get_device((r)->res), (r), (o))
879 #define bhnd_bus_read_multi_4(r, o, d, c) \
880     ((r)->direct) ? \
881 	bus_read_multi_4((r)->res, (o), (d), (c)) : \
882 	BHND_BUS_READ_MULTI_4( \
883 	    device_get_parent(rman_get_device((r)->res)),	\
884 	    rman_get_device((r)->res), (r), (o), (d), (c))
885 #define bhnd_bus_read_region_4(r, o, d, c) \
886     ((r)->direct) ? \
887 	bus_read_region_4((r)->res, (o), (d), (c)) : \
888 	BHND_BUS_READ_REGION_4( \
889 	    device_get_parent(rman_get_device((r)->res)),	\
890 	    rman_get_device((r)->res), (r), (o), (d), (c))
891 #define bhnd_bus_write_4(r, o, v) \
892     ((r)->direct) ? \
893 	bus_write_4((r)->res, (o), (v)) : \
894 	BHND_BUS_WRITE_4( \
895 	    device_get_parent(rman_get_device((r)->res)),	\
896 	    rman_get_device((r)->res), (r), (o), (v))
897 #define bhnd_bus_write_multi_4(r, o, d, c) \
898     ((r)->direct) ? \
899 	bus_write_multi_4((r)->res, (o), (d), (c)) : \
900 	BHND_BUS_WRITE_MULTI_4( \
901 	    device_get_parent(rman_get_device((r)->res)),	\
902 	    rman_get_device((r)->res), (r), (o), (d), (c))
903 #define bhnd_bus_write_region_4(r, o, d, c) \
904     ((r)->direct) ? \
905 	bus_write_region_4((r)->res, (o), (d), (c)) : \
906 	BHND_BUS_WRITE_REGION_4( \
907 	    device_get_parent(rman_get_device((r)->res)),	\
908 	    rman_get_device((r)->res), (r), (o), (d), (c))
909 #define bhnd_bus_read_stream_4(r, o) \
910     ((r)->direct) ? \
911 	bus_read_stream_4((r)->res, (o)) : \
912 	BHND_BUS_READ_STREAM_4( \
913 	    device_get_parent(rman_get_device((r)->res)),	\
914 	    rman_get_device((r)->res), (r), (o))
915 #define bhnd_bus_read_multi_stream_4(r, o, d, c) \
916     ((r)->direct) ? \
917 	bus_read_multi_stream_4((r)->res, (o), (d), (c)) : \
918 	BHND_BUS_READ_MULTI_STREAM_4( \
919 	    device_get_parent(rman_get_device((r)->res)),	\
920 	    rman_get_device((r)->res), (r), (o), (d), (c))
921 #define bhnd_bus_read_region_stream_4(r, o, d, c) \
922     ((r)->direct) ? \
923 	bus_read_region_stream_4((r)->res, (o), (d), (c)) : \
924 	BHND_BUS_READ_REGION_STREAM_4( \
925 	    device_get_parent(rman_get_device((r)->res)),	\
926 	    rman_get_device((r)->res), (r), (o), (d), (c))
927 #define bhnd_bus_write_stream_4(r, o, v) \
928     ((r)->direct) ? \
929 	bus_write_stream_4((r)->res, (o), (v)) : \
930 	BHND_BUS_WRITE_STREAM_4( \
931 	    device_get_parent(rman_get_device((r)->res)),	\
932 	    rman_get_device((r)->res), (r), (o), (v))
933 #define bhnd_bus_write_multi_stream_4(r, o, d, c) \
934     ((r)->direct) ? \
935 	bus_write_multi_stream_4((r)->res, (o), (d), (c)) : \
936 	BHND_BUS_WRITE_MULTI_STREAM_4( \
937 	    device_get_parent(rman_get_device((r)->res)),	\
938 	    rman_get_device((r)->res), (r), (o), (d), (c))
939 #define bhnd_bus_write_region_stream_4(r, o, d, c) \
940     ((r)->direct) ? \
941 	bus_write_region_stream_4((r)->res, (o), (d), (c)) : \
942 	BHND_BUS_WRITE_REGION_STREAM_4( \
943 	    device_get_parent(rman_get_device((r)->res)),	\
944 	    rman_get_device((r)->res), (r), (o), (d), (c))
945 #define bhnd_bus_set_multi_4(r, o, v, c) \
946     ((r)->direct) ? \
947 	bus_set_multi_4((r)->res, (o), (v), (c)) : \
948 	BHND_BUS_SET_MULTI_4( \
949 	    device_get_parent(rman_get_device((r)->res)),	\
950 	    rman_get_device((r)->res), (r), (o), (v), (c))
951 #define bhnd_bus_set_region_4(r, o, v, c) \
952     ((r)->direct) ? \
953 	bus_set_region_4((r)->res, (o), (v), (c)) : \
954 	BHND_BUS_SET_REGION_4( \
955 	    device_get_parent(rman_get_device((r)->res)),	\
956 	    rman_get_device((r)->res), (r), (o), (v), (c))
957 
958 #endif /* _BHND_BHND_H_ */
959