1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2006-2013 Solarflare Communications Inc.
5  * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
6  */
7 
8 #ifndef CDX_BITFIELD_H
9 #define CDX_BITFIELD_H
10 
11 #include <linux/bitfield.h>
12 
13 /* Lowest bit numbers and widths */
14 #define CDX_DWORD_LBN 0
15 #define CDX_DWORD_WIDTH 32
16 
17 /* Specified attribute (e.g. LBN) of the specified field */
18 #define CDX_VAL(field, attribute) field ## _ ## attribute
19 /* Low bit number of the specified field */
20 #define CDX_LOW_BIT(field) CDX_VAL(field, LBN)
21 /* Bit width of the specified field */
22 #define CDX_WIDTH(field) CDX_VAL(field, WIDTH)
23 /* High bit number of the specified field */
24 #define CDX_HIGH_BIT(field) (CDX_LOW_BIT(field) + CDX_WIDTH(field) - 1)
25 
26 /* A doubleword (i.e. 4 byte) datatype - little-endian in HW */
27 struct cdx_dword {
28 	__le32 cdx_u32;
29 };
30 
31 /* Value expanders for printk */
32 #define CDX_DWORD_VAL(dword)				\
33 	((unsigned int)le32_to_cpu((dword).cdx_u32))
34 
35 /*
36  * Extract bit field portion [low,high) from the 32-bit little-endian
37  * element which contains bits [min,max)
38  */
39 #define CDX_DWORD_FIELD(dword, field)					\
40 	(FIELD_GET(GENMASK(CDX_HIGH_BIT(field), CDX_LOW_BIT(field)),	\
41 		   le32_to_cpu((dword).cdx_u32)))
42 
43 /*
44  * Creates the portion of the named bit field that lies within the
45  * range [min,max).
46  */
47 #define CDX_INSERT_FIELD(field, value)				\
48 	(FIELD_PREP(GENMASK(CDX_HIGH_BIT(field),		\
49 			    CDX_LOW_BIT(field)), value))
50 
51 /*
52  * Creates the portion of the named bit fields that lie within the
53  * range [min,max).
54  */
55 #define CDX_INSERT_FIELDS(field1, value1,		\
56 			  field2, value2,		\
57 			  field3, value3,		\
58 			  field4, value4,		\
59 			  field5, value5,		\
60 			  field6, value6,		\
61 			  field7, value7)		\
62 	(CDX_INSERT_FIELD(field1, (value1)) |		\
63 	 CDX_INSERT_FIELD(field2, (value2)) |		\
64 	 CDX_INSERT_FIELD(field3, (value3)) |		\
65 	 CDX_INSERT_FIELD(field4, (value4)) |		\
66 	 CDX_INSERT_FIELD(field5, (value5)) |		\
67 	 CDX_INSERT_FIELD(field6, (value6)) |		\
68 	 CDX_INSERT_FIELD(field7, (value7)))
69 
70 #define CDX_POPULATE_DWORD(dword, ...)					\
71 	(dword).cdx_u32 = cpu_to_le32(CDX_INSERT_FIELDS(__VA_ARGS__))
72 
73 /* Populate a dword field with various numbers of arguments */
74 #define CDX_POPULATE_DWORD_7 CDX_POPULATE_DWORD
75 #define CDX_POPULATE_DWORD_6(dword, ...) \
76 	CDX_POPULATE_DWORD_7(dword, CDX_DWORD, 0, __VA_ARGS__)
77 #define CDX_POPULATE_DWORD_5(dword, ...) \
78 	CDX_POPULATE_DWORD_6(dword, CDX_DWORD, 0, __VA_ARGS__)
79 #define CDX_POPULATE_DWORD_4(dword, ...) \
80 	CDX_POPULATE_DWORD_5(dword, CDX_DWORD, 0, __VA_ARGS__)
81 #define CDX_POPULATE_DWORD_3(dword, ...) \
82 	CDX_POPULATE_DWORD_4(dword, CDX_DWORD, 0, __VA_ARGS__)
83 #define CDX_POPULATE_DWORD_2(dword, ...) \
84 	CDX_POPULATE_DWORD_3(dword, CDX_DWORD, 0, __VA_ARGS__)
85 #define CDX_POPULATE_DWORD_1(dword, ...) \
86 	CDX_POPULATE_DWORD_2(dword, CDX_DWORD, 0, __VA_ARGS__)
87 #define CDX_SET_DWORD(dword) \
88 	CDX_POPULATE_DWORD_1(dword, CDX_DWORD, 0xffffffff)
89 
90 #endif /* CDX_BITFIELD_H */
91