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