xref: /freebsd/sys/dev/bhnd/bhnd.h (revision ce437beff1f521b5ac35ea67d0669c46b3bef27f)
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 int				 bhnd_nvram_getvar_str(device_t dev,
325 				     const char *name, char *buf, size_t len,
326 				     size_t *rlen);
327 
328 int				 bhnd_nvram_getvar_uint(device_t dev,
329 				     const char *name, void *value, int width);
330 int				 bhnd_nvram_getvar_uint8(device_t dev,
331 				     const char *name, uint8_t *value);
332 int				 bhnd_nvram_getvar_uint16(device_t dev,
333 				     const char *name, uint16_t *value);
334 int				 bhnd_nvram_getvar_uint32(device_t dev,
335 				     const char *name, uint32_t *value);
336 
337 int				 bhnd_nvram_getvar_int(device_t dev,
338 				     const char *name, void *value, int width);
339 int				 bhnd_nvram_getvar_int8(device_t dev,
340 				     const char *name, int8_t *value);
341 int				 bhnd_nvram_getvar_int16(device_t dev,
342 				     const char *name, int16_t *value);
343 int				 bhnd_nvram_getvar_int32(device_t dev,
344 				     const char *name, int32_t *value);
345 
346 int				 bhnd_nvram_getvar_array(device_t dev,
347 				     const char *name, void *buf, size_t count,
348 				     bhnd_nvram_type type);
349 
350 bool				 bhnd_bus_generic_is_hw_disabled(device_t dev,
351 				     device_t child);
352 bool				 bhnd_bus_generic_is_region_valid(device_t dev,
353 				     device_t child, bhnd_port_type type,
354 				     u_int port, u_int region);
355 int				 bhnd_bus_generic_get_nvram_var(device_t dev,
356 				     device_t child, const char *name,
357 				     void *buf, size_t *size,
358 				     bhnd_nvram_type type);
359 const struct bhnd_chipid	*bhnd_bus_generic_get_chipid(device_t dev,
360 				     device_t child);
361 int				 bhnd_bus_generic_read_board_info(device_t dev,
362 				     device_t child,
363 				     struct bhnd_board_info *info);
364 struct bhnd_resource		*bhnd_bus_generic_alloc_resource (device_t dev,
365 				     device_t child, int type, int *rid,
366 				     rman_res_t start, rman_res_t end,
367 				     rman_res_t count, u_int flags);
368 int				 bhnd_bus_generic_release_resource (device_t dev,
369 				     device_t child, int type, int rid,
370 				     struct bhnd_resource *r);
371 int				 bhnd_bus_generic_activate_resource (device_t dev,
372 				     device_t child, int type, int rid,
373 				     struct bhnd_resource *r);
374 int				 bhnd_bus_generic_deactivate_resource (device_t dev,
375 				     device_t child, int type, int rid,
376 				     struct bhnd_resource *r);
377 bhnd_attach_type		 bhnd_bus_generic_get_attach_type(device_t dev,
378 				     device_t child);
379 
380 
381 
382 /**
383  * Return the active host bridge core for the bhnd bus, if any, or NULL if
384  * not found.
385  *
386  * @param dev A bhnd bus device.
387  */
388 static inline device_t
389 bhnd_find_hostb_device(device_t dev) {
390 	return (BHND_BUS_FIND_HOSTB_DEVICE(dev));
391 }
392 
393 /**
394  * Return true if the hardware components required by @p dev are known to be
395  * unpopulated or otherwise unusable.
396  *
397  * In some cases, enumerated devices may have pins that are left floating, or
398  * the hardware may otherwise be non-functional; this method allows a parent
399  * device to explicitly specify if a successfully enumerated @p dev should
400  * be disabled.
401  *
402  * @param dev A bhnd bus child device.
403  */
404 static inline bool
405 bhnd_is_hw_disabled(device_t dev) {
406 	return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), dev));
407 }
408 
409 /**
410  * Return the BHND chip identification info for the bhnd bus.
411  *
412  * @param dev A bhnd bus child device.
413  */
414 static inline const struct bhnd_chipid *
415 bhnd_get_chipid(device_t dev) {
416 	return (BHND_BUS_GET_CHIPID(device_get_parent(dev), dev));
417 };
418 
419 /**
420  * Return the BHND attachment type of the parent bhnd bus.
421  *
422  * @param dev A bhnd bus child device.
423  *
424  * @retval BHND_ATTACH_ADAPTER if the bus is resident on a bridged adapter,
425  * such as a WiFi chipset.
426  * @retval BHND_ATTACH_NATIVE if the bus provides hardware services (clock,
427  * CPU, etc) to a directly attached native host.
428  */
429 static inline bhnd_attach_type
430 bhnd_get_attach_type (device_t dev) {
431 	return (BHND_BUS_GET_ATTACH_TYPE(device_get_parent(dev), dev));
432 }
433 
434 /**
435  * Attempt to read the BHND board identification from the bhnd bus.
436  *
437  * This relies on NVRAM access, and will fail if a valid NVRAM device cannot
438  * be found, or is not yet attached.
439  *
440  * @param dev The parent of @p child.
441  * @param child The bhnd device requesting board info.
442  * @param[out] info On success, will be populated with the bhnd(4) device's
443  * board information.
444  *
445  * @retval 0 success
446  * @retval ENODEV	No valid NVRAM source could be found.
447  * @retval non-zero	If reading @p name otherwise fails, a regular unix
448  *			error code will be returned.
449  */
450 static inline int
451 bhnd_read_board_info(device_t dev, struct bhnd_board_info *info)
452 {
453 	return (BHND_BUS_READ_BOARD_INFO(device_get_parent(dev), dev, info));
454 }
455 
456 /**
457  * Read an NVRAM variable, coerced to the requested @p type.
458  *
459  * @param 		dev	A bhnd bus child device.
460  * @param		name	The NVRAM variable name.
461  * @param[out]		buf	A buffer large enough to hold @p len bytes. On
462  *				success, the requested value will be written to
463  *				this buffer. This argment may be NULL if
464  *				the value is not desired.
465  * @param[in,out]	len	The maximum capacity of @p buf. On success,
466  *				will be set to the actual size of the requested
467  *				value.
468  * @param		type	The desired data representation to be written
469  *				to @p buf.
470  *
471  * @retval 0		success
472  * @retval ENOENT	The requested variable was not found.
473  * @retval ENODEV	No valid NVRAM source could be found.
474  * @retval ENOMEM	If a buffer of @p size is too small to hold the
475  *			requested value.
476  * @retval EOPNOTSUPP	If the value cannot be coerced to @p type.
477  * @retval ERANGE	If value coercion would overflow @p type.
478  * @retval non-zero	If reading @p name otherwise fails, a regular unix
479  *			error code will be returned.
480  */
481 static inline int
482 bhnd_nvram_getvar(device_t dev, const char *name, void *buf, size_t *len,
483      bhnd_nvram_type type)
484 {
485 	return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), dev, name, buf,
486 	    len, type));
487 }
488 
489 /**
490  * Allocate a resource from a device's parent bhnd(4) bus.
491  *
492  * @param dev The device requesting resource ownership.
493  * @param type The type of resource to allocate. This may be any type supported
494  * by the standard bus APIs.
495  * @param rid The bus-specific handle identifying the resource being allocated.
496  * @param start The start address of the resource.
497  * @param end The end address of the resource.
498  * @param count The size of the resource.
499  * @param flags The flags for the resource to be allocated. These may be any
500  * values supported by the standard bus APIs.
501  *
502  * To request the resource's default addresses, pass @p start and
503  * @p end values of @c 0 and @c ~0, respectively, and
504  * a @p count of @c 1.
505  *
506  * @retval NULL The resource could not be allocated.
507  * @retval resource The allocated resource.
508  */
509 static inline struct bhnd_resource *
510 bhnd_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
511     rman_res_t end, rman_res_t count, u_int flags)
512 {
513 	return BHND_BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, type, rid,
514 	    start, end, count, flags);
515 }
516 
517 
518 /**
519  * Allocate a resource from a device's parent bhnd(4) bus, using the
520  * resource's default start, end, and count values.
521  *
522  * @param dev The device requesting resource ownership.
523  * @param type The type of resource to allocate. This may be any type supported
524  * by the standard bus APIs.
525  * @param rid The bus-specific handle identifying the resource being allocated.
526  * @param flags The flags for the resource to be allocated. These may be any
527  * values supported by the standard bus APIs.
528  *
529  * @retval NULL The resource could not be allocated.
530  * @retval resource The allocated resource.
531  */
532 static inline struct bhnd_resource *
533 bhnd_alloc_resource_any(device_t dev, int type, int *rid, u_int flags)
534 {
535 	return bhnd_alloc_resource(dev, type, rid, 0, ~0, 1, flags);
536 }
537 
538 /**
539  * Activate a previously allocated bhnd resource.
540  *
541  * @param dev The device holding ownership of the allocated resource.
542  * @param type The type of the resource.
543  * @param rid The bus-specific handle identifying the resource.
544  * @param r A pointer to the resource returned by bhnd_alloc_resource or
545  * BHND_BUS_ALLOC_RESOURCE.
546  *
547  * @retval 0 success
548  * @retval non-zero an error occurred while activating the resource.
549  */
550 static inline int
551 bhnd_activate_resource(device_t dev, int type, int rid,
552    struct bhnd_resource *r)
553 {
554 	return BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
555 	    rid, r);
556 }
557 
558 /**
559  * Deactivate a previously activated bhnd resource.
560  *
561  * @param dev The device holding ownership of the activated resource.
562  * @param type The type of the resource.
563  * @param rid The bus-specific handle identifying the resource.
564  * @param r A pointer to the resource returned by bhnd_alloc_resource or
565  * BHND_BUS_ALLOC_RESOURCE.
566  *
567  * @retval 0 success
568  * @retval non-zero an error occurred while activating the resource.
569  */
570 static inline int
571 bhnd_deactivate_resource(device_t dev, int type, int rid,
572    struct bhnd_resource *r)
573 {
574 	return BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), dev, type,
575 	    rid, r);
576 }
577 
578 /**
579  * Free a resource allocated by bhnd_alloc_resource().
580  *
581  * @param dev The device holding ownership of the resource.
582  * @param type The type of the resource.
583  * @param rid The bus-specific handle identifying the resource.
584  * @param r A pointer to the resource returned by bhnd_alloc_resource or
585  * BHND_ALLOC_RESOURCE.
586  *
587  * @retval 0 success
588  * @retval non-zero an error occurred while activating the resource.
589  */
590 static inline int
591 bhnd_release_resource(device_t dev, int type, int rid,
592    struct bhnd_resource *r)
593 {
594 	return BHND_BUS_RELEASE_RESOURCE(device_get_parent(dev), dev, type,
595 	    rid, r);
596 }
597 
598 /**
599  * Return true if @p region_num is a valid region on @p port_num of
600  * @p type attached to @p dev.
601  *
602  * @param dev A bhnd bus child device.
603  * @param type The port type being queried.
604  * @param port_num The port number being queried.
605  * @param region_num The region number being queried.
606  */
607 static inline bool
608 bhnd_is_region_valid(device_t dev, bhnd_port_type type, u_int port_num,
609     u_int region_num)
610 {
611 	return (BHND_BUS_IS_REGION_VALID(device_get_parent(dev), dev, type,
612 	    port_num, region_num));
613 }
614 
615 /**
616  * Return the number of ports of type @p type attached to @p def.
617  *
618  * @param dev A bhnd bus child device.
619  * @param type The port type being queried.
620  */
621 static inline u_int
622 bhnd_get_port_count(device_t dev, bhnd_port_type type) {
623 	return (BHND_BUS_GET_PORT_COUNT(device_get_parent(dev), dev, type));
624 }
625 
626 /**
627  * Return the number of memory regions mapped to @p child @p port of
628  * type @p type.
629  *
630  * @param dev A bhnd bus child device.
631  * @param port The port number being queried.
632  * @param type The port type being queried.
633  */
634 static inline u_int
635 bhnd_get_region_count(device_t dev, bhnd_port_type type, u_int port) {
636 	return (BHND_BUS_GET_REGION_COUNT(device_get_parent(dev), dev, type,
637 	    port));
638 }
639 
640 /**
641  * Return the resource-ID for a memory region on the given device port.
642  *
643  * @param dev A bhnd bus child device.
644  * @param type The port type.
645  * @param port The port identifier.
646  * @param region The identifier of the memory region on @p port.
647  *
648  * @retval int The RID for the given @p port and @p region on @p device.
649  * @retval -1 No such port/region found.
650  */
651 static inline int
652 bhnd_get_port_rid(device_t dev, bhnd_port_type type, u_int port, u_int region)
653 {
654 	return BHND_BUS_GET_PORT_RID(device_get_parent(dev), dev, type, port,
655 	    region);
656 }
657 
658 /**
659  * Decode a port / region pair on @p dev defined by @p rid.
660  *
661  * @param dev A bhnd bus child device.
662  * @param type The resource type.
663  * @param rid The resource identifier.
664  * @param[out] port_type The decoded port type.
665  * @param[out] port The decoded port identifier.
666  * @param[out] region The decoded region identifier.
667  *
668  * @retval 0 success
669  * @retval non-zero No matching port/region found.
670  */
671 static inline int
672 bhnd_decode_port_rid(device_t dev, int type, int rid, bhnd_port_type *port_type,
673     u_int *port, u_int *region)
674 {
675 	return BHND_BUS_DECODE_PORT_RID(device_get_parent(dev), dev, type, rid,
676 	    port_type, port, region);
677 }
678 
679 /**
680  * Get the address and size of @p region on @p port.
681  *
682  * @param dev A bhnd bus child device.
683  * @param port_type The port type.
684  * @param port The port identifier.
685  * @param region The identifier of the memory region on @p port.
686  * @param[out] region_addr The region's base address.
687  * @param[out] region_size The region's size.
688  *
689  * @retval 0 success
690  * @retval non-zero No matching port/region found.
691  */
692 static inline int
693 bhnd_get_region_addr(device_t dev, bhnd_port_type port_type, u_int port,
694     u_int region, bhnd_addr_t *region_addr, bhnd_size_t *region_size)
695 {
696 	return BHND_BUS_GET_REGION_ADDR(device_get_parent(dev), dev, port_type,
697 	    port, region, region_addr, region_size);
698 }
699 
700 /*
701  * bhnd bus-level equivalents of the bus_(read|write|set|barrier|...)
702  * macros (compatible with bhnd_resource).
703  *
704  * Generated with bhnd/tools/bus_macro.sh
705  */
706 #define bhnd_bus_barrier(r, o, l, f) \
707     ((r)->direct) ? \
708 	bus_barrier((r)->res, (o), (l), (f)) : \
709 	BHND_BUS_BARRIER( \
710 	    device_get_parent(rman_get_device((r)->res)),	\
711 	    rman_get_device((r)->res), (r), (o), (l), (f))
712 #define bhnd_bus_read_1(r, o) \
713     ((r)->direct) ? \
714 	bus_read_1((r)->res, (o)) : \
715 	BHND_BUS_READ_1( \
716 	    device_get_parent(rman_get_device((r)->res)),	\
717 	    rman_get_device((r)->res), (r), (o))
718 #define bhnd_bus_read_multi_1(r, o, d, c) \
719     ((r)->direct) ? \
720 	bus_read_multi_1((r)->res, (o), (d), (c)) : \
721 	BHND_BUS_READ_MULTI_1( \
722 	    device_get_parent(rman_get_device((r)->res)),	\
723 	    rman_get_device((r)->res), (r), (o), (d), (c))
724 #define bhnd_bus_read_region_1(r, o, d, c) \
725     ((r)->direct) ? \
726 	bus_read_region_1((r)->res, (o), (d), (c)) : \
727 	BHND_BUS_READ_REGION_1( \
728 	    device_get_parent(rman_get_device((r)->res)),	\
729 	    rman_get_device((r)->res), (r), (o), (d), (c))
730 #define bhnd_bus_write_1(r, o, v) \
731     ((r)->direct) ? \
732 	bus_write_1((r)->res, (o), (v)) : \
733 	BHND_BUS_WRITE_1( \
734 	    device_get_parent(rman_get_device((r)->res)),	\
735 	    rman_get_device((r)->res), (r), (o), (v))
736 #define bhnd_bus_write_multi_1(r, o, d, c) \
737     ((r)->direct) ? \
738 	bus_write_multi_1((r)->res, (o), (d), (c)) : \
739 	BHND_BUS_WRITE_MULTI_1( \
740 	    device_get_parent(rman_get_device((r)->res)),	\
741 	    rman_get_device((r)->res), (r), (o), (d), (c))
742 #define bhnd_bus_write_region_1(r, o, d, c) \
743     ((r)->direct) ? \
744 	bus_write_region_1((r)->res, (o), (d), (c)) : \
745 	BHND_BUS_WRITE_REGION_1( \
746 	    device_get_parent(rman_get_device((r)->res)),	\
747 	    rman_get_device((r)->res), (r), (o), (d), (c))
748 #define bhnd_bus_read_stream_1(r, o) \
749     ((r)->direct) ? \
750 	bus_read_stream_1((r)->res, (o)) : \
751 	BHND_BUS_READ_STREAM_1( \
752 	    device_get_parent(rman_get_device((r)->res)),	\
753 	    rman_get_device((r)->res), (r), (o))
754 #define bhnd_bus_read_multi_stream_1(r, o, d, c) \
755     ((r)->direct) ? \
756 	bus_read_multi_stream_1((r)->res, (o), (d), (c)) : \
757 	BHND_BUS_READ_MULTI_STREAM_1( \
758 	    device_get_parent(rman_get_device((r)->res)),	\
759 	    rman_get_device((r)->res), (r), (o), (d), (c))
760 #define bhnd_bus_read_region_stream_1(r, o, d, c) \
761     ((r)->direct) ? \
762 	bus_read_region_stream_1((r)->res, (o), (d), (c)) : \
763 	BHND_BUS_READ_REGION_STREAM_1( \
764 	    device_get_parent(rman_get_device((r)->res)),	\
765 	    rman_get_device((r)->res), (r), (o), (d), (c))
766 #define bhnd_bus_write_stream_1(r, o, v) \
767     ((r)->direct) ? \
768 	bus_write_stream_1((r)->res, (o), (v)) : \
769 	BHND_BUS_WRITE_STREAM_1( \
770 	    device_get_parent(rman_get_device((r)->res)),	\
771 	    rman_get_device((r)->res), (r), (o), (v))
772 #define bhnd_bus_write_multi_stream_1(r, o, d, c) \
773     ((r)->direct) ? \
774 	bus_write_multi_stream_1((r)->res, (o), (d), (c)) : \
775 	BHND_BUS_WRITE_MULTI_STREAM_1( \
776 	    device_get_parent(rman_get_device((r)->res)),	\
777 	    rman_get_device((r)->res), (r), (o), (d), (c))
778 #define bhnd_bus_write_region_stream_1(r, o, d, c) \
779     ((r)->direct) ? \
780 	bus_write_region_stream_1((r)->res, (o), (d), (c)) : \
781 	BHND_BUS_WRITE_REGION_STREAM_1( \
782 	    device_get_parent(rman_get_device((r)->res)),	\
783 	    rman_get_device((r)->res), (r), (o), (d), (c))
784 #define bhnd_bus_set_multi_1(r, o, v, c) \
785     ((r)->direct) ? \
786 	bus_set_multi_1((r)->res, (o), (v), (c)) : \
787 	BHND_BUS_SET_MULTI_1( \
788 	    device_get_parent(rman_get_device((r)->res)),	\
789 	    rman_get_device((r)->res), (r), (o), (v), (c))
790 #define bhnd_bus_set_region_1(r, o, v, c) \
791     ((r)->direct) ? \
792 	bus_set_region_1((r)->res, (o), (v), (c)) : \
793 	BHND_BUS_SET_REGION_1( \
794 	    device_get_parent(rman_get_device((r)->res)),	\
795 	    rman_get_device((r)->res), (r), (o), (v), (c))
796 #define bhnd_bus_read_2(r, o) \
797     ((r)->direct) ? \
798 	bus_read_2((r)->res, (o)) : \
799 	BHND_BUS_READ_2( \
800 	    device_get_parent(rman_get_device((r)->res)),	\
801 	    rman_get_device((r)->res), (r), (o))
802 #define bhnd_bus_read_multi_2(r, o, d, c) \
803     ((r)->direct) ? \
804 	bus_read_multi_2((r)->res, (o), (d), (c)) : \
805 	BHND_BUS_READ_MULTI_2( \
806 	    device_get_parent(rman_get_device((r)->res)),	\
807 	    rman_get_device((r)->res), (r), (o), (d), (c))
808 #define bhnd_bus_read_region_2(r, o, d, c) \
809     ((r)->direct) ? \
810 	bus_read_region_2((r)->res, (o), (d), (c)) : \
811 	BHND_BUS_READ_REGION_2( \
812 	    device_get_parent(rman_get_device((r)->res)),	\
813 	    rman_get_device((r)->res), (r), (o), (d), (c))
814 #define bhnd_bus_write_2(r, o, v) \
815     ((r)->direct) ? \
816 	bus_write_2((r)->res, (o), (v)) : \
817 	BHND_BUS_WRITE_2( \
818 	    device_get_parent(rman_get_device((r)->res)),	\
819 	    rman_get_device((r)->res), (r), (o), (v))
820 #define bhnd_bus_write_multi_2(r, o, d, c) \
821     ((r)->direct) ? \
822 	bus_write_multi_2((r)->res, (o), (d), (c)) : \
823 	BHND_BUS_WRITE_MULTI_2( \
824 	    device_get_parent(rman_get_device((r)->res)),	\
825 	    rman_get_device((r)->res), (r), (o), (d), (c))
826 #define bhnd_bus_write_region_2(r, o, d, c) \
827     ((r)->direct) ? \
828 	bus_write_region_2((r)->res, (o), (d), (c)) : \
829 	BHND_BUS_WRITE_REGION_2( \
830 	    device_get_parent(rman_get_device((r)->res)),	\
831 	    rman_get_device((r)->res), (r), (o), (d), (c))
832 #define bhnd_bus_read_stream_2(r, o) \
833     ((r)->direct) ? \
834 	bus_read_stream_2((r)->res, (o)) : \
835 	BHND_BUS_READ_STREAM_2( \
836 	    device_get_parent(rman_get_device((r)->res)),	\
837 	    rman_get_device((r)->res), (r), (o))
838 #define bhnd_bus_read_multi_stream_2(r, o, d, c) \
839     ((r)->direct) ? \
840 	bus_read_multi_stream_2((r)->res, (o), (d), (c)) : \
841 	BHND_BUS_READ_MULTI_STREAM_2( \
842 	    device_get_parent(rman_get_device((r)->res)),	\
843 	    rman_get_device((r)->res), (r), (o), (d), (c))
844 #define bhnd_bus_read_region_stream_2(r, o, d, c) \
845     ((r)->direct) ? \
846 	bus_read_region_stream_2((r)->res, (o), (d), (c)) : \
847 	BHND_BUS_READ_REGION_STREAM_2( \
848 	    device_get_parent(rman_get_device((r)->res)),	\
849 	    rman_get_device((r)->res), (r), (o), (d), (c))
850 #define bhnd_bus_write_stream_2(r, o, v) \
851     ((r)->direct) ? \
852 	bus_write_stream_2((r)->res, (o), (v)) : \
853 	BHND_BUS_WRITE_STREAM_2( \
854 	    device_get_parent(rman_get_device((r)->res)),	\
855 	    rman_get_device((r)->res), (r), (o), (v))
856 #define bhnd_bus_write_multi_stream_2(r, o, d, c) \
857     ((r)->direct) ? \
858 	bus_write_multi_stream_2((r)->res, (o), (d), (c)) : \
859 	BHND_BUS_WRITE_MULTI_STREAM_2( \
860 	    device_get_parent(rman_get_device((r)->res)),	\
861 	    rman_get_device((r)->res), (r), (o), (d), (c))
862 #define bhnd_bus_write_region_stream_2(r, o, d, c) \
863     ((r)->direct) ? \
864 	bus_write_region_stream_2((r)->res, (o), (d), (c)) : \
865 	BHND_BUS_WRITE_REGION_STREAM_2( \
866 	    device_get_parent(rman_get_device((r)->res)),	\
867 	    rman_get_device((r)->res), (r), (o), (d), (c))
868 #define bhnd_bus_set_multi_2(r, o, v, c) \
869     ((r)->direct) ? \
870 	bus_set_multi_2((r)->res, (o), (v), (c)) : \
871 	BHND_BUS_SET_MULTI_2( \
872 	    device_get_parent(rman_get_device((r)->res)),	\
873 	    rman_get_device((r)->res), (r), (o), (v), (c))
874 #define bhnd_bus_set_region_2(r, o, v, c) \
875     ((r)->direct) ? \
876 	bus_set_region_2((r)->res, (o), (v), (c)) : \
877 	BHND_BUS_SET_REGION_2( \
878 	    device_get_parent(rman_get_device((r)->res)),	\
879 	    rman_get_device((r)->res), (r), (o), (v), (c))
880 #define bhnd_bus_read_4(r, o) \
881     ((r)->direct) ? \
882 	bus_read_4((r)->res, (o)) : \
883 	BHND_BUS_READ_4( \
884 	    device_get_parent(rman_get_device((r)->res)),	\
885 	    rman_get_device((r)->res), (r), (o))
886 #define bhnd_bus_read_multi_4(r, o, d, c) \
887     ((r)->direct) ? \
888 	bus_read_multi_4((r)->res, (o), (d), (c)) : \
889 	BHND_BUS_READ_MULTI_4( \
890 	    device_get_parent(rman_get_device((r)->res)),	\
891 	    rman_get_device((r)->res), (r), (o), (d), (c))
892 #define bhnd_bus_read_region_4(r, o, d, c) \
893     ((r)->direct) ? \
894 	bus_read_region_4((r)->res, (o), (d), (c)) : \
895 	BHND_BUS_READ_REGION_4( \
896 	    device_get_parent(rman_get_device((r)->res)),	\
897 	    rman_get_device((r)->res), (r), (o), (d), (c))
898 #define bhnd_bus_write_4(r, o, v) \
899     ((r)->direct) ? \
900 	bus_write_4((r)->res, (o), (v)) : \
901 	BHND_BUS_WRITE_4( \
902 	    device_get_parent(rman_get_device((r)->res)),	\
903 	    rman_get_device((r)->res), (r), (o), (v))
904 #define bhnd_bus_write_multi_4(r, o, d, c) \
905     ((r)->direct) ? \
906 	bus_write_multi_4((r)->res, (o), (d), (c)) : \
907 	BHND_BUS_WRITE_MULTI_4( \
908 	    device_get_parent(rman_get_device((r)->res)),	\
909 	    rman_get_device((r)->res), (r), (o), (d), (c))
910 #define bhnd_bus_write_region_4(r, o, d, c) \
911     ((r)->direct) ? \
912 	bus_write_region_4((r)->res, (o), (d), (c)) : \
913 	BHND_BUS_WRITE_REGION_4( \
914 	    device_get_parent(rman_get_device((r)->res)),	\
915 	    rman_get_device((r)->res), (r), (o), (d), (c))
916 #define bhnd_bus_read_stream_4(r, o) \
917     ((r)->direct) ? \
918 	bus_read_stream_4((r)->res, (o)) : \
919 	BHND_BUS_READ_STREAM_4( \
920 	    device_get_parent(rman_get_device((r)->res)),	\
921 	    rman_get_device((r)->res), (r), (o))
922 #define bhnd_bus_read_multi_stream_4(r, o, d, c) \
923     ((r)->direct) ? \
924 	bus_read_multi_stream_4((r)->res, (o), (d), (c)) : \
925 	BHND_BUS_READ_MULTI_STREAM_4( \
926 	    device_get_parent(rman_get_device((r)->res)),	\
927 	    rman_get_device((r)->res), (r), (o), (d), (c))
928 #define bhnd_bus_read_region_stream_4(r, o, d, c) \
929     ((r)->direct) ? \
930 	bus_read_region_stream_4((r)->res, (o), (d), (c)) : \
931 	BHND_BUS_READ_REGION_STREAM_4( \
932 	    device_get_parent(rman_get_device((r)->res)),	\
933 	    rman_get_device((r)->res), (r), (o), (d), (c))
934 #define bhnd_bus_write_stream_4(r, o, v) \
935     ((r)->direct) ? \
936 	bus_write_stream_4((r)->res, (o), (v)) : \
937 	BHND_BUS_WRITE_STREAM_4( \
938 	    device_get_parent(rman_get_device((r)->res)),	\
939 	    rman_get_device((r)->res), (r), (o), (v))
940 #define bhnd_bus_write_multi_stream_4(r, o, d, c) \
941     ((r)->direct) ? \
942 	bus_write_multi_stream_4((r)->res, (o), (d), (c)) : \
943 	BHND_BUS_WRITE_MULTI_STREAM_4( \
944 	    device_get_parent(rman_get_device((r)->res)),	\
945 	    rman_get_device((r)->res), (r), (o), (d), (c))
946 #define bhnd_bus_write_region_stream_4(r, o, d, c) \
947     ((r)->direct) ? \
948 	bus_write_region_stream_4((r)->res, (o), (d), (c)) : \
949 	BHND_BUS_WRITE_REGION_STREAM_4( \
950 	    device_get_parent(rman_get_device((r)->res)),	\
951 	    rman_get_device((r)->res), (r), (o), (d), (c))
952 #define bhnd_bus_set_multi_4(r, o, v, c) \
953     ((r)->direct) ? \
954 	bus_set_multi_4((r)->res, (o), (v), (c)) : \
955 	BHND_BUS_SET_MULTI_4( \
956 	    device_get_parent(rman_get_device((r)->res)),	\
957 	    rman_get_device((r)->res), (r), (o), (v), (c))
958 #define bhnd_bus_set_region_4(r, o, v, c) \
959     ((r)->direct) ? \
960 	bus_set_region_4((r)->res, (o), (v), (c)) : \
961 	BHND_BUS_SET_REGION_4( \
962 	    device_get_parent(rman_get_device((r)->res)),	\
963 	    rman_get_device((r)->res), (r), (o), (v), (c))
964 
965 #endif /* _BHND_BHND_H_ */
966