xref: /freebsd/sys/dev/bhnd/bhnd_match.h (revision 10ff414c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * $FreeBSD$
32  */
33 
34 #ifndef _BHND_BHND_MATCH_H_
35 #define _BHND_BHND_MATCH_H_
36 
37 #include "bhnd_types.h"
38 
39 /**
40  * A hardware revision match descriptor.
41  */
42 struct bhnd_hwrev_match {
43 	uint16_t	start;	/**< first revision, or BHND_HWREV_INVALID
44 					     to match on any revision. */
45 	uint16_t	end;	/**< last revision, or BHND_HWREV_INVALID
46 					     to match on any revision. */
47 };
48 
49 /* Copy match field @p _name from @p _src */
50 #define	_BHND_COPY_MATCH_FIELD(_src, _name)	\
51 	.m.match._name = (_src)->m.match._name,	\
52 	._name = (_src)->_name
53 
54 /* Set match field @p _name with @p _value */
55 #define	_BHND_SET_MATCH_FIELD(_name, _value)	\
56 	.m.match._name = 1, ._name = _value
57 
58 /**
59  * Wildcard hardware revision match descriptor.
60  */
61 #define	BHND_HWREV_ANY		{ BHND_HWREV_INVALID, BHND_HWREV_INVALID }
62 #define	BHND_HWREV_IS_ANY(_m)	\
63 	((_m)->start == BHND_HWREV_INVALID && (_m)->end == BHND_HWREV_INVALID)
64 
65 /**
66  * Hardware revision match descriptor for an inclusive range.
67  *
68  * @param _start The first applicable hardware revision.
69  * @param _end The last applicable hardware revision, or BHND_HWREV_INVALID
70  * to match on any revision.
71  */
72 #define	BHND_HWREV_RANGE(_start, _end)	{ _start, _end }
73 
74 /**
75  * Hardware revision match descriptor for a single revision.
76  *
77  * @param _hwrev The hardware revision to match on.
78  */
79 #define	BHND_HWREV_EQ(_hwrev)	BHND_HWREV_RANGE(_hwrev, _hwrev)
80 
81 /**
82  * Hardware revision match descriptor for any revision equal to or greater
83  * than @p _start.
84  *
85  * @param _start The first hardware revision to match on.
86  */
87 #define	BHND_HWREV_GTE(_start)	BHND_HWREV_RANGE(_start, BHND_HWREV_INVALID)
88 
89 /**
90  * Hardware revision match descriptor for any revision equal to or less
91  * than @p _end.
92  *
93  * @param _end The last hardware revision to match on.
94  */
95 #define	BHND_HWREV_LTE(_end)	BHND_HWREV_RANGE(0, _end)
96 
97 /**
98  * A bhnd(4) core match descriptor.
99  */
100 struct bhnd_core_match {
101 	/** Select fields to be matched */
102 	union {
103 		uint8_t match_flags;
104 		struct {
105 			uint8_t
106 			    core_vendor:1,
107 			    core_id:1,
108 			    core_rev:1,
109 			    core_class:1,
110 			    core_idx:1,
111 			    core_unit:1,
112 			    flags_unused:2;
113 		} match;
114 	} m;
115 
116 	uint16_t		core_vendor;	/**< required JEP106 device vendor */
117 	uint16_t		core_id;	/**< required core ID */
118 	struct bhnd_hwrev_match	core_rev;	/**< matching core revisions. */
119 	bhnd_devclass_t		core_class;	/**< required bhnd class */
120 	u_int			core_idx;	/**< required core index */
121 	int			core_unit;	/**< required core unit */
122 };
123 
124 #define	_BHND_CORE_MATCH_COPY(_src)			\
125 	_BHND_COPY_MATCH_FIELD(_src, core_vendor),	\
126 	_BHND_COPY_MATCH_FIELD(_src, core_id),		\
127 	_BHND_COPY_MATCH_FIELD(_src, core_rev),		\
128 	_BHND_COPY_MATCH_FIELD(_src, core_class),	\
129 	_BHND_COPY_MATCH_FIELD(_src, core_idx),		\
130 	_BHND_COPY_MATCH_FIELD(_src, core_unit)		\
131 
132 #define	BHND_MATCH_CORE_VENDOR(_v)	_BHND_SET_MATCH_FIELD(core_vendor, _v)
133 #define	BHND_MATCH_CORE_ID(_id)		_BHND_SET_MATCH_FIELD(core_id, _id)
134 #define	BHND_MATCH_CORE_REV(_rev)	_BHND_SET_MATCH_FIELD(core_rev,	\
135 					    BHND_ ## _rev)
136 #define	BHND_MATCH_CORE_CLASS(_cls)	_BHND_SET_MATCH_FIELD(core_class, _cls)
137 #define	BHND_MATCH_CORE_IDX(_idx)	_BHND_SET_MATCH_FIELD(core_idx, _idx)
138 #define	BHND_MATCH_CORE_UNIT(_unit)	_BHND_SET_MATCH_FIELD(core_unit, _unit)
139 
140 /**
141  * Match against the given @p _vendor and @p _id,
142  */
143 #define	BHND_MATCH_CORE(_vendor, _id)		\
144 	BHND_MATCH_CORE_VENDOR(_vendor),	\
145 	BHND_MATCH_CORE_ID(_id)
146 
147 /**
148  * A bhnd(4) chip match descriptor.
149  */
150 struct bhnd_chip_match {
151 	/** Select fields to be matched */
152 	union {
153 		uint8_t match_flags;
154 		struct {
155 			uint8_t
156 			    chip_id:1,
157 			    chip_rev:1,
158 			    chip_pkg:1,
159 			    chip_type:1,
160 			    flags_unused:4;
161 		} match;
162 
163 	} m;
164 
165 	uint16_t		chip_id;	/**< required chip id */
166 	struct bhnd_hwrev_match	chip_rev;	/**< matching chip revisions */
167 	uint8_t			chip_pkg;	/**< required package */
168 	uint8_t			chip_type;	/**< required chip type (BHND_CHIPTYPE_*) */
169 };
170 
171 #define	_BHND_CHIP_MATCH_COPY(_src)		\
172 	_BHND_COPY_MATCH_FIELD(_src, chip_id),	\
173 	_BHND_COPY_MATCH_FIELD(_src, chip_rev),	\
174 	_BHND_COPY_MATCH_FIELD(_src, chip_pkg),	\
175 	_BHND_COPY_MATCH_FIELD(_src, chip_type),\
176 
177 /** Set the required chip ID within a bhnd match descriptor */
178 #define	BHND_MATCH_CHIP_ID(_cid)	_BHND_SET_MATCH_FIELD(chip_id,	\
179 					    BHND_CHIPID_ ## _cid)
180 
181 /** Set the required chip revision range within a bhnd match descriptor */
182 #define	BHND_MATCH_CHIP_REV(_rev)	_BHND_SET_MATCH_FIELD(chip_rev,	\
183 					    BHND_ ## _rev)
184 
185 /** Set the required package ID within a bhnd match descriptor */
186 #define	BHND_MATCH_CHIP_PKG(_pkg)	_BHND_SET_MATCH_FIELD(chip_pkg,	\
187 					    BHND_PKGID_ ## _pkg)
188 
189 /** Set the required chip type within a bhnd match descriptor */
190 #define	BHND_MATCH_CHIP_TYPE(_type)	_BHND_SET_MATCH_FIELD(chip_type,	\
191 					    BHND_CHIPTYPE_ ## _type)
192 
193 /** Set the required chip and package ID within a bhnd match descriptor */
194 #define	BHND_MATCH_CHIP_IP(_cid, _pkg)	\
195     BHND_MATCH_CHIP_ID(_cid), BHND_MATCH_CHIP_PKG(_pkg)
196 
197 /** Set the required chip ID, package ID, and revision within a bhnd_device_match
198  *  instance */
199 #define	BHND_MATCH_CHIP_IPR(_cid, _pkg, _rev)	\
200     BHND_MATCH_CHIP_ID(_cid),			\
201     BHND_MATCH_CHIP_PKG(_pkg),			\
202     BHND_MATCH_CHIP_REV(_rev)
203 
204 /** Set the required chip ID and revision within a bhnd_device_match
205  *  instance */
206 #define	BHND_MATCH_CHIP_IR(_cid, _rev)	\
207     BHND_MATCH_CHIP_ID(_cid), BHND_MATCH_CHIP_REV(_rev)
208 
209 /**
210  * A bhnd(4) board match descriptor.
211  */
212 struct bhnd_board_match {
213 	/** Select fields to be matched */
214 	union {
215 		uint8_t match_flags;
216 		struct {
217 			uint8_t
218 			    board_vendor:1,
219 			    board_type:1,
220 			    board_devid:1,
221 			    board_rev:1,
222 			    board_srom_rev:1,
223 			    flags_unused:3;
224 		} match;
225 	} m;
226 
227 	uint16_t		board_vendor;	/**< required board vendor */
228 	uint16_t		board_type;	/**< required board type */
229 	uint16_t		board_devid;	/**< required board devid */
230 	struct bhnd_hwrev_match	board_rev;	/**< matching board revisions */
231 	struct bhnd_hwrev_match	board_srom_rev;	/**< matching board srom revisions */
232 };
233 
234 #define	_BHND_BOARD_MATCH_COPY(_src)			\
235 	_BHND_COPY_MATCH_FIELD(_src, board_vendor),	\
236 	_BHND_COPY_MATCH_FIELD(_src, board_type),	\
237 	_BHND_COPY_MATCH_FIELD(_src, board_devid),	\
238 	_BHND_COPY_MATCH_FIELD(_src, board_rev),	\
239 	_BHND_COPY_MATCH_FIELD(_src, board_srom_rev)
240 
241 /** Set the required board vendor within a bhnd match descriptor */
242 #define	BHND_MATCH_BOARD_VENDOR(_v)	_BHND_SET_MATCH_FIELD(board_vendor, _v)
243 
244 /** Set the required board type within a bhnd match descriptor */
245 #define	BHND_MATCH_BOARD_TYPE(_type)	_BHND_SET_MATCH_FIELD(board_type, \
246 					    BHND_BOARD_ ## _type)
247 
248 /** Set the required board devid within a bhnd match descriptor */
249 #define	BHND_MATCH_BOARD_DEVID(_devid)	_BHND_SET_MATCH_FIELD(board_devid, \
250 					    (_devid))
251 
252 /** Set the required SROM revision range within a bhnd match descriptor */
253 #define	BHND_MATCH_SROMREV(_rev)	_BHND_SET_MATCH_FIELD(board_srom_rev, \
254 					    BHND_HWREV_ ## _rev)
255 
256 /** Set the required board revision range within a bhnd match descriptor */
257 #define	BHND_MATCH_BOARD_REV(_rev)	_BHND_SET_MATCH_FIELD(board_rev, \
258 					    BHND_ ## _rev)
259 
260 /** Set the required board vendor and type within a bhnd match descriptor */
261 #define	BHND_MATCH_BOARD(_vend, _type)	\
262 	BHND_MATCH_BOARD_VENDOR(_vend), BHND_MATCH_BOARD_TYPE(_type)
263 
264 /**
265  * A bhnd(4) device match descriptor.
266  *
267  * @warning Matching on board 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_device_match {
271 	/** Select fields to be matched */
272 	union {
273 		uint32_t match_flags;
274 		struct {
275 			uint32_t
276 			core_vendor:1,
277 			core_id:1,
278 			core_rev:1,
279 			core_class:1,
280 			core_idx:1,
281 			core_unit:1,
282 			chip_id:1,
283 			chip_rev:1,
284 			chip_pkg:1,
285 			chip_type:1,
286 			board_vendor:1,
287 			board_type:1,
288 			board_devid:1,
289 			board_rev:1,
290 			board_srom_rev:1,
291 			flags_unused:15;
292 		} match;
293 	} m;
294 
295 	uint16_t		core_vendor;	/**< required JEP106 device vendor */
296 	uint16_t		core_id;	/**< required core ID */
297 	struct bhnd_hwrev_match	core_rev;	/**< matching core revisions. */
298 	bhnd_devclass_t		core_class;	/**< required bhnd class */
299 	u_int			core_idx;	/**< required core index */
300 	int			core_unit;	/**< required core unit */
301 
302 	uint16_t		chip_id;	/**< required chip id */
303 	struct bhnd_hwrev_match	chip_rev;	/**< matching chip revisions */
304 	uint8_t			chip_pkg;	/**< required package */
305 	uint8_t			chip_type;	/**< required chip type (BHND_CHIPTYPE_*) */
306 
307 	uint16_t		board_vendor;	/**< required board vendor */
308 	uint16_t		board_type;	/**< required board type */
309 	uint16_t		board_devid;	/**< required board devid */
310 	struct bhnd_hwrev_match	board_rev;	/**< matching board revisions */
311 	struct bhnd_hwrev_match	board_srom_rev;	/**< matching board srom revisions */
312 };
313 
314 /** Define a wildcard match requirement (matches on any device). */
315 #define	BHND_MATCH_ANY		.m.match_flags = 0
316 #define	BHND_MATCH_IS_ANY(_m)	\
317 	((_m)->m.match_flags == 0)
318 
319 #endif /* _BHND_BHND_MATCH_H_ */
320